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"
|
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"
|
|
|
|
#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");
|
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.
|
|
|
|
///
|
|
|
|
unsigned LabelNumber;
|
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-09-06 18:34:40 +00:00
|
|
|
: AsmPrinter(o, tm, T), LabelNumber(0) {
|
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);
|
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,
|
|
|
|
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();
|
|
|
|
int new_symbol;
|
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:
|
2005-01-22 23:41:55 +00:00
|
|
|
std::cerr << "printOp() does not handle immediate values\n";
|
|
|
|
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;
|
|
|
|
|
2005-04-13 17:17:28 +00:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
2006-05-04 00:49:59 +00:00
|
|
|
O << Mang->getValueName(MO.getGlobal());
|
2005-01-22 23:41:55 +00:00
|
|
|
return;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
|
|
|
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:
|
|
|
|
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) {
|
2006-05-02 05:37:32 +00:00
|
|
|
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.
|
|
|
|
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) {
|
2006-05-03 01:29:57 +00:00
|
|
|
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();
|
2006-05-03 01:29:57 +00:00
|
|
|
unsigned Size = TD->getTypeSize(C->getType());
|
|
|
|
// unsigned Align = TD->getTypeAlignmentShift(C->getType());
|
2006-02-06 17:15:17 +00:00
|
|
|
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 */)) {
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection("\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
|
|
|
|
2006-05-03 01:29:57 +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";
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection("", 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:
|
2006-05-09 04:59:56 +00:00
|
|
|
SwitchToDataSection(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();
|
2006-09-14 18:23:27 +00:00
|
|
|
case GlobalValue::DLLImportLinkage:
|
|
|
|
std::cerr << "DLLImport linkage is not supported by this target!\n";
|
|
|
|
abort();
|
|
|
|
case GlobalValue::DLLExportLinkage:
|
|
|
|
std::cerr << "DLLExport linkage is not supported by this target!\n";
|
|
|
|
abort();
|
|
|
|
default:
|
|
|
|
assert(0 && "Unknown linkage type!");
|
2005-01-22 23:41:55 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
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,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
|
|
|
printOperand(MI, OpNo);
|
|
|
|
return false;
|
|
|
|
}
|
2006-06-21 15:42:36 +00:00
|
|
|
|
|
|
|
bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
|
|
|
|
unsigned OpNo,
|
|
|
|
unsigned AsmVariant,
|
|
|
|
const char *ExtraCode) {
|
|
|
|
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;
|
|
|
|
}
|