-
Notifications
You must be signed in to change notification settings - Fork 2
/
pyvm.py
79 lines (57 loc) · 1.72 KB
/
pyvm.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
""" module for reading the pyc file """
from sys import argv
from code import Code
from operations import operations, stk
from utils import get_pyc_list, have_arg
from opcodes import CALL_FUNCTION, RETURN_VALUE
def call_function(code_obj, cur):
""" executes the function """
argc = code_obj.get_oparg(cur)
func = stk.get_top_n(argc)
# backup func's local vars
bkup_locals = func.varnames[:]
while argc:
argc -= 1
func.varnames[argc] = stk.pop()
stk.pop()
execute(func)
# restore func's local vars
func.varnames = bkup_locals[:]
return cur + 3
def execute(code_obj):
""" executes the code object """
cur = 0
while not code_obj.is_end(cur):
opcode = code_obj.get_opcode(cur)
if opcode == CALL_FUNCTION:
cur = call_function(code_obj, cur)
continue
if opcode == RETURN_VALUE:
return
try:
if have_arg(opcode):
cur = operations[opcode](code_obj, cur)
else:
cur = operations[opcode](cur)
except KeyError:
msg = 'opcode {1} at {0} not found'.format(cur, hex(opcode))
raise Exception(msg)
def execute_pyc(pyc_file):
""" executes the pyc file """
# getting the bytecode as a list of bytes
pyc_lst = get_pyc_list(pyc_file)
# getting the codeobject from the pyc list
code_obj = Code(pyc_lst)
# executing the code object
print
execute(code_obj)
print
def main():
""" main function """
if len(argv) != 2 or '.pyc' not in argv[1]:
print 'usage: pyvm.py filename.pyc'
else:
pyc_file = argv[1]
execute_pyc(pyc_file)
if __name__ == '__main__':
main()