int llvm_test_callsite_attributes(void) { LLVMEnablePrettyStackTrace(); LLVMModuleRef M = llvm_load_module(false, true); LLVMValueRef F = LLVMGetFirstFunction(M); while (F) { LLVMBasicBlockRef BB; for (BB = LLVMGetFirstBasicBlock(F); BB; BB = LLVMGetNextBasicBlock(BB)) { LLVMValueRef I; for (I = LLVMGetFirstInstruction(BB); I; I = LLVMGetNextInstruction(I)) { if (LLVMIsACallInst(I)) { // Read attributes int Idx, ParamCount; for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F); Idx <= ParamCount; ++Idx) { int AttrCount = LLVMGetCallSiteAttributeCount(I, Idx); LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc( AttrCount * sizeof(LLVMAttributeRef)); assert(Attrs); LLVMGetCallSiteAttributes(I, Idx, Attrs); free(Attrs); } } } } F = LLVMGetNextFunction(F); } LLVMDisposeModule(M); return 0; }
int test_kal_codegen_function() { kal_named_value *val; unsigned int arg_count = 1; char **args = malloc(sizeof(char*) * arg_count); args[0] = "foo"; LLVMModuleRef module = LLVMModuleCreateWithName("kal"); LLVMBuilderRef builder = LLVMCreateBuilder(); kal_ast_node *prototype = kal_ast_prototype_create("my_func", args, arg_count); kal_ast_node *lhs = kal_ast_variable_create("foo"); kal_ast_node *rhs = kal_ast_number_create(20); kal_ast_node *body = kal_ast_binary_expr_create(KAL_BINOP_PLUS, lhs, rhs); kal_ast_node *node = kal_ast_function_create(prototype, body); kal_codegen_reset(); LLVMValueRef value = kal_codegen(node, module, builder); mu_assert(value != NULL, ""); mu_assert(LLVMGetNamedFunction(module, "my_func") == value, ""); mu_assert(LLVMCountParams(value) == 1, ""); val = kal_codegen_named_value("foo"); mu_assert(val->value == LLVMGetParam(value, 0), ""); mu_assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(value, 0))) == LLVMDoubleTypeKind, ""); LLVMDisposeBuilder(builder); LLVMDisposeModule(module); kal_ast_node_free(node); return 0; }
void vm_state_destroy(struct vm_state *vm) { LLVMBasicBlockRef current_block, return_block; char *error; current_block = LLVMGetInsertBlock(vm->builder); return_block = LLVMInsertBasicBlock(current_block, "ret"); LLVMPositionBuilderAtEnd(vm->builder, current_block); LLVMBuildBr(vm->builder, return_block); LLVMPositionBuilderAtEnd(vm->builder, return_block); LLVMBuildRetVoid(vm->builder); LLVMMoveBasicBlockAfter(return_block, current_block); LLVMDumpModule(vm->module); error = NULL; LLVMVerifyModule(vm->module, LLVMAbortProcessAction, &error); LLVMDisposeMessage(error); LLVMDisposeBuilder(vm->builder); LLVMDisposeModule(vm->module); symbol_table_destroy(vm->symtab); }
static void handle_line(char **tokens, int ntokens) { char *name = tokens[0]; LLVMValueRef param; LLVMValueRef res; LLVMModuleRef M = LLVMModuleCreateWithName(name); LLVMTypeRef I64ty = LLVMInt64Type(); LLVMTypeRef I64Ptrty = LLVMPointerType(I64ty, 0); LLVMTypeRef Fty = LLVMFunctionType(I64ty, &I64Ptrty, 1, 0); LLVMValueRef F = LLVMAddFunction(M, name, Fty); LLVMBuilderRef builder = LLVMCreateBuilder(); LLVMPositionBuilderAtEnd(builder, LLVMAppendBasicBlock(F, "entry")); LLVMGetParams(F, ¶m); LLVMSetValueName(param, "in"); res = build_from_tokens(tokens + 1, ntokens - 1, builder, param); if (res) { char *irstr = LLVMPrintModuleToString(M); puts(irstr); LLVMDisposeMessage(irstr); } LLVMDisposeBuilder(builder); LLVMDisposeModule(M); }
void evergreen_delete_compute_state(struct pipe_context *ctx_, void* state) { struct r600_context *ctx = (struct r600_context *)ctx_; COMPUTE_DBG(ctx->screen, "*** evergreen_delete_compute_state\n"); struct r600_pipe_compute *shader = state; if (!shader) return; #ifdef HAVE_OPENCL #if HAVE_LLVM < 0x0306 for (unsigned i = 0; i < shader->num_kernels; i++) { struct r600_kernel *kernel = &shader->kernels[i]; LLVMDisposeModule(module); } FREE(shader->kernels); LLVMContextDispose(shader->llvm_ctx); #else radeon_shader_binary_clean(&shader->binary); r600_destroy_shader(&shader->bc); /* TODO destroy shader->code_bo, shader->const_bo * we'll need something like r600_buffer_free */ #endif #endif FREE(shader); }
int llvm_test_function_attributes(void) { LLVMEnablePrettyStackTrace(); LLVMModuleRef M = llvm_load_module(false, true); LLVMValueRef F = LLVMGetFirstFunction(M); while (F) { // Read attributes int Idx, ParamCount; for (Idx = LLVMAttributeFunctionIndex, ParamCount = LLVMCountParams(F); Idx <= ParamCount; ++Idx) { int AttrCount = LLVMGetAttributeCountAtIndex(F, Idx); LLVMAttributeRef *Attrs = (LLVMAttributeRef *)malloc(AttrCount * sizeof(LLVMAttributeRef)); assert(Attrs); LLVMGetAttributesAtIndex(F, Idx, Attrs); free(Attrs); } F = LLVMGetNextFunction(F); } LLVMDisposeModule(M); return 0; }
/** * Free gallivm object's LLVM allocations, but not the gallivm object itself. */ static void free_gallivm_state(struct gallivm_state *gallivm) { #if HAVE_LLVM >= 0x207 /* XXX or 0x208? */ /* This leads to crashes w/ some versions of LLVM */ LLVMModuleRef mod; char *error; if (gallivm->engine && gallivm->provider) LLVMRemoveModuleProvider(gallivm->engine, gallivm->provider, &mod, &error); #endif if (gallivm->passmgr) { LLVMDisposePassManager(gallivm->passmgr); } #if 0 /* XXX this seems to crash with all versions of LLVM */ if (gallivm->provider) LLVMDisposeModuleProvider(gallivm->provider); #endif if (HAVE_LLVM >= 0x207 && gallivm->engine) { /* This will already destroy any associated module */ LLVMDisposeExecutionEngine(gallivm->engine); } else { LLVMDisposeModule(gallivm->module); } #if !USE_MCJIT /* Don't free the TargetData, it's owned by the exec engine */ #else if (gallivm->target) { LLVMDisposeTargetData(gallivm->target); } #endif /* Never free the LLVM context. */ #if 0 if (gallivm->context) LLVMContextDispose(gallivm->context); #endif if (gallivm->builder) LLVMDisposeBuilder(gallivm->builder); gallivm->engine = NULL; gallivm->target = NULL; gallivm->module = NULL; gallivm->provider = NULL; gallivm->passmgr = NULL; gallivm->context = NULL; gallivm->builder = NULL; }
int main (void) { LLVMModuleRef module = LLVMModuleCreateWithName("kal"); LLVMBuilderRef builder = LLVMCreateBuilder(); // LLVMInitializeNativeTarget(); LLVMTypeRef funcType = LLVMFunctionType(LLVMVoidType(), NULL, 0, 0); LLVMValueRef func = LLVMAddFunction(module, "main", funcType); LLVMSetLinkage(func, LLVMExternalLinkage); LLVMBasicBlockRef block = LLVMAppendBasicBlock(func, "entry"); LLVMPositionBuilderAtEnd(builder, block); LLVMValueRef cond = LLVMBuildICmp(builder, LLVMIntNE, LLVMConstInt(LLVMInt32Type(), 2, 0), LLVMConstInt(LLVMInt32Type(), 1, 0), "ifcond"); LLVMValueRef owning_block = LLVMGetBasicBlockParent(LLVMGetInsertBlock(builder)); //TODO: WRONG?? //LLVMValueRef owning_block = LLVMBasicBlockAsValue(LLVMGetPreviousBasicBlock(LLVMGetInsertBlock(builder))); // 2. Generate new blocks for cases. LLVMBasicBlockRef then_ref = LLVMAppendBasicBlock(owning_block, "then"); LLVMBasicBlockRef else_ref = LLVMAppendBasicBlock(owning_block, "else"); LLVMBasicBlockRef merge_ref = LLVMAppendBasicBlock(owning_block, "ifmerge"); // 3. Branch conditionally on then or else. LLVMBuildCondBr(builder, cond, then_ref, else_ref); // 4. Build then branch prologue. LLVMPositionBuilderAtEnd(builder, then_ref); LLVMValueRef hi1 = LLVMBuildXor(builder, LLVMGetUndef(LLVMInt32Type()), LLVMGetUndef(LLVMInt32Type()), "subtmp"); // 5. Connect then branch to merge block. LLVMBuildBr(builder, merge_ref); then_ref = LLVMGetInsertBlock(builder); // 6. Build else branch prologue. LLVMPositionBuilderAtEnd(builder, else_ref); LLVMValueRef hi2 = LLVMBuildXor(builder, LLVMGetUndef(LLVMInt32Type()), LLVMGetUndef(LLVMInt32Type()), "subtmp2"); // 7. Connect else branch to merge block. LLVMBuildBr(builder, merge_ref); else_ref = LLVMGetInsertBlock(builder); // 8. Position ourselves after the merge block. LLVMPositionBuilderAtEnd(builder, merge_ref); // 9. Build the phi node. // LLVMValueRef phi = LLVMBuildPhi(builder, LLVMDoubleType(), "phi"); // 10. Add incoming edges. // LLVMAddIncoming(phi, &hi1, &then_ref, 1); // LLVMAddIncoming(phi, &hi2, &else_ref, 1); LLVMDumpModule(module); LLVMDisposeBuilder(builder); LLVMDisposeModule(module); return 0; }
int module_dump(bool Lazy, bool New) { LLVMModuleRef M = load_module(Lazy, New); char *irstr = LLVMPrintModuleToString(M); puts(irstr); LLVMDisposeMessage(irstr); LLVMDisposeModule(M); return 0; }
static void codegen_cleanup(compile_t* c) { while(c->frame != NULL) pop_frame(c); LLVMDisposeBuilder(c->builder); LLVMDisposeModule(c->module); LLVMContextDispose(c->context); LLVMDisposeTargetMachine(c->machine); reach_free(c->reachable); }
int llvm_add_named_metadata_operand(void) { LLVMModuleRef m = LLVMModuleCreateWithName("Mod"); LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) }; // This used to trigger an assertion LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(values, 1)); LLVMDisposeModule(m); return 0; }
/** * Free gallivm object's LLVM allocations, but not the gallivm object itself. */ static void free_gallivm_state(struct gallivm_state *gallivm) { #if HAVE_LLVM >= 0x207 /* XXX or 0x208? */ /* This leads to crashes w/ some versions of LLVM */ LLVMModuleRef mod; char *error; if (gallivm->engine && gallivm->provider) LLVMRemoveModuleProvider(gallivm->engine, gallivm->provider, &mod, &error); #endif #if 0 /* XXX this seems to crash with all versions of LLVM */ if (gallivm->provider) LLVMDisposeModuleProvider(gallivm->provider); #endif if (gallivm->passmgr) LLVMDisposePassManager(gallivm->passmgr); #if HAVE_LLVM >= 0x207 if (gallivm->module) LLVMDisposeModule(gallivm->module); #endif #if 0 /* Don't free the exec engine, it's a global/singleton */ if (gallivm->engine) LLVMDisposeExecutionEngine(gallivm->engine); #endif #if 0 /* Don't free the TargetData, it's owned by the exec engine */ LLVMDisposeTargetData(gallivm->target); #endif if (gallivm->context) LLVMContextDispose(gallivm->context); if (gallivm->builder) LLVMDisposeBuilder(gallivm->builder); gallivm->engine = NULL; gallivm->target = NULL; gallivm->module = NULL; gallivm->provider = NULL; gallivm->passmgr = NULL; gallivm->context = NULL; gallivm->builder = NULL; }
static void codegen_cleanup(compile_t* c) { while(c->frame != NULL) pop_frame(c); LLVMDIBuilderDestroy(c->di); LLVMDisposeBuilder(c->builder); LLVMDisposeModule(c->module); LLVMContextDispose(c->context); LLVMDisposeTargetMachine(c->machine); tbaa_metadatas_free(c->tbaa_mds); reach_free(c->reach); }
int test_kal_codegen_binary_expr() { LLVMModuleRef module = LLVMModuleCreateWithName("kal"); LLVMBuilderRef builder = LLVMCreateBuilder(); kal_ast_node *lhs = kal_ast_number_create(20); kal_ast_node *rhs = kal_ast_number_create(30); kal_ast_node *node = kal_ast_binary_expr_create(KAL_BINOP_PLUS, lhs, rhs); LLVMValueRef value = kal_codegen(node, module, builder); mu_assert(LLVMGetTypeKind(LLVMTypeOf(value)) == LLVMDoubleTypeKind, ""); mu_assert(LLVMIsConstant(value), ""); LLVMDisposeBuilder(builder); LLVMDisposeModule(module); kal_ast_node_free(node); return 0; }
SCM llvm_module_destroy(SCM scm_self) { struct llvm_module_t *self = get_llvm_no_check(scm_self); if (self->engine) { if (self->module) { char *error = NULL; LLVMRemoveModule(self->engine, self->module, &self->module, &error); if (error) LLVMDisposeMessage(error); }; LLVMDisposeExecutionEngine(self->engine); self->engine = NULL; }; if (self->module) { LLVMDisposeModule(self->module); self->module = NULL; }; return SCM_UNSPECIFIED; }
bool codegen_merge_runtime_bitcode(compile_t* c) { strlist_t* search = package_paths(); char path[FILENAME_MAX]; LLVMModuleRef runtime = NULL; for(strlist_t* p = search; p != NULL && runtime == NULL; p = strlist_next(p)) { path_cat(strlist_data(p), "libponyrt.bc", path); runtime = LLVMParseIRFileInContext(c->context, path); } errors_t* errors = c->opt->check.errors; if(runtime == NULL) { errorf(errors, NULL, "couldn't find libponyrt.bc"); return false; } if(c->opt->verbosity >= VERBOSITY_MINIMAL) fprintf(stderr, "Merging runtime\n"); #if PONY_LLVM >= 308 // runtime is freed by the function. if(LLVMLinkModules2(c->module, runtime)) { errorf(errors, NULL, "libponyrt.bc contains errors"); return false; } #else if(LLVMLinkModules(c->module, runtime, LLVMLinkerDestroySource /* unused */, NULL)) { errorf(errors, NULL, "libponyrt.bc contains errors"); LLVMDisposeModule(runtime); return false; } #endif return true; }
int module_list_functions(void) { LLVMModuleRef M = load_module(false, false); LLVMValueRef f; f = LLVMGetFirstFunction(M); while (f) { if (LLVMIsDeclaration(f)) { printf("FunctionDeclaration: %s\n", LLVMGetValueName(f)); } else { LLVMBasicBlockRef bb; LLVMValueRef isn; unsigned nisn = 0; unsigned nbb = 0; printf("FunctionDefinition: %s [#bb=%u]\n", LLVMGetValueName(f), LLVMCountBasicBlocks(f)); for (bb = LLVMGetFirstBasicBlock(f); bb; bb = LLVMGetNextBasicBlock(bb)) { nbb++; for (isn = LLVMGetFirstInstruction(bb); isn; isn = LLVMGetNextInstruction(isn)) { nisn++; if (LLVMIsACallInst(isn)) { LLVMValueRef callee = LLVMGetOperand(isn, LLVMGetNumOperands(isn) - 1); printf(" calls: %s\n", LLVMGetValueName(callee)); } } } printf(" #isn: %u\n", nisn); printf(" #bb: %u\n\n", nbb); } f = LLVMGetNextFunction(f); } LLVMDisposeModule(M); return 0; }
int module_list_globals(void) { LLVMModuleRef M = load_module(false, false); LLVMValueRef g; g = LLVMGetFirstGlobal(M); while (g) { LLVMTypeRef T = LLVMTypeOf(g); char *s = LLVMPrintTypeToString(T); printf("Global%s: %s %s\n", LLVMIsDeclaration(g) ? "Declaration" : "Definition", LLVMGetValueName(g), s); LLVMDisposeMessage(s); g = LLVMGetNextGlobal(g); } LLVMDisposeModule(M); return 0; }
int test_kal_codegen_prototype() { kal_named_value *val; unsigned int arg_count = 3; char **args = malloc(sizeof(char*) * arg_count); args[0] = "foo"; args[1] = "bar"; args[2] = "baz"; LLVMModuleRef module = LLVMModuleCreateWithName("kal"); LLVMBuilderRef builder = LLVMCreateBuilder(); kal_ast_node *node = kal_ast_prototype_create("my_func", args, 3); kal_codegen_reset(); LLVMValueRef value = kal_codegen(node, module, builder); mu_assert(value != NULL, ""); mu_assert(LLVMGetNamedFunction(module, "my_func") == value, ""); mu_assert(LLVMCountParams(value) == 3, ""); val = kal_codegen_named_value("foo"); mu_assert(val->value == LLVMGetParam(value, 0), ""); mu_assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(value, 0))) == LLVMDoubleTypeKind, ""); val = kal_codegen_named_value("bar"); mu_assert(val->value == LLVMGetParam(value, 1), ""); mu_assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(value, 1))) == LLVMDoubleTypeKind, ""); val = kal_codegen_named_value("baz"); mu_assert(val->value == LLVMGetParam(value, 2), ""); mu_assert(LLVMGetTypeKind(LLVMTypeOf(LLVMGetParam(value, 2))) == LLVMDoubleTypeKind, ""); LLVMDisposeBuilder(builder); LLVMDisposeModule(module); kal_ast_node_free(node); return 0; }
int main(int argc, char** argv) { llvm::llvm_shutdown_obj Y; llvm::error_code ec; std::string se; std::string file; if (argc < 2) { llvm::errs() << "Usage llvm-disasm <file>\n"; return 1; } llvm::InitializeAllTargetInfos(); llvm::InitializeAllTargetMCs(); llvm::InitializeAllAsmParsers(); llvm::InitializeAllDisassemblers(); file = argv[1]; llvm::OwningPtr<llvm::object::Binary> bin; ec = llvm::object::createBinary(file, bin); if (ec) { llvm::errs() << file << ": " << ec.message() << "\n"; return 1; } if (!bin->isELF()) { llvm::errs() << file << " isn't an object file\n"; return 1; } llvm::object::ObjectFile* obj = llvm::dyn_cast<llvm::object::ObjectFile>(bin.get()); if (!obj) { llvm::errs() << file << ": failed to cast to llvm::ObjectFile\n"; return 1; } llvm::Triple tri; tri.setArch(llvm::Triple::ArchType(obj->getArch())); std::string targetName = tri.str(); const llvm::Target* target = llvm::TargetRegistry::lookupTarget(targetName, se); if (!target) { llvm::errs() << file << ": failed to get the target descriptor for " << targetName << "\n"; return 1; } STI = target->createMCSubtargetInfo(targetName, "", ""); if (!STI) { llvm::errs() << file << ": " << ": to get the subtarget info!\n"; return 1; } disasm = target->createMCDisassembler(*STI); if (!disasm) { llvm::errs() << file << ": " << ": to get the disassembler!\n"; return 1; } MII = target->createMCInstrInfo(); if (!MII) { llvm::errs() << file << ": no instruction info for target\n"; return 1; } MRI = target->createMCRegInfo(targetName); if (!MRI) { llvm::errs() << file << ": no register info for target\n"; return 1; } llvmBuilder = LLVMCreateBuilder(); LLVMModuleRef llvmModule = LLVMModuleCreateWithName("test"); LLVMTypeRef mainType = LLVMFunctionType(LLVMInt32Type(), NULL, 0, 0); LLVMValueRef mainFn = LLVMAddFunction(llvmModule, "main", mainType); LLVMBasicBlockRef blk = LLVMAppendBasicBlock(mainFn, ""); LLVMPositionBuilderAtEnd(llvmBuilder, blk); for (llvm::object::section_iterator i = obj->begin_sections(), e = obj->end_sections(); i != e; i.increment(ec)) { if (ec) { llvm::errs() << "Failed to increment the section iterator!\n"; return 1; } bool isText; llvm::StringRef secName; if (i->getName(secName)) { llvm::errs() << file << ": failed to get the section name\n"; break; } if (i->isText(isText)) { llvm::errs() << file << ": " << secName << ": failed to determine the section type\n"; break; } if (!isText) { continue; } std::set<llvm::object::SymbolRef> symbols; for (llvm::object::symbol_iterator isym = obj->begin_symbols(); isym != obj->end_symbols(); isym.increment(ec)) { bool res; llvm::StringRef symName; llvm::object::SymbolRef::Type symType; if (ec) { llvm::errs() << "Failed to increment the symbol iterator!\n"; return 1; } if (isym->getName(symName)) { llvm::errs() << file << ": " << secName << ": failed to get the symbol name!\n"; return 1; } /* uint64_t secSize, secBase, symAddr; i->getAddress(secBase); i->getSize(secSize); isym->getAddress(symAddr); if (i->containsSymbol(*isym, res)) { llvm::errs() << file << ": " << secName << ": " << symName << ": failed to check whether the symbol is in the section!\n"; return 1; } if (!res) { continue; } if (symAddr < secBase || symAddr >= secBase + secSize) { continue; } */ llvm::object::section_iterator i2 = llvm::object::section_iterator(llvm::object::SectionRef()); isym->getSection(i2); if (i2 != i) { continue; } if (isym->getType(symType)) { llvm::errs() << file << ": " << secName << ": " << symName << ": failed to get the symbol type!\n"; return 1; } if (symType != llvm::object::SymbolRef::ST_Function) { continue; } symbols.insert(*isym); } for (std::set<llvm::object::SymbolRef>::const_iterator isym = symbols.begin(); isym != symbols.end(); ++isym) { if (analyzeSymbol(*isym)) { return 1; } } } LLVMDumpModule(llvmModule); LLVMDisposeModule(llvmModule); LLVMDisposeBuilder(llvmBuilder); return 0; }