|
33 | 33 | #include "llvm/Support/Casting.h" |
34 | 34 | #include "llvm/Support/ErrorHandling.h" |
35 | 35 | #include "llvm/Support/SMLoc.h" |
| 36 | +#include "llvm/TargetParser/SubtargetFeature.h" |
36 | 37 | #include <algorithm> |
37 | 38 | #include <cassert> |
38 | 39 | #include <cstddef> |
@@ -410,6 +411,18 @@ class SystemZAsmParser : public MCTargetAsmParser { |
410 | 411 |
|
411 | 412 | private: |
412 | 413 | 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 | + |
413 | 426 | enum RegisterGroup { |
414 | 427 | RegGR, |
415 | 428 | RegFP, |
@@ -494,16 +507,16 @@ class SystemZAsmParser : public MCTargetAsmParser { |
494 | 507 |
|
495 | 508 | public: |
496 | 509 | 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) { |
500 | 512 | MCAsmParserExtension::Initialize(Parser); |
501 | 513 |
|
502 | 514 | // Alias the .word directive to .short. |
503 | 515 | parser.addAliasForDirective(".word", ".short"); |
504 | 516 |
|
505 | 517 | // Initialize the set of available features. |
506 | | - setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); |
| 518 | + CurrentFeatures = sti.getFeatureBits(); |
| 519 | + setAvailableFeatures(ComputeAvailableFeatures(CurrentFeatures)); |
507 | 520 | } |
508 | 521 |
|
509 | 522 | // Override MCTargetAsmParser. |
@@ -1382,17 +1395,35 @@ bool SystemZAsmParser::parseDirectiveMachine(SMLoc L) { |
1382 | 1395 | Parser.getTok().isNot(AsmToken::String)) |
1383 | 1396 | return TokError("unexpected token in '.machine' directive"); |
1384 | 1397 |
|
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 | + |
1386 | 1423 | Parser.Lex(); |
1387 | 1424 | if (parseEOL()) |
1388 | 1425 | return true; |
1389 | 1426 |
|
1390 | | - MCSubtargetInfo &STI = copySTI(); |
1391 | | - STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, ""); |
1392 | | - setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
1393 | | - |
1394 | | - getTargetStreamer().emitMachine(CPU); |
1395 | | - |
1396 | 1427 | return false; |
1397 | 1428 | } |
1398 | 1429 |
|
|
0 commit comments