-
Notifications
You must be signed in to change notification settings - Fork 15
/
constants.py
268 lines (229 loc) · 9.21 KB
/
constants.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
import sys
import time
from rpython.rlib.rarithmetic import r_int64, r_uint
from rsqueakvm.util.bitmanipulation import splitter
# ___________________________________________________________________________
# Slot Names
CHARACTER_VALUE_INDEX = 0 # Page 630 of the blue book
STREAM_ARRAY_INDEX = 0 # Page 631 of the blue book
STREAM_INDEX_INDEX = 1
STREAM_READ_LIMIT_INDEX = 2
STREAM_WRITE_LIMIT_INDEX = 3
CLASS_SUPERCLASS_INDEX = 0
CLASS_METHODDICT_INDEX = 1
CLASS_FORMAT_INDEX = 2
CLASS_NAME_INDEX = 6 # in the mini.image, at least
# MethodDict
METHODDICT_TALLY_INDEX = 0
METHODDICT_VALUES_INDEX = 1
METHODDICT_NAMES_INDEX = 2
# Message
MESSAGE_SELECTOR_INDEX = 0
MESSAGE_ARGUMENTS_INDEX = 1
MESSAGE_LOOKUP_CLASS_INDEX = 2
# ContextPart
CTXPART_SENDER_INDEX = 0
CTXPART_PC_INDEX = 1
CTXPART_STACKP_INDEX = 2
METHOD_HEADER_INDEX = 0
# BlockContext < ContextPart
BLKCTX_BLOCK_ARGUMENT_COUNT_INDEX = 3
BLKCTX_INITIAL_IP_INDEX = 4
BLKCTX_HOME_INDEX = 5
BLKCTX_STACK_START = 6
# MethodContext < ContextPart
MTHDCTX_METHOD = 3
MTHDCTX_CLOSURE_OR_NIL = 4
MTHDCTX_RECEIVER = 5
MTHDCTX_TEMP_FRAME_START = 6
# BlockClosure < Object
BLKCLSR_OUTER_CONTEXT = 0
BLKCLSR_STARTPC = 1
BLKCLSR_NUMARGS = 2
BLKCLSR_SIZE = 3
FORM_BITS = 0
FORM_WIDTH = 1
FORM_HEIGHT = 2
FORM_DEPTH = 3
# ___________________________________________________________________________
# Miscellaneous constants
COMPILED_METHOD_FULL_FRAME_SIZE = 56
COMPILED_METHOD_SMALL_FRAME_SIZE = 16
LITERAL_START = 1 # index of the first literal after the method header
BYTES_PER_WORD = 4
BITS_PER_WORD = BYTES_PER_WORD * 8
WORDS_IN_FLOAT = 2 # Fixed number of word-slots in a Squeak Float object
INTERP_PROXY_MAJOR = 1
INTERP_PROXY_MINOR = 13
# The Delta between Squeak Epoch (Jan 1st 1901) and POSIX Epoch (Jan 1st 1970)
SQUEAK_EPOCH_DELTA_MICROSECONDS = r_int64(2177452800000000L)
# ___________________________________________________________________________
# Special objects indices
SPECIAL_OBJECTS_SIZE = 70 # some room
SO_NIL = 0
SO_FALSE = 1
SO_TRUE = 2
SO_SCHEDULERASSOCIATIONPOINTER = 3
SO_BITMAP_CLASS = 4
SO_SMALLINTEGER_CLASS = 5
SO_STRING_CLASS = 6
SO_ARRAY_CLASS = 7
SO_SMALLTALK = 8 # Deprecated
SO_FLOAT_CLASS = 9
SO_METHODCONTEXT_CLASS = 10
SO_BLOCKCONTEXT_CLASS = 11
SO_POINT_CLASS = 12
SO_LARGEPOSITIVEINTEGER_CLASS = 13
SO_DISPLAY_OBJECT = 14
SO_MESSAGE_CLASS = 15
SO_COMPILEDMETHOD_CLASS = 16
SO_LOW_SPACE_SEMAPHORE = 17
SO_SEMAPHORE_CLASS = 18
SO_CHARACTER_CLASS = 19
SO_DOES_NOT_UNDERSTAND = 20
SO_CANNOT_RETURN = 21
SO_PROCESS_SIGNALIGN_LOW_SPACE = 22 # the process that triggered the low space semaphore. mostly nil
SO_SPECIAL_SELECTORS_ARRAY = 23
SO_CHARACTER_TABLE_ARRAY = 24
SO_MUST_BE_BOOLEAN = 25
SO_BYTEARRAY_CLASS = 26
SO_PROCESS_CLASS = 27
SO_COMPACT_CLASSES_ARRAY = 28
SO_TIMER_SEMAPHORE = 29
SO_USER_INTERRUPT_SEMAPHORE = 30
SO_FLOAT_ZERO = 31
SO_LARGEPOSITIVEINTEGER_ZERO = 32
SO_A_POINT = 33
SO_CANNOT_INTERPRET = 34
SO_A_METHODCONTEXT = 35 # deprecated in closure images
SO_BLOCKCLOSURE_CLASS = 36
SO_A_BLOCKCONTEXT = 37 # deprecated in closure images
SO_EXTERNAL_OBJECTS_ARRAY = 38
SO_PSEUDOCONTEXT_CLASS = 39
SO_TRANSLATEDMETHOD_CLASS = 40
SO_FINALIZATION_SEMPAHORE = 41
SO_LARGENEGATIVEINTEGER_CLASS = 42
SO_RUN_WITH_IN = 49
SO_JIT_HOOK = 58 # really selectorCounterTripped
SO_JIT_HOOK_RCVR = 59 # really selectorTrap
constant_objects_in_special_object_table = {
"nil": (SO_NIL, "FIXED"),
"true": (SO_TRUE, "FIXED"),
"false": (SO_FALSE, "FIXED"),
"charactertable": (SO_CHARACTER_TABLE_ARRAY, "POINTERS"),
"schedulerassociationpointer": (SO_SCHEDULERASSOCIATIONPOINTER, "FIXED"),
"special_selectors": (SO_SPECIAL_SELECTORS_ARRAY, "POINTERS"),
"smalltalkdict": (SO_SMALLTALK, "FIXED"),
"doesNotUnderstand": (SO_DOES_NOT_UNDERSTAND, "BYTES"),
"mustBeBoolean": (SO_MUST_BE_BOOLEAN, "BYTES"),
"runWithIn": (SO_RUN_WITH_IN, "BYTES"),
"cannotReturn": (SO_CANNOT_RETURN, "BYTES"),
# classes
"Bitmap": (SO_BITMAP_CLASS, "FIXED"),
"SmallInteger": (SO_SMALLINTEGER_CLASS, "FIXED"),
"String": (SO_STRING_CLASS, "FIXED"),
"Array": (SO_ARRAY_CLASS, "FIXED"),
"Float": (SO_FLOAT_CLASS, "FIXED"),
"MethodContext": (SO_METHODCONTEXT_CLASS, "FIXED"),
"BlockContext": (SO_BLOCKCONTEXT_CLASS, "FIXED"),
"BlockClosure": (SO_BLOCKCLOSURE_CLASS, "FIXED"),
"Point": (SO_POINT_CLASS, "FIXED"),
"LargePositiveInteger": (SO_LARGEPOSITIVEINTEGER_CLASS, "FIXED"),
"Message": (SO_MESSAGE_CLASS, "FIXED"),
"CompiledMethod": (SO_COMPILEDMETHOD_CLASS, "FIXED"),
"Semaphore": (SO_SEMAPHORE_CLASS, "FIXED"),
"Character": (SO_CHARACTER_CLASS, "FIXED"),
"ByteArray": (SO_BYTEARRAY_CLASS, "FIXED"),
"Process": (SO_PROCESS_CLASS, "FIXED"),
# "PseudoContext" : (SO_PSEUDOCONTEXT_CLASS, "FIXED"),
# "TranslatedMethod" : (SO_TRANSLATEDMETHOD_CLASS, "FIXED"),
"LargeNegativeInteger" : (SO_LARGENEGATIVEINTEGER_CLASS, "FIXED"),
# ours, not in the table, but we'd like it to
"ClassBinding": (SPECIAL_OBJECTS_SIZE + 30, "FIXED"),
"Metaclass": (SPECIAL_OBJECTS_SIZE + 31, "FIXED"),
"Processor": (SPECIAL_OBJECTS_SIZE + 32, "FIXED"),
"ByteSymbol": (SPECIAL_OBJECTS_SIZE + 33, "FIXED")
}
variables_in_special_object_table = {
"display": SO_DISPLAY_OBJECT,
"interrupt_semaphore": SO_USER_INTERRUPT_SEMAPHORE,
"timerSemaphore": SO_TIMER_SEMAPHORE,
"low_space_semaphore": SO_LOW_SPACE_SEMAPHORE,
"jit_hook_selector": SO_JIT_HOOK,
"jit_hook_receiver": SO_JIT_HOOK_RCVR
}
def init_special_objects_mapping(constant_objects_in_special_object_table):
d = {}
for name, (idx, t) in constant_objects_in_special_object_table.items():
d[name] = idx
return d
constant_objects_in_special_object_table_wo_types = init_special_objects_mapping(constant_objects_in_special_object_table)
objects_in_special_object_table = {}
objects_in_special_object_table.update(constant_objects_in_special_object_table_wo_types)
objects_in_special_object_table.update(variables_in_special_object_table)
from rpython.rlib.rarithmetic import LONG_BIT
TAGGED_MAXINT = 2 ** (LONG_BIT - 2) - 1
TAGGED_MININT = -2 ** (LONG_BIT - 2)
TAGGED_MAXINT32 = 2 ** (32 - 2) - 1
TAGGED_MININT32 = -2 ** (32 - 2)
TAGGED_MASK = int(2 ** (LONG_BIT - 1) - 1)
MAXINT = sys.maxint
MININT = -sys.maxint-1
U_MAXINT = r_uint(2 ** LONG_BIT - 1)
if LONG_BIT == 32:
IS_64BIT = False
BYTES_PER_MACHINE_INT = 4
BYTES_PER_MACHINE_LONGLONG = 8
elif LONG_BIT == 64:
IS_64BIT = True
BYTES_PER_MACHINE_INT = 8
BYTES_PER_MACHINE_LONGLONG = 8
else:
assert False
# Entries into SO_SPECIAL_SELECTORS_ARRAY:
#(#+ 1 #- 1 #< 1 #> 1 #<= 1 #>= 1 #= 1 #~= 1 #* 1 #/ 1 #\\ 1 #@ 1 #bitShift: 1 #// 1 #bitAnd: 1 #bitOr: 1 #at: 1 #at:put: 2 #size 0 #next 0 #nextPut: 1 #atEnd 0 #== 1 #class 0 #blockCopy: 1 #value 0 #value: 1 #do: 1 #new 0 #new: 1 #x 0 #y 0)
SPECIAL_SELECTORS = ['+', '-', '<', '>', '<=', '>=', '=', '~=', '*', '/', '\\\\',
'@', 'bitShift:', '//', 'bitAnd:', 'bitOr:', 'at:',
'at:put:', 'size', 'next', 'nextPut:', 'atEnd', '==',
'class', 'blockCopy:', 'value', 'value:', 'do:', 'new',
'new:', 'x', 'y']
def find_selectorindex(selector):
return SPECIAL_SELECTORS.index(selector) * 2
find_selectorindex._annspecialcase_ = "specialize:memo"
def decode_compiled_method_header(header):
"""Decode 30-bit method header and apply new format.
(index 0) 9 bits: main part of primitive number (#primitive)
(index 9) 8 bits: number of literals (#numLiterals)
(index 17) 1 bit: whether a large frame size is needed (#frameSize)
(index 18) 6 bits: number of temporary variables (#numTemps)
(index 24) 4 bits: number of arguments to the method (#numArgs)
(index 28) 1 bit: high-bit of primitive number (#primitive)
(index 29) 1 bit: flag bit, ignored by the VM (#flag)
"""
if header < 0: # sign bit == 1 (bit index depends on 32b/64b)
return decode_alternate_compiled_method_header(header)
primitive, literalsize, islarge, tempsize, numargs, highbit = (
splitter[9,8,1,6,4,1](header))
primitive = primitive + (highbit << 9)
assert tempsize >= numargs
return primitive, literalsize, islarge, tempsize, numargs
def decode_alternate_compiled_method_header(header):
"""Decode 30-bit method header and apply new format.
(index 0) 16 bits: number of literals (#numLiterals)
(index 16) 1 bit: has primitive
(index 17) 1 bit: whether a large frame size is needed (#frameSize)
(index 18) 6 bits: number of temporary variables (#numTemps)
(index 24) 4 bits: number of arguments to the method (#numArgs)
(index 28) 2 bits: reserved for an access modifier (00-unused, 01-private, 10-protected, 11-public)
"""
literalsize, has_primitive, islarge, tempsize, numargs, access_mod = (
splitter[16,1,1,6,4,2](header))
return bool(has_primitive), literalsize, islarge, tempsize, numargs
#___________________________________________________________________________
# Interpreter constants
#
INTERRUPT_COUNTER_SIZE = 10000
LITERAL_LIST_UNROLL_SIZE = 7 # up to which size the wrapping of literal arrays in methods is unrolled
CompileTime = time.time()
SYSTEM_ATTRIBUTE_IMAGE_NAME_INDEX = 1
SYSTEM_ATTRIBUTE_IMAGE_ARGS_INDEX = 2