title: LLVM exceptions; how to unwind
body: <p>at the moment, i'm inserting variables into the beginning of block scope using CreateEntryBlockAlloca:</p>
  
  <pre><code>template &lt;typename VariableType&gt;
              static inline llvm::AllocaInst *CreateEntryBlockAlloca(BuilderParameter&amp; buildParameters,
                      const std::string &amp;VarName) {
                  HAssertMsg( 1 != 0 , "Not Implemented");
              };
  
              template &lt;&gt;
                inline llvm::AllocaInst *CreateEntryBlockAlloca&lt;double&gt;(BuilderParameter&amp; buildParameters,
                      const std::string &amp;VarName) {
                  llvm::Function* TheFunction = buildParameters.dag.llvmFunction;
                  llvm::IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),
                          TheFunction-&gt;getEntryBlock().begin());
                  return TmpB.CreateAlloca(llvm::Type::getDoubleTy(buildParameters.getLLVMContext()), 0,
                          VarName.c_str());
              }
  </code></pre>
  
  <p>Now, i want to add Allocas for non-POD types (that might require a destructor/cleanup function at exit). However, it is not enough to add destructor calls at the end of the exit scope block, since it is not clear how to have them be invoked when a regular DWARF exception is thrown (for the purpose of this argument, lets say that exceptions are thrown from Call points that invoke C++ functions which only throw a POD type, so no, in my case, ignorance is bliss, and i would like to stay away from <a href="http://llvm.org/releases/2.9/docs/ExceptionHandling.html" rel="nofollow">intrinsic llvm exceptions</a> unless i understand them better).</p>
  
  <p>I was thinking that may be i could have a table with offsets in the stack with the Alloca registers, and have the exception handler (at the bottom of the stack, at the invocation point of the JIT function) walk over those offsets on the table and call destructors appropiately.</p>
  
  <p>The thing i don't know is how to query the offset of the Alloca'ed registers created with CreateAlloca. <strong>How can i do that reliably?</strong></p>
  
  <p>Also, if you think there is a better way to achieve this, please enlighten me on the path of the llvm</p>
  
  <ul>
  <li><strong>Technical Comment</strong>: the JIT code is being called inside a <a href="https://github.com/olk/boost.context" rel="nofollow">boost::context</a> which only invokes the JIT code inside a try catch, and does nothing on the catch, it just exits from the context and returns to the main execution stack. the idea is that if i handle the unwinding in the main execution stack, any function i call (for say, cleaning up stack variables) will not overwrite those same stack contents from the terminated JIT context, so it will not be corrupted. Hope i'm making enough sense</li>
  </ul>