LLVM  13.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"
14 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Module.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include <cstring>
21 #include <system_error>
22 
23 using namespace llvm;
24 
27  SlotMapping *Slots, bool UpgradeDebugInfo,
28  DataLayoutCallbackTy DataLayoutCallback) {
29  SourceMgr SM;
30  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
32 
34  return LLParser(F.getBuffer(), SM, Err, M, Index,
35  M ? M->getContext() : Context, Slots)
36  .Run(UpgradeDebugInfo, DataLayoutCallback);
37 }
38 
41  SlotMapping *Slots,
42  DataLayoutCallbackTy DataLayoutCallback) {
43  return ::parseAssemblyInto(F, M, Index, Err, Slots,
44  /*UpgradeDebugInfo*/ true, DataLayoutCallback);
45 }
46 
47 std::unique_ptr<Module>
49  SlotMapping *Slots,
50  DataLayoutCallbackTy DataLayoutCallback) {
51  std::unique_ptr<Module> M =
52  std::make_unique<Module>(F.getBufferIdentifier(), Context);
53 
54  if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback))
55  return nullptr;
56 
57  return M;
58 }
59 
60 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
61  SMDiagnostic &Err,
63  SlotMapping *Slots) {
66  if (std::error_code EC = FileOrErr.getError()) {
67  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
68  "Could not open input file: " + EC.message());
69  return nullptr;
70  }
71 
72  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
73 }
74 
78  bool UpgradeDebugInfo,
79  DataLayoutCallbackTy DataLayoutCallback) {
80  std::unique_ptr<Module> M =
81  std::make_unique<Module>(F.getBufferIdentifier(), Context);
82  std::unique_ptr<ModuleSummaryIndex> Index =
83  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
84 
85  if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
86  DataLayoutCallback))
87  return {nullptr, nullptr};
88 
89  return {std::move(M), std::move(Index)};
90 }
91 
93  SMDiagnostic &Err,
95  SlotMapping *Slots) {
97  /*UpgradeDebugInfo*/ true,
98  [](StringRef) { return None; });
99 }
100 
104  bool UpgradeDebugInfo,
105  DataLayoutCallbackTy DataLayoutCallback) {
107  MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
108  if (std::error_code EC = FileOrErr.getError()) {
109  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
110  "Could not open input file: " + EC.message());
111  return {nullptr, nullptr};
112  }
113 
114  return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
115  Context, Slots, UpgradeDebugInfo,
116  DataLayoutCallback);
117 }
118 
122  DataLayoutCallbackTy DataLayoutCallback) {
123  return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
124  /*UpgradeDebugInfo*/ true,
125  DataLayoutCallback);
126 }
127 
129  StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
130  SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
131  return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
132  /*UpgradeDebugInfo*/ false,
133  DataLayoutCallback);
134 }
135 
136 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
137  SMDiagnostic &Err,
139  SlotMapping *Slots) {
140  MemoryBufferRef F(AsmString, "<string>");
141  return parseAssembly(F, Err, Context, Slots);
142 }
143 
146  SMDiagnostic &Err) {
147  SourceMgr SM;
148  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
149  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
150 
151  // The parser holds a reference to a context that is unused when parsing the
152  // index, but we need to initialize it.
153  LLVMContext unusedContext;
154  return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
155  .Run(true, [](StringRef) { return None; });
156 }
157 
158 std::unique_ptr<ModuleSummaryIndex>
160  std::unique_ptr<ModuleSummaryIndex> Index =
161  std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
162 
164  return nullptr;
165 
166  return Index;
167 }
168 
169 std::unique_ptr<ModuleSummaryIndex>
173  if (std::error_code EC = FileOrErr.getError()) {
174  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
175  "Could not open input file: " + EC.message());
176  return nullptr;
177  }
178 
179  return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
180 }
181 
183  const Module &M, const SlotMapping *Slots) {
184  SourceMgr SM;
185  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
186  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
187  Constant *C;
188  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
190  return nullptr;
191  return C;
192 }
193 
195  const SlotMapping *Slots) {
196  unsigned Read;
197  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
198  if (!Ty)
199  return nullptr;
200  if (Read != Asm.size()) {
201  SourceMgr SM;
202  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
203  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
204  Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
205  SourceMgr::DK_Error, "expected end of string");
206  return nullptr;
207  }
208  return Ty;
209 }
211  SMDiagnostic &Err, const Module &M,
212  const SlotMapping *Slots) {
213  SourceMgr SM;
214  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
215  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
216  Type *Ty;
217  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
218  .parseTypeAtBeginning(Ty, Read, Slots))
219  return nullptr;
220  return Ty;
221 }
MemoryBuffer.h
llvm
Definition: AllocatorList.h:23
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:136
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:4024
Parser.h
llvm::LLParser::parseTypeAtBeginning
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition: LLParser.cpp:97
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:210
llvm::parseSummaryIndexAssembly
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:159
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
Module.h
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:92
llvm::parseAssembly
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:48
STLExtras.h
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
ModuleSummaryIndex.h
llvm::LLParser
Definition: LLParser.h:84
F
#define F(x, y, z)
Definition: MD5.cpp:56
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:194
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:113
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:251
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
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:60
llvm::LLParser::Run
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:62
llvm::None
const NoneType None
Definition: None.h:23
SourceMgr.h
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:168
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
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
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:68
llvm::MemoryBuffer::getFileOrSTDIN
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
Definition: MemoryBuffer.cpp:144
llvm::parseAssemblyFileWithIndex
ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:120
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
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:170
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:128
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm::LLParser::parseStandaloneConstantValue
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition: LLParser.cpp:84
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:67
parseAssemblyInto
static bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:25
parseSummaryIndexAssemblyInto
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition: Parser.cpp:144
parseAssemblyWithIndex
static ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:76
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:141
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:262
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:182
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:1026
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
parseAssemblyFileWithIndex
static ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:102
llvm::parseAssemblyInto
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:39