Debian Codegraph @

rpython/jit/tl/tla/tla.py

raw symbols SCIP
1
2 from rpython.rlib.jit import JitDriver
3
4
5 class W_Object:
6
7 def getrepr(self):
8 """
9 Return an RPython string which represent the object
10 """
11 raise NotImplementedError
12
13 def is_true(self):
14 raise NotImplementedError
15
16 def add(self, w_other):
17 raise NotImplementedError
18
19
20
21 class W_IntObject(W_Object):
22
23 def __init__(self, intvalue):
24 self.intvalue = intvalue
25
26 def getrepr(self):
27 return str(self.intvalue)
28
29 def is_true(self):
30 return self.intvalue != 0
31
32 def add(self, w_other):
33 if isinstance(w_other, W_IntObject):
34 sum = self.intvalue + w_other.intvalue
35 return W_IntObject(sum)
36 else:
37 raise OperationError
38
39 def sub(self, w_other):
40 if isinstance(w_other, W_IntObject):
41 sum = self.intvalue - w_other.intvalue
42 return W_IntObject(sum)
43 else:
44 raise OperationError
45
46 class W_StringObject(W_Object):
47
48 def __init__(self, strvalue):
49 self.strvalue = strvalue
50
51 def getrepr(self):
52 return self.strvalue
53
54 def is_true(self):
55 return len(self.strvalue) != 0
56
57
58 class OperationError(Exception):
59 pass
60
61 # ____________________________________________________________
62
63 OPNAMES = []
64 HASARG = []
65
66 def define_op(name, has_arg=False):
67 globals()[name] = len(OPNAMES)
68 OPNAMES.append(name)
69 HASARG.append(has_arg)
70
71 define_op("CONST_INT", True)
72 define_op("POP")
73 define_op("ADD")
74 define_op("RETURN")
75 define_op("JUMP_IF", True)
76 define_op("DUP")
77 define_op("SUB")
78 define_op("NEWSTR", True)
79
80
81 # ____________________________________________________________
82
83 def get_printable_location(pc, bytecode):
84 op = ord(bytecode[pc])
85 name = OPNAMES[op]
86 if HASARG[op]:
87 arg = str(ord(bytecode[pc + 1]))
88 else:
89 arg = ''
90 return "%s: %s %s" % (pc, name, arg)
91
92 jitdriver = JitDriver(greens=['pc', 'bytecode'],
93 reds=['self'],
94 virtualizables=['self'],
95 get_printable_location=get_printable_location)
96
97 class Frame(object):
98 _virtualizable_ = ['stackpos', 'stack[*]']
99
100 def __init__(self, bytecode):
101 self.bytecode = bytecode
102 self.stack = [None] * 8
103 self.stackpos = 0
104
105 def push(self, w_x):
106 self.stack[self.stackpos] = w_x
107 self.stackpos += 1
108
109 def pop(self):
110 stackpos = self.stackpos - 1
111 assert stackpos >= 0
112 self.stackpos = stackpos
113 res = self.stack[stackpos]
114 self.stack[stackpos] = None
115 return res
116
117 def interp(self):
118 bytecode = self.bytecode
119 pc = 0
120
121 while pc < len(bytecode):
122 jitdriver.jit_merge_point(bytecode=bytecode, pc=pc, self=self)
123 opcode = ord(bytecode[pc])
124 pc += 1
125
126 if opcode == CONST_INT:
127 value = ord(bytecode[pc])
128 pc += 1
129 w_z = W_IntObject(value)
130 self.push(w_z)
131
132 elif opcode == POP:
133 self.pop()
134
135 elif opcode == DUP:
136 w_x = self.pop()
137 self.push(w_x)
138 self.push(w_x)
139
140 elif opcode == ADD:
141 w_y = self.pop()
142 w_x = self.pop()
143 w_z = w_x.add(w_y)
144 self.push(w_z)
145
146 elif opcode == SUB:
147 w_y = self.pop()
148 w_x = self.pop()
149 w_z = w_x.sub(w_y)
150 self.push(w_z)
151 elif opcode == JUMP_IF:
152 target = ord(bytecode[pc])
153 pc += 1
154 w_x = self.pop()
155 if w_x.is_true():
156 pc = target
157 jitdriver.can_enter_jit(bytecode=bytecode, pc=pc, self=self)
158
159 elif opcode == NEWSTR:
160 char = bytecode[pc]
161 pc += 1
162 w_z = W_StringObject(char)
163 self.push(w_z)
164
165 elif opcode == RETURN:
166 w_x = self.pop()
167 assert self.stackpos == 0
168 return w_x
169
170 else:
171 assert False, 'Unknown opcode: %d' % opcode
172
173
174 def run(bytecode, w_arg):
175 frame = Frame(bytecode)
176 frame.push(w_arg)
177 w_result = frame.interp()
178 return w_result