-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeGenerator.cs
More file actions
921 lines (855 loc) · 26.9 KB
/
CodeGenerator.cs
File metadata and controls
921 lines (855 loc) · 26.9 KB
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
using System;
using System.Collections.Generic;
using System.IO;
namespace Compiler
{
class CodeGenerator
{
private AST_Node _tree;
private Block _currentBlock;
private string _functionDefinitions = "";
private List<string> _externFunctions = new List<string> { "_printf", "_malloc", "_free" };
private int _labelCounter = 0;
List<DataSectionVar> _varsToDeclare = new List<DataSectionVar>
{
new DataSectionVar("__temp", DataSize.DWORD, "0"),
DataSectionVar.StringConstant("format", "%?", true),
DataSectionVar.StringConstant("hex_str", "0x", false),
DataSectionVar.StringConstant("formatin", "%?", false),
DataSectionVar.StringConstant("true_string", "true", false),
DataSectionVar.StringConstant("false_string", "false", false),
};
private HashSet<string> _helperFunctionsUsed = new HashSet<string>();
private HashSet<string> _macrosUsed = new HashSet<string>();
// Constructor
// program: program's source code as string
public CodeGenerator(AST_Node tree)
{
_tree = tree;
}
// Method generates assembly program from input program
// input: none
// return: none
public string GenerateAssembly()
{
string programAssembly = ToAssembly(_tree);
string data = DataSectionAssembly();
return
"global _main\n" +
ExternFunctions() +
"\n" +
MacrosAssembly() +
"\n" +
"section .data\n" +
Indent(data) +
"\n" +
"section .text\n" +
"_main:\n" +
Indent(
"push ebp\n" +
"mov ebp, esp\n\n" +
programAssembly +
"\n" +
"mov esp, ebp\n" +
"pop ebp\n" +
"mov eax, 0\n" +
"ret"
) + "\n\n" +
_functionDefinitions +
HelperFunctionsAssembly();
}
// Methods generate assembly code from subtrees
// tree: subtree to turn to assembly code
// return: assembly as string
private string ToAssembly(AST_Node tree)
{
switch (tree)
{
// --- Expressions
case BinaryOperator op when op.Operator == TokenCode.ASSIGN_OP:
return AssignmentAssembly(op);
case BinaryOperator op:
return ToAssembly(op);
case UnaryOperator op:
return ToAssembly(op);
case TernaryOperator op:
return ToAssembly(op);
case IPrimitive p:
return ToAssembly(p);
case Variable v:
return ToAssembly(v);
case Cast c:
return ToAssembly(c);
case FunctionCall call:
return ToAssembly(call);
case ArrayIndex arrayIndex:
return ToAssembly(arrayIndex);
// --- Statements
case ForLoop stmt:
return ToAssembly(stmt);
case Block block:
return ToAssembly(block);
case ExpressionStatement stmt:
return ToAssembly(stmt.GetExpression());
case PrintStatement stmt:
return ToAssembly(stmt);
case ReturnStatement stmt:
return ToAssembly(stmt);
case VariableDeclaration decl:
return ToAssembly(decl);
case FunctionDeclaration decl:
string toAdd = ToAssembly(decl);
_functionDefinitions += toAdd;
return "";
case ExternStatement stmt:
_externFunctions.Add(stmt.Identifier);
return "";
case IfStatement stmt:
return ToAssembly(stmt);
case WhileLoop stmt:
return ToAssembly(stmt);
case SwitchCase stmt:
return ToAssembly(stmt);
case NewExpression expr:
return ToAssembly(expr);
case DeleteStatement stmt:
return ToAssembly(stmt);
case LocalArray expr:
return ToAssembly(expr);
default:
return "";
}
}
// Methods generate assembly code for a block of statements
// tree: Block to turn into ASM
// return: assembly as string
private string ToAssembly(Block block)
{
// change current block
Block prevBlock = _currentBlock;
_currentBlock = block;
string result = "";
// allocate memory for local variables
int stackOffset = block.SymbolTable.VariableBytes();
if (stackOffset != 0)
result += "sub esp, " + stackOffset + "\n";
// add assembly code for all statements
foreach (Statement stmt in block.Children)
{
result += ToAssembly(stmt);
}
// deallocate memory from the stack
if (stackOffset != 0)
result += "add esp, " + stackOffset + "\n";
// return to previous block and return
_currentBlock = prevBlock;
return result;
}
// binary operator assembly rules:
// result ax
// operands ax, bx
public static string DEFAULT_OPERATOR_BINARY = "Invalid binary operation passed semantic analysis";
public static string DEFAULT_TYPE_BINARY = "Binary operator has no type (or invalid type) after semantic analysis";
private string ToAssembly(BinaryOperator op)
{
string operandsASM = "";
if (op.Operand(0).Type != TypeCode.BOOL) // bool logical operators use operands differently (short-circuit)
{
// get operand2 on stack
operandsASM += ToAssembly(op.Operand(1));
operandsASM += "push eax\n";
// get operand1 in eax
operandsASM += ToAssembly(op.Operand(0));
// pop operand2 to ebx
operandsASM += "pop ebx\n";
}
// check ptr operations
if (op.Operand(0).Type.Pointer != 0)
return operandsASM + PointerOperatorAssembly(op.Operand(0), "eax", op.Operand(1), "ebx", op);
else if (op.Operand(1).Type.Pointer != 0)
return operandsASM + PointerOperatorAssembly(op.Operand(1), "ebx", op.Operand(0), "eax", op);
// calculate based on input type
switch (op.Operand(0).Type)
{
case ValueType t when t == new ValueType(TypeCode.INT):
return operandsASM +
op.Operator switch
{
// --- Arithmetic
TokenCode.ADD_OP => "add eax, ebx\n",
TokenCode.SUB_OP => "sub eax, ebx\n",
TokenCode.MUL_OP => "mul ebx\n",
TokenCode.DIV_OP => "xor edx, edx\ndiv ebx\n",
TokenCode.MOD_OP => "xor edx, edx\ndiv ebx\n" +
"mov eax, edx\n",
// --- Bitwise
TokenCode.BIT_OR_OP => "or eax, ebx\n",
TokenCode.BIT_XOR_OP => "xor eax, ebx\n",
TokenCode.BIT_AND_OP => "and eax, ebx\n",
TokenCode.LEFT_SHIFT => "mov cl, bl\n" +
"shl eax, cl\n",
TokenCode.RIGHT_SHIFT => "mov cl, bl\n" +
"shr eax, cl\n",
// --- Relational
TokenCode.LESS_OP => "cmp eax, ebx\nmov eax, 0\nsetl al\n",
TokenCode.LESS_EQUAL_OP => "cmp eax, ebx\nmov eax, 0\nsetle al\n",
TokenCode.GREATER_OP => "cmp eax, ebx\nmov eax, 0\nsetg al\n",
TokenCode.GREATER_EQUAL_OP => "cmp eax, ebx\nmov eax, 0\nsetge al\n",
TokenCode.EQUAL_OP => "cmp eax, ebx\nmov eax, 0\nsete al\n",
TokenCode.NOT_EQUAL_OP => "cmp eax, ebx\nmov eax, 0\nsetne al\n",
_ => throw new ImplementationError(DEFAULT_OPERATOR_BINARY)
};
case ValueType t when t == new ValueType(TypeCode.FLOAT):
return operandsASM +
// load eax and ebx to fpu
"mov [__temp], eax\n" +
"fld dword [__temp]\n" +
"mov [__temp], ebx\n" +
"fld dword [__temp]\n" +
// calculate operation
op.Operator switch
{
// --- Arithmetic
TokenCode.ADD_OP => "faddp\n",
TokenCode.SUB_OP => "fsubp\n",
TokenCode.MUL_OP => "fmulp\n",
TokenCode.DIV_OP => "fdivp\n",
TokenCode.POW_OP => HelperCall("pow"),
// --- Relational
TokenCode.LESS_OP => Macro("float_comparison", "0000000000000000b"), // not condition flags
TokenCode.LESS_EQUAL_OP => Macro("float_comparison_inverse", "0000000100000000b"), // not greater,
TokenCode.GREATER_OP => Macro("float_comparison", "0000000100000000b"), // carry flag
TokenCode.GREATER_EQUAL_OP => Macro("float_comparison_inverse", "0000000000000000b"), // not less
TokenCode.EQUAL_OP => Macro("float_comparison", "0100000000000000b"), // zero flag
TokenCode.NOT_EQUAL_OP => Macro("float_comparison_inverse", "0100000000000000b"), // not equal
_ => throw new ImplementationError(DEFAULT_OPERATOR_BINARY)
} +
// mov result from fpu to eax if type is float
(op.Type == TypeCode.FLOAT ?
"fstp dword [__temp]\n" +
"mov eax, [__temp]\n" : "");
case ValueType t when t == new ValueType(TypeCode.BOOL):
string label = GetLabel();
return operandsASM +
op.Operator switch
{
TokenCode.LOGIC_AND_OP => ToAssembly(op.Operand(0)) +
"cmp eax, 0\n" +
"je " + label + "\n" +
ToAssembly(op.Operand(1)) +
label + ":\n",
TokenCode.LOGIC_OR_OP => ToAssembly(op.Operand(0)) +
"cmp eax, 1\n" +
"je " + label + "\n" +
ToAssembly(op.Operand(1)) +
label + ":\n",
_ => throw new ImplementationError(DEFAULT_OPERATOR_BINARY)
};
default:
throw new ImplementationError(DEFAULT_TYPE_BINARY);
}
}
public string PointerOperatorAssembly(Expression ptr, string ptrReg, Expression other, string otherReg, BinaryOperator op)
{
switch (op.Operator)
{
case TokenCode.ADD_OP:
// add integer
return "lea eax, [" + ptrReg + " + " + other.Type.Size() + " * " + otherReg + "]\n";
case TokenCode.SUB_OP:
if(other.Type.Pointer != 0)
{
// pointer subtraction
return "sub " + ptrReg + ", " + otherReg + "\n" +
(ptrReg == "eax" ? "" : "mov " + ptrReg + ", eax\n") +
"xor edx, edx\n" +
"mov ebx, " + other.Type.Size() + "\n" +
"div ebx\n";
}
else
{
// sub integer
return "neg " + otherReg + "\n" +
"lea eax, [" + ptrReg + " + " + other.Type.Size() + " * " + otherReg + "]\n";
}
default:
return "";
}
}
// generates assembly for assignment
// rules: value to assign at eax, moves into memory
private string AssignmentAssembly(BinaryOperator op)
{
switch (op.Operand(0))
{
case Variable v:
Tuple<string, string> addressASM = VariableAddress(v);
return ToAssembly(op.Operand(1)) +
addressASM.Item1 +
"mov [" + addressASM.Item2 + "], eax\n";
case UnaryOperator valueAt when valueAt.Operator == TokenCode.MUL_OP:
return ToAssembly(valueAt.Operand()) + // address
"push eax\n" +
ToAssembly(op.Operand(1)) + // new value at eax
"pop ebx\n" + // address at ebx
"mov [ebx], eax\n";
case ArrayIndex arrayIndex:
return ToAssembly(arrayIndex.Index()) +
"push eax\n" + // index in stack
ToAssembly(arrayIndex.Array()) +
"push eax\n" + // base in stack
ToAssembly(op.Operand(1)) + // new value in eax
"pop ebx\n" + // base in ebx
"pop ecx\n" + // index in ecx
"mov [ebx + " + arrayIndex.Type.Size() + " * ecx], " + RegisterName('a', arrayIndex.Type.Size()) + "\n";
default:
throw new ImplementationError("Invalid assignment passed semantic analysis");
}
}
// returns register name based on size
// input: base register letter (a, b, c, d), size in bytes
// return: register name
private string RegisterName(char letter, int size)
{
return size switch
{
1 => letter + "l",
2 => letter + "x",
4 => "e" + letter + "x",
_ => ""
};
}
// unary operator assembly rules:
// operand -> result: ax -> ax
public static string DEFAULT_OPERATOR_UNARY = "Invalid unary operator passed semantic analysis";
public static string DEFAULT_TYPE_UNARY = "Unary operator has no type (or invalid type) after semantic analysis";
private string ToAssembly(UnaryOperator op)
{
string operandASM = ToAssembly(op.Operand());
// calculate result of op
switch (op.Type)
{
case ValueType t when op.Operator == TokenCode.BIT_AND_OP:
Tuple<string, string> addressASM = VariableAddress(op.Operand() as Variable);
return addressASM.Item1 +
"lea eax, [" + addressASM.Item2 + "]\n";
case ValueType t when op.Operator == TokenCode.MUL_OP:
return operandASM +
"mov eax, [eax]\n";
case ValueType t when t == new ValueType(TypeCode.INT):
return operandASM +
(op.Operator, op.Prefix) switch
{
(TokenCode.BIT_NOT_OP, true) => "not eax\n",
(TokenCode.SUB_OP, true) => "neg eax\n", // negation
(TokenCode.EXCLAMATION_MARK, false) => HelperCall("factorial"),
_ => throw new ImplementationError(DEFAULT_OPERATOR_UNARY)
};
case ValueType t when t == new ValueType(TypeCode.FLOAT):
return operandASM +
// load operand to fpu
"mov [__temp], eax\n" +
"fld dword [__temp]\n" +
(op.Operator, op.Prefix) switch
{
(TokenCode.SUB_OP, true) => "fchs\n", // negation
_ => throw new ImplementationError(DEFAULT_OPERATOR_UNARY)
} +
// move result back into eax
"fstp dword [__temp]\n" +
"mov eax, [__temp]\n";
case ValueType t when t == new ValueType(TypeCode.BOOL):
return operandASM +
(op.Operator, op.Prefix) switch
{
(TokenCode.EXCLAMATION_MARK, true) => "cmp eax, 0\n" +
"mov eax, 0\n" +
"sete al\n", // logical not
_ => throw new ImplementationError(DEFAULT_OPERATOR_UNARY)
};
default:
throw new ImplementationError(DEFAULT_TYPE_UNARY);
}
}
// ternary operator assembly:
// condition operand: eax
// operands: ebx, ecx
// result: eax
private string ToAssembly(TernaryOperator op)
{
string operandsASM = "";
// get operand3 on stack
operandsASM += ToAssembly(op.Operand(2));
operandsASM += "push eax\n";
// get operand2 on the stack
operandsASM += ToAssembly(op.Operand(1));
operandsASM += "push eax\n";
// put all operands in place
operandsASM += ToAssembly(op.Operand(0));
operandsASM += "pop ebx\npop ecx\n";
// add code for ternary op
return operandsASM +
"cmp eax, 0\n" +
"cmove eax, ecx\n" +
"cmovne eax, ebx\n";
}
// primitive
// place at eax
private string ToAssembly(IPrimitive primitive)
{
switch (primitive)
{
case Primitive<int> p:
return "mov eax, " + p.Value + "\n";
case Primitive<float> p:
DataSectionVar floatConst = DataSectionVar.FloatConstant(p.Value);
_varsToDeclare.Add(floatConst);
return "mov eax, [" + floatConst.Name + "]\n";
case Primitive<bool> p:
return "mov eax, " + (p.Value ? 1 : 0) + "\n";
case Primitive<char> p:
return "mov eax, " + (int)p.Value + "\n";
default:
return "";
}
}
// generate assembly for casting
// operand at eax
// result at eax
private string ToAssembly(Cast cast)
{
string result = ToAssembly(cast.Child());
// only float cast changes data
ValueType floatType = new ValueType(TypeCode.FLOAT);
if (cast.FromType == floatType && cast.Type != floatType)
{
// load eax to fpu
result += "mov [__temp], eax\n" +
"fld dword [__temp]\n";
// store in eax as integer
result += "fistp dword [__temp]\n" +
"mov eax, [__temp]\n";
}
if(cast.Type == floatType && cast.FromType != floatType)
{
// load eax to fpu as integer
// load eax to fpu
result += "mov [__temp], eax\n" +
"fild dword [__temp]\n";
// store in eax
result += "fstp dword [__temp]\n" +
"mov eax, [__temp]\n";
}
return result;
}
// function call assembly
private string ToAssembly(FunctionCall call)
{
string result = "";
// push pebp
// find function call entry
Tuple<string, string> functionScopeInfo = VariableAddress(call.Function());
SymbolTableEntry entry = _currentBlock.SymbolTable.GetOuterEntry(call.Function()).Item1;
if (entry.SymbolType != SymbolType.BUILTIN_FUNCTION)
{
if (functionScopeInfo.Item1 != "")
{
// push pebp
result += functionScopeInfo.Item1;
result += "push ebx\n";
}
else
result += "push ebp\n";
}
// push arguments
for(int i = call.ArgumentCount() - 1; i >= 0 ; i--)
{
result += ToAssembly(call.GetArgument(i)) +
"push eax\n";
}
if (entry.SymbolType == SymbolType.BUILTIN_FUNCTION)
result += HelperCall(call.Function().Identifier);
else
result += "call " + call.Function().Identifier + "\n";
// pop arguments
int argCount = call.ArgumentCount();
if (argCount != 0)
result += "add esp, " + argCount * 4 + "\n";
return result;
}
// array index assembly
private string ToAssembly(ArrayIndex arrayIndex)
{
int size = arrayIndex.Type.Size();
return
// index expression in stack
ToAssembly(arrayIndex.Index()) +
"push eax\n" +
// array in eax, index in ebx
ToAssembly(arrayIndex.Array()) +
"pop ebx\n" +
// access element
(size == 4 ?
"mov eax, [eax + ebx * " + size + "]\n" :
"movzx eax, " + OperationSize(size) + " [eax + ebx * " + size + "]\n");
}
// returns name of operation size based on size in bytes (bytes, word, dword...)
// input: size in bytes
// return: size name
private string OperationSize(int size)
{
return size switch
{
1 => "byte",
2 => "word",
4 => "dword",
_ => ""
};
}
// generate assembly for variable reference
// load local var from memory to eax
private string ToAssembly(Variable variable)
{
Tuple<string, string> addressASM = VariableAddress(variable);
return addressASM.Item1 +
"mov eax, [" + addressASM.Item2 + "]\n";
}
// method gets assembly address of variable
// input: variable
// return: assembly for address, tuple (code before, address)
private Tuple<string, string> VariableAddress(Variable variable)
{
Tuple<SymbolTableEntry, SymbolTable> entry = _currentBlock.SymbolTable.GetOuterEntry(variable);
if (entry.Item2 == _currentBlock.SymbolTable)
return Tuple.Create("", "ebp" + AddressOffsetFormat(-entry.Item1.Address));
else
{
string asm = "";
string baseRegister = "ebp";
SymbolTable outer = _currentBlock.SymbolTable;
while (entry.Item2 != outer)
{
// get address of last parameter (pebp)
SymbolTableEntry pebp = outer.GetEntry("pebp", variable.Line);
asm += "mov ebx, [" + baseRegister + AddressOffsetFormat(-pebp.Address) + "]\n";
baseRegister = "ebx";
// get outer table
outer = outer.GetOuterTable();
}
entry = outer.GetOuterEntry(variable);
return Tuple.Create(asm, "ebx" + AddressOffsetFormat(-entry.Item1.Address));
}
}
// method returns address offset in correct format
// input: offset value
// return: offset string
private string AddressOffsetFormat(int offset)
{
return offset.ToString(" + #; - #;");
}
// generate assembly for variable declaration
// load local var from memory to eax
private string ToAssembly(VariableDeclaration variableDeclaration)
{
string result = "";
// add assignments ASM
foreach (AST_Node child in variableDeclaration.Children)
{
result += ToAssembly(child);
}
return result;
}
// generate assembly for function definition
private string ToAssembly(FunctionDeclaration function)
{
return function.Identifier + ":\n" +
Indent(
"push ebp\n" +
"mov ebp, esp\n\n" +
ToAssembly(function.GetChild(0)) +
"\nmov esp, ebp\n" +
"pop ebp\n" +
"ret"
) +
"\n\n";
}
// Method generates assembly for a print statement
private string ToAssembly(PrintStatement statement)
{
ValueType type = statement.GetExpression().Type;
if (type.Pointer != 0)
{
if(type.TypeCode == TypeCode.CHAR && type.Pointer == 1)
return ToAssembly(statement.GetExpression()) +
HelperCall("print_str");
return ToAssembly(statement.GetExpression()) +
HelperCall("print_ptr");
}
return ToAssembly(statement.GetExpression()) +
statement.GetExpression().Type.TypeCode switch
{
TypeCode.INT => HelperCall("print_int"),
TypeCode.FLOAT => HelperCall("print_float"),
TypeCode.BOOL => HelperCall("print_bool"),
TypeCode.CHAR => HelperCall("print_char"),
_ => ""
};
}
// Method generates assembly for a return statement
// return value in eax
private string ToAssembly(ReturnStatement stmt)
{
return
// calculate return value in eax
ToAssembly(stmt.GetExpression()) +
"mov esp, ebp\n" +
"pop ebp\n" +
"ret\n";
}
// Method generates assembly for an if statement
private string ToAssembly(IfStatement stmt)
{
string elseLabel = GetLabel();
string finalLabel = stmt.HasElse() ? GetLabel() : "";
return
// condition in eax
ToAssembly(stmt.GetCondition()) +
// cmp and jump
"cmp eax, 0\n" +
"je " + elseLabel + "\n" +
// code for if block
ToAssembly(stmt.GetTrueBlock()) +
(stmt.HasElse() ? "jmp " + finalLabel + "\n" : "") +
// end of if block, start else
elseLabel + ":\n" +
(stmt.HasElse() ? ToAssembly(stmt.GetFalseBlock()) : "") +
(stmt.HasElse() ? finalLabel + ":\n" : "");
}
// Method generates assembly for an if statement
private string ToAssembly(WhileLoop stmt)
{
if (stmt.IsDoWhile)
{
string loopStartLabel = GetLabel();
return
loopStartLabel + ":\n" +
ToAssembly(stmt.GetBlock()) +
ToAssembly(stmt.GetCondition()) +
"cmp eax, 0\n" +
"jne " + loopStartLabel + "\n";
}
else
{
string loopStartLabel = GetLabel();
string loopEndLabel = GetLabel();
return
loopStartLabel + ":\n" +
ToAssembly(stmt.GetCondition()) +
"cmp eax, 0\n" + // if false end loop
"je " + loopEndLabel + "\n" +
ToAssembly(stmt.GetBlock()) +
"jmp " + loopStartLabel + "\n" +
loopEndLabel + ":\n";
}
}
// Method generates assembly for an if statement
private string ToAssembly(ForLoop stmt)
{
// change current block
Block prevBlock = _currentBlock;
_currentBlock = stmt;
string loopStartLabel = GetLabel();
string loopEndLabel = GetLabel();
string result = "";
// start block
int stackOffset = stmt.SymbolTable.VariableBytes();
if (stackOffset != 0)
result += "sub esp, " + stackOffset + "\n";
// loop
result +=
// initialization
ToAssembly(stmt.GetChild(ForLoop.INIT_INDEX)) +
loopStartLabel + ":\n" +
// check condition
ToAssembly(stmt.GetChild(ForLoop.CONDITION_INDEX)) +
"cmp al, 0\n" +
"je " + loopEndLabel + "\n" +
// body
ToAssembly(stmt.GetChild(ForLoop.BODY_INDEX)) +
// loop end
ToAssembly(stmt.GetChild(ForLoop.ACTION_INDEX)) +
"jmp " + loopStartLabel + "\n" +
loopEndLabel + ":\n";
// end block and return
if (stackOffset != 0)
result += "add esp, " + stackOffset + "\n";
_currentBlock = prevBlock;
return result;
}
// Method generates assembly for a switch case statement
private string ToAssembly(SwitchCase stmt)
{
string result = "";
// generate labels
List<string> caseLabels = new List<string>();
int caseCount = (stmt.Children.Count - 1) / 2;
for (int i = 0; i < caseCount; i++)
{
caseLabels.Add(GetLabel());
}
string endLabel = GetLabel();
int caseStartIndex = stmt.HasDefault ? 2 : 1;
// get switch value on the stack
result += ToAssembly(stmt.GetChild(0)) +
"push eax\n";
// case jumps
for(int i = 0; i < caseCount; i++)
{
Expression caseExpression = stmt.GetChild(caseStartIndex + 2 * i) as Expression;
result +=
ToAssembly(caseExpression) +
"cmp [esp], eax\n" +
"je " + caseLabels[i] + "\n";
}
// default
if(stmt.HasDefault)
{
result += ToAssembly(stmt.GetChild(1)) +
"jmp " + endLabel + "\n";
}
// case blocks
for(int i = 0; i < caseCount; i++)
{
Statement caseStatement = stmt.GetChild(caseStartIndex + 2 * i + 1) as Statement;
result +=
caseLabels[i] + ":\n" +
ToAssembly(caseStatement);
if(i != caseCount - 1)
result += "jmp " + endLabel + "\n";
}
// switch case exit
result += endLabel + ":\n" +
"sub esp, 4\n\n"; // pop switch value
return result;
}
// Method adds necessary global variables after turning program to assembly
// input: none
// return: assembly code for data section
private string DataSectionAssembly()
{
string result = "";
foreach(DataSectionVar v in _varsToDeclare)
{
result += v.ToAssembly();
}
return result;
}
// Method adds indent before every line
// input: string to indent
// return: indented string
private string Indent(string str)
{
return "\t" + str.Replace("\n", "\n\t");
}
// Method returns an assembly call to a helper function and makes sure it's added to the final assembly.
// input: name of helper function
// return: function call assembly as string
private string HelperCall(string functionName)
{
_helperFunctionsUsed.Add(functionName);
return "call " + functionName + "\n";
}
// Method returns an assembly macro use and makes sure it's added to the final assembly
// input: name of macro, parameters
// return: macro call
private string Macro(string macroName, string parameters)
{
_macrosUsed.Add(macroName);
return macroName + " " + parameters + "\n";
}
// Method returns assembly code for used helper functions and macros
// input: none
// return: assembly code for definitions of used functions
private string HelperFunctionsAssembly()
{
string[] functions = Properties.Resources.Functions.Split(";FUNCTION;\r\n", StringSplitOptions.RemoveEmptyEntries);
string result = "";
// add used functions to result
foreach(string function in functions)
{
string name = function.Split(":")[0];
// if this function was used, add its definition
if (_helperFunctionsUsed.Contains(name))
result += function;
}
return result;
}
private string MacrosAssembly()
{
string result = "";
string[] macros = Properties.Resources.Macros.Split("%macro ", StringSplitOptions.RemoveEmptyEntries);
// add used macros to result
foreach (string macro in macros)
{
string name = macro.Split()[0];
// if this function was used, add its definition
if (_macrosUsed.Contains(name))
result += "%macro " + macro + "\n";
}
return result;
}
// Method returns a unique label name every call
// input: none
// return: unique label name as string
private string GetLabel()
{
return "__" + _labelCounter++;
}
// Method reuturns asm for defining extern functions
private string ExternFunctions()
{
string result = "";
foreach(string func in _externFunctions)
{
result += "extern " + func + "\n";
}
return result;
}
private string ToAssembly(NewExpression expr)
{
ValueType type = expr.Type;
type.Pointer--;
return ToAssembly(expr.Size()) +
"mov ebx, " + new ValueType(expr.Type.TypeCode, expr.Type.Pointer - 1, expr.Line).Size() + "\n" +
"mul ebx\n" +
"push eax\n" +
"call _malloc\n" +
"add esp, 4\n";
}
private string ToAssembly(DeleteStatement stmt)
{
return ToAssembly(stmt.GetExpression()) +
"push eax\n" +
"call _free\n" +
"add esp, 4\n";
}
private string ToAssembly(LocalArray expr)
{
string ASM = "";
// get address of local array
int address = _currentBlock.SymbolTable.GetEntry(expr.GetIdentifier(), expr.Line).Address;
// initialize elements
for(int i = 0; i < expr.Children.Count; i++)
{
int elementSize = (expr.Children[i] as Expression).Type.Size();
int elementAddress = address - i * elementSize;
ASM +=
ToAssembly(expr.Children[i]) +
"mov [ebp" + AddressOffsetFormat(-elementAddress) + "], " + RegisterName('a', elementSize) + "\n";
}
// mov address to eax
return ASM +
"lea eax, [ebp" + AddressOffsetFormat(-address) + "]\n";
}
}
}