Example #1
0
 def on_first_call(self, wrapped_function):
     old_block = get_tape().active_basicblock
     parent_node = get_tape().req_node
     get_tape().open_scope(lambda x: x[0], None, 'begin-' + self.name)
     block = get_tape().active_basicblock
     block.alloc_pool = defaultdict(set)
     del parent_node.children[-1]
     self.node = get_tape().req_node
     print 'Compiling function', self.name
     result = wrapped_function(*self.compile_args)
     if result is not None:
         self.result = memorize(result)
     else:
         self.result = None
     print 'Done compiling function', self.name
     p_return_address = get_tape().program.malloc(1, 'ci')
     get_tape().function_basicblocks[block] = p_return_address
     return_address = regint.load_mem(p_return_address)
     get_tape().active_basicblock.set_exit(
         instructions.jmpi(return_address, add_to_prog=False))
     self.last_sub_block = get_tape().active_basicblock
     get_tape().close_scope(old_block, parent_node, 'end-' + self.name)
     old_block.set_exit(instructions.jmp(0, add_to_prog=False),
                        get_tape().active_basicblock)
     self.basic_block = block
Example #2
0
 def on_call(self, base, bases):
     if base is not None:
         instructions.starg(regint(base))
     block = self.basic_block
     if block not in get_tape().function_basicblocks:
         raise CompilerError('unknown function')
     old_block = get_tape().active_basicblock
     old_block.set_exit(instructions.jmp(0, add_to_prog=False), block)
     p_return_address = get_tape().function_basicblocks[block]
     return_address = get_tape().new_reg('r')
     old_block.return_address_store = instructions.ldint(return_address, 0)
     instructions.stmint(return_address, p_return_address)
     get_tape().start_new_basicblock(name='call-' + self.name)
     get_tape().active_basicblock.set_return(old_block, self.last_sub_block)
     get_tape().req_node.children.append(self.node)
     if self.result is not None:
         return unmemorize(self.result)
Example #3
0
 def on_call(self, base, bases):
     if base is not None:
         instructions.starg(regint(base))
     block = self.basic_block
     if block not in get_tape().function_basicblocks:
         raise CompilerError('unknown function')
     old_block = get_tape().active_basicblock
     old_block.set_exit(instructions.jmp(0, add_to_prog=False), block)
     p_return_address = get_tape().function_basicblocks[block]
     return_address = get_tape().new_reg('ci')
     old_block.return_address_store = instructions.ldint(return_address, 0)
     instructions.stmint(return_address, p_return_address)
     get_tape().start_new_basicblock(name='call-' + self.name)
     get_tape().active_basicblock.set_return(old_block, self.last_sub_block)
     get_tape().req_node.children.append(self.node)
     if self.result is not None:
         return unmemorize(self.result)
Example #4
0
def end_if():
    try:
        state = instructions.program.curr_tape.if_states.pop()
    except IndexError:
        raise CompilerError('No open if/else block')
    branch = instructions.jmpeqz(regint.conv(state.condition), 0, \
                                     add_to_prog=False)
    # start next block
    get_tape().close_scope(state.start_block, state.req_child.parent, 'end-if')
    if state.has_else:
        # jump to else block if condition == 0
        state.start_block.set_exit(branch, state.else_block)
        # set if block to skip else
        jump = instructions.jmp(0, add_to_prog=False)
        state.if_exit_block.set_exit(jump, instructions.program.curr_block)
    else:
        # set start block's conditional jump to next block
        state.start_block.set_exit(branch, instructions.program.curr_block)
        # nothing to compute without else
        state.req_child.aggregator = lambda x: x[0]
Example #5
0
def end_if():
    try:
        state = instructions.program.curr_tape.if_states.pop()
    except IndexError:
        raise CompilerError('No open if/else block')
    branch = instructions.jmpeqz(regint.conv(state.condition), 0, \
                                     add_to_prog=False)
    # start next block
    get_tape().close_scope(state.start_block, state.req_child.parent, 'end-if')
    if state.has_else:
        # jump to else block if condition == 0
        state.start_block.set_exit(branch, state.else_block)
        # set if block to skip else
        jump = instructions.jmp(0, add_to_prog=False)
        state.if_exit_block.set_exit(jump, instructions.program.curr_block)
    else:
        # set start block's conditional jump to next block
        state.start_block.set_exit(branch, instructions.program.curr_block)
        # nothing to compute without else
        state.req_child.aggregator = lambda x: x[0]
Example #6
0
 def on_first_call(self, wrapped_function):
     old_block = get_tape().active_basicblock
     parent_node = get_tape().req_node
     get_tape().open_scope(lambda x: x[0], None, 'begin-' + self.name)
     block = get_tape().active_basicblock
     block.alloc_pool = defaultdict(set)
     del parent_node.children[-1]
     self.node = get_tape().req_node
     print 'Compiling function', self.name
     result = wrapped_function(*self.compile_args)
     if result is not None:
         self.result = memorize(result)
     else:
         self.result = None
     print 'Done compiling function', self.name
     p_return_address = get_tape().program.malloc(1, 'ci')
     get_tape().function_basicblocks[block] = p_return_address
     return_address = regint.load_mem(p_return_address)
     get_tape().active_basicblock.set_exit(instructions.jmpi(return_address, add_to_prog=False))
     self.last_sub_block = get_tape().active_basicblock
     get_tape().close_scope(old_block, parent_node, 'end-' + self.name)
     old_block.set_exit(instructions.jmp(0, add_to_prog=False), get_tape().active_basicblock)
     self.basic_block = block