<html><head><meta http-equiv="Content-Type" content="text/html charset=big5"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">Thank you. It's work on Linux by following your instructions. <div><div><div>clang -target mips-linux-gnu</div><div><br></div>Do you know how to set clang -target option for iMac? Can we get the -target options available by command, </div><div>clang --help</div><div><div><br></div><div>Jonathan</div><div><br></div><div>On 2013/2/20, at ¤W¤È10:09, Akira Hatanaka <<a href="mailto:ahatanak@gmail.com">ahatanak@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Does it make a difference if you give the "-target" option to clang?<br><br>$ clang -target mips-linux-gnu ch8_3.cpp  -o ch8_3.bc -emit-llvm -c<br><br>The .s file generated this way looks quite different from the one in your email.<br>
<br><div class="gmail_quote">On Tue, Feb 19, 2013 at 5:06 PM, 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">
<div style="word-wrap:break-word"><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 style="white-space:pre-wrap"> </span>.section .mdebug.abi32</div>
<div><span style="white-space:pre-wrap">  </span>.previous</div><div><span style="white-space:pre-wrap">        </span>.file<span style="white-space:pre-wrap">   </span>"ch8_3.bc"</div><div><span style="white-space:pre-wrap">     </span>.text</div>
<div><span style="white-space:pre-wrap">  </span>.globl<span style="white-space:pre-wrap">  </span>_Z5sum_iiz</div><div><span style="white-space:pre-wrap">       </span>.align<span style="white-space:pre-wrap">  </span>2</div><div>
<span style="white-space:pre-wrap">     </span>.type<span style="white-space:pre-wrap">   </span>_Z5sum_iiz,@function</div><div><span style="white-space:pre-wrap">     </span>.set<span style="white-space:pre-wrap">    </span>nomips16                # @_Z5sum_iiz</div>
<div><span style="white-space:pre-wrap">  </span>.ent<span style="white-space:pre-wrap">    </span>_Z5sum_iiz</div><div>_Z5sum_iiz:</div><div><span style="white-space:pre-wrap">     </span>.cfi_startproc</div><div><span style="white-space:pre-wrap">   </span>.frame<span style="white-space:pre-wrap">  </span>$sp,64,$ra</div>
<div><span style="white-space:pre-wrap">  </span>.mask <span style="white-space:pre-wrap">  </span>0x80000000,-4</div><div><span style="white-space:pre-wrap">    </span>.fmask<span style="white-space:pre-wrap">  </span>0x00000000,0</div>
<div><span style="white-space:pre-wrap">  </span>.set<span style="white-space:pre-wrap">    </span>noreorder</div><div><span style="white-space:pre-wrap">        </span>.set<span style="white-space:pre-wrap">    </span>nomacro</div><div>
<span style="white-space:pre-wrap">     </span>.set<span style="white-space:pre-wrap">    </span>noat</div><div># BB#0:</div><div><span style="white-space:pre-wrap">       </span>lui<span style="white-space:pre-wrap">     </span>$2, %hi(_gp_disp)</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$2, $2, %lo(_gp_disp)</div><div><span style="white-space:pre-wrap">    </span>addiu<span style="white-space:pre-wrap">   </span>$sp, $sp, -64</div>
<div>$tmp2:</div><div><span style="white-space:pre-wrap">     </span>.cfi_def_cfa_offset 64</div><div><span style="white-space:pre-wrap">   </span>sw<span style="white-space:pre-wrap">      </span>$ra, 60($sp)            # 4-byte Folded Spill</div>
<div>$tmp3:</div><div><span style="white-space:pre-wrap">     </span>.cfi_offset 31, -4</div><div><span style="white-space:pre-wrap">       </span>addu<span style="white-space:pre-wrap">    </span>$gp, $2, $25</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$7, 76($sp)</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$6, 72($sp)</div><div><span style="white-space:pre-wrap">      </span>sw<span style="white-space:pre-wrap">      </span>$5, 68($sp)</div><div>
<span style="white-space:pre-wrap">     </span>lw<span style="white-space:pre-wrap">      </span>$3, %got(__stack_chk_guard)($gp)</div><div><span style="white-space:pre-wrap"> </span>lw<span style="white-space:pre-wrap">      </span>$1, 0($3)</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 56($sp)</div><div><span style="white-space:pre-wrap">      </span>sw<span style="white-space:pre-wrap">      </span>$4, 52($sp)</div><div>
<span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$zero, 48($sp)<span style="white-space:pre-wrap">  </span>// i</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$zero, 44($sp)<span style="white-space:pre-wrap">  </span>// val</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$zero, 40($sp)<span style="white-space:pre-wrap">  </span>// sum</div><div><span style="white-space:pre-wrap">   </span>addiu<span style="white-space:pre-wrap">   </span>$1, $sp, 68</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 16($sp)<span style="white-space:pre-wrap">                     </span>// arg_ptr1</div><div><span style="white-space:pre-wrap">      </span>sw<span style="white-space:pre-wrap">      </span>$zero, 48($sp)</div>
<div><span style="white-space:pre-wrap">  </span>b<span style="white-space:pre-wrap">       </span>$BB0_2</div><div><span style="white-space:pre-wrap">   </span>addiu<span style="white-space:pre-wrap">   </span>$2, $zero, 40</div><div>
$BB0_1:                                 #   in Loop: Header=BB0_2 Depth=1</div><div><span style="white-space:pre-wrap">        </span>lw<span style="white-space:pre-wrap">      </span>$1, 0($4)<span style="white-space:pre-wrap">                       </span>// $1 = *arg_ptr</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 44($sp)<span style="white-space:pre-wrap">                     </span>// val</div><div><span style="white-space:pre-wrap">   </span>lw<span style="white-space:pre-wrap">      </span>$4, 40($sp)<span style="white-space:pre-wrap">                     </span>// sum</div>
<div><span style="white-space:pre-wrap">  </span>addu<span style="white-space:pre-wrap">    </span>$1, $4, $1</div><div><span style="white-space:pre-wrap">       </span>sw<span style="white-space:pre-wrap">      </span>$1, 40($sp)<span style="white-space:pre-wrap">                     </span>// sum += val</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$1, 48($sp)</div><div><span style="white-space:pre-wrap">      </span>addiu<span style="white-space:pre-wrap">   </span>$1, $1, 1</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 48($sp)</div><div>$BB0_2:                                 # =>This Inner Loop Header: Depth=1</div><div><span style="white-space:pre-wrap"> </span>lw<span style="white-space:pre-wrap">      </span>$1, 52($sp)</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$4, 48($sp)</div><div><span style="white-space:pre-wrap">      </span>slt<span style="white-space:pre-wrap">     </span>$1, $4, $1</div><div>
<span style="white-space:pre-wrap">     </span>beq<span style="white-space:pre-wrap">     </span>$1, $zero, $BB0_6</div><div><span style="white-space:pre-wrap">        </span>nop</div><div># BB#3:                                 #   in Loop: Header=BB0_2 Depth=1</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$4, 16($sp)<span style="white-space:pre-wrap">                     </span>// arg_ptr1</div><div><span style="white-space:pre-wrap">      </span>sltu<span style="white-space:pre-wrap">    </span>$1, $2, $4</div>
<div><span style="white-space:pre-wrap">  </span>bne<span style="white-space:pre-wrap">     </span>$1, $zero, $BB0_5</div><div><span style="white-space:pre-wrap">        </span>nop</div><div># BB#4:                                 #   in Loop: Header=BB0_2 Depth=1</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$1, $4, 8<span style="white-space:pre-wrap">               </span>// arg_ptr2 + 8</div><div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$5, 28($sp)<span style="white-space:pre-wrap">                     </span>// arg_ptr2_offset has no initial value</div>
<div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 16($sp)</div><div><span style="white-space:pre-wrap">      </span>b<span style="white-space:pre-wrap">       </span>$BB0_1</div><div><span style="white-space:pre-wrap">   </span>addu<span style="white-space:pre-wrap">    </span>$4, $5, $4</div>
<div>$BB0_5:                                 #   in Loop: Header=BB0_2 Depth=1</div><div><span style="white-space:pre-wrap">     </span>lw<span style="white-space:pre-wrap">      </span>$4, 24($sp)<span style="white-space:pre-wrap">                     </span>// arg_ptr2</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$1, $4, 8<span style="white-space:pre-wrap">               </span>// arg_ptr2 + 8</div><div><span style="white-space:pre-wrap">  </span>sw<span style="white-space:pre-wrap">      </span>$1, 24($sp)</div>
<div><span style="white-space:pre-wrap">  </span>b<span style="white-space:pre-wrap">       </span>$BB0_1</div><div><span style="white-space:pre-wrap">   </span>nop</div><div>$BB0_6:</div><div><span style="white-space:pre-wrap">        </span>lw<span style="white-space:pre-wrap">      </span>$1, 0($3)</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$3, 56($sp)</div><div><span style="white-space:pre-wrap">      </span>bne<span style="white-space:pre-wrap">     </span>$1, $3, $BB0_8</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$2, 40($sp)</div><div># BB#7:                                 # %SP_return</div><div><span style="white-space:pre-wrap">   </span>lw<span style="white-space:pre-wrap">      </span>$ra, 60($sp)            # 4-byte Folded Reload</div>
<div><span style="white-space:pre-wrap">  </span>jr<span style="white-space:pre-wrap">      </span>$ra</div><div><span style="white-space:pre-wrap">      </span>addiu<span style="white-space:pre-wrap">   </span>$sp, $sp, 64</div><div>
$BB0_8:                                 # %CallStackCheckFailBlk</div><div><span style="white-space:pre-wrap">      </span>lw<span style="white-space:pre-wrap">      </span>$25, %call16(__stack_chk_fail)($gp)</div><div><span style="white-space:pre-wrap">      </span>jalr<span style="white-space:pre-wrap">    </span>$25</div>
<div><span style="white-space:pre-wrap">  </span>nop</div><div><span style="white-space:pre-wrap">      </span>.set<span style="white-space:pre-wrap">    </span>at</div><div><span style="white-space:pre-wrap">       </span>.set<span style="white-space:pre-wrap">    </span>macro</div>
<div><span style="white-space:pre-wrap">  </span>.set<span style="white-space:pre-wrap">    </span>reorder</div><div><span style="white-space:pre-wrap">  </span>.end<span style="white-space:pre-wrap">    </span>_Z5sum_iiz</div><div>
$tmp4:</div><div><span style="white-space:pre-wrap">        </span>.size<span style="white-space:pre-wrap">   </span>_Z5sum_iiz, ($tmp4)-_Z5sum_iiz</div><div><span style="white-space:pre-wrap">   </span>.cfi_endproc</div><div><br></div>
<div><span style="white-space:pre-wrap">  </span>.globl<span style="white-space:pre-wrap">  </span>main</div><div><span style="white-space:pre-wrap">     </span>.align<span style="white-space:pre-wrap">  </span>2</div><div><span style="white-space:pre-wrap">        </span>.type<span style="white-space:pre-wrap">   </span>main,@function</div>
<div><span style="white-space:pre-wrap">  </span>.set<span style="white-space:pre-wrap">    </span>nomips16                # @main</div><div><span style="white-space:pre-wrap">  </span>.ent<span style="white-space:pre-wrap">    </span>main</div>
<div>main:</div><div><span style="white-space:pre-wrap">      </span>.cfi_startproc</div><div><span style="white-space:pre-wrap">   </span>.frame<span style="white-space:pre-wrap">  </span>$sp,48,$ra</div><div><span style="white-space:pre-wrap">       </span>.mask <span style="white-space:pre-wrap">  </span>0x80000000,-4</div>
<div><span style="white-space:pre-wrap">  </span>.fmask<span style="white-space:pre-wrap">  </span>0x00000000,0</div><div><span style="white-space:pre-wrap">     </span>.set<span style="white-space:pre-wrap">    </span>noreorder</div>
<div><span style="white-space:pre-wrap">  </span>.set<span style="white-space:pre-wrap">    </span>nomacro</div><div><span style="white-space:pre-wrap">  </span>.set<span style="white-space:pre-wrap">    </span>noat</div><div># BB#0:</div>
<div><span style="white-space:pre-wrap">  </span>lui<span style="white-space:pre-wrap">     </span>$2, %hi(_gp_disp)</div><div><span style="white-space:pre-wrap">        </span>addiu<span style="white-space:pre-wrap">   </span>$2, $2, %lo(_gp_disp)</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$sp, $sp, -48</div><div>$tmp7:</div><div><span style="white-space:pre-wrap">       </span>.cfi_def_cfa_offset 48</div><div><span style="white-space:pre-wrap">   </span>sw<span style="white-space:pre-wrap">      </span>$ra, 44($sp)            # 4-byte Folded Spill</div>
<div>$tmp8:</div><div><span style="white-space:pre-wrap">     </span>.cfi_offset 31, -4</div><div><span style="white-space:pre-wrap">       </span>addu<span style="white-space:pre-wrap">    </span>$gp, $2, $25</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$zero, 40($sp)</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$1, $zero, 5</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$1, 20($sp)<span style="white-space:pre-wrap">                     </span>// arg[5]</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$1, $zero, 4</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$1, 16($sp)<span style="white-space:pre-wrap">                     </span>// arg[4]</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$1, $zero, 6</div><div><span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$1, 24($sp)</div>
<div><span style="white-space:pre-wrap">  </span>lw<span style="white-space:pre-wrap">      </span>$25, %call16(_Z5sum_iiz)($gp)</div><div><span style="white-space:pre-wrap">    </span>addiu<span style="white-space:pre-wrap">   </span>$4, $zero, 6</div>
<div><span style="white-space:pre-wrap">  </span>addiu<span style="white-space:pre-wrap">   </span>$5, $zero, 1</div><div><span style="white-space:pre-wrap">     </span>addiu<span style="white-space:pre-wrap">   </span>$6, $zero, 2</div>
<div><span style="white-space:pre-wrap">  </span>jalr<span style="white-space:pre-wrap">    </span>$25</div><div><span style="white-space:pre-wrap">      </span>addiu<span style="white-space:pre-wrap">   </span>$7, $zero, 3</div><div>
<span style="white-space:pre-wrap">     </span>sw<span style="white-space:pre-wrap">      </span>$2, 36($sp)</div><div><span style="white-space:pre-wrap">      </span>lw<span style="white-space:pre-wrap">      </span>$ra, 44($sp)            # 4-byte Folded Reload</div>
<div><span style="white-space:pre-wrap">  </span>jr<span style="white-space:pre-wrap">      </span>$ra</div><div><span style="white-space:pre-wrap">      </span>addiu<span style="white-space:pre-wrap">   </span>$sp, $sp, 48</div><div>
<span style="white-space:pre-wrap">     </span>.set<span style="white-space:pre-wrap">    </span>at</div><div><span style="white-space:pre-wrap">       </span>.set<span style="white-space:pre-wrap">    </span>macro</div><div><span style="white-space:pre-wrap">    </span>.set<span style="white-space:pre-wrap">    </span>reorder</div>
<div><span style="white-space:pre-wrap">  </span>.end<span style="white-space:pre-wrap">    </span>main</div><div>$tmp9:</div><div><span style="white-space:pre-wrap">        </span>.size<span style="white-space:pre-wrap">   </span>main, ($tmp9)-main</div>
<div><span style="white-space:pre-wrap">  </span>.cfi_endproc</div><div><br></div><div><br></div></div><div><div class="h5"><div><br></div><div><div>On 2013/2/20, at ¤W¤È3:04, Akira Hatanaka <<a href="mailto:ahatanak@gmail.com" target="_blank">ahatanak@gmail.com</a>> wrote:</div>
<br><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></div></div></div></blockquote></div><br>
</blockquote></div><br></div></body></html>