def printlabelname(i, flag, fgl):
    if i in g.marker:
        g.debug("Check It:- " + str(i))
        if (g.splitins[i].lbl == True):
            if (flag == 1):
                print("_exit:")
                SaveContext()
                out("PU", 0)
                out("CA", "fflush")
                out("M", 1, "%eax")
                out("M", 0, "%ebx")
                out("int", "$0x80")
                fgl = 1
                flag = 0

            print("\n" + g.splitins[i].lblname + ":")
            # g.debug(str(g.splitins[i].paramlist[0][0]))
            out("PO", "tempretaddr")
            for j in range(0, len(g.splitins[i].paramlist)):
                k = len(g.splitins[i].paramlist) - j - 1
                out("PO", g.splitins[i].paramlist[k][0])
            out("PU", "tempretaddr")
            out("PU", "%ebp")
            out("M", "%esp", "%ebp")
        else:
            SaveContext()
            print("\n" + str(g.splitins[i].lblname) + ":")
    return flag, fgl
def PUSH(line):
    i = line
    var = isregassigned(g.splitins[i].dst)
    g.debug("func::push--var=" + str(var))
    if (var != '-1'):
        out("PU", regname(var))
    else:
        out("PU", g.splitins[i].dst)
def print_functions():
    print(".section .text")
    print(" ")
    print("\t.global _start")
    g.debug(g.marker)
    for i in g.marker:
        if g.splitins[i].lbl == True and g.splitins[i].lblname != "u_main0":
            print(".type " + g.splitins[i].lblname + " , @function\n")
Beispiel #4
0
def emptyreg(lineno, regno):
    if (g.regalloc[regno] == '-1'):
        g.regalloc[regno] = '0DNA'
        return True
    varsinline = []
    varsinline.append(g.splitins[lineno - 1].src1)
    varsinline.append(g.splitins[lineno - 1].src2)
    varsinline.append(g.splitins[lineno - 1].dst)
    for i in g.regalloc:
        if g.regalloc[isregassigned(i)] != '0DNA' and i not in g.nextuse[
                lineno - 1].keys() and i not in varsinline:
            if (i != '-1'):
                g.debug("empline no: " + str(lineno) + " movl  " +
                        str(regname(isregassigned(i)) + "," + str(i)))
                print("movl  " +
                      str(regname(isregassigned(i)) + " , " + str(i)))
                g.debug("empline no: " + str(lineno) + " movl " +
                        regname(regno) + "," + str(regname(isregassigned(i))))
                print("movl " + regname(regno) + " , " +
                      str(regname(isregassigned(i))))
                g.regalloc[isregassigned(i)] = g.regalloc[regno]
                g.regalloc[regno] = '0DNA'
            else:
                g.debug("empline no: " + str(lineno) + " movl " +
                        regname(regno) + "," + str(regname(isregassigned(i))))
                print("movl " + regname(regno) + " , " +
                      str(regname(isregassigned(i))))
                g.regalloc[isregassigned(i)] = g.regalloc[regno]
                g.regalloc[regno] = '0DNA'
            # print( "line no: "+str(lineno)+ " movl  "+str(regname(isregassigned(i))+","+str(i)))
            # regtoassign=isregassigned(i)
            # regalloc[regtoassign]='0DNA'
            return True
    tempvar = None
    tempnextuse = -1
    for j in range(0, 6):
        if (g.regalloc[j] != '0DNA'):
            if g.regalloc[j] in varsinline:
                continue
            i = g.regalloc[j]
            if (tempnextuse == -1):
                tempvar = i
                tempnextuse = g.nextuse[lineno - 1][i]
            elif (tempnextuse < g.nextuse[lineno - 1][i]):
                tempvar = i
                tempnextuse = g.nextuse[lineno - 1][i]
    g.debug("empline no: " + str(lineno) + "  movl " +
            str(regname(isregassigned(tempvar)) + "," + str(tempvar)))
    print("movl " +
          str(regname(isregassigned(tempvar)) + " , " + str(tempvar)))
    g.debug("empline no: " + str(lineno) + " movl " + regname(regno) + "," +
            str(regname(isregassigned(tempvar))))
    print("movl " + regname(regno) + " , " +
          str(regname(isregassigned(tempvar))))
    g.regalloc[isregassigned(tempvar)] = g.regalloc[regno]
    g.regalloc[regno] = '0DNA'
    return True
