Skip to content

Commit e9b8c1e

Browse files
authored
Revert "[ms] [llvm-ml] Implement support for PROC NEAR/FAR (#131707)"
This reverts commit 9b4f747.
1 parent 312d6b4 commit e9b8c1e

File tree

6 files changed

+26
-214
lines changed

6 files changed

+26
-214
lines changed

llvm/include/llvm/MC/MCParser/MCMasmParser.h

-29
This file was deleted.

llvm/include/llvm/MC/MCSymbolCOFF.h

-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class MCSymbolCOFF : public MCSymbol {
2525
SF_ClassShift = 0,
2626

2727
SF_SafeSEH = 0x0100,
28-
SF_FarProc = 0x0200,
2928
SF_WeakExternalCharacteristicsMask = 0x0E00,
3029
SF_WeakExternalCharacteristicsShift = 9,
3130
};
@@ -67,9 +66,6 @@ class MCSymbolCOFF : public MCSymbol {
6766
modifyFlags(SF_SafeSEH, SF_SafeSEH);
6867
}
6968

70-
bool isFarProc() const { return getFlags() & SF_FarProc; }
71-
void setIsFarProc() const { modifyFlags(SF_FarProc, SF_FarProc); }
72-
7369
static bool classof(const MCSymbol *S) { return S->isCOFF(); }
7470
};
7571

llvm/lib/MC/MCParser/COFFMasmParser.cpp

+21-59
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
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"
1615
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
17-
#include "llvm/MC/MCParser/MCMasmParser.h"
1816
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
1917
#include "llvm/MC/MCSectionCOFF.h"
2018
#include "llvm/MC/MCStreamer.h"
@@ -43,7 +41,6 @@ class COFFMasmParser : public MCAsmParserExtension {
4341
StringRef COMDATSymName, COFF::COMDATType Type,
4442
Align Alignment);
4543

