This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datalayout.py
57 lines (42 loc) · 1.64 KB
/
datalayout.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
#!/usr/bin/env python3
"""Data layout computation pass. Each symbol whose location (alloct)
is not a register, is allocated in the local stack frame (LocalSymbol) or in
the data section of the executable (GlobalSymbol)."""
class SymbolLayout(object):
def __init__(self, symname, bsize):
self.symname = symname
self.bsize = bsize
class LocalSymbolLayout(SymbolLayout):
def __init__(self, symname, fpreloff, bsize):
self.symname = symname
self.fpreloff = fpreloff
self.bsize = bsize
def __repr__(self):
return self.symname + ": fp + (" + repr(self.fpreloff) + ") [def byte " + \
repr(self.bsize) + "]"
class GlobalSymbolLayout(SymbolLayout):
def __init__(self, symname, bsize):
self.symname = symname
self.bsize = bsize
def __repr__(self):
return self.symname + ": def byte " + repr(self.bsize)
def perform_data_layout(root):
perform_data_layout_of_program(root)
for defin in root.defs.children:
perform_data_layout_of_function(defin)
def perform_data_layout_of_function(funcroot):
offs = 0 # prev fp
prefix = "_l_" + funcroot.symbol.name + "_"
for var in funcroot.body.symtab:
if var.stype.size == 0:
continue
bsize = var.stype.size // 8
offs -= bsize
var.set_alloc_info(LocalSymbolLayout(prefix + var.name, offs, bsize))
funcroot.body.stackroom = -offs
def perform_data_layout_of_program(root):
prefix = "_g_"
for var in root.symtab:
if var.stype.size == 0:
continue
var.set_alloc_info(GlobalSymbolLayout(prefix + var.name, var.stype.size // 8))