def SUB(line):
    i = line
    if (isInt(g.splitins[i].src1) or isInt(g.splitins[i].src2)):
        if (isInt(g.splitins[i].src1) and isInt(g.splitins[i].src2)):
            a = regs(i, g.splitins[i].dst)
            out("M", g.splitins[i].src1, a)
            out("S", g.splitins[i].src2, a)
        elif (isInt(g.splitins[i].src1)):
            if (g.splitins[i].dst != g.splitins[i].src2):
                b = regs(i, g.splitins[i].src2)
                a = regs(i, g.splitins[i].dst)
                out("M", 0, a)
                out("S", b, a)
                out("A", g.splitins[i].src1, a)
            else:
                a = regs(i, g.splitins[i].dst)
                tmp = regs(i, "tempac5")
                out("M", a, tmp)
                out("M", g.splitins[i].src1, a)
                out("S", tmp, a)
                g.regalloc[isregassigned('tempac5')] = '-1'
        else:
            g.debug("something is fishyd")
            if (g.splitins[i].dst != g.splitins[i].src1):
                b = regs(i, g.splitins[i].src1)
                a = regs(i, g.splitins[i].dst)
                out("M", b, a)
                out("S", g.splitins[i].src2, a)
            else:
                a = regs(i, g.splitins[i].dst)
                out("S", g.splitins[i].src2, a)
    else:
        if (g.splitins[i].dst != g.splitins[i].src1
                and g.splitins[i].dst != g.splitins[i].src2):
            b = regs(i, g.splitins[i].src1)
            c = regs(i, g.splitins[i].src2)
            a = regs(i, g.splitins[i].dst)
            out("M", b, a)
            out("S", c, a)
        elif (g.splitins[i].dst == g.splitins[i].src1
              and g.splitins[i].dst != g.splitins[i].src2):
            c = regs(i, g.splitins[i].src2)
            a = regs(i, g.splitins[i].dst)
            out("S", c, a)
        elif (g.splitins[i].dst != g.splitins[i].src1
              and g.splitins[i].dst == g.splitins[i].src2):
            b = regs(i, g.splitins[i].src1)
            a = regs(i, g.splitins[i].dst)
            out("S", a, b)
            out("A", b, a)
            out("X", a, b)
        elif (g.splitins[i].dst == g.splitins[i].src1
              and g.splitins[i].dst == g.splitins[i].src2):
            a = regs(i, g.splitins[i].dst)
            out("S", a, a)
def convertassem():
    flag = 1
    fgl = 0
    #Create data section of Assembly Code
    createdatasection()
    g.debug(g.marker)
    # UNCOMMENTED
    updatejumpttrgt()
    for i in range(len(g.splitins)):
        flag, fgl = printlabelname(i, flag, fgl)
        if (g.splitins[i].op == '='):
            EQUAL(i)
        elif (g.splitins[i].op == '+'):
            ADD(i)
        elif (g.splitins[i].op == '-'):
            SUB(i)
        elif (g.splitins[i].op == '*'):
            MULL(i)
        elif (g.splitins[i].op == '/'):
            DIVIDE(i)
        elif (g.splitins[i].op == '%'):
            MOD(i)
        elif (g.splitins[i].op == 'ifgoto'):
            IFGOTO(i)
        elif (g.splitins[i].op == 'and'):
            AND(i)
        elif (g.splitins[i].op == 'or'):
            OR(i)
        elif (g.splitins[i].op == 'xor'):
            XOR(i)
        elif (g.splitins[i].op == 'not'):
            NOT(i)
        elif (g.splitins[i].func == True):
            FUNC(i)
        elif (g.splitins[i].returnc == True):
            RET(i)
        elif (g.splitins[i].lbl == True):
            continue
        elif (g.splitins[i].inputc == True):
            INPUT(i)
        elif (g.splitins[i].printc == True):
            PRINT(i)
        else:
            #Only for debugging
            g.splitins[i].printobj()
            raise ValueError("INVALID MODE:- Don't You know I m Idiot?")
    if (fgl == 0):
        print("_exit:")
        SaveContext()
        out("M", 1, "%eax")
        out("M", 0, "%ebx")
        out("int", "$0x80")
def RET(line):
    SaveContext()
    out("M", "%ebp", "%esp")
    out("PO", "%ebp")
    i = line
    if (g.splitins[i].dst is not None):
        out("PO", "tempretaddr")
        var = isregassigned(g.splitins[i].dst)
        g.debug("func::push--var=" + str(var))
        if (var != '-1'):
            out("PU", regname(var))
        else:
            out("PU", g.splitins[i].dst)
        out("PU", "tempretaddr")
    out("R")
 def convert(self, param):
     # print(param)
     if(len(param)==1):
         return 0
     self.lineno=param[0]
     self.op=param[1]
     # print(param)
     if (param[1]=="ifgoto"):
         self.jmp=True
         self.cmpl=True
         self.cmpltype=param[2]
         self.src1,self.src1index=varname(param[3])
         self.src2,self.src2index=varname(param[4])
         self.jlno=param[5]
         g.basicblock.append(int(self.lineno))
         g.basicblock.append(int(self.jlno)-1)
         # g.splitins[i].jlno
         g.marker.append(int(self.jlno)-1)
     elif (param[1]=="call"):
         g.basicblock.append(int(self.lineno))
         g.basicblock.append(int(self.lineno)+1)
         self.func=True
         self.funcname="u_"+param[2]
     elif (param[1]=="ret"):
         self.returnc=True
     elif (param[1]=="label"):
         # print("i m here")
         # g.basicblock.append(int(self.lineno))
         g.marker.append(int(self.lineno)-1)
         self.lbl=True
         self.lblname="u_"+param[2]
     elif (param[1]=="print"):
         g.debug("printer")
         self.printc=True
         self.src1,self.src1index=varname(param[2])
     elif (param[1]=="input"):
         self.inputc=True
         self.src1,self.src1index=varname(param[2])
     elif (param[1]=="=" or param[1]=='not'):
         self.dst,self.dstindex=varname(param[2])
         self.src1,self.src1index=varname(param[3])
         # g.variables.append(varname(param[2]))
         # g.variables.append(varname(param[3]))
     else:
         # print(param)
         self.dst,self.dstindex=varname(param[2])
         self.src1,self.src1index=varname(param[3])
         self.src2,self.src2index=varname(param[4])
def printlabelname(i, flag, fgl):
    if i in g.marker:
        g.debug("Check It:- " + str(i))
        if (g.splitins[i].lbl == True):
            if (flag == 1):
                print("_exit:")
                SaveContext()
                out("M", 1, "%eax")
                out("M", 0, "%ebx")
                out("int", "$0x80")
                fgl = 1
            flag = 0
            print("\n" + g.splitins[i].lblname + ":")
            out("PU", "%ebp")
            out("M", "%esp", "%ebp")
        else:
            print("\n" + str(g.splitins[i].lblname) + ":")
    return flag, fgl
