Skip to content

Commit c626ef9

Browse files
[SystemZ] Implement .machine (push|pop) directives (#137302)
The `.machine push` and `.machine pop` directives were missing from the SystemZ Backend Asm Parser. This PR adds them, and expands the corresponding test to test proper operation. Internally, this is modeled as a simple stack implemented on a `SmallVector<StringRef>`.
1 parent 1a14ef1 commit c626ef9

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

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

Lines changed: 31 additions & 9 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,12 @@ 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 uses `setAvailableFeatures` to
417+
// apply the result.
418+
SmallVector<FeatureBitset> MachineStack;
419+
413420
enum RegisterGroup {
414421
RegGR,
415422
RegFP,
@@ -494,9 +501,8 @@ class SystemZAsmParser : public MCTargetAsmParser {
494501

495502
public:
496503
SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
497-
const MCInstrInfo &MII,
498-
const MCTargetOptions &Options)
499-
: MCTargetAsmParser(Options, sti, MII), Parser(parser) {
504+
const MCInstrInfo &MII, const MCTargetOptions &Options)
505+
: MCTargetAsmParser(Options, sti, MII), Parser(parser) {
500506
MCAsmParserExtension::Initialize(Parser);
501507

502508
// Alias the .word directive to .short.
@@ -1382,16 +1388,32 @@ bool SystemZAsmParser::parseDirectiveMachine(SMLoc L) {
13821388
Parser.getTok().isNot(AsmToken::String))
13831389
return TokError("unexpected token in '.machine' directive");
13841390

1385-
StringRef CPU = Parser.getTok().getIdentifier();
1391+
StringRef Id = Parser.getTok().getIdentifier();
1392+
SMLoc IdLoc = Parser.getTok().getLoc();
1393+
13861394
Parser.Lex();
13871395
if (parseEOL())
13881396
return true;
13891397

1390-
MCSubtargetInfo &STI = copySTI();
1391-
STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
1392-
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
1393-
1394-
getTargetStreamer().emitMachine(CPU);
1398+
// Parse push and pop directives first
1399+
if (Id == "push") {
1400+
// Push the Current FeatureBitSet onto the stack.
1401+
MachineStack.push_back(getAvailableFeatures());
1402+
} else if (Id == "pop") {
1403+
// If the stack is not empty pop the topmost FeatureBitset and use it.
1404+
if (MachineStack.empty())
1405+
return Error(IdLoc,
1406+
"pop without corresponding push in '.machine' directive");
1407+
setAvailableFeatures(MachineStack.back());
1408+
MachineStack.pop_back();
1409+
} else {
1410+
// Try to interpret the Identifier as a CPU spec and derive the
1411+
// FeatureBitset from that.
1412+
MCSubtargetInfo &STI = copySTI();
1413+
STI.setDefaultFeatures(Id, /*TuneCPU*/ Id, "");
1414+
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
1415+
}
1416+
getTargetStreamer().emitMachine(Id);
13951417

13961418
return false;
13971419
}

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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
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
9+
10+
.machine pop z13
11+
// CHECK: :[[@LINE-1]]:14: error: expected newline
512

6-
# CHECK: [[#@LINE+1]]:10: error: unexpected token in '.machine' directive
713
.machine 42
14+
// CHECK: :[[@LINE-1]]:10: error: unexpected token in '.machine' directive
815

9-
# CHECK: [[#@LINE+1]]:13: error: expected newline
1016
.machine z13+
17+
// 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)