2004-04-25 07:04:49 +00:00
|
|
|
//===-- EmitAssembly.cpp - Emit SparcV9 Specific .s File -------------------==//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-09-19 13:47:27 +00:00
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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-28 20:40:59 +00:00
|
|
|
#include "llvm/Pass.h"
|
2002-04-18 18:15:38 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2003-12-17 22:06:28 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2004-01-15 22:44:19 +00:00
|
|
|
#include "llvm/Support/Mangler.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2003-10-06 15:41:21 +00:00
|
|
|
#include "Support/Statistic.h"
|
2004-02-25 18:44:15 +00:00
|
|
|
#include "SparcV9Internals.h"
|
2004-08-16 21:55:02 +00:00
|
|
|
#include "MachineFunctionInfo.h"
|
2003-08-05 16:01:50 +00:00
|
|
|
#include <string>
|
2003-11-13 00:22:19 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2001-09-19 13:47:27 +00:00
|
|
|
namespace {
|
2003-11-13 00:22:19 +00:00
|
|
|
Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
|
2003-10-06 15:41:21 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Utility functions
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// getAsCString - Return the specified array as a C compatible string, only
|
2004-01-14 17:15:17 +00:00
|
|
|
/// if the predicate isString() is true.
|
2003-11-13 00:22:19 +00:00
|
|
|
///
|
|
|
|
std::string getAsCString(const ConstantArray *CVA) {
|
2004-01-14 17:15:17 +00:00
|
|
|
assert(CVA->isString() && "Array is not string compatible!");
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2004-01-14 17:15:17 +00:00
|
|
|
std::string Result = "\"";
|
|
|
|
for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
|
|
|
|
|
|
|
|
if (C == '"') {
|
|
|
|
Result += "\\\"";
|
|
|
|
} else if (C == '\\') {
|
|
|
|
Result += "\\\\";
|
|
|
|
} else if (isprint(C)) {
|
|
|
|
Result += C;
|
|
|
|
} else {
|
|
|
|
Result += '\\'; // print all other chars as octal value
|
|
|
|
// Convert C to octal representation
|
|
|
|
Result += ((C >> 6) & 7) + '0';
|
|
|
|
Result += ((C >> 3) & 7) + '0';
|
|
|
|
Result += ((C >> 0) & 7) + '0';
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
Result += "\"";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
return Result;
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
inline bool ArrayTypeIsString(const ArrayType* arrayType) {
|
|
|
|
return (arrayType->getElementType() == Type::UByteTy ||
|
|
|
|
arrayType->getElementType() == Type::SByteTy);
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2004-06-02 05:55:25 +00:00
|
|
|
unsigned findOptimalStorageSize(const TargetMachine &TM, const Type *Ty) {
|
|
|
|
// All integer types smaller than ints promote to 4 byte integers.
|
|
|
|
if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4)
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
return TM.getTargetData().getTypeSize(Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
inline const std::string
|
|
|
|
TypeToDataDirective(const Type* type) {
|
2004-06-17 18:19:28 +00:00
|
|
|
switch(type->getTypeID()) {
|
2003-11-07 17:45:28 +00:00
|
|
|
case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
|
|
|
|
return ".byte";
|
|
|
|
case Type::UShortTyID: case Type::ShortTyID:
|
|
|
|
return ".half";
|
|
|
|
case Type::UIntTyID: case Type::IntTyID:
|
|
|
|
return ".word";
|
|
|
|
case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID:
|
|
|
|
return ".xword";
|
|
|
|
case Type::FloatTyID:
|
|
|
|
return ".word";
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return ".xword";
|
|
|
|
case Type::ArrayTyID:
|
|
|
|
if (ArrayTypeIsString((ArrayType*) type))
|
|
|
|
return ".ascii";
|
|
|
|
else
|
|
|
|
return "<InvaliDataTypeForPrinting>";
|
|
|
|
default:
|
|
|
|
return "<InvaliDataTypeForPrinting>";
|
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Get the size of the constant for the given target.
|
|
|
|
/// If this is an unsized array, return 0.
|
|
|
|
///
|
|
|
|
inline unsigned int
|
|
|
|
ConstantToSize(const Constant* CV, const TargetMachine& target) {
|
|
|
|
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) {
|
2003-11-07 17:45:28 +00:00
|
|
|
const ArrayType *aty = cast<ArrayType>(CVA->getType());
|
|
|
|
if (ArrayTypeIsString(aty))
|
|
|
|
return 1 + CVA->getNumOperands();
|
|
|
|
}
|
|
|
|
|
2004-06-02 05:55:25 +00:00
|
|
|
return findOptimalStorageSize(target, CV->getType());
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Align data larger than one L1 cache line on L1 cache line boundaries.
|
|
|
|
/// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
|
|
|
|
///
|
|
|
|
inline unsigned int
|
|
|
|
SizeToAlignment(unsigned int size, const TargetMachine& target) {
|
2004-03-01 06:43:29 +00:00
|
|
|
const unsigned short cacheLineSize = 16;
|
2003-11-13 00:22:19 +00:00
|
|
|
if (size > (unsigned) cacheLineSize / 2)
|
|
|
|
return cacheLineSize;
|
|
|
|
else
|
|
|
|
for (unsigned sz=1; /*no condition*/; sz *= 2)
|
|
|
|
if (sz >= size)
|
|
|
|
return sz;
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Get the size of the type and then use SizeToAlignment.
|
|
|
|
///
|
|
|
|
inline unsigned int
|
|
|
|
TypeToAlignment(const Type* type, const TargetMachine& target) {
|
2004-06-02 05:55:25 +00:00
|
|
|
return SizeToAlignment(findOptimalStorageSize(target, type), target);
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
2002-03-18 03:07:26 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Get the size of the constant and then use SizeToAlignment.
|
|
|
|
/// Handles strings as a special case;
|
|
|
|
inline unsigned int
|
|
|
|
ConstantToAlignment(const Constant* CV, const TargetMachine& target) {
|
|
|
|
if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV))
|
|
|
|
if (ArrayTypeIsString(cast<ArrayType>(CVA->getType())))
|
|
|
|
return SizeToAlignment(1 + CVA->getNumOperands(), target);
|
2002-03-18 03:07:26 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
return TypeToAlignment(CV->getType(), target);
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
} // End anonymous namespace
|
2003-11-07 17:45:28 +00:00
|
|
|
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// Code abstracted away from the AsmPrinter
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class AsmPrinter {
|
2004-01-15 22:44:19 +00:00
|
|
|
// Mangle symbol names appropriately
|
|
|
|
Mangler *Mang;
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
public:
|
|
|
|
std::ostream &toAsm;
|
|
|
|
const TargetMachine &Target;
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
enum Sections {
|
|
|
|
Unknown,
|
|
|
|
Text,
|
|
|
|
ReadOnlyData,
|
|
|
|
InitRWData,
|
|
|
|
ZeroInitRWData,
|
|
|
|
} CurSection;
|
|
|
|
|
|
|
|
AsmPrinter(std::ostream &os, const TargetMachine &T)
|
2004-01-15 22:44:19 +00:00
|
|
|
: /* idTable(0), */ toAsm(os), Target(T), CurSection(Unknown) {}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2004-01-15 22:44:19 +00:00
|
|
|
~AsmPrinter() {
|
|
|
|
delete Mang;
|
|
|
|
}
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
// (start|end)(Module|Function) - Callback methods invoked by subclasses
|
|
|
|
void startModule(Module &M) {
|
2004-01-15 22:44:19 +00:00
|
|
|
Mang = new Mangler(M);
|
2003-11-07 17:45:28 +00:00
|
|
|
}
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
void PrintZeroBytesToPad(int numBytes) {
|
2004-02-09 22:15:33 +00:00
|
|
|
//
|
|
|
|
// Always use single unsigned bytes for padding. We don't know upon
|
|
|
|
// what data size the beginning address is aligned, so using anything
|
|
|
|
// other than a byte may cause alignment errors in the assembler.
|
|
|
|
//
|
2003-11-13 00:22:19 +00:00
|
|
|
while (numBytes--)
|
|
|
|
printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Print a single constant value.
|
|
|
|
///
|
|
|
|
void printSingleConstantValue(const Constant* CV);
|
|
|
|
|
|
|
|
/// Print a constant value or values (it may be an aggregate).
|
|
|
|
/// Uses printSingleConstantValue() to print each individual value.
|
|
|
|
///
|
|
|
|
void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
|
|
|
|
|
|
|
|
// Print a constant (which may be an aggregate) prefixed by all the
|
|
|
|
// appropriate directives. Uses printConstantValueOnly() to print the
|
|
|
|
// value or values.
|
|
|
|
void printConstant(const Constant* CV, std::string valID = "") {
|
|
|
|
if (valID.length() == 0)
|
|
|
|
valID = getID(CV);
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
// Print .size and .type only if it is not a string.
|
2004-01-14 17:15:17 +00:00
|
|
|
if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
|
|
|
|
if (CVA->isString()) {
|
|
|
|
// print it as a string and return
|
|
|
|
toAsm << valID << ":\n";
|
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
|
|
|
|
return;
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
toAsm << "\t.type" << "\t" << valID << ",#object\n";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned int constSize = ConstantToSize(CV, Target);
|
|
|
|
if (constSize)
|
|
|
|
toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
toAsm << valID << ":\n";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
printConstantValueOnly(CV);
|
|
|
|
}
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
// enterSection - Use this method to enter a different section of the output
|
|
|
|
// executable. This is used to only output necessary section transitions.
|
|
|
|
//
|
|
|
|
void enterSection(enum Sections S) {
|
|
|
|
if (S == CurSection) return; // Only switch section if necessary
|
|
|
|
CurSection = S;
|
|
|
|
|
|
|
|
toAsm << "\n\t.section ";
|
|
|
|
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;
|
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
toAsm << "\n";
|
|
|
|
}
|
2001-09-19 13:47:27 +00:00
|
|
|
|
2004-01-15 22:44:19 +00:00
|
|
|
// getID Wrappers - Ensure consistent usage
|
2004-02-25 18:44:15 +00:00
|
|
|
// Symbol names in SparcV9 assembly language have these rules:
|
2004-01-15 22:44:19 +00:00
|
|
|
// (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }*
|
|
|
|
// (b) A name beginning in "." is treated as a local name.
|
2003-11-13 00:22:19 +00:00
|
|
|
std::string getID(const Function *F) {
|
2004-01-15 22:44:19 +00:00
|
|
|
return Mang->getValueName(F);
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
|
|
|
std::string getID(const BasicBlock *BB) {
|
2004-01-15 22:44:19 +00:00
|
|
|
return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB);
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
|
|
|
std::string getID(const GlobalVariable *GV) {
|
2004-01-15 22:44:19 +00:00
|
|
|
return Mang->getValueName(GV);
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
|
|
|
std::string getID(const Constant *CV) {
|
2004-01-15 22:44:19 +00:00
|
|
|
return ".C_" + Mang->getValueName(CV);
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
|
|
|
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 getID(F);
|
|
|
|
assert(0 && "Unexpected type of GlobalValue!");
|
|
|
|
return "";
|
|
|
|
}
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-11-13 00:22:19 +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) + ")";
|
|
|
|
}
|
2003-08-05 16:01:50 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// ConstantExprToString() - Convert a ConstantExpr to an asm expression
|
|
|
|
/// and return this as a string.
|
|
|
|
///
|
|
|
|
std::string ConstantExprToString(const ConstantExpr* CE,
|
|
|
|
const TargetMachine& target);
|
|
|
|
|
|
|
|
/// valToExprString - Helper function for ConstantExprToString().
|
|
|
|
/// Appends result to argument string S.
|
|
|
|
///
|
|
|
|
std::string valToExprString(const Value* V, const TargetMachine& target);
|
|
|
|
};
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
/// Print a single constant value.
|
|
|
|
///
|
|
|
|
void AsmPrinter::printSingleConstantValue(const Constant* CV) {
|
|
|
|
assert(CV->getType() != Type::VoidTy &&
|
|
|
|
CV->getType() != Type::LabelTy &&
|
|
|
|
"Unexpected type for Constant");
|
|
|
|
|
|
|
|
assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
|
|
|
|
&& "Aggregate types should be handled outside this function");
|
|
|
|
|
|
|
|
toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t";
|
|
|
|
|
2004-07-18 00:44:37 +00:00
|
|
|
if (const GlobalValue* GV = dyn_cast<GlobalValue>(CV)) {
|
|
|
|
toAsm << getID(GV) << "\n";
|
2003-11-13 00:22:19 +00:00
|
|
|
} 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
|
|
|
|
// 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!");
|
2003-05-31 07:27:17 +00:00
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
|
|
|
|
toAsm << "\t! " << CV->getType()->getDescription()
|
|
|
|
<< " value: " << Val << "\n";
|
2004-02-10 05:16:44 +00:00
|
|
|
} else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
|
|
|
|
toAsm << (int)CB->getValue() << "\n";
|
2003-11-13 00:22:19 +00:00
|
|
|
} else {
|
|
|
|
WriteAsOperand(toAsm, CV, false, false) << "\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(0 && "Unknown elementary type for constant");
|
|
|
|
}
|
|
|
|
}
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// Print a constant value or values (it may be an aggregate).
|
|
|
|
/// Uses printSingleConstantValue() to print each individual value.
|
|
|
|
///
|
|
|
|
void AsmPrinter::printConstantValueOnly(const Constant* CV,
|
2004-01-14 17:15:17 +00:00
|
|
|
int numPadBytesAfter) {
|
|
|
|
if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
|
|
|
|
if (CVA->isString()) {
|
|
|
|
// print the string alone and return
|
|
|
|
toAsm << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n";
|
|
|
|
} else {
|
|
|
|
// Not a string. Print the values in successive locations
|
2004-08-04 08:44:43 +00:00
|
|
|
for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
|
|
|
|
printConstantValueOnly(CVA->getOperand(i));
|
2004-01-14 17:15:17 +00:00
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
} 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());
|
|
|
|
unsigned sizeSoFar = 0;
|
2004-08-04 08:44:43 +00:00
|
|
|
for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
|
|
|
|
const Constant* field = CVS->getOperand(i);
|
2003-11-13 00:22:19 +00:00
|
|
|
|
|
|
|
// Check if padding is needed and insert one or more 0s.
|
|
|
|
unsigned fieldSize =
|
|
|
|
Target.getTargetData().getTypeSize(field->getType());
|
2004-08-04 08:44:43 +00:00
|
|
|
int padSize = ((i == e-1? cvsLayout->StructSize
|
2003-11-13 00:22:19 +00:00
|
|
|
: 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!");
|
2004-02-15 05:55:15 +00:00
|
|
|
} else if (isa<ConstantAggregateZero>(CV)) {
|
|
|
|
PrintZeroBytesToPad(Target.getTargetData().getTypeSize(CV->getType()));
|
|
|
|
} else
|
2003-11-13 00:22:19 +00:00
|
|
|
printSingleConstantValue(CV);
|
2003-08-01 15:55:53 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
if (numPadBytesAfter)
|
|
|
|
PrintZeroBytesToPad(numPadBytesAfter);
|
|
|
|
}
|
2003-08-01 15:55:53 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// ConstantExprToString() - Convert a ConstantExpr to an asm expression
|
|
|
|
/// and return this as a string.
|
|
|
|
///
|
|
|
|
std::string AsmPrinter::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(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
|
2002-08-22 02:58:36 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
case Instruction::Cast:
|
|
|
|
// Support only non-converting casts for now, i.e., a no-op.
|
|
|
|
// This assertion is not a complete check.
|
|
|
|
assert(target.getTargetData().getTypeSize(CE->getType()) ==
|
|
|
|
target.getTargetData().getTypeSize(CE->getOperand(0)->getType()));
|
|
|
|
S += "(" + valToExprString(CE->getOperand(0), target) + ")";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Add:
|
|
|
|
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:
|
|
|
|
assert(0 && "Unsupported operator in ConstantExprToString()");
|
|
|
|
break;
|
2002-08-22 02:58:36 +00:00
|
|
|
}
|
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
return S;
|
|
|
|
}
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
/// valToExprString - Helper function for ConstantExprToString().
|
|
|
|
/// Appends result to argument string S.
|
|
|
|
///
|
|
|
|
std::string AsmPrinter::valToExprString(const Value* V,
|
|
|
|
const TargetMachine& target) {
|
|
|
|
std::string S;
|
|
|
|
bool failed = false;
|
2004-07-18 00:44:37 +00:00
|
|
|
if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
|
|
|
|
S += getID(GV);
|
|
|
|
} else if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
|
2003-11-13 00:22:19 +00:00
|
|
|
if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
|
|
|
|
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))
|
|
|
|
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 ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
|
|
|
|
S += ConstantExprToString(CE, target);
|
2002-08-22 02:58:36 +00:00
|
|
|
else
|
|
|
|
failed = true;
|
2003-11-13 00:22:19 +00:00
|
|
|
} else
|
|
|
|
failed = true;
|
2002-08-22 02:58:36 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
if (failed) {
|
|
|
|
assert(0 && "Cannot convert value to string");
|
|
|
|
S += "<illegal-value>";
|
2002-08-22 02:58:36 +00:00
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
return S;
|
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
|
|
|
|
2002-02-03 23:41:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-02-25 18:44:15 +00:00
|
|
|
// SparcV9AsmPrinter Code
|
2002-02-03 23:41:08 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
namespace {
|
2003-11-07 17:45:28 +00:00
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
struct SparcV9AsmPrinter : public FunctionPass, public AsmPrinter {
|
|
|
|
inline SparcV9AsmPrinter(std::ostream &os, const TargetMachine &t)
|
2003-11-13 00:22:19 +00:00
|
|
|
: AsmPrinter(os, t) {}
|
2002-04-29 14:57:45 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
const Function *currFunction;
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
const char *getPassName() const {
|
2004-02-25 18:44:15 +00:00
|
|
|
return "Output SparcV9 Assembly for Functions";
|
2003-11-13 00:22:19 +00:00
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
virtual bool doInitialization(Module &M) {
|
|
|
|
startModule(M);
|
|
|
|
return false;
|
|
|
|
}
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
currFunction = &F;
|
|
|
|
emitFunction(F);
|
|
|
|
return false;
|
|
|
|
}
|
2002-04-28 21:27:06 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
virtual bool doFinalization(Module &M) {
|
|
|
|
emitGlobals(M);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void emitFunction(const Function &F);
|
|
|
|
private :
|
|
|
|
void emitBasicBlock(const MachineBasicBlock &MBB);
|
|
|
|
void emitMachineInst(const MachineInstr *MI);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned int printOperands(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
void printOneOperand(const MachineOperand &Op, MachineOpCode opCode);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum);
|
|
|
|
bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum);
|
2002-02-03 23:41:08 +00:00
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned getOperandMask(unsigned Opcode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case V9::SUBccr:
|
|
|
|
case V9::SUBcci: return 1 << 3; // Remove CC argument
|
|
|
|
default: return 0; // By default, don't hack operands...
|
|
|
|
}
|
2002-02-03 23:41:08 +00:00
|
|
|
}
|
2003-11-13 00:22:19 +00:00
|
|
|
|
|
|
|
void emitGlobals(const Module &M);
|
|
|
|
void printGlobalVariable(const GlobalVariable *GV);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
2001-11-27 00:03:19 +00:00
|
|
|
|
2001-10-28 21:38:52 +00:00
|
|
|
inline bool
|
2004-02-25 18:44:15 +00:00
|
|
|
SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI,
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned int opNum) {
|
2004-02-11 20:47:34 +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
|
2004-02-25 18:44:15 +00:00
|
|
|
SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI,
|
2003-11-13 00:22:19 +00:00
|
|
|
unsigned int opNum) {
|
2004-06-02 05:55:25 +00:00
|
|
|
if (Target.getInstrInfo()->isLoad(MI->getOpcode()))
|
2001-10-28 21:38:52 +00:00
|
|
|
return (opNum == 0);
|
2004-06-02 05:55:25 +00:00
|
|
|
else if (Target.getInstrInfo()->isStore(MI->getOpcode()))
|
2001-10-28 21:38:52 +00:00
|
|
|
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
|
2004-02-25 18:44:15 +00:00
|
|
|
SparcV9AsmPrinter::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-11-13 00:22:19 +00:00
|
|
|
if (OpIsBranchTargetLabel(MI, opNum)) {
|
2004-02-11 20:47:34 +00:00
|
|
|
PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpcode());
|
2003-11-13 00:22:19 +00:00
|
|
|
return 2;
|
|
|
|
} else if (OpIsMemoryAddressBase(MI, opNum)) {
|
|
|
|
toAsm << "[";
|
2004-02-11 20:47:34 +00:00
|
|
|
PrintOp1PlusOp2(mop, MI->getOperand(opNum+1), MI->getOpcode());
|
2003-11-13 00:22:19 +00:00
|
|
|
toAsm << "]";
|
|
|
|
return 2;
|
|
|
|
} else {
|
2004-02-11 20:47:34 +00:00
|
|
|
printOneOperand(mop, MI->getOpcode());
|
2003-11-13 00:22:19 +00:00
|
|
|
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
|
2004-02-25 18:44:15 +00:00
|
|
|
SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop,
|
2003-11-13 00:22:19 +00:00
|
|
|
MachineOpCode opCode)
|
2001-10-28 21:38:52 +00:00
|
|
|
{
|
2002-07-10 21:41:21 +00:00
|
|
|
bool needBitsFlag = true;
|
|
|
|
|
2003-12-14 13:24:17 +00:00
|
|
|
if (mop.isHiBits32())
|
2002-07-10 21:41:21 +00:00
|
|
|
toAsm << "%lm(";
|
2003-12-14 13:24:17 +00:00
|
|
|
else if (mop.isLoBits32())
|
2002-07-10 21:41:21 +00:00
|
|
|
toAsm << "%lo(";
|
2003-12-14 13:24:17 +00:00
|
|
|
else if (mop.isHiBits64())
|
2002-07-10 21:41:21 +00:00
|
|
|
toAsm << "%hh(";
|
2003-12-14 13:24:17 +00:00
|
|
|
else if (mop.isLoBits64())
|
2002-07-10 21:41:21 +00:00
|
|
|
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:
|
|
|
|
{
|
2004-02-13 21:01:20 +00:00
|
|
|
int regNum = (int)mop.getReg();
|
2003-07-10 19:42:11 +00:00
|
|
|
|
2004-06-02 05:55:25 +00:00
|
|
|
if (regNum == Target.getRegInfo()->getInvalidRegNum()) {
|
2003-05-31 07:27:17 +00:00
|
|
|
// better to print code with NULL registers than to die
|
|
|
|
toAsm << "<NULL VALUE>";
|
|
|
|
} else {
|
2004-06-02 05:55:25 +00:00
|
|
|
toAsm << "%" << Target.getRegInfo()->getUnifiedRegName(regNum);
|
2003-05-31 07:27:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-10-28 21:38:52 +00:00
|
|
|
|
2003-11-07 17:45:28 +00:00
|
|
|
case MachineOperand::MO_ConstantPoolIndex:
|
|
|
|
{
|
2004-05-04 21:09:02 +00:00
|
|
|
toAsm << ".CPI_" << getID(currFunction)
|
2003-11-07 17:45:28 +00:00
|
|
|
<< "_" << mop.getConstantPoolIndex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-05-31 07:27:17 +00:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
|
|
|
{
|
|
|
|
const Value *Val = mop.getVRegValue();
|
2004-02-25 18:44:15 +00:00
|
|
|
assert(Val && "\tNULL Value in SparcV9AsmPrinter");
|
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);
|
2004-05-04 21:09:02 +00:00
|
|
|
else if (const Function *F = dyn_cast<Function>(Val))
|
|
|
|
toAsm << getID(F);
|
2003-05-31 07:27:17 +00:00
|
|
|
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
|
2004-02-25 18:44:15 +00:00
|
|
|
assert(0 && "Unrecognized value in SparcV9AsmPrinter");
|
2003-05-31 07:27:17 +00:00
|
|
|
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
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) {
|
2004-02-11 20:47:34 +00:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
2001-10-24 22:05:34 +00:00
|
|
|
|
2004-06-02 05:55:25 +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
|
|
|
|
2004-06-02 05:55:25 +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-09-23 17:27:28 +00:00
|
|
|
if (NeedComma) toAsm << ", "; // Handle comma outputting
|
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";
|
2003-10-06 15:41:21 +00:00
|
|
|
++EmittedInsts;
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) {
|
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)
|
2004-02-12 02:27:10 +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
|
|
|
}
|
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
void SparcV9AsmPrinter::emitFunction(const Function &F) {
|
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";
|
2003-11-07 17:45:28 +00:00
|
|
|
|
|
|
|
// Emit constant pool for this function
|
|
|
|
const MachineConstantPool *MCP = MachineFunction::get(&F).getConstantPool();
|
|
|
|
const std::vector<Constant*> &CP = MCP->getConstants();
|
|
|
|
|
|
|
|
enterSection(AsmPrinter::ReadOnlyData);
|
|
|
|
for (unsigned i = 0, e = CP.size(); i != e; ++i) {
|
2004-05-04 21:41:45 +00:00
|
|
|
std::string cpiName = ".CPI_" + methName + "_" + utostr(i);
|
2003-11-07 17:45:28 +00:00
|
|
|
printConstant(CP[i], cpiName);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) {
|
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
|
|
|
|
2003-11-13 00:22:19 +00:00
|
|
|
if (GV->hasInitializer() && ! GV->getInitializer()->isNullValue()) {
|
2001-10-28 21:38:52 +00:00
|
|
|
printConstant(GV->getInitializer(), getID(GV));
|
2003-11-13 00:22:19 +00:00
|
|
|
} 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) << ","
|
2004-06-02 05:55:25 +00:00
|
|
|
<< findOptimalStorageSize(Target, GV->getType()->getElementType())
|
2002-01-20 22:54:45 +00:00
|
|
|
<< "\n";
|
2001-10-28 21:38:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-25 18:44:15 +00:00
|
|
|
void SparcV9AsmPrinter::emitGlobals(const Module &M) {
|
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
|
|
|
}
|
|
|
|
|
2004-01-13 21:27:59 +00:00
|
|
|
FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out,
|
|
|
|
const TargetMachine &TM) {
|
2004-02-25 18:44:15 +00:00
|
|
|
return new SparcV9AsmPrinter(Out, TM);
|
2001-09-19 13:47:27 +00:00
|
|
|
}
|