-
Notifications
You must be signed in to change notification settings - Fork 65
/
CogARMCompilerTests.class.st
299 lines (266 loc) · 9.38 KB
/
CogARMCompilerTests.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
Class {
#name : #CogARMCompilerTests,
#superclass : #AbstractInstructionTests,
#category : #'VMMaker-OriginalTests'
}
{ #category : #running }
CogARMCompilerTests >> assertCheckLongArithOpCodeSize: bytes [
self assert: bytes > 4
]
{ #category : #running }
CogARMCompilerTests >> assertCheckQuickArithOpCodeSize: bytes [
"The problem is that there are negative value, which are not quick encodable in ARM"
self assert: bytes <= 4
]
{ #category : #accessing }
CogARMCompilerTests >> concreteCompilerClass [
^CogARMCompilerForTests
]
{ #category : #private }
CogARMCompilerTests >> disassembleInstructionAt: index In: inst into: binaryBlock [
"Manage the fact that in the simulator inst machineCode object is an Array and the disassembler requires a ByteArray or some such."
^self processor
disassembleInstructionAt: index
In: (self memoryAsBytes: inst machineCode object)
into: binaryBlock
]
{ #category : #private }
CogARMCompilerTests >> memoryAsBytes: instructionMachineCode [
"Manage the fact that in the simulator inst machineCode object is an Array and the disassembler requires a ByteArray or some such."
| bytes |
instructionMachineCode isCObjectAccessor ifTrue:
[^self memoryAsBytes: instructionMachineCode object].
instructionMachineCode isArray ifFalse:
[self assert: instructionMachineCode class isBits.
^instructionMachineCode].
bytes := ByteArray new: instructionMachineCode size * 4.
1 to: instructionMachineCode size do:
[:i|
(instructionMachineCode at: i) ifNotNil:
[:word|
bytes unsignedLongAt: i - 1* 4 + 1 put: word]].
^bytes
]
{ #category : #'cogit compatibility' }
CogARMCompilerTests >> objectMemory [
^self
]
{ #category : #accessing }
CogARMCompilerTests >> processor [
processor ifNil:
[processor := GdbARMAlien new].
^processor
]
{ #category : #accessing }
CogARMCompilerTests >> strip: aFancyDisassembledInstruction [
"When an immediate > 15, the disassembler appends ' ; 0x\d\d'. That is stripped. Also strip any prepended stuff delimted by $: - perhaps memory addresses etc?"
^((aFancyDisassembledInstruction
allButFirst: (aFancyDisassembledInstruction indexOf: $: ifAbsent:[-1]) + 1)
copyUpTo: $;) withBlanksTrimmed
]
{ #category : #tests }
CogARMCompilerTests >> testAdd [
"self new testAdd"
"the forms are valid, "
"test AddCqR"
self concreteCompilerClass registersWithNamesDo: [ :reg :regName |
#(0 16rF 16rFF) do:
[:n| | inst len |
inst := self gen: AddCqR operand: n operand: reg.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 0
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'adds ', regName, ', ', regName, ', #', n asString.
self assert: herIntended equals: plainJane]]].
"test AddCwR"
self concreteCompilerClass registersWithNamesDo: [ :reg :regName |
#(16rFFFFFFFF 16r88888888 0) do:
[:n| | inst len |
inst := self gen: AddCwR operand: n operand: reg.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 0
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'mov ip, #', (n bitAnd: 16rFF << 24) signedIntFromLong asString.
self assert: herIntended equals: plainJane].
self
disassembleInstructionAt: 4
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'orr ip, ip, #', (n bitAnd: 16rFF << 16) asString.
self assert: herIntended equals: plainJane].
self
disassembleInstructionAt: 8
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'orr ip, ip, #', (n bitAnd: 16rFF << 8) signedIntFromLong asString.
self assert: herIntended equals: plainJane].
self
disassembleInstructionAt: 12
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'orr ip, ip, #', (n bitAnd: 16rFF) asString.
self assert: herIntended equals: plainJane].
self
disassembleInstructionAt: 16
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'adds ', regName, ', ', regName, ', ip'.
self assert: herIntended equals: plainJane]]]
]
{ #category : #tests }
CogARMCompilerTests >> testGenerateMul [
"self new testGenerateMul"
"Currently demonstrates that the CogARMCompiler>genMulR:R: algorithm ain't gonna work too well"
|memory compiler |
compiler := self gen: nil. "grab something usefully like a compiler for later abuse"
self resetGen. "initialise important stuff"
"test MulRR"
self gen: LogicalShiftLeftCqR operand: 1 operand:1. "LSL r1 - to make the right bit pattern for a SmallInt that has had the flag masked out"
self gen: LogicalShiftLeftCqR operand: 1 operand:0. "LSL r1 - to make the right bit pattern for a SmallInt that has had the flag masked out"
self gen: MoveRR operand: 0 operand: 2.
compiler genMulR: 1 R: 0.
memory := self generateInstructions.
self disassembleOpcodesIn: memory to: Transcript .
{SmallInteger maxVal@2. SmallInteger minVal@2. 0@0. 1@1. -1@0. -1@1. -1@ -1} do:[:val | | x y |
x := val x.
y := val y.
self processor reset.
processor signedLongAt: 13 put: x;
signedLongAt: 17 put: y.
[[processor pc < memory size] whileTrue:
[self processor singleStepIn: memory]]
on: Error
do: [:ex| ].
Transcript show: x asString, ' * ', y asString; cr.
processor printRegisterState: processor registerState on: Transcript.
Transcript flush]
]
{ #category : #tests }
CogARMCompilerTests >> testMoveCwR [
"self new testMoveCwR"
#(16rFF00FF00 16r00000012 16r12345678) do:
[:n|
self concreteCompilerClass dataRegistersWithAccessorsDo:
[ :r :rgetter :rset | | inst len memory |
inst := self gen: MoveCwR operand: n operand: r.
len := inst concretizeAt: 0.
memory := self memoryAsBytes: inst machineCode.
self processor
reset.
[[processor pc < inst machineCodeSize] whileTrue:
[self processor singleStepIn: memory]]
on: Error
do: [:ex| ].
self concreteCompilerClass dataRegistersWithAccessorsDo:
[:ireg :getter :setter| | expected |
expected := getter == rgetter ifTrue: [ n ] ifFalse: [0].
self assert: (self processor perform: getter) = expected].
self assert: self processor pc = 16]]
]
{ #category : #tests }
CogARMCompilerTests >> testPerformAdd [
"self new testPerformAdd"
| memory |
#(16rFF00FF00 16r00000012 16r12345678) do:
[:n| | |
self concreteCompilerClass dataRegistersWithAccessorsDo:
[ :r :rgetter :rset |
self resetGen. "initialise important stuff"
self gen: MoveCwR operand: n operand: r.
self gen: AddCqR operand: 42 operand: r.
memory := self generateInstructions.
"self disassembleOpcodesIn: memory to: Transcript."
self processor
reset.
[[processor pc < memory size] whileTrue:
[self processor singleStepIn: memory]]
on: Error
do: [:ex| ].
self concreteCompilerClass dataRegistersWithAccessorsDo:
[:ireg :getter :setter| | expected |
expected := getter == rgetter ifTrue: [ n + 42] ifFalse: [0].
self assert: (self processor perform: getter) = expected].
self assert: self processor pc = 20]]
]
{ #category : #tests }
CogARMCompilerTests >> testPrefetchAw [
"self new testPrefetchAw"
#(16rFF00FF00 16r00000012) do:
[:n| | inst len |
inst := self gen: PrefetchAw operand: n.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 12
In: inst
into: [:str :sz| | plainJane herIntended |
"Convert e.g. '00000000: movl %eax, 0x2(%eax) : 89 40 02' to 'movl %eax, 0x2(%eax)'"
plainJane := self strip: str.
herIntended := 'pld [sl', ((n bitAnd: 16rFF) = 0 ifTrue: [''] ifFalse: [', #', (n bitAnd: 16rFF) asString]) ,']'.
self assert: herIntended equals: plainJane.
self assert: len = 16]].
]
{ #category : #tests }
CogARMCompilerTests >> testPushR [
"self new testPushR"
self concreteCompilerClass registersWithNamesDo:
[:r :name | | inst len |
inst := self gen: PushR operand: r.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 0
In: inst
into: [:str :sz| | plainJane herIntended |
"Convert e.g. '00000000: movl %eax, 0x2(%eax) : 89 40 02' to 'movl %eax, 0x2(%eax)'"
plainJane := self strip: str.
herIntended := 'push {', name ,'}'.
self assert: herIntended equals: plainJane.
self assert: len = 4]].
]
{ #category : #tests }
CogARMCompilerTests >> testRetN [
"self new testRetN"
#(0) do:
[:n| | inst len |
inst := self gen: RetN operand: n.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 0
In: inst
into: [:str :sz| | plainJane |
"Convert e.g. '00000000: movl %eax, 0x2(%eax) : 89 40 02' to 'movl %eax, 0x2(%eax)'"
plainJane := self strip: str.
self assert: (plainJane match: 'mov pc, lr').
self assert: len = sz]].
#(1 2 3 4 5 6 7) do:
[:n| | inst len |
inst := self gen: RetN operand: n * 4.
len := inst concretizeAt: 0.
self
disassembleInstructionAt: 0
In: inst
into: [:str :sz| | plainJane herIntended |
plainJane := self strip: str.
herIntended := 'add sp, sp, #', (n * 4 )asString.
self assert: herIntended equals: plainJane].
self
disassembleInstructionAt: 4
In: inst
into: [:str :sz| | plainJane |
plainJane := self strip: str.
self assert: (plainJane match: 'mov pc, lr').
self assert: len = 8]]
]
{ #category : #'cogit compatibility' }
CogARMCompilerTests >> wordSize [
^4
]