<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>I didn't have Mips board. I compile as the commands and check the asm output as below.</div><div><br></div><div><div>1. Question: </div><div>The distance of caller arg[4] and arg[5] is 4 bytes. But the the callee get every </div><div>arg[] by 8 bytes offset (arg_ptr1+8 or arg_ptr2+8). I assume the #BB#4 and #BB#5 are the arg_ptr which is the pointer to access the stack arguments.</div></div><div><br></div><div>2. Question:</div><div>Stack memory 28($sp) has no initial value. If this memory address is a relocation record which set value by linker/loader, then it maybe is correct.</div><div><br></div><div><br></div><div>clang -c ch8_3.cpp -emit-llvm -o ch8_3.bc</div><div>llc -march=mips -relocation-model=pic -filetype=asm ch8_3.bc -o ch8_3.mips.s</div><div><br></div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.section .mdebug.abi32</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.previous</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.file<span class="Apple-tab-span" style="white-space:pre"> </span>"ch8_3.bc"</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.text</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.globl<span class="Apple-tab-span" style="white-space:pre"> </span>_Z5sum_iiz</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.align<span class="Apple-tab-span" style="white-space:pre"> </span>2</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.type<span class="Apple-tab-span" style="white-space:pre"> </span>_Z5sum_iiz,@function</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>nomips16 # @_Z5sum_iiz</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.ent<span class="Apple-tab-span" style="white-space:pre"> </span>_Z5sum_iiz</div><div>_Z5sum_iiz:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_startproc</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.frame<span class="Apple-tab-span" style="white-space:pre"> </span>$sp,64,$ra</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.mask <span class="Apple-tab-span" style="white-space:pre"> </span>0x80000000,-4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.fmask<span class="Apple-tab-span" style="white-space:pre"> </span>0x00000000,0</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>noreorder</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>nomacro</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>noat</div><div># BB#0:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lui<span class="Apple-tab-span" style="white-space:pre"> </span>$2, %hi(_gp_disp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$2, $2, %lo(_gp_disp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$sp, $sp, -64</div><div>$tmp2:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_def_cfa_offset 64</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$ra, 60($sp) # 4-byte Folded Spill</div><div>$tmp3:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_offset 31, -4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addu<span class="Apple-tab-span" style="white-space:pre"> </span>$gp, $2, $25</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$7, 76($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$6, 72($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$5, 68($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$3, %got(__stack_chk_guard)($gp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 0($3)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 56($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$4, 52($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$zero, 48($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// i</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$zero, 44($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// val</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$zero, 40($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// sum</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $sp, 68</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 16($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$zero, 48($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>b<span class="Apple-tab-span" style="white-space:pre"> </span>$BB0_2</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$2, $zero, 40</div><div>$BB0_1: # in Loop: Header=BB0_2 Depth=1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 0($4)<span class="Apple-tab-span" style="white-space:pre"> </span>// $1 = *arg_ptr</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 44($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// val</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$4, 40($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// sum</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $4, $1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 40($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// sum += val</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 48($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $1, 1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 48($sp)</div><div>$BB0_2: # =>This Inner Loop Header: Depth=1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 52($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$4, 48($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>slt<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $4, $1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>beq<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $zero, $BB0_6</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>nop</div><div># BB#3: # in Loop: Header=BB0_2 Depth=1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$4, 16($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sltu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $2, $4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>bne<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $zero, $BB0_5</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>nop</div><div># BB#4: # in Loop: Header=BB0_2 Depth=1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $4, 8<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr2 + 8</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$5, 28($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr2_offset has no initial value</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 16($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>b<span class="Apple-tab-span" style="white-space:pre"> </span>$BB0_1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addu<span class="Apple-tab-span" style="white-space:pre"> </span>$4, $5, $4</div><div>$BB0_5: # in Loop: Header=BB0_2 Depth=1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$4, 24($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr2</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $4, 8<span class="Apple-tab-span" style="white-space:pre"> </span>// arg_ptr2 + 8</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 24($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>b<span class="Apple-tab-span" style="white-space:pre"> </span>$BB0_1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>nop</div><div>$BB0_6:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 0($3)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$3, 56($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>bne<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $3, $BB0_8</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$2, 40($sp)</div><div># BB#7: # %SP_return</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$ra, 60($sp) # 4-byte Folded Reload</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>jr<span class="Apple-tab-span" style="white-space:pre"> </span>$ra</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$sp, $sp, 64</div><div>$BB0_8: # %CallStackCheckFailBlk</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$25, %call16(__stack_chk_fail)($gp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>jalr<span class="Apple-tab-span" style="white-space:pre"> </span>$25</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>nop</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>at</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>macro</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>reorder</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.end<span class="Apple-tab-span" style="white-space:pre"> </span>_Z5sum_iiz</div><div>$tmp4:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.size<span class="Apple-tab-span" style="white-space:pre"> </span>_Z5sum_iiz, ($tmp4)-_Z5sum_iiz</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_endproc</div><div><br></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.globl<span class="Apple-tab-span" style="white-space:pre"> </span>main</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.align<span class="Apple-tab-span" style="white-space:pre"> </span>2</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.type<span class="Apple-tab-span" style="white-space:pre"> </span>main,@function</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>nomips16 # @main</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.ent<span class="Apple-tab-span" style="white-space:pre"> </span>main</div><div>main:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_startproc</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.frame<span class="Apple-tab-span" style="white-space:pre"> </span>$sp,48,$ra</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.mask <span class="Apple-tab-span" style="white-space:pre"> </span>0x80000000,-4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.fmask<span class="Apple-tab-span" style="white-space:pre"> </span>0x00000000,0</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>noreorder</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>nomacro</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>noat</div><div># BB#0:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lui<span class="Apple-tab-span" style="white-space:pre"> </span>$2, %hi(_gp_disp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$2, $2, %lo(_gp_disp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$sp, $sp, -48</div><div>$tmp7:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_def_cfa_offset 48</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$ra, 44($sp) # 4-byte Folded Spill</div><div>$tmp8:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_offset 31, -4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addu<span class="Apple-tab-span" style="white-space:pre"> </span>$gp, $2, $25</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$zero, 40($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $zero, 5</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 20($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg[5]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $zero, 4</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 16($sp)<span class="Apple-tab-span" style="white-space:pre"> </span>// arg[4]</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$1, $zero, 6</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$1, 24($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$25, %call16(_Z5sum_iiz)($gp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$4, $zero, 6</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$5, $zero, 1</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$6, $zero, 2</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>jalr<span class="Apple-tab-span" style="white-space:pre"> </span>$25</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$7, $zero, 3</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>sw<span class="Apple-tab-span" style="white-space:pre"> </span>$2, 36($sp)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>lw<span class="Apple-tab-span" style="white-space:pre"> </span>$ra, 44($sp) # 4-byte Folded Reload</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>jr<span class="Apple-tab-span" style="white-space:pre"> </span>$ra</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>addiu<span class="Apple-tab-span" style="white-space:pre"> </span>$sp, $sp, 48</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>at</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>macro</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.set<span class="Apple-tab-span" style="white-space:pre"> </span>reorder</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.end<span class="Apple-tab-span" style="white-space:pre"> </span>main</div><div>$tmp9:</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.size<span class="Apple-tab-span" style="white-space:pre"> </span>main, ($tmp9)-main</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>.cfi_endproc</div><div><br></div><div><br></div></div><div><br></div><div><div>On 2013/2/20, at 上午3:04, Akira Hatanaka <<a href="mailto:ahatanak@gmail.com">ahatanak@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Which part of the generated code do you think is not correct? Could you be more specific?<br><br>I compiled this program with clang and ran it on a mips board. It returns the expected result (21).<br><br><div class="gmail_quote">
On Tue, Feb 19, 2013 at 4:15 AM, Jonathan <span dir="ltr"><<a href="mailto:gamma_chen@yahoo.com.tw" target="_blank">gamma_chen@yahoo.com.tw</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I check the Mips backend for the following C code fragment compile result. It seems not correct. Is it my misunderstand or it's a bug.<br>
<br>
//ch8_3.cpp<br>
#include <stdarg.h><br>
<br>
int sum_i(int amount, ...)<br>
{<br>
int i = 0;<br>
int val = 0;<br>
int sum = 0;<br>
<br>
va_list vl;<br>
va_start(vl, amount);<br>
for (i = 0; i < amount; i++)<br>
{<br>
val = va_arg(vl, int);<br>
sum += val;<br>
}<br>
va_end(vl);<br>
<br>
return sum;<br>
}<br>
<br>
int main()<br>
{<br>
int a = sum_i(6, 1, 2, 3, 4, 5, 6);<br>
<br>
return a;<br>
}<br>
<br>
<br>
<br>
_______________________________________________<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>
</blockquote></div><br>
</blockquote></div><br></body></html>