I'm writing a C compiler in OCaml and I've run into a small problem. I wrote the following piece of code to generate a pointer to a string constant, so I could compile C expressions of the form "const char* p = "test\n";" : let strval = const_stringz codecontext v in dump_value strval; dump_value i32_zero; const_gep strval [| i32_zero; i32_zero |] The dump statements I put confirm that the string array and the index values have the types I would expect: [6 x i8] c"test\0A\00" i32 0 However, the code still seems to break an assertion: Constants.cpp:1487: static llvm::Constant* llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, llvm::Value* const*, unsigned int): Assertion `Ty && "GEP indices invalid!"' failed. Aborted Could someone tell me what I'm doing wrong here? -- View this message in context: http://old.nabble.com/Pointer-to-String-Constant-tp27788693p27788693.html Sent from the LLVM - Dev mailing list archive at Nabble.com.
On Mar 4, 2010, at 4:50 PM, Nyx wrote:> > I'm writing a C compiler in OCaml and I've run into a small problem. I wrote > the following piece of code to generate a pointer to a string constant, so I > could compile C expressions of the form "const char* p = "test\n";" : > > let strval = const_stringz codecontext v in > dump_value strval; > dump_value i32_zero; > const_gep strval [| i32_zero; i32_zero |] > > The dump statements I put confirm that the string array and the index values > have the types I would expect: > > [6 x i8] c"test\0A\00" > i32 0 > > However, the code still seems to break an assertion: > > Constants.cpp:1487: static llvm::Constant* > llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, llvm::Value* const*, > unsigned int): Assertion `Ty && "GEP indices invalid!"' failed. > Aborted > > Could someone tell me what I'm doing wrong here?It's hard to tell without more of the source code, but you probably need something like: getelementptr [6 x i8] * c"test\0A\00", i32 0, i32 0 -wb^-1
>> It's hard to tell without more of the source code, but you probably needsomething like:>> getelementptr [6 x i8] * c"test\0A\00", i32 0, i32 0Isn't that pretty much what I have? I'm doing a constant gep on the string constant, which registers as an array of type [6 x i8], with two i32 0 indices. Note that I tried with a non-constant gep also, and it does the same thing. -- View this message in context: http://old.nabble.com/Pointer-to-String-Constant-tp27788693p27789107.html Sent from the LLVM - Dev mailing list archive at Nabble.com.
On Thu, Mar 04, 2010 at 04:50:41PM -0800, Nyx wrote:> > I'm writing a C compiler in OCaml and I've run into a small problem. I wrote > the following piece of code to generate a pointer to a string constant, so I > could compile C expressions of the form "const char* p = "test\n";" : > > let strval = const_stringz codecontext v in > dump_value strval; > dump_value i32_zero; > const_gep strval [| i32_zero; i32_zero |] > > The dump statements I put confirm that the string array and the index values > have the types I would expect: > > [6 x i8] c"test\0A\00" > i32 0 > > However, the code still seems to break an assertion: > > Constants.cpp:1487: static llvm::Constant* > llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, llvm::Value* const*, > unsigned int): Assertion `Ty && "GEP indices invalid!"' failed. > Aborted > > Could someone tell me what I'm doing wrong here?A getelementptr takes a pointer as the first element. Unlike C, arrays are first class values, not pointers. To index into an array, you need extractvalue. To have a C like array, you would need to allocate one in memory, either as a global variable or with malloc, or alloca. Any of those would give you a pointer to an array, which you could index. Have a look at http://www.llvm.org/docs/GetElementPtr.html for more details on getelementptr. Tom