-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
OpalCompiler.class.st
629 lines (529 loc) · 18.4 KB
/
OpalCompiler.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
"
I provide the API of the whole Compiler Package for the case that the input is sourcecode.
(if there is already and AST, call #generate (to compile) or #evaluate directly on the node)
a pre-configures compiler instance can be requested with:
- Smalltalk compiler
- a Class compiler
The compiler instance (actually: the compilation context) needs to be setup. See #class: #source: #noPattern: #requestor: for the most important accessors (more are in the accessing protocol).
See the class comment of CompilationContext for more information.
The final step is one of three actions:
-> parsing: parse source and return an AST.
-> compile: parse and compile, return a CompiledMethod
-> evaluate: parse, compile, evaluate and return result
Example:
Smalltalk compiler
source: 'test 1+2';
class: Object;
compile.
This returns a CompiledMethod.
"
Class {
#name : #OpalCompiler,
#superclass : #Object,
#instVars : [
'ast',
'source',
'context',
'receiver',
'compilationContext',
'compilationContextClass'
],
#classInstVars : [
'overlayEnvironment'
],
#category : #'OpalCompiler-Core-FrontEnd'
}
{ #category : #overlay }
OpalCompiler class >> compilerClasses [
"here the classes for the overlay can changed. the default is just the compiler"
^self package definedClasses
"Compiler and AST:
self package definedClasses, RBProgramNode package definedClasses, {TRBProgramNodeVisitor}
"
]
{ #category : #options }
OpalCompiler class >> compilerSettingsOn: aBuilder [
<systemsettings>
(aBuilder group: #compiler)
label: 'Compiler';
description: 'All Compiler Settings';
with: [
(aBuilder setting: #createAndEnableOverlay)
label: 'Compiler Overlay Environment';
target: self;
default: false;
description: 'Create a overlay environment for the OpalCompiler package and sets this copy as the global compiler. After this all classes in the package OpalCompiler are not used anymore outside of tests. Simplifies changing the compiler'.
(aBuilder pickOne: #bytecodeBackend)
order: 0;
target: CompilationContext;
description: 'Sets the bytecode encoding of the compiled code generated by the Opal compiler';
label: 'Bytecode Backend';
default: EncoderForSistaV1;
domainValues:
{#V3PlusClosures -> EncoderForV3PlusClosures.
#SistaV1 -> EncoderForSistaV1 }.
CompilationContext compilerSettingsOn: aBuilder]
]
{ #category : #overlay }
OpalCompiler class >> createAndEnableOverlay [
^self overlayIsActive
]
{ #category : #overlay }
OpalCompiler class >> createAndEnableOverlay: aBoolean [
aBoolean = self overlayIsActive ifTrue: [ ^self ].
aBoolean
ifTrue: [ self startUsingOverlayForDevelopment ]
ifFalse: [ self stopUsingOverlayForDevelopment ]
]
{ #category : #public }
OpalCompiler class >> debuggerMethodMapForMethod: aMethod [
^ DebuggerMethodMapOpal forMethod: aMethod
]
{ #category : #'old - deprecated' }
OpalCompiler class >> evaluate: textOrString [
self
deprecated: 'Please use new compiler API instead'
transformWith: '`@receiver evaluate: `@statements1 ' -> '`@receiver new source: `@statements1; evaluate'.
^ self new
source: textOrString;
evaluate
]
{ #category : #'old - deprecated' }
OpalCompiler class >> evaluate: textOrString for: anObject logged: logFlag [
self
deprecated: 'Please use new compiler API instead'
transformWith:
'`@receiver evaluate: `@statements1 for: `@statements2 logged: `@statements3 '
-> '`@receiver new source: `@statements1; logged: `@statements3; receiver: `@statements2; evaluate.'.
^ self new
source: textOrString;
logged: logFlag;
receiver: anObject;
evaluate
]
{ #category : #'old - deprecated' }
OpalCompiler class >> evaluate: textOrString for: anObject notifying: aController logged: logFlag [
self
deprecated: 'Please use new compiler API instead'
transformWith:
'`@receiver evaluate: `@statements1 for: `@statements2 notifying: `@statements3 logged: `@statements4'
-> '`@receiver new source: `@statements1; logged: `@statements4; receiver: `@statements2; requestor: `@statements3; evaluate'.
^ self new
source: textOrString;
logged: logFlag;
receiver: anObject;
requestor: aController;
evaluate
]
{ #category : #'old - deprecated' }
OpalCompiler class >> evaluate: textOrString logged: logFlag [
self
deprecated: 'Please use new compiler API instead'
transformWith: '`@receiver evaluate: `@statements1 logged: `@statements2 ' -> '`@receiver new source: `@statements1; logged: `@statements2; evaluate.'.
^ self new
source: textOrString;
logged: logFlag;
evaluate
]
{ #category : #'old - deprecated' }
OpalCompiler class >> evaluate: textOrString notifying: aController logged: logFlag [
self
deprecated: 'Please use new compiler API instead'
transformWith:
'`@receiver evaluate: `@statements1 notifying: `@statements2 logged: `@statements3'
-> '`@receiver new source: `@statements1; logged: `@statements3; requestor: `@statements2; evaluate'.
^ self new
source: textOrString;
logged: logFlag;
requestor: aController;
evaluate
]
{ #category : #'old - deprecated' }
OpalCompiler class >> format: textOrStream in: aClass notifying: aRequestor [
self
deprecated: 'Please use new compiler API instead'
transformWith:
'`@receiver format: `@statements1 in: `@statements2 notifying: `@statements3'
-> '`@receiver new source: `@statements1; class: `@statements2; requestor: `@statements3; format'.
^ self new
source: textOrStream;
class: aClass;
requestor: aRequestor;
format
]
{ #category : #public }
OpalCompiler class >> isActive [
^Smalltalk compilerClass == self
]
{ #category : #overlay }
OpalCompiler class >> overlayEnvironment [
^overlayEnvironment ifNil: [ overlayEnvironment := Dictionary new ]
]
{ #category : #overlay }
OpalCompiler class >> overlayIsActive [
^overlayEnvironment notNil
]
{ #category : #overlay }
OpalCompiler class >> overlayStep1CopyClasses [
"now we put a copy of all the classes into the environment"
self compilerClasses do: [ :class | self overlayEnvironment at: class name put: class copy ]
]
{ #category : #overlay }
OpalCompiler class >> overlayStep2Recompile [
"now we recompile the classes in the environment with itself as an overlay"
self overlayEnvironment valuesDo: [ :class |
class methodsDo: [ :method |
| newMethod |
newMethod := class compiler
bindings: self overlayEnvironment;
compile: method sourceCode.
class addSelectorSilently: method selector withMethod: newMethod ] ]
]
{ #category : #overlay }
OpalCompiler class >> overlayStep3FixSuperclassPointers [
"make sure superclass pointers are correct"
self overlayEnvironment valuesDo: [ :class |
(class isTrait not and: [self overlayEnvironment includesKey: class superclass name])
ifTrue: [ class superclass: (self overlayEnvironment at: class superclass name)]]
]
{ #category : #overlay }
OpalCompiler class >> overlayStep4SetImageCompiler [
"make the copy the default compiler for the image"
SmalltalkImage compilerClass: (self overlayEnvironment at: #OpalCompiler).
ASTCache reset.
]
{ #category : #overlay }
OpalCompiler class >> overlayStep5UpdateInstances [
"transform existing instances to be instances of the overlay"
self compilerClasses do: [ :class |
class allInstances do: [ :object |
(self overlayEnvironment at: class name) adoptInstance: object ]]
]
{ #category : #public }
OpalCompiler class >> recompileAll [
"Recompile all classes and traits in the system."
Smalltalk image recompile
]
{ #category : #public }
OpalCompiler class >> register [
SmalltalkImage current compilerClass: self
]
{ #category : #overlay }
OpalCompiler class >> startUsingOverlayForDevelopment [
"this method sets up an overlay so we can change the compiler package without breaking the compiler"
<script>
"We copy all compiler classes into the overlayEnvironment, recompile to update referenced classes,
fix the superclasses and finaly set the compiler overlay as the image default compiler."
self
overlayStep1CopyClasses;
overlayStep2Recompile;
overlayStep3FixSuperclassPointers;
overlayStep4SetImageCompiler;
overlayStep5UpdateInstances
]
{ #category : #overlay }
OpalCompiler class >> stopUsingOverlayForDevelopment [
"set compiler back to normal and throw away overlay environment"
<script>
SmalltalkImage compilerClass: nil.
overlayEnvironment := nil.
ASTCache reset
]
{ #category : #plugins }
OpalCompiler >> addPlugin: aClass [
compilationContext addASTTransformationPlugin: aClass
]
{ #category : #private }
OpalCompiler >> allowParseErrorsNonInteractive [
^self compilationContext optionParseErrorsNonInteractiveOnly and: [ self isInteractive not ]
]
{ #category : #accessing }
OpalCompiler >> bindings: aDictionary [
"allows to define additional binding, note: Globals are not shadowed"
self compilationContext bindings: aDictionary
]
{ #category : #private }
OpalCompiler >> callPlugins [
| plugins |
plugins := compilationContext astTransformPlugins ifEmpty: [ ^self ].
plugins sort: [:a :b | a priority > b priority]. "priority 0 is sorted last"
plugins do: [ :each | ast := each transform: ast].
]
{ #category : #accessing }
OpalCompiler >> class: aClass [
self compilationContext class: aClass.
]
{ #category : #accessing }
OpalCompiler >> compilationContext [
^ compilationContext ifNil: [ compilationContext := self compilationContextClass default ]
]
{ #category : #accessing }
OpalCompiler >> compilationContext: anObject [
compilationContext := anObject
]
{ #category : #plugins }
OpalCompiler >> compilationContextClass [
^compilationContextClass ifNil: [ CompilationContext ]
]
{ #category : #plugins }
OpalCompiler >> compilationContextClass: aClass [
compilationContextClass := aClass.
]
{ #category : #'public access' }
OpalCompiler >> compile [
^compilationContext noPattern
ifTrue: [ self compileDoit ]
ifFalse: [ self compileMethod ]
]
{ #category : #'public access' }
OpalCompiler >> compile: textOrString [
^self
source: textOrString;
compile.
]
{ #category : #private }
OpalCompiler >> compileDoit [
| cm |
["We need to first compile with the AST having the text
offset of the original selected code. This way the #location
will be correct regarding the requestor"
self parseDoIt.
self callPlugins.
cm := ast generateWithSource ]
on: SyntaxErrorNotification
do: [ :exception |
self compilationContext requestor
ifNotNil: [
self compilationContext requestor
notify: exception errorMessage , ' ->'
at: exception location
in: exception errorCode.
^ self compilationContext failBlock value ]
ifNil: [ exception pass ] ].
"We need to re-parse so that the AST is in sync with the
bytecode to be able to debug the doit. Recompile not needed"
ast := RBParser parseMethod: ast formattedCode.
ast compilationContext: self compilationContext.
self doSemanticAnalysis.
"we store the ast in the doit method so that the debugger can read variables from the requestor"
cm propertyAt: #ast put: ast.
^cm
]
{ #category : #'public access' }
OpalCompiler >> compileMethod [
| cm |
[ [ self parse.
self callPlugins.
] on: ReparseAfterSourceEditing
do:
[ :notification |
self source: notification newSource.
notification retry.
].
cm := compilationContext optionEmbeddSources
ifTrue: [ ast generateWithSource ]
ifFalse: [ast generate: self compilationContext compiledMethodTrailer ]
] on: SyntaxErrorNotification
do: [ :exception |
self compilationContext requestor
ifNotNil: [
self compilationContext requestor
notify: exception errorMessage , ' ->'
at: exception location
in: exception errorCode.
^ self compilationContext failBlock value ]
ifNil: [ exception pass ]].
^cm
]
{ #category : #accessing }
OpalCompiler >> compiledMethodTrailer: bytes [
self compilationContext compiledMethodTrailer: bytes
]
{ #category : #accessing }
OpalCompiler >> context: aContext [
context := aContext
]
{ #category : #'public access' }
OpalCompiler >> decompileMethod: aCompiledMethod [
^ Smalltalk globals
at: #FBDDecompiler
ifPresent: [ :decompilerClass | [ decompilerClass new decompile: aCompiledMethod ]
on: Error
do: [ RBMethodNode errorMethodNode: aCompiledMethod selector errorMessage: 'Decompilation failed'. ] ]
ifAbsent: [ RBMethodNode errorMethodNode: aCompiledMethod selector errorMessage: 'No decompiler available'. ]
]
{ #category : #private }
OpalCompiler >> doSemanticAnalysis [
^[ast doSemanticAnalysis]
on: OCSemanticError
do: [ :ex | ex defaultAction. ^ self compilationContext failBlock value ]
]
{ #category : #plugins }
OpalCompiler >> encoderClass: aClass [
self compilationContext encoderClass: aClass
]
{ #category : #accessing }
OpalCompiler >> environment: anEnvironment [
"Set the environment (dictionary of class, traits and globals) used during the compilation"
self compilationContext environment: anEnvironment
]
{ #category : #'public access' }
OpalCompiler >> evaluate [
"Compiles the sourceStream into a parse tree, then generates code into
a method. If aContext is not nil, the text can refer to temporaries in that
context (the Debugger uses this). If aRequestor is not nil, then it will receive
a notify:at: message before the attempt to evaluate is aborted. Finally, the
compiled method is invoked from here via withArgs:executeMethod:, hence
the system no longer creates Doit method litter on errors."
| value |
self noPattern: true.
self getSourceFromRequestorSelection.
self class: (context ifNil: [ receiver class ] ifNotNil: [ context compiledCode methodClass ]).
value := receiver withArgs: (context ifNil: [ #() ] ifNotNil: [ {context} ]) executeMethod: self compileDoit.
self logDoIt.
^ value
]
{ #category : #'public access' }
OpalCompiler >> evaluate: textOrString [
^self
source: textOrString;
evaluate
]
{ #category : #accessing }
OpalCompiler >> failBlock: aBlock [
self compilationContext failBlock: aBlock.
]
{ #category : #'public access' }
OpalCompiler >> format [
^self parse formattedCode.
]
{ #category : #'public access' }
OpalCompiler >> format: textOrString [
^self
source: textOrString;
format
]
{ #category : #private }
OpalCompiler >> getSourceFromRequestorSelection [
| selectionString |
"if the requestor can provide a selection, we use that as the source"
(self compilationContext requestor respondsTo: #selection) ifFalse: [ ^self ].
selectionString := self compilationContext requestor selection asString.
selectionString isEmptyOrNil ifTrue: [ ^self ].
self source: selectionString.
]
{ #category : #testing }
OpalCompiler >> isInteractive [
^self isInteractiveFor: compilationContext requestor
]
{ #category : #private }
OpalCompiler >> isInteractiveFor: aRequestor [
"the requester can override if the compiler is interactive or not"
aRequestor ifNil: [^ false ].
^ (aRequestor respondsTo: #interactive)
ifTrue: [ aRequestor interactive ]
ifFalse: [ true ]
]
{ #category : #private }
OpalCompiler >> logDoIt [
self compilationContext logged ifFalse: [ ^self ].
Smalltalk globals
at: #SystemAnnouncer
ifPresent: [ :sysAnn |
sysAnn uniqueInstance evaluated: source contents context: context ]
]
{ #category : #accessing }
OpalCompiler >> logged: aBoolean [
self compilationContext logged: aBoolean.
]
{ #category : #accessing }
OpalCompiler >> noPattern: aBoolean [
self compilationContext noPattern: aBoolean.
"when we compile an expression, embedd sources, as the resulting method will never be installed"
compilationContext parseOptions: #(+ #optionEmbeddSources).
]
{ #category : #'public access' }
OpalCompiler >> options: anOptionsArray [
self compilationContext parseOptions: anOptionsArray
]
{ #category : #'public access' }
OpalCompiler >> parse [
ast := self compilationContext noPattern
ifTrue: [ self parseExpression ]
ifFalse: [ self parseMethod ].
ast methodNode compilationContext: self compilationContext.
self doSemanticAnalysis.
^ast
]
{ #category : #'public access' }
OpalCompiler >> parse: textOrString [
^self
source: textOrString;
parse
]
{ #category : #private }
OpalCompiler >> parseDoIt [
"we parse doits without doing name analysis here, it is done in #transformDoit"
ast := self parseExpression.
^self transformDoit
]
{ #category : #private }
OpalCompiler >> parseExpression [
^ (self compilationContext optionParseErrors or: [ self allowParseErrorsNonInteractive ])
ifTrue: [self parserClass parseFaultyExpression: source contents]
ifFalse: [self parserClass parseExpression: source contents]
]
{ #category : #'public access' }
OpalCompiler >> parseLiterals: aString [
^self parserClass parseLiterals: aString
]
{ #category : #private }
OpalCompiler >> parseMethod [
^ (self compilationContext optionParseErrors or: [ self allowParseErrorsNonInteractive ])
ifTrue: [self parserClass parseFaultyMethod: source contents]
ifFalse: [self parserClass parseMethod: source contents]
]
{ #category : #'public access' }
OpalCompiler >> parseSelector: aString [
"Answer the message selector for the argument, aString, which should parse successfully up to the temporary declaration or the end of the method header."
^[self parserClass parseMethodPattern: aString] on: Error do: [nil].
]
{ #category : #plugins }
OpalCompiler >> parserClass [
^self compilationContext parserClass
]
{ #category : #accessing }
OpalCompiler >> productionEnvironment: anObject [
compilationContext productionEnvironment: anObject
]
{ #category : #accessing }
OpalCompiler >> receiver: anObject [
receiver := anObject.
]
{ #category : #accessing }
OpalCompiler >> requestor: aRequestor [
self compilationContext requestor: aRequestor.
self compilationContext interactive: (self isInteractiveFor: aRequestor).
]
{ #category : #plugins }
OpalCompiler >> requestorScopeClass: aClass [
"clients can set their own subclass of OCRequestorScope if needed"
self compilationContext requestorScopeClass: aClass
]
{ #category : #accessing }
OpalCompiler >> source: aString [
source := aString readStream.
]
{ #category : #'public access' }
OpalCompiler >> transformDoit [
"if we compile an expression, we wrap it in a method here"
self compilationContext noPattern ifFalse: [ ^ast ].
ast := context
ifNil: [ast asDoit]
ifNotNil: [ast asDoitForContext: context].
"we need to set the compilation context in the new method node"
ast compilationContext: self compilationContext.
context ifNotNil: [ast methodClass: context receiver class].
self doSemanticAnalysis.
^ast
]