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"
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
29 O << getRegisterName(Reg);
30}
31
33 StringRef Annot, const MCSubtargetInfo &STI,
34 raw_ostream &O) {
35 if (!printAliasInstr(MI, Address, O))
37 printAnnotation(O, Annot);
38}
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 Op.getExpr()->print(O, &MAI);
52 }
53}
54
55void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
56 raw_ostream &O, const char *Modifier) {
57 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
58 const MCOperand &Op = MI->getOperand(OpNo);
59 if (Op.isReg()) {
60 O << getRegisterName(Op.getReg());
61 } else if (Op.isImm()) {
62 O << '#' << Op.getImm();
63 } else {
64 assert(Op.isExpr() && "unknown operand kind in printOperand");
65 O << '#';
66 Op.getExpr()->print(O, &MAI);
67 }
68}
69
70void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
71 raw_ostream &O,
72 const char *Modifier) {
73 const MCOperand &Base = MI->getOperand(OpNo);
74 const MCOperand &Disp = MI->getOperand(OpNo+1);
75
76 // Print displacement first
77
78 // If the global address expression is a part of displacement field with a
79 // register base, we should not emit any prefix symbol here, e.g.
80 // mov.w &foo, r1
81 // vs
82 // mov.w glb(r1), r2
83 // Otherwise (!) msp430-as will silently miscompile the output :(
84 if (Base.getReg() == MSP430::SR)
85 O << '&';
86
87 if (Disp.isExpr())
88 Disp.getExpr()->print(O, &MAI);
89 else {
90 assert(Disp.isImm() && "Expected immediate in displacement field");
91 O << Disp.getImm();
92 }
93
94 // Print register base field
95 if ((Base.getReg() != MSP430::SR) &&
96 (Base.getReg() != MSP430::PC))
97 O << '(' << getRegisterName(Base.getReg()) << ')';
98}
99
100void MSP430InstPrinter::printIndRegOperand(const MCInst *MI, unsigned OpNo,
101 raw_ostream &O) {
102 const MCOperand &Base = MI->getOperand(OpNo);
103 O << "@" << getRegisterName(Base.getReg());
104}
105
106void MSP430InstPrinter::printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
107 raw_ostream &O) {
108 const MCOperand &Base = MI->getOperand(OpNo);
109 O << "@" << getRegisterName(Base.getReg()) << "+";
110}
111
112void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
113 raw_ostream &O) {
114 unsigned CC = MI->getOperand(OpNo).getImm();
115
116 switch (CC) {
117 default:
118 llvm_unreachable("Unsupported CC code");
119 case MSP430CC::COND_E:
120 O << "eq";
121 break;
123 O << "ne";
124 break;
126 O << "hs";
127 break;
129 O << "lo";
130 break;
132 O << "ge";
133 break;
134 case MSP430CC::COND_L:
135 O << 'l';
136 break;
137 case MSP430CC::COND_N:
138 O << 'n';
139 break;
140 }
141}
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:52
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:185
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:37
int64_t getImm() const
Definition: MCInst.h:81
bool isImm() const
Definition: MCInst.h:63
const MCExpr * getExpr() const
Definition: MCInst.h:115
bool isExpr() const
Definition: MCInst.h:66
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:51
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