How can I declare a global variable in LLVM?

后端 未结 1 1501
太阳男子
太阳男子 2021-02-05 14:20

I\'d like to record some dynamic behaviors into some global variables. So I wrote a pass to instrument the code and insert some instructions to update the global variable. I tri

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-05 14:51

    There is a tool which can answer this and many other questions about LLVM API: llc -march=cpp. You can generate a bitcode file using Clang or llvm-gcc, and then build a C++ code which should reconstruct the same module using the cpp backend.

    A sample output, showing how to define a global int * variable:

    // Global Variable Declarations
    
    GlobalVariable* gvar_ptr_abc = new GlobalVariable(/*Module=*/*mod, 
            /*Type=*/PointerTy_0,
            /*isConstant=*/false,
            /*Linkage=*/GlobalValue::CommonLinkage,
            /*Initializer=*/0, // has initializer, specified below
            /*Name=*/"abc");
    gvar_ptr_abc->setAlignment(4);
    
    // Constant Definitions
    ConstantPointerNull* const_ptr_2 = ConstantPointerNull::get(PointerTy_0);
    
    // Global Variable Definitions
    gvar_ptr_abc->setInitializer(const_ptr_2);
    

    0 讨论(0)
提交回复
热议问题