title: Direct C function call using GCC's inline assembly
body: <p>If you want to call a C/C++ function from inline assembly, you can do something like this:</p>
  
  <pre><code>void callee() {}
  void caller()
  {
      asm("call *%0" : : "r"(callee));
  }
  </code></pre>
  
  <p>GCC will then emit code which looks like this:</p>
  
  <pre><code>movl $callee, %eax
  call *%eax
  </code></pre>
  
  <p>This can be problematic since the indirect call will destroy the pipeline on older CPUs.</p>
  
  <p>Since the address of <code>callee</code> is eventually a constant, one can imagine that it would be possible to use the <code>i</code> constraint. Quoting from the GCC online <a href="http://gcc.gnu.org/onlinedocs/gcc/Simple-Constraints.html#Simple-Constraints" rel="noreferrer">docs</a>:</p>
  
  <blockquote>
    <p>`i'</p>
    
    <p>An immediate integer operand (one with constant value) is allowed. This
    includes symbolic constants whose
    values will be known only at assembly
    time or later.</p>
  </blockquote>
  
  <p>If I try to use it like this:</p>
  
  <pre><code>asm("call %0" : : "i"(callee));
  </code></pre>
  
  <p>I get the following error from the assembler:</p>
  
  <blockquote>
    <p>Error: suffix or operands invalid for `call'</p>
  </blockquote>
  
  <p>This is because GCC emits the code</p>
  
  <pre><code>call $callee
  </code></pre>
  
  <p>Instead of</p>
  
  <pre><code>call callee
  </code></pre>
  
  <p>So my question is whether it is possible to make GCC output the correct <code>call</code>.</p>