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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
#include "Python.h"
#include "pycore_uops.h"
#include "pycore_uop_ids.h"
#define op(name, ...) /* NAME is ignored */
typedef struct _Py_UOpsSymType _Py_UOpsSymType;
typedef struct _Py_UOpsAbstractInterpContext _Py_UOpsAbstractInterpContext;
typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
static int
dummy_func(void) {
PyCodeObject *code;
int oparg;
_Py_UOpsSymType *flag;
_Py_UOpsSymType *left;
_Py_UOpsSymType *right;
_Py_UOpsSymType *value;
_Py_UOpsSymType *res;
_Py_UOpsSymType *iter;
_Py_UOpsSymType *top;
_Py_UOpsSymType *bottom;
_Py_UOpsAbstractFrame *frame;
_Py_UOpsAbstractInterpContext *ctx;
_PyUOpInstruction *this_instr;
_PyBloomFilter *dependencies;
int modified;
// BEGIN BYTECODES //
op(_LOAD_FAST_CHECK, (-- value)) {
value = GETLOCAL(oparg);
// We guarantee this will error - just bail and don't optimize it.
if (sym_is_null(value)) {
goto out_of_space;
}
}
op(_LOAD_FAST, (-- value)) {
value = GETLOCAL(oparg);
}
op(_LOAD_FAST_AND_CLEAR, (-- value)) {
value = GETLOCAL(oparg);
_Py_UOpsSymType *temp = sym_new_null(ctx);
if (temp == NULL) {
goto out_of_space;
}
GETLOCAL(oparg) = temp;
}
op(_STORE_FAST, (value --)) {
GETLOCAL(oparg) = value;
}
op(_PUSH_NULL, (-- res)) {
res = sym_new_null(ctx);
if (res == NULL) {
goto out_of_space;
};
}
op(_GUARD_BOTH_INT, (left, right -- left, right)) {
if (sym_matches_type(left, &PyLong_Type) &&
sym_matches_type(right, &PyLong_Type)) {
REPLACE_OP(this_instr, _NOP, 0, 0);
}
sym_set_type(left, &PyLong_Type);
sym_set_type(right, &PyLong_Type);
}
op(_GUARD_BOTH_FLOAT, (left, right -- left, right)) {
if (sym_matches_type(left, &PyFloat_Type) &&
sym_matches_type(right, &PyFloat_Type)) {
REPLACE_OP(this_instr, _NOP, 0 ,0);
}
sym_set_type(left, &PyFloat_Type);
sym_set_type(right, &PyFloat_Type);
}
op(_BINARY_OP_ADD_INT, (left, right -- res)) {
// TODO constant propagation
(void)left;
(void)right;
res = sym_new_known_type(ctx, &PyLong_Type);
if (res == NULL) {
goto out_of_space;
}
}
op(_LOAD_CONST, (-- value)) {
// There should be no LOAD_CONST. It should be all
// replaced by peephole_opt.
Py_UNREACHABLE();
}
op(_LOAD_CONST_INLINE, (ptr/4 -- value)) {
value = sym_new_const(ctx, ptr);
if (value == NULL) {
goto out_of_space;
}
}
op(_LOAD_CONST_INLINE_BORROW, (ptr/4 -- value)) {
value = sym_new_const(ctx, ptr);
if (value == NULL) {
goto out_of_space;
}
}
op(_LOAD_CONST_INLINE_WITH_NULL, (ptr/4 -- value, null)) {
value = sym_new_const(ctx, ptr);
if (value == NULL) {
goto out_of_space;
}
null = sym_new_null(ctx);
if (null == NULL) {
goto out_of_space;
}
}
op(_LOAD_CONST_INLINE_BORROW_WITH_NULL, (ptr/4 -- value, null)) {
value = sym_new_const(ctx, ptr);
if (value == NULL) {
goto out_of_space;
}
null = sym_new_null(ctx);
if (null == NULL) {
goto out_of_space;
}
}
op(_COPY, (bottom, unused[oparg-1] -- bottom, unused[oparg-1], top)) {
assert(oparg > 0);
top = bottom;
}
op(_SWAP, (bottom, unused[oparg-2], top --
top, unused[oparg-2], bottom)) {
}
op(_LOAD_ATTR_INSTANCE_VALUE, (index/1, owner -- attr, null if (oparg & 1))) {
_LOAD_ATTR_NOT_NULL
(void)index;
(void)owner;
}
op(_LOAD_ATTR_MODULE, (index/1, owner -- attr, null if (oparg & 1))) {
_LOAD_ATTR_NOT_NULL
(void)index;
(void)owner;
}
op(_LOAD_ATTR_WITH_HINT, (hint/1, owner -- attr, null if (oparg & 1))) {
_LOAD_ATTR_NOT_NULL
(void)hint;
(void)owner;
}
op(_LOAD_ATTR_SLOT, (index/1, owner -- attr, null if (oparg & 1))) {
_LOAD_ATTR_NOT_NULL
(void)index;
(void)owner;
}
op(_LOAD_ATTR_CLASS, (descr/4, owner -- attr, null if (oparg & 1))) {
_LOAD_ATTR_NOT_NULL
(void)descr;
(void)owner;
}
op(_CHECK_FUNCTION_EXACT_ARGS, (func_version/2, callable, self_or_null, unused[oparg] -- callable, self_or_null, unused[oparg])) {
sym_set_type(callable, &PyFunction_Type);
(void)self_or_null;
(void)func_version;
}
op(_CHECK_CALL_BOUND_METHOD_EXACT_ARGS, (callable, null, unused[oparg] -- callable, null, unused[oparg])) {
sym_set_null(null);
sym_set_type(callable, &PyMethod_Type);
}
op(_INIT_CALL_PY_EXACT_ARGS, (callable, self_or_null, args[oparg] -- new_frame: _Py_UOpsAbstractFrame *)) {
int argcount = oparg;
(void)callable;
PyFunctionObject *func = (PyFunctionObject *)(this_instr + 2)->operand;
if (func == NULL) {
goto error;
}
PyCodeObject *co = (PyCodeObject *)func->func_code;
assert(self_or_null != NULL);
assert(args != NULL);
if (sym_is_not_null(self_or_null)) {
// Bound method fiddling, same as _INIT_CALL_PY_EXACT_ARGS in VM
args--;
argcount++;
}
_Py_UOpsSymType **localsplus_start = ctx->n_consumed;
int n_locals_already_filled = 0;
// Can determine statically, so we interleave the new locals
// and make the current stack the new locals.
// This also sets up for true call inlining.
if (sym_is_known(self_or_null)) {
localsplus_start = args;
n_locals_already_filled = argcount;
}
new_frame = ctx_frame_new(ctx, co, localsplus_start, n_locals_already_filled, 0);
if (new_frame == NULL){
goto out_of_space;
}
}
op(_POP_FRAME, (retval -- res)) {
SYNC_SP();
ctx->frame->stack_pointer = stack_pointer;
ctx_frame_pop(ctx);
stack_pointer = ctx->frame->stack_pointer;
res = retval;
}
op(_PUSH_FRAME, (new_frame: _Py_UOpsAbstractFrame * -- unused if (0))) {
SYNC_SP();
ctx->frame->stack_pointer = stack_pointer;
ctx->frame = new_frame;
ctx->curr_frame_depth++;
stack_pointer = new_frame->stack_pointer;
}
op(_UNPACK_SEQUENCE, (seq -- values[oparg])) {
/* This has to be done manually */
(void)seq;
for (int i = 0; i < oparg; i++) {
values[i] = sym_new_unknown(ctx);
if (values[i] == NULL) {
goto out_of_space;
}
}
}
op(_UNPACK_EX, (seq -- values[oparg & 0xFF], unused, unused[oparg >> 8])) {
/* This has to be done manually */
(void)seq;
int totalargs = (oparg & 0xFF) + (oparg >> 8) + 1;
for (int i = 0; i < totalargs; i++) {
values[i] = sym_new_unknown(ctx);
if (values[i] == NULL) {
goto out_of_space;
}
}
}
op(_ITER_NEXT_RANGE, (iter -- iter, next)) {
next = sym_new_known_type(ctx, &PyLong_Type);
if (next == NULL) {
goto out_of_space;
}
(void)iter;
}
// END BYTECODES //
}
|