def emitUjump(self, _target=None): tac = TAC(InstrType.ujump, "goto") tac.target = _target # make the target a string (for printing purpose only) _target = _target if _target != None else "" self.addTac(tac, "goto "+ str(_target))
def emitUjump(self, _target=None): tac = TAC(InstrType.ujump, "goto") tac.target = _target # make the target a string (for printing purpose only) _target = _target if _target != None else "" self.addTac(tac, "goto " + str(_target))
def emitRet(self, _src=None): tac = TAC(InstrType.ret, "ret") tac.src1 =_src # make the _src a string (for printing purpose only) _src = _src if _src != None else {"place": ""} self.addTac( tac, "ret "+str(_src["place"]))
def emitAssgn(self, _op, _dest, _src1, _src2): tac = TAC(InstrType.assgn, _op) tac.dest = _dest tac.src1 = _src1 tac.src2 = _src2 self.addTac(tac, tac.dest["place"] + " = " + tac.src1["place"] + " " + tac.op +" " + tac.src2["place"])
def emitRet(self, _src=None): tac = TAC(InstrType.ret, "ret") tac.src1 = _src # make the _src a string (for printing purpose only) _src = _src if _src != None else {"place": ""} self.addTac(tac, "ret " + str(_src["place"]))
def emitCjump(self, _op, _src1, _src2, _target=None): tac = TAC(InstrType.cjump, _op) tac.src1 = _src1 tac.src2 = _src2 tac.target = _target # make the target a string (for printing purpose only) _target = _target if _target != None else "" self.addTac(tac, "if " + tac.src1["place"] + " " + tac.op +" " + tac.src2["place"] + " goto " + str(_target))
def emitCall(self, _target, _nParams = None, _returnVal = None): tac = TAC(InstrType.call, "call") tac.target = _target tac.src1 = _nParams tac.src2 = _returnVal # make the target a string (for printing purpose only) _nParams = _nParams if _nParams != None else "" _returnVal = _returnVal if _returnVal != None else {"place": ""} self.addTac(tac, "call "+ str(_target) + " "+str(_nParams) + " " + str(_returnVal["place"]))
def emitParams(self, _src): tac = TAC(InstrType.params, "params") tac.src1 = _src self.addTac(tac, "params " + str(_src["place"]))
def emitPrintStr(self): tac = TAC(InstrType.libFn, "printStr") tac.target = "printStr" self.addTac(tac, "printStr")
def emitCopy(self, _dest, _src): tac = TAC(InstrType.copy, "=") tac.dest = _dest tac.src1 = _src self.addTac(tac, tac.dest["place"] + " = " + tac.src["place"])
def emitParams(self, _src): tac = TAC(InstrType.params, "params") tac.src =_src self.tac.append(tac) self.addTac( tac, "params "+str(_src["place"]))
def emitEcho(self): tac = TAC(InstrType.libFn, "echo") self.addTac(tac, "echo")
def parseLine(self, irLine): # split the line and get the parts irParts = map(lambda x: x.strip(), irLine.split(",")) # the first part is line number ln = irParts[0] # second part is operator op = irParts[1] # depending upon operator, src, dest and target will be determined instrType = Operators[op] # create the new tac object for this line tac = TAC(instrType, op, ln) # switch case for instruction types and determine src, dest and targets dest = srcs = src1 = src2 = destST = srcST = src1ST = src2ST = None if instrType == InstrType.copy: # for destination # check if it's already declared, else create a new entry for it dest = irParts[2] destST = self._st.lookup(dest) if destST == None: destST = self._st.insert(dest, "ID") # for source src1 = irParts[3] # look into the symbol table for this lexeme src1ST = self._st.lookup(src1) if src1ST == None: if IsInt(src1): src1ST = self._st.insert(src1, "INT") else: print "IR.py:59:: " + src1 + " not declared in" print irLine # update the tc tac._destST = destST tac._src1ST = src1ST elif instrType == InstrType.assgn: # for destination dest = irParts[2] destST = self._st.lookup(dest) if destST == None: destST = self._st.insert(dest, "ID") # for sources srcs = irParts[3:] i = 0 for src in srcs: srcST = self._st.lookup(src) if srcST == None: if IsInt(src): srcST = self._st.insert(src, "INT") else: print "IR.py:80:: " + src + " not declared in" print irLine if i == 0: src1ST = srcST else: src2ST = srcST i += 1 # update the tc tac._destST = destST tac._src1ST = src1ST tac._src2ST = src2ST elif instrType == InstrType.label: # update the label. It'll be just after label operator tac._target = irParts[2] elif instrType == InstrType.ujump: # update the target, it will be last on line tac._target = irParts[-1] elif instrType == InstrType.cjump: # get the srcs srcs = [irParts[2], irParts[4]] i = 0 for src in srcs: srcST = self._st.lookup(src) if srcST == None: if IsInt(src): srcST = self._st.insert(src, "INT") else: print "IR.py:110:: " + src + " not declared in" print irLine if i == 0: src1ST = srcST else: src2ST = srcST i += 1 # get the relational operator relop = irParts[3] # get the target target = irParts[-1] # update the tac tac._src1ST = src1ST tac._src2ST = src2ST tac._op = relop tac._target = target elif instrType == InstrType.params: # get the src src1 = irParts[2] # look into the symbol table for this lexeme src1ST = self._st.lookup(src1) if src1ST == None: if IsInt(src1): src1ST = self._st.insert(src1, "INT") else: print "IR:137:: " + src1 + " not declared in" print irLine # update the tac tac._src1ST = src1ST elif instrType == InstrType.call: # get the return val if len(irParts) >= 4: dest = irParts[3] # look into the symbol table for this lexeme destST = self._st.lookup(dest) if destST == None: destST = self._st.insert(dest, "ID") # get the target label target = irParts[2] # update the tac tac._destST = destST tac._target = target elif instrType == InstrType.ret: # get the return val src1 = irParts[2] # look into the symbol table for this lexeme src1ST = self._st.lookup(src1) if src1ST == None: if IsInt(src1): src1ST = self._st.insert(src1, "INT") else: print src1 + " not declared in" print irLine # update the tac tac._src1ST = src1ST elif instrType == InstrType.libFn: # get the return val if len(irParts) >= 3: dest = irParts[2] # look into the symbol table for this lexeme destST = self._st.lookup(dest) if destST == None: destST = self._st.insert(dest, "ID") # get the target label target = irParts[1] # update the tac tac._destST = destST tac._target = target tac._typ = InstrType.libFn self._tac.append(tac)
def emitExit(self): tac = TAC(InstrType.libFn, "exit") tac.target = "exit" self.addTac(tac, "exit")