blob: f6565bdec6ed89faa9f5fd94dcf2aaf8e68f93a2 [file] [log] [blame]
Anton Korobeynikov425a9382009-10-21 00:10:30 +00001//===-- 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"
Anton Korobeynikova791a042009-10-21 19:16:49 +000015#include "MSP430.h"
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000016#include "MSP430InstrInfo.h"
Anton Korobeynikov425a9382009-10-21 00:10:30 +000017#include "MSP430InstPrinter.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov425a9382009-10-21 00:10:30 +000023using namespace llvm;
24
25
26// Include the auto-generated portion of the assembly writer.
27#define MachineInstr MCInst
Anton Korobeynikov425a9382009-10-21 00:10:30 +000028#include "MSP430GenAsmWriter.inc"
29#undef MachineInstr
Anton Korobeynikov425a9382009-10-21 00:10:30 +000030
31void MSP430InstPrinter::printInst(const MCInst *MI) {
32 printInstruction(MI);
33}
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000034
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000035void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo) {
36 const MCOperand &Op = MI->getOperand(OpNo);
37 if (Op.isImm())
38 O << Op.getImm();
39 else {
40 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000041 O << *Op.getExpr();
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000042 }
43}
44
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000045void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
46 const char *Modifier) {
Anton Korobeynikovdd5e1eb2009-10-21 00:13:58 +000047 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000048 const MCOperand &Op = MI->getOperand(OpNo);
49 if (Op.isReg()) {
50 O << getRegisterName(Op.getReg());
51 } else if (Op.isImm()) {
52 O << '#' << Op.getImm();
53 } else {
54 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000055 O << '#' << *Op.getExpr();
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000056 }
57}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000058
59void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
60 const char *Modifier) {
61 const MCOperand &Base = MI->getOperand(OpNo);
62 const MCOperand &Disp = MI->getOperand(OpNo+1);
63
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000064 // Print displacement first
65 if (Disp.isExpr()) {
Chris Lattner1e9a11b2010-01-18 00:37:40 +000066 O << '&' << *Disp.getExpr();
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000067 } else {
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000068 assert(Disp.isImm() && "Expected immediate in displacement field");
69 if (!Base.getReg())
70 O << '&';
71
72 O << Disp.getImm();
73 }
74
75
76 // Print register base field
77 if (Base.getReg()) {
78 O << '(' << getRegisterName(Base.getReg()) << ')';
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000079 }
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000080}
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000081
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000082void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
83 unsigned CC = MI->getOperand(OpNo).getImm();
84
85 switch (CC) {
86 default:
87 llvm_unreachable("Unsupported CC code");
88 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000089 case MSP430CC::COND_E:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000090 O << "eq";
91 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000092 case MSP430CC::COND_NE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000093 O << "ne";
94 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000095 case MSP430CC::COND_HS:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000096 O << "hs";
97 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000098 case MSP430CC::COND_LO:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000099 O << "lo";
100 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000101 case MSP430CC::COND_GE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000102 O << "ge";
103 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000104 case MSP430CC::COND_L:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000105 O << 'l';
106 break;
107 }
108}