forked from mozart/mozart2-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGen.oz
1801 lines (1742 loc) · 67.6 KB
/
CodeGen.oz
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%%%
%%% Author:
%%% Leif Kornstaedt <[email protected]>
%%%
%%% Copyright:
%%% Leif Kornstaedt, 1997-2001
%%%
%%% Last change:
%%% $Date$ by $Author$
%%% $Revision$
%%%
%%% This file is part of Mozart, an implementation of Oz 3:
%%% http://www.mozart-oz.org
%%%
%%% See the file "LICENSE" or
%%% http://www.mozart-oz.org/LICENSE.html
%%% for information on usage and redistribution
%%% of this file, and for a DISCLAIMER OF ALL
%%% WARRANTIES.
%%%
%%
%% General Notes:
%%
%% meth codeGen(CS ?VInstr)
%% CS is an instance of the CodeStore class. It encapsulates the
%% internal state of the code generator (generation of virtual
%% registers as well as compiler switches) and stores the produced
%% code. Its methods annotate this code, perform register assignment,
%% and emit the code.
%%
%\define DEBUG_DEFS
functor
import
Debug(getRaiseOnBlock setRaiseOnBlock) at 'x-oz://boot/Debug'
CompilerSupport(isBuiltin featureLess) at 'x-oz://boot/CompilerSupport'
Space(new ask merge)
FD(decl distinct sumC reflect assign)
System(show printName eq)
Property(get)
Builtins(getInfo)
Core
RunTime(literals procValues)
CodeStore('class')
RecordC(reflectArity)
export
%% mixin classes for the abstract syntax:
typeOf: CodeGenTypeOf
stepPoint: CodeGenStepPoint
declaration: CodeGenDeclaration
skipNode: CodeGenSkipNode
equation: CodeGenEquation
construction: CodeGenConstruction
definition: CodeGenDefinition
clauseBody: CodeGenClauseBody
application: CodeGenApplication
ifNode: CodeGenIfNode
ifClause: CodeGenIfClause
patternCase: CodeGenPatternCase
patternClause: CodeGenPatternClause
sideCondition: CodeGenSideCondition
recordPattern: CodeGenRecordPattern
equationPattern: CodeGenEquationPattern
elseNode: CodeGenElseNode
noElse: CodeGenNoElse
tryNode: CodeGenTryNode
lockNode: CodeGenLockNode
classNode: CodeGenClassNode
method: CodeGenMethod
methFormal: CodeGenMethFormal
methFormalOptional: CodeGenMethFormalOptional
methFormalWithDefault: CodeGenMethFormalWithDefault
objectLockNode: CodeGenObjectLockNode
getSelf: CodeGenGetSelf
exceptionNode: CodeGenExceptionNode
valueNode: CodeGenValueNode
variable: CodeGenVariable
variableOccurrence: CodeGenVariableOccurrence
patternVariableOccurrence: CodeGenPatternVariableOccurrence
%% mixin classes for token representations:
token: CodeGenToken
procedureToken: CodeGenProcedureToken
define
proc {CodeGenList Nodes CS VHd VTl}
case Nodes of Node|Noder then VInter in
{Node codeGen(CS VHd VInter)}
{CodeGenList Noder CS VInter VTl}
[] nil then
VHd = VTl
end
end
fun {CoordNoDebug Coord}
case {Label Coord} of pos then Coord
else {Adjoin Coord pos}
end
end
fun {IsStep Coord}
case {Label Coord} of pos then false
[] unit then false
else true
end
end
proc {StepPoint Coord Kind VHd VTl VInter1 VInter2}
if {IsStep Coord} then
VHd = vDebugEntry(_ Coord Kind VInter1)
VInter2 = vDebugExit(_ Coord Kind VTl)
else
VHd = VInter1
VInter2 = VTl
end
end
proc {MakeUnify Reg1 Reg2 VHd VTl}
if {IsDet Reg1} andthen {IsDet Reg2} andthen Reg1 == Reg2 then
%% We omit the unification to avoid unnecessary Reg occurrences.
%% (Reg1 or Reg2 can be undetermined in SideConditions, see PR#634.
%% The reason is the the test is processed before the subpattern,
%% and registers are assigned to variables only when the subpattern
%% is processed.)
VHd = VTl
else
VHd = vUnify(_ Reg1 Reg2 VTl)
end
end
proc {MakePermanent Vs VHd Cont1 Cont2 VTl CS}
if CS.staticVarnamesSwitch orelse CS.dynamicVarnamesSwitch then
RegIndices = {FoldR Vs
fun {$ V In} Reg in
{V reg(?Reg)}
case {V getPrintName($)} of unit then In
elseof PN andthen {IsAtom PN} then Reg#_#PN|In
else In
end
end nil}
in
case RegIndices of nil then
VHd = Cont1
Cont2 = VTl
else
{ForAll RegIndices proc {$ _#I#_} {CS nextYIndex(?I)} end}
VHd = vMakePermanent(_ RegIndices Cont1)
if CS.staticVarnamesSwitch then Regs in
Regs = {Map RegIndices fun {$ Reg#_#_} Reg end}
Cont2 = vClear(_ Regs VTl)
else
Cont2 = VTl
end
end
else
VHd = Cont1
Cont2 = VTl
end
end
%%
%% Instances of PseudoVariableOccurrences are used when in some
%% code generation context a variable occurrence is required but
%% only a register index is available (for example, in case of
%% a late expansion where said register is freshly generated).
%%
class PseudoVariableOccurrence
prop final
feat reg value coord
meth init(Reg)
self.reg = Reg
end
meth getCoord($) C = self.coord in
if {IsDet C} then C else unit end
end
meth getVariable($)
self
end
meth getPrintName($)
unit
end
meth isToplevel($)
false
end
meth getCodeGenValue($)
self.value
end
meth reg($)
self.reg
end
meth makeEquation(CS VO VHd VTl) Value = self.value in
if {IsDet Value} andthen ({IsNumber Value} orelse {IsLiteral Value})
then
VHd = vEquateConstant(_ Value {VO reg($)} VTl)
else
{MakeUnify self.reg {VO reg($)} VHd VTl}
end
end
meth makeRecordArgument(CS VHd VTl $) Value = self.value in
VHd = VTl
if {IsDet Value} andthen ({IsNumber Value} orelse {IsLiteral Value})
then constant(Value)
else value(self.reg)
end
end
meth makeVO(CS VHd VTl ?VO)
VHd = VTl
VO = self
end
end
fun {NewPseudoVariableOccurrence CS}
{New PseudoVariableOccurrence init({CS newReg($)})}
end
proc {MakeMessageArgs ActualArgs CS ?Regs VHd VTl}
case ActualArgs of Arg|Argr then Reg1 Regr VInter VO in
{Arg makeVO(CS VHd VInter ?VO)}
{VO reg(?Reg1)}
Regs = Reg1|Regr
{MakeMessageArgs Argr CS ?Regr VInter VTl}
[] nil then
Regs = nil
VHd = VTl
end
end
fun {GetRegs VOs}
case VOs of VO|VOr then {VO reg($)}|{GetRegs VOr}
[] nil then nil
end
end
local
proc {LoadActualArgs ActualArgs CS VHd VTl ?NewArgs}
case ActualArgs of Arg|Argr then Value VInter NewArgr in
{Arg getCodeGenValue(?Value)}
if {IsDet Value} andthen {IsName Value} then PVO in
PVO = {NewPseudoVariableOccurrence CS}
PVO.value = Value
VHd = vEquateConstant(_ Value {PVO reg($)} VInter)
NewArgs = PVO|NewArgr
else
VHd = VInter
NewArgs = Arg|NewArgr
end
{LoadActualArgs Argr CS VInter VTl ?NewArgr}
[] nil then
VHd = VTl
NewArgs = nil
end
end
proc {MakeBuiltinApplication Builtinname Coord ActualArgs CS VHd VTl}
case Builtinname of 'Object.new' then
[Arg1 Arg2 Arg3] = ActualArgs ObjReg Cont
in
%% this ensures that the created object is always a fresh
%% register and that the message is sent before the new
%% object is unified with the output variable. This is
%% needed for the correctness of the sendMsg-optimization
%% performed in the CodeEmitter:
{CS newReg(?ObjReg)}
VHd = vCallBuiltin(_ 'Object.new'
[{Arg1 reg($)} {Arg2 reg($)} ObjReg]
Coord Cont)
Cont = vUnify(_ ObjReg {Arg3 reg($)} VTl)
[] 'Number.\'+\'' then [Arg1 Arg2 Arg3] = ActualArgs Value in
{Arg1 getCodeGenValue(?Value)}
if {IsDet Value} then
case Value of 1 then
VHd = vCallBuiltin(_ 'Int.\'+1\''
[{Arg2 reg($)} {Arg3 reg($)}]
Coord VTl)
[] ~1 then
VHd = vCallBuiltin(_ 'Int.\'-1\''
[{Arg2 reg($)} {Arg3 reg($)}]
Coord VTl)
else skip
end
end
if {IsDet VHd} then skip
else Value in
{Arg2 getCodeGenValue(?Value)}
if {IsDet Value} then
case Value of 1 then
VHd = vCallBuiltin(_ 'Int.\'+1\''
[{Arg1 reg($)} {Arg3 reg($)}]
Coord VTl)
[] ~1 then
VHd = vCallBuiltin(_ 'Int.\'-1\''
[{Arg1 reg($)} {Arg3 reg($)}]
Coord VTl)
else skip
end
end
end
[] 'Number.\'-\'' then [Arg1 Arg2 Arg3] = ActualArgs Value in
{Arg2 getCodeGenValue(?Value)}
if {IsDet Value} then
case Value of 1 then
VHd = vCallBuiltin(_ 'Int.\'-1\''
[{Arg1 reg($)} {Arg3 reg($)}]
Coord VTl)
[] ~1 then
VHd = vCallBuiltin(_ 'Int.\'+1\''
[{Arg1 reg($)} {Arg3 reg($)}]
Coord VTl)
else skip
end
else skip
end
elseif CS.controlFlowInfoSwitch then skip
elsecase Builtinname of 'Value.\'.\'' then
[Arg1 Arg2 Arg3] = ActualArgs Feature in
{Arg2 getCodeGenValue(?Feature)}
if {IsDet Feature}
andthen ({IsLiteral Feature} orelse {IsInt Feature})
then Value1 ValueF AlwaysSucceeds in
{Arg1 getCodeGenValue(?Value1)}
if {IsDet Value1} then
if {IsRecord Value1} andthen
{HasFeature Value1 Feature}
then
AlwaysSucceeds = true
ValueF = Value1.Feature
else
AlwaysSucceeds = false
end
elsecase {Value.status Value1}
of kinded(record) andthen
{Member Feature {RecordC.reflectArity Value1}}
then
%% When the record was very large and exceeded the width
%% limit, then SA approximated it with an OFS instead.
%% We check here if the feature is present. This is
%% the additional case that we need in order to optimize
%% away references to e.g. List.last.
AlwaysSucceeds = true
ValueF = Value1^Feature
else
AlwaysSucceeds = false
end
if AlwaysSucceeds
andthen {IsObject ValueF}
andthen {HasFeature ValueF
Core.imAVariableOccurrence}
andthen {IsDet {ValueF reg($)}}
then
%% Evaluate by issuing an equation.
%% Note: {Value1.Feature reg($)} may be undetermined
%% for nested records annotated by valToSubst.
{Arg3 makeEquation(CS Value1.Feature VHd VTl)}
else
%% Because the static analyzer may annotate some
%% variable equality at Arg3, we cannot use the
%% (dereferencing) {Arg3 reg($)} call but have to
%% use the variable's original register:
VHd = vInlineDot(_ {Arg1 reg($)} Feature
{{Arg3 getVariable($)} reg($)}
AlwaysSucceeds Coord VTl)
end
end
[] 'Object.\'@\'' then [Arg1 Arg2] = ActualArgs Feature in
{Arg1 getCodeGenValue(?Feature)}
if {IsDet Feature}
andthen ({IsInt Feature} orelse {IsLiteral Feature})
then
VHd = vInlineAt(_ Feature {Arg2 reg($)} VTl)
end
[] 'Value.catAccessOO' then [Arg1 Arg2] = ActualArgs Feature in
{Arg1 getCodeGenValue(?Feature)}
if {IsDet Feature}
andthen ({IsInt Feature} orelse {IsLiteral Feature})
then
VHd = vInlineAt(_ Feature {Arg2 reg($)} VTl)
end
[] 'Object.\'<-\'' then [Arg1 Arg2] = ActualArgs Feature in
{Arg1 getCodeGenValue(?Feature)}
if {IsDet Feature}
andthen ({IsInt Feature} orelse {IsLiteral Feature})
then
VHd = vInlineAssign(_ Feature {Arg2 reg($)} VTl)
end
[] 'Value.catAssignOO' then [Arg1 Arg2] = ActualArgs Feature in
{Arg1 getCodeGenValue(?Feature)}
if {IsDet Feature}
andthen ({IsInt Feature} orelse {IsLiteral Feature})
then
VHd = vInlineAssign(_ Feature {Arg2 reg($)} VTl)
end
[] 'Object.\',\'' then [Arg1 Arg2] = ActualArgs Value in
{Arg2 getCodeGenValue(?Value)}
if {{Arg1 getVariable($)} isToplevel($)}
andthen {IsDet Value} andthen {IsRecord Value}
andthen {Record.all Value
fun {$ Arg}
{Not {HasFeature Arg Core.imAVariableOccurrence}}
orelse {IsDet {Arg reg($)}}
end}
then RecordArity ActualArgs Regs Cont1 in
RecordArity = if {IsTuple Value} then {Width Value}
else {Arity Value}
end
ActualArgs = {Record.toList Value}
{MakeMessageArgs ActualArgs CS ?Regs VHd Cont1}
Cont1 = vCallMethod(_ {Arg1 reg($)}
{Label Value} RecordArity
Regs Coord VTl)
end
else skip
end
if {IsDet VHd} orelse
%% this is the case when makeEquation above calls MakeUnify and
%% the equation is trivial (same reg on both sides). In this case
%% no instruction is inserted, we have just unified VHd=VTl. So,
%% here we just check whether we have done that.
{System.eq VHd VTl}
then skip
else
VHd = vCallBuiltin(_ Builtinname {GetRegs ActualArgs} Coord VTl)
end
end
proc {MakeDirectApplication Value Coord ActualArgs CS VHd VTl}
if {CompilerSupport.isBuiltin Value} then Builtinname in
Builtinname = {System.printName Value}
{MakeBuiltinApplication Builtinname Coord ActualArgs CS VHd VTl}
else
VHd = vCallConstant(_ Value {GetRegs ActualArgs} Coord VTl)
end
end
in
proc {MakeApplication Designator Coord ActualArgs CS VHd VTl}
VInter NewActualArgs Proc
in
{LoadActualArgs ActualArgs CS VHd VInter ?NewActualArgs}
{Designator getCodeGenValue(?Proc)}
if {IsDet Proc} andthen {IsProcedure Proc} then
{MakeDirectApplication Proc Coord NewActualArgs CS VInter VTl}
else Value Def in
{Designator getValue(?Value)}
if {HasFeature Value Core.imAToken}
andthen ({Value getDefinition(?Def)} Def \= unit)
then
{Def codeGenApply(Designator Coord NewActualArgs CS VInter VTl)}
elseif {{Designator getVariable($)} isToplevel($)}
andthen {Not CS.controlFlowInfoSwitch}
then
VInter = vCallGlobal(_ {Designator reg($)}
{GetRegs NewActualArgs} Coord VTl)
else
VInter = vCall(_ {Designator reg($)} {GetRegs NewActualArgs}
Coord VTl)
end
end
end
proc {MakeRunTimeProcApplication Name Coord ActualArgs CS VHd VTl}
VInter NewActualArgs
in
{LoadActualArgs ActualArgs CS VHd VInter ?NewActualArgs}
{MakeDirectApplication RunTime.procValues.Name
Coord NewActualArgs CS VInter VTl}
end
end
proc {MakeException Lab Literal Coord VOs CS VHd VTl} Reg VO VArgs VInter in
{CS newReg(?Reg)}
VO = {New PseudoVariableOccurrence init(Reg)}
VArgs = constant(Literal)|{Append
case Coord of unit then
[constant('') constant(unit)]
else
[constant(Coord.1) constant(Coord.2)]
end
{Map VOs fun {$ VO} value({VO reg($)}) end}}
VHd = vEquateRecord(_ Lab {Length VArgs} Reg VArgs VInter)
{MakeRunTimeProcApplication 'Exception.raiseError' {CoordNoDebug Coord}
[VO] CS VInter VTl}
end
\insert PatternMatching
local
fun {MakeFromPropSub FromProp CS VHd VTl}
case FromProp of VO|VOr then ArgIn VInter ConsReg X = unit NewArg in
ArgIn = {MakeFromPropSub VOr CS VHd VInter}
{CS newReg(?ConsReg)}
{VO makeRecordArgument(CS X X ?NewArg)}
VInter = vEquateRecord(_ '|' 2 ConsReg [NewArg ArgIn] VTl)
value(ConsReg)
[] nil then
VHd = VTl
constant(nil)
end
end
in
proc {MakeFromProp FromProp CS VHd VTl ?VO} Reg in
case FromProp of _|_ then
value(Reg) = {MakeFromPropSub FromProp CS VHd VTl}
[] nil then
{CS newReg(?Reg)}
VHd = vEquateConstant(_ nil Reg VTl)
end
VO = {New PseudoVariableOccurrence init(Reg)}
end
end
local
fun {MakeAttrFeatSub Xs CS}
case Xs of X|Xr then
case X of _#_ then X
else VO in
VO = {NewPseudoVariableOccurrence CS}
VO.value = RunTime.literals.ooFreeFlag
X#VO
end|{MakeAttrFeatSub Xr CS}
[] nil then
nil
end
end
in
proc {MakeAttrFeat Kind AttrFeat CS VHd VTl ?VO}
Label = {New Core.valueNode init(Kind unit)}
Args = {MakeAttrFeatSub AttrFeat CS}
in
VO = {NewPseudoVariableOccurrence CS}
{MakeConstruction CS VO Label Args VHd VTl}
end
end
local
proc {MakeConstructionBasic CS VO Label Feats TheLabel Args VHd VTl}
case Feats of nil then % transform `f()' into `f':
VHd = vEquateConstant(_ Label {VO reg($)} VTl)
else PairList Rec RecordArity VArgs VInter in
PairList = {List.zip Feats Args
fun {$ F Arg}
case Arg of _#T then F#T else F#Arg end
end}
try
Rec = {List.toRecord Label PairList}
catch error(kernel(recordConstruction ...) ...) then C in
{TheLabel getCoord(?C)}
{CS.reporter
error(coord: C kind: 'code generation error'
msg: 'duplicate feature in record construction')}
Rec = {AdjoinList Label() PairList}
end
RecordArity = if {IsTuple Rec} then {Width Rec}
else {Arity Rec}
end
VArgs#VInter = {Record.foldR Rec
fun {$ X In#VTl} VArg VHd in
{X makeRecordArgument(CS VHd VTl ?VArg)}
(VArg|In)#VHd
end nil#VTl}
VHd = vEquateRecord(_ Label RecordArity {VO reg($)} VArgs VInter)
end
end
proc {MakeConstructionTuple CS VO TheLabel Rec VHd VTl}
C SubtreesReg SubtreesVO WidthValue Cont
in
%% translate the construction as:
%% {`List.toTuple` Label [Subtree1 ... Subtreen] ?Reg}
{TheLabel getCoord(?C)}
{CS newReg(?SubtreesReg)}
SubtreesVO = {New PseudoVariableOccurrence init(SubtreesReg)}
WidthValue = {Width Rec}
case WidthValue of 0 then
VHd = vEquateConstant(_ nil SubtreesReg Cont)
else
fun {MakeList I VHd VTl}
if I =< WidthValue then ArgIn VInter1 ConsReg VInter2 NewArg in
ArgIn = {MakeList I + 1 VHd VInter1}
{CS newReg(?ConsReg)}
{Rec.I makeRecordArgument(CS VInter1 VInter2 ?NewArg)}
VInter2 = vEquateRecord(_ '|' 2 ConsReg [NewArg ArgIn] VTl)
value(ConsReg)
else
VHd = VTl
constant(nil)
end
end
Arg VInter1 VInter2 NewArg
in
Arg = {MakeList 2 VHd VInter1}
{Rec.1 makeRecordArgument(CS VInter1 VInter2 ?NewArg)}
VInter2 = vEquateRecord(_ '|' 2 SubtreesReg [NewArg Arg] Cont)
end
{MakeRunTimeProcApplication 'List.toTuple' {CoordNoDebug C}
[TheLabel SubtreesVO VO] CS Cont VTl}
end
proc {MakeConstructionRecord CS VO TheLabel Args VHd VTl}
C SubtreesReg SubtreesVO Cont
in
%% translate the construction as:
%% {`List.toRecord` Label [Feat1#Subtree1 ... Featn#Subtreen] ?Reg}
{TheLabel getCoord(?C)}
{CS newReg(?SubtreesReg)}
SubtreesVO = {New PseudoVariableOccurrence init(SubtreesReg)}
case Args of (F1#A1)|Argr then % else it would have been a tuple
fun {MakePairList Args VHd VTl}
case Args of F#A|Argr then
ArgIn VInter1 PairReg ConsReg PairArg1 PairArg2
VInter2 VInter3
in
ArgIn = {MakePairList Argr VHd VInter1}
{CS newReg(?PairReg)}
{CS newReg(?ConsReg)}
PairArg1 = if {IsInt F} orelse {IsLiteral F} then constant(F)
else value({F reg($)})
end
{A makeRecordArgument(CS VInter1 VInter2 ?PairArg2)}
VInter2 = vEquateRecord(_ '#' 2 PairReg [PairArg1 PairArg2]
VInter3)
VInter3 = vEquateRecord(_ '|' 2 ConsReg
[value(PairReg) ArgIn] VTl)
value(ConsReg)
[] nil then
VHd = VTl
constant(nil)
end
end
Arg VInter1 PairReg PairArg1 PairArg2 VInter2 VInter3
in
Arg = {MakePairList Argr VHd VInter1}
{CS newReg(?PairReg)}
PairArg1 = if {IsInt F1} orelse {IsLiteral F1} then constant(F1)
else value({F1 reg($)})
end
{A1 makeRecordArgument(CS VInter1 VInter2 ?PairArg2)}
VInter2 = vEquateRecord(_ '#' 2 PairReg
[PairArg1 PairArg2] VInter3)
VInter3 = vEquateRecord(_ '|' 2 SubtreesReg
[value(PairReg) Arg] Cont)
end
if {HasFeature TheLabel Core.imAVariableOccurrence} then
{MakeRunTimeProcApplication 'List.toRecord' {CoordNoDebug C}
[TheLabel SubtreesVO VO] CS Cont VTl}
else LabelReg LabelVO LabelValue Inter in
{CS newReg(?LabelReg)}
LabelVO = {New PseudoVariableOccurrence init(LabelReg)}
{TheLabel getCodeGenValue(?LabelValue)}
Cont = vEquateConstant(_ LabelValue LabelReg Inter)
{MakeRunTimeProcApplication 'List.toRecord' {CoordNoDebug C}
[LabelVO SubtreesVO VO] CS Inter VTl}
end
end
in
proc {MakeConstruction CS VO TheLabel Args VHd VTl}
%% Determine in which way the record may be constructed.
Label Feats LabelIsDet FeatsAreDet
in
{TheLabel getCodeGenValue(?Label)}
Feats = {List.mapInd Args
fun {$ I Arg}
case Arg of F#_ then {F getCodeGenValue($)}
else I
end
end}
LabelIsDet = {IsDet Label}
FeatsAreDet = {All Feats IsDet}
if LabelIsDet andthen FeatsAreDet then
{MakeConstructionBasic CS VO Label Feats TheLabel Args VHd VTl}
elseif FeatsAreDet then PairList Rec in
PairList = {List.zip Feats Args
fun {$ F Arg}
case Arg of _#T then F#T else F#Arg end
end}
try
Rec = {List.toRecord someLabel PairList}
catch error(kernel(recordConstruction ...) ...) then C in
{TheLabel getCoord(?C)}
{CS.reporter
error(coord: C kind: 'code generation error'
msg: 'duplicate feature in record construction')}
Rec = {AdjoinList someLabel() PairList}
end
if {IsTuple Rec} then
{MakeConstructionTuple CS VO TheLabel Rec VHd VTl}
else NewArgs in
NewArgs = {Record.toListInd Rec}
{MakeConstructionRecord CS VO TheLabel NewArgs VHd VTl}
end
else NewArgs in
NewArgs = {List.zip Feats Args
fun {$ FV Arg} F T in
case Arg of X#Y then F = X T = Y else T = Arg end
if {IsDet FV} andthen
({IsInt FV} orelse {IsLiteral FV})
then FV
else F
end#T
end}
{MakeConstructionRecord CS VO TheLabel NewArgs VHd VTl}
end
end
end
class CodeGenConstructionOrPattern
meth getCodeGenValue($)
if {IsDet {@label getCodeGenValue($)}}
andthen {All @args
fun {$ Arg}
case Arg of F#_ then {IsDet {F getCodeGenValue($)}}
else true
end
end}
then {@value getValue($)}
else _
end
end
end
class CodeGenStatement
meth startCodeGen(Nodes State Reporter OldVs NewVs ?GPNs ?Code)
CS StartAddr GRegs BodyCode0 NLiveRegs
BodyCode1 BodyCode2 BodyCode StartLabel EndLabel
in
CS = {New CodeStore.'class' init(State Reporter)}
{ForAll OldVs proc {$ V} {V setFreshReg(CS)} end}
{ForAll NewVs proc {$ V} {V setFreshReg(CS)} end}
{CS startDefinition()}
{CodeGenList Nodes CS StartAddr nil}
{CS endDefinition(StartAddr nil nil ?GRegs ?BodyCode0 ?NLiveRegs)}
BodyCode0 = BodyCode1#BodyCode2
BodyCode = BodyCode1
{CS getRegNames(GRegs ?GPNs)}
StartLabel = {NewName}
EndLabel = {NewName}
Code =
lbl(StartLabel)|
definition(x(0) EndLabel
pid('Toplevel abstraction' 0 pos('' 1 0) [sited]
NLiveRegs)
unit {List.mapInd GRegs fun {$ I _} g(I - 1) end}
BodyCode)|
endDefinition(StartLabel)|
{Append BodyCode2 [lbl(EndLabel) tailCall(x(0) 0)]}
end
end
class CodeGenTypeOf from CodeGenStatement
meth codeGen(CS VHd VTl)
VHd = vEquateConstant(_ @value {@res reg($)} VTl)
end
end
class CodeGenStepPoint from CodeGenStatement
meth codeGen(CS VHd VTl) VInter1 VInter2 in
{CodeGenList @statements CS VInter1 VInter2}
{StepPoint @coord @kind VHd VTl VInter1 VInter2}
end
end
class CodeGenDeclaration from CodeGenStatement
meth codeGen(CS VHd VTl) Cont1 Cont2 in
{ForAll @localVars proc {$ V} {V setReg(CS)} end}
{MakePermanent @localVars VHd Cont1 Cont2 VTl CS}
{CodeGenList @statements CS Cont1 Cont2}
end
end
class CodeGenSkipNode from CodeGenStatement
meth codeGen(CS VHd VTl) VInter in
{StepPoint @coord 'skip' VHd VTl VInter VInter}
end
end
class CodeGenEquation from CodeGenStatement
meth codeGen(CS VHd VTl)
{@right makeEquation(CS @left VHd VTl)}
end
end
class CodeGenConstruction from CodeGenConstructionOrPattern
meth makeEquation(CS VO VHd VTl)
{MakeConstruction CS VO @label @args VHd VTl}
end
meth makeVO(CS VHd VTl ?VO)
VO = {NewPseudoVariableOccurrence CS}
CodeGenConstruction, makeEquation(CS VO VHd VTl)
end
meth makeRecordArgument(CS VHd VTl $) Label Feats in
{@label getCodeGenValue(?Label)}
Feats = {List.mapInd @args
fun {$ I Arg}
case Arg of F#_ then {F getCodeGenValue($)} else I end
end}
if {IsDet Label} andthen {All Feats IsDet} then
case Feats of nil then
VHd = VTl
constant(Label)
else PairList Rec RecordArity VArgs in
PairList = {List.zip Feats @args
fun {$ F Arg}
case Arg of _#T then F#T else F#Arg end
end}
try
Rec = {List.toRecord Label PairList}
catch error(kernel(recordConstruction ...) ...) then C in
{@label getCoord(?C)}
{CS.reporter
error(coord: C kind: 'code generation error'
msg: 'duplicate feature in record construction')}
Rec = {AdjoinList Label() PairList}
end
RecordArity = if {IsTuple Rec} then {Width Rec}
else {Arity Rec}
end
VArgs#VHd = {Record.foldR Rec
fun {$ X In#VTl} VArg VHd in
{X makeRecordArgument(CS VHd VTl ?VArg)}
(VArg|In)#VHd
end nil#VTl}
record(Label RecordArity VArgs)
end
else VO in
VO = {NewPseudoVariableOccurrence CS}
CodeGenConstruction, makeEquation(CS VO VHd VTl)
value({VO reg($)})
end
end
end
class CodeGenDefinition from CodeGenStatement
meth codeGen(CS VHd VTl)
VHd0 VTl0 V FileName Line Col PrintName PredId OuterNLiveRegs StateReg
in
{@designator getVariable(?V)}
case @coord of unit then FileName = '' Line = 1 Col = 0
elseof C then FileName = C.1 Line = C.2 Col = C.3
end
PrintName = case {V getPrintName($)} of unit then @printName
elseof PN then PN
end
PredId = pid(PrintName {Length @formalArgs} pos(FileName Line Col)
if {Member sited @procFlags} then [sited]
else nil
end
OuterNLiveRegs)
\ifdef DEBUG_DEFS
{System.show PredId}
\endif
if @isStateUsing then
if {CS.state getSwitch(staticvarnames $)} then
{CS newSelfReg(?StateReg)}
else
{CS newReg(?StateReg)}
end
else
StateReg = none
end
case @toCopy of unit then
FormalRegs AllRegs AllRegs2 BodyVInter BodyVInstr GRegs Code VInter
Cont1 Cont2
in
{CS startDefinition()}
FormalRegs = {Map @formalArgs
fun {$ V}
{V setReg(CS)}
{V reg($)}
end}
{MakePermanent @formalArgs BodyVInter Cont1 Cont2 nil CS}
{CodeGenList @statements CS Cont1 Cont2}
AllRegs = case @allVariables of nil then nil
elseof Vs then {GetRegs Vs}
end
case StateReg of none then
BodyVInstr = BodyVInter
VHd0 = VInter
AllRegs2 = AllRegs
else
BodyVInstr = vSetSelf(_ StateReg BodyVInter)
VHd0 = vGetSelf(_ StateReg VInter)
AllRegs2 = StateReg|AllRegs
end
{CS endDefinition(BodyVInstr FormalRegs AllRegs2 ?GRegs ?Code
?OuterNLiveRegs)}
VInter = vDefinition(_ {V reg($)} PredId @procedureRef
GRegs Code VTl0)
else
VInter FormalRegs AllRegs
InnerBodyVInter InnerBodyVInstr InnerGRegs InnerCode
InnerDefinitionReg InnerPredId InnerNLiveRegs
OuterBodyVInstr OuterBodyVInter2 OuterGRegs OuterCode
in
{CS startDefinition()}
FormalRegs = {Map @formalArgs
fun {$ V}
{V setReg(CS)}
{V reg($)}
end}
{CS startDefinition()}
{CodeGenList @statements CS InnerBodyVInter nil}
AllRegs = case @allVariables of nil then nil
elseof Vs then {GetRegs Vs}
end
case StateReg of none then
InnerBodyVInstr = InnerBodyVInter
VHd0 = VInter
else
InnerBodyVInstr = vSetSelf(_ StateReg InnerBodyVInter)
VHd0 = vGetSelf(_ StateReg VInter)
end
{CS endDefinition(InnerBodyVInstr nil AllRegs
?InnerGRegs ?InnerCode ?InnerNLiveRegs)}
{CS newReg(?InnerDefinitionReg)}
InnerPredId = {Adjoin PredId
pid(case PrintName of '' then ''
else {VirtualString.toAtom PrintName#'/body'}
end 0
4: if {Member sited @procFlags} then [sited]
else nil
end
5: InnerNLiveRegs)}
case @toCopy of nil then Reg OuterBodyVInter1 in
{CS newReg(?Reg)}
OuterBodyVInstr = vEquateConstant(_ nil Reg OuterBodyVInter1)
OuterBodyVInter1 = vDefinitionCopy(_ Reg InnerDefinitionReg
InnerPredId unit
InnerGRegs InnerCode
OuterBodyVInter2)
elseof Xs then
fun {MakeCopyList Xs VHd VTl}
case Xs of X|Xr then ArgIn VInter1 ConsReg ConsArg1 in
ArgIn = {MakeCopyList Xr VHd VInter1}
{CS newReg(?ConsReg)}
ConsArg1 = if {ForeignPointer.is X} then
procedureRef(X)
elseif {IsName X} then
constant(X)
else
{Exception.raiseError
compiler(internalTypeError X
'ForeignPointerOrName')}
unit
end
VInter1 = vEquateRecord(_ '|' 2 ConsReg [ConsArg1 ArgIn]
VTl)
value(ConsReg)
[] nil then
VHd = VTl
constant(nil)
end
end
Reg OuterBodyVInter1
in
value(Reg) = {MakeCopyList Xs OuterBodyVInstr OuterBodyVInter1}
OuterBodyVInter1 = vDefinitionCopy(_ Reg InnerDefinitionReg
InnerPredId unit
InnerGRegs InnerCode
OuterBodyVInter2)
end
OuterBodyVInter2 = vCall(_ InnerDefinitionReg nil unit nil)
{CS endDefinition(OuterBodyVInstr FormalRegs AllRegs
?OuterGRegs ?OuterCode ?OuterNLiveRegs)}
VInter = vDefinition(_ {V reg($)} PredId @procedureRef
OuterGRegs OuterCode VTl0)
end
{StepPoint @coord 'definition' VHd VTl VHd0 VTl0}
statements <- unit % hand them to the garbage collector
end
meth codeGenApply(Designator Coord ActualArgs CS VHd VTl)
case @procedureRef of unit then
VHd = vCall(_ {Designator reg($)} {GetRegs ActualArgs} Coord VTl)
elseof ID andthen {ForeignPointer.is ID} then
VHd = vCallProcedureRef(_ ID {GetRegs ActualArgs} Coord VTl)
end
end
end
class CodeGenClauseBody from CodeGenDefinition
feat ClauseBodyShared ClauseBodyTail
meth codeGen(CS VHd VTl)
%% code for the clause body is only generated when it is applied
VHd = VTl
end
meth codeGenApply(Designator Coord ActualArgs CS VHd VTl)
ActualArgs = nil % by construction
VHd = self.ClauseBodyShared
VTl = self.ClauseBodyTail % see log message for rev 1.132
if {IsFree VHd} then Label Addr in
{CS newLabel(?Label)}
VHd = vShared(_ _ Label Addr)
{CodeGenList @statements CS Addr VTl}
end
end
end
class CodeGenApplication from CodeGenStatement
meth codeGen(CS VHd VTl)
if {IsDet self.codeGenMakeEquateLiteral} then VHd0 VTl0 in
%% the application is either a toplevel application of NewName
%% or any application of NewUniqueName:
VHd0 = vEquateConstant(_ self.codeGenMakeEquateLiteral
{{List.last @actualArgs} reg($)} VTl0)