Path: blob/main/contrib/llvm-project/llvm/lib/Target/Lanai/LanaiAsmPrinter.cpp
35271 views
//===-- LanaiAsmPrinter.cpp - Lanai LLVM assembly writer ------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains a printer that converts from our internal representation9// of machine-dependent LLVM code to the Lanai assembly language.10//11//===----------------------------------------------------------------------===//1213#include "LanaiAluCode.h"14#include "LanaiCondCode.h"15#include "LanaiInstrInfo.h"16#include "LanaiMCInstLower.h"17#include "LanaiTargetMachine.h"18#include "MCTargetDesc/LanaiInstPrinter.h"19#include "TargetInfo/LanaiTargetInfo.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/MC/TargetRegistry.h"35#include "llvm/Support/raw_ostream.h"3637#define DEBUG_TYPE "asm-printer"3839using namespace llvm;4041namespace {42class LanaiAsmPrinter : public AsmPrinter {43public:44explicit LanaiAsmPrinter(TargetMachine &TM,45std::unique_ptr<MCStreamer> Streamer)46: AsmPrinter(TM, std::move(Streamer)) {}4748StringRef getPassName() const override { return "Lanai Assembly Printer"; }4950void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);51bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,52const char *ExtraCode, raw_ostream &O) override;53void emitInstruction(const MachineInstr *MI) override;54bool isBlockOnlyReachableByFallthrough(55const MachineBasicBlock *MBB) const override;5657private:58void customEmitInstruction(const MachineInstr *MI);59void emitCallInstruction(const MachineInstr *MI);60};61} // end of anonymous namespace6263void LanaiAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,64raw_ostream &O) {65const MachineOperand &MO = MI->getOperand(OpNum);6667switch (MO.getType()) {68case MachineOperand::MO_Register:69O << LanaiInstPrinter::getRegisterName(MO.getReg());70break;7172case MachineOperand::MO_Immediate:73O << MO.getImm();74break;7576case MachineOperand::MO_MachineBasicBlock:77O << *MO.getMBB()->getSymbol();78break;7980case MachineOperand::MO_GlobalAddress:81O << *getSymbol(MO.getGlobal());82break;8384case MachineOperand::MO_BlockAddress: {85MCSymbol *BA = GetBlockAddressSymbol(MO.getBlockAddress());86O << BA->getName();87break;88}8990case MachineOperand::MO_ExternalSymbol:91O << *GetExternalSymbolSymbol(MO.getSymbolName());92break;9394case MachineOperand::MO_JumpTableIndex:95O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'96<< MO.getIndex();97break;9899case MachineOperand::MO_ConstantPoolIndex:100O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'101<< MO.getIndex();102return;103104default:105llvm_unreachable("<unknown operand type>");106}107}108109// PrintAsmOperand - Print out an operand for an inline asm expression.110bool LanaiAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,111const char *ExtraCode, raw_ostream &O) {112// Does this asm operand have a single letter operand modifier?113if (ExtraCode && ExtraCode[0]) {114if (ExtraCode[1])115return true; // Unknown modifier.116117switch (ExtraCode[0]) {118// The highest-numbered register of a pair.119case 'H': {120if (OpNo == 0)121return true;122const MachineOperand &FlagsOP = MI->getOperand(OpNo - 1);123if (!FlagsOP.isImm())124return true;125const InlineAsm::Flag Flags(FlagsOP.getImm());126const unsigned NumVals = Flags.getNumOperandRegisters();127if (NumVals != 2)128return true;129unsigned RegOp = OpNo + 1;130if (RegOp >= MI->getNumOperands())131return true;132const MachineOperand &MO = MI->getOperand(RegOp);133if (!MO.isReg())134return true;135Register Reg = MO.getReg();136O << LanaiInstPrinter::getRegisterName(Reg);137return false;138}139default:140return AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O);141}142}143printOperand(MI, OpNo, O);144return false;145}146147//===----------------------------------------------------------------------===//148void LanaiAsmPrinter::emitCallInstruction(const MachineInstr *MI) {149assert((MI->getOpcode() == Lanai::CALL || MI->getOpcode() == Lanai::CALLR) &&150"Unsupported call function");151152LanaiMCInstLower MCInstLowering(OutContext, *this);153MCSubtargetInfo STI = getSubtargetInfo();154// Insert save rca instruction immediately before the call.155// TODO: We should generate a pc-relative mov instruction here instead156// of pc + 16 (should be mov .+16 %rca).157OutStreamer->emitInstruction(MCInstBuilder(Lanai::ADD_I_LO)158.addReg(Lanai::RCA)159.addReg(Lanai::PC)160.addImm(16),161STI);162163// Push rca onto the stack.164// st %rca, [--%sp]165OutStreamer->emitInstruction(MCInstBuilder(Lanai::SW_RI)166.addReg(Lanai::RCA)167.addReg(Lanai::SP)168.addImm(-4)169.addImm(LPAC::makePreOp(LPAC::ADD)),170STI);171172// Lower the call instruction.173if (MI->getOpcode() == Lanai::CALL) {174MCInst TmpInst;175MCInstLowering.Lower(MI, TmpInst);176TmpInst.setOpcode(Lanai::BT);177OutStreamer->emitInstruction(TmpInst, STI);178} else {179OutStreamer->emitInstruction(MCInstBuilder(Lanai::ADD_R)180.addReg(Lanai::PC)181.addReg(MI->getOperand(0).getReg())182.addReg(Lanai::R0)183.addImm(LPCC::ICC_T),184STI);185}186}187188void LanaiAsmPrinter::customEmitInstruction(const MachineInstr *MI) {189LanaiMCInstLower MCInstLowering(OutContext, *this);190MCSubtargetInfo STI = getSubtargetInfo();191MCInst TmpInst;192MCInstLowering.Lower(MI, TmpInst);193OutStreamer->emitInstruction(TmpInst, STI);194}195196void LanaiAsmPrinter::emitInstruction(const MachineInstr *MI) {197Lanai_MC::verifyInstructionPredicates(MI->getOpcode(),198getSubtargetInfo().getFeatureBits());199200MachineBasicBlock::const_instr_iterator I = MI->getIterator();201MachineBasicBlock::const_instr_iterator E = MI->getParent()->instr_end();202203do {204if (I->isCall()) {205emitCallInstruction(&*I);206continue;207}208209customEmitInstruction(&*I);210} while ((++I != E) && I->isInsideBundle());211}212213// isBlockOnlyReachableByFallthough - Return true if the basic block has214// exactly one predecessor and the control transfer mechanism between215// the predecessor and this block is a fall-through.216// FIXME: could the overridden cases be handled in analyzeBranch?217bool LanaiAsmPrinter::isBlockOnlyReachableByFallthrough(218const MachineBasicBlock *MBB) const {219// The predecessor has to be immediately before this block.220const MachineBasicBlock *Pred = *MBB->pred_begin();221222// If the predecessor is a switch statement, assume a jump table223// implementation, so it is not a fall through.224if (const BasicBlock *B = Pred->getBasicBlock())225if (isa<SwitchInst>(B->getTerminator()))226return false;227228// Check default implementation229if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))230return false;231232// Otherwise, check the last instruction.233// Check if the last terminator is an unconditional branch.234MachineBasicBlock::const_iterator I = Pred->end();235while (I != Pred->begin() && !(--I)->isTerminator()) {236}237238return !I->isBarrier();239}240241// Force static initialization.242extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeLanaiAsmPrinter() {243RegisterAsmPrinter<LanaiAsmPrinter> X(getTheLanaiTarget());244}245246247