-
Notifications
You must be signed in to change notification settings - Fork 34
/
run_8cc.py
executable file
·141 lines (116 loc) · 3.96 KB
/
run_8cc.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
#!/usr/bin/python3
import os.path
import sys
import subprocess
import argparse
## Utility
def pr(*args):
print("INFO:", *args)
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
def call(*cmd):
try:
print(subprocess.list2cmdline(cmd))
if subprocess.call(cmd):
exit(1)
except Exception as e:
eprint(e)
exit(1)
def get_compiler_cmds():
try:
out = subprocess.run(['g++', '-v', '--help'], capture_output=True)
except Exception:
eprint('Error: g++ is not installed.')
exit(1)
cmd = ['g++']
# If constexpr-*-limit flags are supported, set to the maximum number.
if b'fconstexpr-loop-limit' in out.stdout:
cmd.append('-fconstexpr-loop-limit={}'.format(2**31 - 1))
if b'fconstexpr-ops-limit' in out.stdout:
cmd.append('-fconstexpr-ops-limit={}'.format(2**31 - 1))
return cmd
def redirect(cmd, dst):
with open(dst, 'w') as f:
try:
print('{} > {}'.format(cmd, dst))
if subprocess.call(cmd, stdout=f):
exit(1)
except Exception as e:
eprint(e)
exit(1)
def to_cpp_string(fname, target=''):
pr('Convert the content of "{}" into C++ string literal'.format(fname))
with open(fname, 'r') as f:
s = f.read()
s = 'R"(' + target + s + ')"\n'
with open(fname, 'w') as f:
f.write(s)
def replace_line(fname, var, new_str):
pr('Change the value of "{}" in "{}"'.format(var, fname))
with open(fname, 'r') as f:
ls = f.readlines()
with open(fname, 'w') as f:
for l in ls:
if var in l:
f.write(new_str)
else:
f.write(l)
## Argument Parser
DIR = os.path.dirname(__file__)
TARGET_LIST = ['eir', 'rb', 'py', 'js', 'el',
'vim', 'tex', 'cl', 'sh', 'java',
'c', 'cpp', 'x86', 'i', 'ws',
'bef', 'bf', 'pietasm', 'piet', 'unl']
argparser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
description='Example:\n$ {}/%(prog)s x86 ./test/putchar.c -o ./putchar.exe'.format(DIR))
argparser.add_argument('target',
choices=TARGET_LIST,
metavar='target',
help='target language of compilation\n[%(choices)s]')
argparser.add_argument('infile',
metavar='file',
help='input C file')
argparser.add_argument('-o',
metavar='<file>',
help='set output file name')
## Main
O_DIR = "{}/out".format(DIR)
config_hpp = '{}/config.hpp'.format(DIR)
cc_cpp = '{}/8cc.cpp'.format(DIR)
elc_cpp = '{}/elc.cpp'.format(DIR)
args = argparser.parse_args()
target = args.target
infile = args.infile
infile_txt = infile + ".txt"
fname, _ = os.path.splitext(infile)
bname = os.path.basename(fname)
eirfile = '{}/out/{}.eir'.format(DIR, bname)
outfile = args.o
if outfile == None:
if target == 'x86':
outfile = fname + '.exe'
else:
outfile = fname + target
compiler_cmds = get_compiler_cmds()
pr("Start compilation from \"{}\" to \"{}\"".format(infile, outfile))
pr("Convert C program into C++ string literal")
call('cp', infile, infile_txt)
to_cpp_string(infile_txt)
replace_line(config_hpp,
'#define EIGHT_CC_INPUT_FILE', '#define EIGHT_CC_INPUT_FILE "{}"\n'.format(infile_txt))
pr("Compile C into ELVM IR")
args = compiler_cmds + ['-std=c++14', '-o', '{}/{}_eir.exe'.format(O_DIR, bname), cc_cpp]
call(*args)
if target == 'eir':
redirect('{}/{}_eir.exe'.format(O_DIR, bname), outfile)
exit(0)
redirect('{}/{}_eir.exe'.format(O_DIR, bname), eirfile)
pr("Convert ELVM IR into C++ string literal")
to_cpp_string(eirfile, target + '\n')
replace_line(config_hpp,
'#define ELC_INPUT_FILE', '#define ELC_INPUT_FILE "{}"\n'.format(eirfile))
pr("Compile ELVM IR into {} file".format(target))
args = compiler_cmds + ['-std=c++14', '-o', '{}/{}_out.exe'.format(O_DIR, bname), elc_cpp]
call(*args)
redirect('{}/{}_out.exe'.format(O_DIR, bname), outfile)
pr("\"{}\" was generated successfully".format(outfile))