Beispiel #10
0
def codegen(infile):
    #ebx=1 ,ecx=2, esi=3, edi=4, eax=5, edx=6
    i=0
    filename = infile
    f = open(filename, 'r')
    data = f.read()
    lines=data.split('\n')
    g.splitins2 = []
    for i in lines:
        f=i.split(",")
        g.splitins2.append(f)
    g.splitins=[]
    x=[]
    for l in g.splitins2:
        x=[]
        for i in l:
            i=i.strip(" ")
            x.append(i)
        temp=instruction()
        temp.convert(x)
        if(len(x)!=1):
            g.splitins.append(temp)
    g.basicblock.append(len(g.splitins))
    # for i in g.splitins:
    #     i.printobj()
    unique = set(g.variables)
    g.variables = list(unique)
    unique2=set(g.basicblock)
    g.basicblock = list(unique2)
    unique3 = set(g.marker)
    g.marker = list(unique3)
    g.basicblock.sort()
    g.debug(g.basicblock)
    g.debug(g.marker)
    build_nextusetable()
    g.debug("*********************************************************************************")
    for i in g.nextuse:
        g.debug(i)
    g.debug("*********************************************************************************")
    convertassem()
Beispiel #11
0
def createdatasection():
    print "\n"
    tempvar = [
        'tempac1', 'tempac2', 'tempac3', 'tempac4', 'tempac5', 'tempac6',
        'tempretaddr'
    ]
    print(".section .data")
    # strIO="format_input:\n\t.ascii \"%d\\0\"\nformat_output:\n \t.ascii \"%d\\n\\0\"\nL_INPUT:\n\t.long 0"
    # print(strIO)
    for i in g.printstrings:
        # g.debug("data"+i[0])
        g.debug(i)
        print(i[0] + ":\n\t.ascii " + i[1] + "\n")
    print("format_input:\n\t.ascii \"%d\\0\"\n" + "L_INPUT:\n\t.long 0\n")
    # print("array_block:")
    # print("\t.fill 100")
    g.debug(g.variables)
    for i in g.variables:
        g.debug(i)
        if (isInt(i)):
            continue
        print(str(i[0]) + ":")
        if (i[1] == 0):
            print("\t.long 0")
        else:
            print("\t.space " + str(4 * int(i[1])))
    for i in tempvar:
        print(str(i) + ":")
        print("\t.long 0")
    # print("returnval:")
    # print("\t.long 0")
    # print("returnaddr:")
    # print("\t.long 0")
    print(".section .data")
    print(" ")
Beispiel #12
0
def PRINT(line):
    i = line
    SaveContext()
    # if(not isInt(g.splitins[i].src1)):
    #     inp=regs(i,g.splitins[i].src1)
    g.debug("print line strs ::" + str(g.splitins[i].paramlist))
    for j in range(1, len(g.splitins[i].paramlist)):
        g.debug("j::" + str(j))
        g.debug("list " + str(g.splitins[i].paramlist[j]))
        k = len(g.splitins[i].paramlist) - j
        if (g.splitins[i].paramlist[k][1] == None):
            out("PU", g.splitins[i].paramlist[k][0])  #pushing in opp dir
    # out("PU",inp)
    # out("PU","$format_output")
    out("PU", g.splitins[i].paramlist[0])
    out("CA", "printf")
