Advertisement
Guest User

dispatch_object_t issue - main.cpp

a guest
Dec 13th, 2012
56
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 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
  2.  
  3. #include <llvm/Support/DynamicLibrary.h>
  4. #include <llvm/Support/Host.h>
  5. #include <llvm/Support/Program.h>
  6. #include <llvm/Bitcode/Archive.h>
  7. #include <llvm/LLVMContext.h>
  8. #include <llvm/Module.h>
  9. #include <llvm/DerivedTypes.h>
  10. #include <llvm/Constants.h>
  11. #include <llvm/Instructions.h>
  12. #include <llvm/LinkAllPasses.h>
  13. #include <llvm/Linker.h>
  14. #include <llvm/PassManager.h>
  15. #include <llvm/Analysis/Verifier.h>
  16. #include <llvm/Analysis/ValueTracking.h>
  17. #include <llvm/Bitcode/ReaderWriter.h>
  18. #include <llvm/ExecutionEngine/JIT.h>
  19. #include <llvm/ExecutionEngine/Interpreter.h>
  20. #include <llvm/ExecutionEngine/GenericValue.h>
  21. #include <llvm/Support/raw_ostream.h>
  22. #include <llvm/Support/MemoryBuffer.h>
  23. #include <llvm/Support/system_error.h>
  24. #include <llvm/Support/IRBuilder.h>
  25. #include <llvm/Support/IRReader.h>
  26. #include <llvm/Support/PathV1.h>
  27. #include <llvm/Support/TargetSelect.h>
  28. #include <llvm/Target/TargetData.h>
  29. #include <llvm/Transforms/Utils/Cloning.h>
  30. #include <llvm/ValueSymbolTable.h>
  31. #include <algorithm>
  32. #include <string>
  33. #include <fcntl.h>
  34. using namespace llvm;
  35.  
  36.  
  37. Module * generateCode()
  38. {
  39.     // Module Construction
  40.     Module* mod = new Module("", getGlobalContext());
  41.     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");
  42.     mod->setTargetTriple("x86_64-apple-macosx10.6.0");
  43.    
  44.     // Type Definitions
  45.     std::vector<Type*>FuncTy_0_args;
  46.     FunctionType* FuncTy_0 = FunctionType::get(
  47.                                                /*Result=*/Type::getVoidTy(mod->getContext()),
  48.                                                /*Params=*/FuncTy_0_args,
  49.                                                /*isVarArg=*/false);
  50.    
  51.     StructType *StructTy_struct_dispatch_group_s = mod->getTypeByName("struct.dispatch_group_s");
  52.     if (!StructTy_struct_dispatch_group_s) {
  53.         StructTy_struct_dispatch_group_s = StructType::create(mod->getContext(), "struct.dispatch_group_s");
  54.     }
  55.     std::vector<Type*>StructTy_struct_dispatch_group_s_fields;
  56.     if (StructTy_struct_dispatch_group_s->isOpaque()) {
  57.         StructTy_struct_dispatch_group_s->setBody(StructTy_struct_dispatch_group_s_fields, /*isPacked=*/false);
  58.     }
  59.    
  60.     PointerType* PointerTy_2 = PointerType::get(StructTy_struct_dispatch_group_s, 0);
  61.    
  62.     PointerType* PointerTy_1 = PointerType::get(PointerTy_2, 0);
  63.    
  64.     StructType *StructTy_union_dispatch_object_t = mod->getTypeByName("union.dispatch_object_t");
  65.     if (!StructTy_union_dispatch_object_t) {
  66.         StructTy_union_dispatch_object_t = StructType::create(mod->getContext(), "union.dispatch_object_t");
  67.     }
  68.     std::vector<Type*>StructTy_union_dispatch_object_t_fields;
  69.     StructType *StructTy_struct_dispatch_object_s = mod->getTypeByName("struct.dispatch_object_s");
  70.     if (!StructTy_struct_dispatch_object_s) {
  71.         StructTy_struct_dispatch_object_s = StructType::create(mod->getContext(), "struct.dispatch_object_s");
  72.     }
  73.     std::vector<Type*>StructTy_struct_dispatch_object_s_fields;
  74.     if (StructTy_struct_dispatch_object_s->isOpaque()) {
  75.         StructTy_struct_dispatch_object_s->setBody(StructTy_struct_dispatch_object_s_fields, false);
  76.     }
  77.    
  78.     PointerType* PointerTy_4 = PointerType::get(StructTy_struct_dispatch_object_s, 0);
  79.    
  80.     StructTy_union_dispatch_object_t_fields.push_back(PointerTy_4);
  81.     if (StructTy_union_dispatch_object_t->isOpaque()) {
  82.         StructTy_union_dispatch_object_t->setBody(StructTy_union_dispatch_object_t_fields, false);
  83.     }
  84.    
  85.     PointerType* PointerTy_3 = PointerType::get(StructTy_union_dispatch_object_t, 0);
  86.    
  87.     std::vector<Type*>FuncTy_6_args;
  88.     FunctionType* FuncTy_6 = FunctionType::get(
  89.                                                /*Result=*/PointerTy_2,
  90.                                                /*Params=*/FuncTy_6_args,
  91.                                                /*isVarArg=*/false);
  92.    
  93.     PointerType* PointerTy_5 = PointerType::get(FuncTy_6, 0);
  94.    
  95.     PointerType* PointerTy_7 = PointerType::get(IntegerType::get(mod->getContext(), 8), 0);
  96.    
  97.     std::vector<Type*>FuncTy_9_args;
  98.     FuncTy_9_args.push_back(PointerTy_7);
  99.     FuncTy_9_args.push_back(PointerTy_7);
  100.     FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 64));
  101.     FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 32));
  102.     FuncTy_9_args.push_back(IntegerType::get(mod->getContext(), 1));
  103.     FunctionType* FuncTy_9 = FunctionType::get(
  104.                                                /*Result=*/Type::getVoidTy(mod->getContext()),
  105.                                                /*Params=*/FuncTy_9_args,
  106.                                                /*isVarArg=*/false);
  107.    
  108.     PointerType* PointerTy_8 = PointerType::get(FuncTy_9, 0);
  109.    
  110.     PointerType* PointerTy_10 = PointerType::get(PointerTy_4, 0);
  111.    
  112.     std::vector<Type*>FuncTy_12_args;
  113.     FuncTy_12_args.push_back(PointerTy_4);
  114.     FunctionType* FuncTy_12 = FunctionType::get(
  115.                                                 /*Result=*/Type::getVoidTy(mod->getContext()),
  116.                                                 /*Params=*/FuncTy_12_args,
  117.                                                 /*isVarArg=*/false);
  118.    
  119.     PointerType* PointerTy_11 = PointerType::get(FuncTy_12, 0);
  120.    
  121.    
  122.     // Function Declarations
  123.    
  124.     Function* func_foo = mod->getFunction("foo");
  125.     if (!func_foo) {
  126.         func_foo = Function::Create(
  127.                                     /*Type=*/FuncTy_0,
  128.                                     /*Linkage=*/GlobalValue::ExternalLinkage,
  129.                                     /*Name=*/"foo", mod);
  130.         func_foo->setCallingConv(CallingConv::C);
  131.     }
  132.     AttrListPtr func_foo_PAL;
  133.     {
  134.         SmallVector<AttributeWithIndex, 4> Attrs;
  135.         AttributeWithIndex PAWI;
  136.         PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None  | Attribute::NoUnwind | Attribute::StackProtect | Attribute::UWTable;
  137.         Attrs.push_back(PAWI);
  138.         func_foo_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
  139.        
  140.     }
  141.     func_foo->setAttributes(func_foo_PAL);
  142.    
  143.     Function* func_dispatch_group_create = mod->getFunction("dispatch_group_create");
  144.     if (!func_dispatch_group_create) {
  145.         func_dispatch_group_create = Function::Create(
  146.                                                       /*Type=*/FuncTy_6,
  147.                                                       /*Linkage=*/GlobalValue::ExternalLinkage,
  148.                                                       /*Name=*/"dispatch_group_create", mod); // (external, no body)
  149.         func_dispatch_group_create->setCallingConv(CallingConv::C);
  150.     }
  151.     AttrListPtr func_dispatch_group_create_PAL;
  152.     func_dispatch_group_create->setAttributes(func_dispatch_group_create_PAL);
  153.    
  154.     Function* func_dispatch_release = mod->getFunction("dispatch_release");
  155.     if (!func_dispatch_release) {
  156.         func_dispatch_release = Function::Create(
  157.                                                  /*Type=*/FuncTy_12,
  158.                                                  /*Linkage=*/GlobalValue::ExternalLinkage,
  159.                                                  /*Name=*/"dispatch_release", mod); // (external, no body)
  160.         func_dispatch_release->setCallingConv(CallingConv::C);
  161.     }
  162.     AttrListPtr func_dispatch_release_PAL;
  163.     {
  164.         SmallVector<AttributeWithIndex, 4> Attrs;
  165.         AttributeWithIndex PAWI;
  166.         PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None  | Attribute::NoUnwind;
  167.         Attrs.push_back(PAWI);
  168.         func_dispatch_release_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
  169.        
  170.     }
  171.     func_dispatch_release->setAttributes(func_dispatch_release_PAL);
  172.    
  173.     Function* func_llvm_memcpy_p0i8_p0i8_i64 = mod->getFunction("llvm.memcpy.p0i8.p0i8.i64");
  174.     if (!func_llvm_memcpy_p0i8_p0i8_i64) {
  175.         func_llvm_memcpy_p0i8_p0i8_i64 = Function::Create(
  176.                                                           /*Type=*/FuncTy_9,
  177.                                                           /*Linkage=*/GlobalValue::ExternalLinkage,
  178.                                                           /*Name=*/"llvm.memcpy.p0i8.p0i8.i64", mod); // (external, no body)
  179.         func_llvm_memcpy_p0i8_p0i8_i64->setCallingConv(CallingConv::C);
  180.     }
  181.     AttrListPtr func_llvm_memcpy_p0i8_p0i8_i64_PAL;
  182.     {
  183.         SmallVector<AttributeWithIndex, 4> Attrs;
  184.         AttributeWithIndex PAWI;
  185.         PAWI.Index = 1U; PAWI.Attrs = Attribute::None  | Attribute::NoCapture;
  186.         Attrs.push_back(PAWI);
  187.         PAWI.Index = 2U; PAWI.Attrs = Attribute::None  | Attribute::NoCapture;
  188.         Attrs.push_back(PAWI);
  189.         PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None  | Attribute::NoUnwind;
  190.         Attrs.push_back(PAWI);
  191.         func_llvm_memcpy_p0i8_p0i8_i64_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
  192.        
  193.     }
  194.     func_llvm_memcpy_p0i8_p0i8_i64->setAttributes(func_llvm_memcpy_p0i8_p0i8_i64_PAL);
  195.    
  196.     // Global Variable Declarations
  197.    
  198.    
  199.     // Constant Definitions
  200.     ConstantInt* const_int32_13 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("1"), 10));
  201.     ConstantInt* const_int64_14 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("8"), 10));
  202.     ConstantInt* const_int32_15 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("8"), 10));
  203.     ConstantInt* const_int1_16 = ConstantInt::get(mod->getContext(), APInt(1, StringRef("0"), 10));
  204.     ConstantInt* const_int32_17 = ConstantInt::get(mod->getContext(), APInt(32, StringRef("0"), 10));
  205.    
  206.     // Global Variable Definitions
  207.    
  208.     // Function Definitions
  209.    
  210.     // Function: foo (func_foo)
  211.     {
  212.        
  213.         BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func_foo,0);
  214.        
  215.         // Block entry (label_entry)
  216.         AllocaInst* ptr_g = new AllocaInst(PointerTy_2, "g", label_entry);
  217.         ptr_g->setAlignment(8);
  218.         AllocaInst* ptr_agg_tmp = new AllocaInst(StructTy_union_dispatch_object_t, "agg.tmp", label_entry);
  219.         ptr_agg_tmp->setAlignment(8);
  220.         AllocaInst* ptr__compoundliteral = new AllocaInst(StructTy_union_dispatch_object_t, ".compoundliteral", label_entry);
  221.         ptr__compoundliteral->setAlignment(8);
  222.         CallInst* ptr_call = CallInst::Create(func_dispatch_group_create, "call", label_entry);
  223.         ptr_call->setCallingConv(CallingConv::C);
  224.         ptr_call->setTailCall(false);
  225.         AttrListPtr ptr_call_PAL;
  226.         ptr_call->setAttributes(ptr_call_PAL);
  227.        
  228.         StoreInst* void_18 = new StoreInst(ptr_call, ptr_g, false, label_entry);
  229.         void_18->setAlignment(8);
  230.         CastInst* ptr__dg = new BitCastInst(ptr__compoundliteral, PointerTy_1, "_dg", label_entry);
  231.         LoadInst* ptr_19 = new LoadInst(ptr_g, "", false, label_entry);
  232.         ptr_19->setAlignment(8);
  233.         StoreInst* void_20 = new StoreInst(ptr_19, ptr__dg, false, label_entry);
  234.         void_20->setAlignment(8);
  235.         CastInst* ptr_21 = new BitCastInst(ptr_agg_tmp, PointerTy_7, "", label_entry);
  236.         CastInst* ptr_22 = new BitCastInst(ptr__compoundliteral, PointerTy_7, "", label_entry);
  237.         std::vector<Value*> void_23_params;
  238.         void_23_params.push_back(ptr_21);
  239.         void_23_params.push_back(ptr_22);
  240.         void_23_params.push_back(const_int64_14);
  241.         void_23_params.push_back(const_int32_15);
  242.         void_23_params.push_back(const_int1_16);
  243.         CallInst* void_23 = CallInst::Create(func_llvm_memcpy_p0i8_p0i8_i64, void_23_params, "", label_entry);
  244.         void_23->setCallingConv(CallingConv::C);
  245.         void_23->setTailCall(false);
  246.         AttrListPtr void_23_PAL;
  247.         void_23->setAttributes(void_23_PAL);
  248.        
  249.         std::vector<Value*> ptr_coerce_dive_indices;
  250.         ptr_coerce_dive_indices.push_back(const_int32_17);
  251.         ptr_coerce_dive_indices.push_back(const_int32_17);
  252.         Instruction* ptr_coerce_dive = GetElementPtrInst::Create(ptr_agg_tmp, ptr_coerce_dive_indices, "coerce.dive", label_entry);
  253.         LoadInst* ptr_24 = new LoadInst(ptr_coerce_dive, "", false, label_entry);
  254.         CallInst* void_25 = CallInst::Create(func_dispatch_release, ptr_24, "", label_entry);
  255.         void_25->setCallingConv(CallingConv::C);
  256.         void_25->setTailCall(false);
  257.         AttrListPtr void_25_PAL;
  258.         {
  259.             SmallVector<AttributeWithIndex, 4> Attrs;
  260.             AttributeWithIndex PAWI;
  261.             PAWI.Index = 4294967295U; PAWI.Attrs = Attribute::None  | Attribute::NoUnwind;
  262.             Attrs.push_back(PAWI);
  263.             void_25_PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
  264.            
  265.         }
  266.         void_25->setAttributes(void_25_PAL);
  267.        
  268.         ReturnInst::Create(mod->getContext(), label_entry);
  269.        
  270.     }
  271.    
  272.     return mod;
  273. }
  274.  
  275. Module * readModuleFromBitcode(std::string inputPath)
  276. {
  277.     OwningPtr<MemoryBuffer> mb;
  278.     error_code err = MemoryBuffer::getFile(inputPath.c_str(), mb);
  279.     if (err) {
  280.         printf("Can't open '%s'.\n", inputPath.c_str());
  281.         return NULL;
  282.     }
  283.    
  284.     std::string error;
  285.     Module *module = ParseBitcodeFile(&(*mb), getGlobalContext(), &error);
  286.     if (! module) {
  287.         fprintf(stderr, "Can't parse '%s': %s.\n", inputPath.c_str(), error.c_str());
  288.         return NULL;
  289.     }
  290.    
  291.     return module;
  292. }
  293.  
  294. bool writeModuleToBitcode(Module *module, std::string outputPath)
  295. {
  296.     if (verifyModule(*module, PrintMessageAction))
  297.     {
  298.         fprintf(stderr, "Module verification failed.\n");
  299.         return true;
  300.     }
  301.    
  302.     int fd = open(outputPath.c_str(), O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
  303.     raw_fd_ostream out(fd, true);
  304.     WriteBitcodeToFile(module, out);
  305.    
  306.     return false;
  307. }
  308.  
  309.  
  310.  
  311. int main(int argc, char **argv)
  312. {
  313.     // Read other.bc
  314.     Module *otherModule = readModuleFromBitcode("other.bc");
  315.    
  316.     // Generate and write main.bc
  317.     Module *mainModuleWritten = generateCode();
  318.     writeModuleToBitcode(mainModuleWritten, "main.bc");
  319.    
  320.     // Read main.bc
  321.     Module *mainModule = readModuleFromBitcode("main.bc");
  322.    
  323.     // Link other.bc into main.bc
  324.     Linker linker("", mainModule);
  325.     std::string errMsg;
  326.     bool isErr = linker.LinkInModule(otherModule, &errMsg);
  327.     if (isErr)
  328.     {
  329.         fprintf(stderr, "Couldn't link in module: %s\n", errMsg.c_str());
  330.         return 1;
  331.     }
  332.  
  333.    
  334.     generateCode();
  335. }
Advertisement
RAW Paste Data Copied
Advertisement