-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode_remapper.py
39 lines (38 loc) · 1.44 KB
/
opcode_remapper.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
import re
import random
current_opcode_file = open('../cpython/Lib/opcode.py')
non_argument = [x for x in range(1, 90)]
single_argument = [x for x in range(90, 144)]
extended_args = [x for x in range(144, 255)]
random.shuffle(non_argument)
random.shuffle(single_argument)
random.shuffle(extended_args)
regex_for_opcode = r'^(?P<key>[a-z_]+)+\(\'+(?P<name>[A-Z_]+)+\'+\,\s+(?P<code>\d+)(?P<extra>.*)'
new_opcode_file = open('opcode.py', 'w+')
previous_code = None
for line in current_opcode_file.readlines():
rex = re.compile(regex_for_opcode).match(line)
if rex:
op_code = int(rex.group('code'))
if op_code < 90:
new_code = non_argument.pop()
line = line.replace(rex.group('code'), str(new_code))
new_opcode_file.write(line)
previous_code = new_code
elif op_code < 144:
new_code = single_argument.pop()
line = line.replace(rex.group('code'), str(new_code))
new_opcode_file.write(line)
previous_code = new_code
else:
new_code = extended_args.pop()
line = line.replace(rex.group('code'), str(new_code))
new_opcode_file.write(line)
previous_code = new_code
elif line.startswith('has'):
num = re.findall(r'\d+', line)
if len(num) > 0:
line = line.replace(num[0], str(previous_code))
new_opcode_file.write(line)
else:
new_opcode_file.write(line)