1 //===-- AVRAsmPrinter.cpp - AVR LLVM assembly writer ----------------------===// 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 file contains a printer that converts from our internal representation 10 // of machine-dependent LLVM code to GAS-format AVR assembly language. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AVR.h" 15 #include "AVRMCInstLower.h" 16 #include "AVRSubtarget.h" 17 #include "MCTargetDesc/AVRInstPrinter.h" 18 #include "MCTargetDesc/AVRMCExpr.h" 19 #include "TargetInfo/AVRTargetInfo.h" 20 21 #include "llvm/CodeGen/AsmPrinter.h" 22 #include "llvm/CodeGen/MachineFunction.h" 23 #include "llvm/CodeGen/MachineInstr.h" 24 #include "llvm/CodeGen/TargetRegisterInfo.h" 25 #include "llvm/CodeGen/TargetSubtargetInfo.h" 26 #include "llvm/IR/Mangler.h" 27 #include "llvm/MC/MCInst.h" 28 #include "llvm/MC/MCStreamer.h" 29 #include "llvm/MC/MCSymbol.h" 30 #include "llvm/Support/ErrorHandling.h" 31 #include "llvm/Support/TargetRegistry.h" 32 #include "llvm/Support/raw_ostream.h" 33 34 #define DEBUG_TYPE "avr-asm-printer" 35 36 namespace llvm { 37 38 /// An AVR assembly code printer. 39 class AVRAsmPrinter : public AsmPrinter { 40 public: 41 AVRAsmPrinter(TargetMachine &TM, 42 std::unique_ptr<MCStreamer> Streamer) 43 : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) { } 44 45 StringRef getPassName() const override { return "AVR Assembly Printer"; } 46 47 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O); 48 49 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 50 const char *ExtraCode, raw_ostream &O) override; 51 52 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, 53 const char *ExtraCode, raw_ostream &O) override; 54 55 void emitInstruction(const MachineInstr *MI) override; 56 57 const MCExpr *lowerConstant(const Constant *CV) override; 58 59 private: 60 const MCRegisterInfo &MRI; 61 }; 62 63 void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 64 raw_ostream &O) { 65 const MachineOperand &MO = MI->getOperand(OpNo); 66 67 switch (MO.getType()) { 68 case MachineOperand::MO_Register: 69 O << AVRInstPrinter::getPrettyRegisterName(MO.getReg(), MRI); 70 break; 71 case MachineOperand::MO_Immediate: 72 O << MO.getImm(); 73 break; 74 case MachineOperand::MO_GlobalAddress: 75 O << getSymbol(MO.getGlobal()); 76 break; 77 case MachineOperand::MO_ExternalSymbol: 78 O << *GetExternalSymbolSymbol(MO.getSymbolName()); 79 break; 80 case MachineOperand::MO_MachineBasicBlock: 81 O << *MO.getMBB()->getSymbol(); 82 break; 83 default: 84 llvm_unreachable("Not implemented yet!"); 85 } 86 } 87 88 bool AVRAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 89 const char *ExtraCode, raw_ostream &O) { 90 // Default asm printer can only deal with some extra codes, 91 // so try it first. 92 bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O); 93 94 if (Error && ExtraCode && ExtraCode[0]) { 95 if (ExtraCode[1] != 0) 96 return true; // Unknown modifier. 97 98 if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') { 99 const MachineOperand &RegOp = MI->getOperand(OpNum); 100 101 assert(RegOp.isReg() && "Operand must be a register when you're" 102 "using 'A'..'Z' operand extracodes."); 103 Register Reg = RegOp.getReg(); 104 105 unsigned ByteNumber = ExtraCode[0] - 'A'; 106 107 unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 108 unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 109 (void)NumOpRegs; 110 111 const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>(); 112 const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); 113 114 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(Reg); 115 unsigned BytesPerReg = TRI.getRegSizeInBits(*RC) / 8; 116 assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported."); 117 118 unsigned RegIdx = ByteNumber / BytesPerReg; 119 assert(RegIdx < NumOpRegs && "Multibyte index out of range."); 120 121 Reg = MI->getOperand(OpNum + RegIdx).getReg(); 122 123 if (BytesPerReg == 2) { 124 Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi 125 : AVR::sub_lo); 126 } 127 128 O << AVRInstPrinter::getPrettyRegisterName(Reg, MRI); 129 return false; 130 } 131 } 132 133 if (Error) 134 printOperand(MI, OpNum, O); 135 136 return false; 137 } 138 139 bool AVRAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 140 unsigned OpNum, const char *ExtraCode, 141 raw_ostream &O) { 142 if (ExtraCode && ExtraCode[0]) { 143 llvm_unreachable("This branch is not implemented yet"); 144 } 145 146 const MachineOperand &MO = MI->getOperand(OpNum); 147 (void)MO; 148 assert(MO.isReg() && "Unexpected inline asm memory operand"); 149 150 // TODO: We should be able to look up the alternative name for 151 // the register if it's given. 152 // TableGen doesn't expose a way of getting retrieving names 153 // for registers. 154 if (MI->getOperand(OpNum).getReg() == AVR::R31R30) { 155 O << "Z"; 156 } else { 157 assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 && 158 "Wrong register class for memory operand."); 159 O << "Y"; 160 } 161 162 // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion 163 // and the second operand is an Imm. 164 unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 165 unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 166 167 if (NumOpRegs == 2) { 168 O << '+' << MI->getOperand(OpNum + 1).getImm(); 169 } 170 171 return false; 172 } 173 174 void AVRAsmPrinter::emitInstruction(const MachineInstr *MI) { 175 AVRMCInstLower MCInstLowering(OutContext, *this); 176 177 MCInst I; 178 MCInstLowering.lowerInstruction(*MI, I); 179 EmitToStreamer(*OutStreamer, I); 180 } 181 182 const MCExpr *AVRAsmPrinter::lowerConstant(const Constant *CV) { 183 MCContext &Ctx = OutContext; 184 185 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 186 bool IsProgMem = GV->getAddressSpace() == AVR::ProgramMemory; 187 if (IsProgMem) { 188 const MCExpr *Expr = MCSymbolRefExpr::create(getSymbol(GV), Ctx); 189 return AVRMCExpr::create(AVRMCExpr::VK_AVR_PM, Expr, false, Ctx); 190 } 191 } 192 193 return AsmPrinter::lowerConstant(CV); 194 } 195 196 } // end of namespace llvm 197 198 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAVRAsmPrinter() { 199 llvm::RegisterAsmPrinter<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget()); 200 } 201 202