2005-01-24 18:37:48 +00:00
|
|
|
//===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
|
2005-04-21 23:13:11 +00:00
|
|
|
//
|
2005-01-22 23:41:55 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 23:13:11 +00:00
|
|
|
//
|
2005-01-22 23:41:55 +00: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-19 22:59:26 +00:00
|
|
|
#define DEBUG_TYPE "asm-printer"
|
2005-01-22 23:41:55 +00:00
|
|
|
#include "Alpha.h"
|
|
|
|
#include "AlphaInstrInfo.h"
|
2005-09-29 22:54:56 +00:00
|
|
|
#include "AlphaTargetMachine.h"
|
2005-01-22 23:41:55 +00:00
|
|
|
#include "llvm/Module.h"
|
2005-03-17 15:38:16 +00:00
|
|
|
#include "llvm/Type.h"
|
2005-01-22 23:41:55 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2006-09-06 18:34:40 +00:00
|
|
|
#include "llvm/Target/TargetAsmInfo.h"
|
2005-01-22 23:41:55 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2006-12-06 18:19:53 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2005-01-22 23:41:55 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2006-12-19 22:59:26 +00:00
|
|
|
STATISTIC(EmittedInsts, "Number of machine instrs printed");
|
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
namespace {
|
2006-09-06 18:34:40 +00:00
|
|
|
struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
|
2005-01-22 23:41:55 +00:00
|
|
|
|
|
|
|
/// Unique incrementer for label values for referencing Global values.
|
|
|
|
///
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2006-09-07 22:06:40 +00:00
|
|
|
AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
|
2006-12-07 23:55:55 +00:00
|
|
|
: AsmPrinter(o, tm, T) {
|
2005-02-04 14:09:38 +00:00
|
|
|
}
|
2005-01-22 23:41:55 +00:00
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "Alpha Assembly Printer";
|
|
|
|
}
|
|
|
|
bool printInstruction(const MachineInstr *MI);
|
|
|
|
void printOp(const MachineOperand &MO, bool IsCallOp = false);
|
2005-11-30 18:54:35 +00:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum);
|
2005-01-22 23:41:55 +00:00
|
|
|
void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
|
2005-04-21 23:13:11 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &F);
|
2005-01-22 23:41:55 +00:00
|
|
|
bool doInitialization(Module &M);
|
|
|
|
bool doFinalization(Module &M);
|
2006-10-05 02:47:13 +00:00
|
|
|
|
2006-06-21 13:37:27 +00:00
|
|
|
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
|
|
|
unsigned AsmVariant, const char *ExtraCode);
|
2006-06-21 15:42:36 +00:00
|
|
|
bool PrintAsmMemoryOperand(const MachineInstr *MI,
|
2007-04-16 18:10:23 +00:00
|
|
|
unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode);
|
2005-01-22 23:41:55 +00: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.
|
|
|
|
///
|
2006-10-05 02:47:13 +00:00
|
|
|
FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
|
|
|
|
TargetMachine &tm) {
|
2006-09-07 22:06:40 +00:00
|
|
|
return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
|
2005-01-22 23:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "AlphaGenAsmWriter.inc"
|
|
|
|
|
2005-11-30 18:54:35 +00:00
|
|
|
void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
|
2005-01-22 23:41:55 +00:00
|
|
|
{
|
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
2006-05-04 18:05:43 +00:00
|
|
|
if (MO.getType() == MachineOperand::MO_Register) {
|
2005-01-22 23:41:55 +00:00
|
|
|
assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
|
2005-01-24 18:37:48 +00:00
|
|
|
O << TM.getRegisterInfo()->get(MO.getReg()).Name;
|
2005-01-22 23:41:55 +00:00
|
|
|
} else if (MO.isImmediate()) {
|
|
|
|
O << MO.getImmedValue();
|
2006-05-17 19:24:31 +00:00
|
|
|
assert(MO.getImmedValue() < (1 << 30));
|
2005-01-22 23:41:55 +00:00
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
|
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
switch (MO.getType()) {
|
2006-05-04 18:05:43 +00:00
|
|
|
case MachineOperand::MO_Register:
|
2005-01-24 18:37:48 +00:00
|
|
|
O << RI.get(MO.getReg()).Name;
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
|
|
|
|
2006-05-04 17:21:20 +00:00
|
|
|
case MachineOperand::MO_Immediate:
|
2006-12-07 22:21:48 +00:00
|
|
|
cerr << "printOp() does not handle immediate values\n";
|
2005-01-22 23:41:55 +00:00
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
|
2006-04-22 18:53:45 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
printBasicBlockLabel(MO.getMachineBasicBlock());
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2006-09-06 18:34:40 +00:00
|
|
|
O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
|
2005-11-21 06:51:52 +00:00
|
|
|
<< MO.getConstantPoolIndex();
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
return;
|
|
|
|
|
2007-02-13 23:41:16 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress: {
|
|
|
|
GlobalValue *GV = MO.getGlobal();
|
|
|
|
O << Mang->getValueName(GV);
|
|
|
|
if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
|
|
|
|
ExtWeakSymbols.insert(GV);
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
2007-02-13 23:41:16 +00:00
|
|
|
}
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2006-09-18 18:01:03 +00:00
|
|
|
case MachineOperand::MO_JumpTableIndex:
|
|
|
|
O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
|
|
|
|
<< '_' << MO.getJumpTableIndex();
|
|
|
|
return;
|
|
|
|
|
2005-01-22 23:41:55 +00: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 07:51:23 +00:00
|
|
|
SetupMachineFunction(MF);
|
2005-01-22 23:41:55 +00:00
|
|
|
O << "\n\n";
|
|
|
|
|
|
|
|
// Print out constants referenced by the function
|
2005-11-21 08:29:17 +00:00
|
|
|
EmitConstantPool(MF.getConstantPool());
|
2005-01-22 23:41:55 +00:00
|
|
|
|
2006-09-18 18:01:03 +00:00
|
|
|
// Print out jump tables referenced by the function
|
2006-10-05 03:01:21 +00:00
|
|
|
EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
|
2006-09-18 18:01:03 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
// Print out labels for the function.
|
2006-02-04 19:13:09 +00:00
|
|
|
const Function *F = MF.getFunction();
|
2006-10-05 02:47:13 +00:00
|
|
|
SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
|
|
|
|
|
2006-02-04 19:13:09 +00: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-07 23:55:55 +00:00
|
|
|
O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
|
2006-02-04 19:13:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-05-27 03:39:30 +00:00
|
|
|
O << "\t.ent " << CurrentFnName << "\n";
|
2005-01-22 23:41:55 +00: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-07 23:55:55 +00:00
|
|
|
if (I != MF.begin()) {
|
|
|
|
printBasicBlockLabel(I, true);
|
|
|
|
O << '\n';
|
|
|
|
}
|
2005-01-22 23:41:55 +00:00
|
|
|
for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
|
|
|
|
II != E; ++II) {
|
|
|
|
// Print the assembly for the instruction.
|
2006-12-07 23:55:55 +00:00
|
|
|
++EmittedInsts;
|
2005-01-22 23:41:55 +00:00
|
|
|
O << "\t";
|
2006-12-07 23:55:55 +00:00
|
|
|
if (!printInstruction(II)) {
|
|
|
|
assert(0 && "Unhandled instruction in asm writer!");
|
|
|
|
abort();
|
|
|
|
}
|
2005-01-22 23:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-25 22:55:15 +00:00
|
|
|
O << "\t.end " << CurrentFnName << "\n";
|
2005-01-22 23:41:55 +00:00
|
|
|
|
|
|
|
// We didn't modify anything.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlphaAsmPrinter::doInitialization(Module &M)
|
|
|
|
{
|
2007-01-24 21:09:16 +00:00
|
|
|
if(TM.getSubtarget<AlphaSubtarget>().hasCT())
|
|
|
|
O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
|
2005-04-14 16:24:00 +00:00
|
|
|
else
|
2007-01-24 21:09:16 +00:00
|
|
|
O << "\t.arch ev6\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
O << "\t.set noat\n";
|
2007-02-13 23:41:16 +00:00
|
|
|
AsmPrinter::doInitialization(M);
|
2005-01-22 23:41:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
bool AlphaAsmPrinter::doFinalization(Module &M) {
|
2006-05-03 01:29:57 +00:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2007-02-13 23:41:16 +00:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
|
2005-01-22 23:41:55 +00:00
|
|
|
|
2007-02-13 23:41:16 +00:00
|
|
|
if (!I->hasInitializer()) continue; // External global require no code
|
|
|
|
|
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
|
|
|
unsigned Align = TD->getPreferredAlignmentLog(I);
|
|
|
|
|
|
|
|
//1: hidden?
|
|
|
|
if (I->hasHiddenVisibility())
|
|
|
|
O << TAI->getHiddenDirective() << name << "\n";
|
|
|
|
|
|
|
|
//2: kind
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage:
|
|
|
|
O << TAI->getWeakRefDirective() << name << '\n';
|
|
|
|
break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
break;
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
|
|
|
cerr << "Unknown linkage type!\n";
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
//3: Section (if changed)
|
|
|
|
if (I->hasSection() &&
|
|
|
|
(I->getSection() == ".ctors" ||
|
|
|
|
I->getSection() == ".dtors")) {
|
|
|
|
std::string SectionName = ".section\t" + I->getSection()
|
|
|
|
+ ",\"aw\",@progbits";
|
|
|
|
SwitchToDataSection(SectionName.c_str());
|
|
|
|
} else {
|
|
|
|
if (C->isNullValue())
|
|
|
|
SwitchToDataSection("\t.section\t.bss", I);
|
|
|
|
else
|
|
|
|
SwitchToDataSection("\t.section\t.data", I);
|
2006-12-07 17:39:14 +00:00
|
|
|
}
|
2007-02-13 23:41:16 +00:00
|
|
|
|
|
|
|
//4: Type, Size, Align
|
|
|
|
O << "\t.type\t" << name << ", @object\n";
|
|
|
|
O << "\t.size\t" << name << ", " << Size << "\n";
|
|
|
|
EmitAlignment(Align, I);
|
|
|
|
|
|
|
|
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';
|
|
|
|
}
|
2006-12-07 17:39:14 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-21 13:37:27 +00:00
|
|
|
|
|
|
|
/// PrintAsmOperand - Print out an operand for an inline asm expression.
|
|
|
|
///
|
|
|
|
bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
|
2007-04-16 18:10:23 +00:00
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
2006-06-21 13:37:27 +00:00
|
|
|
printOperand(MI, OpNo);
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-21 15:42:36 +00:00
|
|
|
|
|
|
|
bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
2007-04-16 18:10:23 +00:00
|
|
|
unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
2006-06-21 15:42:36 +00:00
|
|
|
if (ExtraCode && ExtraCode[0])
|
|
|
|
return true; // Unknown modifier.
|
2006-07-03 17:57:34 +00:00
|
|
|
O << "0(";
|
2006-06-21 15:42:36 +00:00
|
|
|
printOperand(MI, OpNo);
|
2006-07-03 17:57:34 +00:00
|
|
|
O << ")";
|
2006-06-21 15:42:36 +00:00
|
|
|
return false;
|
|
|
|
}
|