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
int_type_by_size(int size
)
248 case 1: return LLVMInt1Type();
249 case 8: return LLVMInt8Type();
250 case 16: return LLVMInt16Type();
251 case 32: return LLVMInt32Type();
252 case 64: return LLVMInt64Type();
255 die("invalid bit size %d", size
);
258 return NULL
; /* not reached */
261 static LLVMTypeRef
insn_symbol_type(LLVMModuleRef module
, struct instruction
*insn
)
264 return symbol_type(module
, insn
->type
);
266 return int_type_by_size(insn
->size
);
269 static LLVMLinkage
data_linkage(struct symbol
*sym
)
271 if (sym
->ctype
.modifiers
& MOD_STATIC
)
272 return LLVMPrivateLinkage
;
274 return LLVMExternalLinkage
;
277 static LLVMLinkage
function_linkage(struct symbol
*sym
)
279 if (sym
->ctype
.modifiers
& MOD_STATIC
)
280 return LLVMInternalLinkage
;
282 return LLVMExternalLinkage
;
285 #define MAX_PSEUDO_NAME 64
287 static void pseudo_name(pseudo_t pseudo
, char *buf
)
289 switch (pseudo
->type
) {
291 snprintf(buf
, MAX_PSEUDO_NAME
, "R%d", pseudo
->nr
);
304 snprintf(buf
, MAX_PSEUDO_NAME
, "PHI%d", pseudo
->nr
);
311 static LLVMValueRef
pseudo_to_value(struct function
*fn
, struct instruction
*insn
, pseudo_t pseudo
)
313 LLVMValueRef result
= NULL
;
315 switch (pseudo
->type
) {
317 result
= pseudo
->priv
;
320 struct symbol
*sym
= pseudo
->sym
;
321 struct expression
*expr
;
323 assert(sym
->bb_target
== NULL
);
325 expr
= sym
->initializer
;
327 switch (expr
->type
) {
329 const char *s
= expr
->string
->data
;
330 LLVMValueRef indices
[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) };
333 data
= LLVMAddGlobal(fn
->module
, LLVMArrayType(LLVMInt8Type(), strlen(s
) + 1), ".str");
334 LLVMSetLinkage(data
, LLVMPrivateLinkage
);
335 LLVMSetGlobalConstant(data
, 1);
336 LLVMSetInitializer(data
, LLVMConstString(strdup(s
), strlen(s
) + 1, true));
338 result
= LLVMConstGEP(data
, indices
, ARRAY_SIZE(indices
));
342 struct symbol
*sym
= expr
->symbol
;
344 result
= LLVMGetNamedGlobal(fn
->module
, show_ident(sym
->ident
));
345 assert(result
!= NULL
);
352 const char *name
= show_ident(sym
->ident
);
353 LLVMTypeRef type
= symbol_type(fn
->module
, sym
);
355 if (LLVMGetTypeKind(type
) == LLVMFunctionTypeKind
) {
356 result
= LLVMGetNamedFunction(fn
->module
, name
);
358 result
= LLVMAddFunction(fn
->module
, name
, type
);
360 result
= LLVMGetNamedGlobal(fn
->module
, name
);
362 result
= LLVMAddGlobal(fn
->module
, type
, name
);
368 result
= LLVMConstInt(int_type_by_size(pseudo
->size
), pseudo
->value
, 1);
371 result
= LLVMGetParam(fn
->fn
, pseudo
->nr
- 1);
375 result
= pseudo
->priv
;
387 static LLVMValueRef
calc_gep(LLVMBuilderRef builder
, LLVMValueRef base
, LLVMValueRef off
)
389 LLVMTypeRef type
= LLVMTypeOf(base
);
390 unsigned int as
= LLVMGetPointerAddressSpace(type
);
391 LLVMTypeRef bytep
= LLVMPointerType(LLVMInt8Type(), as
);
394 /* convert base to char* type */
395 base
= LLVMBuildPointerCast(builder
, base
, bytep
, "");
396 /* addr = base + off */
397 addr
= LLVMBuildInBoundsGEP(builder
, base
, &off
, 1, "");
398 /* convert back to the actual pointer type */
399 addr
= LLVMBuildPointerCast(builder
, addr
, type
, "");
403 static LLVMRealPredicate
translate_fop(int opcode
)
405 static const LLVMRealPredicate trans_tbl
[] = {
406 [OP_SET_EQ
] = LLVMRealOEQ
,
407 [OP_SET_NE
] = LLVMRealUNE
,
408 [OP_SET_LE
] = LLVMRealOLE
,
409 [OP_SET_GE
] = LLVMRealOGE
,
410 [OP_SET_LT
] = LLVMRealOLT
,
411 [OP_SET_GT
] = LLVMRealOGT
,
412 /* Are these used with FP? */
413 [OP_SET_B
] = LLVMRealOLT
,
414 [OP_SET_A
] = LLVMRealOGT
,
415 [OP_SET_BE
] = LLVMRealOLE
,
416 [OP_SET_AE
] = LLVMRealOGE
,
419 return trans_tbl
[opcode
];
422 static LLVMIntPredicate
translate_op(int opcode
)
424 static const LLVMIntPredicate trans_tbl
[] = {
425 [OP_SET_EQ
] = LLVMIntEQ
,
426 [OP_SET_NE
] = LLVMIntNE
,
427 [OP_SET_LE
] = LLVMIntSLE
,
428 [OP_SET_GE
] = LLVMIntSGE
,
429 [OP_SET_LT
] = LLVMIntSLT
,
430 [OP_SET_GT
] = LLVMIntSGT
,
431 [OP_SET_B
] = LLVMIntULT
,
432 [OP_SET_A
] = LLVMIntUGT
,
433 [OP_SET_BE
] = LLVMIntULE
,
434 [OP_SET_AE
] = LLVMIntUGE
,
437 return trans_tbl
[opcode
];
440 static void output_op_binary(struct function
*fn
, struct instruction
*insn
)
442 LLVMValueRef lhs
, rhs
, target
;
443 char target_name
[64];
445 lhs
= pseudo_to_value(fn
, insn
, insn
->src1
);
447 rhs
= pseudo_to_value(fn
, insn
, insn
->src2
);
449 pseudo_name(insn
->target
, target_name
);
451 switch (insn
->opcode
) {
454 if (symbol_is_fp_type(insn
->type
))
455 target
= LLVMBuildFAdd(fn
->builder
, lhs
, rhs
, target_name
);
457 target
= LLVMBuildAdd(fn
->builder
, lhs
, rhs
, target_name
);
460 if (symbol_is_fp_type(insn
->type
))
461 target
= LLVMBuildFSub(fn
->builder
, lhs
, rhs
, target_name
);
463 target
= LLVMBuildSub(fn
->builder
, lhs
, rhs
, target_name
);
466 if (symbol_is_fp_type(insn
->type
))
467 target
= LLVMBuildFMul(fn
->builder
, lhs
, rhs
, target_name
);
469 target
= LLVMBuildMul(fn
->builder
, lhs
, rhs
, target_name
);
472 assert(!symbol_is_fp_type(insn
->type
));
473 target
= LLVMBuildMul(fn
->builder
, lhs
, rhs
, target_name
);
476 if (symbol_is_fp_type(insn
->type
))
477 target
= LLVMBuildFDiv(fn
->builder
, lhs
, rhs
, target_name
);
479 target
= LLVMBuildUDiv(fn
->builder
, lhs
, rhs
, target_name
);
482 assert(!symbol_is_fp_type(insn
->type
));
483 target
= LLVMBuildSDiv(fn
->builder
, lhs
, rhs
, target_name
);
486 assert(!symbol_is_fp_type(insn
->type
));
487 target
= LLVMBuildURem(fn
->builder
, lhs
, rhs
, target_name
);
490 assert(!symbol_is_fp_type(insn
->type
));
491 target
= LLVMBuildSRem(fn
->builder
, lhs
, rhs
, target_name
);
494 assert(!symbol_is_fp_type(insn
->type
));
495 target
= LLVMBuildShl(fn
->builder
, lhs
, rhs
, target_name
);
498 assert(!symbol_is_fp_type(insn
->type
));
499 target
= LLVMBuildLShr(fn
->builder
, lhs
, rhs
, target_name
);
502 assert(!symbol_is_fp_type(insn
->type
));
503 target
= LLVMBuildAShr(fn
->builder
, lhs
, rhs
, target_name
);
508 assert(!symbol_is_fp_type(insn
->type
));
509 target
= LLVMBuildAnd(fn
->builder
, lhs
, rhs
, target_name
);
512 assert(!symbol_is_fp_type(insn
->type
));
513 target
= LLVMBuildOr(fn
->builder
, lhs
, rhs
, target_name
);
516 assert(!symbol_is_fp_type(insn
->type
));
517 target
= LLVMBuildXor(fn
->builder
, lhs
, rhs
, target_name
);
520 LLVMValueRef lhs_nz
, rhs_nz
;
521 LLVMTypeRef dst_type
;
523 lhs_nz
= LLVMBuildIsNotNull(fn
->builder
, lhs
, "");
524 rhs_nz
= LLVMBuildIsNotNull(fn
->builder
, rhs
, "");
525 target
= LLVMBuildAnd(fn
->builder
, lhs_nz
, rhs_nz
, target_name
);
527 dst_type
= insn_symbol_type(fn
->module
, insn
);
528 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
532 LLVMValueRef lhs_nz
, rhs_nz
;
533 LLVMTypeRef dst_type
;
535 lhs_nz
= LLVMBuildIsNotNull(fn
->builder
, lhs
, "");
536 rhs_nz
= LLVMBuildIsNotNull(fn
->builder
, rhs
, "");
537 target
= LLVMBuildOr(fn
->builder
, lhs_nz
, rhs_nz
, target_name
);
539 dst_type
= insn_symbol_type(fn
->module
, insn
);
540 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
548 insn
->target
->priv
= target
;
551 static void output_op_compare(struct function
*fn
, struct instruction
*insn
)
553 LLVMValueRef lhs
, rhs
, target
;
554 char target_name
[64];
556 lhs
= pseudo_to_value(fn
, insn
, insn
->src1
);
558 if (insn
->src2
->type
== PSEUDO_VAL
)
559 rhs
= LLVMConstInt(LLVMTypeOf(lhs
), insn
->src2
->value
, 1);
561 rhs
= pseudo_to_value(fn
, insn
, insn
->src2
);
563 pseudo_name(insn
->target
, target_name
);
565 LLVMTypeRef dst_type
= insn_symbol_type(fn
->module
, insn
);
567 if (LLVMGetTypeKind(LLVMTypeOf(lhs
)) == LLVMIntegerTypeKind
) {
568 LLVMIntPredicate op
= translate_op(insn
->opcode
);
570 target
= LLVMBuildICmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
572 LLVMRealPredicate op
= translate_fop(insn
->opcode
);
574 target
= LLVMBuildFCmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
577 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
579 insn
->target
->priv
= target
;
582 static void output_op_ret(struct function
*fn
, struct instruction
*insn
)
584 pseudo_t pseudo
= insn
->src
;
586 if (pseudo
&& pseudo
!= VOID
) {
587 LLVMValueRef result
= pseudo_to_value(fn
, insn
, pseudo
);
589 LLVMBuildRet(fn
->builder
, result
);
591 LLVMBuildRetVoid(fn
->builder
);
594 static LLVMValueRef
calc_memop_addr(struct function
*fn
, struct instruction
*insn
)
596 LLVMTypeRef int_type
, addr_type
;
597 LLVMValueRef src
, off
, addr
;
600 /* int type large enough to hold a pointer */
601 int_type
= LLVMIntType(bits_in_pointer
);
602 off
= LLVMConstInt(int_type
, insn
->offset
, 0);
604 /* convert src to the effective pointer type */
605 src
= pseudo_to_value(fn
, insn
, insn
->src
);
606 as
= LLVMGetPointerAddressSpace(LLVMTypeOf(src
));
607 addr_type
= LLVMPointerType(insn_symbol_type(fn
->module
, insn
), as
);
608 src
= LLVMBuildPointerCast(fn
->builder
, src
, addr_type
, "");
610 /* addr = src + off */
611 addr
= calc_gep(fn
->builder
, src
, off
);
616 static void output_op_load(struct function
*fn
, struct instruction
*insn
)
618 LLVMValueRef addr
, target
;
620 addr
= calc_memop_addr(fn
, insn
);
623 target
= LLVMBuildLoad(fn
->builder
, addr
, "load_target");
625 insn
->target
->priv
= target
;
628 static void output_op_store(struct function
*fn
, struct instruction
*insn
)
630 LLVMValueRef addr
, target
, target_in
;
632 addr
= calc_memop_addr(fn
, insn
);
634 target_in
= pseudo_to_value(fn
, insn
, insn
->target
);
637 target
= LLVMBuildStore(fn
->builder
, target_in
, addr
);
639 insn
->target
->priv
= target
;
642 static LLVMValueRef
bool_value(struct function
*fn
, LLVMValueRef value
)
644 if (LLVMTypeOf(value
) != LLVMInt1Type())
645 value
= LLVMBuildIsNotNull(fn
->builder
, value
, "cond");
650 static void output_op_cbr(struct function
*fn
, struct instruction
*br
)
652 LLVMValueRef cond
= bool_value(fn
,
653 pseudo_to_value(fn
, br
, br
->cond
));
655 LLVMBuildCondBr(fn
->builder
, cond
,
660 static void output_op_br(struct function
*fn
, struct instruction
*br
)
662 LLVMBuildBr(fn
->builder
, br
->bb_true
->priv
);
665 static void output_op_sel(struct function
*fn
, struct instruction
*insn
)
667 LLVMValueRef target
, src1
, src2
, src3
;
669 src1
= bool_value(fn
, pseudo_to_value(fn
, insn
, insn
->src1
));
670 src2
= pseudo_to_value(fn
, insn
, insn
->src2
);
671 src3
= pseudo_to_value(fn
, insn
, insn
->src3
);
673 target
= LLVMBuildSelect(fn
->builder
, src1
, src2
, src3
, "select");
675 insn
->target
->priv
= target
;
678 static void output_op_switch(struct function
*fn
, struct instruction
*insn
)
680 LLVMValueRef sw_val
, target
;
681 struct basic_block
*def
= NULL
;
682 struct multijmp
*jmp
;
685 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
686 if (jmp
->begin
== jmp
->end
) { /* case N */
688 } else if (jmp
->begin
< jmp
->end
) { /* case M..N */
690 } else /* default case */
692 } END_FOR_EACH_PTR(jmp
);
694 sw_val
= pseudo_to_value(fn
, insn
, insn
->target
);
695 target
= LLVMBuildSwitch(fn
->builder
, sw_val
,
696 def
? def
->priv
: NULL
, n_jmp
);
698 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
699 if (jmp
->begin
== jmp
->end
) { /* case N */
701 LLVMConstInt(LLVMInt32Type(), jmp
->begin
, 0),
703 } else if (jmp
->begin
< jmp
->end
) { /* case M..N */
706 } END_FOR_EACH_PTR(jmp
);
708 insn
->target
->priv
= target
;
711 static void output_op_call(struct function
*fn
, struct instruction
*insn
)
713 LLVMValueRef target
, func
;
718 FOR_EACH_PTR(insn
->arguments
, arg
) {
720 } END_FOR_EACH_PTR(arg
);
722 args
= calloc(n_arg
, sizeof(LLVMValueRef
));
725 FOR_EACH_PTR(insn
->arguments
, arg
) {
726 args
[i
++] = pseudo_to_value(fn
, insn
, arg
);
727 } END_FOR_EACH_PTR(arg
);
729 func
= pseudo_to_value(fn
, insn
, insn
->func
);
730 target
= LLVMBuildCall(fn
->builder
, func
, args
, n_arg
, "");
732 insn
->target
->priv
= target
;
735 static void output_op_phisrc(struct function
*fn
, struct instruction
*insn
)
738 struct instruction
*phi
;
740 assert(insn
->target
->priv
== NULL
);
743 v
= pseudo_to_value(fn
, insn
, insn
->phi_src
);
745 FOR_EACH_PTR(insn
->phi_users
, phi
) {
746 LLVMValueRef load
, ptr
;
748 assert(phi
->opcode
== OP_PHI
);
749 /* phi must be load from alloca */
750 load
= phi
->target
->priv
;
751 assert(LLVMGetInstructionOpcode(load
) == LLVMLoad
);
752 ptr
= LLVMGetOperand(load
, 0);
753 /* store v to alloca */
754 LLVMBuildStore(fn
->builder
, v
, ptr
);
755 } END_FOR_EACH_PTR(phi
);
758 static void output_op_phi(struct function
*fn
, struct instruction
*insn
)
760 LLVMValueRef load
= insn
->target
->priv
;
763 assert(LLVMGetInstructionOpcode(load
) == LLVMLoad
);
764 /* forward load has no parent block */
765 assert(!LLVMGetInstructionParent(load
));
766 /* finalize load in current block */
767 LLVMInsertIntoBuilder(fn
->builder
, load
);
770 static void output_op_ptrcast(struct function
*fn
, struct instruction
*insn
)
772 LLVMValueRef src
, target
;
773 char target_name
[64];
775 src
= insn
->src
->priv
;
777 src
= pseudo_to_value(fn
, insn
, insn
->src
);
779 pseudo_name(insn
->target
, target_name
);
781 assert(!symbol_is_fp_type(insn
->type
));
783 target
= LLVMBuildBitCast(fn
->builder
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
785 insn
->target
->priv
= target
;
788 static void output_op_cast(struct function
*fn
, struct instruction
*insn
, LLVMOpcode op
)
790 LLVMValueRef src
, target
;
791 char target_name
[64];
793 src
= insn
->src
->priv
;
795 src
= pseudo_to_value(fn
, insn
, insn
->src
);
797 pseudo_name(insn
->target
, target_name
);
799 assert(!symbol_is_fp_type(insn
->type
));
801 if (insn
->size
< LLVMGetIntTypeWidth(LLVMTypeOf(src
)))
802 target
= LLVMBuildTrunc(fn
->builder
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
804 target
= LLVMBuildCast(fn
->builder
, op
, src
, insn_symbol_type(fn
->module
, insn
), target_name
);
806 insn
->target
->priv
= target
;
809 static void output_insn(struct function
*fn
, struct instruction
*insn
)
811 switch (insn
->opcode
) {
813 output_op_ret(fn
, insn
);
816 output_op_br(fn
, insn
);
819 output_op_cbr(fn
, insn
);
828 output_op_switch(fn
, insn
);
830 case OP_COMPUTEDGOTO
:
834 output_op_phisrc(fn
, insn
);
837 output_op_phi(fn
, insn
);
840 output_op_load(fn
, insn
);
846 output_op_store(fn
, insn
);
851 case OP_INLINED_CALL
:
855 output_op_call(fn
, insn
);
858 output_op_cast(fn
, insn
, LLVMZExt
);
861 output_op_cast(fn
, insn
, LLVMSExt
);
867 output_op_ptrcast(fn
, insn
);
869 case OP_BINARY
... OP_BINARY_END
:
870 output_op_binary(fn
, insn
);
872 case OP_BINCMP
... OP_BINCMP_END
:
873 output_op_compare(fn
, insn
);
876 output_op_sel(fn
, insn
);
882 LLVMValueRef src
, target
;
883 char target_name
[64];
885 src
= pseudo_to_value(fn
, insn
, insn
->src
);
887 pseudo_name(insn
->target
, target_name
);
889 target
= LLVMBuildNot(fn
->builder
, src
, target_name
);
891 insn
->target
->priv
= target
;
919 static void output_bb(struct function
*fn
, struct basic_block
*bb
, unsigned long generation
)
921 struct instruction
*insn
;
923 bb
->generation
= generation
;
925 FOR_EACH_PTR(bb
->insns
, insn
) {
929 output_insn(fn
, insn
);
931 END_FOR_EACH_PTR(insn
);
936 static void output_fn(LLVMModuleRef module
, struct entrypoint
*ep
)
938 unsigned long generation
= ++bb_generation
;
939 struct symbol
*sym
= ep
->name
;
940 struct symbol
*base_type
= sym
->ctype
.base_type
;
941 struct symbol
*ret_type
= sym
->ctype
.base_type
->ctype
.base_type
;
942 LLVMTypeRef arg_types
[MAX_ARGS
];
943 LLVMTypeRef return_type
;
944 struct function function
= { .module
= module
};
945 struct basic_block
*bb
;
950 FOR_EACH_PTR(base_type
->arguments
, arg
) {
951 struct symbol
*arg_base_type
= arg
->ctype
.base_type
;
953 arg_types
[nr_args
++] = symbol_type(module
, arg_base_type
);
954 } END_FOR_EACH_PTR(arg
);
956 name
= show_ident(sym
->ident
);
958 return_type
= symbol_type(module
, ret_type
);
960 function
.type
= LLVMFunctionType(return_type
, arg_types
, nr_args
, 0);
962 function
.fn
= LLVMAddFunction(module
, name
, function
.type
);
963 LLVMSetFunctionCallConv(function
.fn
, LLVMCCallConv
);
965 LLVMSetLinkage(function
.fn
, function_linkage(sym
));
967 function
.builder
= LLVMCreateBuilder();
971 FOR_EACH_PTR(ep
->bbs
, bb
) {
972 if (bb
->generation
== generation
)
975 LLVMBasicBlockRef bbr
;
977 struct instruction
*insn
;
979 sprintf(bbname
, "L%d", nr_bb
++);
980 bbr
= LLVMAppendBasicBlock(function
.fn
, bbname
);
984 /* allocate alloca for each phi */
985 FOR_EACH_PTR(bb
->insns
, insn
) {
986 LLVMBasicBlockRef entrybbr
;
987 LLVMTypeRef phi_type
;
990 if (!insn
->bb
|| insn
->opcode
!= OP_PHI
)
992 /* insert alloca into entry block */
993 entrybbr
= LLVMGetEntryBasicBlock(function
.fn
);
994 LLVMPositionBuilderAtEnd(function
.builder
, entrybbr
);
995 phi_type
= insn_symbol_type(module
, insn
);
996 ptr
= LLVMBuildAlloca(function
.builder
, phi_type
, "");
997 /* emit forward load for phi */
998 LLVMClearInsertionPosition(function
.builder
);
999 insn
->target
->priv
= LLVMBuildLoad(function
.builder
, ptr
, "phi");
1000 } END_FOR_EACH_PTR(insn
);
1002 END_FOR_EACH_PTR(bb
);
1004 FOR_EACH_PTR(ep
->bbs
, bb
) {
1005 if (bb
->generation
== generation
)
1008 LLVMPositionBuilderAtEnd(function
.builder
, bb
->priv
);
1010 output_bb(&function
, bb
, generation
);
1012 END_FOR_EACH_PTR(bb
);
1015 static LLVMValueRef
output_data(LLVMModuleRef module
, struct symbol
*sym
)
1017 struct expression
*initializer
= sym
->initializer
;
1018 LLVMValueRef initial_value
;
1023 switch (initializer
->type
) {
1025 initial_value
= LLVMConstInt(symbol_type(module
, sym
), initializer
->value
, 1);
1028 struct symbol
*sym
= initializer
->symbol
;
1030 initial_value
= LLVMGetNamedGlobal(module
, show_ident(sym
->ident
));
1032 initial_value
= output_data(module
, sym
);
1036 const char *s
= initializer
->string
->data
;
1038 initial_value
= LLVMConstString(strdup(s
), strlen(s
) + 1, true);
1045 LLVMTypeRef type
= symbol_type(module
, sym
);
1047 initial_value
= LLVMConstNull(type
);
1050 name
= show_ident(sym
->ident
);
1052 data
= LLVMAddGlobal(module
, LLVMTypeOf(initial_value
), name
);
1054 LLVMSetLinkage(data
, data_linkage(sym
));
1055 if (sym
->ctype
.modifiers
& MOD_CONST
)
1056 LLVMSetGlobalConstant(data
, 1);
1057 if (sym
->ctype
.modifiers
& MOD_TLS
)
1058 LLVMSetThreadLocal(data
, 1);
1059 if (sym
->ctype
.alignment
)
1060 LLVMSetAlignment(data
, sym
->ctype
.alignment
);
1062 if (!(sym
->ctype
.modifiers
& MOD_EXTERN
))
1063 LLVMSetInitializer(data
, initial_value
);
1068 static int is_prototype(struct symbol
*sym
)
1070 if (sym
->type
== SYM_NODE
)
1071 sym
= sym
->ctype
.base_type
;
1072 return sym
&& sym
->type
== SYM_FN
&& !sym
->stmt
;
1075 static int compile(LLVMModuleRef module
, struct symbol_list
*list
)
1079 FOR_EACH_PTR(list
, sym
) {
1080 struct entrypoint
*ep
;
1083 if (is_prototype(sym
))
1086 ep
= linearize_symbol(sym
);
1088 output_fn(module
, ep
);
1090 output_data(module
, sym
);
1092 END_FOR_EACH_PTR(sym
);
1097 #ifndef LLVM_DEFAULT_TARGET_TRIPLE
1098 #define LLVM_DEFAULT_TARGET_TRIPLE LLVM_HOSTTRIPLE
1101 #define X86_LINUX_LAYOUT \
1102 "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1103 "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-" \
1104 "a0:0:64-f80:32:32-n8:16:32-S128"
1106 #define X86_64_LINUX_LAYOUT \
1107 "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1108 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" \
1109 "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
1111 static void set_target(LLVMModuleRef module
)
1113 char target
[] = LLVM_DEFAULT_TARGET_TRIPLE
;
1114 const char *arch
, *vendor
, *os
, *env
, *layout
= NULL
;
1117 arch
= strtok(target
, "-");
1118 vendor
= strtok(NULL
, "-");
1119 os
= strtok(NULL
, "-");
1120 env
= strtok(NULL
, "-");
1127 if (!strcmp(arch
, "x86_64") && !strcmp(os
, "linux")) {
1129 layout
= X86_64_LINUX_LAYOUT
;
1132 layout
= X86_LINUX_LAYOUT
;
1136 /* unsupported target */
1140 snprintf(triple
, sizeof(triple
), "%s-%s-%s-%s", arch
, vendor
, os
, env
);
1141 LLVMSetTarget(module
, triple
);
1142 LLVMSetDataLayout(module
, layout
);
1145 int main(int argc
, char **argv
)
1147 struct string_list
*filelist
= NULL
;
1148 struct symbol_list
*symlist
;
1149 LLVMModuleRef module
;
1152 symlist
= sparse_initialize(argc
, argv
, &filelist
);
1154 module
= LLVMModuleCreateWithName("sparse");
1157 compile(module
, symlist
);
1159 /* need ->phi_users */
1161 FOR_EACH_PTR_NOTAG(filelist
, file
) {
1162 symlist
= sparse(file
);
1165 compile(module
, symlist
);
1166 } END_FOR_EACH_PTR_NOTAG(file
);
1168 LLVMVerifyModule(module
, LLVMPrintMessageAction
, NULL
);
1170 LLVMWriteBitcodeToFD(module
, STDOUT_FILENO
, 0, 0);
1172 LLVMDisposeModule(module
);