Updated while_test.go to new code generation. Added new versions of IF, ELSE, ENDIF based on comparison.go
This commit is contained in:
parent
df7e5349ac
commit
3d6f786949
6 changed files with 645 additions and 1370 deletions
|
|
@ -9,11 +9,12 @@ import (
|
|||
"c65gm/internal/utils"
|
||||
)
|
||||
|
||||
// ElseCommand handles ELSE statements in IF...ELSE...ENDIF blocks
|
||||
// ElseCommand handles ELSE statements
|
||||
// Syntax: ELSE
|
||||
// Marks alternative branch in IF...ELSE...ENDIF
|
||||
type ElseCommand struct {
|
||||
skipLabel string
|
||||
endLabel string
|
||||
skipLabel string
|
||||
endifLabel string
|
||||
}
|
||||
|
||||
func (c *ElseCommand) WillHandle(line preproc.Line) bool {
|
||||
|
|
@ -21,7 +22,6 @@ func (c *ElseCommand) WillHandle(line preproc.Line) bool {
|
|||
if err != nil || len(params) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.ToUpper(params[0]) == "ELSE"
|
||||
}
|
||||
|
||||
|
|
@ -32,30 +32,25 @@ func (c *ElseCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext
|
|||
}
|
||||
|
||||
if len(params) != 1 {
|
||||
return fmt.Errorf("ELSE: wrong number of parameters (%d), expected 1", len(params))
|
||||
return fmt.Errorf("ELSE: expected 1 parameter, got %d", len(params))
|
||||
}
|
||||
|
||||
// Pop the IF skip label
|
||||
label, err := ctx.IfStack.Pop()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ELSE: %w", err)
|
||||
// Pop skip label (where IF jumps on FALSE)
|
||||
var err2 error
|
||||
c.skipLabel, err2 = ctx.IfStack.Pop()
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("ELSE: not inside IF block")
|
||||
}
|
||||
c.skipLabel = label
|
||||
|
||||
// Push new end label
|
||||
c.endLabel = ctx.IfStack.Push()
|
||||
// Push new endif label (where to jump after IF block)
|
||||
c.endifLabel = ctx.IfStack.Push()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ElseCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
|
||||
// Jump to end (skip else block if condition was true)
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.endLabel))
|
||||
|
||||
// Place skip label (jumped here if condition was false)
|
||||
asm = append(asm, c.skipLabel)
|
||||
|
||||
return asm, nil
|
||||
return []string{
|
||||
fmt.Sprintf("\tjmp %s", c.endifLabel),
|
||||
c.skipLabel,
|
||||
}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,264 +0,0 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"c65gm/internal/compiler"
|
||||
"c65gm/internal/preproc"
|
||||
)
|
||||
|
||||
func TestElseCommand_WillHandle(t *testing.T) {
|
||||
cmd := &ElseCommand{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
want bool
|
||||
}{
|
||||
{"ELSE", "ELSE", true},
|
||||
{"not ELSE", "IF a = b", false},
|
||||
{"empty", "", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
|
||||
got := cmd.WillHandle(line)
|
||||
if got != tt.want {
|
||||
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndIfCommand_WillHandle(t *testing.T) {
|
||||
cmd := &EndIfCommand{}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
want bool
|
||||
}{
|
||||
{"ENDIF", "ENDIF", true},
|
||||
{"not ENDIF", "IF a = b", false},
|
||||
{"empty", "", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
|
||||
got := cmd.WillHandle(line)
|
||||
if got != tt.want {
|
||||
t.Errorf("WillHandle() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIfElseEndif_Integration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
lines []string
|
||||
setupVars func(*compiler.SymbolTable)
|
||||
wantAsm []string
|
||||
}{
|
||||
{
|
||||
name: "IF...ENDIF (no ELSE)",
|
||||
lines: []string{
|
||||
"IF a = b",
|
||||
"ENDIF",
|
||||
},
|
||||
setupVars: func(st *compiler.SymbolTable) {
|
||||
st.AddVar("a", "", compiler.KindByte, 0)
|
||||
st.AddVar("b", "", compiler.KindByte, 0)
|
||||
},
|
||||
wantAsm: []string{
|
||||
"; IF a = b",
|
||||
"\tlda a",
|
||||
"\tcmp b",
|
||||
"\tbne _I1",
|
||||
"; ENDIF",
|
||||
"_I1",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "IF...ELSE...ENDIF",
|
||||
lines: []string{
|
||||
"IF a = b",
|
||||
"ELSE",
|
||||
"ENDIF",
|
||||
},
|
||||
setupVars: func(st *compiler.SymbolTable) {
|
||||
st.AddVar("a", "", compiler.KindByte, 0)
|
||||
st.AddVar("b", "", compiler.KindByte, 0)
|
||||
},
|
||||
wantAsm: []string{
|
||||
"; IF a = b",
|
||||
"\tlda a",
|
||||
"\tcmp b",
|
||||
"\tbne _I1",
|
||||
"; ELSE",
|
||||
"\tjmp _I2",
|
||||
"_I1",
|
||||
"; ENDIF",
|
||||
"_I2",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "nested IF statements",
|
||||
lines: []string{
|
||||
"IF a = 10",
|
||||
"IF b = 20",
|
||||
"ENDIF",
|
||||
"ENDIF",
|
||||
},
|
||||
setupVars: func(st *compiler.SymbolTable) {
|
||||
st.AddVar("a", "", compiler.KindByte, 0)
|
||||
st.AddVar("b", "", compiler.KindByte, 0)
|
||||
},
|
||||
wantAsm: []string{
|
||||
"; IF a = 10",
|
||||
"\tlda a",
|
||||
"\tcmp #$0a",
|
||||
"\tbne _I1",
|
||||
"; IF b = 20",
|
||||
"\tlda b",
|
||||
"\tcmp #$14",
|
||||
"\tbne _I2",
|
||||
"; ENDIF",
|
||||
"_I2",
|
||||
"; ENDIF",
|
||||
"_I1",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := compiler.NewCompilerContext(preproc.NewPragma())
|
||||
tt.setupVars(ctx.SymbolTable)
|
||||
|
||||
var allAsm []string
|
||||
|
||||
for _, lineText := range tt.lines {
|
||||
line := preproc.Line{Text: lineText, Kind: preproc.Source, PragmaSetIndex: 0}
|
||||
|
||||
// Determine which command to use
|
||||
var cmd compiler.Command
|
||||
if strings.HasPrefix(strings.ToUpper(lineText), "IF") {
|
||||
cmd = &IfCommand{}
|
||||
} else if strings.ToUpper(lineText) == "ELSE" {
|
||||
cmd = &ElseCommand{}
|
||||
} else if strings.ToUpper(lineText) == "ENDIF" {
|
||||
cmd = &EndIfCommand{}
|
||||
} else {
|
||||
t.Fatalf("unknown command: %s", lineText)
|
||||
}
|
||||
|
||||
err := cmd.Interpret(line, ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Interpret(%q) error = %v", lineText, err)
|
||||
}
|
||||
|
||||
asm, err := cmd.Generate(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("Generate(%q) error = %v", lineText, err)
|
||||
}
|
||||
|
||||
allAsm = append(allAsm, fmt.Sprintf("; %s", lineText))
|
||||
allAsm = append(allAsm, asm...)
|
||||
}
|
||||
|
||||
if !equalAsmElse(allAsm, tt.wantAsm) {
|
||||
t.Errorf("Assembly mismatch\ngot:\n%s\nwant:\n%s",
|
||||
strings.Join(allAsm, "\n"),
|
||||
strings.Join(tt.wantAsm, "\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestElseCommand_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "ELSE without IF",
|
||||
line: "ELSE",
|
||||
wantErr: "stack underflow",
|
||||
},
|
||||
{
|
||||
name: "wrong param count",
|
||||
line: "ELSE extra",
|
||||
wantErr: "wrong number of parameters",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := compiler.NewCompilerContext(preproc.NewPragma())
|
||||
cmd := &ElseCommand{}
|
||||
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
|
||||
|
||||
err := cmd.Interpret(line, ctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndIfCommand_Errors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
wantErr string
|
||||
}{
|
||||
{
|
||||
name: "ENDIF without IF",
|
||||
line: "ENDIF",
|
||||
wantErr: "stack underflow",
|
||||
},
|
||||
{
|
||||
name: "wrong param count",
|
||||
line: "ENDIF extra",
|
||||
wantErr: "wrong number of parameters",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := compiler.NewCompilerContext(preproc.NewPragma())
|
||||
cmd := &EndIfCommand{}
|
||||
line := preproc.Line{Text: tt.line, Kind: preproc.Source}
|
||||
|
||||
err := cmd.Interpret(line, ctx)
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
if !strings.Contains(err.Error(), tt.wantErr) {
|
||||
t.Errorf("error = %q, want substring %q", err.Error(), tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// equalAsmElse compares two assembly slices for equality
|
||||
func equalAsmElse(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
|
@ -9,8 +9,9 @@ import (
|
|||
"c65gm/internal/utils"
|
||||
)
|
||||
|
||||
// EndIfCommand handles ENDIF statements to close IF...ENDIF blocks
|
||||
// EndIfCommand handles ENDIF statements
|
||||
// Syntax: ENDIF
|
||||
// Ends current IF block
|
||||
type EndIfCommand struct {
|
||||
endLabel string
|
||||
}
|
||||
|
|
@ -20,7 +21,6 @@ func (c *EndIfCommand) WillHandle(line preproc.Line) bool {
|
|||
if err != nil || len(params) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.ToUpper(params[0]) == "ENDIF"
|
||||
}
|
||||
|
||||
|
|
@ -31,20 +31,19 @@ func (c *EndIfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContex
|
|||
}
|
||||
|
||||
if len(params) != 1 {
|
||||
return fmt.Errorf("ENDIF: wrong number of parameters (%d), expected 1", len(params))
|
||||
return fmt.Errorf("ENDIF: expected 1 parameter, got %d", len(params))
|
||||
}
|
||||
|
||||
// Pop the end label (from IF or ELSE)
|
||||
label, err := ctx.IfStack.Pop()
|
||||
if err != nil {
|
||||
return fmt.Errorf("ENDIF: %w", err)
|
||||
// Pop end label
|
||||
var err2 error
|
||||
c.endLabel, err2 = ctx.IfStack.Pop()
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("ENDIF: not inside IF block")
|
||||
}
|
||||
c.endLabel = label
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *EndIfCommand) Generate(_ *compiler.CompilerContext) ([]string, error) {
|
||||
// Just place the end label
|
||||
return []string{c.endLabel}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,29 +10,12 @@ import (
|
|||
)
|
||||
|
||||
// IfCommand handles IF conditional statements
|
||||
// Syntax:
|
||||
//
|
||||
// IF <param1> <op> <param2> # basic syntax
|
||||
// IF <param1> <op> <param2> THEN # optional THEN keyword
|
||||
//
|
||||
// Supported operators (for now): =, ==, <>, !=
|
||||
// More operators (>, <, >=, <=) can be added later
|
||||
//
|
||||
// Uses short jumps by default (inverted branch condition)
|
||||
// Uses long jumps if pragma _P_USE_LONG_JUMP is set
|
||||
// Syntax: IF <param1> <op> <param2>
|
||||
// Operators: =, ==, <>, !=, >, <, >=, <=
|
||||
type IfCommand struct {
|
||||
operator string // =, <>, etc.
|
||||
|
||||
param1VarName string
|
||||
param1VarKind compiler.VarKind
|
||||
param1Value uint16
|
||||
param1IsVar bool
|
||||
|
||||
param2VarName string
|
||||
param2VarKind compiler.VarKind
|
||||
param2Value uint16
|
||||
param2IsVar bool
|
||||
|
||||
operator string
|
||||
param1 *operandInfo
|
||||
param2 *operandInfo
|
||||
useLongJump bool
|
||||
skipLabel string
|
||||
}
|
||||
|
|
@ -42,7 +25,6 @@ func (c *IfCommand) WillHandle(line preproc.Line) bool {
|
|||
if err != nil || len(params) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.ToUpper(params[0]) == "IF"
|
||||
}
|
||||
|
||||
|
|
@ -52,34 +34,13 @@ func (c *IfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext)
|
|||
return err
|
||||
}
|
||||
|
||||
paramCount := len(params)
|
||||
|
||||
// IF <param1> <op> <param2> [THEN]
|
||||
if paramCount != 4 && paramCount != 5 {
|
||||
return fmt.Errorf("IF: wrong number of parameters (%d), expected 4 or 5", paramCount)
|
||||
}
|
||||
|
||||
// Check optional THEN keyword
|
||||
if paramCount == 5 {
|
||||
if strings.ToUpper(params[4]) != "THEN" {
|
||||
return fmt.Errorf("IF: parameter #5 must be 'THEN', got %q", params[4])
|
||||
}
|
||||
}
|
||||
|
||||
// Parse operator
|
||||
c.operator = params[2]
|
||||
switch c.operator {
|
||||
case "=", "==":
|
||||
c.operator = "=" // normalize
|
||||
case "<>", "!=":
|
||||
c.operator = "<>" // normalize
|
||||
default:
|
||||
return fmt.Errorf("IF: unsupported operator %q (only =, ==, <>, != supported for now)", c.operator)
|
||||
if len(params) != 4 {
|
||||
return fmt.Errorf("IF: expected 4 parameters, got %d", len(params))
|
||||
}
|
||||
|
||||
c.operator = normalizeOperator(params[2])
|
||||
scope := ctx.CurrentScope()
|
||||
|
||||
// Create constant lookup function
|
||||
constLookup := func(name string) (int64, bool) {
|
||||
sym := ctx.SymbolTable.Lookup(name, scope)
|
||||
if sym != nil && sym.IsConst() {
|
||||
|
|
@ -89,541 +50,65 @@ func (c *IfCommand) Interpret(line preproc.Line, ctx *compiler.CompilerContext)
|
|||
}
|
||||
|
||||
// Parse param1
|
||||
c.param1VarName, c.param1VarKind, c.param1Value, c.param1IsVar, err = compiler.ParseOperandParam(
|
||||
varName, varKind, value, isVar, err := compiler.ParseOperandParam(
|
||||
params[1], ctx.SymbolTable, scope, constLookup)
|
||||
if err != nil {
|
||||
return fmt.Errorf("IF: param1: %w", err)
|
||||
}
|
||||
c.param1 = &operandInfo{
|
||||
varName: varName,
|
||||
varKind: varKind,
|
||||
value: value,
|
||||
isVar: isVar,
|
||||
}
|
||||
|
||||
// Parse param2
|
||||
c.param2VarName, c.param2VarKind, c.param2Value, c.param2IsVar, err = compiler.ParseOperandParam(
|
||||
varName, varKind, value, isVar, err = compiler.ParseOperandParam(
|
||||
params[3], ctx.SymbolTable, scope, constLookup)
|
||||
if err != nil {
|
||||
return fmt.Errorf("IF: param2: %w", err)
|
||||
}
|
||||
c.param2 = &operandInfo{
|
||||
varName: varName,
|
||||
varKind: varKind,
|
||||
value: value,
|
||||
isVar: isVar,
|
||||
}
|
||||
|
||||
// Check pragma for long jump
|
||||
// Check pragma
|
||||
ps := ctx.Pragma.GetPragmaSetByIndex(line.PragmaSetIndex)
|
||||
longJumpPragma := ps.GetPragma("_P_USE_LONG_JUMP")
|
||||
c.useLongJump = longJumpPragma != "" && longJumpPragma != "0"
|
||||
|
||||
// Push skip label onto IF stack
|
||||
// Create skip label (jumps here on FALSE, or to ELSE if present)
|
||||
c.skipLabel = ctx.IfStack.Push()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *IfCommand) Generate(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
switch c.operator {
|
||||
case "=":
|
||||
return c.generateEqual(ctx)
|
||||
case "<>":
|
||||
return c.generateNotEqual(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("IF: internal error - unsupported operator %q", c.operator)
|
||||
op, err := parseOperator(c.operator)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("IF: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// generateEqual generates code for == comparison
|
||||
func (c *IfCommand) generateEqual(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
|
||||
// Constant folding: both literals
|
||||
if !c.param1IsVar && !c.param2IsVar {
|
||||
if c.param1Value != c.param2Value {
|
||||
// Always false - skip entire IF block
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
}
|
||||
// If equal, do nothing (condition always true)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Generate comparison based on types
|
||||
if c.useLongJump {
|
||||
return c.generateEqualLongJump(ctx)
|
||||
}
|
||||
return c.generateEqualShortJump(ctx)
|
||||
}
|
||||
|
||||
// generateEqualShortJump generates optimized short jumps (inverted condition)
|
||||
func (c *IfCommand) generateEqualShortJump(_ *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
|
||||
// Determine effective types for comparison
|
||||
kind1, kind2 := c.param1VarKind, c.param2VarKind
|
||||
if !c.param1IsVar {
|
||||
kind1 = inferKindFromValue(c.param1Value)
|
||||
}
|
||||
if !c.param2IsVar {
|
||||
kind2 = inferKindFromValue(c.param2Value)
|
||||
}
|
||||
|
||||
// byte == byte
|
||||
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
|
||||
}
|
||||
|
||||
// Inverted: if NOT equal, skip
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// word == word
|
||||
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
|
||||
// Compare low bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
|
||||
}
|
||||
|
||||
// If low bytes differ, skip
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
|
||||
|
||||
// Compare high bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
|
||||
}
|
||||
|
||||
// If high bytes differ, skip
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Mixed byte/word comparisons - extend byte to word
|
||||
// byte == word or word == byte
|
||||
var byteVal uint16
|
||||
var byteIsVar bool
|
||||
var byteName string
|
||||
var wordVal uint16
|
||||
var wordIsVar bool
|
||||
var wordName string
|
||||
|
||||
if kind1 == compiler.KindByte {
|
||||
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
} else {
|
||||
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
}
|
||||
|
||||
// Check word high byte must be 0
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
|
||||
}
|
||||
asm = append(asm, "\tcmp #0")
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
|
||||
|
||||
// Compare low bytes
|
||||
if byteIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
|
||||
}
|
||||
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", c.skipLabel))
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// generateEqualLongJump generates traditional long jumps (old style)
|
||||
func (c *IfCommand) generateEqualLongJump(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
successLabel := ctx.GeneralStack.Push() // temporary label
|
||||
|
||||
// Similar logic but with inverted branches
|
||||
kind1, kind2 := c.param1VarKind, c.param2VarKind
|
||||
if !c.param1IsVar {
|
||||
kind1 = inferKindFromValue(c.param1Value)
|
||||
}
|
||||
if !c.param2IsVar {
|
||||
kind2 = inferKindFromValue(c.param2Value)
|
||||
}
|
||||
|
||||
// byte == byte
|
||||
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// word == word
|
||||
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
|
||||
// Compare low bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
|
||||
}
|
||||
|
||||
failLabel := ctx.GeneralStack.Push()
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", failLabel))
|
||||
|
||||
// Compare high bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
|
||||
asm = append(asm, failLabel)
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Mixed comparisons similar to short jump
|
||||
var byteVal uint16
|
||||
var byteIsVar bool
|
||||
var byteName string
|
||||
var wordVal uint16
|
||||
var wordIsVar bool
|
||||
var wordName string
|
||||
|
||||
if kind1 == compiler.KindByte {
|
||||
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
} else {
|
||||
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
}
|
||||
|
||||
failLabel := ctx.GeneralStack.Push()
|
||||
|
||||
// Check word high byte must be 0
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
|
||||
}
|
||||
asm = append(asm, "\tcmp #0")
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", failLabel))
|
||||
|
||||
// Compare low bytes
|
||||
if byteIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
|
||||
}
|
||||
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbeq %s", successLabel))
|
||||
asm = append(asm, failLabel)
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// generateNotEqual generates code for != comparison
|
||||
func (c *IfCommand) generateNotEqual(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
|
||||
// Constant folding: both literals
|
||||
if !c.param1IsVar && !c.param2IsVar {
|
||||
if c.param1Value == c.param2Value {
|
||||
// Always false - skip entire IF block
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
}
|
||||
// If not equal, do nothing (condition always true)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Generate comparison based on types
|
||||
if c.useLongJump {
|
||||
return c.generateNotEqualLongJump(ctx)
|
||||
}
|
||||
return c.generateNotEqualShortJump(ctx)
|
||||
}
|
||||
|
||||
// generateNotEqualShortJump generates optimized short jumps for !=
|
||||
func (c *IfCommand) generateNotEqualShortJump(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
|
||||
kind1, kind2 := c.param1VarKind, c.param2VarKind
|
||||
if !c.param1IsVar {
|
||||
kind1 = inferKindFromValue(c.param1Value)
|
||||
}
|
||||
if !c.param2IsVar {
|
||||
kind2 = inferKindFromValue(c.param2Value)
|
||||
}
|
||||
|
||||
// byte != byte
|
||||
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
|
||||
}
|
||||
|
||||
// Inverted: if EQUAL, skip
|
||||
asm = append(asm, fmt.Sprintf("\tbeq %s", c.skipLabel))
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// word != word - need to check if ANY byte differs
|
||||
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
|
||||
successLabel := ctx.GeneralStack.Push()
|
||||
|
||||
// Compare low bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
|
||||
}
|
||||
|
||||
// If low bytes differ, condition is true - continue
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
|
||||
// Compare high bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
|
||||
}
|
||||
|
||||
// If high bytes differ, condition is true - continue
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
|
||||
// Both bytes equal - skip
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Mixed byte/word - similar logic
|
||||
var byteVal uint16
|
||||
var byteIsVar bool
|
||||
var byteName string
|
||||
var wordVal uint16
|
||||
var wordIsVar bool
|
||||
var wordName string
|
||||
|
||||
if kind1 == compiler.KindByte {
|
||||
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
} else {
|
||||
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
}
|
||||
|
||||
successLabel := ctx.GeneralStack.Push()
|
||||
|
||||
// Check word high byte != 0 means not equal
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
|
||||
}
|
||||
asm = append(asm, "\tcmp #0")
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
|
||||
// Compare low bytes
|
||||
if byteIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
|
||||
}
|
||||
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbeq %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// generateNotEqualLongJump generates traditional long jumps for !=
|
||||
func (c *IfCommand) generateNotEqualLongJump(ctx *compiler.CompilerContext) ([]string, error) {
|
||||
var asm []string
|
||||
successLabel := ctx.GeneralStack.Push()
|
||||
|
||||
kind1, kind2 := c.param1VarKind, c.param2VarKind
|
||||
if !c.param1IsVar {
|
||||
kind1 = inferKindFromValue(c.param1Value)
|
||||
}
|
||||
if !c.param2IsVar {
|
||||
kind2 = inferKindFromValue(c.param2Value)
|
||||
}
|
||||
|
||||
// byte != byte
|
||||
if kind1 == compiler.KindByte && kind2 == compiler.KindByte {
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// word != word
|
||||
if kind1 == compiler.KindWord && kind2 == compiler.KindWord {
|
||||
// Compare low bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value&0xFF)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value&0xFF)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
|
||||
// Compare high bytes
|
||||
if c.param1IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", c.param1VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(c.param1Value>>8)))
|
||||
}
|
||||
|
||||
if c.param2IsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s+1", c.param2VarName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(c.param2Value>>8)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
}
|
||||
|
||||
// Mixed byte/word
|
||||
var byteVal uint16
|
||||
var byteIsVar bool
|
||||
var byteName string
|
||||
var wordVal uint16
|
||||
var wordIsVar bool
|
||||
var wordName string
|
||||
|
||||
if kind1 == compiler.KindByte {
|
||||
byteVal, byteIsVar, byteName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
wordVal, wordIsVar, wordName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
} else {
|
||||
byteVal, byteIsVar, byteName = c.param2Value, c.param2IsVar, c.param2VarName
|
||||
wordVal, wordIsVar, wordName = c.param1Value, c.param1IsVar, c.param1VarName
|
||||
}
|
||||
|
||||
// Check word high byte != 0
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s+1", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(wordVal>>8)))
|
||||
}
|
||||
asm = append(asm, "\tcmp #0")
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
|
||||
// Compare low bytes
|
||||
if byteIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tlda %s", byteName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tlda #$%02x", uint8(byteVal)))
|
||||
}
|
||||
|
||||
if wordIsVar {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp %s", wordName))
|
||||
} else {
|
||||
asm = append(asm, fmt.Sprintf("\tcmp #$%02x", uint8(wordVal&0xFF)))
|
||||
}
|
||||
|
||||
asm = append(asm, fmt.Sprintf("\tbne %s", successLabel))
|
||||
asm = append(asm, fmt.Sprintf("\tjmp %s", c.skipLabel))
|
||||
asm = append(asm, successLabel)
|
||||
return asm, nil
|
||||
|
||||
// Generate comparison (jumps to skipLabel on FALSE)
|
||||
gen, err := newComparisonGenerator(
|
||||
op,
|
||||
c.param1,
|
||||
c.param2,
|
||||
c.useLongJump,
|
||||
ctx.IfStack,
|
||||
ctx.GeneralStack,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("IF: %w", err)
|
||||
}
|
||||
|
||||
cmpAsm, err := gen.generate()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("IF: %w", err)
|
||||
}
|
||||
|
||||
return cmpAsm, nil
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -115,8 +115,8 @@ func TestWhileAllOperators(t *testing.T) {
|
|||
}{
|
||||
{"equal", "WHILE x = 10", "bne"},
|
||||
{"not equal", "WHILE x <> 10", "beq"},
|
||||
{"greater", "WHILE x > 10", "beq"},
|
||||
{"less", "WHILE x < 10", "beq"},
|
||||
{"greater", "WHILE x > 10", "bcs"},
|
||||
{"less", "WHILE x < 10", "bcs"},
|
||||
{"greater equal", "WHILE x >= 10", "bcc"},
|
||||
{"less equal", "WHILE x <= 10", "bcc"},
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue