Ejemplo n.º 1
0
    def FramePop(self, param_count, scope_depth):
        p = param_count
        code = [
            '',
            #x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            #x.pushl(x.static('display', x.ebx)),
            #x.pushl('$pop1_msg'),
            #x.call("printf"),
            #x.addl(x.cint(8), x.esp),

            x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            x.movl(x.mem(x.ebp, -(7+p)*4), x.eax),
            x.movl(x.eax, x.static('display', x.ebx)),

            #x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            #x.pushl(x.static('display', x.ebx)),
            #x.pushl('$pop2_msg'),
            #x.call("printf"),
            #x.addl(x.cint(8), x.esp),

            x.movl(x.mem(x.ebp, -(6+p)*4), x.edx),
            x.movl(x.mem(x.ebp, -(5+p)*4), x.ecx),
            x.movl(x.mem(x.ebp, -(4+p)*4), x.esi),
            x.movl(x.mem(x.ebp, -(3+p)*4), x.edi),
            x.movl(x.mem(x.ebp, -(2+p)*4), x.ebx),
            x.movl(x.ebp, x.esp),                # restore stack pointer
            x.movl(x.mem(x.esp, -(1+p)*4), x.ebp),   # restore base(frame) pointer
        ]
        return code
Ejemplo n.º 2
0
 def FramePop(self, param_count, scope_depth):
     """Generate a framepop.
     @param param_count : the number of params the function recieves.
     @param scope_depth : the scope the depth the function was declared at.
     """
     p = param_count
     code = [
         "",
         # x.movl(x.cint(4*(scope_depth-1)), x.ebx),
         # x.pushl(x.static('display', x.ebx)),
         # x.pushl('$pop1_msg'),
         # x.call("printf"),
         # x.addl(x.cint(8), x.esp),
         x.movl(x.cint(4 * (scope_depth - 1)), x.ebx),
         x.movl(x.mem(x.ebp, -(7 + p) * 4), x.eax),
         x.movl(x.eax, x.static("display", x.ebx)),
         # x.movl(x.cint(4*(scope_depth-1)), x.ebx),
         # x.pushl(x.static('display', x.ebx)),
         # x.pushl('$pop2_msg'),
         # x.call("printf"),
         # x.addl(x.cint(8), x.esp),
         x.movl(x.mem(x.ebp, -(6 + p) * 4), x.edx),
         x.movl(x.mem(x.ebp, -(5 + p) * 4), x.ecx),
         x.movl(x.mem(x.ebp, -(4 + p) * 4), x.esi),
         x.movl(x.mem(x.ebp, -(3 + p) * 4), x.edi),
         x.movl(x.mem(x.ebp, -(2 + p) * 4), x.ebx),
         x.movl(x.ebp, x.esp),  # restore stack pointer
         x.movl(x.mem(x.esp, -(1 + p) * 4), x.ebp),  # restore base(frame) pointer
     ]
     return code
Ejemplo n.º 3
0
 def Oprm(self, i):
     #print i
     code = [
         x.movl(x.mem(x.esp, i.b.type.offset), x.eax),
         x.movl(x.eax, x.mem(x.esp, -(1+i.a)*4)),
     ]
     return code
Ejemplo n.º 4
0
 def InitCode(self):
     """The pre-amble to the generated code. Contains data and the _start
     label. Creates a new stack frame."""
     return [
         ".section .data",
         "printf_msg:",
         r'  .ascii "%d\n\0"',
         ## START DEBUG MESSAGES ##
         "push_msg:",
         r'  .ascii "push 0x%x\n\0"',
         "pop1_msg:",
         r'  .ascii "pop1 0x%x\n\0"',
         "pop2_msg:",
         r'  .ascii "pop2 0x%x\n\0"',
         ## END DEBUG MESSAGES ##
         ## The display stores the location of non-local stack frames
         ## see. def emit(...)
         "display:",
         r"  .long %s" % ", ".join("0" for x in xrange(self.table.max_depth)),
         "",
         ".section .text",
         ".global _start",
         x.label("_start"),
         x.movl(x.esp, x.ebp),
         x.movl(x.ebp, "0(%ebp)"),
         #'.global main',
         # x86.label('main'),
     ]
Ejemplo n.º 5
0
 def Iprm(self, i):
     """Generator code for IPRM : Put an Input Param on the Stack"""
     if isinstance(i.b.type, il.Func):
         # this is a function param not a value
         code = [x.leal(i.b.type.entry, x.eax), x.movl(x.eax, x.mem(x.esp, -(2 + i.a) * 4))]
     else:
         code = [x.movl(x.loc(i.b.type), x.eax), x.movl(x.eax, x.mem(x.esp, -(2 + i.a) * 4))]
     return code
Ejemplo n.º 6
0
 def Rprm(self, i):
     #print i
     code = [
         x.movl(x.mem(x.esp, -(2+i.a)*4), x.eax),
         x.movl(x.eax, x.loc(i.result.type)),
     ]
     #self.var[i.result] = self.bp_offset
     #self.bp_offset += 1
     return code
Ejemplo n.º 7
0
 def Iprm(self, i):
     if isinstance(i.b.type, il.Func):
         # this is a function param not a value
         code = [
             x.leal(i.b.type.entry, x.eax),
             x.movl(x.eax, x.mem(x.esp, -(2+i.a)*4)),
         ]
     else:
         code = [
             x.movl(x.loc(i.b.type), x.eax),
             x.movl(x.eax, x.mem(x.esp, -(2+i.a)*4)),
         ]
     return code
Ejemplo n.º 8
0
 def BranchOp(self, i):
     ops = {il.IFEQ:x.je, il.IFNE:x.jne, il.IFLT:x.jl, il.IFLE:x.jle,
            il.IFGT:x.jg, il.IFGE:x.jge}
     code = [
         x.movl(x.loc(i.a.type), x.eax),
         x.cmpl(x.loc(i.b.type), x.eax),
         ops[i.op](i.result.name)
     ]
     #for i in code:
         #print i
     return code
Ejemplo n.º 9
0
 def InitCode(self):
     return [
         '.section .data',
         'printf_msg:',
         r'  .ascii "%d\n\0"',
         'push_msg:',
         r'  .ascii "push 0x%x\n\0"',
         'pop1_msg:',
         r'  .ascii "pop1 0x%x\n\0"',
         'pop2_msg:',
         r'  .ascii "pop2 0x%x\n\0"',
         'display:',
         r'  .long %s' % ', '.join('0' for x in xrange(self.table.max_depth)),
         '',
         '.section .text',
         '.global _start',
         x.label('_start'),
         x.movl(x.esp, x.ebp),
         x.movl(x.ebp, '0(%ebp)'),
         #'.global main',
         #x86.label('main'),
     ]
Ejemplo n.º 10
0
 def Op(self, i):
     # print i
     ops = {il.ADD: x.addl, il.SUB: x.subl, il.MUL: x.imull}
     code = []
     if i.op == il.DIV:
         code += [x.movl(x.cint(0), x.edx)]
         code += self.emit(x.movl, i.a, x.eax)
         code += self.emit(x.divl, i.b)
         code += self.emit(x.movl, x.eax, i.result)
     else:
         code += self.emit(x.movl, i.a, x.eax)
         code += self.emit(ops[i.op], i.b, x.eax)
         code += self.emit(x.movl, x.eax, i.result)
     return code
Ejemplo n.º 11
0
    def emit(self, inst, src, targ=None):
        '''emit the specified instruction with src as source operand and targ
            as the target operand. Handles requesting the operands from a non-local
            stack frame as necessary.

            using ebx or ecx is not allowed for either the src or targ.
        '''
        #print inst, src, targ
        if isinstance(src, il.Symbol) and isinstance(targ, il.Symbol):
            raise Exception, 'Cannot emit an instruction with both source and target as symbols'
        elif isinstance(src, il.Symbol) and targ is None:
            if src.islocal(self.cfunc):
                return [ inst(x.loc(src.type)) ]
            return [
                x.movl(x.cint(4*(src.scope_depth-1)), x.ebx),
                x.movl(x.static('display', x.ebx), x.ebx),
                inst(x.mem(x.ebx, src.type.offset)),
            ]
        elif not isinstance(src, il.Symbol) and targ is None:
            return [ inst(src), ]
        elif isinstance(src, il.Symbol) and targ is not None:
            if src.islocal(self.cfunc):
                return [ inst(x.loc(src.type), targ) ]
            return [
                x.movl(x.cint(4*(src.scope_depth-1)), x.ebx),
                x.movl(x.static('display', x.ebx), x.ebx),
                inst(x.mem(x.ebx, src.type.offset), targ),
            ]
        elif isinstance(targ, il.Symbol):
            #print inst, src, targ, targ.type.basereg
            if targ.islocal(self.cfunc):
                return [ inst(src, x.loc(targ.type)) ]
            return [
                x.movl(x.cint(4*(targ.scope_depth-1)), x.ebx),
                x.movl(x.static('display', x.ebx), x.ebx),
                x.movl(x.mem(x.ebx, targ.type.offset), x.ecx),
                inst(src, x.ecx),
                x.movl(x.ecx, x.mem(x.ebx, targ.type.offset)),
            ]
        else:
            return [ inst(src, targ) ]
Ejemplo n.º 12
0
 def Mv(self, i):
     code = [x.movl(x.loc(i.a.type), x.eax), x.movl(x.eax, x.loc(i.result.type))]
     return code
Ejemplo n.º 13
0
    def FramePush(self, param_count, scope_depth):
        p = param_count
        code = [
            # 0(ebp) == return address
            x.movl(x.ebp, x.mem(x.esp, -(1+p)*4)), #     1
            x.movl(x.esp, x.ebp),
            x.movl(x.ebx, x.mem(x.ebp, -(2+p)*4)),  # 2
            x.movl(x.edi, x.mem(x.ebp, -(3+p)*4)),  # 3
            x.movl(x.esi, x.mem(x.ebp, -(4+p)*4)),  # 4
            x.movl(x.ecx, x.mem(x.ebp, -(5+p)*4)),  # 5
            x.movl(x.edx, x.mem(x.ebp, -(6+p)*4)),  # 6
            x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            x.movl(x.static('display', x.ebx), x.eax),
            x.movl(x.eax, x.mem(x.ebp, -(7+p)*4)), #7


            #x.subl(x.cint(8*4), x.esp),
            #x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            #x.pushl(x.static('display', x.ebx)),
            #x.pushl('$push_msg'),
            #x.call("printf"),
            #x.addl(x.cint(8*4 + 8), x.esp),


            x.movl(x.cint(4*(scope_depth-1)), x.ebx),
            x.movl(x.ebp, x.static('display', x.ebx)),
            '',

        ]
        return code
Ejemplo n.º 14
0
 def FramePush(self, param_count, scope_depth):
     """Generate a framepush.
     @param param_count : the number of params the function recieves.
     @param scope_depth : the scope the depth the function was declared at.
     """
     p = param_count
     code = [
         # 0(ebp) == return address
         x.movl(x.ebp, x.mem(x.esp, -(1 + p) * 4)),  #     1
         x.movl(x.esp, x.ebp),
         x.movl(x.ebx, x.mem(x.ebp, -(2 + p) * 4)),  # 2
         x.movl(x.edi, x.mem(x.ebp, -(3 + p) * 4)),  # 3
         x.movl(x.esi, x.mem(x.ebp, -(4 + p) * 4)),  # 4
         x.movl(x.ecx, x.mem(x.ebp, -(5 + p) * 4)),  # 5
         x.movl(x.edx, x.mem(x.ebp, -(6 + p) * 4)),  # 6
         x.movl(x.cint(4 * (scope_depth - 1)), x.ebx),
         x.movl(x.static("display", x.ebx), x.eax),
         x.movl(x.eax, x.mem(x.ebp, -(7 + p) * 4)),  # 7
         # x.subl(x.cint(8*4), x.esp),
         # x.movl(x.cint(4*(scope_depth-1)), x.ebx),
         # x.pushl(x.static('display', x.ebx)),
         # x.pushl('$push_msg'),
         # x.call("printf"),
         # x.addl(x.cint(8*4 + 8), x.esp),
         x.movl(x.cint(4 * (scope_depth - 1)), x.ebx),
         x.movl(x.ebp, x.static("display", x.ebx)),
         "",
     ]
     return code
Ejemplo n.º 15
0
 def Rprm(self, i):
     """Generate code for RPRM : Get a Return Param"""
     code = [x.movl(x.mem(x.esp, -(2 + i.a) * 4), x.eax), x.movl(x.eax, x.loc(i.result.type))]
     return code
Ejemplo n.º 16
0
 def Oprm(self, i):
     """Generator code for OPRM : Output a param"""
     # print i
     code = [x.movl(x.mem(x.esp, i.b.type.offset), x.eax), x.movl(x.eax, x.mem(x.esp, -(1 + i.a) * 4))]
     return code
Ejemplo n.º 17
0
 def Gprm(self, i):
     """Generator code for GPRM : Get Input Param i"""
     code = [x.movl(x.mem(x.ebp, -(1 + i.a) * 4), x.eax), x.movl(x.eax, x.loc(i.result.type))]
     return code
Ejemplo n.º 18
0
    def emit(self, inst, src, targ=None):
        """emit the specified instruction with src as source operand and targ
            as the target operand. Handles requesting the operands from a
            non-local stack frame as necessary.

            using ebx or ecx is not allowed for either the src or targ.
        """

        ## We have several choices to make in order to emit the right inst.
        ## (1) does the inst only refer to symbols local to the current scope?
        ##      (a) if yes: emit the instruction with no changes
        ##      (b) if no: detirmine what changes have to be made.
        ## (2) We now know the instruction is not local. One (but not both) of
        ##     the symbols does not come from this stack frame. That means they
        ##     have to be fetched from the appropriate stack frame. We detirmine
        ##     the appropriate frame to fetch (or put) the sym from(to) by
        ##     consulting the display.
        ## (3) The display is a static array:
        ##
        ##               +------------+------------+------------+------------+
        ##   scope depth | 0          | 1          | 2          | 3          |
        ##               +------------+------------+------------+------------+
        ## frame address | 0x???????? | 0x???????? | 0x???????? | 0x???????? |
        ##               +------------+------------+------------+------------+
        ##
        ##     The 'frame address' contains the address of the the stack frame
        ##     most recently seen function. When that function returns it will
        ##     update the display with the previous address it contained. When
        ##     A new function is called at that scope depth a new address will
        ##     be placed in the display and the old will be saved.
        ##
        ## (4) Therefore to get the non-local symbols we simply need to consult
        ##     the display to find the appropriate frame. Each symbol knows what
        ##     scope-depth it was declared at.
        ##
        ## NB: The symbols store there scope depths starting at 1. Therefore, in
        ##     order to index into the display one must subtract 1 from the
        ##     stored scope depth.
        ##
        ## NB: We multiply by 4 because each address is 4 bytes wide.

        if isinstance(src, il.Symbol) and isinstance(targ, il.Symbol):
            raise Exception, ("Cannot emit an instruction with both source and target as " "symbols")
        elif isinstance(src, il.Symbol) and targ is None:
            if src.islocal(self.cfunc):
                return [inst(x.loc(src.type))]
            return [
                x.movl(x.cint(4 * (src.scope_depth - 1)), x.ebx),  # compute the
                # index into the
                # display and
                # store in ebx
                x.movl(x.static("display", x.ebx), x.ebx),  # get the non-
                # local stack
                # pointer and
                # store in ebx
                inst(x.mem(x.ebx, src.type.offset)),  # emit the inst.
            ]
        elif not isinstance(src, il.Symbol) and targ is None:
            return [inst(src)]
        elif isinstance(src, il.Symbol) and targ is not None:
            if src.islocal(self.cfunc):
                return [inst(x.loc(src.type), targ)]
            return [
                x.movl(x.cint(4 * (src.scope_depth - 1)), x.ebx),
                x.movl(x.static("display", x.ebx), x.ebx),
                inst(x.mem(x.ebx, src.type.offset), targ),
            ]
        elif isinstance(targ, il.Symbol):
            # print inst, src, targ, targ.type.basereg
            if targ.islocal(self.cfunc):
                return [inst(src, x.loc(targ.type))]
            return [
                ## This version is slightly tricky we have to fetch and store
                ## the contents of the variable stored in the non-local stack
                ## frame.
                x.movl(x.cint(4 * (targ.scope_depth - 1)), x.ebx),
                x.movl(x.static("display", x.ebx), x.ebx),
                x.movl(x.mem(x.ebx, targ.type.offset), x.ecx),
                inst(src, x.ecx),
                x.movl(x.ecx, x.mem(x.ebx, targ.type.offset)),
            ]
        else:
            return [inst(src, targ)]
Ejemplo n.º 19
0
 def Gprm(self, i):
     code = [
         x.movl(x.mem(x.ebp, -(1+i.a)*4), x.eax),
         x.movl(x.eax, x.loc(i.result.type)),
     ]
     return code