2005-01-25 02:37:48 +08:00
|
|
|
//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
|
2005-04-22 07:13:11 +08:00
|
|
|
//
|
2005-01-23 07:41:55 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:13:11 +08:00
|
|
|
//
|
2005-01-23 07:41:55 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to GAS-format Alpha assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "Alpha.h"
|
|
|
|
#include "AlphaInstrInfo.h"
|
2005-09-30 06:54:56 +08:00
|
|
|
#include "AlphaTargetMachine.h"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "llvm/Module.h"
|
2005-03-17 23:38:16 +08:00
|
|
|
#include "llvm/Type.h"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2006-09-07 02:34:40 +08:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2006-12-07 02:19:53 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2008-08-21 08:14:44 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2005-01-23 07:41:55 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-20 06:59:26 +08:00
|
|
|
STATISTIC(EmittedInsts, "Number of machine instrs printed");
|
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
namespace {
|
2006-09-07 02:34:40 +08:00
|
|
|
struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
|
2005-01-23 07:41:55 +08:00
|
|
|
|
|
|
|
/// Unique incrementer for label values for referencing Global values.
|
|
|
|
///
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2008-08-21 08:14:44 +08:00
|
|
|
AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
|
2006-12-08 07:55:55 +08:00
|
|
|
: AsmPrinter(o, tm, T) {
|
2005-02-04 22:09:38 +08:00
|
|
|
}
|
2005-01-23 07:41:55 +08:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Alpha Assembly Printer";
|
|
|
|
}
|
|
|
|
bool printInstruction(const MachineInstr *MI);
|
|
|
|
void printOp(const MachineOperand &MO, bool IsCallOp = false);
|
2005-12-01 02:54:35 +08:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum);
|
2005-01-23 07:41:55 +08:00
|
|
|
void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
|
2008-08-07 17:53:57 +08:00
|
|
|
void printModuleLevelGV(const GlobalVariable* GVar);
|
2005-04-22 07:13:11 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
2005-01-23 07:41:55 +08:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
2008-08-07 17:53:57 +08:00
|
|
|
|
2006-06-21 21:37:27 +08:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode);
|
2008-08-07 17:53:57 +08:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI,
|
2007-04-17 02:10:23 +08:00
|
|
|
unsigned OpNo,
|
2008-08-07 17:53:57 +08:00
|
|
|
unsigned AsmVariant,
|
2007-04-17 02:10:23 +08:00
|
|
|
const char *ExtraCode);
|
2005-01-23 07:41:55 +08:00
|
|
|
};
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
/// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
|
|
|
|
/// assembly code for a MachineFunction to the given output stream,
|
|
|
|
/// using the given target machine description. This should work
|
|
|
|
/// regardless of whether the function is in SSA form.
|
|
|
|
///
|
2008-08-21 08:14:44 +08:00
|
|
|
FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
|
2006-10-05 10:47:13 +08:00
|
|
|
TargetMachine &tm) {
|
2006-09-08 06:06:40 +08:00
|
|
|
return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2005-01-23 07:41:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "AlphaGenAsmWriter.inc"
|
|
|
|
|
2005-12-01 02:54:35 +08:00
|
|
|
void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
|
2005-01-23 07:41:55 +08:00
|
|
|
{
|
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
2006-05-05 02:05:43 +08:00
|
|
|
if (MO.getType() == MachineOperand::MO_Register) {
|
2008-02-11 02:45:23 +08:00
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
|
|
|
|
"Not physreg??");
|
2008-02-27 05:11:01 +08:00
|
|
|
O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
|
2005-01-23 07:41:55 +08:00
|
|
|
} else if (MO.isImmediate()) {
|
2007-12-31 04:49:49 +08:00
|
|
|
O << MO.getImm();
|
|
|
|
assert(MO.getImm() < (1 << 30));
|
2005-01-23 07:41:55 +08:00
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
|
2008-02-11 02:45:23 +08:00
|
|
|
const TargetRegisterInfo &RI = *TM.getRegisterInfo();
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
switch (MO.getType()) {
|
2006-05-05 02:05:43 +08:00
|
|
|
case MachineOperand::MO_Register:
|
2008-02-27 05:11:01 +08:00
|
|
|
O << RI.get(MO.getReg()).AsmName;
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
2006-05-05 01:21:20 +08:00
|
|
|
case MachineOperand::MO_Immediate:
|
2006-12-08 06:21:48 +08:00
|
|
|
cerr << "printOp() does not handle immediate values\n";
|
2005-01-23 07:41:55 +08:00
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
2007-12-31 07:10:15 +08:00
|
|
|
printBasicBlockLabel(MO.getMBB());
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2007-10-14 13:57:21 +08:00
|
|
|
O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
|
2007-12-31 07:10:15 +08:00
|
|
|
<< MO.getIndex();
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
return;
|
|
|
|
|
2007-02-14 07:41:16 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
GlobalValue *GV = MO.getGlobal();
|
|
|
|
O << Mang->getValueName(GV);
|
|
|
|
if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
|
|
|
|
ExtWeakSymbols.insert(GV);
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
2007-02-14 07:41:16 +08:00
|
|
|
}
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2006-09-19 02:01:03 +08:00
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
2007-10-14 13:57:21 +08:00
|
|
|
O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
|
2007-12-31 07:10:15 +08:00
|
|
|
<< '_' << MO.getIndex();
|
2006-09-19 02:01:03 +08:00
|
|
|
return;
|
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
default:
|
|
|
|
O << "<unknown operand type: " << MO.getType() << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// runOnMachineFunction - This uses the printMachineInstruction()
|
|
|
|
/// method to print assembly for each instruction.
|
|
|
|
///
|
|
|
|
bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
2005-11-21 15:51:23 +08:00
|
|
|
SetupMachineFunction(MF);
|
2005-01-23 07:41:55 +08:00
|
|
|
O << "\n\n";
|
|
|
|
|
|
|
|
// Print out constants referenced by the function
|
2005-11-21 16:29:17 +08:00
|
|
|
EmitConstantPool(MF.getConstantPool());
|
2005-01-23 07:41:55 +08:00
|
|
|
|
2006-09-19 02:01:03 +08:00
|
|
|
// Print out jump tables referenced by the function
|
2006-10-05 11:01:21 +08:00
|
|
|
EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
|
2006-09-19 02:01:03 +08:00
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
// Print out labels for the function.
|
2006-02-05 03:13:09 +08:00
|
|
|
const Function *F = MF.getFunction();
|
2008-09-25 06:13:07 +08:00
|
|
|
SwitchToTextSection(TAI->SectionForGlobal(F).c_str(), F);
|
2008-08-07 17:53:57 +08:00
|
|
|
|
2006-02-05 03:13:09 +08:00
|
|
|
EmitAlignment(4, F);
|
|
|
|
switch (F->getLinkage()) {
|
|
|
|
default: assert(0 && "Unknown linkage type!");
|
|
|
|
case Function::InternalLinkage: // Symbols default to internal.
|
|
|
|
break;
|
|
|
|
case Function::ExternalLinkage:
|
|
|
|
O << "\t.globl " << CurrentFnName << "\n";
|
|
|
|
break;
|
|
|
|
case Function::WeakLinkage:
|
|
|
|
case Function::LinkOnceLinkage:
|
2006-12-08 07:55:55 +08:00
|
|
|
O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
|
2006-02-05 03:13:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-08-09 02:25:07 +08:00
|
|
|
printVisibility(CurrentFnName, F->getVisibility());
|
|
|
|
|
2005-05-27 11:39:30 +08:00
|
|
|
O << "\t.ent " << CurrentFnName << "\n";
|
2005-01-23 07:41:55 +08:00
|
|
|
|
|
|
|
O << CurrentFnName << ":\n";
|
|
|
|
|
|
|
|
// Print out code for the function.
|
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
|
|
|
|
I != E; ++I) {
|
2006-12-08 07:55:55 +08:00
|
|
|
if (I != MF.begin()) {
|
2008-02-28 08:43:03 +08:00
|
|
|
printBasicBlockLabel(I, true, true);
|
2006-12-08 07:55:55 +08:00
|
|
|
O << '\n';
|
|
|
|
}
|
2005-01-23 07:41:55 +08:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
|
|
|
II != E; ++II) {
|
|
|
|
// Print the assembly for the instruction.
|
2006-12-08 07:55:55 +08:00
|
|
|
++EmittedInsts;
|
|
|
|
if (!printInstruction(II)) {
|
|
|
|
assert(0 && "Unhandled instruction in asm writer!");
|
|
|
|
abort();
|
|
|
|
}
|
2005-01-23 07:41:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-26 06:55:15 +08:00
|
|
|
O << "\t.end " << CurrentFnName << "\n";
|
2005-01-23 07:41:55 +08:00
|
|
|
|
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlphaAsmPrinter::doInitialization(Module &M)
|
|
|
|
{
|
2007-01-25 05:09:16 +08:00
|
|
|
if(TM.getSubtarget<AlphaSubtarget>().hasCT())
|
|
|
|
O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
|
2005-04-15 00:24:00 +08:00
|
|
|
else
|
2007-01-25 05:09:16 +08:00
|
|
|
O << "\t.arch ev6\n";
|
2005-07-23 04:52:16 +08:00
|
|
|
O << "\t.set noat\n";
|
2007-07-26 03:33:14 +08:00
|
|
|
return AsmPrinter::doInitialization(M);
|
2005-01-23 07:41:55 +08:00
|
|
|
}
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2008-08-07 17:53:57 +08:00
|
|
|
void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
|
2006-05-03 09:29:57 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2008-08-07 17:53:57 +08:00
|
|
|
if (!GVar->hasInitializer()) return; // External global require no code
|
|
|
|
|
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(GVar))
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string SectionName = TAI->SectionForGlobal(GVar);
|
|
|
|
std::string name = Mang->getValueName(GVar);
|
|
|
|
Constant *C = GVar->getInitializer();
|
|
|
|
unsigned Size = TD->getABITypeSize(C->getType());
|
|
|
|
unsigned Align = TD->getPreferredAlignmentLog(GVar);
|
|
|
|
|
|
|
|
// 0: Switch to section
|
|
|
|
SwitchToDataSection(SectionName.c_str());
|
|
|
|
|
|
|
|
// 1: Check visibility
|
2008-08-09 02:25:07 +08:00
|
|
|
printVisibility(name, GVar->getVisibility());
|
2008-08-07 17:53:57 +08:00
|
|
|
|
|
|
|
// 2: Kind
|
|
|
|
switch (GVar->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
case GlobalValue::CommonLinkage:
|
|
|
|
O << TAI->getWeakRefDirective() << name << '\n';
|
|
|
|
break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
O << TAI->getGlobalDirective() << name << "\n";
|
2007-02-14 07:41:16 +08:00
|
|
|
break;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
|
|
|
cerr << "Unknown linkage type!\n";
|
|
|
|
abort();
|
|
|
|
}
|
2008-08-07 17:53:57 +08:00
|
|
|
|
|
|
|
// 3: Type, Size, Align
|
|
|
|
if (TAI->hasDotTypeDotSizeDirective()) {
|
2007-02-14 07:41:16 +08:00
|
|
|
O << "\t.type\t" << name << ", @object\n";
|
|
|
|
O << "\t.size\t" << name << ", " << Size << "\n";
|
|
|
|
}
|
2006-12-08 01:39:14 +08:00
|
|
|
|
2008-08-07 17:53:57 +08:00
|
|
|
EmitAlignment(Align, GVar);
|
|
|
|
|
|
|
|
O << name << ":\n";
|
|
|
|
|
|
|
|
// If the initializer is a extern weak symbol, remember to emit the weak
|
|
|
|
// reference!
|
|
|
|
if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
|
|
|
|
if (GV->hasExternalWeakLinkage())
|
|
|
|
ExtWeakSymbols.insert(GV);
|
|
|
|
|
|
|
|
EmitGlobalConstant(C);
|
|
|
|
O << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlphaAsmPrinter::doFinalization(Module &M) {
|
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
|
|
|
printModuleLevelGV(I);
|
|
|
|
|
2007-07-26 03:33:14 +08:00
|
|
|
return AsmPrinter::doFinalization(M);
|
2005-01-23 07:41:55 +08:00
|
|
|
}
|
2006-06-21 21:37:27 +08:00
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2008-08-07 17:53:57 +08:00
|
|
|
unsigned AsmVariant,
|
2007-04-17 02:10:23 +08:00
|
|
|
const char *ExtraCode) {
|
2006-06-21 21:37:27 +08:00
|
|
|
printOperand(MI, OpNo);
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-21 23:42:36 +08:00
|
|
|
|
2008-08-07 17:53:57 +08:00
|
|
|
bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
2007-04-17 02:10:23 +08:00
|
|
|
unsigned OpNo,
|
2008-08-07 17:53:57 +08:00
|
|
|
unsigned AsmVariant,
|
2007-04-17 02:10:23 +08:00
|
|
|
const char *ExtraCode) {
|
2006-06-21 23:42:36 +08:00
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
2006-07-04 01:57:34 +08:00
|
|
|
O << "0(";
|
2006-06-21 23:42:36 +08:00
|
|
|
printOperand(MI, OpNo);
|
2006-07-04 01:57:34 +08:00
|
|
|
O << ")";
|
2006-06-21 23:42:36 +08:00
|
|
|
return false;
|
|
|
|
}
|