-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGen.pas
1095 lines (871 loc) · 32.2 KB
/
CodeGen.pas
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
/// Þíèò êîäîãåíåðàòîðà
unit CodeGen;
interface
uses
System,
System.Diagnostics,
System.Diagnostics.SymbolStore,
System.Collections,
System.Collections.Generic,
System.Reflection.Emit,
System.Runtime.InteropServices,
System.IO,
System.Reflection,
ASyntaxTree,
ASemanticTree,
CommonUnit;
type
GCodeGenerator = class
private
il: Emit.ILGenerator := nil;
docs: Dictionary<string, ISymbolDocumentWriter>;
ModuleName: string;
AsmbName: AssemblyName;
Asmb: Emit.AssemblyBuilder;
Modb: Emit.ModuleBuilder;
TypeBuilder: Emit.TypeBuilder;
typeBuilders := new List<Emit.TypeBuilder>;
Module: SProgramNode;
ContinueLabels: Stack<&Label>;
BreakLabels: Stack<&Label>;
methodBuilder: Emit.MethodBuilder;
PEFileKind := PEFileKinds.ConsoleApplication;
GlobalSymbolTable: Dictionary<string, Emit.FieldBuilder>;
SymbolTable: Dictionary<string, Emit.LocalBuilder>;
ArgsSymbolTable: Dictionary<string, integer>;
ArgsTypes: Dictionary<integer, &Type>;
LabelSymbolTable: Dictionary<string, Emit.Label>;
ResultVar: Emit.LocalBuilder;
GenResult := False;
GenExitMark := false;
ExitLabel: &Label;
procedure GenModule;
procedure GenUnit(UnitTree: SUnitTree);
procedure GenMethod(Mthd: SFunctionNode; EntryPoint: boolean);
procedure GenMethod(Mthd: SNetFunctionNode; EntryPoint: boolean);
procedure GenMethod(Mthd: SNativeFunctionNode; EntryPoint: boolean);
procedure GenStmt(Stmt: SStmtNode; AsExpr: boolean);
procedure GenFunction(CFunctionNode: SCallFunctionNode);
procedure GenExpr(Expr: SExprNode; params ExpectedTypes: array of &Type);
procedure GenActionExprNode(Expr: SActionNode; params ExpectedTypes: array of &Type);
procedure MarkSequencePoint(il: Emit.ILGenerator; fname: string; BeginLine, BeginColumn, EndLine, EndColumn: integer);
procedure Store(Name: string; _type: &Type);
public
constructor Create(ModuleName: string; Module: SProgramNode);
end;
CodeGenException = class(Exception)
Loc: Location;
constructor Create(message: string; Loc: Location);
begin
inherited Create(message);
self.Loc := Loc;
end;
end;
const
RT_CURSOR = 1;
RT_BITMAP = 2;
RT_ICON = 3;
RT_RCDATA = 10;
RT_HTML = 23;
RT_MANIFEST = 24;
implementation
function ArrayToStr<T>(a: array of T; delimiter: string): string;
begin
result := '[';
for var i := 0 to a.Length - 2 do
result := result + object(a[i]).ToString + delimiter;
result := result + object(a[a.Length - 1]).ToString + ']';
end;
constructor GCodeGenerator.Create(ModuleName: string; Module: SProgramNode);
begin
var oldCurDir := Environment.CurrentDirectory;
Environment.CurrentDirectory := IO.Path.GetDirectoryName(moduleName);
Self.ModuleName := ModuleName;
Self.Module := Module;
var ext := '.exe';
if (Module is SLibraryTree) then ext := '.dll';
DeleteFile(IO.Path.ChangeExtension(moduleName, ext));
AsmbName := new AssemblyName(IO.Path.GetFileNameWithoutExtension(moduleName));
Asmb := System.AppDomain.CurrentDomain.DefineDynamicAssembly(AsmbName, Emit.AssemblyBuilderAccess.Save);
var needDefineVersion := False;
var ResDefine := False;
var product, productVersion, company, copyright, trademark: string;
// Îáðàáîòêà äèðåêòèâ
foreach d: CompilerDirective in Options.CompilerDirectives do
begin
if d.Name.ToLower = 'apptype' then
begin
if (d.Value.ToLower = 'windows') or (d.Value.ToLower = 'window') then
PEFileKind := PEFileKinds.WindowApplication
else if d.Value.ToLower = 'console' then
PEFileKind := PEFileKinds.ConsoleApplication
else raise new CodeGenException('îæèäàëîñü çíà÷åíèå "windows" èëè "console" äëÿ äèðåêòèâû "apptype".', d);
end
else if d.Name.ToLower = 'product' then
begin
needDefineVersion := True;
product := d.Value;
end
else if d.Name.ToLower = 'productversion' then
begin
needDefineVersion := True;
productVersion := d.Value;
end
else if d.Name.ToLower = 'company' then
begin
needDefineVersion := True;
company := d.Value;
end
else if d.Name.ToLower = 'copyright' then
begin
needDefineVersion := True;
copyright := d.Value;
end
else if d.Name.ToLower = 'trademark' then
begin
needDefineVersion := True;
trademark := d.Value;
end
else if d.Name.ToLower = 'win32res' then
begin
asmb.DefineUnmanagedResource(d.Value)
end
else if (d.Name.ToLower = 'res') or (d.Name.ToLower = 'resource') then
begin
try
asmb.AddResourceFile(Path.GetFileName(d.Value), d.Value, ResourceAttributes.Public);
ResDefine := True;
except
end;
//var rw := asmb.DefineResource('res.resources', 'kz-kz', 'res.resources');
//rw.AddResource(Path.GetFileName(d.Value), &File.ReadAllBytes (d.Value));
end;
end;
modb := asmb.DefineDynamicModule(AsmbName.Name,
AsmbName.Name + ext, Options.Debug);
docs := new Dictionary<string, ISymbolDocumentWriter>;
GlobalSymbolTable := new Dictionary<string, FieldBuilder>;
GenModule;
if needDefineVersion and not ResDefine then
asmb.DefineVersionInfoResource(product, productVersion, company, copyright, trademark);
foreach a: Emit.TypeBuilder in typeBuilders do a.CreateType;
asmb.Save(AsmbName.Name + ext);
modb.CreateGlobalFunctions;
SymbolTable := nil;
LabelSymbolTable := nil;
il := nil;
Environment.CurrentDirectory := oldCurDir;
Options.OutFile := modb.FullyQualifiedName;
end;
procedure GCodeGenerator.GenModule;
begin
for var i := 0 to Module.UsedUnits.Count - 1 do
self.GenUnit((Module.UsedUnits[i] as SUnitTree));
if (Module is SLibraryTree) then
typeBuilder := modb.DefineType(AsmbName.Name + '.' + AsmbName.Name, TypeAttributes.Public)
else typeBuilder := modb.DefineType(AsmbName.Name + '.' + 'Program', TypeAttributes.Public);
for var i := 0 to Module.GlobalVarList.Count - 1 do
begin
if (Module.GlobalVarList[i] is SGlobalDeclareNode) then
begin
var GlobalDeclareNode := SGlobalDeclareNode(Module.GlobalVarList[i]);
var myFieldBuilder := TypeBuilder.DefineField(GlobalDeclareNode.Name,
GlobalDeclareNode._Type, FieldAttributes.&Public or FieldAttributes.Static);
GlobalSymbolTable.Add(GlobalDeclareNode.Name, myFieldBuilder);
end;
end;
//DefineMethods(Module.SGeneric_Functions);
foreach a: SFunctionNode in Module.SGeneric_Functions do
a.ModulBuilder := modb;
foreach a: SFunctionNode in Module.SGeneric_Functions do
a.TypBuilder := typeBuilder;
if (Module is SProgramTree) then GenMethod(Module.SGeneric_Functions[0], True)
else GenMethod(Module.SGeneric_Functions[0], False);
if not (Module is SProgramTree) then
for var i := 1 to Module.SGeneric_Functions.Count - 1 do
GenMethod(Module.SGeneric_Functions[i], False);
typeBuilder.CreateType();
end;
procedure GCodeGenerator.GenUnit(UnitTree: SUnitTree);
begin
for var i := 0 to UnitTree.UsedUnits.Count - 1 do
self.GenUnit((UnitTree.UsedUnits[i] as SUnitTree));
typeBuilder := modb.DefineType(UnitTree.UnitName + '.' + UnitTree.UnitName, TypeAttributes.Public);
for var i := 0 to UnitTree.GlobalVarList.Count - 1 do
begin
if (UnitTree.GlobalVarList[i] is SGlobalDeclareNode) then
begin
var GlobalDeclareNode := SGlobalDeclareNode(UnitTree.GlobalVarList[i]);
var myFieldBuilder := TypeBuilder.DefineField(GlobalDeclareNode.Name,
GlobalDeclareNode._Type, FieldAttributes.&Public or FieldAttributes.Static);
GlobalSymbolTable.Add(GlobalDeclareNode.Name, myFieldBuilder);
end;
end;
foreach a: SFunctionNode in UnitTree.SGeneric_Functions do
a.ModulBuilder := modb;
foreach a: SFunctionNode in UnitTree.SGeneric_Functions do
a.TypBuilder := typeBuilder;
typeBuilders.Add(typeBuilder);
SymbolTable := nil;
LabelSymbolTable := nil;
il := nil;
end;
procedure GCodeGenerator.GenMethod(Mthd: SFunctionNode; EntryPoint: boolean);
begin
if Mthd.MthdBuilder = nil then
begin
var Types := new &Type[Mthd.ParametersType.Count];
for var b := 0 to Mthd.ParametersType.Count - 1 do
begin
Types[b] := Mthd.ParametersType[b]._Type;
end;
if Mthd is SNetFunctionNode then
methodBuilder := Mthd.typBuilder.DefineMethod(Mthd.Name, MethodAttributes.Static or MethodAttributes.&Public, Mthd.ReturnType, Types)
else if (Mthd is SNativeFunctionNode) then
begin
methodBuilder := typeBuilder.DefinePInvokeMethod(
(Mthd as SNativeFunctionNode).DllNameMethod,
(Mthd as SNativeFunctionNode).DllName,
MethodAttributes.Public or MethodAttributes.Static or MethodAttributes.PinvokeImpl or MethodAttributes.HideBySig,
CallingConventions.Standard,
Mthd.ReturnType,
Types,
CallingConvention.Winapi,
(Mthd as SNativeFunctionNode).CharSet);
methodBuilder.SetImplementationFlags(MethodImplAttributes($80 or 0));
end
else raise new Exception('ôèãíÿ');
Mthd.MthdBuilder := methodBuilder;
if Mthd is SNativeFunctionNode then
GenMethod((Mthd as SNativeFunctionNode), False)
else if Mthd is SNetFunctionNode then
GenMethod((Mthd as SNetFunctionNode), EntryPoint)
else raise new Exception('ôèãíÿ');
end;
end;
procedure GCodeGenerator.GenMethod(Mthd: SNetFunctionNode; EntryPoint: boolean);
begin
var ArgsTypes2 := ArgsTypes;
var ArgsSymbolTable2 := ArgsSymbolTable;
var SymbolTable2 := SymbolTable;
var LabelSymbolTable2 := LabelSymbolTable;
var ContinueLabels2 := ContinueLabels;
var BreakLabels2 := BreakLabels;
var GenExitMark2 := GenExitMark;
var GenResult2 := GenResult;
var il2 := il;
ArgsTypes := new Dictionary<integer, &Type>;
ArgsSymbolTable := new Dictionary<string, integer>;
SymbolTable := new Dictionary<string, Emit.LocalBuilder>;
LabelSymbolTable := new Dictionary<string, Emit.Label>;
ContinueLabels := new Stack<&Label>;
BreakLabels := new Stack<&Label>;
GenExitMark := false;
GenResult := false;
for var i := 0 to Mthd.ParametersType.Count - 1 do
begin
ArgsSymbolTable[Mthd.ParametersType[i].Name] := i;
ArgsTypes[i] := Mthd.ParametersType[i]._Type;
end;
methodBuilder := Mthd.MthdBuilder;
if EntryPoint then asmb.SetEntryPoint(methodBuilder, PEFileKind);
il := methodBuilder.GetILGenerator;
GenStmt(mthd.Body, false);
if (Mthd.ReturnType <> typeof(void)) and GenResult then
il.Emit(Emit.OpCodes.Ldloc, ResultVar)
else
il.Emit(Emit.OpCodes.Nop);
if GenExitMark then il.MarkLabel(ExitLabel);
il.Emit(Emit.OpCodes.Ret);
ArgsTypes := ArgsTypes2;
ArgsSymbolTable := ArgsSymbolTable2;
SymbolTable := SymbolTable2;
LabelSymbolTable := LabelSymbolTable2;
ContinueLabels := ContinueLabels2;
BreakLabels := BreakLabels2;
GenExitMark := GenExitMark2;
GenResult := GenResult2;
il := il2;
end;
procedure GCodeGenerator.GenMethod(Mthd: SNativeFunctionNode; EntryPoint: boolean);
begin
// åãî ãåíåðèðîâàòü íå íàäî, îí óæå áûë îáüÿâëåí - ýòîãî äîñòàòî÷íî
end;
procedure GCodeGenerator.GenFunction(CFunctionNode: SCallFunctionNode);
begin
if (CFunctionNode is SCallOtherFunctionNode) then
begin
var CallFunctionNode := SCallOtherFunctionNode(CFunctionNode);
for var i := 0 to CallFunctionNode.PassedParameters.Count - 1 do
begin
GenExpr(CallFunctionNode.PassedParameters[i], CallFunctionNode.PassedParameters[i]._Type);
{ if CallFunctionNode.PassedParameters[i]._Type.IsValueType then
il.Emit(Emit.OpCodes.Box, CallFunctionNode.PassedParameters[i]._Type);}
end;
if not CallFunctionNode.MthdInfo.IsVirtual then
il.Emit(Emit.OpCodes.Call, CallFunctionNode.MthdInfo)
else
il.Emit(Emit.OpCodes.CallVirt, CallFunctionNode.MthdInfo);
end
else if (CFunctionNode is SCallOwnFunctionNode) then
begin
var SelfFuncStmt := SCallOwnFunctionNode(CFunctionNode);
GenMethod(SelfFuncStmt._Function, false);
for var i := 0 to SelfFuncStmt.PassedParameters.Count - 1 do
begin
GenExpr(SelfFuncStmt.PassedParameters[i], SelfFuncStmt.PassedParameters[i]._Type);
{if SelfFuncStmt.PassedParameters[i]._Type.IsValueType then
il.Emit(Emit.OpCodes.Box, SelfFuncStmt.PassedParameters[i]._Type);}
end;
var mthdinfo := SelfFuncStmt._Function.MthdBuilder.GetBaseDefinition;
if not mthdinfo.IsVirtual then
il.Emit(Emit.OpCodes.Call, MthdInfo)
else
il.Emit(Emit.OpCodes.CallVirt, MthdInfo);
end
end;
procedure GCodeGenerator.GenStmt(Stmt: SStmtNode; AsExpr: boolean);
begin
//if Stmt = nil then exit;
if Options.Debug then MarkSequencePoint(il, Stmt.FileName , Stmt.BeginLine, Stmt.BeginColumn, Stmt.EndLine, Stmt.EndColumn);
if (Stmt is SStmtListNode) then
begin
for var i := 0 to (Stmt as SStmtListNode).StmtList.Count - 1 do
GenStmt((Stmt as SStmtListNode).StmtList[i], false);
end
else if (Stmt is SArrayElemNode) then
begin
var ArrayElemNode := SArrayElemNode(Stmt);
GenExpr(ArrayElemNode.Arr, ArrayElemNode._Type.MakeArrayType);
foreach Expr: SExprNode in ArrayElemNode.Index do
GenExpr(Expr, typeof(integer));
il.Emit(Emit.OpCodes.Ldelem, ArrayElemNode._Type);
end
else if (Stmt is SDotAssignNode) then
begin
var DotAssign := (stmt as SDotAssignNode);
GenExpr(DotAssign.DotNode.FirstStmt, DotAssign.DotNode.FirstStmt._Type);
GenExpr(DotAssign.Expr, DotAssign.Expr._Type);
GenStmt(DotAssign.DotNode.SecondStmt, AsExpr);
end
else if (Stmt is SPropertyAssignNode) then
begin
var PropertyAssign := (stmt as SPropertyAssignNode);
GenExpr(PropertyAssign.Expr, PropertyAssign.Prop._Type);
GenStmt(PropertyAssign.Prop, AsExpr);
end
else if (Stmt is SVarAssignNode) then
begin
var Assign := (stmt as SVarAssignNode);
GenExpr(assign.Expr, Assign.Expr._Type);
Store(assign.Variable.Name, Assign.Expr._Type);
end
else if (Stmt is SArrayAssignNode) then
begin
var ArrayAssign := (stmt as SArrayAssignNode);
GenExpr(ArrayAssign.Arr.Arr, ArrayAssign.Arr._Type.MakeArrayType);
foreach Expr: SExprNode in ArrayAssign.Arr.Index do
GenExpr(Expr, typeof(integer));
GenExpr(ArrayAssign.Expr, ArrayAssign.Expr._Type);
il.Emit(Emit.OpCodes.Stelem, ArrayAssign.Arr._Type);
end
else if (Stmt is SDeclareNode) then
begin
var DeclareNode := (stmt as SDeclareNode);
SymbolTable.Item[DeclareNode.Name] := il.DeclareLocal(DeclareNode._Type);
end
else if (Stmt is SDeclareAndAssignNode) then
begin
var DeclareAndAssignNode := (stmt as SDeclareAndAssignNode);
var Declare := new SDeclareNode;
Declare.Name := DeclareAndAssignNode.Name;
Declare._Type := DeclareAndAssignNode._Type;
GenStmt(Declare, false);
var Assign := new SVarAssignNode;
Assign.Variable := DeclareAndAssignNode;
Assign.Expr := DeclareAndAssignNode.Expr;
GenStmt(Assign, false);
end
else if (Stmt is SVarListNode) then
begin
for var i := 0 to (Stmt as SVarListNode).VarList.Count - 1 do
GenStmt((Stmt as SVarListNode).VarList[i], false);
end
else if (Stmt is SPropertyNode) then
begin
var PropertyNode := SPropertyNode(Stmt);
foreach Expr: SExprNode in PropertyNode.PassedParameters do
GenExpr(Expr, Expr._Type);
var MthdInfo: MethodInfo;
if PropertyNode.NeedRead then
MthdInfo := PropertyNode.Prop.GetGetMethod
else
MthdInfo := PropertyNode.Prop.GetSetMethod;
if not mthdinfo.IsVirtual then
il.Emit(Emit.OpCodes.Call, MthdInfo)
else
il.Emit(Emit.OpCodes.CallVirt, MthdInfo);
end
else if (Stmt is SDotNode) then
begin
var DotNode := SDotNode(Stmt);
GenExpr(DotNode.FirstStmt, DotNode.FirstStmt._Type);
{if DotNode.FirstStmt._Type.IsValueType then
il.Emit(Emit.OpCodes.Box, DotNode.FirstStmt._Type);}
GenStmt(DotNode.SecondStmt, AsExpr);
end
else if (Stmt is SCallFunctionNode) then
begin
GenFunction(Stmt as SCallFunctionNode);
if ((Stmt as SCallFunctionNode)._Type <> typeof(void)) and not AsExpr then
il.Emit(OpCodes.Pop);
end
else if (Stmt is SExitNode) then
begin
if not GenExitMark then
ExitLabel := il.DefineLabel;
il.Emit(OpCodes.Br, ExitLabel);
GenExitMark := True;
end
else if (Stmt is SBreakNode) then
begin
if BreakLabels.Count > 0 then
il.Emit(OpCodes.Br, BreakLabels.Peek)
else
raise new Exception('Íåëüçÿ âñòàâëÿòü ïðîöåäóðó break âíå òåëà öèêëà');
end
else if (Stmt is SContinueNode) then
begin
if ContinueLabels.Count > 0 then
il.Emit(OpCodes.Br, ContinueLabels.Peek)
else
raise new Exception('Íåëüçÿ âñòàâëÿòü ïðîöåäóðó continue âíå òåëà öèêëà');
end
else if (Stmt is SReturnNode) then
begin
if (MethodBuilder.ReturnType <> typeof(void)) then
begin
var Return := (stmt as SReturnNode);
GenExpr(Return.Expr, MethodBuilder.ReturnType);
il.Emit(OpCodes.Ret);
end
else raise new Exception('Íåëüçÿ èñïîëüçîâàòü ôóíêöèþ return â ïðîöåäóðå');
end
else if (Stmt is SPrintNode) then
begin
var Print := (stmt as SPrintNode);
GenExpr(Print.Expr, typeof(object));
il.Emit(Emit.OpCodes.Call, typeof(System.Console).GetMethod('WriteLine', new System.&Type[1] (Print.Expr._Type)));
end
else if (Stmt is SRaiseNode) then
begin
var &Raise := (stmt as SRaiseNode);
GenExpr(&Raise.Expr, typeof(System.Exception));
il.Emit(Emit.OpCodes.Throw);
end
else if (Stmt is SIfNode) then
begin
var ifElseStmt := SIfNode(Stmt);
if (ifElseStmt.ElseBody <> nil) then
begin
var ElseLabel := il.DefineLabel;
var EndLabel := il.DefineLabel;
GenExpr(ifElseStmt.Condition, typeof(boolean));
il.Emit(OpCodes.Brfalse, ElseLabel);
GenStmt(ifElseStmt.ThenBody, false);
il.Emit(OpCodes.Br, EndLabel);
il.MarkLabel(ElseLabel);
GenStmt(ifElseStmt.ElseBody, false);
il.MarkLabel(EndLabel)
end
else
begin
GenExpr(ifElseStmt.Condition, typeof(boolean));
var EndLabel := il.DefineLabel;
il.Emit(OpCodes.Brfalse, EndLabel);
GenStmt(ifElseStmt.ThenBody, false);
il.MarkLabel(EndLabel)
end
end
else if (Stmt is SLabelDefNode) then
begin
var name := (stmt as SLabelDefNode).LabelNode.Name;
if not LabelSymbolTable.ContainsKey(name) then
LabelSymbolTable[name] := il.DefineLabel;
il.MarkLabel(LabelSymbolTable[name]);
if (stmt as SLabelDefNode).Body <> nil then
GenStmt((stmt as SLabelDefNode).Body, false);
end
else if (Stmt is SGotoNode) then
begin
var name := (stmt as SGotoNode).LabelNode.Name;
if not LabelSymbolTable.ContainsKey(name) then
LabelSymbolTable[name] := il.DefineLabel;
il.Emit(Emit.OpCodes.Br, LabelSymbolTable[name]);
end
else if (Stmt is SForNode) then
begin
var forLoop := (stmt as SForNode);
var LabelCheckExpr := il.DefineLabel;
var LabelBody := il.DefineLabel;
var ContinueLabel := il.DefineLabel;
var BreakLabel := il.DefineLabel;
BreakLabels.Push(BreakLabel);
ContinueLabels.Push(ContinueLabel);
GenStmt(forLoop.InitStmt, false);
il.Emit(OpCodes.Br, LabelCheckExpr);
il.MarkLabel(LabelBody);
GenStmt(forLoop.Body, false);
il.MarkLabel(ContinueLabel);
GenStmt(forLoop.IncStmt, false);
il.MarkLabel(LabelCheckExpr);
GenExpr(forLoop.WhileExpr, typeof(boolean));
il.Emit(OpCodes.Brtrue, LabelBody);
il.MarkLabel(BreakLabel);
BreakLabels.Pop;
ContinueLabels.Pop;
end
else if (Stmt is SRepeatNode) then
begin
var repeatUntilLoop := SRepeatNode(stmt);
var StartLabel := il.DefineLabel;
var BreakLabel := il.DefineLabel;
BreakLabels.Push(BreakLabel);
ContinueLabels.Push(StartLabel);
il.MarkLabel(StartLabel);
GenStmt(repeatUntilLoop.Body, false);
GenExpr(repeatUntilLoop.Condition, typeof(boolean));
il.Emit(OpCodes.Brfalse, StartLabel);
il.MarkLabel(BreakLabel);
end
else if (Stmt is SWhileNode) then
begin
var whileLoop := SWhileNode(stmt);
var StartLabel := il.DefineLabel;
var EndLabel := il.DefineLabel;
BreakLabels.Push(EndLabel);
ContinueLabels.Push(StartLabel);
il.MarkLabel(StartLabel);
GenExpr(whileLoop.Condition, typeof(boolean));
il.Emit(OpCodes.Brfalse, EndLabel);
GenStmt(whileLoop.Body, false);
il.Emit(OpCodes.Br, StartLabel);
il.MarkLabel(EndLabel)
end
else if (Stmt is STryNode) then
begin
var TryNode := STryNode(stmt);
il.BeginExceptionBlock;
GenStmt(TryNode.TryStatements, false);
if TryNode.ExceptionFilters <> nil then
begin
foreach a: SExceptionFilterNode in TryNode.ExceptionFilters do
begin
il.BeginCatchBlock(a.ExceptionType);
SymbolTable.Item[a.ExceptionVar.Name] := il.DeclareLocal(a.ExceptionVar._Type);
Store(a.ExceptionVar.Name, a.ExceptionVar._Type);
GenStmt(a.Body, false);
end;
end;
if TryNode.FinallyStatements <> nil then
begin
il.BeginFinallyBlock;
GenStmt(TryNode.FinallyStatements, false);
end;
il.EndExceptionBlock;
end
else
raise new System.Exception('Íå çíàþ êàê ñãåíåðèðîâàòü ' + stmt.GetType.ToString);
end;
procedure GCodeGenerator.GenExpr(Expr: SExprNode; params ExpectedTypes: array of &Type);
type arroftype = array of &Type;
begin
//var deliveredType := TypeOfExpr(expr);
var ExpectedTypes_NoBUG: arroftype := ExpectedTypes.Clone as arroftype;
if (Expr is SStringLiteral) then
begin
il.Emit(Emit.OpCodes.Ldstr, SStringLiteral(expr).Value);
end
else if (Expr is SCostantNullNode) then
begin
il.Emit(Emit.OpCodes.Ldnull);
end
else if (Expr is SIntegerLiteral) then
begin
il.Emit(Emit.OpCodes.Ldc_I4, SIntegerLiteral(expr).Value);
end
else if (Expr is SRealLiteral) then
begin
il.Emit(Emit.OpCodes.Ldc_R8, SRealLiteral(expr).Value);
end
else if (Expr is SCharLiteral) then
begin
il.Emit(Emit.OpCodes.Ldc_I4, SCharLiteral(expr).Value);
end
else if (Expr is SBooleanLiteral) then
begin
if SBooleanLiteral(expr).Value then
il.Emit(Emit.OpCodes.Ldc_I4, 1) else il.Emit(Emit.OpCodes.Ldc_I4, 0);
end
else if (Expr is SVariableNode) then
begin
var ident := SVariableNode(expr).Name;
if SymbolTable.ContainsKey(ident) then
if not expr.IsDotExpr then
il.Emit(Emit.OpCodes.Ldloc, SymbolTable[ident])
else il.Emit(Emit.OpCodes.Ldloca, SymbolTable[ident])
else if ArgsSymbolTable.ContainsKey(ident) then
if not expr.IsDotExpr then
il.Emit(Emit.OpCodes.Ldarg, ArgsSymbolTable[ident])
else il.Emit(Emit.OpCodes.Ldarga, ArgsSymbolTable[ident])
else if GlobalSymbolTable.ContainsKey(ident) then
{if not expr.IsDotExpr then
il.Emit(Emit.OpCodes.Ldsfld, GlobalSymbolTable[ident])
else }
il.Emit(Emit.OpCodes.Ldsfld, GlobalSymbolTable[ident])
else if (ident.ToLower = 'result') and GenResult then
if not expr.IsDotExpr then
il.Emit(Emit.OpCodes.Ldloc, ResultVar)
else il.Emit(Emit.OpCodes.Ldloca, ResultVar)
else
raise new System.Exception('íåîáúÿâëåííàÿ ïåðåìåííàÿ "' + ident + '"');
end
else if (Expr is SDotNode) then
begin
GenStmt((Expr as SDotNode), true);
end
else if (Expr is SPropertyNode) then
begin
GenStmt((Expr as SPropertyNode), true);
end
else if (Expr is SCallFunctionNode) then
begin
GenFunction(Expr as SCallFunctionNode);
end
else if (Expr is SArrayElemNode) then
begin
GenStmt((Expr as SArrayElemNode), true);
end
else if (Expr is SNewArrayNode) then
begin
// il.Emit(OpCodes.Nop);
var NewArray := SNewArrayNode(Expr);
if NewArray.Length <> nil then GenExpr(NewArray.Length, typeof(integer))
else il.Emit(OpCodes.Ldc_I4, 0);
il.Emit(OpCodes.Newarr, NewArray.OfType);
end
else if (Expr is STypeOfNode) then
begin
var TypeOfNode := STypeOfNode(Expr);
il.Emit(Emit.OpCodes.Ldtoken, TypeOfNode.TypeExpr);
il.Emit(Emit.OpCodes.Call, typeof(System.&Type).GetMethod('GetTypeFromHandle', new System.&Type[1] (typeof(System.RuntimeTypeHandle))));
end
else if (Expr is SSizeOfNode) then
begin
var SizeOfNode := SSizeOfNode(Expr);
il.Emit(Emit.OpCodes.Ldloc, il.DeclareLocal(SizeOfNode.TypeExpr));
il.Emit(Emit.OpCodes.Box, SizeOfNode.TypeExpr);
il.Emit(Emit.OpCodes.Call, typeof(System.Runtime.InteropServices.Marshal).GetMethod('SizeOf', new System.&Type[1] (typeof(object))));
end
else if (Expr is SNewObjNode) then
begin
var NewObj := SNewObjNode(Expr);
for var i := 0 to NewObj.PassedParameters.Count - 1 do
GenExpr(NewObj.PassedParameters[i], NewObj.PassedParameters[i]._Type);
il.Emit(OpCodes.Newobj, NewObj.ConstInfo);
end
else if (Expr is SActionNode) then
begin
GenActionExprNode((Expr as SActionNode), ExpectedTypes_NoBUG)
end
else raise new Exception('íå çíàþ êàê ñãåíåðèðîâàòü ñëåä. âûðàæåíèå òèïà ' + Expr.GetType.ToString);
foreach t: &Type in ExpectedTypes_NoBUG do
if Expr._Type.IsSubclassOf(t) then exit;
if &Array.IndexOf(ExpectedTypes_NoBUG, Expr._Type) = -1 then
if (&Array.IndexOf(ExpectedTypes_NoBUG, typeof(string)) <> -1) and (Expr._Type <> typeof(string)) then
begin
{il.Emit(Emit.OpCodes.Box, Expr._Type);
il.Emit(Emit.OpCodes.Callvirt, typeof(object).GetMethod('ToString'));}
end
else
begin
if (&Array.IndexOf(ExpectedTypes_NoBUG, typeof(real)) <> -1) and (Expr._Type = typeof(integer)) then
il.Emit(Emit.OpCodes.Conv_R8)
else raise new System.Exception('Íåëüçÿ ïðåîáðàçîâàòü òèï ' + Expr._Type.Name + ' ê ' + ArrayToStr(ExpectedTypes_NoBUG, ','));
end;
end;
procedure GCodeGenerator.GenActionExprNode(Expr: SActionNode; params ExpectedTypes: array of &Type);
begin
if (Expr is SBinaryOperation) then
begin
if Expr.Operands.Count = 2 then
begin
GenExpr(expr.Operands[0], Expr._Type);
GenExpr(expr.Operands[1], Expr._Type);
if (Expr._Type = typeof(integer)) or (Expr._Type = typeof(real)) then
begin
if (Expr is SAddBinaryOperation) then
case (Expr as SAddBinaryOperation).Action of
Addition: il.Emit(Emit.OpCodes.Add);
Subtraction: il.Emit(Emit.OpCodes.Sub);
end
else if (Expr is SMulBinaryOperation) then
case (Expr as SMulBinaryOperation).Action of
Multiplication: il.Emit(Emit.OpCodes.Mul);
Division: il.Emit(Emit.OpCodes.Div);
end;
end
else if (Expr._Type = typeof(string)) then
begin
il.Emit(Emit.OpCodes.Call, typeof(string).GetMethod('Concat', new System.&Type[2](typeof(string), typeof(string))));
end else raise new CodeGenException(string.Format('Áèíàðíàÿ îïåðàöèÿ {0} íå ïðåìèíèìà ê òèïó {1}}', Expr.ToString, Expr._Type.ToString), Expr)
end
else if (Expr.Operands.Count = 1) and (Expr is SAddBinaryOperation) and ((Expr as SAddBinaryOperation).Action = Subtraction) then
begin
GenExpr(expr.Operands[0], Expr._Type);
il.Emit(Emit.OpCodes.Neg);
end
else raise new System.Exception('Áèíàðíûå îïåðàöèè äîëæíû èìåòü 2 îïåðàíäà');
end
else if (Expr is SIntegerOperation) then
begin
if Expr.Operands.Count = 2 then
begin
GenExpr(expr.Operands[0], typeof(integer));
GenExpr(expr.Operands[1], typeof(integer));
case (Expr as SIntegerOperation).Action of
DivisionInteger: il.Emit(Emit.OpCodes.Div);
ModInteger: il.Emit(Emit.OpCodes.Rem);
end;
end else raise new System.Exception('Öåëî÷èñëåííûå îïåðàöèè äîëæíû èìåòü 2 îïåðàíäà');
end
else if (Expr is SCompareOperation) then
begin
if Expr.Operands.Count = 2 then
begin
GenExpr(expr.Operands[0], expr.Operands[0]._Type);
GenExpr(expr.Operands[1], expr.Operands[1]._Type);
case (Expr as SCompareOperation).Action of
Equal:
begin
il.Emit(Emit.OpCodes.Ceq);
end;
NoEqual:
begin
il.Emit(Emit.OpCodes.Ceq);
il.Emit(Emit.OpCodes.Ldc_I4, 0);
il.Emit(Emit.OpCodes.Ceq);
end;
GreaterThan:
begin
il.Emit(Emit.OpCodes.Cgt);
end;
GreaterThanOrEqual:
begin
il.Emit(Emit.OpCodes.Clt);
il.Emit(Emit.OpCodes.Ldc_I4, 0);
il.Emit(Emit.OpCodes.Ceq);
end;
LessThan:
begin
il.Emit(Emit.OpCodes.Clt);
end;
LessThanOrEqual:
begin
il.Emit(Emit.OpCodes.Cgt);
il.Emit(Emit.OpCodes.Ldc_I4, 0);
il.Emit(Emit.OpCodes.Ceq);
end;
end;
end else raise new System.Exception('Îïåðàöèè ñðàâíåíèÿ äîëæíû èìåòü 2 îïåðàíäà');
end
else if (Expr is SLogicalOperation) then
begin
if (Expr as SLogicalOperation).Action = logic_not then
begin
if Expr.Operands.Count = 1 then