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