1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-26 04:32:44 +01:00
llvm-mirror/lib/MC/MCParser/MCAsmParser.cpp
Eric Astor ca668acee6 [ms] [llvm-ml] Add a draft MASM parser
Summary:
Many directives are unavailable, and support for others may be limited.

This first draft has preliminary support for:
    - conditional directives (including errors),
    - data allocation (unsigned types up to 8 bytes, and ALIGN),
    - equates/variables (numeric and text),
    - and procedure directives (without parameters),
as well as COMMENT, ECHO, INCLUDE, INCLUDELIB, PUBLIC, and EXTERN. Text variables (aka text macros) are expanded in-place wherever the identifier occurs.

We deliberately ignore all ml.exe processor directives.

Prominent features not yet supported:
    - structs
    - macros (both procedures and functions)
    - procedures (with specified parameters)
    - substitution & expansion operators

Conditional directives are complicated by the fact that "ifdef rax" is a valid way to check if a file is being assembled for a 64-bit x86 processor; we add support for "ifdef <register>" in general, which requires adding a tryParseRegister method to all MCTargetAsmParsers. (Some targets require backtracking in the non-register case.)

Reviewers: rnk, thakis

Reviewed By: thakis

Subscribers: kerbowa, merge_guards_bot, wuzish, arsenm, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, mgorny, sbc100, jgravelle-google, hiraditya, aheejin, kbarton, fedor.sergeev, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei, jsji, Jim, s.egerton, pzheng, sameer.abuasal, apazos, luismarques, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D72680
2020-02-16 12:30:46 -05:00

142 lines
3.8 KiB
C++

//===-- MCAsmParser.cpp - Abstract Asm Parser Interface -------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
using namespace llvm;
cl::opt<unsigned> AsmMacroMaxNestingDepth(
"asm-macro-max-nesting-depth", cl::init(20), cl::Hidden,
cl::desc("The maximum nesting depth allowed for assembly macros."));
MCAsmParser::MCAsmParser() {}
MCAsmParser::~MCAsmParser() = default;
void MCAsmParser::setTargetParser(MCTargetAsmParser &P) {
assert(!TargetParser && "Target parser is already initialized!");
TargetParser = &P;
TargetParser->Initialize(*this);
}
const AsmToken &MCAsmParser::getTok() const {
return getLexer().getTok();
}
bool MCAsmParser::parseTokenLoc(SMLoc &Loc) {
Loc = getTok().getLoc();
return false;
}
bool MCAsmParser::parseEOL(const Twine &Msg) {
if (getTok().getKind() != AsmToken::EndOfStatement)
return Error(getTok().getLoc(), Msg);
Lex();
return false;
}
bool MCAsmParser::parseToken(AsmToken::TokenKind T, const Twine &Msg) {
if (T == AsmToken::EndOfStatement)
return parseEOL(Msg);
if (getTok().getKind() != T)
return Error(getTok().getLoc(), Msg);
Lex();
return false;
}
bool MCAsmParser::parseIntToken(int64_t &V, const Twine &Msg) {
if (getTok().getKind() != AsmToken::Integer)
return TokError(Msg);
V = getTok().getIntVal();
Lex();
return false;
}
bool MCAsmParser::parseOptionalToken(AsmToken::TokenKind T) {
bool Present = (getTok().getKind() == T);
if (Present)
parseToken(T);
return Present;
}
bool MCAsmParser::check(bool P, const Twine &Msg) {
return check(P, getTok().getLoc(), Msg);
}
bool MCAsmParser::check(bool P, SMLoc Loc, const Twine &Msg) {
if (P)
return Error(Loc, Msg);
return false;
}
bool MCAsmParser::TokError(const Twine &Msg, SMRange Range) {
return Error(getLexer().getLoc(), Msg, Range);
}
bool MCAsmParser::Error(SMLoc L, const Twine &Msg, SMRange Range) {
MCPendingError PErr;
PErr.Loc = L;
Msg.toVector(PErr.Msg);
PErr.Range = Range;
PendingErrors.push_back(PErr);
// If we threw this parsing error after a lexing error, this should
// supercede the lexing error and so we remove it from the Lexer
// before it can propagate
if (getTok().is(AsmToken::Error))
getLexer().Lex();
return true;
}
bool MCAsmParser::addErrorSuffix(const Twine &Suffix) {
// Make sure lexing errors have propagated to the parser.
if (getTok().is(AsmToken::Error))
Lex();
for (auto &PErr : PendingErrors)
Suffix.toVector(PErr.Msg);
return true;
}
bool MCAsmParser::parseMany(function_ref<bool()> parseOne, bool hasComma) {
if (parseOptionalToken(AsmToken::EndOfStatement))
return false;
while (true) {
if (parseOne())
return true;
if (parseOptionalToken(AsmToken::EndOfStatement))
return false;
if (hasComma && parseToken(AsmToken::Comma))
return true;
}
return false;
}
bool MCAsmParser::parseExpression(const MCExpr *&Res) {
SMLoc L;
return parseExpression(Res, L);
}
void MCParsedAsmOperand::dump() const {
// Cannot completely remove virtual function even in release mode.
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dbgs() << " " << *this;
#endif
}