-
Notifications
You must be signed in to change notification settings - Fork 0
/
poa.py
240 lines (193 loc) · 6.55 KB
/
poa.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
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
"""
Module for compiling Python opcode instructions
"""
__version__ = '0.1'
__author__ = 'Fiete Minge'
import sys
import os
import types
import _opcode
def parse_definition_file(fp) -> dict[str, tuple[int, bool]]:
def _canconv(t, v):
try:
_=t(v)
return True
except Exception:
return False
opcode_definitions = {}
for line in fp.readlines():
if line.startswith('#define '):
_s = list(filter(lambda x:x, line.strip().split(' ')))
if len(_s) == 3 and _canconv(int, _s[-1]):
opcode_definitions[_s[1]] = int(_s[2])
if (_have_argument := opcode_definitions.get('HAVE_ARGUMENT')):
opcode_definitions = {name: (opcode, opcode>=_have_argument) for name, opcode in opcode_definitions.items()}
else:
return None
del opcode_definitions['HAVE_ARGUMENT']
return opcode_definitions
def parse_opcode_file(fp) -> tuple[list[tuple[str, int]], bytes]:
def _canconv(t, v):
try:
_=t(v)
return True
except Exception:
return False
exec_flow = []
lines = []
idx = 0
for lidx, line in enumerate(fp.readlines()):
line = line.strip()
if line.replace(' ', '').replace('\t', '').startswith('//') or not line:
continue
if line.endswith(":") and _canconv(int, line[:-1]):
linenr = int(line[:-1])
cur_offset = idx
lines.append((linenr, cur_offset))
continue
params = list(filter(lambda x:x,
line.strip().replace('\t', ' ').split(' ')))
if len(params) == 2 and opcodes.get(params[0]) and opcodes[params[0]][1] and _canconv(int, params[1]):
exec_flow.append((params[0], int(params[1])))
elif len(params) == 1 and opcodes.get(params[0]) and (not opcodes[params[0]][1]):
exec_flow.append((params[0], None))
else:
print("[ERROR]: Failed parsing in line {}".format(lidx))
return None
idx += 2
lnotab = []
for lidx, line_info in enumerate(lines):
if lidx == 0:
# byte increase = 0 since it's the beginning of the bytecode
lnotab.append(0)
lnotab.append(line_info[0]) # line number
else:
lnotab.append(line_info[1]-lines[lidx-1][1])
lnotab.append(line_info[0]-lines[lidx-1][0])
lnotab = bytes(lnotab)
return (exec_flow, lnotab)
def parse_opcode_string(string: str):
return parse_opcode_file(__import__('io').StringIO(string))
def get_definition_map() -> dict[str, tuple[int, bool]]:
_opc_path = os.path.split(sys.executable)[0]+"\\include\\opcode.h"
if not os.path.isfile(_opc_path):
return None
with open(_opc_path, 'r') as fp:
return parse_definition_file(fp)
def calc_stacksize(tokens: list[tuple[str, int]]) -> int:
max_size = 0
stacksize = 0
for instruction, argument in tokens:
instruction = opcodes[instruction][0]
if argument != None:
stacksize += _opcode.stack_effect(instruction, argument)
else:
stacksize += _opcode.stack_effect(instruction)
if stacksize > max_size:
max_size = stacksize
return max_size
def assemble_instructionflow(flow: list[tuple[str, int]]) -> bytes:
bytecode = []
for instruction in flow:
bytecode.append(opcodes[instruction[0]][0])
bytecode.append(instruction[1] if instruction[1] else 0)
return bytes(bytecode)
if not (opcodes := get_definition_map()):
print("[ERROR]: Failed generating opcode-map.")
def parse_function_signature(signature: str):
# example: "functionname(argument1, argument2, argument3)"
signature = signature.strip().replace(' ','')
name = ""
arguments = []
buffer = ""
depth = 0
ignore_metadata = False
for idx, c in enumerate(signature):
if c == '(':
name = buffer
buffer = ''
depth += 1
elif c == ',' and depth == 1:
arguments.append(buffer)
buffer = ''
ignore_metadata = False
elif c == ':':
ignore_metadata = True
elif c == ')':
if depth == 0:
raise Exception("EOL: closing non-opened bracket.")
depth -= 1
if depth == 0:
break
elif not ignore_metadata:
buffer += c
if buffer:
arguments.append(buffer)
return name, tuple(arguments)
def make_function(
argcount: int,
posonlyargcount: int,
kwonlyargcount: int,
nlocals: int,
stacksize: int,
flags: int,
codestring: bytes,
constants: tuple, # constant values starting with (None,)
names: tuple, # (Function names etc.)
varnames: tuple, # variable names including parameters
filename: str,
name: str, # function name
firstlineno: int,
lnotab: bytes,
globals: dict):
_tcode = types.CodeType(argcount,
posonlyargcount,
kwonlyargcount,
nlocals,
stacksize,
flags,
codestring,
constants,
names,
varnames,
filename,
name,
firstlineno,
lnotab)
return types.FunctionType(_tcode, globals)
# decorator inline assembly
# doesnt support *args or **kwargs
def pasm(signature: str, code: str, locals, constants, functions, globals={}):
tokens, lnotab = parse_opcode_string(code)
code = assemble_instructionflow(tokens)
function_name, function_arguments = parse_function_signature(signature)
compiled = make_function(
argcount=len(function_arguments),
posonlyargcount=0,
kwonlyargcount=0,
nlocals=len(locals)+len(function_arguments),
stacksize=calc_stacksize(tokens),
flags=0,
codestring=code,
constants=constants,
names=functions,
varnames=function_arguments+locals,
filename="<string>",
name=function_name,
firstlineno=1,
lnotab=lnotab,
globals=globals)
def wrap(func):
func(compiled)
return wrap
if __name__ == '__main__' and opcodes:
if len(sys.argv) != 2:
sys.exit("Usage: python {} [path-to-pyasm-file]".format(sys.argv[0]))
if not os.path.isfile(sys.argv[1]):
sys.exit("[ERROR]: File does not exist.")
with open(sys.argv[1], 'r') as fp:
if not (parsed := parse_opcode_file(fp)):
sys.exit(2)
flow, lnotab = parsed
print("lnotab:", str(lnotab))
print("Code:", str(assemble_instructionflow(flow)))