blob: 6ea930bc394ce5f9c7b7dc828383835501a1d55f [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#define NO_ASM_WRITER_BOILERPLATE
29#include "MSP430GenAsmWriter.inc"
30#undef MachineInstr
Anton Korobeynikov425a9382009-10-21 00:10:30 +000031
32void MSP430InstPrinter::printInst(const MCInst *MI) {
33 printInstruction(MI);
34}
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000035
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000036void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo) {
37 const MCOperand &Op = MI->getOperand(OpNo);
38 if (Op.isImm())
39 O << Op.getImm();
40 else {
41 assert(Op.isExpr() && "unknown pcrel immediate operand");
42 Op.getExpr()->print(O, &MAI);
43 }
44}
45
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000046void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
47 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");
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000056 O << '#';
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000057 Op.getExpr()->print(O, &MAI);
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000058 }
59}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000060
61void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
62 const char *Modifier) {
63 const MCOperand &Base = MI->getOperand(OpNo);
64 const MCOperand &Disp = MI->getOperand(OpNo+1);
65
Anton Korobeynikoveb85a3f2009-10-21 00:12:27 +000066 // FIXME: move global to displacement field!
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000067 if (Base.isExpr()) {
68 O << '&';
69 Base.getExpr()->print(O, &MAI);
70 } else if (Disp.isImm() && !Base.isReg())
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000071 printOperand(MI, OpNo);
72 else if (Base.isReg()) {
73 if (Disp.getImm()) {
74 O << Disp.getImm() << '(';
75 printOperand(MI, OpNo);
76 O << ')';
77 } else {
78 O << '@';
79 printOperand(MI, OpNo);
80 }
81 } else {
82 Base.dump();
83 Disp.dump();
84 llvm_unreachable("Unsupported memory operand");
85 }
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000086}
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000087
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000088void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
89 unsigned CC = MI->getOperand(OpNo).getImm();
90
91 switch (CC) {
92 default:
93 llvm_unreachable("Unsupported CC code");
94 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000095 case MSP430CC::COND_E:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000096 O << "eq";
97 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +000098 case MSP430CC::COND_NE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000099 O << "ne";
100 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000101 case MSP430CC::COND_HS:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000102 O << "hs";
103 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000104 case MSP430CC::COND_LO:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000105 O << "lo";
106 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000107 case MSP430CC::COND_GE:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000108 O << "ge";
109 break;
Anton Korobeynikova791a042009-10-21 19:16:49 +0000110 case MSP430CC::COND_L:
Anton Korobeynikov7199ce52009-10-21 00:13:05 +0000111 O << 'l';
112 break;
113 }
114}