Improved branch code generation
This commit is contained in:
parent
91e300e49a
commit
403e78f655
@ -1,7 +1,7 @@
|
||||
main() {
|
||||
x := f(1) + f(2) + f(3)
|
||||
|
||||
if x != 9 {
|
||||
if x != f(8) {
|
||||
exit(42)
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ func toASTNode(tokens token.List) (Node, error) {
|
||||
return &Return{Value: value}, nil
|
||||
|
||||
case keyword.Loop:
|
||||
blockStart := tokens.IndexKind(token.BlockStart) + 1
|
||||
blockStart := tokens.IndexKind(token.BlockStart)
|
||||
blockEnd := tokens.LastIndexKind(token.BlockEnd)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
tree, err := Parse(tokens[blockStart:blockEnd])
|
||||
return &Loop{Body: tree}, err
|
||||
body, err := Parse(tokens[blockStart+1 : blockEnd])
|
||||
return &Loop{Body: body}, err
|
||||
|
||||
case keyword.If:
|
||||
blockStart := tokens.IndexKind(token.BlockStart) + 1
|
||||
blockStart := tokens.IndexKind(token.BlockStart)
|
||||
blockEnd := tokens.LastIndexKind(token.BlockEnd)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
condition := expression.Parse(tokens[1:token.BlockStart])
|
||||
tree, err := Parse(tokens[blockStart:blockEnd])
|
||||
return &If{Condition: condition, Body: tree}, err
|
||||
condition := expression.Parse(tokens[1:blockStart])
|
||||
body, err := Parse(tokens[blockStart+1 : blockEnd])
|
||||
return &If{Condition: condition, Body: body}, err
|
||||
|
||||
default:
|
||||
return nil, errors.New(&errors.KeywordNotImplemented{Keyword: tokens[0].Text()}, nil, tokens[0].Position)
|
||||
|
@ -10,13 +10,14 @@ import (
|
||||
// CompileIf compiles a branch instruction.
|
||||
func (f *Function) CompileIf(branch *ast.If) error {
|
||||
condition := branch.Condition
|
||||
tmpRight := f.cpu.Input[1]
|
||||
tmpRight := f.cpu.MustFindFree(f.cpu.General)
|
||||
err := f.ExpressionToRegister(condition.Children[1], tmpRight)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.cpu.Use(tmpRight)
|
||||
tmpLeft := f.cpu.Input[0]
|
||||
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.cpu.Free(tmpRight)
|
||||
elseLabel := fmt.Sprintf("%s_if_%d_else", f.Name, f.count.branch)
|
||||
|
||||
switch condition.Token.Text() {
|
||||
|
Loading…
Reference in New Issue
Block a user