84
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
159
|
3 |
All Rights Reserved
|
84
|
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 |
|
249
|
13 |
ExecutableFunction subclass:#CompiledCode
|
623
|
14 |
instanceVariableNames:'flags byteCode literals'
|
|
15 |
classVariableNames:'NoByteCodeSignal InvalidByteCodeSignal InvalidInstructionSignal
|
|
16 |
BadLiteralsSignal NonBooleanReceiverSignal ArgumentSignal'
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Kernel-Methods'
|
84
|
19 |
!
|
|
20 |
|
249
|
21 |
!CompiledCode class methodsFor:'documentation'!
|
84
|
22 |
|
88
|
23 |
copyright
|
|
24 |
"
|
|
25 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
159
|
26 |
All Rights Reserved
|
88
|
27 |
|
|
28 |
This software is furnished under a license and may be used
|
|
29 |
only in accordance with the terms of that license and with the
|
|
30 |
inclusion of the above copyright notice. This software may not
|
|
31 |
be provided or otherwise made available to, or used by, any
|
|
32 |
other person. No title to or ownership of the software is
|
|
33 |
hereby transferred.
|
|
34 |
"
|
|
35 |
!
|
|
36 |
|
84
|
37 |
documentation
|
|
38 |
"
|
|
39 |
This is an abstract class, to merge common attributes of Blocks and
|
328
|
40 |
Methods i.e. describe all objects consisting of either compiled or
|
|
41 |
interpreted code.
|
84
|
42 |
|
|
43 |
Instance variables:
|
|
44 |
|
357
|
45 |
flags <SmallInteger> special flag bits coded in a number
|
225
|
46 |
byteCode <ByteArray> bytecode if its an interpreted codeobject
|
|
47 |
literals <Array> the block/methods literal array
|
|
48 |
|
|
49 |
|
|
50 |
Class variables:
|
|
51 |
|
|
52 |
NoByteCodeSignal raised if a codeObject is about to be executed
|
|
53 |
which has neither code nor byteCode (i.e. both are nil)
|
|
54 |
InvalidByteCodeSignal raised if byteCode is not an instance of ByteArray
|
|
55 |
InvalidInstructionSignal raised if an invalid instruction opcode is encountered
|
|
56 |
BadLiteralsSignal raised if literalArray is not an array
|
|
57 |
NonBooleanReceiverSignal raised for conditional jumps where receiver is not a boolean
|
|
58 |
ArgumentSignal raised if argument count is not what the codeObject expects
|
|
59 |
|
|
60 |
all of these signals are children of ExecutionErrorSignal.
|
84
|
61 |
|
|
62 |
NOTICE: layout known by runtime system and compiler - do not change
|
|
63 |
"
|
623
|
64 |
!
|
|
65 |
|
|
66 |
version
|
1133
|
67 |
^ '$Header: /cvs/stx/stx/libbasic/CompiledCode.st,v 1.22 1996-04-02 22:00:16 cg Exp $'
|
84
|
68 |
! !
|
|
69 |
|
249
|
70 |
!CompiledCode class methodsFor:'initialization'!
|
106
|
71 |
|
|
72 |
initialize
|
|
73 |
NoByteCodeSignal isNil ifTrue:[
|
159
|
74 |
NoByteCodeSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
75 |
NoByteCodeSignal nameClass:self message:#noByteCodeSignal.
|
|
76 |
NoByteCodeSignal notifierString:'nil byteCode in code-object - not executable'.
|
|
77 |
|
|
78 |
InvalidByteCodeSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
79 |
InvalidByteCodeSignal nameClass:self message:#invalidByteCodeSignal.
|
|
80 |
InvalidByteCodeSignal notifierString:'invalid byteCode in code-object - not executable'.
|
|
81 |
|
|
82 |
InvalidInstructionSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
83 |
InvalidInstructionSignal nameClass:self message:#invalidInstructionSignal.
|
|
84 |
InvalidInstructionSignal notifierString:'invalid instruction in code-object - not executable'.
|
|
85 |
|
|
86 |
BadLiteralsSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
87 |
BadLiteralsSignal nameClass:self message:#badLiteralsSignal.
|
|
88 |
BadLiteralsSignal notifierString:'bad literal table in code-object - should not happen'.
|
|
89 |
|
|
90 |
NonBooleanReceiverSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
91 |
NonBooleanReceiverSignal nameClass:self message:#nonBooleanReceiverSignal.
|
|
92 |
NonBooleanReceiverSignal notifierString:'if/while on non-boolean receiver'.
|
|
93 |
|
|
94 |
ArgumentSignal := ExecutionErrorSignal newSignalMayProceed:true.
|
|
95 |
ArgumentSignal nameClass:self message:#argumentSignal.
|
|
96 |
ArgumentSignal notifierString:'bad argument(s)'.
|
106
|
97 |
]
|
|
98 |
! !
|
|
99 |
|
345
|
100 |
!CompiledCode class methodsFor:'Signal constants'!
|
106
|
101 |
|
|
102 |
argumentSignal
|
|
103 |
"return the signal raised when something's wrong with the
|
|
104 |
arguments"
|
|
105 |
|
|
106 |
^ ArgumentSignal
|
623
|
107 |
!
|
|
108 |
|
|
109 |
executionErrorSignal
|
|
110 |
"return the parent-signal of all execution errors"
|
|
111 |
|
|
112 |
^ ExecutionErrorSignal
|
106
|
113 |
! !
|
|
114 |
|
249
|
115 |
!CompiledCode class methodsFor:'queries'!
|
84
|
116 |
|
|
117 |
isBuiltInClass
|
|
118 |
"this class is known by the run-time-system"
|
|
119 |
|
|
120 |
^ true
|
|
121 |
! !
|
|
122 |
|
249
|
123 |
!CompiledCode methodsFor:'accessing'!
|
84
|
124 |
|
|
125 |
byteCode
|
|
126 |
"return the bytecode (a ByteArray)"
|
|
127 |
|
|
128 |
^ byteCode
|
|
129 |
!
|
|
130 |
|
|
131 |
literals
|
|
132 |
"return the literal array"
|
|
133 |
|
|
134 |
^ literals
|
|
135 |
! !
|
|
136 |
|
623
|
137 |
!CompiledCode methodsFor:'error handling'!
|
|
138 |
|
|
139 |
badArgumentArray
|
|
140 |
"this error is triggered, if a non array is passed to
|
|
141 |
#valueWithReceiver:.. methods"
|
|
142 |
|
|
143 |
^ ArgumentSignal
|
|
144 |
raiseRequestWith:self
|
|
145 |
errorString:'argumentArray must be an Array'
|
|
146 |
!
|
|
147 |
|
|
148 |
badLiteralTable
|
|
149 |
"this error is triggered, when a block/method is called with a bad literal
|
|
150 |
array (i.e. non-array) - this can only happen, if the
|
|
151 |
compiler is broken or someone played around with a block/methods
|
|
152 |
literal table or the GC is broken and corrupted it."
|
|
153 |
|
|
154 |
^ BadLiteralsSignal raise.
|
|
155 |
!
|
|
156 |
|
|
157 |
invalidByteCode
|
|
158 |
"this error is triggered when the interpreter tries to execute a
|
|
159 |
code object, where the byteCode is nonNil, but not a ByteArray.
|
|
160 |
Can only happen when Compiler/runtime system is broken or
|
|
161 |
someone played around with a block/method."
|
|
162 |
|
|
163 |
^ InvalidByteCodeSignal raise.
|
|
164 |
!
|
|
165 |
|
|
166 |
invalidInstruction
|
|
167 |
"this error is triggered when the bytecode-interpreter tries to
|
|
168 |
execute an invalid bytecode instruction.
|
|
169 |
Can only happen when Compiler/runtime system is broken or
|
|
170 |
someone played around with the block/methods code."
|
|
171 |
|
|
172 |
^ InvalidInstructionSignal raise.
|
|
173 |
!
|
|
174 |
|
|
175 |
noByteCode
|
|
176 |
"this error is triggered when the interpreter tries to execute a
|
|
177 |
code object, where both the code and byteCode instances are nil.
|
|
178 |
This can happen if:
|
|
179 |
the Compiler/runtime system is broken,
|
|
180 |
someone played around with a block/method,
|
|
181 |
compilation of a lazy method failed
|
|
182 |
(i.e. the lazy method contains an error or
|
|
183 |
it contains primitive code and there is no stc compiler available)
|
|
184 |
an unloaded object modules method is called for.
|
|
185 |
Only the first case is to be considered serious - it should not happen
|
|
186 |
if the system is used correctly."
|
|
187 |
|
|
188 |
^ NoByteCodeSignal raise.
|
|
189 |
!
|
|
190 |
|
|
191 |
receiverNotBoolean:anObject
|
|
192 |
"this error is triggered when the bytecode-interpreter tries to
|
|
193 |
execute ifTrue:/ifFalse or whileTrue: type of expressions where the
|
|
194 |
receiver is neither true nor false."
|
|
195 |
|
|
196 |
^ NonBooleanReceiverSignal raise.
|
|
197 |
!
|
|
198 |
|
|
199 |
tooManyArguments
|
|
200 |
"this error is triggered, when a method/block tries to perform a send with
|
|
201 |
more arguments than supported by the interpreter. This can only happen,
|
|
202 |
if the compiler has been changed without updating the VM."
|
|
203 |
|
|
204 |
^ ArgumentSignal
|
|
205 |
raiseRequestWith:self
|
|
206 |
errorString:'too many args in send'
|
|
207 |
! !
|
|
208 |
|
|
209 |
!CompiledCode methodsFor:'private accessing'!
|
|
210 |
|
|
211 |
byteCode:aByteArray
|
|
212 |
"set the bytecode field - DANGER ALERT"
|
|
213 |
|
|
214 |
byteCode := aByteArray
|
|
215 |
!
|
|
216 |
|
|
217 |
dynamic
|
|
218 |
"return the flag stating that the machine code was created
|
|
219 |
dynamically (from bytecode)."
|
|
220 |
|
|
221 |
%{ /* NOCONTEXT */
|
|
222 |
|
|
223 |
/* made this a primitive to get define in stc.h */
|
|
224 |
|
1133
|
225 |
RETURN ((__intVal(__INST(flags)) & F_DYNAMIC) ? true : false);
|
623
|
226 |
%}
|
|
227 |
!
|
|
228 |
|
|
229 |
dynamic:aBoolean
|
|
230 |
"set/clear the flag bit stating that the machine code was created
|
|
231 |
dynamically and should be flushed on image-restart.
|
|
232 |
Obsolete - now done in VM"
|
|
233 |
|
|
234 |
%{ /* NOCONTEXT */
|
|
235 |
|
1133
|
236 |
int newFlags = __intVal(__INST(flags));
|
623
|
237 |
|
|
238 |
/* made this a primitive to get define in stc.h */
|
|
239 |
if (aBoolean == true)
|
|
240 |
newFlags |= F_DYNAMIC;
|
|
241 |
else
|
|
242 |
newFlags &= ~F_DYNAMIC;
|
|
243 |
|
1133
|
244 |
__INST(flags) = __MKSMALLINT(newFlags);
|
623
|
245 |
%}
|
|
246 |
!
|
|
247 |
|
|
248 |
literals:aLiteralArray
|
|
249 |
"set the literal array for evaluation - DANGER ALERT"
|
|
250 |
|
|
251 |
literals := aLiteralArray
|
|
252 |
!
|
|
253 |
|
|
254 |
markFlag
|
|
255 |
"return the mark bits value as a boolean"
|
|
256 |
|
|
257 |
%{ /* NOCONTEXT */
|
|
258 |
|
|
259 |
/* made this a primitive to get define in stc.h */
|
|
260 |
|
1133
|
261 |
RETURN ((__intVal(__INST(flags)) & F_MARKBIT) ? true : false);
|
623
|
262 |
%}
|
|
263 |
!
|
|
264 |
|
|
265 |
markFlag:aBoolean
|
|
266 |
"set/clear the mark flag bit.
|
|
267 |
This bit is not used by the VM, but instead free to mark codeObjects
|
|
268 |
for any (debugging/tracing) use. For example, the coverage test uses
|
|
269 |
these to mark reached methods. (inspired by a note in c.l.s)"
|
|
270 |
|
|
271 |
%{ /* NOCONTEXT */
|
|
272 |
|
1133
|
273 |
int newFlags = __intVal(__INST(flags));
|
623
|
274 |
|
|
275 |
/* made this a primitive to get define in stc.h */
|
|
276 |
if (aBoolean == true)
|
|
277 |
newFlags |= F_MARKBIT;
|
|
278 |
else
|
|
279 |
newFlags &= ~F_MARKBIT;
|
|
280 |
|
1133
|
281 |
__INST(flags) = __MKSMALLINT(newFlags);
|
623
|
282 |
%}
|
|
283 |
! !
|
|
284 |
|
328
|
285 |
!CompiledCode methodsFor:'queries'!
|
|
286 |
|
|
287 |
messages
|
|
288 |
"return a Set of all symbols referenced by this thingy.
|
|
289 |
(this is more than the message selectors, since also global names
|
|
290 |
and symbols found in immediate arrays are included)."
|
|
291 |
|
|
292 |
|symbolSet|
|
|
293 |
|
|
294 |
symbolSet := IdentitySet new.
|
|
295 |
literals notNil ifTrue:[
|
|
296 |
literals do: [ :lit |
|
370
|
297 |
lit isSymbol ifTrue: [
|
328
|
298 |
symbolSet add: lit
|
|
299 |
] ifFalse: [
|
370
|
300 |
lit isArray ifTrue: [
|
328
|
301 |
lit traverse: [ :el |
|
370
|
302 |
el isSymbol ifTrue: [symbolSet add: el]
|
328
|
303 |
]
|
|
304 |
]
|
|
305 |
]
|
|
306 |
]
|
|
307 |
].
|
|
308 |
^ symbolSet
|
|
309 |
|
|
310 |
"
|
|
311 |
(CompiledCode compiledMethodAt:#messages) messages
|
|
312 |
"
|
|
313 |
! !
|
|
314 |
|
623
|
315 |
CompiledCode initialize!
|