Bram Adams
2006-Oct-03 19:24 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
Hi, Op 3-okt-06, om 20:48 heeft Chris Lattner het volgende geschreven:> You'd have to change it to something like: > > void foo(int X, ...) { > P = va_start(); > bar(X, P); > } > > void bar(int X, valist P) { > use(P); > }Can the other va_...-intrinsics be used in bar as were the "P = va_start" in bar? The va_start probably is unnecessary now in bar, but where dus the va_end need to be: at the end of foo or of bar or doesn't it matter? Thanks for the remark, Bram Adams GH-SEL, INTEC, Ghent University (Belgium)
Chris Lattner
2006-Oct-03 21:58 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
On Tue, 3 Oct 2006, Bram Adams wrote:>> You'd have to change it to something like: >> void foo(int X, ...) { >> P = va_start(); >> bar(X, P); >> } >> >> void bar(int X, valist P) { >> use(P); >> } > > Can the other va_...-intrinsics be used in bar as were the "P > va_start" in bar? The va_start probably is unnecessary now in bar, > but where dus the va_end need to be: at the end of foo or of bar or > doesn't it matter?All the non-vastart calls can be anywhere. va_end in particular codegens to a noop on all targets llvm currently supports, fwiw. -Chris -- http://nondot.org/sabre/ http://llvm.org/
Bram Adams
2006-Oct-05 12:46 UTC
[LLVMdev] Extracting all BasicBlocks of a Function into new Function
Hi, Chris Lattner wrote:> All the non-vastart calls can be anywhere. va_end in particular codegens > to a noop on all targets llvm currently supports, fwiw. >Things go well, except for the following (pathological?) C program: int va_double_sum(int count,...){ int i,sum=0; va_list ap; va_start(ap,count); for(i=0;i<count;i++){ sum+=va_arg(ap,int); } va_end(ap); va_start(ap,count);/* same vararg opened twice!!! */ for(i=0;i<count;i++){ sum+=va_arg(ap,int); } va_end(ap); return sum; } The problematic issue here is that the va_list is opened and used twice, which should work according to GCC. If I convert the program's LLVM bytecode to the following pseudo-bytecode: int va_double_sum(int count,...){ /*declare va_list*/ /*llvm.va_start va_list*/ /*call extracted_sum(count,va_list)*/ /*llvm.va_end va_list*/ } int extracted_sum(int count,/*vararg-argument*/){ /*see original va_double_sum WITHOUT the llvm.va_start's and llvm.va_end's and WITH all uses of the original va_list replaced with the extra vararg-argument*/ } Apparently, the problem is how to "rewind" the vararg-argument back to its first element when the second loop is reached, as it seems that the code just continues at position count and beyond, resulting in overflow. I'm not sure whether this code snippet is valid ANSI C, and if it is, one probably shouldn't use this approach, but I'm just curious to know how (and if) this problem could be solved. Kind regards, Bram Adams GH-SEL, INTEC, Ghent University (Belgium)
Possibly Parallel Threads
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function
- [LLVMdev] Extracting all BasicBlocks of a Function into new Function