Improved branch code generation

This commit is contained in:
Eduard Urbach 2024-07-07 14:07:34 +02:00
parent 91e300e49a
commit 403e78f655
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
3 changed files with 11 additions and 9 deletions

View File

@ -1,7 +1,7 @@
main() { main() {
x := f(1) + f(2) + f(3) x := f(1) + f(2) + f(3)
if x != 9 { if x != f(8) {
exit(42) exit(42)
} }

View File

@ -33,7 +33,7 @@ func toASTNode(tokens token.List) (Node, error) {
return &Return{Value: value}, nil return &Return{Value: value}, nil
case keyword.Loop: case keyword.Loop:
blockStart := tokens.IndexKind(token.BlockStart) + 1 blockStart := tokens.IndexKind(token.BlockStart)
blockEnd := tokens.LastIndexKind(token.BlockEnd) blockEnd := tokens.LastIndexKind(token.BlockEnd)
if blockStart == -1 { if blockStart == -1 {
@ -44,11 +44,11 @@ func toASTNode(tokens token.List) (Node, error) {
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End()) return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
} }
tree, err := Parse(tokens[blockStart:blockEnd]) body, err := Parse(tokens[blockStart+1 : blockEnd])
return &Loop{Body: tree}, err return &Loop{Body: body}, err
case keyword.If: case keyword.If:
blockStart := tokens.IndexKind(token.BlockStart) + 1 blockStart := tokens.IndexKind(token.BlockStart)
blockEnd := tokens.LastIndexKind(token.BlockEnd) blockEnd := tokens.LastIndexKind(token.BlockEnd)
if blockStart == -1 { if blockStart == -1 {
@ -59,9 +59,9 @@ func toASTNode(tokens token.List) (Node, error) {
return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End()) return nil, errors.New(errors.MissingBlockEnd, nil, tokens[len(tokens)-1].End())
} }
condition := expression.Parse(tokens[1:token.BlockStart]) condition := expression.Parse(tokens[1:blockStart])
tree, err := Parse(tokens[blockStart:blockEnd]) body, err := Parse(tokens[blockStart+1 : blockEnd])
return &If{Condition: condition, Body: tree}, err return &If{Condition: condition, Body: body}, err
default: default:
return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, nil, tokens[0].Position) return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, nil, tokens[0].Position)

View File

@ -10,13 +10,14 @@ import (
// CompileIf compiles a branch instruction. // CompileIf compiles a branch instruction.
func (f *Function) CompileIf(branch *ast.If) error { func (f *Function) CompileIf(branch *ast.If) error {
condition := branch.Condition condition := branch.Condition
tmpRight := f.cpu.Input[1] tmpRight := f.cpu.MustFindFree(f.cpu.General)
err := f.ExpressionToRegister(condition.Children[1], tmpRight) err := f.ExpressionToRegister(condition.Children[1], tmpRight)
if err != nil { if err != nil {
return err return err
} }
f.cpu.Use(tmpRight)
tmpLeft := f.cpu.Input[0] tmpLeft := f.cpu.Input[0]
err = f.ExpressionToRegister(condition.Children[0], tmpLeft) err = f.ExpressionToRegister(condition.Children[0], tmpLeft)
@ -25,6 +26,7 @@ func (f *Function) CompileIf(branch *ast.If) error {
} }
f.assembler.RegisterRegister(asm.COMPARE, tmpLeft, tmpRight) f.assembler.RegisterRegister(asm.COMPARE, tmpLeft, tmpRight)
f.cpu.Free(tmpRight)
elseLabel := fmt.Sprintf("%s_if_%d_else", f.Name, f.count.branch) elseLabel := fmt.Sprintf("%s_if_%d_else", f.Name, f.count.branch)
switch condition.Token.Text() { switch condition.Token.Text() {