1 //===-- AVRInstPrinter.cpp - Convert AVR MCInst to assembly syntax --------===//
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
7 //===----------------------------------------------------------------------===//
9 // This class prints an AVR MCInst to a .s file.
11 //===----------------------------------------------------------------------===//
13 #include "AVRInstPrinter.h"
15 #include "MCTargetDesc/AVRMCTargetDesc.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrDesc.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCRegisterInfo.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
27 #define DEBUG_TYPE "asm-printer"
31 // Include the auto-generated portion of the assembly writer.
32 #define PRINT_ALIAS_INSTR
33 #include "AVRGenAsmWriter.inc"
35 void AVRInstPrinter::printInst(const MCInst
*MI
, raw_ostream
&O
,
36 StringRef Annot
, const MCSubtargetInfo
&STI
) {
37 unsigned Opcode
= MI
->getOpcode();
39 // First handle load and store instructions with postinc or predec
40 // of the form "ld reg, X+".
41 // TODO: We should be able to rewrite this using TableGen data.
47 printOperand(MI
, 0, O
);
50 if (Opcode
== AVR::LDRdPtrPd
)
53 printOperand(MI
, 1, O
);
55 if (Opcode
== AVR::LDRdPtrPi
)
60 printOperand(MI
, 0, O
);
62 printOperand(MI
, 1, O
);
68 if (Opcode
== AVR::STPtrPdRr
)
71 printOperand(MI
, 1, O
);
73 if (Opcode
== AVR::STPtrPiRr
)
77 printOperand(MI
, 2, O
);
80 if (!printAliasInstr(MI
, O
))
81 printInstruction(MI
, O
);
83 printAnnotation(O
, Annot
);
88 const char *AVRInstPrinter::getPrettyRegisterName(unsigned RegNum
,
89 MCRegisterInfo
const &MRI
) {
90 // GCC prints register pairs by just printing the lower register
91 // If the register contains a subregister, print it instead
92 if (MRI
.getNumSubRegIndices() > 0) {
93 unsigned RegLoNum
= MRI
.getSubReg(RegNum
, AVR::sub_lo
);
94 RegNum
= (RegLoNum
!= AVR::NoRegister
) ? RegLoNum
: RegNum
;
97 return getRegisterName(RegNum
);
100 void AVRInstPrinter::printOperand(const MCInst
*MI
, unsigned OpNo
,
102 const MCOperand
&Op
= MI
->getOperand(OpNo
);
103 const MCOperandInfo
&MOI
= this->MII
.get(MI
->getOpcode()).OpInfo
[OpNo
];
106 bool isPtrReg
= (MOI
.RegClass
== AVR::PTRREGSRegClassID
) ||
107 (MOI
.RegClass
== AVR::PTRDISPREGSRegClassID
) ||
108 (MOI
.RegClass
== AVR::ZREGRegClassID
);
111 O
<< getRegisterName(Op
.getReg(), AVR::ptr
);
113 O
<< getPrettyRegisterName(Op
.getReg(), MRI
);
115 } else if (Op
.isImm()) {
118 assert(Op
.isExpr() && "Unknown operand kind in printOperand");
123 /// This is used to print an immediate value that ends up
124 /// being encoded as a pc-relative value.
125 void AVRInstPrinter::printPCRelImm(const MCInst
*MI
, unsigned OpNo
,
127 const MCOperand
&Op
= MI
->getOperand(OpNo
);
130 int64_t Imm
= Op
.getImm();
133 // Print a position sign if needed.
134 // Negative values have their sign printed automatically.
140 assert(Op
.isExpr() && "Unknown pcrel immediate operand");
145 void AVRInstPrinter::printMemri(const MCInst
*MI
, unsigned OpNo
,
147 assert(MI
->getOperand(OpNo
).isReg() && "Expected a register for the first operand");
149 const MCOperand
&OffsetOp
= MI
->getOperand(OpNo
+ 1);
151 // Print the register.
152 printOperand(MI
, OpNo
, O
);
154 // Print the {+,-}offset.
155 if (OffsetOp
.isImm()) {
156 int64_t Offset
= OffsetOp
.getImm();
162 } else if (OffsetOp
.isExpr()) {
163 O
<< *OffsetOp
.getExpr();
165 llvm_unreachable("unknown type for offset");
169 } // end of namespace llvm