Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 1 | //===--- MicrosoftCXXABI.cpp - Emit LLVM Code from ASTs for a Module ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 10 | // This provides C++ code generation targeting the Microsoft Visual C++ ABI. |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 11 | // The class in this file generates structures that follow the Microsoft |
| 12 | // Visual C++ ABI, which is actually not very well documented at all outside |
| 13 | // of Microsoft. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "CGCXXABI.h" |
| 18 | #include "CodeGenModule.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclCXX.h" |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace clang; |
| 23 | using namespace CodeGen; |
| 24 | |
| 25 | namespace { |
| 26 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 27 | class MicrosoftCXXABI : public CGCXXABI { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 28 | public: |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 29 | MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {} |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 30 | |
| 31 | void BuildConstructorSignature(const CXXConstructorDecl *Ctor, |
| 32 | CXXCtorType Type, |
| 33 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 34 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 35 | // 'this' is already in place |
| 36 | // TODO: 'for base' flag |
| 37 | } |
| 38 | |
| 39 | void BuildDestructorSignature(const CXXDestructorDecl *Ctor, |
| 40 | CXXDtorType Type, |
| 41 | CanQualType &ResTy, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 42 | SmallVectorImpl<CanQualType> &ArgTys) { |
John McCall | 4c40d98 | 2010-08-31 07:33:07 +0000 | [diff] [blame] | 43 | // 'this' is already in place |
| 44 | // TODO: 'for base' flag |
| 45 | } |
| 46 | |
| 47 | void BuildInstanceFunctionParams(CodeGenFunction &CGF, |
| 48 | QualType &ResTy, |
| 49 | FunctionArgList &Params) { |
| 50 | BuildThisParam(CGF, Params); |
| 51 | // TODO: 'for base' flag |
| 52 | } |
| 53 | |
| 54 | void EmitInstanceFunctionProlog(CodeGenFunction &CGF) { |
| 55 | EmitThisParam(CGF); |
| 56 | // TODO: 'for base' flag |
| 57 | } |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 58 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 59 | void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 60 | llvm::GlobalVariable *DeclPtr, |
| 61 | bool PerformInit); |
| 62 | |
| 63 | |
John McCall | fd70826 | 2011-01-27 02:46:02 +0000 | [diff] [blame] | 64 | // ==== Notes on array cookies ========= |
| 65 | // |
| 66 | // MSVC seems to only use cookies when the class has a destructor; a |
| 67 | // two-argument usual array deallocation function isn't sufficient. |
| 68 | // |
| 69 | // For example, this code prints "100" and "1": |
| 70 | // struct A { |
| 71 | // char x; |
| 72 | // void *operator new[](size_t sz) { |
| 73 | // printf("%u\n", sz); |
| 74 | // return malloc(sz); |
| 75 | // } |
| 76 | // void operator delete[](void *p, size_t sz) { |
| 77 | // printf("%u\n", sz); |
| 78 | // free(p); |
| 79 | // } |
| 80 | // }; |
| 81 | // int main() { |
| 82 | // A *p = new A[100]; |
| 83 | // delete[] p; |
| 84 | // } |
| 85 | // Whereas it prints "104" and "104" if you give A a destructor. |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 86 | |
| 87 | bool requiresArrayCookie(const CXXDeleteExpr *expr, QualType elementType); |
| 88 | bool requiresArrayCookie(const CXXNewExpr *expr); |
| 89 | CharUnits getArrayCookieSizeImpl(QualType type); |
| 90 | llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF, |
| 91 | llvm::Value *NewPtr, |
| 92 | llvm::Value *NumElements, |
| 93 | const CXXNewExpr *expr, |
| 94 | QualType ElementType); |
| 95 | llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF, |
| 96 | llvm::Value *allocPtr, |
| 97 | CharUnits cookieSize); |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
John McCall | e2b45e2 | 2012-05-01 05:23:51 +0000 | [diff] [blame] | 102 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr, |
| 103 | QualType elementType) { |
| 104 | // Microsoft seems to completely ignore the possibility of a |
| 105 | // two-argument usual deallocation function. |
| 106 | return elementType.isDestructedType(); |
| 107 | } |
| 108 | |
| 109 | bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) { |
| 110 | // Microsoft seems to completely ignore the possibility of a |
| 111 | // two-argument usual deallocation function. |
| 112 | return expr->getAllocatedType().isDestructedType(); |
| 113 | } |
| 114 | |
| 115 | CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) { |
| 116 | // The array cookie is always a size_t; we then pad that out to the |
| 117 | // alignment of the element type. |
| 118 | ASTContext &Ctx = getContext(); |
| 119 | return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()), |
| 120 | Ctx.getTypeAlignInChars(type)); |
| 121 | } |
| 122 | |
| 123 | llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF, |
| 124 | llvm::Value *allocPtr, |
| 125 | CharUnits cookieSize) { |
| 126 | unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace(); |
| 127 | llvm::Value *numElementsPtr = |
| 128 | CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS)); |
| 129 | return CGF.Builder.CreateLoad(numElementsPtr); |
| 130 | } |
| 131 | |
| 132 | llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF, |
| 133 | llvm::Value *newPtr, |
| 134 | llvm::Value *numElements, |
| 135 | const CXXNewExpr *expr, |
| 136 | QualType elementType) { |
| 137 | assert(requiresArrayCookie(expr)); |
| 138 | |
| 139 | // The size of the cookie. |
| 140 | CharUnits cookieSize = getArrayCookieSizeImpl(elementType); |
| 141 | |
| 142 | // Compute an offset to the cookie. |
| 143 | llvm::Value *cookiePtr = newPtr; |
| 144 | |
| 145 | // Write the number of elements into the appropriate slot. |
| 146 | unsigned AS = cast<llvm::PointerType>(newPtr->getType())->getAddressSpace(); |
| 147 | llvm::Value *numElementsPtr |
| 148 | = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS)); |
| 149 | CGF.Builder.CreateStore(numElements, numElementsPtr); |
| 150 | |
| 151 | // Finally, compute a pointer to the actual data buffer by skipping |
| 152 | // over the cookie completely. |
| 153 | return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr, |
| 154 | cookieSize.getQuantity()); |
| 155 | } |
| 156 | |
John McCall | 20bb175 | 2012-05-01 06:13:13 +0000 | [diff] [blame] | 157 | void MicrosoftCXXABI::EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, |
| 158 | llvm::GlobalVariable *DeclPtr, |
| 159 | bool PerformInit) { |
| 160 | // FIXME: this code was only tested for global initialization. |
| 161 | // Not sure whether we want thread-safe static local variables as VS |
| 162 | // doesn't make them thread-safe. |
| 163 | |
| 164 | // Emit the initializer and add a global destructor if appropriate. |
| 165 | CGF.EmitCXXGlobalVarDeclInit(D, DeclPtr, PerformInit); |
| 166 | } |
| 167 | |
Charles Davis | 071cc7d | 2010-08-16 03:33:14 +0000 | [diff] [blame] | 168 | CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) { |
Charles Davis | c392664 | 2010-06-09 23:25:41 +0000 | [diff] [blame] | 169 | return new MicrosoftCXXABI(CGM); |
| 170 | } |
| 171 | |