46-
bool parseDirectiveModel(StringRef, SMLoc);
4744
bool parseDirectiveProc(StringRef, SMLoc);
4845
bool parseDirectiveEndProc(StringRef, SMLoc);
4946
bool parseDirectiveSegment(StringRef, SMLoc);
@@ -170,7 +167,7 @@ class COFFMasmParser : public MCAsmParserExtension {
170167
// .exit
171168
// .fardata
172169
// .fardata?
173-
addDirectiveHandler<&COFFMasmParser::parseDirectiveModel>(".model");
170+
addDirectiveHandler<&COFFMasmParser::IgnoreDirective>(".model");
174171
// .stack
175172
// .startup
176173

@@ -204,13 +201,8 @@ class COFFMasmParser : public MCAsmParserExtension {
204201
}
205202

206203
/// Stack of active procedure definitions.
207-
enum ProcDistance { PROC_DISTANCE_NEAR = 0, PROC_DISTANCE_FAR = 1 };
208-
struct ProcInfo {
209-
StringRef Name;
210-
ProcDistance Distance = PROC_DISTANCE_NEAR;
211-
bool IsFramed = false;
212-
};
213-
SmallVector<ProcInfo, 1> CurrentProcedures;
204+
SmallVector<StringRef, 1> CurrentProcedures;
205+
SmallVector<bool, 1> CurrentProceduresFramed;
214206

215207
public:
216208
COFFMasmParser() = default;
@@ -443,75 +435,48 @@ bool COFFMasmParser::parseDirectiveOption(StringRef Directive, SMLoc Loc) {
443435
return false;
444436
}
445437

446-
/// parseDirectiveModel
447-
/// ::= ".model" "flat"
448-
bool COFFMasmParser::parseDirectiveModel(StringRef Directive, SMLoc Loc) {
449-
if (!getLexer().is(AsmToken::Identifier))
450-
return TokError("expected identifier in directive");
451-
452-
StringRef ModelType = getTok().getIdentifier();
453-
if (!ModelType.equals_insensitive("flat")) {
454-
return TokError(
455-
"expected 'flat' for memory model; no other models supported");
456-
}
457-
458-
// Ignore; no action necessary.
459-
Lex();
460-
return false;
461-
}
462-
463438
/// parseDirectiveProc
464439
/// TODO(epastor): Implement parameters and other attributes.
465-
/// ::= label "proc" [[distance]] [[frame]]
440+
/// ::= label "proc" [[distance]]
466441
/// statements
467442
/// label "endproc"
468443
bool COFFMasmParser::parseDirectiveProc(StringRef Directive, SMLoc Loc) {
469444
if (!getStreamer().getCurrentFragment())
470445
return Error(getTok().getLoc(), "expected section directive");
471446

472-
ProcInfo Proc;
473-
if (getParser().parseIdentifier(Proc.Name))
447+
StringRef Label;
448+
if (getParser().parseIdentifier(Label))
474449
return Error(Loc, "expected identifier for procedure");
475-
while (getLexer().is(AsmToken::Identifier)) {
450+
if (getLexer().is(AsmToken::Identifier)) {
476451
StringRef nextVal = getTok().getString();
477452
SMLoc nextLoc = getTok().getLoc();
478453
if (nextVal.equals_insensitive("far")) {
454+
// TODO(epastor): Handle far procedure definitions.
479455
Lex();
480-
Proc.Distance = PROC_DISTANCE_FAR;
481-
nextVal = getTok().getString();
482-
nextLoc = getTok().getLoc();
456+
return Error(nextLoc, "far procedure definitions not yet supported");
483457
} else if (nextVal.equals_insensitive("near")) {
484458
Lex();
485-
Proc.Distance = PROC_DISTANCE_NEAR;
486-
nextVal = getTok().getString();
487-
nextLoc = getTok().getLoc();
488-
} else if (nextVal.equals_insensitive("frame")) {
489-
Lex();
490-
Proc.IsFramed = true;
491459
nextVal = getTok().getString();
492460
nextLoc = getTok().getLoc();
493-
} else {
494-
break;
495461
}
496462
}
497-
MCSymbolCOFF *Sym =
498-
cast<MCSymbolCOFF>(getContext().getOrCreateSymbol(Proc.Name));
463+
MCSymbolCOFF *Sym = cast<MCSymbolCOFF>(getContext().getOrCreateSymbol(Label));
499464

500465
// Define symbol as simple external function
501466
Sym->setExternal(true);
502467
Sym->setType(COFF::IMAGE_SYM_DTYPE_FUNCTION << COFF::SCT_COMPLEX_TYPE_SHIFT);
503-
if (Proc.Distance == PROC_DISTANCE_FAR)
504-
Sym->setIsFarProc();
505-
506-
cast<MCMasmParser>(getParser())
507-
.setDefaultRetIsFar(Proc.Distance == PROC_DISTANCE_FAR);
508468

509-
if (Proc.IsFramed) {
469+
bool Framed = false;
470+
if (getLexer().is(AsmToken::Identifier) &&
471+
getTok().getString().equals_insensitive("frame")) {
472+
Lex();
473+
Framed = true;
510474
getStreamer().emitWinCFIStartProc(Sym, Loc);
511475
}
512476
getStreamer().emitLabel(Sym, Loc);
513477

514-
CurrentProcedures.push_back(std::move(Proc));
478+
CurrentProcedures.push_back(Label);
479+
CurrentProceduresFramed.push_back(Framed);
515480
return false;
516481
}
517482
bool COFFMasmParser::parseDirectiveEndProc(StringRef Directive, SMLoc Loc) {
@@ -522,18 +487,15 @@ bool COFFMasmParser::parseDirectiveEndProc(StringRef Directive, SMLoc Loc) {
522487

523488
if (CurrentProcedures.empty())
524489
return Error(Loc, "endp outside of procedure block");
525-
else if (!CurrentProcedures.back().Name.equals_insensitive(Label))
490+
else if (!CurrentProcedures.back().equals_insensitive(Label))
526491
return Error(LabelLoc, "endp does not match current procedure '" +
527-
CurrentProcedures.back().Name + "'");
492+
CurrentProcedures.back() + "'");
528493

529-
if (CurrentProcedures.back().IsFramed) {
494+
if (CurrentProceduresFramed.back()) {
530495
getStreamer().emitWinCFIEndProc(Loc);
531496
}
532497
CurrentProcedures.pop_back();
533-
cast<MCMasmParser>(getParser())
534-
.setDefaultRetIsFar(!CurrentProcedures.empty() &&
535-
CurrentProcedures.back().Distance ==
536-
PROC_DISTANCE_FAR);
498+
CurrentProceduresFramed.pop_back();
537499
return false;
538500
}
539501

llvm/lib/MC/MCParser/MasmParser.cpp

+3-14
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
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"
4039
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
4140
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
4241
#include "llvm/MC/MCRegisterInfo.h"
@@ -66,7 +65,6 @@
6665
#include <memory>
6766
#include <optional>
6867
#include <sstream>
69-
#include <stdbool.h>
7068
#include <string>
7169
#include <tuple>
7270
#include <utility>
@@ -375,7 +373,7 @@ FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
375373
/// The concrete assembly parser instance.
376374
// Note that this is a full MCAsmParser, not an MCAsmParserExtension!
377375
// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
378-
class MasmParser : public MCMasmParser {
376+
class MasmParser : public MCAsmParser {
379377
private:
380378
SourceMgr::DiagHandlerTy SavedDiagHandler;
381379
void *SavedDiagContext;
@@ -450,9 +448,6 @@ class MasmParser : public MCMasmParser {
450448
/// Are we parsing ms-style inline assembly?
451449
bool ParsingMSInlineAsm = false;
452450

453-
/// Is the current default `ret` instruction far?
454-
bool DefaultRetIsFar = false;
455-
456451
// Current <...> expression depth.
457452
unsigned AngleBracketDepth = 0U;
458453

@@ -478,14 +473,6 @@ class MasmParser : public MCMasmParser {
478473
DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
479474
}
480475

481-
/// @name MCMasmParser Interface
482-
/// {
483-
484-
bool getDefaultRetIsFar() const override { return DefaultRetIsFar; }
485-
void setDefaultRetIsFar(bool IsFar) override { DefaultRetIsFar = IsFar; }
486-
487-
/// }
488-
489476
/// @name MCAsmParser Interface
490477
/// {
491478

@@ -517,6 +504,8 @@ class MasmParser : public MCMasmParser {
517504
}
518505
bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
519506

507+
bool isParsingMasm() const override { return true; }
508+
520509
bool defineMacro(StringRef Name, StringRef Value) override;
521510

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

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

+2-52
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
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"
2928
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
3029
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
3130
#include "llvm/MC/MCRegisterInfo.h"
3231
#include "llvm/MC/MCSection.h"
3332
#include "llvm/MC/MCStreamer.h"
3433
#include "llvm/MC/MCSubtargetInfo.h"
3534
#include "llvm/MC/MCSymbol.h"
36-
#include "llvm/MC/MCSymbolCOFF.h"
3735
#include "llvm/MC/TargetRegistry.h"
3836
#include "llvm/Support/CommandLine.h"
3937
#include "llvm/Support/Compiler.h"
@@ -1202,10 +1200,6 @@ class X86AsmParser : public MCTargetAsmParser {
12021200
void MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op, OperandVector &Operands,
12031201
MCStreamer &Out, bool MatchingInlineAsm);
12041202

1205-
void MatchMASMFarCallToNear(SMLoc IDLoc, X86Operand &Op,
1206-
OperandVector &Operands, MCStreamer &Out,
1207-
bool MatchingInlineAsm);
1208-
12091203
bool ErrorMissingFeature(SMLoc IDLoc, const FeatureBitset &MissingFeatures,
12101204
bool MatchingInlineAsm);
12111205

@@ -2744,11 +2738,11 @@ bool X86AsmParser::parseIntelOperand(OperandVector &Operands, StringRef Name) {
27442738
if ((BaseReg || IndexReg || RegNo || DefaultBaseReg))
27452739
Operands.push_back(X86Operand::CreateMem(
27462740
getPointerWidth(), RegNo, Disp, BaseReg, IndexReg, Scale, Start, End,
2747-
Size, DefaultBaseReg, /*SymName=*/SM.getSymName(), /*OpDecl=*/nullptr,
2741+
Size, DefaultBaseReg, /*SymName=*/StringRef(), /*OpDecl=*/nullptr,
27482742
/*FrontendSize=*/0, /*UseUpRegs=*/false, MaybeDirectBranchDest));
27492743
else
27502744
Operands.push_back(X86Operand::CreateMem(
2751-
getPointerWidth(), Disp, Start, End, Size, /*SymName=*/SM.getSymName(),
2745+
getPointerWidth(), Disp, Start, End, Size, /*SymName=*/StringRef(),
27522746
/*OpDecl=*/nullptr, /*FrontendSize=*/0, /*UseUpRegs=*/false,
27532747
MaybeDirectBranchDest));
27542748
return false;
@@ -3446,14 +3440,6 @@ bool X86AsmParser::parseInstruction(ParseInstructionInfo &Info, StringRef Name,
34463440
}
34473441
}
34483442

3449-
if (Parser.isParsingMasm() && !is64BitMode()) {
3450-
// MASM implicitly converts "ret" to "retf" in far procedures; this is
3451-
// reflected in the default return type in the MCContext.
3452-
if (PatchedName == "ret" &&
3453-
cast<MCMasmParser>(getParser()).getDefaultRetIsFar())
3454-
PatchedName = "retf";
3455-
}
3456-
34573443
// Determine whether this is an instruction prefix.
34583444
// FIXME:
34593445
// Enhance prefixes integrity robustness. for example, following forms
@@ -4142,11 +4128,6 @@ bool X86AsmParser::matchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode,
41424128
// First, handle aliases that expand to multiple instructions.
41434129
MatchFPUWaitAlias(IDLoc, static_cast<X86Operand &>(*Operands[0]), Operands,
41444130
Out, MatchingInlineAsm);
4145-
if (getParser().isParsingMasm() && !is64BitMode()) {
4146-
MatchMASMFarCallToNear(IDLoc, static_cast<X86Operand &>(*Operands[0]),
4147-
Operands, Out, MatchingInlineAsm);
4148-
}
4149-
41504131
unsigned Prefixes = getPrefixes(Operands);
41514132

41524133
MCInst Inst;
@@ -4208,37 +4189,6 @@ void X86AsmParser::MatchFPUWaitAlias(SMLoc IDLoc, X86Operand &Op,
42084189
}
42094190
}
42104191

4211-
void X86AsmParser::MatchMASMFarCallToNear(SMLoc IDLoc, X86Operand &Op,
4212-
OperandVector &Operands,
4213-
MCStreamer &Out,
4214-
bool MatchingInlineAsm) {
4215-
// FIXME: This should be replaced with a real .td file alias mechanism.
4216-
// Also, MatchInstructionImpl should actually *do* the EmitInstruction
4217-
// call.
4218-
if (Op.getToken() != "call")
4219-
return;
4220-
// This is a call instruction...
4221-
4222-
X86Operand &Operand = static_cast<X86Operand &>(*Operands[1]);
4223-
MCSymbol *Sym = getContext().lookupSymbol(Operand.getSymName());
4224-
if (Sym == nullptr || !Sym->isInSection() || !Sym->isCOFF() ||
4225-
!dyn_cast<MCSymbolCOFF>(Sym)->isFarProc())
4226-
return;
4227-
// Sym is a reference to a far proc in a code section....
4228-
4229-
if (Out.getCurrentSectionOnly() == &Sym->getSection()) {
4230-
// This is a call to a symbol declared as a far proc, and will be emitted as
4231-
// a near call... so we need to explicitly push the code section register
4232-
// before the call.
4233-
MCInst Inst;
4234-
Inst.setOpcode(X86::PUSH32r);
4235-
Inst.addOperand(MCOperand::createReg(MCRegister(X86::CS)));
4236-
Inst.setLoc(IDLoc);
4237-
if (!MatchingInlineAsm)
4238-
emitInstruction(Inst, Operands, Out);
4239-
}
4240-
}
4241-
42424192
bool X86AsmParser::ErrorMissingFeature(SMLoc IDLoc,
42434193
const FeatureBitset &MissingFeatures,
42444194
bool MatchingInlineAsm) {

0 commit comments

Comments
 (0)