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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "asm-printer"
|
|
|
|
#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"
|
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"
|
|
|
|
#include "llvm/DebugInfo.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2011-07-15 07:50:31 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.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/Mangler.h"
|
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
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;
|
2009-02-24 16:30:20 +08:00
|
|
|
public:
|
2010-04-04 16:18:47 +08:00
|
|
|
explicit XCoreAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
2012-12-17 00:20:48 +08:00
|
|
|
: AsmPrinter(TM, Streamer), Subtarget(TM.getSubtarget<XCoreSubtarget>()),
|
|
|
|
MCInstLowering(*this) {}
|
2008-11-07 18:59:00 +08:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
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,
|
|
|
|
raw_ostream &O);
|
2009-07-15 23:36:37 +08:00
|
|
|
|
2010-04-04 15:05:53 +08:00
|
|
|
void emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV);
|
2010-01-19 13:38:33 +08:00
|
|
|
virtual void EmitGlobalVariable(const GlobalVariable *GV);
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2010-04-05 12:44:02 +08:00
|
|
|
void EmitFunctionEntryLabel();
|
2010-01-28 14:22:43 +08:00
|
|
|
void EmitInstruction(const MachineInstr *MI);
|
2012-12-17 00:20:48 +08:00
|
|
|
void EmitFunctionBodyStart();
|
2010-01-28 14:22:43 +08:00
|
|
|
void EmitFunctionBodyEnd();
|
2008-11-07 18:59:00 +08:00
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2010-04-04 15:05:53 +08:00
|
|
|
void XCoreAsmPrinter::emitArrayBound(MCSymbol *Sym, const GlobalVariable *GV) {
|
2008-11-07 19:21:09 +08:00
|
|
|
assert(((GV->hasExternalLinkage() ||
|
2008-11-07 18:59:00 +08:00
|
|
|
GV->hasWeakLinkage()) ||
|
2008-11-07 19:21:09 +08:00
|
|
|
GV->hasLinkOnceLinkage()) && "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);
|
|
|
|
|
|
|
|
OutStreamer.EmitRawText("\t.set\t" + Twine(Sym->getName()) +
|
|
|
|
".globound," + Twine(ATy->getNumElements()));
|
|
|
|
|
2008-11-07 18:59:00 +08:00
|
|
|
if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage()) {
|
|
|
|
// TODO Use COMDAT groups for LinkOnceLinkage
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitRawText(MAI->getWeakDefDirective() +Twine(Sym->getName())+
|
|
|
|
".globound");
|
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
|
|
|
|
2012-10-09 00:38:25 +08:00
|
|
|
const DataLayout *TD = TM.getDataLayout();
|
2009-08-19 13:49:37 +08:00
|
|
|
OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GV, Mang,TM));
|
2010-01-16 08:21:18 +08:00
|
|
|
|
2009-07-22 01:59:36 +08:00
|
|
|
|
2010-03-13 05:19:23 +08:00
|
|
|
MCSymbol *GVSym = Mang->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
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitRawText("\t.cc_top " + Twine(GVSym->getName()) + ".data," +
|
|
|
|
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:
|
2010-01-16 08:21:18 +08:00
|
|
|
emitArrayBound(GVSym, GV);
|
2010-04-04 15:05:53 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
|
|
|
|
|
2009-07-22 01:59:36 +08:00
|
|
|
// TODO Use COMDAT groups for LinkOnceLinkage
|
2010-01-18 05:43:43 +08:00
|
|
|
if (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())
|
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;
|
|
|
|
case GlobalValue::DLLImportLinkage:
|
|
|
|
llvm_unreachable("DLLImport linkage is not supported by this target!");
|
|
|
|
case GlobalValue::DLLExportLinkage:
|
|
|
|
llvm_unreachable("DLLExport linkage is not supported by this target!");
|
|
|
|
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);
|
|
|
|
OutStreamer.EmitRawText("\t.size " + Twine(GVSym->getName()) + "," +
|
|
|
|
Twine(Size));
|
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
|
2010-04-04 15:23:00 +08:00
|
|
|
OutStreamer.EmitRawText("\t.cc_bottom " + Twine(GVSym->getName()) + ".data");
|
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
|
2010-04-04 15:05:53 +08:00
|
|
|
OutStreamer.EmitRawText("\t.cc_bottom " + Twine(CurrentFnSym->getName()) +
|
|
|
|
".function");
|
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
|
|
|
|
OutStreamer.EmitRawText("\t.cc_top " + Twine(CurrentFnSym->getName()) +
|
|
|
|
".function," + CurrentFnSym->getName());
|
|
|
|
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) {
|
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:
|
2010-03-13 05:19:23 +08:00
|
|
|
O << *Mang->getSymbol(MO.getGlobal());
|
2008-11-07 18:59:00 +08:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2009-08-23 05:43:10 +08:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
|
2008-11-07 18:59:00 +08:00
|
|
|
<< '_' << MO.getIndex();
|
|
|
|
break;
|
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2009-08-23 05:43:10 +08:00
|
|
|
O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
|
2008-11-07 18:59:00 +08:00
|
|
|
<< '_' << MO.getIndex();
|
2010-01-23 03:11:53 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-28 14:22:43 +08:00
|
|
|
void XCoreAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2010-04-04 15:05:53 +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);
|
|
|
|
|
|
|
|
OutStreamer.EmitInstruction(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
|
|
|
}
|