1 //===-- AVRAsmPrinter.cpp - AVR LLVM assembly writer ----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains a printer that converts from our internal representation 11 // of machine-dependent LLVM code to GAS-format AVR assembly language. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AVR.h" 16 #include "AVRMCInstLower.h" 17 #include "AVRSubtarget.h" 18 #include "InstPrinter/AVRInstPrinter.h" 19 20 #include "llvm/CodeGen/AsmPrinter.h" 21 #include "llvm/CodeGen/MachineInstr.h" 22 #include "llvm/CodeGen/MachineFunction.h" 23 #include "llvm/IR/Mangler.h" 24 #include "llvm/MC/MCInst.h" 25 #include "llvm/MC/MCStreamer.h" 26 #include "llvm/MC/MCSymbol.h" 27 #include "llvm/Support/ErrorHandling.h" 28 #include "llvm/Support/TargetRegistry.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/Target/TargetRegisterInfo.h" 31 #include "llvm/Target/TargetSubtargetInfo.h" 32 33 #define DEBUG_TYPE "avr-asm-printer" 34 35 namespace llvm { 36 37 /// An AVR assembly code printer. 38 class AVRAsmPrinter : public AsmPrinter { 39 public: 40 AVRAsmPrinter(TargetMachine &TM, 41 std::unique_ptr<MCStreamer> Streamer) 42 : AsmPrinter(TM, std::move(Streamer)), MRI(*TM.getMCRegisterInfo()) { } 43 44 StringRef getPassName() const override { return "AVR Assembly Printer"; } 45 46 void printOperand(const MachineInstr *MI, unsigned OpNo, raw_ostream &O, 47 const char *Modifier = 0); 48 49 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNum, 50 unsigned AsmVariant, const char *ExtraCode, 51 raw_ostream &O) override; 52 53 bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum, 54 unsigned AsmVariant, const char *ExtraCode, 55 raw_ostream &O) override; 56 57 void EmitInstruction(const MachineInstr *MI) override; 58 59 private: 60 const MCRegisterInfo &MRI; 61 }; 62 63 void AVRAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, 64 raw_ostream &O, const char *Modifier) { 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 unsigned AsmVariant, const char *ExtraCode, 90 raw_ostream &O) { 91 // Default asm printer can only deal with some extra codes, 92 // so try it first. 93 bool Error = AsmPrinter::PrintAsmOperand(MI, OpNum, AsmVariant, ExtraCode, O); 94 95 if (Error && ExtraCode && ExtraCode[0]) { 96 if (ExtraCode[1] != 0) 97 return true; // Unknown modifier. 98 99 if (ExtraCode[0] >= 'A' && ExtraCode[0] <= 'Z') { 100 const MachineOperand &RegOp = MI->getOperand(OpNum); 101 102 assert(RegOp.isReg() && "Operand must be a register when you're" 103 "using 'A'..'Z' operand extracodes."); 104 unsigned Reg = RegOp.getReg(); 105 106 unsigned ByteNumber = ExtraCode[0] - 'A'; 107 108 unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 109 unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 110 111 const AVRSubtarget &STI = MF->getSubtarget<AVRSubtarget>(); 112 const TargetRegisterInfo &TRI = *STI.getRegisterInfo(); 113 114 unsigned BytesPerReg = TRI.getMinimalPhysRegClass(Reg)->getSize(); 115 assert(BytesPerReg <= 2 && "Only 8 and 16 bit regs are supported."); 116 117 unsigned RegIdx = ByteNumber / BytesPerReg; 118 assert(RegIdx < NumOpRegs && "Multibyte index out of range."); 119 120 Reg = MI->getOperand(OpNum + RegIdx).getReg(); 121 122 if (BytesPerReg == 2) { 123 Reg = TRI.getSubReg(Reg, ByteNumber % BytesPerReg ? AVR::sub_hi 124 : AVR::sub_lo); 125 } 126 127 O << AVRInstPrinter::getPrettyRegisterName(Reg, MRI); 128 return false; 129 } 130 } 131 132 printOperand(MI, OpNum, O); 133 134 return false; 135 } 136 137 bool AVRAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 138 unsigned OpNum, unsigned AsmVariant, 139 const char *ExtraCode, 140 raw_ostream &O) { 141 if (ExtraCode && ExtraCode[0]) { 142 llvm_unreachable("This branch is not implemented yet"); 143 } 144 145 const MachineOperand &MO = MI->getOperand(OpNum); 146 assert(MO.isReg() && "Unexpected inline asm memory operand"); 147 148 // TODO: We can look up the alternative name for the register if it's given. 149 if (MI->getOperand(OpNum).getReg() == AVR::R31R30) { 150 O << "Z"; 151 } else { 152 assert(MI->getOperand(OpNum).getReg() == AVR::R29R28 && 153 "Wrong register class for memory operand."); 154 O << "Y"; 155 } 156 157 // If NumOpRegs == 2, then we assume it is product of a FrameIndex expansion 158 // and the second operand is an Imm. 159 unsigned OpFlags = MI->getOperand(OpNum - 1).getImm(); 160 unsigned NumOpRegs = InlineAsm::getNumOperandRegisters(OpFlags); 161 162 if (NumOpRegs == 2) { 163 O << '+' << MI->getOperand(OpNum + 1).getImm(); 164 } 165 166 return false; 167 } 168 169 void AVRAsmPrinter::EmitInstruction(const MachineInstr *MI) { 170 AVRMCInstLower MCInstLowering(OutContext, *this); 171 172 MCInst I; 173 MCInstLowering.lowerInstruction(*MI, I); 174 EmitToStreamer(*OutStreamer, I); 175 } 176 177 } // end of namespace llvm 178 179 extern "C" void LLVMInitializeAVRAsmPrinter() { 180 llvm::RegisterAsmPrinter<llvm::AVRAsmPrinter> X(llvm::getTheAVRTarget()); 181 } 182 183