-
Notifications
You must be signed in to change notification settings - Fork 0
/
tonyc.py
executable file
·147 lines (112 loc) · 3.54 KB
/
tonyc.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
#!/usr/bin/env python3
import os
import argparse
from tony import *
BUILTINS_LIB = 'libbuiltins.so'
def readFile(file):
with open(file, 'r', encoding = 'unicode_escape') as f:
s = f.read()
return s
def compile(file,
llvm_to_stdout=False,
as_to_stdout=False,
print_ast=False,
show_commands=True,
optimization=1,
exec_name=None,
testing=False):
if BUILTINS_LIB not in os.listdir():
cmd_make_builtins = '$(make) builtins'
os.system(cmd_make_builtins)
if show_commands: print(cmd_make_builtins)
if exec_name != None:
testing = True
# we use the testing mode to clear the intermediate files
''' Reading the code '''
if file == None:
testing = True
sys.stdin.reconfigure(encoding='unicode_escape')
code = sys.stdin.read()
else:
file_prefix = file.split('.')[0]
code = readFile(file)
''' Lexing & Parsing '''
try:
ast = parser.parse(code)
except Exception as e:
print(e)
exit()
''' Printing the Abstract Syntax Tree '''
if print_ast:
print(ast)
exit()
''' Semantic analysis '''
try:
ast.sem(SymbolTable())
except Exception as e:
print(e)
exit()
''' LLVM IR Code Generation '''
llvm_ir = str(ast.codegen(opt_level=optimization))
if llvm_to_stdout:
print(llvm_ir)
exit()
else:
llvm_output = 'a.ll' if testing else f'{file_prefix}.ll'
with open(llvm_output, 'w') as f:
print(llvm_ir, file=f)
''' Compiling to Assembly '''
as_output = 'a.s' if testing else f'{file_prefix}.s'
cmd_llc = f'llc {llvm_output} --relocation-model=pic -o {as_output}'
os.system(cmd_llc)
if show_commands: print(cmd_llc)
if as_to_stdout:
with open(as_output, 'r') as f:
print(f.read())
os.remove(as_output)
os.remove(llvm_output)
exit()
''' Making the final executable '''
executable = 'a.out' if testing else f'{file_prefix}.out'
if exec_name != None:
executable = exec_name
cmd_gcc = f'gcc {as_output} -L . -Wl,-rpath={os.getcwd()} -lbuiltins -o {executable}'
os.system(cmd_gcc)
if show_commands: print(cmd_gcc)
''' Clean files if running tests '''
if testing:
os.remove(as_output)
os.remove(llvm_output)
return
if __name__ == '__main__':
argparser = argparse.ArgumentParser()
argparser.add_argument('filename', default='', type=str, nargs='?')
argparser.add_argument('-f', action='store_true')
argparser.add_argument('-i', action='store_true')
argparser.add_argument('-O2', action='store_true')
argparser.add_argument('-O3', action='store_true')
argparser.add_argument('--commands', action='store_true')
argparser.add_argument('--ast', action='store_true')
argparser.add_argument('-o', type=str)
args = argparser.parse_args()
optimization = 1
if args.O2:
optimization = 2
if args.O3:
optimization = 3
if args.f or args.i or args.ast:
file = None
else:
file = args.filename
if args.i and args.f:
print('Please specify at most one of the arguments -f and -i')
exit()
exec_name = args.o if args.o else None
compile(
file,
show_commands=args.commands,
llvm_to_stdout=args.i,
as_to_stdout=args.f,
print_ast=args.ast,
exec_name=exec_name,
optimization=optimization)