File: ivmdump.py

package info (click to toggle)
psyco-doc 1.6-1
  • links: PTS
  • area: contrib
  • in suites: lenny
  • size: 1,832 kB
  • ctags: 3,236
  • sloc: ansic: 23,895; python: 5,646; perl: 1,309; makefile: 153
file content (110 lines) | stat: -rw-r--r-- 3,572 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import sys, os
from struct import unpack, calcsize, error


class argtypes:
    char = "b", "0x%x"
    byte = code_t = "B", "0x%x"
    int  = "i", "0x%x"
    long = word_t = "l", "0x%x"
    def stack(t):
        if isinstance(t, tuple):
            return t[0], "[%d]"
        else:
            return "", "[%s]" % t, t
    def indirect(t):
        return t

fn = os.path.join(os.path.dirname(__file__), '../c/ivm/prolog/insns-table.py')
execfile(fn, argtypes.__dict__, globals())


class Mode:
    
    def __init__(self, opcode):
        self.opcode = opcode
        if opcode in insntable:
            self.insns = insntable[opcode]
        else:
            self.insns = [("<%d>" % opcode,)]
        if len(self.insns) == 1:
            self.singleinsn = self.insns[0][0]
        else:
            self.singleinsn = None
        self.stackpushes = stackpushes.get(opcode)
        self.unpackfmt = "="
        self.template = ""
        self.constantargs = []
        i = 0
        line = '%10x\t'
        for insn in self.insns:
            args = []
            for arg in insn[1:]:
                if isinstance(arg, tuple):
                    self.unpackfmt += arg[0]
                    args.append(arg[1])
                    if len(arg)>2:
                        self.constantargs.append((i, arg[2]))
                else:
                    args.append(str(arg))
                    self.constantargs.append((i, arg))
                i += 1
            line += '%-11s %s' % (insn[0], ', '.join(args))
            self.template += line
            line = '\n          \t'
        self.unpacksize = calcsize(self.unpackfmt)
        
    def dump(self, data, address, position):
        data = data[position:position+self.unpacksize]
        args = unpack(self.unpackfmt, data)
        result = self.template % ((address,) + args)
        return position+self.unpacksize, result.split('\n'), args

    def getargs(self, data, position):
        data = data[position:position+self.unpacksize]
        args = list(unpack(self.unpackfmt, data))
        for i, value in self.constantargs:
            args.insert(i, value)
        return args

insnlist = [Mode(opcode) for opcode in range(256)]


def dump(data, originaddr):
    l = len(data)
    if l>8 and data[-4:] == '\x00\x00\x00\x00':
        p, = unpack("l", data[-8:-4])
        queue = ["", "          (promotion chained list: 0x%x)" % p]
        l -= 4
    else:
        queue = []
    depth = None
    result = []
    p = 0
    try:
        while p < l:
            mode = insnlist[ord(data[p])]
            p, lines, args = mode.dump(data, originaddr+p, p+1)
            if depth is not None:
                if mode.stackpushes is None:
                    depth = None
                else:
                    depth += mode.stackpushes
                    lines[-1] = '%-40s [%d]' % (lines[-1], depth)
            if mode.singleinsn == 'assertdepth':
                asserteddepth = args[0]/4
                if depth is not None and asserteddepth != depth:
                    err = '************* assertion error **************'
                    lines.append(err)
                    print >> sys.stderr, err
                    print >> sys.stderr, originaddr
                else:
                    lines = [(s+' ')[:s.find('assertdepth')] for s in lines]
                depth = asserteddepth
            result += lines
    except error:
        while p < l:
            result.append("  %10x\t<%d>" % (originaddr+p, ord(data[p])))
            p += 1
    result += queue
    return result