blob: 8fcbab58c96a5af5264bba1fe1fa718c298ea321 [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 Korobeynikov7199ce52009-10-21 00:13:05 +000015#include "MSP430InstrInfo.h"
Anton Korobeynikov425a9382009-10-21 00:10:30 +000016#include "MSP430InstPrinter.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCAsmInfo.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FormattedStream.h"
22#include "MSP430GenInstrNames.inc"
23using namespace llvm;
24
25
26// Include the auto-generated portion of the assembly writer.
27#define MachineInstr MCInst
28#define MSP430AsmPrinter MSP430InstPrinter // FIXME: REMOVE.
29#define NO_ASM_WRITER_BOILERPLATE
30#include "MSP430GenAsmWriter.inc"
31#undef MachineInstr
32#undef MSP430AsmPrinter
33
34void MSP430InstPrinter::printInst(const MCInst *MI) {
35 printInstruction(MI);
36}
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000037
38void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
39 const char *Modifier) {
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000040 const MCOperand &Op = MI->getOperand(OpNo);
41 if (Op.isReg()) {
42 O << getRegisterName(Op.getReg());
43 } else if (Op.isImm()) {
44 O << '#' << Op.getImm();
45 } else {
46 assert(Op.isExpr() && "unknown operand kind in printOperand");
Anton Korobeynikoveb85a3f2009-10-21 00:12:27 +000047 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
48 O << (isMemOp ? '&' : '#');
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000049 Op.getExpr()->print(O, &MAI);
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000050 }
51}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000052
53void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
54 const char *Modifier) {
55 const MCOperand &Base = MI->getOperand(OpNo);
56 const MCOperand &Disp = MI->getOperand(OpNo+1);
57
Anton Korobeynikoveb85a3f2009-10-21 00:12:27 +000058 // FIXME: move global to displacement field!
59 if (Base.isExpr())
60 printOperand(MI, OpNo, "mem");
61 else if (Disp.isImm() && !Base.isReg())
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000062 printOperand(MI, OpNo);
63 else if (Base.isReg()) {
64 if (Disp.getImm()) {
65 O << Disp.getImm() << '(';
66 printOperand(MI, OpNo);
67 O << ')';
68 } else {
69 O << '@';
70 printOperand(MI, OpNo);
71 }
72 } else {
73 Base.dump();
74 Disp.dump();
75 llvm_unreachable("Unsupported memory operand");
76 }
77
78}
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000079void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
80 unsigned CC = MI->getOperand(OpNo).getImm();
81
82 switch (CC) {
83 default:
84 llvm_unreachable("Unsupported CC code");
85 break;
86 case MSP430::COND_E:
87 O << "eq";
88 break;
89 case MSP430::COND_NE:
90 O << "ne";
91 break;
92 case MSP430::COND_HS:
93 O << "hs";
94 break;
95 case MSP430::COND_LO:
96 O << "lo";
97 break;
98 case MSP430::COND_GE:
99 O << "ge";
100 break;
101 case MSP430::COND_L:
102 O << 'l';
103 break;
104 }
105}