Skip to content
Snippets Groups Projects
Commit 1aae298a authored by Nicholas Wesley Robinson's avatar Nicholas Wesley Robinson
Browse files

more expr and literals

parent a7da5205
No related branches found
No related tags found
No related merge requests found
......@@ -433,13 +433,22 @@ class ExprNode(ASTNode):
def codeGen(self):
if hasattr(self, "code") and self.code != "":
return
self.code = ""
# Unary:
# Unary operations:
if not self.left:
self.code = ""
return
# get right output
if not hasattr(self.right, "code"):
self.right.codeGen()
self.code += self.right.code
if self.op == '-':
self.code += p('neg', 'eax')
return
if self.op == '!':
self.code += p('setz', 'eax')
return
self.code += self.codeGenLeftRight()
......@@ -447,15 +456,48 @@ class ExprNode(ASTNode):
# ebx = left result
# eax = right result
# so, basically do "eax = ebx op eax"
if self.op in ['+', '-']:#, '*', '/', '%']:
# Binary operations:
# Add, Subtract, Multiply
if self.op in ['+', '-', '*']:
# operation -> generated code
ops = {
'+': "add",
'-': "sub"
'-': "sub",
'*': "imul"
}
self.code += p(ops[self.op], "ebx", "eax", " " + ops[self.op] + " left and right")
self.code += p(ops[self.op], "ebx", "eax", " left:ebx " + self.op + " right:eax")
self.code += p("mov", "eax", "ebx", " move result to eax")
return
# Divide, Modulus
if self.op in ['/', '%']:
# switch eax and ebx, because "idiv ebx" -> eax = edx:eax / ebx
self.code += p('xchg', 'eax', 'ebx')
self.code += p('push', 'edx') # save edx in case someone else was using it
self.code += p('cdq', '', None, " set edx to the sign of eax")
self.code += p("idiv", "ebx", "", " left:eax " + self.op + " right:ebx")
# eax = eax / abx
# edx = eax % ebx
if self.op == '%':
self.code += p("mov", "eax", "edx", " move quotient result to eax") # quotient is in edx
self.code += p('pop', 'edx') # restore edx
return
# Comparisons:
if self.op in ['&&', '&']:
self.code += p("and", "eax", "ebx", " right:eax " + self.op + " left:ebx")
return
if self.op in ['|', '||']:
self.code += p("por", "eax", "ebx", " right:eax " + self.op + " left:ebx")
return
# if self.op in ['==', '!=', '<=', '>=', '>', '<']: TODO
# return
# if self.op == 'instanceof': TODO
# return
# generate shorter code if self.op = comparison
def getIfFalse(self, label):
......
......@@ -42,16 +42,40 @@ class LiteralNode(ASTNode):
self.myType = TypeStruct(self.name, node)
def codeGen(self):
if hasattr(self, "code") and self.code != "":
return
self.code = ""
# int
if self.name == 'int':
self.code += p("mov", "eax", str(self.getConstant()), " set to literal int")
# boolean
if self.name == 'boolean':
if self.getConstant:
self.code = p("mov", "eax", "1", " set to literal true")
self.code += p("mov", "eax", "1", " set to literal true")
return
else:
self.code = p("mov", "eax", "0", " set to literal false")
self.code += p("mov", "eax", "0", " set to literal false")
return
# char TODO
# if self.name == 'char':
# self.code += p("dd", str(self.getConstant()), "", " set to literal string")
# self.code += p("pop", "eax")
# return
# string TODO
# if self.name == 'java.lang.String':
# self.code += p("dd", str(self.getConstant()), "", " set to literal char")
# self.code += p("pop", "eax")
# return
# null
if self.name == 'null':
self.code += p("mov", "eax", "0", " set to literal null")
return
# int
if self.name == 'int':
self.code += p("mov", "eax", str(self.getConstant()), " set to literal int")
return
def getConstant(self):
if self.name == 'int':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment