Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // clang++ main.cpp -g -fvisibility-inlines-hidden -I/usr/local/Cellar/llvm/3.1//include -gdwarf-2 -fPIC -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.6 -Wall -Wno-unused-parameter -DDEBUG -DMAC -DLLVM -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/local/Cellar/llvm/3.1/lib/ -lLLVMAnalysis -lLLVMArchive -lLLVMAsmParser -lLLVMAsmPrinter -lLLVMBitReader -lLLVMBitWriter -lLLVMCodeGen -lLLVMCore -lLLVMExecutionEngine -lLLVMInstCombine -lLLVMInstrumentation -lLLVMInterpreter -lLLVMJIT -lLLVMLinker -lLLVMMC -lLLVMMCParser -lLLVMScalarOpts -lLLVMSelectionDAG -lLLVMSupport -lLLVMTarget -lLLVMTransformUtils -lLLVMVectorize -lLLVMX86AsmParser -lLLVMX86AsmPrinter -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info -lLLVMX86Utils -lLLVMipa -lLLVMipo -lclangAnalysis -lclangAST -lclangBasic -lclangCodeGen -lclangDriver -lclangEdit -lclangFrontend -lclangLex -lclangParse -lclangRewrite -lclangSema -lclangSerialization -lclangTooling
- #include <llvm/Support/DynamicLibrary.h>
- #include <llvm/Support/Host.h>
- #include <llvm/Support/Program.h>
- #include <llvm/Bitcode/Archive.h>
- #include <llvm/LLVMContext.h>
- #include <llvm/Module.h>
- #include <llvm/DerivedTypes.h>
- #include <llvm/Constants.h>
- #include <llvm/Instructions.h>
- #include <llvm/LinkAllPasses.h>
- #include <llvm/Linker.h>
- #include <llvm/PassManager.h>
- #include <llvm/Analysis/Verifier.h>
- #include <llvm/Analysis/ValueTracking.h>
- #include <llvm/Bitcode/ReaderWriter.h>
- #include <llvm/ExecutionEngine/JIT.h>
- #include <llvm/ExecutionEngine/Interpreter.h>
- #include <llvm/ExecutionEngine/GenericValue.h>
- #include <llvm/Support/raw_ostream.h>
- #include <llvm/Support/MemoryBuffer.h>
- #include <llvm/Support/system_error.h>
- #include <llvm/Support/IRBuilder.h>
- #include <llvm/Support/IRReader.h>
- #include <llvm/Support/PathV1.h>
- #include <llvm/Support/TargetSelect.h>
- #include <llvm/Target/TargetData.h>
- #include <llvm/Transforms/Utils/Cloning.h>
- #include <llvm/ValueSymbolTable.h>
- #include <algorithm>
- #include <string>
- #include <fcntl.h>
- using namespace llvm;
- Module * generateCode()
- {
- // Module Construction
- Module* mod = new Module("", getGlobalContext());
- mod->setDataLayout("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128");
- mod->setTargetTriple("x86_64-apple-macosx10.6.0");
- // Type Definitions
- std::vector<Type*>FuncTy_0_args;
- FunctionType* FuncTy_0 = FunctionType::get(
- /*Result=*/Type::getVoidTy(mod->getContext()),
- /*Params=*/FuncTy_0_args,
- /*isVarArg=*/false);
- StructType *StructTy_struct_dispatch_group_s = mod->getTypeByName("struct.dispatch_group_s");
- if (!StructTy_struct_dispatch_group_s) {
- StructTy_struct_dispatch_group_s = StructType::create(mod->getContext(), "struct.dispatch_group_s");
- }
- std::vector<Type*>StructTy_struct_dispatch_group_s_fields;
- if (StructTy_struct_dispatch_group_s->isOpaque()) {
- StructTy_struct_dispatch_group_s->setBody(StructTy_struct_dispatch_group_s_fields, /*isPacked=*/false);
- }
- PointerType* PointerTy_2 = PointerType::get(StructTy_struct_dispatch_group_s, 0);
- PointerType* PointerTy_1 = PointerType::get(PointerTy_2, 0);
- StructType *StructTy_union_dispatch_object_t = mod->getTypeByName("union.dispatch_object_t");
- if (!StructTy_union_dispatch_object_t) {
- StructTy_union_dispatch_object_t = StructType::create(mod->getContext(), "union.dispatch_object_t");
- }
- std::vector<Type*>StructTy_union_dispatch_object_t_fields;
- StructType *StructTy_struct_dispatch_object_s = mod->getTypeByName("struct.dispatch_object_s");
- if (!StructTy_struct_dispatch_object_s) {
- StructTy_struct_dispatch_object_s = StructType::create(mod->getContext(), "struct.dispatch_object_s");
- }
- std::vector<Type*>StructTy_struct_dispatch_object_s_fields;
- if (StructTy_struct_dispatch_object_s->isOpaque()) {
- StructTy_struct_dispatch_object_s->setBody(StructTy_struct_dispatch_object_s_fields, false);
- }
- PointerType* PointerTy_4 = PointerType::get(StructTy_struct_dispatch_object_s, 0);
- StructTy_union_dispatch_object_t_fields.push_back(PointerTy_4);
- if (StructTy_union_dispatch_object_t->isOpaque()) {
- StructTy_union_dispatch_object_t->setBody(StructTy_union_dispatch_object_t_fields, false);
- }
- PointerType* PointerTy_3 = PointerType::get(StructTy_union_dispatch_object_t, 0);
- std::vector<Type*>FuncTy_6_args;
- FunctionType* FuncTy_6 = FunctionType::get(
- /*Result=*/PointerTy_2,
- /*Params=*/FuncTy_6_args,
- /*isVarArg=*/false);
- PointerType* PointerTy_5 = PointerType::get(FuncTy_6, 0);
- PointerType* PointerTy_7 = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
- std::vector<Type*>FuncTy_9_args;
- FuncTy_9_args.push_back(PointerTy_7);
- FuncTy_9_args.push_back(PointerTy_7);
- FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 64));
- FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 32));
- FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 1));
- FunctionType* FuncTy_9 = FunctionType::get(
- /*Result=*/Type::getVoidTy(mod->getContext()),
- /*Params=*/FuncTy_9_args,
- /*isVarArg=*/false);
- PointerType* PointerTy_8 = PointerType::get(FuncTy_9, 0);
- PointerType* PointerTy_10 = PointerType::get(PointerTy_4, 0);
- std::vector<Type*>FuncTy_12_args;
- FuncTy_12_args.push_back(PointerTy_4);
- FunctionType* FuncTy_12 = FunctionType::get(
- /*Result=*/Type::getVoidTy(mod->getContext()),
- /*Params=*/FuncTy_12_args,
- /*isVarArg=*/false);
- PointerType* PointerTy_11 = PointerType::get(FuncTy_12, 0);
- // Function Declarations
- Function* func_foo = mod->getFunction("foo");
- if (!func_foo) {
- func_foo = Function::Create(
- /*Type=*/FuncTy_0,
- /*Linkage=*/GlobalValue::ExternalLinkage,
- /*Name=*/"foo", mod);
- func_foo->setCallingConv(CallingConv::C);
- }
- AttrListPtr func_foo_PAL;
- {
- SmallVector<AttributeWithIndex, 4> Attrs;
- AttributeWithIndex PAWI;
- PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None | Attribute::NoUnwind | Attribute::StackProtect | Attribute::UWTable;
- Attrs.push_back(PAWI);
- func_foo_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
- }
- func_foo->setAttributes(func_foo_PAL);
- Function* func_dispatch_group_create = mod->getFunction("dispatch_group_create");
- if (!func_dispatch_group_create) {
- func_dispatch_group_create = Function::Create(
- /*Type=*/FuncTy_6,
- /*Linkage=*/GlobalValue::ExternalLinkage,
- /*Name=*/"dispatch_group_create", mod); // (external, no body)
- func_dispatch_group_create->setCallingConv(CallingConv::C);
- }
- AttrListPtr func_dispatch_group_create_PAL;
- func_dispatch_group_create->setAttributes(func_dispatch_group_create_PAL);
- Function* func_dispatch_release = mod->getFunction("dispatch_release");
- if (!func_dispatch_release) {
- func_dispatch_release = Function::Create(
- /*Type=*/FuncTy_12,
- /*Linkage=*/GlobalValue::ExternalLinkage,
- /*Name=*/"dispatch_release", mod); // (external, no body)
- func_dispatch_release->setCallingConv(CallingConv::C);
- }
- AttrListPtr func_dispatch_release_PAL;
- {
- SmallVector<AttributeWithIndex, 4> Attrs;
- AttributeWithIndex PAWI;
- PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None | Attribute::NoUnwind;
- Attrs.push_back(PAWI);
- func_dispatch_release_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
- }
- func_dispatch_release->setAttributes(func_dispatch_release_PAL);
- Function* func_llvm_memcpy_p0i8_p0i8_i64 = mod->getFunction("llvm.memcpy.p0i8.p0i8.i64");
- if (!func_llvm_memcpy_p0i8_p0i8_i64) {
- func_llvm_memcpy_p0i8_p0i8_i64 = Function::Create(
- /*Type=*/FuncTy_9,
- /*Linkage=*/GlobalValue::ExternalLinkage,
- /*Name=*/"llvm.memcpy.p0i8.p0i8.i64", mod); // (external, no body)
- func_llvm_memcpy_p0i8_p0i8_i64->setCallingConv(CallingConv::C);
- }
- AttrListPtr func_llvm_memcpy_p0i8_p0i8_i64_PAL;
- {
- SmallVector<AttributeWithIndex, 4> Attrs;
- AttributeWithIndex PAWI;
- PAWI.Index = 1U; PAWI.Attrs = Attribute::None | Attribute::NoCapture;
- Attrs.push_back(PAWI);
- PAWI.Index = 2U; PAWI.Attrs = Attribute::None | Attribute::NoCapture;
- Attrs.push_back(PAWI);
- PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None | Attribute::NoUnwind;
- Attrs.push_back(PAWI);
- func_llvm_memcpy_p0i8_p0i8_i64_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
- }
- func_llvm_memcpy_p0i8_p0i8_i64->setAttributes(func_llvm_memcpy_p0i8_p0i8_i64_PAL);
- // Global Variable Declarations
- // Constant Definitions
- ConstantInt* const_int32_13 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
- ConstantInt* const_int64_14 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("8"), 10));
- ConstantInt* const_int32_15 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("8"), 10));
- ConstantInt* const_int1_16 = ConstantInt::get(mod->getContext(), APInt(1, StringRef("0"), 10));
- ConstantInt* const_int32_17 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
- // Global Variable Definitions
- // Function Definitions
- // Function: foo (func_foo)
- {
- BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func_foo,0);
- // Block entry (label_entry)
- AllocaInst* ptr_g = new AllocaInst(PointerTy_2, "g", label_entry);
- ptr_g->setAlignment(8);
- AllocaInst* ptr_agg_tmp = new AllocaInst(StructTy_union_dispatch_object_t, "agg.tmp", label_entry);
- ptr_agg_tmp->setAlignment(8);
- AllocaInst* ptr__compoundliteral = new AllocaInst(StructTy_union_dispatch_object_t, ".compoundliteral", label_entry);
- ptr__compoundliteral->setAlignment(8);
- CallInst* ptr_call = CallInst::Create(func_dispatch_group_create, "call", label_entry);
- ptr_call->setCallingConv(CallingConv::C);
- ptr_call->setTailCall(false);
- AttrListPtr ptr_call_PAL;
- ptr_call->setAttributes(ptr_call_PAL);
- StoreInst* void_18 = new StoreInst(ptr_call, ptr_g, false, label_entry);
- void_18->setAlignment(8);
- CastInst* ptr__dg = new BitCastInst(ptr__compoundliteral, PointerTy_1, "_dg", label_entry);
- LoadInst* ptr_19 = new LoadInst(ptr_g, "", false, label_entry);
- ptr_19->setAlignment(8);
- StoreInst* void_20 = new StoreInst(ptr_19, ptr__dg, false, label_entry);
- void_20->setAlignment(8);
- CastInst* ptr_21 = new BitCastInst(ptr_agg_tmp, PointerTy_7, "", label_entry);
- CastInst* ptr_22 = new BitCastInst(ptr__compoundliteral, PointerTy_7, "", label_entry);
- std::vector<Value*> void_23_params;
- void_23_params.push_back(ptr_21);
- void_23_params.push_back(ptr_22);
- void_23_params.push_back(const_int64_14);
- void_23_params.push_back(const_int32_15);
- void_23_params.push_back(const_int1_16);
- CallInst* void_23 = CallInst::Create(func_llvm_memcpy_p0i8_p0i8_i64, void_23_params, "", label_entry);
- void_23->setCallingConv(CallingConv::C);
- void_23->setTailCall(false);
- AttrListPtr void_23_PAL;
- void_23->setAttributes(void_23_PAL);
- std::vector<Value*> ptr_coerce_dive_indices;
- ptr_coerce_dive_indices.push_back(const_int32_17);
- ptr_coerce_dive_indices.push_back(const_int32_17);
- Instruction* ptr_coerce_dive = GetElementPtrInst::Create(ptr_agg_tmp, ptr_coerce_dive_indices, "coerce.dive", label_entry);
- LoadInst* ptr_24 = new LoadInst(ptr_coerce_dive, "", false, label_entry);
- CallInst* void_25 = CallInst::Create(func_dispatch_release, ptr_24, "", label_entry);
- void_25->setCallingConv(CallingConv::C);
- void_25->setTailCall(false);
- AttrListPtr void_25_PAL;
- {
- SmallVector<AttributeWithIndex, 4> Attrs;
- AttributeWithIndex PAWI;
- PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None | Attribute::NoUnwind;
- Attrs.push_back(PAWI);
- void_25_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
- }
- void_25->setAttributes(void_25_PAL);
- ReturnInst::Create(mod->getContext(), label_entry);
- }
- return mod;
- }
- Module * readModuleFromBitcode(std::string inputPath)
- {
- OwningPtr<MemoryBuffer> mb;
- error_code err = MemoryBuffer::getFile(inputPath.c_str(), mb);
- if (err) {
- printf("Can't open '%s'.\n", inputPath.c_str());
- return NULL;
- }
- std::string error;
- Module *module = ParseBitcodeFile(&(*mb), getGlobalContext(), &error);
- if (! module) {
- fprintf(stderr, "Can't parse '%s': %s.\n", inputPath.c_str(), error.c_str());
- return NULL;
- }
- return module;
- }
- bool writeModuleToBitcode(Module *module, std::string outputPath)
- {
- if (verifyModule(*module, PrintMessageAction))
- {
- fprintf(stderr, "Module verification failed.\n");
- return true;
- }
- int fd = open(outputPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
- raw_fd_ostream out(fd, true);
- WriteBitcodeToFile(module, out);
- return false;
- }
- int main(int argc, char **argv)
- {
- // Read other.bc
- Module *otherModule = readModuleFromBitcode("other.bc");
- // Generate and write main.bc
- Module *mainModuleWritten = generateCode();
- writeModuleToBitcode(mainModuleWritten, "main.bc");
- // Read main.bc
- Module *mainModule = readModuleFromBitcode("main.bc");
- // Link other.bc into main.bc
- Linker linker("", mainModule);
- std::string errMsg;
- bool isErr = linker.LinkInModule(otherModule, &errMsg);
- if (isErr)
- {
- fprintf(stderr, "Couldn't link in module: %s\n", errMsg.c_str());
- return 1;
- }
- generateCode();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement