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); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 73 | void printMachineInstruction(const MachineInstr *MI); |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 74 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 75 | bool runOnMachineFunction(MachineFunction &F); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 76 | bool doInitialization(Module &M); |
| 77 | bool doFinalization(Module &M); |
| 78 | }; |
| 79 | } // end of anonymous namespace |
| 80 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 81 | #include "SparcV8GenAsmWriter.inc" |
| 82 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 83 | /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 |
| 84 | /// assembly code for a MachineFunction to the given output stream, |
| 85 | /// using the given target machine description. This should work |
| 86 | /// regardless of whether the function is in SSA form. |
| 87 | /// |
| 88 | FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, |
| 89 | TargetMachine &tm) { |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 90 | return new SparcV8AsmPrinter(o, tm); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 94 | /// |
| 95 | static inline char toOctal(int X) { |
| 96 | return (X&7)+'0'; |
| 97 | } |
| 98 | |
| 99 | /// getAsCString - Return the specified array as a C compatible |
| 100 | /// string, only if the predicate isStringCompatible is true. |
| 101 | /// |
| 102 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 103 | assert(CVA->isString() && "Array is not string compatible!"); |
| 104 | |
| 105 | O << "\""; |
| 106 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 107 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 108 | |
| 109 | if (C == '"') { |
| 110 | O << "\\\""; |
| 111 | } else if (C == '\\') { |
| 112 | O << "\\\\"; |
| 113 | } else if (isprint(C)) { |
| 114 | O << C; |
| 115 | } else { |
| 116 | switch(C) { |
| 117 | case '\b': O << "\\b"; break; |
| 118 | case '\f': O << "\\f"; break; |
| 119 | case '\n': O << "\\n"; break; |
| 120 | case '\r': O << "\\r"; break; |
| 121 | case '\t': O << "\\t"; break; |
| 122 | default: |
| 123 | O << '\\'; |
| 124 | O << toOctal(C >> 6); |
| 125 | O << toOctal(C >> 3); |
| 126 | O << toOctal(C >> 0); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | O << "\""; |
| 132 | } |
| 133 | |
| 134 | // Print out the specified constant, without a storage class. Only the |
| 135 | // constants valid in constant expressions can occur here. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 136 | void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) { |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 137 | if (CV->isNullValue() || isa<UndefValue> (CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 138 | O << "0"; |
| 139 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 140 | assert(CB == ConstantBool::True); |
| 141 | O << "1"; |
| 142 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 143 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 144 | O << CI->getValue(); |
| 145 | else |
| 146 | O << (unsigned long long)CI->getValue(); |
| 147 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 148 | O << CI->getValue(); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 149 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 150 | // This is a constant address for a global variable or function. Use the |
| 151 | // name of the variable or function as the address value. |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 152 | O << Mang->getValueName(GV); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 153 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 154 | const TargetData &TD = TM.getTargetData(); |
| 155 | switch(CE->getOpcode()) { |
| 156 | case Instruction::GetElementPtr: { |
| 157 | // generate a symbolic expression for the byte address |
| 158 | const Constant *ptrVal = CE->getOperand(0); |
| 159 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 160 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 161 | O << "("; |
| 162 | emitConstantValueOnly(ptrVal); |
| 163 | O << ") + " << Offset; |
| 164 | } else { |
| 165 | emitConstantValueOnly(ptrVal); |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | case Instruction::Cast: { |
| 170 | // Support only non-converting or widening casts for now, that is, ones |
| 171 | // that do not involve a change in value. This assertion is really gross, |
| 172 | // and may not even be a complete check. |
| 173 | Constant *Op = CE->getOperand(0); |
| 174 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 175 | |
| 176 | // Pointers on ILP32 machines can be losslessly converted back and |
| 177 | // forth into 32-bit or wider integers, regardless of signedness. |
| 178 | assert(((isa<PointerType>(OpTy) |
| 179 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 180 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 181 | || (isa<PointerType>(Ty) |
| 182 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 183 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 184 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 185 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 186 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 187 | O << "("; |
| 188 | emitConstantValueOnly(Op); |
| 189 | O << ")"; |
| 190 | break; |
| 191 | } |
| 192 | case Instruction::Add: |
| 193 | O << "("; |
| 194 | emitConstantValueOnly(CE->getOperand(0)); |
| 195 | O << ") + ("; |
| 196 | emitConstantValueOnly(CE->getOperand(1)); |
| 197 | O << ")"; |
| 198 | break; |
| 199 | default: |
| 200 | assert(0 && "Unsupported operator!"); |
| 201 | } |
| 202 | } else { |
| 203 | assert(0 && "Unknown constant value!"); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Print a constant value or values, with the appropriate storage class as a |
| 208 | // prefix. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 209 | void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 210 | const TargetData &TD = TM.getTargetData(); |
| 211 | |
Brian Gaeke | 9d2427c | 2004-06-18 08:59:16 +0000 | [diff] [blame] | 212 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 213 | if (CVA->isString()) { |
| 214 | O << "\t.ascii\t"; |
| 215 | printAsCString(O, CVA); |
| 216 | O << "\n"; |
| 217 | } else { // Not a string. Print the values in successive locations |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 218 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++) |
| 219 | emitGlobalConstant(CVA->getOperand(i)); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 220 | } |
| 221 | return; |
| 222 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 223 | // Print the fields in successive locations. Pad to align if needed! |
| 224 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 225 | unsigned sizeSoFar = 0; |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 226 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) { |
| 227 | const Constant* field = CVS->getOperand(i); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 228 | |
| 229 | // Check if padding is needed and insert one or more 0s. |
| 230 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 231 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 232 | : cvsLayout->MemberOffsets[i+1]) |
| 233 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 234 | sizeSoFar += fieldSize + padSize; |
| 235 | |
| 236 | // Now print the actual field value |
| 237 | emitGlobalConstant(field); |
| 238 | |
| 239 | // Insert the field padding unless it's zero bytes... |
| 240 | if (padSize) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 241 | O << "\t.skip\t " << padSize << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 242 | } |
| 243 | assert(sizeSoFar == cvsLayout->StructSize && |
| 244 | "Layout of constant struct may be incorrect!"); |
| 245 | return; |
| 246 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 247 | // FP Constants are printed as integer constants to avoid losing |
| 248 | // precision... |
| 249 | double Val = CFP->getValue(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 250 | switch (CFP->getType()->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 251 | default: assert(0 && "Unknown floating point type!"); |
| 252 | case Type::FloatTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 253 | O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 254 | return; |
| 255 | } |
| 256 | case Type::DoubleTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 257 | O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n"; |
| 258 | 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] | 259 | return; |
| 260 | } |
| 261 | } |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 262 | } else if (isa<UndefValue> (CV)) { |
| 263 | unsigned size = TD.getTypeSize (CV->getType ()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 264 | O << "\t.skip\t " << size << "\n"; |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 265 | return; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 266 | } else if (isa<ConstantAggregateZero> (CV)) { |
| 267 | unsigned size = TD.getTypeSize (CV->getType ()); |
| 268 | for (unsigned i = 0; i < size; ++i) |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 269 | O << "\t.byte 0\n"; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 270 | return; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | const Type *type = CV->getType(); |
| 274 | O << "\t"; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 275 | switch (type->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 276 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 277 | O << ".byte"; |
| 278 | break; |
| 279 | case Type::UShortTyID: case Type::ShortTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 280 | O << ".half"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 281 | break; |
| 282 | case Type::FloatTyID: case Type::PointerTyID: |
| 283 | case Type::UIntTyID: case Type::IntTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 284 | O << ".word"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 285 | break; |
| 286 | case Type::DoubleTyID: |
| 287 | case Type::ULongTyID: case Type::LongTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 288 | O << ".xword"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | assert (0 && "Can't handle printing this type of thing"); |
| 292 | break; |
| 293 | } |
| 294 | O << "\t"; |
| 295 | emitConstantValueOnly(CV); |
| 296 | O << "\n"; |
| 297 | } |
| 298 | |
| 299 | /// printConstantPool - Print to the current output stream assembly |
| 300 | /// representations of the constants in the constant pool MCP. This is |
| 301 | /// used to print out constants which have been "spilled to memory" by |
| 302 | /// the code generator. |
| 303 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 304 | void SparcV8AsmPrinter::printConstantPool(MachineConstantPool *MCP) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 305 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 306 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 307 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 308 | if (CP.empty()) return; |
| 309 | |
| 310 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 311 | O << "\t.section \".rodata\"\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 312 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 313 | << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 314 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!" |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 315 | << *CP[i] << "\n"; |
| 316 | emitGlobalConstant(CP[i]); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 321 | /// method to print assembly for each instruction. |
| 322 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 323 | bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 324 | // BBNumber is used here so that a given Printer will never give two |
| 325 | // BBs the same name. (If you have a better way, please let me know!) |
| 326 | static unsigned BBNumber = 0; |
| 327 | |
| 328 | O << "\n\n"; |
| 329 | // What's my mangled name? |
| 330 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 331 | |
| 332 | // Print out constants referenced by the function |
| 333 | printConstantPool(MF.getConstantPool()); |
| 334 | |
| 335 | // Print out labels for the function. |
| 336 | O << "\t.text\n"; |
| 337 | O << "\t.align 16\n"; |
| 338 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 339 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 340 | O << CurrentFnName << ":\n"; |
| 341 | |
| 342 | // Number each basic block so that we can consistently refer to them |
| 343 | // in PC-relative references. |
| 344 | NumberForBB.clear(); |
| 345 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 346 | I != E; ++I) { |
| 347 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 348 | } |
| 349 | |
| 350 | // Print out code for the function. |
| 351 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 352 | I != E; ++I) { |
| 353 | // Print a label for the basic block. |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 354 | O << ".LBB" << Mang->getValueName(MF.getFunction ()) |
| 355 | << "_" << I->getNumber () << ":\t! " |
| 356 | << I->getBasicBlock ()->getName () << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 357 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 358 | II != E; ++II) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 359 | // Print the assembly for the instruction. |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 360 | printMachineInstruction(II); |
| 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 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 424 | /// printMachineInstruction -- Print out a single SparcV8 LLVM instruction |
| 425 | /// MI in GAS syntax to the current output stream. |
| 426 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 427 | void SparcV8AsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 428 | O << "\t"; |
Chris Lattner | 1c4f435 | 2005-12-16 06:52:00 +0000 | [diff] [blame] | 429 | if (printInstruction(MI)) return; |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 430 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 431 | unsigned Opcode = MI->getOpcode(); |
Chris Lattner | 143e0ea | 2004-06-02 05:47:26 +0000 | [diff] [blame] | 432 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 433 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
Brian Gaeke | d303a20 | 2004-07-16 10:31:47 +0000 | [diff] [blame] | 434 | |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 435 | O << Desc.Name << " "; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 436 | |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 437 | // print non-immediate, non-register-def operands |
| 438 | // then print immediate operands |
| 439 | // then print register-def operands. |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 440 | std::vector<int> print_order; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 441 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 442 | if (!(MI->getOperand (i).isImmediate () |
| 443 | || (MI->getOperand (i).isRegister () |
| 444 | && MI->getOperand (i).isDef ()))) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 445 | print_order.push_back (i); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 446 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 447 | if (MI->getOperand (i).isImmediate ()) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 448 | print_order.push_back (i); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 449 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 450 | if (MI->getOperand (i).isRegister () && MI->getOperand (i).isDef ()) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 451 | print_order.push_back (i); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 452 | for (unsigned i = 0, e = print_order.size (); i != e; ++i) { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 453 | printOperand (MI, print_order[i]); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 454 | if (i != (print_order.size () - 1)) |
| 455 | O << ", "; |
| 456 | } |
| 457 | O << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 460 | bool SparcV8AsmPrinter::doInitialization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 461 | Mang = new Mangler(M); |
| 462 | return false; // success |
| 463 | } |
| 464 | |
| 465 | // SwitchSection - Switch to the specified section of the executable if we are |
| 466 | // not already in it! |
| 467 | // |
| 468 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 469 | const char *NewSection) { |
| 470 | if (CurSection != NewSection) { |
| 471 | CurSection = NewSection; |
| 472 | if (!CurSection.empty()) |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 473 | OS << "\t.section \"" << NewSection << "\"\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 477 | bool SparcV8AsmPrinter::doFinalization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 478 | const TargetData &TD = TM.getTargetData(); |
| 479 | std::string CurSection; |
| 480 | |
| 481 | // Print out module-level global variables here. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 482 | 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] | 483 | if (I->hasInitializer()) { // External global require no code |
| 484 | O << "\n\n"; |
| 485 | std::string name = Mang->getValueName(I); |
| 486 | Constant *C = I->getInitializer(); |
| 487 | unsigned Size = TD.getTypeSize(C->getType()); |
| 488 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 489 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 490 | if (C->isNullValue() && |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 491 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 492 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
| 493 | SwitchSection(O, CurSection, ".data"); |
| 494 | if (I->hasInternalLinkage()) |
| 495 | O << "\t.local " << name << "\n"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 496 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 497 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 498 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 499 | O << "\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 500 | WriteAsOperand(O, I, true, true, &M); |
| 501 | O << "\n"; |
| 502 | } else { |
| 503 | switch (I->getLinkage()) { |
| 504 | case GlobalValue::LinkOnceLinkage: |
| 505 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 506 | // Nonnull linkonce -> weak |
| 507 | O << "\t.weak " << name << "\n"; |
| 508 | SwitchSection(O, CurSection, ""); |
Brian Gaeke | b27df44 | 2004-09-29 03:25:40 +0000 | [diff] [blame] | 509 | O << "\t.section\t\".llvm.linkonce.d." << name << "\",\"aw\",@progbits\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 510 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 511 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 512 | case GlobalValue::AppendingLinkage: |
| 513 | // FIXME: appending linkage variables should go into a section of |
| 514 | // their name or something. For now, just emit them as external. |
| 515 | case GlobalValue::ExternalLinkage: |
| 516 | // If external or appending, declare as a global symbol |
| 517 | O << "\t.globl " << name << "\n"; |
| 518 | // FALL THROUGH |
| 519 | case GlobalValue::InternalLinkage: |
| 520 | if (C->isNullValue()) |
| 521 | SwitchSection(O, CurSection, ".bss"); |
| 522 | else |
| 523 | SwitchSection(O, CurSection, ".data"); |
| 524 | break; |
Misha Brukman | c11c44f | 2004-11-19 21:49:19 +0000 | [diff] [blame] | 525 | case GlobalValue::GhostLinkage: |
| 526 | std::cerr << "Should not have any unmaterialized functions!\n"; |
| 527 | abort(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | O << "\t.align " << Align << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 531 | O << "\t.type " << name << ",#object\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 532 | O << "\t.size " << name << "," << Size << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 533 | O << name << ":\t\t\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 534 | WriteAsOperand(O, I, true, true, &M); |
| 535 | O << " = "; |
| 536 | WriteAsOperand(O, C, false, false, &M); |
| 537 | O << "\n"; |
| 538 | emitGlobalConstant(C); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | delete Mang; |
| 543 | return false; // success |
| 544 | } |