2001-09-19 13:47:27 +00:00
|
|
|
//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
|
|
|
|
//
|
|
|
|
// This file implements all of the stuff neccesary to output a .s file from
|
|
|
|
// LLVM. The code in this file assumes that the specified module has already
|
|
|
|
// been compiled into the internal data structures of the Module.
|
|
|
|
//
|
|
|
|
// The entry point of this file is the UltraSparc::emitAssembly method.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SparcInternals.h"
|
|
|
|
#include "llvm/Analysis/SlotCalculator.h"
|
2001-10-28 21:38:52 +00:00
|
|
|
#include "llvm/Transforms/Linker.h"
|
2001-09-19 13:47:27 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2001-10-28 21:38:52 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
|
|
|
#include "llvm/GlobalValue.h"
|
|
|
|
#include "llvm/ConstPoolVals.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-09-19 13:47:27 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-10-28 21:38:52 +00:00
|
|
|
#include "llvm/Support/HashExtras.h"
|
2001-09-19 13:47:27 +00:00
|
|
|
#include "llvm/Support/StringExtras.h"
|
2001-11-08 05:12:37 +00:00
|
|
|
#include <locale.h>
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
class SparcAsmPrinter {
|
2001-10-28 21:38:52 +00:00
|
|
|
typedef hash_map<const Value*, int> ValIdMap;
|
|
|
|
typedef ValIdMap:: iterator ValIdMapIterator;
|
|
|
|
typedef ValIdMap::const_iterator ValIdMapConstIterator;
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
ostream &toAsm;
|
2001-10-28 21:38:52 +00:00
|
|
|
SlotCalculator Table; // map anonymous values to unique integer IDs
|
|
|
|
ValIdMap valToIdMap; // used for values not handled by SlotCalculator
|
2001-10-15 15:54:43 +00:00
|
|
|
const UltraSparc &Target;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
enum Sections {
|
|
|
|
Unknown,
|
|
|
|
Text,
|
2001-10-28 21:38:52 +00:00
|
|
|
ReadOnlyData,
|
|
|
|
InitRWData,
|
|
|
|
UninitRWData,
|
2001-09-19 13:47:27 +00:00
|
|
|
} CurSection;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
public:
|
2001-10-15 15:54:43 +00:00
|
|
|
inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t)
|
2001-11-08 05:12:37 +00:00
|
|
|
: toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) {
|
2001-09-19 13:47:27 +00:00
|
|
|
emitModule(M);
|
|
|
|
}
|
|
|
|
|
|
|
|
private :
|
|
|
|
void emitModule(const Module *M);
|
|
|
|
void emitMethod(const Method *M);
|
2001-10-28 21:38:52 +00:00
|
|
|
void emitGlobalsAndConstants(const Module* module);
|
2001-09-19 13:47:27 +00:00
|
|
|
//void processMethodArgument(const MethodArgument *MA);
|
|
|
|
void emitBasicBlock(const BasicBlock *BB);
|
|
|
|
void emitMachineInst(const MachineInstr *MI);
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void printGlobalVariable(const GlobalVariable* GV);
|
|
|
|
void printConstant(const ConstPoolVal* CV, string valID = string(""));
|
|
|
|
|
|
|
|
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
void printOneOperand(const MachineOperand &Op);
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
// enterSection - Use this method to enter a different section of the output
|
|
|
|
// executable. This is used to only output neccesary section transitions.
|
|
|
|
//
|
|
|
|
void enterSection(enum Sections S) {
|
|
|
|
if (S == CurSection) return; // Only switch section if neccesary
|
|
|
|
CurSection = S;
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n\t.section ";
|
2001-10-28 21:38:52 +00:00
|
|
|
switch (S)
|
|
|
|
{
|
|
|
|
default: assert(0 && "Bad section name!");
|
2001-11-08 05:12:37 +00:00
|
|
|
case Text: toAsm << "\".text\""; break;
|
|
|
|
case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
|
|
|
|
case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
|
|
|
|
case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
string getValidSymbolName(const string &S) {
|
2001-09-28 15:07:24 +00:00
|
|
|
string Result;
|
2001-11-08 05:12:37 +00:00
|
|
|
|
|
|
|
// Symbol names in Sparc assembly language have these rules:
|
|
|
|
// (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
|
|
|
|
// (b) A name beginning in "." is treated as a local name.
|
|
|
|
// (c) Names beginning with "_" are reserved by ANSI C and shd not be used.
|
|
|
|
//
|
|
|
|
if (S[0] == '_' || isdigit(S[0]))
|
|
|
|
Result += "ll";
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < S.size(); ++i)
|
|
|
|
{
|
|
|
|
char C = S[i];
|
|
|
|
if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C))
|
|
|
|
Result += C;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Result += '_';
|
|
|
|
Result += char('0' + ((unsigned char)C >> 4));
|
|
|
|
Result += char('0' + (C & 0xF));
|
|
|
|
}
|
2001-09-28 15:07:24 +00:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
// getID - Return a valid identifier for the specified value. Base it on
|
|
|
|
// the name of the identifier if possible, use a numbered value based on
|
|
|
|
// prefix otherwise. FPrefix is always prepended to the output identifier.
|
|
|
|
//
|
|
|
|
string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
|
2001-11-08 05:12:37 +00:00
|
|
|
string Result;
|
2001-09-19 13:47:27 +00:00
|
|
|
string FP(FPrefix ? FPrefix : ""); // "Forced prefix"
|
|
|
|
if (V->hasName()) {
|
2001-11-08 05:12:37 +00:00
|
|
|
Result = FP + V->getName();
|
2001-09-19 13:47:27 +00:00
|
|
|
} else {
|
2001-10-28 21:38:52 +00:00
|
|
|
int valId = Table.getValSlot(V);
|
|
|
|
if (valId == -1) {
|
|
|
|
ValIdMapConstIterator I = valToIdMap.find(V);
|
|
|
|
valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size())
|
|
|
|
: (*I).second;
|
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
Result = FP + string(Prefix) + itostr(valId);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
return getValidSymbolName(Result);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
// getID Wrappers - Ensure consistent usage...
|
2001-10-28 21:38:52 +00:00
|
|
|
string getID(const Module *M) {
|
|
|
|
return getID(M, "LLVMModule_");
|
|
|
|
}
|
|
|
|
string getID(const Method *M) {
|
|
|
|
return getID(M, "LLVMMethod_");
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
string getID(const BasicBlock *BB) {
|
2001-10-28 21:38:52 +00:00
|
|
|
return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
string getID(const GlobalVariable *GV) {
|
|
|
|
return getID(GV, "LLVMGlobal_", ".G_");
|
|
|
|
}
|
|
|
|
string getID(const ConstPoolVal *CV) {
|
|
|
|
return getID(CV, "LLVMConst_", ".C_");
|
|
|
|
}
|
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
unsigned getOperandMask(unsigned Opcode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case SUBcc: return 1 << 3; // Remove CC argument
|
2001-09-28 15:07:24 +00:00
|
|
|
case BA: case BRZ: // Remove Arg #0, which is always null or xcc
|
|
|
|
case BRLEZ: case BRLZ:
|
|
|
|
case BRNZ: case BRGZ:
|
|
|
|
case BRGEZ: return 1 << 0;
|
2001-10-01 02:32:34 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
default: return 0; // By default, don't hack operands...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
|
|
|
SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
|
|
|
|
unsigned int opNum) {
|
|
|
|
switch (MI->getOpCode()) {
|
|
|
|
case JMPLCALL:
|
|
|
|
case JMPLRET: return (opNum == 0);
|
|
|
|
default: return false;
|
2001-10-15 19:21:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
|
|
|
SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
|
|
|
|
unsigned int opNum) {
|
|
|
|
if (Target.getInstrInfo().isLoad(MI->getOpCode()))
|
|
|
|
return (opNum == 0);
|
|
|
|
else if (Target.getInstrInfo().isStore(MI->getOpCode()))
|
|
|
|
return (opNum == 1);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
#define PrintOp1PlusOp2(Op1, Op2) \
|
2001-11-08 05:12:37 +00:00
|
|
|
printOneOperand(Op1); toAsm << "+"; printOneOperand(Op2);
|
2001-10-15 19:21:31 +00:00
|
|
|
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
unsigned int
|
|
|
|
SparcAsmPrinter::printOperands(const MachineInstr *MI,
|
|
|
|
unsigned int opNum)
|
|
|
|
{
|
|
|
|
const MachineOperand& Op = MI->getOperand(opNum);
|
|
|
|
|
|
|
|
if (OpIsBranchTargetLabel(MI, opNum))
|
|
|
|
{
|
|
|
|
PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (OpIsMemoryAddressBase(MI, opNum))
|
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "[";
|
2001-10-28 21:38:52 +00:00
|
|
|
PrintOp1PlusOp2(Op, MI->getOperand(opNum+1));
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "]";
|
2001-10-28 21:38:52 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printOneOperand(Op);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2001-10-24 22:05:34 +00:00
|
|
|
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
2001-11-08 05:12:37 +00:00
|
|
|
SparcAsmPrinter::printOneOperand(const MachineOperand &op)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
switch (op.getOperandType())
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
case MachineOperand::MO_CCRegister:
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
int RegNum = (int)op.getAllocatedRegNum();
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
// ****this code is temporary till NULL Values are fixed
|
|
|
|
if (RegNum == 10000) {
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "<NULL VALUE>";
|
2001-10-28 21:38:52 +00:00
|
|
|
} else {
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
const Value *Val = op.getVRegValue();
|
2001-10-28 21:38:52 +00:00
|
|
|
if (!Val)
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\t<*NULL Value*>";
|
2001-10-28 21:38:52 +00:00
|
|
|
else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(BB);
|
2001-10-28 21:38:52 +00:00
|
|
|
else if (const Method *M = dyn_cast<const Method>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(M);
|
2001-10-28 21:38:52 +00:00
|
|
|
else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(GV);
|
2001-10-28 21:38:52 +00:00
|
|
|
else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(CV);
|
2001-10-28 21:38:52 +00:00
|
|
|
else
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "<unknown value=" << Val << ">";
|
2001-10-28 21:38:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << op.getImmedValue();
|
2001-10-28 21:38:52 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << op; // use dump field
|
2001-10-28 21:38:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-10-24 22:05:34 +00:00
|
|
|
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
|
|
|
SparcAsmPrinter::emitMachineInst(const MachineInstr *MI)
|
|
|
|
{
|
|
|
|
unsigned Opcode = MI->getOpCode();
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
|
|
|
|
return; // IGNORE PHI NODES
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
unsigned Mask = getOperandMask(Opcode);
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
bool NeedComma = false;
|
2001-10-28 21:38:52 +00:00
|
|
|
unsigned N = 1;
|
|
|
|
for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N)
|
|
|
|
if (! ((1 << OpNum) & Mask)) { // Ignore this operand?
|
2001-11-08 05:12:37 +00:00
|
|
|
if (NeedComma) toAsm << ", "; // Handle comma outputing
|
2001-10-28 21:38:52 +00:00
|
|
|
NeedComma = true;
|
|
|
|
N = printOperands(MI, OpNum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
N = 1;
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << endl;
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
|
|
|
SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB)
|
|
|
|
{
|
2001-09-19 13:47:27 +00:00
|
|
|
// Emit a label for the basic block
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(BB) << ":\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// Get the vector of machine instructions corresponding to this bb.
|
|
|
|
const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec();
|
|
|
|
MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end();
|
|
|
|
|
|
|
|
// Loop over all of the instructions in the basic block...
|
|
|
|
for (; MII != MIE; ++MII)
|
|
|
|
emitMachineInst(*MII);
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n"; // Seperate BB's with newlines
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
|
|
|
SparcAsmPrinter::emitMethod(const Method *M)
|
|
|
|
{
|
2001-09-19 13:47:27 +00:00
|
|
|
if (M->isExternal()) return;
|
|
|
|
|
|
|
|
// Make sure the slot table has information about this method...
|
|
|
|
Table.incorporateMethod(M);
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
string methName = getID(M);
|
|
|
|
toAsm << "!****** Outputing Method: " << methName << " ******\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
enterSection(Text);
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
|
|
|
|
//toAsm << "\t.type\t" << methName << ",#function\n";
|
|
|
|
toAsm << "\t.type\t" << methName << ", 2\n";
|
|
|
|
toAsm << methName << ":\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// Output code for all of the basic blocks in the method...
|
|
|
|
for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
emitBasicBlock(*I);
|
|
|
|
|
|
|
|
// Output a .size directive so the debugger knows the extents of the function
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << ".EndOf_" << methName << ":\n\t.size "
|
|
|
|
<< methName << ", .EndOf_"
|
|
|
|
<< methName << "-" << methName << endl;
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// Put some spaces between the methods
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// Forget all about M.
|
|
|
|
Table.purgeMethod();
|
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
|
|
|
ArrayTypeIsString(ArrayType* arrayType)
|
|
|
|
{
|
|
|
|
return (arrayType->getElementType() == Type::UByteTy ||
|
|
|
|
arrayType->getElementType() == Type::SByteTy);
|
|
|
|
}
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
inline const string TypeToDataDirective(const Type* type)
|
|
|
|
{
|
|
|
|
switch(type->getPrimitiveID())
|
|
|
|
{
|
|
|
|
case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
|
|
|
|
return ".byte";
|
|
|
|
case Type::UShortTyID: case Type::ShortTyID:
|
|
|
|
return ".half";
|
|
|
|
case Type::UIntTyID: case Type::IntTyID:
|
|
|
|
return ".word";
|
|
|
|
case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
|
|
|
|
return ".xword";
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return ".single";
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return ".double";
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
if (ArrayTypeIsString((ArrayType*) type))
|
|
|
|
return ".ascii";
|
|
|
|
else
|
|
|
|
return "<InvaliDataTypeForPrinting>";
|
|
|
|
default:
|
2001-10-28 21:38:52 +00:00
|
|
|
return "<InvaliDataTypeForPrinting>";
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int ConstantToSize(const ConstPoolVal* CV,
|
|
|
|
const TargetMachine& target) {
|
|
|
|
if (ConstPoolArray* AV = dyn_cast<ConstPoolArray>(CV))
|
|
|
|
if (ArrayTypeIsString((ArrayType*) CV->getType()))
|
|
|
|
return 1 + AV->getNumOperands();
|
|
|
|
|
|
|
|
return target.findOptimalStorageSize(CV->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline
|
|
|
|
unsigned int TypeToSize(const Type* type, const TargetMachine& target)
|
|
|
|
{
|
|
|
|
return target.findOptimalStorageSize(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline unsigned int
|
|
|
|
TypeToAlignment(const Type* type, const TargetMachine& target)
|
|
|
|
{
|
|
|
|
if (type->getPrimitiveID() == Type::ArrayTyID &&
|
|
|
|
ArrayTypeIsString((ArrayType*) type))
|
|
|
|
return target.findOptimalStorageSize(Type::LongTy);
|
|
|
|
|
|
|
|
return target.findOptimalStorageSize(type);
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID)
|
|
|
|
{
|
|
|
|
if (valID.length() == 0)
|
|
|
|
valID = getID(CV);
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
assert(CV->getType() != Type::VoidTy &&
|
|
|
|
CV->getType() != Type::TypeTy &&
|
|
|
|
CV->getType() != Type::LabelTy &&
|
|
|
|
"Unexpected type for ConstPoolVal");
|
|
|
|
|
|
|
|
toAsm << "\t.align\t" << TypeToAlignment(CV->getType(), Target)
|
|
|
|
<< endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << valID << ":" << endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\t"
|
|
|
|
<< TypeToDataDirective(CV->getType()) << "\t";
|
|
|
|
|
|
|
|
if (ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV))
|
2001-10-29 13:39:38 +00:00
|
|
|
if (isStringCompatible(CPA))
|
2001-11-08 05:12:37 +00:00
|
|
|
{
|
|
|
|
toAsm << getAsCString(CPA) << endl;
|
|
|
|
return;
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
if (CV->getType()->isPrimitiveType())
|
|
|
|
{
|
|
|
|
if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy)
|
|
|
|
toAsm << "0r"; // FP constants must have this prefix
|
|
|
|
toAsm << CV->getStrValue() << endl;
|
|
|
|
}
|
|
|
|
else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV))
|
|
|
|
{
|
|
|
|
if (! CPP->isNullValue())
|
|
|
|
assert(0 && "Cannot yet print non-null pointer constants to assembly");
|
|
|
|
else
|
2001-11-08 14:29:57 +00:00
|
|
|
toAsm << (void*) NULL << endl;
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
|
|
|
else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV))
|
|
|
|
{
|
|
|
|
assert(0 && "Cannot yet initialize pointer refs in assembly");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0 && "Cannot yet print non-primitive constants to assembly");
|
|
|
|
// toAsm << CV->getStrValue() << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
toAsm << "\t.type" << "\t" << valID << ",#object" << endl;
|
|
|
|
toAsm << "\t.size" << "\t" << valID << ","
|
|
|
|
<< ConstantToSize(CV, Target) << endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
|
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\t.global\t" << getID(GV) << endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
printConstant(GV->getInitializer(), getID(GV));
|
|
|
|
else {
|
2001-11-08 14:29:57 +00:00
|
|
|
toAsm << "\t.align\t"
|
|
|
|
<< TypeToAlignment(GV->getType()->getValueType(), Target) << endl;
|
|
|
|
toAsm << "\t.type\t" << getID(GV) << ",#object" << endl;
|
|
|
|
toAsm << "\t.reserve\t" << getID(GV) << ","
|
2001-11-08 05:12:37 +00:00
|
|
|
<< TypeToSize(GV->getType()->getValueType(), Target)
|
|
|
|
<< endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
FoldConstPools(const Module *M,
|
2001-11-08 05:12:37 +00:00
|
|
|
hash_set<const ConstPoolVal*>& moduleConstPool)
|
|
|
|
{
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (! (*I)->isExternal())
|
|
|
|
{
|
|
|
|
const hash_set<const ConstPoolVal*>& pool =
|
|
|
|
MachineCodeForMethod::get(*I).getConstantPoolValues();
|
|
|
|
moduleConstPool.insert(pool.begin(), pool.end());
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SparcAsmPrinter::emitGlobalsAndConstants(const Module *M)
|
|
|
|
{
|
|
|
|
// First, get the constants there were marked by the code generator for
|
|
|
|
// inclusion in the assembly code data area and fold them all into a
|
|
|
|
// single constant pool since there may be lots of duplicates. Also,
|
|
|
|
// lets force these constants into the slot table so that we can get
|
|
|
|
// unique names for unnamed constants also.
|
|
|
|
//
|
|
|
|
hash_set<const ConstPoolVal*> moduleConstPool;
|
|
|
|
FoldConstPools(M, moduleConstPool);
|
|
|
|
|
|
|
|
// Now, emit the three data sections separately; the cost of I/O should
|
|
|
|
// make up for the cost of extra passes over the globals list!
|
|
|
|
//
|
|
|
|
// Read-only data section (implies initialized)
|
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
|
|
|
{
|
|
|
|
const GlobalVariable* GV = *GI;
|
|
|
|
if (GV->hasInitializer() && GV->isConstant())
|
|
|
|
{
|
|
|
|
if (GI == M->gbegin())
|
|
|
|
enterSection(ReadOnlyData);
|
|
|
|
printGlobalVariable(GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(),
|
|
|
|
E = moduleConstPool.end(); I != E; ++I)
|
|
|
|
printConstant(*I);
|
|
|
|
|
|
|
|
// Initialized read-write data section
|
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
|
|
|
{
|
|
|
|
const GlobalVariable* GV = *GI;
|
|
|
|
if (GV->hasInitializer() && ! GV->isConstant())
|
|
|
|
{
|
|
|
|
if (GI == M->gbegin())
|
|
|
|
enterSection(InitRWData);
|
|
|
|
printGlobalVariable(GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uninitialized read-write data section
|
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
|
|
|
{
|
|
|
|
const GlobalVariable* GV = *GI;
|
|
|
|
if (! GV->hasInitializer())
|
|
|
|
{
|
|
|
|
if (GI == M->gbegin())
|
|
|
|
enterSection(UninitRWData);
|
|
|
|
printGlobalVariable(GV);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << endl;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
SparcAsmPrinter::emitModule(const Module *M)
|
|
|
|
{
|
2001-09-19 13:47:27 +00:00
|
|
|
// TODO: Look for a filename annotation on M to emit a .file directive
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
emitMethod(*I);
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
emitGlobalsAndConstants(M);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
//
|
|
|
|
// emitAssembly - Output assembly language code (a .s file) for the specified
|
|
|
|
// method. The specified method must have been compiled before this may be
|
|
|
|
// used.
|
|
|
|
//
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
2001-11-08 05:12:37 +00:00
|
|
|
UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
SparcAsmPrinter Print(toAsm, M, *this);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|