blob: a88c6ba5f101cb18be7c40b7388efe918872230d [file] [log] [blame]
Charles Davis74ce8592010-06-09 23:25:41 +00001//===--- 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 Lattner57540c52011-04-15 05:22:18 +000010// This provides C++ code generation targeting the Microsoft Visual C++ ABI.
Charles Davis74ce8592010-06-09 23:25:41 +000011// 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 Davis74ce8592010-06-09 23:25:41 +000019#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
Charles Davis74ce8592010-06-09 23:25:41 +000021
22using namespace clang;
23using namespace CodeGen;
24
25namespace {
26
Charles Davis53c59df2010-08-16 03:33:14 +000027class MicrosoftCXXABI : public CGCXXABI {
Charles Davis74ce8592010-06-09 23:25:41 +000028public:
Peter Collingbourne0ff0b372011-01-13 18:57:25 +000029 MicrosoftCXXABI(CodeGenModule &CGM) : CGCXXABI(CGM) {}
John McCall5d865c322010-08-31 07:33:07 +000030
31 void BuildConstructorSignature(const CXXConstructorDecl *Ctor,
32 CXXCtorType Type,
33 CanQualType &ResTy,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000034 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall5d865c322010-08-31 07:33:07 +000035 // '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 Lattner0e62c1c2011-07-23 10:55:15 +000042 SmallVectorImpl<CanQualType> &ArgTys) {
John McCall5d865c322010-08-31 07:33:07 +000043 // '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 McCall29036752011-01-27 02:46:02 +000058
59 // ==== Notes on array cookies =========
60 //
61 // MSVC seems to only use cookies when the class has a destructor; a
62 // two-argument usual array deallocation function isn't sufficient.
63 //
64 // For example, this code prints "100" and "1":
65 // struct A {
66 // char x;
67 // void *operator new[](size_t sz) {
68 // printf("%u\n", sz);
69 // return malloc(sz);
70 // }
71 // void operator delete[](void *p, size_t sz) {
72 // printf("%u\n", sz);
73 // free(p);
74 // }
75 // };
76 // int main() {
77 // A *p = new A[100];
78 // delete[] p;
79 // }
80 // Whereas it prints "104" and "104" if you give A a destructor.
John McCallb91cd662012-05-01 05:23:51 +000081
82 bool requiresArrayCookie(const CXXDeleteExpr *expr, QualType elementType);
83 bool requiresArrayCookie(const CXXNewExpr *expr);
84 CharUnits getArrayCookieSizeImpl(QualType type);
85 llvm::Value *InitializeArrayCookie(CodeGenFunction &CGF,
86 llvm::Value *NewPtr,
87 llvm::Value *NumElements,
88 const CXXNewExpr *expr,
89 QualType ElementType);
90 llvm::Value *readArrayCookieImpl(CodeGenFunction &CGF,
91 llvm::Value *allocPtr,
92 CharUnits cookieSize);
Charles Davis74ce8592010-06-09 23:25:41 +000093};
94
95}
96
John McCallb91cd662012-05-01 05:23:51 +000097bool MicrosoftCXXABI::requiresArrayCookie(const CXXDeleteExpr *expr,
98 QualType elementType) {
99 // Microsoft seems to completely ignore the possibility of a
100 // two-argument usual deallocation function.
101 return elementType.isDestructedType();
102}
103
104bool MicrosoftCXXABI::requiresArrayCookie(const CXXNewExpr *expr) {
105 // Microsoft seems to completely ignore the possibility of a
106 // two-argument usual deallocation function.
107 return expr->getAllocatedType().isDestructedType();
108}
109
110CharUnits MicrosoftCXXABI::getArrayCookieSizeImpl(QualType type) {
111 // The array cookie is always a size_t; we then pad that out to the
112 // alignment of the element type.
113 ASTContext &Ctx = getContext();
114 return std::max(Ctx.getTypeSizeInChars(Ctx.getSizeType()),
115 Ctx.getTypeAlignInChars(type));
116}
117
118llvm::Value *MicrosoftCXXABI::readArrayCookieImpl(CodeGenFunction &CGF,
119 llvm::Value *allocPtr,
120 CharUnits cookieSize) {
121 unsigned AS = cast<llvm::PointerType>(allocPtr->getType())->getAddressSpace();
122 llvm::Value *numElementsPtr =
123 CGF.Builder.CreateBitCast(allocPtr, CGF.SizeTy->getPointerTo(AS));
124 return CGF.Builder.CreateLoad(numElementsPtr);
125}
126
127llvm::Value* MicrosoftCXXABI::InitializeArrayCookie(CodeGenFunction &CGF,
128 llvm::Value *newPtr,
129 llvm::Value *numElements,
130 const CXXNewExpr *expr,
131 QualType elementType) {
132 assert(requiresArrayCookie(expr));
133
134 // The size of the cookie.
135 CharUnits cookieSize = getArrayCookieSizeImpl(elementType);
136
137 // Compute an offset to the cookie.
138 llvm::Value *cookiePtr = newPtr;
139
140 // Write the number of elements into the appropriate slot.
141 unsigned AS = cast<llvm::PointerType>(newPtr->getType())->getAddressSpace();
142 llvm::Value *numElementsPtr
143 = CGF.Builder.CreateBitCast(cookiePtr, CGF.SizeTy->getPointerTo(AS));
144 CGF.Builder.CreateStore(numElements, numElementsPtr);
145
146 // Finally, compute a pointer to the actual data buffer by skipping
147 // over the cookie completely.
148 return CGF.Builder.CreateConstInBoundsGEP1_64(newPtr,
149 cookieSize.getQuantity());
150}
151
Charles Davis53c59df2010-08-16 03:33:14 +0000152CGCXXABI *clang::CodeGen::CreateMicrosoftCXXABI(CodeGenModule &CGM) {
Charles Davis74ce8592010-06-09 23:25:41 +0000153 return new MicrosoftCXXABI(CGM);
154}
155