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"
|
|
|
|
#include "XCoreInstrInfo.h"
|
|
|
|
#include "XCoreSubtarget.h"
|
2009-08-23 04:48:53 +08:00
|
|
|
#include "XCoreMCAsmInfo.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "XCoreTargetMachine.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2010-02-23 21:25:07 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.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"
|
2010-03-13 05:19:23 +08:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-07-28 11:13:23 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2009-07-25 14:49:55 +08:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2010-04-04 14:12:20 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-07-09 03:04:27 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-04 16:18:47 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-11-07 18:59:00 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<unsigned> MaxThreads("xcore-max-threads", cl::Optional,
|
|
|
|
cl::desc("Maximum number of threads (for emulation thread-local storage)"),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::value_desc("number"),
|
|
|
|
cl::init(8));
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class XCoreAsmPrinter : public AsmPrinter {
|
2008-11-07 18:59:00 +08:00
|
|
|
const XCoreSubtarget &Subtarget;
|
2009-02-24 16:30:20 +08:00
|
|
|
public:
|
2010-04-04 16:18:47 +08:00
|
|
|
explicit XCoreAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
|
|
|
: AsmPrinter(TM, Streamer), Subtarget(TM.getSubtarget<XCoreSubtarget>()){}
|
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 printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
|
|
|
|
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-04 12:47:45 +08:00
|
|
|
void printInstruction(const MachineInstr *MI, raw_ostream &O); // autogen'd.
|
2009-09-14 04:19:22 +08:00
|
|
|
static const char *getRegisterName(unsigned RegNo);
|
2009-09-14 04:08: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);
|
|
|
|
void EmitFunctionBodyEnd();
|
2008-11-07 18:59:00 +08:00
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
#include "XCoreGenAsmWriter.inc"
|
|
|
|
|
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");
|
2008-11-07 18:59:00 +08:00
|
|
|
if (const ArrayType *ATy = dyn_cast<ArrayType>(
|
2010-01-16 08:21:18 +08:00
|
|
|
cast<PointerType>(GV->getType())->getElementType())) {
|
2010-04-04 15:05:53 +08:00
|
|
|
OutStreamer.EmitSymbolAttribute(Sym, MCSA_Global);
|
2010-04-04 15:23:00 +08:00
|
|
|
// FIXME: MCStreamerize.
|
|
|
|
OutStreamer.EmitRawText(StringRef(".globound"));
|
|
|
|
OutStreamer.EmitRawText("\t.set\t" + Twine(Sym->getName()));
|
|
|
|
OutStreamer.EmitRawText(".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
|
|
|
|
2009-07-22 01:59:36 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
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);
|
2009-07-22 01:59:36 +08:00
|
|
|
Constant *C = GV->getInitializer();
|
|
|
|
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
|
|
|
|
|
|
|
unsigned Size = TD->getTypeAllocSize(C->getType());
|
|
|
|
if (GV->isThreadLocal()) {
|
|
|
|
Size *= MaxThreads;
|
|
|
|
}
|
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);
|
|
|
|
if (GV->isThreadLocal()) {
|
2010-01-20 05:51:22 +08:00
|
|
|
for (unsigned i = 1; i < MaxThreads; ++i)
|
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)
|
|
|
|
OutStreamer.EmitZeros(4 - Size, 0);
|
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
|
|
|
}
|
|
|
|
|
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-04-04 12:47:45 +08:00
|
|
|
void XCoreAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &O) {
|
|
|
|
printOperand(MI, opNum, O);
|
2008-11-07 18:59:00 +08:00
|
|
|
|
2010-04-05 12:44:02 +08:00
|
|
|
if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
|
2008-11-07 18:59:00 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
O << "+";
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, opNum+1, O);
|
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:
|
2009-09-14 04:31:40 +08:00
|
|
|
O << 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) {
|
2010-04-04 12:47:45 +08:00
|
|
|
printOperand(MI, OpNo, O);
|
2008-11-07 18:59:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-11-07 18:59:00 +08:00
|
|
|
// Check for mov mnemonic
|
2010-07-17 06:35:37 +08:00
|
|
|
if (MI->getOpcode() == XCore::ADD_2rus && !MI->getOperand(2).getImm())
|
|
|
|
O << "\tmov " << getRegisterName(MI->getOperand(0).getReg()) << ", "
|
|
|
|
<< getRegisterName(MI->getOperand(1).getReg());
|
|
|
|
else
|
2010-04-04 15:05:53 +08:00
|
|
|
printInstruction(MI, O);
|
|
|
|
OutStreamer.EmitRawText(O.str());
|
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
|
|
|
}
|