Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 1 | //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to GAS-format Sparc V8 assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "SparcV8.h" |
| 16 | #include "SparcV8InstrInfo.h" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/AsmPrinter.h" |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 23 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 24 | #include "llvm/CodeGen/MachineInstr.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | #include "llvm/Support/Mangler.h" |
Brian Gaeke | 74dfcf1 | 2004-09-02 02:37:43 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
| 28 | #include "llvm/ADT/StringExtras.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
Jim Laskey | b8df7c2 | 2005-08-17 20:04:34 +0000 | [diff] [blame] | 30 | #include "llvm/Support/MathExtras.h" |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 31 | #include <cctype> |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 36 | |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 37 | struct SparcV8AsmPrinter : public AsmPrinter { |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame^] | 38 | SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { |
| 39 | Data16bitsDirective = "\t.half\t"; |
| 40 | Data32bitsDirective = "\t.word\t"; |
| 41 | Data64bitsDirective = "\t.xword\t"; |
| 42 | } |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 43 | |
| 44 | /// We name each basic block in a Function with a unique number, so |
| 45 | /// that we can consistently refer to them later. This is cleared |
| 46 | /// at the beginning of each call to runOnMachineFunction(). |
| 47 | /// |
| 48 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 49 | ValueMapTy NumberForBB; |
| 50 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 51 | virtual const char *getPassName() const { |
| 52 | return "SparcV8 Assembly Printer"; |
| 53 | } |
| 54 | |
| 55 | void emitConstantValueOnly(const Constant *CV); |
| 56 | void emitGlobalConstant(const Constant *CV); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 57 | void printOperand(const MachineInstr *MI, int opNum); |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 58 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 59 | bool runOnMachineFunction(MachineFunction &F); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 60 | bool doInitialization(Module &M); |
| 61 | bool doFinalization(Module &M); |
| 62 | }; |
| 63 | } // end of anonymous namespace |
| 64 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 65 | #include "SparcV8GenAsmWriter.inc" |
| 66 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 67 | /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 |
| 68 | /// assembly code for a MachineFunction to the given output stream, |
| 69 | /// using the given target machine description. This should work |
| 70 | /// regardless of whether the function is in SSA form. |
| 71 | /// |
| 72 | FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, |
| 73 | TargetMachine &tm) { |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 74 | return new SparcV8AsmPrinter(o, tm); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 78 | /// |
| 79 | static inline char toOctal(int X) { |
| 80 | return (X&7)+'0'; |
| 81 | } |
| 82 | |
| 83 | /// getAsCString - Return the specified array as a C compatible |
| 84 | /// string, only if the predicate isStringCompatible is true. |
| 85 | /// |
| 86 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 87 | assert(CVA->isString() && "Array is not string compatible!"); |
| 88 | |
| 89 | O << "\""; |
| 90 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 91 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 92 | |
| 93 | if (C == '"') { |
| 94 | O << "\\\""; |
| 95 | } else if (C == '\\') { |
| 96 | O << "\\\\"; |
| 97 | } else if (isprint(C)) { |
| 98 | O << C; |
| 99 | } else { |
| 100 | switch(C) { |
| 101 | case '\b': O << "\\b"; break; |
| 102 | case '\f': O << "\\f"; break; |
| 103 | case '\n': O << "\\n"; break; |
| 104 | case '\r': O << "\\r"; break; |
| 105 | case '\t': O << "\\t"; break; |
| 106 | default: |
| 107 | O << '\\'; |
| 108 | O << toOctal(C >> 6); |
| 109 | O << toOctal(C >> 3); |
| 110 | O << toOctal(C >> 0); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | O << "\""; |
| 116 | } |
| 117 | |
| 118 | // Print out the specified constant, without a storage class. Only the |
| 119 | // constants valid in constant expressions can occur here. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 120 | void SparcV8AsmPrinter::emitConstantValueOnly(const Constant *CV) { |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 121 | if (CV->isNullValue() || isa<UndefValue> (CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 122 | O << "0"; |
| 123 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 124 | assert(CB == ConstantBool::True); |
| 125 | O << "1"; |
| 126 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 127 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 128 | O << CI->getValue(); |
| 129 | else |
| 130 | O << (unsigned long long)CI->getValue(); |
| 131 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 132 | O << CI->getValue(); |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 133 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 134 | // This is a constant address for a global variable or function. Use the |
| 135 | // name of the variable or function as the address value. |
Chris Lattner | 7330248 | 2004-07-18 07:26:17 +0000 | [diff] [blame] | 136 | O << Mang->getValueName(GV); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 137 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 138 | const TargetData &TD = TM.getTargetData(); |
| 139 | switch(CE->getOpcode()) { |
| 140 | case Instruction::GetElementPtr: { |
| 141 | // generate a symbolic expression for the byte address |
| 142 | const Constant *ptrVal = CE->getOperand(0); |
| 143 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 144 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 145 | O << "("; |
| 146 | emitConstantValueOnly(ptrVal); |
| 147 | O << ") + " << Offset; |
| 148 | } else { |
| 149 | emitConstantValueOnly(ptrVal); |
| 150 | } |
| 151 | break; |
| 152 | } |
| 153 | case Instruction::Cast: { |
| 154 | // Support only non-converting or widening casts for now, that is, ones |
| 155 | // that do not involve a change in value. This assertion is really gross, |
| 156 | // and may not even be a complete check. |
| 157 | Constant *Op = CE->getOperand(0); |
| 158 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 159 | |
| 160 | // Pointers on ILP32 machines can be losslessly converted back and |
| 161 | // forth into 32-bit or wider integers, regardless of signedness. |
| 162 | assert(((isa<PointerType>(OpTy) |
| 163 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 164 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 165 | || (isa<PointerType>(Ty) |
| 166 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 167 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 168 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 169 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 170 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 171 | O << "("; |
| 172 | emitConstantValueOnly(Op); |
| 173 | O << ")"; |
| 174 | break; |
| 175 | } |
| 176 | case Instruction::Add: |
| 177 | O << "("; |
| 178 | emitConstantValueOnly(CE->getOperand(0)); |
| 179 | O << ") + ("; |
| 180 | emitConstantValueOnly(CE->getOperand(1)); |
| 181 | O << ")"; |
| 182 | break; |
| 183 | default: |
| 184 | assert(0 && "Unsupported operator!"); |
| 185 | } |
| 186 | } else { |
| 187 | assert(0 && "Unknown constant value!"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Print a constant value or values, with the appropriate storage class as a |
| 192 | // prefix. |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 193 | void SparcV8AsmPrinter::emitGlobalConstant(const Constant *CV) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 194 | const TargetData &TD = TM.getTargetData(); |
| 195 | |
Brian Gaeke | 9d2427c | 2004-06-18 08:59:16 +0000 | [diff] [blame] | 196 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 197 | if (CVA->isString()) { |
| 198 | O << "\t.ascii\t"; |
| 199 | printAsCString(O, CVA); |
| 200 | O << "\n"; |
| 201 | } else { // Not a string. Print the values in successive locations |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 202 | for (unsigned i = 0, e = CVA->getNumOperands(); i != e; i++) |
| 203 | emitGlobalConstant(CVA->getOperand(i)); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 204 | } |
| 205 | return; |
| 206 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 207 | // Print the fields in successive locations. Pad to align if needed! |
| 208 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 209 | unsigned sizeSoFar = 0; |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 210 | for (unsigned i = 0, e = CVS->getNumOperands(); i != e; i++) { |
| 211 | const Constant* field = CVS->getOperand(i); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 212 | |
| 213 | // Check if padding is needed and insert one or more 0s. |
| 214 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
Chris Lattner | cdf7012 | 2004-08-04 17:27:27 +0000 | [diff] [blame] | 215 | unsigned padSize = ((i == e-1? cvsLayout->StructSize |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 216 | : cvsLayout->MemberOffsets[i+1]) |
| 217 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 218 | sizeSoFar += fieldSize + padSize; |
| 219 | |
| 220 | // Now print the actual field value |
| 221 | emitGlobalConstant(field); |
| 222 | |
| 223 | // Insert the field padding unless it's zero bytes... |
| 224 | if (padSize) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 225 | O << "\t.skip\t " << padSize << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 226 | } |
| 227 | assert(sizeSoFar == cvsLayout->StructSize && |
| 228 | "Layout of constant struct may be incorrect!"); |
| 229 | return; |
| 230 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 231 | // FP Constants are printed as integer constants to avoid losing |
| 232 | // precision... |
| 233 | double Val = CFP->getValue(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 234 | switch (CFP->getType()->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 235 | default: assert(0 && "Unknown floating point type!"); |
| 236 | case Type::FloatTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 237 | O << ".long\t" << FloatToBits(Val) << "\t! float " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | case Type::DoubleTyID: { |
Jim Laskey | cb6682f | 2005-08-17 19:34:49 +0000 | [diff] [blame] | 241 | O << ".word\t0x" << std::hex << (DoubleToBits(Val) >> 32) << std::dec << "\t! double " << Val << "\n"; |
| 242 | O << ".word\t0x" << std::hex << (DoubleToBits(Val) & 0xffffffffUL) << std::dec << "\t! double " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | } |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 246 | } else if (isa<UndefValue> (CV)) { |
| 247 | unsigned size = TD.getTypeSize (CV->getType ()); |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 248 | O << "\t.skip\t " << size << "\n"; |
Brian Gaeke | 54799c2 | 2004-11-14 03:22:05 +0000 | [diff] [blame] | 249 | return; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 250 | } else if (isa<ConstantAggregateZero> (CV)) { |
| 251 | unsigned size = TD.getTypeSize (CV->getType ()); |
| 252 | for (unsigned i = 0; i < size; ++i) |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 253 | O << "\t.byte 0\n"; |
Brian Gaeke | 4dd043f | 2004-11-23 21:10:49 +0000 | [diff] [blame] | 254 | return; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | const Type *type = CV->getType(); |
| 258 | O << "\t"; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 259 | switch (type->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 260 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 261 | O << ".byte"; |
| 262 | break; |
| 263 | case Type::UShortTyID: case Type::ShortTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 264 | O << ".half"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 265 | break; |
| 266 | case Type::FloatTyID: case Type::PointerTyID: |
| 267 | case Type::UIntTyID: case Type::IntTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 268 | O << ".word"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 269 | break; |
| 270 | case Type::DoubleTyID: |
| 271 | case Type::ULongTyID: case Type::LongTyID: |
Brian Gaeke | 3bf960c | 2004-12-09 18:51:01 +0000 | [diff] [blame] | 272 | O << ".xword"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 273 | break; |
| 274 | default: |
| 275 | assert (0 && "Can't handle printing this type of thing"); |
| 276 | break; |
| 277 | } |
| 278 | O << "\t"; |
| 279 | emitConstantValueOnly(CV); |
| 280 | O << "\n"; |
| 281 | } |
| 282 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 283 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 284 | /// method to print assembly for each instruction. |
| 285 | /// |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 286 | bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 287 | SetupMachineFunction(MF); |
| 288 | |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame^] | 289 | // Print out constants referenced by the function |
| 290 | EmitConstantPool(MF.getConstantPool()); |
| 291 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 292 | // BBNumber is used here so that a given Printer will never give two |
| 293 | // BBs the same name. (If you have a better way, please let me know!) |
| 294 | static unsigned BBNumber = 0; |
| 295 | |
| 296 | O << "\n\n"; |
| 297 | // What's my mangled name? |
| 298 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 299 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 300 | // Print out labels for the function. |
| 301 | O << "\t.text\n"; |
| 302 | O << "\t.align 16\n"; |
| 303 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 304 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 305 | O << CurrentFnName << ":\n"; |
| 306 | |
| 307 | // Number each basic block so that we can consistently refer to them |
| 308 | // in PC-relative references. |
| 309 | NumberForBB.clear(); |
| 310 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 311 | I != E; ++I) { |
| 312 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 313 | } |
| 314 | |
| 315 | // Print out code for the function. |
| 316 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 317 | I != E; ++I) { |
| 318 | // Print a label for the basic block. |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 319 | O << ".LBB" << Mang->getValueName(MF.getFunction ()) |
| 320 | << "_" << I->getNumber () << ":\t! " |
| 321 | << I->getBasicBlock ()->getName () << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 322 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
Misha Brukman | 27177f8 | 2005-04-22 18:06:01 +0000 | [diff] [blame] | 323 | II != E; ++II) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 324 | // Print the assembly for the instruction. |
Chris Lattner | 0d8fcd3 | 2005-12-17 06:54:41 +0000 | [diff] [blame] | 325 | O << "\t"; |
| 326 | printInstruction(II); |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 327 | ++EmittedInsts; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | // We didn't modify anything. |
| 332 | return false; |
| 333 | } |
| 334 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 335 | void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 336 | const MachineOperand &MO = MI->getOperand (opNum); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 337 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 338 | bool CloseParen = false; |
| 339 | if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { |
| 340 | O << "%hi("; |
| 341 | CloseParen = true; |
Misha Brukman | f54ef97 | 2004-06-24 23:38:20 +0000 | [diff] [blame] | 342 | } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate()) |
| 343 | { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 344 | O << "%lo("; |
| 345 | CloseParen = true; |
| 346 | } |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 347 | switch (MO.getType()) { |
| 348 | case MachineOperand::MO_VirtualRegister: |
| 349 | if (Value *V = MO.getVRegValueOrNull()) { |
| 350 | O << "<" << V->getName() << ">"; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 351 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 352 | } |
| 353 | // FALLTHROUGH |
| 354 | case MachineOperand::MO_MachineRegister: |
| 355 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 356 | O << "%" << LowercaseString (RI.get(MO.getReg()).Name); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 357 | else |
| 358 | O << "%reg" << MO.getReg(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 359 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 360 | |
| 361 | case MachineOperand::MO_SignExtendedImmed: |
| 362 | case MachineOperand::MO_UnextendedImmed: |
| 363 | O << (int)MO.getImmedValue(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 364 | break; |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 365 | case MachineOperand::MO_MachineBasicBlock: { |
| 366 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 367 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 368 | << "_" << MBBOp->getNumber () << "\t! " |
| 369 | << MBBOp->getBasicBlock ()->getName (); |
| 370 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 371 | } |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 372 | case MachineOperand::MO_PCRelativeDisp: |
| 373 | std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; |
| 374 | abort (); |
| 375 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 376 | case MachineOperand::MO_GlobalAddress: |
| 377 | O << Mang->getValueName(MO.getGlobal()); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 378 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 379 | case MachineOperand::MO_ExternalSymbol: |
| 380 | O << MO.getSymbolName(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 381 | break; |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame] | 382 | case MachineOperand::MO_ConstantPoolIndex: |
Chris Lattner | b5e9eb6 | 2005-12-17 07:11:43 +0000 | [diff] [blame^] | 383 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" |
| 384 | << MO.getConstantPoolIndex(); |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame] | 385 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 386 | default: |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 387 | O << "<unknown operand type>"; abort (); break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 388 | } |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 389 | if (CloseParen) O << ")"; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 392 | bool SparcV8AsmPrinter::doInitialization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 393 | Mang = new Mangler(M); |
| 394 | return false; // success |
| 395 | } |
| 396 | |
Chris Lattner | 994b735 | 2005-12-16 06:34:17 +0000 | [diff] [blame] | 397 | bool SparcV8AsmPrinter::doFinalization(Module &M) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 398 | const TargetData &TD = TM.getTargetData(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 399 | |
| 400 | // Print out module-level global variables here. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 401 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 402 | if (I->hasInitializer()) { // External global require no code |
| 403 | O << "\n\n"; |
| 404 | std::string name = Mang->getValueName(I); |
| 405 | Constant *C = I->getInitializer(); |
| 406 | unsigned Size = TD.getTypeSize(C->getType()); |
| 407 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 408 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 409 | if (C->isNullValue() && |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 410 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 411 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 412 | SwitchSection(".data", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 413 | if (I->hasInternalLinkage()) |
| 414 | O << "\t.local " << name << "\n"; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 415 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 416 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 417 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 418 | O << "\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 419 | WriteAsOperand(O, I, true, true, &M); |
| 420 | O << "\n"; |
| 421 | } else { |
| 422 | switch (I->getLinkage()) { |
| 423 | case GlobalValue::LinkOnceLinkage: |
| 424 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 425 | // Nonnull linkonce -> weak |
| 426 | O << "\t.weak " << name << "\n"; |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 427 | SwitchSection("", I); |
| 428 | O << "\t.section\t\".llvm.linkonce.d." << name |
| 429 | << "\",\"aw\",@progbits\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 430 | break; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 431 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 432 | case GlobalValue::AppendingLinkage: |
| 433 | // FIXME: appending linkage variables should go into a section of |
| 434 | // their name or something. For now, just emit them as external. |
| 435 | case GlobalValue::ExternalLinkage: |
| 436 | // If external or appending, declare as a global symbol |
| 437 | O << "\t.globl " << name << "\n"; |
| 438 | // FALL THROUGH |
| 439 | case GlobalValue::InternalLinkage: |
| 440 | if (C->isNullValue()) |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 441 | SwitchSection(".bss", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 442 | else |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 443 | SwitchSection(".data", I); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 444 | break; |
Misha Brukman | c11c44f | 2004-11-19 21:49:19 +0000 | [diff] [blame] | 445 | case GlobalValue::GhostLinkage: |
| 446 | std::cerr << "Should not have any unmaterialized functions!\n"; |
| 447 | abort(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | O << "\t.align " << Align << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 451 | O << "\t.type " << name << ",#object\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 452 | O << "\t.size " << name << "," << Size << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 453 | O << name << ":\t\t\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 454 | WriteAsOperand(O, I, true, true, &M); |
| 455 | O << " = "; |
| 456 | WriteAsOperand(O, C, false, false, &M); |
| 457 | O << "\n"; |
| 458 | emitGlobalConstant(C); |
| 459 | } |
| 460 | } |
| 461 | |
Chris Lattner | 1dbed16 | 2005-12-17 07:04:29 +0000 | [diff] [blame] | 462 | AsmPrinter::doFinalization(M); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 463 | return false; // success |
| 464 | } |