-
Notifications
You must be signed in to change notification settings - Fork 71
/
VMObjectLayoutTests.class.st
313 lines (278 loc) · 10.8 KB
/
VMObjectLayoutTests.class.st
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
Class {
#name : #VMObjectLayoutTests,
#superclass : #VMInterpreterTests,
#category : #'VMMakerTests-ObjectLayoutTests'
}
{ #category : #helpers }
VMObjectLayoutTests >> formatFromInstSpec: instSpecInt instSize: instSizeInt [
"A class format is composed by"
"<5 bits inst spec><16 bits inst size>"
^ instSpecInt << 16 + instSizeInt
]
{ #category : #helpers }
VMObjectLayoutTests >> installClassIntoClassTableWithInstSpec: aFormatInt instSize: aSizeInt [
| class |
class := self
newClassInOldSpaceWithSlots: aSizeInt
instSpec: aFormatInt.
memory ensureBehaviorHash: class.
^class
]
{ #category : #helper }
VMObjectLayoutTests >> objSizeWithNumberOfSlots: numberOfSlots [
| objSize |
"always have at least one slot for forwarders"
numberOfSlots = 0 ifTrue: [ ^ self objectHeaderSize + 8 ].
objSize := numberOfSlots * wordSize.
"we align to 8 bytes"
objSize % 8 = 0 ifFalse: [ objSize := objSize + wordSize ].
"extra header for big objects"
numberOfSlots > 254 ifTrue: [ objSize := objSize + self objectHeaderSize ].
^ self objectHeaderSize + objSize
]
{ #category : #tests }
VMObjectLayoutTests >> testCharacterIsImmediate [
| char |
char := memory characterObjectOf: $a asInteger.
self assert: (memory isImmediate: char).
self assert: (memory fetchClassTagOf: char) equals: 2
]
{ #category : #tests }
VMObjectLayoutTests >> testHeaderOfObjectEncodesTheCorrectAmountOfSlots [
| class objOop |
0 to: 254 do: [ :slots |
| objSize |
class := self
installClassIntoClassTableWithInstSpec: 1
instSize: slots.
objOop := memory instantiateClass: class.
objSize := self objSizeWithNumberOfSlots: slots.
self assert: (memory slotSizeOf: objOop) equals: slots.
self
assert: (memory bytesInObject: objOop)
equals: objSize ]
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testNegativeIntegerValuesInRange [
self assert: (memory isIntegerValue: memory minSmallInteger)
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testNegativeIntegerValuesNotInRange [
"An integer smaller than the smallest integer is not in a valid range"
self deny: (memory isIntegerValue: memory minSmallInteger - 1)
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectAlignment [
| class objOop1 objOop2 instSpec |
instSpec := 0.
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
objOop1 := memory instantiateClass: class.
objOop2 := memory instantiateClass: class.
self assert: objOop1 \\ 8 equals: 0.
self assert: objOop2 \\ 8 equals: 0
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesAmountOfSlots [
| class objOop header |
0 to: 254 do: [ :slots |
class := self installClassIntoClassTableWithInstSpec: 1 instSize: slots.
objOop := memory instantiateClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 56 bitAnd: 16rFF) equals: slots
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesClassIndex [
| class objOop header classIndex |
0 to: 10 do: [ :slots |
class := self installClassIntoClassTableWithInstSpec: 1 instSize: 0.
objOop := memory instantiateClass: class.
classIndex := memory rawHashBitsOf: class.
header := memory baseHeader: objOop.
memory fetchClassTagOf: objOop.
self assert: (header bitAnd: 16r3FFFFF) equals: classIndex
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesObjecFormatForFixedLayout [
| class objOop header classInstSpec |
"instSpec:
0 = 0 sized objects (UndefinedObject True False et al)
1 = non-indexable objects with inst vars (Point et al)"
0 to: 1 do: [ :instSpec |
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
objOop := memory instantiateClass: class.
classInstSpec := memory instSpecOfClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 24 bitAnd: 16r1F) equals: classInstSpec
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesObjecFormatForIndexableOpaqueLayout16Bit [
| class objOop header classInstSpec instSpec bits maxSize |
"instSpec: 12-15 = 16-bit indexable
12 -> padding 0-bits
13 -> padding 16-bits
14 -> padding 32-bits
15 -> padding 48-bits"
bits := 16.
instSpec := 12.
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
maxSize := (wordSize * 8) / bits.
1 to: maxSize do: [ :size |
| padding |
padding := (wordSize * 8) - (size * bits).
objOop := memory instantiateClass: class indexableSize: size.
classInstSpec := memory instSpecOfClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 24 bitAnd: 16r1F) equals: classInstSpec + (padding / bits)
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesObjecFormatForIndexableOpaqueLayout32Bit [
| class objOop header classInstSpec instSpec bits maxSize |
"instSpec: 10-11 = 32-bit indexable
10 -> padding 0-bits
11 -> padding 32-bits"
bits := 32.
instSpec := 10.
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
maxSize := (wordSize * 8) / bits.
1 to: maxSize do: [ :size |
| padding |
padding := (wordSize * 8) - (size * bits).
objOop := memory instantiateClass: class indexableSize: size.
classInstSpec := memory instSpecOfClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 24 bitAnd: 16r1F) equals: classInstSpec + (padding / bits)
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesObjecFormatForIndexableOpaqueLayout8Bit [
| class objOop header classInstSpec instSpec bits maxSize |
"instSpec: 16-23 = 8-bit indexable
16 -> padding 0-bits
17 -> padding 8-bits
18 -> padding 16-bits
19 -> padding 24-bits
20 -> padding 32-bits
21 -> padding 40-bits
22 -> padding 48-bits
23 -> padding 56-bits"
bits := 8.
instSpec := 16. "8-bit indexable"
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
maxSize := wordSize .
1 to: maxSize do: [ :size |
| padding |
padding := (wordSize * bits) - (size * bits).
objOop := memory instantiateClass: class indexableSize: size.
classInstSpec := memory instSpecOfClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 24 bitAnd: 16r1F) equals: classInstSpec + (padding / bits)
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectHeaderEncodesObjecFormatForIndexablePointerLayout [
| class objOop header classInstSpec instSpec |
instSpec := 2. "instSpec for indexable objects with no inst vars (Array et al)"
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
1 to: 10 do: [ :size |
objOop := memory instantiateClass: class indexableSize: size.
classInstSpec := memory instSpecOfClass: class.
header := memory baseHeader: objOop.
self assert: (header >> 24 bitAnd: 16r1F) equals: classInstSpec
]
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectMinimumSize [
| class objOop1 objOop2 instSpec |
instSpec := 0.
class := self installClassIntoClassTableWithInstSpec: instSpec instSize: 0.
objOop1 := memory instantiateClass: class.
objOop2 := memory instantiateClass: class.
self assert: objOop2 - objOop1 equals: 16
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectWith0SlotsIsAreAlwaysAligned [
| class slots obj1oop obj2oop |
"objects always are allocated with at least one slots for forwarding"
slots := 0.
class := self installClassIntoClassTableWithInstSpec: 1 instSize: slots.
obj1oop := memory instantiateClass: class.
obj2oop := memory instantiateClass: class.
self assert: obj2oop - obj1oop equals: self objectHeaderSize + memory allocationUnit
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectWith0SlotsIsAreAlwaysWithAtLeastOneSlotsForForwarding [
| class slots oop |
slots := 0.
class := self installClassIntoClassTableWithInstSpec: 1 instSize: slots.
oop := memory instantiateClass: class.
self assert: (memory bytesInObject: oop) equals: self objectHeaderSize + memory allocationUnit.
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectWithMoreThan254SlotsHasTheCorrectSize [
| class objOop slots |
slots := 255.
class := self
installClassIntoClassTableWithInstSpec: 1
instSize: slots.
objOop := memory instantiateClass: class.
self assert: (memory slotSizeOf: objOop) equals: slots.
self
assert: (memory bytesInObject: objOop)
equals: (self objSizeWithNumberOfSlots: slots).
]
{ #category : #tests }
VMObjectLayoutTests >> testObjectWithMoreThan254SlotsHasTwoHeaders [
| class objOop bigOopHeader mask numSlots |
mask := 16rFFFFFFFFFFFFFF.
255 to: 265 do: [ :slots |
class := self
installClassIntoClassTableWithInstSpec: 1
instSize: slots.
objOop := memory instantiateClass: class.
bigOopHeader := memory baseHeader: objOop - self objectHeaderSize. "header before the main header"
numSlots := bigOopHeader bitAnd: mask.
self assert: numSlots equals: slots ]
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testPositiveIntegerBorderCase1 [
"Test of the border case when int = 2r000111111... . The highest possible value using usqInt encoding is (2**61) -1 since (2**61) can be confused with a pointer (on a 64 bits machine)
Regarding the cCode implementation, the sign bit can be lost if this test fails. The maxCInteger and numSmallIntegerTagBits guarantees the portability
of the test on 32 and 64 bit computer.
"
self deny: (memory isIntegerValue: memory maxCInteger >> memory numSmallIntegerTagBits)
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testPositiveIntegerBorderCase2 [
"Test of the border case when int = 2r111000000 ... . Regarding the cCode implementation, the sign bit can be lost if this test fails. The maxCInteger and numSmallIntegerTagBits guaranties the portability of the test on 32 and 64 bit computer. "
self deny: (memory isIntegerValue: (memory maxCInteger >> memory numSmallIntegerTagBits) bitInvert) "<=> isIntegerValue: (0001111) bitInvert"
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testPositiveIntegerValuesInRange [
self assert: (memory isIntegerValue: memory maxSmallInteger)
]
{ #category : #'tests - integerValues' }
VMObjectLayoutTests >> testPositiveIntegerValuesNotInRange [
self deny: (memory isIntegerValue: memory maxSmallInteger + 1)
]
{ #category : #tests }
VMObjectLayoutTests >> testSmallIntegerIsImmediate [
| int |
int := memory integerObjectOf: 42.
self assert: (memory isImmediate: int).
self assert: (memory fetchClassTagOf: int) equals: 1
]
{ #category : #tests }
VMObjectLayoutTests >> testVariableObjectWithInstVarsHasTheRightSize [
| class objOop fixedFieldsSize indexableSize |
indexableSize := 12.
fixedFieldsSize := 5.
class := self
installClassIntoClassTableWithInstSpec: 3
instSize: fixedFieldsSize.
objOop := memory instantiateClass: class indexableSize: indexableSize.
self assert: (memory slotSizeOf: objOop) equals: fixedFieldsSize + indexableSize
]