Beispiel #13
0
def MOD(line):
    i = line
    if (isInt(g.splitins[i].src1) or isInt(g.splitins[i].src2)):
        if (isInt(g.splitins[i].src1) and isInt(g.splitins[i].src2)):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            out("M", g.splitins[i].src1, a)
            emptyreg(i, 5)
            out("M", 0, "%edx")
            tmp = regs(i, "$" + str(g.splitins[i].src2))
            out("M", g.splitins[i].src2, tmp)
            out("CD")
            out("D", tmp)
            out("M", "%edx", a)
            g.regalloc[isregassigned("$" + str(g.splitins[i].src2))] = '-1'
            g.regalloc[5] = '-1'
        elif (isInt(g.splitins[i].src1)
              and (g.splitins[i].src2 != g.splitins[i].dst)):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            out("M", g.splitins[i].src1, a)
            emptyreg(i, 5)
            out("M", 0, "%edx")
            b = regs(i, g.splitins[i].src2)
            out("CD")
            out("D", b)
            out("M", "%edx", a)
            g.regalloc[5] = '-1'
        elif (isInt(g.splitins[i].src1)
              and (g.splitins[i].src2 == g.splitins[i].dst)):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            out("M", 0, "%edx")
            c = regs(i, 'tempac3')
            out("M", a, c)
            out("M", g.splitins[i].src1, a)
            out("CD")
            out("D", c)
            out("M", "%edx", a)
            g.regalloc[isregassigned('tempac3')] = '-1'
            g.regalloc[5] = '-1'
        else:
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            out("M", 0, "%edx")
            b = regs(i, g.splitins[i].src1)
            g.debug(str(g.splitins[i].src2))
            g.debug(str(g.splitins[i].src1))
            tmp = regs(i, "$" + str(g.splitins[i].src2))
            out("M", b, a)
            out("M", g.splitins[i].src2, tmp)
            out("CD")
            out("D", tmp)
            out("M", "%edx", a)
            g.regalloc[isregassigned("$" + str(g.splitins[i].src2))] = '-1'
            g.regalloc[5] = '-1'
    else:
        if (g.splitins[i].dst != g.splitins[i].src1
                and g.splitins[i].dst != g.splitins[i].src2):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            a = '%eax'
            # print("a..... =" + str(a))
            out("M", 0, "%edx")
            b = regs(i, g.splitins[i].src1)
            c = regs(i, g.splitins[i].src2)
            out("M", b, a)
            out("CD")
            out("D", c)
            out("M", "%edx", a)
            g.regalloc[5] = '-1'
        elif (g.splitins[i].dst == g.splitins[i].src1
              and g.splitins[i].dst != g.splitins[i].src2):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            out("M", 0, "%edx")
            c = regs(i, g.splitins[i].src2)
            out("CD")
            out("D", c)
            out("M", "%edx", a)
            g.regalloc[5] = '-1'
        elif (g.splitins[i].dst != g.splitins[i].src1
              and g.splitins[i].dst == g.splitins[i].src2):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            out("M", 0, "%edx")
            b = regs(i, g.splitins[i].src1)
            c = regs(i, 'tempac4')
            out("M", a, c)
            out("M", b, a)
            out("CD")
            out("D", c)
            out("M", "%edx", a)
            g.regalloc[isregassigned('tempac4')] = '-1'
            g.regalloc[5] = '-1'
        elif (g.splitins[i].dst == g.splitins[i].src1
              and g.splitins[i].dst == g.splitins[i].src2):
            a = regs(i, g.splitins[i].dst)
            if (g.regalloc[4] == '-1'):
                out("M", a, "%eax")
                g.regalloc[isregassigned(g.splitins[i].dst)] = '-1'
                g.regalloc[4] = g.splitins[i].dst
            else:
                if (a != '%eax'):
                    tmp = g.regalloc[4]
                    tmp2 = isregassigned(g.splitins[i].dst)
                    out("X", a, "%eax")
                    g.regalloc[tmp2] = tmp
                    g.regalloc[4] = g.splitins[i].dst
            a = '%eax'
            emptyreg(i, 5)
            out("M", 0, "%edx")
            out("CD")
            out("D", a)
            out("M", "%edx", a)
            g.regalloc[5] = '-1'
Beispiel #14
0
def updatejumpttrgt():
    for k in g.marker:
        if (g.splitins[k].lbl == False):
            # g.splitins[k-1].lbl=True
            g.debug("Lets see: " + str(k))
            g.splitins[k].lblname = "l_" + g.splitins[k].lineno
Beispiel #15
0
def print_functions():
    g.debug(g.marker)
    for i in g.marker:
        if g.splitins[i].lbl == True:
            print(".type " + g.splitins[i].lblname + " , @function\n")
