2001-09-19 13:47:27 +00:00
|
|
|
//===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==//
|
|
|
|
//
|
2003-08-18 14:43:39 +00:00
|
|
|
// This file implements all of the stuff necessary to output a .s file from
|
2001-09-19 13:47:27 +00:00
|
|
|
// 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 "llvm/CodeGen/MachineInstr.h"
|
2002-10-28 00:28:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2002-12-28 20:15:01 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionInfo.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"
|
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"
|
2003-08-05 16:01:50 +00:00
|
|
|
#include "SparcInternals.h"
|
|
|
|
#include <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-07-24 21:21:32 +00:00
|
|
|
typedef 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,
|
2002-10-13 00:32:18 +00:00
|
|
|
ZeroInitRWData,
|
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-06-25 16:13:21 +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
|
2002-06-25 16:13:21 +00:00
|
|
|
idTable = (GlobalIdTable*)M.getAnnotation(GlobalIdTable::AnnotId);
|
2002-03-18 03:07:26 +00:00
|
|
|
if (idTable == NULL) {
|
2002-06-25 16:13:21 +00:00
|
|
|
idTable = new GlobalIdTable(&M);
|
|
|
|
M.addAnnotation(idTable);
|
2002-03-18 03:07:26 +00:00
|
|
|
}
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2002-06-25 16:13:21 +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-06-25 16:13:21 +00:00
|
|
|
idTable->Table.incorporateFunction(&F);
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2002-06-25 16:13:21 +00:00
|
|
|
void endFunction(Function &) {
|
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-07-16 18:35:16 +00:00
|
|
|
// Check if a value is external or accessible from external code.
|
2002-03-18 03:07:26 +00:00
|
|
|
bool isExternal(const Value* V) {
|
2002-07-16 18:35:16 +00:00
|
|
|
const GlobalValue *GV = dyn_cast<GlobalValue>(V);
|
|
|
|
return GV && GV->hasExternalLinkage();
|
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
|
2003-08-18 14:43:39 +00:00
|
|
|
// executable. This is used to only output necessary section transitions.
|
2001-09-19 13:47:27 +00:00
|
|
|
//
|
|
|
|
void enterSection(enum Sections S) {
|
2003-08-18 14:43:39 +00:00
|
|
|
if (S == CurSection) return; // Only switch section if necessary
|
2001-09-19 13:47:27 +00:00
|
|
|
CurSection = S;
|
|
|
|
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n\t.section ";
|
2001-10-28 21:38:52 +00:00
|
|
|
switch (S)
|
2003-05-31 07:27:17 +00:00
|
|
|
{
|
|
|
|
default: assert(0 && "Bad section name!");
|
|
|
|
case Text: toAsm << "\".text\""; break;
|
|
|
|
case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break;
|
|
|
|
case InitRWData: toAsm << "\".data\",#alloc,#write"; break;
|
|
|
|
case ZeroInitRWData: toAsm << "\".bss\",#alloc,#write"; break;
|
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
toAsm << "\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 16:01:50 +00:00
|
|
|
static std::string getValidSymbolName(const std::string &S) {
|
|
|
|
std::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.
|
|
|
|
//
|
2002-10-14 06:14:18 +00:00
|
|
|
if (isdigit(S[0]))
|
|
|
|
Result = "ll";
|
2001-11-08 05:12:37 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
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
|
|
|
//
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
|
|
|
|
std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
|
2002-10-30 20:16:38 +00:00
|
|
|
|
2003-08-05 16:01:50 +00:00
|
|
|
Result += V->hasName() ? V->getName() : std::string(Prefix);
|
2002-10-30 20:16:38 +00:00
|
|
|
|
2002-03-18 03:07:26 +00:00
|
|
|
// 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);
|
2002-10-30 20:16:38 +00:00
|
|
|
|
|
|
|
// Replace or prefix problem characters in the name
|
|
|
|
Result = getValidSymbolName(Result);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
2002-10-30 20:16:38 +00:00
|
|
|
|
|
|
|
return 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...
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string getID(const Function *F) {
|
2002-04-07 20:49:59 +00:00
|
|
|
return getID(F, "LLVMFunction_");
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2003-08-05 16:01:50 +00:00
|
|
|
std::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
|
|
|
}
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string getID(const GlobalVariable *GV) {
|
2002-07-16 18:35:16 +00:00
|
|
|
return getID(GV, "LLVMGlobal_");
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string getID(const Constant *CV) {
|
2001-10-28 21:38:52 +00:00
|
|
|
return getID(CV, "LLVMConst_", ".C_");
|
|
|
|
}
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string getID(const GlobalValue *GV) {
|
2002-07-16 18:35:16 +00:00
|
|
|
if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
|
|
|
|
return getID(V);
|
|
|
|
else if (const Function *F = dyn_cast<Function>(GV))
|
|
|
|
return getID(F);
|
|
|
|
assert(0 && "Unexpected type of GlobalValue!");
|
|
|
|
return "";
|
|
|
|
}
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-08-05 16:01:50 +00:00
|
|
|
// Combines expressions
|
|
|
|
inline std::string ConstantArithExprToString(const ConstantExpr* CE,
|
|
|
|
const TargetMachine &TM,
|
|
|
|
const std::string &op) {
|
|
|
|
return "(" + valToExprString(CE->getOperand(0), TM) + op
|
|
|
|
+ valToExprString(CE->getOperand(1), TM) + ")";
|
|
|
|
}
|
|
|
|
|
2002-09-05 18:28:10 +00:00
|
|
|
// ConstantExprToString() - Convert a ConstantExpr to an asm expression
|
|
|
|
// and return this as a string.
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string ConstantExprToString(const ConstantExpr* CE,
|
|
|
|
const TargetMachine& target) {
|
|
|
|
std::string S;
|
2002-08-22 02:58:36 +00:00
|
|
|
switch(CE->getOpcode()) {
|
2003-05-31 07:27:17 +00:00
|
|
|
case Instruction::GetElementPtr:
|
|
|
|
{ // generate a symbolic expression for the byte address
|
|
|
|
const Value* ptrVal = CE->getOperand(0);
|
|
|
|
std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
|
2003-08-05 16:01:50 +00:00
|
|
|
const TargetData &TD = target.getTargetData();
|
2003-05-31 07:27:17 +00:00
|
|
|
S += "(" + valToExprString(ptrVal, target) + ") + ("
|
|
|
|
+ utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
|
|
|
|
break;
|
|
|
|
}
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2002-09-05 18:28:10 +00:00
|
|
|
case Instruction::Cast:
|
|
|
|
// Support only non-converting casts for now, i.e., a no-op.
|
|
|
|
// This assertion is not a complete check.
|
2002-12-28 20:15:01 +00:00
|
|
|
assert(target.getTargetData().getTypeSize(CE->getType()) ==
|
|
|
|
target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
|
2002-09-05 18:28:10 +00:00
|
|
|
S += "(" + valToExprString(CE->getOperand(0), target) + ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Add:
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") + (");
|
2002-09-05 18:28:10 +00:00
|
|
|
break;
|
|
|
|
|
2003-08-01 15:55:53 +00:00
|
|
|
case Instruction::Sub:
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") - (");
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Mul:
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") * (");
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Div:
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") / (");
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Rem:
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") % (");
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::And:
|
|
|
|
// Logical && for booleans; bitwise & otherwise
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target,
|
|
|
|
((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Or:
|
|
|
|
// Logical || for booleans; bitwise | otherwise
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target,
|
|
|
|
((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Xor:
|
|
|
|
// Bitwise ^ for all types
|
2003-08-05 16:01:50 +00:00
|
|
|
S += ConstantArithExprToString(CE, target, ") ^ (");
|
2003-08-01 15:55:53 +00:00
|
|
|
break;
|
|
|
|
|
2002-08-22 02:58:36 +00:00
|
|
|
default:
|
|
|
|
assert(0 && "Unsupported operator in ConstantExprToString()");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
// valToExprString - Helper function for ConstantExprToString().
|
|
|
|
// Appends result to argument string S.
|
|
|
|
//
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string valToExprString(const Value* V, const TargetMachine& target) {
|
|
|
|
std::string S;
|
2002-08-22 02:58:36 +00:00
|
|
|
bool failed = false;
|
|
|
|
if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
|
|
|
|
|
|
|
|
if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
|
2003-08-05 16:01:50 +00:00
|
|
|
S += std::string(CB == ConstantBool::True ? "1" : "0");
|
2002-08-22 02:58:36 +00:00
|
|
|
else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
|
|
|
|
S += itostr(CI->getValue());
|
|
|
|
else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
|
|
|
|
S += utostr(CI->getValue());
|
|
|
|
else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
|
|
|
|
S += ftostr(CFP->getValue());
|
|
|
|
else if (isa<ConstantPointerNull>(CV))
|
|
|
|
S += "0";
|
|
|
|
else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
|
2002-09-05 18:28:10 +00:00
|
|
|
S += valToExprString(CPR->getValue(), target);
|
2002-08-22 02:58:36 +00:00
|
|
|
else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
|
|
|
|
S += ConstantExprToString(CE, target);
|
|
|
|
else
|
|
|
|
failed = true;
|
|
|
|
|
|
|
|
} else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
|
|
|
|
S += getID(GV);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
failed = true;
|
|
|
|
|
|
|
|
if (failed) {
|
|
|
|
assert(0 && "Cannot convert value to string");
|
|
|
|
S += "<illegal-value>";
|
|
|
|
}
|
2002-09-05 18:28:10 +00:00
|
|
|
return S;
|
2002-08-22 02:58:36 +00:00
|
|
|
}
|
|
|
|
|
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-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const {
|
|
|
|
return "Output Sparc Assembly for Functions";
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
virtual bool doInitialization(Module &M) {
|
2002-02-03 23:41:08 +00:00
|
|
|
startModule(M);
|
|
|
|
return false;
|
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2002-06-25 16:13:21 +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-06-25 16:13:21 +00:00
|
|
|
virtual bool doFinalization(Module &M) {
|
2002-02-03 23:41:08 +00:00
|
|
|
endModule();
|
|
|
|
return false;
|
2001-11-27 00:03:19 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 21:27:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
void emitFunction(const Function &F);
|
2002-02-03 23:41:08 +00:00
|
|
|
private :
|
2002-10-28 20:01:13 +00:00
|
|
|
void emitBasicBlock(const MachineBasicBlock &MBB);
|
2002-02-03 23:41:08 +00:00
|
|
|
void emitMachineInst(const MachineInstr *MI);
|
|
|
|
|
|
|
|
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
|
2003-05-27 00:02:22 +00:00
|
|
|
void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
|
|
|
bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
|
|
|
|
unsigned getOperandMask(unsigned Opcode) {
|
|
|
|
switch (Opcode) {
|
2003-05-27 22:35:43 +00:00
|
|
|
case V9::SUBccr:
|
|
|
|
case V9::SUBcci: return 1 << 3; // Remove CC argument
|
2002-07-08 23:30:59 +00:00
|
|
|
//case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc
|
2002-02-03 23:41:08 +00:00
|
|
|
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()) {
|
2003-05-27 22:35:43 +00:00
|
|
|
case V9::JMPLCALLr:
|
|
|
|
case V9::JMPLCALLi:
|
|
|
|
case V9::JMPLRETr:
|
|
|
|
case V9::JMPLRETi:
|
2003-05-20 20:32:24 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
2003-05-27 00:02:22 +00:00
|
|
|
#define PrintOp1PlusOp2(mop1, mop2, opCode) \
|
|
|
|
printOneOperand(mop1, opCode); \
|
2001-11-09 02:19:29 +00:00
|
|
|
toAsm << "+"; \
|
2003-05-27 00:02:22 +00:00
|
|
|
printOneOperand(mop2, opCode);
|
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,
|
2003-05-31 07:27:17 +00:00
|
|
|
unsigned int opNum)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2002-07-10 21:41:21 +00:00
|
|
|
const MachineOperand& mop = MI->getOperand(opNum);
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
if (OpIsBranchTargetLabel(MI, opNum))
|
|
|
|
{
|
|
|
|
PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else if (OpIsMemoryAddressBase(MI, opNum))
|
|
|
|
{
|
|
|
|
toAsm << "[";
|
|
|
|
PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
|
|
|
|
toAsm << "]";
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printOneOperand(mop, MI->getOpCode());
|
|
|
|
return 1;
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
2003-05-27 00:02:22 +00:00
|
|
|
SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
|
|
|
|
MachineOpCode opCode)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2002-07-10 21:41:21 +00:00
|
|
|
bool needBitsFlag = true;
|
|
|
|
|
|
|
|
if (mop.opHiBits32())
|
|
|
|
toAsm << "%lm(";
|
|
|
|
else if (mop.opLoBits32())
|
|
|
|
toAsm << "%lo(";
|
|
|
|
else if (mop.opHiBits64())
|
|
|
|
toAsm << "%hh(";
|
|
|
|
else if (mop.opLoBits64())
|
|
|
|
toAsm << "%hm(";
|
|
|
|
else
|
|
|
|
needBitsFlag = false;
|
|
|
|
|
2002-10-28 04:45:29 +00:00
|
|
|
switch (mop.getType())
|
2003-05-31 07:27:17 +00:00
|
|
|
{
|
2003-07-06 20:13:59 +00:00
|
|
|
case MachineOperand::MO_VirtualRegister:
|
2003-07-10 19:42:11 +00:00
|
|
|
case MachineOperand::MO_CCRegister:
|
2003-05-31 07:27:17 +00:00
|
|
|
case MachineOperand::MO_MachineRegister:
|
|
|
|
{
|
|
|
|
int regNum = (int)mop.getAllocatedRegNum();
|
2003-07-10 19:42:11 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
if (regNum == Target.getRegInfo().getInvalidRegNum()) {
|
|
|
|
// better to print code with NULL registers than to die
|
|
|
|
toAsm << "<NULL VALUE>";
|
|
|
|
} else {
|
|
|
|
toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
{
|
|
|
|
const Value *Val = mop.getVRegValue();
|
|
|
|
assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
|
2002-05-19 15:25:51 +00:00
|
|
|
|
2003-07-23 15:30:06 +00:00
|
|
|
if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
|
2003-05-31 07:27:17 +00:00
|
|
|
toAsm << getID(BB);
|
|
|
|
else if (const Function *M = dyn_cast<Function>(Val))
|
|
|
|
toAsm << getID(M);
|
|
|
|
else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val))
|
|
|
|
toAsm << getID(GV);
|
|
|
|
else if (const Constant *CV = dyn_cast<Constant>(Val))
|
|
|
|
toAsm << getID(CV);
|
|
|
|
else
|
|
|
|
assert(0 && "Unrecognized value in SparcFunctionAsmPrinter");
|
|
|
|
break;
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
|
|
|
toAsm << mop.getImmedValue();
|
|
|
|
break;
|
2002-05-19 15:25:51 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
|
|
|
toAsm << (uint64_t) mop.getImmedValue();
|
|
|
|
break;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
default:
|
|
|
|
toAsm << mop; // use dump field
|
|
|
|
break;
|
|
|
|
}
|
2002-07-10 21:41:21 +00:00
|
|
|
|
|
|
|
if (needBitsFlag)
|
|
|
|
toAsm << ")";
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
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
|
|
|
|
2002-11-06 00:34:26 +00:00
|
|
|
if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
|
2003-05-31 07:27:17 +00:00
|
|
|
return; // IGNORE PHI NODES
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2002-10-29 17:35:41 +00:00
|
|
|
toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\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?
|
2003-05-31 07:27:17 +00:00
|
|
|
if (NeedComma) toAsm << ", "; // Handle comma outputing
|
2001-10-28 21:38:52 +00:00
|
|
|
NeedComma = true;
|
|
|
|
N = printOperands(MI, OpNum);
|
2002-11-17 22:57:23 +00:00
|
|
|
} else
|
|
|
|
N = 1;
|
2001-10-28 21:38:52 +00:00
|
|
|
|
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-10-28 20:01:13 +00:00
|
|
|
SparcFunctionAsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2001-09-19 13:47:27 +00:00
|
|
|
// Emit a label for the basic block
|
2002-10-28 20:01:13 +00:00
|
|
|
toAsm << getID(MBB.getBasicBlock()) << ":\n";
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// Loop over all of the instructions in the basic block...
|
2002-10-28 20:01:13 +00:00
|
|
|
for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
|
2002-10-28 01:41:47 +00:00
|
|
|
MII != MIE; ++MII)
|
2001-09-19 13:47:27 +00:00
|
|
|
emitMachineInst(*MII);
|
2003-07-14 17:20:40 +00:00
|
|
|
toAsm << "\n"; // Separate BB's with newlines
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
void
|
2002-06-25 16:13:21 +00:00
|
|
|
SparcFunctionAsmPrinter::emitFunction(const Function &F)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string methName = getID(&F);
|
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...
|
2002-10-28 20:01:13 +00:00
|
|
|
MachineFunction &MF = MachineFunction::get(&F);
|
2002-12-28 20:15:01 +00:00
|
|
|
for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
|
2002-10-28 20:01:13 +00:00
|
|
|
emitBasicBlock(*I);
|
2001-09-19 13:47:27 +00:00
|
|
|
|
|
|
|
// 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-09-16 15:54:02 +00:00
|
|
|
Pass *UltraSparc::getFunctionAsmPrinterPass(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
|
|
|
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Output Sparc Assembly for Module"; }
|
|
|
|
|
2002-06-25 16:13:21 +00:00
|
|
|
virtual bool run(Module &M) {
|
2002-02-03 23:41:08 +00:00
|
|
|
startModule(M);
|
|
|
|
emitGlobalsAndConstants(M);
|
|
|
|
endModule();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-04-28 21:27:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2002-10-13 00:32:18 +00:00
|
|
|
void emitGlobalsAndConstants (const Module &M);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
void printGlobalVariable (const GlobalVariable *GV);
|
|
|
|
void PrintZeroBytesToPad (int numBytes);
|
|
|
|
void printSingleConstantValue (const Constant* CV);
|
2003-05-25 21:59:09 +00:00
|
|
|
void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
|
2003-08-05 16:01:50 +00:00
|
|
|
void printConstant (const Constant* CV, std::string valID = "");
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
static void FoldConstants (const Module &M,
|
|
|
|
hash_set<const Constant*> &moduleConstants);
|
2002-02-03 23:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Can we treat the specified array as a string? Only if it is an array of
|
|
|
|
// ubytes or non-negative sbytes.
|
|
|
|
//
|
2002-10-13 00:32:18 +00:00
|
|
|
static bool isStringCompatible(const ConstantArray *CVA) {
|
|
|
|
const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
|
2002-02-03 23:41:08 +00:00
|
|
|
if (ETy == Type::UByteTy) return true;
|
|
|
|
if (ETy != Type::SByteTy) return false;
|
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
|
|
|
|
if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
|
2002-02-03 23:41:08 +00:00
|
|
|
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.
|
|
|
|
//
|
2003-08-05 16:01:50 +00:00
|
|
|
static std::string getAsCString(const ConstantArray *CVA) {
|
2002-10-13 00:32:18 +00:00
|
|
|
assert(isStringCompatible(CVA) && "Array is not string compatible!");
|
2002-04-18 18:15:38 +00:00
|
|
|
|
2003-08-05 16:01:50 +00:00
|
|
|
std::string Result;
|
2002-10-13 00:32:18 +00:00
|
|
|
const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
|
2002-04-18 18:15:38 +00:00
|
|
|
Result = "\"";
|
2002-10-13 00:32:18 +00:00
|
|
|
for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
|
2003-07-23 15:22:26 +00:00
|
|
|
unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
|
2002-04-18 18:15:38 +00:00
|
|
|
|
2002-05-19 15:25:51 +00:00
|
|
|
if (C == '"') {
|
|
|
|
Result += "\\\"";
|
2002-10-15 19:56:24 +00:00
|
|
|
} else if (C == '\\') {
|
|
|
|
Result += "\\\\";
|
2002-05-19 15:25:51 +00:00
|
|
|
} else if (isprint(C)) {
|
2002-04-18 18:15:38 +00:00
|
|
|
Result += C;
|
|
|
|
} else {
|
2003-07-29 19:57:34 +00:00
|
|
|
Result += '\\'; // print all other chars as octal value
|
|
|
|
Result += toOctal(C >> 6);
|
|
|
|
Result += toOctal(C >> 3);
|
|
|
|
Result += toOctal(C >> 0);
|
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
|
2002-06-05 18:08:26 +00:00
|
|
|
ArrayTypeIsString(const ArrayType* arrayType)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
|
|
|
return (arrayType->getElementType() == Type::UByteTy ||
|
|
|
|
arrayType->getElementType() == Type::SByteTy);
|
|
|
|
}
|
|
|
|
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-08-05 16:01:50 +00:00
|
|
|
inline const std::string
|
2001-11-09 02:19:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
// Get the size of the type
|
|
|
|
//
|
|
|
|
inline unsigned int
|
|
|
|
TypeToSize(const Type* type, const TargetMachine& target)
|
|
|
|
{
|
|
|
|
return target.findOptimalStorageSize(type);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2003-05-31 07:27:17 +00:00
|
|
|
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
|
|
|
|
{
|
|
|
|
const ArrayType *aty = cast<ArrayType>(CVA->getType());
|
|
|
|
if (ArrayTypeIsString(aty))
|
|
|
|
return 1 + CVA->getNumOperands();
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
return TypeToSize(CV->getType(), target);
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2002-10-13 00:32:18 +00:00
|
|
|
return SizeToAlignment(TypeToSize(type, target), target);
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
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
|
|
|
{
|
2002-10-13 00:32:18 +00:00
|
|
|
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
|
|
|
|
if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
|
|
|
|
return SizeToAlignment(1 + CVA->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-10-13 00:32:18 +00:00
|
|
|
SparcModuleAsmPrinter::printSingleConstantValue(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
|
|
|
|
2003-07-30 12:54:47 +00:00
|
|
|
if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
|
|
|
|
{ // This is a constant address for a global variable or method.
|
|
|
|
// Use the name of the variable or method as the address value.
|
|
|
|
assert(isa<GlobalValue>(CPR->getValue()) && "Unexpected non-global");
|
|
|
|
toAsm << getID(CPR->getValue()) << "\n";
|
|
|
|
}
|
|
|
|
else if (isa<ConstantPointerNull>(CV))
|
|
|
|
{ // Null pointer value
|
|
|
|
toAsm << "0\n";
|
|
|
|
}
|
|
|
|
else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
|
|
|
|
{ // Constant expression built from operators, constants, and symbolic addrs
|
|
|
|
toAsm << ConstantExprToString(CE, Target) << "\n";
|
|
|
|
}
|
|
|
|
else if (CV->getType()->isPrimitiveType()) // Check primitive types last
|
2003-05-31 07:27:17 +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";
|
2002-04-11 21:44:02 +00:00
|
|
|
} else {
|
2003-05-31 07:27:17 +00:00
|
|
|
WriteAsOperand(toAsm, CV, false, false) << "\n";
|
2002-04-11 21:44:02 +00:00
|
|
|
}
|
2001-11-08 05:12:37 +00:00
|
|
|
}
|
2003-05-31 07:27:17 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(0 && "Unknown elementary type for constant");
|
|
|
|
}
|
2001-11-09 02:19:29 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
void
|
|
|
|
SparcModuleAsmPrinter::PrintZeroBytesToPad(int numBytes)
|
|
|
|
{
|
|
|
|
for ( ; numBytes >= 8; numBytes -= 8)
|
|
|
|
printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
|
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
if (numBytes >= 4)
|
|
|
|
{
|
|
|
|
printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
|
|
|
|
numBytes -= 4;
|
|
|
|
}
|
2002-10-13 00:32:18 +00:00
|
|
|
|
|
|
|
while (numBytes--)
|
|
|
|
printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
|
|
|
|
}
|
|
|
|
|
2001-11-10 02:03:06 +00:00
|
|
|
// Print a constant value or values (it may be an aggregate).
|
2002-10-13 00:32:18 +00:00
|
|
|
// Uses printSingleConstantValue() to print each individual value.
|
2001-11-10 02:03:06 +00:00
|
|
|
void
|
2002-10-13 00:32:18 +00:00
|
|
|
SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
|
2003-05-25 21:59:09 +00:00
|
|
|
int numPadBytesAfter /* = 0*/)
|
2001-11-10 02:03:06 +00:00
|
|
|
{
|
2002-10-13 00:32:18 +00:00
|
|
|
const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
|
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
if (CVA && isStringCompatible(CVA))
|
|
|
|
{ // print the string alone and return
|
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
|
|
|
|
}
|
|
|
|
else if (CVA)
|
|
|
|
{ // Not a string. Print the values in successive locations
|
|
|
|
const std::vector<Use> &constValues = CVA->getValues();
|
|
|
|
for (unsigned i=0; i < constValues.size(); i++)
|
|
|
|
printConstantValueOnly(cast<Constant>(constValues[i].get()));
|
|
|
|
}
|
|
|
|
else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
|
|
|
|
{ // Print the fields in successive locations. Pad to align if needed!
|
|
|
|
const StructLayout *cvsLayout =
|
|
|
|
Target.getTargetData().getStructLayout(CVS->getType());
|
|
|
|
const std::vector<Use>& constValues = CVS->getValues();
|
|
|
|
unsigned sizeSoFar = 0;
|
|
|
|
for (unsigned i=0, N = constValues.size(); i < N; i++)
|
|
|
|
{
|
|
|
|
const Constant* field = cast<Constant>(constValues[i].get());
|
|
|
|
|
|
|
|
// Check if padding is needed and insert one or more 0s.
|
|
|
|
unsigned fieldSize =
|
|
|
|
Target.getTargetData().getTypeSize(field->getType());
|
|
|
|
int padSize = ((i == N-1? cvsLayout->StructSize
|
|
|
|
: cvsLayout->MemberOffsets[i+1])
|
|
|
|
- cvsLayout->MemberOffsets[i]) - fieldSize;
|
|
|
|
sizeSoFar += (fieldSize + padSize);
|
|
|
|
|
|
|
|
// Now print the actual field value
|
|
|
|
printConstantValueOnly(field, padSize);
|
|
|
|
}
|
|
|
|
assert(sizeSoFar == cvsLayout->StructSize &&
|
|
|
|
"Layout of constant struct may be incorrect!");
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
2003-05-31 07:27:17 +00:00
|
|
|
else
|
2002-10-13 00:32:18 +00:00
|
|
|
printSingleConstantValue(CV);
|
2003-05-25 21:59:09 +00:00
|
|
|
|
|
|
|
if (numPadBytesAfter)
|
|
|
|
PrintZeroBytesToPad(numPadBytesAfter);
|
2001-11-10 02:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2003-08-05 16:01:50 +00:00
|
|
|
SparcModuleAsmPrinter::printConstant(const Constant* CV, std::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.
|
2002-10-13 00:32:18 +00:00
|
|
|
const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
|
2003-05-31 07:27:17 +00:00
|
|
|
if (CVA && isStringCompatible(CVA))
|
|
|
|
{ // print it as a string and return
|
|
|
|
toAsm << valID << ":\n";
|
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
|
|
|
|
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-06-25 16:13:21 +00:00
|
|
|
void SparcModuleAsmPrinter::FoldConstants(const Module &M,
|
2002-07-24 21:21:32 +00:00
|
|
|
hash_set<const Constant*> &MC) {
|
2002-06-25 16:13:21 +00:00
|
|
|
for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2002-07-24 21:21:32 +00:00
|
|
|
const hash_set<const Constant*> &pool =
|
2002-12-28 20:15:01 +00:00
|
|
|
MachineFunction::get(I).getInfo()->getConstantPoolValues();
|
2002-02-03 23:41:08 +00:00
|
|
|
MC.insert(pool.begin(), pool.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2002-09-16 15:54:02 +00:00
|
|
|
if (GV->hasExternalLinkage())
|
|
|
|
toAsm << "\t.global\t" << getID(GV) << "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2002-10-13 00:32:18 +00:00
|
|
|
if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue())
|
2001-10-28 21:38:52 +00:00
|
|
|
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-10-13 00:32:18 +00:00
|
|
|
<< TypeToSize(GV->getType()->getElementType(), Target)
|
2002-01-20 22:54:45 +00:00
|
|
|
<< "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-25 16:13:21 +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-07-24 21:21:32 +00:00
|
|
|
hash_set<const Constant*> moduleConstants;
|
2001-12-03 22:26:30 +00:00
|
|
|
FoldConstants(M, moduleConstants);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2002-08-07 21:39:48 +00:00
|
|
|
// Output constants spilled to memory
|
2002-03-31 19:03:58 +00:00
|
|
|
enterSection(AsmPrinter::ReadOnlyData);
|
2002-08-07 21:39:48 +00:00
|
|
|
for (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-08-07 21:39:48 +00:00
|
|
|
|
|
|
|
// Output global variables...
|
2002-10-13 00:32:18 +00:00
|
|
|
for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI)
|
|
|
|
if (! GI->isExternal()) {
|
|
|
|
assert(GI->hasInitializer());
|
|
|
|
if (GI->isConstant())
|
|
|
|
enterSection(AsmPrinter::ReadOnlyData); // read-only, initialized data
|
|
|
|
else if (GI->getInitializer()->isNullValue())
|
|
|
|
enterSection(AsmPrinter::ZeroInitRWData); // read-write zero data
|
|
|
|
else
|
|
|
|
enterSection(AsmPrinter::InitRWData); // read-write non-zero data
|
|
|
|
|
|
|
|
printGlobalVariable(GI);
|
2002-08-07 21:39:48 +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-09-16 15:54:02 +00:00
|
|
|
Pass *UltraSparc::getModuleAsmPrinterPass(std::ostream &Out) {
|
2002-02-03 23:41:08 +00:00
|
|
|
return new SparcModuleAsmPrinter(Out, *this);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|