Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements various things for builtin functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Builtins.h" |
Chris Lattner | ef6b136 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 16 | #include "clang/Basic/LangOptions.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame^] | 17 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
| 21 | static const Builtin::Info BuiltinInfo[] = { |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 22 | { "not a builtin function", 0, 0, 0, ALL_LANGUAGES }, |
| 23 | #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, ALL_LANGUAGES }, |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 24 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER, BUILTIN_LANG) { #ID, TYPE, ATTRS, HEADER,\ |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 25 | BUILTIN_LANG }, |
Chris Lattner | 5abdec7 | 2009-06-14 01:05:48 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.def" |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 29 | const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { |
| 30 | if (ID < Builtin::FirstTSBuiltin) |
| 31 | return BuiltinInfo[ID]; |
| 32 | assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); |
| 33 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 36 | Builtin::Context::Context() { |
Chris Lattner | 4ef49c1 | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 37 | // Get the target specific builtins from the target. |
Chris Lattner | 651a221 | 2009-06-16 17:27:50 +0000 | [diff] [blame] | 38 | TSRecords = 0; |
| 39 | NumTSRecords = 0; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void Builtin::Context::InitializeTarget(const TargetInfo &Target) { |
| 43 | assert(NumTSRecords == 0 && "Already initialized target?"); |
| 44 | Target.getTargetBuiltins(TSRecords, NumTSRecords); |
Chris Lattner | 4ef49c1 | 2009-06-16 16:18:48 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 47 | /// InitializeBuiltins - Mark the identifiers for all the builtins with their |
| 48 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 49 | /// such. |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 50 | void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 51 | const LangOptions& LangOpts) { |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 52 | // Step #1: mark all target-independent builtins with their ID's. |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 53 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 54 | if (!LangOpts.NoBuiltin || !strchr(BuiltinInfo[i].Attributes, 'f')) { |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 55 | if (LangOpts.ObjC1 || |
| 56 | BuiltinInfo[i].builtin_lang != clang::OBJC_LANG) |
| 57 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
| 58 | } |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 59 | |
Douglas Gregor | f89771c | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 60 | // Step #2: Register target-specific builtins. |
Chris Lattner | 10a5b38 | 2007-01-29 05:24:35 +0000 | [diff] [blame] | 61 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 62 | if (!LangOpts.NoBuiltin || !strchr(TSRecords[i].Attributes, 'f')) |
Douglas Gregor | 69c7951 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 63 | Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | void |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 67 | Builtin::Context::GetBuiltinNames(SmallVectorImpl<const char *> &Names, |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 68 | bool NoBuiltins) { |
| 69 | // Final all target-independent names |
| 70 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 71 | if (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f')) |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 72 | Names.push_back(BuiltinInfo[i].Name); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 74 | // Find target-specific names. |
| 75 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
Eli Friedman | bb5c9ae | 2011-07-05 21:53:01 +0000 | [diff] [blame] | 76 | if (!NoBuiltins || !strchr(TSRecords[i].Attributes, 'f')) |
Douglas Gregor | 4621c6a | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 77 | Names.push_back(TSRecords[i].Name); |
| 78 | } |
| 79 | |
Douglas Gregor | 9246b68 | 2010-12-21 19:47:46 +0000 | [diff] [blame] | 80 | void Builtin::Context::ForgetBuiltin(unsigned ID, IdentifierTable &Table) { |
| 81 | Table.get(GetRecord(ID).Name).setBuiltinID(0); |
| 82 | } |
| 83 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | bool |
| 85 | Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 86 | bool &HasVAListArg) { |
Cedric Venet | d3c80de | 2009-02-14 16:15:20 +0000 | [diff] [blame] | 87 | const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP"); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 88 | if (!Printf) |
| 89 | return false; |
| 90 | |
| 91 | HasVAListArg = (*Printf == 'P'); |
| 92 | |
| 93 | ++Printf; |
| 94 | assert(*Printf == ':' && "p or P specifier must have be followed by a ':'"); |
| 95 | ++Printf; |
| 96 | |
Chris Lattner | ab68faf | 2009-02-19 06:41:13 +0000 | [diff] [blame] | 97 | assert(strchr(Printf, ':') && "printf specifier must end with a ':'"); |
Douglas Gregor | ac5d4c5 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 98 | FormatIdx = strtol(Printf, 0, 10); |
| 99 | return true; |
| 100 | } |
| 101 | |
Ted Kremenek | 5932c35 | 2010-07-16 02:11:15 +0000 | [diff] [blame] | 102 | // FIXME: Refactor with isPrintfLike. |
| 103 | bool |
| 104 | Builtin::Context::isScanfLike(unsigned ID, unsigned &FormatIdx, |
| 105 | bool &HasVAListArg) { |
| 106 | const char *Scanf = strpbrk(GetRecord(ID).Attributes, "sS"); |
| 107 | if (!Scanf) |
| 108 | return false; |
| 109 | |
| 110 | HasVAListArg = (*Scanf == 'S'); |
| 111 | |
| 112 | ++Scanf; |
| 113 | assert(*Scanf == ':' && "s or S specifier must have be followed by a ':'"); |
| 114 | ++Scanf; |
| 115 | |
| 116 | assert(strchr(Scanf, ':') && "printf specifier must end with a ':'"); |
| 117 | FormatIdx = strtol(Scanf, 0, 10); |
| 118 | return true; |
| 119 | } |
| 120 | |