• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- LanaiAsmPrinter.cpp - Lanai 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 the Lanai assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "InstPrinter/LanaiInstPrinter.h"
16 #include "Lanai.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"
36 
37 #define DEBUG_TYPE "asm-printer"
38 
39 using namespace llvm;
40 
41 namespace {
42 class LanaiAsmPrinter : public AsmPrinter {
43 public:
LanaiAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)44   explicit LanaiAsmPrinter(TargetMachine &TM,
45                            std::unique_ptr<MCStreamer> Streamer)
46       : AsmPrinter(TM, std::move(Streamer)) {}
47 
getPassName() const48   const char *getPassName() const override { return "Lanai Assembly Printer"; }
49 
50   void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
51                     const char *Modifier = 0);
52   bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53                        unsigned AsmVariant, const char *ExtraCode,
54                        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 } // end of anonymous namespace
64 
printOperand(const MachineInstr * MI,int OpNum,raw_ostream & O,const char * Modifier)65 void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
66                                    raw_ostream &O, const char *Modifier) {
67   const MachineOperand &MO = MI->getOperand(OpNum);
68 
69   switch (MO.getType()) {
70   case MachineOperand::MO_Register:
71     O << LanaiInstPrinter::getRegisterName(MO.getReg());
72     break;
73 
74   case MachineOperand::MO_Immediate:
75     O << MO.getImm();
76     break;
77 
78   case MachineOperand::MO_MachineBasicBlock:
79     O << *MO.getMBB()->getSymbol();
80     break;
81 
82   case MachineOperand::MO_GlobalAddress:
83     O << *getSymbol(MO.getGlobal());
84     break;
85 
86   case MachineOperand::MO_BlockAddress: {
87     MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
88     O << BA->getName();
89     break;
90   }
91 
92   case MachineOperand::MO_ExternalSymbol:
93     O << *GetExternalSymbolSymbol(MO.getSymbolName());
94     break;
95 
96   case MachineOperand::MO_JumpTableIndex:
97     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
98       << MO.getIndex();
99     break;
100 
101   case MachineOperand::MO_ConstantPoolIndex:
102     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
103       << MO.getIndex();
104     return;
105 
106   default:
107     llvm_unreachable("<unknown operand type>");
108   }
109 }
110 
111 // PrintAsmOperand - Print out an operand for an inline asm expression.
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)112 bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
113                                       unsigned AsmVariant,
114                                       const char *ExtraCode, raw_ostream &O) {
115   // Does this asm operand have a single letter operand modifier?
116   if (ExtraCode && ExtraCode[0]) {
117     if (ExtraCode[1])
118       return true; // Unknown modifier.
119 
120     switch (ExtraCode[0]) {
121     // The highest-numbered register of a pair.
122     case 'H': {
123       if (OpNo == 0)
124         return true;
125       const MachineOperand &FlagsOP = MI->getOperand(OpNo - 1);
126       if (!FlagsOP.isImm())
127         return true;
128       unsigned Flags = FlagsOP.getImm();
129       unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
130       if (NumVals != 2)
131         return true;
132       unsigned RegOp = OpNo + 1;
133       if (RegOp >= MI->getNumOperands())
134         return true;
135       const MachineOperand &MO = MI->getOperand(RegOp);
136       if (!MO.isReg())
137         return true;
138       unsigned Reg = MO.getReg();
139       O << LanaiInstPrinter::getRegisterName(Reg);
140       return false;
141     }
142     default:
143       return true; // Unknown modifier.
144     }
145   }
146   printOperand(MI, OpNo, O);
147   return false;
148 }
149 
150 //===----------------------------------------------------------------------===//
emitCallInstruction(const MachineInstr * MI)151 void LanaiAsmPrinter::emitCallInstruction(const MachineInstr *MI) {
152   assert((MI->getOpcode() == Lanai::CALL || MI->getOpcode() == Lanai::CALLR) &&
153          "Unsupported call function");
154 
155   LanaiMCInstLower MCInstLowering(OutContext, *Mang, *this);
156   MCSubtargetInfo STI = getSubtargetInfo();
157   // Insert save rca instruction immediately before the call.
158   // TODO: We should generate a pc-relative mov instruction here instead
159   // of pc + 16 (should be mov .+16 %rca).
160   OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_I_LO)
161                                    .addReg(Lanai::RCA)
162                                    .addReg(Lanai::PC)
163                                    .addImm(16),
164                                STI);
165 
166   // Push rca onto the stack.
167   //   st %rca, [--%sp]
168   OutStreamer->EmitInstruction(MCInstBuilder(Lanai::SW_RI)
169                                    .addReg(Lanai::RCA)
170                                    .addReg(Lanai::SP)
171                                    .addImm(-4)
172                                    .addImm(LPAC::makePreOp(LPAC::ADD)),
173                                STI);
174 
175   // Lower the call instruction.
176   if (MI->getOpcode() == Lanai::CALL) {
177     MCInst TmpInst;
178     MCInstLowering.Lower(MI, TmpInst);
179     TmpInst.setOpcode(Lanai::BT);
180     OutStreamer->EmitInstruction(TmpInst, STI);
181   } else {
182     OutStreamer->EmitInstruction(MCInstBuilder(Lanai::ADD_R)
183                                      .addReg(Lanai::PC)
184                                      .addReg(MI->getOperand(0).getReg())
185                                      .addReg(Lanai::R0)
186                                      .addImm(LPCC::ICC_T),
187                                  STI);
188   }
189 }
190 
customEmitInstruction(const MachineInstr * MI)191 void LanaiAsmPrinter::customEmitInstruction(const MachineInstr *MI) {
192   LanaiMCInstLower MCInstLowering(OutContext, *Mang, *this);
193   MCSubtargetInfo STI = getSubtargetInfo();
194   MCInst TmpInst;
195   MCInstLowering.Lower(MI, TmpInst);
196   OutStreamer->EmitInstruction(TmpInst, STI);
197 }
198 
EmitInstruction(const MachineInstr * MI)199 void LanaiAsmPrinter::EmitInstruction(const MachineInstr *MI) {
200   MachineBasicBlock::const_instr_iterator I = MI->getIterator();
201   MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
202 
203   do {
204     if (I->isCall()) {
205       emitCallInstruction(&*I);
206       continue;
207     }
208 
209     customEmitInstruction(&*I);
210   } while ((++I != E) && I->isInsideBundle());
211 }
212 
213 // isBlockOnlyReachableByFallthough - Return true if the basic block has
214 // exactly one predecessor and the control transfer mechanism between
215 // the predecessor and this block is a fall-through.
216 // FIXME: could the overridden cases be handled in AnalyzeBranch?
isBlockOnlyReachableByFallthrough(const MachineBasicBlock * MBB) const217 bool LanaiAsmPrinter::isBlockOnlyReachableByFallthrough(
218     const MachineBasicBlock *MBB) const {
219   // The predecessor has to be immediately before this block.
220   const MachineBasicBlock *Pred = *MBB->pred_begin();
221 
222   // If the predecessor is a switch statement, assume a jump table
223   // implementation, so it is not a fall through.
224   if (const BasicBlock *B = Pred->getBasicBlock())
225     if (isa<SwitchInst>(B->getTerminator()))
226       return false;
227 
228   // Check default implementation
229   if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
230     return false;
231 
232   // Otherwise, check the last instruction.
233   // Check if the last terminator is an unconditional branch.
234   MachineBasicBlock::const_iterator I = Pred->end();
235   while (I != Pred->begin() && !(--I)->isTerminator()) {
236   }
237 
238   return !I->isBarrier();
239 }
240 
241 // Force static initialization.
LLVMInitializeLanaiAsmPrinter()242 extern "C" void LLVMInitializeLanaiAsmPrinter() {
243   RegisterAsmPrinter<LanaiAsmPrinter> X(TheLanaiTarget);
244 }
245