Skip to content

Commit 0834e7a

Browse files
[SystemZ] Implement .machine (push|pop) directives
The `.machine push` and `.machine pop` directives were missing from the SystemZ Backend Asm Parser. This commit adds them, and expands the corresponding test to test proper operation.
1 parent 194da37 commit 0834e7a

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Support/Casting.h"
3434
#include "llvm/Support/ErrorHandling.h"
3535
#include "llvm/Support/SMLoc.h"
36+
#include "llvm/TargetParser/SubtargetFeature.h"
3637
#include <algorithm>
3738
#include <cassert>
3839
#include <cstddef>
@@ -410,6 +411,18 @@ class SystemZAsmParser : public MCTargetAsmParser {
410411

411412
private:
412413
MCAsmParser &Parser;
414+
415+
// A vector to contain the stack of FeatureBitsets created by `.machine push`.
416+
// `.machine pop` pops the top of the stack and resets `CurrentFeatures` to
417+
// the result.
418+
SmallVector<FeatureBitset> MachineStack;
419+
// Specifies the current FeatureBitset. It is initialized from the
420+
// SubtargetInfo handed to the constructor of the AsmParser. During parsing,
421+
// it always reflects the current FeatureBitset used to determine which
422+
// machine features are available, i.e. either the default or the result
423+
// of the most reecent `.machine` directive.
424+
FeatureBitset CurrentFeatures;
425+
413426
enum RegisterGroup {
414427
RegGR,
415428
RegFP,
@@ -494,16 +507,16 @@ class SystemZAsmParser : public MCTargetAsmParser {
494507

495508
public:
496509
SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
497-
const MCInstrInfo &MII,
498-
const MCTargetOptions &Options)
499-
: MCTargetAsmParser(Options, sti, MII), Parser(parser) {
510+
const MCInstrInfo &MII, const MCTargetOptions &Options)
511+
: MCTargetAsmParser(Options, sti, MII), Parser(parser) {
500512
MCAsmParserExtension::Initialize(Parser);
501513

502514
// Alias the .word directive to .short.
503515
parser.addAliasForDirective(".word", ".short");
504516

505517
// Initialize the set of available features.
506-
setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
518+
CurrentFeatures = sti.getFeatureBits();
519+
setAvailableFeatures(ComputeAvailableFeatures(CurrentFeatures));
507520
}
508521

509522
// Override MCTargetAsmParser.
@@ -1382,17 +1395,35 @@ bool SystemZAsmParser::parseDirectiveMachine(SMLoc L) {
13821395
Parser.getTok().isNot(AsmToken::String))
13831396
return TokError("unexpected token in '.machine' directive");
13841397

1385-
StringRef CPU = Parser.getTok().getIdentifier();
1398+
StringRef Id = Parser.getTok().getIdentifier();
1399+
1400+
// Parse push and pop directives first
1401+
if (Id == "push") {
1402+
// Push the Current FeatureBitSet onto the stack.
1403+
MachineStack.push_back(CurrentFeatures);
1404+
getTargetStreamer().emitMachine("push");
1405+
} else if (Id == "pop") {
1406+
// If the Stack is not empty, pop the topmost FeatureBitset and use it.
1407+
if (MachineStack.empty())
1408+
return TokError("pop without corresponding push in '.machine' directive");
1409+
CurrentFeatures = MachineStack.back();
1410+
MachineStack.pop_back();
1411+
setAvailableFeatures(CurrentFeatures);
1412+
getTargetStreamer().emitMachine("pop");
1413+
} else {
1414+
// Try to interpret the Identifier as a CPU spec and derive the
1415+
// FeatureBitset from that.
1416+
MCSubtargetInfo &STI = copySTI();
1417+
STI.setDefaultFeatures(Id, /*TuneCPU*/ Id, "");
1418+
CurrentFeatures = STI.getFeatureBits();
1419+
setAvailableFeatures(ComputeAvailableFeatures(CurrentFeatures));
1420+
getTargetStreamer().emitMachine(Id);
1421+
}
1422+
13861423
Parser.Lex();
13871424
if (parseEOL())
13881425
return true;
13891426

1390-
MCSubtargetInfo &STI = copySTI();
1391-
STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
1392-
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
1393-
1394-
getTargetStreamer().emitMachine(CPU);
1395-
13961427
return false;
13971428
}
13981429

llvm/lib/Target/SystemZ/MCTargetDesc/SystemZTargetStreamer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SystemZTargetStreamer : public MCTargetStreamer {
5454

5555
void emitConstantPools() override;
5656

57-
virtual void emitMachine(StringRef CPU) {};
57+
virtual void emitMachine(StringRef CPUOrCommand) {};
5858

5959
virtual void emitExtern(StringRef Str) {};
6060

@@ -85,7 +85,7 @@ class SystemZTargetHLASMStreamer : public SystemZTargetStreamer {
8585
class SystemZTargetELFStreamer : public SystemZTargetStreamer {
8686
public:
8787
SystemZTargetELFStreamer(MCStreamer &S) : SystemZTargetStreamer(S) {}
88-
void emitMachine(StringRef CPU) override {}
88+
void emitMachine(StringRef CPUOrCommand) override {}
8989
};
9090

9191
class SystemZTargetGNUStreamer : public SystemZTargetStreamer {
@@ -94,8 +94,8 @@ class SystemZTargetGNUStreamer : public SystemZTargetStreamer {
9494
public:
9595
SystemZTargetGNUStreamer(MCStreamer &S, formatted_raw_ostream &OS)
9696
: SystemZTargetStreamer(S), OS(OS) {}
97-
void emitMachine(StringRef CPU) override {
98-
OS << "\t.machine " << CPU << "\n";
97+
void emitMachine(StringRef CPUOrCommand) override {
98+
OS << "\t.machine " << CPUOrCommand << "\n";
9999
}
100100
};
101101

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 5
12
# RUN: not llvm-mc -triple=s390x %s 2>&1 | FileCheck %s
23

3-
# CHECK: [[#@LINE+1]]:9: error: unexpected token in '.machine' directive
44
.machine
5+
// CHECK: :[[@LINE-1]]:9: error: unexpected token in '.machine' directive
6+
7+
.machine pop
8+
// CHECK: :[[@LINE-1]]:10: error: pop without corresponding push in '.machine' directive
59

6-
# CHECK: [[#@LINE+1]]:10: error: unexpected token in '.machine' directive
710
.machine 42
11+
// CHECK: :[[@LINE-1]]:10: error: unexpected token in '.machine' directive
812

9-
# CHECK: [[#@LINE+1]]:13: error: expected newline
1013
.machine z13+
14+
// CHECK: :[[@LINE-1]]:13: error: expected newline

llvm/test/MC/SystemZ/machine-directive.s

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,20 @@
55
# CHECK: ^
66
77
# CHECK-NOT: error:
8+
# CHECK: .machine push
89
# CHECK: .machine z13
910
# CHECK: vgbm %v0, 0
1011
# CHECK: .machine zEC12
11-
# CHECK: .machine z13
12+
# CHECK: .machine pop
1213
# CHECK: vgbm %v0, 3
14+
# CHECK: .machine pop
1315

16+
.machine push
1417
.machine z13
18+
.machine push
1519
vgbm %v0, 0
1620
.machine zEC12
1721
vgbm %v0, 1
18-
.machine z13
22+
.machine pop
1923
vgbm %v0, 3
20-
24+
.machine pop

0 commit comments

Comments
 (0)