Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 1 | //===- Parser.cpp - Main dispatch module for the Parser library -----------===// |
Misha Brukman | 13f332c | 2005-04-21 21:10:11 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | 13f332c | 2005-04-21 21:10:11 +0000 | [diff] [blame] | 6 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 8 | // |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 9 | // This library implements the functionality defined in llvm/AsmParser/Parser.h |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 10 | // |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 12 | |
Chandler Carruth | 9aca918 | 2014-01-07 12:34:26 +0000 | [diff] [blame] | 13 | #include "llvm/AsmParser/Parser.h" |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 14 | #include "LLParser.h" |
Benjamin Kramer | 0a446fd | 2015-03-01 21:28:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 17 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chris Lattner | 660c6b9 | 2007-11-18 08:46:26 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | ac161bf | 2009-01-02 07:01:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 21 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 22 | #include <system_error> |
Chris Lattner | d25cad9 | 2004-07-13 08:42:12 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 25 | bool llvm::parseAssemblyInto(MemoryBufferRef F, Module *M, |
| 26 | ModuleSummaryIndex *Index, SMDiagnostic &Err, |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 27 | SlotMapping *Slots, bool UpgradeDebugInfo, |
| 28 | StringRef DataLayoutString) { |
Rafael Espindola | 3f3d7ac | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 29 | SourceMgr SM; |
Alex Lorenz | c627779 | 2015-05-20 20:41:27 +0000 | [diff] [blame] | 30 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F); |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 31 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
Rafael Espindola | 3f3d7ac | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 32 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 33 | LLVMContext Context; |
| 34 | return LLParser(F.getBuffer(), SM, Err, M, Index, |
| 35 | M ? M->getContext() : Context, Slots, UpgradeDebugInfo, |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 36 | DataLayoutString) |
| 37 | .Run(); |
Rafael Espindola | 3f3d7ac | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Adrian Prantl | a8b2ddb | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 40 | std::unique_ptr<Module> |
| 41 | llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 42 | SlotMapping *Slots, bool UpgradeDebugInfo, |
| 43 | StringRef DataLayoutString) { |
Rafael Espindola | 11c07d7 | 2014-08-19 16:58:54 +0000 | [diff] [blame] | 44 | std::unique_ptr<Module> M = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 45 | std::make_unique<Module>(F.getBufferIdentifier(), Context); |
Rafael Espindola | 3f3d7ac | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 46 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 47 | if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo, |
| 48 | DataLayoutString)) |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 49 | return nullptr; |
Rafael Espindola | 3f3d7ac | 2014-08-19 22:05:47 +0000 | [diff] [blame] | 50 | |
Richard Trieu | 73d0652 | 2015-01-17 00:46:44 +0000 | [diff] [blame] | 51 | return M; |
Dan Gohman | 77ac99d | 2009-09-02 17:18:19 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 54 | std::unique_ptr<Module> |
| 55 | llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, |
| 56 | LLVMContext &Context, SlotMapping *Slots, |
| 57 | bool UpgradeDebugInfo, StringRef DataLayoutString) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 58 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 59 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 60 | if (std::error_code EC = FileOrErr.getError()) { |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 61 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 62 | "Could not open input file: " + EC.message()); |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 63 | return nullptr; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 64 | } |
Misha Brukman | 1d9a93d | 2009-01-02 22:46:48 +0000 | [diff] [blame] | 65 | |
Adrian Prantl | a8b2ddb | 2017-10-02 18:31:29 +0000 | [diff] [blame] | 66 | return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots, |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 67 | UpgradeDebugInfo, DataLayoutString); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 70 | ParsedModuleAndIndex llvm::parseAssemblyWithIndex( |
| 71 | MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, |
| 72 | SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) { |
| 73 | std::unique_ptr<Module> M = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 74 | std::make_unique<Module>(F.getBufferIdentifier(), Context); |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 75 | std::unique_ptr<ModuleSummaryIndex> Index = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 76 | std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true); |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 77 | |
| 78 | if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo, |
| 79 | DataLayoutString)) |
| 80 | return {nullptr, nullptr}; |
| 81 | |
| 82 | return {std::move(M), std::move(Index)}; |
| 83 | } |
| 84 | |
| 85 | ParsedModuleAndIndex llvm::parseAssemblyFileWithIndex( |
| 86 | StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, |
| 87 | SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) { |
| 88 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 89 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 90 | if (std::error_code EC = FileOrErr.getError()) { |
| 91 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 92 | "Could not open input file: " + EC.message()); |
| 93 | return {nullptr, nullptr}; |
| 94 | } |
| 95 | |
| 96 | return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err, |
| 97 | Context, Slots, UpgradeDebugInfo, |
| 98 | DataLayoutString); |
| 99 | } |
| 100 | |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 101 | std::unique_ptr<Module> |
| 102 | llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, |
| 103 | LLVMContext &Context, SlotMapping *Slots, |
| 104 | bool UpgradeDebugInfo, StringRef DataLayoutString) { |
Rafael Espindola | d96d553 | 2014-08-26 21:49:01 +0000 | [diff] [blame] | 105 | MemoryBufferRef F(AsmString, "<string>"); |
Yaxun Liu | c00d81e | 2018-01-30 22:32:39 +0000 | [diff] [blame] | 106 | return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo, |
| 107 | DataLayoutString); |
Chris Lattner | 416a0d4 | 2005-05-20 03:25:47 +0000 | [diff] [blame] | 108 | } |
Alex Lorenz | d225595 | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 109 | |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 110 | static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, |
| 111 | ModuleSummaryIndex &Index, |
| 112 | SMDiagnostic &Err) { |
| 113 | SourceMgr SM; |
| 114 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F); |
| 115 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 116 | |
| 117 | // The parser holds a reference to a context that is unused when parsing the |
| 118 | // index, but we need to initialize it. |
| 119 | LLVMContext unusedContext; |
| 120 | return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run(); |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<ModuleSummaryIndex> |
| 124 | llvm::parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err) { |
| 125 | std::unique_ptr<ModuleSummaryIndex> Index = |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 126 | std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false); |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 127 | |
| 128 | if (parseSummaryIndexAssemblyInto(F, *Index, Err)) |
| 129 | return nullptr; |
| 130 | |
| 131 | return Index; |
| 132 | } |
| 133 | |
| 134 | std::unique_ptr<ModuleSummaryIndex> |
| 135 | llvm::parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err) { |
| 136 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
| 137 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 138 | if (std::error_code EC = FileOrErr.getError()) { |
| 139 | Err = SMDiagnostic(Filename, SourceMgr::DK_Error, |
| 140 | "Could not open input file: " + EC.message()); |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
| 144 | return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err); |
| 145 | } |
| 146 | |
Alex Lorenz | d225595 | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 147 | Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err, |
Alex Lorenz | 1de2acd | 2015-08-21 21:32:39 +0000 | [diff] [blame] | 148 | const Module &M, const SlotMapping *Slots) { |
Alex Lorenz | d225595 | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 149 | SourceMgr SM; |
| 150 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 151 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 152 | Constant *C; |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 153 | if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext()) |
Alex Lorenz | 1de2acd | 2015-08-21 21:32:39 +0000 | [diff] [blame] | 154 | .parseStandaloneConstantValue(C, Slots)) |
Alex Lorenz | d225595 | 2015-07-17 22:07:03 +0000 | [diff] [blame] | 155 | return nullptr; |
| 156 | return C; |
| 157 | } |
Quentin Colombet | 81e72b4 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 158 | |
| 159 | Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, |
| 160 | const SlotMapping *Slots) { |
Quentin Colombet | dafed5d | 2016-03-08 00:37:07 +0000 | [diff] [blame] | 161 | unsigned Read; |
| 162 | Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots); |
| 163 | if (!Ty) |
| 164 | return nullptr; |
| 165 | if (Read != Asm.size()) { |
| 166 | SourceMgr SM; |
| 167 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 168 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 169 | Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read), |
| 170 | SourceMgr::DK_Error, "expected end of string"); |
| 171 | return nullptr; |
| 172 | } |
| 173 | return Ty; |
| 174 | } |
| 175 | Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read, |
| 176 | SMDiagnostic &Err, const Module &M, |
| 177 | const SlotMapping *Slots) { |
Quentin Colombet | 81e72b4 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 178 | SourceMgr SM; |
| 179 | std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); |
| 180 | SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); |
| 181 | Type *Ty; |
Teresa Johnson | 63ee0e7 | 2018-06-26 13:56:49 +0000 | [diff] [blame] | 182 | if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext()) |
Quentin Colombet | dafed5d | 2016-03-08 00:37:07 +0000 | [diff] [blame] | 183 | .parseTypeAtBeginning(Ty, Read, Slots)) |
Quentin Colombet | 81e72b4 | 2016-03-07 22:09:05 +0000 | [diff] [blame] | 184 | return nullptr; |
| 185 | return Ty; |
| 186 | } |