//
//===----------------------------------------------------------------------===//
-#include "SparcInternals.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionInfo.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Assembly/Writer.h"
#include "Support/StringExtras.h"
-using std::string;
+#include "SparcInternals.h"
+#include <string>
namespace {
toAsm << "\n";
}
- static string getValidSymbolName(const string &S) {
- string Result;
+ static std::string getValidSymbolName(const std::string &S) {
+ std::string Result;
// Symbol names in Sparc assembly language have these rules:
// (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
// 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) {
- string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
-
- Result += V->hasName() ? V->getName() : string(Prefix);
-
+ std::string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) {
+ std::string Result = FPrefix ? FPrefix : ""; // "Forced prefix"
+
+ Result += V->hasName() ? V->getName() : std::string(Prefix);
+
// Qualify all internal names with a unique id.
if (!isExternal(V)) {
int valId = idTable->Table.getValSlot(V);
valId = I->second;
}
Result = Result + "_" + itostr(valId);
+
+ // Replace or prefix problem characters in the name
+ Result = getValidSymbolName(Result);
}
-
- return getValidSymbolName(Result);
+
+ return Result;
}
// getID Wrappers - Ensure consistent usage...
- string getID(const Function *F) {
+ std::string getID(const Function *F) {
return getID(F, "LLVMFunction_");
}
- string getID(const BasicBlock *BB) {
+ std::string getID(const BasicBlock *BB) {
return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str());
}
- string getID(const GlobalVariable *GV) {
+ std::string getID(const GlobalVariable *GV) {
return getID(GV, "LLVMGlobal_");
}
- string getID(const Constant *CV) {
+ std::string getID(const Constant *CV) {
return getID(CV, "LLVMConst_", ".C_");
}
- string getID(const GlobalValue *GV) {
+ std::string getID(const GlobalValue *GV) {
if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
return getID(V);
else if (const Function *F = dyn_cast<Function>(GV))
return "";
}
+ // 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) + ")";
+ }
+
// ConstantExprToString() - Convert a ConstantExpr to an asm expression
// and return this as a string.
- string ConstantExprToString(const ConstantExpr* CE,
- const TargetMachine& target) {
- string S;
+ std::string ConstantExprToString(const ConstantExpr* CE,
+ const TargetMachine& target) {
+ std::string S;
switch(CE->getOpcode()) {
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());
+ const TargetData &TD = target.getTargetData();
S += "(" + valToExprString(ptrVal, target) + ") + ("
- + utostr(target.DataLayout.getIndexedOffset(ptrVal->getType(),idxVec))
- + ")";
+ + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
break;
}
case Instruction::Cast:
// Support only non-converting casts for now, i.e., a no-op.
// This assertion is not a complete check.
- assert(target.DataLayout.getTypeSize(CE->getType()) ==
- target.DataLayout.getTypeSize(CE->getOperand(0)->getType()));
+ assert(target.getTargetData().getTypeSize(CE->getType()) ==
+ target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
S += "(" + valToExprString(CE->getOperand(0), target) + ")";
break;
case Instruction::Add:
- S += "(" + valToExprString(CE->getOperand(0), target) + ") + ("
- + valToExprString(CE->getOperand(1), target) + ")";
+ S += ConstantArithExprToString(CE, target, ") + (");
+ break;
+
+ case Instruction::Sub:
+ S += ConstantArithExprToString(CE, target, ") - (");
+ break;
+
+ case Instruction::Mul:
+ S += ConstantArithExprToString(CE, target, ") * (");
+ break;
+
+ case Instruction::Div:
+ S += ConstantArithExprToString(CE, target, ") / (");
+ break;
+
+ case Instruction::Rem:
+ S += ConstantArithExprToString(CE, target, ") % (");
+ break;
+
+ case Instruction::And:
+ // Logical && for booleans; bitwise & otherwise
+ S += ConstantArithExprToString(CE, target,
+ ((CE->getType() == Type::BoolTy)? ") && (" : ") & ("));
+ break;
+
+ case Instruction::Or:
+ // Logical || for booleans; bitwise | otherwise
+ S += ConstantArithExprToString(CE, target,
+ ((CE->getType() == Type::BoolTy)? ") || (" : ") | ("));
+ break;
+
+ case Instruction::Xor:
+ // Bitwise ^ for all types
+ S += ConstantArithExprToString(CE, target, ") ^ (");
break;
default:
// valToExprString - Helper function for ConstantExprToString().
// Appends result to argument string S.
//
- string valToExprString(const Value* V, const TargetMachine& target) {
- string S;
+ std::string valToExprString(const Value* V, const TargetMachine& target) {
+ std::string S;
bool failed = false;
if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
- S += string(CB == ConstantBool::True ? "1" : "0");
+ S += std::string(CB == ConstantBool::True ? "1" : "0");
else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
S += itostr(CI->getValue());
else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
void emitMachineInst(const MachineInstr *MI);
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
- void printOneOperand(const MachineOperand &Op);
+ void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
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 V9::SUBccr:
+ case V9::SUBcci: 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...
}
SparcFunctionAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
unsigned int opNum) {
switch (MI->getOpCode()) {
- case JMPLCALL:
- case JMPLRET: return (opNum == 0);
- default: return false;
+ case V9::JMPLCALLr:
+ case V9::JMPLCALLi:
+ case V9::JMPLRETr:
+ case V9::JMPLRETi:
+ return (opNum == 0);
+ default:
+ return false;
}
}
}
-#define PrintOp1PlusOp2(mop1, mop2) \
- printOneOperand(mop1); \
+#define PrintOp1PlusOp2(mop1, mop2, opCode) \
+ printOneOperand(mop1, opCode); \
toAsm << "+"; \
- printOneOperand(mop2);
+ printOneOperand(mop2, opCode);
unsigned int
SparcFunctionAsmPrinter::printOperands(const MachineInstr *MI,
if (OpIsBranchTargetLabel(MI, opNum))
{
- PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
+ PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
return 2;
}
else if (OpIsMemoryAddressBase(MI, opNum))
{
toAsm << "[";
- PrintOp1PlusOp2(mop, MI->getOperand(opNum+1));
+ PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpCode());
toAsm << "]";
return 2;
}
else
{
- printOneOperand(mop);
+ printOneOperand(mop, MI->getOpCode());
return 1;
}
}
-
void
-SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop)
+SparcFunctionAsmPrinter::printOneOperand(const MachineOperand &mop,
+ MachineOpCode opCode)
{
bool needBitsFlag = true;
case MachineOperand::MO_CCRegister:
case MachineOperand::MO_MachineRegister:
{
- int RegNum = (int)mop.getAllocatedRegNum();
+ int regNum = (int)mop.getAllocatedRegNum();
- // better to print code with NULL registers than to die
- if (RegNum == Target.getRegInfo().getInvalidRegNum()) {
+ if (regNum == Target.getRegInfo().getInvalidRegNum()) {
+ // better to print code with NULL registers than to die
toAsm << "<NULL VALUE>";
} else {
- toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum);
+ toAsm << "%" << Target.getRegInfo().getUnifiedRegName(regNum);
}
break;
}
const Value *Val = mop.getVRegValue();
assert(Val && "\tNULL Value in SparcFunctionAsmPrinter");
- if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val))
+ if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val))
toAsm << getID(BB);
else if (const Function *M = dyn_cast<Function>(Val))
toAsm << getID(M);
{
unsigned Opcode = MI->getOpCode();
- if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG)
+ if (Target.getInstrInfo().isDummyPhiInstr(Opcode))
return; // IGNORE PHI NODES
- toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t";
+ toAsm << "\t" << Target.getInstrInfo().getName(Opcode) << "\t";
unsigned Mask = getOperandMask(Opcode);
if (NeedComma) toAsm << ", "; // Handle comma outputing
NeedComma = true;
N = printOperands(MI, OpNum);
- }
- else
- N = 1;
+ } else
+ N = 1;
toAsm << "\n";
}
for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end();
MII != MIE; ++MII)
emitMachineInst(*MII);
- toAsm << "\n"; // Seperate BB's with newlines
+ toAsm << "\n"; // Separate BB's with newlines
}
void
SparcFunctionAsmPrinter::emitFunction(const Function &F)
{
- string methName = getID(&F);
+ std::string methName = getID(&F);
toAsm << "!****** Outputing Function: " << methName << " ******\n";
enterSection(AsmPrinter::Text);
toAsm << "\t.align\t4\n\t.global\t" << methName << "\n";
// Output code for all of the basic blocks in the function...
MachineFunction &MF = MachineFunction::get(&F);
- for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E; ++I)
+ for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I)
emitBasicBlock(*I);
// Output a .size directive so the debugger knows the extents of the function
void printGlobalVariable (const GlobalVariable *GV);
void PrintZeroBytesToPad (int numBytes);
void printSingleConstantValue (const Constant* CV);
- void printConstantValueOnly (const Constant* CV, int numPadBytes = 0);
- void printConstant (const Constant* CV, string valID = "");
+ void printConstantValueOnly (const Constant* CV, int numPadBytesAfter = 0);
+ void printConstant (const Constant* CV, std::string valID = "");
static void FoldConstants (const Module &M,
hash_set<const Constant*> &moduleConstants);
// getAsCString - Return the specified array as a C compatible string, only if
// the predicate isStringCompatible is true.
//
-static string getAsCString(const ConstantArray *CVA) {
+static std::string getAsCString(const ConstantArray *CVA) {
assert(isStringCompatible(CVA) && "Array is not string compatible!");
- string Result;
+ std::string Result;
const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
Result = "\"";
for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
- unsigned char C = (ETy == Type::SByteTy) ?
- (unsigned char)cast<ConstantSInt>(CVA->getOperand(i))->getValue() :
- (unsigned char)cast<ConstantUInt>(CVA->getOperand(i))->getValue();
+ unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
if (C == '"') {
Result += "\\\"";
} else 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;
- }
+ Result += '\\'; // print all other chars as octal value
+ Result += toOctal(C >> 6);
+ Result += toOctal(C >> 3);
+ Result += toOctal(C >> 0);
}
}
Result += "\"";
}
-inline const string
+inline const std::string
TypeToDataDirective(const Type* type)
{
switch(type->getPrimitiveID())
toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
- if (CV->getType()->isPrimitiveType())
+ 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
{
if (CV->getType()->isFloatingPoint()) {
// FP Constants are printed as integer constants to avoid losing
WriteAsOperand(toAsm, CV, false, false) << "\n";
}
}
- else 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.
- 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
{
assert(0 && "Unknown elementary type for constant");
// Uses printSingleConstantValue() to print each individual value.
void
SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV,
- int numPadBytes /* = 0*/)
+ int numPadBytesAfter /* = 0*/)
{
const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
- if (numPadBytes)
- PrintZeroBytesToPad(numPadBytes);
-
if (CVA && isStringCompatible(CVA))
{ // print the string alone and return
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
{ // Print the fields in successive locations. Pad to align if needed!
const StructLayout *cvsLayout =
- Target.DataLayout.getStructLayout(CVS->getType());
+ 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.DataLayout.getTypeSize(field->getType());
+ unsigned fieldSize =
+ Target.getTargetData().getTypeSize(field->getType());
int padSize = ((i == N-1? cvsLayout->StructSize
: cvsLayout->MemberOffsets[i+1])
- cvsLayout->MemberOffsets[i]) - fieldSize;
}
else
printSingleConstantValue(CV);
+
+ if (numPadBytesAfter)
+ PrintZeroBytesToPad(numPadBytesAfter);
}
// Print a constant (which may be an aggregate) prefixed by all the
// appropriate directives. Uses printConstantValueOnly() to print the
// value or values.
void
-SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID)
+SparcModuleAsmPrinter::printConstant(const Constant* CV, std::string valID)
{
if (valID.length() == 0)
valID = getID(CV);
for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
if (!I->isExternal()) {
const hash_set<const Constant*> &pool =
- MachineFunction::get(I).getConstantPoolValues();
+ MachineFunction::get(I).getInfo()->getConstantPoolValues();
MC.insert(pool.begin(), pool.end());
}
}