EngineBuilder: support for custom TargetOptions. Fixes the
[oota-llvm.git] / examples / ExceptionDemo / ExceptionDemo.cpp
index f2203a72b62faed28fb5ea9857b8b9b7a80c3005..41f41e8ffe718dc7a307c2dbbe954fc8429d6b41 100644 (file)
@@ -40,7 +40,8 @@
 //
 //     Cases -1 and 7 are caught by a C++ test harness where the validity of
 //         of a C++ catch(...) clause catching a generated exception with a 
-//         type info type of 7 is questionable.
+//         type info type of 7 is explained by: example in rules 1.6.4 in 
+//         http://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22)
 //
 // This code uses code from the llvm compiler-rt project and the llvm 
 // Kaleidoscope project.
 #include "llvm/Intrinsics.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Target/TargetData.h"
-#include "llvm/Target/TargetSelect.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Support/IRBuilder.h"
 #include "llvm/Support/Dwarf.h"
+#include "llvm/Support/TargetSelect.h"
+
+#ifdef OLD_EXC_SYSTEM
+// See use of UpgradeExceptionHandling(...) below                        
+#include "llvm/AutoUpgrade.h"
+#endif
+
+// FIXME: Although all systems tested with (Linux, OS X), do not need this 
+//        header file included. A user on ubuntu reported, undefined symbols 
+//        for stderr, and fprintf, and the addition of this include fixed the
+//        issue for them. Given that LLVM's best practices include the goal 
+//        of reducing the number of redundant header files included, the 
+//        correct solution would be to find out why these symbols are not 
+//        defined for the system in question, and fix the issue by finding out
+//        which LLVM header file, if any, would include these symbols.
+#include <cstdio>
 
 #include <sstream>
 #include <stdexcept>
@@ -71,7 +87,7 @@
 #endif
 
 // System C++ ABI unwind types from: 
