<?xml version="1.0"?>
<paste-with-annotations>
  <paste>
    <number>
      <integer>60596</integer>
    </number>
    <user>
      <string>zack</string>
    </user>
    <title>
      <string>float return</string>
    </title>
    <contents>
      <string>#include &lt;assert.h&gt;
#include &lt;llvm/Module.h&gt;
#include &lt;llvm/ModuleProvider.h&gt;
#include &lt;llvm/Function.h&gt;
#include &lt;llvm/PassManager.h&gt;
#include &lt;llvm/CallingConv.h&gt;
#include &lt;llvm/Analysis/Verifier.h&gt;
#include &lt;llvm/Assembly/PrintModulePass.h&gt;
#include &lt;llvm/Support/LLVMBuilder.h&gt;
#include &lt;llvm/ExecutionEngine/ExecutionEngine.h&gt;
#include &quot;llvm/ExecutionEngine/GenericValue.h&quot;
#include &lt;iostream&gt;

using namespace llvm;


static Function *
make_mul_add(Module *mod)
{
   Constant *c = mod-&gt;getOrInsertFunction(&quot;mul_add&quot;,
                                          Type::FloatTy, // ret type
                                          Type::FloatTy, // args
                                          Type::FloatTy,
                                          Type::FloatTy,
                                          NULL /* terminator*/);

   Function *func = cast&lt;Function&gt;(c);

   func-&gt;setCallingConv(CallingConv::C);

   Function::arg_iterator args = func-&gt;arg_begin();
   Value* x = args++;
   x-&gt;setName(&quot;x&quot;);
   Value* y = args++;
   y-&gt;setName(&quot;y&quot;);
   Value* z = args++;
   z-&gt;setName(&quot;z&quot;);

   BasicBlock* block = new BasicBlock::BasicBlock(&quot;block&quot;, func);

   LLVMBuilder builder(block);

   /* tmp = x * y */
   Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y, &quot;tmp&quot;);
   /* tmp2 = tmp + z */
   Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z, &quot;tmp2&quot;);

   builder.CreateRet(tmp2);

   return func;
}

typedef float (*my_mul_add)(float, float, float);
static float
eval_function(ExecutionEngine *eng, Function *func,
              float a, float b, float c)
{
#if 0
  std::vector&lt;GenericValue&gt; Args(3);
  Args[0].FloatVal = a;
  Args[1].FloatVal = b;
  Args[2].FloatVal = c;
  GenericValue GV = eng-&gt;runFunction(func, Args);
#else
  void *gen_func = eng-&gt;getPointerToFunction(func);
  fprintf(stderr, &quot;gen_func is %p\n&quot;, gen_func);
  my_mul_add myma = reinterpret_cast&lt;my_mul_add&gt;(gen_func);
  return myma(a, b, c);
#endif
}


int
main(int argc, char**argv)
{
  Module *Mod = new Module(&quot;test&quot;);
  Function *mul_add = make_mul_add(Mod);
  verifyModule(*Mod, PrintMessageAction);

  {
     PassManager PM;
     PM.add(new PrintModulePass(&amp;llvm::cout));
     PM.run(*Mod);
  }

  // Create the JIT.
  ExistingModuleProvider* MP = new ExistingModuleProvider(Mod);
  ExecutionEngine *ExecEng = ExecutionEngine::create(MP, false);

  // run the function
  float result = eval_function(ExecEng, Mod-&gt;getFunction(&quot;mul_add&quot;), 3.f, 5.f, 4.f);
  std::cout &lt;&lt; &quot;Result is &quot; &lt;&lt; result &lt;&lt; &quot;\n&quot;;

  return 0;
}
</string>
    </contents>
    <universal-time>
      <integer>3419515906</integer>
    </universal-time>
    <channel>
      <string>None</string>
    </channel>
    <colorization-mode>
      <string></string>
    </colorization-mode>
    <maybe-spam>
      <null/>
    </maybe-spam>
    <is-unicode>
      <keyword>TRUE</keyword>
    </is-unicode>
  </paste>
  <annotation>
    <number>
      <integer>1</integer>
    </number>
    <user>
      <string>sabre</string>
    </user>
    <title>
      <string>2.3 version</string>
    </title>
    <contents>
      <string>#include &lt;assert.h&gt;
