| Chris Lattner | 0bc3333 | 2004-12-16 16:47:03 +0000 | [diff] [blame] | 1 | //===-- SparcV9AsmPrinter.cpp - Emit SparcV9 Specific .s File --------------==// |
| John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 9 | // |
| Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 10 | // This file implements all of the stuff necessary to output a .s file from |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 11 | // LLVM. The code in this file assumes that the specified module has already |
| 12 | // been compiled into the internal data structures of the Module. |
| 13 | // |
| Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 14 | // This code largely consists of two LLVM Pass's: a FunctionPass and a Pass. |
| 15 | // The FunctionPass is pipelined together with all of the rest of the code |
| 16 | // generation stages, and the Pass runs at the end to emit code for global |
| 17 | // variables and such. |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| Vikram S. Adve | 71b265a | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| Chris Lattner | 63c52f3 | 2002-04-28 20:40:59 +0000 | [diff] [blame] | 24 | #include "llvm/Pass.h" |
| Chris Lattner | c3728c2 | 2002-04-18 18:15:38 +0000 | [diff] [blame] | 25 | #include "llvm/Assembly/Writer.h" |
| Misha Brukman | 0f42585 | 2003-12-17 22:06:28 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 27 | #include "llvm/CodeGen/MachineFunction.h" |
| Misha Brukman | 0f42585 | 2003-12-17 22:06:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Mangler.h" |
| Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 32 | #include "SparcV9Internals.h" |
| Chris Lattner | 9fb30a4 | 2004-08-16 21:55:02 +0000 | [diff] [blame] | 33 | #include "MachineFunctionInfo.h" |
| Misha Brukman | 1e88cfb | 2003-08-05 16:01:50 +0000 | [diff] [blame] | 34 | #include <string> |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 37 | namespace { |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 38 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 39 | |
| 40 | //===--------------------------------------------------------------------===// |
| 41 | // Utility functions |
| 42 | |
| 43 | /// getAsCString - Return the specified array as a C compatible string, only |
| 44 | /// if the predicate isString() is true. |
| 45 | /// |
| 46 | std::string getAsCString(const ConstantArray *CVA) { |
| 47 | assert(CVA->isString() && "Array is not string compatible!"); |
| 48 | |
| 49 | std::string Result = "\""; |
| 50 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 51 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 52 | |
| 53 | if (C == '"') { |
| 54 | Result += "\\\""; |
| 55 | } else if (C == '\\') { |
| 56 | Result += "\\\\"; |
| 57 | } else if (isprint(C)) { |
| 58 | Result += C; |
| 59 | } else { |
| 60 | Result += '\\'; // print all other chars as octal value |
| 61 | // Convert C to octal representation |
| 62 | Result += ((C >> 6) & 7) + '0'; |
| 63 | Result += ((C >> 3) & 7) + '0'; |
| 64 | Result += ((C >> 0) & 7) + '0'; |
| 65 | } |
| 66 | } |
| 67 | Result += "\""; |
| 68 | |
| 69 | return Result; |
| 70 | } |
| 71 | |
| 72 | inline bool ArrayTypeIsString(const ArrayType* arrayType) { |
| 73 | return (arrayType->getElementType() == Type::UByteTy || |
| 74 | arrayType->getElementType() == Type::SByteTy); |
| 75 | } |
| 76 | |
| 77 | unsigned findOptimalStorageSize(const TargetMachine &TM, const Type *Ty) { |
| 78 | // All integer types smaller than ints promote to 4 byte integers. |
| 79 | if (Ty->isIntegral() && Ty->getPrimitiveSize() < 4) |
| 80 | return 4; |
| 81 | |
| 82 | return TM.getTargetData().getTypeSize(Ty); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | inline const std::string |
| 87 | TypeToDataDirective(const Type* type) { |
| 88 | switch(type->getTypeID()) { |
| 89 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 90 | return ".byte"; |
| 91 | case Type::UShortTyID: case Type::ShortTyID: |
| 92 | return ".half"; |
| 93 | case Type::UIntTyID: case Type::IntTyID: |
| 94 | return ".word"; |
| 95 | case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID: |
| 96 | return ".xword"; |
| 97 | case Type::FloatTyID: |
| 98 | return ".word"; |
| 99 | case Type::DoubleTyID: |
| 100 | return ".xword"; |
| 101 | case Type::ArrayTyID: |
| 102 | if (ArrayTypeIsString((ArrayType*) type)) |
| 103 | return ".ascii"; |
| 104 | else |
| 105 | return "<InvaliDataTypeForPrinting>"; |
| 106 | default: |
| 107 | return "<InvaliDataTypeForPrinting>"; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// Get the size of the constant for the given target. |
| 112 | /// If this is an unsized array, return 0. |
| 113 | /// |
| 114 | inline unsigned int |
| 115 | ConstantToSize(const Constant* CV, const TargetMachine& target) { |
| 116 | if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) { |
| 117 | const ArrayType *aty = cast<ArrayType>(CVA->getType()); |
| 118 | if (ArrayTypeIsString(aty)) |
| 119 | return 1 + CVA->getNumOperands(); |
| 120 | } |
| 121 | |
| 122 | return findOptimalStorageSize(target, CV->getType()); |
| 123 | } |
| 124 | |
| 125 | /// Align data larger than one L1 cache line on L1 cache line boundaries. |
| 126 | /// Align all smaller data on the next higher 2^x boundary (4, 8, ...). |
| 127 | /// |
| 128 | inline unsigned int |
| 129 | SizeToAlignment(unsigned int size, const TargetMachine& target) { |
| 130 | const unsigned short cacheLineSize = 16; |
| 131 | if (size > (unsigned) cacheLineSize / 2) |
| 132 | return cacheLineSize; |
| 133 | else |
| 134 | for (unsigned sz=1; /*no condition*/; sz *= 2) |
| 135 | if (sz >= size) |
| 136 | return sz; |
| 137 | } |
| 138 | |
| 139 | /// Get the size of the type and then use SizeToAlignment. |
| 140 | /// |
| 141 | inline unsigned int |
| 142 | TypeToAlignment(const Type* type, const TargetMachine& target) { |
| 143 | return SizeToAlignment(findOptimalStorageSize(target, type), target); |
| 144 | } |
| 145 | |
| 146 | /// Get the size of the constant and then use SizeToAlignment. |
| 147 | /// Handles strings as a special case; |
| 148 | inline unsigned int |
| 149 | ConstantToAlignment(const Constant* CV, const TargetMachine& target) { |
| 150 | if (const ConstantArray* CVA = dyn_cast<ConstantArray>(CV)) |
| 151 | if (ArrayTypeIsString(cast<ArrayType>(CVA->getType()))) |
| 152 | return SizeToAlignment(1 + CVA->getNumOperands(), target); |
| 153 | |
| 154 | return TypeToAlignment(CV->getType(), target); |
| 155 | } |
| 156 | |
| 157 | } // End anonymous namespace |
| Vikram S. Adve | 270f56a | 2002-03-18 03:07:26 +0000 | [diff] [blame] | 158 | |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 159 | namespace { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 160 | enum Sections { |
| 161 | Unknown, |
| 162 | Text, |
| 163 | ReadOnlyData, |
| 164 | InitRWData, |
| 165 | ZeroInitRWData, |
| 166 | }; |
| 167 | |
| 168 | class AsmPrinter { |
| 169 | // Mangle symbol names appropriately |
| 170 | Mangler *Mang; |
| 171 | |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 172 | public: |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 173 | std::ostream &O; |
| 174 | const TargetMachine &TM; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 175 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 176 | enum Sections CurSection; |
| 177 | |
| 178 | AsmPrinter(std::ostream &os, const TargetMachine &T) |
| 179 | : /* idTable(0), */ O(os), TM(T), CurSection(Unknown) {} |
| 180 | |
| 181 | ~AsmPrinter() { |
| 182 | delete Mang; |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 185 | // (start|end)(Module|Function) - Callback methods invoked by subclasses |
| 186 | void startModule(Module &M) { |
| 187 | Mang = new Mangler(M); |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 188 | } |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 189 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 190 | void PrintZeroBytesToPad(int numBytes) { |
| 191 | // |
| 192 | // Always use single unsigned bytes for padding. We don't know upon |
| 193 | // what data size the beginning address is aligned, so using anything |
| 194 | // other than a byte may cause alignment errors in the assembler. |
| 195 | // |
| 196 | while (numBytes--) |
| 197 | printSingleConstantValue(Constant::getNullValue(Type::UByteTy)); |
| 198 | } |
| 199 | |
| 200 | /// Print a single constant value. |
| 201 | /// |
| 202 | void printSingleConstantValue(const Constant* CV); |
| 203 | |
| 204 | /// Print a constant value or values (it may be an aggregate). |
| 205 | /// Uses printSingleConstantValue() to print each individual value. |
| 206 | /// |
| 207 | void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0); |
| 208 | |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 209 | // Print a constant (which may be an aggregate) prefixed by all the |
| 210 | // appropriate directives. Uses printConstantValueOnly() to print the |
| 211 | // value or values. |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 212 | void printConstant(const Constant* CV, std::string valID = "") { |
| 213 | if (valID.length() == 0) |
| 214 | valID = getID(CV); |
| 215 | |
| 216 | O << "\t.align\t" << ConstantToAlignment(CV, TM) << "\n"; |
| 217 | |
| 218 | // Print .size and .type only if it is not a string. |
| 219 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) |
| 220 | if (CVA->isString()) { |
| 221 | // print it as a string and return |
| 222 | O << valID << ":\n"; |
| 223 | O << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n"; |
| 224 | return; |
| 225 | } |
| 226 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 227 | O << "\t.type" << "\t" << valID << ",#object\n"; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 228 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 229 | unsigned int constSize = ConstantToSize(CV, TM); |
| 230 | if (constSize) |
| 231 | O << "\t.size" << "\t" << valID << "," << constSize << "\n"; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 232 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 233 | O << valID << ":\n"; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 234 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 235 | printConstantValueOnly(CV); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 238 | // enterSection - Use this method to enter a different section of the output |
| 239 | // executable. This is used to only output necessary section transitions. |
| 240 | // |
| 241 | void enterSection(enum Sections S) { |
| 242 | if (S == CurSection) return; // Only switch section if necessary |
| 243 | CurSection = S; |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 244 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 245 | O << "\n\t.section "; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 246 | switch (S) |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 247 | { |
| 248 | default: assert(0 && "Bad section name!"); |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 249 | case Text: O << "\".text\""; break; |
| 250 | case ReadOnlyData: O << "\".rodata\",#alloc"; break; |
| 251 | case InitRWData: O << "\".data\",#alloc,#write"; break; |
| 252 | case ZeroInitRWData: O << "\".bss\",#alloc,#write"; break; |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 253 | } |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 254 | O << "\n"; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 255 | } |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 256 | |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 257 | // getID Wrappers - Ensure consistent usage |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 258 | // Symbol names in SparcV9 assembly language have these rules: |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 259 | // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }* |
| 260 | // (b) A name beginning in "." is treated as a local name. |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 261 | std::string getID(const Function *F) { |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 262 | return Mang->getValueName(F); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 263 | } |
| 264 | std::string getID(const BasicBlock *BB) { |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 265 | return ".L_" + getID(BB->getParent()) + "_" + Mang->getValueName(BB); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 266 | } |
| 267 | std::string getID(const GlobalVariable *GV) { |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 268 | return Mang->getValueName(GV); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 269 | } |
| 270 | std::string getID(const Constant *CV) { |
| Misha Brukman | 929d1d1 | 2004-01-15 22:44:19 +0000 | [diff] [blame] | 271 | return ".C_" + Mang->getValueName(CV); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 272 | } |
| 273 | std::string getID(const GlobalValue *GV) { |
| 274 | if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV)) |
| 275 | return getID(V); |
| 276 | else if (const Function *F = dyn_cast<Function>(GV)) |
| 277 | return getID(F); |
| 278 | assert(0 && "Unexpected type of GlobalValue!"); |
| 279 | return ""; |
| 280 | } |
| Vikram S. Adve | c3a5e4e | 2002-08-22 02:58:36 +0000 | [diff] [blame] | 281 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 282 | // Combines expressions |
| 283 | inline std::string ConstantArithExprToString(const ConstantExpr* CE, |
| 284 | const TargetMachine &TM, |
| 285 | const std::string &op) { |
| 286 | return "(" + valToExprString(CE->getOperand(0), TM) + op |
| 287 | + valToExprString(CE->getOperand(1), TM) + ")"; |
| 288 | } |
| 289 | |
| 290 | /// ConstantExprToString() - Convert a ConstantExpr to an asm expression |
| 291 | /// and return this as a string. |
| 292 | /// |
| 293 | std::string ConstantExprToString(const ConstantExpr* CE, |
| 294 | const TargetMachine& target); |
| 295 | |
| 296 | /// valToExprString - Helper function for ConstantExprToString(). |
| 297 | /// Appends result to argument string S. |
| 298 | /// |
| 299 | std::string valToExprString(const Value* V, const TargetMachine& target); |
| 300 | }; |
| 301 | } // End anonymous namespace |
| 302 | |
| 303 | |
| 304 | /// Print a single constant value. |
| 305 | /// |
| 306 | void AsmPrinter::printSingleConstantValue(const Constant* CV) { |
| 307 | assert(CV->getType() != Type::VoidTy && |
| 308 | CV->getType() != Type::LabelTy && |
| 309 | "Unexpected type for Constant"); |
| 310 | |
| 311 | assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV)) |
| 312 | && "Aggregate types should be handled outside this function"); |
| 313 | |
| 314 | O << "\t" << TypeToDataDirective(CV->getType()) << "\t"; |
| 315 | |
| 316 | if (const GlobalValue* GV = dyn_cast<GlobalValue>(CV)) { |
| 317 | O << getID(GV) << "\n"; |
| Chris Lattner | ea9a85a | 2004-10-16 18:14:10 +0000 | [diff] [blame] | 318 | } else if (isa<ConstantPointerNull>(CV) || isa<UndefValue>(CV)) { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 319 | // Null pointer value |
| 320 | O << "0\n"; |
| 321 | } else if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) { |
| 322 | // Constant expression built from operators, constants, and symbolic addrs |
| 323 | O << ConstantExprToString(CE, TM) << "\n"; |
| 324 | } else if (CV->getType()->isPrimitiveType()) { |
| 325 | // Check primitive types last |
| Chris Lattner | f6666db | 2004-10-17 02:44:45 +0000 | [diff] [blame] | 326 | if (isa<UndefValue>(CV)) { |
| 327 | O << "0\n"; |
| 328 | } else if (CV->getType()->isFloatingPoint()) { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 329 | // FP Constants are printed as integer constants to avoid losing |
| 330 | // precision... |
| 331 | double Val = cast<ConstantFP>(CV)->getValue(); |
| 332 | if (CV->getType() == Type::FloatTy) { |
| 333 | float FVal = (float)Val; |
| 334 | char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules |
| 335 | O << *(unsigned int*)ProxyPtr; |
| 336 | } else if (CV->getType() == Type::DoubleTy) { |
| 337 | char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules |
| 338 | O << *(uint64_t*)ProxyPtr; |
| 339 | } else { |
| 340 | assert(0 && "Unknown floating point type!"); |
| 341 | } |
| 342 | |
| 343 | O << "\t! " << CV->getType()->getDescription() |
| 344 | << " value: " << Val << "\n"; |
| 345 | } else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 346 | O << (int)CB->getValue() << "\n"; |
| 347 | } else { |
| 348 | WriteAsOperand(O, CV, false, false) << "\n"; |
| 349 | } |
| 350 | } else { |
| 351 | assert(0 && "Unknown elementary type for constant"); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /// Print a constant value or values (it may be an aggregate). |
| 356 | /// Uses printSingleConstantValue() to print each individual value. |
| 357 | /// |
| 358 | void AsmPrinter::printConstantValueOnly(const Constant* CV, |
| 359 | int numPadBytesAfter) { |
| 360 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 361 | if (CVA->isString()) { |
| 362 | // print the string alone and return |
| 363 | O << "\t" << ".ascii" << "\t" << getAsCString(CVA) << "\n"; |
| 364 | } else { |
| 365 | // Not a string. Print the values in successive locations |
| 366 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i) |
| 367 | printConstantValueOnly(CVA->getOperand(i)); |
| 368 | } |
| 369 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 370 | // Print the fields in successive locations. Pad to align if needed! |
| 371 | const StructLayout *cvsLayout = |
| 372 | TM.getTargetData().getStructLayout(CVS->getType()); |
| 373 | unsigned sizeSoFar = 0; |
| 374 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) { |
| 375 | const Constant* field = CVS->getOperand(i); |
| 376 | |
| 377 | // Check if padding is needed and insert one or more 0s. |
| 378 | unsigned fieldSize = |
| 379 | TM.getTargetData().getTypeSize(field->getType()); |
| 380 | int padSize = ((i == e-1? cvsLayout->StructSize |
| 381 | : cvsLayout->MemberOffsets[i+1]) |
| 382 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 383 | sizeSoFar += (fieldSize + padSize); |
| 384 | |
| 385 | // Now print the actual field value |
| 386 | printConstantValueOnly(field, padSize); |
| 387 | } |
| 388 | assert(sizeSoFar == cvsLayout->StructSize && |
| 389 | "Layout of constant struct may be incorrect!"); |
| Chris Lattner | f6666db | 2004-10-17 02:44:45 +0000 | [diff] [blame] | 390 | } else if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 391 | PrintZeroBytesToPad(TM.getTargetData().getTypeSize(CV->getType())); |
| 392 | } else |
| 393 | printSingleConstantValue(CV); |
| 394 | |
| 395 | if (numPadBytesAfter) |
| 396 | PrintZeroBytesToPad(numPadBytesAfter); |
| 397 | } |
| 398 | |
| 399 | /// ConstantExprToString() - Convert a ConstantExpr to an asm expression |
| 400 | /// and return this as a string. |
| 401 | /// |
| 402 | std::string AsmPrinter::ConstantExprToString(const ConstantExpr* CE, |
| 403 | const TargetMachine& target) { |
| 404 | std::string S; |
| 405 | switch(CE->getOpcode()) { |
| 406 | case Instruction::GetElementPtr: |
| 407 | { // generate a symbolic expression for the byte address |
| 408 | const Value* ptrVal = CE->getOperand(0); |
| 409 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 410 | const TargetData &TD = target.getTargetData(); |
| 411 | S += "(" + valToExprString(ptrVal, target) + ") + (" |
| 412 | + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")"; |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | case Instruction::Cast: |
| 417 | // Support only non-converting casts for now, i.e., a no-op. |
| 418 | // This assertion is not a complete check. |
| 419 | assert(target.getTargetData().getTypeSize(CE->getType()) == |
| 420 | target.getTargetData().getTypeSize(CE->getOperand(0)->getType())); |
| 421 | S += "(" + valToExprString(CE->getOperand(0), target) + ")"; |
| 422 | break; |
| 423 | |
| 424 | case Instruction::Add: |
| 425 | S += ConstantArithExprToString(CE, target, ") + ("); |
| 426 | break; |
| 427 | |
| 428 | case Instruction::Sub: |
| 429 | S += ConstantArithExprToString(CE, target, ") - ("); |
| 430 | break; |
| 431 | |
| 432 | case Instruction::Mul: |
| 433 | S += ConstantArithExprToString(CE, target, ") * ("); |
| 434 | break; |
| 435 | |
| 436 | case Instruction::Div: |
| 437 | S += ConstantArithExprToString(CE, target, ") / ("); |
| 438 | break; |
| 439 | |
| 440 | case Instruction::Rem: |
| 441 | S += ConstantArithExprToString(CE, target, ") % ("); |
| 442 | break; |
| 443 | |
| 444 | case Instruction::And: |
| 445 | // Logical && for booleans; bitwise & otherwise |
| 446 | S += ConstantArithExprToString(CE, target, |
| 447 | ((CE->getType() == Type::BoolTy)? ") && (" : ") & (")); |
| 448 | break; |
| 449 | |
| 450 | case Instruction::Or: |
| 451 | // Logical || for booleans; bitwise | otherwise |
| 452 | S += ConstantArithExprToString(CE, target, |
| 453 | ((CE->getType() == Type::BoolTy)? ") || (" : ") | (")); |
| 454 | break; |
| 455 | |
| 456 | case Instruction::Xor: |
| 457 | // Bitwise ^ for all types |
| 458 | S += ConstantArithExprToString(CE, target, ") ^ ("); |
| 459 | break; |
| 460 | |
| 461 | default: |
| 462 | assert(0 && "Unsupported operator in ConstantExprToString()"); |
| 463 | break; |
| 464 | } |
| 465 | |
| 466 | return S; |
| 467 | } |
| 468 | |
| 469 | /// valToExprString - Helper function for ConstantExprToString(). |
| 470 | /// Appends result to argument string S. |
| 471 | /// |
| 472 | std::string AsmPrinter::valToExprString(const Value* V, |
| 473 | const TargetMachine& target) { |
| 474 | std::string S; |
| 475 | bool failed = false; |
| 476 | if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) { |
| 477 | S += getID(GV); |
| 478 | } else if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known |
| 479 | if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) |
| 480 | S += std::string(CB == ConstantBool::True ? "1" : "0"); |
| 481 | else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 482 | S += itostr(CI->getValue()); |
| 483 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 484 | S += utostr(CI->getValue()); |
| 485 | else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) |
| 486 | S += ftostr(CFP->getValue()); |
| Chris Lattner | ea9a85a | 2004-10-16 18:14:10 +0000 | [diff] [blame] | 487 | else if (isa<ConstantPointerNull>(CV) || isa<UndefValue>(CV)) |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 488 | S += "0"; |
| 489 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) |
| 490 | S += ConstantExprToString(CE, target); |
| 491 | else |
| 492 | failed = true; |
| 493 | } else |
| 494 | failed = true; |
| 495 | |
| 496 | if (failed) { |
| 497 | assert(0 && "Cannot convert value to string"); |
| 498 | S += "<illegal-value>"; |
| 499 | } |
| 500 | return S; |
| 501 | } |
| 502 | |
| 503 | namespace { |
| 504 | |
| 505 | struct SparcV9AsmPrinter : public FunctionPass, public AsmPrinter { |
| 506 | inline SparcV9AsmPrinter(std::ostream &os, const TargetMachine &t) |
| 507 | : AsmPrinter(os, t) {} |
| 508 | |
| 509 | const Function *currFunction; |
| 510 | |
| 511 | const char *getPassName() const { |
| 512 | return "Output SparcV9 Assembly for Functions"; |
| 513 | } |
| 514 | |
| 515 | virtual bool doInitialization(Module &M) { |
| 516 | startModule(M); |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | virtual bool runOnFunction(Function &F) { |
| 521 | currFunction = &F; |
| 522 | emitFunction(F); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | |
| 526 | virtual bool doFinalization(Module &M) { |
| 527 | emitGlobals(M); |
| 528 | return false; |
| 529 | } |
| 530 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 531 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 532 | AU.setPreservesAll(); |
| 533 | } |
| 534 | |
| 535 | void emitFunction(const Function &F); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 536 | private : |
| 537 | void emitBasicBlock(const MachineBasicBlock &MBB); |
| 538 | void emitMachineInst(const MachineInstr *MI); |
| 539 | |
| 540 | unsigned int printOperands(const MachineInstr *MI, unsigned int opNum); |
| 541 | void printOneOperand(const MachineOperand &Op, MachineOpCode opCode); |
| 542 | |
| 543 | bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum); |
| 544 | bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum); |
| 545 | |
| 546 | unsigned getOperandMask(unsigned Opcode) { |
| 547 | switch (Opcode) { |
| 548 | case V9::SUBccr: |
| 549 | case V9::SUBcci: return 1 << 3; // Remove CC argument |
| 550 | default: return 0; // By default, don't hack operands... |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | void emitGlobals(const Module &M); |
| 555 | void printGlobalVariable(const GlobalVariable *GV); |
| 556 | }; |
| 557 | |
| 558 | } // End anonymous namespace |
| Chris Lattner | 036a317 | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 559 | |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 560 | inline bool |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 561 | SparcV9AsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI, |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 562 | unsigned int opNum) { |
| Brian Gaeke | b22186a | 2004-02-11 20:47:34 +0000 | [diff] [blame] | 563 | switch (MI->getOpcode()) { |
| Misha Brukman | 8bde6a6 | 2003-05-27 22:35:43 +0000 | [diff] [blame] | 564 | case V9::JMPLCALLr: |
| 565 | case V9::JMPLCALLi: |
| 566 | case V9::JMPLRETr: |
| 567 | case V9::JMPLRETi: |
| Misha Brukman | 56f4fa1 | 2003-05-20 20:32:24 +0000 | [diff] [blame] | 568 | return (opNum == 0); |
| 569 | default: |
| 570 | return false; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 574 | inline bool |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 575 | SparcV9AsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI, |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 576 | unsigned int opNum) { |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 577 | if (TM.getInstrInfo()->isLoad(MI->getOpcode())) |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 578 | return (opNum == 0); |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 579 | else if (TM.getInstrInfo()->isStore(MI->getOpcode())) |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 580 | return (opNum == 1); |
| 581 | else |
| 582 | return false; |
| 583 | } |
| 584 | |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 585 | unsigned int |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 586 | SparcV9AsmPrinter::printOperands(const MachineInstr *MI, unsigned opNum) { |
| Vikram S. Adve | 4e1ee14 | 2002-07-10 21:41:21 +0000 | [diff] [blame] | 587 | const MachineOperand& mop = MI->getOperand(opNum); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 588 | if (OpIsBranchTargetLabel(MI, opNum)) { |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 589 | printOneOperand(mop, MI->getOpcode()); |
| 590 | O << "+"; |
| 591 | printOneOperand(MI->getOperand(opNum+1), MI->getOpcode()); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 592 | return 2; |
| 593 | } else if (OpIsMemoryAddressBase(MI, opNum)) { |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 594 | O << "["; |
| 595 | printOneOperand(mop, MI->getOpcode()); |
| 596 | O << "+"; |
| 597 | printOneOperand(MI->getOperand(opNum+1), MI->getOpcode()); |
| 598 | O << "]"; |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 599 | return 2; |
| 600 | } else { |
| Brian Gaeke | b22186a | 2004-02-11 20:47:34 +0000 | [diff] [blame] | 601 | printOneOperand(mop, MI->getOpcode()); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 602 | return 1; |
| 603 | } |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 606 | void |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 607 | SparcV9AsmPrinter::printOneOperand(const MachineOperand &mop, |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 608 | MachineOpCode opCode) |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 609 | { |
| Vikram S. Adve | 4e1ee14 | 2002-07-10 21:41:21 +0000 | [diff] [blame] | 610 | bool needBitsFlag = true; |
| 611 | |
| Alkis Evlogimenos | aaba463 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 612 | if (mop.isHiBits32()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 613 | O << "%lm("; |
| Alkis Evlogimenos | aaba463 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 614 | else if (mop.isLoBits32()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 615 | O << "%lo("; |
| Alkis Evlogimenos | aaba463 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 616 | else if (mop.isHiBits64()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 617 | O << "%hh("; |
| Alkis Evlogimenos | aaba463 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 618 | else if (mop.isLoBits64()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 619 | O << "%hm("; |
| Vikram S. Adve | 4e1ee14 | 2002-07-10 21:41:21 +0000 | [diff] [blame] | 620 | else |
| 621 | needBitsFlag = false; |
| 622 | |
| Chris Lattner | 6a30b02 | 2002-10-28 04:45:29 +0000 | [diff] [blame] | 623 | switch (mop.getType()) |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 624 | { |
| Vikram S. Adve | d09c4c3 | 2003-07-06 20:13:59 +0000 | [diff] [blame] | 625 | case MachineOperand::MO_VirtualRegister: |
| Vikram S. Adve | 6528067 | 2003-07-10 19:42:11 +0000 | [diff] [blame] | 626 | case MachineOperand::MO_CCRegister: |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 627 | case MachineOperand::MO_MachineRegister: |
| 628 | { |
| Alkis Evlogimenos | 8cdd021 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 629 | int regNum = (int)mop.getReg(); |
| Vikram S. Adve | 6528067 | 2003-07-10 19:42:11 +0000 | [diff] [blame] | 630 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 631 | if (regNum == TM.getRegInfo()->getInvalidRegNum()) { |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 632 | // better to print code with NULL registers than to die |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 633 | O << "<NULL VALUE>"; |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 634 | } else { |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 635 | O << "%" << TM.getRegInfo()->getUnifiedRegName(regNum); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 636 | } |
| 637 | break; |
| 638 | } |
| Misha Brukman | 8d53167 | 2003-05-31 06:22:37 +0000 | [diff] [blame] | 639 | |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 640 | case MachineOperand::MO_ConstantPoolIndex: |
| 641 | { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 642 | O << ".CPI_" << getID(currFunction) |
| 643 | << "_" << mop.getConstantPoolIndex(); |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 647 | case MachineOperand::MO_PCRelativeDisp: |
| 648 | { |
| 649 | const Value *Val = mop.getVRegValue(); |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 650 | assert(Val && "\tNULL Value in SparcV9AsmPrinter"); |
| Misha Brukman | 8d53167 | 2003-05-31 06:22:37 +0000 | [diff] [blame] | 651 | |
| Chris Lattner | f26a8ee | 2003-07-23 15:30:06 +0000 | [diff] [blame] | 652 | if (const BasicBlock *BB = dyn_cast<BasicBlock>(Val)) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 653 | O << getID(BB); |
| Brian Gaeke | f2ff5dd | 2004-05-04 21:09:02 +0000 | [diff] [blame] | 654 | else if (const Function *F = dyn_cast<Function>(Val)) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 655 | O << getID(F); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 656 | else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Val)) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 657 | O << getID(GV); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 658 | else if (const Constant *CV = dyn_cast<Constant>(Val)) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 659 | O << getID(CV); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 660 | else |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 661 | assert(0 && "Unrecognized value in SparcV9AsmPrinter"); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 662 | break; |
| 663 | } |
| Misha Brukman | 8d53167 | 2003-05-31 06:22:37 +0000 | [diff] [blame] | 664 | |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 665 | case MachineOperand::MO_SignExtendedImmed: |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 666 | O << mop.getImmedValue(); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 667 | break; |
| Misha Brukman | 8d53167 | 2003-05-31 06:22:37 +0000 | [diff] [blame] | 668 | |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 669 | case MachineOperand::MO_UnextendedImmed: |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 670 | O << (uint64_t) mop.getImmedValue(); |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 671 | break; |
| Misha Brukman | 8d53167 | 2003-05-31 06:22:37 +0000 | [diff] [blame] | 672 | |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 673 | default: |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 674 | O << mop; // use dump field |
| Vikram S. Adve | 96b801a | 2003-05-31 07:27:17 +0000 | [diff] [blame] | 675 | break; |
| 676 | } |
| Vikram S. Adve | 4e1ee14 | 2002-07-10 21:41:21 +0000 | [diff] [blame] | 677 | |
| 678 | if (needBitsFlag) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 679 | O << ")"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 682 | void SparcV9AsmPrinter::emitMachineInst(const MachineInstr *MI) { |
| Brian Gaeke | b22186a | 2004-02-11 20:47:34 +0000 | [diff] [blame] | 683 | unsigned Opcode = MI->getOpcode(); |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 684 | |
| Brian Gaeke | 7593525 | 2004-08-18 20:04:24 +0000 | [diff] [blame] | 685 | if (Opcode == V9::PHI) |
| 686 | return; // Ignore Machine-PHI nodes. |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 687 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 688 | O << "\t" << TM.getInstrInfo()->getName(Opcode) << "\t"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 689 | |
| 690 | unsigned Mask = getOperandMask(Opcode); |
| 691 | |
| 692 | bool NeedComma = false; |
| 693 | unsigned N = 1; |
| 694 | for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N) |
| 695 | if (! ((1 << OpNum) & Mask)) { // Ignore this operand? |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 696 | if (NeedComma) O << ", "; // Handle comma outputting |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 697 | NeedComma = true; |
| 698 | N = printOperands(MI, OpNum); |
| Chris Lattner | 5961102 | 2002-11-17 22:57:23 +0000 | [diff] [blame] | 699 | } else |
| 700 | N = 1; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 701 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 702 | O << "\n"; |
| Brian Gaeke | 4547ab1 | 2003-10-06 15:41:21 +0000 | [diff] [blame] | 703 | ++EmittedInsts; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 706 | void SparcV9AsmPrinter::emitBasicBlock(const MachineBasicBlock &MBB) { |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 707 | // Emit a label for the basic block |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 708 | O << getID(MBB.getBasicBlock()) << ":\n"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 709 | |
| 710 | // Loop over all of the instructions in the basic block... |
| Misha Brukman | 181ea4a | 2002-10-28 20:01:13 +0000 | [diff] [blame] | 711 | for (MachineBasicBlock::const_iterator MII = MBB.begin(), MIE = MBB.end(); |
| Chris Lattner | 8710aab | 2002-10-28 01:41:47 +0000 | [diff] [blame] | 712 | MII != MIE; ++MII) |
| Alkis Evlogimenos | 80da865 | 2004-02-12 02:27:10 +0000 | [diff] [blame] | 713 | emitMachineInst(MII); |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 714 | O << "\n"; // Separate BB's with newlines |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 717 | void SparcV9AsmPrinter::emitFunction(const Function &F) { |
| 718 | std::string CurrentFnName = getID(&F); |
| 719 | MachineFunction &MF = MachineFunction::get(&F); |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 720 | O << "!****** Outputing Function: " << CurrentFnName << " ******\n"; |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 721 | |
| 722 | // Emit constant pool for this function |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 723 | const MachineConstantPool *MCP = MF.getConstantPool(); |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 724 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 725 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 726 | enterSection(ReadOnlyData); |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 727 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 728 | std::string cpiName = ".CPI_" + CurrentFnName + "_" + utostr(i); |
| Misha Brukman | 2133b05 | 2003-11-07 17:45:28 +0000 | [diff] [blame] | 729 | printConstant(CP[i], cpiName); |
| 730 | } |
| 731 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 732 | enterSection(Text); |
| 733 | O << "\t.align\t4\n\t.global\t" << CurrentFnName << "\n"; |
| 734 | //O << "\t.type\t" << CurrentFnName << ",#function\n"; |
| 735 | O << "\t.type\t" << CurrentFnName << ", 2\n"; |
| 736 | O << CurrentFnName << ":\n"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 737 | |
| Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 738 | // Output code for all of the basic blocks in the function... |
| Chris Lattner | 4d84d49 | 2002-12-28 20:15:01 +0000 | [diff] [blame] | 739 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); I != E;++I) |
| Misha Brukman | 181ea4a | 2002-10-28 20:01:13 +0000 | [diff] [blame] | 740 | emitBasicBlock(*I); |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 741 | |
| 742 | // Output a .size directive so the debugger knows the extents of the function |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 743 | O << ".EndOf_" << CurrentFnName << ":\n\t.size " |
| 744 | << CurrentFnName << ", .EndOf_" |
| 745 | << CurrentFnName << "-" << CurrentFnName << "\n"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 746 | |
| Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 747 | // Put some spaces between the functions |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 748 | O << "\n\n"; |
| Chris Lattner | d682045 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 751 | void SparcV9AsmPrinter::printGlobalVariable(const GlobalVariable* GV) { |
| Vikram S. Adve | 8cbdbd8 | 2002-09-16 15:54:02 +0000 | [diff] [blame] | 752 | if (GV->hasExternalLinkage()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 753 | O << "\t.global\t" << getID(GV) << "\n"; |
| Vikram S. Adve | 71b265a | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 754 | |
| Chris Lattner | ea9a85a | 2004-10-16 18:14:10 +0000 | [diff] [blame] | 755 | if (GV->hasInitializer() && |
| 756 | !(GV->getInitializer()->isNullValue() || |
| 757 | isa<UndefValue>(GV->getInitializer()))) { |
| Vikram S. Adve | 71b265a | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 758 | printConstant(GV->getInitializer(), getID(GV)); |
| Misha Brukman | 6675f98 | 2003-11-13 00:22:19 +0000 | [diff] [blame] | 759 | } else { |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 760 | O << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(), |
| 761 | TM) << "\n"; |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 762 | O << "\t.type\t" << getID(GV) << ",#object\n"; |
| 763 | O << "\t.reserve\t" << getID(GV) << "," |
| Brian Gaeke | 8658474 | 2004-09-10 18:01:45 +0000 | [diff] [blame] | 764 | << findOptimalStorageSize(TM, GV->getType()->getElementType()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 765 | << "\n"; |
| Vikram S. Adve | 71b265a | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
| 768 | |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 769 | void SparcV9AsmPrinter::emitGlobals(const Module &M) { |
| Chris Lattner | c93df6b | 2002-08-07 21:39:48 +0000 | [diff] [blame] | 770 | // Output global variables... |
| Vikram S. Adve | 10d8164 | 2002-10-13 00:32:18 +0000 | [diff] [blame] | 771 | for (Module::const_giterator GI = M.gbegin(), GE = M.gend(); GI != GE; ++GI) |
| 772 | if (! GI->isExternal()) { |
| 773 | assert(GI->hasInitializer()); |
| 774 | if (GI->isConstant()) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 775 | enterSection(ReadOnlyData); // read-only, initialized data |
| Chris Lattner | ea9a85a | 2004-10-16 18:14:10 +0000 | [diff] [blame] | 776 | else if (GI->getInitializer()->isNullValue() || |
| 777 | isa<UndefValue>(GI->getInitializer())) |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 778 | enterSection(ZeroInitRWData); // read-write zero data |
| Vikram S. Adve | 10d8164 | 2002-10-13 00:32:18 +0000 | [diff] [blame] | 779 | else |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 780 | enterSection(InitRWData); // read-write non-zero data |
| Vikram S. Adve | 10d8164 | 2002-10-13 00:32:18 +0000 | [diff] [blame] | 781 | |
| 782 | printGlobalVariable(GI); |
| Chris Lattner | c93df6b | 2002-08-07 21:39:48 +0000 | [diff] [blame] | 783 | } |
| Chris Lattner | c93df6b | 2002-08-07 21:39:48 +0000 | [diff] [blame] | 784 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 785 | O << "\n"; |
| Vikram S. Adve | 71b265a | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| Chris Lattner | 12754fe | 2004-08-18 05:29:08 +0000 | [diff] [blame] | 788 | FunctionPass *llvm::createAsmPrinterPass(std::ostream &Out, TargetMachine &TM) { |
| Brian Gaeke | 94e95d2 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 789 | return new SparcV9AsmPrinter(Out, TM); |
| Chris Lattner | e772d28 | 2002-02-03 07:48:06 +0000 | [diff] [blame] | 790 | } |