-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackend.c
2324 lines (1910 loc) · 70.4 KB
/
backend.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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "environment.h"
#include "exception.h"
#include "environment.h"
#include "backend.h"
#include "unify.h"
#include "ast.h"
#include "gc.h"
#include <llvm-c/Core.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/ExecutionEngine.h>
#include <llvm-c/Target.h>
#include <llvm-c/Transforms/Scalar.h>
#ifndef CS_MALLOC_NAME /* make it easy to turn off GC */
#define CS_MALLOC_NAME "GC_malloc"
#define CS_MALLOC_NAME2 "GC_malloc_atomic"
#endif
/*
Tell LLVM about some external library functions so we can call them
and about some constants we want to use from jit'd code
*/
void llvm_functions(jit_t * jit)
{
LLVMTypeRef args[2];
LLVMTypeRef fntype;
LLVMTypeRef ret;
LLVMValueRef fn;
/* patch in the printf function */
args[0] = LLVMPointerType(LLVMInt8Type(), 0);
ret = LLVMWordType();
fntype = LLVMFunctionType(ret, args, 1, 1);
fn = LLVMAddFunction(jit->module, "printf", fntype);
/* patch in the exit function */
args[0] = LLVMWordType();
ret = LLVMVoidType();
fntype = LLVMFunctionType(ret, args, 1, 0);
fn = LLVMAddFunction(jit->module, "exit", fntype);
/* patch in the GC_malloc function */
args[0] = LLVMWordType();
ret = LLVMPointerType(LLVMInt8Type(), 0);
fntype = LLVMFunctionType(ret, args, 1, 0);
fn = LLVMAddFunction(jit->module, CS_MALLOC_NAME, fntype);
LLVMAddFunctionAttr(fn, LLVMNoAliasAttribute);
/* patch in the GC_malloc_atomic function */
args[0] = LLVMWordType();
ret = LLVMPointerType(LLVMInt8Type(), 0);
fntype = LLVMFunctionType(ret, args, 1, 0);
fn = LLVMAddFunction(jit->module, CS_MALLOC_NAME2, fntype);
LLVMAddFunctionAttr(fn, LLVMNoAliasAttribute);
}
/* count parameters as represented by %'s in format string */
int count_params(const char * fmt)
{
int len = strlen(fmt);
int i, count = 0;
for (i = 0; i < len - 1; i++)
if (fmt[i] == '%')
if (fmt[i + 1] == '%')
i++;
else
count++;
return count;
}
/*
When an expression is evaluated it is a pain to pass it back out of
jit'd code and print the value returned by the expression. So we
print it on the jit side. So this is our runtime printf for
printing LLVMValRef's from jit'd code.
*/
void llvm_printf(jit_t * jit, const char * fmt, ...)
{
int i, count = count_params(fmt);
va_list ap;
/* get printf function */
LLVMValueRef fn = LLVMGetNamedFunction(jit->module, "printf");
LLVMSetFunctionCallConv(fn, LLVMCCallConv);
/* Add a global variable for format string */
LLVMValueRef str = LLVMConstString(fmt, strlen(fmt), 0);
jit->fmt_str = LLVMAddGlobal(jit->module, LLVMTypeOf(str), "fmt");
LLVMSetInitializer(jit->fmt_str, str);
LLVMSetGlobalConstant(jit->fmt_str, 1);
LLVMSetLinkage(jit->fmt_str, LLVMInternalLinkage);
/* build variadic parameter list for printf */
LLVMValueRef indices[2] = { LLVMConstInt(LLVMWordType(), 0, 0), LLVMConstInt(LLVMWordType(), 0, 0) };
LLVMValueRef GEP = LLVMBuildGEP(jit->builder, jit->fmt_str, indices, 2, "str");
LLVMValueRef args[count + 1];
args[0] = GEP;
va_start(ap, fmt);
for (i = 0; i < count; i++)
args[i + 1] = va_arg(ap, LLVMValueRef);
va_end(ap);
/* build call to printf */
LLVMValueRef call_printf = LLVMBuildCall(jit->builder, fn, args, count + 1, "printf");
LLVMSetTailCall(call_printf, 1);
LLVMAddInstrAttribute(call_printf, 0, LLVMNoUnwindAttribute);
LLVMAddInstrAttribute(call_printf, 1, LLVMNoAliasAttribute);
}
/*
Printing booleans requires special attention. We want to print
the strings "true" and "false". To do this from the jit side we
need this function.
*/
void llvm_printbool(jit_t * jit, LLVMValueRef obj)
{
/* jit an if statement which checks for true/false */
LLVMBasicBlockRef i;
LLVMBasicBlockRef b1;
LLVMBasicBlockRef b2;
LLVMBasicBlockRef e;
i = LLVMAppendBasicBlock(jit->function, "if");
b1 = LLVMAppendBasicBlock(jit->function, "ifbody");
b2 = LLVMAppendBasicBlock(jit->function, "ebody");
e = LLVMAppendBasicBlock(jit->function, "ifend");
LLVMBuildBr(jit->builder, i);
LLVMPositionBuilderAtEnd(jit->builder, i);
LLVMBuildCondBr(jit->builder, obj, b1, b2);
LLVMPositionBuilderAtEnd(jit->builder, b1);
/* print "true" */
llvm_printf(jit, "%s", jit->true_str);
LLVMBuildBr(jit->builder, e);
LLVMPositionBuilderAtEnd(jit->builder, b2);
/* print "false" */
llvm_printf(jit, "%s", jit->false_str);
LLVMBuildBr(jit->builder, e);
LLVMPositionBuilderAtEnd(jit->builder, e);
}
/*
Jits a print of a tuple
*/
void print_tuple(jit_t * jit, type_t * type, LLVMValueRef obj)
{
int count = type->arity;
int i;
llvm_printf(jit, "(");
/* print comma separated list of params */
for (i = 0; i < count - 1; i++)
{
LLVMValueRef index[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), i, 0) };
LLVMValueRef p = LLVMBuildInBoundsGEP(jit->builder, obj, index, 2, "tuple");
p = LLVMBuildLoad(jit->builder, p, "entry");
print_obj(jit, type->param[i], p);
llvm_printf(jit, ", ");
}
/* print final param */
LLVMValueRef index[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), i, 0) };
LLVMValueRef p = LLVMBuildInBoundsGEP(jit->builder, obj, index, 2, "tuple");
p = LLVMBuildLoad(jit->builder, p, "entry");
print_obj(jit, type->param[i], p);
llvm_printf(jit, ")");
}
/*
This jits a printf for various cesium types. We use it to print
the result of expressions that are evaluated, before returning from
a jit'd expression.
*/
void print_obj(jit_t * jit, type_t * type, LLVMValueRef obj)
{
switch (type->typ)
{
case INT:
llvm_printf(jit, "%ld", obj);
break;
case CHAR:
llvm_printf(jit, "%c", obj);
break;
case DOUBLE:
llvm_printf(jit, "%.5g", obj);
break;
case STRING:
llvm_printf(jit, "\"%s\"", obj);
break;
case BOOL:
llvm_printbool(jit, obj);
break;
case FN:
case LAMBDA:
case DATATYPE:
case ARRAY:
case NIL:
llvm_printf(jit, "%s", jit->nil_str);
break;
case TUPLE:
print_tuple(jit, type, obj);
break;
}
}
/*
Initialise the LLVM JIT
*/
jit_t * llvm_init(void)
{
char * error = NULL;
/* create jit struct */
jit_t * jit = (jit_t *) GC_MALLOC(sizeof(jit_t));
/* Jit setup */
LLVMLinkInJIT();
LLVMInitializeNativeTarget();
/* Create module */
jit->module = LLVMModuleCreateWithName("cesium");
/* Create JIT engine */
if (LLVMCreateJITCompilerForModule(&(jit->engine), jit->module, 2, &error) != 0)
{
fprintf(stderr, "%s\n", error);
LLVMDisposeMessage(error);
abort();
}
/* Create optimisation pass pipeline */
jit->pass = LLVMCreateFunctionPassManagerForModule(jit->module);
LLVMAddTargetData(LLVMGetExecutionEngineTargetData(jit->engine), jit->pass);
LLVMAddAggressiveDCEPass(jit->pass); /* */
LLVMAddDeadStoreEliminationPass(jit->pass);
LLVMAddIndVarSimplifyPass(jit->pass);
LLVMAddJumpThreadingPass(jit->pass);
LLVMAddLICMPass(jit->pass);
LLVMAddLoopDeletionPass(jit->pass);
LLVMAddLoopRotatePass(jit->pass);
LLVMAddLoopUnrollPass(jit->pass);
LLVMAddLoopUnswitchPass(jit->pass);
LLVMAddMemCpyOptPass(jit->pass);
LLVMAddReassociatePass(jit->pass);
LLVMAddSCCPPass(jit->pass);
LLVMAddScalarReplAggregatesPass(jit->pass);
LLVMAddSimplifyLibCallsPass(jit->pass);
LLVMAddTailCallEliminationPass(jit->pass);
LLVMAddDemoteMemoryToRegisterPass(jit->pass); /* */
LLVMAddConstantPropagationPass(jit->pass);
LLVMAddInstructionCombiningPass(jit->pass);
LLVMAddPromoteMemoryToRegisterPass(jit->pass);
LLVMAddGVNPass(jit->pass);
LLVMAddCFGSimplificationPass(jit->pass);
/* link in external functions callable from jit'd code */
llvm_functions(jit);
/* initialise some strings */
START_EXEC;
jit->fmt_str = NULL;
jit->true_str = LLVMBuildGlobalStringPtr(jit->builder, "true", "true");
jit->false_str = LLVMBuildGlobalStringPtr(jit->builder, "false", "false");
jit->nil_str = LLVMBuildGlobalStringPtr(jit->builder, "nil", "nil");
END_EXEC;
return jit;
}
/*
If something goes wrong after partially jit'ing something we need
to clean up.
*/
void llvm_reset(jit_t * jit)
{
LLVMDeleteFunction(jit->function);
LLVMDisposeBuilder(jit->builder);
jit->function = NULL;
jit->builder = NULL;
jit->breakto = NULL;
}
/*
Clean up LLVM on exit from Cesium
*/
void llvm_cleanup(jit_t * jit)
{
/* Clean up */
LLVMDisposePassManager(jit->pass);
LLVMDisposeExecutionEngine(jit->engine);
jit->pass = NULL;
jit->engine = NULL;
jit->module = NULL;
jit->fmt_str = NULL;
jit->nil_str = NULL;
jit->true_str = NULL;
jit->false_str = NULL;
}
int is_atomic(type_t * type)
{
typ_t typ = type->typ;
return (typ != ARRAY && typ != TUPLE && typ != DATATYPE && typ != FN && typ != LAMBDA);
}
/*
Jit a call to GC_malloc
*/
LLVMValueRef LLVMBuildGCMalloc(jit_t * jit, LLVMTypeRef type, const char * name, int atomic)
{
LLVMValueRef fn;
if (atomic)
fn = LLVMGetNamedFunction(jit->module, CS_MALLOC_NAME2);
else
fn = LLVMGetNamedFunction(jit->module, CS_MALLOC_NAME);
LLVMValueRef arg[1] = { LLVMSizeOf(type) };
LLVMValueRef gcmalloc = LLVMBuildCall(jit->builder, fn, arg, 1, "malloc");
return LLVMBuildPointerCast(jit->builder, gcmalloc, LLVMPointerType(type, 0), name);
}
/*
Jit a call to GC_malloc to create an array
*/
LLVMValueRef LLVMBuildGCArrayMalloc(jit_t * jit, LLVMTypeRef type, LLVMValueRef num, const char * name, int atomic)
{
LLVMValueRef fn;
if (atomic)
fn = LLVMGetNamedFunction(jit->module, CS_MALLOC_NAME2);
else
fn = LLVMGetNamedFunction(jit->module, CS_MALLOC_NAME);
LLVMValueRef size = LLVMSizeOf(type);
LLVMValueRef arg[1] = { LLVMBuildMul(jit->builder, num, size, "arr_size") };
LLVMValueRef gcmalloc = LLVMBuildCall(jit->builder, fn, arg, 1, "malloc");
return LLVMBuildPointerCast(jit->builder, gcmalloc, LLVMPointerType(type, 0), name);
}
/* Build llvm lambda fn type from ordinary function type */
LLVMTypeRef lambda_fn_type(jit_t * jit, type_t * type)
{
int params = type->arity;
int i;
/* get parameter types, one extra for the environment struct */
LLVMTypeRef * args = (LLVMTypeRef *) GC_MALLOC((params + 1)*sizeof(LLVMTypeRef));
for (i = 0; i < params; i++)
args[i] = type_to_llvm(jit, type->param[i]);
/* we'll cast the environment struct to a pointer */
args[params] = LLVMPointerType(LLVMInt8Type(), 0);
/* get return type */
LLVMTypeRef ret = type_to_llvm(jit, type->ret);
/* make LLVM function type */
return LLVMFunctionType(ret, args, params + 1, 0);
}
/*
Make an llvm lambda struct type from an ordinary function type
*/
LLVMTypeRef lambda_type(jit_t * jit, type_t * type)
{
LLVMTypeRef fn_ty = lambda_fn_type(jit, type);
/* get lambda struct element types */
LLVMTypeRef * str_ty = (LLVMTypeRef *) GC_MALLOC(2*sizeof(LLVMTypeRef));
str_ty[0] = LLVMPointerType(fn_ty, 0);
str_ty[1] = LLVMPointerType(LLVMInt8Type(), 0);
return LLVMStructType(str_ty, 2, 1);
}
/* Build llvm struct type from ordinary tuple type */
LLVMTypeRef tup_type(jit_t * jit, type_t * type)
{
int params = type->arity;
int i;
/* get parameter types */
LLVMTypeRef * args = (LLVMTypeRef *) GC_MALLOC(params*sizeof(LLVMTypeRef));
for (i = 0; i < params; i++)
args[i] = type_to_llvm(jit, type->param[i]);
/* make LLVM struct type */
return LLVMStructType(args, params, 1);
}
/* Build llvm struct type for array type */
LLVMTypeRef arr_type(jit_t * jit, type_t * type)
{
int i;
/* get parameter types */
LLVMTypeRef * args = (LLVMTypeRef *) GC_MALLOC(2*sizeof(LLVMTypeRef));
args[0] = LLVMPointerType(type_to_llvm(jit, type->ret), 0);
args[1] = LLVMWordType();
/* make LLVM struct type */
return LLVMStructType(args, 2, 1);
}
/* Convert a type to an LLVMTypeRef */
LLVMTypeRef type_to_llvm(jit_t * jit, type_t * type)
{
int i;
if (type == t_double)
return LLVMDoubleType();
else if (type == t_int)
return LLVMWordType();
else if (type == t_bool)
return LLVMInt1Type();
else if (type == t_string)
return LLVMPointerType(LLVMInt8Type(), 0);
else if (type == t_char)
return LLVMInt8Type();
else if (type == t_nil)
return LLVMVoidType();
else if (type->typ == FN)
{
int params = type->arity;
/* get parameter types */
LLVMTypeRef * args = (LLVMTypeRef *) GC_MALLOC(params*sizeof(LLVMTypeRef));
for (i = 0; i < params; i++)
args[i] = type_to_llvm(jit, type->param[i]);
/* get return type */
LLVMTypeRef ret = type_to_llvm(jit, type->ret);
/* make LLVM function type */
return LLVMPointerType(LLVMFunctionType(ret, args, params, 0), 0);
} else if (type->typ == LAMBDA)
return LLVMPointerType(lambda_type(jit, type), 0);
else if (type->typ == TUPLE || type->typ == DATATYPE)
return LLVMPointerType(tup_type(jit, type), 0);
else if (type->typ == ARRAY)
return LLVMPointerType(arr_type(jit, type), 0);
else if (type->typ == TYPEVAR)
jit_exception(jit, "Unable to infer types\n");
else
jit_exception(jit, "Internal error: unknown type in type_to_llvm\n");
}
/*
Jit an int literal
*/
int exec_int(jit_t * jit, ast_t * ast)
{
long num = atol(ast->sym->name);
ast->val = LLVMConstInt(LLVMWordType(), num, 0);
return 0;
}
/*
Jit a double literal
*/
int exec_double(jit_t * jit, ast_t * ast)
{
double num = atof(ast->sym->name);
ast->val = LLVMConstReal(LLVMDoubleType(), num);
return 0;
}
/*
Jit a bool
*/
int exec_bool(jit_t * jit, ast_t * ast)
{
if (strcmp(ast->sym->name, "true") == 0)
ast->val = LLVMConstInt(LLVMInt1Type(), 1, 0);
else
ast->val = LLVMConstInt(LLVMInt1Type(), 0, 0);
return 0;
}
/*
Jit a string literali, being careful to replace special
characters with their ascii equivalent
*/
int exec_string(jit_t * jit, ast_t * ast)
{
char * name = ast->sym->name;
if (ast->sym->val == NULL)
{
int length = strlen(name) - 2;
int i, j, bs = 0;
for (i = 0; i < length; i++)
{
if (name[i + 1] == '\\')
{
bs++;
i++;
}
}
char * str = (char *) GC_MALLOC(length + 1 - bs);
for (i = 0, j = 0; i < length; i++, j++)
{
if (name[i + 1] == '\\')
{
switch (name[i + 2])
{
case '0':
str[j] = '\0';
break;
case '\\':
str[j] = '\\';
break;
case 'n':
str[j] = '\n';
break;
case 'r':
str[j] = '\r';
break;
case 't':
str[j] = '\t';
break;
default:
str[j] = name[i + 2];
}
i++;
} else
str[j] = name[i + 1];
}
length = length - bs;
str[length] = '\0';
ast->sym->val = LLVMBuildGlobalStringPtr(jit->builder, str, "string");
}
ast->val = ast->sym->val;
return 0;
}
/*
We have a number of unary ops we want to jit and they
all look the same, so define macros for them.
*/
#define exec_unary(__name, __fop, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
\
exec_ast(jit, expr1); \
\
LLVMValueRef v1 = expr1->val; \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, v1, __str); \
else \
ast->val = __iop(jit->builder, v1, __str); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_unary1(__name, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
\
exec_ast(jit, expr1); \
\
LLVMValueRef v1 = expr1->val; \
\
ast->val = __iop(jit->builder, v1, __str); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_unary_pre(__name, __fop, __iop, __c1, __c2, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
\
exec_place(jit, expr1); \
\
LLVMValueRef v1 = LLVMBuildLoad(jit->builder, \
expr1->val, expr1->sym->name); \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, v1, \
LLVMConstReal(LLVMDoubleType(), __c1), __str); \
else \
ast->val = __iop(jit->builder, v1, \
LLVMConstInt(LLVMWordType(), __c2, 0), __str); \
LLVMBuildStore(jit->builder, ast->val, expr1->val); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_unary_post(__name, __fop, __iop, __c1, __c2, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
\
exec_place(jit, expr1); \
\
LLVMValueRef v1 = LLVMBuildLoad(jit->builder, \
expr1->val, expr1->sym->name); \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, v1, \
LLVMConstReal(LLVMDoubleType(), __c1), __str); \
else \
ast->val = __iop(jit->builder, v1, \
LLVMConstInt(LLVMWordType(), __c2, 0), __str); \
LLVMBuildStore(jit->builder, ast->val, expr1->val); \
\
ast->type = expr1->type; \
ast->val = v1; \
\
return 0; \
}
/* Jit !, ~, -, ... unary ops */
int exec_unary(exec_unminus, LLVMBuildFNeg, LLVMBuildNeg, "unary-minus")
int exec_unary1(exec_lognot, LLVMBuildNot, "log-not")
int exec_unary1(exec_bitnot, LLVMBuildNot, "bit-not")
int exec_unary_pre(exec_preinc, LLVMBuildFAdd, LLVMBuildAdd, 1.0, 1, "pre-inc")
int exec_unary_pre(exec_predec, LLVMBuildFSub, LLVMBuildSub, 1.0, 1, "pre-dec")
int exec_unary_post(exec_postinc, LLVMBuildFAdd, LLVMBuildAdd, 1.0, 1, "post-inc")
int exec_unary_post(exec_postdec, LLVMBuildFSub, LLVMBuildSub, 1.0, 1, "post-dec")
/*
We have a number of binary ops we want to jit and they
all look the same, so define macros for them.
*/
#define exec_binary(__name, __fop, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
ast_t * expr2 = expr1->next; \
\
exec_ast(jit, expr1); \
exec_ast(jit, expr2); \
\
LLVMValueRef v1 = expr1->val, v2 = expr2->val; \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, v1, v2, __str); \
else \
ast->val = __iop(jit->builder, v1, v2, __str); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_binary_rel(__name, __fop, __frel, __iop, __irel, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
ast_t * expr2 = expr1->next; \
\
exec_ast(jit, expr1); \
exec_ast(jit, expr2); \
\
LLVMValueRef v1 = expr1->val, v2 = expr2->val; \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, __frel, v1, v2, __str); \
else \
ast->val = __iop(jit->builder, __irel, v1, v2, __str); \
\
return 0; \
}
#define exec_binary1(__name, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
ast_t * expr2 = expr1->next; \
\
exec_ast(jit, expr1); \
exec_ast(jit, expr2); \
\
LLVMValueRef v1 = expr1->val, v2 = expr2->val; \
\
ast->val = __iop(jit->builder, v1, v2, __str); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_binary_pre(__name, __fop, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
ast_t * expr2 = ast->child->next; \
\
exec_place(jit, expr1); \
exec_ast(jit, expr2); \
\
LLVMValueRef v1 = LLVMBuildLoad(jit->builder, \
expr1->val, expr1->sym->name); \
\
if (expr1->type == t_double) \
ast->val = __fop(jit->builder, v1, expr2->val, __str); \
else \
ast->val = __iop(jit->builder, v1, expr2->val, __str); \
LLVMBuildStore(jit->builder, ast->val, expr1->val); \
\
ast->type = expr1->type; \
\
return 0; \
}
#define exec_binary_pre1(__name, __iop, __str) \
__name(jit_t * jit, ast_t * ast) \
{ \
ast_t * expr1 = ast->child; \
ast_t * expr2 = ast->child->next; \
\
exec_place(jit, expr1); \
exec_ast(jit, expr2); \
\
LLVMValueRef v1 = LLVMBuildLoad(jit->builder, \
expr1->val, expr1->sym->name); \
\
ast->val = __iop(jit->builder, v1, expr2->val, __str); \
LLVMBuildStore(jit->builder, ast->val, expr1->val); \
\
ast->type = expr1->type; \
\
return 0; \
}
/* Jit add, sub, .... ops */
int exec_binary(exec_plus, LLVMBuildFAdd, LLVMBuildAdd, "add")
int exec_binary(exec_minus, LLVMBuildFSub, LLVMBuildSub, "sub")
int exec_binary(exec_times, LLVMBuildFMul, LLVMBuildMul, "times")
int exec_binary(exec_div, LLVMBuildFDiv, LLVMBuildSDiv, "div")
int exec_binary(exec_mod, LLVMBuildFRem, LLVMBuildSRem, "mod")
int exec_binary1(exec_lsh, LLVMBuildShl, "lsh")
int exec_binary1(exec_rsh, LLVMBuildAShr, "rsh")
int exec_binary1(exec_bitor, LLVMBuildOr, "bitor")
int exec_binary1(exec_bitand, LLVMBuildAnd, "bitand")
int exec_binary1(exec_bitxor, LLVMBuildXor, "bitxor")
int exec_binary1(exec_logand, LLVMBuildAnd, "logand")
int exec_binary1(exec_logor, LLVMBuildOr, "logor")
int exec_binary_rel(exec_le, LLVMBuildFCmp, LLVMRealOLE, LLVMBuildICmp, LLVMIntSLE, "le")
int exec_binary_rel(exec_ge, LLVMBuildFCmp, LLVMRealOGE, LLVMBuildICmp, LLVMIntSGE, "ge")
int exec_binary_rel(exec_lt, LLVMBuildFCmp, LLVMRealOLT, LLVMBuildICmp, LLVMIntSLT, "lt")
int exec_binary_rel(exec_gt, LLVMBuildFCmp, LLVMRealOGT, LLVMBuildICmp, LLVMIntSGT, "gt")
int exec_binary_rel(exec_eq, LLVMBuildFCmp, LLVMRealOEQ, LLVMBuildICmp, LLVMIntEQ, "eq")
int exec_binary_rel(exec_ne, LLVMBuildFCmp, LLVMRealONE, LLVMBuildICmp, LLVMIntNE, "ne")
int exec_binary_pre(exec_pluseq, LLVMBuildFAdd, LLVMBuildAdd, "pluseq")
int exec_binary_pre(exec_minuseq, LLVMBuildFSub, LLVMBuildSub, "minuseq")
int exec_binary_pre(exec_timeseq, LLVMBuildFMul, LLVMBuildMul, "timeseq")
int exec_binary_pre(exec_diveq, LLVMBuildFDiv, LLVMBuildSDiv, "diveq")
int exec_binary_pre(exec_modeq, LLVMBuildFRem, LLVMBuildSRem, "modeq")
int exec_binary_pre1(exec_andeq, LLVMBuildAnd, "andeq")
int exec_binary_pre1(exec_oreq, LLVMBuildOr, "oreq")
int exec_binary_pre1(exec_xoreq, LLVMBuildXor, "xoreq")
int exec_binary_pre1(exec_lsheq, LLVMBuildShl, "lsheq")
int exec_binary_pre1(exec_rsheq, LLVMBuildAShr, "rsheq")
/*
Given a function make a lambda function (i.e. function with same
params and an exta one for an env) which calls it
*/
LLVMValueRef make_fn_lambda(jit_t * jit,
LLVMValueRef fn, LLVMTypeRef fn_type)
{
/* make llvm function object */
LLVMValueRef fn_res = LLVMAddFunction(jit->module, "lambda", fn_type);
/* jit setup */
LLVMBuilderRef build_res = LLVMCreateBuilder();
/* first basic block */
LLVMBasicBlockRef entry = LLVMAppendBasicBlock(fn_res, "entry");
LLVMPositionBuilderAtEnd(build_res, entry);
/* make space for arguments */
int count = LLVMCountParams(fn);
LLVMValueRef * args = (LLVMValueRef *) GC_MALLOC(count*sizeof(LLVMValueRef));
/* load arguments */
int i = 0;
for (i = 0; i < count; i++)
args[i] = LLVMGetParam(fn_res, i);
/* call function and return value */
LLVMValueRef ret = LLVMBuildCall(build_res, fn, args, count, "");
LLVMBuildRet(build_res, ret);
/* run the pass manager on the jit'd function */
LLVMRunFunctionPassManager(jit->pass, fn_res);
/* clean up */
LLVMDisposeBuilder(build_res);
return fn_res;
}
/*
Load value of identifier
*/
int exec_ident(jit_t * jit, ast_t * ast)
{
if (ast->tag == AST_SLOT) /* deal specially with slots */
return exec_slot(jit, ast);
bind_t * bind = ast->bind;
if (bind->val != NULL) /* we've already got a value and thus a type */
ast->type = bind->type;
else /* substitute the inferred type and update the binding type */
{
subst_type(&ast->type);
bind->type = ast->type;
}
if (ast->type->typ == ARRAY && ast->type->arity != 0)
{
LLVMTypeRef str_ty = arr_type(jit, ast->type);
LLVMValueRef val = LLVMBuildGCMalloc(jit, str_ty, "tuple_s", 0);
/* insert length into array struct */
LLVMValueRef indices[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 1, 0) };
LLVMValueRef entry = LLVMBuildInBoundsGEP(jit->builder, val, indices, 2, "length");
LLVMBuildStore(jit->builder, LLVMConstInt(LLVMWordType(), ast->type->arity, 0), entry);
/* create array */
int atomic = is_atomic(ast->type->ret);
LLVMValueRef arr = LLVMBuildGCArrayMalloc(jit, type_to_llvm(jit, ast->type->ret), LLVMConstInt(LLVMWordType(), (long) ast->type->arity, 0), "array", atomic);
LLVMValueRef indices2[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 0, 0) };
entry = LLVMBuildInBoundsGEP(jit->builder, val, indices2, 2, "arr");
LLVMBuildStore(jit->builder, arr, entry);
ast->type->arity = 0;
ast->bind->initialised = 0;
exec_place(jit, ast);
LLVMBuildStore(jit->builder, val, ast->val);
ast->bind->initialised = 1;
}
/* if the id is a datatype constructor we do nothing */
if (ast->type->typ == DATATYPE && bind->val == NULL)
return 0;
/* if it's not an LLVM function just load the value */
else if ((ast->type->typ != FN && ast->type->typ != LAMBDA) || bind->ast == NULL)
ast->val = LLVMBuildLoad(jit->builder, bind->val, bind->sym->name);
else if (bind->val != NULL) /* if the function has been jit'd, load that */
ast->val = bind->val;
else if (ast->type->typ == FN || ast->type->typ == LAMBDA)/* jit the fn, update the binding and load it */
{
exec_fndef(jit, bind->ast);
ast->val = bind->val;
}
return 0;
}
/*
Jit a variable declaration
*/
int exec_decl(jit_t * jit, ast_t * ast)
{
bind_t * bind = ast->bind;
subst_type(&bind->type); /* fill in the type */
if (bind->type->typ != TYPEVAR) /* if we now know what it is */
{
LLVMTypeRef type = type_to_llvm(jit, bind->type); /* convert to llvm type */
if (scope_is_global(bind)) /* variable is global */
{
bind->val = LLVMAddGlobal(jit->module, type, bind->sym->name);
LLVMSetInitializer(bind->val, LLVMGetUndef(type));
} else /* variable is local */
bind->val = LLVMBuildAlloca(jit->builder, type, bind->sym->name);
}
ast->type = bind->type;
ast->val = bind->val;
return 0;
}
/*
Jit a slot access
*/
int exec_lslot(jit_t * jit, ast_t * ast)
{
ast_t * id = ast->child;
ast_t * p = id->next;
int i, params = id->type->arity;
if (ast->sym == NULL) /* need some kind of name */
ast->sym = sym_lookup("none");
exec_ast(jit, id);
for (i = 0; i < params; i++)
{
if (id->type->slot[i] == p->sym)
break;
}
/* get slot from datatype */
LLVMValueRef indices[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), i, 0) };
ast->val = LLVMBuildInBoundsGEP(jit->builder, id->val, indices, 2, p->sym->name);
ast->type = id->type->param[i];
return 0;
}
/*
Jit an array location access
*/
int exec_llocation(jit_t * jit, ast_t * ast)
{
ast_t * id = ast->child;
ast_t * p = id->next;
int i;
if (ast->sym == NULL) /* need some kind of name */
ast->sym = sym_lookup("none");
exec_ast(jit, id);
exec_ast(jit, p);
/* get array from datatype */
LLVMValueRef indices[2] = { LLVMConstInt(LLVMInt32Type(), 0, 0), LLVMConstInt(LLVMInt32Type(), 0, 0) };
ast->val = LLVMBuildInBoundsGEP(jit->builder, id->val, indices, 2, "arr");
ast->val = LLVMBuildLoad(jit->builder, ast->val, "array");