#include &lt;llvm/Module.h&gt;
#include &lt;llvm/ModuleProvider.h&gt;
#include &lt;llvm/Function.h&gt;
#include &lt;llvm/PassManager.h&gt;
#include &lt;llvm/CallingConv.h&gt;
#include &lt;llvm/Analysis/Verifier.h&gt;
#include &lt;llvm/Assembly/PrintModulePass.h&gt;
#include &lt;llvm/Support/IRBuilder.h&gt;
#include &lt;llvm/ExecutionEngine/ExecutionEngine.h&gt;
#include &quot;llvm/ExecutionEngine/GenericValue.h&quot;
#include &lt;iostream&gt;

using namespace llvm;


static Function *
make_mul_add(Module *mod)
{
   Constant *c = mod-&gt;getOrInsertFunction(&quot;mul_add&quot;,
                                          Type::FloatTy, // ret type
                                          Type::FloatTy, // args
                                          Type::FloatTy,
                                          Type::FloatTy,
                                          NULL /* terminator*/);

   Function *func = cast&lt;Function&gt;(c);

   func-&gt;setCallingConv(CallingConv::C);

   Function::arg_iterator args = func-&gt;arg_begin();
   Value* x = args++;
   x-&gt;setName(&quot;x&quot;);
   Value* y = args++;
   y-&gt;setName(&quot;y&quot;);
   Value* z = args++;
   z-&gt;setName(&quot;z&quot;);

   BasicBlock* block = BasicBlock::Create(&quot;block&quot;, func);

   IRBuilder builder(block);

   /* tmp = x * y */
   Value* tmp = builder.CreateBinOp(Instruction::Mul, x, y, &quot;tmp&quot;);
   /* tmp2 = tmp + z */
   Value* tmp2 = builder.CreateBinOp(Instruction::Add, tmp, z, &quot;tmp2&quot;);

   builder.CreateRet(tmp2);

   return func;
}

typedef float (*my_mul_add)(float, float, float);
static float
eval_function(ExecutionEngine *eng, Function *func,
              float a, float b, float c)
{
#if 0
  std::vector&lt;GenericValue&gt; Args(3);
  Args[0].FloatVal = a;
  Args[1].FloatVal = b;
  Args[2].FloatVal = c;
  GenericValue GV = eng-&gt;runFunction(func, Args);
#else
  void *gen_func = eng-&gt;getPointerToFunction(func);
  fprintf(stderr, &quot;gen_func is %p\n&quot;, gen_func);
  my_mul_add myma = reinterpret_cast&lt;my_mul_add&gt;(gen_func);
  return myma(a, b, c);
#endif
}


int
main(int argc, char**argv)
{
  Module *Mod = new Module(&quot;test&quot;);
  Function *mul_add = make_mul_add(Mod);
  verifyModule(*Mod, PrintMessageAction);

  {
     PassManager PM;
     PM.add(new PrintModulePass(&amp;llvm::cout));
     PM.run(*Mod);
  }

  // Create the JIT.
  ExistingModuleProvider* MP = new ExistingModuleProvider(Mod);
  ExecutionEngine *ExecEng = ExecutionEngine::create(MP, false);

  // run the function
  float result = eval_function(ExecEng, Mod-&gt;getFunction(&quot;mul_add&quot;), 3.f, 5.f, 4.f);
  std::cout &lt;&lt; &quot;Result is &quot; &lt;&lt; result &lt;&lt; &quot;\n&quot;;

  return 0;
}

</string>
    </contents>
    <universal-time>
      <integer>3419516232</integer>
    </universal-time>
    <channel>
      <string>None</string>
    </channel>
    <colorization-mode>
      <string></string>
    </colorization-mode>
    <maybe-spam>
      <null/>
    </maybe-spam>
    <is-unicode>
      <keyword>TRUE</keyword>
    </is-unicode>
  </annotation>
</paste-with-annotations>