diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py index d0474e356bca9e67d8425bf638e213000080b895..57f57a33d74623b1463356c343f03603dc391c8c 100644 --- a/ExprPrimaryNodes.py +++ b/ExprPrimaryNodes.py @@ -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): diff --git a/UnitNodes.py b/UnitNodes.py index 15b42ccf69d916eea3097c34d0d06d78477ffdba..adac67e0c8efd56b9680cf944f30bed1e58ac779 100644 --- a/UnitNodes.py +++ b/UnitNodes.py @@ -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':