Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 1 | //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to GAS-format Sparc V8 assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SparcV8.h" |
| 16 | #include "SparcV8InstrInfo.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Assembly/Writer.h" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Support/Mangler.h" |
Brian Gaeke | 74dfcf1 | 2004-09-02 02:37:43 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" |
| 27 | #include "llvm/ADT/StringExtras.h" |
| 28 | #include "llvm/Support/CommandLine.h" |
Jim Laskey | b8df7c2 | 2005-08-17 20:04:34 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MathExtras.h" |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 30 | #include <cctype> |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 35 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 36 | struct SparcV8AsmPrinter : public MachineFunctionPass { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 37 | /// Output stream on which we're printing assembly code. |
| 38 | /// |
| 39 | std::ostream &O; |
| 40 | |
| 41 | /// Target machine description which we query for reg. names, data |
| 42 | /// layout, etc. |
| 43 | /// |
| 44 | TargetMachine &TM; |
| 45 | |
| 46 | /// Name-mangler for global names. |
| 47 | /// |
| 48 | Mangler *Mang; |
| 49 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 50 | SparcV8AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { } |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 51 | |
| 52 | /// We name each basic block in a Function with a unique number, so |
| 53 | /// that we can consistently refer to them later. This is cleared |
| 54 | /// at the beginning of each call to runOnMachineFunction(). |
| 55 | /// |
| 56 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 57 | ValueMapTy NumberForBB; |
| 58 | |
| 59 | /// Cache of mangled name for current function. This is |
| 60 | /// recalculated at the beginning of each call to |
| 61 | /// runOnMachineFunction(). |
| 62 | /// |
| 63 | std::string CurrentFnName; |
| 64 | |
| 65 | virtual const char *getPassName() const { |
| 66 | return "SparcV8 Assembly Printer"; |
| 67 | } |
| 68 | |
| 69 | void emitConstantValueOnly(const Constant *CV); |
| 70 | void emitGlobalConstant(const Constant *CV); |
| 71 | void printConstantPool(MachineConstantPool *MCP); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 72 | void printOperand(const MachineInstr *MI, int opNum); |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 73 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 74 | bool runOnMachineFunction(MachineFunction &F); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 75 | bool doInitialization(Module &M); |
| 76 | bool doFinalization(Module &M); |
| 77 | }; |
| 78 | } // end of anonymous namespace |
| 79 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 80 | #include "SparcV8GenAsmWriter.inc" |
| 81 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 82 | /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 |
| 83 | /// assembly code for a MachineFunction to the given output stream, |
| 84 | /// using the given target machine description. This should work |
| 85 | /// regardless of whether the function is in SSA form. |
| 86 | /// |
| 87 | FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, |
| 88 | TargetMachine &tm) { |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 89 | return new SparcV8AsmPrinter(o, tm); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 93 | /// |
| 94 | static inline char toOctal(int X) { |
| 95 | return (X&7)+'0'; |
| 96 | } |
| 97 | |
| 98 | /// getAsCString - Return the specified array as a C compatible |
| 99 | /// string, only if the predicate isStringCompatible is true. |
| 100 | /// |
| 101 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 102 | assert(CVA->isString() && "Array is not string compatible!"); |
| 103 | |
| 104 | O << "\""; |
| 105 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 106 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 107 | |
| 108 | if (C == '"') { |
| 109 | O << "\\\""; |
| 110 | } else if (C == '\\') { |
| 111 | O << "\\\\"; |
| 112 | } else if (isprint(C)) { |
| 113 | O << C; |
| 114 | } else { |
| 115 | switch(C) { |
| 116 | case '\b': O << "\\b"; break; |
| 117 | case '\f': O << "\\f"; break; |
| 118 | case '\n': O << "\\n"; break; |
| 119 | case '\r': O << "\\r"; break; |
| 120 | case '\t': O << "\\t"; break; |
| 121 | default: |
| 122 | O << '\\'; |
| 123 | O << toOctal(C >> 6); |
| 124 | O << toOctal(C >> 3); |
| 125 | O << toOctal(C >> 0); |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | O << "\""; |
| 131 | } |
| 132 | |
| 133 | // Print out the specified constant, without a storage class. Only the |
| 134 | // constants valid in constant expressions can occur here. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 135 | void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) { |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 136 | if (CV->isNullValue() || isa<UndefValue> (CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 137 | O << "0"; |
| 138 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 139 | assert(CB == ConstantBool::True); |
| 140 | O << "1"; |
| 141 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 142 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 143 | O << CI->getValue(); |
| 144 | else |
| 145 | O << (unsigned long long)CI->getValue(); |
| 146 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 147 | O << CI->getValue(); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 148 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 149 | // This is a constant address for a global variable or function. Use the |
| 150 | // name of the variable or function as the address value. |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 151 | O << Mang->getValueName(GV); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 152 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 153 | const TargetData &TD = TM.getTargetData(); |
| 154 | switch(CE->getOpcode()) { |
| 155 | case Instruction::GetElementPtr: { |
| 156 | // generate a symbolic expression for the byte address |
| 157 | const Constant *ptrVal = CE->getOperand(0); |
| 158 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 159 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 160 | O << "("; |
| 161 | emitConstantValueOnly(ptrVal); |
| 162 | O << ") + " << Offset; |
| 163 | } else { |
| 164 | emitConstantValueOnly(ptrVal); |
| 165 | } |
| 166 | break; |
| 167 | } |
| 168 | case Instruction::Cast: { |
| 169 | // Support only non-converting or widening casts for now, that is, ones |
| 170 | // that do not involve a change in value. This assertion is really gross, |
| 171 | // and may not even be a complete check. |
| 172 | Constant *Op = CE->getOperand(0); |
| 173 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 174 | |
| 175 | // Pointers on ILP32 machines can be losslessly converted back and |
| 176 | // forth into 32-bit or wider integers, regardless of signedness. |
| 177 | assert(((isa<PointerType>(OpTy) |
| 178 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 179 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 180 | || (isa<PointerType>(Ty) |
| 181 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 182 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 183 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 184 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 185 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 186 | O << "("; |
| 187 | emitConstantValueOnly(Op); |
| 188 | O << ")"; |
| 189 | break; |
| 190 | } |
| 191 | case Instruction::Add: |
| 192 | O << "("; |
| 193 | emitConstantValueOnly(CE->getOperand(0)); |
| 194 | O << ") + ("; |
| 195 | emitConstantValueOnly(CE->getOperand(1)); |
| 196 | O << ")"; |
| 197 | break; |
| 198 | default: |
| 199 | assert(0 && "Unsupported operator!"); |
| 200 | } |
| 201 | } else { |
| 202 | assert(0 && "Unknown constant value!"); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Print a constant value or values, with the appropriate storage class as a |
| 207 | // prefix. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 208 | void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 209 | const TargetData &TD = TM.getTargetData(); |
| 210 | |
Brian Gaeke | 9d2427c | 2004-06-18 08:59:16 +0000 | [diff] [blame] | 211 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 212 | if (CVA->isString()) { |
| 213 | O << "\t.ascii\t"; |
| 214 | printAsCString(O, CVA); |
| 215 | O << "\n"; |
| 216 | } else { // Not a string. Print the values in successive locations |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 217 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++) |
| 218 | emitGlobalConstant(CVA->getOperand(i)); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 219 | } |
| 220 | return; |
| 221 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 222 | // Print the fields in successive locations. Pad to align if needed! |
| 223 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 224 | unsigned sizeSoFar = 0; |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 225 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) { |
| 226 | const Constant* field = CVS->getOperand(i); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 227 | |
| 228 | // Check if padding is needed and insert one or more 0s. |
| 229 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 230 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 231 | : cvsLayout->MemberOffsets[i+1]) |
| 232 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 233 | sizeSoFar += fieldSize + padSize; |
| 234 | |
| 235 | // Now print the actual field value |
| 236 | emitGlobalConstant(field); |
| 237 | |
| 238 | // Insert the field padding unless it's zero bytes... |
| 239 | if (padSize) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 240 | O << "\t.skip\t " << padSize << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 241 | } |
| 242 | assert(sizeSoFar == cvsLayout->StructSize && |
| 243 | "Layout of constant struct may be incorrect!"); |
| 244 | return; |
| 245 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 246 | // FP Constants are printed as integer constants to avoid losing |
| 247 | // precision... |
| 248 | double Val = CFP->getValue(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 249 | switch (CFP->getType()->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 250 | default: assert(0 && "Unknown floating point type!"); |
| 251 | case Type::FloatTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 252 | O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 253 | return; |
| 254 | } |
| 255 | case Type::DoubleTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 256 | O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n"; |
| 257 | O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | } |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 261 | } else if (isa<UndefValue> (CV)) { |
| 262 | unsigned size = TD.getTypeSize (CV->getType ()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 263 | O << "\t.skip\t " << size << "\n"; |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 264 | return; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 265 | } else if (isa<ConstantAggregateZero> (CV)) { |
| 266 | unsigned size = TD.getTypeSize (CV->getType ()); |
| 267 | for (unsigned i = 0; i < size; ++i) |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 268 | O << "\t.byte 0\n"; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 269 | return; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | const Type *type = CV->getType(); |
| 273 | O << "\t"; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 274 | switch (type->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 275 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 276 | O << ".byte"; |
| 277 | break; |
| 278 | case Type::UShortTyID: case Type::ShortTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 279 | O << ".half"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 280 | break; |
| 281 | case Type::FloatTyID: case Type::PointerTyID: |
| 282 | case Type::UIntTyID: case Type::IntTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 283 | O << ".word"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 284 | break; |
| 285 | case Type::DoubleTyID: |
| 286 | case Type::ULongTyID: case Type::LongTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 287 | O << ".xword"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 288 | break; |
| 289 | default: |
| 290 | assert (0 && "Can't handle printing this type of thing"); |
| 291 | break; |
| 292 | } |
| 293 | O << "\t"; |
| 294 | emitConstantValueOnly(CV); |
| 295 | O << "\n"; |
| 296 | } |
| 297 | |
| 298 | /// printConstantPool - Print to the current output stream assembly |
| 299 | /// representations of the constants in the constant pool MCP. This is |
| 300 | /// used to print out constants which have been "spilled to memory" by |
| 301 | /// the code generator. |
| 302 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 303 | void SparcV8AsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 304 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 305 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 306 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 307 | if (CP.empty()) return; |
| 308 | |
| 309 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 310 | O << "\t.section \".rodata\"\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 311 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 312 | << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 313 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!" |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 314 | << *CP[i] << "\n"; |
| 315 | emitGlobalConstant(CP[i]); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 320 | /// method to print assembly for each instruction. |
| 321 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 322 | bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 323 | // BBNumber is used here so that a given Printer will never give two |
| 324 | // BBs the same name. (If you have a better way, please let me know!) |
| 325 | static unsigned BBNumber = 0; |
| 326 | |
| 327 | O << "\n\n"; |
| 328 | // What's my mangled name? |
| 329 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 330 | |
| 331 | // Print out constants referenced by the function |
| 332 | printConstantPool(MF.getConstantPool()); |
| 333 | |
| 334 | // Print out labels for the function. |
| 335 | O << "\t.text\n"; |
| 336 | O << "\t.align 16\n"; |
| 337 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 338 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 339 | O << CurrentFnName << ":\n"; |
| 340 | |
| 341 | // Number each basic block so that we can consistently refer to them |
| 342 | // in PC-relative references. |
| 343 | NumberForBB.clear(); |
| 344 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 345 | I != E; ++I) { |
| 346 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 347 | } |
| 348 | |
| 349 | // Print out code for the function. |
| 350 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 351 | I != E; ++I) { |
| 352 | // Print a label for the basic block. |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 353 | O << ".LBB" << Mang->getValueName(MF.getFunction ()) |
| 354 | << "_" << I->getNumber () << ":\t! " |
| 355 | << I->getBasicBlock ()->getName () << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 356 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 357 | II != E; ++II) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 358 | // Print the assembly for the instruction. |
Chris Lattner | 0d8fcd3 | 2005-12-17 06:54:41 +0000 | [diff] [blame^] | 359 | O << "\t"; |
| 360 | printInstruction(II); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
| 364 | // We didn't modify anything. |
| 365 | return false; |
| 366 | } |
| 367 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 368 | void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 369 | const MachineOperand &MO = MI->getOperand (opNum); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 370 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 371 | bool CloseParen = false; |
| 372 | if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { |
| 373 | O << "%hi("; |
| 374 | CloseParen = true; |
Misha Brukman | f54ef97 | 2004-06-24 23:38:20 +0000 | [diff] [blame] | 375 | } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate()) |
| 376 | { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 377 | O << "%lo("; |
| 378 | CloseParen = true; |
| 379 | } |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 380 | switch (MO.getType()) { |
| 381 | case MachineOperand::MO_VirtualRegister: |
| 382 | if (Value *V = MO.getVRegValueOrNull()) { |
| 383 | O << "<" << V->getName() << ">"; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 384 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 385 | } |
| 386 | // FALLTHROUGH |
| 387 | case MachineOperand::MO_MachineRegister: |
| 388 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 389 | O << "%" << LowercaseString (RI.get(MO.getReg()).Name); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 390 | else |
| 391 | O << "%reg" << MO.getReg(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 392 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 393 | |
| 394 | case MachineOperand::MO_SignExtendedImmed: |
| 395 | case MachineOperand::MO_UnextendedImmed: |
| 396 | O << (int)MO.getImmedValue(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 397 | break; |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 398 | case MachineOperand::MO_MachineBasicBlock: { |
| 399 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 400 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 401 | << "_" << MBBOp->getNumber () << "\t! " |
| 402 | << MBBOp->getBasicBlock ()->getName (); |
| 403 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 404 | } |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 405 | case MachineOperand::MO_PCRelativeDisp: |
| 406 | std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; |
| 407 | abort (); |
| 408 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 409 | case MachineOperand::MO_GlobalAddress: |
| 410 | O << Mang->getValueName(MO.getGlobal()); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 411 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 412 | case MachineOperand::MO_ExternalSymbol: |
| 413 | O << MO.getSymbolName(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 414 | break; |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame] | 415 | case MachineOperand::MO_ConstantPoolIndex: |
| 416 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
| 417 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 418 | default: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 419 | O << "<unknown operand type>"; abort (); break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 420 | } |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 421 | if (CloseParen) O << ")"; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 424 | bool SparcV8AsmPrinter::doInitialization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 425 | Mang = new Mangler(M); |
| 426 | return false; // success |
| 427 | } |
| 428 | |
| 429 | // SwitchSection - Switch to the specified section of the executable if we are |
| 430 | // not already in it! |
| 431 | // |
| 432 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 433 | const char *NewSection) { |
| 434 | if (CurSection != NewSection) { |
| 435 | CurSection = NewSection; |
| 436 | if (!CurSection.empty()) |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 437 | OS << "\t.section \"" << NewSection << "\"\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 441 | bool SparcV8AsmPrinter::doFinalization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 442 | const TargetData &TD = TM.getTargetData(); |
| 443 | std::string CurSection; |
| 444 | |
| 445 | // Print out module-level global variables here. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 446 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 447 | if (I->hasInitializer()) { // External global require no code |
| 448 | O << "\n\n"; |
| 449 | std::string name = Mang->getValueName(I); |
| 450 | Constant *C = I->getInitializer(); |
| 451 | unsigned Size = TD.getTypeSize(C->getType()); |
| 452 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 453 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 454 | if (C->isNullValue() && |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 455 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 456 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
| 457 | SwitchSection(O, CurSection, ".data"); |
| 458 | if (I->hasInternalLinkage()) |
| 459 | O << "\t.local " << name << "\n"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 460 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 461 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 462 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 463 | O << "\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 464 | WriteAsOperand(O, I, true, true, &M); |
| 465 | O << "\n"; |
| 466 | } else { |
| 467 | switch (I->getLinkage()) { |
| 468 | case GlobalValue::LinkOnceLinkage: |
| 469 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 470 | // Nonnull linkonce -> weak |
| 471 | O << "\t.weak " << name << "\n"; |
| 472 | SwitchSection(O, CurSection, ""); |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 473 | O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 474 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 475 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 476 | case GlobalValue::AppendingLinkage: |
| 477 | // FIXME: appending linkage variables should go into a section of |
| 478 | // their name or something. For now, just emit them as external. |
| 479 | case GlobalValue::ExternalLinkage: |
| 480 | // If external or appending, declare as a global symbol |
| 481 | O << "\t.globl " << name << "\n"; |
| 482 | // FALL THROUGH |
| 483 | case GlobalValue::InternalLinkage: |
| 484 | if (C->isNullValue()) |
| 485 | SwitchSection(O, CurSection, ".bss"); |
| 486 | else |
| 487 | SwitchSection(O, CurSection, ".data"); |
| 488 | break; |
Misha Brukman | c11c44f | 2004-11-19 21:49:19 +0000 | [diff] [blame] | 489 | case GlobalValue::GhostLinkage: |
| 490 | std::cerr << "Should not have any unmaterialized functions!\n"; |
| 491 | abort(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | O << "\t.align " << Align << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 495 | O << "\t.type " << name << ",#object\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 496 | O << "\t.size " << name << "," << Size << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 497 | O << name << ":\t\t\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 498 | WriteAsOperand(O, I, true, true, &M); |
| 499 | O << " = "; |
| 500 | WriteAsOperand(O, C, false, false, &M); |
| 501 | O << "\n"; |
| 502 | emitGlobalConstant(C); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | delete Mang; |
| 507 | return false; // success |
| 508 | } |