blob: f99bd8b90b78563eac15ee8e7448870c1b5c69ae [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000010// This file implements the IdentifierInfo, IdentifierVisitor, and
Chris Lattner91cbf112006-07-03 04:28:52 +000011// IdentifierTable interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Lex/IdentifierTable.h"
16#include "clang/Lex/MacroInfo.h"
Chris Lattner25e0d542006-10-18 06:07:05 +000017#include "clang/Basic/LangOptions.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000018#include <iostream>
19using namespace llvm;
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000023// IdentifierInfo Implementation
Chris Lattner22eb9722006-06-18 05:43:12 +000024//===----------------------------------------------------------------------===//
25
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000026void IdentifierInfo::Destroy() {
Chris Lattner22eb9722006-06-18 05:43:12 +000027 delete Macro;
28}
29
Chris Lattner91cbf112006-07-03 04:28:52 +000030//===----------------------------------------------------------------------===//
31// IdentifierVisitor Implementation
32//===----------------------------------------------------------------------===//
33
34IdentifierVisitor::~IdentifierVisitor() {
35}
Chris Lattner22eb9722006-06-18 05:43:12 +000036
37//===----------------------------------------------------------------------===//
38// Memory Allocation Support
39//===----------------------------------------------------------------------===//
40
41/// The identifier table has a very simple memory allocation pattern: it just
42/// keeps allocating identifiers, then never frees them unless it frees them
43/// all. As such, we use a simple bump-pointer memory allocator to make
44/// allocation speedy. Shark showed that malloc was 27% of the time spent in
45/// IdentifierTable::getIdentifier with malloc, and takes a 4.3% time with this.
46#define USE_ALLOCATOR 1
47#if USE_ALLOCATOR
48
49namespace {
50class MemRegion {
51 unsigned RegionSize;
52 MemRegion *Next;
53 char *NextPtr;
54public:
55 void Init(unsigned size, MemRegion *next) {
56 RegionSize = size;
57 Next = next;
58 NextPtr = (char*)(this+1);
59
60 // FIXME: uses GCC extension.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000061 unsigned Alignment = __alignof__(IdentifierInfo);
Chris Lattner22eb9722006-06-18 05:43:12 +000062 NextPtr = (char*)((intptr_t)(NextPtr+Alignment-1) &
63 ~(intptr_t)(Alignment-1));
64 }
65
66 const MemRegion *getNext() const { return Next; }
67 unsigned getNumBytesAllocated() const {
68 return NextPtr-(const char*)this;
69 }
70
71 /// Allocate - Allocate and return at least the specified number of bytes.
72 ///
73 void *Allocate(unsigned AllocSize, MemRegion **RegPtr) {
74 // FIXME: uses GCC extension.
Chris Lattnerc79f6fb2006-07-04 17:53:21 +000075 unsigned Alignment = __alignof__(IdentifierInfo);
Chris Lattner22eb9722006-06-18 05:43:12 +000076 // Round size up to an even multiple of the alignment.
77 AllocSize = (AllocSize+Alignment-1) & ~(Alignment-1);
78
79 // If there is space in this region for the identifier, return it.
80 if (unsigned(NextPtr+AllocSize-(char*)this) <= RegionSize) {
81 void *Result = NextPtr;
82 NextPtr += AllocSize;
83 return Result;
84 }
85
86 // Otherwise, we have to allocate a new chunk. Create one twice as big as
87 // this one.
88 MemRegion *NewRegion = (MemRegion *)malloc(RegionSize*2);
89 NewRegion->Init(RegionSize*2, this);
90
91 // Update the current "first region" pointer to point to the new region.
92 *RegPtr = NewRegion;
93
94 // Try allocating from it now.
95 return NewRegion->Allocate(AllocSize, RegPtr);
96 }
97
98 /// Deallocate - Release all memory for this region to the system.
99 ///
100 void Deallocate() {
101 MemRegion *next = Next;
102 free(this);
103 if (next)
104 next->Deallocate();
105 }
106};
107}
108
109#endif
110
111//===----------------------------------------------------------------------===//
112// IdentifierTable Implementation
113//===----------------------------------------------------------------------===//
114
115
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000116/// IdentifierBucket - The hash table consists of an array of these. If Info is
117/// non-null, this is an extant entry, otherwise, it is a hole.
Chris Lattner22eb9722006-06-18 05:43:12 +0000118struct IdentifierBucket {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000119 /// FullHashValue - This remembers the full hash value of the identifier for
120 /// easy scanning.
121 unsigned FullHashValue;
Chris Lattner22eb9722006-06-18 05:43:12 +0000122
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000123 /// Info - This is a pointer to the actual identifier info object.
124 IdentifierInfo *Info;
Chris Lattner22eb9722006-06-18 05:43:12 +0000125};
126
Chris Lattner25e0d542006-10-18 06:07:05 +0000127IdentifierTable::IdentifierTable(const LangOptions &LangOpts) {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000128 HashTableSize = 8192; // Start with space for 8K identifiers.
129 IdentifierBucket *TableArray = new IdentifierBucket[HashTableSize]();
130 memset(TableArray, 0, HashTableSize*sizeof(IdentifierBucket));
131
Chris Lattner22eb9722006-06-18 05:43:12 +0000132 TheTable = TableArray;
133 NumIdentifiers = 0;
134#if USE_ALLOCATOR
135 TheMemory = malloc(8*4096);
136 ((MemRegion*)TheMemory)->Init(8*4096, 0);
137#endif
138
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000139 // Populate the identifier table with info about keywords for the current
140 // language.
Chris Lattner25e0d542006-10-18 06:07:05 +0000141 AddKeywords(LangOpts);
Chris Lattner22eb9722006-06-18 05:43:12 +0000142}
143
144IdentifierTable::~IdentifierTable() {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000145 IdentifierBucket *TableArray = (IdentifierBucket*)TheTable;
146 for (unsigned i = 0, e = HashTableSize; i != e; ++i) {
147 if (IdentifierInfo *Id = TableArray[i].Info) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000148 // Free memory referenced by the identifier (e.g. macro info).
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000149 Id->Destroy();
Chris Lattner22eb9722006-06-18 05:43:12 +0000150
Chris Lattner22eb9722006-06-18 05:43:12 +0000151#if !USE_ALLOCATOR
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000152 // Free the memory for the identifier itself.
Chris Lattner22eb9722006-06-18 05:43:12 +0000153 free(Id);
154#endif
Chris Lattner22eb9722006-06-18 05:43:12 +0000155 }
156 }
157#if USE_ALLOCATOR
158 ((MemRegion*)TheMemory)->Deallocate();
159#endif
160 delete [] TableArray;
161}
162
163/// HashString - Compute a hash code for the specified string.
164///
165static unsigned HashString(const char *Start, const char *End) {
166 unsigned int Result = 0;
167 // Perl hash function.
168 while (Start != End)
169 Result = Result * 33 + *Start++;
170 Result = Result + (Result >> 5);
171 return Result;
172}
173
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000174IdentifierInfo &IdentifierTable::get(const char *NameStart,
Chris Lattner0e1cf1f2006-07-04 18:53:52 +0000175 const char *NameEnd) {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000176 IdentifierBucket *TableArray = (IdentifierBucket*)TheTable;
Chris Lattner22eb9722006-06-18 05:43:12 +0000177
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000178 unsigned HTSize = HashTableSize;
179 unsigned FullHashValue = HashString(NameStart, NameEnd);
180 unsigned BucketNo = FullHashValue & (HTSize-1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000181 unsigned Length = NameEnd-NameStart;
182
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000183 unsigned ProbeAmt = 1;
184 while (1) {
185 IdentifierBucket &Bucket = TableArray[BucketNo];
186 IdentifierInfo *BucketII = Bucket.Info;
187 // If we found an empty bucket, this identifier isn't in the table yet.
188 if (BucketII == 0) break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000189
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000190 // If the full hash value matches, check deeply for a match. The common
191 // case here is that we are only looking at the buckets (for identifier info
192 // being non-null and for the full hash value) not at the identifiers. This
193 // is important for cache locality.
194 if (Bucket.FullHashValue == FullHashValue &&
195 BucketII->getNameLength() == Length &&
196 memcmp(BucketII->getName(), NameStart, Length) == 0)
197 // We found a match!
198 return *BucketII;
199
200 // Okay, we didn't find the identifier. Probe to the next bucket.
201 BucketNo = (BucketNo+ProbeAmt) & (HashTableSize-1);
202
203 // Use quadratic probing, it has fewer clumping artifacts than linear
204 // probing and has good cache behavior in the common case.
205 ++ProbeAmt;
206 }
207
208 // Okay, the identifier doesn't already exist, and BucketNo is the bucket to
209 // fill in. Allocate a new identifier with space for the null-terminated
210 // string at the end.
211 unsigned AllocSize = sizeof(IdentifierInfo)+Length+1;
Chris Lattner22eb9722006-06-18 05:43:12 +0000212#if USE_ALLOCATOR
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000213 IdentifierInfo *Identifier = (IdentifierInfo*)
Chris Lattner22eb9722006-06-18 05:43:12 +0000214 ((MemRegion*)TheMemory)->Allocate(AllocSize, (MemRegion**)&TheMemory);
215#else
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000216 IdentifierInfo *Identifier = (IdentifierInfo*)malloc(AllocSize);
Chris Lattner22eb9722006-06-18 05:43:12 +0000217#endif
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000218 Identifier->NameLen = Length;
219 Identifier->Macro = 0;
220 Identifier->TokenID = tok::identifier;
221 Identifier->PPID = tok::pp_not_keyword;
222 Identifier->ObjCID = tok::objc_not_keyword;
223 Identifier->IsExtension = false;
224 Identifier->IsPoisoned = false;
225 Identifier->IsOtherTargetMacro = false;
226 Identifier->FETokenInfo = 0;
227 ++NumIdentifiers;
Chris Lattner22eb9722006-06-18 05:43:12 +0000228
229 // Copy the string information.
230 char *StrBuffer = (char*)(Identifier+1);
231 memcpy(StrBuffer, NameStart, Length);
232 StrBuffer[Length] = 0; // Null terminate string.
233
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000234 // Fill in the bucket for the hash table.
235 TableArray[BucketNo].Info = Identifier;
236 TableArray[BucketNo].FullHashValue = FullHashValue;
237
238 // If the hash table is now more than 3/4 full, rehash into a larger table.
239 if (NumIdentifiers > HashTableSize*3/4)
240 RehashTable();
241
242 return *Identifier;
Chris Lattner22eb9722006-06-18 05:43:12 +0000243}
244
Chris Lattnerc79f6fb2006-07-04 17:53:21 +0000245IdentifierInfo &IdentifierTable::get(const std::string &Name) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000246 // Don't use c_str() here: no need to be null terminated.
247 const char *NameBytes = &Name[0];
248 unsigned Size = Name.size();
249 return get(NameBytes, NameBytes+Size);
250}
251
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000252void IdentifierTable::RehashTable() {
253 unsigned NewSize = HashTableSize*2;
254 IdentifierBucket *NewTableArray = new IdentifierBucket[NewSize]();
255 memset(NewTableArray, 0, NewSize*sizeof(IdentifierBucket));
256
257 // Rehash all the identifier into their new buckets. Luckily we already have
258 // the hash values available :).
259 IdentifierBucket *CurTable = (IdentifierBucket *)TheTable;
260 for (IdentifierBucket *IB = CurTable, *E = CurTable+HashTableSize;
261 IB != E; ++IB) {
262 if (IB->Info) {
263 // Fast case, bucket available.
264 unsigned FullHash = IB->FullHashValue;
265 unsigned NewBucket = FullHash & (NewSize-1);
266 if (NewTableArray[NewBucket].Info == 0) {
267 NewTableArray[FullHash & (NewSize-1)].Info = IB->Info;
268 NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
269 continue;
270 }
271
272 unsigned ProbeSize = 1;
273 do {
274 NewBucket = (NewBucket + ProbeSize++) & (NewSize-1);
275 } while (NewTableArray[NewBucket].Info);
276
277 // Finally found a slot. Fill it in.
278 NewTableArray[FullHash & (NewSize-1)].Info = IB->Info;
279 NewTableArray[FullHash & (NewSize-1)].FullHashValue = FullHash;
280 }
281 }
282
283 delete[] CurTable;
284
285 TheTable = NewTableArray;
286 HashTableSize = NewSize;
287}
288
289
Chris Lattner91cbf112006-07-03 04:28:52 +0000290/// VisitIdentifiers - This method walks through all of the identifiers,
291/// invoking IV->VisitIdentifier for each of them.
292void IdentifierTable::VisitIdentifiers(const IdentifierVisitor &IV) {
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000293 IdentifierBucket *TableArray = (IdentifierBucket*)TheTable;
294 for (unsigned i = 0, e = HashTableSize; i != e; ++i) {
295 if (IdentifierInfo *Id = TableArray[i].Info)
296 IV.VisitIdentifier(*Id);
Chris Lattner91cbf112006-07-03 04:28:52 +0000297 }
298}
Chris Lattner22eb9722006-06-18 05:43:12 +0000299
Chris Lattner25e0d542006-10-18 06:07:05 +0000300//===----------------------------------------------------------------------===//
301// Language Keyword Implementation
302//===----------------------------------------------------------------------===//
303
304/// AddKeyword - This method is used to associate a token ID with specific
305/// identifiers because they are language keywords. This causes the lexer to
306/// automatically map matching identifiers to specialized token codes.
307///
308/// The C90/C99/CPP flags are set to 0 if the token should be enabled in the
309/// specified langauge, set to 1 if it is an extension in the specified
310/// language, and set to 2 if disabled in the specified language.
311static void AddKeyword(const std::string &Keyword, tok::TokenKind TokenCode,
Chris Lattnera4271e42006-10-20 06:13:26 +0000312 int C90, int C99, int CXX,
Chris Lattner25e0d542006-10-18 06:07:05 +0000313 const LangOptions &LangOpts, IdentifierTable &Table) {
Chris Lattnera4271e42006-10-20 06:13:26 +0000314 int Flags = LangOpts.CPlusPlus ? CXX : (LangOpts.C99 ? C99 : C90);
Chris Lattner25e0d542006-10-18 06:07:05 +0000315
316 // Don't add this keyword if disabled in this language or if an extension
317 // and extensions are disabled.
318 if (Flags + LangOpts.NoExtensions >= 2) return;
319
320 const char *Str = &Keyword[0];
321 IdentifierInfo &Info = Table.get(Str, Str+Keyword.size());
322 Info.setTokenID(TokenCode);
323 Info.setIsExtensionToken(Flags == 1);
324}
325
326/// AddPPKeyword - Register a preprocessor keyword like "define" "undef" or
327/// "elif".
328static void AddPPKeyword(tok::PPKeywordKind PPID,
329 const char *Name, unsigned NameLen,
330 IdentifierTable &Table) {
331 Table.get(Name, Name+NameLen).setPPKeywordID(PPID);
332}
333
334/// AddObjCKeyword - Register an Objective-C @keyword like "class" "selector" or
335/// "property".
336static void AddObjCKeyword(tok::ObjCKeywordKind ObjCID,
337 const char *Name, unsigned NameLen,
338 IdentifierTable &Table) {
339 Table.get(Name, Name+NameLen).setObjCKeywordID(ObjCID);
340}
341
342/// AddKeywords - Add all keywords to the symbol table.
343///
344void IdentifierTable::AddKeywords(const LangOptions &LangOpts) {
345 enum {
346 C90Shift = 0,
347 EXTC90 = 1 << C90Shift,
348 NOTC90 = 2 << C90Shift,
349 C99Shift = 2,
350 EXTC99 = 1 << C99Shift,
351 NOTC99 = 2 << C99Shift,
352 CPPShift = 4,
353 EXTCPP = 1 << CPPShift,
354 NOTCPP = 2 << CPPShift,
355 Mask = 3
356 };
357
358 // Add keywords and tokens for the current language.
359#define KEYWORD(NAME, FLAGS) \
360 AddKeyword(#NAME, tok::kw_ ## NAME, \
361 ((FLAGS) >> C90Shift) & Mask, \
362 ((FLAGS) >> C99Shift) & Mask, \
363 ((FLAGS) >> CPPShift) & Mask, LangOpts, *this);
364#define ALIAS(NAME, TOK) \
365 AddKeyword(NAME, tok::kw_ ## TOK, 0, 0, 0, LangOpts, *this);
366#define PPKEYWORD(NAME) \
367 AddPPKeyword(tok::pp_##NAME, #NAME, strlen(#NAME), *this);
368#define OBJC1_AT_KEYWORD(NAME) \
369 if (LangOpts.ObjC1) \
370 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
371#define OBJC2_AT_KEYWORD(NAME) \
372 if (LangOpts.ObjC2) \
373 AddObjCKeyword(tok::objc_##NAME, #NAME, strlen(#NAME), *this);
374#include "clang/Basic/TokenKinds.def"
375}
376
377
378//===----------------------------------------------------------------------===//
379// Stats Implementation
380//===----------------------------------------------------------------------===//
381
Chris Lattner22eb9722006-06-18 05:43:12 +0000382/// PrintStats - Print statistics about how well the identifier table is doing
383/// at hashing identifiers.
384void IdentifierTable::PrintStats() const {
Chris Lattner22eb9722006-06-18 05:43:12 +0000385 unsigned NumEmptyBuckets = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000386 unsigned AverageIdentifierSize = 0;
387 unsigned MaxIdentifierLength = 0;
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000388 unsigned NumProbed = 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000389
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000390 IdentifierBucket *TableArray = (IdentifierBucket*)TheTable;
391 for (unsigned i = 0, e = HashTableSize; i != e; ++i) {
392 if (TableArray[i].Info == 0) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000393 ++NumEmptyBuckets;
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000394 continue;
395 }
396 IdentifierInfo *Id = TableArray[i].Info;
397
398 AverageIdentifierSize += Id->getNameLength();
399 if (MaxIdentifierLength < Id->getNameLength())
400 MaxIdentifierLength = Id->getNameLength();
Chris Lattner22eb9722006-06-18 05:43:12 +0000401
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000402 // Count the number of times something was probed.
403 if ((TableArray[i].FullHashValue & (e-1)) != i)
404 ++NumProbed;
405
406 // TODO: Figure out maximum times an identifier had to probe for -stats.
Chris Lattner22eb9722006-06-18 05:43:12 +0000407 }
408
409 std::cerr << "\n*** Identifier Table Stats:\n";
410 std::cerr << "# Identifiers: " << NumIdentifiers << "\n";
411 std::cerr << "# Empty Buckets: " << NumEmptyBuckets << "\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000412 std::cerr << "Hash density (#identifiers per bucket): "
Chris Lattnerf2e3ac32006-10-27 03:59:10 +0000413 << NumIdentifiers/(double)HashTableSize << "\n";
414 std::cerr << "Num probed identifiers: " << NumProbed << " ("
415 << NumProbed*100.0/NumIdentifiers << "%)\n";
Chris Lattner22eb9722006-06-18 05:43:12 +0000416 std::cerr << "Ave identifier length: "
417 << (AverageIdentifierSize/(double)NumIdentifiers) << "\n";
418 std::cerr << "Max identifier length: " << MaxIdentifierLength << "\n";
419
420 // Compute statistics about the memory allocated for identifiers.
421#if USE_ALLOCATOR
422 unsigned BytesUsed = 0;
423 unsigned NumRegions = 0;
424 const MemRegion *R = (MemRegion*)TheMemory;
425 for (; R; R = R->getNext(), ++NumRegions) {
426 BytesUsed += R->getNumBytesAllocated();
427 }
428 std::cerr << "\nNumber of memory regions: " << NumRegions << "\n";
429 std::cerr << "Bytes allocated for identifiers: " << BytesUsed << "\n";
430#endif
431}
432
433