Exemple #1
0
 def write_asm(self, fp=1, commented=True):
     """Write assembly representation with comments."""
     codes = []
     for i, (op, val) in enumerate(self.lines):
         if i in self.label_map.values():
             label_str = u'L%s:' % _unicode(i)
             codes.append(padding(label_str, 8))
         else:
             codes.append(u' ' * 8)
         code = OP_NAMES[op]
         if len(code.encode('utf-8')) == 3:
             code += u' '
         if code is None:
             code = u'inst%s' % _unicode(op)
         if OP_USEVAL[op]:
             if op in c.OP_JUMPS:
                 slabel = padding(_unicode(self.label_map[val]), 3)
                 code_val = u'%s L%s' % (code, slabel)
             else:
                 sval = padding(_unicode(val), 4)
                 code_val = u'%s %s' % (code, sval)
         else:
             code_val = code
         code_val = padding(code_val, 10)
         comment = self.debug.comment(i) if self.debug else u''
         sline = padding(_unicode(i), 3)
         if commented:
             codes.append(u'%s ; L%s %s\n' % (code_val, sline, comment))
         else:
             codes.append(u'%s\n' % code_val)
     return u''.join(codes)
Exemple #2
0
 def build_comments(self, primitive, code_map):
     self.comments = []
     for i in range(0, len(self.lines)):
         self.comments.append([])
     for (pos, dir, step), i in code_map.items():
         if dir >= 3:
             continue
         self.comments[i].append(primitive.pane[pos])
         srow = padding(_unicode(pos[0]), 3, left=False)
         scol = padding(_unicode(pos[1]), 3, left=False)
         sdir = padding(DIR_NAMES[dir], 5)
         self.comments[i].append(u'[%s,%s] %s%s' % (srow, scol, sdir, _unicode(step)))
Exemple #3
0
 def show(self, pc, fp=2):
     op, value = self.lines[pc]
     os.write(fp, (u'L%s %s(%s) %s ;' %
                   (_unicode(pc), OP_NAMES[op], unichr(0x1100 + op),
                    value if OP_USEVAL[op] else '')).encode('utf-8'))
     os.write(fp, self.comment(pc).encode('utf-8'))
     os.write(fp, '\n')
Exemple #4
0
 def storage(self, storage, selected=None):
     def _list(list):
         items = []
         node = list.head
         while node:
             items.append(str(node.value))
             node = node.next
         return '[' + ', '.join(items) + ']'
     for i, space in enumerate(storage):
         marker = u':' if space == selected else u' '
         os.write(2, (u'%s (%s):%s' % (unichr(0x11a8 + i - 1), _unicode(i), marker)).encode('utf-8'))
         os.write(2, (u'%s\n' % _list(space)).encode('utf-8'))
Exemple #5
0
def mainloop(program, debug):
    jit.set_param(driver, 'trace_limit', 30000)
    program = jit.promote(program)
    jit.assert_green(program)
    pc = 0
    stacksize = 0
    is_queue = False
    storage = Storage()
    storage = jit.promote(storage)
    selected = storage[0]
    jit.assert_green(selected)

    while pc < program.size:
        #  debug.storage(storage, selected)
        #  raw_input()
        #  debug.show(pc)
        stackok = program.get_req_size(pc) <= stacksize
        driver.jit_merge_point(pc=pc,
                               stackok=stackok,
                               is_queue=is_queue,
                               program=program,
                               stacksize=stacksize,
                               storage=storage,
                               selected=selected)
        op = program.get_op(pc)
        jit.assert_green(op)
        stacksize += -c.OP_STACKDEL[op] + c.OP_STACKADD[op]
        if op == c.OP_ADD:
            selected.add()
        elif op == c.OP_SUB:
            selected.sub()
        elif op == c.OP_MUL:
            selected.mul()
        elif op == c.OP_DIV:
            selected.div()
        elif op == c.OP_MOD:
            selected.mod()
        elif op == c.OP_POP:
            selected.pop()
        elif op == c.OP_PUSH:
            value = program.get_operand(pc)
            big_value = bigint.fromint(value)
            selected.push(big_value)
        elif op == c.OP_DUP:
            selected.dup()
        elif op == c.OP_SWAP:
            selected.swap()
        elif op == c.OP_SEL:
            value = program.get_operand(pc)
            selected = storage[value]
            stacksize = len(selected)
            is_queue = value == c.VAL_QUEUE
        elif op == c.OP_MOV:
            r = selected.pop()
            value = program.get_operand(pc)
            storage[value].push(r)
        elif op == c.OP_CMP:
            selected.cmp()
        elif op == c.OP_BRPOP1 or op == c.OP_BRPOP2 or op == c.OP_JMP or op == c.OP_BRZ:
            if op == c.OP_BRPOP1 or op == c.OP_BRPOP2:
                jump = not stackok
            elif op == c.OP_JMP:
                jump = True
            elif op == c.OP_BRZ:
                jump = 0 == selected.pop_longlong()
            else:
                assert False
            if jump:
                value = program.get_label(pc)
                pc = value
                stackok = program.get_req_size(pc) <= stacksize
                driver.can_enter_jit(pc=pc,
                                     stackok=stackok,
                                     is_queue=is_queue,
                                     program=program,
                                     stacksize=stacksize,
                                     storage=storage,
                                     selected=selected)
                continue
        elif op == c.OP_POPNUM:
            r = selected.pop_longlong()
            write_number(r)
        elif op == c.OP_POPCHAR:
            r = selected.pop_longlong()
            write_utf8(r)
        elif op == c.OP_PUSHNUM:
            num = read_number()
            selected.push(num)
        elif op == c.OP_PUSHCHAR:
            char = read_utf8()
            selected.push(char)
        elif op == c.OP_NONE:
            pass
        elif op == c.OP_HALT:
            break
        else:
            os.write(errfp,
                     (u'Missing operator: %s' % _unicode(op)).encode('utf-8'))
            assert False
        pc += 1

    if len(selected) > 0:
        return int(selected.pop_longlong())
    else:
        return 0
Exemple #6
0
def write_number(value):
    os.write(outfp, _unicode(value).encode('utf-8'))