diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/BrainF/BrainF.cpp | 7 | ||||
-rw-r--r-- | examples/ExceptionDemo/ExceptionDemo.cpp | 2935 | ||||
-rw-r--r-- | examples/Kaleidoscope/Chapter5/toy.cpp | 4 | ||||
-rw-r--r-- | examples/Kaleidoscope/Chapter6/toy.cpp | 4 | ||||
-rw-r--r-- | examples/Kaleidoscope/Chapter7/toy.cpp | 2 |
5 files changed, 1468 insertions, 1484 deletions
diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp index 8536915..54f3553 100644 --- a/examples/BrainF/BrainF.cpp +++ b/examples/BrainF/BrainF.cpp @@ -294,8 +294,7 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, // Make part of PHI instruction now, wait until end of loop to finish PHINode *phi_0 = PHINode::Create(PointerType::getUnqual(IntegerType::getInt8Ty(C)), - headreg, testbb); - phi_0->reserveOperandSpace(2); + 2, headreg, testbb); phi_0->addIncoming(curhead, bb_0); curhead = phi_0; @@ -449,8 +448,8 @@ void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, //%head.%d = phi i8 *[%head.%d, %main.%d] PHINode *phi_1 = builder-> - CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), headreg); - phi_1->reserveOperandSpace(1); + CreatePHI(PointerType::getUnqual(IntegerType::getInt8Ty(C)), 1, + headreg); phi_1->addIncoming(head_0, testbb); curhead = phi_1; } diff --git a/examples/ExceptionDemo/ExceptionDemo.cpp b/examples/ExceptionDemo/ExceptionDemo.cpp index 95ccd24..e5bd377 100644 --- a/examples/ExceptionDemo/ExceptionDemo.cpp +++ b/examples/ExceptionDemo/ExceptionDemo.cpp @@ -1,12 +1,11 @@ -//===-- examples/ExceptionDemo/ExceptionDemo.cpp - -// An example use of the llvm Exception mechanism --===// +//===-- ExceptionDemo.cpp - An example using llvm Exceptions --------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // -//===--------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// // // Demo program which implements an example LLVM exception implementation, and // shows several test cases including the handling of foreign exceptions. @@ -46,8 +45,7 @@ // This code uses code from the llvm compiler-rt project and the llvm // Kaleidoscope project. // -//===--------------------------------------------------------------------===// - +//===----------------------------------------------------------------------===// #include "llvm/LLVMContext.h" #include "llvm/DerivedTypes.h" @@ -64,11 +62,17 @@ #include "llvm/Support/IRBuilder.h" #include "llvm/Support/Dwarf.h" +// 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 <string> + #include <sstream> -#include <map> -#include <vector> #include <stdexcept> @@ -80,8 +84,8 @@ // http://refspecs.freestandards.org/abi-eh-1.21.html extern "C" { - -typedef enum { + + typedef enum { _URC_NO_REASON = 0, _URC_FOREIGN_EXCEPTION_CAUGHT = 1, _URC_FATAL_PHASE2_ERROR = 2, @@ -91,43 +95,43 @@ typedef enum { _URC_HANDLER_FOUND = 6, _URC_INSTALL_CONTEXT = 7, _URC_CONTINUE_UNWIND = 8 -} _Unwind_Reason_Code; - -typedef enum { + } _Unwind_Reason_Code; + + typedef enum { _UA_SEARCH_PHASE = 1, _UA_CLEANUP_PHASE = 2, _UA_HANDLER_FRAME = 4, _UA_FORCE_UNWIND = 8, _UA_END_OF_STACK = 16 -} _Unwind_Action; - -struct _Unwind_Exception; - -typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code, - struct _Unwind_Exception *); - -struct _Unwind_Exception { + } _Unwind_Action; + + struct _Unwind_Exception; + + typedef void (*_Unwind_Exception_Cleanup_Fn) (_Unwind_Reason_Code, + struct _Unwind_Exception *); + + struct _Unwind_Exception { uint64_t exception_class; _Unwind_Exception_Cleanup_Fn exception_cleanup; - + uintptr_t private_1; uintptr_t private_2; - + // @@@ The IA-64 ABI says that this structure must be double-word aligned. // Taking that literally does not make much sense generically. Instead // we provide the maximum alignment required by any type for the machine. -} __attribute__((__aligned__)); - -struct _Unwind_Context; -typedef struct _Unwind_Context* _Unwind_Context_t; - -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); -extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context); -extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context); - + } __attribute__((__aligned__)); + + struct _Unwind_Context; + typedef struct _Unwind_Context *_Unwind_Context_t; + + 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); + extern uintptr_t _Unwind_GetIP (_Unwind_Context_t context); + extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context); + } // extern "C" // @@ -136,8 +140,8 @@ extern uintptr_t _Unwind_GetRegionStart (_Unwind_Context_t context); /// This is our simplistic type info struct OurExceptionType_t { - /// type info type - int type; + /// type info type + int type; }; @@ -148,10 +152,10 @@ struct OurExceptionType_t { /// on a double word boundary. This is necessary to match the standard: /// http://refspecs.freestandards.org/abi-eh-1.21.html struct OurBaseException_t { - struct OurExceptionType_t type; - - // Note: This is properly aligned in unwind.h - struct _Unwind_Exception unwindException; + struct OurExceptionType_t type; + + // Note: This is properly aligned in unwind.h + struct _Unwind_Exception unwindException; }; @@ -169,7 +173,7 @@ static std::map<std::string, llvm::Value*> namedValues; int64_t ourBaseFromUnwindOffset; const unsigned char ourBaseExcpClassChars[] = - {'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'}; +{'o', 'b', 'j', '\0', 'b', 'a', 's', '\0'}; static uint64_t ourBaseExceptionClass = 0; @@ -177,13 +181,13 @@ 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; +static llvm::StructType *ourExceptionType; +static llvm::StructType *ourUnwindExceptionType; -static llvm::ConstantInt* ourExceptionNotThrownState; -static llvm::ConstantInt* ourExceptionThrownState; -static llvm::ConstantInt* ourExceptionCaughtState; +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; @@ -204,35 +208,32 @@ 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::Module& module, - const llvm::Type* retType, - const ArgTypes& theArgTypes, - const ArgNames& theArgNames, - const std::string& functName, +llvm::Function *createFunction(llvm::Module &module, + const llvm::Type *retType, + const ArgTypes &theArgTypes, + const ArgNames &theArgNames, + const std::string &functName, llvm::GlobalValue::LinkageTypes linkage, bool declarationOnly, bool isVarArg) { - llvm::FunctionType* functType = llvm::FunctionType::get(retType, - theArgTypes, - isVarArg); - llvm::Function* ret = llvm::Function::Create(functType, - linkage, - functName, - &module); - if (!ret || declarationOnly) - return(ret); - - namedValues.clear(); - unsigned i = 0; - for (llvm::Function::arg_iterator argIndex = ret->arg_begin(); - i != theArgNames.size(); - ++argIndex, ++i) { - - argIndex->setName(theArgNames[i]); - namedValues[theArgNames[i]] = argIndex; - } - + llvm::FunctionType *functType = + llvm::FunctionType::get(retType, theArgTypes, isVarArg); + llvm::Function *ret = + llvm::Function::Create(functType, linkage, functName, &module); + if (!ret || declarationOnly) return(ret); + + namedValues.clear(); + unsigned i = 0; + for (llvm::Function::arg_iterator argIndex = ret->arg_begin(); + i != theArgNames.size(); + ++argIndex, ++i) { + + argIndex->setName(theArgNames[i]); + namedValues[theArgNames[i]] = argIndex; + } + + return(ret); } @@ -243,18 +244,18 @@ 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::Function& function, - const std::string &varName, - const llvm::Type* type, - llvm::Constant* initWith = NULL) { - llvm::BasicBlock& block = function.getEntryBlock(); - llvm::IRBuilder<> tmp(&block, block.begin()); - llvm::AllocaInst* ret = tmp.CreateAlloca(type, 0, varName.c_str()); - - if (initWith) - tmp.CreateStore(initWith, ret); - - return(ret); +static llvm::AllocaInst *createEntryBlockAlloca(llvm::Function &function, + const std::string &varName, + const llvm::Type *type, + llvm::Constant *initWith = 0) { + llvm::BasicBlock &block = function.getEntryBlock(); + llvm::IRBuilder<> tmp(&block, block.begin()); + llvm::AllocaInst *ret = tmp.CreateAlloca(type, 0, varName.c_str()); + + if (initWith) + tmp.CreateStore(initWith, ret); + + return(ret); } @@ -274,15 +275,15 @@ 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 char* format) { - if (format) { - // Note: No NULL check - fprintf(stderr, format, intToPrint); - } - else { - // Note: No NULL check - fprintf(stderr, "::print32Int(...):NULL arg.\n"); - } +void print32Int(int intToPrint, const char *format) { + if (format) { + // Note: No NULL check + fprintf(stderr, format, intToPrint); + } + else { + // Note: No NULL check + fprintf(stderr, "::print32Int(...):NULL arg.\n"); + } } @@ -291,27 +292,27 @@ 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 char* format) { - if (format) { - // Note: No NULL check - fprintf(stderr, format, intToPrint); - } - else { - // Note: No NULL check - fprintf(stderr, "::print64Int(...):NULL arg.\n"); - } +void print64Int(long int intToPrint, const char *format) { + if (format) { + // Note: No NULL check + fprintf(stderr, format, intToPrint); + } + else { + // Note: No NULL check + fprintf(stderr, "::print64Int(...):NULL arg.\n"); + } } /// Prints a C string to stderr /// @param toPrint string to print -void printStr(char* toPrint) { - if (toPrint) { - fprintf(stderr, "%s", toPrint); - } - else { - fprintf(stderr, "::printStr(...):NULL arg.\n"); - } +void printStr(char *toPrint) { + if (toPrint) { + fprintf(stderr, "%s", toPrint); + } + else { + fprintf(stderr, "::printStr(...):NULL arg.\n"); + } } @@ -319,17 +320,17 @@ 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(OurUnwindException* expToDelete) { +void deleteOurException(OurUnwindException *expToDelete) { #ifdef DEBUG - fprintf(stderr, - "deleteOurException(...).\n"); + fprintf(stderr, + "deleteOurException(...).\n"); #endif - - if (expToDelete && - (expToDelete->exception_class == ourBaseExceptionClass)) { - - free(((char*) expToDelete) + ourBaseFromUnwindOffset); - } + + if (expToDelete && + (expToDelete->exception_class == ourBaseExceptionClass)) { + + free(((char*) expToDelete) + ourBaseFromUnwindOffset); + } } @@ -340,27 +341,27 @@ void deleteOurException(OurUnwindException* expToDelete) { /// @unlink /// @param expToDelete exception instance to delete void deleteFromUnwindOurException(_Unwind_Reason_Code reason, - OurUnwindException* expToDelete) { + OurUnwindException *expToDelete) { #ifdef DEBUG - fprintf(stderr, - "deleteFromUnwindOurException(...).\n"); + fprintf(stderr, + "deleteFromUnwindOurException(...).\n"); #endif - - deleteOurException(expToDelete); + + deleteOurException(expToDelete); } /// Creates (allocates on the heap), an exception (OurException instance), /// of the supplied type info type. /// @param type type info type -OurUnwindException* createOurException(int type) { - size_t size = sizeof(OurException); - OurException* ret = (OurException*) memset(malloc(size), 0, size); - (ret->type).type = type; - (ret->unwindException).exception_class = ourBaseExceptionClass; - (ret->unwindException).exception_cleanup = deleteFromUnwindOurException; - - return(&(ret->unwindException)); +OurUnwindException *createOurException(int type) { + size_t size = sizeof(OurException); + OurException *ret = (OurException*) memset(malloc(size), 0, size); + (ret->type).type = type; + (ret->unwindException).exception_class = ourBaseExceptionClass; + (ret->unwindException).exception_cleanup = deleteFromUnwindOurException; + + return(&(ret->unwindException)); } @@ -369,22 +370,22 @@ 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) { - uintptr_t result = 0; - uintptr_t shift = 0; - unsigned char byte; - const uint8_t* p = *data; - - do { - byte = *p++; - result |= (byte & 0x7f) << shift; - shift += 7; - } - while (byte & 0x80); - - *data = p; - - return result; +static uintptr_t readULEB128(const uint8_t **data) { + uintptr_t result = 0; + uintptr_t shift = 0; + unsigned char byte; + const uint8_t *p = *data; + + do { + byte = *p++; + result |= (byte & 0x7f) << shift; + shift += 7; + } + while (byte & 0x80); + + *data = p; + + return result; } @@ -393,26 +394,26 @@ 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) { - uintptr_t result = 0; - uintptr_t shift = 0; - unsigned char byte; - const uint8_t* p = *data; - - do { - byte = *p++; - result |= (byte & 0x7f) << shift; - shift += 7; - } - while (byte & 0x80); - - *data = p; - - if ((byte & 0x40) && (shift < (sizeof(result) << 3))) { - result |= (~0 << shift); - } - - return result; +static uintptr_t readSLEB128(const uint8_t **data) { + uintptr_t result = 0; + uintptr_t shift = 0; + unsigned char byte; + const uint8_t *p = *data; + + do { + byte = *p++; + result |= (byte & 0x7f) << shift; + shift += 7; + } + while (byte & 0x80); + + *data = p; + + if ((byte & 0x40) && (shift < (sizeof(result) << 3))) { + result |= (~0 << shift); + } + + return result; } @@ -422,82 +423,82 @@ 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) { - uintptr_t result = 0; - const uint8_t* p = *data; - - if (encoding == llvm::dwarf::DW_EH_PE_omit) - return(result); - - // first get value - switch (encoding & 0x0F) { - case llvm::dwarf::DW_EH_PE_absptr: - result = *((uintptr_t*)p); - p += sizeof(uintptr_t); - break; - case llvm::dwarf::DW_EH_PE_uleb128: - result = readULEB128(&p); - break; - // Note: This case has not been tested - case llvm::dwarf::DW_EH_PE_sleb128: - result = readSLEB128(&p); - break; - case llvm::dwarf::DW_EH_PE_udata2: - result = *((uint16_t*)p); - p += sizeof(uint16_t); - break; - case llvm::dwarf::DW_EH_PE_udata4: - result = *((uint32_t*)p); - p += sizeof(uint32_t); - break; - case llvm::dwarf::DW_EH_PE_udata8: - result = *((uint64_t*)p); - p += sizeof(uint64_t); - break; - case llvm::dwarf::DW_EH_PE_sdata2: - result = *((int16_t*)p); - p += sizeof(int16_t); - break; - case llvm::dwarf::DW_EH_PE_sdata4: - result = *((int32_t*)p); - p += sizeof(int32_t); - break; - case llvm::dwarf::DW_EH_PE_sdata8: - result = *((int64_t*)p); - p += sizeof(int64_t); - break; - default: - // not supported - abort(); - break; - } - - // then add relative offset - switch (encoding & 0x70) { - case llvm::dwarf::DW_EH_PE_absptr: - // do nothing - break; - case llvm::dwarf::DW_EH_PE_pcrel: - result += (uintptr_t)(*data); - break; - case llvm::dwarf::DW_EH_PE_textrel: - case llvm::dwarf::DW_EH_PE_datarel: - case llvm::dwarf::DW_EH_PE_funcrel: - case llvm::dwarf::DW_EH_PE_aligned: - default: - // not supported - abort(); - break; - } - - // then apply indirection - if (encoding & llvm::dwarf::DW_EH_PE_indirect) { - result = *((uintptr_t*)result); - } - - *data = p; - - return result; +static uintptr_t readEncodedPointer(const uint8_t **data, uint8_t encoding) { + uintptr_t result = 0; + const uint8_t *p = *data; + + if (encoding == llvm::dwarf::DW_EH_PE_omit) + return(result); + + // first get value + switch (encoding & 0x0F) { + case llvm::dwarf::DW_EH_PE_absptr: + result = *((uintptr_t*)p); + p += sizeof(uintptr_t); + break; + case llvm::dwarf::DW_EH_PE_uleb128: + result = readULEB128(&p); + break; + // Note: This case has not been tested + case llvm::dwarf::DW_EH_PE_sleb128: + result = readSLEB128(&p); + break; + case llvm::dwarf::DW_EH_PE_udata2: + result = *((uint16_t*)p); + p += sizeof(uint16_t); + break; + case llvm::dwarf::DW_EH_PE_udata4: + result = *((uint32_t*)p); + p += sizeof(uint32_t); + break; + case llvm::dwarf::DW_EH_PE_udata8: + result = *((uint64_t*)p); + p += sizeof(uint64_t); + break; + case llvm::dwarf::DW_EH_PE_sdata2: + result = *((int16_t*)p); + p += sizeof(int16_t); + break; + case llvm::dwarf::DW_EH_PE_sdata4: + result = *((int32_t*)p); + p += sizeof(int32_t); + break; + case llvm::dwarf::DW_EH_PE_sdata8: + result = *((int64_t*)p); + p += sizeof(int64_t); + break; + default: + // not supported + abort(); + break; + } + + // then add relative offset + switch (encoding & 0x70) { + case llvm::dwarf::DW_EH_PE_absptr: + // do nothing + break; + case llvm::dwarf::DW_EH_PE_pcrel: + result += (uintptr_t)(*data); + break; + case llvm::dwarf::DW_EH_PE_textrel: + case llvm::dwarf::DW_EH_PE_datarel: + case llvm::dwarf::DW_EH_PE_funcrel: + case llvm::dwarf::DW_EH_PE_aligned: + default: + // not supported + abort(); + break; + } + + // then apply indirection + if (encoding & llvm::dwarf::DW_EH_PE_indirect) { + result = *((uintptr_t*)result); + } + + *data = p; + + return result; } @@ -524,74 +525,74 @@ static bool handleActionValue(int64_t *resultAction, uintptr_t actionEntry, uint64_t exceptionClass, struct _Unwind_Exception *exceptionObject) { - bool ret = false; - - if (!resultAction || - !exceptionObject || - (exceptionClass != ourBaseExceptionClass)) - return(ret); - - struct OurBaseException_t* excp = (struct OurBaseException_t*) - (((char*) exceptionObject) + ourBaseFromUnwindOffset); - struct OurExceptionType_t *excpType = &(excp->type); - int type = excpType->type; - + bool ret = false; + + if (!resultAction || + !exceptionObject || + (exceptionClass != ourBaseExceptionClass)) + return(ret); + + struct OurBaseException_t *excp = (struct OurBaseException_t*) + (((char*) exceptionObject) + ourBaseFromUnwindOffset); + struct OurExceptionType_t *excpType = &(excp->type); + int type = excpType->type; + #ifdef DEBUG - fprintf(stderr, - "handleActionValue(...): exceptionObject = <%p>, " - "excp = <%p>.\n", - exceptionObject, - excp); + fprintf(stderr, + "handleActionValue(...): exceptionObject = <%p>, " + "excp = <%p>.\n", + exceptionObject, + excp); #endif - - const uint8_t *actionPos = (uint8_t*) actionEntry, - *tempActionPos; - int64_t typeOffset = 0, - actionOffset; - - for (int i = 0; true; ++i) { - // Each emitted dwarf action corresponds to a 2 tuple of - // type info address offset, and action offset to the next - // emitted action. - typeOffset = readSLEB128(&actionPos); - tempActionPos = actionPos; - actionOffset = readSLEB128(&tempActionPos); - + + const uint8_t *actionPos = (uint8_t*) actionEntry, + *tempActionPos; + int64_t typeOffset = 0, + actionOffset; + + for (int i = 0; true; ++i) { + // Each emitted dwarf action corresponds to a 2 tuple of + // type info address offset, and action offset to the next + // emitted action. + typeOffset = readSLEB128(&actionPos); + tempActionPos = actionPos; + actionOffset = readSLEB128(&tempActionPos); + #ifdef DEBUG - fprintf(stderr, - "handleActionValue(...):typeOffset: <%lld>, " - "actionOffset: <%lld>.\n", - typeOffset, - actionOffset); + fprintf(stderr, + "handleActionValue(...):typeOffset: <%lld>, " + "actionOffset: <%lld>.\n", + typeOffset, + actionOffset); #endif - assert((typeOffset >= 0) && - "handleActionValue(...):filters are not supported."); - - // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector - // argument has been matched. - if ((typeOffset > 0) && - (type == (classInfo[-typeOffset])->type)) { + assert((typeOffset >= 0) && + "handleActionValue(...):filters are not supported."); + + // Note: A typeOffset == 0 implies that a cleanup llvm.eh.selector + // argument has been matched. + if ((typeOffset > 0) && + (type == (classInfo[-typeOffset])->type)) { #ifdef DEBUG - fprintf(stderr, - "handleActionValue(...):actionValue <%d> found.\n", - i); + fprintf(stderr, + "handleActionValue(...):actionValue <%d> found.\n", + i); #endif - *resultAction = i + 1; - ret = true; - break; - } - + *resultAction = i + 1; + ret = true; + break; + } + #ifdef DEBUG - fprintf(stderr, - "handleActionValue(...):actionValue not found.\n"); + fprintf(stderr, + "handleActionValue(...):actionValue not found.\n"); #endif - if (!actionOffset) - break; - - actionPos += actionOffset; - } - - return(ret); + if (!actionOffset) + break; + + actionPos += actionOffset; + } + + return(ret); } @@ -607,180 +608,177 @@ 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_t* lsda, - _Unwind_Action actions, - uint64_t exceptionClass, - struct _Unwind_Exception* exceptionObject, - _Unwind_Context_t context) { - _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND; - - if (!lsda) - return(ret); - + const uint8_t *lsda, + _Unwind_Action actions, + uint64_t exceptionClass, + struct _Unwind_Exception *exceptionObject, + _Unwind_Context_t context) { + _Unwind_Reason_Code ret = _URC_CONTINUE_UNWIND; + + if (!lsda) + return(ret); + #ifdef DEBUG - fprintf(stderr, - "handleLsda(...):lsda is non-zero.\n"); + fprintf(stderr, + "handleLsda(...):lsda is non-zero.\n"); #endif - - // Get the current instruction pointer and offset it before next - // instruction in the current frame which threw the exception. - uintptr_t pc = _Unwind_GetIP(context)-1; - - // Get beginning current frame's code (as defined by the - // emitted dwarf code) - uintptr_t funcStart = _Unwind_GetRegionStart(context); - uintptr_t pcOffset = pc - funcStart; - struct OurExceptionType_t** classInfo = NULL; - - // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding - // dwarf emission - - // Parse LSDA header. - uint8_t lpStartEncoding = *lsda++; - - if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) { - readEncodedPointer(&lsda, lpStartEncoding); - } - - uint8_t ttypeEncoding = *lsda++; - uintptr_t classInfoOffset; - - if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) { - // Calculate type info locations in emitted dwarf code which - // were flagged by type info arguments to llvm.eh.selector - // intrinsic - classInfoOffset = readULEB128(&lsda); - classInfo = (struct OurExceptionType_t**) (lsda + classInfoOffset); - } - - // Walk call-site table looking for range that - // includes current PC. - - uint8_t callSiteEncoding = *lsda++; - uint32_t callSiteTableLength = readULEB128(&lsda); - const uint8_t* callSiteTableStart = lsda; - const uint8_t* callSiteTableEnd = callSiteTableStart + - callSiteTableLength; - const uint8_t* actionTableStart = callSiteTableEnd; - const uint8_t* callSitePtr = callSiteTableStart; - - bool foreignException = false; - - while (callSitePtr < callSiteTableEnd) { - uintptr_t start = readEncodedPointer(&callSitePtr, - callSiteEncoding); - uintptr_t length = readEncodedPointer(&callSitePtr, + + // Get the current instruction pointer and offset it before next + // instruction in the current frame which threw the exception. + uintptr_t pc = _Unwind_GetIP(context)-1; + + // Get beginning current frame's code (as defined by the + // emitted dwarf code) + uintptr_t funcStart = _Unwind_GetRegionStart(context); + uintptr_t pcOffset = pc - funcStart; + struct OurExceptionType_t **classInfo = NULL; + + // Note: See JITDwarfEmitter::EmitExceptionTable(...) for corresponding + // dwarf emission + + // Parse LSDA header. + uint8_t lpStartEncoding = *lsda++; + + if (lpStartEncoding != llvm::dwarf::DW_EH_PE_omit) { + readEncodedPointer(&lsda, lpStartEncoding); + } + + uint8_t ttypeEncoding = *lsda++; + uintptr_t classInfoOffset; + + if (ttypeEncoding != llvm::dwarf::DW_EH_PE_omit) { + // Calculate type info locations in emitted dwarf code which + // were flagged by type info arguments to llvm.eh.selector + // intrinsic + classInfoOffset = readULEB128(&lsda); + classInfo = (struct OurExceptionType_t**) (lsda + classInfoOffset); + } + + // Walk call-site table looking for range that + // includes current PC. + + uint8_t callSiteEncoding = *lsda++; + uint32_t callSiteTableLength = readULEB128(&lsda); + const uint8_t *callSiteTableStart = lsda; + const uint8_t *callSiteTableEnd = callSiteTableStart + + callSiteTableLength; + const uint8_t *actionTableStart = callSiteTableEnd; + const uint8_t *callSitePtr = callSiteTableStart; + + bool foreignException = false; + + while (callSitePtr < callSiteTableEnd) { + uintptr_t start = readEncodedPointer(&callSitePtr, + callSiteEncoding); + uintptr_t length = readEncodedPointer(&callSitePtr, + callSiteEncoding); + uintptr_t landingPad = readEncodedPointer(&callSitePtr, callSiteEncoding); - uintptr_t landingPad = readEncodedPointer(&callSitePtr, - callSiteEncoding); - - // Note: Action value - uintptr_t actionEntry = readULEB128(&callSitePtr); - - if (exceptionClass != ourBaseExceptionClass) { - // We have been notified of a foreign exception being thrown, - // and we therefore need to execute cleanup landing pads - actionEntry = 0; - foreignException = true; - } - - if (landingPad == 0) { + + // Note: Action value + uintptr_t actionEntry = readULEB128(&callSitePtr); + + if (exceptionClass != ourBaseExceptionClass) { + // We have been notified of a foreign exception being thrown, + // and we therefore need to execute cleanup landing pads + actionEntry = 0; + foreignException = true; + } + + if (landingPad == 0) { #ifdef DEBUG - fprintf(stderr, - "handleLsda(...): No landing pad found.\n"); + fprintf(stderr, + "handleLsda(...): No landing pad found.\n"); #endif - - continue; // no landing pad for this entry - } - - if (actionEntry) { - actionEntry += ((uintptr_t) actionTableStart) - 1; - } - else { + + continue; // no landing pad for this entry + } + + if (actionEntry) { + actionEntry += ((uintptr_t) actionTableStart) - 1; + } + else { #ifdef DEBUG - fprintf(stderr, - "handleLsda(...):No action table found.\n"); + fprintf(stderr, + "handleLsda(...):No action table found.\n"); #endif - } - - bool exceptionMatched = false; - - if ((start <= pcOffset) && (pcOffset < (start + length))) { + } + + bool exceptionMatched = false; + + if ((start <= pcOffset) && (pcOffset < (start + length))) { #ifdef DEBUG - fprintf(stderr, - "handleLsda(...): Landing pad found.\n"); + fprintf(stderr, + "handleLsda(...): Landing pad found.\n"); #endif - int64_t actionValue = 0; - - if (actionEntry) { - exceptionMatched = handleActionValue - ( - &actionValue, - classInfo, - actionEntry, - exceptionClass, - exceptionObject - ); - } - - if (!(actions & _UA_SEARCH_PHASE)) { + int64_t actionValue = 0; + + if (actionEntry) { + exceptionMatched = handleActionValue(&actionValue, + classInfo, + actionEntry, + exceptionClass, + exceptionObject); + } + + if (!(actions & _UA_SEARCH_PHASE)) { #ifdef DEBUG - fprintf(stderr, - "handleLsda(...): installed landing pad " - "context.\n"); + fprintf(stderr, + "handleLsda(...): installed landing pad " + "context.\n"); #endif - - // Found landing pad for the PC. - // Set Instruction Pointer to so we re-enter function - // at landing pad. The landing pad is created by the - // compiler to take two parameters in registers. - _Unwind_SetGR(context, - __builtin_eh_return_data_regno(0), - (uintptr_t)exceptionObject); - - // Note: this virtual register directly corresponds - // to the return of the llvm.eh.selector intrinsic - if (!actionEntry || !exceptionMatched) { - // We indicate cleanup only - _Unwind_SetGR(context, - __builtin_eh_return_data_regno(1), - 0); - } - else { - // Matched type info index of llvm.eh.selector intrinsic - // passed here. - _Unwind_SetGR(context, - __builtin_eh_return_data_regno(1), - actionValue); - } - - // To execute landing pad set here - _Unwind_SetIP(context, funcStart + landingPad); - ret = _URC_INSTALL_CONTEXT; - } - else if (exceptionMatched) { + + // Found landing pad for the PC. + // Set Instruction Pointer to so we re-enter function + // at landing pad. The landing pad is created by the + // compiler to take two parameters in registers. + _Unwind_SetGR(context, + __builtin_eh_return_data_regno(0), + (uintptr_t)exceptionObject); + + // Note: this virtual register directly corresponds + // to the return of the llvm.eh.selector intrinsic + if (!actionEntry || !exceptionMatched) { + // We indicate cleanup only + _Unwind_SetGR(context, + __builtin_eh_return_data_regno(1), + 0); + } + else { + // Matched type info index of llvm.eh.selector intrinsic + // passed here. + _Unwind_SetGR(context, + __builtin_eh_return_data_regno(1), + actionValue); + } + + // To execute landing pad set here + _Unwind_SetIP(context, funcStart + landingPad); + ret = _URC_INSTALL_CONTEXT; + } + else if (exceptionMatched) { #ifdef DEBUG - fprintf(stderr, - "handleLsda(...): setting handler found.\n"); + fprintf(stderr, + "handleLsda(...): setting handler found.\n"); #endif - ret = _URC_HANDLER_FOUND; - } - else { - // Note: Only non-clean up handlers are marked as - // found. Otherwise the clean up handlers will be - // re-found and executed during the clean up - // phase. + ret = _URC_HANDLER_FOUND; + } + else { + // Note: Only non-clean up handlers are marked as + // found. Otherwise the clean up handlers will be + // re-found and executed during the clean up + // phase. #ifdef DEBUG - fprintf(stderr, - "handleLsda(...): cleanup handler found.\n"); + fprintf(stderr, + "handleLsda(...): cleanup handler found.\n"); #endif - } - - break; - } + } + + break; } - - return(ret); + } + + return(ret); } @@ -796,38 +794,38 @@ static _Unwind_Reason_Code handleLsda(int version, /// @param context unwind system context /// @returns minimally supported unwinding control indicator _Unwind_Reason_Code ourPersonality(int version, - _Unwind_Action actions, - uint64_t exceptionClass, - struct _Unwind_Exception* exceptionObject, - _Unwind_Context_t context) { + _Unwind_Action actions, + uint64_t exceptionClass, + struct _Unwind_Exception *exceptionObject, + _Unwind_Context_t context) { #ifdef DEBUG - fprintf(stderr, - "We are in ourPersonality(...):actions is <%d>.\n", - actions); - - if (actions & _UA_SEARCH_PHASE) { - fprintf(stderr, "ourPersonality(...):In search phase.\n"); - } - else { - fprintf(stderr, "ourPersonality(...):In non-search phase.\n"); - } + fprintf(stderr, + "We are in ourPersonality(...):actions is <%d>.\n", + actions); + + if (actions & _UA_SEARCH_PHASE) { + fprintf(stderr, "ourPersonality(...):In search phase.\n"); + } + else { + fprintf(stderr, "ourPersonality(...):In non-search phase.\n"); + } #endif - - const uint8_t* lsda = _Unwind_GetLanguageSpecificData(context); - + + const uint8_t *lsda = _Unwind_GetLanguageSpecificData(context); + #ifdef DEBUG - fprintf(stderr, - "ourPersonality(...):lsda = <%p>.\n", - lsda); + fprintf(stderr, + "ourPersonality(...):lsda = <%p>.\n", + lsda); #endif - - // The real work of the personality function is captured here - return(handleLsda(version, - lsda, - actions, - exceptionClass, - exceptionObject, - context)); + + // The real work of the personality function is captured here + return(handleLsda(version, + lsda, + actions, + exceptionClass, + exceptionObject, + context)); } @@ -840,14 +838,14 @@ _Unwind_Reason_Code ourPersonality(int version, /// @returns class value uint64_t genClass(const unsigned char classChars[], size_t classCharsSize) { - uint64_t ret = classChars[0]; - - for (unsigned i = 1; i < classCharsSize; ++i) { - ret <<= 8; - ret += classChars[i]; - } - - return(ret); + uint64_t ret = classChars[0]; + + for (unsigned i = 1; i < classCharsSize; ++i) { + ret <<= 8; + ret += classChars[i]; + } + + return(ret); } } // extern "C" @@ -869,36 +867,36 @@ 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::LLVMContext& context, - llvm::Module& module, - 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::Constant* stringConstant = - llvm::ConstantArray::get(context, toPrint); - - if (useGlobal) { - // Note: Does not work without allocation - stringVar = - new llvm::GlobalVariable(module, - stringConstant->getType(), - true, - llvm::GlobalValue::LinkerPrivateLinkage, - stringConstant, - ""); - } - else { - stringVar = builder.CreateAlloca(stringConstant->getType()); - builder.CreateStore(stringConstant, stringVar); - } - - llvm::Value* cast = - builder.CreatePointerCast(stringVar, - builder.getInt8Ty()->getPointerTo()); - builder.CreateCall(printFunct, cast); + llvm::Function *printFunct = module.getFunction("printStr"); + + llvm::Value *stringVar; + llvm::Constant *stringConstant = + llvm::ConstantArray::get(context, toPrint); + + if (useGlobal) { + // Note: Does not work without allocation + stringVar = + new llvm::GlobalVariable(module, + stringConstant->getType(), + true, + llvm::GlobalValue::LinkerPrivateLinkage, + stringConstant, + ""); + } + else { + stringVar = builder.CreateAlloca(stringConstant->getType()); + builder.CreateStore(stringConstant, stringVar); + } + + llvm::Value *cast = + builder.CreatePointerCast(stringVar, + builder.getInt8Ty()->getPointerTo()); + builder.CreateCall(printFunct, cast); } @@ -914,35 +912,35 @@ 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::LLVMContext& context, - llvm::Module& module, - llvm::IRBuilder<>& builder, - llvm::Function& printFunct, - llvm::Value& toPrint, +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); - llvm::Value *stringVar; - - if (useGlobal) { - // Note: Does not seem to work without allocation - stringVar = - new llvm::GlobalVariable(module, - stringConstant->getType(), - true, - llvm::GlobalValue::LinkerPrivateLinkage, - stringConstant, - ""); - } - else { - stringVar = builder.CreateAlloca(stringConstant->getType()); - builder.CreateStore(stringConstant, stringVar); - } - - llvm::Value* cast = - builder.CreateBitCast(stringVar, - builder.getInt8Ty()->getPointerTo()); - builder.CreateCall2(&printFunct, &toPrint, cast); + llvm::Constant *stringConstant = llvm::ConstantArray::get(context, format); + llvm::Value *stringVar; + + if (useGlobal) { + // Note: Does not seem to work without allocation + stringVar = + new llvm::GlobalVariable(module, + stringConstant->getType(), + true, + llvm::GlobalValue::LinkerPrivateLinkage, + stringConstant, + ""); + } + else { + stringVar = builder.CreateAlloca(stringConstant->getType()); + builder.CreateStore(stringConstant, stringVar); + } + + llvm::Value *cast = + builder.CreateBitCast(stringVar, + builder.getInt8Ty()->getPointerTo()); + builder.CreateCall2(&printFunct, &toPrint, cast); } @@ -965,64 +963,61 @@ void generateIntegerPrint(llvm::LLVMContext& context, /// @param exceptionCaughtFlag reference exception caught/thrown status storage /// @param exceptionStorage reference to exception pointer storage /// @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) { - assert(exceptionCaughtFlag && - "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag " - "is NULL"); - assert(exceptionStorage && - "ExceptionDemo::createFinallyBlock(...):exceptionStorage " - "is NULL"); - - *exceptionCaughtFlag = - createEntryBlockAlloca(toAddTo, - "exceptionCaught", - ourExceptionNotThrownState->getType(), - ourExceptionNotThrownState); - - const llvm::PointerType* exceptionStorageType = - builder.getInt8Ty()->getPointerTo(); - *exceptionStorage = - createEntryBlockAlloca(toAddTo, - "exceptionStorage", - exceptionStorageType, - llvm::ConstantPointerNull::get( - exceptionStorageType)); - - llvm::BasicBlock *ret = llvm::BasicBlock::Create(context, - blockName, - &toAddTo); - - builder.SetInsertPoint(ret); - - std::ostringstream bufferToPrint; - bufferToPrint << "Gen: Executing finally block " - << blockName - << " in " - << functionId - << std::endl; - generateStringPrint(context, - module, - builder, - bufferToPrint.str(), - USE_GLOBAL_STR_CONSTS); - - llvm::SwitchInst* theSwitch = - builder.CreateSwitch(builder.CreateLoad(*exceptionCaughtFlag), - &terminatorBlock, - 2); - theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock); - theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock); - - return(ret); +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) { + assert(exceptionCaughtFlag && + "ExceptionDemo::createFinallyBlock(...):exceptionCaughtFlag " + "is NULL"); + assert(exceptionStorage && + "ExceptionDemo::createFinallyBlock(...):exceptionStorage " + "is NULL"); + + *exceptionCaughtFlag = + createEntryBlockAlloca(toAddTo, + "exceptionCaught", + ourExceptionNotThrownState->getType(), + ourExceptionNotThrownState); + + const llvm::PointerType *exceptionStorageType = + builder.getInt8Ty()->getPointerTo(); + *exceptionStorage = + createEntryBlockAlloca(toAddTo, + "exceptionStorage", + exceptionStorageType, + llvm::ConstantPointerNull::get( + exceptionStorageType)); + + llvm::BasicBlock *ret = llvm::BasicBlock::Create(context, + blockName, + &toAddTo); + + builder.SetInsertPoint(ret); + + std::ostringstream bufferToPrint; + bufferToPrint << "Gen: Executing finally block " + << blockName << " in " << functionId << "\n"; + generateStringPrint(context, + module, + builder, + bufferToPrint.str(), + USE_GLOBAL_STR_CONSTS); + + llvm::SwitchInst *theSwitch = + builder.CreateSwitch(builder.CreateLoad(*exceptionCaughtFlag), + &terminatorBlock, + 2); + theSwitch->addCase(ourExceptionCaughtState, &terminatorBlock); + theSwitch->addCase(ourExceptionThrownState, &unwindResumeBlock); + + return(ret); } @@ -1038,36 +1033,36 @@ 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::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, - &toAddTo); - - builder.SetInsertPoint(ret); - - std::ostringstream bufferToPrint; - bufferToPrint << "Gen: Executing catch block " - << blockName - << " in " - << functionId - << std::endl; - generateStringPrint(context, - module, - builder, - bufferToPrint.str(), - USE_GLOBAL_STR_CONSTS); - builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag); - builder.CreateBr(&terminatorBlock); - - return(ret); +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, + &toAddTo); + + builder.SetInsertPoint(ret); + + std::ostringstream bufferToPrint; + bufferToPrint << "Gen: Executing catch block " + << blockName + << " in " + << functionId + << std::endl; + generateStringPrint(context, + module, + builder, + bufferToPrint.str(), + USE_GLOBAL_STR_CONSTS); + builder.CreateStore(ourExceptionCaughtState, &exceptionCaughtFlag); + builder.CreateBr(&terminatorBlock); + + return(ret); } @@ -1091,275 +1086,269 @@ 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::LLVMContext& context = module.getContext(); - llvm::Function *toPrint32Int = module.getFunction("print32Int"); - - ArgTypes argTypes; - argTypes.push_back(builder.getInt32Ty()); - - ArgNames argNames; - argNames.push_back("exceptTypeToThrow"); - - llvm::Function* ret = createFunction(module, - builder.getVoidTy(), - argTypes, - argNames, - ourId, - llvm::Function::ExternalLinkage, - false, - false); - - // Block which calls invoke - llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context, - "entry", - ret); - // Normal block for invoke - llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context, - "normal", - ret); - // Unwind block for invoke - 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); - - // Foreign exception handler - llvm::BasicBlock *externalExceptionBlock = - llvm::BasicBlock::Create(context, "externalException", ret); - - // Block which calls _Unwind_Resume - 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); - - std::string nextName; - std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch); - llvm::Value* exceptionCaughtFlag = NULL; - llvm::Value* exceptionStorage = NULL; - - // Finally block which will branch to unwindResumeBlock if - // exception is not caught. Initializes/allocates stack locations. - llvm::BasicBlock* finallyBlock = createFinallyBlock(context, - module, - builder, - *ret, - nextName = "finally", - ourId, - *endBlock, - *unwindResumeBlock, - &exceptionCaughtFlag, - &exceptionStorage); - - for (unsigned i = 0; i < numExceptionsToCatch; ++i) { - nextName = ourTypeInfoNames[exceptionTypesToCatch[i]]; - - // One catch block per type info to be caught - catchBlocks[i] = createCatchBlock(context, - module, - builder, - *ret, - nextName, - ourId, - *finallyBlock, - *exceptionCaughtFlag); - } - - // Entry Block - - builder.SetInsertPoint(entryBlock); - - std::vector<llvm::Value*> args; - args.push_back(namedValues["exceptTypeToThrow"]); - builder.CreateInvoke(&toInvoke, - normalBlock, - exceptionBlock, - args.begin(), - args.end()); - - // End Block - - builder.SetInsertPoint(endBlock); - - generateStringPrint(context, - module, - builder, - "Gen: In end block: exiting in " + ourId + ".\n", - USE_GLOBAL_STR_CONSTS); - llvm::Function *deleteOurException = - module.getFunction("deleteOurException"); - - // Note: function handles NULL exceptions - builder.CreateCall(deleteOurException, - builder.CreateLoad(exceptionStorage)); - builder.CreateRetVoid(); - - // Normal Block - - builder.SetInsertPoint(normalBlock); - - generateStringPrint(context, - module, - builder, - "Gen: No exception in " + ourId + "!\n", - USE_GLOBAL_STR_CONSTS); - - // Finally block is always called - builder.CreateBr(finallyBlock); - - // Unwind Resume Block - - builder.SetInsertPoint(unwindResumeBlock); - - llvm::Function *resumeOurException = - module.getFunction("_Unwind_Resume"); - builder.CreateCall(resumeOurException, - builder.CreateLoad(exceptionStorage)); - builder.CreateUnreachable(); - - // Exception Block - - builder.SetInsertPoint(exceptionBlock); - - llvm::Function *ehException = module.getFunction("llvm.eh.exception"); - - // Retrieve thrown exception - 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()); - - args.clear(); - args.push_back(unwindException); - args.push_back(functPtr); - - // Note: Skipping index 0 - for (unsigned i = 0; i < numExceptionsToCatch; ++i) { - // Set up type infos to be caught - args.push_back( - module.getGlobalVariable( - ourTypeInfoNames[exceptionTypesToCatch[i]])); - } - - args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0)); - - llvm::Function *ehSelector = module.getFunction("llvm.eh.selector"); - - // Set up this exeption block as the landing pad which will handle - // given type infos. See case Intrinsic::eh_selector in - // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...) - // implemented in FunctionLoweringInfo.cpp to see how the implementation - // 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()); - - // Retrieve exception_class member from thrown exception - // (_Unwind_Exception instance). This member tells us whether or not - // the exception is foreign. - llvm::Value* unwindExceptionClass = - builder.CreateLoad( - builder.CreateStructGEP( - builder.CreatePointerCast( - unwindException, - ourUnwindExceptionType->getPointerTo()), - 0)); - - // Branch to the externalExceptionBlock if the exception is foreign or - // to a catch router if not. Either way the finally block will be run. - builder.CreateCondBr( - builder.CreateICmpEQ(unwindExceptionClass, - llvm::ConstantInt::get(builder.getInt64Ty(), - ourBaseExceptionClass)), - exceptionRouteBlock, - externalExceptionBlock); - - // External Exception Block - - builder.SetInsertPoint(externalExceptionBlock); - - generateStringPrint(context, - module, - builder, - "Gen: Foreign exception received.\n", - USE_GLOBAL_STR_CONSTS); - - // Branch to the finally block - builder.CreateBr(finallyBlock); - - // Exception Route Block - - builder.SetInsertPoint(exceptionRouteBlock); - - // Casts exception pointer (_Unwind_Exception instance) to parent - // (OurException instance). - // - // Note: ourBaseFromUnwindOffset is usually negative - llvm::Value* typeInfoThrown = - builder.CreatePointerCast( - builder.CreateConstGEP1_64(unwindException, - ourBaseFromUnwindOffset), - ourExceptionType->getPointerTo()); - - // Retrieve thrown exception type info type - // - // Note: Index is not relative to pointer but instead to structure - // unlike a true getelementptr (GEP) instruction - typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0); - - llvm::Value* typeInfoThrownType = - builder.CreateStructGEP(typeInfoThrown, 0); - - generateIntegerPrint(context, - module, - builder, - *toPrint32Int, - *(builder.CreateLoad(typeInfoThrownType)), - "Gen: Exception type <%d> received (stack unwound) " - " in " + - ourId + - ".\n", - USE_GLOBAL_STR_CONSTS); - - // Route to matched type info catch block or run cleanup finally block - llvm::SwitchInst* switchToCatchBlock = - builder.CreateSwitch(retTypeInfoIndex, - finallyBlock, - numExceptionsToCatch); - - unsigned nextTypeToCatch; - - for (unsigned i = 1; i <= numExceptionsToCatch; ++i) { - nextTypeToCatch = i - 1; - switchToCatchBlock->addCase(llvm::ConstantInt::get( - llvm::Type::getInt32Ty(context), - i), - catchBlocks[nextTypeToCatch]); - } - - llvm::verifyFunction(*ret); - fpm.run(*ret); - - return(ret); +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; + argTypes.push_back(builder.getInt32Ty()); + + ArgNames argNames; + argNames.push_back("exceptTypeToThrow"); + + llvm::Function *ret = createFunction(module, + builder.getVoidTy(), + argTypes, + argNames, + ourId, + llvm::Function::ExternalLinkage, + false, + false); + + // Block which calls invoke + llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context, + "entry", + ret); + // Normal block for invoke + llvm::BasicBlock *normalBlock = llvm::BasicBlock::Create(context, + "normal", + ret); + // Unwind block for invoke + 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); + + // Foreign exception handler + llvm::BasicBlock *externalExceptionBlock = + llvm::BasicBlock::Create(context, "externalException", ret); + + // Block which calls _Unwind_Resume + 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); + + std::string nextName; + std::vector<llvm::BasicBlock*> catchBlocks(numExceptionsToCatch); + llvm::Value *exceptionCaughtFlag = NULL; + llvm::Value *exceptionStorage = NULL; + + // Finally block which will branch to unwindResumeBlock if + // exception is not caught. Initializes/allocates stack locations. + llvm::BasicBlock *finallyBlock = createFinallyBlock(context, + module, + builder, + *ret, + nextName = "finally", + ourId, + *endBlock, + *unwindResumeBlock, + &exceptionCaughtFlag, + &exceptionStorage); + + for (unsigned i = 0; i < numExceptionsToCatch; ++i) { + nextName = ourTypeInfoNames[exceptionTypesToCatch[i]]; + + // One catch block per type info to be caught + catchBlocks[i] = createCatchBlock(context, + module, + builder, + *ret, + nextName, + ourId, + *finallyBlock, + *exceptionCaughtFlag); + } + + // Entry Block + + builder.SetInsertPoint(entryBlock); + + std::vector<llvm::Value*> args; + args.push_back(namedValues["exceptTypeToThrow"]); + builder.CreateInvoke(&toInvoke, + normalBlock, + exceptionBlock, + args.begin(), + args.end()); + + // End Block + + builder.SetInsertPoint(endBlock); + + generateStringPrint(context, + module, + builder, + "Gen: In end block: exiting in " + ourId + ".\n", + USE_GLOBAL_STR_CONSTS); + llvm::Function *deleteOurException = + module.getFunction("deleteOurException"); + + // Note: function handles NULL exceptions + builder.CreateCall(deleteOurException, + builder.CreateLoad(exceptionStorage)); + builder.CreateRetVoid(); + + // Normal Block + + builder.SetInsertPoint(normalBlock); + + generateStringPrint(context, + module, + builder, + "Gen: No exception in " + ourId + "!\n", + USE_GLOBAL_STR_CONSTS); + + // Finally block is always called + builder.CreateBr(finallyBlock); + + // Unwind Resume Block + + builder.SetInsertPoint(unwindResumeBlock); + + llvm::Function *resumeOurException = + module.getFunction("_Unwind_Resume"); + builder.CreateCall(resumeOurException, + builder.CreateLoad(exceptionStorage)); + builder.CreateUnreachable(); + + // Exception Block + + builder.SetInsertPoint(exceptionBlock); + + llvm::Function *ehException = module.getFunction("llvm.eh.exception"); + + // Retrieve thrown exception + 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()); + + args.clear(); + args.push_back(unwindException); + args.push_back(functPtr); + + // Note: Skipping index 0 + for (unsigned i = 0; i < numExceptionsToCatch; ++i) { + // Set up type infos to be caught + args.push_back(module.getGlobalVariable( + ourTypeInfoNames[exceptionTypesToCatch[i]])); + } + + args.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), 0)); + + llvm::Function *ehSelector = module.getFunction("llvm.eh.selector"); + + // Set up this exeption block as the landing pad which will handle + // given type infos. See case Intrinsic::eh_selector in + // SelectionDAGBuilder::visitIntrinsicCall(...) and AddCatchInfo(...) + // implemented in FunctionLoweringInfo.cpp to see how the implementation + // 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()); + + // Retrieve exception_class member from thrown exception + // (_Unwind_Exception instance). This member tells us whether or not + // the exception is foreign. + llvm::Value *unwindExceptionClass = + builder.CreateLoad(builder.CreateStructGEP( + builder.CreatePointerCast(unwindException, + ourUnwindExceptionType->getPointerTo()), + 0)); + + // Branch to the externalExceptionBlock if the exception is foreign or + // to a catch router if not. Either way the finally block will be run. + builder.CreateCondBr(builder.CreateICmpEQ(unwindExceptionClass, + llvm::ConstantInt::get(builder.getInt64Ty(), + ourBaseExceptionClass)), + exceptionRouteBlock, + externalExceptionBlock); + + // External Exception Block + + builder.SetInsertPoint(externalExceptionBlock); + + generateStringPrint(context, + module, + builder, + "Gen: Foreign exception received.\n", + USE_GLOBAL_STR_CONSTS); + + // Branch to the finally block + builder.CreateBr(finallyBlock); + + // Exception Route Block + + builder.SetInsertPoint(exceptionRouteBlock); + + // Casts exception pointer (_Unwind_Exception instance) to parent + // (OurException instance). + // + // Note: ourBaseFromUnwindOffset is usually negative + llvm::Value *typeInfoThrown = + builder.CreatePointerCast(builder.CreateConstGEP1_64(unwindException, + ourBaseFromUnwindOffset), + ourExceptionType->getPointerTo()); + + // Retrieve thrown exception type info type + // + // Note: Index is not relative to pointer but instead to structure + // unlike a true getelementptr (GEP) instruction + typeInfoThrown = builder.CreateStructGEP(typeInfoThrown, 0); + + llvm::Value *typeInfoThrownType = + builder.CreateStructGEP(typeInfoThrown, 0); + + generateIntegerPrint(context, + module, + builder, + *toPrint32Int, + *(builder.CreateLoad(typeInfoThrownType)), + "Gen: Exception type <%d> received (stack unwound) " + " in " + + ourId + + ".\n", + USE_GLOBAL_STR_CONSTS); + + // Route to matched type info catch block or run cleanup finally block + llvm::SwitchInst *switchToCatchBlock = + builder.CreateSwitch(retTypeInfoIndex, + finallyBlock, + numExceptionsToCatch); + + unsigned nextTypeToCatch; + + for (unsigned i = 1; i <= numExceptionsToCatch; ++i) { + nextTypeToCatch = i - 1; + switchToCatchBlock->addCase(llvm::ConstantInt::get( + llvm::Type::getInt32Ty(context), i), + catchBlocks[nextTypeToCatch]); + } + + llvm::verifyFunction(*ret); + fpm.run(*ret); + + return(ret); } @@ -1378,107 +1367,107 @@ 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::FunctionPassManager& fpm, - std::string ourId, - int32_t nativeThrowType, - llvm::Function& nativeThrowFunct) { - llvm::LLVMContext& context = module.getContext(); - namedValues.clear(); - ArgTypes unwindArgTypes; - unwindArgTypes.push_back(builder.getInt32Ty()); - ArgNames unwindArgNames; - unwindArgNames.push_back("exceptTypeToThrow"); - - llvm::Function *ret = createFunction(module, - builder.getVoidTy(), - unwindArgTypes, - unwindArgNames, - ourId, - llvm::Function::ExternalLinkage, - false, - false); - - // Throws either one of our exception or a native C++ exception depending - // on a runtime argument value containing a type info type. - llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context, - "entry", - ret); - // Throws a foreign exception - llvm::BasicBlock *nativeThrowBlock = - llvm::BasicBlock::Create(context, - "nativeThrow", - ret); - // Throws one of our Exceptions - llvm::BasicBlock *generatedThrowBlock = - llvm::BasicBlock::Create(context, - "generatedThrow", - ret); - // Retrieved runtime type info type to throw - llvm::Value* exceptionType = namedValues["exceptTypeToThrow"]; - - // nativeThrowBlock block - - builder.SetInsertPoint(nativeThrowBlock); - - // Throws foreign exception - builder.CreateCall(&nativeThrowFunct, exceptionType); - builder.CreateUnreachable(); - - // entry block - - builder.SetInsertPoint(entryBlock); - - llvm::Function *toPrint32Int = module.getFunction("print32Int"); - generateIntegerPrint(context, - module, - builder, - *toPrint32Int, - *exceptionType, - "\nGen: About to throw exception type <%d> in " + - ourId + - ".\n", - USE_GLOBAL_STR_CONSTS); - - // 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::SwitchInst* theSwitch = builder.CreateSwitch(exceptionType, - generatedThrowBlock, - 1); - - theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), - nativeThrowType), - nativeThrowBlock); - - // generatedThrow block - - builder.SetInsertPoint(generatedThrowBlock); - - 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"]); - - // Throw generated Exception - builder.CreateCall(raiseOurException, exception); - builder.CreateUnreachable(); - - llvm::verifyFunction(*ret); - fpm.run(*ret); - - return(ret); +llvm::Function *createThrowExceptionFunction(llvm::Module &module, + llvm::IRBuilder<> &builder, + llvm::FunctionPassManager &fpm, + std::string ourId, + int32_t nativeThrowType, + llvm::Function &nativeThrowFunct) { + llvm::LLVMContext &context = module.getContext(); + namedValues.clear(); + ArgTypes unwindArgTypes; + unwindArgTypes.push_back(builder.getInt32Ty()); + ArgNames unwindArgNames; + unwindArgNames.push_back("exceptTypeToThrow"); + + llvm::Function *ret = createFunction(module, + builder.getVoidTy(), + unwindArgTypes, + unwindArgNames, + ourId, + llvm::Function::ExternalLinkage, + false, + false); + + // Throws either one of our exception or a native C++ exception depending + // on a runtime argument value containing a type info type. + llvm::BasicBlock *entryBlock = llvm::BasicBlock::Create(context, + "entry", + ret); + // Throws a foreign exception + llvm::BasicBlock *nativeThrowBlock = + llvm::BasicBlock::Create(context, + "nativeThrow", + ret); + // Throws one of our Exceptions + llvm::BasicBlock *generatedThrowBlock = + llvm::BasicBlock::Create(context, + "generatedThrow", + ret); + // Retrieved runtime type info type to throw + llvm::Value *exceptionType = namedValues["exceptTypeToThrow"]; + + // nativeThrowBlock block + + builder.SetInsertPoint(nativeThrowBlock); + + // Throws foreign exception + builder.CreateCall(&nativeThrowFunct, exceptionType); + builder.CreateUnreachable(); + + // entry block + + builder.SetInsertPoint(entryBlock); + + llvm::Function *toPrint32Int = module.getFunction("print32Int"); + generateIntegerPrint(context, + module, + builder, + *toPrint32Int, + *exceptionType, + "\nGen: About to throw exception type <%d> in " + + ourId + + ".\n", + USE_GLOBAL_STR_CONSTS); + + // 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::SwitchInst *theSwitch = builder.CreateSwitch(exceptionType, + generatedThrowBlock, + 1); + + theSwitch->addCase(llvm::ConstantInt::get(llvm::Type::getInt32Ty(context), + nativeThrowType), + nativeThrowBlock); + + // generatedThrow block + + builder.SetInsertPoint(generatedThrowBlock); + + 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"]); + + // Throw generated Exception + builder.CreateCall(raiseOurException, exception); + builder.CreateUnreachable(); + + llvm::verifyFunction(*ret); + fpm.run(*ret); + + return(ret); } static void createStandardUtilityFunctions(unsigned numTypeInfos, - llvm::Module& module, - 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 @@ -1500,81 +1489,80 @@ 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::FunctionPassManager& fpm, +llvm::Function *createUnwindExceptionTest(llvm::Module &module, + llvm::IRBuilder<> &builder, + llvm::FunctionPassManager &fpm, std::string nativeThrowFunctName) { - // Number of type infos to generate - unsigned numTypeInfos = 6; - - // Initialze intrisics and external functions to use along with exception - // and type info globals. - createStandardUtilityFunctions(numTypeInfos, - module, - builder); - 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); - // Inner function will catch even type infos - unsigned innerExceptionTypesToCatch[] = {6, 2, 4}; - size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) / - sizeof(unsigned); - - // Generate inner function. - llvm::Function* innerCatchFunct = - createCatchWrappedInvokeFunction(module, - builder, - fpm, - *throwFunct, - "innerCatchFunct", - numExceptionTypesToCatch, - innerExceptionTypesToCatch); - - // Outer function will catch odd type infos - unsigned outerExceptionTypesToCatch[] = {3, 1, 5}; - numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) / - sizeof(unsigned); - - // Generate outer function - llvm::Function* outerCatchFunct = - createCatchWrappedInvokeFunction(module, - builder, - fpm, - *innerCatchFunct, - "outerCatchFunct", - numExceptionTypesToCatch, - outerExceptionTypesToCatch); - - // Return outer function to run - return(outerCatchFunct); + // Number of type infos to generate + unsigned numTypeInfos = 6; + + // Initialze intrisics and external functions to use along with exception + // and type info globals. + createStandardUtilityFunctions(numTypeInfos, + module, + builder); + 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); + // Inner function will catch even type infos + unsigned innerExceptionTypesToCatch[] = {6, 2, 4}; + size_t numExceptionTypesToCatch = sizeof(innerExceptionTypesToCatch) / + sizeof(unsigned); + + // Generate inner function. + llvm::Function *innerCatchFunct = + createCatchWrappedInvokeFunction(module, + builder, + fpm, + *throwFunct, + "innerCatchFunct", + numExceptionTypesToCatch, + innerExceptionTypesToCatch); + + // Outer function will catch odd type infos + unsigned outerExceptionTypesToCatch[] = {3, 1, 5}; + numExceptionTypesToCatch = sizeof(outerExceptionTypesToCatch) / + sizeof(unsigned); + + // Generate outer function + llvm::Function *outerCatchFunct = + createCatchWrappedInvokeFunction(module, + builder, + fpm, + *innerCatchFunct, + "outerCatchFunct", + numExceptionTypesToCatch, + outerExceptionTypesToCatch); + + // Return outer function to run + return(outerCatchFunct); } /// Represents our foreign exceptions class OurCppRunException : public std::runtime_error { public: - OurCppRunException(const std::string reason) : - std::runtime_error(reason) {} - - OurCppRunException (const OurCppRunException& toCopy) : - std::runtime_error(toCopy) {} - - OurCppRunException& operator = (const OurCppRunException& toCopy) { - return(reinterpret_cast<OurCppRunException&>( - std::runtime_error::operator = (toCopy) - )); - } - - ~OurCppRunException (void) throw () {} + OurCppRunException(const std::string reason) : + std::runtime_error(reason) {} + + OurCppRunException (const OurCppRunException &toCopy) : + std::runtime_error(toCopy) {} + + OurCppRunException &operator = (const OurCppRunException &toCopy) { + return(reinterpret_cast<OurCppRunException&>( + std::runtime_error::operator=(toCopy))); + } + + ~OurCppRunException (void) throw () {} }; @@ -1583,13 +1571,13 @@ public: /// generated function contract. extern "C" void throwCppException (int32_t ignoreIt) { - throw(OurCppRunException("thrown by throwCppException(...)")); + throw(OurCppRunException("thrown by throwCppException(...)")); } 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 @@ -1598,45 +1586,44 @@ 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::ExecutionEngine* engine, - llvm::Function* function, +void runExceptionThrow(llvm::ExecutionEngine *engine, + llvm::Function *function, int32_t typeToThrow) { - - // Find test's function pointer - OurExceptionThrowFunctType functPtr = - reinterpret_cast<OurExceptionThrowFunctType>( - reinterpret_cast<intptr_t>( - engine->getPointerToFunction(function) - ) - ); - - try { - // Run test - (*functPtr)(typeToThrow); - } - catch (OurCppRunException exc) { - // Catch foreign C++ exception - fprintf(stderr, - "\nrunExceptionThrow(...):In C++ catch OurCppRunException " - "with reason: %s.\n", - 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. - - fprintf(stderr, - "\nrunExceptionThrow(...):In C++ catch all.\n"); - } + + // Find test's function pointer + OurExceptionThrowFunctType functPtr = + reinterpret_cast<OurExceptionThrowFunctType>( + reinterpret_cast<intptr_t>(engine->getPointerToFunction(function))); + + try { + // Run test + (*functPtr)(typeToThrow); + } + catch (OurCppRunException exc) { + // Catch foreign C++ exception + fprintf(stderr, + "\nrunExceptionThrow(...):In C++ catch OurCppRunException " + "with reason: %s.\n", + 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. + + fprintf(stderr, + "\nrunExceptionThrow(...):In C++ catch all.\n"); + } } // // End test functions // +typedef llvm::ArrayRef<const llvm::Type*> TypeArray; + /// This initialization routine creates type info globals and /// adds external function declarations to module. /// @param numTypeInfos number of linear type info associated type info types @@ -1644,287 +1631,285 @@ void runExceptionThrow(llvm::ExecutionEngine* engine, /// @param module code for module instance /// @param builder builder instance static void createStandardUtilityFunctions(unsigned numTypeInfos, - llvm::Module& module, - llvm::IRBuilder<>& builder) { - - llvm::LLVMContext& context = module.getContext(); - - // Exception initializations - - // Setup exception catch state - ourExceptionNotThrownState = - llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0), - ourExceptionThrownState = - llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1), - ourExceptionCaughtState = - llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2), - - - // Create our type info type - ourTypeInfoType = llvm::StructType::get(context, - builder.getInt32Ty(), - NULL); - - // Create OurException type - ourExceptionType = llvm::StructType::get(context, - ourTypeInfoType, - NULL); - - // Create portion of _Unwind_Exception type - // - // Note: Declaring only a portion of the _Unwind_Exception struct. - // Does this cause problems? - ourUnwindExceptionType = llvm::StructType::get(context, - builder.getInt64Ty(), - NULL); - struct OurBaseException_t dummyException; - - // Calculate offset of OurException::unwindException member. - ourBaseFromUnwindOffset = ((uintptr_t) &dummyException) - - ((uintptr_t) &(dummyException.unwindException)); - + llvm::Module &module, + llvm::IRBuilder<> &builder) { + + llvm::LLVMContext &context = module.getContext(); + + // Exception initializations + + // Setup exception catch state + ourExceptionNotThrownState = + llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 0), + ourExceptionThrownState = + llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 1), + ourExceptionCaughtState = + llvm::ConstantInt::get(llvm::Type::getInt8Ty(context), 2), + + + + // Create our type info type + ourTypeInfoType = llvm::StructType::get(context, + TypeArray(builder.getInt32Ty())); + + // Create OurException type + ourExceptionType = llvm::StructType::get(context, + TypeArray(ourTypeInfoType)); + + // Create portion of _Unwind_Exception type + // + // Note: Declaring only a portion of the _Unwind_Exception struct. + // Does this cause problems? + ourUnwindExceptionType = + 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)); + #ifdef DEBUG - fprintf(stderr, - "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset " - "= %lld, sizeof(struct OurBaseException_t) - " - "sizeof(struct _Unwind_Exception) = %lu.\n", - ourBaseFromUnwindOffset, - sizeof(struct OurBaseException_t) - - sizeof(struct _Unwind_Exception)); + fprintf(stderr, + "createStandardUtilityFunctions(...):ourBaseFromUnwindOffset " + "= %lld, sizeof(struct OurBaseException_t) - " + "sizeof(struct _Unwind_Exception) = %lu.\n", + ourBaseFromUnwindOffset, + sizeof(struct OurBaseException_t) - + sizeof(struct _Unwind_Exception)); #endif - - size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char); - - // Create our _Unwind_Exception::exception_class value - ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars); - - // Type infos - - std::string baseStr = "typeInfo", typeInfoName; - std::ostringstream typeInfoNameBuilder; - std::vector<llvm::Constant*> structVals; + + size_t numChars = sizeof(ourBaseExcpClassChars) / sizeof(char); + + // Create our _Unwind_Exception::exception_class value + ourBaseExceptionClass = genClass(ourBaseExcpClassChars, numChars); + + // Type infos + + std::string baseStr = "typeInfo", typeInfoName; + std::ostringstream typeInfoNameBuilder; + std::vector<llvm::Constant*> structVals; + + llvm::Constant *nextStruct; + llvm::GlobalVariable *nextGlobal = NULL; + + // Generate each type info + // + // Note: First type info is not used. + for (unsigned i = 0; i <= numTypeInfos; ++i) { + structVals.clear(); + structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i)); + nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals); - llvm::Constant *nextStruct; - llvm::GlobalVariable* nextGlobal = NULL; - - // Generate each type info - // - // Note: First type info is not used. - for (unsigned i = 0; i <= numTypeInfos; ++i) { - structVals.clear(); - structVals.push_back(llvm::ConstantInt::get(builder.getInt32Ty(), i)); - nextStruct = llvm::ConstantStruct::get(ourTypeInfoType, structVals); - - typeInfoNameBuilder.str(""); - typeInfoNameBuilder << baseStr << i; - typeInfoName = typeInfoNameBuilder.str(); - - // Note: Does not seem to work without allocation - nextGlobal = - new llvm::GlobalVariable(module, - ourTypeInfoType, - true, - llvm::GlobalValue::ExternalLinkage, - nextStruct, - typeInfoName); - - ourTypeInfoNames.push_back(typeInfoName); - ourTypeInfoNamesIndex[i] = typeInfoName; - } - - ArgNames argNames; - ArgTypes argTypes; - llvm::Function* funct = NULL; - - // print32Int - - const llvm::Type* retType = builder.getVoidTy(); - - argTypes.clear(); - argTypes.push_back(builder.getInt32Ty()); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "print32Int", - llvm::Function::ExternalLinkage, - true, - false); - - // print64Int - - retType = builder.getVoidTy(); - - argTypes.clear(); - argTypes.push_back(builder.getInt64Ty()); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "print64Int", - llvm::Function::ExternalLinkage, - true, - false); - - // printStr - - retType = builder.getVoidTy(); - - argTypes.clear(); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "printStr", - llvm::Function::ExternalLinkage, - true, - false); - - // throwCppException - - retType = builder.getVoidTy(); - - argTypes.clear(); - argTypes.push_back(builder.getInt32Ty()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "throwCppException", - llvm::Function::ExternalLinkage, - true, - false); - - // deleteOurException - - retType = builder.getVoidTy(); - - argTypes.clear(); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "deleteOurException", - llvm::Function::ExternalLinkage, - true, - false); - - // createOurException - - retType = builder.getInt8Ty()->getPointerTo(); - - argTypes.clear(); - argTypes.push_back(builder.getInt32Ty()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "createOurException", - llvm::Function::ExternalLinkage, - true, - false); - - // _Unwind_RaiseException - - retType = builder.getInt32Ty(); - - argTypes.clear(); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - funct = createFunction(module, - retType, - argTypes, - argNames, - "_Unwind_RaiseException", - llvm::Function::ExternalLinkage, - true, - false); - - funct->addFnAttr(llvm::Attribute::NoReturn); - - // _Unwind_Resume - - retType = builder.getInt32Ty(); - - argTypes.clear(); - argTypes.push_back(builder.getInt8Ty()->getPointerTo()); - - argNames.clear(); - - funct = createFunction(module, - retType, - argTypes, - argNames, - "_Unwind_Resume", - llvm::Function::ExternalLinkage, - true, - false); - - funct->addFnAttr(llvm::Attribute::NoReturn); - - // ourPersonality - - retType = builder.getInt32Ty(); - - argTypes.clear(); - 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()); - - argNames.clear(); - - createFunction(module, - retType, - argTypes, - argNames, - "ourPersonality", - llvm::Function::ExternalLinkage, - true, - false); - - // llvm.eh.selector intrinsic - - getDeclaration(&module, llvm::Intrinsic::eh_selector); - - // llvm.eh.exception intrinsic - - getDeclaration(&module, llvm::Intrinsic::eh_exception); - - // llvm.eh.typeid.for intrinsic - - getDeclaration(&module, llvm::Intrinsic::eh_typeid_for); + typeInfoNameBuilder.str(""); + typeInfoNameBuilder << baseStr << i; + typeInfoName = typeInfoNameBuilder.str(); + + // Note: Does not seem to work without allocation + nextGlobal = + new llvm::GlobalVariable(module, + ourTypeInfoType, + true, + llvm::GlobalValue::ExternalLinkage, + nextStruct, + typeInfoName); + + ourTypeInfoNames.push_back(typeInfoName); + ourTypeInfoNamesIndex[i] = typeInfoName; + } + + ArgNames argNames; + ArgTypes argTypes; + llvm::Function *funct = NULL; + + // print32Int + + const llvm::Type *retType = builder.getVoidTy(); + + argTypes.clear(); + argTypes.push_back(builder.getInt32Ty()); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "print32Int", + llvm::Function::ExternalLinkage, + true, + false); + + // print64Int + + retType = builder.getVoidTy(); + + argTypes.clear(); + argTypes.push_back(builder.getInt64Ty()); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "print64Int", + llvm::Function::ExternalLinkage, + true, + false); + + // printStr + + retType = builder.getVoidTy(); + + argTypes.clear(); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "printStr", + llvm::Function::ExternalLinkage, + true, + false); + + // throwCppException + + retType = builder.getVoidTy(); + + argTypes.clear(); + argTypes.push_back(builder.getInt32Ty()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "throwCppException", + llvm::Function::ExternalLinkage, + true, + false); + + // deleteOurException + + retType = builder.getVoidTy(); + + argTypes.clear(); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "deleteOurException", + llvm::Function::ExternalLinkage, + true, + false); + + // createOurException + + retType = builder.getInt8Ty()->getPointerTo(); + + argTypes.clear(); + argTypes.push_back(builder.getInt32Ty()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "createOurException", + llvm::Function::ExternalLinkage, + true, + false); + + // _Unwind_RaiseException + + retType = builder.getInt32Ty(); + + argTypes.clear(); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + funct = createFunction(module, + retType, + argTypes, + argNames, + "_Unwind_RaiseException", + llvm::Function::ExternalLinkage, + true, + false); + + funct->addFnAttr(llvm::Attribute::NoReturn); + + // _Unwind_Resume + + retType = builder.getInt32Ty(); + + argTypes.clear(); + argTypes.push_back(builder.getInt8Ty()->getPointerTo()); + + argNames.clear(); + + funct = createFunction(module, + retType, + argTypes, + argNames, + "_Unwind_Resume", + llvm::Function::ExternalLinkage, + true, + false); + + funct->addFnAttr(llvm::Attribute::NoReturn); + + // ourPersonality + + retType = builder.getInt32Ty(); + + argTypes.clear(); + 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()); + + argNames.clear(); + + createFunction(module, + retType, + argTypes, + argNames, + "ourPersonality", + llvm::Function::ExternalLinkage, + true, + false); + + // llvm.eh.selector intrinsic + + getDeclaration(&module, llvm::Intrinsic::eh_selector); + + // llvm.eh.exception intrinsic + + getDeclaration(&module, llvm::Intrinsic::eh_exception); + + // llvm.eh.typeid.for intrinsic + + getDeclaration(&module, llvm::Intrinsic::eh_typeid_for); } -//===---------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// // Main test driver code. -//===---------------------------------------------------------------------===// +//===----------------------------------------------------------------------===// /// Demo main routine which takes the type info types to throw. A test will /// be run for each given type info type. While type info types with the value @@ -1932,99 +1917,99 @@ 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, char* argv[]) { - if (argc == 1) { - fprintf(stderr, - "\nUsage: ExceptionDemo <exception type to throw> " - "[<type 2>...<type n>].\n" - " Each type must have the value of 1 - 6 for " - "generated exceptions to be caught;\n" - " the value -1 for foreign C++ exceptions to be " - "generated and thrown;\n" - " or the values > 6 for exceptions to be ignored.\n" - "\nTry: ExceptionDemo 2 3 7 -1\n" - " for a full test.\n\n"); - return(0); - } - - // If not set, exception handling will not be turned on - llvm::JITExceptionHandling = true; - - llvm::InitializeNativeTarget(); - llvm::LLVMContext& context = llvm::getGlobalContext(); - llvm::IRBuilder<> theBuilder(context); - - // Make the module, which holds all the code. - 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(); - - { - llvm::FunctionPassManager fpm(module); - - // Set up the optimizer pipeline. - // Start with registering info about how the - // target lays out data structures. - fpm.add(new llvm::TargetData(*executionEngine->getTargetData())); - - // Optimizations turned on +int main(int argc, char *argv[]) { + if (argc == 1) { + fprintf(stderr, + "\nUsage: ExceptionDemo <exception type to throw> " + "[<type 2>...<type n>].\n" + " Each type must have the value of 1 - 6 for " + "generated exceptions to be caught;\n" + " the value -1 for foreign C++ exceptions to be " + "generated and thrown;\n" + " or the values > 6 for exceptions to be ignored.\n" + "\nTry: ExceptionDemo 2 3 7 -1\n" + " for a full test.\n\n"); + return(0); + } + + // If not set, exception handling will not be turned on + llvm::JITExceptionHandling = true; + + llvm::InitializeNativeTarget(); + llvm::LLVMContext &context = llvm::getGlobalContext(); + llvm::IRBuilder<> theBuilder(context); + + // Make the module, which holds all the code. + 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(); + + { + llvm::FunctionPassManager fpm(module); + + // Set up the optimizer pipeline. + // Start with registering info about how the + // target lays out data structures. + fpm.add(new llvm::TargetData(*executionEngine->getTargetData())); + + // Optimizations turned on #ifdef ADD_OPT_PASSES - - // Basic AliasAnslysis support for GVN. - fpm.add(llvm::createBasicAliasAnalysisPass()); - - // Promote allocas to registers. - fpm.add(llvm::createPromoteMemoryToRegisterPass()); - - // Do simple "peephole" optimizations and bit-twiddling optzns. - fpm.add(llvm::createInstructionCombiningPass()); - - // Reassociate expressions. - fpm.add(llvm::createReassociatePass()); - - // Eliminate Common SubExpressions. - fpm.add(llvm::createGVNPass()); - - // Simplify the control flow graph (deleting unreachable - // blocks, etc). - fpm.add(llvm::createCFGSimplificationPass()); + + // Basic AliasAnslysis support for GVN. + fpm.add(llvm::createBasicAliasAnalysisPass()); + + // Promote allocas to registers. + fpm.add(llvm::createPromoteMemoryToRegisterPass()); + + // Do simple "peephole" optimizations and bit-twiddling optzns. + fpm.add(llvm::createInstructionCombiningPass()); + + // Reassociate expressions. + fpm.add(llvm::createReassociatePass()); + + // Eliminate Common SubExpressions. + fpm.add(llvm::createGVNPass()); + + // Simplify the control flow graph (deleting unreachable + // blocks, etc). + fpm.add(llvm::createCFGSimplificationPass()); #endif // ADD_OPT_PASSES - - fpm.doInitialization(); - - // Generate test code using function throwCppException(...) as - // the function which throws foreign exceptions. - llvm::Function* toRun = - createUnwindExceptionTest(*module, - theBuilder, - fpm, - "throwCppException"); - - fprintf(stderr, "\nBegin module dump:\n\n"); - - module->dump(); - - fprintf(stderr, "\nEnd module dump:\n"); - - fprintf(stderr, "\n\nBegin Test:\n"); - - for (int i = 1; i < argc; ++i) { - // Run test for each argument whose value is the exception - // type to throw. - runExceptionThrow(executionEngine, - toRun, - (unsigned) strtoul(argv[i], NULL, 10)); - } - - fprintf(stderr, "\nEnd Test:\n\n"); - } - - delete executionEngine; + + fpm.doInitialization(); + + // Generate test code using function throwCppException(...) as + // the function which throws foreign exceptions. + llvm::Function *toRun = + createUnwindExceptionTest(*module, + theBuilder, + fpm, + "throwCppException"); + + fprintf(stderr, "\nBegin module dump:\n\n"); + + module->dump(); + + fprintf(stderr, "\nEnd module dump:\n"); + + fprintf(stderr, "\n\nBegin Test:\n"); + + for (int i = 1; i < argc; ++i) { + // Run test for each argument whose value is the exception + // type to throw. + runExceptionThrow(executionEngine, + toRun, + (unsigned) strtoul(argv[i], NULL, 10)); + } + + fprintf(stderr, "\nEnd Test:\n\n"); + } + + delete executionEngine; - return 0; + return 0; } diff --git a/examples/Kaleidoscope/Chapter5/toy.cpp b/examples/Kaleidoscope/Chapter5/toy.cpp index 26b3db6..5dcc7ed 100644 --- a/examples/Kaleidoscope/Chapter5/toy.cpp +++ b/examples/Kaleidoscope/Chapter5/toy.cpp @@ -550,7 +550,7 @@ Value *IfExprAST::Codegen() { // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); @@ -592,7 +592,7 @@ Value *ForExprAST::Codegen() { Builder.SetInsertPoint(LoopBB); // Start the PHI node with an entry for Start. - PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), VarName.c_str()); + PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str()); Variable->addIncoming(StartVal, PreheaderBB); // Within the loop, the variable is defined equal to the PHI node. If it diff --git a/examples/Kaleidoscope/Chapter6/toy.cpp b/examples/Kaleidoscope/Chapter6/toy.cpp index 838125a..c557699 100644 --- a/examples/Kaleidoscope/Chapter6/toy.cpp +++ b/examples/Kaleidoscope/Chapter6/toy.cpp @@ -654,7 +654,7 @@ Value *IfExprAST::Codegen() { // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); @@ -696,7 +696,7 @@ Value *ForExprAST::Codegen() { Builder.SetInsertPoint(LoopBB); // Start the PHI node with an entry for Start. - PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), VarName.c_str()); + PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, VarName.c_str()); Variable->addIncoming(StartVal, PreheaderBB); // Within the loop, the variable is defined equal to the PHI node. If it diff --git a/examples/Kaleidoscope/Chapter7/toy.cpp b/examples/Kaleidoscope/Chapter7/toy.cpp index e63578f..6afd118 100644 --- a/examples/Kaleidoscope/Chapter7/toy.cpp +++ b/examples/Kaleidoscope/Chapter7/toy.cpp @@ -750,7 +750,7 @@ Value *IfExprAST::Codegen() { // Emit merge block. TheFunction->getBasicBlockList().push_back(MergeBB); Builder.SetInsertPoint(MergeBB); - PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), + PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp"); PN->addIncoming(ThenV, ThenBB); |