-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
790 lines (702 loc) · 32.4 KB
/
codegen.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
// LLVM Core
#include <llvm-c/Types.h>
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/OrcBindings.h>
// Optimizations
#include <llvm-c/Transforms/IPO.h>
#include <llvm-c/Transforms/PassManagerBuilder.h>
#include <llvm-c/Transforms/Scalar.h>
#include <llvm-c/Transforms/Vectorize.h>
#include "expr.h"
#include "stmt.h"
#include "types.h"
#include "display.h"
#include "codegen.h"
#include "timer.h"
#include "runtime.h"
typedef struct{
LLVMValueRef ref;
uint64_t hash;
} VariableRef;
static uint64_t variableRefPointer = 0;
static VariableRef *variables = NULL;
static LLVMTypeRef get_generic_structure_type(){
LLVMTypeRef types [] = {LLVMInt8Type(), LLVMInt64Type()};
return LLVMStructType(types, 2, 0);
}
//static LLVMValueRef get_container_structure(){
// LLVMValueRef
//}
static uint64_t hash(const char *str, uint64_t length){
uint64_t hash = 5381;
uint64_t c = 0;
// printf("\nHashing..\n");
while (c < length)
hash = ((hash << 5) + hash) + str[c++]; /* hash * 33 + c */
// printf("\nInput String : [%s] Hash : %lu\n", str, hash);
return hash;
}
static LLVMValueRef get_variable_ref(Expression *varE, uint8_t declareIfNotfound, LLVMBuilderRef builder){
varE->hash = hash(varE->token.string, varE->token.length);
//dbg("Searching for hash %ld", varE->hash);
for(uint64_t i = 0;i < variableRefPointer;i++){
if(variables[i].hash == varE->hash){
return variables[i].ref;
}
}
if(declareIfNotfound){
//dbg("Declaring ");
//lexer_print_token(varE->token, 0);
variableRefPointer++;
variables = (VariableRef *)realloc(variables, sizeof(VariableRef) * variableRefPointer);
LLVMValueRef ref;
switch(varE->valueType){
case VALUE_GEN:
ref = LLVMBuildAlloca(builder, get_generic_structure_type(), "localGeneric");
break;
case VALUE_STR:
ref = LLVMBuildAlloca(builder, LLVMPointerType(LLVMInt8Type(), 0), "localString");
break;
case VALUE_NUM:
ref = LLVMBuildAlloca(builder, LLVMDoubleType(), "localDouble");
break;
case VALUE_INT:
ref = LLVMBuildAlloca(builder, LLVMInt64Type(), "localInt");
break;
case VALUE_STRUCT:
ref = LLVMBuildAlloca(builder, get_generic_structure_type(), "localContainer");
break;
case VALUE_BOOL:
ref = LLVMBuildAlloca(builder, LLVMInt1Type(), "localBool");
break;
}
variables[variableRefPointer - 1].ref = ref;
variables[variableRefPointer - 1].hash = varE->hash;
return ref;
}
return LLVMConstNull(LLVMInt1Type());
}
// Marks which runtime functions have been used in the program
// to perform the global mapping of their physical address
// to the engine
static uint8_t runtime_function_used[ALGI_RUNTIME_FUNCTION_COUNT] = {0};
static ValueType convert_llvmtype_to_algitype(LLVMTypeRef type){
if(type == get_generic_structure_type())
return VALUE_GEN;
if(type == LLVMInt1Type())
return VALUE_BOOL;
if(type == LLVMInt64Type())
return VALUE_INT;
if(type == LLVMDoubleType())
return VALUE_NUM;
return VALUE_STR;
}
static LLVMValueRef build_cast_call(LLVMBuilderRef builder, LLVMModuleRef module, LLVMValueRef val, ValueType castType){
LLVMTypeRef argumentType[2];
argumentType[0] = LLVMInt32Type();
LLVMValueRef argumentValue[2];
LLVMTypeRef returnType, valueType;
const char* callee = "__algi_invalid_cast";
if(LLVMIsAAllocaInst(val)){
valueType = LLVMGetAllocatedType(val);
val = LLVMBuildLoad(builder, val, "loadtmp");
}
else{
valueType = LLVMTypeOf(val);
}
argumentType[1] = valueType;
argumentValue[1] = val;
ValueType algiType = convert_llvmtype_to_algitype(valueType);
switch(castType){
case VALUE_INT:
{
runtime_function_used[ALGI_TO_INT] = 1;
callee = "__algi_to_int";
returnType = LLVMInt64Type();
}
break;
case VALUE_NUM:
{
runtime_function_used[ALGI_TO_DOUBLE] = 1;
callee = "__algi_to_double";
returnType = LLVMDoubleType();
}
break;
case VALUE_STR:
{
runtime_function_used[ALGI_TO_STRING] = 1;
callee = "__algi_to_string";
returnType = LLVMPointerType(LLVMInt8Type(), 0);
}
break;
case VALUE_BOOL:
{
runtime_function_used[ALGI_TO_BOOLEAN] = 1;
callee = "__algi_to_boolean";
returnType = LLVMInt1Type();
}
break;
default:
{
algiType = VALUE_UND;
returnType = LLVMVoidType();
}
break;
}
if(algiType == VALUE_STR && LLVMGetTypeKind(valueType) == LLVMArrayTypeKind){
dbg("Changing string type for val : \n");
LLVMDumpValue(val);
size_t l;
val = LLVMBuildGlobalString(builder, LLVMGetAsString(val, &l), "__runtime_call_tmp");
argumentValue[1] = val;
}
else if(algiType == VALUE_GEN){
argumentValue[0] = LLVMBuildExtractValue(builder, val, 0, "genextrct0");
argumentValue[0] = LLVMBuildIntCast(builder, argumentValue[0], LLVMInt32Type(), "gen0cast");
LLVMValueRef cons = LLVMConstInt(LLVMInt32Type(), VALUE_GEN, 0);
argumentValue[0] = LLVMBuildAdd(builder, argumentValue[0], cons, "gentype");
argumentValue[1] = LLVMBuildExtractValue(builder, val, 1, "genextrct1");
}
if(algiType != VALUE_GEN)
argumentValue[0] = LLVMConstInt(LLVMInt32Type(), algiType, 0);
LLVMTypeRef f = LLVMFunctionType(returnType, argumentType, 1, 1);
LLVMValueRef fn = LLVMGetNamedFunction(module, callee);
if(fn == NULL){
fn = LLVMAddFunction(module, callee, f);
}
return LLVMBuildCall(builder, fn, argumentValue, 2, "__algi_internal_cast_res");
}
static LLVMValueRef expr_compile(Expression *e, LLVMContextRef context, LLVMBuilderRef builder, LLVMModuleRef module){
if(e == NULL)
return LLVMConstNull(LLVMInt64Type());
switch(e->type){
case EXPR_CONSTANT:
{
switch(e->token.type){
case TOKEN_integer:
return LLVMConstInt(LLVMInt64Type(), e->consex.ival, 0);
case TOKEN_number:
return LLVMConstReal(LLVMDoubleType(), e->consex.dval);
case TOKEN_string:
return LLVMConstString(e->consex.sval, strlen(e->consex.sval), 0);
case TOKEN_True:
return LLVMConstInt(LLVMInt1Type(), 1, 1);
case TOKEN_False:
return LLVMConstInt(LLVMInt1Type(), 0, 1);
default:
return LLVMConstNull(LLVMInt1Type());
}
}
case EXPR_UNARY:
{
LLVMValueRef expVal = expr_compile(e->unex.right, context, builder, module);
LLVMTypeRef t = LLVMTypeOf(expVal);
if(LLVMIsAAllocaInst(expVal)){
t = LLVMGetAllocatedType(expVal);
expVal = LLVMBuildLoad(builder, expVal, "tmpLoad");
}
#define ISINT() \
if(t == LLVMInt64Type() \
|| t == LLVMInt1Type())
#define ISFLT() \
if(t == LLVMDoubleType())
#define ISBOOL() \
if(t == LLVMInt1Type())
switch(e->token.type){
case TOKEN_minus:
ISINT()
return LLVMBuildNeg(builder, expVal, "negtmp");
else ISFLT()
return LLVMBuildFNeg(builder, expVal, "fnegtmp");
return build_cast_call(builder, module, expVal, VALUE_NUM);
case TOKEN_not:
ISBOOL()
return LLVMBuildNot(builder, expVal, "nottmp");
return build_cast_call(builder, module, expVal, VALUE_BOOL);
case TOKEN_Integer:
ISINT(){
ISBOOL()
return LLVMBuildZExt(builder, expVal, LLVMInt64Type(), "bcastint");
return expVal;
}
else ISFLT()
return LLVMBuildFPToSI(builder, expVal, LLVMInt64Type(), "fcasttmp");
return build_cast_call(builder, module, expVal, VALUE_INT);
case TOKEN_Number:
ISINT(){
ISBOOL()
return LLVMBuildUIToFP(builder, expVal, LLVMDoubleType(), "bdoublecasttmp");
return LLVMBuildSIToFP(builder, expVal, LLVMDoubleType(), "doublecasttmp");
}
else ISFLT()
return expVal;
return build_cast_call(builder, module, expVal, VALUE_NUM);
// case TOKEN_Structure:
// return LLVMBuildPointerCast(builder, expVal, LLVMInt64Type(), "pointercasttmp");
case TOKEN_Boolean:
ISINT(){
ISBOOL()
return expVal;
return LLVMBuildIntCast(builder, expVal, LLVMInt1Type(), "boolcasttmp");
}
return build_cast_call(builder, module, expVal, VALUE_BOOL);
case TOKEN_String:
return build_cast_call(builder, module, expVal, VALUE_STR);
case TOKEN_Type:
if(t == get_generic_structure_type()){
expVal = LLVMBuildExtractValue(builder, expVal, 0, "typeextract");
return LLVMBuildIntCast(builder, expVal, LLVMInt64Type(), "typecnv");
}
return LLVMConstInt(LLVMInt64Type(), e->valueType, 0);
default:
// TODO: Handle this properly
return LLVMConstNull(LLVMInt1Type());
#undef ISNUM
}
}
case EXPR_VARIABLE:
return get_variable_ref(e, 1, builder);
case EXPR_BINARY:
{
LLVMValueRef left = expr_compile(e->binex.left, context, builder, module);
LLVMValueRef right = expr_compile(e->binex.right, context, builder, module);
LLVMTypeRef leftType = LLVMVoidType();
LLVMTypeRef rightType = LLVMVoidType();
if(LLVMIsAAllocaInst(left)){
leftType = LLVMGetAllocatedType(left);
left = LLVMBuildLoad(builder, left, "tmpLoad");
}
else
leftType = LLVMTypeOf(left);
if(LLVMIsAAllocaInst(right)){
rightType = LLVMGetAllocatedType(right);
right = LLVMBuildLoad(builder, right, "tmpLoad");
}
else
rightType = LLVMTypeOf(right);
if((leftType != LLVMInt64Type()) || (rightType != LLVMInt64Type())){
if(leftType == LLVMInt64Type()){
left = LLVMBuildSIToFP(builder, left, LLVMDoubleType(), "dconvtmp");
}
else
right = LLVMBuildSIToFP(builder, right, LLVMDoubleType(), "dconvtmp");
}
#define IFINT() \
if(leftType == LLVMInt64Type() && rightType == LLVMInt64Type())
#define IFTYPECHECK(insIfInt, tempName) \
IFINT() \
return LLVMBuild##insIfInt(builder, left, right, "i" tempName); \
return LLVMBuildF##insIfInt(builder, left, right, "f" tempName);
switch(e->token.type){
case TOKEN_plus:
IFTYPECHECK(Add, "addtmp")
case TOKEN_minus:
IFTYPECHECK(Sub, "subtmp")
case TOKEN_star:
IFTYPECHECK(Mul, "multmp")
case TOKEN_backslash:
IFINT()
return LLVMBuildSDiv(builder, left, right, "idivtmp");
return LLVMBuildFDiv(builder, left, right, "fdivtmp");
case TOKEN_greater:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntSGT, left, right, "igttmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealOGT, left, right, "fgttmp");
case TOKEN_greater_equal:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntSGE, left, right, "igetmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealOGE, left, right, "fgetmp");
case TOKEN_lesser:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntSLT, left, right, "ilttmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealOLT, left, right, "flttmp");
case TOKEN_lesser_equal:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntSLE, left, right, "iletmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealOLE, left, right, "fletmp");
case TOKEN_equal_equal:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntEQ, left, right, "ieqtmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealOEQ, left, right, "feqtmp");
case TOKEN_not_equal:
IFINT(){
return LLVMBuildICmp(builder, LLVMIntNE, left, right, "inetmp");
}
else
return LLVMBuildFCmp(builder, LLVMRealONE, left, right, "fnetmp");
default: // TOKEN_cap
{
if(LLVMTypeOf(left) != LLVMDoubleType()){
left = LLVMBuildSIToFP(builder, left, LLVMDoubleType(), "dcasttmp");
}
else
LLVMDumpValue(left);
if(LLVMTypeOf(right) != LLVMDoubleType()){
right = LLVMBuildSIToFP(builder, right, LLVMDoubleType(), "dcasttmp");
}
LLVMValueRef fn = LLVMGetNamedFunction(module, "pow");
if(fn == NULL){
LLVMTypeRef params[] = {LLVMDoubleType(), LLVMDoubleType()};
LLVMTypeRef ret = LLVMDoubleType();
LLVMTypeRef fType = LLVMFunctionType(ret, params, 2, 0);
fn = LLVMAddFunction(module, "pow", fType);
}
LLVMValueRef ref[] = {left, right};
return LLVMBuildCall(builder, fn, ref, 2, "__algi_internal_pow_res");
}
}
}
case EXPR_REFERENCE:
{
//LLVMValueRef ref = get_variable_ref(e, 0);
return LLVMConstNull(LLVMInt1Type());
}
default: // EXPR_CALL. EXPR_DEFINE will be handled by the statements
{
LLVMValueRef args[e->calex.arity];
for(uint64_t i = 0;i < e->calex.arity;i++){
args[i] = expr_compile(e->calex.args[i], context, builder, module);
}
char *fName = (char *)malloc(e->token.length + 1);
strncpy(fName, e->token.string, e->token.length);
fName[e->token.length] = 0;
LLVMValueRef fn = LLVMGetNamedFunction(module, fName);
return LLVMBuildCall(builder, fn, args, e->calex.arity, "__algi_internal_func_res");
}
}
}
static LLVMValueRef blockstmt_compile(BlockStatement, LLVMBuilderRef, LLVMModuleRef, LLVMContextRef, uint8_t);
static LLVMValueRef statement_compile(Statement *s, LLVMBuilderRef builder, LLVMModuleRef module, LLVMContextRef context){
switch(s->type){
case STATEMENT_SET:
{
LLVMValueRef target = expr_compile(s->sets.target, context, builder, module);
LLVMValueRef value = expr_compile(s->sets.value, context, builder, module);
if(s->sets.value->valueType == VALUE_STR){
if(LLVMGetTypeKind(LLVMTypeOf(value))
== LLVMArrayTypeKind){
size_t length;
LLVMValueRef vRef = LLVMBuildGlobalString(builder, LLVMGetAsString(value, &length), "gString");
LLVMValueRef idx[] = {LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 0, 0)};
//value = LLVMBuildStore(builder, LLVMBuildGEP(builder, vRef, idx, 2, "gStringGEP"), target);
if(s->sets.target->valueType != VALUE_GEN)
return LLVMBuildStore(builder, LLVMBuildGEP(builder, vRef, idx, 2, "gStringGEP"), target);
else
value = vRef;
}
else if(LLVMIsAAllocaInst(value)){
value = LLVMBuildLoad(builder, value, "tmpStringLoad");
}
}
if(s->sets.target->valueType == VALUE_GEN){
if(LLVMIsAAllocaInst(value))
value = LLVMBuildLoad(builder, value, "tmpValLoad");
LLVMTypeRef argType[] = {LLVMPointerType(get_generic_structure_type(), 0),
LLVMInt32Type(), LLVMTypeOf(value)};
LLVMTypeRef fType = LLVMFunctionType(LLVMVoidType(), argType, 2, 1);
LLVMValueRef fn;
if((fn = LLVMGetNamedFunction(module, "__algi_generic_store")) == NULL)
fn = LLVMAddFunction(module, "__algi_generic_store", fType);
runtime_function_used[ALGI_GENERIC_STORE] = 1;
LLVMValueRef r[3];
r[0] = target;
r[1] = LLVMConstInt(LLVMInt32Type(), s->sets.value->valueType, 0);
r[2] = value;
return LLVMBuildCall(builder, fn, r, 3, "");
}
// if(LLVMTypeOf(target) != LLVMTypeOf(value)){
//
// }
return LLVMBuildStore(builder, value, target);
}
break;
case STATEMENT_IF:
{
LLVMValueRef r = expr_compile(s->ifs.condition, context, builder, module);
//r = LLVMBuildFCmp(builder, LLVMRealOEQ, r, LLVMConstReal(LLVMDoubleType(), 0.0), "ifcond");
LLVMValueRef parent = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef thenBB = LLVMAppendBasicBlockInContext(context, parent, "then");
LLVMBasicBlockRef elseBB = LLVMAppendBasicBlock(parent, "else");
LLVMBasicBlockRef mergeBB = LLVMAppendBasicBlock(parent, "ifcont");
LLVMBuildCondBr(builder, r, thenBB, elseBB);
LLVMPositionBuilderAtEnd(builder, thenBB);
blockstmt_compile(s->ifs.thenBlock, builder, module, context, 0);
LLVMBuildBr(builder, mergeBB);
//thenBB = LLVMGetInsertBlock(builder);
//LLVMInsertBasicBlock(elseBB, "else");
LLVMPositionBuilderAtEnd(builder, elseBB);
LLVMValueRef elseV = NULL;
if(s->ifs.elseIf == NULL && s->ifs.elseBlock.count > 0)
elseV = blockstmt_compile(s->ifs.elseBlock, builder, module, context, 0);
else if(s->ifs.elseIf != NULL)
elseV = statement_compile(s->ifs.elseIf, builder, module, context);
LLVMBuildBr(builder, mergeBB);
//elseBB = LLVMGetInsertBlock(builder);
//LLVMInsertBasicBlock(mergeBB, "merge");
LLVMPositionBuilderAtEnd(builder, mergeBB);
//LLVMValueRef phi = LLVMBuildPhi(builder, LLVMDoubleType(), "ifphi");
//LLVMAddIncoming(phi, &thenV, &thenBB, 1);
//if(elseV != NULL)
// LLVMAddIncoming(phi, &elseV, &elseBB, 1);
return LLVMConstInt(LLVMInt1Type(), 0, 0);
}
break;
case STATEMENT_WHILE:
{
LLVMValueRef parent = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef wCond = LLVMAppendBasicBlock(parent, "wcond");
LLVMBuildBr(builder, wCond);
LLVMPositionBuilderAtEnd(builder, wCond);
LLVMValueRef condV = expr_compile(s->whiles.condition, context, builder, module);
LLVMBasicBlockRef wbody = LLVMAppendBasicBlock(parent, "wbody");
LLVMBasicBlockRef cont = LLVMAppendBasicBlock(parent, "wcont");
LLVMBuildCondBr(builder, condV, wbody, cont);
LLVMPositionBuilderAtEnd(builder, wbody);
blockstmt_compile(s->whiles.statements, builder, module, context, 0);
LLVMBuildBr(builder, wCond);
//LLVMBuildCondBr(builder, condV, wbody, cont);
LLVMPositionBuilderAtEnd(builder, cont);
//LLVMBuildRetVoid(builder);
//LLVMBuildBr(builder, parent);
return LLVMConstInt(LLVMInt1Type(), 0, 0);
}
case STATEMENT_DO:
{
LLVMValueRef parent = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder));
LLVMBasicBlockRef dbody = LLVMAppendBasicBlock(parent, "dbody");
LLVMBuildBr(builder, dbody);
LLVMPositionBuilderAtEnd(builder, dbody);
blockstmt_compile(s->dos.statements, builder, module, context, 0);
LLVMValueRef cond = expr_compile(s->dos.condition, context, builder, module);
LLVMBasicBlockRef cont = LLVMAppendBasicBlock(parent, "dcont");
LLVMBuildCondBr(builder, cond, dbody, cont);
LLVMPositionBuilderAtEnd(builder, cont);
return LLVMConstInt(LLVMInt1Type(), 0, 0);
}
case STATEMENT_PRINT:
{
LLVMValueRef params[2];
LLVMValueRef ret;
for(uint64_t i = 0;i < s->prints.count;i++){
LLVMValueRef val = expr_compile(s->prints.args[i], context, builder, module);
LLVMTypeRef valRef = LLVMVoidType();
if(LLVMIsAAllocaInst(val)){
//dbg("It is alloca dude!\n");
valRef = LLVMGetAllocatedType(val);
val = LLVMBuildLoad(builder, val, "valueLoad");
}
else
valRef = LLVMTypeOf(val);
LLVMTypeRef paramTypes[2];
LLVMValueRef idx[] = {LLVMConstInt(LLVMInt32Type(), 0, 0),
LLVMConstInt(LLVMInt32Type(), 0, 0)};
LLVMValueRef fspec;
if(valRef == get_generic_structure_type()){
//fspec = LLVMBuildGlobalString(builder, "%g", "genspec");
LLVMTypeRef params[] = {get_generic_structure_type()};
LLVMTypeRef agvpt = LLVMFunctionType(LLVMVoidType(), params, 1, 0);
LLVMValueRef ref;
if((ref = LLVMGetNamedFunction(module, "__algi_generic_print")) == NULL){
runtime_function_used[ALGI_GENERIC_PRINT] = 1;
ref = LLVMAddFunction(module, "__algi_generic_print", agvpt);
}
LLVMValueRef args[] = {val};
return LLVMBuildCall(builder, ref, args, 1, "");
}
else if(valRef == LLVMInt64Type()){
if((fspec = LLVMGetNamedGlobal(module, "intSpec")) == NULL)
fspec = LLVMBuildGlobalString(builder, "%" PRId64, "intSpec");
paramTypes[1] = LLVMInt64Type();
params[1] = val;
}
else if(valRef == LLVMDoubleType()){
if((fspec = LLVMGetNamedGlobal(module, "floatSpec")) == NULL)
fspec = LLVMBuildGlobalString(builder, "%.10g", "floatSpec");
paramTypes[1] = LLVMDoubleType();
params[1] = val;
}
else{
if((fspec = LLVMGetNamedGlobal(module, "strSpec")) == NULL)
fspec = LLVMBuildGlobalString(builder, "%s", "strSpec");
paramTypes[1] = LLVMPointerType(LLVMInt8Type(), 0);
if(valRef == LLVMPointerType(LLVMInt8Type(), 0))
params[1] = val;
else if(valRef == LLVMInt1Type()){
params[1] = LLVMBuildSelect(builder, val,
LLVMBuildInBoundsGEP(builder, LLVMBuildGlobalString(builder, "True\0", "boolResT"),
idx, 2, "gep"),
LLVMBuildInBoundsGEP(builder, LLVMBuildGlobalString(builder, "False\0", "boolResF"),
idx, 2, "gep"), "boolSelection");
//params[1] = LLVMBuildInBoundsGEP(builder, params[1], idx, 2, "str");
}
else {
size_t length;
const char *str = LLVMGetAsString(val, &length);
params[1] = LLVMBuildGlobalString(builder, str, "pString");
params[1] = LLVMBuildInBoundsGEP(builder, params[1], idx, 2, "str");
}
}
params[0] = LLVMBuildInBoundsGEP(builder, fspec, idx, 2, "gep");
paramTypes[0] = LLVMPointerType(LLVMInt8Type(), 0);
LLVMTypeRef ftype = LLVMFunctionType(LLVMInt32Type(), paramTypes, 1, 1);
LLVMValueRef pf;
if((pf = LLVMGetNamedFunction(module, "printf")) == NULL)
pf = LLVMAddFunction(module, "printf", ftype);
//dbg("builder : %p\n");
//dbg("params : %p\n", params);
//dbg("pf : %p\n", pf);
ret = LLVMBuildCall(builder, pf, params, 2, "pftemp");
}
return ret;
}
break;
default:
return LLVMConstInt(LLVMInt1Type(), 1, 0);
}
}
static LLVMValueRef func;
static LLVMValueRef blockstmt_compile(BlockStatement s, LLVMBuilderRef builder, LLVMModuleRef module,
LLVMContextRef context, uint8_t insertBlock){
LLVMBasicBlockRef bbr;
LLVMBasicBlockRef bb;
if(insertBlock){
bbr = LLVMGetInsertBlock(builder);
if(bbr != NULL){
dbg("GetInsertBlock : %p\n", bbr);
LLVMValueRef bbp = LLVMGetBasicBlockParent(bbr);
dbg("GetBasicBlockParent : %p\n", bbp);
bb = LLVMAppendBasicBlock(
bbp, "block");
}
else{
dbg("Creating new block");
bb = LLVMAppendBasicBlock(func, "block");
}
LLVMPositionBuilderAtEnd(builder, bb);
}
LLVMValueRef ret;
for(uint64_t i = 0;i < s.count;i++){
ret = statement_compile(s.statements[i], builder, module, context);
}
if(insertBlock){
LLVMBuildRetVoid(builder);
return LLVMBasicBlockAsValue(bb);
}
else
return ret;
}
static LLVMExecutionEngineRef globalEngine;
static LLVMBuilderRef globalBuilder;
static LLVMContextRef globalContext;
static void algi_show_err(const char *msg){
err("%s", msg);
}
void codegen_compile(BlockStatement bs){
LLVMModuleRef globalModule = LLVMModuleCreateWithName("AlgiModule");
LLVMTypeRef ret_type = LLVMFunctionType(LLVMVoidType(), NULL, 0, 0);
func = LLVMAddFunction(globalModule, "Main", ret_type);
globalBuilder = LLVMCreateBuilder();
globalContext = LLVMContextCreate();
timer_start("Compilation");
blockstmt_compile(bs, globalBuilder, globalModule, globalContext, 1);
timer_end();
char *err = NULL;
dbg("Compiled code\n");
LLVMDumpModule(globalModule);
int hasErr = LLVMVerifyModule(globalModule, LLVMReturnStatusAction, &err);
if(hasErr)
algi_show_err(err);
LLVMDisposeMessage(err);
if(hasErr){
#ifdef DEBUG
err("Press C to abort : ");
char c = getc(stdin);
if(c == 'c' || c == 'C')
#endif
exit(EXIT_FAILURE);
}
err = NULL;
timer_start("Initializing JIT");
LLVMLinkInMCJIT();
LLVMInitializeNativeTarget();
LLVMInitializeNativeAsmParser();
LLVMInitializeNativeAsmPrinter();
if(LLVMCreateExecutionEngineForModule(&globalEngine, globalModule, &err) != 0){
printf("\nFailed to create execution engine!\n");
abort();
}
if(err){
printf("\nError : %s\n", err);
LLVMDisposeMessage(err);
exit(EXIT_FAILURE);
}
// Map Algi runtime functions
for(uint64_t i = 0;i < ALGI_RUNTIME_FUNCTION_COUNT;i++){
if(runtime_function_used[i]){
LLVMAddGlobalMapping(globalEngine, LLVMGetNamedFunction(globalModule, algi_runtime_functions[i].name),
algi_runtime_functions[i].address);
}
}
LLVMSetModuleDataLayout(globalModule, LLVMGetExecutionEngineTargetData(globalEngine));
timer_end();
timer_start("Optimization");
// Optimization
LLVMPassManagerBuilderRef pmb = LLVMPassManagerBuilderCreate();
LLVMPassManagerBuilderSetOptLevel(pmb, 2);
LLVMPassManagerRef optimizer = LLVMCreatePassManager();
LLVMPassManagerBuilderPopulateModulePassManager(pmb, optimizer);
LLVMRunPassManager(optimizer, globalModule);
timer_end();
LLVMDisposePassManager(optimizer);
LLVMPassManagerBuilderDispose(pmb);
#ifdef DEBUG
dbg("Optimized code \n");
LLVMDumpModule(globalModule);
dbg("Press any key to run the program");
dbg("Press C to cancel : ");
char c = getc(stdin);
if(c != 'c' && c != 'C'){
#endif
void(*Main)(void) = (void (*)(void))LLVMGetFunctionAddress(globalEngine, "Main");
#ifdef DEBUG
timer_start("Execution");
#endif
Main();
#ifdef DEBUG
timer_end();
}
else
warn("Run cancelled!");
#endif
}
void codegen_dispose(){
if(variables != NULL)
free(variables);
LLVMDisposeBuilder(globalBuilder);
if(globalEngine != NULL){
LLVMRunStaticDestructors(globalEngine);
LLVMDisposeExecutionEngine(globalEngine);
}
LLVMShutdown();
LLVMContextDispose(globalContext);
}
void codegen_llvm_shutdown(){
LLVMShutdown();
}