title: Accessing elements in LLVM arrays
body: <p>I am trying to get started with LLVM in order to add just-in-time compilation for my code, but finding it very difficult find references on how to do the things I want in LLVM, despite having checked through the Kaleidoscope tutorial, the language reference manual, the programmer's manual and the doxygen documentation. Are there more references to LLVM's C++ API than these?</p>
  
  <p>Now for the concrete question. I have allocated an array object with two elements (which I assume corresponds to <code>double[2]</code> in C++):</p>
  
  <pre><code>const llvm::Type* double_t = llvm::Type::getDoubleTy(llvm::getGlobalContext());
  const llvm::Type* array_t =  llvm::ArrayType::get(double_t,2)
  </code></pre>
  
  <p>Later in the code, I create a function, where this array is one of the arguments. Then, in my function I extract the first element in the array and return it to the user:</p>
  
  <pre><code>llvm::Function::arg_iterator AI = jit_function_-&gt;arg_begin();
  llvm::Value *x = AI;
  llvm::Value *x0 = Builder.CreateExtractValue(x,0);
  Builder.CreateRet(x0);
  </code></pre>
  
  <p>The code jits fine, but when I try to run it, it doesn't work. For example:</p>
  
  <pre><code>typedef double (*GenType)(double[2]);
  GenType FP = GenType(intptr_t(TheExecutionEngine-&gt;getPointerToFunction(jit_function_)));
  double y[2] = {10,20};
  double r = FP(y);
  printf("r = %g\n", r);
  </code></pre>
  
  <p>The return value is just nonsense and I can't see what I'm doing wrong. If I pass the values in the array (10 and 20) as scalar arguments to the function, it works fine.</p>