Skip to content

Commit 35949c3

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 35949c3

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

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

Lines changed: 45 additions & 10 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 machine specs created by `.machine push`.
416+
SmallVector<StringRef> MachineStack;
417+
// Specifies the current CPU. It is initialized to "default", which represents
418+
// the feature bit set in use before any `.machine` directive is issued.
419+
StringRef CurrentCPU = "default";
420+
// The feature bits associated with the "default" cpu. Derived from the
421+
// SubtargetInfo handed to the constructor of the AsmParser. This is
422+
// used to recreate the feature bits if the "default" cpu is saved by a
423+
// `.machine push` and then restored by a `.machine pop`.
424+
const FeatureBitset &DefaultBits;
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),
512+
DefaultBits(sti.getFeatureBits()) {
500513
MCAsmParserExtension::Initialize(Parser);
501514

502515
// Alias the .word directive to .short.
503516
parser.addAliasForDirective(".word", ".short");
504517

505518
// Initialize the set of available features.
506-
setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
519+
setAvailableFeatures(ComputeAvailableFeatures(DefaultBits));
507520
}
508521

509522
// Override MCTargetAsmParser.
@@ -1382,17 +1395,39 @@ 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+
StringRef CPU = "";
1402+
if (Id == "push") {
1403+
if (CurrentCPU.empty())
1404+
llvm_unreachable(".machine push without a cpu to save");
1405+
MachineStack.push_back(CurrentCPU);
1406+
} else if (Id == "pop") {
1407+
if (MachineStack.empty())
1408+
return TokError("pop without corresponding push in '.machine' directive");
1409+
CPU = MachineStack.back();
1410+
MachineStack.pop_back();
1411+
} else {
1412+
CPU = Id;
1413+
}
1414+
13861415
Parser.Lex();
13871416
if (parseEOL())
13881417
return true;
13891418

1390-
MCSubtargetInfo &STI = copySTI();
1391-
STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
1392-
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
1393-
1394-
getTargetStreamer().emitMachine(CPU);
1419+
// check if we need to change cpu
1420+
if (CPU == "default") {
1421+
setAvailableFeatures(DefaultBits);
1422+
CurrentCPU = "default";
1423+
} else if (!CPU.empty()) {
1424+
MCSubtargetInfo &STI = copySTI();
1425+
STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
1426+
setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
1427+
CurrentCPU = CPU;
13951428

1429+
getTargetStreamer().emitMachine(CPU);
1430+
}
13961431
return false;
13971432
}
13981433

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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
# CHECK: .machine z13
1212
# CHECK: vgbm %v0, 3
1313

14+
.machine push
1415
.machine z13
16+
.machine push
1517
vgbm %v0, 0
1618
.machine zEC12
1719
vgbm %v0, 1
18-
.machine z13
20+
.machine pop
1921
vgbm %v0, 3
20-
22+
.machine pop

0 commit comments

Comments
 (0)