Debian Codegraph @

rpython/memory/lltypelayout.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype, llmemory, llarena
2 from rpython.rlib.rarithmetic import is_emulated_long
3
4 import struct
5
6 memory_alignment = struct.calcsize("P")
7
8 primitive_to_fmt = {lltype.Signed: "l",
9 lltype.Unsigned: "L",
10 lltype.Char: "c",
11 lltype.UniChar: "i", # 4 bytes
12 lltype.Bool: "B",
13 lltype.Float: "d",
14 llmemory.Address: "P",
15 }
16 if is_emulated_long:
17 primitive_to_fmt.update( {
18 lltype.Signed: "q",
19 lltype.Unsigned: "Q",
20 } )
21
22 #___________________________________________________________________________
23 # Utility functions that know about the memory layout of the lltypes
24 # in the simulation
25
26 #returns some sort of layout information that is useful for the simulatorptr
27 def get_layout(TYPE):
28 layout = {}
29 if isinstance(TYPE, lltype.Primitive):
30 try:
31 return primitive_to_fmt[TYPE]
32 except KeyError:
33 from rpython.rtyper.lltypesystem import rffi
34 return rffi.sizeof(TYPE)
35 elif isinstance(TYPE, lltype.Ptr):
36 return "P"
37 elif isinstance(TYPE, lltype.Struct):
38 curr = 0
39 for name in TYPE._names:
40 layout[name] = curr
41 curr += get_fixed_size(TYPE._flds[name])
42 layout["_size"] = curr
43 return layout
44 elif isinstance(TYPE, lltype.Array):
45 return (get_fixed_size(lltype.Signed), get_fixed_size(TYPE.OF))
46 elif isinstance(TYPE, lltype.OpaqueType):
47 return "i"
48 elif isinstance(TYPE, lltype.FuncType):
49 return "i"
50 else:
51 assert 0, "type %s not yet implemented" % (TYPE, )
52
53 def get_fixed_size(TYPE):
54 if isinstance(TYPE, lltype.Primitive):
55 if TYPE == lltype.Void:
56 return 0
57 try:
58 return struct.calcsize(primitive_to_fmt[TYPE])
59 except KeyError:
60 from rpython.rtyper.lltypesystem import rffi
61 return rffi.sizeof(TYPE)
62 elif isinstance(TYPE, lltype.Ptr):
63 return struct.calcsize("P")
64 elif isinstance(TYPE, lltype.Struct):
65 return get_layout(TYPE)["_size"]
66 elif isinstance(TYPE, lltype.Array):
67 return get_fixed_size(lltype.Unsigned)
68 elif isinstance(TYPE, lltype.OpaqueType):
69 return get_fixed_size(lltype.Unsigned)
70 elif isinstance(TYPE, lltype.FuncType):
71 return get_fixed_size(lltype.Unsigned)
72 assert 0, "not yet implemented"
73
74 def get_variable_size(TYPE):
75 if isinstance(TYPE, lltype.Array):
76 return get_fixed_size(TYPE.OF)
77 elif isinstance(TYPE, lltype.Primitive):
78 return 0
79 elif isinstance(TYPE, lltype.Struct):
80 if TYPE._arrayfld is not None:
81 return get_variable_size(TYPE._flds[TYPE._arrayfld])
82 else:
83 return 0
84 elif isinstance(TYPE, lltype.OpaqueType):
85 return 0
86 elif isinstance(TYPE, lltype.FuncType):
87 return 0
88 elif isinstance(TYPE, lltype.Ptr):
89 return 0
90 else:
91 assert 0, "not yet implemented"
92
93 def sizeof(TYPE, i=None):
94 fixedsize = get_fixed_size(TYPE)
95 varsize = get_variable_size(TYPE)
96 if i is None:
97 assert varsize == 0
98 return fixedsize
99 else:
100 return fixedsize + i * varsize
101
102 def convert_offset_to_int(offset):
103 if isinstance(offset, llmemory.FieldOffset):
104 layout = get_layout(offset.TYPE)
105 return layout[offset.fldname]
106 elif isinstance(offset, llmemory.CompositeOffset):
107 return sum([convert_offset_to_int(item) for item in offset.offsets])
108 elif type(offset) == llmemory.AddressOffset:
109 return 0
110 elif isinstance(offset, llmemory.ItemOffset):
111 return sizeof(offset.TYPE) * offset.repeat
112 elif isinstance(offset, llmemory.ArrayItemsOffset):
113 if offset.TYPE._hints.get('nolength', None):
114 return 0
115 return get_fixed_size(lltype.Signed)
116 elif isinstance(offset, llmemory.GCHeaderOffset):
117 return sizeof(offset.gcheaderbuilder.HDR)
118 elif isinstance(offset, llmemory.ArrayLengthOffset):
119 return 0
120 elif isinstance(offset, llarena.RoundedUpForAllocation):
121 basesize = convert_offset_to_int(offset.basesize)
122 if isinstance(offset.minsize, llmemory.AddressOffset):
123 minsize = convert_offset_to_int(offset.minsize)
124 if minsize > basesize:
125 basesize = minsize
126 mask = memory_alignment - 1
127 return (basesize + mask) & ~ mask
128 else:
129 raise Exception("unknown offset type %r"%offset)