Skip to content

Commit

Permalink
fix fn call test
Browse files Browse the repository at this point in the history
  • Loading branch information
Westsi committed Jul 3, 2024
1 parent c1ed631 commit 0030f60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
25 changes: 13 additions & 12 deletions codegen/aarch64_clang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ func (g *AARCH64Generator) GenerateInfix(node *ast.InfixExpression) StorageLoc {
func (g *AARCH64Generator) GetInfixOperands(node *ast.InfixExpression) (string, string, StorageLoc) {
tracer.Trace("GetInfixOperands")
defer tracer.Untrace("GetInfixOperands")
var leftcall, rightcall bool
var leftS, rightS string
var destLoc StorageLoc
switch left := node.Left.(type) {
Expand All @@ -330,19 +331,8 @@ func (g *AARCH64Generator) GetInfixOperands(node *ast.InfixExpression) (string,
case *ast.CallExpression:
g.GenerateCall(left)
leftS = StorageLocs[X0]
leftcall = true
case *ast.IntegerLiteral:
// leftS = "$" + fmt.Sprintf("%d", left.Value)
// var sloc StorageLoc
// for _, v := range Sls {
// _, ok := g.VirtualRegisters[v]
// if !ok {
// g.VirtualRegisters[v] = "TEMP"
// sloc = v
// break
// }
// }
// leftS = StorageLocs[sloc]
// g.out.WriteString("mov " + leftS + ", " + fmt.Sprintf("%d", left.Value) + "\n")
leftS = StorageLocs[g.GenerateIntegerLiteral(left)]
}

Expand All @@ -357,6 +347,17 @@ func (g *AARCH64Generator) GetInfixOperands(node *ast.InfixExpression) (string,
case *ast.CallExpression:
g.GenerateCall(right)
rightS = StorageLocs[X0]
rightcall = true
}

if leftcall && rightS == "x0" {
rightS = "x1"
g.VirtualRegisters[X1] = "TEMP"
}

if rightcall && leftS == "x0" {
leftS = "x1"
g.VirtualRegisters[X1] = "TEMP"
}

for _, v := range Sls {
Expand Down
13 changes: 13 additions & 0 deletions codegen/x86_64_as/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ func (g *X64Generator) GenerateInfix(node *ast.InfixExpression) StorageLoc {
func (g *X64Generator) GetInfixOperands(node *ast.InfixExpression) (string, string, StorageLoc) {
tracer.Trace("GetInfixOperands")
defer tracer.Untrace("GetInfixOperands")
var leftcall, rightcall bool
var leftS, rightS string
var leftLoc StorageLoc // TODO: fix bug with something like int y = x*8 where x would be
switch left := node.Left.(type) {
Expand All @@ -371,6 +372,7 @@ func (g *X64Generator) GetInfixOperands(node *ast.InfixExpression) (string, stri
leftS = StorageLocs[leftLoc]
case *ast.CallExpression:
g.GenerateCall(left)
leftcall = true
leftS = StorageLocs[RAX]
}

Expand All @@ -384,6 +386,17 @@ func (g *X64Generator) GetInfixOperands(node *ast.InfixExpression) (string, stri
case *ast.CallExpression:
g.GenerateCall(right)
rightS = StorageLocs[RAX]
rightcall = true
}

if leftcall && rightS == "%rax" {
rightS = "%rcx"
g.VirtualRegisters[RCX] = "TEMP"
}

if rightcall && leftS == "%rax" {
leftS = "%rcx"
g.VirtualRegisters[RCX] = "TEMP"
}
return leftS, rightS, leftLoc
}
Expand Down

0 comments on commit 0030f60

Please sign in to comment.