-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnaive_transform.py
37 lines (28 loc) · 1.09 KB
/
naive_transform.py
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
import dis
import copy
from mydynamo import eval_frame
from mydynamo.bytecode_transformation import create_instruction
from mydynamo.bytecode_transformation import debug_checks
from mydynamo.bytecode_transformation import transform_code_object
from mydynamo.guards import GuardedCode
def mytransform(frame, catch_size):
def insert_ops(instructions, code_options):
# change a + b to a * b + a + b
instructions.insert(1, create_instruction("BINARY_ADD"))
instructions.insert(0, create_instruction("BINARY_MULTIPLY"))
instructions.insert(0, create_instruction("LOAD_FAST", arg=1, argval='b'))
instructions.insert(0, create_instruction("LOAD_FAST", arg=0, argval='a'))
#debug_checks(frame.f_code)
return GuardedCode(transform_code_object(frame.f_code, insert_ops))
mytransform_context = eval_frame.context(mytransform)
@mytransform_context
def add(a, b):
return a + b
def transformed_add(a, b):
return a * b + a + b
A = 5
B = 6
print("transforming a + b to a * b + a + b")
print(add(A, B))
print("should be equal to")
print(transformed_add(A, B))