-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmconcc.py
executable file
·244 lines (215 loc) · 10.1 KB
/
xmconcc.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
241
242
243
244
#!/usr/bin/python3
###################################################################
includes = []
allocptr = 8700000
defines = {}
def reset_preprocess_vars():
global includes
global allocptr
global defines
includes = []
allocptr = 8700000
defines = {}
# обработать директивы в коде
def preprocess(include_path, code):
code = code.replace('\t', '').replace('\\\n', '').split('\n')
result = ''
global includes
global allocptr
global defines
for line in code:
line = line.strip()
if line.startswith('/define '):
defines[line.split(' ')[1]] = line[(line.find(':') + 1):].replace('\\n', '\n')
elif line.startswith('/alloc ') and not '[' in line.split(' ')[1]:
allocptr -= 1
defines[line.split(' ')[1]] = str(allocptr).zfill(7)
elif line.startswith('/alloc '):
allocptr -= int(line.split(' ')[1].split('[')[1].split(']')[0], base=0)
defines[line.split(' ')[1].split('[')[0]] = str(allocptr).zfill(7)
defines[line.split(' ')[1].split('[')[0] + '.length'] = line.split(' ')[1].split('[')[1].split(']')[0]
elif line.startswith('/free '):
allocptr += int(line.split(' ')[1], base=0)
elif line.startswith('/include ') and not line.split('"')[1] in includes:
f = open(include_path + '/' + line.split('"')[1], 'r')
result += preprocess(include_path, f.read()) + '\n'
f.close()
includes += [line.split('"')[1]]
elif line.startswith('/include '):
continue
for line in code:
line = line.strip()
if not line.startswith('#') and not line.startswith('/') and line != '':
for i in range(4):
for name in defines:
line = line.replace('{' + name + '}!', defines[name] + ' .')
line = line.replace('{' + name + '}', defines[name])
result += line + '\n'
return result[:-1]
# получить древо кода
def maketree(code):
tree = []
continue_label = 0
# формат элемента древа:
# [0] строка, определяющая тип операции
# ('asm', 'string' и т. д.)
# [1] является массивом аргументов операции
# (например, у 'label' это одна строка)
for line in code.split('\n'):
if line == '':
continue
elif line.startswith('"'):
tree += [['string', [line.split('"')[2].strip(), line.split('"')[1]]]]
elif line.startswith('$'):
tree += [['asm', [line[1:].strip()]]]
else:
line = line.replace("'\\0'", str(0)).replace("';'", str(ord(';'))).replace(' ;', ';').replace(';', ' ;')
for word in line.split(' '):
word = word.replace('(', '').replace(')', '')
if word == '':
continue
elif word[0].isdigit():
tree += [['push_number', [int(word, base=0)]]]
elif word.startswith('#'):
break
elif word[0] == "'":
tree += [['push_number', [ord(word[1])]]]
elif word[0] == "&":
tree += [['push_string_addr', [word[1:]]]]
elif word[0] == "~":
tree += [['push_label', [word[1:]]]]
elif word.startswith('<') and word.endswith('>'):
tree += [['push_extern_label', [word[1:-1]]]]
elif word[0] == "@":
tree += [['push_label', ['__C' + str(continue_label)]]]
tree += [['push_label', [word[1:]]]]
tree += [['call', ['goto']]]
tree += [['label', ['__C' + str(continue_label)]]]
continue_label += 1
elif word == ';':
tree += [['reset_stack_pointer', []]]
elif word.endswith(':'):
tree += [['label', [word.replace(':', '')]]]
else:
tree += [['call', [word]]]
return tree
# перевести в ассемблерный код Makexm2c
def compile_for_xmtwolime(prog_name, tree, outfile, mt=False):
current_label = 0 # номер следующей вспомогательной метки
def asm(code):
print(code, file=outfile)
# получить номер п. регистра
def getreg(base):
return '%R_FA_' + str(base) + '%'
# получить регистр хранения указателя стека
def getrstackptr():
return getreg(9)
# получить начало стека
def getstackstart():
return 6900100
# получить регистр для хранения адреса возврата
def getrret():
return getreg(8)
# получить регистр для хранения номера текущего потока
def getrthread():
return getreg(7)
###########################
asm(prog_name + ':')
asm('mov2 ' + getrstackptr() + ', ' + str(getstackstart()).zfill(7))
asm('mov %R_FA_24%, %OUT_ST%') # сброс указателя вывода
asm('mov ' + getrthread() + ', 0')
asm('mov2 ' + getreg(0) + ', 6900000')
asm('mov2 ' + getreg(1) + ', ' + str(getstackstart()).zfill(7))
asm('mov2 ' + getreg(2) + ', <' + prog_name + '_L' + str(current_label) + '>')
asm('mov ' + getreg(3) + ', 1')
asm('tnp ' + getreg(3))
asm(prog_name + '_L' + str(current_label) + ':')
current_label += 1
asm('isv ' + getreg(3) + ', ' + getreg(0))
asm('inc ' + getreg(0))
asm('if ' + getreg(0) + ' < ' + getreg(1) + ', ' + getreg(2))
###########################
for block in tree:
if block[0] == 'label':
asm(prog_name + '_l' + block[1][0] + ':')
elif block[0] == 'string':
asm(prog_name + '_S' + block[1][0] + ':')
asm('.goto +' + str(len(block[1][1]) + 1))
if len(block[1][1]) > 0:
asm('.ascii <' + prog_name + '_S' + block[1][0] + '> "' + block[1][1] + '"')
elif block[0] == 'asm':
asm(block[1][0])
elif block[0] == 'push_number':
if block[1][0] >= -1 and block[1][0] <= 127:
asm('mov ' + getreg(0) + ', ' + str(block[1][0]))
else:
asm('mov2 ' + getreg(0) + ', ' + str(block[1][0]).replace('-', '').zfill(7))
if block[1][0] < 0:
asm('tnp ' + getreg(0))
asm('isv ' + getreg(0) + ', ' + getrstackptr())
asm('inc ' + getrstackptr())
elif block[0] == 'push_label':
asm('mov2 ' + getreg(0) + ', <' + prog_name + '_l' + block[1][0] + '>')
asm('isv ' + getreg(0) + ', ' + getrstackptr())
asm('inc ' + getrstackptr())
elif block[0] == 'push_extern_label':
asm('mov2 ' + getreg(0) + ', <' + block[1][0] + '>')
asm('isv ' + getreg(0) + ', ' + getrstackptr())
asm('inc ' + getrstackptr())
elif block[0] == 'push_string_addr':
asm('mov2 ' + getreg(0) + ', <' + prog_name + '_S' + block[1][0] + '>')
asm('isv ' + getreg(0) + ', ' + getrstackptr())
asm('inc ' + getrstackptr())
elif block[0] == 'reset_stack_pointer':
asm('mov2 ' + getrstackptr() + ', ' + str(getstackstart()).zfill(7))
elif block[0] == 'call' and mt:
asm('mov2 ' + getreg(0) + ', <__0___next_thread>')
if block[1][0] != 'goto' and block[1][0] != 'xm2_code':
asm('mov2 ' + getrret() + ', <' + prog_name + '_L' + str(current_label) + '>')
else:
asm('dec ' + getrstackptr())
asm('ild ' + getrstackptr() + ', ' + getrret())
asm('jmp ' + getreg(0))
if block[1][0] != 'goto' and block[1][0] != 'xm2_code':
asm(prog_name + '_L' + str(current_label) + ':')
current_label += 1
asm('mov2 ' + getreg(0) + ', <__0_' + block[1][0] + '>')
asm('mov2 ' + getrret() + ', <' + prog_name + '_L' + str(current_label) + '>')
asm('jmp ' + getreg(0))
asm(prog_name + '_L' + str(current_label) + ':')
current_label += 1
elif block[0] == 'call' and (block[1][0] == 'goto' or block[1][0] == 'xm2_code'):
asm('dec ' + getrstackptr())
asm('ild ' + getrstackptr() + ', ' + getrret())
asm('jmp ' + getrret())
elif block[0] == 'call':
asm('mov2 ' + getreg(0) + ', <__0_' + block[1][0] + '>')
asm('mov2 ' + getrret() + ', <' + prog_name + '_L' + str(current_label) + '>')
asm('jmp ' + getreg(0))
asm(prog_name + '_L' + str(current_label) + ':')
current_label += 1
###################################################################
if __name__ == '__main__':
import sys
if len(sys.argv) > 1 and sys.argv[1] == '--help':
print('xmconcc - The XMConC Compiler.')
print('Usage: ./xmconcc.py [--help] INCLUDE_PATH NAME PROG [-mt]')
print('')
print(' --help print this message and exit')
print(' INCLUDE_PATH directory of header-files')
print(' NAME name of XMConC program')
print(' PROG path to XmConC source file')
print(' -mt support for multi-threading')
sys.exit(1)
try:
fcontent = ''
progname = sys.argv[2]
if len(ascii(progname).split("'")[1]) != 3:
print('error: len(ascii(progname).split("\'")[1]) != 3', file=sys.stderr)
sys.exit(3)
f = open(sys.argv[3], 'r')
fcontent = f.read()
f.close()
except Exception:
sys.exit(2)
compile_for_xmtwolime(progname, maketree(preprocess(sys.argv[1], fcontent)), sys.stdout, mt=(True if len(sys.argv) > 4 and sys.argv[4] == '-mt' else False))