Beispiel #16
0
def IFGOTO(line):
    i = line
    inst = g.splitins[i]
    # SaveContext()
    for i in range(0, 6):
        g.debug(i)
        if (g.regalloc[i] != '-1'):
            g.debug("i dont know why?")
    if (not isInt(inst.src1) and isInt(inst.src2)):
        inst = revert(inst)
        #read a
        a = "$" + str(inst.src1)
        g.debug(a)
        #read b if needed
        b = regs(i, inst.src2)
        g.debug(b)
    elif (isInt(inst.src1) and isInt(inst.src2)):
        if (isInt(inst.src1)):
            a = "$" + str(inst.src1)
        g.debug(a)
        b = regs(i, "$" + str(inst.src2))
        g.debug(b)
    else:
        #read a if needed
        if (isInt(inst.src1)):
            a = "$" + str(inst.src1)
        else:
            a = regs(i, inst.src1)
        g.debug(a)
        #read b if needed
        if (isInt(inst.src2)):
            b = "$" + str(inst.src2)
        else:
            b = regs(i, inst.src2)
        g.debug(b)

    out('C', a, b)
    if (isInt(inst.src1) and isInt(inst.src2)):
        g.regalloc[isregassigned("$" + str(inst.src2))] = '-1'

    if (isInt(inst.jlno)):
        label = "l_" + str(inst.jlno)
    else:
        label = "u_" + str(inst.jlno)
    #Save Context before jump
    SaveContext()
    if (inst.cmpltype == 'eq'):
        out("JE", label)
    elif (inst.cmpltype == 'leq'):
        out("JLE", label)
    elif (inst.cmpltype == 'geq'):
        out("JGE", label)
    elif (inst.cmpltype == 'g'):
        out("JG", label)
    elif (inst.cmpltype == 'l'):
        out("JL", label)
    else:
        #For debugging
        raise ValueError("INVALID LABEL:-  IFGOTO() in file assemblygen.py")
Beispiel #17
0
def convertassem():
    flag = 0
    fgl = 0
    #Create data section of Assembly Code
    g.debug(g.marker)
    # UNCOMMENTED
    # updatejumpttrgt()
    print_functions()
    for i in range(len(g.splitins)):
        if (g.splitins[i].lblname == "u_main0"):
            print "\n_start:"
            flag = 1
        elif (g.splitins[i].op == 'func'):
            flag, fgl = printlabelname(i, flag, fgl)
        elif (g.splitins[i].op == 'label'):
            SaveContext()
            print("\n" + str(g.splitins[i].lblname) + ":")
        elif (g.splitins[i].op == '='):
            EQUAL(i)
        elif (g.splitins[i].op == '+'):
            ADD(i)
        elif (g.splitins[i].op == '-'):
            SUB(i)
        elif (g.splitins[i].op == '*'):
            MULL(i)
        elif (g.splitins[i].op == '/'):
            DIVIDE(i)
        elif (g.splitins[i].op == '%'):
            MOD(i)
        elif (g.splitins[i].op == 'ifgoto'):
            IFGOTO(i)
        elif (g.splitins[i].op == "goto"):
            GOTO(i)
        elif (g.splitins[i].op == 'and'):
            AND(i)
        elif (g.splitins[i].op == 'or'):
            OR(i)
        elif (g.splitins[i].op == 'xor'):
            XOR(i)
        elif (g.splitins[i].op == 'not'):
            NOT(i)
        elif (g.splitins[i].op == 'push'):
            PUSH(i)
        elif (g.splitins[i].op == 'pop'):
            POP(i)
        elif (g.splitins[i].func == True):
            FUNC(i)
        elif (g.splitins[i].returnc == True):
            RET(i)
        elif (g.splitins[i].lbl == True):
            continue
        elif (g.splitins[i].inputc == True):
            INPUT(i)
        elif (g.splitins[i].printc == True):
            PRINT(i)
        elif (g.splitins[i].op == 'declare'):
            continue
        else:
            #Only for debugging
            g.splitins[i].printobj()
            raise ValueError("INVALID MODE:- Don't You know I m Idiot?")
    if (fgl == 0):
        print("_exit:")
        SaveContext()
        out("PU", 0)
        out("CA", "fflush")
        out("M", 1, "%eax")
        out("M", 0, "%ebx")
        out("int", "$0x80")
    # print("called createdatasection")
    createdatasection()
