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
;
28 static LLVMTypeRef
symbol_type(struct symbol
*sym
);
30 static LLVMTypeRef
func_return_type(struct symbol
*sym
)
32 return symbol_type(sym
->ctype
.base_type
);
35 static LLVMTypeRef
sym_func_type(struct symbol
*sym
)
37 int n_arg
= symbol_list_size(sym
->arguments
);
38 LLVMTypeRef
*arg_type
= calloc(n_arg
, sizeof(LLVMTypeRef
));
39 LLVMTypeRef ret_type
= func_return_type(sym
);
43 FOR_EACH_PTR(sym
->arguments
, arg
) {
44 struct symbol
*arg_sym
= arg
->ctype
.base_type
;
46 arg_type
[idx
++] = symbol_type(arg_sym
);
47 } END_FOR_EACH_PTR(arg
);
49 return LLVMFunctionType(ret_type
, arg_type
, n_arg
, sym
->variadic
);
52 static LLVMTypeRef
sym_array_type(struct symbol
*sym
)
54 LLVMTypeRef elem_type
;
55 struct symbol
*base_type
;
57 base_type
= sym
->ctype
.base_type
;
58 /* empty struct is undefined [6.7.2.1(8)] */
59 assert(base_type
->bit_size
> 0);
61 elem_type
= symbol_type(base_type
);
65 return LLVMArrayType(elem_type
, sym
->bit_size
/ base_type
->bit_size
);
68 #define MAX_STRUCT_MEMBERS 64
70 static LLVMTypeRef
sym_struct_type(struct symbol
*sym
)
72 LLVMTypeRef elem_types
[MAX_STRUCT_MEMBERS
];
73 struct symbol
*member
;
78 snprintf(buffer
, sizeof(buffer
), "struct.%s", sym
->ident
? sym
->ident
->name
: "anno");
79 ret
= LLVMStructCreateNamed(LLVMGetGlobalContext(), buffer
);
80 /* set ->aux to avoid recursion */
83 FOR_EACH_PTR(sym
->symbol_list
, member
) {
84 LLVMTypeRef member_type
;
86 assert(nr
< MAX_STRUCT_MEMBERS
);
88 member_type
= symbol_type(member
);
90 elem_types
[nr
++] = member_type
;
91 } END_FOR_EACH_PTR(member
);
93 LLVMStructSetBody(ret
, elem_types
, nr
, 0 /* packed? */);
97 static LLVMTypeRef
sym_union_type(struct symbol
*sym
)
103 * There's no union support in the LLVM API so we treat unions as
104 * opaque structs. The downside is that we lose type information on the
105 * members but as LLVM doesn't care, neither do we.
107 union_size
= sym
->bit_size
/ 8;
109 elements
= LLVMArrayType(LLVMInt8Type(), union_size
);
111 return LLVMStructType(&elements
, 1, 0 /* packed? */);
114 static LLVMTypeRef
sym_ptr_type(struct symbol
*sym
)
118 /* 'void *' is treated like 'char *' */
119 if (is_void_type(sym
->ctype
.base_type
))
120 type
= LLVMInt8Type();
122 type
= symbol_type(sym
->ctype
.base_type
);
124 return LLVMPointerType(type
, 0);
127 static LLVMTypeRef
sym_basetype_type(struct symbol
*sym
)
129 LLVMTypeRef ret
= NULL
;
131 if (is_float_type(sym
)) {
132 switch (sym
->bit_size
) {
134 ret
= LLVMFloatType();
137 ret
= LLVMDoubleType();
140 ret
= LLVMX86FP80Type();
143 die("invalid bit size %d for type %d", sym
->bit_size
, sym
->type
);
147 switch (sym
->bit_size
) {
149 ret
= LLVMVoidType();
152 ret
= LLVMInt1Type();
155 ret
= LLVMInt8Type();
158 ret
= LLVMInt16Type();
161 ret
= LLVMInt32Type();
164 ret
= LLVMInt64Type();
167 die("invalid bit size %d for type %d", sym
->bit_size
, sym
->type
);
175 static LLVMTypeRef
symbol_type(struct symbol
*sym
)
177 LLVMTypeRef ret
= NULL
;
179 /* don't cache the result for SYM_NODE */
180 if (sym
->type
== SYM_NODE
)
181 return symbol_type(sym
->ctype
.base_type
);
188 ret
= LLVMIntType(sym
->bit_size
);
192 ret
= symbol_type(sym
->ctype
.base_type
);
195 ret
= sym_basetype_type(sym
);
198 ret
= sym_ptr_type(sym
);
201 ret
= sym_union_type(sym
);
204 ret
= sym_struct_type(sym
);
207 ret
= sym_array_type(sym
);
210 ret
= sym_func_type(sym
);
216 /* cache the result */
221 static LLVMTypeRef
insn_symbol_type(struct instruction
*insn
)
224 return symbol_type(insn
->type
);
226 switch (insn
->size
) {
227 case 8: return LLVMInt8Type();
228 case 16: return LLVMInt16Type();
229 case 32: return LLVMInt32Type();
230 case 64: return LLVMInt64Type();
233 die("invalid bit size %d", insn
->size
);
237 return NULL
; /* not reached */
240 static LLVMLinkage
data_linkage(struct symbol
*sym
)
242 if (sym
->ctype
.modifiers
& MOD_STATIC
)
243 return LLVMPrivateLinkage
;
245 return LLVMExternalLinkage
;
248 static LLVMLinkage
function_linkage(struct symbol
*sym
)
250 if (sym
->ctype
.modifiers
& MOD_STATIC
)
251 return LLVMInternalLinkage
;
253 return LLVMExternalLinkage
;
256 #define MAX_PSEUDO_NAME 64
258 static const char *pseudo_name(pseudo_t pseudo
, char *buf
)
260 switch (pseudo
->type
) {
262 snprintf(buf
, MAX_PSEUDO_NAME
, "R%d.", pseudo
->nr
);
265 snprintf(buf
, MAX_PSEUDO_NAME
, "PHI%d.", pseudo
->nr
);
283 static LLVMValueRef
get_sym_value(LLVMModuleRef module
, struct symbol
*sym
)
285 const char *name
= show_ident(sym
->ident
);
286 LLVMTypeRef type
= symbol_type(sym
);
287 LLVMValueRef result
= NULL
;
288 struct expression
*expr
;
290 assert(sym
->bb_target
== NULL
);
292 expr
= sym
->initializer
;
293 if (expr
&& !sym
->ident
) {
294 switch (expr
->type
) {
296 const char *s
= expr
->string
->data
;
297 LLVMValueRef indices
[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) };
300 data
= LLVMAddGlobal(module
, LLVMArrayType(LLVMInt8Type(), strlen(s
) + 1), ".str");
301 LLVMSetLinkage(data
, LLVMPrivateLinkage
);
302 LLVMSetGlobalConstant(data
, 1);
303 LLVMSetInitializer(data
, LLVMConstString(strdup(s
), strlen(s
) + 1, true));
305 result
= LLVMConstGEP(data
, indices
, ARRAY_SIZE(indices
));
313 if (LLVMGetTypeKind(type
) == LLVMFunctionTypeKind
) {
314 result
= LLVMGetNamedFunction(module
, name
);
316 result
= LLVMAddFunction(module
, name
, type
);
318 result
= LLVMGetNamedGlobal(module
, name
);
320 result
= LLVMAddGlobal(module
, type
, name
);
326 static LLVMValueRef
constant_value(unsigned long long val
, LLVMTypeRef dtype
)
330 switch (LLVMGetTypeKind(dtype
)) {
331 case LLVMPointerTypeKind
:
332 if (val
!= 0) { // for example: ... = (void*) 0x123;
333 LLVMTypeRef itype
= LLVMIntType(bits_in_pointer
);
334 result
= LLVMConstInt(itype
, val
, 1);
335 result
= LLVMConstIntToPtr(result
, dtype
);
337 result
= LLVMConstPointerNull(dtype
);
340 case LLVMIntegerTypeKind
:
341 result
= LLVMConstInt(dtype
, val
, 1);
343 case LLVMArrayTypeKind
:
344 case LLVMStructTypeKind
:
347 result
= LLVMConstNull(dtype
);
355 static LLVMValueRef
val_to_value(unsigned long long val
, struct symbol
*ctype
)
361 dtype
= symbol_type(ctype
);
362 result
= constant_value(val
, dtype
);
365 sparse_error(ctype
->pos
, "no value possible for %s", show_typename(ctype
));
366 return LLVMGetUndef(symbol_type(ctype
));
369 static LLVMValueRef
pseudo_to_value(struct function
*fn
, struct symbol
*ctype
, pseudo_t pseudo
)
371 LLVMValueRef result
= NULL
;
373 switch (pseudo
->type
) {
375 result
= pseudo
->priv
;
378 result
= get_sym_value(fn
->module
, pseudo
->sym
);
381 result
= val_to_value(pseudo
->value
, ctype
);
384 result
= LLVMGetParam(fn
->fn
, pseudo
->nr
- 1);
388 result
= pseudo
->priv
;
394 result
= LLVMGetUndef(symbol_type(ctype
));
403 static LLVMValueRef
pseudo_to_rvalue(struct function
*fn
, struct symbol
*ctype
, pseudo_t pseudo
)
405 LLVMValueRef val
= pseudo_to_value(fn
, ctype
, pseudo
);
406 LLVMTypeRef dtype
= symbol_type(ctype
);
407 char name
[MAX_PSEUDO_NAME
];
409 pseudo_name(pseudo
, name
);
410 return LLVMBuildBitCast(fn
->builder
, val
, dtype
, name
);
413 static LLVMValueRef
value_to_ivalue(struct function
*fn
, struct symbol
*ctype
, LLVMValueRef val
)
415 const char *name
= LLVMGetValueName(val
);
416 LLVMTypeRef dtype
= symbol_type(ctype
);
418 if (LLVMGetTypeKind(LLVMTypeOf(val
)) == LLVMPointerTypeKind
) {
419 LLVMTypeRef dtype
= LLVMIntType(bits_in_pointer
);
420 val
= LLVMBuildPtrToInt(fn
->builder
, val
, dtype
, name
);
422 if (ctype
&& is_int_type(ctype
)) {
423 val
= LLVMBuildIntCast(fn
->builder
, val
, dtype
, name
);
428 static LLVMValueRef
value_to_pvalue(struct function
*fn
, struct symbol
*ctype
, LLVMValueRef val
)
430 const char *name
= LLVMGetValueName(val
);
431 LLVMTypeRef dtype
= symbol_type(ctype
);
433 assert(is_ptr_type(ctype
));
434 switch (LLVMGetTypeKind(LLVMTypeOf(val
))) {
435 case LLVMIntegerTypeKind
:
436 val
= LLVMBuildIntToPtr(fn
->builder
, val
, dtype
, name
);
438 case LLVMPointerTypeKind
:
439 val
= LLVMBuildBitCast(fn
->builder
, val
, dtype
, name
);
447 static LLVMValueRef
adjust_type(struct function
*fn
, struct symbol
*ctype
, LLVMValueRef val
)
449 if (is_int_type(ctype
))
450 return value_to_ivalue(fn
, ctype
, val
);
451 if (is_ptr_type(ctype
))
452 return value_to_pvalue(fn
, ctype
, val
);
457 * Get the LLVMValue corresponding to the pseudo
458 * and force the type corresponding to ctype.
460 static LLVMValueRef
get_operand(struct function
*fn
, struct symbol
*ctype
, pseudo_t pseudo
)
462 LLVMValueRef target
= pseudo_to_value(fn
, ctype
, pseudo
);
463 return adjust_type(fn
, ctype
, target
);
467 * Get the LLVMValue corresponding to the pseudo
468 * and force the type corresponding to ctype but
469 * map all pointers to intptr_t.
471 static LLVMValueRef
get_ioperand(struct function
*fn
, struct symbol
*ctype
, pseudo_t pseudo
)
473 LLVMValueRef target
= pseudo_to_value(fn
, ctype
, pseudo
);
474 return value_to_ivalue(fn
, ctype
, target
);
477 static LLVMValueRef
calc_gep(LLVMBuilderRef builder
, LLVMValueRef base
, LLVMValueRef off
)
479 LLVMTypeRef type
= LLVMTypeOf(base
);
480 unsigned int as
= LLVMGetPointerAddressSpace(type
);
481 LLVMTypeRef bytep
= LLVMPointerType(LLVMInt8Type(), as
);
483 const char *name
= LLVMGetValueName(off
);
485 /* convert base to char* type */
486 base
= LLVMBuildPointerCast(builder
, base
, bytep
, name
);
487 /* addr = base + off */
488 addr
= LLVMBuildInBoundsGEP(builder
, base
, &off
, 1, name
);
489 /* convert back to the actual pointer type */
490 addr
= LLVMBuildPointerCast(builder
, addr
, type
, name
);
494 static LLVMRealPredicate
translate_fop(int opcode
)
496 static const LLVMRealPredicate trans_tbl
[] = {
497 [OP_FCMP_ORD
] = LLVMRealORD
,
498 [OP_FCMP_OEQ
] = LLVMRealOEQ
,
499 [OP_FCMP_ONE
] = LLVMRealONE
,
500 [OP_FCMP_OLE
] = LLVMRealOLE
,
501 [OP_FCMP_OGE
] = LLVMRealOGE
,
502 [OP_FCMP_OLT
] = LLVMRealOLT
,
503 [OP_FCMP_OGT
] = LLVMRealOGT
,
504 [OP_FCMP_UEQ
] = LLVMRealUEQ
,
505 [OP_FCMP_UNE
] = LLVMRealUNE
,
506 [OP_FCMP_ULE
] = LLVMRealULE
,
507 [OP_FCMP_UGE
] = LLVMRealUGE
,
508 [OP_FCMP_ULT
] = LLVMRealULT
,
509 [OP_FCMP_UGT
] = LLVMRealUGT
,
510 [OP_FCMP_UNO
] = LLVMRealUNO
,
513 return trans_tbl
[opcode
];
516 static LLVMIntPredicate
translate_op(int opcode
)
518 static const LLVMIntPredicate trans_tbl
[] = {
519 [OP_SET_EQ
] = LLVMIntEQ
,
520 [OP_SET_NE
] = LLVMIntNE
,
521 [OP_SET_LE
] = LLVMIntSLE
,
522 [OP_SET_GE
] = LLVMIntSGE
,
523 [OP_SET_LT
] = LLVMIntSLT
,
524 [OP_SET_GT
] = LLVMIntSGT
,
525 [OP_SET_B
] = LLVMIntULT
,
526 [OP_SET_A
] = LLVMIntUGT
,
527 [OP_SET_BE
] = LLVMIntULE
,
528 [OP_SET_AE
] = LLVMIntUGE
,
531 return trans_tbl
[opcode
];
534 static void output_op_binary(struct function
*fn
, struct instruction
*insn
)
536 LLVMValueRef lhs
, rhs
, target
;
537 char target_name
[64];
539 lhs
= get_ioperand(fn
, insn
->type
, insn
->src1
);
540 rhs
= get_ioperand(fn
, insn
->type
, insn
->src2
);
542 pseudo_name(insn
->target
, target_name
);
544 switch (insn
->opcode
) {
547 target
= LLVMBuildAdd(fn
->builder
, lhs
, rhs
, target_name
);
550 target
= LLVMBuildSub(fn
->builder
, lhs
, rhs
, target_name
);
553 target
= LLVMBuildMul(fn
->builder
, lhs
, rhs
, target_name
);
556 target
= LLVMBuildUDiv(fn
->builder
, lhs
, rhs
, target_name
);
559 assert(!is_float_type(insn
->type
));
560 target
= LLVMBuildSDiv(fn
->builder
, lhs
, rhs
, target_name
);
563 assert(!is_float_type(insn
->type
));
564 target
= LLVMBuildURem(fn
->builder
, lhs
, rhs
, target_name
);
567 assert(!is_float_type(insn
->type
));
568 target
= LLVMBuildSRem(fn
->builder
, lhs
, rhs
, target_name
);
571 assert(!is_float_type(insn
->type
));
572 target
= LLVMBuildShl(fn
->builder
, lhs
, rhs
, target_name
);
575 assert(!is_float_type(insn
->type
));
576 target
= LLVMBuildLShr(fn
->builder
, lhs
, rhs
, target_name
);
579 assert(!is_float_type(insn
->type
));
580 target
= LLVMBuildAShr(fn
->builder
, lhs
, rhs
, target_name
);
585 target
= LLVMBuildFAdd(fn
->builder
, lhs
, rhs
, target_name
);
588 target
= LLVMBuildFSub(fn
->builder
, lhs
, rhs
, target_name
);
591 target
= LLVMBuildFMul(fn
->builder
, lhs
, rhs
, target_name
);
594 target
= LLVMBuildFDiv(fn
->builder
, lhs
, rhs
, target_name
);
599 assert(!is_float_type(insn
->type
));
600 target
= LLVMBuildAnd(fn
->builder
, lhs
, rhs
, target_name
);
603 assert(!is_float_type(insn
->type
));
604 target
= LLVMBuildOr(fn
->builder
, lhs
, rhs
, target_name
);
607 assert(!is_float_type(insn
->type
));
608 target
= LLVMBuildXor(fn
->builder
, lhs
, rhs
, target_name
);
615 target
= adjust_type(fn
, insn
->type
, target
);
616 insn
->target
->priv
= target
;
619 static void output_op_compare(struct function
*fn
, struct instruction
*insn
)
621 LLVMValueRef lhs
, rhs
, target
;
622 char target_name
[64];
624 lhs
= pseudo_to_value(fn
, NULL
, insn
->src1
);
625 if (insn
->src2
->type
== PSEUDO_VAL
)
626 rhs
= constant_value(insn
->src2
->value
, LLVMTypeOf(lhs
));
628 rhs
= pseudo_to_value(fn
, NULL
, insn
->src2
);
630 rhs
= LLVMGetUndef(symbol_type(insn
->type
));
632 pseudo_name(insn
->target
, target_name
);
634 LLVMTypeRef dst_type
= insn_symbol_type(insn
);
636 switch (LLVMGetTypeKind(LLVMTypeOf(lhs
))) {
637 case LLVMPointerTypeKind
:
638 lhs
= value_to_pvalue(fn
, &ptr_ctype
, lhs
);
639 rhs
= value_to_pvalue(fn
, &ptr_ctype
, rhs
);
642 case LLVMIntegerTypeKind
: {
643 LLVMIntPredicate op
= translate_op(insn
->opcode
);
645 if (LLVMGetTypeKind(LLVMTypeOf(rhs
)) == LLVMPointerTypeKind
) {
646 LLVMTypeRef ltype
= LLVMTypeOf(lhs
);
647 rhs
= LLVMBuildPtrToInt(fn
->builder
, rhs
, ltype
, "");
649 target
= LLVMBuildICmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
652 case LLVMHalfTypeKind
:
653 case LLVMFloatTypeKind
:
654 case LLVMDoubleTypeKind
:
655 case LLVMX86_FP80TypeKind
:
656 case LLVMFP128TypeKind
:
657 case LLVMPPC_FP128TypeKind
: {
658 LLVMRealPredicate op
= translate_fop(insn
->opcode
);
660 target
= LLVMBuildFCmp(fn
->builder
, op
, lhs
, rhs
, target_name
);
667 target
= LLVMBuildZExt(fn
->builder
, target
, dst_type
, target_name
);
669 insn
->target
->priv
= target
;
672 static void output_op_ret(struct function
*fn
, struct instruction
*insn
)
674 pseudo_t pseudo
= insn
->src
;
676 if (pseudo
&& pseudo
!= VOID
) {
677 LLVMValueRef result
= get_operand(fn
, insn
->type
, pseudo
);
678 LLVMBuildRet(fn
->builder
, result
);
680 LLVMBuildRetVoid(fn
->builder
);
683 static LLVMValueRef
calc_memop_addr(struct function
*fn
, struct instruction
*insn
)
685 LLVMTypeRef int_type
, addr_type
;
686 LLVMValueRef src
, off
, addr
;
689 /* int type large enough to hold a pointer */
690 int_type
= LLVMIntType(bits_in_pointer
);
691 off
= LLVMConstInt(int_type
, insn
->offset
, 0);
693 /* convert src to the effective pointer type */
694 src
= pseudo_to_value(fn
, insn
->type
, insn
->src
);
695 as
= LLVMGetPointerAddressSpace(LLVMTypeOf(src
));
696 addr_type
= LLVMPointerType(insn_symbol_type(insn
), as
);
697 src
= LLVMBuildPointerCast(fn
->builder
, src
, addr_type
, LLVMGetValueName(src
));
699 /* addr = src + off */
700 addr
= calc_gep(fn
->builder
, src
, off
);
705 static void output_op_load(struct function
*fn
, struct instruction
*insn
)
707 LLVMValueRef addr
, target
;
708 char name
[MAX_PSEUDO_NAME
];
710 addr
= calc_memop_addr(fn
, insn
);
713 pseudo_name(insn
->target
, name
);
714 target
= LLVMBuildLoad(fn
->builder
, addr
, name
);
716 insn
->target
->priv
= target
;
719 static void output_op_store(struct function
*fn
, struct instruction
*insn
)
721 LLVMValueRef addr
, target_in
;
723 addr
= calc_memop_addr(fn
, insn
);
725 target_in
= pseudo_to_rvalue(fn
, insn
->type
, insn
->target
);
728 LLVMBuildStore(fn
->builder
, target_in
, addr
);
731 static LLVMValueRef
bool_value(struct function
*fn
, LLVMValueRef value
)
733 if (LLVMTypeOf(value
) != LLVMInt1Type())
734 value
= LLVMBuildIsNotNull(fn
->builder
, value
, LLVMGetValueName(value
));
739 static void output_op_cbr(struct function
*fn
, struct instruction
*br
)
741 LLVMValueRef cond
= bool_value(fn
,
742 pseudo_to_value(fn
, NULL
, br
->cond
));
744 LLVMBuildCondBr(fn
->builder
, cond
,
749 static void output_op_br(struct function
*fn
, struct instruction
*br
)
751 LLVMBuildBr(fn
->builder
, br
->bb_true
->priv
);
754 static void output_op_sel(struct function
*fn
, struct instruction
*insn
)
756 LLVMValueRef target
, src1
, src2
, src3
;
757 char name
[MAX_PSEUDO_NAME
];
759 src1
= bool_value(fn
, pseudo_to_value(fn
, NULL
, insn
->src1
));
760 src2
= get_operand(fn
, insn
->type
, insn
->src2
);
761 src3
= get_operand(fn
, insn
->type
, insn
->src3
);
763 pseudo_name(insn
->target
, name
);
764 target
= LLVMBuildSelect(fn
->builder
, src1
, src2
, src3
, name
);
766 insn
->target
->priv
= adjust_type(fn
, insn
->type
, target
);
769 static void output_op_switch(struct function
*fn
, struct instruction
*insn
)
771 LLVMValueRef sw_val
, target
;
772 struct basic_block
*def
= NULL
;
773 struct multijmp
*jmp
;
776 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
777 if (jmp
->begin
<= jmp
->end
) {
778 n_jmp
+= (jmp
->end
- jmp
->begin
) + 1;
779 } else /* default case */
781 } END_FOR_EACH_PTR(jmp
);
783 sw_val
= get_ioperand(fn
, insn
->type
, insn
->cond
);
784 target
= LLVMBuildSwitch(fn
->builder
, sw_val
,
785 def
? def
->priv
: NULL
, n_jmp
);
787 FOR_EACH_PTR(insn
->multijmp_list
, jmp
) {
790 for (val
= jmp
->begin
; val
<= jmp
->end
; val
++) {
791 LLVMValueRef Val
= val_to_value(val
, insn
->type
);
792 LLVMAddCase(target
, Val
, jmp
->target
->priv
);
794 } END_FOR_EACH_PTR(jmp
);
797 static void output_op_call(struct function
*fn
, struct instruction
*insn
)
799 LLVMValueRef target
, func
;
800 struct symbol
*ctype
;
806 n_arg
= pseudo_list_size(insn
->arguments
);
807 args
= calloc(n_arg
, sizeof(LLVMValueRef
));
809 PREPARE_PTR_LIST(insn
->fntypes
, ctype
);
810 if (insn
->func
->type
== PSEUDO_REG
|| insn
->func
->type
== PSEUDO_PHI
)
811 func
= get_operand(fn
, ctype
, insn
->func
);
813 func
= pseudo_to_value(fn
, ctype
, insn
->func
);
815 FOR_EACH_PTR(insn
->arguments
, arg
) {
816 NEXT_PTR_LIST(ctype
);
817 args
[i
++] = pseudo_to_rvalue(fn
, ctype
, arg
);
818 } END_FOR_EACH_PTR(arg
);
819 FINISH_PTR_LIST(ctype
);
821 pseudo_name(insn
->target
, name
);
822 target
= LLVMBuildCall(fn
->builder
, func
, args
, n_arg
, name
);
824 insn
->target
->priv
= target
;
827 static void output_op_phisrc(struct function
*fn
, struct instruction
*insn
)
829 insn
->src
->priv
= get_operand(fn
, insn
->type
, insn
->src
);
832 static void output_op_phi(struct function
*fn
, struct instruction
*insn
)
834 LLVMTypeRef dst_type
= insn_symbol_type(insn
);
836 insn
->target
->priv
= LLVMBuildPhi(fn
->builder
, dst_type
, "");
839 static void output_op_ptrcast(struct function
*fn
, struct instruction
*insn
)
841 LLVMValueRef src
, target
;
843 struct symbol
*otype
= insn
->orig_type
;
845 char target_name
[64];
847 src
= get_operand(fn
, otype
, insn
->src
);
848 pseudo_name(insn
->target
, target_name
);
850 dtype
= symbol_type(insn
->type
);
851 switch (insn
->opcode
) {
853 case OP_SEXT
: // FIXME
854 assert(is_int_type(otype
));
855 assert(is_ptr_type(insn
->type
));
859 assert(is_ptr_type(otype
));
860 assert(is_int_type(insn
->type
));
864 case OP_ZEXT
: // FIXME
865 assert(is_ptr_type(otype
));
866 assert(is_ptr_type(insn
->type
));
873 target
= LLVMBuildCast(fn
->builder
, op
, src
, dtype
, target_name
);
874 insn
->target
->priv
= target
;
877 static void output_op_cast(struct function
*fn
, struct instruction
*insn
, LLVMOpcode op
)
879 LLVMValueRef src
, target
;
881 struct symbol
*otype
= insn
->orig_type
;
882 char target_name
[64];
884 if (is_ptr_type(insn
->type
)) // cast to void* is OP_CAST ...
885 return output_op_ptrcast(fn
, insn
);
887 assert(is_int_type(insn
->type
));
889 src
= get_operand(fn
, otype
, insn
->src
);
890 pseudo_name(insn
->target
, target_name
);
892 dtype
= symbol_type(insn
->type
);
893 if (is_ptr_type(otype
)) {
895 } else if (is_float_type(otype
)) {
896 assert(op
== LLVMFPToUI
|| op
== LLVMFPToSI
);
897 } else if (is_int_type(otype
)) {
898 unsigned int width
= otype
->bit_size
;
899 if (insn
->size
< width
)
901 else if (insn
->size
== width
)
907 target
= LLVMBuildCast(fn
->builder
, op
, src
, dtype
, target_name
);
908 insn
->target
->priv
= target
;
911 static void output_op_fpcast(struct function
*fn
, struct instruction
*insn
)
913 LLVMTypeRef dtype
= symbol_type(insn
->type
);
914 LLVMValueRef src
, target
;
915 struct symbol
*otype
= insn
->orig_type
;
918 assert(is_float_type(insn
->type
));
920 pseudo_name(insn
->target
, name
);
921 src
= get_operand(fn
, otype
, insn
->src
);
922 switch (insn
->opcode
) {
924 target
= LLVMBuildFPCast(fn
->builder
, src
, dtype
, name
);
927 target
= LLVMBuildSIToFP(fn
->builder
, src
, dtype
, name
);
930 target
= LLVMBuildUIToFP(fn
->builder
, src
, dtype
, name
);
935 insn
->target
->priv
= target
;
938 static void output_op_label(struct function
*fn
, struct instruction
*insn
)
940 insn
->target
->priv
= LLVMBlockAddress(fn
->fn
, insn
->bb_true
->priv
);
943 static void output_op_setval(struct function
*fn
, struct instruction
*insn
)
945 struct expression
*val
= insn
->val
;
950 target
= LLVMBlockAddress(fn
->fn
, val
->symbol
->bb_target
->priv
);
956 insn
->target
->priv
= target
;
959 static void output_op_setfval(struct function
*fn
, struct instruction
*insn
)
961 LLVMTypeRef dtype
= symbol_type(insn
->type
);
964 target
= LLVMConstReal(dtype
, insn
->fvalue
);
965 insn
->target
->priv
= target
;
968 static void output_insn(struct function
*fn
, struct instruction
*insn
)
970 switch (insn
->opcode
) {
972 output_op_ret(fn
, insn
);
975 output_op_br(fn
, insn
);
978 output_op_cbr(fn
, insn
);
984 output_op_label(fn
, insn
);
987 output_op_setval(fn
, insn
);
990 output_op_setfval(fn
, insn
);
993 output_op_switch(fn
, insn
);
995 case OP_COMPUTEDGOTO
:
999 output_op_phisrc(fn
, insn
);
1002 output_op_phi(fn
, insn
);
1005 output_op_load(fn
, insn
);
1008 output_op_store(fn
, insn
);
1010 case OP_INLINED_CALL
:
1013 output_op_call(fn
, insn
);
1016 output_op_cast(fn
, insn
, LLVMZExt
);
1019 output_op_cast(fn
, insn
, LLVMSExt
);
1022 output_op_cast(fn
, insn
, LLVMTrunc
);
1025 output_op_cast(fn
, insn
, LLVMFPToUI
);
1028 output_op_cast(fn
, insn
, LLVMFPToSI
);
1030 case OP_UCVTF
: case OP_SCVTF
:
1032 output_op_fpcast(fn
, insn
);
1037 output_op_ptrcast(fn
, insn
);
1039 case OP_BINARY
... OP_BINARY_END
:
1040 output_op_binary(fn
, insn
);
1042 case OP_FPCMP
... OP_BINCMP_END
:
1043 output_op_compare(fn
, insn
);
1046 output_op_sel(fn
, insn
);
1052 LLVMValueRef src
, target
;
1053 char target_name
[64];
1055 src
= pseudo_to_value(fn
, insn
->type
, insn
->src
);
1057 pseudo_name(insn
->target
, target_name
);
1059 target
= LLVMBuildNot(fn
->builder
, src
, target_name
);
1061 insn
->target
->priv
= target
;
1066 LLVMValueRef src
, target
;
1067 char target_name
[64];
1069 src
= pseudo_to_value(fn
, insn
->type
, insn
->src
);
1071 pseudo_name(insn
->target
, target_name
);
1073 if (insn
->opcode
== OP_FNEG
)
1074 target
= LLVMBuildFNeg(fn
->builder
, src
, target_name
);
1076 target
= LLVMBuildNeg(fn
->builder
, src
, target_name
);
1078 insn
->target
->priv
= target
;
1103 static void output_bb(struct function
*fn
, struct basic_block
*bb
)
1105 struct instruction
*insn
;
1107 FOR_EACH_PTR(bb
->insns
, insn
) {
1111 output_insn(fn
, insn
);
1113 END_FOR_EACH_PTR(insn
);
1118 static void output_fn(LLVMModuleRef module
, struct entrypoint
*ep
)
1120 struct symbol
*sym
= ep
->name
;
1121 struct symbol
*base_type
= sym
->ctype
.base_type
;
1122 struct function function
= { .module
= module
};
1123 struct basic_block
*bb
;
1127 function
.fn
= get_sym_value(module
, sym
);
1128 LLVMSetFunctionCallConv(function
.fn
, LLVMCCallConv
);
1129 LLVMSetLinkage(function
.fn
, function_linkage(sym
));
1131 function
.builder
= LLVMCreateBuilder();
1133 /* give a name to each argument */
1134 nr_args
= symbol_list_size(base_type
->arguments
);
1135 for (i
= 0; i
< nr_args
; i
++) {
1136 char name
[MAX_PSEUDO_NAME
];
1139 arg
= LLVMGetParam(function
.fn
, i
);
1140 snprintf(name
, sizeof(name
), "ARG%d.", i
+1);
1141 LLVMSetValueName(arg
, name
);
1144 /* create the BBs */
1145 FOR_EACH_PTR(ep
->bbs
, bb
) {
1147 LLVMBasicBlockRef bbr
;
1150 sprintf(bbname
, "L%d", nr_bb
++);
1151 bbr
= LLVMAppendBasicBlock(function
.fn
, bbname
);
1155 END_FOR_EACH_PTR(bb
);
1157 FOR_EACH_PTR(ep
->bbs
, bb
) {
1158 LLVMPositionBuilderAtEnd(function
.builder
, bb
->priv
);
1160 output_bb(&function
, bb
);
1162 END_FOR_EACH_PTR(bb
);
1164 FOR_EACH_PTR(ep
->bbs
, bb
) { // complete the OP_PHIs
1165 struct instruction
*insn
;
1167 FOR_EACH_PTR(bb
->insns
, insn
) {
1170 if (!insn
->bb
|| insn
->opcode
!= OP_PHI
)
1173 FOR_EACH_PTR(insn
->phi_list
, phi
) {
1174 struct instruction
*phisrc
;
1175 LLVMBasicBlockRef bref
;
1182 bref
= phisrc
->bb
->priv
;
1183 vref
= phisrc
->src
->priv
;
1184 LLVMAddIncoming(insn
->target
->priv
, &vref
, &bref
, 1);
1185 } END_FOR_EACH_PTR(phi
);
1186 } END_FOR_EACH_PTR(insn
);
1187 } END_FOR_EACH_PTR(bb
);
1190 static LLVMValueRef
output_data(LLVMModuleRef module
, struct symbol
*sym
)
1192 struct expression
*initializer
= sym
->initializer
;
1193 LLVMValueRef initial_value
;
1198 switch (initializer
->type
) {
1200 initial_value
= LLVMConstInt(symbol_type(sym
), initializer
->value
, 1);
1203 initial_value
= LLVMConstReal(symbol_type(sym
), initializer
->fvalue
);
1206 struct symbol
*sym
= initializer
->symbol
;
1208 initial_value
= LLVMGetNamedGlobal(module
, show_ident(sym
->ident
));
1210 initial_value
= output_data(module
, sym
);
1214 const char *s
= initializer
->string
->data
;
1216 initial_value
= LLVMConstString(strdup(s
), strlen(s
) + 1, true);
1220 warning(initializer
->pos
, "can't initialize type: %s", show_typename(sym
));
1221 initial_value
= NULL
;
1225 LLVMTypeRef type
= symbol_type(sym
);
1227 initial_value
= LLVMConstNull(type
);
1233 name
= sym
->ident
? show_ident(sym
->ident
) : "" ;
1235 data
= LLVMAddGlobal(module
, LLVMTypeOf(initial_value
), name
);
1237 LLVMSetLinkage(data
, data_linkage(sym
));
1238 if (sym
->ctype
.modifiers
& MOD_CONST
)
1239 LLVMSetGlobalConstant(data
, 1);
1240 if (sym
->ctype
.modifiers
& MOD_TLS
)
1241 LLVMSetThreadLocal(data
, 1);
1242 if (sym
->ctype
.alignment
)
1243 LLVMSetAlignment(data
, sym
->ctype
.alignment
);
1245 if (!(sym
->ctype
.modifiers
& MOD_EXTERN
))
1246 LLVMSetInitializer(data
, initial_value
);
1251 static int is_prototype(struct symbol
*sym
)
1253 if (sym
->type
== SYM_NODE
)
1254 sym
= sym
->ctype
.base_type
;
1255 return sym
&& sym
->type
== SYM_FN
&& !sym
->stmt
;
1258 static int compile(LLVMModuleRef module
, struct symbol_list
*list
)
1262 FOR_EACH_PTR(list
, sym
) {
1263 struct entrypoint
*ep
;
1266 if (is_prototype(sym
)) {
1267 // this will do the LLVMAddFunction() we want
1268 get_sym_value(module
, sym
);
1272 ep
= linearize_symbol(sym
);
1274 output_fn(module
, ep
);
1276 output_data(module
, sym
);
1278 END_FOR_EACH_PTR(sym
);
1283 #ifndef LLVM_DEFAULT_TARGET_TRIPLE
1284 #define LLVM_DEFAULT_TARGET_TRIPLE LLVM_HOSTTRIPLE
1287 #define X86_LINUX_LAYOUT \
1288 "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1289 "i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-" \
1290 "a0:0:64-f80:32:32-n8:16:32-S128"
1292 #define X86_64_LINUX_LAYOUT \
1293 "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-" \
1294 "i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-" \
1295 "a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
1297 static void set_target(LLVMModuleRef module
)
1299 char target
[] = LLVM_DEFAULT_TARGET_TRIPLE
;
1300 const char *arch
, *vendor
, *os
, *env
, *layout
= NULL
;
1303 arch
= strtok(target
, "-");
1304 vendor
= strtok(NULL
, "-");
1305 os
= strtok(NULL
, "-");
1306 env
= strtok(NULL
, "-");
1313 if (!strcmp(arch
, "x86_64") && !strcmp(os
, "linux")) {
1315 layout
= X86_64_LINUX_LAYOUT
;
1318 layout
= X86_LINUX_LAYOUT
;
1322 /* unsupported target */
1326 snprintf(triple
, sizeof(triple
), "%s-%s-%s-%s", arch
, vendor
, os
, env
);
1327 LLVMSetTarget(module
, triple
);
1328 LLVMSetDataLayout(module
, layout
);
1331 int main(int argc
, char **argv
)
1333 struct string_list
*filelist
= NULL
;
1334 struct symbol_list
*symlist
;
1335 LLVMModuleRef module
;
1338 symlist
= sparse_initialize(argc
, argv
, &filelist
);
1340 module
= LLVMModuleCreateWithName("sparse");
1343 compile(module
, symlist
);
1345 /* need ->phi_users */
1347 FOR_EACH_PTR(filelist
, file
) {
1348 symlist
= sparse(file
);
1351 compile(module
, symlist
);
1352 } END_FOR_EACH_PTR(file
);
1354 LLVMVerifyModule(module
, LLVMPrintMessageAction
, NULL
);
1356 LLVMWriteBitcodeToFD(module
, STDOUT_FILENO
, 0, 0);
1358 LLVMDisposeModule(module
);