llvm.org GIT mirror llvm / fcfc7b2
[CodeGen] Move printing MO_CFIIndex operands to MachineOperand::print Work towards the unification of MIR and debug output by refactoring the interfaces. Before this patch we printed "<call frame instruction>" in the debug output. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321084 91177308-0d34-0410-b5e6-96231b3b80d8 Francis Visoiu Mistrih 2 years ago
5 changed file(s) with 165 addition(s) and 122 deletion(s). Raw diff Collapse all Expand all
690690 .. code-block:: text
691691
692692 EH_LABEL
693
694 CFIIndex Operands
695 ^^^^^^^^^^^^^^^^^
696
697 A CFI Index operand is holding an index into a per-function side-table,
698 ``MachineFunction::getFrameInstructions()``, which references all the frame
699 instructions in a ``MachineFunction``. A ``CFI_INSTRUCTION`` may look like it
700 contains multiple operands, but the only operand it contains is the CFI Index.
701 The other operands are tracked by the ``MCCFIInstruction`` object.
702
703 The syntax is:
704
705 .. code-block:: text
706
707 CFI_INSTRUCTION offset %w30, -16
708
709 which may be emitted later in the MC layer as:
710
711 .. code-block:: text
712
713 .cfi_offset w30, -16
693714
694715 .. TODO: Describe the parsers default behaviour when optional YAML attributes
695716 are missing.
701722 .. TODO: Describe the syntax of the stack object machine operands and their
702723 YAML definitions.
703724 .. TODO: Describe the syntax of the block address machine operands.
704 .. TODO: Describe the syntax of the CFI index machine operands.
705725 .. TODO: Describe the syntax of the metadata machine operands, and the
706726 instructions debug location attribute.
707727 .. TODO: Describe the syntax of the register live out machine operands.
2828 class MachineBasicBlock;
2929 class MachineInstr;
3030 class MachineRegisterInfo;
31 class MCCFIInstruction;
3132 class MDNode;
3233 class ModuleSlotTracker;
3334 class TargetMachine;
794794 case MachineOperand::MO_GlobalAddress:
795795 case MachineOperand::MO_RegisterLiveOut:
796796 case MachineOperand::MO_Metadata:
797 case MachineOperand::MO_MCSymbol: {
797 case MachineOperand::MO_MCSymbol:
798 case MachineOperand::MO_CFIIndex: {
798799 unsigned TiedOperandIdx = 0;
799800 if (ShouldPrintRegisterTies && Op.isReg() && Op.isTied() && !Op.isDef())
800801 TiedOperandIdx = Op.getParent()->findTiedOperandIdx(OpIdx);
824825 OS << StringRef(TRI->getRegMaskNames()[RegMaskInfo->second]).lower();
825826 else
826827 printCustomRegMask(Op.getRegMask(), OS, TRI);
827 break;
828 }
829 case MachineOperand::MO_CFIIndex: {
830 const MachineFunction &MF = *Op.getParent()->getMF();
831 print(MF.getFrameInstructions()[Op.getCFIIndex()], TRI);
832828 break;
833829 }
834830 case MachineOperand::MO_IntrinsicID: {
977973 }
978974 }
979975
980 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
981 const TargetRegisterInfo *TRI) {
982 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
983 if (Reg == -1) {
984 OS << "";
985 return;
986 }
987 OS << printReg(Reg, TRI);
988 }
989
990 void MIPrinter::print(const MCCFIInstruction &CFI,
991 const TargetRegisterInfo *TRI) {
992 switch (CFI.getOperation()) {
993 case MCCFIInstruction::OpSameValue:
994 OS << "same_value ";
995 if (MCSymbol *Label = CFI.getLabel())
996 MachineOperand::printSymbol(OS, *Label);
997 printCFIRegister(CFI.getRegister(), OS, TRI);
998 break;
999 case MCCFIInstruction::OpRememberState:
1000 OS << "remember_state ";
1001 if (MCSymbol *Label = CFI.getLabel())
1002 MachineOperand::printSymbol(OS, *Label);
1003 break;
1004 case MCCFIInstruction::OpRestoreState:
1005 OS << "restore_state ";
1006 if (MCSymbol *Label = CFI.getLabel())
1007 MachineOperand::printSymbol(OS, *Label);
1008 break;
1009 case MCCFIInstruction::OpOffset:
1010 OS << "offset ";
1011 if (MCSymbol *Label = CFI.getLabel())
1012 MachineOperand::printSymbol(OS, *Label);
1013 printCFIRegister(CFI.getRegister(), OS, TRI);
1014 OS << ", " << CFI.getOffset();
1015 break;
1016 case MCCFIInstruction::OpDefCfaRegister:
1017 OS << "def_cfa_register ";
1018 if (MCSymbol *Label = CFI.getLabel())
1019 MachineOperand::printSymbol(OS, *Label);
1020 printCFIRegister(CFI.getRegister(), OS, TRI);
1021 break;
1022 case MCCFIInstruction::OpDefCfaOffset:
1023 OS << "def_cfa_offset ";
1024 if (MCSymbol *Label = CFI.getLabel())
1025 MachineOperand::printSymbol(OS, *Label);
1026 OS << CFI.getOffset();
1027 break;
1028 case MCCFIInstruction::OpDefCfa:
1029 OS << "def_cfa ";
1030 if (MCSymbol *Label = CFI.getLabel())
1031 MachineOperand::printSymbol(OS, *Label);
1032 printCFIRegister(CFI.getRegister(), OS, TRI);
1033 OS << ", " << CFI.getOffset();
1034 break;
1035 case MCCFIInstruction::OpRelOffset:
1036 OS << "rel_offset ";
1037 if (MCSymbol *Label = CFI.getLabel())
1038 MachineOperand::printSymbol(OS, *Label);
1039 printCFIRegister(CFI.getRegister(), OS, TRI);
1040 OS << ", " << CFI.getOffset();
1041 break;
1042 case MCCFIInstruction::OpAdjustCfaOffset:
1043 OS << "adjust_cfa_offset ";
1044 if (MCSymbol *Label = CFI.getLabel())
1045 MachineOperand::printSymbol(OS, *Label);
1046 OS << CFI.getOffset();
1047 break;
1048 case MCCFIInstruction::OpRestore:
1049 OS << "restore ";
1050 if (MCSymbol *Label = CFI.getLabel())
1051 MachineOperand::printSymbol(OS, *Label);
1052 printCFIRegister(CFI.getRegister(), OS, TRI);
1053 break;
1054 case MCCFIInstruction::OpEscape: {
1055 OS << "escape ";
1056 if (MCSymbol *Label = CFI.getLabel())
1057 MachineOperand::printSymbol(OS, *Label);
1058 if (!CFI.getValues().empty()) {
1059 size_t e = CFI.getValues().size() - 1;
1060 for (size_t i = 0; i < e; ++i)
1061 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
1062 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
1063 }
1064 break;
1065 }
1066 case MCCFIInstruction::OpUndefined:
1067 OS << "undefined ";
1068 if (MCSymbol *Label = CFI.getLabel())
1069 MachineOperand::printSymbol(OS, *Label);
1070 printCFIRegister(CFI.getRegister(), OS, TRI);
1071 break;
1072 case MCCFIInstruction::OpRegister:
1073 OS << "register ";
1074 if (MCSymbol *Label = CFI.getLabel())
1075 MachineOperand::printSymbol(OS, *Label);
1076 printCFIRegister(CFI.getRegister(), OS, TRI);
1077 OS << ", ";
1078 printCFIRegister(CFI.getRegister2(), OS, TRI);
1079 break;
1080 case MCCFIInstruction::OpWindowSave:
1081 OS << "window_save ";
1082 if (MCSymbol *Label = CFI.getLabel())
1083 MachineOperand::printSymbol(OS, *Label);
1084 break;
1085 default:
1086 // TODO: Print the other CFI Operations.
1087 OS << "";
1088 break;
1089 }
1090 }
1091
1092976 void llvm::printMIR(raw_ostream &OS, const Module &M) {
1093977 yaml::Output Out(OS);
1094978 Out << const_cast(M);
411411 return nullptr;
412412 }
413413
414 static void printCFIRegister(unsigned DwarfReg, raw_ostream &OS,
415 const TargetRegisterInfo *TRI) {
416 if (!TRI) {
417 OS << "%dwarfreg." << DwarfReg;
418 return;
419 }
420
421 int Reg = TRI->getLLVMRegNum(DwarfReg, true);
422 if (Reg == -1) {
423 OS << "";
424 return;
425 }
426 OS << printReg(Reg, TRI);
427 }
428
414429 void MachineOperand::printSubregIdx(raw_ostream &OS, uint64_t Index,
415430 const TargetRegisterInfo *TRI) {
416431 OS << "%subreg.";
487502 OS << "%stack." << FrameIndex;
488503 if (!Name.empty())
489504 OS << '.' << Name;
505 }
506
507 static void printCFI(raw_ostream &OS, const MCCFIInstruction &CFI,
508 const TargetRegisterInfo *TRI) {
509 switch (CFI.getOperation()) {
510 case MCCFIInstruction::OpSameValue:
511 OS << "same_value ";
512 if (MCSymbol *Label = CFI.getLabel())
513 MachineOperand::printSymbol(OS, *Label);
514 printCFIRegister(CFI.getRegister(), OS, TRI);
515 break;
516 case MCCFIInstruction::OpRememberState:
517 OS << "remember_state ";
518 if (MCSymbol *Label = CFI.getLabel())
519 MachineOperand::printSymbol(OS, *Label);
520 break;
521 case MCCFIInstruction::OpRestoreState:
522 OS << "restore_state ";
523 if (MCSymbol *Label = CFI.getLabel())
524 MachineOperand::printSymbol(OS, *Label);
525 break;
526 case MCCFIInstruction::OpOffset:
527 OS << "offset ";
528 if (MCSymbol *Label = CFI.getLabel())
529 MachineOperand::printSymbol(OS, *Label);
530 printCFIRegister(CFI.getRegister(), OS, TRI);
531 OS << ", " << CFI.getOffset();
532 break;
533 case MCCFIInstruction::OpDefCfaRegister:
534 OS << "def_cfa_register ";
535 if (MCSymbol *Label = CFI.getLabel())
536 MachineOperand::printSymbol(OS, *Label);
537 printCFIRegister(CFI.getRegister(), OS, TRI);
538 break;
539 case MCCFIInstruction::OpDefCfaOffset:
540 OS << "def_cfa_offset ";
541 if (MCSymbol *Label = CFI.getLabel())
542 MachineOperand::printSymbol(OS, *Label);
543 OS << CFI.getOffset();
544 break;
545 case MCCFIInstruction::OpDefCfa:
546 OS << "def_cfa ";
547 if (MCSymbol *Label = CFI.getLabel())
548 MachineOperand::printSymbol(OS, *Label);
549 printCFIRegister(CFI.getRegister(), OS, TRI);
550 OS << ", " << CFI.getOffset();
551 break;
552 case MCCFIInstruction::OpRelOffset:
553 OS << "rel_offset ";
554 if (MCSymbol *Label = CFI.getLabel())
555 MachineOperand::printSymbol(OS, *Label);
556 printCFIRegister(CFI.getRegister(), OS, TRI);
557 OS << ", " << CFI.getOffset();
558 break;
559 case MCCFIInstruction::OpAdjustCfaOffset:
560 OS << "adjust_cfa_offset ";
561 if (MCSymbol *Label = CFI.getLabel())
562 MachineOperand::printSymbol(OS, *Label);
563 OS << CFI.getOffset();
564 break;
565 case MCCFIInstruction::OpRestore:
566 OS << "restore ";
567 if (MCSymbol *Label = CFI.getLabel())
568 MachineOperand::printSymbol(OS, *Label);
569 printCFIRegister(CFI.getRegister(), OS, TRI);
570 break;
571 case MCCFIInstruction::OpEscape: {
572 OS << "escape ";
573 if (MCSymbol *Label = CFI.getLabel())
574 MachineOperand::printSymbol(OS, *Label);
575 if (!CFI.getValues().empty()) {
576 size_t e = CFI.getValues().size() - 1;
577 for (size_t i = 0; i < e; ++i)
578 OS << format("0x%02x", uint8_t(CFI.getValues()[i])) << ", ";
579 OS << format("0x%02x", uint8_t(CFI.getValues()[e])) << ", ";
580 }
581 break;
582 }
583 case MCCFIInstruction::OpUndefined:
584 OS << "undefined ";
585 if (MCSymbol *Label = CFI.getLabel())
586 MachineOperand::printSymbol(OS, *Label);
587 printCFIRegister(CFI.getRegister(), OS, TRI);
588 break;
589 case MCCFIInstruction::OpRegister:
590 OS << "register ";
591 if (MCSymbol *Label = CFI.getLabel())
592 MachineOperand::printSymbol(OS, *Label);
593 printCFIRegister(CFI.getRegister(), OS, TRI);
594 OS << ", ";
595 printCFIRegister(CFI.getRegister2(), OS, TRI);
596 break;
597 case MCCFIInstruction::OpWindowSave:
598 OS << "window_save ";
599 if (MCSymbol *Label = CFI.getLabel())
600 MachineOperand::printSymbol(OS, *Label);
601 break;
602 default:
603 // TODO: Print the other CFI Operations.
604 OS << "";
605 break;
606 }
490607 }
491608
492609 void MachineOperand::print(raw_ostream &OS, const TargetRegisterInfo *TRI,
692809 case MachineOperand::MO_MCSymbol:
693810 printSymbol(OS, *getMCSymbol());
694811 break;
695 case MachineOperand::MO_CFIIndex:
696 OS << "";
697 break;
812 case MachineOperand::MO_CFIIndex: {
813 if (const MachineFunction *MF = getMFIfAvailable(*this))
814 printCFI(OS, MF->getFrameInstructions()[getCFIIndex()], TRI);
815 else
816 OS << "";
817 break;
818 }
698819 case MachineOperand::MO_IntrinsicID: {
699820 Intrinsic::ID ID = getIntrinsicID();
700821 if (ID < Intrinsic::num_intrinsics)
335335 ASSERT_TRUE(OS.str() == "");
336336 }
337337
338 TEST(MachineOperandTest, PrintCFI) {
339 // Create a MachineOperand with a CFI index but no function and print it.
340 MachineOperand MO = MachineOperand::CreateCFIIndex(8);
341
342 // Checking some preconditions on the newly created
343 // MachineOperand.
344 ASSERT_TRUE(MO.isCFIIndex());
345 ASSERT_TRUE(MO.getCFIIndex() == 8);
346
347 std::string str;
348 // Print a MachineOperand containing a CFI Index node but no machine function
349 // attached to it.
350 raw_string_ostream OS(str);
351 MO.print(OS, /*TRI=*/nullptr, /*IntrinsicInfo=*/nullptr);
352 ASSERT_TRUE(OS.str() == "");
353 }
354
338355 } // end namespace