| 1 | //===-- LanaiAsmPrinter.cpp - Lanai 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 the Lanai assembly language. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "LanaiAsmPrinter.h" |
| 15 | #include "LanaiAluCode.h" |
| 16 | #include "LanaiCondCode.h" |
| 17 | #include "LanaiMCInstLower.h" |
| 18 | #include "LanaiTargetMachine.h" |
| 19 | #include "MCTargetDesc/LanaiInstPrinter.h" |
| 20 | #include "TargetInfo/LanaiTargetInfo.h" |
| 21 | #include "llvm/CodeGen/AsmPrinter.h" |
| 22 | #include "llvm/CodeGen/AsmPrinterAnalysis.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineFunctionAnalysisManager.h" |
| 25 | #include "llvm/CodeGen/MachineInstr.h" |
| 26 | #include "llvm/CodeGen/MachinePassManager.h" |
| 27 | #include "llvm/IR/Analysis.h" |
| 28 | #include "llvm/IR/Mangler.h" |
| 29 | #include "llvm/IR/PassManager.h" |
| 30 | #include "llvm/MC/MCAsmInfo.h" |
| 31 | #include "llvm/MC/MCInst.h" |
| 32 | #include "llvm/MC/MCInstBuilder.h" |
| 33 | #include "llvm/MC/MCStreamer.h" |
| 34 | #include "llvm/MC/MCSymbol.h" |
| 35 | #include "llvm/MC/TargetRegistry.h" |
| 36 | #include "llvm/Support/Compiler.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | |
| 39 | #define DEBUG_TYPE "asm-printer" |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | namespace { |
| 44 | class LanaiAsmPrinter : public AsmPrinter { |
| 45 | public: |
| 46 | explicit LanaiAsmPrinter(TargetMachine &TM, |
| 47 | std::unique_ptr<MCStreamer> Streamer) |
| 48 | : AsmPrinter(TM, std::move(Streamer), ID) {} |
| 49 | |
| 50 | StringRef getPassName() const override { return "Lanai Assembly Printer" ; } |
| 51 | |
| 52 | void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O); |
| 53 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 54 | const char *, raw_ostream &O) override; |
| 55 | void emitInstruction(const MachineInstr *MI) override; |
| 56 | bool isBlockOnlyReachableByFallthrough( |
| 57 | const MachineBasicBlock *MBB) const override; |
| 58 | |
| 59 | private: |
| 60 | void customEmitInstruction(const MachineInstr *MI); |
| 61 | void emitCallInstruction(const MachineInstr *MI); |
| 62 | |
| 63 | public: |
| 64 | static char ID; |
| 65 | }; |
| 66 | } // end of anonymous namespace |
| 67 | |
| 68 | void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum, |
| 69 | raw_ostream &O) { |
| 70 | const MachineOperand &MO = MI->getOperand(i: OpNum); |
| 71 | |
| 72 | switch (MO.getType()) { |
| 73 | case MachineOperand::MO_Register: |
| 74 | O << LanaiInstPrinter::getRegisterName(Reg: MO.getReg()); |
| 75 | break; |
| 76 | |
| 77 | case MachineOperand::MO_Immediate: |
| 78 | O << MO.getImm(); |
| 79 | break; |
| 80 | |
| 81 | case MachineOperand::MO_MachineBasicBlock: |
| 82 | O << *MO.getMBB()->getSymbol(); |
| 83 | break; |
| 84 | |
| 85 | case MachineOperand::MO_GlobalAddress: |
| 86 | O << *getSymbol(GV: MO.getGlobal()); |
| 87 | break; |
| 88 | |
| 89 | case MachineOperand::MO_BlockAddress: { |
| 90 | MCSymbol *BA = GetBlockAddressSymbol(BA: MO.getBlockAddress()); |
| 91 | O << BA->getName(); |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | case MachineOperand::MO_ExternalSymbol: |
| 96 | O << *GetExternalSymbolSymbol(Sym: MO.getSymbolName()); |
| 97 | break; |
| 98 | |
| 99 | case MachineOperand::MO_JumpTableIndex: |
| 100 | O << MAI.getInternalSymbolPrefix() << "JTI" << getFunctionNumber() << '_' |
| 101 | << MO.getIndex(); |
| 102 | break; |
| 103 | |
| 104 | case MachineOperand::MO_ConstantPoolIndex: |
| 105 | O << MAI.getInternalSymbolPrefix() << "CPI" << getFunctionNumber() << '_' |
| 106 | << MO.getIndex(); |
| 107 | return; |
| 108 | |
| 109 | default: |
| 110 | llvm_unreachable("<unknown operand type>" ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // PrintAsmOperand - Print out an operand for an inline asm expression. |
| 115 | bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 116 | const char *, raw_ostream &O) { |
| 117 | // Does this asm operand have a single letter operand modifier? |
| 118 | if (ExtraCode && ExtraCode[0]) { |
| 119 | if (ExtraCode[1]) |
| 120 | return true; // Unknown modifier. |
| 121 | |
| 122 | switch (ExtraCode[0]) { |
| 123 | // The highest-numbered register of a pair. |
| 124 | case 'H': { |
| 125 | if (OpNo == 0) |
| 126 | return true; |
| 127 | const MachineOperand &FlagsOP = MI->getOperand(i: OpNo - 1); |
| 128 | if (!FlagsOP.isImm()) |
| 129 | return true; |
| 130 | const InlineAsm::Flag Flags(FlagsOP.getImm()); |
| 131 | const unsigned NumVals = Flags.getNumOperandRegisters(); |
| 132 | if (NumVals != 2) |
| 133 | return true; |
| 134 | unsigned RegOp = OpNo + 1; |
| 135 | if (RegOp >= MI->getNumOperands()) |
| 136 | return true; |
| 137 | const MachineOperand &MO = MI->getOperand(i: RegOp); |
| 138 | if (!MO.isReg()) |
| 139 | return true; |
| 140 | Register Reg = MO.getReg(); |
| 141 | O << LanaiInstPrinter::getRegisterName(Reg); |
| 142 | return false; |
| 143 | } |
| 144 | default: |
| 145 | return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, OS&: O); |
| 146 | } |
| 147 | } |
| 148 | printOperand(MI, OpNum: OpNo, O); |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | void LanaiAsmPrinter::emitCallInstruction(const MachineInstr *MI) { |
| 154 | assert((MI->getOpcode() == Lanai::CALL || MI->getOpcode() == Lanai::CALLR) && |
| 155 | "Unsupported call function" ); |
| 156 | |
| 157 | LanaiMCInstLower MCInstLowering(OutContext, *this); |
| 158 | MCSubtargetInfo STI = getSubtargetInfo(); |
| 159 | // Insert save rca instruction immediately before the call. |
| 160 | // TODO: We should generate a pc-relative mov instruction here instead |
| 161 | // of pc + 16 (should be mov .+16 %rca). |
| 162 | OutStreamer->emitInstruction(Inst: MCInstBuilder(Lanai::ADD_I_LO) |
| 163 | .addReg(Reg: Lanai::RCA) |
| 164 | .addReg(Reg: Lanai::PC) |
| 165 | .addImm(Val: 16), |
| 166 | STI); |
| 167 | |
| 168 | // Push rca onto the stack. |
| 169 | // st %rca, [--%sp] |
| 170 | OutStreamer->emitInstruction(Inst: MCInstBuilder(Lanai::SW_RI) |
| 171 | .addReg(Reg: Lanai::RCA) |
| 172 | .addReg(Reg: Lanai::SP) |
| 173 | .addImm(Val: -4) |
| 174 | .addImm(Val: LPAC::makePreOp(AluOp: LPAC::ADD)), |
| 175 | STI); |
| 176 | |
| 177 | // Lower the call instruction. |
| 178 | if (MI->getOpcode() == Lanai::CALL) { |
| 179 | MCInst TmpInst; |
| 180 | MCInstLowering.Lower(MI, OutMI&: TmpInst); |
| 181 | TmpInst.setOpcode(Lanai::BT); |
| 182 | OutStreamer->emitInstruction(Inst: TmpInst, STI); |
| 183 | } else { |
| 184 | OutStreamer->emitInstruction(Inst: MCInstBuilder(Lanai::ADD_R) |
| 185 | .addReg(Reg: Lanai::PC) |
| 186 | .addReg(Reg: MI->getOperand(i: 0).getReg()) |
| 187 | .addReg(Reg: Lanai::R0) |
| 188 | .addImm(Val: LPCC::ICC_T), |
| 189 | STI); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void LanaiAsmPrinter::customEmitInstruction(const MachineInstr *MI) { |
| 194 | LanaiMCInstLower MCInstLowering(OutContext, *this); |
| 195 | MCSubtargetInfo STI = getSubtargetInfo(); |
| 196 | MCInst TmpInst; |
| 197 | MCInstLowering.Lower(MI, OutMI&: TmpInst); |
| 198 | OutStreamer->emitInstruction(Inst: TmpInst, STI); |
| 199 | } |
| 200 | |
| 201 | void LanaiAsmPrinter::emitInstruction(const MachineInstr *MI) { |
| 202 | Lanai_MC::verifyInstructionPredicates(Opcode: MI->getOpcode(), |
| 203 | Features: getSubtargetInfo().getFeatureBits()); |
| 204 | |
| 205 | MachineBasicBlock::const_instr_iterator I = MI->getIterator(); |
| 206 | MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end(); |
| 207 | |
| 208 | do { |
| 209 | if (I->isCall()) { |
| 210 | emitCallInstruction(MI: &*I); |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | customEmitInstruction(MI: &*I); |
| 215 | } while ((++I != E) && I->isInsideBundle()); |
| 216 | } |
| 217 | |
| 218 | // isBlockOnlyReachableByFallthough - Return true if the basic block has |
| 219 | // exactly one predecessor and the control transfer mechanism between |
| 220 | // the predecessor and this block is a fall-through. |
| 221 | // FIXME: could the overridden cases be handled in analyzeBranch? |
| 222 | bool LanaiAsmPrinter::isBlockOnlyReachableByFallthrough( |
| 223 | const MachineBasicBlock *MBB) const { |
| 224 | // The predecessor has to be immediately before this block. |
| 225 | const MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 226 | |
| 227 | // If the predecessor is a switch statement, assume a jump table |
| 228 | // implementation, so it is not a fall through. |
| 229 | if (const BasicBlock *B = Pred->getBasicBlock()) |
| 230 | if (isa<SwitchInst>(Val: B->getTerminator())) |
| 231 | return false; |
| 232 | |
| 233 | // Check default implementation |
| 234 | if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB)) |
| 235 | return false; |
| 236 | |
| 237 | // Otherwise, check the last instruction. |
| 238 | // Check if the last terminator is an unconditional branch. |
| 239 | MachineBasicBlock::const_iterator I = Pred->end(); |
| 240 | while (I != Pred->begin() && !(--I)->isTerminator()) { |
| 241 | } |
| 242 | |
| 243 | return !I->isBarrier(); |
| 244 | } |
| 245 | |
| 246 | char LanaiAsmPrinter::ID = 0; |
| 247 | |
| 248 | INITIALIZE_PASS(LanaiAsmPrinter, "lanai-asm-printer" , "Lanai Assembly Printer" , |
| 249 | false, false) |
| 250 | |
| 251 | // Force static initialization. |
| 252 | extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void |
| 253 | LLVMInitializeLanaiAsmPrinter() { |
| 254 | RegisterAsmPrinter<LanaiAsmPrinter> X(getTheLanaiTarget()); |
| 255 | } |
| 256 | |
| 257 | PreservedAnalyses LanaiAsmPrinterBeginPass::run(Module &M, |
| 258 | ModuleAnalysisManager &MAM) { |
| 259 | LanaiAsmPrinter &AsmPrinter = static_cast<LanaiAsmPrinter &>( |
| 260 | MAM.getResult<AsmPrinterAnalysis>(IR&: M).getPrinter()); |
| 261 | setupModuleAsmPrinter(M, MAM, AsmPrinter); |
| 262 | AsmPrinter.doInitialization(M); |
| 263 | return PreservedAnalyses::all(); |
| 264 | } |
| 265 | |
| 266 | PreservedAnalyses |
| 267 | LanaiAsmPrinterPass::run(MachineFunction &MF, |
| 268 | MachineFunctionAnalysisManager &MFAM) { |
| 269 | LanaiAsmPrinter &AsmPrinter = static_cast<LanaiAsmPrinter &>( |
| 270 | MFAM.getResult<ModuleAnalysisManagerMachineFunctionProxy>(IR&: MF) |
| 271 | .getCachedResult<AsmPrinterAnalysis>(IR&: *MF.getFunction().getParent()) |
| 272 | ->getPrinter()); |
| 273 | setupMachineFunctionAsmPrinter(MFAM, MF, AsmPrinter); |
| 274 | AsmPrinter.runOnMachineFunction(MF); |
| 275 | return PreservedAnalyses::all(); |
| 276 | } |
| 277 | |
| 278 | PreservedAnalyses LanaiAsmPrinterEndPass::run(Module &M, |
| 279 | ModuleAnalysisManager &MAM) { |
| 280 | LanaiAsmPrinter &AsmPrinter = static_cast<LanaiAsmPrinter &>( |
| 281 | MAM.getResult<AsmPrinterAnalysis>(IR&: M).getPrinter()); |
| 282 | setupModuleAsmPrinter(M, MAM, AsmPrinter); |
| 283 | AsmPrinter.doFinalization(M); |
| 284 | return PreservedAnalyses::all(); |
| 285 | } |
| 286 | |