Skip to content

Commit b4d7c74

Browse files
committed
Introduce public MCMasmParser interface, and use it for MASM-specific context
Moves the DefaultRetIsFar information from MCContext to MCMasmParser.
1 parent f7da4b9 commit b4d7c74

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

llvm/include/llvm/MC/MCContext.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,9 @@ class MCContext {
9797
IsDXContainer
9898
};
9999

100-
enum DefaultRetType { IsNear, IsFar };
101-
102100
private:
103101
Environment Env;
104102

105-
DefaultRetType DefaultRet = IsNear;
106-
107103
/// The name of the Segment where Swift5 Reflection Section data will be
108104
/// outputted
109105
StringRef Swift5ReflectionSegmentName;
@@ -398,9 +394,6 @@ class MCContext {
398394

399395
Environment getObjectFileType() const { return Env; }
400396

401-
DefaultRetType getDefaultRetType() const { return DefaultRet; }
402-
void setDefaultRetType(DefaultRetType DR) { DefaultRet = DR; }
403-
404397
const StringRef &getSwift5ReflectionSegmentName() const {
405398
return Swift5ReflectionSegmentName;
406399
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===- llvm/MC/MasmParser.h - MASM Parser Interface -------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_MC_MCPARSER_MCMASMPARSER_H
10+
#define LLVM_MC_MCPARSER_MCMASMPARSER_H
11+
12+
#include "llvm/MC/MCParser/MCAsmParser.h"
13+
14+
namespace llvm {
15+
16+
/// MASM-type assembler parser interface.
17+
class MCMasmParser : public MCAsmParser {
18+
public:
19+
virtual bool getDefaultRetIsFar() const = 0;
20+
virtual void setDefaultRetIsFar(bool IsFar) = 0;
21+
22+
bool isParsingMasm() const override { return true; }
23+
24+
static bool classof(const MCAsmParser *AP) { return AP->isParsingMasm(); }
25+
};
26+
27+
} // end namespace llvm
28+
29+
#endif // LLVM_MC_MCPARSER_MCMASMPARSER_H

llvm/lib/MC/MCParser/COFFMasmParser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
#include "llvm/MC/MCAsmMacro.h"
1313
#include "llvm/MC/MCContext.h"
1414
#include "llvm/MC/MCParser/MCAsmLexer.h"
15+
#include "llvm/MC/MCParser/MCAsmParser.h"
1516
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
17+
#include "llvm/MC/MCParser/MCMasmParser.h"
1618
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
1719
#include "llvm/MC/MCSectionCOFF.h"
1820
#include "llvm/MC/MCStreamer.h"
@@ -501,9 +503,8 @@ bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
501503
if (Proc.Distance == PROC_DISTANCE_FAR)
502504
Sym->setIsFarProc();
503505

504-
getContext().setDefaultRetType(Proc.Distance == PROC_DISTANCE_NEAR
505-
? MCContext::IsNear
506-
: MCContext::IsFar);
506+
cast<MCMasmParser>(getParser())
507+
.setDefaultRetIsFar(Proc.Distance == PROC_DISTANCE_FAR);
507508

508509
if (Proc.IsFramed) {
509510
getStreamer().emitWinCFIStartProc(Sym, Loc);
@@ -529,11 +530,10 @@ bool COFFMasmParser::parseDirectiveEndProc(StringRef Directive, SMLoc Loc) {
529530
getStreamer().emitWinCFIEndProc(Loc);
530531
}
531532
CurrentProcedures.pop_back();
532-
getContext().setDefaultRetType(
533-
(CurrentProcedures.empty() ||
534-
CurrentProcedures.back().Distance == PROC_DISTANCE_NEAR)
535-
? MCContext::IsNear
536-
: MCContext::IsFar);
533+
cast<MCMasmParser>(getParser())
534+
.setDefaultRetIsFar(!CurrentProcedures.empty() &&
535+
CurrentProcedures.back().Distance ==
536+
PROC_DISTANCE_FAR);
537537
return false;
538538
}
539539

llvm/lib/MC/MCParser/MasmParser.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/MC/MCParser/MCAsmLexer.h"
3737
#include "llvm/MC/MCParser/MCAsmParser.h"
3838
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
39+
#include "llvm/MC/MCParser/MCMasmParser.h"
3940
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
4041
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
4142
#include "llvm/MC/MCRegisterInfo.h"
@@ -65,6 +66,7 @@
6566
#include <memory>
6667
#include <optional>
6768
#include <sstream>
69+
#include <stdbool.h>
6870
#include <string>
6971
#include <tuple>
7072
#include <utility>
@@ -373,7 +375,7 @@ FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
373375
/// The concrete assembly parser instance.
374376
// Note that this is a full MCAsmParser, not an MCAsmParserExtension!
375377
// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
376-
class MasmParser : public MCAsmParser {
378+
class MasmParser : public MCMasmParser {
377379
private:
378380
AsmLexer Lexer;
379381
MCContext &Ctx;
@@ -456,6 +458,9 @@ class MasmParser : public MCAsmParser {
456458
/// Are we parsing ms-style inline assembly?
457459
bool ParsingMSInlineAsm = false;
458460

461+
/// Is the current default `ret` instruction far?
462+
bool DefaultRetIsFar = false;
463+
459464
// Current <...> expression depth.
460465
unsigned AngleBracketDepth = 0U;
461466

@@ -481,6 +486,14 @@ class MasmParser : public MCAsmParser {
481486
DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
482487
}
483488

489+
/// @name MCMasmParser Interface
490+
/// {
491+
492+
bool getDefaultRetIsFar() const override { return DefaultRetIsFar; }
493+
void setDefaultRetIsFar(bool IsFar) override { DefaultRetIsFar = IsFar; }
494+
495+
/// }
496+
484497
/// @name MCAsmParser Interface
485498
/// {
486499

@@ -517,8 +530,6 @@ class MasmParser : public MCAsmParser {
517530
}
518531
bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
519532

520-
bool isParsingMasm() const override { return true; }
521-
522533
bool defineMacro(StringRef Name, StringRef Value) override;
523534

524535
bool lookUpField(StringRef Name, AsmFieldInfo &Info) const override;

llvm/lib/Target/X86/AsmParser/X86AsmParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/MC/MCInstrInfo.h"
2626
#include "llvm/MC/MCParser/MCAsmLexer.h"
2727
#include "llvm/MC/MCParser/MCAsmParser.h"
28+
#include "llvm/MC/MCParser/MCMasmParser.h"
2829
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
2930
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
3031
#include "llvm/MC/MCRegisterInfo.h"
@@ -3451,7 +3452,7 @@ bool X86AsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
34513452
// MASM implicitly converts "ret" to "retf" in far procedures; this is
34523453
// reflected in the default return type in the MCContext.
34533454
if (PatchedName == "ret" &&
3454-
getContext().getDefaultRetType() == MCContext::IsFar)
3455+
cast<MCMasmParser>(getParser()).getDefaultRetIsFar())
34553456
PatchedName = "retf";
34563457
}
34573458

0 commit comments

Comments
 (0)