llvm.org GIT mirror llvm / 18b8323
Promoted the getTok() method to MCAsmParser so that the two token accessor functions are declared consistently. Modified the clients of MCAsmParser to reflect this change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@93916 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Callanan 10 years ago
5 changed file(s) with 65 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
5454 /// inclusion first.
5555 virtual const AsmToken &Lex() = 0;
5656
57 /// getTok - Get the current AsmToken from the stream.
58 const AsmToken &getTok();
59
5760 /// ParseExpression - Parse an arbitrary expression.
5861 ///
5962 /// @param Res - The value of the expression. The result is undefined
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/MC/MCAsmParser.h"
10 #include "llvm/MC/MCAsmLexer.h"
1011 #include "llvm/MC/MCParsedAsmOperand.h"
1112 #include "llvm/Support/SourceMgr.h"
1213 using namespace llvm;
1718 MCAsmParser::~MCAsmParser() {
1819 }
1920
21 const AsmToken &MCAsmParser::getTok() {
22 return getLexer().getTok();
23 }
24
2025 bool MCAsmParser::ParseExpression(const MCExpr *&Res) {
2126 SMLoc L;
2227 return ParseExpression(Res, L);
2328 }
24
2529
2630 /// getStartLoc - Get the location of the first token of this operand.
2731 SMLoc MCParsedAsmOperand::getStartLoc() const { return SMLoc(); }
221221 /// TODO this is likely to change to allow different register types and or to
222222 /// parse for a specific register type.
223223 bool ARMAsmParser::MaybeParseRegister(ARMOperand &Op, bool ParseWriteBack) {
224 const AsmToken &Tok = getLexer().getTok();
224 const AsmToken &Tok = Parser.getTok();
225225 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
226226
227227 // FIXME: Validate register for the current architecture; we have to do
235235
236236 bool Writeback = false;
237237 if (ParseWriteBack) {
238 const AsmToken &ExclaimTok = getLexer().getTok();
238 const AsmToken &ExclaimTok = Parser.getTok();
239239 if (ExclaimTok.is(AsmToken::Exclaim)) {
240240 Writeback = true;
241241 Parser.Lex(); // Eat exclaim token
250250 /// Parse a register list, return false if successful else return true or an
251251 /// error. The first token must be a '{' when called.
252252 bool ARMAsmParser::ParseRegisterList(ARMOperand &Op) {
253 assert(getLexer().getTok().is(AsmToken::LCurly) &&
253 assert(Parser.getTok().is(AsmToken::LCurly) &&
254254 "Token is not an Left Curly Brace");
255255 Parser.Lex(); // Eat left curly brace token.
256256
257 const AsmToken &RegTok = getLexer().getTok();
257 const AsmToken &RegTok = Parser.getTok();
258258 SMLoc RegLoc = RegTok.getLoc();
259259 if (RegTok.isNot(AsmToken::Identifier))
260260 return Error(RegLoc, "register expected");
266266
267267 int HighRegNum = RegNum;
268268 // TODO ranges like "{Rn-Rm}"
269 while (getLexer().getTok().is(AsmToken::Comma)) {
269 while (Parser.getTok().is(AsmToken::Comma)) {
270270 Parser.Lex(); // Eat comma token.
271271
272 const AsmToken &RegTok = getLexer().getTok();
272 const AsmToken &RegTok = Parser.getTok();
273273 SMLoc RegLoc = RegTok.getLoc();
274274 if (RegTok.isNot(AsmToken::Identifier))
275275 return Error(RegLoc, "register expected");
286286
287287 Parser.Lex(); // Eat identifier token.
288288 }
289 const AsmToken &RCurlyTok = getLexer().getTok();
289 const AsmToken &RCurlyTok = Parser.getTok();
290290 if (RCurlyTok.isNot(AsmToken::RCurly))
291291 return Error(RCurlyTok.getLoc(), "'}' expected");
292292 Parser.Lex(); // Eat left curly brace token.
299299 /// TODO Only preindexing and postindexing addressing are started, unindexed
300300 /// with option, etc are still to do.
301301 bool ARMAsmParser::ParseMemory(ARMOperand &Op) {
302 assert(getLexer().getTok().is(AsmToken::LBrac) &&
302 assert(Parser.getTok().is(AsmToken::LBrac) &&
303303 "Token is not an Left Bracket");
304304 Parser.Lex(); // Eat left bracket token.
305305
306 const AsmToken &BaseRegTok = getLexer().getTok();
306 const AsmToken &BaseRegTok = Parser.getTok();
307307 if (BaseRegTok.isNot(AsmToken::Identifier))
308308 return Error(BaseRegTok.getLoc(), "register expected");
309309 if (MaybeParseRegister(Op, false))
318318
319319 // First look for preindexed address forms, that is after the "[Rn" we now
320320 // have to see if the next token is a comma.
321 const AsmToken &Tok = getLexer().getTok();
321 const AsmToken &Tok = Parser.getTok();
322322 if (Tok.is(AsmToken::Comma)) {
323323 Preindexed = true;
324324 Parser.Lex(); // Eat comma token.
330330 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
331331 Offset, OffsetIsReg, OffsetRegNum))
332332 return true;
333 const AsmToken &RBracTok = getLexer().getTok();
333 const AsmToken &RBracTok = Parser.getTok();
334334 if (RBracTok.isNot(AsmToken::RBrac))
335335 return Error(RBracTok.getLoc(), "']' expected");
336336 Parser.Lex(); // Eat right bracket token.
337337
338 const AsmToken &ExclaimTok = getLexer().getTok();
338 const AsmToken &ExclaimTok = Parser.getTok();
339339 if (ExclaimTok.is(AsmToken::Exclaim)) {
340340 Writeback = true;
341341 Parser.Lex(); // Eat exclaim token
359359 const MCExpr *ShiftAmount;
360360 const MCExpr *Offset;
361361
362 const AsmToken &NextTok = getLexer().getTok();
362 const AsmToken &NextTok = Parser.getTok();
363363 if (NextTok.isNot(AsmToken::EndOfStatement)) {
364364 if (NextTok.isNot(AsmToken::Comma))
365365 return Error(NextTok.getLoc(), "',' expected");
397397 OffsetRegShifted = false;
398398 OffsetIsReg = false;
399399 OffsetRegNum = -1;
400 const AsmToken &NextTok = getLexer().getTok();
400 const AsmToken &NextTok = Parser.getTok();
401401 if (NextTok.is(AsmToken::Plus))
402402 Parser.Lex(); // Eat plus token.
403403 else if (NextTok.is(AsmToken::Minus)) {
405405 Parser.Lex(); // Eat minus token
406406 }
407407 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
408 const AsmToken &OffsetRegTok = getLexer().getTok();
408 const AsmToken &OffsetRegTok = Parser.getTok();
409409 if (OffsetRegTok.is(AsmToken::Identifier)) {
410410 OffsetIsReg = !MaybeParseRegister(Op, false);
411411 if (OffsetIsReg)
414414 // If we parsed a register as the offset then their can be a shift after that
415415 if (OffsetRegNum != -1) {
416416 // Look for a comma then a shift
417 const AsmToken &Tok = getLexer().getTok();
417 const AsmToken &Tok = Parser.getTok();
418418 if (Tok.is(AsmToken::Comma)) {
419419 Parser.Lex(); // Eat comma token.
420420
421 const AsmToken &Tok = getLexer().getTok();
421 const AsmToken &Tok = Parser.getTok();
422422 if (ParseShift(ShiftType, ShiftAmount))
423423 return Error(Tok.getLoc(), "shift expected");
424424 OffsetRegShifted = true;
426426 }
427427 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
428428 // Look for #offset following the "[Rn," or "[Rn],"
429 const AsmToken &HashTok = getLexer().getTok();
429 const AsmToken &HashTok = Parser.getTok();
430430 if (HashTok.isNot(AsmToken::Hash))
431431 return Error(HashTok.getLoc(), "'#' expected");
432432 Parser.Lex(); // Eat hash token.
442442 /// rrx
443443 /// and returns true if it parses a shift otherwise it returns false.
444444 bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount) {
445 const AsmToken &Tok = getLexer().getTok();
445 const AsmToken &Tok = Parser.getTok();
446446 if (Tok.isNot(AsmToken::Identifier))
447447 return true;
448448 const StringRef &ShiftName = Tok.getString();
465465 return false;
466466
467467 // Otherwise, there must be a '#' and a shift amount.
468 const AsmToken &HashTok = getLexer().getTok();
468 const AsmToken &HashTok = Parser.getTok();
469469 if (HashTok.isNot(AsmToken::Hash))
470470 return Error(HashTok.getLoc(), "'#' expected");
471471 Parser.Lex(); // Eat hash token.
575575 Op = ARMOperand::CreateImm(ImmVal);
576576 return false;
577577 default:
578 return Error(getLexer().getTok().getLoc(), "unexpected token in operand");
578 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
579579 }
580580 }
581581
584584 SmallVectorImpl &Operands) {
585585 Operands.push_back(new ARMOperand(ARMOperand::CreateToken(Name)));
586586
587 SMLoc Loc = getLexer().getTok().getLoc();
587 SMLoc Loc = Parser.getTok().getLoc();
588588 if (getLexer().isNot(AsmToken::EndOfStatement)) {
589589
590590 // Read the first operand.
660660 /// ParseDirectiveThumbFunc
661661 /// ::= .thumbfunc symbol_name
662662 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
663 const AsmToken &Tok = getLexer().getTok();
663 const AsmToken &Tok = Parser.getTok();
664664 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
665665 return Error(L, "unexpected token in .syntax directive");
666 StringRef ATTRIBUTE_UNUSED SymbolName = getLexer().getTok().getIdentifier();
666 StringRef ATTRIBUTE_UNUSED SymbolName = Parser.getTok().getIdentifier();
667667 Parser.Lex(); // Consume the identifier token.
668668
669669 if (getLexer().isNot(AsmToken::EndOfStatement))
678678 /// ParseDirectiveSyntax
679679 /// ::= .syntax unified | divided
680680 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
681 const AsmToken &Tok = getLexer().getTok();
681 const AsmToken &Tok = Parser.getTok();
682682 if (Tok.isNot(AsmToken::Identifier))
683683 return Error(L, "unexpected token in .syntax directive");
684684 const StringRef &Mode = Tok.getString();
695695 return Error(L, "unrecognized syntax mode in .syntax directive");
696696
697697 if (getLexer().isNot(AsmToken::EndOfStatement))
698 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
698 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
699699 Parser.Lex();
700700
701701 // TODO tell the MC streamer the mode
706706 /// ParseDirectiveCode
707707 /// ::= .code 16 | 32
708708 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
709 const AsmToken &Tok = getLexer().getTok();
709 const AsmToken &Tok = Parser.getTok();
710710 if (Tok.isNot(AsmToken::Integer))
711711 return Error(L, "unexpected token in .code directive");
712 int64_t Val = getLexer().getTok().getIntVal();
712 int64_t Val = Parser.getTok().getIntVal();
713713 bool thumb_mode;
714714 if (Val == 16) {
715715 Parser.Lex();
723723 return Error(L, "invalid operand to .code directive");
724724
725725 if (getLexer().isNot(AsmToken::EndOfStatement))
726 return Error(getLexer().getTok().getLoc(), "unexpected token in directive");
726 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
727727 Parser.Lex();
728728
729729 // TODO tell the MC streamer the mode
244244 bool X86ATTAsmParser::ParseRegister(unsigned &RegNo,
245245 SMLoc &StartLoc, SMLoc &EndLoc) {
246246 RegNo = 0;
247 const AsmToken &TokPercent = getLexer().getTok();
247 const AsmToken &TokPercent = Parser.getTok();
248248 assert(TokPercent.is(AsmToken::Percent) && "Invalid token kind!");
249249 StartLoc = TokPercent.getLoc();
250250 Parser.Lex(); // Eat percent token.
251251
252 const AsmToken &Tok = getLexer().getTok();
252 const AsmToken &Tok = Parser.getTok();
253253 if (Tok.isNot(AsmToken::Identifier))
254254 return Error(Tok.getLoc(), "invalid register name");
255255
278278 }
279279 case AsmToken::Dollar: {
280280 // $42 -> immediate.
281 SMLoc Start = getLexer().getTok().getLoc(), End;
281 SMLoc Start = Parser.getTok().getLoc(), End;
282282 Parser.Lex();
283283 const MCExpr *Val;
284284 if (getParser().ParseExpression(Val, End))
290290
291291 /// ParseMemOperand: segment: disp(basereg, indexreg, scale)
292292 X86Operand *X86ATTAsmParser::ParseMemOperand() {
293 SMLoc MemStart = getLexer().getTok().getLoc();
293 SMLoc MemStart = Parser.getTok().getLoc();
294294
295295 // FIXME: If SegReg ':' (e.g. %gs:), eat and remember.
296296 unsigned SegReg = 0;
318318 } else {
319319 // Okay, we have a '('. We don't know if this is an expression or not, but
320320 // so we have to eat the ( to see beyond it.
321 SMLoc LParenLoc = getLexer().getTok().getLoc();
321 SMLoc LParenLoc = Parser.getTok().getLoc();
322322 Parser.Lex(); // Eat the '('.
323323
324324 if (getLexer().is(AsmToken::Percent) || getLexer().is(AsmToken::Comma)) {
371371 // Parse the scale amount:
372372 // ::= ',' [scale-expression]
373373 if (getLexer().isNot(AsmToken::Comma)) {
374 Error(getLexer().getTok().getLoc(),
374 Error(Parser.getTok().getLoc(),
375375 "expected comma in scale expression");
376376 return 0;
377377 }
378378 Parser.Lex(); // Eat the comma.
379379
380380 if (getLexer().isNot(AsmToken::RParen)) {
381 SMLoc Loc = getLexer().getTok().getLoc();
381 SMLoc Loc = Parser.getTok().getLoc();
382382
383383 int64_t ScaleVal;
384384 if (getParser().ParseAbsoluteExpression(ScaleVal))
395395 } else if (getLexer().isNot(AsmToken::RParen)) {
396396 // Otherwise we have the unsupported form of a scale amount without an
397397 // index.
398 SMLoc Loc = getLexer().getTok().getLoc();
398 SMLoc Loc = Parser.getTok().getLoc();
399399
400400 int64_t Value;
401401 if (getParser().ParseAbsoluteExpression(Value))
408408
409409 // Ok, we've eaten the memory operand, verify we have a ')' and eat it too.
410410 if (getLexer().isNot(AsmToken::RParen)) {
411 Error(getLexer().getTok().getLoc(), "unexpected token in memory operand");
411 Error(Parser.getTok().getLoc(), "unexpected token in memory operand");
412412 return 0;
413413 }
414 SMLoc MemEnd = getLexer().getTok().getLoc();
414 SMLoc MemEnd = Parser.getTok().getLoc();
415415 Parser.Lex(); // Eat the ')'.
416416
417417 return X86Operand::CreateMem(SegReg, Disp, BaseReg, IndexReg, Scale,
428428
429429 // Parse '*' modifier.
430430 if (getLexer().is(AsmToken::Star)) {
431 SMLoc Loc = getLexer().getTok().getLoc();
431 SMLoc Loc = Parser.getTok().getLoc();
432432 Operands.push_back(X86Operand::CreateToken("*", Loc));
433433 Parser.Lex(); // Eat the star.
434434 }
125125 // Handle conditional assembly here before calling ParseStatement()
126126 if (Lexer.getKind() == AsmToken::Identifier) {
127127 // If we have an identifier, handle it as the key symbol.
128 AsmToken ID = Lexer.getTok();
128 AsmToken ID = getTok();
129129 SMLoc IDLoc = ID.getLoc();
130130 StringRef IDVal = ID.getString();
131131
232232 case AsmToken::String:
233233 case AsmToken::Identifier: {
234234 // This is a symbol reference.
235 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
235 MCSymbol *Sym = CreateSymbol(getTok().getIdentifier());
236236 EndLoc = Lexer.getLoc();
237237 Lex(); // Eat identifier.
238238
248248 return false;
249249 }
250250 case AsmToken::Integer:
251 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
251 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
252252 EndLoc = Lexer.getLoc();
253253 Lex(); // Eat token.
254254 return false;
434434 }
435435
436436 // Statements always start with an identifier.
437 AsmToken ID = Lexer.getTok();
437 AsmToken ID = getTok();
438438 SMLoc IDLoc = ID.getLoc();
439439 StringRef IDVal;
440440 if (ParseIdentifier(IDVal))
810810 Lexer.isNot(AsmToken::String))
811811 return true;
812812
813 Res = Lexer.getTok().getIdentifier();
813 Res = getTok().getIdentifier();
814814
815815 Lex(); // Consume the identifier token.
816816
907907 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
908908
909909 Data = "";
910 StringRef Str = Lexer.getTok().getStringContents();
910 StringRef Str = getTok().getStringContents();
911911 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
912912 if (Str[i] != '\\') {
913913 Data += Str[i];
13361336
13371337 if (Lexer.isNot(AsmToken::Identifier))
13381338 return TokError("expected segment name after '.zerofill' directive");
1339 StringRef Segment = Lexer.getTok().getString();
1339 StringRef Segment = getTok().getString();
13401340 Lex();
13411341
13421342 if (Lexer.isNot(AsmToken::Comma))
13461346 if (Lexer.isNot(AsmToken::Identifier))
13471347 return TokError("expected section name after comma in '.zerofill' "
13481348 "directive");
1349 StringRef Section = Lexer.getTok().getString();
1349 StringRef Section = getTok().getString();
13501350 Lex();
13511351
13521352 // If this is the end of the line all that was wanted was to create the
13681368
13691369 // handle the identifier as the key symbol.
13701370 SMLoc IDLoc = Lexer.getLoc();
1371 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
1371 MCSymbol *Sym = CreateSymbol(getTok().getString());
13721372 Lex();
13731373
13741374 if (Lexer.isNot(AsmToken::Comma))
14431443 if (Lexer.isNot(AsmToken::String))
14441444 return TokError("expected string in '.abort' directive");
14451445
1446 Str = Lexer.getTok().getString();
1446 Str = getTok().getString();
14471447
14481448 Lex();
14491449 }
14991499 if (Lexer.isNot(AsmToken::String))
15001500 return TokError("expected string in '.include' directive");
15011501
1502 std::string Filename = Lexer.getTok().getString();
1502 std::string Filename = getTok().getString();
15031503 SMLoc IncludeLoc = Lexer.getLoc();
15041504 Lex();
15051505
16631663 // FIXME: I'm not sure what this is.
16641664 int64_t FileNumber = -1;
16651665 if (Lexer.is(AsmToken::Integer)) {
1666 FileNumber = Lexer.getTok().getIntVal();
1666 FileNumber = getTok().getIntVal();
16671667 Lex();
16681668
16691669 if (FileNumber < 1)
16731673 if (Lexer.isNot(AsmToken::String))
16741674 return TokError("unexpected token in '.file' directive");
16751675
1676 StringRef ATTRIBUTE_UNUSED FileName = Lexer.getTok().getString();
1676 StringRef ATTRIBUTE_UNUSED FileName = getTok().getString();
16771677 Lex();
16781678
16791679 if (Lexer.isNot(AsmToken::EndOfStatement))
16911691 if (Lexer.isNot(AsmToken::Integer))
16921692 return TokError("unexpected token in '.line' directive");
16931693
1694 int64_t LineNumber = Lexer.getTok().getIntVal();
1694 int64_t LineNumber = getTok().getIntVal();
16951695 (void) LineNumber;
16961696 Lex();
16971697
17121712 return TokError("unexpected token in '.loc' directive");
17131713
17141714 // FIXME: What are these fields?
1715 int64_t FileNumber = Lexer.getTok().getIntVal();
1715 int64_t FileNumber = getTok().getIntVal();
17161716 (void) FileNumber;
17171717 // FIXME: Validate file.
17181718
17211721 if (Lexer.isNot(AsmToken::Integer))
17221722 return TokError("unexpected token in '.loc' directive");
17231723
1724 int64_t Param2 = Lexer.getTok().getIntVal();
1724 int64_t Param2 = getTok().getIntVal();
17251725 (void) Param2;
17261726 Lex();
17271727
17291729 if (Lexer.isNot(AsmToken::Integer))
17301730 return TokError("unexpected token in '.loc' directive");
17311731
1732 int64_t Param3 = Lexer.getTok().getIntVal();
1732 int64_t Param3 = getTok().getIntVal();
17331733 (void) Param3;
17341734 Lex();
17351735