remove 'Escape' from token character class
[smatch.git] / sparse-llvm.c
blob3d2218ef61d21bcb456bb9b49ad0cd1752583c5a
1 /*
2 * Example usage:
3 * ./sparse-llvm hello.c | llc | as -o hello.o
4 */
6 #include <llvm-c/Core.h>
7 #include <llvm-c/BitWriter.h>
8 #include <llvm-c/Analysis.h>
9 #include <llvm-c/Target.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <assert.h>
17 #include "symbol.h"
18 #include "expression.h"
19 #include "linearize.h"
20 #include "flow.h"
22 struct function {
23 LLVMBuilderRef builder;
24 LLVMTypeRef type;
25 LLVMValueRef fn;
26 LLVMModuleRef module;
29 static inline bool symbol_is_fp_type(struct symbol *sym)
31 if (!sym)
32 return false;
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;
48 LLVMTypeRef ret_type;
49 struct symbol *arg;
50 int n_arg = 0;
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) {
62 n_arg++;
63 } END_FOR_EACH_PTR(arg);
65 arg_type = calloc(n_arg, sizeof(LLVMTypeRef));
67 int idx = 0;
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,
74 sym->variadic);
76 return func_type;
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);
89 if (!elem_type)
90 return NULL;
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;
101 char buffer[256];
102 LLVMTypeRef ret;
103 unsigned nr = 0;
105 snprintf(buffer, sizeof(buffer), "struct.%s", sym->ident ? sym->ident->name : "anno");
106 ret = LLVMStructCreateNamed(LLVMGetGlobalContext(), buffer);
107 /* set ->aux to avoid recursion */
108 sym->aux = ret;
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? */);
121 return ret;
124 static LLVMTypeRef sym_union_type(LLVMModuleRef module, struct symbol *sym)
126 LLVMTypeRef elements;
127 unsigned union_size;
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)
143 LLVMTypeRef type;
145 /* 'void *' is treated like 'char *' */
146 if (is_void_type(sym->ctype.base_type))
147 type = LLVMInt8Type();
148 else
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) {
160 case 32:
161 ret = LLVMFloatType();
162 break;
163 case 64:
164 ret = LLVMDoubleType();
165 break;
166 case 80:
167 ret = LLVMX86FP80Type();
168 break;
169 default:
170 die("invalid bit size %d for type %d", sym->bit_size, sym->type);
171 break;
173 } else {
174 switch (sym->bit_size) {
175 case -1:
176 ret = LLVMVoidType();
177 break;
178 case 1:
179 ret = LLVMInt1Type();
180 break;
181 case 8:
182 ret = LLVMInt8Type();
183 break;
184 case 16:
185 ret = LLVMInt16Type();
186 break;
187 case 32:
188 ret = LLVMInt32Type();
189 break;
190 case 64:
191 ret = LLVMInt64Type();
192 break;
193 default:
194 die("invalid bit size %d for type %d", sym->bit_size, sym->type);
195 break;
199 return ret;
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);
210 if (sym->aux)
211 return sym->aux;
213 switch (sym->type) {
214 case SYM_BITFIELD:
215 case SYM_ENUM:
216 ret = symbol_type(module, sym->ctype.base_type);
217 break;
218 case SYM_BASETYPE:
219 ret = sym_basetype_type(sym);
220 break;
221 case SYM_PTR:
222 ret = sym_ptr_type(module, sym);
223 break;
224 case SYM_UNION:
225 ret = sym_union_type(module, sym);
226 break;
227 case SYM_STRUCT:
228 ret = sym_struct_type(module, sym);
229 break;
230 case SYM_ARRAY:
231 ret = sym_array_type(module, sym);
232 break;
233 case SYM_FN:
234 ret = sym_func_type(module, sym);
235 break;
236 default:
237 assert(0);
240 /* cache the result */
241 sym->aux = ret;
242 return ret;
245 static LLVMTypeRef insn_symbol_type(LLVMModuleRef module, struct instruction *insn)
247 if (insn->type)
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();
256 default:
257 die("invalid bit size %d", insn->size);
258 break;
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) {
285 case PSEUDO_REG:
286 snprintf(buf, MAX_PSEUDO_NAME, "R%d", pseudo->nr);
287 break;
288 case PSEUDO_SYM:
289 assert(0);
290 break;
291 case PSEUDO_VAL:
292 assert(0);
293 break;
294 case PSEUDO_ARG: {
295 assert(0);
296 break;
298 case PSEUDO_PHI:
299 snprintf(buf, MAX_PSEUDO_NAME, "PHI%d", pseudo->nr);
300 break;
301 default:
302 assert(0);
306 static LLVMValueRef pseudo_to_value(struct function *fn, struct instruction *insn, pseudo_t pseudo)
308 LLVMValueRef result = NULL;
310 switch (pseudo->type) {
311 case PSEUDO_REG:
312 result = pseudo->priv;
313 break;
314 case PSEUDO_SYM: {
315 struct symbol *sym = pseudo->sym;
316 struct expression *expr;
318 assert(sym->bb_target == NULL);
320 expr = sym->initializer;
321 if (expr) {
322 switch (expr->type) {
323 case EXPR_STRING: {
324 const char *s = expr->string->data;
325 LLVMValueRef indices[] = { LLVMConstInt(LLVMInt64Type(), 0, 0), LLVMConstInt(LLVMInt64Type(), 0, 0) };
326 LLVMValueRef data;
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));
334 break;
336 case EXPR_SYMBOL: {
337 struct symbol *sym = expr->symbol;
339 result = LLVMGetNamedGlobal(fn->module, show_ident(sym->ident));
340 assert(result != NULL);
341 break;
343 default:
344 assert(0);
346 } else {
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);
352 if (!result)
353 result = LLVMAddFunction(fn->module, name, type);
354 } else {
355 result = LLVMGetNamedGlobal(fn->module, name);
356 if (!result)
357 result = LLVMAddGlobal(fn->module, type, name);
360 break;
362 case PSEUDO_VAL:
363 result = LLVMConstInt(insn_symbol_type(fn->module, insn), pseudo->value, 1);
364 break;
365 case PSEUDO_ARG: {
366 result = LLVMGetParam(fn->fn, pseudo->nr - 1);
367 break;
369 case PSEUDO_PHI:
370 result = pseudo->priv;
371 break;
372 case PSEUDO_VOID:
373 result = NULL;
374 break;
375 default:
376 assert(0);
379 return result;
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);
387 LLVMValueRef addr;
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, "");
395 return addr;
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) {
447 /* Binary */
448 case OP_ADD:
449 if (symbol_is_fp_type(insn->type))
450 target = LLVMBuildFAdd(fn->builder, lhs, rhs, target_name);
451 else
452 target = LLVMBuildAdd(fn->builder, lhs, rhs, target_name);
453 break;
454 case OP_SUB:
455 if (symbol_is_fp_type(insn->type))
456 target = LLVMBuildFSub(fn->builder, lhs, rhs, target_name);
457 else
458 target = LLVMBuildSub(fn->builder, lhs, rhs, target_name);
459 break;
460 case OP_MULU:
461 if (symbol_is_fp_type(insn->type))
462 target = LLVMBuildFMul(fn->builder, lhs, rhs, target_name);
463 else
464 target = LLVMBuildMul(fn->builder, lhs, rhs, target_name);
465 break;
466 case OP_MULS:
467 assert(!symbol_is_fp_type(insn->type));
468 target = LLVMBuildMul(fn->builder, lhs, rhs, target_name);
469 break;
470 case OP_DIVU:
471 if (symbol_is_fp_type(insn->type))
472 target = LLVMBuildFDiv(fn->builder, lhs, rhs, target_name);
473 else
474 target = LLVMBuildUDiv(fn->builder, lhs, rhs, target_name);
475 break;
476 case OP_DIVS:
477 assert(!symbol_is_fp_type(insn->type));
478 target = LLVMBuildSDiv(fn->builder, lhs, rhs, target_name);
479 break;
480 case OP_MODU:
481 assert(!symbol_is_fp_type(insn->type));
482 target = LLVMBuildURem(fn->builder, lhs, rhs, target_name);
483 break;
484 case OP_MODS:
485 assert(!symbol_is_fp_type(insn->type));
486 target = LLVMBuildSRem(fn->builder, lhs, rhs, target_name);
487 break;
488 case OP_SHL:
489 assert(!symbol_is_fp_type(insn->type));
490 target = LLVMBuildShl(fn->builder, lhs, rhs, target_name);
491 break;
492 case OP_LSR:
493 assert(!symbol_is_fp_type(insn->type));
494 target = LLVMBuildLShr(fn->builder, lhs, rhs, target_name);
495 break;
496 case OP_ASR:
497 assert(!symbol_is_fp_type(insn->type));
498 target = LLVMBuildAShr(fn->builder, lhs, rhs, target_name);
499 break;
501 /* Logical */
502 case OP_AND:
503 assert(!symbol_is_fp_type(insn->type));
504 target = LLVMBuildAnd(fn->builder, lhs, rhs, target_name);
505 break;
506 case OP_OR:
507 assert(!symbol_is_fp_type(insn->type));
508 target = LLVMBuildOr(fn->builder, lhs, rhs, target_name);
509 break;
510 case OP_XOR:
511 assert(!symbol_is_fp_type(insn->type));
512 target = LLVMBuildXor(fn->builder, lhs, rhs, target_name);
513 break;
514 case OP_AND_BOOL: {
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);
524 break;
526 case OP_OR_BOOL: {
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);
536 break;
538 default:
539 assert(0);
540 break;
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);
555 else
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);
566 } else {
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);
585 } else
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;
593 unsigned int as;
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);
607 return addr;
611 static void output_op_load(struct function *fn, struct instruction *insn)
613 LLVMValueRef addr, target;
615 addr = calc_memop_addr(fn, insn);
617 /* perform load */
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);
631 /* perform store */
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");
642 return value;
645 static void output_op_br(struct function *fn, struct instruction *br)
647 if (br->cond) {
648 LLVMValueRef cond = bool_value(fn,
649 pseudo_to_value(fn, br, br->cond));
651 LLVMBuildCondBr(fn->builder, cond,
652 br->bb_true->priv,
653 br->bb_false->priv);
654 } else
655 LLVMBuildBr(fn->builder,
656 br->bb_true ? br->bb_true->priv :
657 br->bb_false->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;
678 int n_jmp = 0;
680 FOR_EACH_PTR(insn->multijmp_list, jmp) {
681 if (jmp->begin == jmp->end) { /* case N */
682 n_jmp++;
683 } else if (jmp->begin < jmp->end) { /* case M..N */
684 assert(0);
685 } else /* default case */
686 def = jmp->target;
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 */
695 LLVMAddCase(target,
696 LLVMConstInt(LLVMInt32Type(), jmp->begin, 0),
697 jmp->target->priv);
698 } else if (jmp->begin < jmp->end) { /* case M..N */
699 assert(0);
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;
709 int n_arg = 0, i;
710 struct pseudo *arg;
711 LLVMValueRef *args;
713 FOR_EACH_PTR(insn->arguments, arg) {
714 n_arg++;
715 } END_FOR_EACH_PTR(arg);
717 args = calloc(n_arg, sizeof(LLVMValueRef));
719 i = 0;
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)
732 LLVMValueRef v;
733 struct instruction *phi;
735 assert(insn->target->priv == NULL);
737 /* target = src */
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;
757 /* forward load */
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;
771 if (!src)
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;
789 if (!src)
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);
798 else
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) {
807 case OP_RET:
808 output_op_ret(fn, insn);
809 break;
810 case OP_BR:
811 output_op_br(fn, insn);
812 break;
813 case OP_SYMADDR:
814 assert(0);
815 break;
816 case OP_SETVAL:
817 assert(0);
818 break;
819 case OP_SWITCH:
820 output_op_switch(fn, insn);
821 break;
822 case OP_COMPUTEDGOTO:
823 assert(0);
824 break;
825 case OP_PHISOURCE:
826 output_op_phisrc(fn, insn);
827 break;
828 case OP_PHI:
829 output_op_phi(fn, insn);
830 break;
831 case OP_LOAD:
832 output_op_load(fn, insn);
833 break;
834 case OP_LNOP:
835 assert(0);
836 break;
837 case OP_STORE:
838 output_op_store(fn, insn);
839 break;
840 case OP_SNOP:
841 assert(0);
842 break;
843 case OP_INLINED_CALL:
844 assert(0);
845 break;
846 case OP_CALL:
847 output_op_call(fn, insn);
848 break;
849 case OP_CAST:
850 output_op_cast(fn, insn, LLVMZExt);
851 break;
852 case OP_SCAST:
853 output_op_cast(fn, insn, LLVMSExt);
854 break;
855 case OP_FPCAST:
856 assert(0);
857 break;
858 case OP_PTRCAST:
859 output_op_ptrcast(fn, insn);
860 break;
861 case OP_BINARY ... OP_BINARY_END:
862 output_op_binary(fn, insn);
863 break;
864 case OP_BINCMP ... OP_BINCMP_END:
865 output_op_compare(fn, insn);
866 break;
867 case OP_SEL:
868 output_op_sel(fn, insn);
869 break;
870 case OP_SLICE:
871 assert(0);
872 break;
873 case OP_NOT: {
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;
884 break;
886 case OP_NEG:
887 assert(0);
888 break;
889 case OP_CONTEXT:
890 assert(0);
891 break;
892 case OP_RANGE:
893 assert(0);
894 break;
895 case OP_NOP:
896 assert(0);
897 break;
898 case OP_DEATHNOTE:
899 break;
900 case OP_ASM:
901 assert(0);
902 break;
903 case OP_COPY:
904 assert(0);
905 break;
906 default:
907 break;
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) {
918 if (!insn->bb)
919 continue;
921 output_insn(fn, insn);
923 END_FOR_EACH_PTR(insn);
926 #define MAX_ARGS 64
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;
938 struct symbol *arg;
939 const char *name;
940 int nr_args = 0;
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();
961 static int nr_bb;
963 FOR_EACH_PTR(ep->bbs, bb) {
964 if (bb->generation == generation)
965 continue;
967 LLVMBasicBlockRef bbr;
968 char bbname[32];
969 struct instruction *insn;
971 sprintf(bbname, "L%d", nr_bb++);
972 bbr = LLVMAppendBasicBlock(function.fn, bbname);
974 bb->priv = bbr;
976 /* allocate alloca for each phi */
977 FOR_EACH_PTR(bb->insns, insn) {
978 LLVMBasicBlockRef entrybbr;
979 LLVMTypeRef phi_type;
980 LLVMValueRef ptr;
982 if (!insn->bb || insn->opcode != OP_PHI)
983 continue;
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)
998 continue;
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;
1011 LLVMValueRef data;
1012 const char *name;
1014 if (initializer) {
1015 switch (initializer->type) {
1016 case EXPR_VALUE:
1017 initial_value = LLVMConstInt(symbol_type(module, sym), initializer->value, 1);
1018 break;
1019 case EXPR_SYMBOL: {
1020 struct symbol *sym = initializer->symbol;
1022 initial_value = LLVMGetNamedGlobal(module, show_ident(sym->ident));
1023 if (!initial_value)
1024 initial_value = output_data(module, sym);
1025 break;
1027 case EXPR_STRING: {
1028 const char *s = initializer->string->data;
1030 initial_value = LLVMConstString(strdup(s), strlen(s) + 1, true);
1031 break;
1033 default:
1034 assert(0);
1036 } else {
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);
1057 return data;
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)
1069 struct symbol *sym;
1071 FOR_EACH_PTR(list, sym) {
1072 struct entrypoint *ep;
1073 expand_symbol(sym);
1075 if (is_prototype(sym))
1076 continue;
1078 ep = linearize_symbol(sym);
1079 if (ep)
1080 output_fn(module, ep);
1081 else
1082 output_data(module, sym);
1084 END_FOR_EACH_PTR(sym);
1086 return 0;
1089 #ifndef LLVM_DEFAULT_TARGET_TRIPLE
1090 #define LLVM_DEFAULT_TARGET_TRIPLE LLVM_HOSTTRIPLE
1091 #endif
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;
1107 char triple[256];
1109 arch = strtok(target, "-");
1110 vendor = strtok(NULL, "-");
1111 os = strtok(NULL, "-");
1112 env = strtok(NULL, "-");
1114 if (!os)
1115 return;
1116 if (!env)
1117 env = "unknown";
1119 if (!strcmp(arch, "x86_64") && !strcmp(os, "linux")) {
1120 if (arch_m64) {
1121 layout = X86_64_LINUX_LAYOUT;
1122 } else {
1123 arch = "i386";
1124 layout = X86_LINUX_LAYOUT;
1128 /* unsupported target */
1129 if (!layout)
1130 return;
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;
1142 char *file;
1144 symlist = sparse_initialize(argc, argv, &filelist);
1146 module = LLVMModuleCreateWithName("sparse");
1147 set_target(module);
1149 compile(module, symlist);
1151 /* need ->phi_users */
1152 dbg_dead = 1;
1153 FOR_EACH_PTR_NOTAG(filelist, file) {
1154 symlist = sparse(file);
1155 if (die_if_error)
1156 return 1;
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);
1166 return 0;