blob: 59b448aa8727c7b037f7dba8436e359a89f2a428 [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
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000038void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo) {
39 const MCOperand &Op = MI->getOperand(OpNo);
40 if (Op.isImm())
41 O << Op.getImm();
42 else {
43 assert(Op.isExpr() && "unknown pcrel immediate operand");
44 Op.getExpr()->print(O, &MAI);
45 }
46}
47
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000048void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
49 const char *Modifier) {
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000050 const MCOperand &Op = MI->getOperand(OpNo);
51 if (Op.isReg()) {
52 O << getRegisterName(Op.getReg());
53 } else if (Op.isImm()) {
54 O << '#' << Op.getImm();
55 } else {
56 assert(Op.isExpr() && "unknown operand kind in printOperand");
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000057 O << '#';
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000058 Op.getExpr()->print(O, &MAI);
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000059 }
60}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000061
62void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
63 const char *Modifier) {
64 const MCOperand &Base = MI->getOperand(OpNo);
65 const MCOperand &Disp = MI->getOperand(OpNo+1);
66
Anton Korobeynikoveb85a3f2009-10-21 00:12:27 +000067 // FIXME: move global to displacement field!
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000068 if (Base.isExpr()) {
69 O << '&';
70 Base.getExpr()->print(O, &MAI);
71 } else if (Disp.isImm() && !Base.isReg())
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000072 printOperand(MI, OpNo);
73 else if (Base.isReg()) {
74 if (Disp.getImm()) {
75 O << Disp.getImm() << '(';
76 printOperand(MI, OpNo);
77 O << ')';
78 } else {
79 O << '@';
80 printOperand(MI, OpNo);
81 }
82 } else {
83 Base.dump();
84 Disp.dump();
85 llvm_unreachable("Unsupported memory operand");
86 }
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000087}
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000088
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000089void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
90 unsigned CC = MI->getOperand(OpNo).getImm();
91
92 switch (CC) {
93 default:
94 llvm_unreachable("Unsupported CC code");
95 break;
96 case MSP430::COND_E:
97 O << "eq";
98 break;
99 case MSP430::COND_NE:
100 O << "ne";
101 break;
102 case MSP430::COND_HS:
103 O << "hs";
104 break;
105 case MSP430::COND_LO:
106 O << "lo";
107 break;
108 case MSP430::COND_GE:
109 O << "ge";
110 break;
111 case MSP430::COND_L:
112 O << 'l';
113 break;
114 }
115}