Anton Korobeynikov | 425a938 | 2009-10-21 00:10:30 +0000 | [diff] [blame] | 1 | //===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class prints an MSP430 MCInst to a .s file. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "asm-printer" |
| 15 | #include "MSP430InstPrinter.h" |
| 16 | #include "llvm/MC/MCInst.h" |
| 17 | #include "llvm/MC/MCAsmInfo.h" |
| 18 | #include "llvm/MC/MCExpr.h" |
| 19 | #include "llvm/Support/ErrorHandling.h" |
| 20 | #include "llvm/Support/FormattedStream.h" |
| 21 | #include "MSP430GenInstrNames.inc" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | |
| 25 | // Include the auto-generated portion of the assembly writer. |
| 26 | #define MachineInstr MCInst |
| 27 | #define MSP430AsmPrinter MSP430InstPrinter // FIXME: REMOVE. |
| 28 | #define NO_ASM_WRITER_BOILERPLATE |
| 29 | #include "MSP430GenAsmWriter.inc" |
| 30 | #undef MachineInstr |
| 31 | #undef MSP430AsmPrinter |
| 32 | |
| 33 | void MSP430InstPrinter::printInst(const MCInst *MI) { |
| 34 | printInstruction(MI); |
| 35 | } |
Anton Korobeynikov | 0f66de4 | 2009-10-21 00:11:27 +0000 | [diff] [blame] | 36 | |
| 37 | void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo, |
| 38 | const char *Modifier) { |
Anton Korobeynikov | 0f66de4 | 2009-10-21 00:11:27 +0000 | [diff] [blame] | 39 | const MCOperand &Op = MI->getOperand(OpNo); |
| 40 | if (Op.isReg()) { |
| 41 | O << getRegisterName(Op.getReg()); |
| 42 | } else if (Op.isImm()) { |
| 43 | O << '#' << Op.getImm(); |
| 44 | } else { |
| 45 | assert(Op.isExpr() && "unknown operand kind in printOperand"); |
Anton Korobeynikov | eb85a3f | 2009-10-21 00:12:27 +0000 | [diff] [blame] | 46 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 47 | O << (isMemOp ? '&' : '#'); |
Anton Korobeynikov | baffc35 | 2009-10-21 00:12:08 +0000 | [diff] [blame] | 48 | Op.getExpr()->print(O, &MAI); |
Anton Korobeynikov | 0f66de4 | 2009-10-21 00:11:27 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
Anton Korobeynikov | baffc35 | 2009-10-21 00:12:08 +0000 | [diff] [blame] | 51 | |
| 52 | void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo, |
| 53 | const char *Modifier) { |
| 54 | const MCOperand &Base = MI->getOperand(OpNo); |
| 55 | const MCOperand &Disp = MI->getOperand(OpNo+1); |
| 56 | |
Anton Korobeynikov | eb85a3f | 2009-10-21 00:12:27 +0000 | [diff] [blame] | 57 | // FIXME: move global to displacement field! |
| 58 | if (Base.isExpr()) |
| 59 | printOperand(MI, OpNo, "mem"); |
| 60 | else if (Disp.isImm() && !Base.isReg()) |
Anton Korobeynikov | baffc35 | 2009-10-21 00:12:08 +0000 | [diff] [blame] | 61 | printOperand(MI, OpNo); |
| 62 | else if (Base.isReg()) { |
| 63 | if (Disp.getImm()) { |
| 64 | O << Disp.getImm() << '('; |
| 65 | printOperand(MI, OpNo); |
| 66 | O << ')'; |
| 67 | } else { |
| 68 | O << '@'; |
| 69 | printOperand(MI, OpNo); |
| 70 | } |
| 71 | } else { |
| 72 | Base.dump(); |
| 73 | Disp.dump(); |
| 74 | llvm_unreachable("Unsupported memory operand"); |
| 75 | } |
| 76 | |
| 77 | } |