Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1 | //===- Parser.cpp - Main dispatch module for the Parser library -----------===// |
Misha Brukman | 019b639 | 2005-04-21 21:10:11 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 019b639 | 2005-04-21 21:10:11 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chandler Carruth | bc65a8d | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 10 | // This library implements the functionality defined in llvm/AsmParser/Parser.h |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | |
Chandler Carruth | bc65a8d | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 14 | #include "llvm/AsmParser/Parser.h" |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 15 | #include "LLParser.h" |
Benjamin Kramer | d59c5f9 | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Module.h" |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 18 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chris Lattner | 8e3a8e0 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | df98617 | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | ae9f3a3 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 22 | #include <cstring> |
Rafael Espindola | d5132f9 | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 23 | #include <system_error> |
Chris Lattner | 65cd4b0 | 2004-07-13 08:42:12 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 26 | bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M, |
| 27 | ModuleSummaryIndex *Index, SMDiagnostic &Err, |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 28 | SlotMapping *Slots, bool UpgradeDebugInfo, |
| 29 | StringRef DataLayoutString) { |
Rafael Espindola | b12ab60 | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 30 | SourceMgr SM; |
Alex Lorenz | 3f1b2bd | 2015-05-20 20:41:27 +0000 | [diff] [blame] | 31 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F); |
Rafael Espindola | 2292996 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 32 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
Rafael Espindola | b12ab60 | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 33 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 34 | LLVMContext Context; |
| 35 | return LLParser(F.getBuffer(), SM, Err, M, Index, |
| 36 | M ? M->getContext() : Context, Slots, UpgradeDebugInfo, |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 37 | DataLayoutString) |
| 38 | .Run(); |
Rafael Espindola | b12ab60 | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Adrian Prantl | 733fe2f | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 41 | std::unique_ptr<Module> |
| 42 | llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 43 | SlotMapping *Slots, bool UpgradeDebugInfo, |
| 44 | StringRef DataLayoutString) { |
Rafael Espindola | 9b29ff9 | 2014-08-19 16:58:54 +0000 | [diff] [blame] | 45 | std::unique_ptr<Module> M = |
Rafael Espindola | 2292996 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 46 | make_unique<Module>(F.getBufferIdentifier(), Context); |
Rafael Espindola | b12ab60 | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 47 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 48 | if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo, |
| 49 | DataLayoutString)) |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 50 | return nullptr; |
Rafael Espindola | b12ab60 | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 51 | |
Richard Trieu | 6cf3f3f | 2015-01-17 00:46:44 +0000 | [diff] [blame] | 52 | return M; |
Dan Gohman | 2ec5fe5 | 2009-09-02 17:18:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 55 | std::unique_ptr<Module> |
| 56 | llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, |
| 57 | LLVMContext &Context, SlotMapping *Slots, |
| 58 | bool UpgradeDebugInfo, StringRef DataLayoutString) { |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 59 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 60 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 61 | if (std::error_code EC = FileOrErr.getError()) { |
Chris Lattner | 3f2d5f6 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 62 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 63 | "Could not open input file: " + EC.message()); |
Craig Topper | 0b6cb71 | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 64 | return nullptr; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 65 | } |
Misha Brukman | 9ea4034 | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 66 | |
Adrian Prantl | 733fe2f | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 67 | return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots, |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 68 | UpgradeDebugInfo, DataLayoutString); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 71 | ParsedModuleAndIndex llvm::parseAssemblyWithIndex( |
| 72 | MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, |
| 73 | SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) { |
| 74 | std::unique_ptr<Module> M = |
| 75 | make_unique<Module>(F.getBufferIdentifier(), Context); |
| 76 | std::unique_ptr<ModuleSummaryIndex> Index = |
| 77 | make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true); |
| 78 | |
| 79 | if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo, |
| 80 | DataLayoutString)) |
| 81 | return {nullptr, nullptr}; |
| 82 | |
| 83 | return {std::move(M), std::move(Index)}; |
| 84 | } |
| 85 | |
| 86 | ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex( |
| 87 | StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, |
| 88 | SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) { |
| 89 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 90 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 91 | if (std::error_code EC = FileOrErr.getError()) { |
| 92 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 93 | "Could not open input file: " + EC.message()); |
| 94 | return {nullptr, nullptr}; |
| 95 | } |
| 96 | |
| 97 | return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err, |
| 98 | Context, Slots, UpgradeDebugInfo, |
| 99 | DataLayoutString); |
| 100 | } |
| 101 | |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 102 | std::unique_ptr<Module> |
| 103 | llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, |
| 104 | LLVMContext &Context, SlotMapping *Slots, |
| 105 | bool UpgradeDebugInfo, StringRef DataLayoutString) { |
Rafael Espindola | 2292996 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 106 | MemoryBufferRef F(AsmString, "<string>"); |
Yaxun Liu | 7c2d049 | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 107 | return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo, |
| 108 | DataLayoutString); |
Chris Lattner | 6184feb | 2005-05-20 03:25:47 +0000 | [diff] [blame] | 109 | } |
Alex Lorenz | 4b50ecb | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 110 | |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 111 | static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, |
| 112 | ModuleSummaryIndex &Index, |
| 113 | SMDiagnostic &Err) { |
| 114 | SourceMgr SM; |
| 115 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F); |
| 116 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 117 | |
| 118 | // The parser holds a reference to a context that is unused when parsing the |
| 119 | // index, but we need to initialize it. |
| 120 | LLVMContext unusedContext; |
| 121 | return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run(); |
| 122 | } |
| 123 | |
| 124 | std::unique_ptr<ModuleSummaryIndex> |
| 125 | llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) { |
| 126 | std::unique_ptr<ModuleSummaryIndex> Index = |
| 127 | make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); |
| 128 | |
| 129 | if (parseSummaryIndexAssemblyInto(F, *Index, Err)) |
| 130 | return nullptr; |
| 131 | |
| 132 | return Index; |
| 133 | } |
| 134 | |
| 135 | std::unique_ptr<ModuleSummaryIndex> |
| 136 | llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) { |
| 137 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 138 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 139 | if (std::error_code EC = FileOrErr.getError()) { |
| 140 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 141 | "Could not open input file: " + EC.message()); |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err); |
| 146 | } |
| 147 | |
Alex Lorenz | 4b50ecb | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 148 | Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err, |
Alex Lorenz | 0e99876 | 2015-08-21 21:32:39 +0000 | [diff] [blame] | 149 | const Module &M, const SlotMapping *Slots) { |
Alex Lorenz | 4b50ecb | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 150 | SourceMgr SM; |
| 151 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 152 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 153 | Constant *C; |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 154 | if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext()) |
Alex Lorenz | 0e99876 | 2015-08-21 21:32:39 +0000 | [diff] [blame] | 155 | .parseStandaloneConstantValue(C, Slots)) |
Alex Lorenz | 4b50ecb | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 156 | return nullptr; |
| 157 | return C; |
| 158 | } |
Quentin Colombet | 2ba0323 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 159 | |
| 160 | Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, |
| 161 | const SlotMapping *Slots) { |
Quentin Colombet | cbd4dbb | 2016-03-08 00:37:07 +0000 | [diff] [blame] | 162 | unsigned Read; |
| 163 | Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots); |
| 164 | if (!Ty) |
| 165 | return nullptr; |
| 166 | if (Read != Asm.size()) { |
| 167 | SourceMgr SM; |
| 168 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 169 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 170 | Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read), |
| 171 | SourceMgr::DK_Error, "expected end of string"); |
| 172 | return nullptr; |
| 173 | } |
| 174 | return Ty; |
| 175 | } |
| 176 | Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read, |
| 177 | SMDiagnostic &Err, const Module &M, |
| 178 | const SlotMapping *Slots) { |
Quentin Colombet | 2ba0323 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 179 | SourceMgr SM; |
| 180 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 181 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 182 | Type *Ty; |
Teresa Johnson | c6dda90 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 183 | if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext()) |
Quentin Colombet | cbd4dbb | 2016-03-08 00:37:07 +0000 | [diff] [blame] | 184 | .parseTypeAtBeginning(Ty, Read, Slots)) |
Quentin Colombet | 2ba0323 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 185 | return nullptr; |
| 186 | return Ty; |
| 187 | } |