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.
|
|
|
|
//
|
2002-04-27 06:56:12 +00:00
|
|
|
// This code largely consists of two LLVM Pass's: a FunctionPass and a Pass.
|
|
|
|
// The FunctionPass is pipelined together with all of the rest of the code
|
|
|
|
// generation stages, and the Pass runs at the end to emit code for global
|
|
|
|
// variables and such.
|
2001-09-19 13:47:27 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SparcInternals.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-03 07:48:06 +00:00
|
|
|
#include "llvm/CodeGen/MachineCodeForMethod.h"
|
2001-10-28 21:38:52 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-10-28 21:38:52 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2002-03-18 03:07:26 +00:00
|
|
|
#include "llvm/Annotation.h"
|
2001-09-19 13:47:27 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-09-19 13:47:27 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-04-07 22:49:37 +00:00
|
|
|
#include "llvm/SlotCalculator.h"
|
2002-04-28 20:40:59 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-04-18 18:15:38 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/StringExtras.h"
|
|
|
|
#include "Support/HashExtras.h"
|
2002-03-03 20:46:32 +00:00
|
|
|
#include <iostream>
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::string;
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2002-03-18 03:07:26 +00:00
|
|
|
class GlobalIdTable: public Annotation {
|
|
|
|
static AnnotationID AnnotId;
|
|
|
|
friend class AsmPrinter; // give access to AnnotId
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
typedef std::hash_map<const Value*, int> ValIdMap;
|
2001-10-28 21:38:52 +00:00
|
|
|
typedef ValIdMap::const_iterator ValIdMapConstIterator;
|
2002-03-18 03:07:26 +00:00
|
|
|
typedef ValIdMap:: iterator ValIdMapIterator;
|
|
|
|
public:
|
2002-04-28 04:50:54 +00:00
|
|
|
SlotCalculator Table; // map anonymous values to unique integer IDs
|
2002-02-03 23:41:08 +00:00
|
|
|
ValIdMap valToIdMap; // used for values not handled by SlotCalculator
|
2002-03-18 03:07:26 +00:00
|
|
|
|
2002-04-28 04:50:54 +00:00
|
|
|
GlobalIdTable(Module* M) : Annotation(AnnotId), Table(M, true) {}
|
2002-03-18 03:07:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
AnnotationID GlobalIdTable::AnnotId =
|
|
|
|
AnnotationManager::getID("ASM PRINTER GLOBAL TABLE ANNOT");
|
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Code Shared By the two printer passes, as a mixin
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
class AsmPrinter {
|
|
|
|
GlobalIdTable* idTable;
|
2002-02-03 23:41:08 +00:00
|
|
|
public:
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostream &toAsm;
|
2002-02-04 15:53:23 +00:00
|
|
|
const TargetMachine &Target;
|
2002-03-18 03:07:26 +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;
|
|
|
|
|
2002-02-04 15:53:23 +00:00
|
|
|
AsmPrinter(std::ostream &os, const TargetMachine &T)
|
2002-03-18 03:07:26 +00:00
|
|
|
: idTable(0), toAsm(os), Target(T), CurSection(Unknown) {}
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// (start|end)(Module|Function) - Callback methods to be invoked by subclasses
|
2002-02-03 23:41:08 +00:00
|
|
|
void startModule(Module *M) {
|
2002-03-18 03:07:26 +00:00
|
|
|
// Create the global id table if it does not already exist
|
|
|
|
idTable = (GlobalIdTable*) M->getAnnotation(GlobalIdTable::AnnotId);
|
|
|
|
if (idTable == NULL) {
|
|
|
|
idTable = new GlobalIdTable(M);
|
|
|
|
M->addAnnotation(idTable);
|
|
|
|
}
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2002-04-07 20:49:59 +00:00
|
|
|
void startFunction(Function *F) {
|
2002-04-07 22:49:37 +00:00
|
|
|
// Make sure the slot table has information about this function...
|
2002-04-28 04:50:54 +00:00
|
|
|
idTable->Table.incorporateFunction(F);
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2002-04-07 20:49:59 +00:00
|
|
|
void endFunction(Function *F) {
|
2002-04-28 04:50:54 +00:00
|
|
|
idTable->Table.purgeFunction(); // Forget all about F
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
|
|
|
void endModule() {
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2002-03-18 03:07:26 +00:00
|
|
|
// Check if a name is external or accessible from external code.
|
|
|
|
// Only functions can currently be external. "main" is the only name
|
|
|
|
// that is visible externally.
|
|
|
|
bool isExternal(const Value* V) {
|
2002-04-07 20:49:59 +00:00
|
|
|
const Function *F = dyn_cast<Function>(V);
|
|
|
|
return F && (F->isExternal() || F->getName() == "main");
|
2002-03-18 03:07:26 +00:00
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
static std::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
|
2002-03-18 03:07:26 +00:00
|
|
|
// the name of the identifier if possible (qualified by the type), and
|
|
|
|
// use a numbered value based on prefix otherwise.
|
|
|
|
// FPrefix is always prepended to the output identifier.
|
2001-09-19 13:47:27 +00:00
|
|
|
//
|
|
|
|
string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
|
2002-03-18 03:07:26 +00:00
|
|
|
string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
|
|
|
|
|
|
|
|
Result = Result + (V->hasName()? V->getName() : string(Prefix));
|
|
|
|
|
|
|
|
// Qualify all internal names with a unique id.
|
|
|
|
if (!isExternal(V)) {
|
2002-04-28 04:50:54 +00:00
|
|
|
int valId = idTable->Table.getValSlot(V);
|
2001-10-28 21:38:52 +00:00
|
|
|
if (valId == -1) {
|
2002-03-18 03:07:26 +00:00
|
|
|
GlobalIdTable::ValIdMapConstIterator I = idTable->valToIdMap.find(V);
|
|
|
|
if (I == idTable->valToIdMap.end())
|
|
|
|
valId = idTable->valToIdMap[V] = idTable->valToIdMap.size();
|
2002-02-03 23:41:08 +00:00
|
|
|
else
|
|
|
|
valId = I->second;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2002-03-18 03:07:26 +00:00
|
|
|
Result = Result + "_" + itostr(valId);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
2002-03-18 03:07:26 +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...
|
2002-04-07 20:49:59 +00:00
|
|
|
string getID(const Function *F) {
|
|
|
|
return getID(F, "LLVMFunction_");
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
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_");
|
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
string getID(const Constant *CV) {
|
2001-10-28 21:38:52 +00:00
|
|
|
return getID(CV, "LLVMConst_", ".C_");
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
};
|
|
|
|
|
2001-11-27 00:03:19 +00:00
|
|
|
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-07 20:49:59 +00:00
|
|
|
// SparcFunctionAsmPrinter Code
|
2002-02-03 23:41:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
struct SparcFunctionAsmPrinter : public FunctionPass, public AsmPrinter {
|
2002-04-07 20:49:59 +00:00
|
|
|
inline SparcFunctionAsmPrinter(std::ostream &os, const TargetMachine &t)
|
2002-02-03 23:41:08 +00:00
|
|
|
: AsmPrinter(os, t) {}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
virtual bool doInitialization(Module *M) {
|
|
|
|
startModule(M);
|
|
|
|
return false;
|
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual bool runOnFunction(Function *F) {
|
2002-04-07 20:49:59 +00:00
|
|
|
startFunction(F);
|
|
|
|
emitFunction(F);
|
|
|
|
endFunction(F);
|
2002-02-03 23:41:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
virtual bool doFinalization(Module *M) {
|
|
|
|
endModule();
|
|
|
|
return false;
|
2001-11-27 00:03:19 +00:00
|
|
|
}
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
void emitFunction(const Function *F);
|
2002-02-03 23:41:08 +00:00
|
|
|
private :
|
|
|
|
void emitBasicBlock(const BasicBlock *BB);
|
|
|
|
void emitMachineInst(const MachineInstr *MI);
|
|
|
|
|
|
|
|
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
void printOneOperand(const MachineOperand &Op);
|
|
|
|
|
|
|
|
bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
|
|
|
|
unsigned getOperandMask(unsigned Opcode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case SUBcc: return 1 << 3; // Remove CC argument
|
|
|
|
case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
|
|
|
|
default: return 0; // By default, don't hack operands...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
|
|
|
|
unsigned int opNum) {
|
2001-10-28 21:38:52 +00:00
|
|
|
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
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
|
|
|
|
unsigned int opNum) {
|
2001-10-28 21:38:52 +00:00
|
|
|
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-09 02:19:29 +00:00
|
|
|
printOneOperand(Op1); \
|
|
|
|
toAsm << "+"; \
|
|
|
|
printOneOperand(Op2);
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
unsigned int
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
|
2001-10-28 21:38:52 +00:00
|
|
|
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
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::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
|
|
|
|
2002-03-31 19:03:58 +00:00
|
|
|
// better to print code with NULL registers than to die
|
2001-11-13 23:08:19 +00:00
|
|
|
if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
|
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*>";
|
2002-04-28 19:55:58 +00:00
|
|
|
else if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(BB);
|
2002-04-28 19:55:58 +00:00
|
|
|
else if (const Function *M = dyn_cast<Function>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(M);
|
2002-04-28 19:55:58 +00:00
|
|
|
else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << getID(GV);
|
2002-04-28 19:55:58 +00:00
|
|
|
else if (const Constant *CV = dyn_cast<Constant>(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:
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << (long)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
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::emitMachineInst(const MachineInstr *MI)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::emitBasicBlock(const BasicBlock *BB)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
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
|
2002-04-07 20:49:59 +00:00
|
|
|
SparcFunctionAsmPrinter::emitFunction(const Function *M)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
string methName = getID(M);
|
2002-04-07 20:49:59 +00:00
|
|
|
toAsm << "!****** Outputing Function: " << methName << " ******\n";
|
2002-02-03 23:41:08 +00:00
|
|
|
enterSection(AsmPrinter::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
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Output code for all of the basic blocks in the function...
|
|
|
|
for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
2001-09-19 13:47:27 +00:00
|
|
|
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 "
|
2002-02-03 23:41:08 +00:00
|
|
|
<< methName << ", .EndOf_"
|
|
|
|
<< methName << "-" << methName << "\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// Put some spaces between the functions
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n\n";
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
} // End anonymous namespace
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
Pass *UltraSparc::getFunctionAsmPrinterPass(PassManager &PM, std::ostream &Out){
|
2002-04-07 20:49:59 +00:00
|
|
|
return new SparcFunctionAsmPrinter(Out, *this);
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-04-07 20:49:59 +00:00
|
|
|
// SparcFunctionAsmPrinter Code
|
2002-02-03 23:41:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SparcModuleAsmPrinter : public Pass, public AsmPrinter {
|
|
|
|
public:
|
2002-02-24 23:02:40 +00:00
|
|
|
SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t)
|
|
|
|
: AsmPrinter(os, t) {}
|
2002-02-03 23:41:08 +00:00
|
|
|
|
|
|
|
virtual bool run(Module *M) {
|
|
|
|
startModule(M);
|
|
|
|
emitGlobalsAndConstants(M);
|
|
|
|
endModule();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitGlobalsAndConstants(const Module *M);
|
|
|
|
|
|
|
|
void printGlobalVariable(const GlobalVariable *GV);
|
|
|
|
void printSingleConstant( const Constant* CV);
|
|
|
|
void printConstantValueOnly(const Constant* CV);
|
|
|
|
void printConstant( const Constant* CV, std::string valID = "");
|
|
|
|
|
|
|
|
static void FoldConstants(const Module *M,
|
|
|
|
std::hash_set<const Constant*> &moduleConstants);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Can we treat the specified array as a string? Only if it is an array of
|
|
|
|
// ubytes or non-negative sbytes.
|
|
|
|
//
|
|
|
|
static bool isStringCompatible(ConstantArray *CPA) {
|
|
|
|
const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
|
|
|
|
if (ETy == Type::UByteTy) return true;
|
|
|
|
if (ETy != Type::SByteTy) return false;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < CPA->getNumOperands(); ++i)
|
|
|
|
if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// toOctal - Convert the low order bits of X into an octal letter
|
|
|
|
static inline char toOctal(int X) {
|
|
|
|
return (X&7)+'0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// getAsCString - Return the specified array as a C compatible string, only if
|
|
|
|
// the predicate isStringCompatible is true.
|
|
|
|
//
|
|
|
|
static string getAsCString(ConstantArray *CPA) {
|
2002-04-18 18:15:38 +00:00
|
|
|
assert(isStringCompatible(CPA) && "Array is not string compatible!");
|
|
|
|
|
|
|
|
string Result;
|
|
|
|
const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType();
|
|
|
|
Result = "\"";
|
|
|
|
for (unsigned i = 0; i < CPA->getNumOperands(); ++i) {
|
|
|
|
unsigned char C = (ETy == Type::SByteTy) ?
|
|
|
|
(unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() :
|
|
|
|
(unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue();
|
|
|
|
|
|
|
|
if (isprint(C)) {
|
|
|
|
Result += C;
|
|
|
|
} else {
|
|
|
|
switch(C) {
|
|
|
|
case '\a': Result += "\\a"; break;
|
|
|
|
case '\b': Result += "\\b"; break;
|
|
|
|
case '\f': Result += "\\f"; break;
|
|
|
|
case '\n': Result += "\\n"; break;
|
|
|
|
case '\r': Result += "\\r"; break;
|
|
|
|
case '\t': Result += "\\t"; break;
|
|
|
|
case '\v': Result += "\\v"; break;
|
|
|
|
default:
|
|
|
|
Result += '\\';
|
|
|
|
Result += toOctal(C >> 6);
|
|
|
|
Result += toOctal(C >> 3);
|
|
|
|
Result += toOctal(C >> 0);
|
|
|
|
break;
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-04-18 18:15:38 +00:00
|
|
|
Result += "\"";
|
|
|
|
|
|
|
|
return Result;
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
|
|
|
ArrayTypeIsString(ArrayType* arrayType)
|
|
|
|
{
|
|
|
|
return (arrayType->getElementType() == Type::UByteTy ||
|
|
|
|
arrayType->getElementType() == Type::SByteTy);
|
|
|
|
}
|
|
|
|
|
2001-11-09 02:19:29 +00:00
|
|
|
inline const string
|
|
|
|
TypeToDataDirective(const Type* type)
|
2001-11-08 05:12:37 +00:00
|
|
|
{
|
|
|
|
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:
|
2002-04-11 21:44:02 +00:00
|
|
|
return ".word";
|
2001-11-08 05:12:37 +00:00
|
|
|
case Type::DoubleTyID:
|
2002-04-11 21:44:02 +00:00
|
|
|
return ".xword";
|
2001-11-08 05:12:37 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
// Get the size of the constant for the given target.
|
|
|
|
// If this is an unsized array, return 0.
|
|
|
|
//
|
2001-11-09 02:19:29 +00:00
|
|
|
inline unsigned int
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantToSize(const Constant* CV, const TargetMachine& target)
|
2001-11-09 02:19:29 +00:00
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
|
2001-11-10 02:03:06 +00:00
|
|
|
{
|
|
|
|
ArrayType *aty = cast<ArrayType>(CPA->getType());
|
|
|
|
if (ArrayTypeIsString(aty))
|
|
|
|
return 1 + CPA->getNumOperands();
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
return target.findOptimalStorageSize(CV->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-09 02:19:29 +00:00
|
|
|
// Align data larger than one L1 cache line on L1 cache line boundaries.
|
2001-11-10 02:03:06 +00:00
|
|
|
// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
|
2001-11-09 02:19:29 +00:00
|
|
|
//
|
2001-10-28 21:38:52 +00:00
|
|
|
inline unsigned int
|
2001-11-10 02:03:06 +00:00
|
|
|
SizeToAlignment(unsigned int size, const TargetMachine& target)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-11-09 02:19:29 +00:00
|
|
|
unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
|
2001-11-10 02:03:06 +00:00
|
|
|
if (size > (unsigned) cacheLineSize / 2)
|
2001-11-09 02:19:29 +00:00
|
|
|
return cacheLineSize;
|
|
|
|
else
|
|
|
|
for (unsigned sz=1; /*no condition*/; sz *= 2)
|
2001-11-10 02:03:06 +00:00
|
|
|
if (sz >= size)
|
2001-11-09 02:19:29 +00:00
|
|
|
return sz;
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
// Get the size of the type and then use SizeToAlignment.
|
|
|
|
//
|
|
|
|
inline unsigned int
|
|
|
|
TypeToAlignment(const Type* type, const TargetMachine& target)
|
|
|
|
{
|
|
|
|
return SizeToAlignment(target.findOptimalStorageSize(type), target);
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
// Get the size of the constant and then use SizeToAlignment.
|
|
|
|
// Handles strings as a special case;
|
|
|
|
inline unsigned int
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantToAlignment(const Constant* CV, const TargetMachine& target)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV))
|
2001-11-10 02:03:06 +00:00
|
|
|
if (ArrayTypeIsString(cast<ArrayType>(CPA->getType())))
|
|
|
|
return SizeToAlignment(1 + CPA->getNumOperands(), target);
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
return TypeToAlignment(CV->getType(), target);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Print a single constant value.
|
|
|
|
void
|
2002-02-03 23:41:08 +00:00
|
|
|
SparcModuleAsmPrinter::printSingleConstant(const Constant* CV)
|
2001-11-10 02:03:06 +00:00
|
|
|
{
|
2001-11-08 05:12:37 +00:00
|
|
|
assert(CV->getType() != Type::VoidTy &&
|
|
|
|
CV->getType() != Type::TypeTy &&
|
|
|
|
CV->getType() != Type::LabelTy &&
|
2001-12-03 22:26:30 +00:00
|
|
|
"Unexpected type for Constant");
|
2001-11-08 05:12:37 +00:00
|
|
|
|
2002-04-11 21:44:02 +00:00
|
|
|
assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
|
|
|
|
&& "Aggregate types should be handled outside this function");
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
|
2001-11-08 05:12:37 +00:00
|
|
|
|
|
|
|
if (CV->getType()->isPrimitiveType())
|
|
|
|
{
|
2002-04-11 21:44:02 +00:00
|
|
|
if (CV->getType()->isFloatingPoint()) {
|
|
|
|
// FP Constants are printed as integer constants to avoid losing
|
|
|
|
// precision...
|
|
|
|
double Val = cast<ConstantFP>(CV)->getValue();
|
|
|
|
if (CV->getType() == Type::FloatTy) {
|
|
|
|
float FVal = (float)Val;
|
|
|
|
char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
|
|
|
|
toAsm << *(unsigned int*)ProxyPtr;
|
|
|
|
} else if (CV->getType() == Type::DoubleTy) {
|
|
|
|
char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
|
|
|
|
toAsm << *(uint64_t*)ProxyPtr;
|
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown floating point type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
toAsm << "\t! " << CV->getType()->getDescription()
|
|
|
|
<< " value: " << Val << "\n";
|
|
|
|
} else {
|
2002-04-18 18:15:38 +00:00
|
|
|
WriteAsOperand(toAsm, CV, false, false) << "\n";
|
2002-04-11 21:44:02 +00:00
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV))
|
2001-11-08 05:12:37 +00:00
|
|
|
{
|
2002-01-20 22:54:45 +00:00
|
|
|
assert(CPP->isNullValue() &&
|
|
|
|
"Cannot yet print non-null pointer constants to assembly");
|
|
|
|
toAsm << "0\n";
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
2002-01-20 22:54:45 +00:00
|
|
|
else if (isa<ConstantPointerRef>(CV))
|
2001-11-08 05:12:37 +00:00
|
|
|
{
|
|
|
|
assert(0 && "Cannot yet initialize pointer refs in assembly");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-11-09 02:19:29 +00:00
|
|
|
assert(0 && "Unknown elementary type for constant");
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
2001-11-09 02:19:29 +00:00
|
|
|
}
|
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
// Print a constant value or values (it may be an aggregate).
|
|
|
|
// Uses printSingleConstant() to print each individual value.
|
|
|
|
void
|
2002-02-03 23:41:08 +00:00
|
|
|
SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV)
|
2001-11-10 02:03:06 +00:00
|
|
|
{
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
|
2001-11-10 02:03:06 +00:00
|
|
|
|
|
|
|
if (CPA && isStringCompatible(CPA))
|
|
|
|
{ // print the string alone and return
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
|
|
|
else if (CPA)
|
|
|
|
{ // Not a string. Print the values in successive locations
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Use> &constValues = CPA->getValues();
|
2001-11-10 02:03:06 +00:00
|
|
|
for (unsigned i=1; i < constValues.size(); i++)
|
2001-12-03 22:26:30 +00:00
|
|
|
this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
2001-12-03 22:26:30 +00:00
|
|
|
else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV))
|
2001-11-10 02:03:06 +00:00
|
|
|
{ // Print the fields in successive locations
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::vector<Use>& constValues = CPS->getValues();
|
2001-11-10 02:03:06 +00:00
|
|
|
for (unsigned i=1; i < constValues.size(); i++)
|
2001-12-03 22:26:30 +00:00
|
|
|
this->printConstantValueOnly(cast<Constant>(constValues[i].get()));
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
this->printSingleConstant(CV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print a constant (which may be an aggregate) prefixed by all the
|
|
|
|
// appropriate directives. Uses printConstantValueOnly() to print the
|
|
|
|
// value or values.
|
2001-11-09 02:19:29 +00:00
|
|
|
void
|
2002-02-03 23:41:08 +00:00
|
|
|
SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
|
2001-11-09 02:19:29 +00:00
|
|
|
{
|
|
|
|
if (valID.length() == 0)
|
|
|
|
valID = getID(CV);
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
|
2001-11-09 02:19:29 +00:00
|
|
|
|
|
|
|
// Print .size and .type only if it is not a string.
|
2001-12-03 22:26:30 +00:00
|
|
|
ConstantArray *CPA = dyn_cast<ConstantArray>(CV);
|
2001-11-09 02:19:29 +00:00
|
|
|
if (CPA && isStringCompatible(CPA))
|
|
|
|
{ // print it as a string and return
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << valID << ":\n";
|
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n";
|
2001-11-09 02:19:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-11-10 02:03:06 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\t.type" << "\t" << valID << ",#object\n";
|
2001-11-10 02:03:06 +00:00
|
|
|
|
|
|
|
unsigned int constSize = ConstantToSize(CV, Target);
|
|
|
|
if (constSize)
|
2002-02-03 23:41:08 +00:00
|
|
|
toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
|
2001-11-10 02:03:06 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << valID << ":\n";
|
2001-11-09 02:19:29 +00:00
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
printConstantValueOnly(CV);
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
void SparcModuleAsmPrinter::FoldConstants(const Module *M,
|
|
|
|
std::hash_set<const Constant*> &MC) {
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!(*I)->isExternal()) {
|
|
|
|
const std::hash_set<const Constant*> &pool =
|
|
|
|
MachineCodeForMethod::get(*I).getConstantPoolValues();
|
|
|
|
MC.insert(pool.begin(), pool.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\t.global\t" << getID(GV) << "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
|
|
|
|
if (GV->hasInitializer())
|
|
|
|
printConstant(GV->getInitializer(), getID(GV));
|
|
|
|
else {
|
2002-02-03 23:41:08 +00:00
|
|
|
toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(),
|
|
|
|
Target) << "\n";
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\t.type\t" << getID(GV) << ",#object\n";
|
2001-11-08 14:29:57 +00:00
|
|
|
toAsm << "\t.reserve\t" << getID(GV) << ","
|
2002-02-03 07:48:06 +00:00
|
|
|
<< Target.findOptimalStorageSize(GV->getType()->getElementType())
|
2002-01-20 22:54:45 +00:00
|
|
|
<< "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) {
|
2001-10-28 21:38:52 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2002-01-20 22:54:45 +00:00
|
|
|
std::hash_set<const Constant*> moduleConstants;
|
2001-12-03 22:26:30 +00:00
|
|
|
FoldConstants(M, moduleConstants);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
// 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!
|
2002-03-31 19:03:58 +00:00
|
|
|
|
|
|
|
// Section 1 : Read-only data section (implies initialized)
|
|
|
|
enterSection(AsmPrinter::ReadOnlyData);
|
2001-10-28 21:38:52 +00:00
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
2002-03-31 19:03:58 +00:00
|
|
|
if ((*GI)->hasInitializer() && (*GI)->isConstant())
|
|
|
|
printGlobalVariable(*GI);
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
for (std::hash_set<const Constant*>::const_iterator
|
|
|
|
I = moduleConstants.begin(),
|
2001-12-03 22:26:30 +00:00
|
|
|
E = moduleConstants.end(); I != E; ++I)
|
2001-10-28 21:38:52 +00:00
|
|
|
printConstant(*I);
|
|
|
|
|
2002-03-31 19:03:58 +00:00
|
|
|
// Section 2 : Initialized read-write data section
|
|
|
|
enterSection(AsmPrinter::InitRWData);
|
2001-10-28 21:38:52 +00:00
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
2002-03-31 19:03:58 +00:00
|
|
|
if ((*GI)->hasInitializer() && ! (*GI)->isConstant())
|
|
|
|
printGlobalVariable(*GI);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2002-03-31 19:03:58 +00:00
|
|
|
// Section 3 : Uninitialized read-write data section
|
|
|
|
enterSection(AsmPrinter::UninitRWData);
|
2001-10-28 21:38:52 +00:00
|
|
|
for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI)
|
2002-03-31 19:03:58 +00:00
|
|
|
if (! (*GI)->hasInitializer())
|
|
|
|
printGlobalVariable(*GI);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
toAsm << "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
2002-02-03 07:48:06 +00:00
|
|
|
} // End anonymous namespace
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) {
|
|
|
|
return new SparcModuleAsmPrinter(Out, *this);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|