2008-11-07 18:59:00 +08:00
|
|
|
//===-- XCoreAsmPrinter.cpp - XCore LLVM assembly writer ------------------===//
|
|
|
|
//
|
|
|
|
// 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 the XAS-format XCore assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "XCore.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "InstPrinter/XCoreInstPrinter.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "XCoreInstrInfo.h"
|
2012-12-17 00:20:48 +08:00
|
|
|
#include "XCoreMCInstLower.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "XCoreSubtarget.h"
|
|
|
|
#include "XCoreTargetMachine.h"
|
2014-03-04 18:07:28 +08:00
|
|
|
#include "XCoreTargetStreamer.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2010-02-23 21:25:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-03-06 08:46:21 +08:00
|
|
|
#include "llvm/IR/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2014-01-08 05:19:40 +08:00
|
|
|
#include "llvm/IR/Mangler.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2011-07-15 07:50:31 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2012-12-17 00:20:48 +08:00
|
|
|
#include "llvm/MC/MCInst.h"
|
2009-08-19 13:49:37 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2009-09-14 01:14:04 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2009-07-09 03:04:27 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-08-25 02:08:43 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2010-04-04 16:18:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
|
2008-11-07 18:59:00 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class XCoreAsmPrinter : public AsmPrinter {
|
2008-11-07 18:59:00 +08:00
|
|
|
const XCoreSubtarget &Subtarget;
|
2012-12-17 00:20:48 +08:00
|
|
|
XCoreMCInstLower MCInstLowering;
|
2014-01-27 07:57:05 +08:00
|
|
|
XCoreTargetStreamer &getTargetStreamer();
|
|
|
|
|
2009-02-24 16:30:20 +08:00
|
|
|
public:
|
2015-01-19 04:29:04 +08:00
|
|
|
explicit XCoreAsmPrinter(TargetMachine &TM,
|
|
|
|
std::unique_ptr<MCStreamer> Streamer)
|
|
|
|
: AsmPrinter(TM, std::move(Streamer)),
|
|
|
|
Subtarget(TM.getSubtarget<XCoreSubtarget>()), MCInstLowering(*this) {}
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2014-04-29 15:57:00 +08:00
|
|
|
const char *getPassName() const override {
|
2008-11-07 18:59:00 +08:00
|
|
|
return "XCore Assembly Printer";
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
|
2010-02-23 21:25:07 +08:00
|
|
|
const std::string &directive = ".jmptable");
|
2010-04-04 12:47:45 +08:00
|
|
|
void printInlineJT32(const MachineInstr *MI, int opNum, raw_ostream &O) {
|
|
|
|
printInlineJT(MI, opNum, O, ".jmptable32");
|
2010-02-23 21:25:07 +08:00
|
|
|
}
|
2010-04-04 12:47:45 +08:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
|
2008-11-07 18:59:00 +08:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
2014-04-29 15:57:00 +08:00
|
|
|
raw_ostream &O) override;
|
2014-03-07 00:37:48 +08:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &O) override;
|
2009-07-15 23:36:37 +08:00
|
|
|
|
2010-04-04 15:05:53 +08:00
|
|
|
void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
|
2014-04-29 15:57:00 +08:00
|
|
|
void EmitGlobalVariable(const GlobalVariable *GV) override;
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2014-04-29 15:57:00 +08:00
|
|
|
void EmitFunctionEntryLabel() override;
|
|
|
|
void EmitInstruction(const MachineInstr *MI) override;
|
|
|
|
void EmitFunctionBodyStart() override;
|
|
|
|
void EmitFunctionBodyEnd() override;
|
2008-11-07 18:59:00 +08:00
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2014-01-27 07:57:05 +08:00
|
|
|
XCoreTargetStreamer &XCoreAsmPrinter::getTargetStreamer() {
|
|
|
|
return static_cast<XCoreTargetStreamer&>(*OutStreamer.getTargetStreamer());
|
|
|
|
}
|
|
|
|
|
2010-04-04 15:05:53 +08:00
|
|
|
void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
|
2014-02-18 19:21:59 +08:00
|
|
|
assert( ( GV->hasExternalLinkage() || GV->hasWeakLinkage() ||
|
|
|
|
GV->hasLinkOnceLinkage() || GV->hasCommonLinkage() ) &&
|
|
|
|
"Unexpected linkage");
|
2011-07-18 12:54:35 +08:00
|
|
|
if (ArrayType *ATy = dyn_cast<ArrayType>(
|
2013-08-01 15:52:05 +08:00
|
|
|
cast<PointerType>(GV->getType())->getElementType())) {
|
|
|
|
|
|
|
|
MCSymbol *SymGlob = OutContext.GetOrCreateSymbol(
|
|
|
|
Twine(Sym->getName() + StringRef(".globound")));
|
|
|
|
OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Global);
|
2013-10-11 18:27:13 +08:00
|
|
|
OutStreamer.EmitAssignment(SymGlob,
|
|
|
|
MCConstantExpr::Create(ATy->getNumElements(),
|
|
|
|
OutContext));
|
2014-02-18 19:21:59 +08:00
|
|
|
if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
|
|
|
|
GV->hasCommonLinkage()) {
|
2013-10-11 18:27:13 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(SymGlob, MCSA_Weak);
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-19 13:38:33 +08:00
|
|
|
void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
|
2009-07-22 01:59:36 +08:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (!GV->hasInitializer() ||
|
|
|
|
EmitSpecialLLVMGlobal(GV))
|
|
|
|
return;
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2014-08-05 05:25:23 +08:00
|
|
|
const DataLayout *TD = TM.getSubtargetImpl()->getDataLayout();
|
2014-02-08 22:53:28 +08:00
|
|
|
OutStreamer.SwitchSection(
|
|
|
|
getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
|
2010-01-16 08:21:18 +08:00
|
|
|
|
2013-10-30 01:07:16 +08:00
|
|
|
MCSymbol *GVSym = getSymbol(GV);
|
2011-06-20 02:37:11 +08:00
|
|
|
const Constant *C = GV->getInitializer();
|
2009-07-22 01:59:36 +08:00
|
|
|
unsigned Align = (unsigned)TD->getPreferredTypeAlignmentShift(C->getType());
|
|
|
|
|
|
|
|
// Mark the start of the global
|
2014-01-27 07:57:05 +08:00
|
|
|
getTargetStreamer().emitCCTopData(GVSym->getName());
|
2009-07-22 01:59:36 +08:00
|
|
|
|
|
|
|
switch (GV->getLinkage()) {
|
|
|
|
case GlobalValue::AppendingLinkage:
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("AppendingLinkage is not supported by this target!");
|
2009-07-22 01:59:36 +08:00
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
|
|
|
case GlobalValue::WeakAnyLinkage:
|
|
|
|
case GlobalValue::WeakODRLinkage:
|
|
|
|
case GlobalValue::ExternalLinkage:
|
2014-02-18 19:21:59 +08:00
|
|
|
case GlobalValue::CommonLinkage:
|
2010-01-16 08:21:18 +08:00
|
|
|
emitArrayBound(GVSym, GV);
|
2010-04-04 15:05:53 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
|
|
|
|
|
2014-02-18 19:21:59 +08:00
|
|
|
if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
|
|
|
|
GV->hasCommonLinkage())
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Weak);
|
2009-07-22 01:59:36 +08:00
|
|
|
// FALL THROUGH
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
case GlobalValue::PrivateLinkage:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown linkage type!");
|
|
|
|
}
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2010-04-28 09:08:40 +08:00
|
|
|
EmitAlignment(Align > 2 ? Align : 2, GV);
|
2009-07-22 01:59:36 +08:00
|
|
|
|
|
|
|
if (GV->isThreadLocal()) {
|
2013-05-05 01:01:55 +08:00
|
|
|
report_fatal_error("TLS is not supported by this target!");
|
2009-07-22 01:59:36 +08:00
|
|
|
}
|
2013-05-05 01:01:55 +08:00
|
|
|
unsigned Size = TD->getTypeAllocSize(C->getType());
|
2009-08-23 05:43:10 +08:00
|
|
|
if (MAI->hasDotTypeDotSizeDirective()) {
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
|
2014-01-27 07:57:05 +08:00
|
|
|
OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext));
|
2009-07-22 01:59:36 +08:00
|
|
|
}
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitLabel(GVSym);
|
2009-07-22 01:59:36 +08:00
|
|
|
|
|
|
|
EmitGlobalConstant(C);
|
2010-01-20 05:51:22 +08:00
|
|
|
// The ABI requires that unsigned scalar types smaller than 32 bits
|
2010-01-21 01:53:51 +08:00
|
|
|
// are padded to 32 bits.
|
2010-01-20 05:51:22 +08:00
|
|
|
if (Size < 4)
|
2013-01-09 11:52:05 +08:00
|
|
|
OutStreamer.EmitZeros(4 - Size);
|
2009-07-22 01:59:36 +08:00
|
|
|
|
|
|
|
// Mark the end of the global
|
2014-01-27 19:50:13 +08:00
|
|
|
getTargetStreamer().emitCCBottomData(GVSym->getName());
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
|
2012-12-17 00:20:48 +08:00
|
|
|
void XCoreAsmPrinter::EmitFunctionBodyStart() {
|
|
|
|
MCInstLowering.Initialize(Mang, &MF->getContext());
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:22:43 +08:00
|
|
|
/// EmitFunctionBodyEnd - Targets can override this to emit stuff after
|
|
|
|
/// the last basic block in the function.
|
|
|
|
void XCoreAsmPrinter::EmitFunctionBodyEnd() {
|
|
|
|
// Emit function end directives
|
2014-01-27 19:50:13 +08:00
|
|
|
getTargetStreamer().emitCCBottomFunction(CurrentFnSym->getName());
|
2010-01-28 14:22:43 +08:00
|
|
|
}
|
|
|
|
|
2010-04-05 12:44:02 +08:00
|
|
|
void XCoreAsmPrinter::EmitFunctionEntryLabel() {
|
|
|
|
// Mark the start of the function
|
2014-01-27 07:57:05 +08:00
|
|
|
getTargetStreamer().emitCCTopFunction(CurrentFnSym->getName());
|
2010-04-05 12:44:02 +08:00
|
|
|
OutStreamer.EmitLabel(CurrentFnSym);
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
|
2010-02-23 21:25:07 +08:00
|
|
|
void XCoreAsmPrinter::
|
2010-04-04 12:47:45 +08:00
|
|
|
printInlineJT(const MachineInstr *MI, int opNum, raw_ostream &O,
|
|
|
|
const std::string &directive) {
|
2010-02-23 21:25:07 +08:00
|
|
|
unsigned JTI = MI->getOperand(opNum).getIndex();
|
|
|
|
const MachineFunction *MF = MI->getParent()->getParent();
|
|
|
|
const MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
|
|
|
|
const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
|
|
|
|
const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
|
|
|
|
O << "\t" << directive << " ";
|
|
|
|
for (unsigned i = 0, e = JTBBs.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *MBB = JTBBs[i];
|
|
|
|
if (i > 0)
|
|
|
|
O << ",";
|
2010-03-14 05:04:28 +08:00
|
|
|
O << *MBB->getSymbol();
|
2010-02-23 21:25:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-04 12:47:45 +08:00
|
|
|
void XCoreAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &O) {
|
2014-08-05 05:25:23 +08:00
|
|
|
const DataLayout *DL = TM.getSubtargetImpl()->getDataLayout();
|
2008-11-07 18:59:00 +08:00
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_Register:
|
2012-12-17 00:20:48 +08:00
|
|
|
O << XCoreInstPrinter::getRegisterName(MO.getReg());
|
2008-11-07 18:59:00 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
O << MO.getImm();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2010-03-14 05:04:28 +08:00
|
|
|
O << *MO.getMBB()->getSymbol();
|
2008-11-07 18:59:00 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2013-10-30 01:07:16 +08:00
|
|
|
O << *getSymbol(MO.getGlobal());
|
2008-11-07 18:59:00 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2014-01-04 03:21:54 +08:00
|
|
|
O << DL->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
|
2008-11-07 18:59:00 +08:00
|
|
|
<< '_' << MO.getIndex();
|
|
|
|
break;
|
2009-11-19 07:20:42 +08:00
|
|
|
case MachineOperand::MO_BlockAddress:
|
2010-01-18 05:43:43 +08:00
|
|
|
O << *GetBlockAddressSymbol(MO.getBlockAddress());
|
2008-11-07 18:59:00 +08:00
|
|
|
break;
|
|
|
|
default:
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("not implemented");
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool XCoreAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2010-04-04 13:29:35 +08:00
|
|
|
unsigned AsmVariant,const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
2013-07-16 20:48:34 +08:00
|
|
|
// Print the operand if there is no operand modifier.
|
|
|
|
if (!ExtraCode || !ExtraCode[0]) {
|
|
|
|
printOperand(MI, OpNo, O);
|
|
|
|
return false;
|
|
|
|
}
|
2012-06-26 21:49:27 +08:00
|
|
|
|
2013-07-16 20:48:34 +08:00
|
|
|
// Otherwise fallback on the default implementation.
|
|
|
|
return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
|
2014-03-07 00:37:48 +08:00
|
|
|
bool XCoreAsmPrinter::
|
|
|
|
PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode,
|
|
|
|
raw_ostream &O) {
|
|
|
|
if (ExtraCode && ExtraCode[0]) {
|
|
|
|
return true; // Unknown modifier.
|
|
|
|
}
|
|
|
|
printOperand(MI, OpNum, O);
|
|
|
|
O << '[';
|
|
|
|
printOperand(MI, OpNum + 1, O);
|
|
|
|
O << ']';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-01-28 14:22:43 +08:00
|
|
|
void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2014-06-27 06:52:05 +08:00
|
|
|
SmallString<128> Str;
|
|
|
|
raw_svector_ostream O(Str);
|
2010-07-17 06:35:37 +08:00
|
|
|
|
2011-10-11 20:55:35 +08:00
|
|
|
switch (MI->getOpcode()) {
|
2013-06-17 04:34:27 +08:00
|
|
|
case XCore::DBG_VALUE:
|
|
|
|
llvm_unreachable("Should be handled target independently");
|
2011-10-11 20:55:35 +08:00
|
|
|
case XCore::ADD_2rus:
|
|
|
|
if (MI->getOperand(2).getImm() == 0) {
|
2012-12-17 00:20:48 +08:00
|
|
|
O << "\tmov "
|
|
|
|
<< XCoreInstPrinter::getRegisterName(MI->getOperand(0).getReg()) << ", "
|
|
|
|
<< XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg());
|
2011-10-11 20:55:35 +08:00
|
|
|
OutStreamer.EmitRawText(O.str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2012-12-17 00:20:48 +08:00
|
|
|
case XCore::BR_JT:
|
|
|
|
case XCore::BR_JT32:
|
|
|
|
O << "\tbru "
|
|
|
|
<< XCoreInstPrinter::getRegisterName(MI->getOperand(1).getReg()) << '\n';
|
|
|
|
if (MI->getOpcode() == XCore::BR_JT)
|
|
|
|
printInlineJT(MI, 0, O);
|
|
|
|
else
|
|
|
|
printInlineJT32(MI, 0, O);
|
|
|
|
O << '\n';
|
|
|
|
OutStreamer.EmitRawText(O.str());
|
|
|
|
return;
|
2011-10-11 20:55:35 +08:00
|
|
|
}
|
2012-12-17 00:20:48 +08:00
|
|
|
|
|
|
|
MCInst TmpInst;
|
|
|
|
MCInstLowering.Lower(MI, TmpInst);
|
|
|
|
|
2014-01-29 07:12:42 +08:00
|
|
|
EmitToStreamer(OutStreamer, TmpInst);
|
2008-11-07 18:59:00 +08:00
|
|
|
}
|
|
|
|
|
2009-07-25 14:49:55 +08:00
|
|
|
// Force static initialization.
|
2009-08-15 20:53:15 +08:00
|
|
|
extern "C" void LLVMInitializeXCoreAsmPrinter() {
|
|
|
|
RegisterAsmPrinter<XCoreAsmPrinter> X(TheXCoreTarget);
|
2009-07-25 14:49:55 +08:00
|
|
|
}
|