3 * ./sparse-llvm hello.c | llc | as -o hello.o
6 #include <llvm-c/Core.h>
7 #include <llvm-c/BitWriter.h>
8 #include <llvm-c/Analysis.h>
9 #include <llvm-c/Target.h>
18 #include "expression.h"
19 #include "linearize.h"
23 LLVMBuilderRef builder
;
29 static inline bool symbol_is_fp_type(struct symbol
*sym
)
34 return sym
->ctype
.base_type
== &fp_type
;
37 static LLVMTypeRef
symbol_type(LLVMModuleRef module
, struct symbol
*sym
);
39 static LLVMTypeRef
func_return_type(LLVMModuleRef module
, struct symbol
*sym
)
41 return symbol_type(module
, sym
->ctype
.base_type
);
44 static LLVMTypeRef
sym_func_type(LLVMModuleRef module
, struct symbol
*sym
)
46 LLVMTypeRef
*arg_type
;
47 LLVMTypeRef func_type
;
52 /* to avoid strangeness with varargs [for now], we build
53 * the function and type anew, for each call. This
54 * is probably wrong. We should look up the
55 * symbol declaration info.
58 ret_type
= func_return_type(module
, sym
);
60 /* count args, build argument type information */
61 FOR_EACH_PTR(sym
->arguments
, arg
) {
63 } END_FOR_EACH_PTR(arg
);
65 arg_type
= calloc(n_arg
, sizeof(LLVMTypeRef
));
68 FOR_EACH_PTR(sym
->arguments
, arg
) {
69 struct symbol
*arg_sym
= arg
->ctype
.base_type
;
71 arg_type
[idx
++] = symbol_type(module
, arg_sym
);
72 } END_FOR_EACH_PTR(arg
);
73 func_type
= LLVMFunctionType(ret_type
, arg_type
, n_arg
,
79 static LLVMTypeRef
sym_array_type(LLVMModuleRef module
, struct symbol
*sym
)
81 LLVMTypeRef elem_type
;
82 struct symbol
*base_type
;
84 base_type
= sym
->ctype
.base_type
;
85 /* empty struct is undefined [6.7.2.1(8)] */
86 assert(base_type
->bit_size
> 0);
88 elem_type
= symbol_type(module
, base_type
);
92 return LLVMArrayType(elem_type
, sym
->bit_size
/ base_type
->bit_size
);
95 #define MAX_STRUCT_MEMBERS 64
97 static LLVMTypeRef
sym_struct_type(LLVMModuleRef module
, struct symbol
*sym
)
99 LLVMTypeRef elem_types
[MAX_STRUCT_MEMBERS
];
100 struct symbol
*member
;
105 snprintf(buffer
, sizeof(buffer
), "struct.%s", sym
->ident
? sym
->ident
->name
: "anno");
106 ret
= LLVMStructCreateNamed(LLVMGetGlobalContext(), buffer
);
107 /* set ->aux to avoid recursion */
110 FOR_EACH_PTR(sym
->symbol_list
, member
) {
111 LLVMTypeRef member_type
;
113 assert(nr
< MAX_STRUCT_MEMBERS
);
115 member_type
= symbol_type(module
, member
);
117 elem_types
[nr
++] = member_type
;
118 } END_FOR_EACH_PTR(member
);
120 LLVMStructSetBody(ret
, elem_types
, nr
, 0 /* packed? */);
124 static LLVMTypeRef
sym_union_type(LLVMModuleRef module
, struct symbol
*sym
)
126 LLVMTypeRef elements
;
130 * There's no union support in the LLVM API so we treat unions as
131 * opaque structs. The downside is that we lose type information on the
132 * members but as LLVM doesn't care, neither do we.
134 union_size
= sym
->bit_size
/ 8;
136 elements
= LLVMArrayType(LLVMInt8Type(), union_size
);
138 return LLVMStructType(&elements
, 1, 0 /* packed? */);
141 static LLVMTypeRef
sym_ptr_type(LLVMModuleRef module
, struct symbol
*sym
)
145 /* 'void *' is treated like 'char *' */
146 if (is_void_type(sym
->ctype
.base_type
))
147 type
= LLVMInt8Type();
149 type
= symbol_type(module
, sym
->ctype
.base_type
);
151 return LLVMPointerType(type
, 0);
154 static LLVMTypeRef
sym_basetype_type(struct symbol
*sym
)
156 LLVMTypeRef ret
= NULL
;
158 if (symbol_is_fp_type(sym
)) {
159 switch (sym
->bit_size
) {
161 ret
= LLVMFloatType();
164 ret
= LLVMDoubleType();
167 ret
= LLVMX86FP80Type();
170 die("invalid bit size %d for type %d", sym
->bit_size
, sym
->type
);
174 switch (sym
->bit_size
) {
176 ret
= LLVMVoidType();
179 ret
= LLVMInt1Type();
182 ret
= LLVMInt8Type();
185 ret
= LLVMInt16Type();
188 ret
= LLVMInt32Type();
191 ret
= LLVMInt64Type();
194 die("invalid bit size %d for type %d", sym
->bit_size
, sym
->type
);
202 static LLVMTypeRef
symbol_type(LLVMModuleRef module
, struct symbol
*sym
)
204 LLVMTypeRef ret
= NULL
;
206 /* don't cache the result for SYM_NODE */
207 if (sym
->type
== SYM_NODE
)
208 return symbol_type(module
, sym
->ctype
.base_type
);
216 ret
= symbol_type(module
, sym
->ctype
.base_type
);
219 ret
= sym_basetype_type(sym
);
222 ret
= sym_ptr_type(module
, sym
);
225 ret
= sym_union_type(module
, sym
);
228 ret
= sym_struct_type(module
, sym
);
231 ret
= sym_array_type(module
, sym
);
234 ret
= sym_func_type(module
, sym
);
240 /* cache the result */
245 static LLVMTypeRef
insn_symbol_type(LLVMModuleRef module
, struct instruction
*insn
)
248 return symbol_type(module
, insn
->type
);
250 switch (insn
->size
) {
251 case 8: return LLVMInt8Type();
252 case 16: return LLVMInt16Type();
253 case 32: return LLVMInt32Type();
254 case 64: return LLVMInt64Type();
257 die("invalid bit size %d", insn
->size
);
261 return NULL
; /* not reached */
264 static LLVMLinkage
data_linkage(struct symbol
*sym
)
266 if (sym
->ctype
.modifiers
& MOD_STATIC
)
267 return LLVMPrivateLinkage
;
269 return LLVMExternalLinkage
;
272 static LLVMLinkage
function_linkage(struct symbol
*sym
)
274 if (sym
->ctype
.modifiers
& MOD_STATIC
)
275 return LLVMInternalLinkage
;
277 return LLVMExternalLinkage
;
280 #define MAX_PSEUDO_NAME 64
282 static void pseudo_name(pseudo_t pseudo
, char *buf
)
284 switch (pseudo
->type
) {
286 snprintf(buf
, MAX_PSEUDO_NAME
, "R%d", pseudo
->nr
);
299 snprintf(buf
, MAX_PSEUDO_NAME
, "PHI%d", pseudo
->nr
);
306 static LLVMValueRef
pseudo_to_value(struct function
*fn
, struct instruction
*insn
, pseudo_t pseudo
)
308 LLVMValueRef result
= NULL
;
310 switch (pseudo
->type
) {
312 result
= pseudo
->priv
;
315 struct symbol
*sym
= pseudo
->sym
;
316 struct expression
*expr
;
318 assert(sym
->bb_target
== NULL
);
320 expr
= sym
->initializer
;
322 switch (expr
->type
) {
324 const char *s
= expr
->string
->data
;
325 LLVMValueRef indices
[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) };
328 data
= LLVMAddGlobal(fn
->module
, LLVMArrayType(LLVMInt8Type(), strlen(s
) + 1), ".str");
329 LLVMSetLinkage(data
, LLVMPrivateLinkage
);
330 LLVMSetGlobalConstant(data
, 1);
331 LLVMSetInitializer(data
, LLVMConstString(strdup(s
), strlen(s
) + 1, true));
333 result
= LLVMConstGEP(data
, indices
, ARRAY_SIZE(indices
));
337 struct symbol
*sym
= expr
->symbol
;
339 result
= LLVMGetNamedGlobal(fn
->module
, show_ident(sym
->ident
));
340 assert(result
!= NULL
);
347 const char *name
= show_ident(sym
->ident
);
348 LLVMTypeRef type
= symbol_type(fn
->module
, sym
);
350 if (LLVMGetTypeKind(type
) == LLVMFunctionTypeKind
) {
351 result
= LLVMGetNamedFunction(fn
->module
, name
);
353 result
= LLVMAddFunction(fn
->module
, name
, type
);
355 result
= LLVMGetNamedGlobal(fn
->module
, name
);
357 result
= LLVMAddGlobal(fn
->module
, type
, name
);
363 result
= LLVMConstInt(insn_symbol_type(fn
->module
, insn
), pseudo
->value
, 1);
366 result
= LLVMGetParam(fn
->fn
, pseudo
->nr
- 1);
370 result
= pseudo
->priv
;
382 static LLVMValueRef
calc_gep(LLVMBuilderRef builder
, LLVMValueRef base
, LLVMValueRef off
)
384 LLVMTypeRef type
= LLVMTypeOf(base
);
385 unsigned int as
= LLVMGetPointerAddressSpace(type
);
386 LLVMTypeRef bytep
= LLVMPointerType(LLVMInt8Type(), as
);
389 /* convert base to char* type */
390 base
= LLVMBuildPointerCast(builder
, base
, bytep
, "");
391 /* addr = base + off */
392 addr
= LLVMBuildInBoundsGEP(builder
, base
, &off
, 1, "");
393 /* convert back to the actual pointer type */
394 addr
= LLVMBuildPointerCast(builder
, addr
, type
, "");
398 static LLVMRealPredicate
translate_fop(int opcode
)
400 static const LLVMRealPredicate trans_tbl
[] = {
401 [OP_SET_EQ
] = LLVMRealOEQ
,
402 [OP_SET_NE
] = LLVMRealUNE
,
403 [OP_SET_LE
] = LLVMRealOLE
,
404 [OP_SET_GE
] = LLVMRealOGE
,
405 [OP_SET_LT
] = LLVMRealOLT
,
406 [OP_SET_GT
] = LLVMRealOGT
,
407 /* Are these used with FP? */
408 [OP_SET_B
] = LLVMRealOLT
,
409 [OP_SET_A
] = LLVMRealOGT
,
410 [OP_SET_BE
] = LLVMRealOLE
,
411 [OP_SET_AE
] = LLVMRealOGE
,
414 return trans_tbl
[opcode
];
417 static LLVMIntPredicate
translate_op(int opcode
)
419 static const LLVMIntPredicate trans_tbl
[] = {
420 [OP_SET_EQ
] = LLVMIntEQ
,
421 [OP_SET_NE
] = LLVMIntNE
,
422 [OP_SET_LE
] = LLVMIntSLE
,
423 [OP_SET_GE
] = LLVMIntSGE
,
424 [OP_SET_LT
] = LLVMIntSLT
,
425 [OP_SET_GT
] = LLVMIntSGT
,
426 [OP_SET_B
] = LLVMIntULT
,
427 [OP_SET_A
] = LLVMIntUGT
,
428 [OP_SET_BE
] = LLVMIntULE
,
429 [OP_SET_AE
] = LLVMIntUGE
,
432 return trans_tbl
[opcode
];
435 static void output_op_binary(struct function
*fn
, struct instruction
*insn
)
437 LLVMValueRef lhs
, rhs
, target
;
438 char target_name
[64];
440 lhs
= pseudo_to_value(fn
, insn
, insn
->src1
);
442 rhs
= pseudo_to_value(fn
, insn
, insn
->src2
);
444 pseudo_name(insn
->target
, target_name
);
446 switch (insn
->opcode
) {
449 if (symbol_is_fp_type(insn
->type
))
450 target
= LLVMBuildFAdd(fn
->builder
, lhs
, rhs
, target_name
);
452 target
= LLVMBuildAdd(fn
->builder
, lhs
, rhs
, target_name
);
455 if (symbol_is_fp_type(insn
->type
))
456 target
= LLVMBuildFSub(fn
->builder
, lhs
, rhs
, target_name
);
458 target
= LLVMBuildSub(fn
->builder
, lhs
, rhs
, target_name
);
461 if (symbol_is_fp_type(insn
->type
))
462 target
= LLVMBuildFMul(fn
->builder
, lhs
, rhs
, target_name
);
464 target
= LLVMBuildMul(fn
->builder
, lhs
, rhs
, target_name
);
467 assert(!symbol_is_fp_type(insn
->type
));
468 target
= LLVMBuildMul(fn
->builder
, lhs
, rhs
, target_name
);
471 if (symbol_is_fp_type(insn
->type
))
472 target
= LLVMBuildFDiv(fn
->builder
, lhs
, rhs
, target_name
);
474 target
= LLVMBuildUDiv(fn
->builder
, lhs
, rhs
, target_name
);
477 assert(!symbol_is_fp_type(insn
->type
));
478 target
= LLVMBuildSDiv(fn
->builder
, lhs
, rhs
, target_name
);
481 assert(!symbol_is_fp_type(insn
->type
));
482 target
= LLVMBuildURem(fn
->builder
, lhs
, rhs
, target_name
);
485 assert(!symbol_is_fp_type(insn
->type
));
486 target
= LLVMBuildSRem(fn
->builder
, lhs
, rhs
, target_name
);
489 assert(!symbol_is_fp_type(insn
->type
));
490 target
= LLVMBuildShl(fn
->builder
, lhs
, rhs
, target_name
);
493 assert(!symbol_is_fp_type(insn
->type
));
494 target
= LLVMBuildLShr(fn
->builder
, lhs
, rhs
, target_name
);
497 assert(!symbol_is_fp_type(insn
->type
));
498 target
= LLVMBuildAShr(fn
->builder
, lhs
, rhs
, target_name
);
503 assert(!symbol_is_fp_type(insn
->type
));
504 target
= LLVMBuildAnd(fn
->builder
, lhs
, rhs
, target_name
);
507 assert(!symbol_is_fp_type(insn
->type
));
508 target
= LLVMBuildOr(fn
->builder
, lhs
, rhs
, target_name
);
511 assert(!symbol_is_fp_type(insn
->type
));
512 target
= LLVMBuildXor(fn
->builder
, lhs
, rhs
, target_name
);
515 LLVMValueRef lhs_nz
, rhs_nz
;
516 LLVMTypeRef dst_type
;
518 lhs_nz
= LLVMBuildIsNotNull(fn
->builder
, lhs
, "");
519 rhs_nz
= LLVMBuildIsNotNull(fn
->builder
, rhs
, "");
520 target
= LLVMBuildAnd(fn
->builder
, lhs_nz
, rhs_nz
, target_name
);
522 dst_type
= insn_symbol_type(fn
->module
, insn
);
523 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
527 LLVMValueRef lhs_nz
, rhs_nz
;
528 LLVMTypeRef dst_type
;
530 lhs_nz
= LLVMBuildIsNotNull(fn
->builder
, lhs
, "");
531 rhs_nz
= LLVMBuildIsNotNull(fn
->builder
, rhs
, "");
532 target
= LLVMBuildOr(fn
->builder
, lhs_nz
, rhs_nz
, target_name
);
534 dst_type
= insn_symbol_type(fn
->module
, insn
);
535 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
543 insn
->target
->priv
= target
;
546 static void output_op_compare(struct function
*fn
, struct instruction
*insn
)
548 LLVMValueRef lhs
, rhs
, target
;
549 char target_name
[64];
551 lhs
= pseudo_to_value(fn
, insn
, insn
->src1
);
553 if (insn
->src2
->type
== PSEUDO_VAL
)
554 rhs
= LLVMConstInt(LLVMTypeOf(lhs
), insn
->src2
->value
, 1);
556 rhs
= pseudo_to_value(fn
, insn
, insn
->src2
);
558 pseudo_name(insn
->target
, target_name
);
560 LLVMTypeRef dst_type
= insn_symbol_type(fn
->module
, insn
);
562 if (LLVMGetTypeKind(LLVMTypeOf(lhs
)) == LLVMIntegerTypeKind
) {
563 LLVMIntPredicate op
= translate_op(insn
->opcode
);
565 target
= LLVMBuildICmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
567 LLVMRealPredicate op
= translate_fop(insn
->opcode
);
569 target
= LLVMBuildFCmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
572 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
574 insn
->target
->priv
= target
;
577 static void output_op_ret(struct function
*fn
, struct instruction
*insn
)
579 pseudo_t pseudo
= insn
->src
;
581 if (pseudo
&& pseudo
!= VOID
) {
582 LLVMValueRef result
= pseudo_to_value(fn
, insn
, pseudo
);
584 LLVMBuildRet(fn
->builder
, result
);
586 LLVMBuildRetVoid(fn
->builder
);
589 static LLVMValueRef
calc_memop_addr(struct function
*fn
, struct instruction
*insn
)
591 LLVMTypeRef int_type
, addr_type
;
592 LLVMValueRef src
, off
, addr
;
595 /* int type large enough to hold a pointer */
596 int_type
= LLVMIntType(bits_in_pointer
);
597 off
= LLVMConstInt(int_type
, insn
->offset
, 0);
599 /* convert src to the effective pointer type */
600 src
= pseudo_to_value(fn
, insn
, insn
->src
);
601 as
= LLVMGetPointerAddressSpace(LLVMTypeOf(src
));
602 addr_type
= LLVMPointerType(insn_symbol_type(fn
->module
, insn
), as
);
603 src
= LLVMBuildPointerCast(fn
->builder
, src
, addr_type
, "");
605 /* addr = src + off */
606 addr
= calc_gep(fn
->builder
, src
, off
);
611 static void output_op_load(struct function
*fn
, struct instruction
*insn
)
613 LLVMValueRef addr
, target
;
615 addr
= calc_memop_addr(fn
, insn
);
618 target
= LLVMBuildLoad(fn
->builder
, addr
, "load_target");
620 insn
->target
->priv
= target
;
623 static void output_op_store(struct function
*fn
, struct instruction
*insn
)
625 LLVMValueRef addr
, target
, target_in
;
627 addr
= calc_memop_addr(fn
, insn
);
629 target_in
= pseudo_to_value(fn
, insn
, insn
->target
);
632 target
= LLVMBuildStore(fn
->builder
, target_in
, addr
);
634 insn
->target
->priv
= target
;
637 static LLVMValueRef
bool_value(struct function
*fn
, LLVMValueRef value
)
639 if (LLVMTypeOf(value
) != LLVMInt1Type())
640 value
= LLVMBuildIsNotNull(fn
->builder
, value
, "cond");
645 static void output_op_br(struct function
*fn
, struct instruction
*br
)
648 LLVMValueRef cond
= bool_value(fn
,
649 pseudo_to_value(fn
, br
, br
->cond
));
651 LLVMBuildCondBr(fn
->builder
, cond
,
655 LLVMBuildBr(fn
->builder
,
656 br
->bb_true
? br
->bb_true
->priv
:
660 static void output_op_sel(struct function
*fn
, struct instruction
*insn
)
662 LLVMValueRef target
, src1
, src2
, src3
;
664 src1
= bool_value(fn
, pseudo_to_value(fn
, insn
, insn
->src1
));
665 src2
= pseudo_to_value(fn
, insn
, insn
->src2
);
666 src3
= pseudo_to_value(fn
, insn
, insn
->src3
);
668 target
= LLVMBuildSelect(fn
->builder
, src1
, src2
, src3
, "select");
670 insn
->target
->priv
= target
;
673 static void output_op_switch(struct function
*fn
, struct instruction
*insn
)
675 LLVMValueRef sw_val
, target
;
676 struct basic_block
*def
= NULL
;
677 struct multijmp
*jmp
;
680 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
681 if (jmp
->begin
== jmp
->end
) { /* case N */
683 } else if (jmp
->begin
< jmp
->end
) { /* case M..N */
685 } else /* default case */
687 } END_FOR_EACH_PTR(jmp
);
689 sw_val
= pseudo_to_value(fn
, insn
, insn
->target
);
690 target
= LLVMBuildSwitch(fn
->builder
, sw_val
,
691 def
? def
->priv
: NULL
, n_jmp
);
693 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
694 if (jmp
->begin
== jmp
->end
) { /* case N */
696 LLVMConstInt(LLVMInt32Type(), jmp
->begin
, 0),
698 } else if (jmp
->begin
< jmp
->end
) { /* case M..N */
701 } END_FOR_EACH_PTR(jmp
);
703 insn
->target
->priv
= target
;
706 static void output_op_call(struct function
*fn
, struct instruction
*insn
)
708 LLVMValueRef target
, func
;
713 FOR_EACH_PTR(insn
->arguments
, arg
) {
715 } END_FOR_EACH_PTR(arg
);
717 args
= calloc(n_arg
, sizeof(LLVMValueRef
));
720 FOR_EACH_PTR(insn
->arguments
, arg
) {
721 args
[i
++] = pseudo_to_value(fn
, insn
, arg
);
722 } END_FOR_EACH_PTR(arg
);
724 func
= pseudo_to_value(fn
, insn
, insn
->func
);
725 target
= LLVMBuildCall(fn
->builder
, func
, args
, n_arg
, "");
727 insn
->target
->priv
= target
;
730 static void output_op_phisrc(struct function
*fn
, struct instruction
*insn
)
733 struct instruction
*phi
;
735 assert(insn
->target
->priv
== NULL
);
738 v
= pseudo_to_value(fn
, insn
, insn
->phi_src
);
740 FOR_EACH_PTR(insn
->phi_users
, phi
) {
741 LLVMValueRef load
, ptr
;
743 assert(phi
->opcode
== OP_PHI
);
744 /* phi must be load from alloca */
745 load
= phi
->target
->priv
;
746 assert(LLVMGetInstructionOpcode(load
) == LLVMLoad
);
747 ptr
= LLVMGetOperand(load
, 0);
748 /* store v to alloca */
749 LLVMBuildStore(fn
->builder
, v
, ptr
);
750 } END_FOR_EACH_PTR(phi
);
753 static void output_op_phi(struct function
*fn
, struct instruction
*insn
)
755 LLVMValueRef load
= insn
->target
->priv
;
758 assert(LLVMGetInstructionOpcode(load
) == LLVMLoad
);
759 /* forward load has no parent block */
760 assert(!LLVMGetInstructionParent(load
));
761 /* finalize load in current block */
762 LLVMInsertIntoBuilder(fn
->builder
, load
);
765 static void output_op_ptrcast(struct function
*fn
, struct instruction
*insn
)
767 LLVMValueRef src
, target
;
768 char target_name
[64];
770 src
= insn
->src
->priv
;
772 src
= pseudo_to_value(fn
, insn
, insn
->src
);
774 pseudo_name(insn
->target
, target_name
);
776 assert(!symbol_is_fp_type(insn
->type
));
778 target
= LLVMBuildBitCast(fn
->builder
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
780 insn
->target
->priv
= target
;
783 static void output_op_cast(struct function
*fn
, struct instruction
*insn
, LLVMOpcode op
)
785 LLVMValueRef src
, target
;
786 char target_name
[64];
788 src
= insn
->src
->priv
;
790 src
= pseudo_to_value(fn
, insn
, insn
->src
);
792 pseudo_name(insn
->target
, target_name
);
794 assert(!symbol_is_fp_type(insn
->type
));
796 if (insn
->size
< LLVMGetIntTypeWidth(LLVMTypeOf(src
)))
797 target
= LLVMBuildTrunc(fn
->builder
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
799 target
= LLVMBuildCast(fn
->builder
, op
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
801 insn
->target
->priv
= target
;
804 static void output_insn(struct function
*fn
, struct instruction
*insn
)
806 switch (insn
->opcode
) {
808 output_op_ret(fn
, insn
);
811 output_op_br(fn
, insn
);
820 output_op_switch(fn
, insn
);
822 case OP_COMPUTEDGOTO
:
826 output_op_phisrc(fn
, insn
);
829 output_op_phi(fn
, insn
);
832 output_op_load(fn
, insn
);
838 output_op_store(fn
, insn
);
843 case OP_INLINED_CALL
:
847 output_op_call(fn
, insn
);
850 output_op_cast(fn
, insn
, LLVMZExt
);
853 output_op_cast(fn
, insn
, LLVMSExt
);
859 output_op_ptrcast(fn
, insn
);
861 case OP_BINARY
... OP_BINARY_END
:
862 output_op_binary(fn
, insn
);
864 case OP_BINCMP
... OP_BINCMP_END
:
865 output_op_compare(fn
, insn
);
868 output_op_sel(fn
, insn
);
874 LLVMValueRef src
, target
;
875 char target_name
[64];
877 src
= pseudo_to_value(fn
, insn
, insn
->src
);
879 pseudo_name(insn
->target
, target_name
);
881 target
= LLVMBuildNot(fn
->builder
, src
, target_name
);
883 insn
->target
->priv
= target
;
911 static void output_bb(struct function
*fn
, struct basic_block
*bb
, unsigned long generation
)
913 struct instruction
*insn
;
915 bb
->generation
= generation
;
917 FOR_EACH_PTR(bb
->insns
, insn
) {
921 output_insn(fn
, insn
);
923 END_FOR_EACH_PTR(insn
);
928 static void output_fn(LLVMModuleRef module
, struct entrypoint
*ep
)
930 unsigned long generation
= ++bb_generation
;
931 struct symbol
*sym
= ep
->name
;
932 struct symbol
*base_type
= sym
->ctype
.base_type
;
933 struct symbol
*ret_type
= sym
->ctype
.base_type
->ctype
.base_type
;
934 LLVMTypeRef arg_types
[MAX_ARGS
];
935 LLVMTypeRef return_type
;
936 struct function function
= { .module
= module
};
937 struct basic_block
*bb
;
942 FOR_EACH_PTR(base_type
->arguments
, arg
) {
943 struct symbol
*arg_base_type
= arg
->ctype
.base_type
;
945 arg_types
[nr_args
++] = symbol_type(module
, arg_base_type
);
946 } END_FOR_EACH_PTR(arg
);
948 name
= show_ident(sym
->ident
);
950 return_type
= symbol_type(module
, ret_type
);
952 function
.type
= LLVMFunctionType(return_type
, arg_types
, nr_args
, 0);
954 function
.fn
= LLVMAddFunction(module
, name
, function
.type
);
955 LLVMSetFunctionCallConv(function
.fn
, LLVMCCallConv
);
957 LLVMSetLinkage(function
.fn
, function_linkage(sym
));
959 function
.builder
= LLVMCreateBuilder();
963 FOR_EACH_PTR(ep
->bbs
, bb
) {
964 if (bb
->generation
== generation
)
967 LLVMBasicBlockRef bbr
;
969 struct instruction
*insn
;
971 sprintf(bbname
, "L%d", nr_bb
++);
972 bbr
= LLVMAppendBasicBlock(function
.fn
, bbname
);
976 /* allocate alloca for each phi */
977 FOR_EACH_PTR(bb
->insns
, insn
) {
978 LLVMBasicBlockRef entrybbr
;
979 LLVMTypeRef phi_type
;
982 if (!insn
->bb
|| insn
->opcode
!= OP_PHI
)
984 /* insert alloca into entry block */
985 entrybbr
= LLVMGetEntryBasicBlock(function
.fn
);
986 LLVMPositionBuilderAtEnd(function
.builder
, entrybbr
);
987 phi_type
= insn_symbol_type(module
, insn
);
988 ptr
= LLVMBuildAlloca(function
.builder
, phi_type
, "");
989 /* emit forward load for phi */
990 LLVMClearInsertionPosition(function
.builder
);
991 insn
->target
->priv
= LLVMBuildLoad(function
.builder
, ptr
, "phi");
992 } END_FOR_EACH_PTR(insn
);
994 END_FOR_EACH_PTR(bb
);
996 FOR_EACH_PTR(ep
->bbs
, bb
) {
997 if (bb
->generation
== generation
)
1000 LLVMPositionBuilderAtEnd(function
.builder
, bb
->priv
);
1002 output_bb(&function
, bb
, generation
);
1004 END_FOR_EACH_PTR(bb
);
1007 static LLVMValueRef
output_data(LLVMModuleRef module
, struct symbol
*sym
)
1009 struct expression
*initializer
= sym
->initializer
;
1010 LLVMValueRef initial_value
;
1015 switch (initializer
->type
) {
1017 initial_value
= LLVMConstInt(symbol_type(module
, sym
), initializer
->value
, 1);
1020 struct symbol
*sym
= initializer
->symbol
;
1022 initial_value
= LLVMGetNamedGlobal(module
, show_ident(sym
->ident
));
1024 initial_value
= output_data(module
, sym
);
1028 const char *s
= initializer
->string
->data
;
1030 initial_value
= LLVMConstString(strdup(s
), strlen(s
) + 1, true);
1037 LLVMTypeRef type
= symbol_type(module
, sym
);
1039 initial_value
= LLVMConstNull(type
);
1042 name
= show_ident(sym
->ident
);
1044 data
= LLVMAddGlobal(module
, LLVMTypeOf(initial_value
), name
);
1046 LLVMSetLinkage(data
, data_linkage(sym
));
1047 if (sym
->ctype
.modifiers
& MOD_CONST
)
1048 LLVMSetGlobalConstant(data
, 1);
1049 if (sym
->ctype
.modifiers
& MOD_TLS
)
1050 LLVMSetThreadLocal(data
, 1);
1051 if (sym
->ctype
.alignment
)
1052 LLVMSetAlignment(data
, sym
->ctype
.alignment
);
1054 if (!(sym
->ctype
.modifiers
& MOD_EXTERN
))
1055 LLVMSetInitializer(data
, initial_value
);
1060 static int is_prototype(struct symbol
*sym
)
1062 if (sym
->type
== SYM_NODE
)
1063 sym
= sym
->ctype
.base_type
;
1064 return sym
&& sym
->type
== SYM_FN
&& !sym
->stmt
;
1067 static int compile(LLVMModuleRef module
, struct symbol_list
*list
)
1071 FOR_EACH_PTR(list
, sym
) {
1072 struct entrypoint
*ep
;
1075 if (is_prototype(sym
))
1078 ep
= linearize_symbol(sym
);
1080 output_fn(module
, ep
);
1082 output_data(module
, sym
);
1084 END_FOR_EACH_PTR(sym
);
1089 #ifndef LLVM_DEFAULT_TARGET_TRIPLE
1090 #define LLVM_DEFAULT_TARGET_TRIPLE LLVM_HOSTTRIPLE
1093 #define X86_LINUX_LAYOUT \
1094 "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1095 "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-" \
1096 "a0:0:64-f80:32:32-n8:16:32-S128"
1098 #define X86_64_LINUX_LAYOUT \
1099 "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1100 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" \
1101 "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
1103 static void set_target(LLVMModuleRef module
)
1105 char target
[] = LLVM_DEFAULT_TARGET_TRIPLE
;
1106 const char *arch
, *vendor
, *os
, *env
, *layout
= NULL
;
1109 arch
= strtok(target
, "-");
1110 vendor
= strtok(NULL
, "-");
1111 os
= strtok(NULL
, "-");
1112 env
= strtok(NULL
, "-");
1119 if (!strcmp(arch
, "x86_64") && !strcmp(os
, "linux")) {
1121 layout
= X86_64_LINUX_LAYOUT
;
1124 layout
= X86_LINUX_LAYOUT
;
1128 /* unsupported target */
1132 snprintf(triple
, sizeof(triple
), "%s-%s-%s-%s", arch
, vendor
, os
, env
);
1133 LLVMSetTarget(module
, triple
);
1134 LLVMSetDataLayout(module
, layout
);
1137 int main(int argc
, char **argv
)
1139 struct string_list
*filelist
= NULL
;
1140 struct symbol_list
*symlist
;
1141 LLVMModuleRef module
;
1144 symlist
= sparse_initialize(argc
, argv
, &filelist
);
1146 module
= LLVMModuleCreateWithName("sparse");
1149 compile(module
, symlist
);
1151 /* need ->phi_users */
1153 FOR_EACH_PTR_NOTAG(filelist
, file
) {
1154 symlist
= sparse(file
);
1157 compile(module
, symlist
);
1158 } END_FOR_EACH_PTR_NOTAG(file
);
1160 LLVMVerifyModule(module
, LLVMPrintMessageAction
, NULL
);
1162 LLVMWriteBitcodeToFD(module
, STDOUT_FILENO
, 0, 0);
1164 LLVMDisposeModule(module
);