Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions minzc/pkg/frill/assert_string_args_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package frill

import (
"testing"

"github.com/minz/minzc/pkg/pipeline"
)

// The Z80 assert bootstrap used to load string-literal arguments as the raw
// placeholder value 0 instead of the interned string label, so the asserted
// function received a pointer to address 0. The mir2 runner resolves the same
// symbols via vm.ResolveSymbol, which is why only the z80 verdicts were wrong.
// Regression for the frill/expr_eval.frl and frill/stress_test.frl failures
// (`eval "5" == 5` got 0, `char_at 0 0 == 65` got 0).
const stringArgAssertSrc = `
let first_byte (s : u16) : u8 = peek s

assert first_byte "A" == 65
`

func TestZ80AssertResolvesStringArgs(t *testing.T) {
hm, err := Compile(stringArgAssertSrc, "string_arg.frl")
if err != nil {
t.Fatalf("compile: %v", err)
}
opts := pipeline.DefaultOptions()
opts.AssertMode = "none"
steps, err := pipeline.CompileHIRSteps(hm, opts)
if err != nil {
t.Fatalf("compile steps: %v", err)
}
if err := pipeline.RunAssertsMIR2(hm, steps.MIR2Module); err != nil {
t.Errorf("mir2: %v", err)
}
if err := pipeline.RunAssertsZ80(hm, steps.MIR2Module, steps.Allocation, steps.Assembly); err != nil {
t.Errorf("z80: %v", err)
}
}
133 changes: 133 additions & 0 deletions minzc/pkg/frill/forward_ref_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package frill

import (
"testing"

"github.com/minz/minzc/pkg/hir"
"github.com/minz/minzc/pkg/mir2"
"github.com/minz/minzc/pkg/pipeline"
)

// A call to a function parsed later in the module used to default its result
// type to u8. The mir2 VM returns the full register regardless of the
// contract, so the wrong type only produced wrong code on the Z80 backend,
// which reads exactly the contract register (A for u8). Regression for the
// frill/expr_eval.frl z80 assert failure (eval_atom → forward call to
// eval_expr typed u8).
const forwardRefSrc = `
let use_helper (x : u16) : u16 = helper x
let helper (n : u16) : u16 = n + 256

(* the callee's return type is inferred from a body with a forward call *)
let use_g (x : u16) : u16 = g x
let g (n : u16) = n + 256

assert use_helper 0 == 256
assert use_g 0 == 256
`

func TestForwardCallAdoptsDeclaredReturnType(t *testing.T) {
hm, err := Compile(forwardRefSrc, "forward_ref.frl")
if err != nil {
t.Fatalf("compile: %v", err)
}
for _, pair := range [][2]string{{"use_helper", "helper"}, {"use_g", "g"}} {
fname, callee := pair[0], pair[1]
fn := funcByName(hm, fname)
if fn == nil {
t.Fatalf("function %s not found", fname)
}
found := false
walkBodyExprs(fn.Body, func(e hir.Expr) {
if call, ok := e.(*hir.CallExpr); ok && call.Fn == callee {
found = true
if call.Ty != mir2.TyU16 {
t.Errorf("%s: call to %s has type %v, want u16", fname, callee, call.Ty)
}
}
})
if !found {
t.Fatalf("%s: no call to %s found", fname, callee)
}
}
}

func TestForwardCallZ80Asserts(t *testing.T) {
hm, err := Compile(forwardRefSrc, "forward_ref.frl")
if err != nil {
t.Fatalf("compile: %v", err)
}
opts := pipeline.DefaultOptions()
opts.AssertMode = "none"
steps, err := pipeline.CompileHIRSteps(hm, opts)
if err != nil {
t.Fatalf("compile steps: %v", err)
}
if err := pipeline.RunAssertsMIR2(hm, steps.MIR2Module); err != nil {
t.Errorf("mir2: %v", err)
}
if err := pipeline.RunAssertsZ80(hm, steps.MIR2Module, steps.Allocation, steps.Assembly); err != nil {
t.Errorf("z80: %v", err)
}
}

func funcByName(hm *hir.Module, name string) *hir.Func {
for _, f := range hm.Funcs {
if f.Name == name {
return f
}
}
return nil
}

func walkBodyExprs(b *hir.Block, fn func(hir.Expr)) {
if b == nil {
return
}
var walkStmt func(s hir.Stmt)
var walkExpr func(e hir.Expr)
walkExpr = func(e hir.Expr) {
if e == nil {
return
}
fn(e)
switch t := e.(type) {
case *hir.BinExpr:
walkExpr(t.L)
walkExpr(t.R)
case *hir.CallExpr:
for _, a := range t.Args {
walkExpr(a)
}
case *hir.CastExpr:
walkExpr(t.X)
case *hir.CondExpr:
walkExpr(t.Cond)
walkExpr(t.Then)
walkExpr(t.Else)
case *hir.LetInExpr:
walkExpr(t.Init)
walkExpr(t.Body)
case *hir.UnaryExpr:
walkExpr(t.X)
}
}
walkStmt = func(s hir.Stmt) {
switch t := s.(type) {
case *hir.ReturnStmt:
if t.Val != nil {
walkExpr(t.Val)
}
for _, v := range t.Vals {
walkExpr(v)
}
case *hir.VarDeclStmt:
walkExpr(t.Init)
case *hir.ExprStmt:
walkExpr(t.Expr)
}
}
for _, s := range b.Body {
walkStmt(s)
}
}
Loading
Loading