1*9880d681SAndroid Build Coastguard Worker //===- MIParser.cpp - Machine instructions parser implementation ----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the parsing of machine instructions.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "MIParser.h"
15*9880d681SAndroid Build Coastguard Worker #include "MILexer.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringMap.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/AsmParser/Parser.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/AsmParser/SlotMapping.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBasicBlock.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ModuleSlotTracker.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ValueSymbolTable.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/SourceMgr.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker using namespace llvm;
38*9880d681SAndroid Build Coastguard Worker
PerFunctionMIParsingState(MachineFunction & MF,SourceMgr & SM,const SlotMapping & IRSlots)39*9880d681SAndroid Build Coastguard Worker PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
40*9880d681SAndroid Build Coastguard Worker SourceMgr &SM, const SlotMapping &IRSlots)
41*9880d681SAndroid Build Coastguard Worker : MF(MF), SM(&SM), IRSlots(IRSlots) {
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker namespace {
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker /// A wrapper struct around the 'MachineOperand' struct that includes a source
47*9880d681SAndroid Build Coastguard Worker /// range and other attributes.
48*9880d681SAndroid Build Coastguard Worker struct ParsedMachineOperand {
49*9880d681SAndroid Build Coastguard Worker MachineOperand Operand;
50*9880d681SAndroid Build Coastguard Worker StringRef::iterator Begin;
51*9880d681SAndroid Build Coastguard Worker StringRef::iterator End;
52*9880d681SAndroid Build Coastguard Worker Optional<unsigned> TiedDefIdx;
53*9880d681SAndroid Build Coastguard Worker
ParsedMachineOperand__anon02f799f30111::ParsedMachineOperand54*9880d681SAndroid Build Coastguard Worker ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
55*9880d681SAndroid Build Coastguard Worker StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
56*9880d681SAndroid Build Coastguard Worker : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
57*9880d681SAndroid Build Coastguard Worker if (TiedDefIdx)
58*9880d681SAndroid Build Coastguard Worker assert(Operand.isReg() && Operand.isUse() &&
59*9880d681SAndroid Build Coastguard Worker "Only used register operands can be tied");
60*9880d681SAndroid Build Coastguard Worker }
61*9880d681SAndroid Build Coastguard Worker };
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker class MIParser {
64*9880d681SAndroid Build Coastguard Worker MachineFunction &MF;
65*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error;
66*9880d681SAndroid Build Coastguard Worker StringRef Source, CurrentSource;
67*9880d681SAndroid Build Coastguard Worker MIToken Token;
68*9880d681SAndroid Build Coastguard Worker const PerFunctionMIParsingState &PFS;
69*9880d681SAndroid Build Coastguard Worker /// Maps from instruction names to op codes.
70*9880d681SAndroid Build Coastguard Worker StringMap<unsigned> Names2InstrOpCodes;
71*9880d681SAndroid Build Coastguard Worker /// Maps from register names to registers.
72*9880d681SAndroid Build Coastguard Worker StringMap<unsigned> Names2Regs;
73*9880d681SAndroid Build Coastguard Worker /// Maps from register mask names to register masks.
74*9880d681SAndroid Build Coastguard Worker StringMap<const uint32_t *> Names2RegMasks;
75*9880d681SAndroid Build Coastguard Worker /// Maps from subregister names to subregister indices.
76*9880d681SAndroid Build Coastguard Worker StringMap<unsigned> Names2SubRegIndices;
77*9880d681SAndroid Build Coastguard Worker /// Maps from slot numbers to function's unnamed basic blocks.
78*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
79*9880d681SAndroid Build Coastguard Worker /// Maps from slot numbers to function's unnamed values.
80*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const Value *> Slots2Values;
81*9880d681SAndroid Build Coastguard Worker /// Maps from target index names to target indices.
82*9880d681SAndroid Build Coastguard Worker StringMap<int> Names2TargetIndices;
83*9880d681SAndroid Build Coastguard Worker /// Maps from direct target flag names to the direct target flag values.
84*9880d681SAndroid Build Coastguard Worker StringMap<unsigned> Names2DirectTargetFlags;
85*9880d681SAndroid Build Coastguard Worker /// Maps from direct target flag names to the bitmask target flag values.
86*9880d681SAndroid Build Coastguard Worker StringMap<unsigned> Names2BitmaskTargetFlags;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker public:
89*9880d681SAndroid Build Coastguard Worker MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
90*9880d681SAndroid Build Coastguard Worker StringRef Source);
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker /// \p SkipChar gives the number of characters to skip before looking
93*9880d681SAndroid Build Coastguard Worker /// for the next token.
94*9880d681SAndroid Build Coastguard Worker void lex(unsigned SkipChar = 0);
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker /// Report an error at the current location with the given message.
97*9880d681SAndroid Build Coastguard Worker ///
98*9880d681SAndroid Build Coastguard Worker /// This function always return true.
99*9880d681SAndroid Build Coastguard Worker bool error(const Twine &Msg);
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker /// Report an error at the given location with the given message.
102*9880d681SAndroid Build Coastguard Worker ///
103*9880d681SAndroid Build Coastguard Worker /// This function always return true.
104*9880d681SAndroid Build Coastguard Worker bool error(StringRef::iterator Loc, const Twine &Msg);
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker bool
107*9880d681SAndroid Build Coastguard Worker parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
108*9880d681SAndroid Build Coastguard Worker bool parseBasicBlocks();
109*9880d681SAndroid Build Coastguard Worker bool parse(MachineInstr *&MI);
110*9880d681SAndroid Build Coastguard Worker bool parseStandaloneMBB(MachineBasicBlock *&MBB);
111*9880d681SAndroid Build Coastguard Worker bool parseStandaloneNamedRegister(unsigned &Reg);
112*9880d681SAndroid Build Coastguard Worker bool parseStandaloneVirtualRegister(unsigned &Reg);
113*9880d681SAndroid Build Coastguard Worker bool parseStandaloneStackObject(int &FI);
114*9880d681SAndroid Build Coastguard Worker bool parseStandaloneMDNode(MDNode *&Node);
115*9880d681SAndroid Build Coastguard Worker
116*9880d681SAndroid Build Coastguard Worker bool
117*9880d681SAndroid Build Coastguard Worker parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
118*9880d681SAndroid Build Coastguard Worker bool parseBasicBlock(MachineBasicBlock &MBB);
119*9880d681SAndroid Build Coastguard Worker bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
120*9880d681SAndroid Build Coastguard Worker bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker bool parseRegister(unsigned &Reg);
123*9880d681SAndroid Build Coastguard Worker bool parseRegisterFlag(unsigned &Flags);
124*9880d681SAndroid Build Coastguard Worker bool parseSubRegisterIndex(unsigned &SubReg);
125*9880d681SAndroid Build Coastguard Worker bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
126*9880d681SAndroid Build Coastguard Worker bool parseSize(unsigned &Size);
127*9880d681SAndroid Build Coastguard Worker bool parseRegisterOperand(MachineOperand &Dest,
128*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &TiedDefIdx, bool IsDef = false);
129*9880d681SAndroid Build Coastguard Worker bool parseImmediateOperand(MachineOperand &Dest);
130*9880d681SAndroid Build Coastguard Worker bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
131*9880d681SAndroid Build Coastguard Worker const Constant *&C);
132*9880d681SAndroid Build Coastguard Worker bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
133*9880d681SAndroid Build Coastguard Worker bool parseIRType(StringRef::iterator Loc, StringRef Source, unsigned &Read,
134*9880d681SAndroid Build Coastguard Worker Type *&Ty);
135*9880d681SAndroid Build Coastguard Worker // \p MustBeSized defines whether or not \p Ty must be sized.
136*9880d681SAndroid Build Coastguard Worker bool parseIRType(StringRef::iterator Loc, Type *&Ty, bool MustBeSized = true);
137*9880d681SAndroid Build Coastguard Worker bool parseTypedImmediateOperand(MachineOperand &Dest);
138*9880d681SAndroid Build Coastguard Worker bool parseFPImmediateOperand(MachineOperand &Dest);
139*9880d681SAndroid Build Coastguard Worker bool parseMBBReference(MachineBasicBlock *&MBB);
140*9880d681SAndroid Build Coastguard Worker bool parseMBBOperand(MachineOperand &Dest);
141*9880d681SAndroid Build Coastguard Worker bool parseStackFrameIndex(int &FI);
142*9880d681SAndroid Build Coastguard Worker bool parseStackObjectOperand(MachineOperand &Dest);
143*9880d681SAndroid Build Coastguard Worker bool parseFixedStackFrameIndex(int &FI);
144*9880d681SAndroid Build Coastguard Worker bool parseFixedStackObjectOperand(MachineOperand &Dest);
145*9880d681SAndroid Build Coastguard Worker bool parseGlobalValue(GlobalValue *&GV);
146*9880d681SAndroid Build Coastguard Worker bool parseGlobalAddressOperand(MachineOperand &Dest);
147*9880d681SAndroid Build Coastguard Worker bool parseConstantPoolIndexOperand(MachineOperand &Dest);
148*9880d681SAndroid Build Coastguard Worker bool parseSubRegisterIndexOperand(MachineOperand &Dest);
149*9880d681SAndroid Build Coastguard Worker bool parseJumpTableIndexOperand(MachineOperand &Dest);
150*9880d681SAndroid Build Coastguard Worker bool parseExternalSymbolOperand(MachineOperand &Dest);
151*9880d681SAndroid Build Coastguard Worker bool parseMDNode(MDNode *&Node);
152*9880d681SAndroid Build Coastguard Worker bool parseMetadataOperand(MachineOperand &Dest);
153*9880d681SAndroid Build Coastguard Worker bool parseCFIOffset(int &Offset);
154*9880d681SAndroid Build Coastguard Worker bool parseCFIRegister(unsigned &Reg);
155*9880d681SAndroid Build Coastguard Worker bool parseCFIOperand(MachineOperand &Dest);
156*9880d681SAndroid Build Coastguard Worker bool parseIRBlock(BasicBlock *&BB, const Function &F);
157*9880d681SAndroid Build Coastguard Worker bool parseBlockAddressOperand(MachineOperand &Dest);
158*9880d681SAndroid Build Coastguard Worker bool parseTargetIndexOperand(MachineOperand &Dest);
159*9880d681SAndroid Build Coastguard Worker bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
160*9880d681SAndroid Build Coastguard Worker bool parseMachineOperand(MachineOperand &Dest,
161*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &TiedDefIdx);
162*9880d681SAndroid Build Coastguard Worker bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
163*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &TiedDefIdx);
164*9880d681SAndroid Build Coastguard Worker bool parseOffset(int64_t &Offset);
165*9880d681SAndroid Build Coastguard Worker bool parseAlignment(unsigned &Alignment);
166*9880d681SAndroid Build Coastguard Worker bool parseOperandsOffset(MachineOperand &Op);
167*9880d681SAndroid Build Coastguard Worker bool parseIRValue(const Value *&V);
168*9880d681SAndroid Build Coastguard Worker bool parseMemoryOperandFlag(unsigned &Flags);
169*9880d681SAndroid Build Coastguard Worker bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
170*9880d681SAndroid Build Coastguard Worker bool parseMachinePointerInfo(MachinePointerInfo &Dest);
171*9880d681SAndroid Build Coastguard Worker bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
172*9880d681SAndroid Build Coastguard Worker
173*9880d681SAndroid Build Coastguard Worker private:
174*9880d681SAndroid Build Coastguard Worker /// Convert the integer literal in the current token into an unsigned integer.
175*9880d681SAndroid Build Coastguard Worker ///
176*9880d681SAndroid Build Coastguard Worker /// Return true if an error occurred.
177*9880d681SAndroid Build Coastguard Worker bool getUnsigned(unsigned &Result);
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker /// Convert the integer literal in the current token into an uint64.
180*9880d681SAndroid Build Coastguard Worker ///
181*9880d681SAndroid Build Coastguard Worker /// Return true if an error occurred.
182*9880d681SAndroid Build Coastguard Worker bool getUint64(uint64_t &Result);
183*9880d681SAndroid Build Coastguard Worker
184*9880d681SAndroid Build Coastguard Worker /// If the current token is of the given kind, consume it and return false.
185*9880d681SAndroid Build Coastguard Worker /// Otherwise report an error and return true.
186*9880d681SAndroid Build Coastguard Worker bool expectAndConsume(MIToken::TokenKind TokenKind);
187*9880d681SAndroid Build Coastguard Worker
188*9880d681SAndroid Build Coastguard Worker /// If the current token is of the given kind, consume it and return true.
189*9880d681SAndroid Build Coastguard Worker /// Otherwise return false.
190*9880d681SAndroid Build Coastguard Worker bool consumeIfPresent(MIToken::TokenKind TokenKind);
191*9880d681SAndroid Build Coastguard Worker
192*9880d681SAndroid Build Coastguard Worker void initNames2InstrOpCodes();
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker /// Try to convert an instruction name to an opcode. Return true if the
195*9880d681SAndroid Build Coastguard Worker /// instruction name is invalid.
196*9880d681SAndroid Build Coastguard Worker bool parseInstrName(StringRef InstrName, unsigned &OpCode);
197*9880d681SAndroid Build Coastguard Worker
198*9880d681SAndroid Build Coastguard Worker bool parseInstruction(unsigned &OpCode, unsigned &Flags);
199*9880d681SAndroid Build Coastguard Worker
200*9880d681SAndroid Build Coastguard Worker bool assignRegisterTies(MachineInstr &MI,
201*9880d681SAndroid Build Coastguard Worker ArrayRef<ParsedMachineOperand> Operands);
202*9880d681SAndroid Build Coastguard Worker
203*9880d681SAndroid Build Coastguard Worker bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
204*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID);
205*9880d681SAndroid Build Coastguard Worker
206*9880d681SAndroid Build Coastguard Worker void initNames2Regs();
207*9880d681SAndroid Build Coastguard Worker
208*9880d681SAndroid Build Coastguard Worker /// Try to convert a register name to a register number. Return true if the
209*9880d681SAndroid Build Coastguard Worker /// register name is invalid.
210*9880d681SAndroid Build Coastguard Worker bool getRegisterByName(StringRef RegName, unsigned &Reg);
211*9880d681SAndroid Build Coastguard Worker
212*9880d681SAndroid Build Coastguard Worker void initNames2RegMasks();
213*9880d681SAndroid Build Coastguard Worker
214*9880d681SAndroid Build Coastguard Worker /// Check if the given identifier is a name of a register mask.
215*9880d681SAndroid Build Coastguard Worker ///
216*9880d681SAndroid Build Coastguard Worker /// Return null if the identifier isn't a register mask.
217*9880d681SAndroid Build Coastguard Worker const uint32_t *getRegMask(StringRef Identifier);
218*9880d681SAndroid Build Coastguard Worker
219*9880d681SAndroid Build Coastguard Worker void initNames2SubRegIndices();
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker /// Check if the given identifier is a name of a subregister index.
222*9880d681SAndroid Build Coastguard Worker ///
223*9880d681SAndroid Build Coastguard Worker /// Return 0 if the name isn't a subregister index class.
224*9880d681SAndroid Build Coastguard Worker unsigned getSubRegIndex(StringRef Name);
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker const BasicBlock *getIRBlock(unsigned Slot);
227*9880d681SAndroid Build Coastguard Worker const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
228*9880d681SAndroid Build Coastguard Worker
229*9880d681SAndroid Build Coastguard Worker const Value *getIRValue(unsigned Slot);
230*9880d681SAndroid Build Coastguard Worker
231*9880d681SAndroid Build Coastguard Worker void initNames2TargetIndices();
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker /// Try to convert a name of target index to the corresponding target index.
234*9880d681SAndroid Build Coastguard Worker ///
235*9880d681SAndroid Build Coastguard Worker /// Return true if the name isn't a name of a target index.
236*9880d681SAndroid Build Coastguard Worker bool getTargetIndex(StringRef Name, int &Index);
237*9880d681SAndroid Build Coastguard Worker
238*9880d681SAndroid Build Coastguard Worker void initNames2DirectTargetFlags();
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker /// Try to convert a name of a direct target flag to the corresponding
241*9880d681SAndroid Build Coastguard Worker /// target flag.
242*9880d681SAndroid Build Coastguard Worker ///
243*9880d681SAndroid Build Coastguard Worker /// Return true if the name isn't a name of a direct flag.
244*9880d681SAndroid Build Coastguard Worker bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
245*9880d681SAndroid Build Coastguard Worker
246*9880d681SAndroid Build Coastguard Worker void initNames2BitmaskTargetFlags();
247*9880d681SAndroid Build Coastguard Worker
248*9880d681SAndroid Build Coastguard Worker /// Try to convert a name of a bitmask target flag to the corresponding
249*9880d681SAndroid Build Coastguard Worker /// target flag.
250*9880d681SAndroid Build Coastguard Worker ///
251*9880d681SAndroid Build Coastguard Worker /// Return true if the name isn't a name of a bitmask target flag.
252*9880d681SAndroid Build Coastguard Worker bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
253*9880d681SAndroid Build Coastguard Worker };
254*9880d681SAndroid Build Coastguard Worker
255*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
256*9880d681SAndroid Build Coastguard Worker
MIParser(const PerFunctionMIParsingState & PFS,SMDiagnostic & Error,StringRef Source)257*9880d681SAndroid Build Coastguard Worker MIParser::MIParser(const PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
258*9880d681SAndroid Build Coastguard Worker StringRef Source)
259*9880d681SAndroid Build Coastguard Worker : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
260*9880d681SAndroid Build Coastguard Worker {}
261*9880d681SAndroid Build Coastguard Worker
lex(unsigned SkipChar)262*9880d681SAndroid Build Coastguard Worker void MIParser::lex(unsigned SkipChar) {
263*9880d681SAndroid Build Coastguard Worker CurrentSource = lexMIToken(
264*9880d681SAndroid Build Coastguard Worker CurrentSource.data() + SkipChar, Token,
265*9880d681SAndroid Build Coastguard Worker [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
266*9880d681SAndroid Build Coastguard Worker }
267*9880d681SAndroid Build Coastguard Worker
error(const Twine & Msg)268*9880d681SAndroid Build Coastguard Worker bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
269*9880d681SAndroid Build Coastguard Worker
error(StringRef::iterator Loc,const Twine & Msg)270*9880d681SAndroid Build Coastguard Worker bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
271*9880d681SAndroid Build Coastguard Worker const SourceMgr &SM = *PFS.SM;
272*9880d681SAndroid Build Coastguard Worker assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
273*9880d681SAndroid Build Coastguard Worker const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
274*9880d681SAndroid Build Coastguard Worker if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
275*9880d681SAndroid Build Coastguard Worker // Create an ordinary diagnostic when the source manager's buffer is the
276*9880d681SAndroid Build Coastguard Worker // source string.
277*9880d681SAndroid Build Coastguard Worker Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
278*9880d681SAndroid Build Coastguard Worker return true;
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker // Create a diagnostic for a YAML string literal.
281*9880d681SAndroid Build Coastguard Worker Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
282*9880d681SAndroid Build Coastguard Worker Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
283*9880d681SAndroid Build Coastguard Worker Source, None, None);
284*9880d681SAndroid Build Coastguard Worker return true;
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker
toString(MIToken::TokenKind TokenKind)287*9880d681SAndroid Build Coastguard Worker static const char *toString(MIToken::TokenKind TokenKind) {
288*9880d681SAndroid Build Coastguard Worker switch (TokenKind) {
289*9880d681SAndroid Build Coastguard Worker case MIToken::comma:
290*9880d681SAndroid Build Coastguard Worker return "','";
291*9880d681SAndroid Build Coastguard Worker case MIToken::equal:
292*9880d681SAndroid Build Coastguard Worker return "'='";
293*9880d681SAndroid Build Coastguard Worker case MIToken::colon:
294*9880d681SAndroid Build Coastguard Worker return "':'";
295*9880d681SAndroid Build Coastguard Worker case MIToken::lparen:
296*9880d681SAndroid Build Coastguard Worker return "'('";
297*9880d681SAndroid Build Coastguard Worker case MIToken::rparen:
298*9880d681SAndroid Build Coastguard Worker return "')'";
299*9880d681SAndroid Build Coastguard Worker default:
300*9880d681SAndroid Build Coastguard Worker return "<unknown token>";
301*9880d681SAndroid Build Coastguard Worker }
302*9880d681SAndroid Build Coastguard Worker }
303*9880d681SAndroid Build Coastguard Worker
expectAndConsume(MIToken::TokenKind TokenKind)304*9880d681SAndroid Build Coastguard Worker bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
305*9880d681SAndroid Build Coastguard Worker if (Token.isNot(TokenKind))
306*9880d681SAndroid Build Coastguard Worker return error(Twine("expected ") + toString(TokenKind));
307*9880d681SAndroid Build Coastguard Worker lex();
308*9880d681SAndroid Build Coastguard Worker return false;
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
consumeIfPresent(MIToken::TokenKind TokenKind)311*9880d681SAndroid Build Coastguard Worker bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
312*9880d681SAndroid Build Coastguard Worker if (Token.isNot(TokenKind))
313*9880d681SAndroid Build Coastguard Worker return false;
314*9880d681SAndroid Build Coastguard Worker lex();
315*9880d681SAndroid Build Coastguard Worker return true;
316*9880d681SAndroid Build Coastguard Worker }
317*9880d681SAndroid Build Coastguard Worker
parseBasicBlockDefinition(DenseMap<unsigned,MachineBasicBlock * > & MBBSlots)318*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlockDefinition(
319*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
320*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::MachineBasicBlockLabel));
321*9880d681SAndroid Build Coastguard Worker unsigned ID = 0;
322*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
323*9880d681SAndroid Build Coastguard Worker return true;
324*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
325*9880d681SAndroid Build Coastguard Worker auto Name = Token.stringValue();
326*9880d681SAndroid Build Coastguard Worker lex();
327*9880d681SAndroid Build Coastguard Worker bool HasAddressTaken = false;
328*9880d681SAndroid Build Coastguard Worker bool IsLandingPad = false;
329*9880d681SAndroid Build Coastguard Worker unsigned Alignment = 0;
330*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = nullptr;
331*9880d681SAndroid Build Coastguard Worker if (consumeIfPresent(MIToken::lparen)) {
332*9880d681SAndroid Build Coastguard Worker do {
333*9880d681SAndroid Build Coastguard Worker // TODO: Report an error when multiple same attributes are specified.
334*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
335*9880d681SAndroid Build Coastguard Worker case MIToken::kw_address_taken:
336*9880d681SAndroid Build Coastguard Worker HasAddressTaken = true;
337*9880d681SAndroid Build Coastguard Worker lex();
338*9880d681SAndroid Build Coastguard Worker break;
339*9880d681SAndroid Build Coastguard Worker case MIToken::kw_landing_pad:
340*9880d681SAndroid Build Coastguard Worker IsLandingPad = true;
341*9880d681SAndroid Build Coastguard Worker lex();
342*9880d681SAndroid Build Coastguard Worker break;
343*9880d681SAndroid Build Coastguard Worker case MIToken::kw_align:
344*9880d681SAndroid Build Coastguard Worker if (parseAlignment(Alignment))
345*9880d681SAndroid Build Coastguard Worker return true;
346*9880d681SAndroid Build Coastguard Worker break;
347*9880d681SAndroid Build Coastguard Worker case MIToken::IRBlock:
348*9880d681SAndroid Build Coastguard Worker // TODO: Report an error when both name and ir block are specified.
349*9880d681SAndroid Build Coastguard Worker if (parseIRBlock(BB, *MF.getFunction()))
350*9880d681SAndroid Build Coastguard Worker return true;
351*9880d681SAndroid Build Coastguard Worker lex();
352*9880d681SAndroid Build Coastguard Worker break;
353*9880d681SAndroid Build Coastguard Worker default:
354*9880d681SAndroid Build Coastguard Worker break;
355*9880d681SAndroid Build Coastguard Worker }
356*9880d681SAndroid Build Coastguard Worker } while (consumeIfPresent(MIToken::comma));
357*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
358*9880d681SAndroid Build Coastguard Worker return true;
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::colon))
361*9880d681SAndroid Build Coastguard Worker return true;
362*9880d681SAndroid Build Coastguard Worker
363*9880d681SAndroid Build Coastguard Worker if (!Name.empty()) {
364*9880d681SAndroid Build Coastguard Worker BB = dyn_cast_or_null<BasicBlock>(
365*9880d681SAndroid Build Coastguard Worker MF.getFunction()->getValueSymbolTable().lookup(Name));
366*9880d681SAndroid Build Coastguard Worker if (!BB)
367*9880d681SAndroid Build Coastguard Worker return error(Loc, Twine("basic block '") + Name +
368*9880d681SAndroid Build Coastguard Worker "' is not defined in the function '" +
369*9880d681SAndroid Build Coastguard Worker MF.getName() + "'");
370*9880d681SAndroid Build Coastguard Worker }
371*9880d681SAndroid Build Coastguard Worker auto *MBB = MF.CreateMachineBasicBlock(BB);
372*9880d681SAndroid Build Coastguard Worker MF.insert(MF.end(), MBB);
373*9880d681SAndroid Build Coastguard Worker bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
374*9880d681SAndroid Build Coastguard Worker if (!WasInserted)
375*9880d681SAndroid Build Coastguard Worker return error(Loc, Twine("redefinition of machine basic block with id #") +
376*9880d681SAndroid Build Coastguard Worker Twine(ID));
377*9880d681SAndroid Build Coastguard Worker if (Alignment)
378*9880d681SAndroid Build Coastguard Worker MBB->setAlignment(Alignment);
379*9880d681SAndroid Build Coastguard Worker if (HasAddressTaken)
380*9880d681SAndroid Build Coastguard Worker MBB->setHasAddressTaken();
381*9880d681SAndroid Build Coastguard Worker MBB->setIsEHPad(IsLandingPad);
382*9880d681SAndroid Build Coastguard Worker return false;
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker
parseBasicBlockDefinitions(DenseMap<unsigned,MachineBasicBlock * > & MBBSlots)385*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlockDefinitions(
386*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
387*9880d681SAndroid Build Coastguard Worker lex();
388*9880d681SAndroid Build Coastguard Worker // Skip until the first machine basic block.
389*9880d681SAndroid Build Coastguard Worker while (Token.is(MIToken::Newline))
390*9880d681SAndroid Build Coastguard Worker lex();
391*9880d681SAndroid Build Coastguard Worker if (Token.isErrorOrEOF())
392*9880d681SAndroid Build Coastguard Worker return Token.isError();
393*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::MachineBasicBlockLabel))
394*9880d681SAndroid Build Coastguard Worker return error("expected a basic block definition before instructions");
395*9880d681SAndroid Build Coastguard Worker unsigned BraceDepth = 0;
396*9880d681SAndroid Build Coastguard Worker do {
397*9880d681SAndroid Build Coastguard Worker if (parseBasicBlockDefinition(MBBSlots))
398*9880d681SAndroid Build Coastguard Worker return true;
399*9880d681SAndroid Build Coastguard Worker bool IsAfterNewline = false;
400*9880d681SAndroid Build Coastguard Worker // Skip until the next machine basic block.
401*9880d681SAndroid Build Coastguard Worker while (true) {
402*9880d681SAndroid Build Coastguard Worker if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
403*9880d681SAndroid Build Coastguard Worker Token.isErrorOrEOF())
404*9880d681SAndroid Build Coastguard Worker break;
405*9880d681SAndroid Build Coastguard Worker else if (Token.is(MIToken::MachineBasicBlockLabel))
406*9880d681SAndroid Build Coastguard Worker return error("basic block definition should be located at the start of "
407*9880d681SAndroid Build Coastguard Worker "the line");
408*9880d681SAndroid Build Coastguard Worker else if (consumeIfPresent(MIToken::Newline)) {
409*9880d681SAndroid Build Coastguard Worker IsAfterNewline = true;
410*9880d681SAndroid Build Coastguard Worker continue;
411*9880d681SAndroid Build Coastguard Worker }
412*9880d681SAndroid Build Coastguard Worker IsAfterNewline = false;
413*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::lbrace))
414*9880d681SAndroid Build Coastguard Worker ++BraceDepth;
415*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::rbrace)) {
416*9880d681SAndroid Build Coastguard Worker if (!BraceDepth)
417*9880d681SAndroid Build Coastguard Worker return error("extraneous closing brace ('}')");
418*9880d681SAndroid Build Coastguard Worker --BraceDepth;
419*9880d681SAndroid Build Coastguard Worker }
420*9880d681SAndroid Build Coastguard Worker lex();
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker // Verify that we closed all of the '{' at the end of a file or a block.
423*9880d681SAndroid Build Coastguard Worker if (!Token.isError() && BraceDepth)
424*9880d681SAndroid Build Coastguard Worker return error("expected '}'"); // FIXME: Report a note that shows '{'.
425*9880d681SAndroid Build Coastguard Worker } while (!Token.isErrorOrEOF());
426*9880d681SAndroid Build Coastguard Worker return Token.isError();
427*9880d681SAndroid Build Coastguard Worker }
428*9880d681SAndroid Build Coastguard Worker
parseBasicBlockLiveins(MachineBasicBlock & MBB)429*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
430*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_liveins));
431*9880d681SAndroid Build Coastguard Worker lex();
432*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::colon))
433*9880d681SAndroid Build Coastguard Worker return true;
434*9880d681SAndroid Build Coastguard Worker if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
435*9880d681SAndroid Build Coastguard Worker return false;
436*9880d681SAndroid Build Coastguard Worker do {
437*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::NamedRegister))
438*9880d681SAndroid Build Coastguard Worker return error("expected a named register");
439*9880d681SAndroid Build Coastguard Worker unsigned Reg = 0;
440*9880d681SAndroid Build Coastguard Worker if (parseRegister(Reg))
441*9880d681SAndroid Build Coastguard Worker return true;
442*9880d681SAndroid Build Coastguard Worker MBB.addLiveIn(Reg);
443*9880d681SAndroid Build Coastguard Worker lex();
444*9880d681SAndroid Build Coastguard Worker } while (consumeIfPresent(MIToken::comma));
445*9880d681SAndroid Build Coastguard Worker return false;
446*9880d681SAndroid Build Coastguard Worker }
447*9880d681SAndroid Build Coastguard Worker
parseBasicBlockSuccessors(MachineBasicBlock & MBB)448*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
449*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_successors));
450*9880d681SAndroid Build Coastguard Worker lex();
451*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::colon))
452*9880d681SAndroid Build Coastguard Worker return true;
453*9880d681SAndroid Build Coastguard Worker if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
454*9880d681SAndroid Build Coastguard Worker return false;
455*9880d681SAndroid Build Coastguard Worker do {
456*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::MachineBasicBlock))
457*9880d681SAndroid Build Coastguard Worker return error("expected a machine basic block reference");
458*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccMBB = nullptr;
459*9880d681SAndroid Build Coastguard Worker if (parseMBBReference(SuccMBB))
460*9880d681SAndroid Build Coastguard Worker return true;
461*9880d681SAndroid Build Coastguard Worker lex();
462*9880d681SAndroid Build Coastguard Worker unsigned Weight = 0;
463*9880d681SAndroid Build Coastguard Worker if (consumeIfPresent(MIToken::lparen)) {
464*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
465*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal after '('");
466*9880d681SAndroid Build Coastguard Worker if (getUnsigned(Weight))
467*9880d681SAndroid Build Coastguard Worker return true;
468*9880d681SAndroid Build Coastguard Worker lex();
469*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
470*9880d681SAndroid Build Coastguard Worker return true;
471*9880d681SAndroid Build Coastguard Worker }
472*9880d681SAndroid Build Coastguard Worker MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
473*9880d681SAndroid Build Coastguard Worker } while (consumeIfPresent(MIToken::comma));
474*9880d681SAndroid Build Coastguard Worker MBB.normalizeSuccProbs();
475*9880d681SAndroid Build Coastguard Worker return false;
476*9880d681SAndroid Build Coastguard Worker }
477*9880d681SAndroid Build Coastguard Worker
parseBasicBlock(MachineBasicBlock & MBB)478*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlock(MachineBasicBlock &MBB) {
479*9880d681SAndroid Build Coastguard Worker // Skip the definition.
480*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::MachineBasicBlockLabel));
481*9880d681SAndroid Build Coastguard Worker lex();
482*9880d681SAndroid Build Coastguard Worker if (consumeIfPresent(MIToken::lparen)) {
483*9880d681SAndroid Build Coastguard Worker while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
484*9880d681SAndroid Build Coastguard Worker lex();
485*9880d681SAndroid Build Coastguard Worker consumeIfPresent(MIToken::rparen);
486*9880d681SAndroid Build Coastguard Worker }
487*9880d681SAndroid Build Coastguard Worker consumeIfPresent(MIToken::colon);
488*9880d681SAndroid Build Coastguard Worker
489*9880d681SAndroid Build Coastguard Worker // Parse the liveins and successors.
490*9880d681SAndroid Build Coastguard Worker // N.B: Multiple lists of successors and liveins are allowed and they're
491*9880d681SAndroid Build Coastguard Worker // merged into one.
492*9880d681SAndroid Build Coastguard Worker // Example:
493*9880d681SAndroid Build Coastguard Worker // liveins: %edi
494*9880d681SAndroid Build Coastguard Worker // liveins: %esi
495*9880d681SAndroid Build Coastguard Worker //
496*9880d681SAndroid Build Coastguard Worker // is equivalent to
497*9880d681SAndroid Build Coastguard Worker // liveins: %edi, %esi
498*9880d681SAndroid Build Coastguard Worker while (true) {
499*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::kw_successors)) {
500*9880d681SAndroid Build Coastguard Worker if (parseBasicBlockSuccessors(MBB))
501*9880d681SAndroid Build Coastguard Worker return true;
502*9880d681SAndroid Build Coastguard Worker } else if (Token.is(MIToken::kw_liveins)) {
503*9880d681SAndroid Build Coastguard Worker if (parseBasicBlockLiveins(MBB))
504*9880d681SAndroid Build Coastguard Worker return true;
505*9880d681SAndroid Build Coastguard Worker } else if (consumeIfPresent(MIToken::Newline)) {
506*9880d681SAndroid Build Coastguard Worker continue;
507*9880d681SAndroid Build Coastguard Worker } else
508*9880d681SAndroid Build Coastguard Worker break;
509*9880d681SAndroid Build Coastguard Worker if (!Token.isNewlineOrEOF())
510*9880d681SAndroid Build Coastguard Worker return error("expected line break at the end of a list");
511*9880d681SAndroid Build Coastguard Worker lex();
512*9880d681SAndroid Build Coastguard Worker }
513*9880d681SAndroid Build Coastguard Worker
514*9880d681SAndroid Build Coastguard Worker // Parse the instructions.
515*9880d681SAndroid Build Coastguard Worker bool IsInBundle = false;
516*9880d681SAndroid Build Coastguard Worker MachineInstr *PrevMI = nullptr;
517*9880d681SAndroid Build Coastguard Worker while (true) {
518*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))
519*9880d681SAndroid Build Coastguard Worker return false;
520*9880d681SAndroid Build Coastguard Worker else if (consumeIfPresent(MIToken::Newline))
521*9880d681SAndroid Build Coastguard Worker continue;
522*9880d681SAndroid Build Coastguard Worker if (consumeIfPresent(MIToken::rbrace)) {
523*9880d681SAndroid Build Coastguard Worker // The first parsing pass should verify that all closing '}' have an
524*9880d681SAndroid Build Coastguard Worker // opening '{'.
525*9880d681SAndroid Build Coastguard Worker assert(IsInBundle);
526*9880d681SAndroid Build Coastguard Worker IsInBundle = false;
527*9880d681SAndroid Build Coastguard Worker continue;
528*9880d681SAndroid Build Coastguard Worker }
529*9880d681SAndroid Build Coastguard Worker MachineInstr *MI = nullptr;
530*9880d681SAndroid Build Coastguard Worker if (parse(MI))
531*9880d681SAndroid Build Coastguard Worker return true;
532*9880d681SAndroid Build Coastguard Worker MBB.insert(MBB.end(), MI);
533*9880d681SAndroid Build Coastguard Worker if (IsInBundle) {
534*9880d681SAndroid Build Coastguard Worker PrevMI->setFlag(MachineInstr::BundledSucc);
535*9880d681SAndroid Build Coastguard Worker MI->setFlag(MachineInstr::BundledPred);
536*9880d681SAndroid Build Coastguard Worker }
537*9880d681SAndroid Build Coastguard Worker PrevMI = MI;
538*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::lbrace)) {
539*9880d681SAndroid Build Coastguard Worker if (IsInBundle)
540*9880d681SAndroid Build Coastguard Worker return error("nested instruction bundles are not allowed");
541*9880d681SAndroid Build Coastguard Worker lex();
542*9880d681SAndroid Build Coastguard Worker // This instruction is the start of the bundle.
543*9880d681SAndroid Build Coastguard Worker MI->setFlag(MachineInstr::BundledSucc);
544*9880d681SAndroid Build Coastguard Worker IsInBundle = true;
545*9880d681SAndroid Build Coastguard Worker if (!Token.is(MIToken::Newline))
546*9880d681SAndroid Build Coastguard Worker // The next instruction can be on the same line.
547*9880d681SAndroid Build Coastguard Worker continue;
548*9880d681SAndroid Build Coastguard Worker }
549*9880d681SAndroid Build Coastguard Worker assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
550*9880d681SAndroid Build Coastguard Worker lex();
551*9880d681SAndroid Build Coastguard Worker }
552*9880d681SAndroid Build Coastguard Worker return false;
553*9880d681SAndroid Build Coastguard Worker }
554*9880d681SAndroid Build Coastguard Worker
parseBasicBlocks()555*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBasicBlocks() {
556*9880d681SAndroid Build Coastguard Worker lex();
557*9880d681SAndroid Build Coastguard Worker // Skip until the first machine basic block.
558*9880d681SAndroid Build Coastguard Worker while (Token.is(MIToken::Newline))
559*9880d681SAndroid Build Coastguard Worker lex();
560*9880d681SAndroid Build Coastguard Worker if (Token.isErrorOrEOF())
561*9880d681SAndroid Build Coastguard Worker return Token.isError();
562*9880d681SAndroid Build Coastguard Worker // The first parsing pass should have verified that this token is a MBB label
563*9880d681SAndroid Build Coastguard Worker // in the 'parseBasicBlockDefinitions' method.
564*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::MachineBasicBlockLabel));
565*9880d681SAndroid Build Coastguard Worker do {
566*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB = nullptr;
567*9880d681SAndroid Build Coastguard Worker if (parseMBBReference(MBB))
568*9880d681SAndroid Build Coastguard Worker return true;
569*9880d681SAndroid Build Coastguard Worker if (parseBasicBlock(*MBB))
570*9880d681SAndroid Build Coastguard Worker return true;
571*9880d681SAndroid Build Coastguard Worker // The method 'parseBasicBlock' should parse the whole block until the next
572*9880d681SAndroid Build Coastguard Worker // block or the end of file.
573*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
574*9880d681SAndroid Build Coastguard Worker } while (Token.isNot(MIToken::Eof));
575*9880d681SAndroid Build Coastguard Worker return false;
576*9880d681SAndroid Build Coastguard Worker }
577*9880d681SAndroid Build Coastguard Worker
parse(MachineInstr * & MI)578*9880d681SAndroid Build Coastguard Worker bool MIParser::parse(MachineInstr *&MI) {
579*9880d681SAndroid Build Coastguard Worker // Parse any register operands before '='
580*9880d681SAndroid Build Coastguard Worker MachineOperand MO = MachineOperand::CreateImm(0);
581*9880d681SAndroid Build Coastguard Worker SmallVector<ParsedMachineOperand, 8> Operands;
582*9880d681SAndroid Build Coastguard Worker while (Token.isRegister() || Token.isRegisterFlag()) {
583*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
584*9880d681SAndroid Build Coastguard Worker Optional<unsigned> TiedDefIdx;
585*9880d681SAndroid Build Coastguard Worker if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
586*9880d681SAndroid Build Coastguard Worker return true;
587*9880d681SAndroid Build Coastguard Worker Operands.push_back(
588*9880d681SAndroid Build Coastguard Worker ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
589*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::comma))
590*9880d681SAndroid Build Coastguard Worker break;
591*9880d681SAndroid Build Coastguard Worker lex();
592*9880d681SAndroid Build Coastguard Worker }
593*9880d681SAndroid Build Coastguard Worker if (!Operands.empty() && expectAndConsume(MIToken::equal))
594*9880d681SAndroid Build Coastguard Worker return true;
595*9880d681SAndroid Build Coastguard Worker
596*9880d681SAndroid Build Coastguard Worker unsigned OpCode, Flags = 0;
597*9880d681SAndroid Build Coastguard Worker if (Token.isError() || parseInstruction(OpCode, Flags))
598*9880d681SAndroid Build Coastguard Worker return true;
599*9880d681SAndroid Build Coastguard Worker
600*9880d681SAndroid Build Coastguard Worker Type *Ty = nullptr;
601*9880d681SAndroid Build Coastguard Worker if (isPreISelGenericOpcode(OpCode)) {
602*9880d681SAndroid Build Coastguard Worker // For generic opcode, a type is mandatory.
603*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
604*9880d681SAndroid Build Coastguard Worker if (parseIRType(Loc, Ty))
605*9880d681SAndroid Build Coastguard Worker return true;
606*9880d681SAndroid Build Coastguard Worker }
607*9880d681SAndroid Build Coastguard Worker
608*9880d681SAndroid Build Coastguard Worker // Parse the remaining machine operands.
609*9880d681SAndroid Build Coastguard Worker while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
610*9880d681SAndroid Build Coastguard Worker Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
611*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
612*9880d681SAndroid Build Coastguard Worker Optional<unsigned> TiedDefIdx;
613*9880d681SAndroid Build Coastguard Worker if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
614*9880d681SAndroid Build Coastguard Worker return true;
615*9880d681SAndroid Build Coastguard Worker Operands.push_back(
616*9880d681SAndroid Build Coastguard Worker ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
617*9880d681SAndroid Build Coastguard Worker if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
618*9880d681SAndroid Build Coastguard Worker Token.is(MIToken::lbrace))
619*9880d681SAndroid Build Coastguard Worker break;
620*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::comma))
621*9880d681SAndroid Build Coastguard Worker return error("expected ',' before the next machine operand");
622*9880d681SAndroid Build Coastguard Worker lex();
623*9880d681SAndroid Build Coastguard Worker }
624*9880d681SAndroid Build Coastguard Worker
625*9880d681SAndroid Build Coastguard Worker DebugLoc DebugLocation;
626*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::kw_debug_location)) {
627*9880d681SAndroid Build Coastguard Worker lex();
628*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::exclaim))
629*9880d681SAndroid Build Coastguard Worker return error("expected a metadata node after 'debug-location'");
630*9880d681SAndroid Build Coastguard Worker MDNode *Node = nullptr;
631*9880d681SAndroid Build Coastguard Worker if (parseMDNode(Node))
632*9880d681SAndroid Build Coastguard Worker return true;
633*9880d681SAndroid Build Coastguard Worker DebugLocation = DebugLoc(Node);
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
636*9880d681SAndroid Build Coastguard Worker // Parse the machine memory operands.
637*9880d681SAndroid Build Coastguard Worker SmallVector<MachineMemOperand *, 2> MemOperands;
638*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::coloncolon)) {
639*9880d681SAndroid Build Coastguard Worker lex();
640*9880d681SAndroid Build Coastguard Worker while (!Token.isNewlineOrEOF()) {
641*9880d681SAndroid Build Coastguard Worker MachineMemOperand *MemOp = nullptr;
642*9880d681SAndroid Build Coastguard Worker if (parseMachineMemoryOperand(MemOp))
643*9880d681SAndroid Build Coastguard Worker return true;
644*9880d681SAndroid Build Coastguard Worker MemOperands.push_back(MemOp);
645*9880d681SAndroid Build Coastguard Worker if (Token.isNewlineOrEOF())
646*9880d681SAndroid Build Coastguard Worker break;
647*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::comma))
648*9880d681SAndroid Build Coastguard Worker return error("expected ',' before the next machine memory operand");
649*9880d681SAndroid Build Coastguard Worker lex();
650*9880d681SAndroid Build Coastguard Worker }
651*9880d681SAndroid Build Coastguard Worker }
652*9880d681SAndroid Build Coastguard Worker
653*9880d681SAndroid Build Coastguard Worker const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
654*9880d681SAndroid Build Coastguard Worker if (!MCID.isVariadic()) {
655*9880d681SAndroid Build Coastguard Worker // FIXME: Move the implicit operand verification to the machine verifier.
656*9880d681SAndroid Build Coastguard Worker if (verifyImplicitOperands(Operands, MCID))
657*9880d681SAndroid Build Coastguard Worker return true;
658*9880d681SAndroid Build Coastguard Worker }
659*9880d681SAndroid Build Coastguard Worker
660*9880d681SAndroid Build Coastguard Worker // TODO: Check for extraneous machine operands.
661*9880d681SAndroid Build Coastguard Worker MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
662*9880d681SAndroid Build Coastguard Worker MI->setFlags(Flags);
663*9880d681SAndroid Build Coastguard Worker if (Ty)
664*9880d681SAndroid Build Coastguard Worker MI->setType(Ty);
665*9880d681SAndroid Build Coastguard Worker for (const auto &Operand : Operands)
666*9880d681SAndroid Build Coastguard Worker MI->addOperand(MF, Operand.Operand);
667*9880d681SAndroid Build Coastguard Worker if (assignRegisterTies(*MI, Operands))
668*9880d681SAndroid Build Coastguard Worker return true;
669*9880d681SAndroid Build Coastguard Worker if (MemOperands.empty())
670*9880d681SAndroid Build Coastguard Worker return false;
671*9880d681SAndroid Build Coastguard Worker MachineInstr::mmo_iterator MemRefs =
672*9880d681SAndroid Build Coastguard Worker MF.allocateMemRefsArray(MemOperands.size());
673*9880d681SAndroid Build Coastguard Worker std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
674*9880d681SAndroid Build Coastguard Worker MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
675*9880d681SAndroid Build Coastguard Worker return false;
676*9880d681SAndroid Build Coastguard Worker }
677*9880d681SAndroid Build Coastguard Worker
parseStandaloneMBB(MachineBasicBlock * & MBB)678*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
679*9880d681SAndroid Build Coastguard Worker lex();
680*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::MachineBasicBlock))
681*9880d681SAndroid Build Coastguard Worker return error("expected a machine basic block reference");
682*9880d681SAndroid Build Coastguard Worker if (parseMBBReference(MBB))
683*9880d681SAndroid Build Coastguard Worker return true;
684*9880d681SAndroid Build Coastguard Worker lex();
685*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Eof))
686*9880d681SAndroid Build Coastguard Worker return error(
687*9880d681SAndroid Build Coastguard Worker "expected end of string after the machine basic block reference");
688*9880d681SAndroid Build Coastguard Worker return false;
689*9880d681SAndroid Build Coastguard Worker }
690*9880d681SAndroid Build Coastguard Worker
parseStandaloneNamedRegister(unsigned & Reg)691*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
692*9880d681SAndroid Build Coastguard Worker lex();
693*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::NamedRegister))
694*9880d681SAndroid Build Coastguard Worker return error("expected a named register");
695*9880d681SAndroid Build Coastguard Worker if (parseRegister(Reg))
696*9880d681SAndroid Build Coastguard Worker return true;
697*9880d681SAndroid Build Coastguard Worker lex();
698*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Eof))
699*9880d681SAndroid Build Coastguard Worker return error("expected end of string after the register reference");
700*9880d681SAndroid Build Coastguard Worker return false;
701*9880d681SAndroid Build Coastguard Worker }
702*9880d681SAndroid Build Coastguard Worker
parseStandaloneVirtualRegister(unsigned & Reg)703*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStandaloneVirtualRegister(unsigned &Reg) {
704*9880d681SAndroid Build Coastguard Worker lex();
705*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::VirtualRegister))
706*9880d681SAndroid Build Coastguard Worker return error("expected a virtual register");
707*9880d681SAndroid Build Coastguard Worker if (parseRegister(Reg))
708*9880d681SAndroid Build Coastguard Worker return true;
709*9880d681SAndroid Build Coastguard Worker lex();
710*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Eof))
711*9880d681SAndroid Build Coastguard Worker return error("expected end of string after the register reference");
712*9880d681SAndroid Build Coastguard Worker return false;
713*9880d681SAndroid Build Coastguard Worker }
714*9880d681SAndroid Build Coastguard Worker
parseStandaloneStackObject(int & FI)715*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStandaloneStackObject(int &FI) {
716*9880d681SAndroid Build Coastguard Worker lex();
717*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::StackObject))
718*9880d681SAndroid Build Coastguard Worker return error("expected a stack object");
719*9880d681SAndroid Build Coastguard Worker if (parseStackFrameIndex(FI))
720*9880d681SAndroid Build Coastguard Worker return true;
721*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Eof))
722*9880d681SAndroid Build Coastguard Worker return error("expected end of string after the stack object reference");
723*9880d681SAndroid Build Coastguard Worker return false;
724*9880d681SAndroid Build Coastguard Worker }
725*9880d681SAndroid Build Coastguard Worker
parseStandaloneMDNode(MDNode * & Node)726*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
727*9880d681SAndroid Build Coastguard Worker lex();
728*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::exclaim))
729*9880d681SAndroid Build Coastguard Worker return error("expected a metadata node");
730*9880d681SAndroid Build Coastguard Worker if (parseMDNode(Node))
731*9880d681SAndroid Build Coastguard Worker return true;
732*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Eof))
733*9880d681SAndroid Build Coastguard Worker return error("expected end of string after the metadata node");
734*9880d681SAndroid Build Coastguard Worker return false;
735*9880d681SAndroid Build Coastguard Worker }
736*9880d681SAndroid Build Coastguard Worker
printImplicitRegisterFlag(const MachineOperand & MO)737*9880d681SAndroid Build Coastguard Worker static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
738*9880d681SAndroid Build Coastguard Worker assert(MO.isImplicit());
739*9880d681SAndroid Build Coastguard Worker return MO.isDef() ? "implicit-def" : "implicit";
740*9880d681SAndroid Build Coastguard Worker }
741*9880d681SAndroid Build Coastguard Worker
getRegisterName(const TargetRegisterInfo * TRI,unsigned Reg)742*9880d681SAndroid Build Coastguard Worker static std::string getRegisterName(const TargetRegisterInfo *TRI,
743*9880d681SAndroid Build Coastguard Worker unsigned Reg) {
744*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
745*9880d681SAndroid Build Coastguard Worker return StringRef(TRI->getName(Reg)).lower();
746*9880d681SAndroid Build Coastguard Worker }
747*9880d681SAndroid Build Coastguard Worker
748*9880d681SAndroid Build Coastguard Worker /// Return true if the parsed machine operands contain a given machine operand.
isImplicitOperandIn(const MachineOperand & ImplicitOperand,ArrayRef<ParsedMachineOperand> Operands)749*9880d681SAndroid Build Coastguard Worker static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
750*9880d681SAndroid Build Coastguard Worker ArrayRef<ParsedMachineOperand> Operands) {
751*9880d681SAndroid Build Coastguard Worker for (const auto &I : Operands) {
752*9880d681SAndroid Build Coastguard Worker if (ImplicitOperand.isIdenticalTo(I.Operand))
753*9880d681SAndroid Build Coastguard Worker return true;
754*9880d681SAndroid Build Coastguard Worker }
755*9880d681SAndroid Build Coastguard Worker return false;
756*9880d681SAndroid Build Coastguard Worker }
757*9880d681SAndroid Build Coastguard Worker
verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,const MCInstrDesc & MCID)758*9880d681SAndroid Build Coastguard Worker bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
759*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID) {
760*9880d681SAndroid Build Coastguard Worker if (MCID.isCall())
761*9880d681SAndroid Build Coastguard Worker // We can't verify call instructions as they can contain arbitrary implicit
762*9880d681SAndroid Build Coastguard Worker // register and register mask operands.
763*9880d681SAndroid Build Coastguard Worker return false;
764*9880d681SAndroid Build Coastguard Worker
765*9880d681SAndroid Build Coastguard Worker // Gather all the expected implicit operands.
766*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 4> ImplicitOperands;
767*9880d681SAndroid Build Coastguard Worker if (MCID.ImplicitDefs)
768*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
769*9880d681SAndroid Build Coastguard Worker ImplicitOperands.push_back(
770*9880d681SAndroid Build Coastguard Worker MachineOperand::CreateReg(*ImpDefs, true, true));
771*9880d681SAndroid Build Coastguard Worker if (MCID.ImplicitUses)
772*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
773*9880d681SAndroid Build Coastguard Worker ImplicitOperands.push_back(
774*9880d681SAndroid Build Coastguard Worker MachineOperand::CreateReg(*ImpUses, false, true));
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget().getRegisterInfo();
777*9880d681SAndroid Build Coastguard Worker assert(TRI && "Expected target register info");
778*9880d681SAndroid Build Coastguard Worker for (const auto &I : ImplicitOperands) {
779*9880d681SAndroid Build Coastguard Worker if (isImplicitOperandIn(I, Operands))
780*9880d681SAndroid Build Coastguard Worker continue;
781*9880d681SAndroid Build Coastguard Worker return error(Operands.empty() ? Token.location() : Operands.back().End,
782*9880d681SAndroid Build Coastguard Worker Twine("missing implicit register operand '") +
783*9880d681SAndroid Build Coastguard Worker printImplicitRegisterFlag(I) + " %" +
784*9880d681SAndroid Build Coastguard Worker getRegisterName(TRI, I.getReg()) + "'");
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker return false;
787*9880d681SAndroid Build Coastguard Worker }
788*9880d681SAndroid Build Coastguard Worker
parseInstruction(unsigned & OpCode,unsigned & Flags)789*9880d681SAndroid Build Coastguard Worker bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
790*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::kw_frame_setup)) {
791*9880d681SAndroid Build Coastguard Worker Flags |= MachineInstr::FrameSetup;
792*9880d681SAndroid Build Coastguard Worker lex();
793*9880d681SAndroid Build Coastguard Worker }
794*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier))
795*9880d681SAndroid Build Coastguard Worker return error("expected a machine instruction");
796*9880d681SAndroid Build Coastguard Worker StringRef InstrName = Token.stringValue();
797*9880d681SAndroid Build Coastguard Worker if (parseInstrName(InstrName, OpCode))
798*9880d681SAndroid Build Coastguard Worker return error(Twine("unknown machine instruction name '") + InstrName + "'");
799*9880d681SAndroid Build Coastguard Worker lex();
800*9880d681SAndroid Build Coastguard Worker return false;
801*9880d681SAndroid Build Coastguard Worker }
802*9880d681SAndroid Build Coastguard Worker
parseRegister(unsigned & Reg)803*9880d681SAndroid Build Coastguard Worker bool MIParser::parseRegister(unsigned &Reg) {
804*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
805*9880d681SAndroid Build Coastguard Worker case MIToken::underscore:
806*9880d681SAndroid Build Coastguard Worker Reg = 0;
807*9880d681SAndroid Build Coastguard Worker break;
808*9880d681SAndroid Build Coastguard Worker case MIToken::NamedRegister: {
809*9880d681SAndroid Build Coastguard Worker StringRef Name = Token.stringValue();
810*9880d681SAndroid Build Coastguard Worker if (getRegisterByName(Name, Reg))
811*9880d681SAndroid Build Coastguard Worker return error(Twine("unknown register name '") + Name + "'");
812*9880d681SAndroid Build Coastguard Worker break;
813*9880d681SAndroid Build Coastguard Worker }
814*9880d681SAndroid Build Coastguard Worker case MIToken::VirtualRegister: {
815*9880d681SAndroid Build Coastguard Worker unsigned ID;
816*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
817*9880d681SAndroid Build Coastguard Worker return true;
818*9880d681SAndroid Build Coastguard Worker const auto RegInfo = PFS.VirtualRegisterSlots.find(ID);
819*9880d681SAndroid Build Coastguard Worker if (RegInfo == PFS.VirtualRegisterSlots.end())
820*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined virtual register '%") + Twine(ID) +
821*9880d681SAndroid Build Coastguard Worker "'");
822*9880d681SAndroid Build Coastguard Worker Reg = RegInfo->second;
823*9880d681SAndroid Build Coastguard Worker break;
824*9880d681SAndroid Build Coastguard Worker }
825*9880d681SAndroid Build Coastguard Worker // TODO: Parse other register kinds.
826*9880d681SAndroid Build Coastguard Worker default:
827*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be a register");
828*9880d681SAndroid Build Coastguard Worker }
829*9880d681SAndroid Build Coastguard Worker return false;
830*9880d681SAndroid Build Coastguard Worker }
831*9880d681SAndroid Build Coastguard Worker
parseRegisterFlag(unsigned & Flags)832*9880d681SAndroid Build Coastguard Worker bool MIParser::parseRegisterFlag(unsigned &Flags) {
833*9880d681SAndroid Build Coastguard Worker const unsigned OldFlags = Flags;
834*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
835*9880d681SAndroid Build Coastguard Worker case MIToken::kw_implicit:
836*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Implicit;
837*9880d681SAndroid Build Coastguard Worker break;
838*9880d681SAndroid Build Coastguard Worker case MIToken::kw_implicit_define:
839*9880d681SAndroid Build Coastguard Worker Flags |= RegState::ImplicitDefine;
840*9880d681SAndroid Build Coastguard Worker break;
841*9880d681SAndroid Build Coastguard Worker case MIToken::kw_def:
842*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Define;
843*9880d681SAndroid Build Coastguard Worker break;
844*9880d681SAndroid Build Coastguard Worker case MIToken::kw_dead:
845*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Dead;
846*9880d681SAndroid Build Coastguard Worker break;
847*9880d681SAndroid Build Coastguard Worker case MIToken::kw_killed:
848*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Kill;
849*9880d681SAndroid Build Coastguard Worker break;
850*9880d681SAndroid Build Coastguard Worker case MIToken::kw_undef:
851*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Undef;
852*9880d681SAndroid Build Coastguard Worker break;
853*9880d681SAndroid Build Coastguard Worker case MIToken::kw_internal:
854*9880d681SAndroid Build Coastguard Worker Flags |= RegState::InternalRead;
855*9880d681SAndroid Build Coastguard Worker break;
856*9880d681SAndroid Build Coastguard Worker case MIToken::kw_early_clobber:
857*9880d681SAndroid Build Coastguard Worker Flags |= RegState::EarlyClobber;
858*9880d681SAndroid Build Coastguard Worker break;
859*9880d681SAndroid Build Coastguard Worker case MIToken::kw_debug_use:
860*9880d681SAndroid Build Coastguard Worker Flags |= RegState::Debug;
861*9880d681SAndroid Build Coastguard Worker break;
862*9880d681SAndroid Build Coastguard Worker default:
863*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be a register flag");
864*9880d681SAndroid Build Coastguard Worker }
865*9880d681SAndroid Build Coastguard Worker if (OldFlags == Flags)
866*9880d681SAndroid Build Coastguard Worker // We know that the same flag is specified more than once when the flags
867*9880d681SAndroid Build Coastguard Worker // weren't modified.
868*9880d681SAndroid Build Coastguard Worker return error("duplicate '" + Token.stringValue() + "' register flag");
869*9880d681SAndroid Build Coastguard Worker lex();
870*9880d681SAndroid Build Coastguard Worker return false;
871*9880d681SAndroid Build Coastguard Worker }
872*9880d681SAndroid Build Coastguard Worker
parseSubRegisterIndex(unsigned & SubReg)873*9880d681SAndroid Build Coastguard Worker bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
874*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::colon));
875*9880d681SAndroid Build Coastguard Worker lex();
876*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier))
877*9880d681SAndroid Build Coastguard Worker return error("expected a subregister index after ':'");
878*9880d681SAndroid Build Coastguard Worker auto Name = Token.stringValue();
879*9880d681SAndroid Build Coastguard Worker SubReg = getSubRegIndex(Name);
880*9880d681SAndroid Build Coastguard Worker if (!SubReg)
881*9880d681SAndroid Build Coastguard Worker return error(Twine("use of unknown subregister index '") + Name + "'");
882*9880d681SAndroid Build Coastguard Worker lex();
883*9880d681SAndroid Build Coastguard Worker return false;
884*9880d681SAndroid Build Coastguard Worker }
885*9880d681SAndroid Build Coastguard Worker
parseRegisterTiedDefIndex(unsigned & TiedDefIdx)886*9880d681SAndroid Build Coastguard Worker bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
887*9880d681SAndroid Build Coastguard Worker if (!consumeIfPresent(MIToken::kw_tied_def))
888*9880d681SAndroid Build Coastguard Worker return error("expected 'tied-def' after '('");
889*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
890*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal after 'tied-def'");
891*9880d681SAndroid Build Coastguard Worker if (getUnsigned(TiedDefIdx))
892*9880d681SAndroid Build Coastguard Worker return true;
893*9880d681SAndroid Build Coastguard Worker lex();
894*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
895*9880d681SAndroid Build Coastguard Worker return true;
896*9880d681SAndroid Build Coastguard Worker return false;
897*9880d681SAndroid Build Coastguard Worker }
898*9880d681SAndroid Build Coastguard Worker
parseSize(unsigned & Size)899*9880d681SAndroid Build Coastguard Worker bool MIParser::parseSize(unsigned &Size) {
900*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
901*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal for the size");
902*9880d681SAndroid Build Coastguard Worker if (getUnsigned(Size))
903*9880d681SAndroid Build Coastguard Worker return true;
904*9880d681SAndroid Build Coastguard Worker lex();
905*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
906*9880d681SAndroid Build Coastguard Worker return true;
907*9880d681SAndroid Build Coastguard Worker return false;
908*9880d681SAndroid Build Coastguard Worker }
909*9880d681SAndroid Build Coastguard Worker
assignRegisterTies(MachineInstr & MI,ArrayRef<ParsedMachineOperand> Operands)910*9880d681SAndroid Build Coastguard Worker bool MIParser::assignRegisterTies(MachineInstr &MI,
911*9880d681SAndroid Build Coastguard Worker ArrayRef<ParsedMachineOperand> Operands) {
912*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
913*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
914*9880d681SAndroid Build Coastguard Worker if (!Operands[I].TiedDefIdx)
915*9880d681SAndroid Build Coastguard Worker continue;
916*9880d681SAndroid Build Coastguard Worker // The parser ensures that this operand is a register use, so we just have
917*9880d681SAndroid Build Coastguard Worker // to check the tied-def operand.
918*9880d681SAndroid Build Coastguard Worker unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
919*9880d681SAndroid Build Coastguard Worker if (DefIdx >= E)
920*9880d681SAndroid Build Coastguard Worker return error(Operands[I].Begin,
921*9880d681SAndroid Build Coastguard Worker Twine("use of invalid tied-def operand index '" +
922*9880d681SAndroid Build Coastguard Worker Twine(DefIdx) + "'; instruction has only ") +
923*9880d681SAndroid Build Coastguard Worker Twine(E) + " operands");
924*9880d681SAndroid Build Coastguard Worker const auto &DefOperand = Operands[DefIdx].Operand;
925*9880d681SAndroid Build Coastguard Worker if (!DefOperand.isReg() || !DefOperand.isDef())
926*9880d681SAndroid Build Coastguard Worker // FIXME: add note with the def operand.
927*9880d681SAndroid Build Coastguard Worker return error(Operands[I].Begin,
928*9880d681SAndroid Build Coastguard Worker Twine("use of invalid tied-def operand index '") +
929*9880d681SAndroid Build Coastguard Worker Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
930*9880d681SAndroid Build Coastguard Worker " isn't a defined register");
931*9880d681SAndroid Build Coastguard Worker // Check that the tied-def operand wasn't tied elsewhere.
932*9880d681SAndroid Build Coastguard Worker for (const auto &TiedPair : TiedRegisterPairs) {
933*9880d681SAndroid Build Coastguard Worker if (TiedPair.first == DefIdx)
934*9880d681SAndroid Build Coastguard Worker return error(Operands[I].Begin,
935*9880d681SAndroid Build Coastguard Worker Twine("the tied-def operand #") + Twine(DefIdx) +
936*9880d681SAndroid Build Coastguard Worker " is already tied with another register operand");
937*9880d681SAndroid Build Coastguard Worker }
938*9880d681SAndroid Build Coastguard Worker TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
939*9880d681SAndroid Build Coastguard Worker }
940*9880d681SAndroid Build Coastguard Worker // FIXME: Verify that for non INLINEASM instructions, the def and use tied
941*9880d681SAndroid Build Coastguard Worker // indices must be less than tied max.
942*9880d681SAndroid Build Coastguard Worker for (const auto &TiedPair : TiedRegisterPairs)
943*9880d681SAndroid Build Coastguard Worker MI.tieOperands(TiedPair.first, TiedPair.second);
944*9880d681SAndroid Build Coastguard Worker return false;
945*9880d681SAndroid Build Coastguard Worker }
946*9880d681SAndroid Build Coastguard Worker
parseRegisterOperand(MachineOperand & Dest,Optional<unsigned> & TiedDefIdx,bool IsDef)947*9880d681SAndroid Build Coastguard Worker bool MIParser::parseRegisterOperand(MachineOperand &Dest,
948*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &TiedDefIdx,
949*9880d681SAndroid Build Coastguard Worker bool IsDef) {
950*9880d681SAndroid Build Coastguard Worker unsigned Reg;
951*9880d681SAndroid Build Coastguard Worker unsigned Flags = IsDef ? RegState::Define : 0;
952*9880d681SAndroid Build Coastguard Worker while (Token.isRegisterFlag()) {
953*9880d681SAndroid Build Coastguard Worker if (parseRegisterFlag(Flags))
954*9880d681SAndroid Build Coastguard Worker return true;
955*9880d681SAndroid Build Coastguard Worker }
956*9880d681SAndroid Build Coastguard Worker if (!Token.isRegister())
957*9880d681SAndroid Build Coastguard Worker return error("expected a register after register flags");
958*9880d681SAndroid Build Coastguard Worker if (parseRegister(Reg))
959*9880d681SAndroid Build Coastguard Worker return true;
960*9880d681SAndroid Build Coastguard Worker lex();
961*9880d681SAndroid Build Coastguard Worker unsigned SubReg = 0;
962*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::colon)) {
963*9880d681SAndroid Build Coastguard Worker if (parseSubRegisterIndex(SubReg))
964*9880d681SAndroid Build Coastguard Worker return true;
965*9880d681SAndroid Build Coastguard Worker }
966*9880d681SAndroid Build Coastguard Worker if ((Flags & RegState::Define) == 0) {
967*9880d681SAndroid Build Coastguard Worker if (consumeIfPresent(MIToken::lparen)) {
968*9880d681SAndroid Build Coastguard Worker unsigned Idx;
969*9880d681SAndroid Build Coastguard Worker if (parseRegisterTiedDefIndex(Idx))
970*9880d681SAndroid Build Coastguard Worker return true;
971*9880d681SAndroid Build Coastguard Worker TiedDefIdx = Idx;
972*9880d681SAndroid Build Coastguard Worker }
973*9880d681SAndroid Build Coastguard Worker } else if (consumeIfPresent(MIToken::lparen)) {
974*9880d681SAndroid Build Coastguard Worker // Virtual registers may have a size with GlobalISel.
975*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
976*9880d681SAndroid Build Coastguard Worker return error("unexpected size on physical register");
977*9880d681SAndroid Build Coastguard Worker unsigned Size;
978*9880d681SAndroid Build Coastguard Worker if (parseSize(Size))
979*9880d681SAndroid Build Coastguard Worker return true;
980*9880d681SAndroid Build Coastguard Worker
981*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &MRI = MF.getRegInfo();
982*9880d681SAndroid Build Coastguard Worker MRI.setSize(Reg, Size);
983*9880d681SAndroid Build Coastguard Worker } else if (PFS.GenericVRegs.count(Reg)) {
984*9880d681SAndroid Build Coastguard Worker // Generic virtual registers must have a size.
985*9880d681SAndroid Build Coastguard Worker // If we end up here this means the size hasn't been specified and
986*9880d681SAndroid Build Coastguard Worker // this is bad!
987*9880d681SAndroid Build Coastguard Worker return error("generic virtual registers must have a size");
988*9880d681SAndroid Build Coastguard Worker }
989*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateReg(
990*9880d681SAndroid Build Coastguard Worker Reg, Flags & RegState::Define, Flags & RegState::Implicit,
991*9880d681SAndroid Build Coastguard Worker Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
992*9880d681SAndroid Build Coastguard Worker Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
993*9880d681SAndroid Build Coastguard Worker Flags & RegState::InternalRead);
994*9880d681SAndroid Build Coastguard Worker return false;
995*9880d681SAndroid Build Coastguard Worker }
996*9880d681SAndroid Build Coastguard Worker
parseImmediateOperand(MachineOperand & Dest)997*9880d681SAndroid Build Coastguard Worker bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
998*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::IntegerLiteral));
999*9880d681SAndroid Build Coastguard Worker const APSInt &Int = Token.integerValue();
1000*9880d681SAndroid Build Coastguard Worker if (Int.getMinSignedBits() > 64)
1001*9880d681SAndroid Build Coastguard Worker return error("integer literal is too large to be an immediate operand");
1002*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateImm(Int.getExtValue());
1003*9880d681SAndroid Build Coastguard Worker lex();
1004*9880d681SAndroid Build Coastguard Worker return false;
1005*9880d681SAndroid Build Coastguard Worker }
1006*9880d681SAndroid Build Coastguard Worker
parseIRConstant(StringRef::iterator Loc,StringRef StringValue,const Constant * & C)1007*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1008*9880d681SAndroid Build Coastguard Worker const Constant *&C) {
1009*9880d681SAndroid Build Coastguard Worker auto Source = StringValue.str(); // The source has to be null terminated.
1010*9880d681SAndroid Build Coastguard Worker SMDiagnostic Err;
1011*9880d681SAndroid Build Coastguard Worker C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent(),
1012*9880d681SAndroid Build Coastguard Worker &PFS.IRSlots);
1013*9880d681SAndroid Build Coastguard Worker if (!C)
1014*9880d681SAndroid Build Coastguard Worker return error(Loc + Err.getColumnNo(), Err.getMessage());
1015*9880d681SAndroid Build Coastguard Worker return false;
1016*9880d681SAndroid Build Coastguard Worker }
1017*9880d681SAndroid Build Coastguard Worker
parseIRConstant(StringRef::iterator Loc,const Constant * & C)1018*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1019*9880d681SAndroid Build Coastguard Worker if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1020*9880d681SAndroid Build Coastguard Worker return true;
1021*9880d681SAndroid Build Coastguard Worker lex();
1022*9880d681SAndroid Build Coastguard Worker return false;
1023*9880d681SAndroid Build Coastguard Worker }
1024*9880d681SAndroid Build Coastguard Worker
parseIRType(StringRef::iterator Loc,StringRef StringValue,unsigned & Read,Type * & Ty)1025*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRType(StringRef::iterator Loc, StringRef StringValue,
1026*9880d681SAndroid Build Coastguard Worker unsigned &Read, Type *&Ty) {
1027*9880d681SAndroid Build Coastguard Worker auto Source = StringValue.str(); // The source has to be null terminated.
1028*9880d681SAndroid Build Coastguard Worker SMDiagnostic Err;
1029*9880d681SAndroid Build Coastguard Worker Ty = parseTypeAtBeginning(Source.c_str(), Read, Err,
1030*9880d681SAndroid Build Coastguard Worker *MF.getFunction()->getParent(), &PFS.IRSlots);
1031*9880d681SAndroid Build Coastguard Worker if (!Ty)
1032*9880d681SAndroid Build Coastguard Worker return error(Loc + Err.getColumnNo(), Err.getMessage());
1033*9880d681SAndroid Build Coastguard Worker return false;
1034*9880d681SAndroid Build Coastguard Worker }
1035*9880d681SAndroid Build Coastguard Worker
parseIRType(StringRef::iterator Loc,Type * & Ty,bool MustBeSized)1036*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRType(StringRef::iterator Loc, Type *&Ty,
1037*9880d681SAndroid Build Coastguard Worker bool MustBeSized) {
1038*9880d681SAndroid Build Coastguard Worker // At this point we enter in the IR world, i.e., to get the correct type,
1039*9880d681SAndroid Build Coastguard Worker // we need to hand off the whole string, not just the current token.
1040*9880d681SAndroid Build Coastguard Worker // E.g., <4 x i64> would give '<' as a token and there is not much
1041*9880d681SAndroid Build Coastguard Worker // the IR parser can do with that.
1042*9880d681SAndroid Build Coastguard Worker unsigned Read = 0;
1043*9880d681SAndroid Build Coastguard Worker if (parseIRType(Loc, StringRef(Loc), Read, Ty))
1044*9880d681SAndroid Build Coastguard Worker return true;
1045*9880d681SAndroid Build Coastguard Worker // The type must be sized, otherwise there is not much the backend
1046*9880d681SAndroid Build Coastguard Worker // can do with it.
1047*9880d681SAndroid Build Coastguard Worker if (MustBeSized && !Ty->isSized())
1048*9880d681SAndroid Build Coastguard Worker return error("expected a sized type");
1049*9880d681SAndroid Build Coastguard Worker // The next token is Read characters from the Loc.
1050*9880d681SAndroid Build Coastguard Worker // However, the current location is not Loc, but Loc + the length of Token.
1051*9880d681SAndroid Build Coastguard Worker // Therefore, subtract the length of Token (range().end() - Loc) to the
1052*9880d681SAndroid Build Coastguard Worker // number of characters to skip before the next token.
1053*9880d681SAndroid Build Coastguard Worker lex(Read - (Token.range().end() - Loc));
1054*9880d681SAndroid Build Coastguard Worker return false;
1055*9880d681SAndroid Build Coastguard Worker }
1056*9880d681SAndroid Build Coastguard Worker
parseTypedImmediateOperand(MachineOperand & Dest)1057*9880d681SAndroid Build Coastguard Worker bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1058*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::IntegerType));
1059*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
1060*9880d681SAndroid Build Coastguard Worker lex();
1061*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
1062*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal");
1063*9880d681SAndroid Build Coastguard Worker const Constant *C = nullptr;
1064*9880d681SAndroid Build Coastguard Worker if (parseIRConstant(Loc, C))
1065*9880d681SAndroid Build Coastguard Worker return true;
1066*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1067*9880d681SAndroid Build Coastguard Worker return false;
1068*9880d681SAndroid Build Coastguard Worker }
1069*9880d681SAndroid Build Coastguard Worker
parseFPImmediateOperand(MachineOperand & Dest)1070*9880d681SAndroid Build Coastguard Worker bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1071*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
1072*9880d681SAndroid Build Coastguard Worker lex();
1073*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::FloatingPointLiteral))
1074*9880d681SAndroid Build Coastguard Worker return error("expected a floating point literal");
1075*9880d681SAndroid Build Coastguard Worker const Constant *C = nullptr;
1076*9880d681SAndroid Build Coastguard Worker if (parseIRConstant(Loc, C))
1077*9880d681SAndroid Build Coastguard Worker return true;
1078*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1079*9880d681SAndroid Build Coastguard Worker return false;
1080*9880d681SAndroid Build Coastguard Worker }
1081*9880d681SAndroid Build Coastguard Worker
getUnsigned(unsigned & Result)1082*9880d681SAndroid Build Coastguard Worker bool MIParser::getUnsigned(unsigned &Result) {
1083*9880d681SAndroid Build Coastguard Worker assert(Token.hasIntegerValue() && "Expected a token with an integer value");
1084*9880d681SAndroid Build Coastguard Worker const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1085*9880d681SAndroid Build Coastguard Worker uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1086*9880d681SAndroid Build Coastguard Worker if (Val64 == Limit)
1087*9880d681SAndroid Build Coastguard Worker return error("expected 32-bit integer (too large)");
1088*9880d681SAndroid Build Coastguard Worker Result = Val64;
1089*9880d681SAndroid Build Coastguard Worker return false;
1090*9880d681SAndroid Build Coastguard Worker }
1091*9880d681SAndroid Build Coastguard Worker
parseMBBReference(MachineBasicBlock * & MBB)1092*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1093*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::MachineBasicBlock) ||
1094*9880d681SAndroid Build Coastguard Worker Token.is(MIToken::MachineBasicBlockLabel));
1095*9880d681SAndroid Build Coastguard Worker unsigned Number;
1096*9880d681SAndroid Build Coastguard Worker if (getUnsigned(Number))
1097*9880d681SAndroid Build Coastguard Worker return true;
1098*9880d681SAndroid Build Coastguard Worker auto MBBInfo = PFS.MBBSlots.find(Number);
1099*9880d681SAndroid Build Coastguard Worker if (MBBInfo == PFS.MBBSlots.end())
1100*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined machine basic block #") +
1101*9880d681SAndroid Build Coastguard Worker Twine(Number));
1102*9880d681SAndroid Build Coastguard Worker MBB = MBBInfo->second;
1103*9880d681SAndroid Build Coastguard Worker if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1104*9880d681SAndroid Build Coastguard Worker return error(Twine("the name of machine basic block #") + Twine(Number) +
1105*9880d681SAndroid Build Coastguard Worker " isn't '" + Token.stringValue() + "'");
1106*9880d681SAndroid Build Coastguard Worker return false;
1107*9880d681SAndroid Build Coastguard Worker }
1108*9880d681SAndroid Build Coastguard Worker
parseMBBOperand(MachineOperand & Dest)1109*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1110*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *MBB;
1111*9880d681SAndroid Build Coastguard Worker if (parseMBBReference(MBB))
1112*9880d681SAndroid Build Coastguard Worker return true;
1113*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateMBB(MBB);
1114*9880d681SAndroid Build Coastguard Worker lex();
1115*9880d681SAndroid Build Coastguard Worker return false;
1116*9880d681SAndroid Build Coastguard Worker }
1117*9880d681SAndroid Build Coastguard Worker
parseStackFrameIndex(int & FI)1118*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStackFrameIndex(int &FI) {
1119*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::StackObject));
1120*9880d681SAndroid Build Coastguard Worker unsigned ID;
1121*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
1122*9880d681SAndroid Build Coastguard Worker return true;
1123*9880d681SAndroid Build Coastguard Worker auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1124*9880d681SAndroid Build Coastguard Worker if (ObjectInfo == PFS.StackObjectSlots.end())
1125*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1126*9880d681SAndroid Build Coastguard Worker "'");
1127*9880d681SAndroid Build Coastguard Worker StringRef Name;
1128*9880d681SAndroid Build Coastguard Worker if (const auto *Alloca =
1129*9880d681SAndroid Build Coastguard Worker MF.getFrameInfo()->getObjectAllocation(ObjectInfo->second))
1130*9880d681SAndroid Build Coastguard Worker Name = Alloca->getName();
1131*9880d681SAndroid Build Coastguard Worker if (!Token.stringValue().empty() && Token.stringValue() != Name)
1132*9880d681SAndroid Build Coastguard Worker return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1133*9880d681SAndroid Build Coastguard Worker "' isn't '" + Token.stringValue() + "'");
1134*9880d681SAndroid Build Coastguard Worker lex();
1135*9880d681SAndroid Build Coastguard Worker FI = ObjectInfo->second;
1136*9880d681SAndroid Build Coastguard Worker return false;
1137*9880d681SAndroid Build Coastguard Worker }
1138*9880d681SAndroid Build Coastguard Worker
parseStackObjectOperand(MachineOperand & Dest)1139*9880d681SAndroid Build Coastguard Worker bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1140*9880d681SAndroid Build Coastguard Worker int FI;
1141*9880d681SAndroid Build Coastguard Worker if (parseStackFrameIndex(FI))
1142*9880d681SAndroid Build Coastguard Worker return true;
1143*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateFI(FI);
1144*9880d681SAndroid Build Coastguard Worker return false;
1145*9880d681SAndroid Build Coastguard Worker }
1146*9880d681SAndroid Build Coastguard Worker
parseFixedStackFrameIndex(int & FI)1147*9880d681SAndroid Build Coastguard Worker bool MIParser::parseFixedStackFrameIndex(int &FI) {
1148*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::FixedStackObject));
1149*9880d681SAndroid Build Coastguard Worker unsigned ID;
1150*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
1151*9880d681SAndroid Build Coastguard Worker return true;
1152*9880d681SAndroid Build Coastguard Worker auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1153*9880d681SAndroid Build Coastguard Worker if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1154*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1155*9880d681SAndroid Build Coastguard Worker Twine(ID) + "'");
1156*9880d681SAndroid Build Coastguard Worker lex();
1157*9880d681SAndroid Build Coastguard Worker FI = ObjectInfo->second;
1158*9880d681SAndroid Build Coastguard Worker return false;
1159*9880d681SAndroid Build Coastguard Worker }
1160*9880d681SAndroid Build Coastguard Worker
parseFixedStackObjectOperand(MachineOperand & Dest)1161*9880d681SAndroid Build Coastguard Worker bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1162*9880d681SAndroid Build Coastguard Worker int FI;
1163*9880d681SAndroid Build Coastguard Worker if (parseFixedStackFrameIndex(FI))
1164*9880d681SAndroid Build Coastguard Worker return true;
1165*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateFI(FI);
1166*9880d681SAndroid Build Coastguard Worker return false;
1167*9880d681SAndroid Build Coastguard Worker }
1168*9880d681SAndroid Build Coastguard Worker
parseGlobalValue(GlobalValue * & GV)1169*9880d681SAndroid Build Coastguard Worker bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1170*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1171*9880d681SAndroid Build Coastguard Worker case MIToken::NamedGlobalValue: {
1172*9880d681SAndroid Build Coastguard Worker const Module *M = MF.getFunction()->getParent();
1173*9880d681SAndroid Build Coastguard Worker GV = M->getNamedValue(Token.stringValue());
1174*9880d681SAndroid Build Coastguard Worker if (!GV)
1175*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined global value '") + Token.range() +
1176*9880d681SAndroid Build Coastguard Worker "'");
1177*9880d681SAndroid Build Coastguard Worker break;
1178*9880d681SAndroid Build Coastguard Worker }
1179*9880d681SAndroid Build Coastguard Worker case MIToken::GlobalValue: {
1180*9880d681SAndroid Build Coastguard Worker unsigned GVIdx;
1181*9880d681SAndroid Build Coastguard Worker if (getUnsigned(GVIdx))
1182*9880d681SAndroid Build Coastguard Worker return true;
1183*9880d681SAndroid Build Coastguard Worker if (GVIdx >= PFS.IRSlots.GlobalValues.size())
1184*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1185*9880d681SAndroid Build Coastguard Worker "'");
1186*9880d681SAndroid Build Coastguard Worker GV = PFS.IRSlots.GlobalValues[GVIdx];
1187*9880d681SAndroid Build Coastguard Worker break;
1188*9880d681SAndroid Build Coastguard Worker }
1189*9880d681SAndroid Build Coastguard Worker default:
1190*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be a global value");
1191*9880d681SAndroid Build Coastguard Worker }
1192*9880d681SAndroid Build Coastguard Worker return false;
1193*9880d681SAndroid Build Coastguard Worker }
1194*9880d681SAndroid Build Coastguard Worker
parseGlobalAddressOperand(MachineOperand & Dest)1195*9880d681SAndroid Build Coastguard Worker bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1196*9880d681SAndroid Build Coastguard Worker GlobalValue *GV = nullptr;
1197*9880d681SAndroid Build Coastguard Worker if (parseGlobalValue(GV))
1198*9880d681SAndroid Build Coastguard Worker return true;
1199*9880d681SAndroid Build Coastguard Worker lex();
1200*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1201*9880d681SAndroid Build Coastguard Worker if (parseOperandsOffset(Dest))
1202*9880d681SAndroid Build Coastguard Worker return true;
1203*9880d681SAndroid Build Coastguard Worker return false;
1204*9880d681SAndroid Build Coastguard Worker }
1205*9880d681SAndroid Build Coastguard Worker
parseConstantPoolIndexOperand(MachineOperand & Dest)1206*9880d681SAndroid Build Coastguard Worker bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1207*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::ConstantPoolItem));
1208*9880d681SAndroid Build Coastguard Worker unsigned ID;
1209*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
1210*9880d681SAndroid Build Coastguard Worker return true;
1211*9880d681SAndroid Build Coastguard Worker auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1212*9880d681SAndroid Build Coastguard Worker if (ConstantInfo == PFS.ConstantPoolSlots.end())
1213*9880d681SAndroid Build Coastguard Worker return error("use of undefined constant '%const." + Twine(ID) + "'");
1214*9880d681SAndroid Build Coastguard Worker lex();
1215*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1216*9880d681SAndroid Build Coastguard Worker if (parseOperandsOffset(Dest))
1217*9880d681SAndroid Build Coastguard Worker return true;
1218*9880d681SAndroid Build Coastguard Worker return false;
1219*9880d681SAndroid Build Coastguard Worker }
1220*9880d681SAndroid Build Coastguard Worker
parseJumpTableIndexOperand(MachineOperand & Dest)1221*9880d681SAndroid Build Coastguard Worker bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1222*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::JumpTableIndex));
1223*9880d681SAndroid Build Coastguard Worker unsigned ID;
1224*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
1225*9880d681SAndroid Build Coastguard Worker return true;
1226*9880d681SAndroid Build Coastguard Worker auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1227*9880d681SAndroid Build Coastguard Worker if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1228*9880d681SAndroid Build Coastguard Worker return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1229*9880d681SAndroid Build Coastguard Worker lex();
1230*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1231*9880d681SAndroid Build Coastguard Worker return false;
1232*9880d681SAndroid Build Coastguard Worker }
1233*9880d681SAndroid Build Coastguard Worker
parseExternalSymbolOperand(MachineOperand & Dest)1234*9880d681SAndroid Build Coastguard Worker bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1235*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::ExternalSymbol));
1236*9880d681SAndroid Build Coastguard Worker const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1237*9880d681SAndroid Build Coastguard Worker lex();
1238*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateES(Symbol);
1239*9880d681SAndroid Build Coastguard Worker if (parseOperandsOffset(Dest))
1240*9880d681SAndroid Build Coastguard Worker return true;
1241*9880d681SAndroid Build Coastguard Worker return false;
1242*9880d681SAndroid Build Coastguard Worker }
1243*9880d681SAndroid Build Coastguard Worker
parseSubRegisterIndexOperand(MachineOperand & Dest)1244*9880d681SAndroid Build Coastguard Worker bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1245*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::SubRegisterIndex));
1246*9880d681SAndroid Build Coastguard Worker StringRef Name = Token.stringValue();
1247*9880d681SAndroid Build Coastguard Worker unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1248*9880d681SAndroid Build Coastguard Worker if (SubRegIndex == 0)
1249*9880d681SAndroid Build Coastguard Worker return error(Twine("unknown subregister index '") + Name + "'");
1250*9880d681SAndroid Build Coastguard Worker lex();
1251*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateImm(SubRegIndex);
1252*9880d681SAndroid Build Coastguard Worker return false;
1253*9880d681SAndroid Build Coastguard Worker }
1254*9880d681SAndroid Build Coastguard Worker
parseMDNode(MDNode * & Node)1255*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMDNode(MDNode *&Node) {
1256*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::exclaim));
1257*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
1258*9880d681SAndroid Build Coastguard Worker lex();
1259*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1260*9880d681SAndroid Build Coastguard Worker return error("expected metadata id after '!'");
1261*9880d681SAndroid Build Coastguard Worker unsigned ID;
1262*9880d681SAndroid Build Coastguard Worker if (getUnsigned(ID))
1263*9880d681SAndroid Build Coastguard Worker return true;
1264*9880d681SAndroid Build Coastguard Worker auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1265*9880d681SAndroid Build Coastguard Worker if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
1266*9880d681SAndroid Build Coastguard Worker return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1267*9880d681SAndroid Build Coastguard Worker lex();
1268*9880d681SAndroid Build Coastguard Worker Node = NodeInfo->second.get();
1269*9880d681SAndroid Build Coastguard Worker return false;
1270*9880d681SAndroid Build Coastguard Worker }
1271*9880d681SAndroid Build Coastguard Worker
parseMetadataOperand(MachineOperand & Dest)1272*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1273*9880d681SAndroid Build Coastguard Worker MDNode *Node = nullptr;
1274*9880d681SAndroid Build Coastguard Worker if (parseMDNode(Node))
1275*9880d681SAndroid Build Coastguard Worker return true;
1276*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateMetadata(Node);
1277*9880d681SAndroid Build Coastguard Worker return false;
1278*9880d681SAndroid Build Coastguard Worker }
1279*9880d681SAndroid Build Coastguard Worker
parseCFIOffset(int & Offset)1280*9880d681SAndroid Build Coastguard Worker bool MIParser::parseCFIOffset(int &Offset) {
1281*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
1282*9880d681SAndroid Build Coastguard Worker return error("expected a cfi offset");
1283*9880d681SAndroid Build Coastguard Worker if (Token.integerValue().getMinSignedBits() > 32)
1284*9880d681SAndroid Build Coastguard Worker return error("expected a 32 bit integer (the cfi offset is too large)");
1285*9880d681SAndroid Build Coastguard Worker Offset = (int)Token.integerValue().getExtValue();
1286*9880d681SAndroid Build Coastguard Worker lex();
1287*9880d681SAndroid Build Coastguard Worker return false;
1288*9880d681SAndroid Build Coastguard Worker }
1289*9880d681SAndroid Build Coastguard Worker
parseCFIRegister(unsigned & Reg)1290*9880d681SAndroid Build Coastguard Worker bool MIParser::parseCFIRegister(unsigned &Reg) {
1291*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::NamedRegister))
1292*9880d681SAndroid Build Coastguard Worker return error("expected a cfi register");
1293*9880d681SAndroid Build Coastguard Worker unsigned LLVMReg;
1294*9880d681SAndroid Build Coastguard Worker if (parseRegister(LLVMReg))
1295*9880d681SAndroid Build Coastguard Worker return true;
1296*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget().getRegisterInfo();
1297*9880d681SAndroid Build Coastguard Worker assert(TRI && "Expected target register info");
1298*9880d681SAndroid Build Coastguard Worker int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1299*9880d681SAndroid Build Coastguard Worker if (DwarfReg < 0)
1300*9880d681SAndroid Build Coastguard Worker return error("invalid DWARF register");
1301*9880d681SAndroid Build Coastguard Worker Reg = (unsigned)DwarfReg;
1302*9880d681SAndroid Build Coastguard Worker lex();
1303*9880d681SAndroid Build Coastguard Worker return false;
1304*9880d681SAndroid Build Coastguard Worker }
1305*9880d681SAndroid Build Coastguard Worker
parseCFIOperand(MachineOperand & Dest)1306*9880d681SAndroid Build Coastguard Worker bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1307*9880d681SAndroid Build Coastguard Worker auto Kind = Token.kind();
1308*9880d681SAndroid Build Coastguard Worker lex();
1309*9880d681SAndroid Build Coastguard Worker auto &MMI = MF.getMMI();
1310*9880d681SAndroid Build Coastguard Worker int Offset;
1311*9880d681SAndroid Build Coastguard Worker unsigned Reg;
1312*9880d681SAndroid Build Coastguard Worker unsigned CFIIndex;
1313*9880d681SAndroid Build Coastguard Worker switch (Kind) {
1314*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_same_value:
1315*9880d681SAndroid Build Coastguard Worker if (parseCFIRegister(Reg))
1316*9880d681SAndroid Build Coastguard Worker return true;
1317*9880d681SAndroid Build Coastguard Worker CFIIndex =
1318*9880d681SAndroid Build Coastguard Worker MMI.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1319*9880d681SAndroid Build Coastguard Worker break;
1320*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_offset:
1321*9880d681SAndroid Build Coastguard Worker if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1322*9880d681SAndroid Build Coastguard Worker parseCFIOffset(Offset))
1323*9880d681SAndroid Build Coastguard Worker return true;
1324*9880d681SAndroid Build Coastguard Worker CFIIndex =
1325*9880d681SAndroid Build Coastguard Worker MMI.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1326*9880d681SAndroid Build Coastguard Worker break;
1327*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa_register:
1328*9880d681SAndroid Build Coastguard Worker if (parseCFIRegister(Reg))
1329*9880d681SAndroid Build Coastguard Worker return true;
1330*9880d681SAndroid Build Coastguard Worker CFIIndex =
1331*9880d681SAndroid Build Coastguard Worker MMI.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1332*9880d681SAndroid Build Coastguard Worker break;
1333*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa_offset:
1334*9880d681SAndroid Build Coastguard Worker if (parseCFIOffset(Offset))
1335*9880d681SAndroid Build Coastguard Worker return true;
1336*9880d681SAndroid Build Coastguard Worker // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1337*9880d681SAndroid Build Coastguard Worker CFIIndex = MMI.addFrameInst(
1338*9880d681SAndroid Build Coastguard Worker MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1339*9880d681SAndroid Build Coastguard Worker break;
1340*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa:
1341*9880d681SAndroid Build Coastguard Worker if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1342*9880d681SAndroid Build Coastguard Worker parseCFIOffset(Offset))
1343*9880d681SAndroid Build Coastguard Worker return true;
1344*9880d681SAndroid Build Coastguard Worker // NB: MCCFIInstruction::createDefCfa negates the offset.
1345*9880d681SAndroid Build Coastguard Worker CFIIndex =
1346*9880d681SAndroid Build Coastguard Worker MMI.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1347*9880d681SAndroid Build Coastguard Worker break;
1348*9880d681SAndroid Build Coastguard Worker default:
1349*9880d681SAndroid Build Coastguard Worker // TODO: Parse the other CFI operands.
1350*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be a cfi operand");
1351*9880d681SAndroid Build Coastguard Worker }
1352*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1353*9880d681SAndroid Build Coastguard Worker return false;
1354*9880d681SAndroid Build Coastguard Worker }
1355*9880d681SAndroid Build Coastguard Worker
parseIRBlock(BasicBlock * & BB,const Function & F)1356*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1357*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1358*9880d681SAndroid Build Coastguard Worker case MIToken::NamedIRBlock: {
1359*9880d681SAndroid Build Coastguard Worker BB = dyn_cast_or_null<BasicBlock>(
1360*9880d681SAndroid Build Coastguard Worker F.getValueSymbolTable().lookup(Token.stringValue()));
1361*9880d681SAndroid Build Coastguard Worker if (!BB)
1362*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined IR block '") + Token.range() + "'");
1363*9880d681SAndroid Build Coastguard Worker break;
1364*9880d681SAndroid Build Coastguard Worker }
1365*9880d681SAndroid Build Coastguard Worker case MIToken::IRBlock: {
1366*9880d681SAndroid Build Coastguard Worker unsigned SlotNumber = 0;
1367*9880d681SAndroid Build Coastguard Worker if (getUnsigned(SlotNumber))
1368*9880d681SAndroid Build Coastguard Worker return true;
1369*9880d681SAndroid Build Coastguard Worker BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1370*9880d681SAndroid Build Coastguard Worker if (!BB)
1371*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined IR block '%ir-block.") +
1372*9880d681SAndroid Build Coastguard Worker Twine(SlotNumber) + "'");
1373*9880d681SAndroid Build Coastguard Worker break;
1374*9880d681SAndroid Build Coastguard Worker }
1375*9880d681SAndroid Build Coastguard Worker default:
1376*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be an IR block reference");
1377*9880d681SAndroid Build Coastguard Worker }
1378*9880d681SAndroid Build Coastguard Worker return false;
1379*9880d681SAndroid Build Coastguard Worker }
1380*9880d681SAndroid Build Coastguard Worker
parseBlockAddressOperand(MachineOperand & Dest)1381*9880d681SAndroid Build Coastguard Worker bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1382*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_blockaddress));
1383*9880d681SAndroid Build Coastguard Worker lex();
1384*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::lparen))
1385*9880d681SAndroid Build Coastguard Worker return true;
1386*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::GlobalValue) &&
1387*9880d681SAndroid Build Coastguard Worker Token.isNot(MIToken::NamedGlobalValue))
1388*9880d681SAndroid Build Coastguard Worker return error("expected a global value");
1389*9880d681SAndroid Build Coastguard Worker GlobalValue *GV = nullptr;
1390*9880d681SAndroid Build Coastguard Worker if (parseGlobalValue(GV))
1391*9880d681SAndroid Build Coastguard Worker return true;
1392*9880d681SAndroid Build Coastguard Worker auto *F = dyn_cast<Function>(GV);
1393*9880d681SAndroid Build Coastguard Worker if (!F)
1394*9880d681SAndroid Build Coastguard Worker return error("expected an IR function reference");
1395*9880d681SAndroid Build Coastguard Worker lex();
1396*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::comma))
1397*9880d681SAndroid Build Coastguard Worker return true;
1398*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = nullptr;
1399*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1400*9880d681SAndroid Build Coastguard Worker return error("expected an IR block reference");
1401*9880d681SAndroid Build Coastguard Worker if (parseIRBlock(BB, *F))
1402*9880d681SAndroid Build Coastguard Worker return true;
1403*9880d681SAndroid Build Coastguard Worker lex();
1404*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
1405*9880d681SAndroid Build Coastguard Worker return true;
1406*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1407*9880d681SAndroid Build Coastguard Worker if (parseOperandsOffset(Dest))
1408*9880d681SAndroid Build Coastguard Worker return true;
1409*9880d681SAndroid Build Coastguard Worker return false;
1410*9880d681SAndroid Build Coastguard Worker }
1411*9880d681SAndroid Build Coastguard Worker
parseTargetIndexOperand(MachineOperand & Dest)1412*9880d681SAndroid Build Coastguard Worker bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1413*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_target_index));
1414*9880d681SAndroid Build Coastguard Worker lex();
1415*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::lparen))
1416*9880d681SAndroid Build Coastguard Worker return true;
1417*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier))
1418*9880d681SAndroid Build Coastguard Worker return error("expected the name of the target index");
1419*9880d681SAndroid Build Coastguard Worker int Index = 0;
1420*9880d681SAndroid Build Coastguard Worker if (getTargetIndex(Token.stringValue(), Index))
1421*9880d681SAndroid Build Coastguard Worker return error("use of undefined target index '" + Token.stringValue() + "'");
1422*9880d681SAndroid Build Coastguard Worker lex();
1423*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
1424*9880d681SAndroid Build Coastguard Worker return true;
1425*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1426*9880d681SAndroid Build Coastguard Worker if (parseOperandsOffset(Dest))
1427*9880d681SAndroid Build Coastguard Worker return true;
1428*9880d681SAndroid Build Coastguard Worker return false;
1429*9880d681SAndroid Build Coastguard Worker }
1430*9880d681SAndroid Build Coastguard Worker
parseLiveoutRegisterMaskOperand(MachineOperand & Dest)1431*9880d681SAndroid Build Coastguard Worker bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1432*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_liveout));
1433*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget().getRegisterInfo();
1434*9880d681SAndroid Build Coastguard Worker assert(TRI && "Expected target register info");
1435*9880d681SAndroid Build Coastguard Worker uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1436*9880d681SAndroid Build Coastguard Worker lex();
1437*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::lparen))
1438*9880d681SAndroid Build Coastguard Worker return true;
1439*9880d681SAndroid Build Coastguard Worker while (true) {
1440*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::NamedRegister))
1441*9880d681SAndroid Build Coastguard Worker return error("expected a named register");
1442*9880d681SAndroid Build Coastguard Worker unsigned Reg = 0;
1443*9880d681SAndroid Build Coastguard Worker if (parseRegister(Reg))
1444*9880d681SAndroid Build Coastguard Worker return true;
1445*9880d681SAndroid Build Coastguard Worker lex();
1446*9880d681SAndroid Build Coastguard Worker Mask[Reg / 32] |= 1U << (Reg % 32);
1447*9880d681SAndroid Build Coastguard Worker // TODO: Report an error if the same register is used more than once.
1448*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::comma))
1449*9880d681SAndroid Build Coastguard Worker break;
1450*9880d681SAndroid Build Coastguard Worker lex();
1451*9880d681SAndroid Build Coastguard Worker }
1452*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
1453*9880d681SAndroid Build Coastguard Worker return true;
1454*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateRegLiveOut(Mask);
1455*9880d681SAndroid Build Coastguard Worker return false;
1456*9880d681SAndroid Build Coastguard Worker }
1457*9880d681SAndroid Build Coastguard Worker
parseMachineOperand(MachineOperand & Dest,Optional<unsigned> & TiedDefIdx)1458*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMachineOperand(MachineOperand &Dest,
1459*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &TiedDefIdx) {
1460*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1461*9880d681SAndroid Build Coastguard Worker case MIToken::kw_implicit:
1462*9880d681SAndroid Build Coastguard Worker case MIToken::kw_implicit_define:
1463*9880d681SAndroid Build Coastguard Worker case MIToken::kw_def:
1464*9880d681SAndroid Build Coastguard Worker case MIToken::kw_dead:
1465*9880d681SAndroid Build Coastguard Worker case MIToken::kw_killed:
1466*9880d681SAndroid Build Coastguard Worker case MIToken::kw_undef:
1467*9880d681SAndroid Build Coastguard Worker case MIToken::kw_internal:
1468*9880d681SAndroid Build Coastguard Worker case MIToken::kw_early_clobber:
1469*9880d681SAndroid Build Coastguard Worker case MIToken::kw_debug_use:
1470*9880d681SAndroid Build Coastguard Worker case MIToken::underscore:
1471*9880d681SAndroid Build Coastguard Worker case MIToken::NamedRegister:
1472*9880d681SAndroid Build Coastguard Worker case MIToken::VirtualRegister:
1473*9880d681SAndroid Build Coastguard Worker return parseRegisterOperand(Dest, TiedDefIdx);
1474*9880d681SAndroid Build Coastguard Worker case MIToken::IntegerLiteral:
1475*9880d681SAndroid Build Coastguard Worker return parseImmediateOperand(Dest);
1476*9880d681SAndroid Build Coastguard Worker case MIToken::IntegerType:
1477*9880d681SAndroid Build Coastguard Worker return parseTypedImmediateOperand(Dest);
1478*9880d681SAndroid Build Coastguard Worker case MIToken::kw_half:
1479*9880d681SAndroid Build Coastguard Worker case MIToken::kw_float:
1480*9880d681SAndroid Build Coastguard Worker case MIToken::kw_double:
1481*9880d681SAndroid Build Coastguard Worker case MIToken::kw_x86_fp80:
1482*9880d681SAndroid Build Coastguard Worker case MIToken::kw_fp128:
1483*9880d681SAndroid Build Coastguard Worker case MIToken::kw_ppc_fp128:
1484*9880d681SAndroid Build Coastguard Worker return parseFPImmediateOperand(Dest);
1485*9880d681SAndroid Build Coastguard Worker case MIToken::MachineBasicBlock:
1486*9880d681SAndroid Build Coastguard Worker return parseMBBOperand(Dest);
1487*9880d681SAndroid Build Coastguard Worker case MIToken::StackObject:
1488*9880d681SAndroid Build Coastguard Worker return parseStackObjectOperand(Dest);
1489*9880d681SAndroid Build Coastguard Worker case MIToken::FixedStackObject:
1490*9880d681SAndroid Build Coastguard Worker return parseFixedStackObjectOperand(Dest);
1491*9880d681SAndroid Build Coastguard Worker case MIToken::GlobalValue:
1492*9880d681SAndroid Build Coastguard Worker case MIToken::NamedGlobalValue:
1493*9880d681SAndroid Build Coastguard Worker return parseGlobalAddressOperand(Dest);
1494*9880d681SAndroid Build Coastguard Worker case MIToken::ConstantPoolItem:
1495*9880d681SAndroid Build Coastguard Worker return parseConstantPoolIndexOperand(Dest);
1496*9880d681SAndroid Build Coastguard Worker case MIToken::JumpTableIndex:
1497*9880d681SAndroid Build Coastguard Worker return parseJumpTableIndexOperand(Dest);
1498*9880d681SAndroid Build Coastguard Worker case MIToken::ExternalSymbol:
1499*9880d681SAndroid Build Coastguard Worker return parseExternalSymbolOperand(Dest);
1500*9880d681SAndroid Build Coastguard Worker case MIToken::SubRegisterIndex:
1501*9880d681SAndroid Build Coastguard Worker return parseSubRegisterIndexOperand(Dest);
1502*9880d681SAndroid Build Coastguard Worker case MIToken::exclaim:
1503*9880d681SAndroid Build Coastguard Worker return parseMetadataOperand(Dest);
1504*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_same_value:
1505*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_offset:
1506*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa_register:
1507*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa_offset:
1508*9880d681SAndroid Build Coastguard Worker case MIToken::kw_cfi_def_cfa:
1509*9880d681SAndroid Build Coastguard Worker return parseCFIOperand(Dest);
1510*9880d681SAndroid Build Coastguard Worker case MIToken::kw_blockaddress:
1511*9880d681SAndroid Build Coastguard Worker return parseBlockAddressOperand(Dest);
1512*9880d681SAndroid Build Coastguard Worker case MIToken::kw_target_index:
1513*9880d681SAndroid Build Coastguard Worker return parseTargetIndexOperand(Dest);
1514*9880d681SAndroid Build Coastguard Worker case MIToken::kw_liveout:
1515*9880d681SAndroid Build Coastguard Worker return parseLiveoutRegisterMaskOperand(Dest);
1516*9880d681SAndroid Build Coastguard Worker case MIToken::Error:
1517*9880d681SAndroid Build Coastguard Worker return true;
1518*9880d681SAndroid Build Coastguard Worker case MIToken::Identifier:
1519*9880d681SAndroid Build Coastguard Worker if (const auto *RegMask = getRegMask(Token.stringValue())) {
1520*9880d681SAndroid Build Coastguard Worker Dest = MachineOperand::CreateRegMask(RegMask);
1521*9880d681SAndroid Build Coastguard Worker lex();
1522*9880d681SAndroid Build Coastguard Worker break;
1523*9880d681SAndroid Build Coastguard Worker }
1524*9880d681SAndroid Build Coastguard Worker // fallthrough
1525*9880d681SAndroid Build Coastguard Worker default:
1526*9880d681SAndroid Build Coastguard Worker // FIXME: Parse the MCSymbol machine operand.
1527*9880d681SAndroid Build Coastguard Worker return error("expected a machine operand");
1528*9880d681SAndroid Build Coastguard Worker }
1529*9880d681SAndroid Build Coastguard Worker return false;
1530*9880d681SAndroid Build Coastguard Worker }
1531*9880d681SAndroid Build Coastguard Worker
parseMachineOperandAndTargetFlags(MachineOperand & Dest,Optional<unsigned> & TiedDefIdx)1532*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMachineOperandAndTargetFlags(
1533*9880d681SAndroid Build Coastguard Worker MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
1534*9880d681SAndroid Build Coastguard Worker unsigned TF = 0;
1535*9880d681SAndroid Build Coastguard Worker bool HasTargetFlags = false;
1536*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::kw_target_flags)) {
1537*9880d681SAndroid Build Coastguard Worker HasTargetFlags = true;
1538*9880d681SAndroid Build Coastguard Worker lex();
1539*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::lparen))
1540*9880d681SAndroid Build Coastguard Worker return true;
1541*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier))
1542*9880d681SAndroid Build Coastguard Worker return error("expected the name of the target flag");
1543*9880d681SAndroid Build Coastguard Worker if (getDirectTargetFlag(Token.stringValue(), TF)) {
1544*9880d681SAndroid Build Coastguard Worker if (getBitmaskTargetFlag(Token.stringValue(), TF))
1545*9880d681SAndroid Build Coastguard Worker return error("use of undefined target flag '" + Token.stringValue() +
1546*9880d681SAndroid Build Coastguard Worker "'");
1547*9880d681SAndroid Build Coastguard Worker }
1548*9880d681SAndroid Build Coastguard Worker lex();
1549*9880d681SAndroid Build Coastguard Worker while (Token.is(MIToken::comma)) {
1550*9880d681SAndroid Build Coastguard Worker lex();
1551*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier))
1552*9880d681SAndroid Build Coastguard Worker return error("expected the name of the target flag");
1553*9880d681SAndroid Build Coastguard Worker unsigned BitFlag = 0;
1554*9880d681SAndroid Build Coastguard Worker if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
1555*9880d681SAndroid Build Coastguard Worker return error("use of undefined target flag '" + Token.stringValue() +
1556*9880d681SAndroid Build Coastguard Worker "'");
1557*9880d681SAndroid Build Coastguard Worker // TODO: Report an error when using a duplicate bit target flag.
1558*9880d681SAndroid Build Coastguard Worker TF |= BitFlag;
1559*9880d681SAndroid Build Coastguard Worker lex();
1560*9880d681SAndroid Build Coastguard Worker }
1561*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
1562*9880d681SAndroid Build Coastguard Worker return true;
1563*9880d681SAndroid Build Coastguard Worker }
1564*9880d681SAndroid Build Coastguard Worker auto Loc = Token.location();
1565*9880d681SAndroid Build Coastguard Worker if (parseMachineOperand(Dest, TiedDefIdx))
1566*9880d681SAndroid Build Coastguard Worker return true;
1567*9880d681SAndroid Build Coastguard Worker if (!HasTargetFlags)
1568*9880d681SAndroid Build Coastguard Worker return false;
1569*9880d681SAndroid Build Coastguard Worker if (Dest.isReg())
1570*9880d681SAndroid Build Coastguard Worker return error(Loc, "register operands can't have target flags");
1571*9880d681SAndroid Build Coastguard Worker Dest.setTargetFlags(TF);
1572*9880d681SAndroid Build Coastguard Worker return false;
1573*9880d681SAndroid Build Coastguard Worker }
1574*9880d681SAndroid Build Coastguard Worker
parseOffset(int64_t & Offset)1575*9880d681SAndroid Build Coastguard Worker bool MIParser::parseOffset(int64_t &Offset) {
1576*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
1577*9880d681SAndroid Build Coastguard Worker return false;
1578*9880d681SAndroid Build Coastguard Worker StringRef Sign = Token.range();
1579*9880d681SAndroid Build Coastguard Worker bool IsNegative = Token.is(MIToken::minus);
1580*9880d681SAndroid Build Coastguard Worker lex();
1581*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
1582*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal after '" + Sign + "'");
1583*9880d681SAndroid Build Coastguard Worker if (Token.integerValue().getMinSignedBits() > 64)
1584*9880d681SAndroid Build Coastguard Worker return error("expected 64-bit integer (too large)");
1585*9880d681SAndroid Build Coastguard Worker Offset = Token.integerValue().getExtValue();
1586*9880d681SAndroid Build Coastguard Worker if (IsNegative)
1587*9880d681SAndroid Build Coastguard Worker Offset = -Offset;
1588*9880d681SAndroid Build Coastguard Worker lex();
1589*9880d681SAndroid Build Coastguard Worker return false;
1590*9880d681SAndroid Build Coastguard Worker }
1591*9880d681SAndroid Build Coastguard Worker
parseAlignment(unsigned & Alignment)1592*9880d681SAndroid Build Coastguard Worker bool MIParser::parseAlignment(unsigned &Alignment) {
1593*9880d681SAndroid Build Coastguard Worker assert(Token.is(MIToken::kw_align));
1594*9880d681SAndroid Build Coastguard Worker lex();
1595*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1596*9880d681SAndroid Build Coastguard Worker return error("expected an integer literal after 'align'");
1597*9880d681SAndroid Build Coastguard Worker if (getUnsigned(Alignment))
1598*9880d681SAndroid Build Coastguard Worker return true;
1599*9880d681SAndroid Build Coastguard Worker lex();
1600*9880d681SAndroid Build Coastguard Worker return false;
1601*9880d681SAndroid Build Coastguard Worker }
1602*9880d681SAndroid Build Coastguard Worker
parseOperandsOffset(MachineOperand & Op)1603*9880d681SAndroid Build Coastguard Worker bool MIParser::parseOperandsOffset(MachineOperand &Op) {
1604*9880d681SAndroid Build Coastguard Worker int64_t Offset = 0;
1605*9880d681SAndroid Build Coastguard Worker if (parseOffset(Offset))
1606*9880d681SAndroid Build Coastguard Worker return true;
1607*9880d681SAndroid Build Coastguard Worker Op.setOffset(Offset);
1608*9880d681SAndroid Build Coastguard Worker return false;
1609*9880d681SAndroid Build Coastguard Worker }
1610*9880d681SAndroid Build Coastguard Worker
parseIRValue(const Value * & V)1611*9880d681SAndroid Build Coastguard Worker bool MIParser::parseIRValue(const Value *&V) {
1612*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1613*9880d681SAndroid Build Coastguard Worker case MIToken::NamedIRValue: {
1614*9880d681SAndroid Build Coastguard Worker V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue());
1615*9880d681SAndroid Build Coastguard Worker break;
1616*9880d681SAndroid Build Coastguard Worker }
1617*9880d681SAndroid Build Coastguard Worker case MIToken::IRValue: {
1618*9880d681SAndroid Build Coastguard Worker unsigned SlotNumber = 0;
1619*9880d681SAndroid Build Coastguard Worker if (getUnsigned(SlotNumber))
1620*9880d681SAndroid Build Coastguard Worker return true;
1621*9880d681SAndroid Build Coastguard Worker V = getIRValue(SlotNumber);
1622*9880d681SAndroid Build Coastguard Worker break;
1623*9880d681SAndroid Build Coastguard Worker }
1624*9880d681SAndroid Build Coastguard Worker case MIToken::NamedGlobalValue:
1625*9880d681SAndroid Build Coastguard Worker case MIToken::GlobalValue: {
1626*9880d681SAndroid Build Coastguard Worker GlobalValue *GV = nullptr;
1627*9880d681SAndroid Build Coastguard Worker if (parseGlobalValue(GV))
1628*9880d681SAndroid Build Coastguard Worker return true;
1629*9880d681SAndroid Build Coastguard Worker V = GV;
1630*9880d681SAndroid Build Coastguard Worker break;
1631*9880d681SAndroid Build Coastguard Worker }
1632*9880d681SAndroid Build Coastguard Worker case MIToken::QuotedIRValue: {
1633*9880d681SAndroid Build Coastguard Worker const Constant *C = nullptr;
1634*9880d681SAndroid Build Coastguard Worker if (parseIRConstant(Token.location(), Token.stringValue(), C))
1635*9880d681SAndroid Build Coastguard Worker return true;
1636*9880d681SAndroid Build Coastguard Worker V = C;
1637*9880d681SAndroid Build Coastguard Worker break;
1638*9880d681SAndroid Build Coastguard Worker }
1639*9880d681SAndroid Build Coastguard Worker default:
1640*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be an IR block reference");
1641*9880d681SAndroid Build Coastguard Worker }
1642*9880d681SAndroid Build Coastguard Worker if (!V)
1643*9880d681SAndroid Build Coastguard Worker return error(Twine("use of undefined IR value '") + Token.range() + "'");
1644*9880d681SAndroid Build Coastguard Worker return false;
1645*9880d681SAndroid Build Coastguard Worker }
1646*9880d681SAndroid Build Coastguard Worker
getUint64(uint64_t & Result)1647*9880d681SAndroid Build Coastguard Worker bool MIParser::getUint64(uint64_t &Result) {
1648*9880d681SAndroid Build Coastguard Worker assert(Token.hasIntegerValue());
1649*9880d681SAndroid Build Coastguard Worker if (Token.integerValue().getActiveBits() > 64)
1650*9880d681SAndroid Build Coastguard Worker return error("expected 64-bit integer (too large)");
1651*9880d681SAndroid Build Coastguard Worker Result = Token.integerValue().getZExtValue();
1652*9880d681SAndroid Build Coastguard Worker return false;
1653*9880d681SAndroid Build Coastguard Worker }
1654*9880d681SAndroid Build Coastguard Worker
parseMemoryOperandFlag(unsigned & Flags)1655*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMemoryOperandFlag(unsigned &Flags) {
1656*9880d681SAndroid Build Coastguard Worker const unsigned OldFlags = Flags;
1657*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1658*9880d681SAndroid Build Coastguard Worker case MIToken::kw_volatile:
1659*9880d681SAndroid Build Coastguard Worker Flags |= MachineMemOperand::MOVolatile;
1660*9880d681SAndroid Build Coastguard Worker break;
1661*9880d681SAndroid Build Coastguard Worker case MIToken::kw_non_temporal:
1662*9880d681SAndroid Build Coastguard Worker Flags |= MachineMemOperand::MONonTemporal;
1663*9880d681SAndroid Build Coastguard Worker break;
1664*9880d681SAndroid Build Coastguard Worker case MIToken::kw_invariant:
1665*9880d681SAndroid Build Coastguard Worker Flags |= MachineMemOperand::MOInvariant;
1666*9880d681SAndroid Build Coastguard Worker break;
1667*9880d681SAndroid Build Coastguard Worker // TODO: parse the target specific memory operand flags.
1668*9880d681SAndroid Build Coastguard Worker default:
1669*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be a memory operand flag");
1670*9880d681SAndroid Build Coastguard Worker }
1671*9880d681SAndroid Build Coastguard Worker if (OldFlags == Flags)
1672*9880d681SAndroid Build Coastguard Worker // We know that the same flag is specified more than once when the flags
1673*9880d681SAndroid Build Coastguard Worker // weren't modified.
1674*9880d681SAndroid Build Coastguard Worker return error("duplicate '" + Token.stringValue() + "' memory operand flag");
1675*9880d681SAndroid Build Coastguard Worker lex();
1676*9880d681SAndroid Build Coastguard Worker return false;
1677*9880d681SAndroid Build Coastguard Worker }
1678*9880d681SAndroid Build Coastguard Worker
parseMemoryPseudoSourceValue(const PseudoSourceValue * & PSV)1679*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
1680*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1681*9880d681SAndroid Build Coastguard Worker case MIToken::kw_stack:
1682*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getStack();
1683*9880d681SAndroid Build Coastguard Worker break;
1684*9880d681SAndroid Build Coastguard Worker case MIToken::kw_got:
1685*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getGOT();
1686*9880d681SAndroid Build Coastguard Worker break;
1687*9880d681SAndroid Build Coastguard Worker case MIToken::kw_jump_table:
1688*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getJumpTable();
1689*9880d681SAndroid Build Coastguard Worker break;
1690*9880d681SAndroid Build Coastguard Worker case MIToken::kw_constant_pool:
1691*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getConstantPool();
1692*9880d681SAndroid Build Coastguard Worker break;
1693*9880d681SAndroid Build Coastguard Worker case MIToken::FixedStackObject: {
1694*9880d681SAndroid Build Coastguard Worker int FI;
1695*9880d681SAndroid Build Coastguard Worker if (parseFixedStackFrameIndex(FI))
1696*9880d681SAndroid Build Coastguard Worker return true;
1697*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getFixedStack(FI);
1698*9880d681SAndroid Build Coastguard Worker // The token was already consumed, so use return here instead of break.
1699*9880d681SAndroid Build Coastguard Worker return false;
1700*9880d681SAndroid Build Coastguard Worker }
1701*9880d681SAndroid Build Coastguard Worker case MIToken::StackObject: {
1702*9880d681SAndroid Build Coastguard Worker int FI;
1703*9880d681SAndroid Build Coastguard Worker if (parseStackFrameIndex(FI))
1704*9880d681SAndroid Build Coastguard Worker return true;
1705*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getFixedStack(FI);
1706*9880d681SAndroid Build Coastguard Worker // The token was already consumed, so use return here instead of break.
1707*9880d681SAndroid Build Coastguard Worker return false;
1708*9880d681SAndroid Build Coastguard Worker }
1709*9880d681SAndroid Build Coastguard Worker case MIToken::kw_call_entry: {
1710*9880d681SAndroid Build Coastguard Worker lex();
1711*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1712*9880d681SAndroid Build Coastguard Worker case MIToken::GlobalValue:
1713*9880d681SAndroid Build Coastguard Worker case MIToken::NamedGlobalValue: {
1714*9880d681SAndroid Build Coastguard Worker GlobalValue *GV = nullptr;
1715*9880d681SAndroid Build Coastguard Worker if (parseGlobalValue(GV))
1716*9880d681SAndroid Build Coastguard Worker return true;
1717*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
1718*9880d681SAndroid Build Coastguard Worker break;
1719*9880d681SAndroid Build Coastguard Worker }
1720*9880d681SAndroid Build Coastguard Worker case MIToken::ExternalSymbol:
1721*9880d681SAndroid Build Coastguard Worker PSV = MF.getPSVManager().getExternalSymbolCallEntry(
1722*9880d681SAndroid Build Coastguard Worker MF.createExternalSymbolName(Token.stringValue()));
1723*9880d681SAndroid Build Coastguard Worker break;
1724*9880d681SAndroid Build Coastguard Worker default:
1725*9880d681SAndroid Build Coastguard Worker return error(
1726*9880d681SAndroid Build Coastguard Worker "expected a global value or an external symbol after 'call-entry'");
1727*9880d681SAndroid Build Coastguard Worker }
1728*9880d681SAndroid Build Coastguard Worker break;
1729*9880d681SAndroid Build Coastguard Worker }
1730*9880d681SAndroid Build Coastguard Worker default:
1731*9880d681SAndroid Build Coastguard Worker llvm_unreachable("The current token should be pseudo source value");
1732*9880d681SAndroid Build Coastguard Worker }
1733*9880d681SAndroid Build Coastguard Worker lex();
1734*9880d681SAndroid Build Coastguard Worker return false;
1735*9880d681SAndroid Build Coastguard Worker }
1736*9880d681SAndroid Build Coastguard Worker
parseMachinePointerInfo(MachinePointerInfo & Dest)1737*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
1738*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
1739*9880d681SAndroid Build Coastguard Worker Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
1740*9880d681SAndroid Build Coastguard Worker Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
1741*9880d681SAndroid Build Coastguard Worker Token.is(MIToken::kw_call_entry)) {
1742*9880d681SAndroid Build Coastguard Worker const PseudoSourceValue *PSV = nullptr;
1743*9880d681SAndroid Build Coastguard Worker if (parseMemoryPseudoSourceValue(PSV))
1744*9880d681SAndroid Build Coastguard Worker return true;
1745*9880d681SAndroid Build Coastguard Worker int64_t Offset = 0;
1746*9880d681SAndroid Build Coastguard Worker if (parseOffset(Offset))
1747*9880d681SAndroid Build Coastguard Worker return true;
1748*9880d681SAndroid Build Coastguard Worker Dest = MachinePointerInfo(PSV, Offset);
1749*9880d681SAndroid Build Coastguard Worker return false;
1750*9880d681SAndroid Build Coastguard Worker }
1751*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
1752*9880d681SAndroid Build Coastguard Worker Token.isNot(MIToken::GlobalValue) &&
1753*9880d681SAndroid Build Coastguard Worker Token.isNot(MIToken::NamedGlobalValue) &&
1754*9880d681SAndroid Build Coastguard Worker Token.isNot(MIToken::QuotedIRValue))
1755*9880d681SAndroid Build Coastguard Worker return error("expected an IR value reference");
1756*9880d681SAndroid Build Coastguard Worker const Value *V = nullptr;
1757*9880d681SAndroid Build Coastguard Worker if (parseIRValue(V))
1758*9880d681SAndroid Build Coastguard Worker return true;
1759*9880d681SAndroid Build Coastguard Worker if (!V->getType()->isPointerTy())
1760*9880d681SAndroid Build Coastguard Worker return error("expected a pointer IR value");
1761*9880d681SAndroid Build Coastguard Worker lex();
1762*9880d681SAndroid Build Coastguard Worker int64_t Offset = 0;
1763*9880d681SAndroid Build Coastguard Worker if (parseOffset(Offset))
1764*9880d681SAndroid Build Coastguard Worker return true;
1765*9880d681SAndroid Build Coastguard Worker Dest = MachinePointerInfo(V, Offset);
1766*9880d681SAndroid Build Coastguard Worker return false;
1767*9880d681SAndroid Build Coastguard Worker }
1768*9880d681SAndroid Build Coastguard Worker
parseMachineMemoryOperand(MachineMemOperand * & Dest)1769*9880d681SAndroid Build Coastguard Worker bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
1770*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::lparen))
1771*9880d681SAndroid Build Coastguard Worker return true;
1772*9880d681SAndroid Build Coastguard Worker unsigned Flags = 0;
1773*9880d681SAndroid Build Coastguard Worker while (Token.isMemoryOperandFlag()) {
1774*9880d681SAndroid Build Coastguard Worker if (parseMemoryOperandFlag(Flags))
1775*9880d681SAndroid Build Coastguard Worker return true;
1776*9880d681SAndroid Build Coastguard Worker }
1777*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::Identifier) ||
1778*9880d681SAndroid Build Coastguard Worker (Token.stringValue() != "load" && Token.stringValue() != "store"))
1779*9880d681SAndroid Build Coastguard Worker return error("expected 'load' or 'store' memory operation");
1780*9880d681SAndroid Build Coastguard Worker if (Token.stringValue() == "load")
1781*9880d681SAndroid Build Coastguard Worker Flags |= MachineMemOperand::MOLoad;
1782*9880d681SAndroid Build Coastguard Worker else
1783*9880d681SAndroid Build Coastguard Worker Flags |= MachineMemOperand::MOStore;
1784*9880d681SAndroid Build Coastguard Worker lex();
1785*9880d681SAndroid Build Coastguard Worker
1786*9880d681SAndroid Build Coastguard Worker if (Token.isNot(MIToken::IntegerLiteral))
1787*9880d681SAndroid Build Coastguard Worker return error("expected the size integer literal after memory operation");
1788*9880d681SAndroid Build Coastguard Worker uint64_t Size;
1789*9880d681SAndroid Build Coastguard Worker if (getUint64(Size))
1790*9880d681SAndroid Build Coastguard Worker return true;
1791*9880d681SAndroid Build Coastguard Worker lex();
1792*9880d681SAndroid Build Coastguard Worker
1793*9880d681SAndroid Build Coastguard Worker MachinePointerInfo Ptr = MachinePointerInfo();
1794*9880d681SAndroid Build Coastguard Worker if (Token.is(MIToken::Identifier)) {
1795*9880d681SAndroid Build Coastguard Worker const char *Word = Flags & MachineMemOperand::MOLoad ? "from" : "into";
1796*9880d681SAndroid Build Coastguard Worker if (Token.stringValue() != Word)
1797*9880d681SAndroid Build Coastguard Worker return error(Twine("expected '") + Word + "'");
1798*9880d681SAndroid Build Coastguard Worker lex();
1799*9880d681SAndroid Build Coastguard Worker
1800*9880d681SAndroid Build Coastguard Worker if (parseMachinePointerInfo(Ptr))
1801*9880d681SAndroid Build Coastguard Worker return true;
1802*9880d681SAndroid Build Coastguard Worker }
1803*9880d681SAndroid Build Coastguard Worker unsigned BaseAlignment = Size;
1804*9880d681SAndroid Build Coastguard Worker AAMDNodes AAInfo;
1805*9880d681SAndroid Build Coastguard Worker MDNode *Range = nullptr;
1806*9880d681SAndroid Build Coastguard Worker while (consumeIfPresent(MIToken::comma)) {
1807*9880d681SAndroid Build Coastguard Worker switch (Token.kind()) {
1808*9880d681SAndroid Build Coastguard Worker case MIToken::kw_align:
1809*9880d681SAndroid Build Coastguard Worker if (parseAlignment(BaseAlignment))
1810*9880d681SAndroid Build Coastguard Worker return true;
1811*9880d681SAndroid Build Coastguard Worker break;
1812*9880d681SAndroid Build Coastguard Worker case MIToken::md_tbaa:
1813*9880d681SAndroid Build Coastguard Worker lex();
1814*9880d681SAndroid Build Coastguard Worker if (parseMDNode(AAInfo.TBAA))
1815*9880d681SAndroid Build Coastguard Worker return true;
1816*9880d681SAndroid Build Coastguard Worker break;
1817*9880d681SAndroid Build Coastguard Worker case MIToken::md_alias_scope:
1818*9880d681SAndroid Build Coastguard Worker lex();
1819*9880d681SAndroid Build Coastguard Worker if (parseMDNode(AAInfo.Scope))
1820*9880d681SAndroid Build Coastguard Worker return true;
1821*9880d681SAndroid Build Coastguard Worker break;
1822*9880d681SAndroid Build Coastguard Worker case MIToken::md_noalias:
1823*9880d681SAndroid Build Coastguard Worker lex();
1824*9880d681SAndroid Build Coastguard Worker if (parseMDNode(AAInfo.NoAlias))
1825*9880d681SAndroid Build Coastguard Worker return true;
1826*9880d681SAndroid Build Coastguard Worker break;
1827*9880d681SAndroid Build Coastguard Worker case MIToken::md_range:
1828*9880d681SAndroid Build Coastguard Worker lex();
1829*9880d681SAndroid Build Coastguard Worker if (parseMDNode(Range))
1830*9880d681SAndroid Build Coastguard Worker return true;
1831*9880d681SAndroid Build Coastguard Worker break;
1832*9880d681SAndroid Build Coastguard Worker // TODO: Report an error on duplicate metadata nodes.
1833*9880d681SAndroid Build Coastguard Worker default:
1834*9880d681SAndroid Build Coastguard Worker return error("expected 'align' or '!tbaa' or '!alias.scope' or "
1835*9880d681SAndroid Build Coastguard Worker "'!noalias' or '!range'");
1836*9880d681SAndroid Build Coastguard Worker }
1837*9880d681SAndroid Build Coastguard Worker }
1838*9880d681SAndroid Build Coastguard Worker if (expectAndConsume(MIToken::rparen))
1839*9880d681SAndroid Build Coastguard Worker return true;
1840*9880d681SAndroid Build Coastguard Worker Dest =
1841*9880d681SAndroid Build Coastguard Worker MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range);
1842*9880d681SAndroid Build Coastguard Worker return false;
1843*9880d681SAndroid Build Coastguard Worker }
1844*9880d681SAndroid Build Coastguard Worker
initNames2InstrOpCodes()1845*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2InstrOpCodes() {
1846*9880d681SAndroid Build Coastguard Worker if (!Names2InstrOpCodes.empty())
1847*9880d681SAndroid Build Coastguard Worker return;
1848*9880d681SAndroid Build Coastguard Worker const auto *TII = MF.getSubtarget().getInstrInfo();
1849*9880d681SAndroid Build Coastguard Worker assert(TII && "Expected target instruction info");
1850*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
1851*9880d681SAndroid Build Coastguard Worker Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
1852*9880d681SAndroid Build Coastguard Worker }
1853*9880d681SAndroid Build Coastguard Worker
parseInstrName(StringRef InstrName,unsigned & OpCode)1854*9880d681SAndroid Build Coastguard Worker bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
1855*9880d681SAndroid Build Coastguard Worker initNames2InstrOpCodes();
1856*9880d681SAndroid Build Coastguard Worker auto InstrInfo = Names2InstrOpCodes.find(InstrName);
1857*9880d681SAndroid Build Coastguard Worker if (InstrInfo == Names2InstrOpCodes.end())
1858*9880d681SAndroid Build Coastguard Worker return true;
1859*9880d681SAndroid Build Coastguard Worker OpCode = InstrInfo->getValue();
1860*9880d681SAndroid Build Coastguard Worker return false;
1861*9880d681SAndroid Build Coastguard Worker }
1862*9880d681SAndroid Build Coastguard Worker
initNames2Regs()1863*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2Regs() {
1864*9880d681SAndroid Build Coastguard Worker if (!Names2Regs.empty())
1865*9880d681SAndroid Build Coastguard Worker return;
1866*9880d681SAndroid Build Coastguard Worker // The '%noreg' register is the register 0.
1867*9880d681SAndroid Build Coastguard Worker Names2Regs.insert(std::make_pair("noreg", 0));
1868*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget().getRegisterInfo();
1869*9880d681SAndroid Build Coastguard Worker assert(TRI && "Expected target register info");
1870*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
1871*9880d681SAndroid Build Coastguard Worker bool WasInserted =
1872*9880d681SAndroid Build Coastguard Worker Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
1873*9880d681SAndroid Build Coastguard Worker .second;
1874*9880d681SAndroid Build Coastguard Worker (void)WasInserted;
1875*9880d681SAndroid Build Coastguard Worker assert(WasInserted && "Expected registers to be unique case-insensitively");
1876*9880d681SAndroid Build Coastguard Worker }
1877*9880d681SAndroid Build Coastguard Worker }
1878*9880d681SAndroid Build Coastguard Worker
getRegisterByName(StringRef RegName,unsigned & Reg)1879*9880d681SAndroid Build Coastguard Worker bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
1880*9880d681SAndroid Build Coastguard Worker initNames2Regs();
1881*9880d681SAndroid Build Coastguard Worker auto RegInfo = Names2Regs.find(RegName);
1882*9880d681SAndroid Build Coastguard Worker if (RegInfo == Names2Regs.end())
1883*9880d681SAndroid Build Coastguard Worker return true;
1884*9880d681SAndroid Build Coastguard Worker Reg = RegInfo->getValue();
1885*9880d681SAndroid Build Coastguard Worker return false;
1886*9880d681SAndroid Build Coastguard Worker }
1887*9880d681SAndroid Build Coastguard Worker
initNames2RegMasks()1888*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2RegMasks() {
1889*9880d681SAndroid Build Coastguard Worker if (!Names2RegMasks.empty())
1890*9880d681SAndroid Build Coastguard Worker return;
1891*9880d681SAndroid Build Coastguard Worker const auto *TRI = MF.getSubtarget().getRegisterInfo();
1892*9880d681SAndroid Build Coastguard Worker assert(TRI && "Expected target register info");
1893*9880d681SAndroid Build Coastguard Worker ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
1894*9880d681SAndroid Build Coastguard Worker ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
1895*9880d681SAndroid Build Coastguard Worker assert(RegMasks.size() == RegMaskNames.size());
1896*9880d681SAndroid Build Coastguard Worker for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
1897*9880d681SAndroid Build Coastguard Worker Names2RegMasks.insert(
1898*9880d681SAndroid Build Coastguard Worker std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
1899*9880d681SAndroid Build Coastguard Worker }
1900*9880d681SAndroid Build Coastguard Worker
getRegMask(StringRef Identifier)1901*9880d681SAndroid Build Coastguard Worker const uint32_t *MIParser::getRegMask(StringRef Identifier) {
1902*9880d681SAndroid Build Coastguard Worker initNames2RegMasks();
1903*9880d681SAndroid Build Coastguard Worker auto RegMaskInfo = Names2RegMasks.find(Identifier);
1904*9880d681SAndroid Build Coastguard Worker if (RegMaskInfo == Names2RegMasks.end())
1905*9880d681SAndroid Build Coastguard Worker return nullptr;
1906*9880d681SAndroid Build Coastguard Worker return RegMaskInfo->getValue();
1907*9880d681SAndroid Build Coastguard Worker }
1908*9880d681SAndroid Build Coastguard Worker
initNames2SubRegIndices()1909*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2SubRegIndices() {
1910*9880d681SAndroid Build Coastguard Worker if (!Names2SubRegIndices.empty())
1911*9880d681SAndroid Build Coastguard Worker return;
1912*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1913*9880d681SAndroid Build Coastguard Worker for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
1914*9880d681SAndroid Build Coastguard Worker Names2SubRegIndices.insert(
1915*9880d681SAndroid Build Coastguard Worker std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
1916*9880d681SAndroid Build Coastguard Worker }
1917*9880d681SAndroid Build Coastguard Worker
getSubRegIndex(StringRef Name)1918*9880d681SAndroid Build Coastguard Worker unsigned MIParser::getSubRegIndex(StringRef Name) {
1919*9880d681SAndroid Build Coastguard Worker initNames2SubRegIndices();
1920*9880d681SAndroid Build Coastguard Worker auto SubRegInfo = Names2SubRegIndices.find(Name);
1921*9880d681SAndroid Build Coastguard Worker if (SubRegInfo == Names2SubRegIndices.end())
1922*9880d681SAndroid Build Coastguard Worker return 0;
1923*9880d681SAndroid Build Coastguard Worker return SubRegInfo->getValue();
1924*9880d681SAndroid Build Coastguard Worker }
1925*9880d681SAndroid Build Coastguard Worker
initSlots2BasicBlocks(const Function & F,DenseMap<unsigned,const BasicBlock * > & Slots2BasicBlocks)1926*9880d681SAndroid Build Coastguard Worker static void initSlots2BasicBlocks(
1927*9880d681SAndroid Build Coastguard Worker const Function &F,
1928*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1929*9880d681SAndroid Build Coastguard Worker ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1930*9880d681SAndroid Build Coastguard Worker MST.incorporateFunction(F);
1931*9880d681SAndroid Build Coastguard Worker for (auto &BB : F) {
1932*9880d681SAndroid Build Coastguard Worker if (BB.hasName())
1933*9880d681SAndroid Build Coastguard Worker continue;
1934*9880d681SAndroid Build Coastguard Worker int Slot = MST.getLocalSlot(&BB);
1935*9880d681SAndroid Build Coastguard Worker if (Slot == -1)
1936*9880d681SAndroid Build Coastguard Worker continue;
1937*9880d681SAndroid Build Coastguard Worker Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
1938*9880d681SAndroid Build Coastguard Worker }
1939*9880d681SAndroid Build Coastguard Worker }
1940*9880d681SAndroid Build Coastguard Worker
getIRBlockFromSlot(unsigned Slot,const DenseMap<unsigned,const BasicBlock * > & Slots2BasicBlocks)1941*9880d681SAndroid Build Coastguard Worker static const BasicBlock *getIRBlockFromSlot(
1942*9880d681SAndroid Build Coastguard Worker unsigned Slot,
1943*9880d681SAndroid Build Coastguard Worker const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
1944*9880d681SAndroid Build Coastguard Worker auto BlockInfo = Slots2BasicBlocks.find(Slot);
1945*9880d681SAndroid Build Coastguard Worker if (BlockInfo == Slots2BasicBlocks.end())
1946*9880d681SAndroid Build Coastguard Worker return nullptr;
1947*9880d681SAndroid Build Coastguard Worker return BlockInfo->second;
1948*9880d681SAndroid Build Coastguard Worker }
1949*9880d681SAndroid Build Coastguard Worker
getIRBlock(unsigned Slot)1950*9880d681SAndroid Build Coastguard Worker const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
1951*9880d681SAndroid Build Coastguard Worker if (Slots2BasicBlocks.empty())
1952*9880d681SAndroid Build Coastguard Worker initSlots2BasicBlocks(*MF.getFunction(), Slots2BasicBlocks);
1953*9880d681SAndroid Build Coastguard Worker return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
1954*9880d681SAndroid Build Coastguard Worker }
1955*9880d681SAndroid Build Coastguard Worker
getIRBlock(unsigned Slot,const Function & F)1956*9880d681SAndroid Build Coastguard Worker const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
1957*9880d681SAndroid Build Coastguard Worker if (&F == MF.getFunction())
1958*9880d681SAndroid Build Coastguard Worker return getIRBlock(Slot);
1959*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
1960*9880d681SAndroid Build Coastguard Worker initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
1961*9880d681SAndroid Build Coastguard Worker return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
1962*9880d681SAndroid Build Coastguard Worker }
1963*9880d681SAndroid Build Coastguard Worker
mapValueToSlot(const Value * V,ModuleSlotTracker & MST,DenseMap<unsigned,const Value * > & Slots2Values)1964*9880d681SAndroid Build Coastguard Worker static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
1965*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const Value *> &Slots2Values) {
1966*9880d681SAndroid Build Coastguard Worker int Slot = MST.getLocalSlot(V);
1967*9880d681SAndroid Build Coastguard Worker if (Slot == -1)
1968*9880d681SAndroid Build Coastguard Worker return;
1969*9880d681SAndroid Build Coastguard Worker Slots2Values.insert(std::make_pair(unsigned(Slot), V));
1970*9880d681SAndroid Build Coastguard Worker }
1971*9880d681SAndroid Build Coastguard Worker
1972*9880d681SAndroid Build Coastguard Worker /// Creates the mapping from slot numbers to function's unnamed IR values.
initSlots2Values(const Function & F,DenseMap<unsigned,const Value * > & Slots2Values)1973*9880d681SAndroid Build Coastguard Worker static void initSlots2Values(const Function &F,
1974*9880d681SAndroid Build Coastguard Worker DenseMap<unsigned, const Value *> &Slots2Values) {
1975*9880d681SAndroid Build Coastguard Worker ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
1976*9880d681SAndroid Build Coastguard Worker MST.incorporateFunction(F);
1977*9880d681SAndroid Build Coastguard Worker for (const auto &Arg : F.args())
1978*9880d681SAndroid Build Coastguard Worker mapValueToSlot(&Arg, MST, Slots2Values);
1979*9880d681SAndroid Build Coastguard Worker for (const auto &BB : F) {
1980*9880d681SAndroid Build Coastguard Worker mapValueToSlot(&BB, MST, Slots2Values);
1981*9880d681SAndroid Build Coastguard Worker for (const auto &I : BB)
1982*9880d681SAndroid Build Coastguard Worker mapValueToSlot(&I, MST, Slots2Values);
1983*9880d681SAndroid Build Coastguard Worker }
1984*9880d681SAndroid Build Coastguard Worker }
1985*9880d681SAndroid Build Coastguard Worker
getIRValue(unsigned Slot)1986*9880d681SAndroid Build Coastguard Worker const Value *MIParser::getIRValue(unsigned Slot) {
1987*9880d681SAndroid Build Coastguard Worker if (Slots2Values.empty())
1988*9880d681SAndroid Build Coastguard Worker initSlots2Values(*MF.getFunction(), Slots2Values);
1989*9880d681SAndroid Build Coastguard Worker auto ValueInfo = Slots2Values.find(Slot);
1990*9880d681SAndroid Build Coastguard Worker if (ValueInfo == Slots2Values.end())
1991*9880d681SAndroid Build Coastguard Worker return nullptr;
1992*9880d681SAndroid Build Coastguard Worker return ValueInfo->second;
1993*9880d681SAndroid Build Coastguard Worker }
1994*9880d681SAndroid Build Coastguard Worker
initNames2TargetIndices()1995*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2TargetIndices() {
1996*9880d681SAndroid Build Coastguard Worker if (!Names2TargetIndices.empty())
1997*9880d681SAndroid Build Coastguard Worker return;
1998*9880d681SAndroid Build Coastguard Worker const auto *TII = MF.getSubtarget().getInstrInfo();
1999*9880d681SAndroid Build Coastguard Worker assert(TII && "Expected target instruction info");
2000*9880d681SAndroid Build Coastguard Worker auto Indices = TII->getSerializableTargetIndices();
2001*9880d681SAndroid Build Coastguard Worker for (const auto &I : Indices)
2002*9880d681SAndroid Build Coastguard Worker Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2003*9880d681SAndroid Build Coastguard Worker }
2004*9880d681SAndroid Build Coastguard Worker
getTargetIndex(StringRef Name,int & Index)2005*9880d681SAndroid Build Coastguard Worker bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2006*9880d681SAndroid Build Coastguard Worker initNames2TargetIndices();
2007*9880d681SAndroid Build Coastguard Worker auto IndexInfo = Names2TargetIndices.find(Name);
2008*9880d681SAndroid Build Coastguard Worker if (IndexInfo == Names2TargetIndices.end())
2009*9880d681SAndroid Build Coastguard Worker return true;
2010*9880d681SAndroid Build Coastguard Worker Index = IndexInfo->second;
2011*9880d681SAndroid Build Coastguard Worker return false;
2012*9880d681SAndroid Build Coastguard Worker }
2013*9880d681SAndroid Build Coastguard Worker
initNames2DirectTargetFlags()2014*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2DirectTargetFlags() {
2015*9880d681SAndroid Build Coastguard Worker if (!Names2DirectTargetFlags.empty())
2016*9880d681SAndroid Build Coastguard Worker return;
2017*9880d681SAndroid Build Coastguard Worker const auto *TII = MF.getSubtarget().getInstrInfo();
2018*9880d681SAndroid Build Coastguard Worker assert(TII && "Expected target instruction info");
2019*9880d681SAndroid Build Coastguard Worker auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2020*9880d681SAndroid Build Coastguard Worker for (const auto &I : Flags)
2021*9880d681SAndroid Build Coastguard Worker Names2DirectTargetFlags.insert(
2022*9880d681SAndroid Build Coastguard Worker std::make_pair(StringRef(I.second), I.first));
2023*9880d681SAndroid Build Coastguard Worker }
2024*9880d681SAndroid Build Coastguard Worker
getDirectTargetFlag(StringRef Name,unsigned & Flag)2025*9880d681SAndroid Build Coastguard Worker bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2026*9880d681SAndroid Build Coastguard Worker initNames2DirectTargetFlags();
2027*9880d681SAndroid Build Coastguard Worker auto FlagInfo = Names2DirectTargetFlags.find(Name);
2028*9880d681SAndroid Build Coastguard Worker if (FlagInfo == Names2DirectTargetFlags.end())
2029*9880d681SAndroid Build Coastguard Worker return true;
2030*9880d681SAndroid Build Coastguard Worker Flag = FlagInfo->second;
2031*9880d681SAndroid Build Coastguard Worker return false;
2032*9880d681SAndroid Build Coastguard Worker }
2033*9880d681SAndroid Build Coastguard Worker
initNames2BitmaskTargetFlags()2034*9880d681SAndroid Build Coastguard Worker void MIParser::initNames2BitmaskTargetFlags() {
2035*9880d681SAndroid Build Coastguard Worker if (!Names2BitmaskTargetFlags.empty())
2036*9880d681SAndroid Build Coastguard Worker return;
2037*9880d681SAndroid Build Coastguard Worker const auto *TII = MF.getSubtarget().getInstrInfo();
2038*9880d681SAndroid Build Coastguard Worker assert(TII && "Expected target instruction info");
2039*9880d681SAndroid Build Coastguard Worker auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2040*9880d681SAndroid Build Coastguard Worker for (const auto &I : Flags)
2041*9880d681SAndroid Build Coastguard Worker Names2BitmaskTargetFlags.insert(
2042*9880d681SAndroid Build Coastguard Worker std::make_pair(StringRef(I.second), I.first));
2043*9880d681SAndroid Build Coastguard Worker }
2044*9880d681SAndroid Build Coastguard Worker
getBitmaskTargetFlag(StringRef Name,unsigned & Flag)2045*9880d681SAndroid Build Coastguard Worker bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2046*9880d681SAndroid Build Coastguard Worker initNames2BitmaskTargetFlags();
2047*9880d681SAndroid Build Coastguard Worker auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2048*9880d681SAndroid Build Coastguard Worker if (FlagInfo == Names2BitmaskTargetFlags.end())
2049*9880d681SAndroid Build Coastguard Worker return true;
2050*9880d681SAndroid Build Coastguard Worker Flag = FlagInfo->second;
2051*9880d681SAndroid Build Coastguard Worker return false;
2052*9880d681SAndroid Build Coastguard Worker }
2053*9880d681SAndroid Build Coastguard Worker
parseMachineBasicBlockDefinitions(PerFunctionMIParsingState & PFS,StringRef Src,SMDiagnostic & Error)2054*9880d681SAndroid Build Coastguard Worker bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2055*9880d681SAndroid Build Coastguard Worker StringRef Src,
2056*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error) {
2057*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2058*9880d681SAndroid Build Coastguard Worker }
2059*9880d681SAndroid Build Coastguard Worker
parseMachineInstructions(const PerFunctionMIParsingState & PFS,StringRef Src,SMDiagnostic & Error)2060*9880d681SAndroid Build Coastguard Worker bool llvm::parseMachineInstructions(const PerFunctionMIParsingState &PFS,
2061*9880d681SAndroid Build Coastguard Worker StringRef Src, SMDiagnostic &Error) {
2062*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseBasicBlocks();
2063*9880d681SAndroid Build Coastguard Worker }
2064*9880d681SAndroid Build Coastguard Worker
parseMBBReference(const PerFunctionMIParsingState & PFS,MachineBasicBlock * & MBB,StringRef Src,SMDiagnostic & Error)2065*9880d681SAndroid Build Coastguard Worker bool llvm::parseMBBReference(const PerFunctionMIParsingState &PFS,
2066*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *&MBB, StringRef Src,
2067*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error) {
2068*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
2069*9880d681SAndroid Build Coastguard Worker }
2070*9880d681SAndroid Build Coastguard Worker
parseNamedRegisterReference(const PerFunctionMIParsingState & PFS,unsigned & Reg,StringRef Src,SMDiagnostic & Error)2071*9880d681SAndroid Build Coastguard Worker bool llvm::parseNamedRegisterReference(const PerFunctionMIParsingState &PFS,
2072*9880d681SAndroid Build Coastguard Worker unsigned &Reg, StringRef Src,
2073*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error) {
2074*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
2075*9880d681SAndroid Build Coastguard Worker }
2076*9880d681SAndroid Build Coastguard Worker
parseVirtualRegisterReference(const PerFunctionMIParsingState & PFS,unsigned & Reg,StringRef Src,SMDiagnostic & Error)2077*9880d681SAndroid Build Coastguard Worker bool llvm::parseVirtualRegisterReference(const PerFunctionMIParsingState &PFS,
2078*9880d681SAndroid Build Coastguard Worker unsigned &Reg, StringRef Src,
2079*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error) {
2080*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Reg);
2081*9880d681SAndroid Build Coastguard Worker }
2082*9880d681SAndroid Build Coastguard Worker
parseStackObjectReference(const PerFunctionMIParsingState & PFS,int & FI,StringRef Src,SMDiagnostic & Error)2083*9880d681SAndroid Build Coastguard Worker bool llvm::parseStackObjectReference(const PerFunctionMIParsingState &PFS,
2084*9880d681SAndroid Build Coastguard Worker int &FI, StringRef Src,
2085*9880d681SAndroid Build Coastguard Worker SMDiagnostic &Error) {
2086*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
2087*9880d681SAndroid Build Coastguard Worker }
2088*9880d681SAndroid Build Coastguard Worker
parseMDNode(const PerFunctionMIParsingState & PFS,MDNode * & Node,StringRef Src,SMDiagnostic & Error)2089*9880d681SAndroid Build Coastguard Worker bool llvm::parseMDNode(const PerFunctionMIParsingState &PFS,
2090*9880d681SAndroid Build Coastguard Worker MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2091*9880d681SAndroid Build Coastguard Worker return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
2092*9880d681SAndroid Build Coastguard Worker }
2093