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