-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinocchio.py
executable file
·63 lines (60 loc) · 1.99 KB
/
pinocchio.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
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
#!/usr/bin/python3
import re
import sys
OUTPUT = re.compile("output ([0-9]+)")
CMD = re.compile("([a-z0-9-]+) in ([0-9]+) <([0-9 ]+)> out ([0-9]+) <([0-9 ]+)>")
MUL_CONST = re.compile("const-mul-([0-9a-f]+)")
MUL_NEG_CONST = re.compile("const-mul-neg-([0-9a-f]+)")
for line in sys.stdin:
line = line.strip()
sp = OUTPUT.fullmatch(line)
if sp:
print("debug v%i" % int(sp.group(1)))
continue
sp = CMD.fullmatch(line)
if not sp:
raise Exception("Unknown line: %s" % line)
cmd = sp.group(1)
num_in = int(sp.group(2))
ins = [int(x) for x in sp.group(3).split(" ")]
assert(len(ins) == num_in)
num_out = int(sp.group(4))
outs = [int(x) for x in sp.group(5).split(" ")]
assert(len(outs) == num_out)
if cmd == 'split':
assert(len(ins) == 1)
print("%s := v%i" % (",".join("v%i" % i for i in outs), ins[0]))
continue
if cmd == 'pack':
assert(len(outs) == 1)
print("v%i =: %s" % (outs[0], ",".join("v%i" % i for i in ins)))
continue
if cmd == 'xor':
assert(len(ins) == 2)
assert(len(outs) == 1)
print("v%i = v%i ^ v%i" % (outs[0], ins[0], ins[1]))
continue
if cmd == 'add':
assert(len(ins) == 2)
assert(len(outs) == 1)
print("v%i = v%i + v%i" % (outs[0], ins[0], ins[1]))
continue
if cmd == 'mul':
assert(len(ins) == 2)
assert(len(outs) == 1)
print("v%i = v%i * v%i" % (outs[0], ins[0], ins[1]))
continue
else:
sp = MUL_CONST.fullmatch(cmd)
if sp:
assert(len(ins) == 1)
assert(len(outs) == 1)
print("v%i = v%i * %i" % (outs[0], ins[0], int(sp.group(1), 16)))
continue
sp = MUL_NEG_CONST.fullmatch(cmd)
if sp:
assert(len(ins) == 1)
assert(len(outs) == 1)
print("v%i = v%i * (-%i)" % (outs[0], ins[0], int(sp.group(1), 16)))
continue
raise Exception("Unknown command: %s" % cmd)