0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
Parser subclass:#ByteCodeCompiler
|
49
|
14 |
instanceVariableNames:'codeBytes codeIndex
|
45
|
15 |
litArray
|
|
16 |
stackDelta extra lineno
|
|
17 |
maxStackDepth
|
|
18 |
relocList'
|
54
|
19 |
classVariableNames:'JumpToAbsJump SequenceNumber STCCompilationFlags
|
|
20 |
ForceSTCCompilation KeepSource'
|
0
|
21 |
poolDictionaries:''
|
|
22 |
category:'System-Compiler'
|
|
23 |
!
|
|
24 |
|
|
25 |
ByteCodeCompiler comment:'
|
4
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
27 |
All Rights Reserved
|
25
|
28 |
|
76
|
29 |
$Header: /cvs/stx/stx/libcomp/ByteCodeCompiler.st,v 1.18 1995-03-06 19:19:38 claus Exp $
|
3
|
30 |
'!
|
|
31 |
|
|
32 |
!ByteCodeCompiler class methodsFor:'documentation'!
|
|
33 |
|
20
|
34 |
copyright
|
|
35 |
"
|
|
36 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
45
|
37 |
All Rights Reserved
|
20
|
38 |
|
|
39 |
This software is furnished under a license and may be used
|
|
40 |
only in accordance with the terms of that license and with the
|
|
41 |
inclusion of the above copyright notice. This software may not
|
|
42 |
be provided or otherwise made available to, or used by, any
|
|
43 |
other person. No title to or ownership of the software is
|
|
44 |
hereby transferred.
|
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
version
|
|
49 |
"
|
76
|
50 |
$Header: /cvs/stx/stx/libcomp/ByteCodeCompiler.st,v 1.18 1995-03-06 19:19:38 claus Exp $
|
20
|
51 |
"
|
|
52 |
!
|
|
53 |
|
3
|
54 |
documentation
|
|
55 |
"
|
19
|
56 |
This class performs compilation into ByteCodes.
|
|
57 |
First, parsing is done using superclass methods,
|
|
58 |
then the parse-tree is converted into an array of symbolic codes
|
|
59 |
and a relocation table;
|
|
60 |
these two are finally combined into a byteArray of the codes.
|
0
|
61 |
|
19
|
62 |
(the intermediate step through symbolic codes is for debugging
|
|
63 |
only - it will vanish)
|
|
64 |
|
|
65 |
There are many dependencies to the run-time-system (especially the
|
|
66 |
interpreter) in here - be careful when playing around ...
|
0
|
67 |
|
19
|
68 |
Instance variables:
|
0
|
69 |
|
49
|
70 |
codeBytes <ByteArry> bytecodes
|
45
|
71 |
codeIndex <SmallInteger> next index to put into code array
|
|
72 |
litArray <OrderedCollection> literals
|
|
73 |
stackDelta <SmallInteger> return value of byteCodeFor:
|
|
74 |
extra <Symbol> return value of byteCodeFor:
|
|
75 |
lineno <Boolean> return value of byteCodeFor:
|
|
76 |
maxStackDepth <SmallInteger> stack need of method
|
|
77 |
relocList <Array> used temporary for relocation
|
19
|
78 |
|
|
79 |
Class variables:
|
|
80 |
|
45
|
81 |
JumpToAbsJump <Dictionary> internal table to map opcodes
|
3
|
82 |
"
|
|
83 |
! !
|
0
|
84 |
|
|
85 |
!ByteCodeCompiler class methodsFor:'compiling methods'!
|
|
86 |
|
|
87 |
compile:methodText forClass:classToCompileFor
|
|
88 |
"compile a source-string for a method in classToCompileFor"
|
|
89 |
|
|
90 |
^ self compile:methodText
|
45
|
91 |
forClass:classToCompileFor
|
|
92 |
inCategory:'others'
|
|
93 |
notifying:nil
|
|
94 |
install:true
|
|
95 |
skipIfSame:false
|
|
96 |
silent:false
|
0
|
97 |
!
|
|
98 |
|
|
99 |
compile:aString forClass:aClass inCategory:cat
|
|
100 |
"compile a source-string for a method in classToCompileFor.
|
|
101 |
The method will get cat as category"
|
|
102 |
|
|
103 |
^ self compile:aString
|
45
|
104 |
forClass:aClass
|
|
105 |
inCategory:cat
|
|
106 |
notifying:nil
|
|
107 |
install:true
|
|
108 |
skipIfSame:false
|
|
109 |
silent:false
|
0
|
110 |
!
|
|
111 |
|
|
112 |
compile:methodText forClass:classToCompileFor notifying:someOne
|
|
113 |
"compile a source-string for a method in classToCompileFor.
|
20
|
114 |
Errors are forwarded to someOne."
|
0
|
115 |
|
|
116 |
^ self compile:methodText
|
45
|
117 |
forClass:classToCompileFor
|
|
118 |
inCategory:'others'
|
|
119 |
notifying:someOne
|
|
120 |
install:true
|
|
121 |
skipIfSame:false
|
|
122 |
silent:false
|
0
|
123 |
!
|
|
124 |
|
|
125 |
compile:aString forClass:aClass inCategory:cat notifying:someOne
|
|
126 |
"compile a source-string for a method in classToCompileFor.
|
|
127 |
errors are forwarded to someOne.
|
|
128 |
The method will get cat as category"
|
|
129 |
|
|
130 |
^ self compile:aString
|
45
|
131 |
forClass:aClass
|
|
132 |
inCategory:cat
|
|
133 |
notifying:someOne
|
|
134 |
install:true
|
|
135 |
skipIfSame:false
|
|
136 |
silent:false
|
0
|
137 |
!
|
|
138 |
|
20
|
139 |
compile:aString forClass:aClass inCategory:cat notifying:someOne install:install
|
|
140 |
"compile a source-string for a method in classToCompileFor.
|
|
141 |
The install-argument controls if the method is to be installed into the
|
|
142 |
classes method-dictionary, or just to be compiled and a method object to be returned.
|
|
143 |
Errors are forwarded to someOne. The method will get cat as category"
|
|
144 |
|
0
|
145 |
^ self compile:aString
|
45
|
146 |
forClass:aClass
|
|
147 |
inCategory:cat
|
|
148 |
notifying:someOne
|
54
|
149 |
install:install
|
45
|
150 |
skipIfSame:false
|
|
151 |
silent:false
|
0
|
152 |
!
|
|
153 |
|
|
154 |
compile:aString forClass:aClass inCategory:cat notifying:someOne
|
45
|
155 |
install:install skipIfSame:skipIfSame
|
20
|
156 |
"compile a source-string for a method in classToCompileFor.
|
|
157 |
The install-argument controls if the method is to be installed into the
|
|
158 |
classes method-dictionary, or just to be compiled and a method object to be returned.
|
|
159 |
Errors are forwarded to someOne. The method will get cat as category.
|
|
160 |
If skipIsSame is true, and the source is the same as an existing
|
|
161 |
methods source, this is a noop (for fast fileIn)."
|
0
|
162 |
|
20
|
163 |
^ self compile:aString
|
45
|
164 |
forClass:aClass
|
|
165 |
inCategory:cat
|
|
166 |
notifying:someOne
|
54
|
167 |
install:install
|
45
|
168 |
skipIfSame:skipIfSame
|
|
169 |
silent:false
|
20
|
170 |
!
|
|
171 |
|
|
172 |
compile:aString forClass:aClass inCategory:cat notifying:someOne
|
45
|
173 |
install:install skipIfSame:skipIfSame silent:silent
|
20
|
174 |
|
|
175 |
"the basic workhorse method for compiling:
|
0
|
176 |
compile a source-string for a method in classToCompileFor.
|
|
177 |
errors are forwarded to someOne (report on Transcript and return
|
|
178 |
#Error, if someOne is nil).
|
|
179 |
|
20
|
180 |
The new method will get cat as category.
|
|
181 |
If install is true, the method will go into the classes method-table,
|
|
182 |
otherwise the method is simply returned (for anonymous methods).
|
0
|
183 |
If skipIsSame is true, and the source is the same as an existing
|
20
|
184 |
methods source, this is a noop (for fast fileIn).
|
|
185 |
The argument, silent controls if errors are to be reported."
|
0
|
186 |
|
54
|
187 |
|compiler newMethod tree lits symbolicCodeArray oldMethod lazy silencio
|
|
188 |
sourceFile sourceStream pos|
|
0
|
189 |
|
|
190 |
aString isNil ifTrue:[^ nil].
|
54
|
191 |
silencio := silent or:[Smalltalk silentLoading == true].
|
0
|
192 |
|
19
|
193 |
"lazy compilation is EXPERIMENTAL"
|
|
194 |
lazy := (LazyCompilation == true) and:[install].
|
|
195 |
|
0
|
196 |
"create a compiler, let it parse and create the parsetree"
|
|
197 |
|
54
|
198 |
compiler := self for:(ReadStream on:aString) in:aClass.
|
0
|
199 |
compiler notifying:someOne.
|
54
|
200 |
silent ifTrue:[
|
|
201 |
"/ compiler ignoreErrors.
|
|
202 |
compiler ignoreWarnings
|
|
203 |
].
|
0
|
204 |
compiler nextToken.
|
|
205 |
(compiler parseMethodSpec == #Error) ifTrue:[
|
45
|
206 |
tree := #Error
|
0
|
207 |
] ifFalse:[
|
45
|
208 |
"check if same source"
|
|
209 |
(skipIfSame and:[compiler selector notNil]) ifTrue:[
|
|
210 |
oldMethod := aClass compiledMethodAt:(compiler selector).
|
|
211 |
oldMethod notNil ifTrue:[
|
|
212 |
oldMethod source = aString ifTrue:[
|
|
213 |
oldMethod isInvalid ifFalse:[
|
54
|
214 |
silencio ifFalse:[
|
45
|
215 |
Transcript showCr:('unchanged: ',aClass name,' ',compiler selector)
|
|
216 |
].
|
49
|
217 |
"
|
|
218 |
same. however, category may be different
|
|
219 |
"
|
|
220 |
(cat notNil and:[cat ~~ oldMethod category]) ifTrue:[
|
|
221 |
oldMethod category:cat.
|
|
222 |
oldMethod changed:#category.
|
|
223 |
aClass addChangeRecordForMethodCategory:oldMethod category:cat.
|
|
224 |
].
|
45
|
225 |
^ oldMethod
|
|
226 |
]
|
|
227 |
]
|
|
228 |
]
|
|
229 |
].
|
|
230 |
lazy ifFalse:[
|
|
231 |
tree := compiler parseMethodBody.
|
|
232 |
compiler tree:tree.
|
|
233 |
]
|
0
|
234 |
].
|
|
235 |
|
|
236 |
(compiler errorFlag or:[tree == #Error]) ifTrue:[
|
45
|
237 |
"/ compiler parseError:'syntax error'.
|
|
238 |
compiler selector notNil ifTrue:[
|
|
239 |
Transcript show:(compiler selector,' ')
|
|
240 |
].
|
|
241 |
Transcript showCr:'syntax error'.
|
|
242 |
^ #Error
|
0
|
243 |
].
|
|
244 |
|
19
|
245 |
"if no error and also no selector ..."
|
0
|
246 |
compiler selector isNil ifTrue:[
|
45
|
247 |
"... it was just a comment or other empty stuff"
|
|
248 |
^ nil
|
0
|
249 |
].
|
|
250 |
|
54
|
251 |
"
|
|
252 |
freak-out support ...
|
|
253 |
"
|
|
254 |
(compiler hasPrimitiveCode
|
|
255 |
or:[ForceSTCCompilation == true and:[compiler selector ~~ #doIt]]) ifTrue:[
|
49
|
256 |
^ compiler compileToMachineCode:aString
|
|
257 |
forClass:aClass
|
|
258 |
inCategory:cat
|
|
259 |
notifying:someOne
|
|
260 |
install:install
|
|
261 |
skipIfSame:skipIfSame
|
|
262 |
silent:silent.
|
19
|
263 |
].
|
|
264 |
|
54
|
265 |
"
|
|
266 |
EXPERIMENTAL: quick loading
|
|
267 |
only create a lazyMethod, which has no byteCode and will
|
|
268 |
compile itself when first called.
|
|
269 |
"
|
19
|
270 |
lazy ifTrue:[
|
45
|
271 |
newMethod := LazyMethod new.
|
54
|
272 |
KeepSource == false ifTrue:[
|
|
273 |
sourceFile := ObjectMemory nameForSources.
|
|
274 |
sourceStream := sourceFile asFilename appendingWriteStream.
|
|
275 |
].
|
|
276 |
sourceStream isNil ifTrue:[
|
|
277 |
newMethod source:aString.
|
|
278 |
] ifFalse:[
|
|
279 |
sourceStream setToEnd.
|
|
280 |
pos := sourceStream position.
|
|
281 |
sourceStream nextChunkPut:aString.
|
|
282 |
sourceStream close.
|
|
283 |
newMethod sourceFilename:sourceFile position:pos.
|
|
284 |
].
|
45
|
285 |
newMethod category:cat.
|
54
|
286 |
Project notNil ifTrue:[
|
|
287 |
newMethod package:(Project current packageName)
|
|
288 |
].
|
19
|
289 |
|
45
|
290 |
aClass addSelector:(compiler selector) withLazyMethod:newMethod.
|
|
291 |
^ newMethod
|
19
|
292 |
].
|
|
293 |
|
54
|
294 |
"
|
|
295 |
produce symbolic code first
|
|
296 |
"
|
0
|
297 |
symbolicCodeArray := compiler genSymbolicCode.
|
|
298 |
(symbolicCodeArray == #Error) ifTrue:[
|
45
|
299 |
compiler selector notNil ifTrue:[
|
|
300 |
Transcript show:(compiler selector,' ')
|
|
301 |
].
|
|
302 |
Transcript showCr:'translation error'.
|
|
303 |
^ #Error
|
0
|
304 |
].
|
|
305 |
|
54
|
306 |
"
|
|
307 |
take this, producing bytecode
|
|
308 |
(someone willin' to make machine code :-)
|
|
309 |
"
|
0
|
310 |
((compiler genByteCodeFrom:symbolicCodeArray) == #Error) ifTrue:[
|
45
|
311 |
compiler selector notNil ifTrue:[
|
|
312 |
Transcript show:(compiler selector,' ')
|
|
313 |
].
|
|
314 |
Transcript showCr:'relocation error - must be simplified'.
|
|
315 |
^ #Error
|
0
|
316 |
].
|
|
317 |
|
54
|
318 |
"
|
|
319 |
finally create the new method-object
|
|
320 |
"
|
0
|
321 |
newMethod := Method new.
|
|
322 |
lits := compiler literalArray.
|
|
323 |
lits notNil ifTrue:[
|
45
|
324 |
"literals MUST be an array - not just any Collection"
|
|
325 |
lits := Array withAll:lits.
|
|
326 |
newMethod literals:lits
|
0
|
327 |
].
|
|
328 |
newMethod byteCode:(compiler code).
|
54
|
329 |
newMethod numberOfMethodVars:(compiler numberOfMethodVars).
|
|
330 |
newMethod numberOfMethodArgs:(compiler numberOfMethodArgs).
|
|
331 |
newMethod stackSize:(compiler maxStackDepth).
|
19
|
332 |
|
54
|
333 |
"
|
|
334 |
if there where any corrections, install the updated source
|
|
335 |
"
|
19
|
336 |
compiler correctedSource notNil ifTrue:[
|
45
|
337 |
newMethod source:compiler correctedSource
|
19
|
338 |
] ifFalse:[
|
45
|
339 |
newMethod source:aString.
|
19
|
340 |
].
|
0
|
341 |
newMethod category:cat.
|
54
|
342 |
Project notNil ifTrue:[
|
|
343 |
newMethod package:(Project current packageName)
|
|
344 |
].
|
0
|
345 |
|
|
346 |
install ifTrue:[
|
45
|
347 |
aClass addSelector:(compiler selector) withMethod:newMethod
|
0
|
348 |
].
|
|
349 |
|
54
|
350 |
silencio ifFalse:[
|
45
|
351 |
Transcript showCr:('compiled: ',aClass name,' ',compiler selector)
|
0
|
352 |
].
|
|
353 |
|
|
354 |
^ newMethod
|
|
355 |
! !
|
|
356 |
|
15
|
357 |
!ByteCodeCompiler class methodsFor:'constants'!
|
|
358 |
|
|
359 |
byteCodeFor:aSymbol
|
|
360 |
"only some exported codes handled here (for BlockNode)"
|
|
361 |
|
25
|
362 |
(aSymbol == #retNil) ifTrue:[^ 1].
|
|
363 |
(aSymbol == #retTrue) ifTrue:[^ 2].
|
|
364 |
(aSymbol == #retFalse) ifTrue:[^ 3].
|
|
365 |
(aSymbol == #ret0) ifTrue:[^ 4].
|
|
366 |
(aSymbol == #retTop) ifTrue:[^ 0].
|
|
367 |
|
15
|
368 |
(aSymbol == #push0) ifTrue:[^120].
|
|
369 |
(aSymbol == #push1) ifTrue:[^121].
|
|
370 |
(aSymbol == #push2) ifTrue:[^139].
|
|
371 |
(aSymbol == #pushMinus1) ifTrue:[^122].
|
|
372 |
(aSymbol == #pushNil) ifTrue:[^ 10].
|
|
373 |
(aSymbol == #pushTrue) ifTrue:[^ 11].
|
|
374 |
(aSymbol == #pushFalse) ifTrue:[^ 12].
|
|
375 |
(aSymbol == #pushSelf) ifTrue:[^ 15].
|
|
376 |
self error
|
|
377 |
! !
|
|
378 |
|
54
|
379 |
!ByteCodeCompiler class methodsFor:'stc compilation defaults'!
|
|
380 |
|
|
381 |
stcCompilationFlags:aString
|
|
382 |
"define the flags (for example, additional -D defines or
|
|
383 |
-I includes to be used when compiling to machine code.
|
|
384 |
These are passed to stc.
|
|
385 |
This can be set from your private.rc file."
|
|
386 |
|
|
387 |
STCCompilationFlags := aString
|
|
388 |
|
|
389 |
"
|
|
390 |
Compiler stcCompilationFlags:'-DVGL -I/usr/local/include +optinline'
|
|
391 |
"
|
|
392 |
!
|
|
393 |
|
|
394 |
forceSTCCompilation:aBoolean
|
|
395 |
"if true, methods are always compiled to machine code (which takes
|
|
396 |
longer, but provides faster code). If false, compilation is
|
|
397 |
to bytecode except for methods containing primitive code.
|
|
398 |
This can be set from your private.rc file or from a workspace
|
|
399 |
for selective compilation to machine code."
|
|
400 |
|
|
401 |
ForceSTCCompilation := aBoolean
|
|
402 |
|
|
403 |
"
|
|
404 |
Compiler forceSTCCompilation:true.
|
|
405 |
Compiler forceSTCCompilation:false.
|
|
406 |
"
|
|
407 |
!
|
|
408 |
|
|
409 |
keepSource:aBoolean
|
|
410 |
"if true, the source of a method is kept as a string in memory;
|
|
411 |
if false, the source is appended to the sources-file (st.src) and
|
|
412 |
only a reference to its fileposition is kept in memory - thus saving
|
|
413 |
space. You have to care for the source-file to not become corrupted
|
|
414 |
in this case; therefore, the default is to keep it as strings for now."
|
|
415 |
|
|
416 |
KeepSource := aBoolean
|
|
417 |
|
|
418 |
"
|
|
419 |
Compiler keepSource:true.
|
|
420 |
Compiler keepSource:false.
|
|
421 |
"
|
|
422 |
! !
|
|
423 |
|
0
|
424 |
!ByteCodeCompiler methodsFor:'accessing'!
|
|
425 |
|
|
426 |
literalArray
|
|
427 |
"return the literal array - only valid after parsing"
|
|
428 |
|
|
429 |
^ litArray
|
|
430 |
!
|
|
431 |
|
|
432 |
code
|
3
|
433 |
"return the bytecode array - only valid after code-generation"
|
0
|
434 |
|
49
|
435 |
^ codeBytes
|
0
|
436 |
!
|
|
437 |
|
|
438 |
maxStackDepth
|
|
439 |
"return the stack-need of the method - only valid after code-generation"
|
|
440 |
|
|
441 |
^ maxStackDepth
|
|
442 |
! !
|
|
443 |
|
|
444 |
!ByteCodeCompiler methodsFor:'code generation'!
|
|
445 |
|
|
446 |
genSymbolicCode
|
|
447 |
"traverse the parse-tree producing symbolicCode - return the codeArray"
|
|
448 |
|
|
449 |
|codeStream thisStatement lastStatement|
|
|
450 |
|
76
|
451 |
codeStream := WriteStream on:(OrderedCollection new:100).
|
0
|
452 |
thisStatement := tree.
|
|
453 |
[thisStatement notNil] whileTrue:[
|
45
|
454 |
lastStatement := thisStatement.
|
|
455 |
thisStatement codeForSideEffectOn:codeStream inBlock:nil.
|
|
456 |
thisStatement := thisStatement nextStatement
|
0
|
457 |
].
|
54
|
458 |
(lastStatement isNil or:[lastStatement isReturnNode not])
|
0
|
459 |
ifTrue:[
|
45
|
460 |
"not a return - add retSelf"
|
54
|
461 |
"
|
|
462 |
if the last statement was a send for side-effect,
|
|
463 |
replace the previous drop by a retSelf
|
|
464 |
"
|
|
465 |
lastStatement notNil ifTrue:[
|
|
466 |
codeStream contents last == #drop ifTrue:[
|
|
467 |
codeStream position:(codeStream position - 1).
|
|
468 |
codeStream nextPut:#retSelf
|
|
469 |
]
|
|
470 |
].
|
45
|
471 |
codeStream nextPut:#retSelf
|
0
|
472 |
].
|
|
473 |
^ codeStream contents
|
|
474 |
!
|
|
475 |
|
19
|
476 |
checkForPrimitiveCode:nr
|
0
|
477 |
"this was added to allow emulation of (some) ST-80
|
|
478 |
primitives (to fileIn Remote-Package)"
|
|
479 |
|
3
|
480 |
|cls sel|
|
|
481 |
|
19
|
482 |
(nr == 75) ifTrue:[ cls := Object. sel := #identityHash ].
|
|
483 |
(nr == 110) ifTrue:[ cls := Object. sel := #== ].
|
|
484 |
(nr == 111) ifTrue:[ cls := Object. sel := #class ].
|
49
|
485 |
"
|
|
486 |
should add more here, to be able to fileIn ST-80 methods
|
|
487 |
containing primitive calls (who gives me the numbers ... ?)
|
|
488 |
"
|
3
|
489 |
cls notNil ifTrue:[
|
45
|
490 |
^ (cls compiledMethodAt:sel) code
|
3
|
491 |
].
|
0
|
492 |
^ nil
|
|
493 |
!
|
|
494 |
|
|
495 |
genByteCodeFrom:symbolicCodeArray
|
|
496 |
"convert symbolicCode into bytecodes"
|
|
497 |
|
49
|
498 |
|symIndex "{Class: SmallInteger }"
|
|
499 |
codeSize "{Class: SmallInteger }"
|
|
500 |
symCodeSize "{Class: SmallInteger }"
|
0
|
501 |
index addr
|
45
|
502 |
codeSymbol nargs needRetry
|
46
|
503 |
stackDepth relocInfo level nvars round t|
|
0
|
504 |
|
|
505 |
symbolicCodeArray isNil ifTrue:[^ self].
|
|
506 |
|
45
|
507 |
round := 0.
|
|
508 |
needRetry := true.
|
0
|
509 |
symCodeSize := symbolicCodeArray size.
|
|
510 |
codeSize := symCodeSize.
|
|
511 |
|
45
|
512 |
litArray := nil.
|
|
513 |
[needRetry] whileTrue:[
|
|
514 |
stackDepth := 0.
|
|
515 |
maxStackDepth := 0.
|
0
|
516 |
|
49
|
517 |
codeBytes := ByteArray uninitializedNew:codeSize.
|
45
|
518 |
relocInfo := Array new:(codeSize + 1).
|
|
519 |
symIndex := 1.
|
|
520 |
codeIndex := 1.
|
0
|
521 |
|
45
|
522 |
needRetry := false.
|
|
523 |
round := round + 1.
|
|
524 |
|
|
525 |
[symIndex <= symCodeSize] whileTrue:[
|
|
526 |
relocInfo at:symIndex put:codeIndex.
|
0
|
527 |
|
45
|
528 |
codeSymbol := symbolicCodeArray at:symIndex.
|
|
529 |
symIndex := symIndex + 1.
|
|
530 |
stackDelta := 0.
|
|
531 |
extra := nil.
|
|
532 |
lineno := false.
|
|
533 |
self appendByteCodeFor:codeSymbol.
|
|
534 |
lineno ifTrue:[
|
|
535 |
self appendByte:((symbolicCodeArray at:symIndex) min:255).
|
|
536 |
symIndex := symIndex + 1
|
|
537 |
].
|
|
538 |
extra notNil ifTrue:[
|
46
|
539 |
(extra == #number) ifTrue:[
|
|
540 |
index := symbolicCodeArray at:symIndex.
|
|
541 |
symIndex := symIndex + 1.
|
|
542 |
self appendSignedByte:index
|
|
543 |
].
|
|
544 |
(extra == #number16) ifTrue:[
|
|
545 |
index := symbolicCodeArray at:symIndex.
|
|
546 |
symIndex := symIndex + 2.
|
|
547 |
self appendSignedWord:index
|
|
548 |
].
|
54
|
549 |
(extra == #unsigned16) ifTrue:[
|
|
550 |
index := symbolicCodeArray at:symIndex.
|
|
551 |
symIndex := symIndex + 2.
|
|
552 |
self appendWord:index
|
|
553 |
].
|
45
|
554 |
(extra == #index) ifTrue:[
|
46
|
555 |
index := symbolicCodeArray at:symIndex.
|
|
556 |
symIndex := symIndex + 1.
|
|
557 |
self appendByte:index
|
|
558 |
].
|
|
559 |
(extra == #lit) ifTrue:[
|
45
|
560 |
index := self addLiteral:(symbolicCodeArray at:symIndex).
|
|
561 |
index > 255 ifTrue:[
|
|
562 |
codeSymbol == #pushLit ifFalse:[
|
46
|
563 |
round ~~ 1 ifTrue:[
|
|
564 |
self parseError:'too many (' , index printString , ') literals in method (at ' , codeSymbol , ') - please simplify'.
|
|
565 |
^ #Error
|
|
566 |
].
|
|
567 |
"move that literal to the front"
|
|
568 |
('COMPILER: move literal', (litArray at:index) , ' to front') errorPrintNL.
|
|
569 |
t := Array new:litArray size.
|
|
570 |
t replaceFrom:2 to:index - 1 with:litArray startingAt:1.
|
|
571 |
t replaceFrom:index with:litArray startingAt:index+1.
|
|
572 |
t at:1 put:(litArray at:index).
|
|
573 |
litArray := t.
|
|
574 |
needRetry := true.
|
|
575 |
index := 0
|
|
576 |
] ifTrue:[
|
|
577 |
"
|
|
578 |
make pushLit into pushLLit and start over
|
|
579 |
"
|
|
580 |
'COMPILER: need long index for literal' errorPrintNL.
|
|
581 |
symbolicCodeArray at:(symIndex - 1) put:#pushLLit.
|
|
582 |
codeSize := codeSize + 1.
|
|
583 |
needRetry := true.
|
|
584 |
index := 0.
|
|
585 |
]
|
45
|
586 |
].
|
|
587 |
symIndex := symIndex + 1.
|
|
588 |
self appendByte:index
|
46
|
589 |
].
|
|
590 |
(extra == #speciallit) ifTrue:[
|
|
591 |
index := self addLiteral:(symbolicCodeArray at:symIndex).
|
|
592 |
index > 255 ifTrue:[
|
|
593 |
round ~~ 1 ifTrue:[
|
|
594 |
self parseError:'too many globals (' ,
|
|
595 |
(symbolicCodeArray at:symIndex) ,
|
|
596 |
' index=' , index printString ,
|
|
597 |
') in method - please simplify'.
|
|
598 |
^ #Error
|
45
|
599 |
].
|
46
|
600 |
"
|
|
601 |
there is a last chance, if we move
|
|
602 |
all global-literals to the beginning of the
|
|
603 |
literal array ...
|
|
604 |
"
|
|
605 |
'COMPILER: resorted literals (almost at limits)' errorPrintNL.
|
|
606 |
self moveGlobalsToFront.
|
|
607 |
needRetry := true.
|
|
608 |
index := 0.
|
|
609 |
].
|
|
610 |
symIndex := symIndex + 1.
|
|
611 |
self appendByte:index.
|
|
612 |
self appendByte:0. "space for inline-generation"
|
|
613 |
self appendByte:0. "space for inline-address"
|
|
614 |
self appendByte:0.
|
|
615 |
self appendByte:0.
|
|
616 |
self appendByte:0.
|
|
617 |
symIndex := symIndex + 5
|
|
618 |
].
|
|
619 |
(extra == #offset) ifTrue:[
|
|
620 |
relocInfo at:symIndex put:codeIndex.
|
|
621 |
self addReloc:symIndex.
|
|
622 |
symIndex := symIndex + 1.
|
|
623 |
self appendByte:0
|
|
624 |
].
|
|
625 |
(extra == #indexLevel) ifTrue:[
|
|
626 |
index := symbolicCodeArray at:symIndex.
|
|
627 |
symIndex := symIndex + 1.
|
|
628 |
self appendByte:index.
|
|
629 |
level := symbolicCodeArray at:symIndex.
|
|
630 |
symIndex := symIndex + 1.
|
|
631 |
self appendByte:level
|
|
632 |
].
|
|
633 |
(extra == #offsetNvarNarg) ifTrue:[
|
|
634 |
relocInfo at:symIndex put:codeIndex.
|
|
635 |
self addReloc:symIndex.
|
|
636 |
symIndex := symIndex + 1.
|
|
637 |
self appendByte:0.
|
|
638 |
nvars := symbolicCodeArray at:symIndex.
|
|
639 |
symIndex := symIndex + 1.
|
|
640 |
self appendByte:nvars.
|
|
641 |
level := symbolicCodeArray at:symIndex.
|
|
642 |
symIndex := symIndex + 1.
|
|
643 |
self appendByte:level
|
|
644 |
].
|
|
645 |
(extra == #absoffset) ifTrue:[
|
|
646 |
relocInfo at:symIndex put:codeIndex.
|
|
647 |
self addReloc:symIndex.
|
|
648 |
addr := symbolicCodeArray at:symIndex.
|
|
649 |
symIndex := symIndex + 1.
|
|
650 |
self appendByte:(addr bitAnd:16rFF).
|
|
651 |
self appendByte:((addr bitShift:-8) bitAnd:16rFF).
|
|
652 |
].
|
|
653 |
(extra == #absoffsetNvarNarg) ifTrue:[
|
|
654 |
relocInfo at:symIndex put:codeIndex.
|
|
655 |
self addReloc:symIndex.
|
|
656 |
addr := symbolicCodeArray at:symIndex.
|
|
657 |
symIndex := symIndex + 1.
|
|
658 |
self appendByte:(addr bitAnd:16rFF).
|
|
659 |
self appendByte:((addr bitShift:-8) bitAnd:16rFF).
|
|
660 |
nvars := symbolicCodeArray at:symIndex.
|
|
661 |
symIndex := symIndex + 1.
|
|
662 |
self appendByte:nvars.
|
|
663 |
level := symbolicCodeArray at:symIndex.
|
|
664 |
symIndex := symIndex + 1.
|
|
665 |
self appendByte:level
|
|
666 |
].
|
|
667 |
(extra == #llit) ifTrue:[
|
|
668 |
index := self addLiteral:(symbolicCodeArray at:symIndex).
|
|
669 |
index > 16rFFFF ifTrue:[
|
|
670 |
self parseError:'too many (' , index printString , ') literals in method - please simplify'.
|
|
671 |
^ #Error
|
|
672 |
].
|
|
673 |
symIndex := symIndex + 1.
|
|
674 |
self appendWord:index
|
|
675 |
].
|
45
|
676 |
].
|
46
|
677 |
|
49
|
678 |
((codeSymbol == #send)
|
|
679 |
or:[codeSymbol == #superSend
|
|
680 |
or:[codeSymbol == #hereSend]]) ifTrue:[
|
45
|
681 |
index := self addLiteral:(symbolicCodeArray at:symIndex).
|
|
682 |
symIndex := symIndex + 1.
|
|
683 |
nargs := symbolicCodeArray at:symIndex.
|
|
684 |
symIndex := symIndex + 1.
|
|
685 |
self appendByte:nargs.
|
|
686 |
self appendByte:index.
|
|
687 |
(codeSymbol == #superSend) ifTrue:[
|
|
688 |
symIndex := symIndex + 1.
|
|
689 |
index := self addLiteral:(classToCompileFor superclass).
|
|
690 |
self appendByte:index
|
|
691 |
].
|
49
|
692 |
(codeSymbol == #hereSend) ifTrue:[
|
|
693 |
symIndex := symIndex + 1.
|
|
694 |
index := self addLiteral:classToCompileFor.
|
|
695 |
self appendByte:index
|
|
696 |
].
|
45
|
697 |
stackDelta := nargs negated
|
|
698 |
] ifFalse:[
|
|
699 |
(codeSymbol == #sendDrop) ifTrue:[
|
|
700 |
index := self addLiteral:(symbolicCodeArray at:symIndex).
|
|
701 |
symIndex := symIndex + 1.
|
|
702 |
nargs := symbolicCodeArray at:symIndex.
|
|
703 |
symIndex := symIndex + 1.
|
|
704 |
self appendByte:nargs.
|
|
705 |
self appendByte:index.
|
|
706 |
stackDelta := (nargs + 1) negated
|
|
707 |
]
|
|
708 |
].
|
|
709 |
stackDepth := stackDepth + stackDelta.
|
|
710 |
(stackDepth > maxStackDepth) ifTrue:[
|
|
711 |
maxStackDepth := stackDepth
|
|
712 |
]
|
|
713 |
].
|
|
714 |
relocInfo at:symIndex put:codeIndex.
|
0
|
715 |
|
45
|
716 |
needRetry ifFalse:[
|
|
717 |
"
|
|
718 |
now relocate - returns true if ok, false if we have to do it again
|
|
719 |
(when short jumps have been changed to long jumps)
|
|
720 |
"
|
|
721 |
relocList notNil ifTrue:[
|
|
722 |
needRetry := (self relocateWith:symbolicCodeArray relocInfo:relocInfo) not.
|
|
723 |
"
|
|
724 |
if returned with false, a relative jump was made into
|
|
725 |
an absolute jump - need to start over with one more byte space
|
|
726 |
"
|
|
727 |
needRetry ifTrue:[
|
|
728 |
relocList := nil.
|
|
729 |
codeSize := codeSize + 1.
|
|
730 |
]
|
|
731 |
]
|
46
|
732 |
] ifTrue:[
|
|
733 |
'compiling again ...' errorPrintNL.
|
|
734 |
]
|
0
|
735 |
].
|
49
|
736 |
"code printNL."
|
0
|
737 |
^ errorFlag
|
|
738 |
!
|
|
739 |
|
1
|
740 |
absJumpFromJump:code
|
3
|
741 |
"given a jump-symbolic code, return corresponding absolute jump"
|
|
742 |
|
1
|
743 |
JumpToAbsJump isNil ifTrue:[
|
45
|
744 |
JumpToAbsJump := IdentityDictionary new.
|
|
745 |
JumpToAbsJump at:#jump put:#jumpabs.
|
|
746 |
JumpToAbsJump at:#trueJump put:#trueJumpabs.
|
|
747 |
JumpToAbsJump at:#falseJump put:#falseJumpabs.
|
|
748 |
JumpToAbsJump at:#nilJump put:#nilJumpabs.
|
|
749 |
JumpToAbsJump at:#notNilJump put:#notNilJumpabs.
|
|
750 |
JumpToAbsJump at:#eqJump put:#eqJumpabs.
|
|
751 |
JumpToAbsJump at:#notEqJump put:#notEqJumpabs.
|
|
752 |
JumpToAbsJump at:#zeroJump put:#zeroJumpabs.
|
|
753 |
JumpToAbsJump at:#notZeroJump put:#notZeroJumpabs.
|
|
754 |
JumpToAbsJump at:#makeBlock put:#makeBlockabs.
|
1
|
755 |
].
|
|
756 |
^ JumpToAbsJump at:code
|
|
757 |
!
|
|
758 |
|
0
|
759 |
relocateWith:symbolicCodeArray relocInfo:relocInfo
|
|
760 |
"helper for genByteCodeFrom - relocate code using relocInfo.
|
|
761 |
if relocation fails badly (due to long relative jumps) patch
|
|
762 |
symbolicCode to use absolute jumps instead and return false
|
3
|
763 |
(genByteCodeFrom will then try again). Otherwise return true.
|
|
764 |
Also, on the fly, jumps to jumps and jumps to return are handled."
|
0
|
765 |
|
49
|
766 |
|delta "{Class: SmallInteger }"
|
|
767 |
codePos "{Class: SmallInteger }"
|
|
768 |
opCodePos "{Class: SmallInteger }"
|
|
769 |
codeOffset "{Class: SmallInteger }"
|
|
770 |
symOffset
|
|
771 |
opcode "{Class: SmallInteger }"
|
|
772 |
dstOpcode jumpTarget
|
3
|
773 |
jumpCode deleteSet|
|
0
|
774 |
|
3
|
775 |
deleteSet := OrderedCollection new.
|
0
|
776 |
relocList do:[:sIndex |
|
45
|
777 |
"have to relocate symCode at sIndex ..."
|
|
778 |
symOffset := symbolicCodeArray at:sIndex. "the target in the symbolic code"
|
|
779 |
codePos := relocInfo at:sIndex. "position of the offet in byte code"
|
|
780 |
codeOffset := relocInfo at:symOffset. "position of the target in byte code"
|
|
781 |
delta := codeOffset - codePos - 1.
|
|
782 |
opCodePos := codePos - 1.
|
49
|
783 |
opcode := codeBytes at:opCodePos.
|
1
|
784 |
|
45
|
785 |
(opcode between:190 and:199) ifTrue:[
|
|
786 |
"an absolute jump/makeBlock"
|
0
|
787 |
|
49
|
788 |
codeBytes at:codePos put:(codeOffset bitAnd:16rFF).
|
|
789 |
codeBytes at:(codePos + 1) put:(codeOffset bitShift:-8)
|
45
|
790 |
] ifFalse:[
|
|
791 |
"get jump-code from long and vlong codes"
|
|
792 |
(opcode between:50 and:59) ifFalse:[
|
|
793 |
(opcode between:60 and:69) ifTrue:[
|
|
794 |
opcode := opcode - 10
|
|
795 |
] ifFalse:[
|
|
796 |
(opcode between:70 and:79) ifTrue:[
|
|
797 |
opcode := opcode - 20
|
|
798 |
] ifFalse:[
|
|
799 |
self error:'invalid code to relocate'
|
|
800 |
]
|
|
801 |
].
|
|
802 |
].
|
1
|
803 |
|
45
|
804 |
"optimize jump to return and jump to jump"
|
1
|
805 |
|
45
|
806 |
(opcode == 54) ifTrue:[
|
|
807 |
"a jump"
|
|
808 |
dstOpcode := symbolicCodeArray at:symOffset.
|
3
|
809 |
|
45
|
810 |
(#(retSelf retTop retNil retTrue retFalse ret0 "blockRetTop") includes:dstOpcode) ifTrue:[
|
|
811 |
"a jump to a return - put in the return instead jump"
|
3
|
812 |
|
45
|
813 |
symbolicCodeArray at:(sIndex - 1) put:dstOpcode.
|
|
814 |
symbolicCodeArray at:sIndex put:dstOpcode.
|
49
|
815 |
codeBytes at:opCodePos put:(self byteCodeFor:dstOpcode).
|
45
|
816 |
delta := 0.
|
|
817 |
deleteSet add:sIndex.
|
3
|
818 |
|
|
819 |
"
|
49
|
820 |
'jump to return at: ' print. (sIndex - 1) printNL.
|
3
|
821 |
"
|
45
|
822 |
] ifFalse:[
|
|
823 |
(dstOpcode == #jump) ifTrue:[
|
|
824 |
"jump to jump to be done soon"
|
|
825 |
jumpTarget := symbolicCodeArray at:(symOffset + 1).
|
3
|
826 |
"
|
|
827 |
'jump to jump at: ' print. (sIndex - 1) print.
|
49
|
828 |
' newTarget:' print. jumpTarget printNL.
|
3
|
829 |
"
|
|
830 |
|
45
|
831 |
symbolicCodeArray at:sIndex put:jumpTarget.
|
|
832 |
symOffset := jumpTarget.
|
|
833 |
codeOffset := relocInfo at:symOffset.
|
|
834 |
delta := codeOffset - codePos - 1.
|
3
|
835 |
|
45
|
836 |
"continue with new delta"
|
|
837 |
]
|
|
838 |
]
|
|
839 |
].
|
|
840 |
(#(50 51 52 53 56 57 58 59) includes:opcode) ifTrue:[
|
|
841 |
"a conditional jump"
|
3
|
842 |
|
45
|
843 |
dstOpcode := symbolicCodeArray at:symOffset.
|
|
844 |
(dstOpcode == #jump) ifTrue:[
|
|
845 |
"conditional jump to unconditional jump"
|
|
846 |
jumpTarget := symbolicCodeArray at:(symOffset + 1).
|
3
|
847 |
"
|
|
848 |
'cond jump to jump at: ' print. (sIndex - 1) print.
|
49
|
849 |
' newTarget:' print. jumpTarget printNL.
|
3
|
850 |
"
|
|
851 |
|
45
|
852 |
symbolicCodeArray at:sIndex put:jumpTarget.
|
|
853 |
symOffset := jumpTarget.
|
|
854 |
codeOffset := relocInfo at:symOffset.
|
|
855 |
delta := codeOffset - codePos - 1.
|
3
|
856 |
|
45
|
857 |
"continue with new delta"
|
|
858 |
].
|
|
859 |
].
|
3
|
860 |
|
45
|
861 |
(delta >= 0) ifTrue:[
|
|
862 |
(delta > 127) ifTrue:[
|
|
863 |
(opcode between:50 and:59) ifFalse:[
|
|
864 |
self error:'invalid code to relocate'
|
49
|
865 |
] ifTrue:[
|
|
866 |
(delta > 255) ifTrue:[
|
|
867 |
"change jmp into vljmp ..."
|
|
868 |
codeBytes at:opCodePos put:(opcode + 20).
|
|
869 |
delta := delta - 256
|
|
870 |
] ifFalse:[
|
|
871 |
"change jmp into ljmp ..."
|
|
872 |
codeBytes at:opCodePos put:(opcode + 10).
|
|
873 |
delta := delta - 128
|
|
874 |
].
|
|
875 |
(delta > 127) ifTrue:[
|
|
876 |
"change symbolic into a jump absolute and fail"
|
|
877 |
jumpCode := symbolicCodeArray at:(sIndex - 1).
|
|
878 |
symbolicCodeArray at:(sIndex - 1) put:(self absJumpFromJump:jumpCode).
|
|
879 |
symbolicCodeArray at:sIndex put:symOffset.
|
3
|
880 |
"
|
49
|
881 |
'change short into abs jump' printNL.
|
3
|
882 |
"
|
49
|
883 |
deleteSet do:[:d | relocList remove:d].
|
|
884 |
^ false
|
|
885 |
]
|
45
|
886 |
].
|
|
887 |
].
|
49
|
888 |
codeBytes at:codePos put:delta
|
45
|
889 |
] ifFalse:[
|
|
890 |
(delta < -128) ifTrue:[
|
|
891 |
(opcode between:50 and:59) ifFalse:[
|
|
892 |
self error:'invalid code to relocate'
|
49
|
893 |
] ifTrue:[
|
|
894 |
(delta < -256) ifTrue:[
|
|
895 |
"change jmp into vljmp ..."
|
|
896 |
codeBytes at:opCodePos put:(opcode + 20).
|
|
897 |
delta := delta + 256
|
|
898 |
] ifFalse:[
|
|
899 |
"change jmp into ljmp ..."
|
|
900 |
codeBytes at:opCodePos put:(opcode + 10).
|
|
901 |
delta := delta + 128
|
|
902 |
].
|
|
903 |
(delta < -128) ifTrue:[
|
|
904 |
"change symbolic into a jump absolute and fail"
|
|
905 |
jumpCode := symbolicCodeArray at:(sIndex - 1).
|
|
906 |
symbolicCodeArray at:(sIndex - 1) put:(self absJumpFromJump:jumpCode).
|
|
907 |
symbolicCodeArray at:sIndex put:symOffset.
|
3
|
908 |
"
|
49
|
909 |
'change short into abs jump' printNL.
|
3
|
910 |
"
|
49
|
911 |
deleteSet do:[:d | relocList remove:d].
|
|
912 |
^ false
|
|
913 |
]
|
45
|
914 |
]
|
|
915 |
].
|
49
|
916 |
codeBytes at:codePos put:(256 + delta)
|
45
|
917 |
]
|
|
918 |
]
|
0
|
919 |
].
|
|
920 |
(errorFlag == #Error) ifTrue:[
|
45
|
921 |
self error:'relocation range error'
|
0
|
922 |
].
|
|
923 |
^ true
|
|
924 |
!
|
|
925 |
|
|
926 |
addLiteral:anObject
|
|
927 |
"add a literal to the literalArray - watch for and eliminate
|
|
928 |
duplicates. return the index of the literal in the Array"
|
|
929 |
|
3
|
930 |
|index class|
|
0
|
931 |
|
|
932 |
litArray isNil ifTrue:[
|
45
|
933 |
litArray := Array with:anObject.
|
|
934 |
^ 1
|
0
|
935 |
].
|
|
936 |
index := litArray identityIndexOf:anObject.
|
|
937 |
(index == 0) ifTrue:[
|
45
|
938 |
"
|
|
939 |
reuse constants if same value and same class
|
|
940 |
"
|
|
941 |
class := anObject class.
|
|
942 |
((class == Float)
|
|
943 |
or:[class == Fraction]) ifTrue:[
|
|
944 |
index := litArray indexOf:anObject.
|
|
945 |
].
|
|
946 |
((index == 0) or:[(litArray at:index) class ~~ class]) ifTrue:[
|
|
947 |
litArray := litArray copyWith:anObject.
|
|
948 |
^ litArray size
|
|
949 |
].
|
0
|
950 |
].
|
|
951 |
^ index
|
|
952 |
!
|
|
953 |
|
|
954 |
appendByteCodeFor:codeSymbol
|
|
955 |
"append the byteCode for an instructionSymbol to the code-Array"
|
|
956 |
|
49
|
957 |
|idx "{Class: SmallInteger }"|
|
|
958 |
|
|
959 |
idx := codeIndex.
|
|
960 |
codeBytes at:idx put:(self byteCodeFor:codeSymbol).
|
|
961 |
codeIndex := idx + 1
|
0
|
962 |
!
|
|
963 |
|
|
964 |
appendByte:aByte
|
|
965 |
"append a byte to the code-Array, checking for byte-range (debug-only)"
|
|
966 |
|
49
|
967 |
|idx "{Class: SmallInteger }"|
|
|
968 |
|
|
969 |
idx := codeIndex.
|
0
|
970 |
(aByte between:0 and:255) ifTrue:[
|
49
|
971 |
codeBytes at:idx put:aByte.
|
|
972 |
codeIndex := idx + 1
|
0
|
973 |
] ifFalse:[
|
45
|
974 |
self error:'byte range error'.
|
|
975 |
errorFlag := #Error
|
0
|
976 |
]
|
|
977 |
!
|
|
978 |
|
45
|
979 |
appendEmtyByte
|
|
980 |
"append an empty byte to the code-Array"
|
|
981 |
|
49
|
982 |
|idx "{Class: SmallInteger }"|
|
|
983 |
|
|
984 |
idx := codeIndex.
|
|
985 |
codeBytes at:idx put:0.
|
|
986 |
codeIndex := idx + 1
|
45
|
987 |
!
|
|
988 |
|
0
|
989 |
appendSignedByte:aByte
|
|
990 |
"append a signedbyte (as in jump-offsets) to the code-Array,
|
|
991 |
check range and report an error if invalid"
|
|
992 |
|
49
|
993 |
|b "{Class: SmallInteger }"
|
|
994 |
idx "{Class: SmallInteger }"|
|
0
|
995 |
|
49
|
996 |
idx := codeIndex.
|
0
|
997 |
b := aByte.
|
|
998 |
(b >= 0) ifTrue:[
|
45
|
999 |
(b > 127) ifTrue:[
|
|
1000 |
self error:'jump-range error'.
|
|
1001 |
errorFlag := #Error
|
|
1002 |
].
|
49
|
1003 |
codeBytes at:idx put:b
|
0
|
1004 |
] ifFalse:[
|
45
|
1005 |
(b < -128) ifTrue:[
|
|
1006 |
self error:'jump-range error'.
|
|
1007 |
errorFlag := #Error
|
|
1008 |
].
|
49
|
1009 |
b := 256 + b
|
0
|
1010 |
].
|
49
|
1011 |
codeBytes at:idx put:b.
|
|
1012 |
codeIndex := idx + 1
|
0
|
1013 |
!
|
|
1014 |
|
45
|
1015 |
appendWord:aWord
|
54
|
1016 |
"append an unsigned word (low-high) to the code-Array,
|
45
|
1017 |
checking for word-range (debug-only)"
|
|
1018 |
|
49
|
1019 |
|idx "{Class: SmallInteger }"|
|
|
1020 |
|
|
1021 |
idx := codeIndex.
|
45
|
1022 |
(aWord between:0 and:16rFFFF) ifTrue:[
|
49
|
1023 |
codeBytes at:idx put:(aWord bitAnd:16rFF).
|
|
1024 |
idx := idx + 1.
|
|
1025 |
codeBytes at:idx put:(aWord bitShift:-8).
|
|
1026 |
codeIndex := idx + 1
|
45
|
1027 |
] ifFalse:[
|
|
1028 |
self error:'word range error'.
|
|
1029 |
errorFlag := #Error
|
|
1030 |
]
|
|
1031 |
!
|
|
1032 |
|
46
|
1033 |
appendSignedWord:aWord
|
|
1034 |
"append a signed word to the code-Array,
|
|
1035 |
check range and report an error if invalid"
|
|
1036 |
|
49
|
1037 |
|w "{Class: SmallInteger }"|
|
46
|
1038 |
|
|
1039 |
w := aWord.
|
|
1040 |
(w >= 0) ifTrue:[
|
|
1041 |
(w > 16r7FFFF) ifTrue:[
|
|
1042 |
self error:'word-range error'.
|
|
1043 |
errorFlag := #Error
|
|
1044 |
].
|
|
1045 |
] ifFalse:[
|
|
1046 |
(w < 16r-8000) ifTrue:[
|
|
1047 |
self error:'word-range error'.
|
|
1048 |
errorFlag := #Error
|
|
1049 |
].
|
|
1050 |
w := (16r10000 + w).
|
|
1051 |
].
|
|
1052 |
self appendWord:w
|
|
1053 |
!
|
|
1054 |
|
0
|
1055 |
addReloc:symIndex
|
|
1056 |
"remember to relocate offset at symIndex later ..."
|
|
1057 |
|
|
1058 |
relocList isNil ifTrue:[
|
45
|
1059 |
relocList := OrderedCollection new.
|
0
|
1060 |
].
|
|
1061 |
relocList add:symIndex
|
|
1062 |
!
|
|
1063 |
|
45
|
1064 |
moveGlobalsToFront
|
|
1065 |
"move all global-literals to the front of the literal array.
|
|
1066 |
This may be the last chance to compile the method, since
|
|
1067 |
for globals, the maximum literal index is 255 - while for normal
|
|
1068 |
literals its a stress-less 65535"
|
|
1069 |
|
|
1070 |
litArray isNil ifTrue:[
|
|
1071 |
^ self error:'oops compiler botch'.
|
|
1072 |
].
|
|
1073 |
litArray sort:[:a :b | "a < b -> #(a b)"
|
|
1074 |
(a isMemberOf:Symbol) ifFalse:[false]
|
|
1075 |
ifTrue:[
|
|
1076 |
(b isMemberOf:Symbol) ifFalse:[true]
|
|
1077 |
ifTrue:[
|
|
1078 |
(a at:1) isUppercase ifFalse:[false]
|
|
1079 |
ifTrue:[
|
|
1080 |
(b at:1) isUppercase ifFalse:[true]
|
|
1081 |
ifTrue:[a < b].
|
|
1082 |
]
|
|
1083 |
]
|
|
1084 |
]
|
|
1085 |
].
|
|
1086 |
|
|
1087 |
"
|
|
1088 |
#(#A #c #B #D #E #a #b #F)
|
|
1089 |
sort:[:a :b |
|
|
1090 |
(a isMemberOf:Symbol) ifFalse:[false]
|
|
1091 |
ifTrue:[
|
|
1092 |
(b isMemberOf:Symbol) ifFalse:[true]
|
|
1093 |
ifTrue:[
|
|
1094 |
(a at:1) isUppercase ifFalse:[false]
|
|
1095 |
ifTrue:[
|
|
1096 |
(b at:1) isUppercase ifFalse:[true]
|
|
1097 |
ifTrue:[(a < b)].
|
|
1098 |
]
|
|
1099 |
]
|
|
1100 |
]
|
|
1101 |
].
|
|
1102 |
|
|
1103 |
"
|
|
1104 |
!
|
|
1105 |
|
0
|
1106 |
byteCodeFor:aSymbol
|
|
1107 |
"given a symbolic instruction, return the corresponding bytecode.
|
|
1108 |
as a side-effect, leave number of bytes pushed/popped by this instr.
|
|
1109 |
in stackDelta, and, if the instruction needs extra arguments, leave
|
|
1110 |
this info in extra"
|
|
1111 |
|
|
1112 |
"standard bytecodes"
|
|
1113 |
|
|
1114 |
(aSymbol == #pushNil) ifTrue:[stackDelta := 1. ^ 10].
|
|
1115 |
(aSymbol == #pushTrue) ifTrue:[stackDelta := 1. ^ 11].
|
|
1116 |
(aSymbol == #pushFalse) ifTrue:[stackDelta := 1. ^ 12].
|
|
1117 |
(aSymbol == #pushLit) ifTrue:[stackDelta := 1. extra := #lit. ^ 14].
|
|
1118 |
(aSymbol == #pushSelf) ifTrue:[stackDelta := 1. ^ 15].
|
|
1119 |
(aSymbol == #pushNum) ifTrue:[stackDelta := 1. extra := #number. ^ 16].
|
|
1120 |
|
|
1121 |
(aSymbol == #pushMethodArg) ifTrue:[stackDelta := 1. extra := #index. ^ 30].
|
|
1122 |
(aSymbol == #pushMethodVar) ifTrue:[stackDelta := 1. extra := #index. ^ 31].
|
|
1123 |
(aSymbol == #pushBlockArg) ifTrue:[stackDelta := 1. extra := #index. ^ 32].
|
|
1124 |
(aSymbol == #pushBlockVar) ifTrue:[stackDelta := 1. extra := #index. ^ 33].
|
|
1125 |
(aSymbol == #pushInstVar) ifTrue:[stackDelta := 1. extra := #index. ^ 34].
|
1
|
1126 |
(aSymbol == #pushOuterBlockArg) ifTrue:[stackDelta := 1. extra := #indexLevel. ^ 42].
|
|
1127 |
(aSymbol == #pushOuterBlockVar) ifTrue:[stackDelta := 1. extra := #indexLevel. ^ 128].
|
0
|
1128 |
|
1
|
1129 |
(aSymbol == #storeMethodVar) ifTrue:[extra := #index. stackDelta := -1. ^ 37].
|
|
1130 |
(aSymbol == #storeBlockVar) ifTrue:[extra := #index. stackDelta := -1. ^ 38].
|
|
1131 |
(aSymbol == #storeInstVar) ifTrue:[extra := #index. stackDelta := -1. ^ 39].
|
0
|
1132 |
|
|
1133 |
(aSymbol == #retTop) ifTrue:[stackDelta := -1. ^ 0].
|
39
|
1134 |
"/ (aSymbol == #blockRetTop) ifTrue:[stackDelta := -1. ^ 6].
|
0
|
1135 |
|
|
1136 |
(aSymbol == #retSelf) ifTrue:[^5].
|
|
1137 |
|
|
1138 |
(aSymbol == #==) ifTrue:[stackDelta := -1. self addLiteral:aSymbol. ^ 45].
|
|
1139 |
(aSymbol == #~~) ifTrue:[stackDelta := -1. self addLiteral:aSymbol. ^ 46].
|
|
1140 |
|
|
1141 |
(aSymbol == #falseJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 50].
|
|
1142 |
(aSymbol == #trueJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 51].
|
|
1143 |
(aSymbol == #nilJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 52].
|
|
1144 |
(aSymbol == #notNilJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 53].
|
|
1145 |
(aSymbol == #jump) ifTrue:[extra := #offset. ^ 54].
|
1
|
1146 |
(aSymbol == #makeBlock) ifTrue:[stackDelta := 1. extra := #offsetNvarNarg. ^ 55].
|
0
|
1147 |
(aSymbol == #zeroJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 56].
|
|
1148 |
(aSymbol == #notZeroJump) ifTrue:[stackDelta := -1. extra := #offset. ^ 57].
|
|
1149 |
(aSymbol == #eqJump) ifTrue:[stackDelta := -2. extra := #offset. ^ 58].
|
|
1150 |
(aSymbol == #notEqJump) ifTrue:[stackDelta := -2. extra := #offset. ^ 59].
|
|
1151 |
|
54
|
1152 |
(aSymbol == #lineno) ifTrue:[lineno := true. ^ 8].
|
|
1153 |
|
0
|
1154 |
(aSymbol == #send) ifTrue:[lineno := true. ^ 19].
|
|
1155 |
(aSymbol == #superSend) ifTrue:[lineno := true. ^ 20].
|
49
|
1156 |
(aSymbol == #hereSend) ifTrue:[lineno := true. ^ 20].
|
0
|
1157 |
|
|
1158 |
(aSymbol == #drop) ifTrue:[stackDelta := -1. ^ 18].
|
|
1159 |
(aSymbol == #dup) ifTrue:[stackDelta := 1. ^ 47].
|
|
1160 |
|
|
1161 |
(aSymbol == #pushClassVar) ifTrue:[stackDelta := 1. extra := #speciallit. ^ 35].
|
|
1162 |
(aSymbol == #pushClassInstVar) ifTrue:[stackDelta := 1. extra := #index. ^ 176].
|
|
1163 |
(aSymbol == #pushGlobal) ifTrue:[stackDelta := 1. extra := #speciallit. ^ 36].
|
|
1164 |
|
|
1165 |
(aSymbol == #storeClassVar) ifTrue:[extra := #speciallit.stackDelta := -1. ^ 40].
|
|
1166 |
(aSymbol == #storeClassInstVar) ifTrue:[extra := #index.stackDelta := -1. ^ 177].
|
|
1167 |
(aSymbol == #storeGlobal) ifTrue:[extra := #speciallit. stackDelta := -1. ^ 41].
|
1
|
1168 |
(aSymbol == #storeOuterBlockVar) ifTrue:[stackDelta := -1. extra := #indexLevel. ^ 129].
|
0
|
1169 |
|
|
1170 |
"optimized bytecodes"
|
|
1171 |
|
|
1172 |
(aSymbol == #retNil) ifTrue:[^ 1].
|
|
1173 |
(aSymbol == #retTrue) ifTrue:[^ 2].
|
|
1174 |
(aSymbol == #retFalse) ifTrue:[^ 3].
|
|
1175 |
(aSymbol == #ret0) ifTrue:[^ 4].
|
|
1176 |
(aSymbol == #retNum) ifTrue:[extra := #number. ^ 127].
|
25
|
1177 |
(aSymbol == #homeRetTop) ifTrue:[^ 7].
|
0
|
1178 |
|
46
|
1179 |
(aSymbol == #pushNum16) ifTrue:[stackDelta := 1. extra := #number16. ^ 17].
|
0
|
1180 |
(aSymbol == #push0) ifTrue:[stackDelta := 1. ^120].
|
|
1181 |
(aSymbol == #push1) ifTrue:[stackDelta := 1. ^121].
|
3
|
1182 |
(aSymbol == #push2) ifTrue:[stackDelta := 1. ^139].
|
0
|
1183 |
(aSymbol == #pushMinus1) ifTrue:[stackDelta := 1. ^122].
|
|
1184 |
|
|
1185 |
(aSymbol == #send0) ifTrue:[lineno := true. extra := #lit. ^21].
|
|
1186 |
(aSymbol == #send1) ifTrue:[lineno := true. extra := #lit. stackDelta := -1. ^22].
|
|
1187 |
(aSymbol == #send2) ifTrue:[lineno := true. extra := #lit. stackDelta := -2. ^23].
|
|
1188 |
(aSymbol == #send3) ifTrue:[lineno := true. extra := #lit. stackDelta := -3. ^24].
|
|
1189 |
|
|
1190 |
(aSymbol == #sendSelf0) ifTrue:[lineno := true. extra := #lit. stackDelta := 1. ^180].
|
|
1191 |
(aSymbol == #sendSelf1) ifTrue:[lineno := true. extra := #lit. ^181].
|
|
1192 |
(aSymbol == #sendSelf2) ifTrue:[lineno := true. extra := #lit. stackDelta := -1. ^182].
|
|
1193 |
(aSymbol == #sendSelf3) ifTrue:[lineno := true. extra := #lit. stackDelta := -2. ^183].
|
|
1194 |
(aSymbol == #sendSelfDrop0) ifTrue:[lineno := true. extra := #lit. ^184].
|
|
1195 |
(aSymbol == #sendSelfDrop1) ifTrue:[lineno := true. extra := #lit. stackDelta := -1. ^185].
|
|
1196 |
(aSymbol == #sendSelfDrop2) ifTrue:[lineno := true. extra := #lit. stackDelta := -2. ^186].
|
|
1197 |
(aSymbol == #sendSelfDrop3) ifTrue:[lineno := true. extra := #lit. stackDelta := -3. ^187].
|
|
1198 |
|
|
1199 |
(aSymbol == #sendDrop) ifTrue:[lineno := true. ^25].
|
|
1200 |
(aSymbol == #sendDrop0) ifTrue:[lineno := true. extra := #lit. stackDelta := -1. ^26].
|
|
1201 |
(aSymbol == #sendDrop1) ifTrue:[lineno := true. extra := #lit. stackDelta := -2. ^27].
|
|
1202 |
(aSymbol == #sendDrop2) ifTrue:[lineno := true. extra := #lit. stackDelta := -3. ^28].
|
|
1203 |
(aSymbol == #sendDrop3) ifTrue:[lineno := true. extra := #lit. stackDelta := -4. ^29].
|
|
1204 |
|
|
1205 |
(aSymbol == #pushMethodVar1) ifTrue:[stackDelta := 1. ^80].
|
|
1206 |
(aSymbol == #pushMethodVar2) ifTrue:[stackDelta := 1. ^81].
|
|
1207 |
(aSymbol == #pushMethodVar3) ifTrue:[stackDelta := 1. ^82].
|
|
1208 |
(aSymbol == #pushMethodVar4) ifTrue:[stackDelta := 1. ^83].
|
|
1209 |
(aSymbol == #pushMethodVar5) ifTrue:[stackDelta := 1. ^84].
|
|
1210 |
(aSymbol == #pushMethodVar6) ifTrue:[stackDelta := 1. ^85].
|
|
1211 |
|
|
1212 |
(aSymbol == #pushMethodArg1) ifTrue:[stackDelta := 1. ^86].
|
|
1213 |
(aSymbol == #pushMethodArg2) ifTrue:[stackDelta := 1. ^87].
|
|
1214 |
(aSymbol == #pushMethodArg3) ifTrue:[stackDelta := 1. ^88].
|
|
1215 |
(aSymbol == #pushMethodArg4) ifTrue:[stackDelta := 1. ^89].
|
|
1216 |
|
|
1217 |
(aSymbol == #pushInstVar1) ifTrue:[stackDelta := 1. ^90].
|
|
1218 |
(aSymbol == #pushInstVar2) ifTrue:[stackDelta := 1. ^91].
|
|
1219 |
(aSymbol == #pushInstVar3) ifTrue:[stackDelta := 1. ^92].
|
|
1220 |
(aSymbol == #pushInstVar4) ifTrue:[stackDelta := 1. ^93].
|
|
1221 |
(aSymbol == #pushInstVar5) ifTrue:[stackDelta := 1. ^94].
|
|
1222 |
(aSymbol == #pushInstVar6) ifTrue:[stackDelta := 1. ^95].
|
|
1223 |
(aSymbol == #pushInstVar7) ifTrue:[stackDelta := 1. ^96].
|
|
1224 |
(aSymbol == #pushInstVar8) ifTrue:[stackDelta := 1. ^97].
|
|
1225 |
(aSymbol == #pushInstVar9) ifTrue:[stackDelta := 1. ^98].
|
|
1226 |
(aSymbol == #pushInstVar10) ifTrue:[stackDelta := 1. ^99].
|
|
1227 |
|
|
1228 |
(aSymbol == #storeMethodVar1) ifTrue:[stackDelta := -1. ^100].
|
|
1229 |
(aSymbol == #storeMethodVar2) ifTrue:[stackDelta := -1. ^101].
|
|
1230 |
(aSymbol == #storeMethodVar3) ifTrue:[stackDelta := -1. ^102].
|
|
1231 |
(aSymbol == #storeMethodVar4) ifTrue:[stackDelta := -1. ^103].
|
|
1232 |
(aSymbol == #storeMethodVar5) ifTrue:[stackDelta := -1. ^104].
|
|
1233 |
(aSymbol == #storeMethodVar6) ifTrue:[stackDelta := -1. ^105].
|
|
1234 |
|
|
1235 |
(aSymbol == #storeInstVar1) ifTrue:[stackDelta := -1. ^110].
|
|
1236 |
(aSymbol == #storeInstVar2) ifTrue:[stackDelta := -1. ^111].
|
|
1237 |
(aSymbol == #storeInstVar3) ifTrue:[stackDelta := -1. ^112].
|
|
1238 |
(aSymbol == #storeInstVar4) ifTrue:[stackDelta := -1. ^113].
|
|
1239 |
(aSymbol == #storeInstVar5) ifTrue:[stackDelta := -1. ^114].
|
|
1240 |
(aSymbol == #storeInstVar6) ifTrue:[stackDelta := -1. ^115].
|
|
1241 |
(aSymbol == #storeInstVar7) ifTrue:[stackDelta := -1. ^116].
|
|
1242 |
(aSymbol == #storeInstVar8) ifTrue:[stackDelta := -1. ^117].
|
|
1243 |
(aSymbol == #storeInstVar9) ifTrue:[stackDelta := -1. ^118].
|
|
1244 |
(aSymbol == #storeInstVar10) ifTrue:[stackDelta := -1. ^119].
|
|
1245 |
|
|
1246 |
(aSymbol == #retMethodVar1) ifTrue:[^160].
|
|
1247 |
(aSymbol == #retMethodVar2) ifTrue:[^161].
|
|
1248 |
(aSymbol == #retMethodVar3) ifTrue:[^162].
|
|
1249 |
(aSymbol == #retMethodVar4) ifTrue:[^163].
|
|
1250 |
(aSymbol == #retMethodVar5) ifTrue:[^164].
|
|
1251 |
(aSymbol == #retMethodVar6) ifTrue:[^165].
|
|
1252 |
|
|
1253 |
(aSymbol == #retInstVar1) ifTrue:[^166].
|
|
1254 |
(aSymbol == #retInstVar2) ifTrue:[^167].
|
|
1255 |
(aSymbol == #retInstVar3) ifTrue:[^168].
|
|
1256 |
(aSymbol == #retInstVar4) ifTrue:[^169].
|
|
1257 |
(aSymbol == #retInstVar5) ifTrue:[^170].
|
|
1258 |
(aSymbol == #retInstVar6) ifTrue:[^171].
|
|
1259 |
(aSymbol == #retInstVar7) ifTrue:[^172].
|
|
1260 |
(aSymbol == #retInstVar8) ifTrue:[^173].
|
|
1261 |
|
|
1262 |
(aSymbol == #retMethodArg1) ifTrue:[^174].
|
|
1263 |
(aSymbol == #retMethodArg2) ifTrue:[^175].
|
|
1264 |
|
|
1265 |
(aSymbol == #pushBlockArg1) ifTrue:[stackDelta := 1. ^140].
|
|
1266 |
(aSymbol == #pushBlockArg2) ifTrue:[stackDelta := 1. ^141].
|
|
1267 |
(aSymbol == #pushBlockArg3) ifTrue:[stackDelta := 1. ^142].
|
|
1268 |
(aSymbol == #pushBlockArg4) ifTrue:[stackDelta := 1. ^143].
|
|
1269 |
|
1
|
1270 |
(aSymbol == #pushOuter1BlockArg) ifTrue:[stackDelta := 1. extra := #index. ^ 43].
|
|
1271 |
(aSymbol == #pushOuter2BlockArg) ifTrue:[stackDelta := 1. extra := #index. ^ 44].
|
0
|
1272 |
|
5
|
1273 |
(aSymbol == #=) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^130].
|
|
1274 |
(aSymbol == #+) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^131].
|
|
1275 |
(aSymbol == #~=) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^132].
|
|
1276 |
(aSymbol == #-) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^133].
|
0
|
1277 |
(aSymbol == #class) ifTrue:[self addLiteral:aSymbol. ^134].
|
5
|
1278 |
(aSymbol == #x) ifTrue:[lineno := true. self addLiteral:aSymbol. ^106].
|
|
1279 |
(aSymbol == #y) ifTrue:[lineno := true. self addLiteral:aSymbol. ^107].
|
|
1280 |
(aSymbol == #width) ifTrue:[lineno := true. self addLiteral:aSymbol. ^108].
|
|
1281 |
(aSymbol == #height) ifTrue:[lineno := true. self addLiteral:aSymbol. ^109].
|
|
1282 |
(aSymbol == #origin) ifTrue:[lineno := true. self addLiteral:aSymbol. ^154].
|
|
1283 |
(aSymbol == #extent) ifTrue:[lineno := true. self addLiteral:aSymbol. ^155].
|
|
1284 |
(aSymbol == #at:) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^135].
|
|
1285 |
(aSymbol == #at:put:)ifTrue:[lineno := true. stackDelta := -2. self addLiteral:aSymbol. ^136].
|
|
1286 |
(aSymbol == #bitAnd:) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^137].
|
|
1287 |
(aSymbol == #bitOr:) ifTrue:[lineno := true. stackDelta := -1. self addLiteral:aSymbol. ^138].
|
|
1288 |
(aSymbol == #plus1) ifTrue:[lineno := true. self addLiteral:#+. ^123].
|
|
1289 |
(aSymbol == #minus1) ifTrue:[lineno := true. self addLiteral:#-. ^124].
|
0
|
1290 |
|
5
|
1291 |
(aSymbol == #incMethodVar) ifTrue:[lineno := true. self addLiteral:#+. extra := #index. ^125].
|
|
1292 |
(aSymbol == #decMethodVar) ifTrue:[lineno := true. self addLiteral:#-. extra := #index. ^126].
|
0
|
1293 |
|
|
1294 |
(aSymbol == #eq0) ifTrue:[self addLiteral:#==. ^48].
|
|
1295 |
(aSymbol == #ne0) ifTrue:[self addLiteral:#~~. ^49].
|
|
1296 |
|
5
|
1297 |
(aSymbol == #>) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -1. ^ 145].
|
|
1298 |
(aSymbol == #>=) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -1. ^ 146].
|
|
1299 |
(aSymbol == #<) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -1. ^ 147].
|
|
1300 |
(aSymbol == #<=) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -1. ^ 148].
|
|
1301 |
(aSymbol == #next) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 149].
|
|
1302 |
(aSymbol == #peek) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 150].
|
|
1303 |
(aSymbol == #value) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 151].
|
|
1304 |
(aSymbol == #value:) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -1. ^ 152].
|
6
|
1305 |
(aSymbol == #value:value:) ifTrue:[lineno := true. self addLiteral:aSymbol. stackDelta := -2. ^ 178].
|
5
|
1306 |
(aSymbol == #size) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 153].
|
|
1307 |
(aSymbol == #asInteger) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 158].
|
|
1308 |
(aSymbol == #rounded) ifTrue:[lineno := true. self addLiteral:aSymbol. ^ 159].
|
0
|
1309 |
(aSymbol == #mk0Block) ifTrue:[^ 156].
|
|
1310 |
(aSymbol == #mkNilBlock) ifTrue:[^ 157].
|
|
1311 |
|
|
1312 |
(aSymbol == #falseJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 190].
|
|
1313 |
(aSymbol == #trueJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 191].
|
|
1314 |
(aSymbol == #nilJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 192].
|
|
1315 |
(aSymbol == #notNilJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 193].
|
|
1316 |
(aSymbol == #jumpabs) ifTrue:[extra := #absoffset. ^ 194].
|
1
|
1317 |
(aSymbol == #makeBlockabs) ifTrue:[stackDelta := 1. extra := #absoffsetNvarNarg. ^ 195].
|
0
|
1318 |
(aSymbol == #zeroJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 196].
|
|
1319 |
(aSymbol == #notZeroJumpabs) ifTrue:[stackDelta := -1. extra := #absoffset. ^ 197].
|
1
|
1320 |
(aSymbol == #eqJumpabs) ifTrue:[stackDelta := -2. extra := #absoffset. ^ 198].
|
|
1321 |
(aSymbol == #notEqJumpabs) ifTrue:[stackDelta := -2. extra := #absoffset. ^ 199].
|
0
|
1322 |
|
|
1323 |
(aSymbol == #pushThisContext) ifTrue:[stackDelta := 1. ^ 144].
|
45
|
1324 |
(aSymbol == #pushLLit) ifTrue:[stackDelta := 1. extra := #llit. ^ 201].
|
0
|
1325 |
|
76
|
1326 |
(aSymbol == #isNil) ifTrue:[^ 188].
|
|
1327 |
(aSymbol == #notNil) ifTrue:[^ 189].
|
|
1328 |
(aSymbol == #not) ifTrue:[lineno := true. ^ 179].
|
|
1329 |
|
0
|
1330 |
self error:'invalid code symbol'.
|
|
1331 |
errorFlag := #Error
|
|
1332 |
! !
|
49
|
1333 |
|
|
1334 |
!ByteCodeCompiler methodsFor:'machine code generation'!
|
|
1335 |
|
|
1336 |
compileToMachineCode:aString forClass:aClass inCategory:cat
|
|
1337 |
notifying:someOne install:install skipIfSame:skipIfSame silent:silent
|
|
1338 |
"this is called to compile primitive code.
|
|
1339 |
This is EXPERIMENTAL and going to be changed to raise an error,
|
|
1340 |
an redefined in subclasses which can do it (either by direct compilation, or by calling
|
|
1341 |
the external stc do do it.
|
|
1342 |
For a description of the arguments, see compile:forClass....."
|
|
1343 |
|
54
|
1344 |
|stFileName stream handle address flags command oFileName soFileName
|
|
1345 |
initName newMethod ok status|
|
|
1346 |
|
|
1347 |
SequenceNumber isNil ifTrue:[
|
|
1348 |
SequenceNumber := 0.
|
|
1349 |
].
|
|
1350 |
SequenceNumber := SequenceNumber + 1.
|
49
|
1351 |
|
54
|
1352 |
initName := 'ttt' , SequenceNumber printString.
|
|
1353 |
stFileName := initName , '.st'.
|
|
1354 |
stream := stFileName asFilename writeStream.
|
49
|
1355 |
|
54
|
1356 |
aClass allSuperclasses reverseDo:[:cls|
|
|
1357 |
cls ~~ Object ifTrue:[
|
|
1358 |
cls fileOutDefinitionOn:stream.
|
|
1359 |
stream nextPut:(stream class chunkSeparator); cr.
|
|
1360 |
]
|
|
1361 |
].
|
49
|
1362 |
aClass fileOutDefinitionOn:stream.
|
54
|
1363 |
stream nextPut:(stream class chunkSeparator); cr.
|
|
1364 |
|
|
1365 |
aClass fileOutPrimitiveDefinitionsOn:stream.
|
49
|
1366 |
|
|
1367 |
stream nextPut:(stream class chunkSeparator).
|
|
1368 |
aClass isMeta ifTrue:[
|
|
1369 |
stream nextPutAll:(aClass name copyTo:(aClass name size - 5)), ' class methodsFor:''' , cat , ''''.
|
|
1370 |
] ifFalse:[
|
|
1371 |
stream nextPutAll:aClass name , ' methodsFor:''' , cat , ''''.
|
|
1372 |
].
|
|
1373 |
stream nextPut:(stream class chunkSeparator).
|
|
1374 |
stream cr.
|
|
1375 |
|
54
|
1376 |
stream nextPutAll:'"{ Line: 1 }"'; cr.
|
|
1377 |
|
49
|
1378 |
stream nextPutAll:aString.
|
|
1379 |
stream nextPut:(stream class chunkSeparator).
|
|
1380 |
stream space.
|
|
1381 |
stream nextPut:(stream class chunkSeparator).
|
|
1382 |
stream close.
|
|
1383 |
|
|
1384 |
"
|
|
1385 |
call stc to compile it
|
|
1386 |
"
|
|
1387 |
|
54
|
1388 |
flags := ' +newIncremental -N' , initName .
|
|
1389 |
STCCompilationFlags notNil ifTrue:[
|
|
1390 |
flags := STCCompilationFlags , flags
|
|
1391 |
].
|
|
1392 |
|
|
1393 |
command := 'stc ' , flags , ' -c ' , stFileName.
|
|
1394 |
"/ command printNL.
|
|
1395 |
|
|
1396 |
ok := true.
|
49
|
1397 |
(OperatingSystem executeCommand:command) ifTrue:[
|
|
1398 |
"for debugging - leave c intermediate"
|
54
|
1399 |
"/ command := 'stc ' , flags , ' -C ' , stFileName.
|
|
1400 |
"/ command printNL.
|
|
1401 |
"/ OperatingSystem executeCommand:command
|
49
|
1402 |
] ifFalse:[
|
54
|
1403 |
command := '../../stc/stc ' , flags , ' -c ' , stFileName.
|
49
|
1404 |
Transcript showCr:command.
|
|
1405 |
(OperatingSystem executeCommand:command) ifTrue:[
|
|
1406 |
"for debugging - leave c intermediate"
|
54
|
1407 |
"/ command := '../../stc/stc ' , flags , ' -C ' , stFileName.
|
|
1408 |
"/ command printNL.
|
|
1409 |
"/ OperatingSystem executeCommand:command
|
49
|
1410 |
] ifFalse:[
|
54
|
1411 |
ok := false.
|
49
|
1412 |
]
|
|
1413 |
].
|
|
1414 |
|
54
|
1415 |
ok ifFalse:[
|
|
1416 |
status := OperatingSystem lastExecStatus.
|
|
1417 |
status >= 16r200 ifTrue:[
|
|
1418 |
self parseError:'STC error during compilation' position:1.
|
|
1419 |
] ifFalse:[
|
|
1420 |
self parseError:'no STC - cannot compile primitive code' position:1.
|
|
1421 |
].
|
|
1422 |
^ #Error
|
|
1423 |
].
|
|
1424 |
|
|
1425 |
oFileName := './' , initName , '.o'.
|
49
|
1426 |
OperatingSystem getOSType = 'irix' ifTrue:[
|
|
1427 |
"
|
|
1428 |
link it to a shared object
|
|
1429 |
"
|
54
|
1430 |
soFileName := './' , initName , '.so'.
|
|
1431 |
OperatingSystem executeCommand:'rm -f ' , soFileName.
|
|
1432 |
OperatingSystem executeCommand:'ld -shared -all -o ' , soFileName , ' ' , oFileName.
|
|
1433 |
oFileName := soFileName.
|
49
|
1434 |
].
|
|
1435 |
|
|
1436 |
ObjectFileLoader isNil ifTrue:[
|
|
1437 |
self parseError:'no dynamic load configured - cannot compile primitive code' position:1.
|
|
1438 |
^ #Error
|
|
1439 |
].
|
54
|
1440 |
"/ ObjectFileLoader verbose:true.
|
49
|
1441 |
|
|
1442 |
"
|
|
1443 |
load the objectfile
|
|
1444 |
"
|
54
|
1445 |
handle := ObjectFileLoader openDynamicObject:oFileName.
|
49
|
1446 |
handle isNil ifTrue:[
|
|
1447 |
self parseError:'dynamic load failed - cannot compile primitive code' position:1.
|
|
1448 |
^ #Error
|
|
1449 |
].
|
54
|
1450 |
"/ ('handle is ' , handle printString) printNL.
|
49
|
1451 |
|
54
|
1452 |
address := ObjectFileLoader getFunction:'__' , initName , '_Init' from:handle.
|
49
|
1453 |
address isNil ifTrue:[
|
54
|
1454 |
address := ObjectFileLoader getFunction:'_' , initName , '_Init' from:handle.
|
49
|
1455 |
address isNil ifTrue:[
|
54
|
1456 |
self parseError:initName , '_Init() lookup failed - cannot compile primitive code' position:1.
|
49
|
1457 |
^ #Error
|
|
1458 |
]
|
|
1459 |
].
|
|
1460 |
|
54
|
1461 |
"/ ('init at ' , address printString) printNL.
|
|
1462 |
|
|
1463 |
ObjectFileLoader callFunctionAt:address forceOld:true arg:2.
|
49
|
1464 |
|
54
|
1465 |
"
|
|
1466 |
did it work ?
|
|
1467 |
"
|
|
1468 |
newMethod := aClass compiledMethodAt:self selector.
|
|
1469 |
newMethod notNil ifTrue:[
|
|
1470 |
newMethod source:aString.
|
|
1471 |
aClass addChangeRecordForMethod:newMethod.
|
|
1472 |
(silent or:[Smalltalk silentLoading == true]) ifFalse:[
|
|
1473 |
Transcript showCr:('compiled: ', aClass name,' ',self selector,' - machine code')
|
|
1474 |
].
|
|
1475 |
^ newMethod.
|
|
1476 |
].
|
|
1477 |
|
|
1478 |
self parseError:'dynamic load failed' position:1.
|
49
|
1479 |
^ #Error
|
|
1480 |
|
|
1481 |
"
|
|
1482 |
|m|
|
|
1483 |
|
|
1484 |
Object subclass:#Test
|
|
1485 |
instanceVariableNames:''
|
|
1486 |
classVariableNames:''
|
|
1487 |
poolDictionaries:''
|
|
1488 |
category:'tests'.
|
|
1489 |
m := ByteCodeCompiler
|
|
1490 |
compile:'foo ^ ''hello'''
|
|
1491 |
forClass:Test
|
|
1492 |
inCategory:'tests'
|
|
1493 |
notifying:nil
|
|
1494 |
install:false
|
|
1495 |
skipIfSame:false.
|
|
1496 |
m inspect
|
|
1497 |
"
|
|
1498 |
"
|
|
1499 |
|m|
|
|
1500 |
|
|
1501 |
Object subclass:#Test
|
|
1502 |
instanceVariableNames:''
|
|
1503 |
classVariableNames:''
|
|
1504 |
poolDictionaries:''
|
|
1505 |
category:'tests'.
|
|
1506 |
m := ByteCodeCompiler
|
|
1507 |
compileToMachineCode:'foo %{ RETURN (_MKSMALLINT(1)); %}'
|
|
1508 |
forClass:Test
|
|
1509 |
inCategory:'tests'
|
|
1510 |
notifying:nil
|
|
1511 |
install:false
|
|
1512 |
skipIfSame:false
|
|
1513 |
silent:false.
|
|
1514 |
m inspect
|
|
1515 |
"
|
|
1516 |
! !
|