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