1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
|
//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This provides Objective-C code generation targetting the Apple runtime.
//
//===----------------------------------------------------------------------===//
#include "CGObjCRuntime.h"
#include "CodeGenModule.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/Basic/LangOptions.h"
#include "llvm/Module.h"
#include "llvm/Support/IRBuilder.h"
using namespace clang;
namespace {
/// ObjCTypesHelper - Helper class that encapsulates lazy
/// construction of varies types used during ObjC generation.
class ObjCTypesHelper {
private:
CodeGen::CodeGenModule &CGM;
const llvm::StructType *CFStringType;
llvm::Constant *CFConstantStringClassReference;
public:
const llvm::Type *LongTy;
public:
ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
~ObjCTypesHelper();
llvm::Constant *getCFConstantStringClassReference();
const llvm::StructType *getCFStringType();
};
class CGObjCMac : public CodeGen::CGObjCRuntime {
private:
CodeGen::CodeGenModule &CGM;
ObjCTypesHelper ObjCTypes;
/// ObjCABI - FIXME: Not sure yet.
unsigned ObjCABI;
/// UsedGlobals - list of globals to pack into the llvm.used metadata
/// to prevent them from being clobbered.
std::vector<llvm::GlobalValue*> UsedGlobals;
/// EmitImageInfo - Emit the image info marker used to encode some module
/// level information.
void EmitImageInfo();
/// FinishModule - Write out global data structures at the end of
/// processing a translation unit.
void FinishModule();
public:
CGObjCMac(CodeGen::CodeGenModule &cgm);
virtual llvm::Constant *GenerateConstantString(const std::string &String);
virtual llvm::Value *GenerateMessageSend(llvm::IRBuilder<> &Builder,
const llvm::Type *ReturnTy,
llvm::Value *Sender,
llvm::Value *Receiver,
Selector Sel,
llvm::Value** ArgV,
unsigned ArgC);
virtual llvm::Value *GenerateMessageSendSuper(llvm::IRBuilder<> &Builder,
const llvm::Type *ReturnTy,
llvm::Value *Sender,
const char *SuperClassName,
llvm::Value *Receiver,
Selector Sel,
llvm::Value** ArgV,
unsigned ArgC);
virtual llvm::Value *LookupClass(llvm::IRBuilder<> &Builder,
llvm::Value *ClassName);
virtual llvm::Value *GetSelector(llvm::IRBuilder<> &Builder, Selector Sel);
virtual llvm::Function *MethodPreamble(const std::string &ClassName,
const std::string &CategoryName,
const std::string &MethodName,
const llvm::Type *ReturnTy,
const llvm::Type *SelfTy,
const llvm::Type **ArgTy,
unsigned ArgC,
bool isClassMethod,
bool isVarArg);
virtual void GenerateCategory(const char *ClassName, const char *CategoryName,
const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
const llvm::SmallVectorImpl<std::string> &Protocols);
virtual void GenerateClass(
const char *ClassName,
const char *SuperClassName,
const int instanceSize,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
const llvm::SmallVectorImpl<std::string> &Protocols);
virtual llvm::Value *GenerateProtocolRef(llvm::IRBuilder<> &Builder,
const char *ProtocolName);
virtual void GenerateProtocol(const char *ProtocolName,
const llvm::SmallVectorImpl<std::string> &Protocols,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes);
virtual llvm::Function *ModuleInitFunction();
};
} // end anonymous namespace
/* *** Helper Functions *** */
/// getConstantGEP() - Help routine to construct simple GEPs.
static llvm::Constant *getConstantGEP(llvm::Constant *C,
unsigned idx0,
unsigned idx1) {
llvm::Value *Idxs[] = {
llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
};
return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
}
/* *** CGObjCMac Public Interface *** */
CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm)
: CGM(cgm),
ObjCTypes(cgm),
ObjCABI(1)
{
// FIXME: How does this get set in GCC? And what does it even mean?
if (ObjCTypes.LongTy != CGM.getTypes().ConvertType(CGM.getContext().IntTy))
ObjCABI = 2;
EmitImageInfo();
}
// This has to perform the lookup every time, since posing and related
// techniques can modify the name -> class mapping.
llvm::Value *CGObjCMac::LookupClass(llvm::IRBuilder<> &Builder,
llvm::Value *ClassName) {
assert(0 && "Cannot lookup classes on Mac runtime.");
return 0;
}
/// GetSelector - Return the pointer to the unique'd string for this selector.
llvm::Value *CGObjCMac::GetSelector(llvm::IRBuilder<> &Builder, Selector Sel) {
assert(0 && "Cannot get selector on Mac runtime.");
return 0;
}
/// Generate a constant CFString object.
/*
struct __builtin_CFString {
const int *isa; // point to __CFConstantStringClassReference
int flags;
const char *str;
long length;
};
*/
llvm::Constant *CGObjCMac::GenerateConstantString(const std::string &String) {
// FIXME: I have no idea what this constant is (it is a magic
// constant in GCC as well). Most likely the encoding of the string
// and at least one part of it relates to UTF-16. Is this just the
// code for UTF-8? Where is this handled for us?
// See: <rdr://2996215>
unsigned flags = 0x07c8;
// FIXME: Use some machinery to unique this. We can't reuse the CGM
// one since we put them in a different section.
llvm::Constant *StringC = llvm::ConstantArray::get(String);
llvm::Constant *StringGV =
new llvm::GlobalVariable(StringC->getType(), true,
llvm::GlobalValue::InternalLinkage,
StringC, ".str", &CGM.getModule());
llvm::Constant *Values[4] = {
ObjCTypes.getCFConstantStringClassReference(),
llvm::ConstantInt::get(llvm::Type::Int32Ty, flags),
getConstantGEP(StringGV, 0, 0), // Decay array -> ptr
llvm::ConstantInt::get(ObjCTypes.LongTy, String.size())
};
llvm::Constant *CFStringC =
llvm::ConstantStruct::get(ObjCTypes.getCFStringType(),
std::vector<llvm::Constant*>(Values, Values+4));
llvm::GlobalVariable *CFStringGV =
new llvm::GlobalVariable(CFStringC->getType(), true,
llvm::GlobalValue::InternalLinkage,
CFStringC, "",
&CGM.getModule());
CFStringGV->setSection("__DATA, __cfstring");
return CFStringGV;
}
/// Generates a message send where the super is the receiver. This is
/// a message send to self with special delivery semantics indicating
/// which class's method should be called.
llvm::Value *CGObjCMac::GenerateMessageSendSuper(llvm::IRBuilder<> &Builder,
const llvm::Type *ReturnTy,
llvm::Value *Sender,
const char *SuperClassName,
llvm::Value *Receiver,
Selector Sel,
llvm::Value** ArgV,
unsigned ArgC) {
assert(0 && "Cannot generate message send to super for Mac runtime.");
return 0;
}
/// Generate code for a message send expression.
llvm::Value *CGObjCMac::GenerateMessageSend(llvm::IRBuilder<> &Builder,
const llvm::Type *ReturnTy,
llvm::Value *Sender,
llvm::Value *Receiver,
Selector Sel,
llvm::Value** ArgV,
unsigned ArgC) {
assert(0 && "Cannot generate message send for Mac runtime.");
return 0;
}
llvm::Value *CGObjCMac::GenerateProtocolRef(llvm::IRBuilder<> &Builder,
const char *ProtocolName) {
assert(0 && "Cannot get protocol reference on Mac runtime.");
return 0;
}
void CGObjCMac::GenerateProtocol(const char *ProtocolName,
const llvm::SmallVectorImpl<std::string> &Protocols,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodNames,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodNames,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes) {
assert(0 && "Cannot generate protocol for Mac runtime.");
}
void CGObjCMac::GenerateCategory(
const char *ClassName,
const char *CategoryName,
const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
const llvm::SmallVectorImpl<std::string> &Protocols) {
assert(0 && "Cannot generate category for Mac runtime.");
}
void CGObjCMac::GenerateClass(
const char *ClassName,
const char *SuperClassName,
const int instanceSize,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarNames,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarTypes,
const llvm::SmallVectorImpl<llvm::Constant *> &IvarOffsets,
const llvm::SmallVectorImpl<Selector> &InstanceMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &InstanceMethodTypes,
const llvm::SmallVectorImpl<Selector> &ClassMethodSels,
const llvm::SmallVectorImpl<llvm::Constant *> &ClassMethodTypes,
const llvm::SmallVectorImpl<std::string> &Protocols) {
assert(0 && "Cannot generate class for Mac runtime.");
}
llvm::Function *CGObjCMac::ModuleInitFunction() {
// Abuse this interface function as a place to finalize.
FinishModule();
return NULL;
}
llvm::Function *CGObjCMac::MethodPreamble(
const std::string &ClassName,
const std::string &CategoryName,
const std::string &MethodName,
const llvm::Type *ReturnTy,
const llvm::Type *SelfTy,
const llvm::Type **ArgTy,
unsigned ArgC,
bool isClassMethod,
bool isVarArg) {
assert(0 && "Cannot generate method preamble for Mac runtime.");
return 0;
}
/* *** Private Interface *** */
/// EmitImageInfo - Emit the image info marker used to encode some module
/// level information.
///
/// See: <rdr://4810609&4810587&4810587>
/// struct IMAGE_INFO {
/// unsigned version;
/// unsigned flags;
/// };
enum ImageInfoFlags {
eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what this implies
eImageInfo_GarbageCollected = (1 << 1),
eImageInfo_GCOnly = (1 << 2)
};
void CGObjCMac::EmitImageInfo() {
unsigned version = 0; // Version is unused?
unsigned flags = 0;
// FIXME: Fix and continue?
if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
flags |= eImageInfo_GarbageCollected;
if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
flags |= eImageInfo_GCOnly;
// Emitted as int[2];
llvm::Constant *values[2] = {
llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
};
llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
llvm::GlobalValue *GV =
new llvm::GlobalVariable(AT, true,
llvm::GlobalValue::InternalLinkage,
llvm::ConstantArray::get(AT, values, 2),
"\01L_OBJC_IMAGE_INFO",
&CGM.getModule());
if (ObjCABI == 1) {
GV->setSection("__OBJC, __image_info,regular");
} else {
GV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
}
UsedGlobals.push_back(GV);
}
void CGObjCMac::FinishModule() {
std::vector<llvm::Constant*> Used;
llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
for (std::vector<llvm::GlobalValue*>::iterator i = UsedGlobals.begin(),
e = UsedGlobals.end(); i != e; ++i) {
Used.push_back(llvm::ConstantExpr::getBitCast(*i, I8Ptr));
}
llvm::ArrayType *AT = llvm::ArrayType::get(I8Ptr, Used.size());
llvm::GlobalValue *GV =
new llvm::GlobalVariable(AT, false,
llvm::GlobalValue::AppendingLinkage,
llvm::ConstantArray::get(AT, Used),
"llvm.used",
&CGM.getModule());
GV->setSection("llvm.metadata");
}
/* *** */
ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
: CGM(cgm),
CFStringType(0),
CFConstantStringClassReference(0),
LongTy(CGM.getTypes().ConvertType(CGM.getContext().LongTy))
{
}
ObjCTypesHelper::~ObjCTypesHelper() {
}
const llvm::StructType *ObjCTypesHelper::getCFStringType() {
if (!CFStringType) {
CFStringType =
llvm::StructType::get(llvm::PointerType::getUnqual(llvm::Type::Int32Ty),
llvm::Type::Int32Ty,
llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
LongTy,
NULL);
CGM.getModule().addTypeName("struct.__builtin_CFString", CFStringType);
}
return CFStringType;
}
llvm::Constant *ObjCTypesHelper::getCFConstantStringClassReference() {
if (!CFConstantStringClassReference) {
llvm::GlobalValue *GV =
new llvm::GlobalVariable(llvm::ArrayType::get(llvm::Type::Int32Ty, 0),
false,
llvm::GlobalValue::ExternalLinkage,
0, "__CFConstantStringClassReference",
&CGM.getModule());
// Decay to pointer.
CFConstantStringClassReference = getConstantGEP(GV, 0, 0);
}
return CFConstantStringClassReference;
}
/* *** */
CodeGen::CGObjCRuntime *CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM){
return new CGObjCMac(CGM);
}
|