Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- Builtins.cpp - Builtin function implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements various things for builtin functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 99ca9d6 | 2009-06-14 01:05:48 +0000 | [diff] [blame^] | 14 | #include "clang/Basic/Builtins.h" |
Chris Lattner | 2fd1c65 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 15 | #include "clang/Basic/IdentifierTable.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
| 17 | using namespace clang; |
| 18 | |
| 19 | static const Builtin::Info BuiltinInfo[] = { |
Douglas Gregor | 2e8a7aa | 2009-02-16 21:58:21 +0000 | [diff] [blame] | 20 | { "not a builtin function", 0, 0, 0, false }, |
| 21 | #define BUILTIN(ID, TYPE, ATTRS) { #ID, TYPE, ATTRS, 0, false }, |
| 22 | #define LIBBUILTIN(ID, TYPE, ATTRS, HEADER) { #ID, TYPE, ATTRS, HEADER, false }, |
Chris Lattner | 99ca9d6 | 2009-06-14 01:05:48 +0000 | [diff] [blame^] | 23 | #include "clang/Basic/Builtins.def" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | const Builtin::Info &Builtin::Context::GetRecord(unsigned ID) const { |
| 27 | if (ID < Builtin::FirstTSBuiltin) |
| 28 | return BuiltinInfo[ID]; |
| 29 | assert(ID - Builtin::FirstTSBuiltin < NumTSRecords && "Invalid builtin ID!"); |
| 30 | return TSRecords[ID - Builtin::FirstTSBuiltin]; |
| 31 | } |
| 32 | |
Douglas Gregor | 37f477e | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 33 | /// \brief Load all of the target builtins. This must be called |
| 34 | /// prior to initializing the builtin identifiers. |
| 35 | void Builtin::Context::InitializeTargetBuiltins(const TargetInfo &Target) { |
| 36 | Target.getTargetBuiltins(TSRecords, NumTSRecords); |
| 37 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 38 | |
| 39 | /// InitializeBuiltins - Mark the identifiers for all the builtins with their |
| 40 | /// appropriate builtin ID # and mark any non-portable builtin identifiers as |
| 41 | /// such. |
| 42 | void Builtin::Context::InitializeBuiltins(IdentifierTable &Table, |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 43 | bool NoBuiltins) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 44 | // Step #1: mark all target-independent builtins with their ID's. |
| 45 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 46 | if (!BuiltinInfo[i].Suppressed && |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 47 | (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))) |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 48 | Table.get(BuiltinInfo[i].Name).setBuiltinID(i); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | 37f477e | 2009-04-22 04:56:28 +0000 | [diff] [blame] | 50 | // Step #2: Register target-specific builtins. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 52 | if (!TSRecords[i].Suppressed && |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 53 | (!NoBuiltins || |
Daniel Dunbar | bff577a | 2009-02-15 18:23:07 +0000 | [diff] [blame] | 54 | (TSRecords[i].Attributes && |
| 55 | !strchr(TSRecords[i].Attributes, 'f')))) |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 56 | Table.get(TSRecords[i].Name).setBuiltinID(i+Builtin::FirstTSBuiltin); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Douglas Gregor | da38c6c | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 59 | void |
| 60 | Builtin::Context::GetBuiltinNames(llvm::SmallVectorImpl<const char *> &Names, |
| 61 | bool NoBuiltins) { |
| 62 | // Final all target-independent names |
| 63 | for (unsigned i = Builtin::NotBuiltin+1; i != Builtin::FirstTSBuiltin; ++i) |
| 64 | if (!BuiltinInfo[i].Suppressed && |
| 65 | (!NoBuiltins || !strchr(BuiltinInfo[i].Attributes, 'f'))) |
| 66 | Names.push_back(BuiltinInfo[i].Name); |
| 67 | |
| 68 | // Find target-specific names. |
| 69 | for (unsigned i = 0, e = NumTSRecords; i != e; ++i) |
| 70 | if (!TSRecords[i].Suppressed && |
| 71 | (!NoBuiltins || |
| 72 | (TSRecords[i].Attributes && |
| 73 | !strchr(TSRecords[i].Attributes, 'f')))) |
| 74 | Names.push_back(TSRecords[i].Name); |
| 75 | } |
| 76 | |
Douglas Gregor | 1742903 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 77 | bool |
| 78 | Builtin::Context::isPrintfLike(unsigned ID, unsigned &FormatIdx, |
| 79 | bool &HasVAListArg) { |
Cédric Venet | c47ed4a | 2009-02-14 16:15:20 +0000 | [diff] [blame] | 80 | const char *Printf = strpbrk(GetRecord(ID).Attributes, "pP"); |
Douglas Gregor | 1742903 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 81 | if (!Printf) |
| 82 | return false; |
| 83 | |
| 84 | HasVAListArg = (*Printf == 'P'); |
| 85 | |
| 86 | ++Printf; |
| 87 | assert(*Printf == ':' && "p or P specifier must have be followed by a ':'"); |
| 88 | ++Printf; |
| 89 | |
Chris Lattner | ca8de5d | 2009-02-19 06:41:13 +0000 | [diff] [blame] | 90 | assert(strchr(Printf, ':') && "printf specifier must end with a ':'"); |
Douglas Gregor | 1742903 | 2009-02-14 00:32:47 +0000 | [diff] [blame] | 91 | FormatIdx = strtol(Printf, 0, 10); |
| 92 | return true; |
| 93 | } |
| 94 | |