title: $gp, .cpload and position independence on MIPS
body: <p>I'm reading about PIC implementation on MIPS on Linux <a href="http://www.linux-mips.org/wiki/PIC_code" rel="nofollow">here</a>. It says:</p>
  
  <blockquote>
    <p>The global pointer which is stored in the $gp register (aka $28) is a callee saved register.</p>
  </blockquote>
  
  <p>The <a href="http://en.wikipedia.org/wiki/MIPS_architecture#Compiler_register_usage" rel="nofollow">Wikipedia article about MIPS</a> says the same.</p>
  
  <p>However, according to them, when a <code>.cpload</code> directive is being used in function prologue, it clobbers the previous value of $gp without saving it first. When a <code>.cprestore</code> is used, it saves the <em>current</em> $gp to the stack frame, as opposed to the value of $gp that was there on function entrance. Same goes for the effect <code>.cprestore</code> has on <code>jal</code>/<code>jalr</code>: it restores $gp once the callee returns - assuming the callee might've clobbered it.</p>
  
  <p>And finally, there's nothing in the function epilogue about $gp.</p>
  
  <p>All in all, doesn't sound like a callee-saved register to me. Sounds like a caller-saved register. What am I misunderstanding here?</p>