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
|
|
|
|
//
|
|
|
|
// 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-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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Support/Mangler.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2006-01-23 07:41:00 +08:00
|
|
|
#include <iostream>
|
2005-01-23 07:41:55 +08: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-22 07:13:11 +08:00
|
|
|
|
|
|
|
AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
|
2005-11-21 15:38:08 +08:00
|
|
|
: AsmPrinter(o, tm), LabelNumber(0) {
|
2005-02-04 22:09:38 +08:00
|
|
|
AlignmentIsInBytes = false;
|
2005-11-21 14:51:52 +08:00
|
|
|
PrivateGlobalPrefix = "$";
|
2005-02-04 22:09:38 +08:00
|
|
|
}
|
2005-01-23 07:41:55 +08: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-14 01:17:28 +08:00
|
|
|
std::string CurSection;
|
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);
|
|
|
|
void printMachineInstruction(const MachineInstr *MI);
|
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);
|
|
|
|
};
|
|
|
|
} // 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-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) {
|
2005-01-23 07:41:55 +08:00
|
|
|
assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
|
2005-01-25 02:37:48 +08:00
|
|
|
O << TM.getRegisterInfo()->get(MO.getReg()).Name;
|
2005-01-23 07:41:55 +08:00
|
|
|
} else if (MO.isImmediate()) {
|
|
|
|
O << MO.getImmedValue();
|
2006-05-18 03:24:31 +08:00
|
|
|
assert(MO.getImmedValue() < (1 << 30));
|
2005-01-23 07:41:55 +08:00
|
|
|
} else {
|
|
|
|
printOp(MO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
|
|
|
|
const MRegisterInfo &RI = *TM.getRegisterInfo();
|
|
|
|
int new_symbol;
|
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:
|
2005-01-25 02:37:48 +08:00
|
|
|
O << RI.get(MO.getReg()).Name;
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
2006-05-05 01:21:20 +08:00
|
|
|
case MachineOperand::MO_Immediate:
|
2005-01-23 07:41:55 +08:00
|
|
|
std::cerr << "printOp() does not handle immediate values\n";
|
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
case MachineOperand::MO_MachineBasicBlock:
|
|
|
|
printBasicBlockLabel(MO.getMachineBasicBlock());
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
2005-11-21 16:29:17 +08:00
|
|
|
O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
|
2005-11-21 14:51:52 +08:00
|
|
|
<< MO.getConstantPoolIndex();
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
case MachineOperand::MO_ExternalSymbol:
|
|
|
|
O << MO.getSymbolName();
|
|
|
|
return;
|
|
|
|
|
2005-04-14 01:17:28 +08:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2006-05-04 08:49:59 +08:00
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2005-01-23 07:41:55 +08:00
|
|
|
return;
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2005-01-23 07:41:55 +08: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-22 07:13:11 +08:00
|
|
|
|
2005-01-23 07:41:55 +08: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 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
|
|
|
|
|
|
|
// Print out labels for the function.
|
2006-02-05 03:13:09 +08:00
|
|
|
const Function *F = MF.getFunction();
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToTextSection(".text", F);
|
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:
|
|
|
|
O << "\t.weak " << CurrentFnName << "\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
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-05-02 13:37:32 +08:00
|
|
|
printBasicBlockLabel(I, true);
|
|
|
|
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.
|
|
|
|
O << "\t";
|
|
|
|
printMachineInstruction(II);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++LabelNumber;
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
AsmPrinter::doInitialization(M);
|
2005-09-30 06:54:56 +08:00
|
|
|
if(TM.getSubtarget<AlphaSubtarget>().hasF2I()
|
|
|
|
|| TM.getSubtarget<AlphaSubtarget>().hasCT())
|
2005-04-15 00:24:00 +08:00
|
|
|
O << "\t.arch ev6\n";
|
|
|
|
else
|
|
|
|
O << "\t.arch ev56\n";
|
2005-07-23 04:52:16 +08:00
|
|
|
O << "\t.set noat\n";
|
2005-01-23 07:41:55 +08:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
bool AlphaAsmPrinter::doFinalization(Module &M) {
|
2006-05-03 09:29:57 +08:00
|
|
|
const TargetData *TD = TM.getTargetData();
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2005-03-15 12:54:21 +08:00
|
|
|
for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
|
2005-01-23 07:41:55 +08:00
|
|
|
if (I->hasInitializer()) { // External global require no code
|
2006-03-09 14:14:35 +08:00
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(I))
|
|
|
|
continue;
|
|
|
|
|
2005-01-23 07:41:55 +08:00
|
|
|
O << "\n\n";
|
|
|
|
std::string name = Mang->getValueName(I);
|
|
|
|
Constant *C = I->getInitializer();
|
2006-05-03 09:29:57 +08:00
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
|
|
|
// unsigned Align = TD->getTypeAlignmentShift(C->getType());
|
2006-02-07 01:15:17 +08:00
|
|
|
unsigned Align = getPreferredAlignmentLog(I);
|
2005-01-23 07:41:55 +08:00
|
|
|
|
2005-04-22 07:13:11 +08:00
|
|
|
if (C->isNullValue() &&
|
2005-01-23 07:41:55 +08:00
|
|
|
(I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
|
|
|
|
I->hasWeakLinkage() /* FIXME: Verify correct */)) {
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection("\t.section .data", I);
|
2005-01-23 07:41:55 +08:00
|
|
|
if (I->hasInternalLinkage())
|
|
|
|
O << "\t.local " << name << "\n";
|
2005-04-22 07:13:11 +08:00
|
|
|
|
2006-05-03 09:29:57 +08:00
|
|
|
O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
|
2006-02-27 18:29:04 +08:00
|
|
|
<< "," << (1 << Align)
|
|
|
|
<< "\n";
|
2005-01-23 07:41:55 +08: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";
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection("", I);
|
2005-01-23 07:41:55 +08: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:
|
2006-05-09 12:59:56 +08:00
|
|
|
SwitchToDataSection(C->isNullValue() ? "\t.section .bss" :
|
|
|
|
"\t.section .data", I);
|
2005-01-23 07:41:55 +08:00
|
|
|
break;
|
|
|
|
case GlobalValue::GhostLinkage:
|
2005-03-11 03:02:02 +08:00
|
|
|
std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
|
2005-01-23 07:41:55 +08:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2005-11-21 15:51:23 +08:00
|
|
|
EmitAlignment(Align);
|
2005-01-23 07:41:55 +08:00
|
|
|
O << "\t.type " << name << ",@object\n";
|
|
|
|
O << "\t.size " << name << "," << Size << "\n";
|
2006-02-27 18:29:04 +08:00
|
|
|
O << name << ":\n";
|
2005-11-21 15:51:23 +08:00
|
|
|
EmitGlobalConstant(C);
|
2005-01-23 07:41:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AsmPrinter::doFinalization(M);
|
|
|
|
return false;
|
|
|
|
}
|