def address(var): #scope = ST.currScope #print ST.SymbolTable[scope]["identifiers"][var]['offset'] ## print var if var.startswith("this."): parent = ST.SymbolTable[ST.currScope]['classname'] #print parent off = [] #if var.split('.')[1] in ST.SymbolTable[parent]['identifiers']: #print ST.currScope off = ST.SymbolTable[parent]["identifiers"][var.split('.') [1]]['offset'] - 4 #else: # sys.exit("Variable not found address line 45 in registeralloc") #else: # extend = ST.SymbolTable[parent]['extends'] # if extend is None: # sys.exit("variable not found") # else: # if var.split('.')[1] in ST.SymbolTable[extend]['identifiers']: # off = ST.SymbolTable[extend]['extend'+var.split('.')[1]] # else: # sys.exit("Variable not found") print('\tmovl 8(%ebp), %edi') print('\taddl $' + str(off) + ", %edi") #print('\tmovl (%edi), %edi') return "(%edi)" if var.startswith("_t"): var = "-" + str(int(var[2:]) * 4) + "(%ebp)" #print("I am awesome") return var #print "-------------" if '@ARRAY' in var: z = ST.getId(var.split('@')[0]) if 'isparam' in z.keys(): print('\tmovl -' + str(ST.getOffset(var.split('@')[0])) + "(%ebp), %edi") return "%edi" else: print('\tleal -' + str(ST.getOffset(var.split('@')[0])) + "(%ebp), %edi") return "%edi" #ST.printSymbolTable() a = ST.getOffset(var) if a < 0: var = str(a * -1) + "(%ebp)" else: var = "-" + str(a) + "(%ebp)" return var
def generateCode(i): global lab if tacTable[i].oper in ['+', '-', '*', '&', '|', '^']: ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') #print('\tmov ' + regName(ry) + ', DWORD PTR ' + tacTable[i].in1) addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rz = getRegIn(i, tacTable[i].in2) if registerDescr[rz] != tacTable[i].in2: printInstr('movl', regName(rz), 'Register', address(tacTable[i].in2), 'Memory') #print('\tmov ' + regName(rz) + ', DWORD PTR ' + tacTable[i].in2) addressDescr[tacTable[i].in2]['Register'] = rz registerDescr[rz] = tacTable[i].in2 rx = getRegOut(i, tacTable[i].out) registerDescr[rx] = tacTable[i].out printInstr('movl', regName(rx), 'Register', regName(ry), 'Register') #print('\tmov ' + regName(rx) + ', ' + regName(ry)) if tacTable[i].oper == '+': printInstr('addl', regName(rx), 'Register', regName(rz), 'Register') #print('\tadd ' + regName(rx) + ', ' + regName(rz)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '-': printInstr('subl', regName(rx), 'Register', regName(rz), 'Register') #print('\tsub ' + regName(rx) + ', ' + regName(rz)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '*': printInstr('imull', regName(rx), 'Register', regName(rz), 'Register') #print('\timul ' + regName(rx) + ', ' + regName(rz)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '&': print('\tandl %' + regName(rz) + ', %' + regName(rx)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '|': print('\torl %' + regName(rz) + ', %' + regName(rx)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '^': print('\txorl %' + regName(rz) + ', %' + regName(rx)) addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '||': #printInstr('movl',regName(rx),'Register',regName(ry),'Register') ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') #print('\tmov ' + regName(ry) + ', DWORD PTR ' + tacTable[i].in1) addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rz = getRegIn(i, tacTable[i].in2) if registerDescr[rz] != tacTable[i].in2: printInstr('movl', regName(rz), 'Register', address(tacTable[i].in2), 'Memory') #print('\tmov ' + regName(rz) + ', DWORD PTR ' + tacTable[i].in2) addressDescr[tacTable[i].in2]['Register'] = rz registerDescr[rz] = tacTable[i].in2 rx = getRegOut(i, tacTable[i].out) registerDescr[rx] = tacTable[i].out printInstr('movl', address(registerDescr[0]), 'Memory', regName(rx), 'Register') #print('\tmov DWORD PTR ' + variable + ', ' + regName(addressDescr[variable]['Register'])) #registerDescr[0] = None #addressDescr[var]['Register'] = None #addressDescr[var]['Memory'] = var print("\tcmpl $0, %" + regName(rz)) lab2 = lab lab += 1 lab3 = lab lab += 1 lab4 = lab lab += 1 print("\tjne .L" + str(lab2)) print("\tcmpl $0, %" + regName(ry)) print("\tje .L" + str(lab3)) print(".L" + str(lab2) + ":") print("\tmovl $1, %" + regName(rx)) print("\tjmp .L" + str(lab4)) print(".L" + str(lab3) + ":") print("\tmovl $0, %" + regName(rx)) print(".L" + str(lab4) + ":") print("\tmovl %" + regName(rx) + ", %" + regName(rz)) elif tacTable[i].oper == '&&': ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') #print('\tmov ' + regName(ry) + ', DWORD PTR ' + tacTable[i].in1) addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rz = getRegIn(i, tacTable[i].in2) if registerDescr[rz] != tacTable[i].in2: printInstr('movl', regName(rz), 'Register', address(tacTable[i].in2), 'Memory') #print('\tmov ' + regName(rz) + ', DWORD PTR ' + tacTable[i].in2) addressDescr[tacTable[i].in2]['Register'] = rz registerDescr[rz] = tacTable[i].in2 rx = getRegOut(i, tacTable[i].out) registerDescr[rx] = tacTable[i].out printInstr('movl', address(registerDescr[0]), 'Memory', regName(rx), 'Register') #print('\tmov DWORD PTR ' + variable + ', ' + regName(addressDescr[variable]['Register'])) #registerDescr[0] = None #addressDescr[var]['Register'] = None #addressDescr[var]['Memory'] = var print("\tcmpl $0, %" + regName(rz)) global lab lab1 = lab lab += 1 lab2 = lab lab += 1 print("\tje .L" + str(lab1)) print("\tcmpl $0, %" + regName(ry)) print("\tje .L" + str(lab1)) print("\tmovl $1, %" + regName(rx)) print("\tjmp .L" + str(lab2)) print(".L" + str(lab1) + ":") print("\tmovl $0, %" + regName(rx)) print(".L" + str(lab2) + ":") print("\tmovl %" + regName(rx) + ", %" + regName(rz)) elif tacTable[i].oper == '=': if not is_number(tacTable[i].in1): ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rx = getRegOut(i, tacTable[i].out) printInstr('movl', regName(rx), 'Register', regName(ry), 'Register') else: rx = getRegOut(i, tacTable[i].out) const = tacTable[i].in1 printInstr('movl', regName(rx), 'Register', const, 'Constant') #print('\tmov ' + regName(rx) + ', ' + regName(ry)) registerDescr[rx] = tacTable[i].out addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == '/' or tacTable[i].oper == '%': if registerDescr[0] != None: printInstr('movl', address(registerDescr[0]), 'Memory', regName(0), 'Register') #print('\tmov DWORD PTR ' + registerDescr[0] + ', ' + regName(0)) addressDescr[registerDescr[0]]['Register'] = None addressDescr[registerDescr[0]]['Memory'] = registerDescr[0] if registerDescr[3] != None: printInstr('movl', address(registerDescr[3]), 'Memory', regName(3), 'Register') #print('\tmov DWORD PTR ' + registerDescr[3] + ', ' + regName(3)) addressDescr[registerDescr[3]]['Register'] = None addressDescr[registerDescr[3]]['Memory'] = registerDescr[3] printInstr('movl', regName(0), 'Register', address(tacTable[i].in1), 'Memory') registerDescr[0] = tacTable[i].in1 addressDescr[tacTable[i].in1]['Register'] = 0 print('\tcltd') registerDescr[3] = tacTable[i].in1 rz = getRegIn(i, tacTable[i].in2) if registerDescr[rz] != tacTable[i].in2: printInstr('movl', regName(rz), 'Register', address(tacTable[i].in2), 'Memory') addressDescr[tacTable[i].in2]['Register'] = rz registerDescr[rz] = tacTable[i].in2 #print('\tidivl %' + regName(rz)) printInstr('idivl', regName(rz), 'Register') rx = getRegOut(i, tacTable[i].out) if tacTable[i].oper == '/': # move eax to rx printInstr('movl', regName(rx), 'Register', 'eax', 'Register') registerDescr[rx] = registerDescr[0] elif tacTable[i].oper == '%': # move edx to rx printInstr('movl', regName(rx), 'Register', 'edx', 'Register') registerDescr[rx] = registerDescr[3] registerDescr[0], registerDescr[3] = None, None addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None registerDescr[rx] = tacTable[i].out elif tacTable[i].oper in ['<', '<=', '==', '>=', '>', '!=']: ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rz = getRegIn(i, tacTable[i].in2) if registerDescr[rz] != tacTable[i].in2: printInstr('movl', regName(rz), 'Register', address(tacTable[i].in2), 'Memory') addressDescr[tacTable[i].in2]['Register'] = rz registerDescr[rz] = tacTable[i].in2 printInstr('cmpl', regName(rz), 'Register', regName(ry), 'Register') rx = getRegOut(i, tacTable[i].out) #if b = a<b is an independent statement and not followed by if? printInstr('movl', address(registerDescr[0]), 'Memory', regName(0), 'Register') addressDescr[registerDescr[0]]['Register'] = None addressDescr[registerDescr[0]]['Memory'] = registerDescr[0] registerDescr[0] = None printInstr('cmp', regName(rx), 'Register', i=i) print('\tmovzbl %al, %' + regName(rx)) registerDescr[rx] = tacTable[i].out addressDescr[tacTable[i].out]['Register'] = rx addressDescr[tacTable[i].out]['Memory'] = None elif tacTable[i].oper == 'if': ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 endBlock() print('\ttestl %' + regName(ry) + ', %' + regName(ry)) print('\tjne .' + tacTable[i].out) elif tacTable[i].oper == 'goto': endBlock() print('\tjmp .' + tacTable[i].out) elif tacTable[i].oper == 'label': endBlock() #print('\t.globl ' + tacTable[i].out) print('.' + tacTable[i].out + ':') elif tacTable[i].oper == 'flabel': endBlock() print('\t.globl ' + tacTable[i].out) print(tacTable[i].out + ':') elif tacTable[i].oper == 'ldar': endBlock() rx = getRegOut(i, tacTable[i].out) if registerDescr[rx] != tacTable[i].out: printInstr('movl', regName(rx), 'Register', address(tacTable[i].out), 'Memory') addressDescr[tacTable[i].out]['Register'] = rx registerDescr[rx] = tacTable[i].out addressDescr[tacTable[i].out]['Memory'] = None ri = getRegOut(i, tacTable[i].in2) if registerDescr[ri] != tacTable[i].in2: printInstr('movl', regName(ri), 'Register', address(tacTable[i].in2), 'Memory') addressDescr[tacTable[i].in2]['Register'] = ri registerDescr[ri] = tacTable[i].in2 z = ST.getId(tacTable[i].in1) if 'isparam' in z.keys(): print('\tmovl ' + str(-1 * ST.getOffset(tacTable[i].in1)) + "(%ebp), %edi") print('\timull $4, %' + regName(ri)) print('\taddl %' + regName(ri) + ', %edi') print('\tmovl (%edi), %' + regName(rx)) #print"boom" else: print('\tmovl -' + str(ST.getOffset(tacTable[i].in1)) + "(%ebp,%" + regName(ri) + ",4), %" + regName(rx)) elif tacTable[i].oper == 'star': endBlock() ry = getRegIn(i, tacTable[i].in2) if registerDescr[ry] != tacTable[i].in2: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in2), 'Memory') addressDescr[tacTable[i].in2]['Register'] = ry registerDescr[ry] = tacTable[i].in2 ri = getRegIn(i, tacTable[i].in1) if registerDescr[ri] != tacTable[i].in1: printInstr('movl', regName(ri), 'Register', address(tacTable[i].in1), 'Memory') addressDescr[tacTable[i].in1]['Register'] = ri registerDescr[ri] = tacTable[i].in1 z = ST.getId(tacTable[i].out) if 'isparam' in z.keys(): #print "BAAAM" print('\tmovl ' + str(-1 * ST.getOffset(tacTable[i].out)) + "(%ebp), %edi") print('\timull $4, %' + regName(ri)) print('\taddl %' + regName(ri) + ', %edi') print('\tmovl %' + regName(ry) + ', (%edi)') #print('\tmovl (%edi), %'+regName(rx)) #print"boom" else: print('\tmovl %' + regName(ry) + ', -' + str(ST.getOffset(tacTable[i].out)) + "(%ebp,%" + regName(ri) + ",4)") elif tacTable[i].oper == 'printInt': endBlock() if is_number(tacTable[i].out): printInstr('movl', regName(0), 'Register', tacTable[i].out, 'Constant') else: printInstr('movl', regName(0), 'Register', address(tacTable[i].out), 'Memory') print('\tsubl $8, %esp') print('\tpushl %' + regName(0)) print('\tpushl $.format') #printInstr('movl',regName(5),'Register','$.format','Memory') print('\tcall printf') print('\taddl $16, %esp') elif tacTable[i].oper == 'scanInt': #spillAllReg() endBlock() print('\tsubl $8, %esp') print('\tleal ' + address(tacTable[i].out) + ', %edi') print('\tpushl %edi') print('\tpushl $.format2') #printInstr('xorl',regName(0),'Register',regName(0),'Register') #if is_number(tacTable[i].in1): #printInstr('movl',regName(4),'Register',tacTable[i].out,'Constant') #else: # printInstr('movl',regName(4),'Register',tacTable[i].in1,'Memory') #printInstr('movl',regName(5),'Register','$.format2','Memory') print('\tcall scanf') print('\taddl $16, %esp') elif tacTable[i].oper in ['call', 'fcall']: endBlock() funname = tacTable[i].in1.split('__')[0] objname = tacTable[i].in1.split('__')[1] #print objname if objname != 'this': print('\tleal ' + str(-1 * ST.getOffset(objname)) + '(%ebp), %eax') print('\tpushl %eax') else: print('\tpushl 8(%ebp)') print('\tcall ' + funname) print('\taddl $' + str(-1 * (ST.SymbolTable[funname + "@" + tacTable[i].in2]['paramoffset']) - 4) + ', %esp') if tacTable[i].oper == 'fcall': #print tacTable[i] printInstr('movl', address(tacTable[i].out), 'Memory', regName(0), 'Register') registerDescr[0] = None addressDescr[tacTable[i].out]['Memory'] = tacTable[i].out addressDescr[tacTable[i].out]['Register'] = None elif tacTable[i].oper in ['return', 'freturn']: endBlock() if tacTable[i].oper == 'freturn': printInstr('movl', regName(0), 'Register', address(tacTable[i].in1), 'Memory') addressDescr[tacTable[i].in1]['Register'] = 0 addressDescr[tacTable[i].in1]['Memory'] = tacTable[i].in1 print('\tleave') print('\tret') elif tacTable[i].oper == '!': ry = getRegIn(i, tacTable[i].in1) if registerDescr[ry] != tacTable[i].in1: printInstr('movl', regName(ry), 'Register', address(tacTable[i].in1), 'Memory') #print('\tmov ' + regName(ry) + ', DWORD PTR ' + tacTable[i].in1) addressDescr[tacTable[i].in1]['Register'] = ry registerDescr[ry] = tacTable[i].in1 rx = getRegOut(i, tacTable[i].out) registerDescr[rx] = tacTable[i].out printInstr('movl', regName(rx), 'Register', regName(ry), 'Register') printInstr('movl', tacTable[i].in1, 'Memory', regName(ry), 'Register') print('\tnotl %' + regName(ry)) printInstr('movl', regName(rx), 'Register', regName(ry), 'Register') addressDescr[tacTable[i].in1]['Register'] = None addressDescr[tacTable[i].in1]['Memory'] = tacTable[i].in1 addressDescr[tacTable[i].out]['Register'] = ry addressDescr[tacTable[i].out]['Memory'] = None registerDescr[ry] = tacTable[i].out elif tacTable[i].oper == 'scope': endBlock() #print tacTable[i].out #ST.printSymbolTable(1,1) ST.currScope = tacTable[i].out elif tacTable[i].oper == 'startscope': endBlock() #print tacTable[i].out #ST.printSymbolTable(1,1) ST.currScope = tacTable[i].out #tempmax = ST.SymbolTable[ST.currScope]['tempmax'] ST.SymbolTable[ST.currScope]['tempmax'] = ST.SymbolTable[ ST.SymbolTable[ST.currScope]['parent']]['tempmax'] print('\tsubl $' + str(ST.numVarScope()) + ', %esp') elif tacTable[i].oper == 'fstartscope': #print tacTable[i].out #ST.printSymbolTable(1,1) ST.currScope = tacTable[i].out tempmax = ST.SymbolTable[ST.currScope]['tempmax'] print('\tpushl %ebp') print('\tmovl %esp, %ebp') print('\tsubl $' + str(ST.printScopeOffset() + tempmax * 4) + ', %esp') elif tacTable[i].oper == 'param': endBlock() print('\tpushl ' + address(tacTable[i].in1))