-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.py
698 lines (596 loc) · 19 KB
/
ir.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
#!/usr/bin/python
import support
__doc__='''Intermediate Representation
Could be improved by relying less on class hierarchy and more on string tags and/or duck typing
Includes lowering and flattening functions'''
#SYMBOLS AND TYPES
basetypes = [ 'Int', 'Float', 'Label', 'Struct', 'Function' ]
qualifiers = [ 'unsigned' ]
class Type(object):
def __init__(self, name, size, basetype, qualifiers=None):
self.name=name
self.size=size
self.basetype=basetype
self.qual_list=qualifiers
class ArrayType(Type):
def __init__(self, name, size, basetype):
self.name=name
self.size=size
self.basetype=basetype
self.qual_list=[]
class StructType(Type):
def __init__(self, name, size, fields):
self.name=name
self.fields=fields
self.size=self.getSize()
self.basetype='Struct'
self.qual_list=[]
def getSize(self):
return sum([ f.size for f in self.fields])
class LabelType(Type):
def __init__(self):
self.name='label'
self.size=0
self.basetype='Label'
self.qual_list=[]
self.ids=0
def __call__(self,target=None):
self.ids+=1
return Symbol(name='label'+`self.ids`, stype=self, value=target)
class FunctionType(Type):
def __init__(self):
self.name='function'
self.size=0
self.basetype='Function'
self.qual_list=[]
standard_types = {
'int' : Type('int', 32, 'Int'),
'short': Type('short', 16, 'Int'),
'char' : Type('char', 8, 'Int'),
'uchar': Type('uchar', 8, 'Int', ['unsigned']),
'uint' : Type('uint', 32, 'Int', ['unsigned']),
'ushort': Type('ushort',16,'Int', ['unsigned']),
'float': Type('float', 32, 'Float'),
'label': LabelType(),
'function' : FunctionType(),
}
class Symbol(object):
def __init__(self, name, stype, value=None):
self.name=name
self.stype=stype
self.value=value # if not None, it is a constant
self.symbol=name
def __repr__(self):
if self.stype.name == 'array' :
return '{} {}[{}] {}'.format(self.stype.name, self.name, self.stype.size, type(self.value) if self.value else '')
return self.stype.name+' '+str(self.name) + ( self.value if type(self.value)==str else '')
def collect_uses(self):
return []
def getFunction(self):
return 'global'
class SymbolTable(list):
def find(self, name):
print 'Looking up', name
for s in self :
if s.name==name : return s
print 'Looking up failed!'
return None
def __repr__(self):
res='SymbolTable:\n'
for s in self :
res+=repr(s)+'\n'
return res
def exclude(self, barred_types):
return [ symb for symb in self if symb.stype not in barred_types ]
#IRNODE
class IRNode(object):
def __init__(self,parent=None, children=None, symtab=None):
self.parent=parent
if children :
self.children=children
for c in self.children:
if hasattr(c, 'parent'): c.parent=self
else : self.children=[]
self.symtab=symtab
def __repr__(self):
from string import split, join
attrs = set(['body','cond', 'value','thenpart','elsepart', 'symbol', 'call', 'step', 'expr', 'target', 'defs', 'global_symtab', 'local_symtab', 'index', 'return_expr', 'dest', 'left', 'right', 'op', 'operand', 'operation' ]) & set(dir(self))
res=`type(self)`+' '+`id(self)`+' {\n'
try :
label=self.getLabel()
res=label.name+': '+res
except Exception, e :
pass
if 'children' in dir(self) and len(self.children) :
res+='\tchildren:\n'
for node in self.children :
rep=repr(node)
res+=join([ '\t'+s for s in rep.split('\n') ],'\n')+'\n'
for d in attrs :
node=getattr(self,d)
rep=repr(node)
res+='\t'+d+': '+join([ '\t'+s for s in rep.split('\n') ],'\n')+'\n'
res+='}'
return res
def navigate(self, action):
action(self)
attrs = set(['body','cond', 'value','thenpart','elsepart', 'symbol', 'call', 'step', 'expr', 'target', 'defs', 'global_symtab', 'local_symtab' ]) & set(dir(self))
if 'children' in dir(self) and len(self.children) :
#print 'navigating children of', type(self), id(self), len(self.children)
for node in self.children :
if hasattr(node, 'navigate'): node.navigate(action)
for d in attrs :
if hasattr(getattr(self,d), 'navigate'):
getattr(self,d).navigate(action)
def replace(self, old, new):
if 'children' in dir(self) and len(self.children) and old in self.children:
self.children[self.children.index(old)]=new
return True
attrs = set(['body','cond', 'value','thenpart','elsepart', 'symbol', 'call', 'step', 'expr', 'target', 'defs', 'global_symtab', 'local_symtab', 'index' ]) & set(dir(self))
for d in attrs :
try :
if getattr(self,d)==old :
setattr(self,d,new)
return True
except Exception :
pass
return False
def collect_uses(self):
return []
#CONST and VAR
class Const(IRNode):
def __init__(self,parent=None, value=0, symb=None, symtab=None):
self.parent=parent
self.value=value
self.symtab=symtab
def lower(self):
sym = Symbol(self.value, standard_types['int'])
stat_list = StatList(self.parent, [sym], self.symtab)
return self.parent.replace(self, stat_list)
class Var(IRNode):
def __init__(self,parent=None, var=None, symtab=None):
self.parent=parent
self.symbol=var
self.symtab=symtab
def collect_uses(self):
return [self.symbol]
def lower(self):
dest = IRVar().name
load = LoadStat(symbol=dest, value=self.symbol)
stat_list = StatList(self.parent, [load], self.symtab)
return self.parent.replace(self, stat_list)
class ArrayVar(IRNode):
def __init__(self,parent=None, var=None, index=None, symtab=None):
self.parent=parent
self.symbol=var
self.index=index
self.index.parent=self
self.symtab=symtab
def collect_uses(self):
return [self.symbol]
def lower(self):
if not isinstance(self.index, Const):
self.index.lower()
print self.index
dest = IRVar().name
index = self.index.children[-1].symbol
load = LoadArrStat(symbol=dest, index=index, value=self.symbol)
children = self.index.children + [load]
stat_list = StatList(self.parent, children, self.symtab)
else:
dest = IRVar().name
load = LoadArrStat(symbol=dest, index=self.index, value=self.symbol)
stat_list = StatList(self.parent, [load], self.symtab)
return self.parent.replace(self, stat_list)
#EXPRESSIONS
class Expr(IRNode):
def getOperator(self):
return self.children[0]
def collect_uses(self):
uses = []
for c in self.children :
try : uses += c.collect_uses()
except AttributeError : pass
return uses
class BinExpr(Expr):
def getOperands(self):
return self.children[1:]
def lower(self):
if not isinstance(self.children[1], StatList):
self.children[1].lower()
if not isinstance(self.children[2], StatList):
self.children[2].lower()
left = self.children[1].children[-1] # last statement from StatList
right = self.children[2].children[-1] # last statement from StatList
op = self.children[0]
dest = IRVar().name
children = []
if isinstance(left, CallStat):
children += CallStat
callDest = IRVar().name
left = LoadStat(symbol=callDest)
else: left = left.symbol
if isinstance(right, CallStat):
children += CallStat
callDest = IRVar().name
right = LoadStat(symbol=callDest)
else: right = right.symbol
stat = BinStat(symbol=dest, left=left, right=right, op=op)
if not isinstance(left, Symbol):
children += self.children[1].children
if not isinstance(right, Symbol):
children += self.children[2].children
children += [stat]
stat_list = StatList(self.parent, children, self.symtab)
return self.parent.replace(self, stat_list)
class UnExpr(Expr):
def getOperand(self):
return self.children[1]
# FIXME: why its need? it should be lowered to Stat
def getFunction(self):
return 'global'
class CallExpr(Expr):
def __init__(self, parent=None, function=None, parameters=None, symtab=None):
self.parent=parent
self.symbol=function
if parameters :
self.children=parameters[:]
for c in self.children:
if hasattr(c, 'parent'): c.parent = self
else : self.children=[]
self.symtab = symtab
def lower(self):
children = []
params = []
for i, param in enumerate(self.children):
print self
param.lower()
param = self.children[i].children[-1]
if isinstance(param, Symbol):
params += [param]
else:
children += self.children[i].children
params += [self.children[i].children[-1].symbol]
expr = [self.symbol, params]
stat = CallStat(call_expr=expr, symtab=self.symtab)
children += [stat]
stat_list = StatList(self.parent, children, self.symtab)
return self.parent.replace(self, stat_list)
# FIXME: why its need? it should be lowered to Stat
def getFunction(self):
return 'global'
#STATEMENTS
class Stat(IRNode):
def setLabel(self, label):
self.label=label
label.value=self # set target
def getLabel(self):
return self.label
def getFunction(self):
if not self.parent : return 'global'
elif type(self.parent)== FunctionDef :
return self.parent
else :
return self.parent.getFunction()
class CallStat(Stat):
'''Procedure call (non returning)'''
def __init__(self, parent=None, call_expr=None, symtab=None):
self.parent=parent
self.call=call_expr
self.symtab=symtab
def collect_uses(self):
# FIXME: this code is broken
# return self.call.collect_uses() + self.symtab.exclude([standard_types['function'],standard_types['label']])
return self.symtab.exclude([standard_types['function'],standard_types['label']])
class IfStat(Stat):
def __init__(self, parent=None, cond=None, thenpart=None, elsepart=None, symtab=None):
self.parent=parent
self.cond=cond
self.thenpart=thenpart
self.elsepart=elsepart
self.cond.parent=self
self.thenpart.parent=self
if self.elsepart : self.elsepart.parent=self
self.symtab=symtab
def lower(self):
exit_label = standard_types['label']()
exit_stat = EmptyStat(self.parent,symtab=self.symtab)
exit_stat.setLabel(exit_label)
if self.elsepart :
then_label = standard_types['label']()
self.thenpart.setLabel(then_label)
branch_to_then = BranchStat(None,self.cond,then_label,self.symtab)
branch_to_exit = BranchStat(None,None,exit_label,self.symtab)
stat_list = StatList(self.parent, [branch_to_then,self.elsepart,branch_to_exit,self.thenpart,exit_stat], self.symtab)
return self.parent.replace(self,stat_list)
else :
branch_to_exit = BranchStat(None,UnExpr(None,['not', self.cond]),exit_label,self.symtab)
stat_list = StatList(self.parent, [branch_to_exit,self.thenpart,exit_stat], self.symtab)
return self.parent.replace(self,stat_list)
class WhileStat(Stat):
def __init__(self, parent=None, cond=None, body=None, symtab=None):
self.parent=parent
self.cond=cond
self.body=body
self.cond.parent=self
self.body.parent=self
self.symtab=symtab
def lower(self):
entry_label = standard_types['label']()
exit_label = standard_types['label']()
exit_stat = EmptyStat(self.parent,symtab=self.symtab)
exit_stat.setLabel(exit_label)
self.cond.lower()
operand = self.cond.children[-1].symbol
unary_expr_target = IRVar().name
condition = UnStat(self.parent, unary_expr_target, operand, 'not')
branch = BranchStat(None,condition,exit_label,self.symtab)
self.cond.children[0].setLabel(entry_label)
loop = BranchStat(None,Const(None, 1),entry_label,self.symtab)
stat_list = StatList(self.parent, [self.cond, condition, branch,self.body,loop,exit_stat], self.symtab)
return self.parent.replace(self,stat_list)
class ForStat(Stat):
def __init__(self, parent=None, init=None, cond=None, step=None, body=None, symtab=None):
self.parent=parent
self.init=init
self.cond=cond
self.step=step
self.body=body
self.cond.parent=self
self.body.parent=self
# self.target.parent=self
self.step.parent=self
self.symtab=symtab
def lower(self):
entry_label = standard_types['label']()
exit_label = standard_types['label']()
exit_stat = EmptyStat(self.parent,symtab=self.symtab)
exit_stat.setLabel(exit_label)
branch = BranchStat(None,UnExpr(None,['not', self.cond]),exit_label,self.symtab)
branch.setLabel(entry_label)
loop = BranchStat(None,Const(None, 1),entry_label,self.symtab)
stat_list = StatList(self.parent, [self.init, branch, self.body, self.step, loop, exit_stat], self.symtab)
return self.parent.replace(self,stat_list)
def collect_uses(self):
return self.init.collect_uses() + self.step.collect_uses()
class AssignStat(Stat):
def __init__(self, parent=None, target=None, expr=None, symtab=None):
self.parent=parent
self.symbol=target
self.expr=expr
self.expr.parent=self
self.symtab=symtab
def collect_uses(self):
try : return self.expr.collect_uses()
except AttributeError : return []
def lower(self):
self.expr.lower()
if isinstance(self.expr.children[-1], CallStat):
callDest = IRVar().name
ret = LoadStat(symbol=callDest)
self.expr.children += [ret]
source = self.expr.children[-1].symbol
stat = StoreStat(symbol=self.symbol, value=source)
children = self.expr.children + [stat]
stat_list = StatList(self.parent, children, self.symtab)
return self.parent.replace(self,stat_list)
class ArrayAssignStat(Stat):
def __init__(self, parent=None, target=None, index=None, expr=None, symtab=None):
self.parent=parent
self.symbol=target
self.index=index
self.index.parent = self
self.expr=expr
self.expr.parent=self
self.symtab=symtab
def collect_uses(self):
try : return self.expr.collect_uses()
except AttributeError : return []
def lower(self):
self.expr.lower()
if isinstance(self.expr.children[-1], Const):
source = self.expr.children[-1]
elif isinstance(self.expr.children[-1], CallStat):
callDest = IRVar().name
ret = LoadStat(symbol=callDest)
self.expr.children += [ret]
source = self.expr.children[-1].symbol
children = self.expr.children
index = self.index
if not isinstance(index, Const):
index.lower()
index = self.index.children[-1].symbol
children += self.index.children
stat = StoreArrStat(symbol=self.symbol, index=index, value=source)
children += [stat]
stat_list = StatList(self.parent, children, self.symtab)
return self.parent.replace(self,stat_list)
class BranchStat(Stat):
def __init__(self, parent=None, cond=None, target=None, symtab=None):
self.parent=parent
self.cond=cond # cond == None -> True
self.target=target
self.cond.parent=self
self.target.parent=self
self.symtab=symtab
def collect_uses(self):
try : return self.cond.collect_uses()
except AttributeError : return []
def is_unconditional(self):
try :
check=self.cond.value
return True
except AttributeError :
return False
class EmptyStat(Stat):
pass
class StoreStat(Stat):
def __init__(self, parent=None, symbol=None, value=None, symtab=None):
self.parent=parent
self.symbol=symbol
self.value=value
self.symtab=symtab
def collect_uses(self):
return [self.symbol]
class StoreArrStat(Stat):
def __init__(self, parent=None, symbol=None, index=None, value=None, symtab=None):
self.parent=parent
self.symbol=symbol
self.index=index
self.value=value
self.symtab=symtab
def collect_uses(self):
return [self.symbol]
class LoadStat(Stat):
def __init__(self, parent=None, symbol=None, value=None, symtab=None):
self.parent=parent
self.symbol=symbol
self.value=value
self.symtab=symtab
def collect_uses(self):
return []
class LoadArrStat(Stat):
def __init__(self, parent=None, symbol=None, index=None, value=None, symtab=None):
self.parent=parent
self.symbol=symbol
self.index=index
self.value=value
self.symtab=symtab
def collect_uses(self):
return []
class StatList(Stat):
def __init__(self,parent=None, children=None, symtab=None):
print 'StatList : new', id(self)
self.parent=parent
if children :
self.children=children[:]
for c in self.children :
c.parent=self
else : self.children=[]
self.symtab=symtab
def append(self, elem):
elem.parent=self
print 'StatList: appending', id(elem), 'of type', type(elem), 'to', id(self)
self.children.append(elem)
def collect_uses(self):
s = []
for c in self.children:
s += c.collect_uses()
return s
def print_content(self):
print 'StatList', id(self), ': [',
for n in self.children :
print id(n),
print ']'
def flatten(self):
'''Remove nested StatLists'''
if type(self.parent)==StatList :
print 'Flattening', id(self), 'into', id(self.parent)
for c in self.children :
c.parent=self.parent
try :
label = self.getLabel()
self.children[0].setLabel(label)
except Exception : pass
i = self.parent.children.index(self)
self.parent.children=self.parent.children[:i]+self.children+self.parent.children[i+1:]
return True
else :
print 'Not flattening', id(self), 'into', id(self.parent), 'of type', type(self.parent)
return False
class Block(Stat):
def __init__(self, parent=None, gl_sym=None, lc_sym=None, defs=None, body=None):
self.parent=parent
self.global_symtab=gl_sym
self.local_symtab=lc_sym
self.body=body
self.defs=defs
self.body.parent=self
self.defs.parent=self
class PrintStat(Stat):
def __init__(self, parent=None, symbol=None, symtab=None):
self.parent=parent
self.symbol=symbol
self.symtab=symtab
def collect_uses(self):
return [self.symbol]
class ReturnStat(Stat):
def __init__(self, parent=None, return_expr=None, symtab=None):
self.parent=parent
return_expr.parent = self
self.children=[return_expr]
self.symtab=symtab
def collect_uses(self):
return []
def lower(self):
self.children[0].lower()
value = self.children[0].children[-1]
children = []
if not isinstance(value, Symbol):
children += self.children[0].children
value = children[-1].symbol
dest = IRVar().name
stat = RetStat(self.parent, value)
children += [stat]
stat_list = StatList(self.parent, children, self.symtab)
return self.parent.replace(self, stat_list)
#DEFINITIONS
class Definition(IRNode):
def __init__(self, parent=None, symbol=None):
self.parent=parent
self.symbol=symbol
class FunctionDef(Definition):
def __init__(self, parent=None, symbol=None, body=None):
self.parent=parent
self.symbol=symbol
self.body=body
self.body.parent=self
def getGlobalSymbols(self):
return self.body.global_symtab.exclude([standard_types['function'],standard_types['label']])
def lower(self):
self.body.navigate(support.lowering)
return True
class DefinitionList(IRNode):
def __init__(self,parent=None, children=None):
self.parent=parent
if children : self.children=children
else : self.children=[]
def append(self, elem):
elem.parent=self
self.children.append(elem)
_var_id = 0
class IRVar:
def __init__(self):
global _var_id
self.id = _var_id
_var_id += 1
self.name = 't' + `self.id`
def print_stat_list(node):
'''Navigation action: print'''
print type(node), id(node)
if type(node)==StatList :
print 'StatList', id(node), ': [',
for n in node.children :
print id(n),
print ']'
class BinStat(Stat):
def __init__(self, parent=None, symbol=None, left=None, right=None, op=None):
self.parent=parent
self.symbol=symbol
self.left=left
self.right=right
self.op = op
def collect_uses(self):
return [self.symbol]
class UnStat(Stat):
def __init__(self, parent=None, symbol=None, operand=None, operation=None):
self.parent=parent
self.symbol=symbol
self.operand=operand
self.operation = operation
def collect_uses(self):
return [self.symbol]
class RetStat(Stat):
def __init__(self, parent=None, symbol=None):
self.parent=parent
self.symbol=symbol