LLVM  16.0.0git
Parser.cpp
Go to the documentation of this file.
1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This library implements the functionality defined in llvm/AsmParser/Parser.h
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/AsmParser/Parser.h"
15 #include "llvm/IR/Module.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include <system_error>
20 
21 using namespace llvm;
22 
25  SlotMapping *Slots, bool UpgradeDebugInfo,
26  DataLayoutCallbackTy DataLayoutCallback) {
27  SourceMgr SM;
28  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
30 
32  return LLParser(F.getBuffer(), SM, Err, M, Index,
33  M ? M->getContext() : Context, Slots)
34  .Run(UpgradeDebugInfo, DataLayoutCallback);
35 }
36 
39  SlotMapping *Slots,
40  DataLayoutCallbackTy DataLayoutCallback) {
41  return ::parseAssemblyInto(F, M, Index, Err, Slots,
42  /*UpgradeDebugInfo*/ true, DataLayoutCallback);
43 }
44 
45 std::unique_ptr<Module>
47  SlotMapping *Slots,
48  DataLayoutCallbackTy DataLayoutCallback) {
49  std::unique_ptr<Module> M =
50  std::make_unique<Module>(F.getBufferIdentifier(), Context);
51 
52  if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback))
53  return nullptr;
54 
55  return M;
56 }
57 
58 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
59  SMDiagnostic &Err,
61  SlotMapping *Slots) {
64  if (std::error_code EC = FileOrErr.getError()) {
65  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
66  "Could not open input file: " + EC.message());
67  return nullptr;
68  }
69 
70  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
71 }
72 
76  bool UpgradeDebugInfo,
77  DataLayoutCallbackTy DataLayoutCallback) {
78  std::unique_ptr<Module> M =
79  std::make_unique<Module>(F.getBufferIdentifier(), Context);
80  std::unique_ptr<ModuleSummaryIndex> Index =
81  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
82 
83  if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
84  DataLayoutCallback))
85  return {nullptr, nullptr};
86 
87  return {std::move(M), std::move(Index)};
88 }
89 
91  SMDiagnostic &Err,
93  SlotMapping *Slots) {
95  /*UpgradeDebugInfo*/ true,
96  [](StringRef) { return std::nullopt; });
97 }
98 
102  bool UpgradeDebugInfo,
103  DataLayoutCallbackTy DataLayoutCallback) {
105  MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
106  if (std::error_code EC = FileOrErr.getError()) {
107  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
108  "Could not open input file: " + EC.message());
109  return {nullptr, nullptr};
110  }
111 
112  return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
113  Context, Slots, UpgradeDebugInfo,
114  DataLayoutCallback);
115 }
116 
120  DataLayoutCallbackTy DataLayoutCallback) {
121  return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
122  /*UpgradeDebugInfo*/ true,
123  DataLayoutCallback);
124 }
125 
127  StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
128  SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
129  return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
130  /*UpgradeDebugInfo*/ false,
131  DataLayoutCallback);
132 }
133 
134 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
135  SMDiagnostic &Err,
137  SlotMapping *Slots) {
138  MemoryBufferRef F(AsmString, "<string>");
139  return parseAssembly(F, Err, Context, Slots);
140 }
141 
144  SMDiagnostic &Err) {
145  SourceMgr SM;
146  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
147  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
148 
149  // The parser holds a reference to a context that is unused when parsing the
150  // index, but we need to initialize it.
151  LLVMContext unusedContext;
152  return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
153  .Run(true, [](StringRef) { return std::nullopt; });
154 }
155 
156 std::unique_ptr<ModuleSummaryIndex>
158  std::unique_ptr<ModuleSummaryIndex> Index =
159  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
160 
162  return nullptr;
163 
164  return Index;
165 }
166 
167 std::unique_ptr<ModuleSummaryIndex>
171  if (std::error_code EC = FileOrErr.getError()) {
172  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
173  "Could not open input file: " + EC.message());
174  return nullptr;
175  }
176 
177  return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
178 }
179 
180 std::unique_ptr<ModuleSummaryIndex>
182  MemoryBufferRef F(AsmString, "<string>");
183  return parseSummaryIndexAssembly(F, Err);
184 }
185 
187  const Module &M, const SlotMapping *Slots) {
188  SourceMgr SM;
189  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
190  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
191  Constant *C;
192  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
194  return nullptr;
195  return C;
196 }
197 
199  const SlotMapping *Slots) {
200  unsigned Read;
201  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
202  if (!Ty)
203  return nullptr;
204  if (Read != Asm.size()) {
205  SourceMgr SM;
206  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
207  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
208  Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
209  SourceMgr::DK_Error, "expected end of string");
210  return nullptr;
211  }
212  return Ty;
213 }
215  SMDiagnostic &Err, const Module &M,
216  const SlotMapping *Slots) {
217  SourceMgr SM;
218  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
219  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
220  Type *Ty;
221  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
222  .parseTypeAtBeginning(Ty, Read, Slots))
223  return nullptr;
224  return Ty;
225 }
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, Optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:151
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::parseAssemblyString
std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:134
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::UpgradeDebugInfo
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Definition: AutoUpgrade.cpp:4416
llvm::LLParser::parseTypeAtBeginning
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition: LLParser.cpp:123
llvm::parseTypeAtBeginning
Type * parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a string Asm that starts with a type.
Definition: Parser.cpp:214
llvm::parseSummaryIndexAssembly
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:157
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::parseAssembly
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return std::nullopt;})
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:46
llvm::LLParser::Run
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:80
llvm::parseAssemblyWithIndex
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:90
llvm::parseSummaryIndexAssemblyString
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:181
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
ModuleSummaryIndex.h
parseSummaryIndexAssemblyInto
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition: Parser.cpp:142
llvm::LLParser
Definition: LLParser.h:96
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::parseType
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:198
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::MemoryBuffer::getMemBuffer
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
Definition: MemoryBuffer.cpp:120
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
LLParser.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::SMDiagnostic
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::parseAssemblyFile
std::unique_ptr< Module > parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:58
parseAssemblyFileWithIndex
static ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:100
SourceMgr.h
llvm::parseAssemblyFileWithIndex
ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return std::nullopt;})
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:118
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::SlotMapping
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:32
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::parseAssemblyInto
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return std::nullopt;})
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:37
Index
uint32_t Index
Definition: ELFObjHandler.cpp:83
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::parseSummaryIndexAssemblyFile
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:168
llvm::parseAssemblyFileWithIndexNoUpgradeDebugInfo
ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback)
Only for use in llvm-as for testing; this does not produce a valid module.
Definition: Parser.cpp:126
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::LLParser::parseStandaloneConstantValue
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition: LLParser.cpp:110
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::ParsedModuleAndIndex
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:69
parseAssemblyInto
static bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:23
Parser.h
llvm::SourceMgr::DK_Error
@ DK_Error
Definition: SourceMgr.h:34
llvm::SourceMgr::AddNewSourceBuffer
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
llvm::SourceMgr::GetMessage
SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const
Return an SMDiagnostic at the specified location with the specified string.
Definition: SourceMgr.cpp:270
llvm::parseConstantValue
Constant * parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type and a constant value in the given string.
Definition: Parser.cpp:186
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
llvm::ModuleSummaryIndex
Class to hold module path string table and global value map, and encapsulate methods for operating on...
Definition: ModuleSummaryIndex.h:1199
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
llvm::SMLoc::getFromPointer
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
parseAssemblyWithIndex
static ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:74