blob: 2f3c6ed3c17e22d0c675e339dc8a1d42326cc0d4 [file] [log] [blame]
Nick Lewycky306084e2010-10-02 01:16:59 +00001//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Nick Lewycky306084e2010-10-02 01:16:59 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This class prints an MSP430 MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
Nick Lewycky306084e2010-10-02 01:16:59 +000013#include "MSP430InstPrinter.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "MSP430.h"
Nick Lewycky306084e2010-10-02 01:16:59 +000015#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCExpr.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/MC/MCInst.h"
Anton Korobeynikov49045c62018-11-15 12:29:43 +000018#include "llvm/MC/MCInstrInfo.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// Include the auto-generated portion of the assembly writer.
Anton Korobeynikov49045c62018-11-15 12:29:43 +000026#define PRINT_ALIAS_INSTR
Nick Lewycky306084e2010-10-02 01:16:59 +000027#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) {
Anton Korobeynikov49045c62018-11-15 12:29:43 +000031 if (!printAliasInstr(MI, O))
32 printInstruction(MI, O);
Owen Andersonbcc3fad2011-09-21 17:58:45 +000033 printAnnotation(O, Annot);
Nick Lewycky306084e2010-10-02 01:16:59 +000034}
35
36void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
37 raw_ostream &O) {
38 const MCOperand &Op = MI->getOperand(OpNo);
Anton Korobeynikov49045c62018-11-15 12:29:43 +000039 if (Op.isImm()) {
40 int64_t Imm = Op.getImm() * 2 + 2;
41 O << "$";
42 if (Imm >= 0)
43 O << '+';
44 O << Imm;
45 } else {
Nick Lewycky306084e2010-10-02 01:16:59 +000046 assert(Op.isExpr() && "unknown pcrel immediate operand");
Matt Arsenault8b643552015-06-09 00:31:39 +000047 Op.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000048 }
49}
50
51void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
52 raw_ostream &O, const char *Modifier) {
Craig Toppere73658d2014-04-28 04:05:08 +000053 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
Nick Lewycky306084e2010-10-02 01:16:59 +000054 const MCOperand &Op = MI->getOperand(OpNo);
55 if (Op.isReg()) {
56 O << getRegisterName(Op.getReg());
57 } else if (Op.isImm()) {
58 O << '#' << Op.getImm();
59 } else {
60 assert(Op.isExpr() && "unknown operand kind in printOperand");
Matt Arsenault8b643552015-06-09 00:31:39 +000061 O << '#';
62 Op.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000063 }
64}
65
66void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
67 raw_ostream &O,
68 const char *Modifier) {
69 const MCOperand &Base = MI->getOperand(OpNo);
70 const MCOperand &Disp = MI->getOperand(OpNo+1);
71
72 // Print displacement first
73
74 // If the global address expression is a part of displacement field with a
75 // register base, we should not emit any prefix symbol here, e.g.
76 // mov.w &foo, r1
77 // vs
78 // mov.w glb(r1), r2
79 // Otherwise (!) msp430-as will silently miscompile the output :(
Anton Korobeynikov49045c62018-11-15 12:29:43 +000080 if (Base.getReg() == MSP430::SR)
Nick Lewycky306084e2010-10-02 01:16:59 +000081 O << '&';
82
83 if (Disp.isExpr())
Matt Arsenault8b643552015-06-09 00:31:39 +000084 Disp.getExpr()->print(O, &MAI);
Nick Lewycky306084e2010-10-02 01:16:59 +000085 else {
86 assert(Disp.isImm() && "Expected immediate in displacement field");
87 O << Disp.getImm();
88 }
89
90 // Print register base field
Anton Korobeynikov49045c62018-11-15 12:29:43 +000091 if ((Base.getReg() != MSP430::SR) &&
92 (Base.getReg() != MSP430::PC))
Nick Lewycky306084e2010-10-02 01:16:59 +000093 O << '(' << getRegisterName(Base.getReg()) << ')';
94}
95
Anton Korobeynikov49045c62018-11-15 12:29:43 +000096void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
97 raw_ostream &O) {
98 const MCOperand &Base = MI->getOperand(OpNo);
99 O << "@" << getRegisterName(Base.getReg());
100}
101
102void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
103 raw_ostream &O) {
104 const MCOperand &Base = MI->getOperand(OpNo);
105 O << "@" << getRegisterName(Base.getReg()) << "+";
106}
107
Nick Lewycky306084e2010-10-02 01:16:59 +0000108void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
109 raw_ostream &O) {
110 unsigned CC = MI->getOperand(OpNo).getImm();
111
112 switch (CC) {
113 default:
114 llvm_unreachable("Unsupported CC code");
Nick Lewycky306084e2010-10-02 01:16:59 +0000115 case MSP430CC::COND_E:
116 O << "eq";
117 break;
118 case MSP430CC::COND_NE:
119 O << "ne";
120 break;
121 case MSP430CC::COND_HS:
122 O << "hs";
123 break;
124 case MSP430CC::COND_LO:
125 O << "lo";
126 break;
127 case MSP430CC::COND_GE:
128 O << "ge";
129 break;
130 case MSP430CC::COND_L:
131 O << 'l';
132 break;
Anton Korobeynikov49045c62018-11-15 12:29:43 +0000133 case MSP430CC::COND_N:
134 O << 'n';
135 break;
Nick Lewycky306084e2010-10-02 01:16:59 +0000136 }
137}