-
Notifications
You must be signed in to change notification settings - Fork 0
/
MODEL.py
499 lines (386 loc) · 13.6 KB
/
MODEL.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import node
import struct
import glob as g
import linecache
from copy import copy
from functools import reduce
from enum import IntEnum, auto
from mnemonic import mnemonic as opc
BASETYPE = ['void', 'int', 'float', 'struct', 'enum']
class SymbolNotFoundException (Exception):
pass
class SymbolRedefineException (Exception):
pass
class TypeRedefineException (Exception):
pass
class SymbolLengthNotSpecifiedException (Exception):
pass
class NonPointerException (Exception):
pass
class ConflictingTypesException(Exception):
pass
class Type:
def __init__(self, basetype = 'void'):
self.basetype = basetype
self.refcount = 0
self.length = 1
self.quals = []
self.members = []
self.name = None
self.hint = None
def __str__(self):
buff = self.basetype
if self.refcount > 0 or self.length > 1:
buff += ' '
buff += '*' * self.refcount
if (self.length > 1):
buff += f'[{self.length}]'
return buff
def __eq__(self, other):
if not isinstance(other, Type):
return NotImplemented
return self.basetype == other.basetype \
and self.refcount == other.refcount \
and self.length == other.length \
and self.quals == other.quals \
and self.members == other.members \
and self.hint == other.hint
def addRefcount(self, plus):
self.refcount += plus
return self
def addQuals(self, quals):
for elem in quals:
if elem not in self.quals:
self.quals.append(elem)
return self
def setHint(self, hint):
self.hint = hint
return self
def setLength(self, length):
self.length = length
return self
def setName(self, name):
self.name = name
return self
def addMember(self, member):
self.members.append(member)
return self
def isBasetype(self):
return self.basetype in BASETYPE
def isScalar(self):
return self.basetype in ('int', 'float', 'enum') and self.refcount == 0 and self.length == 1
def isPointer(self):
return self.refcount > 0 and self.length == 1
def isArray(self):
return self.length > 1
def isStruct(self):
return self.basetype == 'struct'
def isEnum(self):
return self.basetype == 'enum'
class StructField:
def __init__ (self, typ, offset):
self.typ = typ
self.offset = offset
class VarRegion (IntEnum):
GLOBAL = auto()
ARGUMENT = auto()
LOCAL = auto()
class Function:
def __init__(self, symbolname, typ, args, insts):
self.symbolname = symbolname
self.typ = typ
self.args = args
self.insts = insts
class GlobalVar:
def __init__(self, symbolname, typ, insts):
self.symbolname = symbolname
self.typ = typ
self.insts = insts
class Inst:
def __init__(self, opc, arg):
self.opc = opc
self.arg = arg
def serialize(self):
if isinstance(self.arg, int):
arg = format(struct.unpack('>I', struct.pack('>i', self.arg))[0], "d")
elif isinstance(self.arg, float):
arg = format(struct.unpack('>I', struct.pack('>f', self.arg))[0], "d")
else:
print(f"serialize arg unkown type error: {self.arg=}")
return "0"
if arg == '0':
return f'{self.opc.value}'
else:
return f'{self.opc.value}.{arg}'
def debugserial(self):
return f'{self.opc.name} {self.arg}'
class Insts:
def __init__(self, typ = Type(), bytecodes = []):
self.typ = typ
self.bytecodes = bytecodes
class Symbol:
def __init__(self, name, typ, initvalue = 0):
self.name = name
self.typ = typ
self.initvalue = initvalue
def __eq__(self, other):
if not isinstance(other, Symbol):
return NotImplemented
return self.name == other.name and self.typ == other.typ
def setID(self, newid):
self.id = newid
def setRegion(self, region):
self.region = region
def genStoreCode(self):
if (self.region == VarRegion.GLOBAL):
return Inst(opc.STOREG, self.id)
elif (self.region == VarRegion.ARGUMENT):
return Inst(opc.STOREA, self.id)
elif (self.region == VarRegion.LOCAL):
return Inst(opc.STOREL, self.id)
else:
print("PROGRAM ERROR GENSTORECODE")
def genLoadCode(self):
if (self.region == VarRegion.GLOBAL):
return Inst(opc.LOADG, self.id)
elif (self.region == VarRegion.ARGUMENT):
return Inst(opc.LOADA, self.id)
elif (self.region == VarRegion.LOCAL):
return Inst(opc.LOADL, self.id)
else:
print("PROGRAM ERROR GENLOADCODE")
def genAddrCode(self):
if (self.region == VarRegion.GLOBAL):
return Inst(opc.PUSH, self.id)
elif (self.region == VarRegion.ARGUMENT):
return Inst(opc.PUAP, self.id)
elif (self.region == VarRegion.LOCAL):
return Inst(opc.PULP, self.id)
else:
print("PROGRAM ERROR GENLOADCODE")
class scopedEnv:
def __init__(self):
self.variables = []
self.structs = {}
self.enums = {}
self.enumMembers = {}
self.types = {}
class Env:
def __init__(self):
self.functions = []
self.labelitr = 0
self.statics = []
self.strings = {} # string重複時にIDを読み出すためだけに使う
self.staticItr = 0
self.scopeStack = [scopedEnv()]
self.args = []
self.argItr = 0
self.localItr = 0
self.currentFuncName = '__DEFAULT'
self.calledFunc = []
def markCalledFunc(self, funcname):
if not funcname in self.calledFunc:
self.calledFunc.append(funcname)
def functionLookup(self, name):
for elem in self.functions:
if (elem.symbolname == name):
return elem
raise SymbolNotFoundException(f"function '{name}'")
def variableLookup(self, name):
for scope in reversed(self.scopeStack):
for elem in scope.variables:
if (elem.name == name):
return elem
for elem in self.args:
if (elem.name == name):
return elem
for elem in self.statics:
if (type(elem) is Symbol and elem.name == name):
return elem
raise SymbolNotFoundException(f"variable '{name}'")
def enumLookup(self, name):
for scope in reversed(self.scopeStack):
if name in scope.enumMembers:
return scope.enumMembers[name]
raise SymbolNotFoundException(f"variable '{name}'")
def issueString(self, string):
if (string not in self.strings):
self.strings[string] = self.staticItr
self.statics.append(string)
self.staticItr += len(string) + 1
return self.strings[string]
def addFunction(self, function):
for elem in self.functions:
if elem.symbolname == function.symbolname:
if elem.insts is None and function.insts is not None:
if elem.typ == function.typ and elem.args == function.args:
elem.insts = function.insts
else:
raise ConflictingTypesException()
else:
raise SymbolRedefineException()
return
self.functions.append(function)
def addStatic(self, symbol):
symbol.setRegion(VarRegion.GLOBAL)
symbol.setID(self.staticItr)
self.staticItr += self.calcTypeSize(symbol.typ)
self.statics.append(symbol)
def addArg(self, symbol):
symbol.setRegion(VarRegion.ARGUMENT)
symbol.setID(self.argItr)
self.argItr += self.calcTypeSize(symbol.typ)
self.args.append(symbol)
def addLocal(self, symbol):
symbol.setRegion(VarRegion.LOCAL)
symbol.setID(self.localItr)
self.localItr += self.calcTypeSize(symbol.typ)
self.scopeStack[-1].variables.append(symbol)
return symbol
def pushScope(self):
self.scopeStack.append(scopedEnv())
def popScope(self):
self.scopeStack.pop()
def resetFrame(self, funcname):
self.localItr = 0
self.argItr = 0
self.args.clear()
currentFuncName = funcname
def issueLabel(self):
newlabel = self.labelitr
self.labelitr += 1
return newlabel
def addType(self, name, typ):
target = self.scopeStack[-1].types
if name in target:
raise TypeRedefineException(f"Redefined Type '{name}'")
target[name] = typ
def addStruct(self, name, typ):
target = self.scopeStack[-1].structs
if name in target:
raise TypeRedefineException(f"Redefined Struct '{name}'")
target[name] = typ
def addEnum(self, name, typ):
target = self.scopeStack[-1].enums
if name in target:
raise TypeRedefineException(f"Redefined Struct '{name}'")
for elem in typ.members:
self.scopeStack[-1].enumMembers[elem[0]] = elem[1]
target[name] = typ
def getType(self, name, hint = ""):
if hint == 'struct':
return self.getStruct(name)
for scope in reversed(self.scopeStack):
if name in scope.types:
return scope.types[name]
print(f"type '{name}' not found")
def getStruct(self, name):
for scope in reversed(self.scopeStack):
if name in scope.structs:
return scope.structs[name]
def getFrameSize(self):
return self.localItr
def getField(self, typ, field):
if len(typ.members) == 0:
typ = self.getType(typ.basetype, typ.hint)
offset = 0
for elem in typ.members:
if elem[0] == field:
return (offset, elem[1])
offset += self.calcTypeSize(elem[1])
print('field not found exception')
def calcTypeSize(self, typ, hint=None):
# 前準備
if typ.length is None:
if isinstance(hint, list):
length = len(hint)
typ.length = length
else:
print(hint)
raise SymbolLengthNotSpecifiedException
else:
length = typ.length
if isinstance(typ.length, node.AST):
length = typ.length.eval()
typ.length = length
if typ.refcount > 0:
return length
if typ.basetype in ('void', 'int', 'float', 'enum'):
return length
if len(typ.members) == 0:
return self.calcTypeSize(self.getType(typ.basetype, typ.hint)) * length
else:
return reduce(lambda sigma, e: sigma + self.calcTypeSize(e[1]), typ.members, 0) * length
def calcPointeredSize(self, typ):
tmp = copy(typ)
if tmp.length > 1:
tmp.setLength(1).addRefcount(1)
if not tmp.isPointer():
raise NonPointerException()
return None
tmp.addRefcount(-1)
return self.calcTypeSize(tmp)
class TokenInfo:
def __init__(self, lineno, colno, filename = "input.c"):
self.lineno = lineno
self.colno = colno
self.filename = filename
def __str__(self):
return f"{self.filename}:{self.lineno}:{self.colno}"
class ErrorModule:
def __init__(self):
self.reports = []
self.fail = 0
self.notice = 0
def addReport(self, report):
if (report.level == 'fatal' or report.level == 'error'):
self.fail += 1
self.notice += 1
self.reports.append(report)
def hasError(self):
return self.fail != 0
def hasNotice(self):
return self.notice != 0
def report(self):
fatal = 0
error = 0
warning = 0
log = ""
for elem in self.reports:
level = elem.level
if (level == 'fatal'):
level = '\033[35mfatal\033[0m'
fatal += 1
if (level == 'error'):
level = '\033[31merror\033[0m'
error += 1
if (level == 'warning'):
level = '\033[33mwarning\033[0m'
warning += 1
if elem.tok is None:
log += f"\033[1m<position info not available>: {level}\033[1m: {elem.message}\033[0m" + '\n'
else:
log += f"\033[1m{elem.tok}: {level}\033[1m: {elem.message}\033[0m" + '\n'
rawline = linecache.getline(elem.tok.filename, elem.tok.lineno)
line = rawline.lstrip()
deleted = len(rawline) - len(line)
line = line.rstrip()
log += '\t' + line + '\n'
log += '\t' + ' ' * (elem.tok.colno - deleted - 1) + '\033[32m^\033[0m' + '\n'
if (fatal == 0 and error == 0 and warning == 0):
return
msg = ""
if (warning != 0):
msg += f"{warning} warning" + ("s" if warning != 1 else "")
if (error != 0):
msg += (" and" if len(msg) != 0 else "") + f" {error} error" + ("s" if error != 1 else "")
if (fatal != 0):
msg += (" and" if len(msg) != 0 else "") + f" {fatal} fatal error" + ("s" if fatal != 1 else "")
msg += " generated."
print(log + msg)
return log + msg
class Report:
def __init__(self, level, tok, message):
self.level = level
self.tok = tok
self.message = message