2010/1/11 Garrison Venn <span dir="ltr"><<a href="mailto:gvenn.cfe.dev@gmail.com">gvenn.cfe.dev@gmail.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div style="word-wrap: break-word;">Does the C API have an equivalent of stack storage? Via the C++ APIs one can shove the string constant on the stack via<div>a store instruction operation on an alloca instruction--the address needed is the alloca. For example:</div>
<div><br></div><div>llvm::Value* stringVar = builder.CreateAlloca(stringConstant->getType());<br>builder.CreateStore(stringConstant, stringVar);</div><div><br></div><div>The stringVar is your address.</div><div><br></div>
<div>Garrison<br></div></div></blockquote><div><br>Thanks but I want something I can use to generate code for a pointer to a C string in any context so I should avoid alloca. Otherwise I'll risk massive stack growth if the generated code is within a loop for example. It looks like using a global variable is the canonical way to do this so I'll stick with it.<br>
<br>-- James<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div><div><br><div><div><div>
<div></div><div class="h5"><div>On Jan 11, 2010, at 10:07, James Williams wrote:</div><br></div></div><blockquote type="cite"><div><div></div><div class="h5">Hi,<br><br>I've read <a href="http://llvm.org/docs/LangRef.html#t_array" target="_blank">http://llvm.org/docs/LangRef.html#t_array</a> and <a href="http://llvm.org/docs/GetElementPtr.html" target="_blank">http://llvm.org/docs/GetElementPtr.html</a> and if I've understood right there are no operations that act directly on arrays - instead I need to use getelementptr on a pointer to an array to get a pointer to an array element. I also understand that there is no 'address of' operation.<br>
<br>As a result I can't figure out how to use constant derived types without assigning them to a global. Say I want to use the C bindings function LLVMValueRef LLVMConstString(char *, int, int) to get an int8* pointer to a C string constant - there doesn't seem to be any way to directly use the resulting [N x i8] value directly and there's no operator that gives me its address.<br>
<br>The only way I can see to get a pointer to the string constant array is to go through a global variable, for example:<br><br>g = LLVMAddGlobal(module, LLVMTypeOf(v), "__string_" + string_literal_number);<br>
string_literal_number = string_literal_number + 1;<br>v = LLVMConstString(string_literal, string_literal.Length, 0);<br>LLVMSetInitializer(g, v);<br>elements = { LLVMConstInt(LLVMInt32Type(), 0L, 0), LLVMConstInt(LLVMInt32Type(), 0L, 0) };<br>
return LLVMConstInBoundsGEP(g, elements, 2);<br><br>Is it possible to get the address of an element of a constant
array or struct without first initializing a global variable to the constant?<br>
<br>Thanks in advance,<br>-- James Williams<br></div></div><div class="im">
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br></div></blockquote></div><br></div></div></div></div></div></blockquote></div><br>