2012-02-18 12:03:15 +00:00
|
|
|
//===-- HexagonAsmPrinter.cpp - Print machine instrs to Hexagon assembly --===//
|
2011-12-12 21:14:40 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to Hexagon assembly language. This printer is
|
|
|
|
// the output mechanism used by `llc'.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "Hexagon.h"
|
2013-02-20 16:13:27 +00:00
|
|
|
#include "HexagonAsmPrinter.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "HexagonMachineFunctionInfo.h"
|
|
|
|
#include "HexagonTargetMachine.h"
|
2013-02-20 16:13:27 +00:00
|
|
|
#include "HexagonSubtarget.h"
|
|
|
|
#include "MCTargetDesc/HexagonMCInst.h"
|
2012-04-12 17:55:53 +00:00
|
|
|
#include "InstPrinter/HexagonInstPrinter.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2012-04-12 17:55:53 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-04-12 17:55:53 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-04-12 17:55:53 +00:00
|
|
|
#include "llvm/Support/Format.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-03-17 18:46:09 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
#include "llvm/Target/Mangler.h"
|
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
#include "llvm/Target/TargetOptions.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2011-12-12 21:14:40 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<bool> AlignCalls(
|
|
|
|
"hexagon-align-calls", cl::Hidden, cl::init(true),
|
|
|
|
cl::desc("Insert falign after call instruction for Hexagon target"));
|
|
|
|
|
|
|
|
void HexagonAsmPrinter::EmitAlignment(unsigned NumBits,
|
|
|
|
const GlobalValue *GV) const {
|
2012-04-12 17:55:53 +00:00
|
|
|
// For basic block level alignment, use ".falign".
|
2011-12-12 21:14:40 +00:00
|
|
|
if (!GV) {
|
|
|
|
OutStreamer.EmitRawText(StringRef("\t.falign"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmPrinter::EmitAlignment(NumBits, GV);
|
|
|
|
}
|
|
|
|
|
2012-04-12 17:55:53 +00:00
|
|
|
void HexagonAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
|
2011-12-12 21:14:40 +00:00
|
|
|
switch (MO.getType()) {
|
2012-05-03 21:52:53 +00:00
|
|
|
default: llvm_unreachable ("<unknown operand type>");
|
2012-04-12 17:55:53 +00:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
O << HexagonInstPrinter::getRegisterName(MO.getReg());
|
|
|
|
return;
|
2011-12-12 21:14:40 +00:00
|
|
|
case MachineOperand::MO_Immediate:
|
2012-04-12 17:55:53 +00:00
|
|
|
O << MO.getImm();
|
|
|
|
return;
|
2011-12-12 21:14:40 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
O << *MO.getMBB()->getSymbol();
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
O << *GetJTISymbol(MO.getIndex());
|
|
|
|
// FIXME: PIC relocation model.
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
O << *GetCPISymbol(MO.getIndex());
|
|
|
|
return;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << *GetExternalSymbolSymbol(MO.getSymbolName());
|
|
|
|
return;
|
2012-04-12 17:55:53 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2011-12-12 21:14:40 +00:00
|
|
|
// Computing the address of a global symbol, not calling it.
|
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
|
|
|
printOffset(MO.getOffset(), O);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// isBlockOnlyReachableByFallthrough - We need to override this since the
|
|
|
|
// default AsmPrinter does not print labels for any basic block that
|
|
|
|
// is only reachable by a fall through. That works for all cases except
|
|
|
|
// for the case in which the basic block is reachable by a fall through but
|
|
|
|
// through an indirect from a jump table. In this case, the jump table
|
|
|
|
// will contain a label not defined by AsmPrinter.
|
|
|
|
//
|
|
|
|
bool HexagonAsmPrinter::
|
|
|
|
isBlockOnlyReachableByFallthrough(const MachineBasicBlock *MBB) const {
|
|
|
|
if (MBB->hasAddressTaken()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return AsmPrinter::isBlockOnlyReachableByFallthrough(MBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool HexagonAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
2012-04-12 17:55:53 +00:00
|
|
|
raw_ostream &OS) {
|
2011-12-12 21:14:40 +00:00
|
|
|
// Does this asm operand have a single letter operand modifier?
|
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
if (ExtraCode[1] != 0) return true; // Unknown modifier.
|
|
|
|
|
|
|
|
switch (ExtraCode[0]) {
|
2012-06-26 13:49:27 +00:00
|
|
|
default:
|
|
|
|
// See if this is a generic print operand
|
|
|
|
return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, OS);
|
2011-12-12 21:14:40 +00:00
|
|
|
case 'c': // Don't print "$" before a global var name or constant.
|
|
|
|
// Hexagon never has a prefix.
|
|
|
|
printOperand(MI, OpNo, OS);
|
|
|
|
return false;
|
|
|
|
case 'L': // Write second word of DImode reference.
|
|
|
|
// Verify that this operand has two consecutive registers.
|
|
|
|
if (!MI->getOperand(OpNo).isReg() ||
|
|
|
|
OpNo+1 == MI->getNumOperands() ||
|
|
|
|
!MI->getOperand(OpNo+1).isReg())
|
|
|
|
return true;
|
|
|
|
++OpNo; // Return the high-part.
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
// Write 'i' if an integer constant, otherwise nothing. Used to print
|
|
|
|
// addi vs add, etc.
|
|
|
|
if (MI->getOperand(OpNo).isImm())
|
|
|
|
OS << "i";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printOperand(MI, OpNo, OS);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HexagonAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo, unsigned AsmVariant,
|
|
|
|
const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
|
|
|
|
|
|
|
const MachineOperand &Base = MI->getOperand(OpNo);
|
|
|
|
const MachineOperand &Offset = MI->getOperand(OpNo+1);
|
|
|
|
|
|
|
|
if (Base.isReg())
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
else
|
2012-02-07 02:50:20 +00:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-12 21:14:40 +00:00
|
|
|
|
|
|
|
if (Offset.isImm()) {
|
|
|
|
if (Offset.getImm())
|
|
|
|
O << " + #" << Offset.getImm();
|
|
|
|
}
|
|
|
|
else
|
2012-02-07 02:50:20 +00:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-12 21:14:40 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonAsmPrinter::printPredicateOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo,
|
|
|
|
raw_ostream &O) {
|
2012-02-07 02:50:20 +00:00
|
|
|
llvm_unreachable("Unimplemented");
|
2011-12-12 21:14:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// printMachineInstruction -- Print out a single Hexagon MI in Darwin syntax to
|
|
|
|
/// the current output stream.
|
|
|
|
///
|
|
|
|
void HexagonAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2012-05-03 21:52:53 +00:00
|
|
|
if (MI->isBundle()) {
|
|
|
|
std::vector<const MachineInstr*> BundleMIs;
|
|
|
|
|
|
|
|
const MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
MachineBasicBlock::const_instr_iterator MII = MI;
|
|
|
|
++MII;
|
|
|
|
unsigned int IgnoreCount = 0;
|
|
|
|
while (MII != MBB->end() && MII->isInsideBundle()) {
|
|
|
|
const MachineInstr *MInst = MII;
|
|
|
|
if (MInst->getOpcode() == TargetOpcode::DBG_VALUE ||
|
|
|
|
MInst->getOpcode() == TargetOpcode::IMPLICIT_DEF) {
|
|
|
|
IgnoreCount++;
|
|
|
|
++MII;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//BundleMIs.push_back(&*MII);
|
|
|
|
BundleMIs.push_back(MInst);
|
|
|
|
++MII;
|
|
|
|
}
|
|
|
|
unsigned Size = BundleMIs.size();
|
|
|
|
assert((Size+IgnoreCount) == MI->getBundleSize() && "Corrupt Bundle!");
|
|
|
|
for (unsigned Index = 0; Index < Size; Index++) {
|
|
|
|
HexagonMCInst MCI;
|
2013-02-20 16:13:27 +00:00
|
|
|
MCI.setPacketStart(Index == 0);
|
|
|
|
MCI.setPacketEnd(Index == (Size-1));
|
2012-05-03 21:52:53 +00:00
|
|
|
|
|
|
|
HexagonLowerToMC(BundleMIs[Index], MCI, *this);
|
|
|
|
OutStreamer.EmitInstruction(MCI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
HexagonMCInst MCI;
|
|
|
|
if (MI->getOpcode() == Hexagon::ENDLOOP0) {
|
2013-02-20 16:13:27 +00:00
|
|
|
MCI.setPacketStart(true);
|
|
|
|
MCI.setPacketEnd(true);
|
2012-05-03 21:52:53 +00:00
|
|
|
}
|
|
|
|
HexagonLowerToMC(MI, MCI, *this);
|
|
|
|
OutStreamer.EmitInstruction(MCI);
|
|
|
|
}
|
2011-12-12 21:14:40 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintUnmangledNameSafely - Print out the printable characters in the name.
|
|
|
|
/// Don't print things like \n or \0.
|
|
|
|
// static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
|
|
|
|
// for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
|
|
|
|
// Name != E; ++Name)
|
|
|
|
// if (isprint(*Name))
|
|
|
|
// OS << *Name;
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
void HexagonAsmPrinter::printAddrModeBasePlusOffset(const MachineInstr *MI,
|
|
|
|
int OpNo, raw_ostream &O) {
|
|
|
|
const MachineOperand &MO1 = MI->getOperand(OpNo);
|
|
|
|
const MachineOperand &MO2 = MI->getOperand(OpNo+1);
|
|
|
|
|
2012-04-12 17:55:53 +00:00
|
|
|
O << HexagonInstPrinter::getRegisterName(MO1.getReg())
|
2011-12-12 21:14:40 +00:00
|
|
|
<< " + #"
|
|
|
|
<< MO2.getImm();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HexagonAsmPrinter::printGlobalOperand(const MachineInstr *MI, int OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
assert( (MO.getType() == MachineOperand::MO_GlobalAddress) &&
|
|
|
|
"Expecting global address");
|
|
|
|
|
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
|
|
|
if (MO.getOffset() != 0) {
|
|
|
|
O << " + ";
|
|
|
|
O << MO.getOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonAsmPrinter::printJumpTable(const MachineInstr *MI, int OpNo,
|
|
|
|
raw_ostream &O) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
2012-05-03 21:52:53 +00:00
|
|
|
assert( (MO.getType() == MachineOperand::MO_JumpTableIndex) &&
|
|
|
|
"Expecting jump table index");
|
2011-12-12 21:14:40 +00:00
|
|
|
|
|
|
|
// Hexagon_TODO: Do we need name mangling?
|
|
|
|
O << *GetJTISymbol(MO.getIndex());
|
|
|
|
}
|
|
|
|
|
2012-04-12 17:55:53 +00:00
|
|
|
void HexagonAsmPrinter::printConstantPool(const MachineInstr *MI, int OpNo,
|
2012-05-03 21:52:53 +00:00
|
|
|
raw_ostream &O) {
|
2012-04-12 17:55:53 +00:00
|
|
|
const MachineOperand &MO = MI->getOperand(OpNo);
|
|
|
|
assert( (MO.getType() == MachineOperand::MO_ConstantPoolIndex) &&
|
2012-04-23 18:25:57 +00:00
|
|
|
"Expecting constant pool index");
|
2012-04-12 17:55:53 +00:00
|
|
|
|
|
|
|
// Hexagon_TODO: Do we need name mangling?
|
|
|
|
O << *GetCPISymbol(MO.getIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
static MCInstPrinter *createHexagonMCInstPrinter(const Target &T,
|
|
|
|
unsigned SyntaxVariant,
|
|
|
|
const MCAsmInfo &MAI,
|
|
|
|
const MCInstrInfo &MII,
|
|
|
|
const MCRegisterInfo &MRI,
|
|
|
|
const MCSubtargetInfo &STI) {
|
|
|
|
if (SyntaxVariant == 0)
|
|
|
|
return(new HexagonInstPrinter(MAI, MII, MRI));
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-12-12 21:14:40 +00:00
|
|
|
extern "C" void LLVMInitializeHexagonAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<HexagonAsmPrinter> X(TheHexagonTarget);
|
2012-04-12 17:55:53 +00:00
|
|
|
|
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheHexagonTarget,
|
|
|
|
createHexagonMCInstPrinter);
|
2011-12-12 21:14:40 +00:00
|
|
|
}
|