Beispiel #18
0
 def printobj(self):
    g.debug("line no: "+self.lineno)
    g.debug("op: "+self.op)
    g.debug("dst: "+str(self.dst))
    g.debug("src1: "+str(self.src1))
    g.debug("src2: "+str(self.src2))
    g.debug("jmp: "+str(self.jmp))
    g.debug("cmpl: "+str(self.cmpl))
    g.debug("cmpltype: "+str(self.cmpltype))
    g.debug("jlno: "+str(self.jlno))
    g.debug("lbl: "+str(self.lbl))
    g.debug("lblname: "+str(self.lblname))
    g.debug("func: "+str(self.func))
    g.debug("funcname: "+str(self.funcname))
    g.debug("print: "+str(self.printc))
    g.debug("input: "+str(self.inputc))
    g.debug("return: "+str(self.returnc))
    g.debug("\n")
Beispiel #19
0
 def convert(self, param):
     # print(param)
     if (len(param) == 1):
         return 0
     self.lineno = param[0]
     self.op = param[1]
     # print(param)
     if (param[1] == "ifgoto"):
         self.jmp = True
         self.cmpl = True
         self.cmpltype = param[2]
         self.src1, self.src1index = varname(param[3])
         self.src2, self.src2index = varname(param[4])
         self.jlno = param[5]
         g.basicblock.append(int(self.lineno))
         # g.basicblock.append(int(self.jlno)-1)
         # g.splitins[i].jlno
         # g.marker.append(int(self.jlno)-1)
     elif (param[1] == "goto"):
         self.jmp = True
         self.jlno = "l_" + param[2]
         g.basicblock.append(int(self.lineno))
     elif (param[1] == "call"):
         g.basicblock.append(int(self.lineno))
         if (int(self.lineno) <= len(g.splitins)):
             g.basicblock.append(int(self.lineno) + 1)
         self.func = True
         self.funcname = "u_" + param[2]
     elif (param[1] == "ret"):
         g.debug("instruction.py :: ret" + str(param))
         if (len(param) == 3):
             self.dst, self.dstindex = varname(param[2])
         else:
             self.dst = None
         self.returnc = True
     elif (param[1] == "func"):
         # print("i m here")
         # g.basicblock.append(int(self.lineno))
         g.marker.append(int(self.lineno) - 1)
         for i in range(3, len(param)):
             self.paramlist.append(varname(param[i]))
         self.lbl = True
         self.lblname = "u_" + param[2]
     elif (param[1] == "label"):
         # print("i m here")
         g.basicblock.append(int(self.lineno) - 1)
         g.marker.append(int(self.lineno) - 1)
         self.lbl = False
         self.lblname = "l_" + param[2]
     elif (param[1] == "print"):
         g.debug("printer")
         self.printc = True
         self.paramlist = []
         g.printstrings.append(
             ["str_" + str(self.lineno),
              str(param[2][:-1] + "\\0\"")])
         self.paramlist.append("$str_" + str(self.lineno))
         # g.debug("len of param :: "+str(len(param)))
         for i in range(3, len(param)):
             temp, tempindex = varname(param[i])
             self.paramlist.append([temp, tempindex])
         g.debug("print line :: " + str(self.paramlist))
         g.debug("string line :: " + str(g.printstrings))
         # self.src1,self.src1index=varname(param[2])
     elif (param[1] == "input"):
         self.inputc = True
         self.src1, self.src1index = varname(param[2])
     elif (param[1] == "=" or param[1] == 'not'):
         self.dst, self.dstindex = varname(param[2])
         self.src1, self.src1index = varname(param[3])
         # g.variables.append(varname(param[2]))
         # g.variables.append(varname(param[3]))
     elif (param[1] == "push" or param[1] == "pop"):
         self.dst, self.dstindex = varname(param[2])
     elif (param[1] == "declare"):
         g.variables.append(("v_" + param[2], param[3]))
     else:
         # print(param)
         self.dst, self.dstindex = varname(param[2])
         self.src1, self.src1index = varname(param[3])
         self.src2, self.src2index = varname(param[4])