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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Mangler.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-01-22 23:41:00 +00:00
|
|
|
#include <iostream>
|
2005-01-22 23:41:55 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
|
|
|
|
|
|
|
struct AlphaAsmPrinter : public AsmPrinter {
|
|
|
|
|
|
|
|
/// Unique incrementer for label values for referencing Global values.
|
|
|
|
///
|
|
|
|
unsigned LabelNumber;
|
2005-04-21 23:13:11 +00:00
|
|
|
|
|
|
|
AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
|
2005-11-21 07:38:08 +00:00
|
|
|
: AsmPrinter(o, tm), LabelNumber(0) {
|
2005-02-04 14:09:38 +00:00
|
|
|
AlignmentIsInBytes = false;
|
2005-11-21 06:51:52 +00:00
|
|
|
PrivateGlobalPrefix = "$";
|
2005-02-04 14:09:38 +00:00
|
|
|
}
|
2005-01-22 23:41:55 +00:00
|
|
|
|
|
|
|
/// We name each basic block in a Function with a unique number, so
|
|
|
|
/// that we can consistently refer to them later. This is cleared
|
|
|
|
/// at the beginning of each call to runOnMachineFunction().
|
|
|
|
///
|
|
|
|
typedef std::map<const Value *, unsigned> ValueMapTy;
|
|
|
|
ValueMapTy NumberForBB;
|
2005-04-13 17:17:28 +00:00
|
|
|
std::string CurSection;
|
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);
|
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
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);
|
|
|
|
};
|
|
|
|
} // 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.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
|
|
|
|
TargetMachine &tm) {
|
|
|
|
return new AlphaAsmPrinter(o, tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
#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);
|
|
|
|
if (MO.getType() == MachineOperand::MO_MachineRegister) {
|
|
|
|
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();
|
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
|
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
|
|
|
int new_symbol;
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
switch (MO.getType()) {
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
if (Value *V = MO.getVRegValueOrNull()) {
|
|
|
|
O << "<" << V->getName() << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FALLTHROUGH
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
|
|
|
case MachineOperand::MO_CCRegister:
|
2005-01-24 18:37:48 +00:00
|
|
|
O << RI.get(MO.getReg()).Name;
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
|
|
|
std::cerr << "printOp() does not handle immediate values\n";
|
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
2005-01-24 18:37:48 +00:00
|
|
|
std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
|
2005-01-22 23:41:55 +00:00
|
|
|
abort();
|
|
|
|
return;
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
case MachineOperand::MO_MachineBasicBlock: {
|
|
|
|
MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
|
2005-11-21 07:38:08 +00:00
|
|
|
O << PrivateGlobalPrefix << "LBB"
|
|
|
|
<< Mang->getValueName(MBBOp->getParent()->getFunction())
|
2005-01-22 23:41:55 +00:00
|
|
|
<< "_" << MBBOp->getNumber() << "\t" << CommentString << " "
|
|
|
|
<< MBBOp->getBasicBlock()->getName();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2005-11-21 08:29:17 +00:00
|
|
|
O << PrivateGlobalPrefix << "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;
|
|
|
|
|
2005-04-13 17:17:28 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
//Abuse PCrel to specify pcrel calls
|
|
|
|
//calls are the only thing that use this flag
|
2005-12-25 17:36:48 +00:00
|
|
|
// if (MO.isPCRelative())
|
|
|
|
// O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
|
|
|
|
// else
|
2005-04-13 17:17:28 +00:00
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
default:
|
|
|
|
O << "<unknown operand type: " << MO.getType() << ">";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// printMachineInstruction -- Print out a single Alpha MI to
|
|
|
|
/// the current output stream.
|
|
|
|
///
|
|
|
|
void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
|
|
|
|
++EmittedInsts;
|
|
|
|
if (printInstruction(MI))
|
|
|
|
return; // Printer was automatically generated
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
assert(0 && "Unhandled instruction in asm writer!");
|
|
|
|
abort();
|
|
|
|
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
|
|
|
|
|
|
|
// Print out labels for the function.
|
2006-02-04 19:13:09 +00:00
|
|
|
const Function *F = MF.getFunction();
|
|
|
|
SwitchSection(".text", F);
|
|
|
|
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:
|
|
|
|
O << "\t.weak " << CurrentFnName << "\n";
|
|
|
|
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) {
|
|
|
|
// Print a label for the basic block.
|
2005-11-21 07:38:08 +00:00
|
|
|
O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
|
|
|
|
<< ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\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.
|
|
|
|
O << "\t";
|
|
|
|
printMachineInstruction(II);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++LabelNumber;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
AsmPrinter::doInitialization(M);
|
2005-09-29 22:54:56 +00:00
|
|
|
if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
|
|
|
|
|| TM.getSubtarget<AlphaSubtarget>().hasCT())
|
2005-04-14 16:24:00 +00:00
|
|
|
O << "\t.arch ev6\n";
|
|
|
|
else
|
|
|
|
O << "\t.arch ev56\n";
|
2005-07-22 20:52:16 +00:00
|
|
|
O << "\t.set noat\n";
|
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) {
|
|
|
|
const TargetData &TD = TM.getTargetData();
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-03-15 04:54:21 +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
|
|
|
if (I->hasInitializer()) { // External global require no code
|
2006-03-09 06:14:35 +00:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
|
|
|
continue;
|
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
|
|
|
unsigned Size = TD.getTypeSize(C->getType());
|
2006-02-06 17:15:17 +00:00
|
|
|
// unsigned Align = TD.getTypeAlignmentShift(C->getType());
|
|
|
|
unsigned Align = getPreferredAlignmentLog(I);
|
2005-01-22 23:41:55 +00:00
|
|
|
|
2005-04-21 23:13:11 +00:00
|
|
|
if (C->isNullValue() &&
|
2005-01-22 23:41:55 +00:00
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
2005-11-21 07:30:28 +00:00
|
|
|
SwitchSection("\t.section .data", I);
|
2005-01-22 23:41:55 +00:00
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
2005-04-21 23:13:11 +00:00
|
|
|
|
2005-01-22 23:41:55 +00:00
|
|
|
O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
|
2006-02-27 10:29:04 +00:00
|
|
|
<< "," << (1 << Align)
|
|
|
|
<< "\n";
|
2005-01-22 23:41:55 +00:00
|
|
|
} else {
|
|
|
|
switch (I->getLinkage()) {
|
|
|
|
case GlobalValue::LinkOnceLinkage:
|
|
|
|
case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
|
|
|
|
// Nonnull linkonce -> weak
|
|
|
|
O << "\t.weak " << name << "\n";
|
|
|
|
O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
|
2005-11-21 07:30:28 +00:00
|
|
|
SwitchSection("", I);
|
2005-01-22 23:41:55 +00:00
|
|
|
break;
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// FIXME: appending linkage variables should go into a section of
|
|
|
|
// their name or something. For now, just emit them as external.
|
|
|
|
case GlobalValue::ExternalLinkage:
|
|
|
|
// If external or appending, declare as a global symbol
|
|
|
|
O << "\t.globl " << name << "\n";
|
|
|
|
// FALL THROUGH
|
|
|
|
case GlobalValue::InternalLinkage:
|
2005-11-21 07:30:28 +00:00
|
|
|
SwitchSection(C->isNullValue() ? "\t.section .bss" :
|
|
|
|
"\t.section .data", I);
|
2005-01-22 23:41:55 +00:00
|
|
|
break;
|
|
|
|
case GlobalValue::GhostLinkage:
|
2005-03-10 19:02:02 +00:00
|
|
|
std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
|
2005-01-22 23:41:55 +00:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2005-11-21 07:51:23 +00:00
|
|
|
EmitAlignment(Align);
|
2005-01-22 23:41:55 +00:00
|
|
|
O << "\t.type " << name << ",@object\n";
|
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
2006-02-27 10:29:04 +00:00
|
|
|
O << name << ":\n";
|
2005-11-21 07:51:23 +00:00
|
|
|
EmitGlobalConstant(C);
|
2005-01-22 23:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false;
|
|
|
|
}
|