1 //===-- LanaiAsmPrinter.cpp - Lanai LLVM assembly writer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the Lanai assembly language.
13 //===----------------------------------------------------------------------===//
15 #include "InstPrinter/LanaiInstPrinter.h"
17 #include "LanaiInstrInfo.h"
18 #include "LanaiMCInstLower.h"
19 #include "LanaiTargetMachine.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/MachineConstantPool.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstr.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/Mangler.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/MC/MCInst.h"
31 #include "llvm/MC/MCInstBuilder.h"
32 #include "llvm/MC/MCStreamer.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/Support/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
37 #define DEBUG_TYPE "asm-printer"
42 class LanaiAsmPrinter : public AsmPrinter {
44 explicit LanaiAsmPrinter(TargetMachine &TM,
45 std::unique_ptr<MCStreamer> Streamer)
46 : AsmPrinter(TM, std::move(Streamer)) {}
48 const char *getPassName() const override { return "Lanai Assembly Printer"; }
50 void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
51 bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
52 unsigned AsmVariant, const char *ExtraCode,
53 raw_ostream &O) override;
54 void EmitInstruction(const MachineInstr *MI) override;
55 bool isBlockOnlyReachableByFallthrough(
56 const MachineBasicBlock *MBB) const override;
59 void customEmitInstruction(const MachineInstr *MI);
60 void emitCallInstruction(const MachineInstr *MI);
62 } // end of anonymous namespace
64 void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
66 const MachineOperand &MO = MI->getOperand(OpNum);
68 switch (MO.getType()) {
69 case MachineOperand::MO_Register:
70 O << LanaiInstPrinter::getRegisterName(MO.getReg());
73 case MachineOperand::MO_Immediate:
77 case MachineOperand::MO_MachineBasicBlock:
78 O << *MO.getMBB()->getSymbol();
81 case MachineOperand::MO_GlobalAddress:
82 O << *getSymbol(MO.getGlobal());
85 case MachineOperand::MO_BlockAddress: {
86 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
91 case MachineOperand::MO_ExternalSymbol:
92 O << *GetExternalSymbolSymbol(MO.getSymbolName());
95 case MachineOperand::MO_JumpTableIndex:
96 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
100 case MachineOperand::MO_ConstantPoolIndex:
101 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
106 llvm_unreachable("<unknown operand type>");
110 // PrintAsmOperand - Print out an operand for an inline asm expression.
111 bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
112 unsigned /*AsmVariant*/,
113 const char *ExtraCode, raw_ostream &O) {
114 // Does this asm operand have a single letter operand modifier?
115 if (ExtraCode && ExtraCode[0]) {
117 return true; // Unknown modifier.
119 switch (ExtraCode[0]) {
120 // The highest-numbered register of a pair.
124 const MachineOperand &FlagsOP = MI->getOperand(OpNo - 1);
125 if (!FlagsOP.isImm())
127 unsigned Flags = FlagsOP.getImm();
128 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
131 unsigned RegOp = OpNo + 1;
132 if (RegOp >= MI->getNumOperands())
134 const MachineOperand &MO = MI->getOperand(RegOp);
137 unsigned Reg = MO.getReg();
138 O << LanaiInstPrinter::getRegisterName(Reg);
142 return true; // Unknown modifier.
145 printOperand(MI, OpNo, O);
149 //===----------------------------------------------------------------------===//
150 void LanaiAsmPrinter::emitCallInstruction(const MachineInstr *MI) {
151 assert((MI->getOpcode() == Lanai::CALL || MI->getOpcode() == Lanai::CALLR) &&
152 "Unsupported call function");
154 LanaiMCInstLower MCInstLowering(OutContext, *Mang, *this);
155 MCSubtargetInfo STI = getSubtargetInfo();
156 // Insert save rca instruction immediately before the call.
157 // TODO: We should generate a pc-relative mov instruction here instead
158 // of pc + 16 (should be mov .+16 %rca).
159 OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_I_LO)
165 // Push rca onto the stack.
167 OutStreamer->EmitInstruction(MCInstBuilder(Lanai::SW_RI)
171 .addImm(LPAC::makePreOp(LPAC::ADD)),
174 // Lower the call instruction.
175 if (MI->getOpcode() == Lanai::CALL) {
177 MCInstLowering.Lower(MI, TmpInst);
178 TmpInst.setOpcode(Lanai::BT);
179 OutStreamer->EmitInstruction(TmpInst, STI);
181 OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_R)
183 .addReg(MI->getOperand(0).getReg())
185 .addImm(LPCC::ICC_T),
190 void LanaiAsmPrinter::customEmitInstruction(const MachineInstr *MI) {
191 LanaiMCInstLower MCInstLowering(OutContext, *Mang, *this);
192 MCSubtargetInfo STI = getSubtargetInfo();
194 MCInstLowering.Lower(MI, TmpInst);
195 OutStreamer->EmitInstruction(TmpInst, STI);
198 void LanaiAsmPrinter::EmitInstruction(const MachineInstr *MI) {
199 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
200 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
204 emitCallInstruction(&*I);
208 customEmitInstruction(&*I);
209 } while ((++I != E) && I->isInsideBundle());
212 // isBlockOnlyReachableByFallthough - Return true if the basic block has
213 // exactly one predecessor and the control transfer mechanism between
214 // the predecessor and this block is a fall-through.
215 // FIXME: could the overridden cases be handled in AnalyzeBranch?
216 bool LanaiAsmPrinter::isBlockOnlyReachableByFallthrough(
217 const MachineBasicBlock *MBB) const {
218 // The predecessor has to be immediately before this block.
219 const MachineBasicBlock *Pred = *MBB->pred_begin();
221 // If the predecessor is a switch statement, assume a jump table
222 // implementation, so it is not a fall through.
223 if (const BasicBlock *B = Pred->getBasicBlock())
224 if (isa<SwitchInst>(B->getTerminator()))
227 // Check default implementation
228 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
231 // Otherwise, check the last instruction.
232 // Check if the last terminator is an unconditional branch.
233 MachineBasicBlock::const_iterator I = Pred->end();
234 while (I != Pred->begin() && !(--I)->isTerminator()) {
237 return !I->isBarrier();
240 // Force static initialization.
241 extern "C" void LLVMInitializeLanaiAsmPrinter() {
242 RegisterAsmPrinter<LanaiAsmPrinter> X(TheLanaiTarget);