LLVM 22.0.0git
MSP430InstPrinter.cpp
Go to the documentation of this file.
1//===-- MSP430InstPrinter.cpp - Convert MSP430 MCInst to assembly syntax --===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This class prints an MSP430 MCInst to a .s file.
10//
11//===----------------------------------------------------------------------===//
12
13#include "MSP430InstPrinter.h"
14#include "MSP430.h"
15#include "llvm/MC/MCAsmInfo.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
18#include "llvm/MC/MCInstrInfo.h"
20using namespace llvm;
21
22#define DEBUG_TYPE "asm-printer"
23
24// Include the auto-generated portion of the assembly writer.
25#define PRINT_ALIAS_INSTR
26#include "MSP430GenAsmWriter.inc"
27
31
39
40void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
41 raw_ostream &O) {
42 const MCOperand &Op = MI->getOperand(OpNo);
43 if (Op.isImm()) {
44 int64_t Imm = Op.getImm() * 2 + 2;
45 O << "$";
46 if (Imm >= 0)
47 O << '+';
48 O << Imm;
49 } else {
50 assert(Op.isExpr() && "unknown pcrel immediate operand");
51 MAI.printExpr(O, *Op.getExpr());
52 }
53}
54
55void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
56 raw_ostream &O) {
57 const MCOperand &Op = MI->getOperand(OpNo);
58 if (Op.isReg()) {
59 O << getRegisterName(Op.getReg());
60 } else if (Op.isImm()) {
61 O << '#' << Op.getImm();
62 } else {
63 assert(Op.isExpr() && "unknown operand kind in printOperand");
64 O << '#';
65 MAI.printExpr(O, *Op.getExpr());
66 }
67}
68
69void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
70 raw_ostream &O) {
71 const MCOperand &Base = MI->getOperand(OpNo);
72 const MCOperand &Disp = MI->getOperand(OpNo+1);
73
74 // Print displacement first
75
76 // If the global address expression is a part of displacement field with a
77 // register base, we should not emit any prefix symbol here, e.g.
78 // mov.w &foo, r1
79 // vs
80 // mov.w glb(r1), r2
81 // Otherwise (!) msp430-as will silently miscompile the output :(
82 if (Base.getReg() == MSP430::SR)
83 O << '&';
84
85 if (Disp.isExpr())
86 MAI.printExpr(O, *Disp.getExpr());
87 else {
88 assert(Disp.isImm() && "Expected immediate in displacement field");
89 O << Disp.getImm();
90 }
91
92 // Print register base field
93 if ((Base.getReg() != MSP430::SR) &&
94 (Base.getReg() != MSP430::PC))
95 O << '(' << getRegisterName(Base.getReg()) << ')';
96}
97
98void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
99 raw_ostream &O) {
100 const MCOperand &Base = MI->getOperand(OpNo);
101 O << "@" << getRegisterName(Base.getReg());
102}
103
104void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
105 raw_ostream &O) {
106 const MCOperand &Base = MI->getOperand(OpNo);
107 O << "@" << getRegisterName(Base.getReg()) << "+";
108}
109
110void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
111 raw_ostream &O) {
112 unsigned CC = MI->getOperand(OpNo).getImm();
113
114 switch (CC) {
115 default:
116 llvm_unreachable("Unsupported CC code");
117 case MSP430CC::COND_E:
118 O << "eq";
119 break;
121 O << "ne";
122 break;
124 O << "hs";
125 break;
127 O << "lo";
128 break;
130 O << "ge";
131 break;
132 case MSP430CC::COND_L:
133 O << 'l';
134 break;
135 case MSP430CC::COND_N:
136 O << 'n';
137 break;
138 }
139}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
IRTranslator LLVM IR MI
void printExpr(raw_ostream &, const MCExpr &) const
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:188
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:40
int64_t getImm() const
Definition MCInst.h:84
bool isImm() const
Definition MCInst.h:66
const MCExpr * getExpr() const
Definition MCInst.h:118
bool isExpr() const
Definition MCInst.h:69
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
Generic base class for all target subtargets.
static const char * getRegisterName(MCRegister Reg)
void printRegName(raw_ostream &O, MCRegister Reg) override
Print the assembler register name.
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &O) override
Print the specified MCInst to the specified raw_ostream.
void printInstruction(const MCInst *MI, uint64_t Address, raw_ostream &O)
bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &O)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ COND_LO
Definition MSP430.h:26
@ COND_N
Definition MSP430.h:29
@ COND_L
Definition MSP430.h:28
@ COND_E
Definition MSP430.h:23
@ COND_GE
Definition MSP430.h:27
@ COND_NE
Definition MSP430.h:24
@ COND_HS
Definition MSP430.h:25
This is an optimization pass for GlobalISel generic memory operations.
DWARFExpression::Operation Op