LLVM 20.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"
21using namespace llvm;
22
23#define DEBUG_TYPE "asm-printer"
24
25// Include the auto-generated portion of the assembly writer.
26#define PRINT_ALIAS_INSTR
27#include "MSP430GenAsmWriter.inc"
28
30 O << getRegisterName(Reg);
31}
32
34 StringRef Annot, const MCSubtargetInfo &STI,
35 raw_ostream &O) {
36 if (!printAliasInstr(MI, Address, O))
38 printAnnotation(O, Annot);
39}
40
41void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
42 raw_ostream &O) {
43 const MCOperand &Op = MI->getOperand(OpNo);
44 if (Op.isImm()) {
45 int64_t Imm = Op.getImm() * 2 + 2;
46 O << "$";
47 if (Imm >= 0)
48 O << '+';
49 O << Imm;
50 } else {
51 assert(Op.isExpr() && "unknown pcrel immediate operand");
52 Op.getExpr()->print(O, &MAI);
53 }
54}
55
56void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
57 raw_ostream &O, const char *Modifier) {
58 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
59 const MCOperand &Op = MI->getOperand(OpNo);
60 if (Op.isReg()) {
61 O << getRegisterName(Op.getReg());
62 } else if (Op.isImm()) {
63 O << '#' << Op.getImm();
64 } else {
65 assert(Op.isExpr() && "unknown operand kind in printOperand");
66 O << '#';
67 Op.getExpr()->print(O, &MAI);
68 }
69}
70
71void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
72 raw_ostream &O,
73 const char *Modifier) {
74 const MCOperand &Base = MI->getOperand(OpNo);
75 const MCOperand &Disp = MI->getOperand(OpNo+1);
76
77 // Print displacement first
78
79 // If the global address expression is a part of displacement field with a
80 // register base, we should not emit any prefix symbol here, e.g.
81 // mov.w &foo, r1
82 // vs
83 // mov.w glb(r1), r2
84 // Otherwise (!) msp430-as will silently miscompile the output :(
85 if (Base.getReg() == MSP430::SR)
86 O << '&';
87
88 if (Disp.isExpr())
89 Disp.getExpr()->print(O, &MAI);
90 else {
91 assert(Disp.isImm() && "Expected immediate in displacement field");
92 O << Disp.getImm();
93 }
94
95 // Print register base field
96 if ((Base.getReg() != MSP430::SR) &&
97 (Base.getReg() != MSP430::PC))
98 O << '(' << getRegisterName(Base.getReg()) << ')';
99}
100
101void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
102 raw_ostream &O) {
103 const MCOperand &Base = MI->getOperand(OpNo);
104 O << "@" << getRegisterName(Base.getReg());
105}
106
107void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
108 raw_ostream &O) {
109 const MCOperand &Base = MI->getOperand(OpNo);
110 O << "@" << getRegisterName(Base.getReg()) << "+";
111}
112
113void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
114 raw_ostream &O) {
115 unsigned CC = MI->getOperand(OpNo).getImm();
116
117 switch (CC) {
118 default:
119 llvm_unreachable("Unsupported CC code");
120 case MSP430CC::COND_E:
121 O << "eq";
122 break;
124 O << "ne";
125 break;
127 O << "hs";
128 break;
130 O << "lo";
131 break;
133 O << "ge";
134 break;
135 case MSP430CC::COND_L:
136 O << 'l';
137 break;
138 case MSP430CC::COND_N:
139 O << 'n';
140 break;
141 }
142}
IRTranslator LLVM IR MI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class represents an Operation in the Expression.
bool print(raw_ostream &OS, DIDumpOptions DumpOpts, const DWARFExpression *Expr, DWARFUnit *U) const
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:51
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
int64_t getImm() const
Definition: MCInst.h:80
bool isImm() const
Definition: MCInst.h:62
const MCExpr * getExpr() const
Definition: MCInst.h:114
bool isExpr() const
Definition: MCInst.h:65
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 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)
void printRegName(raw_ostream &O, MCRegister Reg) const override
Print the assembler register name.
bool printAliasInstr(const MCInst *MI, uint64_t Address, raw_ostream &O)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#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.
Definition: AddressRanges.h:18