blob: a3ecc674dc404a338a8658359756843fc5f47c7a [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
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 Korobeynikovdd5e1eb2009-10-21 00:13:58 +000050 assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000051 const MCOperand &Op = MI->getOperand(OpNo);
52 if (Op.isReg()) {
53 O << getRegisterName(Op.getReg());
54 } else if (Op.isImm()) {
55 O << '#' << Op.getImm();
56 } else {
57 assert(Op.isExpr() && "unknown operand kind in printOperand");
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000058 O << '#';
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000059 Op.getExpr()->print(O, &MAI);
Anton Korobeynikov0f66de42009-10-21 00:11:27 +000060 }
61}
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000062
63void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
64 const char *Modifier) {
65 const MCOperand &Base = MI->getOperand(OpNo);
66 const MCOperand &Disp = MI->getOperand(OpNo+1);
67
Anton Korobeynikoveb85a3f2009-10-21 00:12:27 +000068 // FIXME: move global to displacement field!
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000069 if (Base.isExpr()) {
70 O << '&';
71 Base.getExpr()->print(O, &MAI);
72 } else if (Disp.isImm() && !Base.isReg())
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000073 printOperand(MI, OpNo);
74 else if (Base.isReg()) {
75 if (Disp.getImm()) {
76 O << Disp.getImm() << '(';
77 printOperand(MI, OpNo);
78 O << ')';
79 } else {
80 O << '@';
81 printOperand(MI, OpNo);
82 }
83 } else {
84 Base.dump();
85 Disp.dump();
86 llvm_unreachable("Unsupported memory operand");
87 }
Anton Korobeynikovbaffc352009-10-21 00:12:08 +000088}
Anton Korobeynikovc9a90ae2009-10-21 00:13:25 +000089
Anton Korobeynikov7199ce52009-10-21 00:13:05 +000090void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
91 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}