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