4

I'm working on a program in LLVM IR, and I am trying to initialize a string that says "Hello World!" but I can't figure out how. The goal of the code is to count the number of characters in the string. Before the string needs to be initialized, and after the headers, I have the following:

int main (int argc, const char *argv[]) {
    //Setting up 
    //Build a pointer to the string - LLVMValueRef *strptr=LLVMBuildGlobalStringPtr(builder, const char *string, const char *name);
    LLVMValueRef *strptr;
    LLVMContextRef context = LLVMContextCreate();
    LLVMBuilderRef builder = LLVMCreateBuilderInContext (context);
    LLVMModuleRef module1 = LLVMModuleCreateWithNameInContext("mod", context);
}
Flow
  • 23,572
  • 15
  • 99
  • 156
FCo
  • 485
  • 5
  • 17
  • What do you can't figure out? I don't know much about this, but I'd say the first `char *` is the content (here `Hello World`), and the second one something like an variable name – JDS May 20 '13 at 19:40
  • I tried that but it didn't work. I don't even know if I should be building a pointer to the string or if I need to make the string a global variable then store it. – FCo May 20 '13 at 19:43

1 Answers1

9

The easiest way to see how such things are by using the C++ backend - it generates the C++ API calls that build the module for you. You can see this done online.

"Compile" this code:

const char* foo() {
  const char* s = "hello world";
  return s;
}

And here are the relevant C++ API calls:

GlobalVariable* gvar_array__str = new GlobalVariable(/*Module=*/*mod, 
 /*Type=*/ArrayTy_0,
 /*isConstant=*/true,
 /*Linkage=*/GlobalValue::PrivateLinkage,
 /*Initializer=*/0, // has initializer, specified below
 /*Name=*/".str");
 gvar_array__str->setAlignment(1);

 // Constant Definitions
 Constant *const_array_4 = ConstantDataArray::getString(mod->getContext(), "hello world", true);
 std::vector<Constant*> const_ptr_5_indices;
 ConstantInt* const_int64_6 = ConstantInt::get(mod->getContext(), APInt(64, StringRef("0"), 10));
 const_ptr_5_indices.push_back(const_int64_6);
 const_ptr_5_indices.push_back(const_int64_6);
 Constant* const_ptr_5 = ConstantExpr::getGetElementPtr(gvar_array__str, const_ptr_5_indices);

 // Global Variable Definitions
 gvar_array__str->setInitializer(const_array_4);

 // Function Definitions

 // Function: foo (func_foo)
 {

  BasicBlock* label_entry = BasicBlock::Create(mod->getContext(), "entry",func_foo,0);

  // Block entry (label_entry)
  ReturnInst::Create(mod->getContext(), const_ptr_5, label_entry);

 }
Flow
  • 23,572
  • 15
  • 99
  • 156
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 4
    Update to anyone who sees this -- don't bother trying the C++ backend, [it doesn't exist anymore](https://stackoverflow.com/questions/14751403/generate-llvm-c-api-code-as-backend). – tonysdg Sep 14 '17 at 20:54
  • @tonysdg do you know any other way to show "how to use c/c++api generate IR"? I felt there is not enough document for those APIs. I don't bother use older version llvm and guess how the new version llvm works. – worldterminator Feb 18 '20 at 11:05
  • @worldterminator Sorry, I haven't been working with LLVM for over a year now. Best of luck!! – tonysdg Feb 22 '20 at 02:59
  • @tonysdg Pretty sure the C++ backend still exists - you just can't generate C++ code with LLVM API calls anymore. – Jeppe Dec 27 '22 at 22:28