Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
Westsi committed Jul 2, 2024
1 parent 3532979 commit 683ee54
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions codegen/x86_64_as/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ func (g *X64Generator) GenerateExpression(node ast.Expression) StorageLoc {
case *ast.Identifier:
return g.GenerateIdentifier(node)
case *ast.IntegerLiteral:
g.out.WriteString("movq $" + fmt.Sprintf("%d", node.Value) + ", %rax\n") //TODO: give this same treatment as the identifier case - picking regs
return RAX
// g.out.WriteString("movq $" + fmt.Sprintf("%d", node.Value) + ", %rax\n")
// return RAX
return g.GenerateIntegerLiteral(node)
case *ast.IfExpression:
g.GenerateIf(node)
case *ast.WhileExpression:
Expand Down Expand Up @@ -366,16 +367,8 @@ func (g *X64Generator) GetInfixOperands(node *ast.InfixExpression) (string, stri
leftS = StorageLocs[g.GenerateInfix(left)]
case *ast.IntegerLiteral:
// leftS = "$" + fmt.Sprintf("%d", left.Value)
for _, v := range Sls {
_, ok := g.VirtualRegisters[v]
if !ok {
g.VirtualRegisters[v] = "TEMP"
leftLoc = v
break
}
}
leftLoc = g.GenerateIntegerLiteral(left)
leftS = StorageLocs[leftLoc]
g.out.WriteString("movq $" + fmt.Sprintf("%d", left.Value) + ", " + leftS + "\n")
}

switch right := node.Right.(type) {
Expand All @@ -389,6 +382,22 @@ func (g *X64Generator) GetInfixOperands(node *ast.InfixExpression) (string, stri
return leftS, rightS, leftLoc
}

func (g *X64Generator) GenerateIntegerLiteral(il *ast.IntegerLiteral) StorageLoc {
var sloc StorageLoc
for _, v := range Sls {
_, ok := g.VirtualRegisters[v]
if !ok {
g.VirtualRegisters[v] = "TEMP"
sloc = v
break
}
}

g.out.WriteString("movq $" + fmt.Sprintf("%d", il.Value) + ", " + StorageLocs[sloc] + "\n")

return sloc
}

func (g *X64Generator) GenerateIf(i *ast.IfExpression) {
tracer.Trace("GenerateIf")
defer tracer.Untrace("GenerateIf")
Expand Down

0 comments on commit 683ee54

Please sign in to comment.