blob: be6d1a84a377518277a69dc97e58350480b95b28 [file] [log] [blame]
Nick Lewycky306084e2010-10-02 01:16:59 +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
Nick Lewycky306084e2010-10-02 01:16:59 +000014#include "MSP430InstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MSP430.h"
Nick Lewycky306084e2010-10-02 01:16:59 +000016#include "llvm/MC/MCAsmInfo.h"
17#include "llvm/MC/MCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/MC/MCInst.h"
Nick Lewycky306084e2010-10-02 01:16:59 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/FormattedStream.h"
21using namespace llvm;
22
Chandler Carruth84e68b22014-04-22 02:41:26 +000023#define DEBUG_TYPE "asm-printer"
24
Nick Lewycky306084e2010-10-02 01:16:59 +000025
26// Include the auto-generated portion of the assembly writer.
27#include "MSP430GenAsmWriter.inc"
28
Owen Andersona0c3b972011-09-15 23:38:46 +000029void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O,
Akira Hatanakab46d0232015-03-27 20:36:02 +000030 StringRef Annot, const MCSubtargetInfo &STI) {
Nick Lewycky306084e2010-10-02 01:16:59 +000031 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000032 printAnnotation(O, Annot);
Nick Lewycky306084e2010-10-02 01:16:59 +000033}
34
35void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
36 raw_ostream &O) {
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");
Matt Arsenault8b643552015-06-09 00:31:39 +000042 Op.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000043 }
44}
45
46void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
47 raw_ostream &O, const char *Modifier) {
Craig Toppere73658d2014-04-28 04:05:08 +000048 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
Nick Lewycky306084e2010-10-02 01:16:59 +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");
Matt Arsenault8b643552015-06-09 00:31:39 +000056 O << '#';
57 Op.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000058 }
59}
60
61void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
62 raw_ostream &O,
63 const char *Modifier) {
64 const MCOperand &Base = MI->getOperand(OpNo);
65 const MCOperand &Disp = MI->getOperand(OpNo+1);
66
67 // Print displacement first
68
69 // If the global address expression is a part of displacement field with a
70 // register base, we should not emit any prefix symbol here, e.g.
71 // mov.w &foo, r1
72 // vs
73 // mov.w glb(r1), r2
74 // Otherwise (!) msp430-as will silently miscompile the output :(
75 if (!Base.getReg())
76 O << '&';
77
78 if (Disp.isExpr())
Matt Arsenault8b643552015-06-09 00:31:39 +000079 Disp.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000080 else {
81 assert(Disp.isImm() && "Expected immediate in displacement field");
82 O << Disp.getImm();
83 }
84
85 // Print register base field
86 if (Base.getReg())
87 O << '(' << getRegisterName(Base.getReg()) << ')';
88}
89
90void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
91 raw_ostream &O) {
92 unsigned CC = MI->getOperand(OpNo).getImm();
93
94 switch (CC) {
95 default:
96 llvm_unreachable("Unsupported CC code");
Nick Lewycky306084e2010-10-02 01:16:59 +000097 case MSP430CC::COND_E:
98 O << "eq";
99 break;
100 case MSP430CC::COND_NE:
101 O << "ne";
102 break;
103 case MSP430CC::COND_HS:
104 O << "hs";
105 break;
106 case MSP430CC::COND_LO:
107 O << "lo";
108 break;
109 case MSP430CC::COND_GE:
110 O << "ge";
111 break;
112 case MSP430CC::COND_L:
113 O << 'l';
114 break;
115 }
116}