Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 1 | //===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This class prints an MSP430 MCInst to a .s file. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 13 | #include "MSP430InstPrinter.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "MSP430.h" |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAsmInfo.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCInst.h" |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCInstrInfo.h" |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/FormattedStream.h" |
| 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "asm-printer" |
| 24 | |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 25 | // Include the auto-generated portion of the assembly writer. |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 26 | #define PRINT_ALIAS_INSTR |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 27 | #include "MSP430GenAsmWriter.inc" |
| 28 | |
Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 29 | void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O, |
Akira Hatanaka | b46d023 | 2015-03-27 20:36:02 +0000 | [diff] [blame] | 30 | StringRef Annot, const MCSubtargetInfo &STI) { |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 31 | if (!printAliasInstr(MI, O)) |
| 32 | printInstruction(MI, O); |
Owen Anderson | bcc3fad | 2011-09-21 17:58:45 +0000 | [diff] [blame] | 33 | printAnnotation(O, Annot); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo, |
| 37 | raw_ostream &O) { |
| 38 | const MCOperand &Op = MI->getOperand(OpNo); |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 39 | if (Op.isImm()) { |
| 40 | int64_t Imm = Op.getImm() * 2 + 2; |
| 41 | O << "$"; |
| 42 | if (Imm >= 0) |
| 43 | O << '+'; |
| 44 | O << Imm; |
| 45 | } else { |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 46 | assert(Op.isExpr() && "unknown pcrel immediate operand"); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 47 | Op.getExpr()->print(O, &MAI); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 52 | raw_ostream &O, const char *Modifier) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 53 | assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported"); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 54 | const MCOperand &Op = MI->getOperand(OpNo); |
| 55 | if (Op.isReg()) { |
| 56 | O << getRegisterName(Op.getReg()); |
| 57 | } else if (Op.isImm()) { |
| 58 | O << '#' << Op.getImm(); |
| 59 | } else { |
| 60 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 61 | O << '#'; |
| 62 | Op.getExpr()->print(O, &MAI); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
| 66 | void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo, |
| 67 | raw_ostream &O, |
| 68 | const char *Modifier) { |
| 69 | const MCOperand &Base = MI->getOperand(OpNo); |
| 70 | const MCOperand &Disp = MI->getOperand(OpNo+1); |
| 71 | |
| 72 | // Print displacement first |
| 73 | |
| 74 | // If the global address expression is a part of displacement field with a |
| 75 | // register base, we should not emit any prefix symbol here, e.g. |
| 76 | // mov.w &foo, r1 |
| 77 | // vs |
| 78 | // mov.w glb(r1), r2 |
| 79 | // Otherwise (!) msp430-as will silently miscompile the output :( |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 80 | if (Base.getReg() == MSP430::SR) |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 81 | O << '&'; |
| 82 | |
| 83 | if (Disp.isExpr()) |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 84 | Disp.getExpr()->print(O, &MAI); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 85 | else { |
| 86 | assert(Disp.isImm() && "Expected immediate in displacement field"); |
| 87 | O << Disp.getImm(); |
| 88 | } |
| 89 | |
| 90 | // Print register base field |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 91 | if ((Base.getReg() != MSP430::SR) && |
| 92 | (Base.getReg() != MSP430::PC)) |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 93 | O << '(' << getRegisterName(Base.getReg()) << ')'; |
| 94 | } |
| 95 | |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 96 | void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo, |
| 97 | raw_ostream &O) { |
| 98 | const MCOperand &Base = MI->getOperand(OpNo); |
| 99 | O << "@" << getRegisterName(Base.getReg()); |
| 100 | } |
| 101 | |
| 102 | void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo, |
| 103 | raw_ostream &O) { |
| 104 | const MCOperand &Base = MI->getOperand(OpNo); |
| 105 | O << "@" << getRegisterName(Base.getReg()) << "+"; |
| 106 | } |
| 107 | |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 108 | void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo, |
| 109 | raw_ostream &O) { |
| 110 | unsigned CC = MI->getOperand(OpNo).getImm(); |
| 111 | |
| 112 | switch (CC) { |
| 113 | default: |
| 114 | llvm_unreachable("Unsupported CC code"); |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 115 | case MSP430CC::COND_E: |
| 116 | O << "eq"; |
| 117 | break; |
| 118 | case MSP430CC::COND_NE: |
| 119 | O << "ne"; |
| 120 | break; |
| 121 | case MSP430CC::COND_HS: |
| 122 | O << "hs"; |
| 123 | break; |
| 124 | case MSP430CC::COND_LO: |
| 125 | O << "lo"; |
| 126 | break; |
| 127 | case MSP430CC::COND_GE: |
| 128 | O << "ge"; |
| 129 | break; |
| 130 | case MSP430CC::COND_L: |
| 131 | O << 'l'; |
| 132 | break; |
Anton Korobeynikov | 49045c6 | 2018-11-15 12:29:43 +0000 | [diff] [blame] | 133 | case MSP430CC::COND_N: |
| 134 | O << 'n'; |
| 135 | break; |
Nick Lewycky | 306084e | 2010-10-02 01:16:59 +0000 | [diff] [blame] | 136 | } |
| 137 | } |