blob: c15d4085bc8b78be07a56656674de971e5d85577 [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
Chris Lattner99fbae72010-04-04 05:04:31 +000031void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
Chris Lattner4e972532010-04-04 04:47:45 +000032 printInstruction(MI, O);
Anton Korobeynikov425a9382009-10-21 00:10:30 +000033}
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000034
Chris Lattner4e972532010-04-04 04:47:45 +000035void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
36 raw_ostream &O) {
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000037 const MCOperand &Op = MI->getOperand(OpNo);
38 if (Op.isImm())
39 O << Op.getImm();
40 else {
41 assert(Op.isExpr() && "unknown pcrel immediate operand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000042 O << *Op.getExpr();
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000043 }
44}
45
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000046void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +000047 raw_ostream &O, const char *Modifier) {
Anton Korobeynikovdd5e1eb2009-10-21 00:13:58 +000048 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000049 const MCOperand &Op = MI->getOperand(OpNo);
50 if (Op.isReg()) {
51 O << getRegisterName(Op.getReg());
52 } else if (Op.isImm()) {
53 O << '#' << Op.getImm();
54 } else {
55 assert(Op.isExpr() && "unknown operand kind in printOperand");
Chris Lattner1e9a11b2010-01-18 00:37:40 +000056 O << '#' << *Op.getExpr();
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000057 }
58}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000059
60void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
Chris Lattner4e972532010-04-04 04:47:45 +000061 raw_ostream &O,
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000062 const char *Modifier) {
63 const MCOperand &Base = MI->getOperand(OpNo);
64 const MCOperand &Disp = MI->getOperand(OpNo+1);
65
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000066 // Print displacement first
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000067
Anton Korobeynikov9a2797f2010-03-06 11:41:12 +000068 // If the global address expression is a part of displacement field with a
69 // register base, we should not emit any prefix symbol here, e.g.
70 // mov.w &foo, r1
71 // vs
72 // mov.w glb(r1), r2
73 // Otherwise (!) msp430-as will silently miscompile the output :(
74 if (!Base.getReg())
75 O << '&';
76
77 if (Disp.isExpr())
78 O << *Disp.getExpr();
79 else {
80 assert(Disp.isImm() && "Expected immediate in displacement field");
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000081 O << Disp.getImm();
82 }
83
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000084 // Print register base field
Anton Korobeynikov9a2797f2010-03-06 11:41:12 +000085 if (Base.getReg())
Anton Korobeynikov1c7e1662009-11-07 17:13:35 +000086 O << '(' << getRegisterName(Base.getReg()) << ')';
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000087}
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000088
Chris Lattner4e972532010-04-04 04:47:45 +000089void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
90 raw_ostream &O) {
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000091 unsigned CC = MI->getOperand(OpNo).getImm();
92
93 switch (CC) {
94 default:
95 llvm_unreachable("Unsupported CC code");
96 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000097 case MSP430CC::COND_E:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000098 O << "eq";
99 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000100 case MSP430CC::COND_NE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000101 O << "ne";
102 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000103 case MSP430CC::COND_HS:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000104 O << "hs";
105 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000106 case MSP430CC::COND_LO:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000107 O << "lo";
108 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000109 case MSP430CC::COND_GE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000110 O << "ge";
111 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000112 case MSP430CC::COND_L:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000113 O << 'l';
114 break;
115 }
116}