Repository navigation
Expand file tree
/
Copy pathASD.java
More file actions
711 lines (571 loc) · 20.3 KB
/
Copy pathASD.java
File metadata and controls
711 lines (571 loc) · 20.3 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
package TP2;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
/**
* @author Erwan IQUEL, Mathieu LE CLEC'H
*/
public class ASD {
public static int level = 0;
/**
* Représentation sous forme de classe interne du Variant/Constructeur Program
*/
static public class Program {
Bloc b // What a program contains. TODO : change when you extend the language
/**
* Constructor
*/
public Program(Bloc b) {
this.b = b;
}
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'un programme VSL+
*/
public String pp() {
return this.b.pp();
}
/**
* Générateur d'un ensemble d'instructions LLVM
* @return Ensemble d'instructions LLVM
*/
public Llvm.IR toIR() throws TypeException {
SymbolTable st = new SymbolTable();
// TODO : change when you extend the language
Llvm.IR retBloc = bloc.toIR(st);
Llvm.Instruction ret = new Llvm.Return(new IntType().toLlvmType(), "0");//TODO: Modifier "0" par le resultat à renvoyer une fois le Load pris en compte
retBloc.appendCode(ret);
return retBloc.ir;
}
}
// TODO: All toIR methods returns the IR, plus extra information (synthesized attributes)
// TODO: They can take extra arguments (inherited attributes)
/**
* Représentation sous forme de classe interne du Variant/Constructeur Program
*/
static public class Bloc {
List<Variable> variables;
List<Instruction> instructions;
Bloc bloc;
List<Instruction> instructions2; // What a program contains. TODO : change when you extend the language
/**
* Constructor
*/
public Bloc(List<Variable> variables, List<Instruction> instructions, Bloc bloc, List<Instruction> instructions2) {
this.variables = variables;
this.instructions = instructions;
this.bloc = bloc;
this.instructions2 = instructions2;
}
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'un bloc VSL+
*/
public String pp() {
String ret = "{\n"
level++;
ret += Utils.indent(level) + "INT ";
for(Variable variable : this.variables) {
ret += variable.pp() + ", ";
}
ret += "\n";
for(Instruction instruction : this.instructions) {
ret += Utils.indent(level) + instruction.pp() + "\n";
}
ret += this.bloc.pp();
for(Instruction instruction : this.instructions2) {
ret += Utils.indent(level) + instruction.pp() + "\n";
}
level--;
return ret + "\n}\n";
}
/**
* Générateur d'un ensemble d'instructions LLVM
* @return Ensemble d'instructions LLVM
*/
public Llvm.IR toIR(SymbolTable st) throws TypeException {
SymbolTable scope = new SymbolTable(st);
Variable.RetVariable retVar = null;
for(Variable variable : this.variables) {
if(retVar == null) retVar = variable.toIR(scope);
else retVar.ir.append(variable.toIR(scope).ir);
}
// computes the IR of the instruction
Instruction.RetInstruction retInst = null
for(Instruction instruction : this.instructions) {
restInst.ir.append(instruction.toIR(scope).ir);
}
if(retInst != null) retVar.ir.append(retInst.ir);
retVar.ir.append(this.bloc.toIR(scope));
Instruction.RetInstruction retInst = null
for(Instruction instruction : this.instructions2) {
restInst.ir.append(instruction.toIR(scope).ir);
}
if(retInst != null) retVar.ir.append(retInst.ir);
// add a return instruction
if(level == 0) {
Llvm.Instruction ret = new Llvm.Return(new IntType().toLlvmType(), "0");//TODO: Modifier "0" par le resultat à renvoyer une fois le Load pris en compte
retVar.ir.appendCode(ret);
}
return retVar.ir;
}
}
/**
* Représentation sous forme de classe interne du Variant Expression
*/
static public abstract class Expression {
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'une expression VSL+
*/
public abstract String pp();
/**
* Générateur d'un ensemble d'instructions LLVM
* * @return Ensemble d'instructions LLVM
*/
public abstract RetExpression toIR(SymbolTable st) throws TypeException;
// TODO: Object returned by toIR on expressions, with IR + synthesized attributes
/**
* Représentation de l'état d'une instruction sous forme de classe interne
*/
static public class RetExpression {
/**
* LLVM instruction
*/
public Llvm.IR ir;
/**
* Type de l'expression (synthesized)
*/
public Type type;
/**
* Resultat de l'expression (synthesized)
* La valeur est soit un ident ou une valeur "immediate"
*/
public String result;
/**
* Constructeur
*/
public RetExpression(Llvm.IR ir, Type type, String result) {
this.ir = ir;
this.type = type;
this.result = result;
}
}
}
/**
* Représentation sous forme de classe interne du Constructeur AddExpression
*/
static public class AddExpression extends Expression {
/**
* Expression gauche
*/
Expression left;
/**
* Expression droite
*/
Expression right;
/**
* Constructeur
*/
public AddExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public String pp() {
return "(" + left.pp() + " + " + right.pp() + ")";
}
@Override
public RetExpression toIR(SymbolTable st) throws TypeException {
RetExpression leftRet = left.toIR(st);
RetExpression rightRet = right.toIR(st);
// We check if the types mismatches
if (!leftRet.type.equals(rightRet.type)) {
throw new TypeException("type mismatch: have " + leftRet.type + " and " + rightRet.type);
}
// We base our build on the left generated IR:
// append right code
leftRet.ir.append(rightRet.ir);
// allocate a new identifier for the result
String result = Utils.newtmp();
// new add instruction result = left + right
Llvm.Instruction add = new Llvm.Add(leftRet.type.toLlvmType(), leftRet.result, rightRet.result, result);
// append this instruction
leftRet.ir.appendCode(add);
// return the generated IR, plus the type of this expression
// and where to find its result
return new RetExpression(leftRet.ir, leftRet.type, result);
}
}
/**
* Représentation sous forme de classe interne du Constructeur SubExpression
*/
static public class SubExpression extends Expression {
/**
* Expression gauche
*/
Expression left;
/**
* Expression droite
*/
Expression right;
/**
* Constructeur
*/
public SubExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public String pp() {
return "(" + left.pp() + " - " + right.pp() + ")";
}
@Override
public RetExpression toIR(SymbolTable st) throws TypeException {
RetExpression leftRet = left.toIR(st);
RetExpression rightRet = right.toIR(st);
if (!leftRet.type.equals(rightRet.type)) {
throw new TypeException("type mismatch: have " + leftRet.type + " and " + rightRet.type);
}
leftRet.ir.append(rightRet.ir);
String result = Utils.newtmp();
Llvm.Instruction sub = new Llvm.Sub(leftRet.type.toLlvmType(), leftRet.result, rightRet.result, result);
leftRet.ir.appendCode(sub);
return new RetExpression(leftRet.ir, leftRet.type, result);
}
}
/**
* Représentation sous forme de classe interne du Constructeur MulExpression
*/
static public class MulExpression extends Expression {
/**
* Expression gauche
*/
Expression left;
/**
* Expression droite
*/
Expression right;
/**
* Constructeur
*/
public MulExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public String pp() {
return "(" + left.pp() + " * " + right.pp() + ")";
}
@Override
public RetExpression toIR(SymbolTable st) throws TypeException {
RetExpression leftRet = left.toIR(st);
RetExpression rightRet = right.toIR(st);
if (!leftRet.type.equals(rightRet.type)) {
throw new TypeException("type mismatch: have " + leftRet.type + " and " + rightRet.type);
}
leftRet.ir.append(rightRet.ir);
String result = Utils.newtmp();
Llvm.Instruction mul = new Llvm.Mul(leftRet.type.toLlvmType(), leftRet.result, rightRet.result, result);
leftRet.ir.appendCode(mul);
return new RetExpression(leftRet.ir, leftRet.type, result);
}
}
/**
* Représentation sous forme de classe interne du Constructeur DivExpression
*/
static public class DivExpression extends Expression {
/**
* Expression gauche
*/
Expression left;
/**
* Expression droite
*/
Expression right;
/**
* Constructeur
*/
public DivExpression(Expression left, Expression right) {
this.left = left;
this.right = right;
}
@Override
public String pp() {
return "(" + left.pp() + " / " + right.pp() + ")";
}
@Override
public RetExpression toIR(SymbolTable st) throws TypeException {
RetExpression leftRet = left.toIR(st);
RetExpression rightRet = right.toIR(st);
if (!leftRet.type.equals(rightRet.type)) {
throw new TypeException("type mismatch: have " + leftRet.type + " and " + rightRet.type);
}
leftRet.ir.append(rightRet.ir);
String result = Utils.newtmp();
Llvm.Instruction div = new Llvm.Div(leftRet.type.toLlvmType(), leftRet.result, rightRet.result, result);
leftRet.ir.appendCode(div);
return new RetExpression(leftRet.ir, leftRet.type, result);
}
}
// Concrete class for Expression: constant (integer) case
/**
* Représentation sous forme de classe interne du Constructeur IntegerExpression
*/
static public class IntegerExpression extends Expression {
/**
* Valeur de l'entier
*/
int value;
/**
* Constructeur
*/
public IntegerExpression(int value) {
this.value = value;
}
@Override
public String pp() {
return "" + this.value;
}
@Override
public RetExpression toIR(SymbolTable st) {
// Here we simply return an empty IR
// the `result' of this expression is the integer itself (as string)
return new RetExpression(new Llvm.IR(Llvm.empty(), Llvm.empty()), new IntType(), "" + this.value);
}
}
/**
* Représentation sous forme de classe interne du Constructeur VariableExpression
*/
static public class VariableExpression extends Expression {
/**
* Nom de la variable
*/
String ident;
/**
* Constructeur
*/
public VariableExpression(String ident) {
this.ident = ident;
}
@Override
public String pp() {
return "" + this.ident;
}
@Override
public RetExpression toIR(SymbolTable st) {
SymbolTable.VariableSymbol identSymbol = (SymbolTable.VariableSymbol) st.lookup(this.ident);
if(identSymbol == null) {
System.err.println("Error: the symbol '" + this.ident + "' doesn't exist in the symbol table.");
System.exit(0);
}
String result = Utils.newtmp();
RetExpression ret = new RetExpression(new Llvm.IR(Llvm.empty(), Llvm.empty()), identSymbol.type, result);
Llvm.Instruction store = new Llvm.Load(result, identSymbol.type.toLlvmType(), identSymbol.type.toLlvmType(), identSymbol.ident);
ret.ir.appendCode(store);
return ret;
}
}
/**
* Représenation sous forme de classe interne du variant Variable
*/
static public abstract class Variable {
/**
* Nom de la variable
*/
String ident;
public Variable(String ident) {
if(ident.equals("return")) {
System.err.println("Warning: '"+ ident +"' is a reserved keyword.");
}
this.ident = ident;
}
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'une variable VSL+
*/
public abstract String pp();
/**
* Générateur d'un ensemble d'instructions LLVM
* @return Ensemble d'instructions LLVM
*/
public abstract RetVariable toIR(SymbolTable st) throws TypeException;
/**
* Représentation de l'état d'une instruction sous forme de classe interne
*/
static public class RetVariable {
/**
* LLVM instruction
*/
public Llvm.IR ir;
/**
* Type de la variable (synthesized)
*/
public Type type;
/**
* Identifiant de la variable (synthesized)
*/
public String result;
/**
* Constructeur
*/
public RetVariable(Llvm.IR ir, Type type, String result) {
this.ir = ir;
this.type = type;
this.result = result;
}
}
}
/**
* Représenation sous forme de classe interne du Constructeur IntegerVariable
*/
static public class IntegerVariable extends Variable {
/**
* Constructeur
*/
public IntegerVariable(String ident) {
super(ident);
}
@Override
public String pp() {
return "" + super.ident;
}
@Override
public RetVariable toIR(SymbolTable st) {
SymbolTable.VariableSymbol symbol = new SymbolTable.VariableSymbol(new IntType(), super.ident);
String result = "%" + super.ident;
RetVariable ret = new RetVariable(new Llvm.IR(Llvm.empty(), Llvm.empty()), new IntType(), result);
if(!st.add(symbol)) {
System.err.println("Warning: the symbol '" + super.ident + "' has already exist in SymbolTable in this scope.");
}
Llvm.Instruction alloca = new Llvm.Alloca(new IntType().toLlvmType(), result);
ret.ir.appendCode(alloca);
//TODO: supprimer lors de la mise en production (rendu final)
if(!symbol.equals(st.lookup(super.ident))) System.err.println("La variable '" + super.ident + "' doit être dans la table des symboles");
return ret;
}
}
/**
* Représentation sous forme de classe interne du Variant Instruction
*/
static public abstract class Instruction {
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'une instruction VSL+
*/
public abstract String pp();
/**
* Générateur d'un ensemble d'instructions LLVM
* @return Ensemble d'instructions LLVM
*/
public abstract RetInstruction toIR(SymbolTable st) throws TypeException;
/**
* Représentation de l'état d'une instruction sous forme de classe interne
*/
static public class RetInstruction {
/**
* LLVM instruction
*/
public Llvm.IR ir;
/**
* Resultat de l'instruction (synthetized)
*/
public String result;
/**
* Constructeur
*/
public RetInstruction(Llvm.IR ir, String result) {
this.ir = ir;
this.result = result;
}
}
}
/**
* Représentation sous forme de classe interne du Constructeur AffInstruction
*/
static public class AffInstruction extends Instruction {
/**
* Nom de la variable à qui on affecte la partie droite de l'instruction
*/
String ident;
/**
* Partie droite de l'affectation
*/
Expression expression;
/**
* Constructeur
*/
public AffInstruction(String ident, Expression expression) {
this.ident = ident;
this.expression = expression;
}
@Override
public String pp() {
return this.ident + " := " + this.expression.pp();
}
@Override
public RetInstruction toIR(SymbolTable st) throws TypeException {
SymbolTable.VariableSymbol identSymbol = (SymbolTable.VariableSymbol) st.lookup(this.ident);
if(identSymbol == null) {
System.err.println("Error: the symbol '" + this.ident + "' doesn't exist in the symbol table.");
System.exit(0);
}
String result = "%" + identSymbol.ident;
RetInstruction ret = new RetInstruction(new Llvm.IR(Llvm.empty(), Llvm.empty()), result);
Expression.RetExpression retExpr = expression.toIR(st);
ret.ir.append(retExpr.ir);
Llvm.Instruction store = new Llvm.Store(retExpr.type.toLlvmType(), retExpr.result, identSymbol.type.toLlvmType(), result);
ret.ir.appendCode(store);
return ret;
}
}
/**
* Représentation du type entier coté ASD (Attention: ne pas confondre avec les types LLVM)
*/
static public abstract class Type {
/**
* Pretty-Printer
* @return Représentation sous forme de chaine de caractère d'un type VSL+
*/
public abstract String pp();
/**
* Transforme un type ASD en type LLVM
* @return Le type LLVM
*/
public abstract Llvm.Type toLlvmType();
}
/**
* Représentation du type entier coté ASD (Attention: ne pas confondre avec les types LLVM)
*/
static class IntType extends Type {
@Override
public String pp() {
return "INT";
}
@Override
public boolean equals(Object obj) {
return obj instanceof IntType;
}
@Override
public Llvm.Type toLlvmType() {
return new Llvm.IntType();
}
}
/**
* Représentation du type entier coté ASD (Attention: ne pas confondre avec les types LLVM)
*/
static class IntPtrType extends Type {
@Override
public String pp() {
return "INT";
}
@Override
public boolean equals(Object obj) {
return obj instanceof IntPtrType;
}
@Override
public Llvm.Type toLlvmType() {
return new Llvm.IntPtrType();
}
}
}