-//     http://refspecs.freestandards.org/abi-eh-1.21.html
+//     http://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22)
 
 extern "C" {
   
@@ -113,9 +129,9 @@ extern "C" {
   } __attribute__((__aligned__));
   
   struct _Unwind_Context;
-  typedef struct _Unwind_Context_Unwind_Context_t;
+  typedef struct _Unwind_Context *_Unwind_Context_t;
   
-  extern const uint8_t_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
+  extern const uint8_t *_Unwind_GetLanguageSpecificData (_Unwind_Context_t c);
   extern uintptr_t _Unwind_GetGR (_Unwind_Context_t c, int i);
   extern void _Unwind_SetGR (_Unwind_Context_t c, int i, uintptr_t n);
   extern void _Unwind_SetIP (_Unwind_Context_t, uintptr_t new_value);
@@ -171,16 +187,19 @@ static uint64_t ourBaseExceptionClass = 0;
 static std::vector<std::string> ourTypeInfoNames;
 static std::map<int, std::string> ourTypeInfoNamesIndex;
 
-static llvm::StructType* ourTypeInfoType;
-static llvm::StructType* ourExceptionType;
-static llvm::StructType* ourUnwindExceptionType;
+static llvm::StructType *ourTypeInfoType;
+#ifndef OLD_EXC_SYSTEM
+static llvm::StructType *ourCaughtResultType;
+#endif
+static llvm::StructType *ourExceptionType;
+static llvm::StructType *ourUnwindExceptionType;
 
-static llvm::ConstantIntourExceptionNotThrownState;
-static llvm::ConstantIntourExceptionThrownState;
-static llvm::ConstantIntourExceptionCaughtState;
+static llvm::ConstantInt *ourExceptionNotThrownState;
+static llvm::ConstantInt *ourExceptionThrownState;
+static llvm::ConstantInt *ourExceptionCaughtState;
 
 typedef std::vector<std::string> ArgNames;
-typedef std::vector<const llvm::Type*> ArgTypes;
+typedef std::vector<llvm::Type*> ArgTypes;
 
 //
 // Code Generation Utilities
@@ -198,11 +217,11 @@ typedef std::vector<const llvm::Type*> ArgTypes;
 /// @param declarationOnly for function declarations
 /// @param isVarArg function uses vararg arguments
 /// @returns function instance
-llvm::Function *createFunction(llvm::Modulemodule,
-                               const llvm::Type* retType,
-                               const ArgTypestheArgTypes,
-                               const ArgNamestheArgNames,
-                               const std::stringfunctName,
+llvm::Function *createFunction(llvm::Module &module,
+                               llvm::Type *retType,
+                               const ArgTypes &theArgTypes,
+                               const ArgNames &theArgNames,
+                               const std::string &functName,
                                llvm::GlobalValue::LinkageTypes linkage,
                                bool declarationOnly,
                                bool isVarArg) {
@@ -234,13 +253,13 @@ llvm::Function *createFunction(llvm::Module& module,
 /// @param type stack variable type
 /// @param initWith optional constant initialization value
 /// @returns AllocaInst instance
-static llvm::AllocaInst *createEntryBlockAlloca(llvm::Functionfunction,
+static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function,
                                                 const std::string &varName,
-                                                const llvm::Type* type,
-                                                llvm::ConstantinitWith = 0) {
-  llvm::BasicBlockblock = function.getEntryBlock(); 
+                                                llvm::Type *type,
+                                                llvm::Constant *initWith = 0) {
+  llvm::BasicBlock &block = function.getEntryBlock(); 
   llvm::IRBuilder<> tmp(&block, block.begin());
-  llvm::AllocaInstret = tmp.CreateAlloca(type, 0, varName.c_str());
+  llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str());
   
   if (initWith) 
     tmp.CreateStore(initWith, ret);
@@ -265,7 +284,7 @@ extern "C" {
 /// Prints a 32 bit number, according to the format, to stderr.
 /// @param intToPrint integer to print 
 /// @param format printf like format to use when printing
-void print32Int(int intToPrint, const charformat) {
+void print32Int(int intToPrint, const char *format) {
   if (format) {
     // Note: No NULL check
     fprintf(stderr, format, intToPrint);
@@ -282,7 +301,7 @@ void print32Int(int intToPrint, const char* format) {
 /// Prints a 64 bit number, according to the format, to stderr.
 /// @param intToPrint integer to print 
 /// @param format printf like format to use when printing
-void print64Int(long int intToPrint, const charformat) {
+void print64Int(long int intToPrint, const char *format) {
   if (format) {
     // Note: No NULL check
     fprintf(stderr, format, intToPrint);
@@ -296,7 +315,7 @@ void print64Int(long int intToPrint, const char* format) {
 
 /// Prints a C string to stderr
 /// @param toPrint string to print
-void printStr(chartoPrint) {
+void printStr(char *toPrint) {
   if (toPrint) {
     fprintf(stderr, "%s", toPrint);
   }
@@ -310,7 +329,7 @@ void printStr(char* toPrint) {
 /// is calculated from the supplied OurBaseException_t::unwindException
 /// member address. Handles (ignores), NULL pointers.
 /// @param expToDelete exception to delete
-void deleteOurException(OurUnwindExceptionexpToDelete) {
+void deleteOurException(OurUnwindException *expToDelete) {
 #ifdef DEBUG
   fprintf(stderr,
           "deleteOurException(...).\n");
@@ -331,7 +350,7 @@ void deleteOurException(OurUnwindException* expToDelete) {
 /// @unlink
 /// @param expToDelete exception instance to delete
 void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
-                                  OurUnwindExceptionexpToDelete) {
+                                  OurUnwindException *expToDelete) {
 #ifdef DEBUG
   fprintf(stderr,
           "deleteFromUnwindOurException(...).\n");
@@ -344,9 +363,9 @@ void deleteFromUnwindOurException(_Unwind_Reason_Code reason,
 /// Creates (allocates on the heap), an exception (OurException instance),
 /// of the supplied type info type.
 /// @param type type info type
-OurUnwindExceptioncreateOurException(int type) {
+OurUnwindException *createOurException(int type) {
   size_t size = sizeof(OurException);
-  OurExceptionret = (OurException*) memset(malloc(size), 0, size);
+  OurException *ret = (OurException*) memset(malloc(size), 0, size);
   (ret->type).type = type;
   (ret->unwindException).exception_class = ourBaseExceptionClass;
   (ret->unwindException).exception_cleanup = deleteFromUnwindOurException;
@@ -360,11 +379,11 @@ OurUnwindException* createOurException(int type) {
 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
 /// @param data reference variable holding memory pointer to decode from
 /// @returns decoded value
-static uintptr_t readULEB128(const uint8_t** data) {
+static uintptr_t readULEB128(const uint8_t **data) {
   uintptr_t result = 0;
   uintptr_t shift = 0;
   unsigned char byte;
-  const uint8_tp = *data;
+  const uint8_t *p = *data;
   
   do {
     byte = *p++;
@@ -384,11 +403,11 @@ static uintptr_t readULEB128(const uint8_t** data) {
 /// @link http://dwarfstd.org/Dwarf3.pdf @unlink
 /// @param data reference variable holding memory pointer to decode from
 /// @returns decoded value
-static uintptr_t readSLEB128(const uint8_t** data) {
+static uintptr_t readSLEB128(const uint8_t **data) {
   uintptr_t result = 0;
   uintptr_t shift = 0;
   unsigned char byte;
-  const uint8_tp = *data;
+  const uint8_t *p = *data;
   
   do {
     byte = *p++;
@@ -413,9 +432,9 @@ static uintptr_t readSLEB128(const uint8_t** data) {
 /// @param data reference variable holding memory pointer to decode from
 /// @param encoding dwarf encoding type
 /// @returns decoded value
-static uintptr_t readEncodedPointer(const uint8_t** data, uint8_t encoding) {
+static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) {
   uintptr_t result = 0;
-  const uint8_tp = *data;
+  const uint8_t *p = *data;
   
   if (encoding == llvm::dwarf::DW_EH_PE_omit) 
     return(result);
@@ -522,7 +541,7 @@ static bool handleActionValue(int64_t *resultAction,
       (exceptionClass != ourBaseExceptionClass))
     return(ret);
   
-  struct OurBaseException_texcp = (struct OurBaseException_t*)
+  struct OurBaseException_t *excp = (struct OurBaseException_t*)
   (((char*) exceptionObject) + ourBaseFromUnwindOffset);
   struct OurExceptionType_t *excpType = &(excp->type);
   int type = excpType->type;
@@ -598,10 +617,10 @@ static bool handleActionValue(int64_t *resultAction,
 /// @param context unwind system context
 /// @returns minimally supported unwinding control indicator 
 static _Unwind_Reason_Code handleLsda(int version, 
-                                      const uint8_tlsda,
+                                      const uint8_t *lsda,
                                       _Unwind_Action actions,
                                       uint64_t exceptionClass, 
-                                    struct _Unwind_ExceptionexceptionObject,
+                                    struct _Unwind_Exception *exceptionObject,
                                       _Unwind_Context_t context) {
   _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND;
   
@@ -621,7 +640,7 @@ static _Unwind_Reason_Code handleLsda(int version,
   // emitted dwarf code)
   uintptr_t funcStart = _Unwind_GetRegionStart(context);
   uintptr_t pcOffset = pc - funcStart;
-  struct OurExceptionType_t** classInfo = NULL;
+  struct OurExceptionType_t **classInfo = NULL;
   
   // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding
   //       dwarf emission
@@ -649,11 +668,11 @@ static _Unwind_Reason_Code handleLsda(int version,
   
   uint8_t         callSiteEncoding = *lsda++;
   uint32_t        callSiteTableLength = readULEB128(&lsda);
-  const uint8_t*  callSiteTableStart = lsda;
-  const uint8_t*  callSiteTableEnd = callSiteTableStart + 
+  const uint8_t   *callSiteTableStart = lsda;
+  const uint8_t   *callSiteTableEnd = callSiteTableStart + 
   callSiteTableLength;
-  const uint8_t*  actionTableStart = callSiteTableEnd;
-  const uint8_t*  callSitePtr = callSiteTableStart;
+  const uint8_t   *actionTableStart = callSiteTableEnd;
+  const uint8_t   *callSitePtr = callSiteTableStart;
   
   bool foreignException = false;
   
@@ -704,14 +723,11 @@ static _Unwind_Reason_Code handleLsda(int version,
       int64_t actionValue = 0;
       
       if (actionEntry) {
-        exceptionMatched = handleActionValue
-        (
-         &actionValue,
-         classInfo, 
-         actionEntry, 
-         exceptionClass, 
-         exceptionObject
-         );
+        exceptionMatched = handleActionValue(&actionValue,
+                                             classInfo, 
+                                             actionEntry, 
+                                             exceptionClass, 
+                                             exceptionObject);
       }
       
       if (!(actions & _UA_SEARCH_PHASE)) {
@@ -789,7 +805,7 @@ static _Unwind_Reason_Code handleLsda(int version,
 _Unwind_Reason_Code ourPersonality(int version, 
                                    _Unwind_Action actions,
                                    uint64_t exceptionClass, 
-                                   struct _Unwind_ExceptionexceptionObject,
+                                   struct _Unwind_Exception *exceptionObject,
                                    _Unwind_Context_t context) {
 #ifdef DEBUG
   fprintf(stderr, 
@@ -804,7 +820,7 @@ _Unwind_Reason_Code ourPersonality(int version,
   }
 #endif
   
-  const uint8_tlsda = _Unwind_GetLanguageSpecificData(context);
+  const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context);
   
 #ifdef DEBUG
   fprintf(stderr, 
@@ -860,15 +876,15 @@ uint64_t genClass(const unsigned char classChars[], size_t classCharsSize)
 ///        generated, and is used to hold the constant string. A value of 
 ///        false indicates that the constant string will be stored on the 
 ///        stack.
-void generateStringPrint(llvm::LLVMContextcontext, 
-                         llvm::Modulemodule,
-                         llvm::IRBuilder<>builder, 
+void generateStringPrint(llvm::LLVMContext &context, 
+                         llvm::Module &module,
+                         llvm::IRBuilder<> &builder, 
                          std::string toPrint,
                          bool useGlobal = true) {
   llvm::Function *printFunct = module.getFunction("printStr");
   
   llvm::Value *stringVar;
-  llvm::ConstantstringConstant = 
+  llvm::Constant *stringConstant = 
   llvm::ConstantArray::get(context, toPrint);
   
   if (useGlobal) {
@@ -886,9 +902,8 @@ void generateStringPrint(llvm::LLVMContext& context,
     builder.CreateStore(stringConstant, stringVar);
   }
   
-  llvm::Value* cast = 
-  builder.CreatePointerCast(stringVar, 
-                            builder.getInt8Ty()->getPointerTo());
+  llvm::Value *cast = builder.CreatePointerCast(stringVar, 
+                                                builder.getInt8PtrTy());
   builder.CreateCall(printFunct, cast);
 }
 
@@ -905,11 +920,11 @@ void generateStringPrint(llvm::LLVMContext& context,
 ///        generated, and is used to hold the constant string. A value of 
 ///        false indicates that the constant string will be stored on the 
 ///        stack.
-void generateIntegerPrint(llvm::LLVMContextcontext, 
-                          llvm::Modulemodule,
-                          llvm::IRBuilder<>builder, 
-                          llvm::FunctionprintFunct,
-                          llvm::ValuetoPrint,
+void generateIntegerPrint(llvm::LLVMContext &context, 
+                          llvm::Module &module,
+                          llvm::IRBuilder<> &builder, 
+                          llvm::Function &printFunct,
+                          llvm::Value &toPrint,
                           std::string format, 
                           bool useGlobal = true) {
   llvm::Constant *stringConstant = llvm::ConstantArray::get(context, format);
@@ -930,9 +945,8 @@ void generateIntegerPrint(llvm::LLVMContext& context,
     builder.CreateStore(stringConstant, stringVar);
   }
   
-  llvm::Value* cast = 
-  builder.CreateBitCast(stringVar, 
-                        builder.getInt8Ty()->getPointerTo());
+  llvm::Value *cast = builder.CreateBitCast(stringVar, 
+                                            builder.getInt8PtrTy());
   builder.CreateCall2(&printFunct, &toPrint, cast);
 }
 
@@ -955,38 +969,55 @@ void generateIntegerPrint(llvm::LLVMContext& context,
 /// @param unwindResumeBlock unwind resume block
 /// @param exceptionCaughtFlag reference exception caught/thrown status storage
 /// @param exceptionStorage reference to exception pointer storage
+#ifndef OLD_EXC_SYSTEM
+/// @param caughtResultStorage reference to landingpad result storage
+#endif
 /// @returns newly created block
-static llvm::BasicBlock* createFinallyBlock(llvm::LLVMContext& context, 
-                                            llvm::Module& module, 
-                                            llvm::IRBuilder<>& builder, 
-                                            llvm::Function& toAddTo,
-                                            std::string& blockName,
-                                            std::string& functionId,
-                                            llvm::BasicBlock& terminatorBlock,
-                                            llvm::BasicBlock& unwindResumeBlock,
-                                            llvm::Value** exceptionCaughtFlag,
-                                            llvm::Value** exceptionStorage) {
+static llvm::BasicBlock *createFinallyBlock(llvm::LLVMContext &context, 
+                                            llvm::Module &module, 
+                                            llvm::IRBuilder<> &builder, 
+                                            llvm::Function &toAddTo,
+                                            std::string &blockName,
+                                            std::string &functionId,
+                                            llvm::BasicBlock &terminatorBlock,
+                                            llvm::BasicBlock &unwindResumeBlock,
+                                            llvm::Value **exceptionCaughtFlag,
+                                            llvm::Value **exceptionStorage
+#ifndef OLD_EXC_SYSTEM
+                                            ,llvm::Value **caughtResultStorage
+#endif
+                                            ) {
   assert(exceptionCaughtFlag && 
          "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag "
          "is NULL");
   assert(exceptionStorage && 
          "ExceptionDemo::createFinallyBlock(...):exceptionStorage "
          "is NULL");
+
+#ifndef OLD_EXC_SYSTEM
+  assert(caughtResultStorage && 
+         "ExceptionDemo::createFinallyBlock(...):caughtResultStorage "
+         "is NULL");
+#endif
   
-  *exceptionCaughtFlag = 
-  createEntryBlockAlloca(toAddTo,
-                         "exceptionCaught",
-                         ourExceptionNotThrownState->getType(),
-                         ourExceptionNotThrownState);
-  
-  const llvm::PointerType* exceptionStorageType = 
-  builder.getInt8Ty()->getPointerTo();
-  *exceptionStorage = 
-  createEntryBlockAlloca(toAddTo,
-                         "exceptionStorage",
-                         exceptionStorageType,
-                         llvm::ConstantPointerNull::get(
-                                                        exceptionStorageType));
+  *exceptionCaughtFlag = createEntryBlockAlloca(toAddTo,
+                                         "exceptionCaught",
+                                         ourExceptionNotThrownState->getType(),
+                                         ourExceptionNotThrownState);
+  
+  llvm::PointerType *exceptionStorageType = builder.getInt8PtrTy();
+  *exceptionStorage = createEntryBlockAlloca(toAddTo,
+                                             "exceptionStorage",
+                                             exceptionStorageType,
+                                             llvm::ConstantPointerNull::get(
+                                               exceptionStorageType));
+#ifndef OLD_EXC_SYSTEM
+  *caughtResultStorage = createEntryBlockAlloca(toAddTo,
+                                              "caughtResultStorage",
+                                              ourCaughtResultType,
+                                              llvm::ConstantAggregateZero::get(
+                                                ourCaughtResultType));
+#endif
   
   llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
                                                    blockName,
@@ -1003,10 +1034,10 @@ static llvm::BasicBlock* createFinallyBlock(llvm::LLVMContext& context,
                       bufferToPrint.str(),
                       USE_GLOBAL_STR_CONSTS);
   
-  llvm::SwitchInst* theSwitch = 
-  builder.CreateSwitch(builder.CreateLoad(*exceptionCaughtFlag), 
-                       &terminatorBlock,
-                       2);
+  llvm::SwitchInst *theSwitch = builder.CreateSwitch(builder.CreateLoad(
+                                                       *exceptionCaughtFlag), 
+                                                     &terminatorBlock,
+                                                     2);
   theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock);
   theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock);
   
@@ -1026,14 +1057,14 @@ static llvm::BasicBlock* createFinallyBlock(llvm::LLVMContext& context,
 /// @param terminatorBlock terminator "end" block
 /// @param exceptionCaughtFlag exception caught/thrown status
 /// @returns newly created block
-static llvm::BasicBlock* createCatchBlock(llvm::LLVMContext& context, 
-                                          llvm::Modulemodule, 
-                                          llvm::IRBuilder<>builder, 
-                                          llvm::FunctiontoAddTo,
-                                          std::stringblockName,
-                                          std::stringfunctionId,
-                                          llvm::BasicBlockterminatorBlock,
-                                          llvm::ValueexceptionCaughtFlag) {
+static llvm::BasicBlock *createCatchBlock(llvm::LLVMContext &context, 
+                                          llvm::Module &module, 
+                                          llvm::IRBuilder<> &builder, 
+                                          llvm::Function &toAddTo,
+                                          std::string &blockName,
+                                          std::string &functionId,
+                                          llvm::BasicBlock &terminatorBlock,
+                                          llvm::Value &exceptionCaughtFlag) {
   
   llvm::BasicBlock *ret = llvm::BasicBlock::Create(context,
                                                    blockName,
@@ -1079,15 +1110,15 @@ static llvm::BasicBlock* createCatchBlock(llvm::LLVMContext& context,
 /// @param exceptionTypesToCatch array of type info types to "catch"
 /// @returns generated function
 static
-llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module, 
-                                                 llvm::IRBuilder<>& builder, 
-                                                 llvm::FunctionPassManager& fpm,
-                                                 llvm::Function& toInvoke,
-                                                 std::string ourId,
-                                                 unsigned numExceptionsToCatch,
-                                                 unsigned exceptionTypesToCatch[]) {
-  
-  llvm::LLVMContextcontext = module.getContext();
+llvm::Function *createCatchWrappedInvokeFunction(llvm::Module &module, 
+                                             llvm::IRBuilder<> &builder, 
+                                             llvm::FunctionPassManager &fpm,
+                                             llvm::Function &toInvoke,
+                                             std::string ourId,
+                                             unsigned numExceptionsToCatch,
+                                             unsigned exceptionTypesToCatch[]) {
+  
+  llvm::LLVMContext &context = module.getContext();
   llvm::Function *toPrint32Int = module.getFunction("print32Int");
   
   ArgTypes argTypes;
@@ -1096,7 +1127,7 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   ArgNames argNames;
   argNames.push_back("exceptTypeToThrow");
   
-  llvm::Functionret = createFunction(module, 
+  llvm::Function *ret = createFunction(module, 
                                        builder.getVoidTy(),
                                        argTypes, 
                                        argNames, 
@@ -1114,33 +1145,39 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
                                                            "normal", 
                                                            ret);
   // Unwind block for invoke
-  llvm::BasicBlock *exceptionBlock = 
-  llvm::BasicBlock::Create(context, "exception", ret);
+  llvm::BasicBlock *exceptionBlock = llvm::BasicBlock::Create(context, 
+                                                              "exception", 
+                                                              ret);
   
   // Block which routes exception to correct catch handler block
-  llvm::BasicBlock *exceptionRouteBlock = 
-  llvm::BasicBlock::Create(context, "exceptionRoute", ret);
+  llvm::BasicBlock *exceptionRouteBlock = llvm::BasicBlock::Create(context, 
+                                                             "exceptionRoute", 
+                                                             ret);
   
   // Foreign exception handler
-  llvm::BasicBlock *externalExceptionBlock = 
-  llvm::BasicBlock::Create(context, "externalException", ret);
+  llvm::BasicBlock *externalExceptionBlock = llvm::BasicBlock::Create(context, 
+                                                          "externalException", 
+                                                          ret);
   
   // Block which calls _Unwind_Resume
-  llvm::BasicBlock *unwindResumeBlock = 
-  llvm::BasicBlock::Create(context, "unwindResume", ret);
+  llvm::BasicBlock *unwindResumeBlock = llvm::BasicBlock::Create(context, 
+                                                               "unwindResume", 
+                                                               ret);
   
   // Clean up block which delete exception if needed
-  llvm::BasicBlock *endBlock = 
-  llvm::BasicBlock::Create(context, "end", ret);
+  llvm::BasicBlock *endBlock = llvm::BasicBlock::Create(context, "end", ret);
   
   std::string nextName;
   std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch);
-  llvm::Value* exceptionCaughtFlag = NULL;
-  llvm::Value* exceptionStorage = NULL;
+  llvm::Value *exceptionCaughtFlag = NULL;
+  llvm::Value *exceptionStorage = NULL;
+#ifndef OLD_EXC_SYSTEM
+  llvm::Value *caughtResultStorage = NULL;
+#endif
   
   // Finally block which will branch to unwindResumeBlock if 
   // exception is not caught. Initializes/allocates stack locations.
-  llvm::BasicBlockfinallyBlock = createFinallyBlock(context, 
+  llvm::BasicBlock *finallyBlock = createFinallyBlock(context, 
                                                       module, 
                                                       builder, 
                                                       *ret, 
@@ -1149,7 +1186,11 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
                                                       *endBlock,
                                                       *unwindResumeBlock,
                                                       &exceptionCaughtFlag,
-                                                      &exceptionStorage);
+                                                      &exceptionStorage
+#ifndef OLD_EXC_SYSTEM
+                                                      ,&caughtResultStorage
+#endif
+                                                      );
   
   for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
     nextName = ourTypeInfoNames[exceptionTypesToCatch[i]];
@@ -1174,8 +1215,7 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   builder.CreateInvoke(&toInvoke, 
                        normalBlock, 
                        exceptionBlock, 
-                       args.begin(), 
-                       args.end());
+                       args);
   
   // End Block
   
@@ -1186,8 +1226,7 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
                       builder, 
                       "Gen: In end block: exiting in " + ourId + ".\n",
                       USE_GLOBAL_STR_CONSTS);
-  llvm::Function *deleteOurException = 
-  module.getFunction("deleteOurException");
+  llvm::Function *deleteOurException = module.getFunction("deleteOurException");
   
   // Note: function handles NULL exceptions
   builder.CreateCall(deleteOurException, 
@@ -1211,28 +1250,57 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   
   builder.SetInsertPoint(unwindResumeBlock);
   
-  llvm::Function *resumeOurException = 
-  module.getFunction("_Unwind_Resume");
+  
+#ifndef OLD_EXC_SYSTEM
+  builder.CreateResume(builder.CreateLoad(caughtResultStorage));
+#else
+  llvm::Function *resumeOurException = module.getFunction("_Unwind_Resume");
   builder.CreateCall(resumeOurException, 
                      builder.CreateLoad(exceptionStorage));
   builder.CreateUnreachable();
+#endif
   
   // Exception Block
   
   builder.SetInsertPoint(exceptionBlock);
   
-  llvm::Function *ehException = module.getFunction("llvm.eh.exception");
+  llvm::Function *personality = module.getFunction("ourPersonality");
   
+#ifndef OLD_EXC_SYSTEM
+  llvm::LandingPadInst *caughtResult = 
+    builder.CreateLandingPad(ourCaughtResultType,
+                             personality,
+                             numExceptionsToCatch,
+                             "landingPad");
+
+  caughtResult->setCleanup(true);
+
+  for (unsigned i = 0; i < numExceptionsToCatch; ++i) {
+    // Set up type infos to be caught
+    caughtResult->addClause(module.getGlobalVariable(
+                             ourTypeInfoNames[exceptionTypesToCatch[i]]));
+  }
+
+  llvm::Value *unwindException = builder.CreateExtractValue(caughtResult, 0);
+  llvm::Value *retTypeInfoIndex = builder.CreateExtractValue(caughtResult, 1);
+
+  // FIXME: Redundant storage which, beyond utilizing value of 
+  //        caughtResultStore for unwindException storage, may be alleviated 
+  //        alltogether with a block rearrangement
+  builder.CreateStore(caughtResult, caughtResultStorage);
+  builder.CreateStore(unwindException, exceptionStorage);
+  builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
+#else
+  llvm::Function *ehException = module.getFunction("llvm.eh.exception");
+
   // Retrieve thrown exception
-  llvm::ValueunwindException = builder.CreateCall(ehException);
+  llvm::Value *unwindException = builder.CreateCall(ehException);
   
   // Store exception and flag
   builder.CreateStore(unwindException, exceptionStorage);
   builder.CreateStore(ourExceptionThrownState, exceptionCaughtFlag);
-  llvm::Function *personality = module.getFunction("ourPersonality");
-  llvm::Value* functPtr = 
-  builder.CreatePointerCast(personality, 
-                            builder.getInt8Ty()->getPointerTo());
+  llvm::Value *functPtr = builder.CreatePointerCast(personality, 
+                                                    builder.getInt8PtrTy());
   
   args.clear();
   args.push_back(unwindException);
@@ -1256,14 +1324,13 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   // handles this call. This landing pad (this exception block), will be 
   // called either because it nees to cleanup (call finally) or a type 
   // info was found which matched the thrown exception.
-  llvm::Value* retTypeInfoIndex = builder.CreateCall(ehSelector, 
-                                                     args.begin(), 
-                                                     args.end());
+  llvm::Value *retTypeInfoIndex = builder.CreateCall(ehSelector, args);
+#endif
   
   // Retrieve exception_class member from thrown exception 
   // (_Unwind_Exception instance). This member tells us whether or not
   // the exception is foreign.
-  llvm::ValueunwindExceptionClass = 
+  llvm::Value *unwindExceptionClass = 
     builder.CreateLoad(builder.CreateStructGEP(
              builder.CreatePointerCast(unwindException, 
                                        ourUnwindExceptionType->getPointerTo()), 
@@ -1298,10 +1365,10 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   // (OurException instance).
   //
   // Note: ourBaseFromUnwindOffset is usually negative
-  llvm::Value* typeInfoThrown = 
-  builder.CreatePointerCast(builder.CreateConstGEP1_64(unwindException,
+  llvm::Value *typeInfoThrown = builder.CreatePointerCast(
+                                  builder.CreateConstGEP1_64(unwindException,
                                                        ourBaseFromUnwindOffset),
-                            ourExceptionType->getPointerTo());
+                                  ourExceptionType->getPointerTo());
   
   // Retrieve thrown exception type info type
   //
@@ -1309,7 +1376,7 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
   //       unlike a true getelementptr (GEP) instruction
   typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0);
   
-  llvm::ValuetypeInfoThrownType = 
+  llvm::Value *typeInfoThrownType = 
   builder.CreateStructGEP(typeInfoThrown, 0);
   
   generateIntegerPrint(context, 
@@ -1324,10 +1391,9 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
                        USE_GLOBAL_STR_CONSTS);
   
   // Route to matched type info catch block or run cleanup finally block
-  llvm::SwitchInst* switchToCatchBlock = 
-  builder.CreateSwitch(retTypeInfoIndex, 
-                       finallyBlock, 
-                       numExceptionsToCatch);
+  llvm::SwitchInst *switchToCatchBlock = builder.CreateSwitch(retTypeInfoIndex, 
+                                                          finallyBlock, 
+                                                          numExceptionsToCatch);
   
   unsigned nextTypeToCatch;
   
@@ -1337,6 +1403,12 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
                                    llvm::Type::getInt32Ty(context), i),
                                 catchBlocks[nextTypeToCatch]);
   }
+
+#ifdef OLD_EXC_SYSTEM
+  // Must be run before verifier                                                
+  UpgradeExceptionHandling(&module);
+#endif
+
   
   llvm::verifyFunction(*ret);
   fpm.run(*ret);
@@ -1360,13 +1432,13 @@ llvm::Function* createCatchWrappedInvokeFunction(llvm::Module& module,
 ///        if the above nativeThrowType matches generated function's arg.
 /// @returns generated function
 static
-llvm::Function* createThrowExceptionFunction(llvm::Module& module, 
-                                             llvm::IRBuilder<>builder, 
-                                             llvm::FunctionPassManagerfpm,
+llvm::Function *createThrowExceptionFunction(llvm::Module &module, 
+                                             llvm::IRBuilder<> &builder, 
+                                             llvm::FunctionPassManager &fpm,
                                              std::string ourId,
                                              int32_t nativeThrowType,
-                                             llvm::FunctionnativeThrowFunct) {
-  llvm::LLVMContextcontext = module.getContext();
+                                             llvm::Function &nativeThrowFunct) {
+  llvm::LLVMContext &context = module.getContext();
   namedValues.clear();
   ArgTypes unwindArgTypes;
   unwindArgTypes.push_back(builder.getInt32Ty());
@@ -1388,17 +1460,15 @@ llvm::Function* createThrowExceptionFunction(llvm::Module& module,
                                                           "entry", 
                                                           ret);
   // Throws a foreign exception
-  llvm::BasicBlock *nativeThrowBlock = 
-  llvm::BasicBlock::Create(context,
-                           "nativeThrow", 
-                           ret);
+  llvm::BasicBlock *nativeThrowBlock = llvm::BasicBlock::Create(context,
+                                                                "nativeThrow", 
+                                                                ret);
   // Throws one of our Exceptions
-  llvm::BasicBlock *generatedThrowBlock = 
-  llvm::BasicBlock::Create(context,
-                           "generatedThrow", 
-                           ret);
+  llvm::BasicBlock *generatedThrowBlock = llvm::BasicBlock::Create(context,
+                                                             "generatedThrow", 
+                                                             ret);
   // Retrieved runtime type info type to throw
-  llvm::ValueexceptionType = namedValues["exceptTypeToThrow"];
+  llvm::Value *exceptionType = namedValues["exceptTypeToThrow"];
   
   // nativeThrowBlock block
   
@@ -1426,7 +1496,7 @@ llvm::Function* createThrowExceptionFunction(llvm::Module& module,
   // Switches on runtime type info type value to determine whether or not
   // a foreign exception is thrown. Defaults to throwing one of our 
   // generated exceptions.
-  llvm::SwitchInsttheSwitch = builder.CreateSwitch(exceptionType,
+  llvm::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType,
                                                      generatedThrowBlock,
                                                      1);
   
@@ -1438,15 +1508,13 @@ llvm::Function* createThrowExceptionFunction(llvm::Module& module,
   
   builder.SetInsertPoint(generatedThrowBlock);
   
-  llvm::Function *createOurException = 
-  module.getFunction("createOurException");
-  llvm::Function *raiseOurException = 
-  module.getFunction("_Unwind_RaiseException");
+  llvm::Function *createOurException = module.getFunction("createOurException");
+  llvm::Function *raiseOurException = module.getFunction(
+                                        "_Unwind_RaiseException");
   
   // Creates exception to throw with runtime type info type.
-  llvm::Value* exception = 
-  builder.CreateCall(createOurException, 
-                     namedValues["exceptTypeToThrow"]);
+  llvm::Value *exception = builder.CreateCall(createOurException, 
+                                              namedValues["exceptTypeToThrow"]);
   
   // Throw generated Exception
   builder.CreateCall(raiseOurException, exception);
@@ -1459,8 +1527,8 @@ llvm::Function* createThrowExceptionFunction(llvm::Module& module,
 }
 
 static void createStandardUtilityFunctions(unsigned numTypeInfos,
-                                           llvm::Modulemodule, 
-                                           llvm::IRBuilder<>builder);
+                                           llvm::Module &module, 
+                                           llvm::IRBuilder<> &builder);
 
 /// Creates test code by generating and organizing these functions into the 
 /// test case. The test case consists of an outer function setup to invoke
@@ -1482,9 +1550,9 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
 /// @param nativeThrowFunctName name of external function which will throw
 ///        a foreign exception
 /// @returns outermost generated test function.
-llvm::Function* createUnwindExceptionTest(llvm::Module& module, 
-                                          llvm::IRBuilder<>builder, 
-                                          llvm::FunctionPassManagerfpm,
+llvm::Function *createUnwindExceptionTest(llvm::Module &module, 
+                                          llvm::IRBuilder<> &builder, 
+                                          llvm::FunctionPassManager &fpm,
                                           std::string nativeThrowFunctName) {
   // Number of type infos to generate
   unsigned numTypeInfos = 6;
@@ -1494,32 +1562,29 @@ llvm::Function* createUnwindExceptionTest(llvm::Module& module,
   createStandardUtilityFunctions(numTypeInfos,
                                  module,
                                  builder);
-  llvm::Function *nativeThrowFunct = 
-  module.getFunction(nativeThrowFunctName);
+  llvm::Function *nativeThrowFunct = module.getFunction(nativeThrowFunctName);
   
   // Create exception throw function using the value ~0 to cause 
   // foreign exceptions to be thrown.
-  llvm::Function* throwFunct = 
-  createThrowExceptionFunction(module,
-                               builder,
-                               fpm,
-                               "throwFunct",
-                               ~0,
-                               *nativeThrowFunct);
+  llvm::Function *throwFunct = createThrowExceptionFunction(module,
+                                                            builder,
+                                                            fpm,
+                                                            "throwFunct",
+                                                            ~0,
+                                                            *nativeThrowFunct);
   // Inner function will catch even type infos
   unsigned innerExceptionTypesToCatch[] = {6, 2, 4};
   size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) / 
-  sizeof(unsigned);
+                                    sizeof(unsigned);
   
   // Generate inner function.
-  llvm::Function* innerCatchFunct = 
-  createCatchWrappedInvokeFunction(module,
-                                   builder,
-                                   fpm,
-                                   *throwFunct,
-                                   "innerCatchFunct",
-                                   numExceptionTypesToCatch,
-                                   innerExceptionTypesToCatch);
+  llvm::Function *innerCatchFunct = createCatchWrappedInvokeFunction(module,
+                                                    builder,
+                                                    fpm,
+                                                    *throwFunct,
+                                                    "innerCatchFunct",
+                                                    numExceptionTypesToCatch,
+                                                    innerExceptionTypesToCatch);
   
   // Outer function will catch odd type infos
   unsigned outerExceptionTypesToCatch[] = {3, 1, 5};
@@ -1527,14 +1592,13 @@ llvm::Function* createUnwindExceptionTest(llvm::Module& module,
   sizeof(unsigned);
   
   // Generate outer function
-  llvm::Function* outerCatchFunct = 
-  createCatchWrappedInvokeFunction(module,
-                                   builder,
-                                   fpm,
-                                   *innerCatchFunct,
-                                   "outerCatchFunct",
-                                   numExceptionTypesToCatch,
-                                   outerExceptionTypesToCatch);
+  llvm::Function *outerCatchFunct = createCatchWrappedInvokeFunction(module,
+                                                    builder,
+                                                    fpm,
+                                                    *innerCatchFunct,
+                                                    "outerCatchFunct",
+                                                    numExceptionTypesToCatch,
+                                                    outerExceptionTypesToCatch);
   
   // Return outer function to run
   return(outerCatchFunct);
@@ -1547,10 +1611,10 @@ public:
   OurCppRunException(const std::string reason) :
   std::runtime_error(reason) {}
   
-  OurCppRunException (const OurCppRunExceptiontoCopy) :
+  OurCppRunException (const OurCppRunException &toCopy) :
   std::runtime_error(toCopy) {}
   
-  OurCppRunException& operator = (const OurCppRunException& toCopy) {
+  OurCppRunException &operator = (const OurCppRunException &toCopy) {
     return(reinterpret_cast<OurCppRunException&>(
                                  std::runtime_error::operator=(toCopy)));
   }
@@ -1570,7 +1634,7 @@ void throwCppException (int32_t ignoreIt) {
 typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
 
 /// This is a test harness which runs test by executing generated 
-/// function with a type info type to throw. Harness wraps the excecution 
+/// function with a type info type to throw. Harness wraps the execution
 /// of generated function in a C++ try catch clause.
 /// @param engine execution engine to use for executing generated function.
 ///        This demo program expects this to be a JIT instance for demo
@@ -1579,8 +1643,8 @@ typedef void (*OurExceptionThrowFunctType) (int32_t typeToThrow);
 /// @param typeToThrow type info type of generated exception to throw, or
 ///        indicator to cause foreign exception to be thrown.
 static
-void runExceptionThrow(llvm::ExecutionEngineengine, 
-                       llvm::Functionfunction, 
+void runExceptionThrow(llvm::ExecutionEngine *engine, 
+                       llvm::Function *function, 
                        int32_t typeToThrow) {
   
   // Find test's function pointer
@@ -1600,12 +1664,12 @@ void runExceptionThrow(llvm::ExecutionEngine* engine,
             exc.what());
   }
   catch (...) {
-    // Catch all exceptions including our generated ones. I'm not sure
-    // why this latter functionality should work, as it seems that
-    // our exceptions should be foreign to C++ (the _Unwind_Exception::
-    // exception_class should be different from the one used by C++), and
-    // therefore C++ should ignore the generated exceptions. 
-    
+    // Catch all exceptions including our generated ones. This latter 
+    // functionality works according to the example in rules 1.6.4 of
+    // http://sourcery.mentor.com/public/cxx-abi/abi-eh.html (v1.22), 
+    // given that these will be exceptions foreign to C++ 
+    // (the _Unwind_Exception::exception_class should be different from 
+    // the one used by C++).
     fprintf(stderr,
             "\nrunExceptionThrow(...):In C++ catch all.\n");
   }
@@ -1615,7 +1679,7 @@ void runExceptionThrow(llvm::ExecutionEngine* engine,
 // End test functions
 //
 
-typedef llvm::ArrayRef<const llvm::Type*> TypeArray;
+typedef llvm::ArrayRef<llvm::Type*> TypeArray;
 
 /// This initialization routine creates type info globals and 
 /// adds external function declarations to module.
@@ -1624,27 +1688,40 @@ typedef llvm::ArrayRef<const llvm::Type*> TypeArray;
 /// @param module code for module instance
 /// @param builder builder instance
 static void createStandardUtilityFunctions(unsigned numTypeInfos,
-                                           llvm::Modulemodule, 
-                                           llvm::IRBuilder<>builder) {
+                                           llvm::Module &module, 
+                                           llvm::IRBuilder<> &builder) {
   
-  llvm::LLVMContextcontext = module.getContext();
+  llvm::LLVMContext &context = module.getContext();
   
   // Exception initializations
   
   // Setup exception catch state
   ourExceptionNotThrownState = 
-  llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
+    llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0),
   ourExceptionThrownState = 
-  llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
+    llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1),
   ourExceptionCaughtState = 
-  llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
+    llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2),
   
   
   
   // Create our type info type
   ourTypeInfoType = llvm::StructType::get(context, 
                                           TypeArray(builder.getInt32Ty()));
-  
+
+#ifndef OLD_EXC_SYSTEM
+
+  llvm::Type *caughtResultFieldTypes[] = {
+    builder.getInt8PtrTy(),
+    builder.getInt32Ty()
+  };
+
+  // Create our landingpad result type
+  ourCaughtResultType = llvm::StructType::get(context,
+                                            TypeArray(caughtResultFieldTypes));
+
+#endif
+
   // Create OurException type
   ourExceptionType = llvm::StructType::get(context, 
                                            TypeArray(ourTypeInfoType));
@@ -1654,12 +1731,14 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   // Note: Declaring only a portion of the _Unwind_Exception struct.
   //       Does this cause problems?
   ourUnwindExceptionType =
-    llvm::StructType::get(context, TypeArray(builder.getInt64Ty()));
+    llvm::StructType::get(context, 
+                    TypeArray(builder.getInt64Ty()));
+
   struct OurBaseException_t dummyException;
   
   // Calculate offset of OurException::unwindException member.
   ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) - 
-  ((uintptr_t) &(dummyException.unwindException));
+                            ((uintptr_t) &(dummyException.unwindException));
   
 #ifdef DEBUG
   fprintf(stderr,
@@ -1683,7 +1762,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   std::vector<llvm::Constant*> structVals;
   
   llvm::Constant *nextStruct;
-  llvm::GlobalVariablenextGlobal = NULL;
+  llvm::GlobalVariable *nextGlobal = NULL;
   
   // Generate each type info
   //
@@ -1712,15 +1791,15 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   
   ArgNames argNames;
   ArgTypes argTypes;
-  llvm::Functionfunct = NULL;
+  llvm::Function *funct = NULL;
   
   // print32Int
   
-  const llvm::Type* retType = builder.getVoidTy();
+  llvm::Type *retType = builder.getVoidTy();
   
   argTypes.clear();
   argTypes.push_back(builder.getInt32Ty());
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1739,7 +1818,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   
   argTypes.clear();
   argTypes.push_back(builder.getInt64Ty());
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1757,7 +1836,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   retType = builder.getVoidTy();
   
   argTypes.clear();
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1793,7 +1872,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   retType = builder.getVoidTy();
   
   argTypes.clear();
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1808,7 +1887,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   
   // createOurException
   
-  retType = builder.getInt8Ty()->getPointerTo();
+  retType = builder.getInt8PtrTy();
   
   argTypes.clear();
   argTypes.push_back(builder.getInt32Ty());
@@ -1829,7 +1908,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   retType = builder.getInt32Ty();
   
   argTypes.clear();
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1849,7 +1928,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   retType = builder.getInt32Ty();
   
   argTypes.clear();
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1872,8 +1951,8 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
   argTypes.push_back(builder.getInt32Ty());
   argTypes.push_back(builder.getInt32Ty());
   argTypes.push_back(builder.getInt64Ty());
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
-  argTypes.push_back(builder.getInt8Ty()->getPointerTo());
+  argTypes.push_back(builder.getInt8PtrTy());
+  argTypes.push_back(builder.getInt8PtrTy());
   
   argNames.clear();
   
@@ -1910,7 +1989,7 @@ static void createStandardUtilityFunctions(unsigned numTypeInfos,
 /// <= 6 and >= 1 will be caught by test functions; and type info types > 6
 /// will result in exceptions which pass through to the test harness. All other
 /// type info types are not supported and could cause a crash.
-int main(int argc, charargv[]) {
+int main(int argc, char *argv[]) {
   if (argc == 1) {
     fprintf(stderr,
             "\nUsage: ExceptionDemo <exception type to throw> "
@@ -1926,20 +2005,22 @@ int main(int argc, char* argv[]) {
   }
   
   // If not set, exception handling will not be turned on
-  llvm::JITExceptionHandling = true;
+  llvm::TargetOptions Opts;
+  Opts.JITExceptionHandling = true;
   
   llvm::InitializeNativeTarget();
-  llvm::LLVMContextcontext = llvm::getGlobalContext();
+  llvm::LLVMContext &context = llvm::getGlobalContext();
   llvm::IRBuilder<> theBuilder(context);
   
   // Make the module, which holds all the code.
-  llvm::Modulemodule = new llvm::Module("my cool jit", context);
+  llvm::Module *module = new llvm::Module("my cool jit", context);
   
   // Build engine with JIT
   llvm::EngineBuilder factory(module);
   factory.setEngineKind(llvm::EngineKind::JIT);
   factory.setAllocateGVsWithCode(false);
-  llvm::ExecutionEngine* executionEngine = factory.create();
+  factory.setTargetOptions(Opts);
+  llvm::ExecutionEngine *executionEngine = factory.create();
   
   {
     llvm::FunctionPassManager fpm(module);
@@ -1976,7 +2057,7 @@ int main(int argc, char* argv[]) {
     
     // Generate test code using function throwCppException(...) as
     // the function which throws foreign exceptions.
-    llvm::FunctiontoRun = 
+    llvm::Function *toRun = 
     createUnwindExceptionTest(*module, 
                               theBuilder, 
                               fpm,