[Alignment] Use llvm::Align in MachineFunction and TargetLowering - fixes mir parsing
[llvm-core.git] / lib / Target / Mips / MipsAsmPrinter.cpp
blob8840a4938c9fc81e4135777e27fbe0ae9545dd33
1 //===- MipsAsmPrinter.cpp - Mips LLVM Assembly Printer --------------------===//
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 MIPS assembly language.
12 //===----------------------------------------------------------------------===//
14 #include "MipsAsmPrinter.h"
15 #include "MCTargetDesc/MipsABIInfo.h"
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MCTargetDesc/MipsInstPrinter.h"
18 #include "MCTargetDesc/MipsMCNaCl.h"
19 #include "MCTargetDesc/MipsMCTargetDesc.h"
20 #include "Mips.h"
21 #include "MipsMCInstLower.h"
22 #include "MipsMachineFunction.h"
23 #include "MipsSubtarget.h"
24 #include "MipsTargetMachine.h"
25 #include "MipsTargetStreamer.h"
26 #include "TargetInfo/MipsTargetInfo.h"
27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/Triple.h"
30 #include "llvm/ADT/Twine.h"
31 #include "llvm/BinaryFormat/ELF.h"
32 #include "llvm/CodeGen/MachineBasicBlock.h"
33 #include "llvm/CodeGen/MachineConstantPool.h"
34 #include "llvm/CodeGen/MachineFrameInfo.h"
35 #include "llvm/CodeGen/MachineFunction.h"
36 #include "llvm/CodeGen/MachineInstr.h"
37 #include "llvm/CodeGen/MachineJumpTableInfo.h"
38 #include "llvm/CodeGen/MachineOperand.h"
39 #include "llvm/CodeGen/TargetRegisterInfo.h"
40 #include "llvm/CodeGen/TargetSubtargetInfo.h"
41 #include "llvm/IR/Attributes.h"
42 #include "llvm/IR/BasicBlock.h"
43 #include "llvm/IR/DataLayout.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/InlineAsm.h"
46 #include "llvm/IR/Instructions.h"
47 #include "llvm/MC/MCContext.h"
48 #include "llvm/MC/MCExpr.h"
49 #include "llvm/MC/MCInst.h"
50 #include "llvm/MC/MCInstBuilder.h"
51 #include "llvm/MC/MCObjectFileInfo.h"
52 #include "llvm/MC/MCSectionELF.h"
53 #include "llvm/MC/MCSymbol.h"
54 #include "llvm/MC/MCSymbolELF.h"
55 #include "llvm/Support/Casting.h"
56 #include "llvm/Support/ErrorHandling.h"
57 #include "llvm/Support/TargetRegistry.h"
58 #include "llvm/Support/raw_ostream.h"
59 #include "llvm/Target/TargetLoweringObjectFile.h"
60 #include "llvm/Target/TargetMachine.h"
61 #include <cassert>
62 #include <cstdint>
63 #include <map>
64 #include <memory>
65 #include <string>
66 #include <vector>
68 using namespace llvm;
70 #define DEBUG_TYPE "mips-asm-printer"
72 extern cl::opt<bool> EmitJalrReloc;
74 MipsTargetStreamer &MipsAsmPrinter::getTargetStreamer() const {
75 return static_cast<MipsTargetStreamer &>(*OutStreamer->getTargetStreamer());
78 bool MipsAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
79 Subtarget = &MF.getSubtarget<MipsSubtarget>();
81 MipsFI = MF.getInfo<MipsFunctionInfo>();
82 if (Subtarget->inMips16Mode())
83 for (std::map<
84 const char *,
85 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
86 it = MipsFI->StubsNeeded.begin();
87 it != MipsFI->StubsNeeded.end(); ++it) {
88 const char *Symbol = it->first;
89 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
90 if (StubsNeeded.find(Symbol) == StubsNeeded.end())
91 StubsNeeded[Symbol] = Signature;
93 MCP = MF.getConstantPool();
95 // In NaCl, all indirect jump targets must be aligned to bundle size.
96 if (Subtarget->isTargetNaCl())
97 NaClAlignIndirectJumpTargets(MF);
99 AsmPrinter::runOnMachineFunction(MF);
101 emitXRayTable();
103 return true;
106 bool MipsAsmPrinter::lowerOperand(const MachineOperand &MO, MCOperand &MCOp) {
107 MCOp = MCInstLowering.LowerOperand(MO);
108 return MCOp.isValid();
111 #include "MipsGenMCPseudoLowering.inc"
113 // Lower PseudoReturn/PseudoIndirectBranch/PseudoIndirectBranch64 to JR, JR_MM,
114 // JALR, or JALR64 as appropriate for the target.
115 void MipsAsmPrinter::emitPseudoIndirectBranch(MCStreamer &OutStreamer,
116 const MachineInstr *MI) {
117 bool HasLinkReg = false;
118 bool InMicroMipsMode = Subtarget->inMicroMipsMode();
119 MCInst TmpInst0;
121 if (Subtarget->hasMips64r6()) {
122 // MIPS64r6 should use (JALR64 ZERO_64, $rs)
123 TmpInst0.setOpcode(Mips::JALR64);
124 HasLinkReg = true;
125 } else if (Subtarget->hasMips32r6()) {
126 // MIPS32r6 should use (JALR ZERO, $rs)
127 if (InMicroMipsMode)
128 TmpInst0.setOpcode(Mips::JRC16_MMR6);
129 else {
130 TmpInst0.setOpcode(Mips::JALR);
131 HasLinkReg = true;
133 } else if (Subtarget->inMicroMipsMode())
134 // microMIPS should use (JR_MM $rs)
135 TmpInst0.setOpcode(Mips::JR_MM);
136 else {
137 // Everything else should use (JR $rs)
138 TmpInst0.setOpcode(Mips::JR);
141 MCOperand MCOp;
143 if (HasLinkReg) {
144 unsigned ZeroReg = Subtarget->isGP64bit() ? Mips::ZERO_64 : Mips::ZERO;
145 TmpInst0.addOperand(MCOperand::createReg(ZeroReg));
148 lowerOperand(MI->getOperand(0), MCOp);
149 TmpInst0.addOperand(MCOp);
151 EmitToStreamer(OutStreamer, TmpInst0);
154 // If there is an MO_JALR operand, insert:
156 // .reloc tmplabel, R_{MICRO}MIPS_JALR, symbol
157 // tmplabel:
159 // This is an optimization hint for the linker which may then replace
160 // an indirect call with a direct branch.
161 static void emitDirectiveRelocJalr(const MachineInstr &MI,
162 MCContext &OutContext,
163 TargetMachine &TM,
164 MCStreamer &OutStreamer,
165 const MipsSubtarget &Subtarget) {
166 for (unsigned int I = MI.getDesc().getNumOperands(), E = MI.getNumOperands();
167 I < E; ++I) {
168 MachineOperand MO = MI.getOperand(I);
169 if (MO.isMCSymbol() && (MO.getTargetFlags() & MipsII::MO_JALR)) {
170 MCSymbol *Callee = MO.getMCSymbol();
171 if (Callee && !Callee->getName().empty()) {
172 MCSymbol *OffsetLabel = OutContext.createTempSymbol();
173 const MCExpr *OffsetExpr =
174 MCSymbolRefExpr::create(OffsetLabel, OutContext);
175 const MCExpr *CaleeExpr =
176 MCSymbolRefExpr::create(Callee, OutContext);
177 OutStreamer.EmitRelocDirective
178 (*OffsetExpr,
179 Subtarget.inMicroMipsMode() ? "R_MICROMIPS_JALR" : "R_MIPS_JALR",
180 CaleeExpr, SMLoc(), *TM.getMCSubtargetInfo());
181 OutStreamer.EmitLabel(OffsetLabel);
182 return;
188 void MipsAsmPrinter::EmitInstruction(const MachineInstr *MI) {
189 MipsTargetStreamer &TS = getTargetStreamer();
190 unsigned Opc = MI->getOpcode();
191 TS.forbidModuleDirective();
193 if (MI->isDebugValue()) {
194 SmallString<128> Str;
195 raw_svector_ostream OS(Str);
197 PrintDebugValueComment(MI, OS);
198 return;
200 if (MI->isDebugLabel())
201 return;
203 // If we just ended a constant pool, mark it as such.
204 if (InConstantPool && Opc != Mips::CONSTPOOL_ENTRY) {
205 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
206 InConstantPool = false;
208 if (Opc == Mips::CONSTPOOL_ENTRY) {
209 // CONSTPOOL_ENTRY - This instruction represents a floating
210 // constant pool in the function. The first operand is the ID#
211 // for this instruction, the second is the index into the
212 // MachineConstantPool that this is, the third is the size in
213 // bytes of this constant pool entry.
214 // The required alignment is specified on the basic block holding this MI.
216 unsigned LabelId = (unsigned)MI->getOperand(0).getImm();
217 unsigned CPIdx = (unsigned)MI->getOperand(1).getIndex();
219 // If this is the first entry of the pool, mark it.
220 if (!InConstantPool) {
221 OutStreamer->EmitDataRegion(MCDR_DataRegion);
222 InConstantPool = true;
225 OutStreamer->EmitLabel(GetCPISymbol(LabelId));
227 const MachineConstantPoolEntry &MCPE = MCP->getConstants()[CPIdx];
228 if (MCPE.isMachineConstantPoolEntry())
229 EmitMachineConstantPoolValue(MCPE.Val.MachineCPVal);
230 else
231 EmitGlobalConstant(MF->getDataLayout(), MCPE.Val.ConstVal);
232 return;
235 switch (Opc) {
236 case Mips::PATCHABLE_FUNCTION_ENTER:
237 LowerPATCHABLE_FUNCTION_ENTER(*MI);
238 return;
239 case Mips::PATCHABLE_FUNCTION_EXIT:
240 LowerPATCHABLE_FUNCTION_EXIT(*MI);
241 return;
242 case Mips::PATCHABLE_TAIL_CALL:
243 LowerPATCHABLE_TAIL_CALL(*MI);
244 return;
247 if (EmitJalrReloc &&
248 (MI->isReturn() || MI->isCall() || MI->isIndirectBranch())) {
249 emitDirectiveRelocJalr(*MI, OutContext, TM, *OutStreamer, *Subtarget);
252 MachineBasicBlock::const_instr_iterator I = MI->getIterator();
253 MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();
255 do {
256 // Do any auto-generated pseudo lowerings.
257 if (emitPseudoExpansionLowering(*OutStreamer, &*I))
258 continue;
260 if (I->getOpcode() == Mips::PseudoReturn ||
261 I->getOpcode() == Mips::PseudoReturn64 ||
262 I->getOpcode() == Mips::PseudoIndirectBranch ||
263 I->getOpcode() == Mips::PseudoIndirectBranch64 ||
264 I->getOpcode() == Mips::TAILCALLREG ||
265 I->getOpcode() == Mips::TAILCALLREG64) {
266 emitPseudoIndirectBranch(*OutStreamer, &*I);
267 continue;
270 // The inMips16Mode() test is not permanent.
271 // Some instructions are marked as pseudo right now which
272 // would make the test fail for the wrong reason but
273 // that will be fixed soon. We need this here because we are
274 // removing another test for this situation downstream in the
275 // callchain.
277 if (I->isPseudo() && !Subtarget->inMips16Mode()
278 && !isLongBranchPseudo(I->getOpcode()))
279 llvm_unreachable("Pseudo opcode found in EmitInstruction()");
281 MCInst TmpInst0;
282 MCInstLowering.Lower(&*I, TmpInst0);
283 EmitToStreamer(*OutStreamer, TmpInst0);
284 } while ((++I != E) && I->isInsideBundle()); // Delay slot check
287 //===----------------------------------------------------------------------===//
289 // Mips Asm Directives
291 // -- Frame directive "frame Stackpointer, Stacksize, RARegister"
292 // Describe the stack frame.
294 // -- Mask directives "(f)mask bitmask, offset"
295 // Tells the assembler which registers are saved and where.
296 // bitmask - contain a little endian bitset indicating which registers are
297 // saved on function prologue (e.g. with a 0x80000000 mask, the
298 // assembler knows the register 31 (RA) is saved at prologue.
299 // offset - the position before stack pointer subtraction indicating where
300 // the first saved register on prologue is located. (e.g. with a
302 // Consider the following function prologue:
304 // .frame $fp,48,$ra
305 // .mask 0xc0000000,-8
306 // addiu $sp, $sp, -48
307 // sw $ra, 40($sp)
308 // sw $fp, 36($sp)
310 // With a 0xc0000000 mask, the assembler knows the register 31 (RA) and
311 // 30 (FP) are saved at prologue. As the save order on prologue is from
312 // left to right, RA is saved first. A -8 offset means that after the
313 // stack pointer subtration, the first register in the mask (RA) will be
314 // saved at address 48-8=40.
316 //===----------------------------------------------------------------------===//
318 //===----------------------------------------------------------------------===//
319 // Mask directives
320 //===----------------------------------------------------------------------===//
322 // Create a bitmask with all callee saved registers for CPU or Floating Point
323 // registers. For CPU registers consider RA, GP and FP for saving if necessary.
324 void MipsAsmPrinter::printSavedRegsBitmask() {
325 // CPU and FPU Saved Registers Bitmasks
326 unsigned CPUBitmask = 0, FPUBitmask = 0;
327 int CPUTopSavedRegOff, FPUTopSavedRegOff;
329 // Set the CPU and FPU Bitmasks
330 const MachineFrameInfo &MFI = MF->getFrameInfo();
331 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
332 const std::vector<CalleeSavedInfo> &CSI = MFI.getCalleeSavedInfo();
333 // size of stack area to which FP callee-saved regs are saved.
334 unsigned CPURegSize = TRI->getRegSizeInBits(Mips::GPR32RegClass) / 8;
335 unsigned FGR32RegSize = TRI->getRegSizeInBits(Mips::FGR32RegClass) / 8;
336 unsigned AFGR64RegSize = TRI->getRegSizeInBits(Mips::AFGR64RegClass) / 8;
337 bool HasAFGR64Reg = false;
338 unsigned CSFPRegsSize = 0;
340 for (const auto &I : CSI) {
341 unsigned Reg = I.getReg();
342 unsigned RegNum = TRI->getEncodingValue(Reg);
344 // If it's a floating point register, set the FPU Bitmask.
345 // If it's a general purpose register, set the CPU Bitmask.
346 if (Mips::FGR32RegClass.contains(Reg)) {
347 FPUBitmask |= (1 << RegNum);
348 CSFPRegsSize += FGR32RegSize;
349 } else if (Mips::AFGR64RegClass.contains(Reg)) {
350 FPUBitmask |= (3 << RegNum);
351 CSFPRegsSize += AFGR64RegSize;
352 HasAFGR64Reg = true;
353 } else if (Mips::GPR32RegClass.contains(Reg))
354 CPUBitmask |= (1 << RegNum);
357 // FP Regs are saved right below where the virtual frame pointer points to.
358 FPUTopSavedRegOff = FPUBitmask ?
359 (HasAFGR64Reg ? -AFGR64RegSize : -FGR32RegSize) : 0;
361 // CPU Regs are saved below FP Regs.
362 CPUTopSavedRegOff = CPUBitmask ? -CSFPRegsSize - CPURegSize : 0;
364 MipsTargetStreamer &TS = getTargetStreamer();
365 // Print CPUBitmask
366 TS.emitMask(CPUBitmask, CPUTopSavedRegOff);
368 // Print FPUBitmask
369 TS.emitFMask(FPUBitmask, FPUTopSavedRegOff);
372 //===----------------------------------------------------------------------===//
373 // Frame and Set directives
374 //===----------------------------------------------------------------------===//
376 /// Frame Directive
377 void MipsAsmPrinter::emitFrameDirective() {
378 const TargetRegisterInfo &RI = *MF->getSubtarget().getRegisterInfo();
380 Register stackReg = RI.getFrameRegister(*MF);
381 unsigned returnReg = RI.getRARegister();
382 unsigned stackSize = MF->getFrameInfo().getStackSize();
384 getTargetStreamer().emitFrame(stackReg, stackSize, returnReg);
387 /// Emit Set directives.
388 const char *MipsAsmPrinter::getCurrentABIString() const {
389 switch (static_cast<MipsTargetMachine &>(TM).getABI().GetEnumValue()) {
390 case MipsABIInfo::ABI::O32: return "abi32";
391 case MipsABIInfo::ABI::N32: return "abiN32";
392 case MipsABIInfo::ABI::N64: return "abi64";
393 default: llvm_unreachable("Unknown Mips ABI");
397 void MipsAsmPrinter::EmitFunctionEntryLabel() {
398 MipsTargetStreamer &TS = getTargetStreamer();
400 // NaCl sandboxing requires that indirect call instructions are masked.
401 // This means that function entry points should be bundle-aligned.
402 if (Subtarget->isTargetNaCl())
403 EmitAlignment(
404 std::max(Log2(MF->getAlignment()), MIPS_NACL_BUNDLE_LOG_ALIGN));
406 if (Subtarget->inMicroMipsMode()) {
407 TS.emitDirectiveSetMicroMips();
408 TS.setUsesMicroMips();
409 TS.updateABIInfo(*Subtarget);
410 } else
411 TS.emitDirectiveSetNoMicroMips();
413 if (Subtarget->inMips16Mode())
414 TS.emitDirectiveSetMips16();
415 else
416 TS.emitDirectiveSetNoMips16();
418 TS.emitDirectiveEnt(*CurrentFnSym);
419 OutStreamer->EmitLabel(CurrentFnSym);
422 /// EmitFunctionBodyStart - Targets can override this to emit stuff before
423 /// the first basic block in the function.
424 void MipsAsmPrinter::EmitFunctionBodyStart() {
425 MipsTargetStreamer &TS = getTargetStreamer();
427 MCInstLowering.Initialize(&MF->getContext());
429 bool IsNakedFunction = MF->getFunction().hasFnAttribute(Attribute::Naked);
430 if (!IsNakedFunction)
431 emitFrameDirective();
433 if (!IsNakedFunction)
434 printSavedRegsBitmask();
436 if (!Subtarget->inMips16Mode()) {
437 TS.emitDirectiveSetNoReorder();
438 TS.emitDirectiveSetNoMacro();
439 TS.emitDirectiveSetNoAt();
443 /// EmitFunctionBodyEnd - Targets can override this to emit stuff after
444 /// the last basic block in the function.
445 void MipsAsmPrinter::EmitFunctionBodyEnd() {
446 MipsTargetStreamer &TS = getTargetStreamer();
448 // There are instruction for this macros, but they must
449 // always be at the function end, and we can't emit and
450 // break with BB logic.
451 if (!Subtarget->inMips16Mode()) {
452 TS.emitDirectiveSetAt();
453 TS.emitDirectiveSetMacro();
454 TS.emitDirectiveSetReorder();
456 TS.emitDirectiveEnd(CurrentFnSym->getName());
457 // Make sure to terminate any constant pools that were at the end
458 // of the function.
459 if (!InConstantPool)
460 return;
461 InConstantPool = false;
462 OutStreamer->EmitDataRegion(MCDR_DataRegionEnd);
465 void MipsAsmPrinter::EmitBasicBlockEnd(const MachineBasicBlock &MBB) {
466 AsmPrinter::EmitBasicBlockEnd(MBB);
467 MipsTargetStreamer &TS = getTargetStreamer();
468 if (MBB.empty())
469 TS.emitDirectiveInsn();
472 /// isBlockOnlyReachableByFallthough - Return true if the basic block has
473 /// exactly one predecessor and the control transfer mechanism between
474 /// the predecessor and this block is a fall-through.
475 bool MipsAsmPrinter::isBlockOnlyReachableByFallthrough(const MachineBasicBlock*
476 MBB) const {
477 // The predecessor has to be immediately before this block.
478 const MachineBasicBlock *Pred = *MBB->pred_begin();
480 // If the predecessor is a switch statement, assume a jump table
481 // implementation, so it is not a fall through.
482 if (const BasicBlock *bb = Pred->getBasicBlock())
483 if (isa<SwitchInst>(bb->getTerminator()))
484 return false;
486 // If this is a landing pad, it isn't a fall through. If it has no preds,
487 // then nothing falls through to it.
488 if (MBB->isEHPad() || MBB->pred_empty())
489 return false;
491 // If there isn't exactly one predecessor, it can't be a fall through.
492 MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), PI2 = PI;
493 ++PI2;
495 if (PI2 != MBB->pred_end())
496 return false;
498 // The predecessor has to be immediately before this block.
499 if (!Pred->isLayoutSuccessor(MBB))
500 return false;
502 // If the block is completely empty, then it definitely does fall through.
503 if (Pred->empty())
504 return true;
506 // Otherwise, check the last instruction.
507 // Check if the last terminator is an unconditional branch.
508 MachineBasicBlock::const_iterator I = Pred->end();
509 while (I != Pred->begin() && !(--I)->isTerminator()) ;
511 return !I->isBarrier();
514 // Print out an operand for an inline asm expression.
515 bool MipsAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNum,
516 const char *ExtraCode, raw_ostream &O) {
517 // Does this asm operand have a single letter operand modifier?
518 if (ExtraCode && ExtraCode[0]) {
519 if (ExtraCode[1] != 0) return true; // Unknown modifier.
521 const MachineOperand &MO = MI->getOperand(OpNum);
522 switch (ExtraCode[0]) {
523 default:
524 // See if this is a generic print operand
525 return AsmPrinter::PrintAsmOperand(MI, OpNum, ExtraCode, O);
526 case 'X': // hex const int
527 if ((MO.getType()) != MachineOperand::MO_Immediate)
528 return true;
529 O << "0x" << Twine::utohexstr(MO.getImm());
530 return false;
531 case 'x': // hex const int (low 16 bits)
532 if ((MO.getType()) != MachineOperand::MO_Immediate)
533 return true;
534 O << "0x" << Twine::utohexstr(MO.getImm() & 0xffff);
535 return false;
536 case 'd': // decimal const int
537 if ((MO.getType()) != MachineOperand::MO_Immediate)
538 return true;
539 O << MO.getImm();
540 return false;
541 case 'm': // decimal const int minus 1
542 if ((MO.getType()) != MachineOperand::MO_Immediate)
543 return true;
544 O << MO.getImm() - 1;
545 return false;
546 case 'y': // exact log2
547 if ((MO.getType()) != MachineOperand::MO_Immediate)
548 return true;
549 if (!isPowerOf2_64(MO.getImm()))
550 return true;
551 O << Log2_64(MO.getImm());
552 return false;
553 case 'z':
554 // $0 if zero, regular printing otherwise
555 if (MO.getType() == MachineOperand::MO_Immediate && MO.getImm() == 0) {
556 O << "$0";
557 return false;
559 // If not, call printOperand as normal.
560 break;
561 case 'D': // Second part of a double word register operand
562 case 'L': // Low order register of a double word register operand
563 case 'M': // High order register of a double word register operand
565 if (OpNum == 0)
566 return true;
567 const MachineOperand &FlagsOP = MI->getOperand(OpNum - 1);
568 if (!FlagsOP.isImm())
569 return true;
570 unsigned Flags = FlagsOP.getImm();
571 unsigned NumVals = InlineAsm::getNumOperandRegisters(Flags);
572 // Number of registers represented by this operand. We are looking
573 // for 2 for 32 bit mode and 1 for 64 bit mode.
574 if (NumVals != 2) {
575 if (Subtarget->isGP64bit() && NumVals == 1 && MO.isReg()) {
576 Register Reg = MO.getReg();
577 O << '$' << MipsInstPrinter::getRegisterName(Reg);
578 return false;
580 return true;
583 unsigned RegOp = OpNum;
584 if (!Subtarget->isGP64bit()){
585 // Endianness reverses which register holds the high or low value
586 // between M and L.
587 switch(ExtraCode[0]) {
588 case 'M':
589 RegOp = (Subtarget->isLittle()) ? OpNum + 1 : OpNum;
590 break;
591 case 'L':
592 RegOp = (Subtarget->isLittle()) ? OpNum : OpNum + 1;
593 break;
594 case 'D': // Always the second part
595 RegOp = OpNum + 1;
597 if (RegOp >= MI->getNumOperands())
598 return true;
599 const MachineOperand &MO = MI->getOperand(RegOp);
600 if (!MO.isReg())
601 return true;
602 Register Reg = MO.getReg();
603 O << '$' << MipsInstPrinter::getRegisterName(Reg);
604 return false;
606 break;
608 case 'w':
609 // Print MSA registers for the 'f' constraint
610 // In LLVM, the 'w' modifier doesn't need to do anything.
611 // We can just call printOperand as normal.
612 break;
616 printOperand(MI, OpNum, O);
617 return false;
620 bool MipsAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
621 unsigned OpNum,
622 const char *ExtraCode,
623 raw_ostream &O) {
624 assert(OpNum + 1 < MI->getNumOperands() && "Insufficient operands");
625 const MachineOperand &BaseMO = MI->getOperand(OpNum);
626 const MachineOperand &OffsetMO = MI->getOperand(OpNum + 1);
627 assert(BaseMO.isReg() && "Unexpected base pointer for inline asm memory operand.");
628 assert(OffsetMO.isImm() && "Unexpected offset for inline asm memory operand.");
629 int Offset = OffsetMO.getImm();
631 // Currently we are expecting either no ExtraCode or 'D','M','L'.
632 if (ExtraCode) {
633 switch (ExtraCode[0]) {
634 case 'D':
635 Offset += 4;
636 break;
637 case 'M':
638 if (Subtarget->isLittle())
639 Offset += 4;
640 break;
641 case 'L':
642 if (!Subtarget->isLittle())
643 Offset += 4;
644 break;
645 default:
646 return true; // Unknown modifier.
650 O << Offset << "($" << MipsInstPrinter::getRegisterName(BaseMO.getReg())
651 << ")";
653 return false;
656 void MipsAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
657 raw_ostream &O) {
658 const MachineOperand &MO = MI->getOperand(opNum);
659 bool closeP = false;
661 if (MO.getTargetFlags())
662 closeP = true;
664 switch(MO.getTargetFlags()) {
665 case MipsII::MO_GPREL: O << "%gp_rel("; break;
666 case MipsII::MO_GOT_CALL: O << "%call16("; break;
667 case MipsII::MO_GOT: O << "%got("; break;
668 case MipsII::MO_ABS_HI: O << "%hi("; break;
669 case MipsII::MO_ABS_LO: O << "%lo("; break;
670 case MipsII::MO_HIGHER: O << "%higher("; break;
671 case MipsII::MO_HIGHEST: O << "%highest(("; break;
672 case MipsII::MO_TLSGD: O << "%tlsgd("; break;
673 case MipsII::MO_GOTTPREL: O << "%gottprel("; break;
674 case MipsII::MO_TPREL_HI: O << "%tprel_hi("; break;
675 case MipsII::MO_TPREL_LO: O << "%tprel_lo("; break;
676 case MipsII::MO_GPOFF_HI: O << "%hi(%neg(%gp_rel("; break;
677 case MipsII::MO_GPOFF_LO: O << "%lo(%neg(%gp_rel("; break;
678 case MipsII::MO_GOT_DISP: O << "%got_disp("; break;
679 case MipsII::MO_GOT_PAGE: O << "%got_page("; break;
680 case MipsII::MO_GOT_OFST: O << "%got_ofst("; break;
683 switch (MO.getType()) {
684 case MachineOperand::MO_Register:
685 O << '$'
686 << StringRef(MipsInstPrinter::getRegisterName(MO.getReg())).lower();
687 break;
689 case MachineOperand::MO_Immediate:
690 O << MO.getImm();
691 break;
693 case MachineOperand::MO_MachineBasicBlock:
694 MO.getMBB()->getSymbol()->print(O, MAI);
695 return;
697 case MachineOperand::MO_GlobalAddress:
698 PrintSymbolOperand(MO, O);
699 break;
701 case MachineOperand::MO_BlockAddress: {
702 MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());
703 O << BA->getName();
704 break;
707 case MachineOperand::MO_ConstantPoolIndex:
708 O << getDataLayout().getPrivateGlobalPrefix() << "CPI"
709 << getFunctionNumber() << "_" << MO.getIndex();
710 if (MO.getOffset())
711 O << "+" << MO.getOffset();
712 break;
714 default:
715 llvm_unreachable("<unknown operand type>");
718 if (closeP) O << ")";
721 void MipsAsmPrinter::
722 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O) {
723 // Load/Store memory operands -- imm($reg)
724 // If PIC target the target is loaded as the
725 // pattern lw $25,%call16($28)
727 // opNum can be invalid if instruction has reglist as operand.
728 // MemOperand is always last operand of instruction (base + offset).
729 switch (MI->getOpcode()) {
730 default:
731 break;
732 case Mips::SWM32_MM:
733 case Mips::LWM32_MM:
734 opNum = MI->getNumOperands() - 2;
735 break;
738 printOperand(MI, opNum+1, O);
739 O << "(";
740 printOperand(MI, opNum, O);
741 O << ")";
744 void MipsAsmPrinter::
745 printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
746 // when using stack locations for not load/store instructions
747 // print the same way as all normal 3 operand instructions.
748 printOperand(MI, opNum, O);
749 O << ", ";
750 printOperand(MI, opNum+1, O);
753 void MipsAsmPrinter::
754 printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
755 const char *Modifier) {
756 const MachineOperand &MO = MI->getOperand(opNum);
757 O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
760 void MipsAsmPrinter::
761 printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O) {
762 for (int i = opNum, e = MI->getNumOperands(); i != e; ++i) {
763 if (i != opNum) O << ", ";
764 printOperand(MI, i, O);
768 void MipsAsmPrinter::EmitStartOfAsmFile(Module &M) {
769 MipsTargetStreamer &TS = getTargetStreamer();
771 // MipsTargetStreamer has an initialization order problem when emitting an
772 // object file directly (see MipsTargetELFStreamer for full details). Work
773 // around it by re-initializing the PIC state here.
774 TS.setPic(OutContext.getObjectFileInfo()->isPositionIndependent());
776 // Compute MIPS architecture attributes based on the default subtarget
777 // that we'd have constructed. Module level directives aren't LTO
778 // clean anyhow.
779 // FIXME: For ifunc related functions we could iterate over and look
780 // for a feature string that doesn't match the default one.
781 const Triple &TT = TM.getTargetTriple();
782 StringRef CPU = MIPS_MC::selectMipsCPU(TT, TM.getTargetCPU());
783 StringRef FS = TM.getTargetFeatureString();
784 const MipsTargetMachine &MTM = static_cast<const MipsTargetMachine &>(TM);
785 const MipsSubtarget STI(TT, CPU, FS, MTM.isLittleEndian(), MTM, 0);
787 bool IsABICalls = STI.isABICalls();
788 const MipsABIInfo &ABI = MTM.getABI();
789 if (IsABICalls) {
790 TS.emitDirectiveAbiCalls();
791 // FIXME: This condition should be a lot more complicated that it is here.
792 // Ideally it should test for properties of the ABI and not the ABI
793 // itself.
794 // For the moment, I'm only correcting enough to make MIPS-IV work.
795 if (!isPositionIndependent() && STI.hasSym32())
796 TS.emitDirectiveOptionPic0();
799 // Tell the assembler which ABI we are using
800 std::string SectionName = std::string(".mdebug.") + getCurrentABIString();
801 OutStreamer->SwitchSection(
802 OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS, 0));
804 // NaN: At the moment we only support:
805 // 1. .nan legacy (default)
806 // 2. .nan 2008
807 STI.isNaN2008() ? TS.emitDirectiveNaN2008()
808 : TS.emitDirectiveNaNLegacy();
810 // TODO: handle O64 ABI
812 TS.updateABIInfo(STI);
814 // We should always emit a '.module fp=...' but binutils 2.24 does not accept
815 // it. We therefore emit it when it contradicts the ABI defaults (-mfpxx or
816 // -mfp64) and omit it otherwise.
817 if ((ABI.IsO32() && (STI.isABI_FPXX() || STI.isFP64bit())) ||
818 STI.useSoftFloat())
819 TS.emitDirectiveModuleFP();
821 // We should always emit a '.module [no]oddspreg' but binutils 2.24 does not
822 // accept it. We therefore emit it when it contradicts the default or an
823 // option has changed the default (i.e. FPXX) and omit it otherwise.
824 if (ABI.IsO32() && (!STI.useOddSPReg() || STI.isABI_FPXX()))
825 TS.emitDirectiveModuleOddSPReg();
827 // Switch to the .text section.
828 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
831 void MipsAsmPrinter::emitInlineAsmStart() const {
832 MipsTargetStreamer &TS = getTargetStreamer();
834 // GCC's choice of assembler options for inline assembly code ('at', 'macro'
835 // and 'reorder') is different from LLVM's choice for generated code ('noat',
836 // 'nomacro' and 'noreorder').
837 // In order to maintain compatibility with inline assembly code which depends
838 // on GCC's assembler options being used, we have to switch to those options
839 // for the duration of the inline assembly block and then switch back.
840 TS.emitDirectiveSetPush();
841 TS.emitDirectiveSetAt();
842 TS.emitDirectiveSetMacro();
843 TS.emitDirectiveSetReorder();
844 OutStreamer->AddBlankLine();
847 void MipsAsmPrinter::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
848 const MCSubtargetInfo *EndInfo) const {
849 OutStreamer->AddBlankLine();
850 getTargetStreamer().emitDirectiveSetPop();
853 void MipsAsmPrinter::EmitJal(const MCSubtargetInfo &STI, MCSymbol *Symbol) {
854 MCInst I;
855 I.setOpcode(Mips::JAL);
856 I.addOperand(
857 MCOperand::createExpr(MCSymbolRefExpr::create(Symbol, OutContext)));
858 OutStreamer->EmitInstruction(I, STI);
861 void MipsAsmPrinter::EmitInstrReg(const MCSubtargetInfo &STI, unsigned Opcode,
862 unsigned Reg) {
863 MCInst I;
864 I.setOpcode(Opcode);
865 I.addOperand(MCOperand::createReg(Reg));
866 OutStreamer->EmitInstruction(I, STI);
869 void MipsAsmPrinter::EmitInstrRegReg(const MCSubtargetInfo &STI,
870 unsigned Opcode, unsigned Reg1,
871 unsigned Reg2) {
872 MCInst I;
874 // Because of the current td files for Mips32, the operands for MTC1
875 // appear backwards from their normal assembly order. It's not a trivial
876 // change to fix this in the td file so we adjust for it here.
878 if (Opcode == Mips::MTC1) {
879 unsigned Temp = Reg1;
880 Reg1 = Reg2;
881 Reg2 = Temp;
883 I.setOpcode(Opcode);
884 I.addOperand(MCOperand::createReg(Reg1));
885 I.addOperand(MCOperand::createReg(Reg2));
886 OutStreamer->EmitInstruction(I, STI);
889 void MipsAsmPrinter::EmitInstrRegRegReg(const MCSubtargetInfo &STI,
890 unsigned Opcode, unsigned Reg1,
891 unsigned Reg2, unsigned Reg3) {
892 MCInst I;
893 I.setOpcode(Opcode);
894 I.addOperand(MCOperand::createReg(Reg1));
895 I.addOperand(MCOperand::createReg(Reg2));
896 I.addOperand(MCOperand::createReg(Reg3));
897 OutStreamer->EmitInstruction(I, STI);
900 void MipsAsmPrinter::EmitMovFPIntPair(const MCSubtargetInfo &STI,
901 unsigned MovOpc, unsigned Reg1,
902 unsigned Reg2, unsigned FPReg1,
903 unsigned FPReg2, bool LE) {
904 if (!LE) {
905 unsigned temp = Reg1;
906 Reg1 = Reg2;
907 Reg2 = temp;
909 EmitInstrRegReg(STI, MovOpc, Reg1, FPReg1);
910 EmitInstrRegReg(STI, MovOpc, Reg2, FPReg2);
913 void MipsAsmPrinter::EmitSwapFPIntParams(const MCSubtargetInfo &STI,
914 Mips16HardFloatInfo::FPParamVariant PV,
915 bool LE, bool ToFP) {
916 using namespace Mips16HardFloatInfo;
918 unsigned MovOpc = ToFP ? Mips::MTC1 : Mips::MFC1;
919 switch (PV) {
920 case FSig:
921 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
922 break;
923 case FFSig:
924 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F14, LE);
925 break;
926 case FDSig:
927 EmitInstrRegReg(STI, MovOpc, Mips::A0, Mips::F12);
928 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
929 break;
930 case DSig:
931 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
932 break;
933 case DDSig:
934 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
935 EmitMovFPIntPair(STI, MovOpc, Mips::A2, Mips::A3, Mips::F14, Mips::F15, LE);
936 break;
937 case DFSig:
938 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F12, Mips::F13, LE);
939 EmitInstrRegReg(STI, MovOpc, Mips::A2, Mips::F14);
940 break;
941 case NoSig:
942 return;
946 void MipsAsmPrinter::EmitSwapFPIntRetval(
947 const MCSubtargetInfo &STI, Mips16HardFloatInfo::FPReturnVariant RV,
948 bool LE) {
949 using namespace Mips16HardFloatInfo;
951 unsigned MovOpc = Mips::MFC1;
952 switch (RV) {
953 case FRet:
954 EmitInstrRegReg(STI, MovOpc, Mips::V0, Mips::F0);
955 break;
956 case DRet:
957 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
958 break;
959 case CFRet:
960 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
961 break;
962 case CDRet:
963 EmitMovFPIntPair(STI, MovOpc, Mips::V0, Mips::V1, Mips::F0, Mips::F1, LE);
964 EmitMovFPIntPair(STI, MovOpc, Mips::A0, Mips::A1, Mips::F2, Mips::F3, LE);
965 break;
966 case NoFPRet:
967 break;
971 void MipsAsmPrinter::EmitFPCallStub(
972 const char *Symbol, const Mips16HardFloatInfo::FuncSignature *Signature) {
973 using namespace Mips16HardFloatInfo;
975 MCSymbol *MSymbol = OutContext.getOrCreateSymbol(StringRef(Symbol));
976 bool LE = getDataLayout().isLittleEndian();
977 // Construct a local MCSubtargetInfo here.
978 // This is because the MachineFunction won't exist (but have not yet been
979 // freed) and since we're at the global level we can use the default
980 // constructed subtarget.
981 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
982 TM.getTargetTriple().str(), TM.getTargetCPU(),
983 TM.getTargetFeatureString()));
986 // .global xxxx
988 OutStreamer->EmitSymbolAttribute(MSymbol, MCSA_Global);
989 const char *RetType;
991 // make the comment field identifying the return and parameter
992 // types of the floating point stub
993 // # Stub function to call rettype xxxx (params)
995 switch (Signature->RetSig) {
996 case FRet:
997 RetType = "float";
998 break;
999 case DRet:
1000 RetType = "double";
1001 break;
1002 case CFRet:
1003 RetType = "complex";
1004 break;
1005 case CDRet:
1006 RetType = "double complex";
1007 break;
1008 case NoFPRet:
1009 RetType = "";
1010 break;
1012 const char *Parms;
1013 switch (Signature->ParamSig) {
1014 case FSig:
1015 Parms = "float";
1016 break;
1017 case FFSig:
1018 Parms = "float, float";
1019 break;
1020 case FDSig:
1021 Parms = "float, double";
1022 break;
1023 case DSig:
1024 Parms = "double";
1025 break;
1026 case DDSig:
1027 Parms = "double, double";
1028 break;
1029 case DFSig:
1030 Parms = "double, float";
1031 break;
1032 case NoSig:
1033 Parms = "";
1034 break;
1036 OutStreamer->AddComment("\t# Stub function to call " + Twine(RetType) + " " +
1037 Twine(Symbol) + " (" + Twine(Parms) + ")");
1039 // probably not necessary but we save and restore the current section state
1041 OutStreamer->PushSection();
1043 // .section mips16.call.fpxxxx,"ax",@progbits
1045 MCSectionELF *M = OutContext.getELFSection(
1046 ".mips16.call.fp." + std::string(Symbol), ELF::SHT_PROGBITS,
1047 ELF::SHF_ALLOC | ELF::SHF_EXECINSTR);
1048 OutStreamer->SwitchSection(M, nullptr);
1050 // .align 2
1052 OutStreamer->EmitValueToAlignment(4);
1053 MipsTargetStreamer &TS = getTargetStreamer();
1055 // .set nomips16
1056 // .set nomicromips
1058 TS.emitDirectiveSetNoMips16();
1059 TS.emitDirectiveSetNoMicroMips();
1061 // .ent __call_stub_fp_xxxx
1062 // .type __call_stub_fp_xxxx,@function
1063 // __call_stub_fp_xxxx:
1065 std::string x = "__call_stub_fp_" + std::string(Symbol);
1066 MCSymbolELF *Stub =
1067 cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
1068 TS.emitDirectiveEnt(*Stub);
1069 MCSymbol *MType =
1070 OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
1071 OutStreamer->EmitSymbolAttribute(MType, MCSA_ELF_TypeFunction);
1072 OutStreamer->EmitLabel(Stub);
1074 // Only handle non-pic for now.
1075 assert(!isPositionIndependent() &&
1076 "should not be here if we are compiling pic");
1077 TS.emitDirectiveSetReorder();
1079 // We need to add a MipsMCExpr class to MCTargetDesc to fully implement
1080 // stubs without raw text but this current patch is for compiler generated
1081 // functions and they all return some value.
1082 // The calling sequence for non pic is different in that case and we need
1083 // to implement %lo and %hi in order to handle the case of no return value
1084 // See the corresponding method in Mips16HardFloat for details.
1086 // mov the return address to S2.
1087 // we have no stack space to store it and we are about to make another call.
1088 // We need to make sure that the enclosing function knows to save S2
1089 // This should have already been handled.
1091 // Mov $18, $31
1093 EmitInstrRegRegReg(*STI, Mips::OR, Mips::S2, Mips::RA, Mips::ZERO);
1095 EmitSwapFPIntParams(*STI, Signature->ParamSig, LE, true);
1097 // Jal xxxx
1099 EmitJal(*STI, MSymbol);
1101 // fix return values
1102 EmitSwapFPIntRetval(*STI, Signature->RetSig, LE);
1104 // do the return
1105 // if (Signature->RetSig == NoFPRet)
1106 // llvm_unreachable("should not be any stubs here with no return value");
1107 // else
1108 EmitInstrReg(*STI, Mips::JR, Mips::S2);
1110 MCSymbol *Tmp = OutContext.createTempSymbol();
1111 OutStreamer->EmitLabel(Tmp);
1112 const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
1113 const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
1114 const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
1115 OutStreamer->emitELFSize(Stub, T_min_E);
1116 TS.emitDirectiveEnd(x);
1117 OutStreamer->PopSection();
1120 void MipsAsmPrinter::EmitEndOfAsmFile(Module &M) {
1121 // Emit needed stubs
1123 for (std::map<
1124 const char *,
1125 const Mips16HardFloatInfo::FuncSignature *>::const_iterator
1126 it = StubsNeeded.begin();
1127 it != StubsNeeded.end(); ++it) {
1128 const char *Symbol = it->first;
1129 const Mips16HardFloatInfo::FuncSignature *Signature = it->second;
1130 EmitFPCallStub(Symbol, Signature);
1132 // return to the text section
1133 OutStreamer->SwitchSection(OutContext.getObjectFileInfo()->getTextSection());
1136 void MipsAsmPrinter::EmitSled(const MachineInstr &MI, SledKind Kind) {
1137 const uint8_t NoopsInSledCount = Subtarget->isGP64bit() ? 15 : 11;
1138 // For mips32 we want to emit the following pattern:
1140 // .Lxray_sled_N:
1141 // ALIGN
1142 // B .tmpN
1143 // 11 NOP instructions (44 bytes)
1144 // ADDIU T9, T9, 52
1145 // .tmpN
1147 // We need the 44 bytes (11 instructions) because at runtime, we'd
1148 // be patching over the full 48 bytes (12 instructions) with the following
1149 // pattern:
1151 // ADDIU SP, SP, -8
1152 // NOP
1153 // SW RA, 4(SP)
1154 // SW T9, 0(SP)
1155 // LUI T9, %hi(__xray_FunctionEntry/Exit)
1156 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1157 // LUI T0, %hi(function_id)
1158 // JALR T9
1159 // ORI T0, T0, %lo(function_id)
1160 // LW T9, 0(SP)
1161 // LW RA, 4(SP)
1162 // ADDIU SP, SP, 8
1164 // We add 52 bytes to t9 because we want to adjust the function pointer to
1165 // the actual start of function i.e. the address just after the noop sled.
1166 // We do this because gp displacement relocation is emitted at the start of
1167 // of the function i.e after the nop sled and to correctly calculate the
1168 // global offset table address, t9 must hold the address of the instruction
1169 // containing the gp displacement relocation.
1170 // FIXME: Is this correct for the static relocation model?
1172 // For mips64 we want to emit the following pattern:
1174 // .Lxray_sled_N:
1175 // ALIGN
1176 // B .tmpN
1177 // 15 NOP instructions (60 bytes)
1178 // .tmpN
1180 // We need the 60 bytes (15 instructions) because at runtime, we'd
1181 // be patching over the full 64 bytes (16 instructions) with the following
1182 // pattern:
1184 // DADDIU SP, SP, -16
1185 // NOP
1186 // SD RA, 8(SP)
1187 // SD T9, 0(SP)
1188 // LUI T9, %highest(__xray_FunctionEntry/Exit)
1189 // ORI T9, T9, %higher(__xray_FunctionEntry/Exit)
1190 // DSLL T9, T9, 16
1191 // ORI T9, T9, %hi(__xray_FunctionEntry/Exit)
1192 // DSLL T9, T9, 16
1193 // ORI T9, T9, %lo(__xray_FunctionEntry/Exit)
1194 // LUI T0, %hi(function_id)
1195 // JALR T9
1196 // ADDIU T0, T0, %lo(function_id)
1197 // LD T9, 0(SP)
1198 // LD RA, 8(SP)
1199 // DADDIU SP, SP, 16
1201 OutStreamer->EmitCodeAlignment(4);
1202 auto CurSled = OutContext.createTempSymbol("xray_sled_", true);
1203 OutStreamer->EmitLabel(CurSled);
1204 auto Target = OutContext.createTempSymbol();
1206 // Emit "B .tmpN" instruction, which jumps over the nop sled to the actual
1207 // start of function
1208 const MCExpr *TargetExpr = MCSymbolRefExpr::create(
1209 Target, MCSymbolRefExpr::VariantKind::VK_None, OutContext);
1210 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::BEQ)
1211 .addReg(Mips::ZERO)
1212 .addReg(Mips::ZERO)
1213 .addExpr(TargetExpr));
1215 for (int8_t I = 0; I < NoopsInSledCount; I++)
1216 EmitToStreamer(*OutStreamer, MCInstBuilder(Mips::SLL)
1217 .addReg(Mips::ZERO)
1218 .addReg(Mips::ZERO)
1219 .addImm(0));
1221 OutStreamer->EmitLabel(Target);
1223 if (!Subtarget->isGP64bit()) {
1224 EmitToStreamer(*OutStreamer,
1225 MCInstBuilder(Mips::ADDiu)
1226 .addReg(Mips::T9)
1227 .addReg(Mips::T9)
1228 .addImm(0x34));
1231 recordSled(CurSled, MI, Kind);
1234 void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI) {
1235 EmitSled(MI, SledKind::FUNCTION_ENTER);
1238 void MipsAsmPrinter::LowerPATCHABLE_FUNCTION_EXIT(const MachineInstr &MI) {
1239 EmitSled(MI, SledKind::FUNCTION_EXIT);
1242 void MipsAsmPrinter::LowerPATCHABLE_TAIL_CALL(const MachineInstr &MI) {
1243 EmitSled(MI, SledKind::TAIL_CALL);
1246 void MipsAsmPrinter::PrintDebugValueComment(const MachineInstr *MI,
1247 raw_ostream &OS) {
1248 // TODO: implement
1251 // Emit .dtprelword or .dtpreldword directive
1252 // and value for debug thread local expression.
1253 void MipsAsmPrinter::EmitDebugValue(const MCExpr *Value, unsigned Size) const {
1254 if (auto *MipsExpr = dyn_cast<MipsMCExpr>(Value)) {
1255 if (MipsExpr && MipsExpr->getKind() == MipsMCExpr::MEK_DTPREL) {
1256 switch (Size) {
1257 case 4:
1258 OutStreamer->EmitDTPRel32Value(MipsExpr->getSubExpr());
1259 break;
1260 case 8:
1261 OutStreamer->EmitDTPRel64Value(MipsExpr->getSubExpr());
1262 break;
1263 default:
1264 llvm_unreachable("Unexpected size of expression value.");
1266 return;
1269 AsmPrinter::EmitDebugValue(Value, Size);
1272 // Align all targets of indirect branches on bundle size. Used only if target
1273 // is NaCl.
1274 void MipsAsmPrinter::NaClAlignIndirectJumpTargets(MachineFunction &MF) {
1275 // Align all blocks that are jumped to through jump table.
1276 if (MachineJumpTableInfo *JtInfo = MF.getJumpTableInfo()) {
1277 const std::vector<MachineJumpTableEntry> &JT = JtInfo->getJumpTables();
1278 for (unsigned I = 0; I < JT.size(); ++I) {
1279 const std::vector<MachineBasicBlock*> &MBBs = JT[I].MBBs;
1281 for (unsigned J = 0; J < MBBs.size(); ++J)
1282 MBBs[J]->setLogAlignment(MIPS_NACL_BUNDLE_LOG_ALIGN);
1286 // If basic block address is taken, block can be target of indirect branch.
1287 for (auto &MBB : MF) {
1288 if (MBB.hasAddressTaken())
1289 MBB.setLogAlignment(MIPS_NACL_BUNDLE_LOG_ALIGN);
1293 bool MipsAsmPrinter::isLongBranchPseudo(int Opcode) const {
1294 return (Opcode == Mips::LONG_BRANCH_LUi
1295 || Opcode == Mips::LONG_BRANCH_LUi2Op
1296 || Opcode == Mips::LONG_BRANCH_LUi2Op_64
1297 || Opcode == Mips::LONG_BRANCH_ADDiu
1298 || Opcode == Mips::LONG_BRANCH_ADDiu2Op
1299 || Opcode == Mips::LONG_BRANCH_DADDiu
1300 || Opcode == Mips::LONG_BRANCH_DADDiu2Op);
1303 // Force static initialization.
1304 extern "C" void LLVMInitializeMipsAsmPrinter() {
1305 RegisterAsmPrinter<MipsAsmPrinter> X(getTheMipsTarget());
1306 RegisterAsmPrinter<MipsAsmPrinter> Y(getTheMipselTarget());
1307 RegisterAsmPrinter<MipsAsmPrinter> A(getTheMips64Target());
1308 RegisterAsmPrinter<MipsAsmPrinter> B(getTheMips64elTarget());