-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGenCode.java
More file actions
631 lines (569 loc) · 25.1 KB
/
GenCode.java
File metadata and controls
631 lines (569 loc) · 25.1 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
import visitor.GJDepthFirst;
import syntaxtree.*;
import java.util.*;
import java.io.*;
public class GenCode extends GJDepthFirst<String, String>{
SymbolTable symboltable;
String register, currtype, exprlist;
LinkedHashMap<String, HashMap<String, Methods>> vtable;
Variables var;
FileWriter llfile;
Boolean storevar;
int regnum, ifnum, whilenum, andnum, indexnum;
public GenCode(SymbolTable st, FileWriter file){
this.symboltable = st;
this.llfile = file;
this.regnum = 0; this.ifnum = 0; this.whilenum = 0;
this.register = ""; this.currtype = ""; this.exprlist = "";
this.storevar = false;
this.var = null;
this.vtable = new LinkedHashMap<String, HashMap<String, Methods>>();
initialize_ll();
}
public String visit(NodeToken n, String argu) { return n.toString(); }
public String visit(MainClass n, String argu) {
write_to_ll("\ndefine i32 @main(){\n");
String nameclass = n.f1.accept(this, argu);
symboltable.currentclass = new Classes(nameclass, null);
symboltable.currentmethod = new Methods("main", nameclass, "void");
symboltable.scope = "Vars";
n.f14.accept(this, argu);
n.f15.accept(this, argu);
write_to_ll("\n\tret i32 0\n}\n");
return null;
}
public String visit(ClassDeclaration n, String argu) {
String name = n.f1.accept(this, argu);
symboltable.currentclass = new Classes(name, null);
symboltable.scope = "Class";
n.f3.accept(this, argu);
n.f4.accept(this, argu);
return null;
}
public String visit(ClassExtendsDeclaration n, String argu) {
String name, parentname;
name = n.f1.accept(this, argu);
parentname = n.f3.accept(this, argu);
symboltable.currentclass = new Classes(name, parentname);
symboltable.scope = "Class";
n.f5.accept(this, argu);
n.f6.accept(this, argu);
return null;
}
public String visit(VarDeclaration n, String argu) {
String buff;
String name = n.f1.accept(this, argu);
if(!"Class".equals(symboltable.scope)){
buff = "\t%"+name+" = alloca "+typeinbytes(n.f0.accept(this, argu))+"\n";
write_to_ll(buff);
}
return name;
}
public String visit(MethodDeclaration n, String argu) {
String name = n.f2.accept(this, argu);
String type = n.f1.accept(this, argu);
regnum = 0;
write_to_ll("\ndefine "+typeinbytes(type)+" @"+symboltable.currentclass.name+"."+name+"(i8* %this");
symboltable.currentmethod = new Methods(name, symboltable.currentclass.name, type);
n.f4.accept(this, argu);
write_to_ll(") {\n");
allocationvars();
symboltable.scope = "Args";
n.f7.accept(this, argu);
n.f8.accept(this, argu);
String ret = n.f10.accept(this, type);
write_to_ll("\tret "+typeinbytes(type)+" "+ret+"\n}\n");
return null;
}
public String visit(FormalParameterList n, String argu) {
String _ret=null;
write_to_ll(", ");
n.f0.accept(this, argu);
n.f1.accept(this, argu);
return null;
}
public String visit(FormalParameter n, String argu) {
String name = n.f1.accept(this, argu);
String type = n.f0.accept(this, argu);
write_to_ll(typeinbytes(type)+" %."+name);
symboltable.scope = "Args";
return null;
}
public String visit(FormalParameterTerm n, String argu) {
write_to_ll(", ");
n.f1.accept(this, argu);
return null;
}
public String visit(BooleanArrayType n, String argu) {
return n.f0.accept(this, argu)+"[]";
}
public String visit(IntegerArrayType n, String argu) {
return n.f0.accept(this, argu)+"[]";
}
// ----------------S T A T E M E N T S----------------
public String visit(AssignmentStatement n, String argu) {
storevar = false;
String reg2 = n.f2.accept(this, "var"), type1=null;
storevar = true;
String reg1 = n.f0.accept(this, "var");
storevar = false;
String type = typeinbytes(currtype);
write_to_ll("\tstore "+type+" "+reg2+", "+type+"* "+reg1+"\n");
return null;
}
public String visit(ArrayAssignmentStatement n, String argu) {
indexnum+=1;
String indexok="index_ok_"+(indexnum-1);
String indexerror="index_error_"+(indexnum-1);
String reg = n.f0.accept(this, "array"), buff, tmpreg;
String type = typeinbytes(currtype.substring(0, currtype.length()-2));
tmpreg=reg;
Boolean boolarray=false;
if(currtype.equals("boolean[]")){
write_to_ll("\t%_"+regnum+" = bitcast i8* "+reg+" to i32*\n");
tmpreg="%_"+regnum;
regnum+=1;
boolarray=true;
}
write_to_ll("\t%_"+regnum+" = load i32, i32* "+tmpreg+"\n");
int regtmp = regnum; regnum+=1;
String expr1 = n.f2.accept(this, argu);
if(boolarray){
buff="\t%_"+regnum+" = icmp sge i32 "+expr1+", "+0+"\n"+
"\t%_"+(regnum+1)+" = icmp slt i32 "+expr1+", %_"+regtmp+"\n"+
"\t%_"+(regnum+2)+" = and i1 %_"+regnum+", %_"+(regnum+1)+"\n"+
"\tbr i1 %_"+(regnum+2)+", label %"+indexok+", label %"+indexerror+"\n"+
"\n"+indexerror+":\n"+"\tcall void @throw_oob()\n\tbr label %"+indexok+"\n"+
"\n"+indexok+":\n";
write_to_ll(buff);
regnum+=3;
String expr2 = n.f5.accept(this, argu);
buff="\t%_"+regnum+" = add i32 4, "+expr1+"\n"+
"\t%_"+(regnum+1)+" = zext i1 "+expr2+" to i8\n"+
"\t%_"+(regnum+2)+" = getelementptr i8, i8* "+reg+", i32 %_"+regnum+"\n"+
"\tstore i8 %_"+(regnum+1)+", i8* %_"+(regnum+2)+"\n";
write_to_ll(buff);
regnum+=3;
}
else{
buff="\t%_"+regnum+" = icmp sge i32 "+expr1+", "+0+"\n"+
"\t%_"+(regnum+1)+" = icmp slt i32 "+expr1+", %_"+regtmp+"\n"+
"\t%_"+(regnum+2)+" = and i1 %_"+regnum+", %_"+(regnum+1)+"\n"+
"\tbr i1 %_"+(regnum+2)+", label %"+indexok+", label %"+indexerror+"\n"+
"\n"+indexerror+":\n"+"\tcall void @throw_oob()\n\tbr label %"+indexok+"\n"+
"\n"+indexok+":\n";
write_to_ll(buff);
regnum+=3;
String expr2 = n.f5.accept(this, argu);
buff="\t%_"+regnum+" = add i32 1, "+expr1+"\n"+
"\t%_"+(regnum+1)+" = getelementptr "+type+", "+type+"* "+reg+", i32 %_"+regnum+"\n"+
"\tstore "+type+" "+expr2+", "+type+"* %_"+(regnum+1)+"\n";
write_to_ll(buff);
regnum+=2;
}
return null;
}
public String visit(IfStatement n, String argu) {
ifnum+=1;
String iflabel="if_"+(ifnum-1);
String elselabel="else_"+(ifnum-1);
String endlabel="fi_"+(ifnum-1);
String expr=n.f2.accept(this, "var");
write_to_ll("\tbr i1 "+expr+", label %"+iflabel+", label %"+elselabel+"\n\n"+iflabel+":\n");
n.f4.accept(this, "var");
write_to_ll("\tbr label %"+endlabel+"\n\n"+elselabel+":\n");
n.f6.accept(this, "var");
write_to_ll("\tbr label %"+endlabel+"\n\n"+endlabel+":\n");
return null;
}
public String visit(WhileStatement n, String argu) {
whilenum+=1;
String whilelabel="while_"+(whilenum-1);
String dolabel="do_"+(whilenum-1);
String donelabel="done_"+(whilenum-1);
write_to_ll("\tbr label %"+whilelabel+"\n\n"+whilelabel+":\n");
String expr=n.f2.accept(this, "expr");
write_to_ll("\tbr i1 "+expr+", label %"+dolabel+", label %"+donelabel+"\n\n"+dolabel+":\n");
n.f4.accept(this, "expr");
write_to_ll("\tbr label %"+whilelabel+"\n\n"+donelabel+":\n");
return null;
}
public String visit(PrintStatement n, String argu) {
String _ret=null;
String regexpr = n.f2.accept(this, " ");
write_to_ll("\tcall void (i32) @print_int(i32 "+regexpr+")\n");
return null;
}
// ----------------E X P R E S S I O N S----------------
public String visit(Expression n, String argu) {
String returned = n.f0.accept(this, argu);
write_to_ll("\n");
return returned;
}
public String visit(AndExpression n, String argu) {
andnum+=1;
String ret=null, reg1, reg2;
String andfalse="and_false_"+(andnum-1);
String andright="and_right_"+(andnum-1);
String andtmp="and_tmp_"+(andnum-1);
String andexit="and_exit_"+(andnum-1);
reg1=n.f0.accept(this, "boolean");
write_to_ll("\tbr i1 "+reg1+", label %"+andright+", label %"+andfalse+
"\n\n"+andfalse+":\n\tbr label %"+andtmp+"\n\n"+andright+":\n");
reg2=n.f2.accept(this, "boolean");
write_to_ll("\tbr label %"+andtmp+"\n\n"+andtmp+":\n\tbr label %"+andexit+
"\n\n"+andexit+":\n"+"\t%_"+regnum+" = phi i1 [ 0, %"+andfalse+" ], [ "+reg2+", %"+andtmp+" ]\n");
ret="%_"+regnum; regnum+=1;
// currtype="boolean";
return ret;
}
public String visit(CompareExpression n, String argu) {
String reg1 = n.f0.accept(this, "int");
String reg2 = n.f2.accept(this, "int");
String ret = "%_"+regnum;
write_to_ll( "\t%_"+regnum+" = icmp slt i32 "+reg1+", "+reg2+"\n");
regnum+=1;
// currtype="boolean";
return ret;
}
public String visit(PlusExpression n, String argu) {
String reg1 = n.f0.accept(this, "int");
String reg2 = n.f2.accept(this, "int");
String ret = "%_"+regnum;
write_to_ll( "\t%_"+regnum+" = add i32 "+reg1+", "+reg2+"\n");
regnum+=1;
// currtype="int";
return ret;
}
public String visit(MinusExpression n, String argu) {
String reg1 = n.f0.accept(this, "int");
String reg2 = n.f2.accept(this, "int");
String ret = "%_"+regnum;
write_to_ll( "\t%_"+regnum+" = sub i32 "+reg1+", "+reg2+"\n");
regnum+=1;
// currtype="int";
return ret;
}
public String visit(TimesExpression n, String argu) {
String reg1 = n.f0.accept(this, "int");
String reg2 = n.f2.accept(this, "int");
String ret = "%_"+regnum;
write_to_ll( "\t%_"+regnum+" = mul i32 "+reg1+", "+reg2+"\n");
regnum+=1;
// currtype="int";
return ret;
}
public String visit(ArrayLookup n, String argu) {
String ret;
indexnum+=1;
String indexok="index_ok_"+(indexnum-1);
String indexerror="index_error_"+(indexnum-1);
String reg = n.f0.accept(this, "array"), buff;
String typearray = currtype;
String type = typeinbytes(currtype.substring(0, currtype.length()-2));
if(typearray.equals("boolean[]")){
buff="\t%_"+regnum+" = bitcast i8* "+reg+" to i32*\n"+
"\t%_"+(regnum+1)+" = load i32, i32* %_"+regnum+"\n";
write_to_ll(buff);
int regtmp = (regnum+1); regnum+=2;
String expr1 = n.f2.accept(this, argu);
buff="\t%_"+regnum+" = icmp sge i32 "+expr1+", "+0+"\n"+
"\t%_"+(regnum+1)+" = icmp slt i32 "+expr1+", %_"+regtmp+"\n"+
"\t%_"+(regnum+2)+" = and i1 %_"+regnum+", %_"+(regnum+1)+"\n"+
"\tbr i1 %_"+(regnum+2)+", label %"+indexok+", label %"+indexerror+"\n"+
"\n"+indexerror+":\n"+"\tcall void @throw_oob()\n\tbr label %"+indexok+"\n"+
"\n"+indexok+":\n"+"\t%_"+(regnum+3)+" = add i32 4, "+expr1+"\n"+
"\t%_"+(regnum+4)+" = getelementptr i8, i8* "+reg+", i32 %_"+(regnum+3)+"\n"+
"\t%_"+(regnum+5)+" = load i8, i8* %_"+(regnum+4)+"\n"+
"\t%_"+(regnum+6)+" = trunc i8 %_"+(regnum+5)+" to i1\n";
write_to_ll(buff);
ret = "%_"+(regnum+6); regnum+=7;
}
else{
write_to_ll("\t%_"+regnum+" = load "+type+", "+type+"* "+reg+"\n");
int regtmp = regnum; regnum+=1;
String expr1 = n.f2.accept(this, argu);
buff="\t%_"+regnum+" = icmp sge i32 "+expr1+", "+0+"\n"+
"\t%_"+(regnum+1)+" = icmp slt i32 "+expr1+", %_"+regtmp+"\n"+
"\t%_"+(regnum+2)+" = and i1 %_"+regnum+", %_"+(regnum+1)+"\n"+
"\tbr i1 %_"+(regnum+2)+", label %"+indexok+", label %"+indexerror+"\n"+
"\n"+indexerror+":\n"+"\tcall void @throw_oob()\n\tbr label %"+indexok+"\n"+
"\n"+indexok+":\n"+"\t%_"+(regnum+3)+" = add i32 1, "+expr1+"\n"+
"\t%_"+(regnum+4)+" = getelementptr "+type+", "+type+"* "+reg+", i32 %_"+(regnum+3)+"\n"+
"\t%_"+(regnum+5)+" = load "+type+", "+type+"* %_"+(regnum+4)+"\n";
write_to_ll(buff);
ret = "%_"+(regnum+5); regnum+=6;
}
return ret;
}
public String visit(ArrayLength n, String argu) {
String ret;
String reg = n.f0.accept(this, "var");
if(currtype.equals("boolean[]")){
write_to_ll("\t%_"+regnum+" = bitcast i8* "+reg+" to i32*\n"+"\t%_"+(regnum+1)+" = load i32, i32* %_"+regnum+"\n");
ret="%_"+(regnum+1); regnum+=2;
}
else{
write_to_ll("\t%_"+regnum+" = load i32, i32* "+reg+"\n");
ret="%_"+regnum; regnum+=1;
}
// currtype = "int";
return ret;
}
public String visit(MessageSend n, String argu) {
String reg = n.f0.accept(this, "var");
Methods method;
int register;
exprlist = "";
String buff = "\t%_"+regnum+" = bitcast i8* "+reg+" to i8***\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = load i8**, i8*** %_"+(regnum-1)+"\n"; regnum+=1;
String name = n.f2.accept(this, null);
int position = vtable.get(currtype).get(name).offset/8;
method = vtable.get(currtype).get(name);
buff = buff+"\t%_"+regnum+" = getelementptr i8*, i8** %_"+(regnum-1)+", i32 "+position+"\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = load i8*, i8** %_"+(regnum-1)+"\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = bitcast i8* %_"+(regnum-1)+" to "+typeinbytes(method.type)+" ("+argsinbytes(method)+")*"+"\n"; regnum+=1;
register = regnum-1;
write_to_ll(buff);
n.f4.accept(this, argu);
String[] args_array = (reg+","+exprlist).split(",");
String[] types_array = argsinbytes(method).split(",");
String printargs = argswithtypes(types_array, args_array);
buff = "\t%_"+regnum+" = call "+typeinbytes(method.type)+" %_"+register+"("+printargs+")\n";
register = regnum; regnum+=1;
write_to_ll(buff);
currtype=method.type;
return "%_"+register;
}
public String visit(ExpressionList n, String argu) {
exprlist=n.f0.accept(this, "var");
n.f1.accept(this, argu);
return null;
}
public String visit(ExpressionTerm n, String argu) {
exprlist = exprlist+","+n.f1.accept(this, "var");
return null;
}
public String visit(IntegerLiteral n, String argu) {
currtype = "int";
return n.f0.accept(this, argu);
}
public String visit(TrueLiteral n, String argu) {
currtype = "boolean";
return "1";
}
public String visit(FalseLiteral n, String argu) {
currtype = "boolean";
return "0";
}
public String visit(Identifier n, String argu) {
String name = n.f0.accept(this, argu);
String buff="", typevar;
if(argu!=null){
if((var = symboltable.findvar(symboltable.currentclass.name, symboltable.currentmethod.name, name, true))!=null){
typevar = typeinbytes(var.type);
Classes findin = symboltable.inclass;
if(findin!=null) {
int offset = 8+findin.vars.get(var.name).offset;
buff = buff+"\t%_"+regnum+" = getelementptr i8, i8* %this, i32 "+offset+"\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = bitcast i8* %_"+(regnum-1)+" to "+typeinbytes(var.type)+"*\n";
write_to_ll(buff);
typevar = typeinbytes(var.type);
register = "%_"+regnum;
name = register;
regnum+=1;
}
else name = "%"+name;
if(!storevar){
write_to_ll("\t%_"+regnum+" = load "+typevar+", "+typevar+"* "+name+"\n");
name = "%_"+regnum;
regnum+=1;
}
currtype = var.type;
}
}
return name;
}
public String visit(ThisExpression n, String argu) {
currtype = symboltable.currentclass.name;
return "%this";
}
public String visit(BooleanArrayAllocationExpression n, String argu) {
indexnum+=1;
String indexok="index_ok_"+(indexnum-1);
String indexerror="index_error_"+(indexnum-1);
String buff, ret;
String reg = n.f3.accept(this, argu);
buff="\t%_"+regnum+" = add i32 4"+", "+reg+"\n"+
"\t%_"+(regnum+1)+" = icmp sge i32 %_"+regnum+", 4\n"+
"\tbr i1 %_"+(regnum+1)+", label %"+indexok+", label %"+indexerror+"\n\n"+
indexerror+":\n\tcall void @throw_nsz()\n\tbr label %"+indexok+"\n\n"+
indexok+":\n\t%_"+(regnum+2)+" = call i8* @calloc(i32 1, i32 %_"+regnum+")\n"+
"\t%_"+(regnum+3)+" = bitcast i8* %_"+(regnum+2)+" to i32*\n"+
"\tstore i32 "+reg+", i32* %_"+(regnum+3);
ret="%_"+(regnum+2); regnum+=4;
write_to_ll(buff);
currtype="boolean[]";
return ret;
}
public String visit(IntegerArrayAllocationExpression n, String argu) {
indexnum+=1;
String indexok="index_ok_"+(indexnum-1);
String indexerror="index_error_"+(indexnum-1);
String buff, ret;
String reg = n.f3.accept(this, argu);
buff="\t%_"+regnum+" = add i32 1"+", "+reg+"\n"+
"\t%_"+(regnum+1)+" = icmp sge i32 %_"+regnum+", 1\n"+
"\tbr i1 %_"+(regnum+1)+", label %"+indexok+", label %"+indexerror+"\n\n"+
indexerror+":\n\tcall void @throw_nsz()\n\tbr label %"+indexok+"\n\n"+
indexok+":\n\t%_"+(regnum+2)+" = call i8* @calloc(i32 %_"+regnum+", i32 4)\n"+
"\t%_"+(regnum+3)+" = bitcast i8* %_"+(regnum+2)+" to i32*\n"+
"\tstore i32 "+reg+", i32* %_"+(regnum+3)+"\n";
ret="%_"+(regnum+3); regnum+=4;
write_to_ll(buff);
currtype="int[]";
return ret;
}
public String visit(AllocationExpression n, String argu) {
String name = n.f1.accept(this, argu), regret=null, buff;
HashMap<String, Methods> methodmap = vtable.get(name);
int regcast = regnum, regcall = regnum;
if(methodmap!=null){
int sizeOffset = symboltable.sizeClass(name)+8;
buff = "\t%_"+regnum+" = call i8* @calloc(i32 1, i32 "+sizeOffset+")\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = bitcast i8* %_"+(regnum-1)+" to i8***\n"; regnum+=1;
buff = buff+"\t%_"+regnum+" = getelementptr ["+vtable.get(name).size()+" x i8*], ["+vtable.get(name).size()+" x i8*]* @."+name+"_vtable, i32 0, i32 0\n"+
"\tstore i8** %_"+regnum+", i8*** %_"+(regnum-1)+"\n"; regnum+=1;
currtype = name;
write_to_ll(buff);
}
return "%_"+(regnum-3);
}
public String visit(NotExpression n, String argu) {
String reg=n.f1.accept(this, "boolean");
String ret = "%_"+regnum;
write_to_ll( "\t%_"+regnum+" = xor i1 1, "+reg+"\n");
regnum+=1;
// currtype="boolean";
return ret;
}
public String visit(BracketExpression n, String argu) {
return n.f1.accept(this, argu);
}
// ----------------F U N C T I O N S----------------
public String typeinbytes(String type){
String returned="";
if("int".equals(type)) returned = "i32";
else if("boolean".equals(type)) returned = "i1";
else if("int[]".equals(type)) returned = "i32*";
else returned = "i8*";
return returned;
}
public String argswithtypes(String[] types, String[] args){
String returned="";
if(types.length==args.length){
for(int i=0; i<types.length; i++){
returned = returned+types[i]+" "+args[i]+",";
}
}
if(returned.length()>0) returned=returned.substring(0, returned.length()-1);
return returned;
}
public void allocationvars(){
Methods method = symboltable.methods.get(symboltable.currentclass.name+symboltable.currentmethod.name);
String buff="";
for (String keyargs : method.args.keySet()) {
var = method.args.get(keyargs);
String vartype = typeinbytes(var.type);
buff = buff+"\t%"+var.name+" = alloca "+vartype+"\n";
buff = buff+"\tstore "+vartype+" %."+var.name+", "+vartype+"* %"+var.name+"\n";
}
write_to_ll(buff);
}
public void initialize_ll(){
String buff = "";
boolean mainclass=true;
for (String keyclass : symboltable.classes.keySet()) {
LinkedHashMap<String, Methods> methodmap = new LinkedHashMap<String, Methods>();
String parentname, nameclass;
nameclass = symboltable.classes.get(keyclass).name;
parentname = nameclass;
createVtable(methodmap, parentname, keyclass);
int argsnum = 1;
if(!mainclass){
buff = "@."+keyclass+"_vtable = global ["+methodmap.size()+" x i8*] [";
for (String keymethod : methodmap.keySet()) {
Methods method_ = methodmap.get(keymethod);
buff = buff+"\n\ti8* bitcast ("+typeinbytes(method_.type)+" (i8*";
for (String keyargs : method_.args.keySet()) {
Variables arg_ = method_.args.get(keyargs);
buff = buff +", "+typeinbytes(arg_.type);
}
if(symboltable.methods.containsKey(keyclass+method_.name)) symboltable.methods.get(keyclass+method_.name).vplace = argsnum-1;
else{
method_.vplace = argsnum-1;
symboltable.methods.put(keyclass+method_.name, method_);
}
buff = buff+")* @"+method_.classpar+"."+method_.name+" to i8*)";
if(argsnum<methodmap.size()) buff = buff+",";
else buff = buff+"\n";
argsnum+=1;
}
}
else{
buff = "@."+keyclass+"_vtable = global [0 x i8*] [";
mainclass = false;
}
buff = buff+"]\n\n";
write_to_ll(buff);
vtable.put(keyclass, methodmap);
}
buff = "declare i8* @calloc(i32, i32)\n"+"declare i32 @printf(i8*, ...)\n"+"declare void @exit(i32)\n\n"+
"@_cint = constant [4 x i8] c\"%d\\0a\\00\"\n"+"@_cOOB = constant [15 x i8] c\"Out of bounds\\0a\\00\"\n"+"@_cNSZ = constant [15 x i8] c\"Negative size\\0a\\00\"\n"+
"\ndefine void @print_int(i32 %i) {\n\t%_str = bitcast [4 x i8]* @_cint to i8*\n\tcall i32 (i8*, ...) @printf(i8* %_str, i32 %i)\n\tret void\n}\n"+
"\ndefine void @throw_oob() {\n\t%_str = bitcast [15 x i8]* @_cOOB to i8*\n\tcall i32 (i8*, ...) @printf(i8* %_str)\n\tcall void @exit(i32 1)\n\tret void\n}\n"+
"\ndefine void @throw_nsz() {\n\t%_str = bitcast [15 x i8]* @_cNSZ to i8*\n\tcall i32 (i8*, ...) @printf(i8* %_str)\n\tcall void @exit(i32 1)\n\tret void\n}\n";
write_to_ll(buff);
}
public String argsinbytes(Methods method){
String buff="i8*,";
for (String keyarg : method.args.keySet()) {
buff=buff+typeinbytes(method.args.get(keyarg).type)+",";
}
buff=buff.substring(0, buff.length()-1);
return buff;
}
public void createVtable(LinkedHashMap<String, Methods> methodmap, String parentname, String keyclass){
String name;
if(parentname!=null) {
name=parentname;
parentname = symboltable.classes.get(parentname).parent;
createVtable(methodmap, parentname, keyclass);
for (String keymethod : symboltable.methods.keySet()) {
// System.out.println(keyclass+" "+symboltable.methods.get(keymethod).classpar+"."+symboltable.methods.get(keymethod).name);
if(symboltable.methods.get(keymethod).classpar.equals(name)){
Methods method = symboltable.methods.get(keymethod);
methodmap.put(method.name, method);
}
}
}
}
public void write_to_ll(String buffer){
try{
llfile.write(buffer);
}
catch (IOException e) {
System.out.println("Error at writing the <file>.ll");
e.printStackTrace();
}
}
public void printHasmap(){
for (String keyclass : symboltable.classes.keySet()) {
HashMap<String, Methods> methodmap = vtable.get(keyclass);
for (String keymethod : methodmap.keySet()) {
Methods method_ = methodmap.get(keymethod);
System.out.println(keyclass+" => "+method_.classpar+"."+method_.name+" : "+method_.offset);
}
}
}
}