-
Notifications
You must be signed in to change notification settings - Fork 4
/
sparse-llvm.c
1171 lines (963 loc) · 26.9 KB
/
sparse-llvm.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
/*
* Example usage:
* ./sparse-llvm hello.c | llc | as -o hello.o
*/
#include <llvm-c/Core.h>
#include <llvm-c/BitWriter.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/Target.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "symbol.h"
#include "expression.h"
#include "linearize.h"
#include "flow.h"
struct function {
LLVMBuilderRef builder;
LLVMTypeRef type;
LLVMValueRef fn;
LLVMModuleRef module;
};
static inline bool symbol_is_fp_type(struct symbol *sym)
{
if (!sym)
return false;
return sym->ctype.base_type == &fp_type;
}
static LLVMTypeRef symbol_type(LLVMModuleRef module, struct symbol *sym);
static LLVMTypeRef func_return_type(LLVMModuleRef module, struct symbol *sym)
{
return symbol_type(module, sym->ctype.base_type);
}
static LLVMTypeRef sym_func_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef *arg_type;
LLVMTypeRef func_type;
LLVMTypeRef ret_type;
struct symbol *arg;
int n_arg = 0;
/* to avoid strangeness with varargs [for now], we build
* the function and type anew, for each call. This
* is probably wrong. We should look up the
* symbol declaration info.
*/
ret_type = func_return_type(module, sym);
/* count args, build argument type information */
FOR_EACH_PTR(sym->arguments, arg) {
n_arg++;
} END_FOR_EACH_PTR(arg);
arg_type = calloc(n_arg, sizeof(LLVMTypeRef));
int idx = 0;
FOR_EACH_PTR(sym->arguments, arg) {
struct symbol *arg_sym = arg->ctype.base_type;
arg_type[idx++] = symbol_type(module, arg_sym);
} END_FOR_EACH_PTR(arg);
func_type = LLVMFunctionType(ret_type, arg_type, n_arg,
sym->variadic);
return func_type;
}
static LLVMTypeRef sym_array_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef elem_type;
struct symbol *base_type;
base_type = sym->ctype.base_type;
/* empty struct is undefined [6.7.2.1(8)] */
assert(base_type->bit_size > 0);
elem_type = symbol_type(module, base_type);
if (!elem_type)
return NULL;
return LLVMArrayType(elem_type, sym->bit_size / base_type->bit_size);
}
#define MAX_STRUCT_MEMBERS 64
static LLVMTypeRef sym_struct_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef elem_types[MAX_STRUCT_MEMBERS];
struct symbol *member;
char buffer[256];
LLVMTypeRef ret;
unsigned nr = 0;
snprintf(buffer, sizeof(buffer), "struct.%s", sym->ident ? sym->ident->name : "anno");
ret = LLVMStructCreateNamed(LLVMGetGlobalContext(), buffer);
/* set ->aux to avoid recursion */
sym->aux = ret;
FOR_EACH_PTR(sym->symbol_list, member) {
LLVMTypeRef member_type;
assert(nr < MAX_STRUCT_MEMBERS);
member_type = symbol_type(module, member);
elem_types[nr++] = member_type;
} END_FOR_EACH_PTR(member);
LLVMStructSetBody(ret, elem_types, nr, 0 /* packed? */);
return ret;
}
static LLVMTypeRef sym_union_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef elements;
unsigned union_size;
/*
* There's no union support in the LLVM API so we treat unions as
* opaque structs. The downside is that we lose type information on the
* members but as LLVM doesn't care, neither do we.
*/
union_size = sym->bit_size / 8;
elements = LLVMArrayType(LLVMInt8Type(), union_size);
return LLVMStructType(&elements, 1, 0 /* packed? */);
}
static LLVMTypeRef sym_ptr_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef type;
/* 'void *' is treated like 'char *' */
if (is_void_type(sym->ctype.base_type))
type = LLVMInt8Type();
else
type = symbol_type(module, sym->ctype.base_type);
return LLVMPointerType(type, 0);
}
static LLVMTypeRef sym_basetype_type(struct symbol *sym)
{
LLVMTypeRef ret = NULL;
if (symbol_is_fp_type(sym)) {
switch (sym->bit_size) {
case 32:
ret = LLVMFloatType();
break;
case 64:
ret = LLVMDoubleType();
break;
case 80:
ret = LLVMX86FP80Type();
break;
default:
die("invalid bit size %d for type %d", sym->bit_size, sym->type);
break;
}
} else {
switch (sym->bit_size) {
case -1:
ret = LLVMVoidType();
break;
case 1:
ret = LLVMInt1Type();
break;
case 8:
ret = LLVMInt8Type();
break;
case 16:
ret = LLVMInt16Type();
break;
case 32:
ret = LLVMInt32Type();
break;
case 64:
ret = LLVMInt64Type();
break;
default:
die("invalid bit size %d for type %d", sym->bit_size, sym->type);
break;
}
}
return ret;
}
static LLVMTypeRef symbol_type(LLVMModuleRef module, struct symbol *sym)
{
LLVMTypeRef ret = NULL;
/* don't cache the result for SYM_NODE */
if (sym->type == SYM_NODE)
return symbol_type(module, sym->ctype.base_type);
if (sym->aux)
return sym->aux;
switch (sym->type) {
case SYM_BITFIELD:
case SYM_ENUM:
ret = symbol_type(module, sym->ctype.base_type);
break;
case SYM_BASETYPE:
ret = sym_basetype_type(sym);
break;
case SYM_PTR:
ret = sym_ptr_type(module, sym);
break;
case SYM_UNION:
ret = sym_union_type(module, sym);
break;
case SYM_STRUCT:
ret = sym_struct_type(module, sym);
break;
case SYM_ARRAY:
ret = sym_array_type(module, sym);
break;
case SYM_FN:
ret = sym_func_type(module, sym);
break;
default:
assert(0);
}
/* cache the result */
sym->aux = ret;
return ret;
}
static LLVMTypeRef insn_symbol_type(LLVMModuleRef module, struct instruction *insn)
{
if (insn->type)
return symbol_type(module, insn->type);
switch (insn->size) {
case 8: return LLVMInt8Type();
case 16: return LLVMInt16Type();
case 32: return LLVMInt32Type();
case 64: return LLVMInt64Type();
default:
die("invalid bit size %d", insn->size);
break;
}
return NULL; /* not reached */
}
static LLVMLinkage data_linkage(struct symbol *sym)
{
if (sym->ctype.modifiers & MOD_STATIC)
return LLVMPrivateLinkage;
return LLVMExternalLinkage;
}
static LLVMLinkage function_linkage(struct symbol *sym)
{
if (sym->ctype.modifiers & MOD_STATIC)
return LLVMInternalLinkage;
return LLVMExternalLinkage;
}
#define MAX_PSEUDO_NAME 64
static void pseudo_name(pseudo_t pseudo, char *buf)
{
switch (pseudo->type) {
case PSEUDO_REG:
snprintf(buf, MAX_PSEUDO_NAME, "R%d", pseudo->nr);
break;
case PSEUDO_SYM:
assert(0);
break;
case PSEUDO_VAL:
assert(0);
break;
case PSEUDO_ARG: {
assert(0);
break;
}
case PSEUDO_PHI:
snprintf(buf, MAX_PSEUDO_NAME, "PHI%d", pseudo->nr);
break;
default:
assert(0);
}
}
static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *insn, pseudo_t pseudo)
{
LLVMValueRef result = NULL;
switch (pseudo->type) {
case PSEUDO_REG:
result = pseudo->priv;
break;
case PSEUDO_SYM: {
struct symbol *sym = pseudo->sym;
struct expression *expr;
assert(sym->bb_target == NULL);
expr = sym->initializer;
if (expr) {
switch (expr->type) {
case EXPR_STRING: {
const char *s = expr->string->data;
LLVMValueRef indices[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) };
LLVMValueRef data;
data = LLVMAddGlobal(fn->module, LLVMArrayType(LLVMInt8Type(), strlen(s) + 1), ".str");
LLVMSetLinkage(data, LLVMPrivateLinkage);
LLVMSetGlobalConstant(data, 1);
LLVMSetInitializer(data, LLVMConstString(strdup(s), strlen(s) + 1, true));
result = LLVMConstGEP(data, indices, ARRAY_SIZE(indices));
break;
}
case EXPR_SYMBOL: {
struct symbol *sym = expr->symbol;
result = LLVMGetNamedGlobal(fn->module, show_ident(sym->ident));
assert(result != NULL);
break;
}
default:
assert(0);
}
} else {
const char *name = show_ident(sym->ident);
LLVMTypeRef type = symbol_type(fn->module, sym);
if (LLVMGetTypeKind(type) == LLVMFunctionTypeKind) {
result = LLVMGetNamedFunction(fn->module, name);
if (!result)
result = LLVMAddFunction(fn->module, name, type);
} else {
result = LLVMGetNamedGlobal(fn->module, name);
if (!result)
result = LLVMAddGlobal(fn->module, type, name);
}
}
break;
}
case PSEUDO_VAL:
result = LLVMConstInt(insn_symbol_type(fn->module, insn), pseudo->value, 1);
break;
case PSEUDO_ARG: {
result = LLVMGetParam(fn->fn, pseudo->nr - 1);
break;
}
case PSEUDO_PHI:
result = pseudo->priv;
break;
case PSEUDO_VOID:
result = NULL;
break;
default:
assert(0);
}
return result;
}
static LLVMValueRef calc_gep(LLVMBuilderRef builder, LLVMValueRef base, LLVMValueRef off)
{
LLVMTypeRef type = LLVMTypeOf(base);
unsigned int as = LLVMGetPointerAddressSpace(type);
LLVMTypeRef bytep = LLVMPointerType(LLVMInt8Type(), as);
LLVMValueRef addr;
/* convert base to char* type */
base = LLVMBuildPointerCast(builder, base, bytep, "");
/* addr = base + off */
addr = LLVMBuildInBoundsGEP(builder, base, &off, 1, "");
/* convert back to the actual pointer type */
addr = LLVMBuildPointerCast(builder, addr, type, "");
return addr;
}
static LLVMRealPredicate translate_fop(int opcode)
{
static const LLVMRealPredicate trans_tbl[] = {
[OP_SET_EQ] = LLVMRealOEQ,
[OP_SET_NE] = LLVMRealUNE,
[OP_SET_LE] = LLVMRealOLE,
[OP_SET_GE] = LLVMRealOGE,
[OP_SET_LT] = LLVMRealOLT,
[OP_SET_GT] = LLVMRealOGT,
/* Are these used with FP? */
[OP_SET_B] = LLVMRealOLT,
[OP_SET_A] = LLVMRealOGT,
[OP_SET_BE] = LLVMRealOLE,
[OP_SET_AE] = LLVMRealOGE,
};
return trans_tbl[opcode];
}
static LLVMIntPredicate translate_op(int opcode)
{
static const LLVMIntPredicate trans_tbl[] = {
[OP_SET_EQ] = LLVMIntEQ,
[OP_SET_NE] = LLVMIntNE,
[OP_SET_LE] = LLVMIntSLE,
[OP_SET_GE] = LLVMIntSGE,
[OP_SET_LT] = LLVMIntSLT,
[OP_SET_GT] = LLVMIntSGT,
[OP_SET_B] = LLVMIntULT,
[OP_SET_A] = LLVMIntUGT,
[OP_SET_BE] = LLVMIntULE,
[OP_SET_AE] = LLVMIntUGE,
};
return trans_tbl[opcode];
}
static void output_op_binary(struct function *fn, struct instruction *insn)
{
LLVMValueRef lhs, rhs, target;
char target_name[64];
lhs = pseudo_to_value(fn, insn, insn->src1);
rhs = pseudo_to_value(fn, insn, insn->src2);
pseudo_name(insn->target, target_name);
switch (insn->opcode) {
/* Binary */
case OP_ADD:
if (symbol_is_fp_type(insn->type))
target = LLVMBuildFAdd(fn->builder, lhs, rhs, target_name);
else
target = LLVMBuildAdd(fn->builder, lhs, rhs, target_name);
break;
case OP_SUB:
if (symbol_is_fp_type(insn->type))
target = LLVMBuildFSub(fn->builder, lhs, rhs, target_name);
else
target = LLVMBuildSub(fn->builder, lhs, rhs, target_name);
break;
case OP_MULU:
if (symbol_is_fp_type(insn->type))
target = LLVMBuildFMul(fn->builder, lhs, rhs, target_name);
else
target = LLVMBuildMul(fn->builder, lhs, rhs, target_name);
break;
case OP_MULS:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildMul(fn->builder, lhs, rhs, target_name);
break;
case OP_DIVU:
if (symbol_is_fp_type(insn->type))
target = LLVMBuildFDiv(fn->builder, lhs, rhs, target_name);
else
target = LLVMBuildUDiv(fn->builder, lhs, rhs, target_name);
break;
case OP_DIVS:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildSDiv(fn->builder, lhs, rhs, target_name);
break;
case OP_MODU:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildURem(fn->builder, lhs, rhs, target_name);
break;
case OP_MODS:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildSRem(fn->builder, lhs, rhs, target_name);
break;
case OP_SHL:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildShl(fn->builder, lhs, rhs, target_name);
break;
case OP_LSR:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildLShr(fn->builder, lhs, rhs, target_name);
break;
case OP_ASR:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildAShr(fn->builder, lhs, rhs, target_name);
break;
/* Logical */
case OP_AND:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildAnd(fn->builder, lhs, rhs, target_name);
break;
case OP_OR:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildOr(fn->builder, lhs, rhs, target_name);
break;
case OP_XOR:
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildXor(fn->builder, lhs, rhs, target_name);
break;
case OP_AND_BOOL: {
LLVMValueRef lhs_nz, rhs_nz;
LLVMTypeRef dst_type;
lhs_nz = LLVMBuildIsNotNull(fn->builder, lhs, "");
rhs_nz = LLVMBuildIsNotNull(fn->builder, rhs, "");
target = LLVMBuildAnd(fn->builder, lhs_nz, rhs_nz, target_name);
dst_type = insn_symbol_type(fn->module, insn);
target = LLVMBuildZExt(fn->builder, target, dst_type, target_name);
break;
}
case OP_OR_BOOL: {
LLVMValueRef lhs_nz, rhs_nz;
LLVMTypeRef dst_type;
lhs_nz = LLVMBuildIsNotNull(fn->builder, lhs, "");
rhs_nz = LLVMBuildIsNotNull(fn->builder, rhs, "");
target = LLVMBuildOr(fn->builder, lhs_nz, rhs_nz, target_name);
dst_type = insn_symbol_type(fn->module, insn);
target = LLVMBuildZExt(fn->builder, target, dst_type, target_name);
break;
}
default:
assert(0);
break;
}
insn->target->priv = target;
}
static void output_op_compare(struct function *fn, struct instruction *insn)
{
LLVMValueRef lhs, rhs, target;
char target_name[64];
lhs = pseudo_to_value(fn, insn, insn->src1);
if (insn->src2->type == PSEUDO_VAL)
rhs = LLVMConstInt(LLVMTypeOf(lhs), insn->src2->value, 1);
else
rhs = pseudo_to_value(fn, insn, insn->src2);
pseudo_name(insn->target, target_name);
LLVMTypeRef dst_type = insn_symbol_type(fn->module, insn);
if (LLVMGetTypeKind(LLVMTypeOf(lhs)) == LLVMIntegerTypeKind) {
LLVMIntPredicate op = translate_op(insn->opcode);
target = LLVMBuildICmp(fn->builder, op, lhs, rhs, target_name);
} else {
LLVMRealPredicate op = translate_fop(insn->opcode);
target = LLVMBuildFCmp(fn->builder, op, lhs, rhs, target_name);
}
target = LLVMBuildZExt(fn->builder, target, dst_type, target_name);
insn->target->priv = target;
}
static void output_op_ret(struct function *fn, struct instruction *insn)
{
pseudo_t pseudo = insn->src;
if (pseudo && pseudo != VOID) {
LLVMValueRef result = pseudo_to_value(fn, insn, pseudo);
LLVMBuildRet(fn->builder, result);
} else
LLVMBuildRetVoid(fn->builder);
}
static LLVMValueRef calc_memop_addr(struct function *fn, struct instruction *insn)
{
LLVMTypeRef int_type, addr_type;
LLVMValueRef src, off, addr;
unsigned int as;
/* int type large enough to hold a pointer */
int_type = LLVMIntType(bits_in_pointer);
off = LLVMConstInt(int_type, insn->offset, 0);
/* convert src to the effective pointer type */
src = pseudo_to_value(fn, insn, insn->src);
as = LLVMGetPointerAddressSpace(LLVMTypeOf(src));
addr_type = LLVMPointerType(insn_symbol_type(fn->module, insn), as);
src = LLVMBuildPointerCast(fn->builder, src, addr_type, "");
/* addr = src + off */
addr = calc_gep(fn->builder, src, off);
return addr;
}
static void output_op_load(struct function *fn, struct instruction *insn)
{
LLVMValueRef addr, target;
addr = calc_memop_addr(fn, insn);
/* perform load */
target = LLVMBuildLoad(fn->builder, addr, "load_target");
insn->target->priv = target;
}
static void output_op_store(struct function *fn, struct instruction *insn)
{
LLVMValueRef addr, target, target_in;
addr = calc_memop_addr(fn, insn);
target_in = pseudo_to_value(fn, insn, insn->target);
/* perform store */
target = LLVMBuildStore(fn->builder, target_in, addr);
insn->target->priv = target;
}
static LLVMValueRef bool_value(struct function *fn, LLVMValueRef value)
{
if (LLVMTypeOf(value) != LLVMInt1Type())
value = LLVMBuildIsNotNull(fn->builder, value, "cond");
return value;
}
static void output_op_cbr(struct function *fn, struct instruction *br)
{
LLVMValueRef cond = bool_value(fn,
pseudo_to_value(fn, br, br->cond));
LLVMBuildCondBr(fn->builder, cond,
br->bb_true->priv,
br->bb_false->priv);
}
static void output_op_br(struct function *fn, struct instruction *br)
{
LLVMBuildBr(fn->builder, br->bb_true->priv);
}
static void output_op_sel(struct function *fn, struct instruction *insn)
{
LLVMValueRef target, src1, src2, src3;
src1 = bool_value(fn, pseudo_to_value(fn, insn, insn->src1));
src2 = pseudo_to_value(fn, insn, insn->src2);
src3 = pseudo_to_value(fn, insn, insn->src3);
target = LLVMBuildSelect(fn->builder, src1, src2, src3, "select");
insn->target->priv = target;
}
static void output_op_switch(struct function *fn, struct instruction *insn)
{
LLVMValueRef sw_val, target;
struct basic_block *def = NULL;
struct multijmp *jmp;
int n_jmp = 0;
FOR_EACH_PTR(insn->multijmp_list, jmp) {
if (jmp->begin == jmp->end) { /* case N */
n_jmp++;
} else if (jmp->begin < jmp->end) { /* case M..N */
assert(0);
} else /* default case */
def = jmp->target;
} END_FOR_EACH_PTR(jmp);
sw_val = pseudo_to_value(fn, insn, insn->target);
target = LLVMBuildSwitch(fn->builder, sw_val,
def ? def->priv : NULL, n_jmp);
FOR_EACH_PTR(insn->multijmp_list, jmp) {
if (jmp->begin == jmp->end) { /* case N */
LLVMAddCase(target,
LLVMConstInt(LLVMInt32Type(), jmp->begin, 0),
jmp->target->priv);
} else if (jmp->begin < jmp->end) { /* case M..N */
assert(0);
}
} END_FOR_EACH_PTR(jmp);
insn->target->priv = target;
}
static void output_op_call(struct function *fn, struct instruction *insn)
{
LLVMValueRef target, func;
int n_arg = 0, i;
struct pseudo *arg;
LLVMValueRef *args;
FOR_EACH_PTR(insn->arguments, arg) {
n_arg++;
} END_FOR_EACH_PTR(arg);
args = calloc(n_arg, sizeof(LLVMValueRef));
i = 0;
FOR_EACH_PTR(insn->arguments, arg) {
args[i++] = pseudo_to_value(fn, insn, arg);
} END_FOR_EACH_PTR(arg);
func = pseudo_to_value(fn, insn, insn->func);
target = LLVMBuildCall(fn->builder, func, args, n_arg, "");
insn->target->priv = target;
}
static void output_op_phisrc(struct function *fn, struct instruction *insn)
{
LLVMValueRef v;
struct instruction *phi;
assert(insn->target->priv == NULL);
/* target = src */
v = pseudo_to_value(fn, insn, insn->phi_src);
FOR_EACH_PTR(insn->phi_users, phi) {
LLVMValueRef load, ptr;
assert(phi->opcode == OP_PHI);
/* phi must be load from alloca */
load = phi->target->priv;
assert(LLVMGetInstructionOpcode(load) == LLVMLoad);
ptr = LLVMGetOperand(load, 0);
/* store v to alloca */
LLVMBuildStore(fn->builder, v, ptr);
} END_FOR_EACH_PTR(phi);
}
static void output_op_phi(struct function *fn, struct instruction *insn)
{
LLVMValueRef load = insn->target->priv;
/* forward load */
assert(LLVMGetInstructionOpcode(load) == LLVMLoad);
/* forward load has no parent block */
assert(!LLVMGetInstructionParent(load));
/* finalize load in current block */
LLVMInsertIntoBuilder(fn->builder, load);
}
static void output_op_ptrcast(struct function *fn, struct instruction *insn)
{
LLVMValueRef src, target;
char target_name[64];
src = insn->src->priv;
if (!src)
src = pseudo_to_value(fn, insn, insn->src);
pseudo_name(insn->target, target_name);
assert(!symbol_is_fp_type(insn->type));
target = LLVMBuildBitCast(fn->builder, src, insn_symbol_type(fn->module, insn), target_name);
insn->target->priv = target;
}
static void output_op_cast(struct function *fn, struct instruction *insn, LLVMOpcode op)
{
LLVMValueRef src, target;
char target_name[64];
src = insn->src->priv;
if (!src)
src = pseudo_to_value(fn, insn, insn->src);
pseudo_name(insn->target, target_name);
assert(!symbol_is_fp_type(insn->type));
if (insn->size < LLVMGetIntTypeWidth(LLVMTypeOf(src)))
target = LLVMBuildTrunc(fn->builder, src, insn_symbol_type(fn->module, insn), target_name);
else
target = LLVMBuildCast(fn->builder, op, src, insn_symbol_type(fn->module, insn), target_name);
insn->target->priv = target;
}
static void output_insn(struct function *fn, struct instruction *insn)
{
switch (insn->opcode) {
case OP_RET:
output_op_ret(fn, insn);
break;
case OP_BR:
output_op_br(fn, insn);
break;
case OP_CBR:
output_op_cbr(fn, insn);
break;
case OP_SYMADDR:
assert(0);
break;
case OP_SETVAL:
assert(0);
break;
case OP_SWITCH:
output_op_switch(fn, insn);
break;
case OP_COMPUTEDGOTO:
assert(0);
break;
case OP_PHISOURCE:
output_op_phisrc(fn, insn);
break;
case OP_PHI:
output_op_phi(fn, insn);
break;
case OP_LOAD:
output_op_load(fn, insn);
break;
case OP_LNOP:
assert(0);
break;
case OP_STORE:
output_op_store(fn, insn);
break;
case OP_SNOP:
assert(0);
break;
case OP_INLINED_CALL:
assert(0);
break;
case OP_CALL:
output_op_call(fn, insn);
break;
case OP_CAST:
output_op_cast(fn, insn, LLVMZExt);
break;
case OP_SCAST:
output_op_cast(fn, insn, LLVMSExt);
break;
case OP_FPCAST:
assert(0);
break;
case OP_PTRCAST:
output_op_ptrcast(fn, insn);
break;
case OP_BINARY ... OP_BINARY_END:
output_op_binary(fn, insn);
break;
case OP_BINCMP ... OP_BINCMP_END:
output_op_compare(fn, insn);
break;
case OP_SEL:
output_op_sel(fn, insn);
break;
case OP_SLICE:
assert(0);
break;
case OP_NOT: {
LLVMValueRef src, target;
char target_name[64];
src = pseudo_to_value(fn, insn, insn->src);
pseudo_name(insn->target, target_name);
target = LLVMBuildNot(fn->builder, src, target_name);
insn->target->priv = target;
break;
}
case OP_NEG:
assert(0);
break;
case OP_CONTEXT:
assert(0);
break;
case OP_RANGE:
assert(0);
break;
case OP_NOP:
assert(0);
break;
case OP_DEATHNOTE:
break;
case OP_ASM:
assert(0);
break;
case OP_COPY:
assert(0);
break;
default:
break;
}
}
static void output_bb(struct function *fn, struct basic_block *bb, unsigned long generation)
{
struct instruction *insn;
bb->generation = generation;
FOR_EACH_PTR(bb->insns, insn) {
if (!insn->bb)
continue;
output_insn(fn, insn);
}
END_FOR_EACH_PTR(insn);
}
#define MAX_ARGS 64
static void output_fn(LLVMModuleRef module, struct entrypoint *ep)
{
unsigned long generation = ++bb_generation;
struct symbol *sym = ep->name;
struct symbol *base_type = sym->ctype.base_type;
struct symbol *ret_type = sym->ctype.base_type->ctype.base_type;
LLVMTypeRef arg_types[MAX_ARGS];
LLVMTypeRef return_type;
struct function function = { .module = module };
struct basic_block *bb;
struct symbol *arg;
const char *name;
int nr_args = 0;
FOR_EACH_PTR(base_type->arguments, arg) {
struct symbol *arg_base_type = arg->ctype.base_type;
arg_types[nr_args++] = symbol_type(module, arg_base_type);
} END_FOR_EACH_PTR(arg);
name = show_ident(sym->ident);
return_type = symbol_type(module, ret_type);
function.type = LLVMFunctionType(return_type, arg_types, nr_args, 0);
function.fn = LLVMAddFunction(module, name, function.type);
LLVMSetFunctionCallConv(function.fn, LLVMCCallConv);
LLVMSetLinkage(function.fn, function_linkage(sym));
function.builder = LLVMCreateBuilder();
static int nr_bb;
FOR_EACH_PTR(ep->bbs, bb) {
if (bb->generation == generation)
continue;
LLVMBasicBlockRef bbr;
char bbname[32];
struct instruction *insn;
sprintf(bbname, "L%d", nr_bb++);
bbr = LLVMAppendBasicBlock(function.fn, bbname);
bb->priv = bbr;
/* allocate alloca for each phi */
FOR_EACH_PTR(bb->insns, insn) {
LLVMBasicBlockRef entrybbr;
LLVMTypeRef phi_type;
LLVMValueRef ptr;
if (!insn->bb || insn->opcode != OP_PHI)
continue;
/* insert alloca into entry block */
entrybbr = LLVMGetEntryBasicBlock(function.fn);
LLVMPositionBuilderAtEnd(function.builder, entrybbr);
phi_type = insn_symbol_type(module, insn);
ptr = LLVMBuildAlloca(function.builder, phi_type, "");
/* emit forward load for phi */
LLVMClearInsertionPosition(function.builder);
insn->target->priv = LLVMBuildLoad(function.builder, ptr, "phi");
} END_FOR_EACH_PTR(insn);
}
END_FOR_EACH_PTR(bb);
FOR_EACH_PTR(ep->bbs, bb) {
if (bb->generation == generation)