Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 1 | //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 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" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 23 | #include "llvm/CodeGen/MachineInstr.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Support/Mangler.h" |
| 26 | #include "Support/Statistic.h" |
| 27 | #include "Support/StringExtras.h" |
| 28 | #include "Support/CommandLine.h" |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 29 | #include <cctype> |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 34 | |
| 35 | struct V8Printer : public MachineFunctionPass { |
| 36 | /// Output stream on which we're printing assembly code. |
| 37 | /// |
| 38 | std::ostream &O; |
| 39 | |
| 40 | /// Target machine description which we query for reg. names, data |
| 41 | /// layout, etc. |
| 42 | /// |
| 43 | TargetMachine &TM; |
| 44 | |
| 45 | /// Name-mangler for global names. |
| 46 | /// |
| 47 | Mangler *Mang; |
| 48 | |
| 49 | V8Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { } |
| 50 | |
| 51 | /// We name each basic block in a Function with a unique number, so |
| 52 | /// that we can consistently refer to them later. This is cleared |
| 53 | /// at the beginning of each call to runOnMachineFunction(). |
| 54 | /// |
| 55 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 56 | ValueMapTy NumberForBB; |
| 57 | |
| 58 | /// Cache of mangled name for current function. This is |
| 59 | /// recalculated at the beginning of each call to |
| 60 | /// runOnMachineFunction(). |
| 61 | /// |
| 62 | std::string CurrentFnName; |
| 63 | |
| 64 | virtual const char *getPassName() const { |
| 65 | return "SparcV8 Assembly Printer"; |
| 66 | } |
| 67 | |
| 68 | void emitConstantValueOnly(const Constant *CV); |
| 69 | void emitGlobalConstant(const Constant *CV); |
| 70 | void printConstantPool(MachineConstantPool *MCP); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 71 | void printOperand(const MachineInstr *MI, int opNum); |
Brian Gaeke | ceb2241 | 2004-06-18 06:27:59 +0000 | [diff] [blame] | 72 | void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 73 | void printMachineInstruction(const MachineInstr *MI); |
| 74 | bool runOnMachineFunction(MachineFunction &F); |
| 75 | bool doInitialization(Module &M); |
| 76 | bool doFinalization(Module &M); |
| 77 | }; |
| 78 | } // end of anonymous namespace |
| 79 | |
| 80 | /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8 |
| 81 | /// assembly code for a MachineFunction to the given output stream, |
| 82 | /// using the given target machine description. This should work |
| 83 | /// regardless of whether the function is in SSA form. |
| 84 | /// |
| 85 | FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o, |
| 86 | TargetMachine &tm) { |
| 87 | return new V8Printer(o, tm); |
| 88 | } |
| 89 | |
| 90 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 91 | /// |
| 92 | static inline char toOctal(int X) { |
| 93 | return (X&7)+'0'; |
| 94 | } |
| 95 | |
| 96 | /// getAsCString - Return the specified array as a C compatible |
| 97 | /// string, only if the predicate isStringCompatible is true. |
| 98 | /// |
| 99 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
| 100 | assert(CVA->isString() && "Array is not string compatible!"); |
| 101 | |
| 102 | O << "\""; |
| 103 | for (unsigned i = 0; i != CVA->getNumOperands(); ++i) { |
| 104 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
| 105 | |
| 106 | if (C == '"') { |
| 107 | O << "\\\""; |
| 108 | } else if (C == '\\') { |
| 109 | O << "\\\\"; |
| 110 | } else if (isprint(C)) { |
| 111 | O << C; |
| 112 | } else { |
| 113 | switch(C) { |
| 114 | case '\b': O << "\\b"; break; |
| 115 | case '\f': O << "\\f"; break; |
| 116 | case '\n': O << "\\n"; break; |
| 117 | case '\r': O << "\\r"; break; |
| 118 | case '\t': O << "\\t"; break; |
| 119 | default: |
| 120 | O << '\\'; |
| 121 | O << toOctal(C >> 6); |
| 122 | O << toOctal(C >> 3); |
| 123 | O << toOctal(C >> 0); |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | O << "\""; |
| 129 | } |
| 130 | |
| 131 | // Print out the specified constant, without a storage class. Only the |
| 132 | // constants valid in constant expressions can occur here. |
| 133 | void V8Printer::emitConstantValueOnly(const Constant *CV) { |
| 134 | if (CV->isNullValue()) |
| 135 | O << "0"; |
| 136 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 137 | assert(CB == ConstantBool::True); |
| 138 | O << "1"; |
| 139 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 140 | if (((CI->getValue() << 32) >> 32) == CI->getValue()) |
| 141 | O << CI->getValue(); |
| 142 | else |
| 143 | O << (unsigned long long)CI->getValue(); |
| 144 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 145 | O << CI->getValue(); |
| 146 | else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV)) |
| 147 | // This is a constant address for a global variable or function. Use the |
| 148 | // name of the variable or function as the address value. |
| 149 | O << Mang->getValueName(CPR->getValue()); |
| 150 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 151 | const TargetData &TD = TM.getTargetData(); |
| 152 | switch(CE->getOpcode()) { |
| 153 | case Instruction::GetElementPtr: { |
| 154 | // generate a symbolic expression for the byte address |
| 155 | const Constant *ptrVal = CE->getOperand(0); |
| 156 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 157 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 158 | O << "("; |
| 159 | emitConstantValueOnly(ptrVal); |
| 160 | O << ") + " << Offset; |
| 161 | } else { |
| 162 | emitConstantValueOnly(ptrVal); |
| 163 | } |
| 164 | break; |
| 165 | } |
| 166 | case Instruction::Cast: { |
| 167 | // Support only non-converting or widening casts for now, that is, ones |
| 168 | // that do not involve a change in value. This assertion is really gross, |
| 169 | // and may not even be a complete check. |
| 170 | Constant *Op = CE->getOperand(0); |
| 171 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 172 | |
| 173 | // Pointers on ILP32 machines can be losslessly converted back and |
| 174 | // forth into 32-bit or wider integers, regardless of signedness. |
| 175 | assert(((isa<PointerType>(OpTy) |
| 176 | && (Ty == Type::LongTy || Ty == Type::ULongTy |
| 177 | || Ty == Type::IntTy || Ty == Type::UIntTy)) |
| 178 | || (isa<PointerType>(Ty) |
| 179 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy |
| 180 | || OpTy == Type::IntTy || OpTy == Type::UIntTy)) |
| 181 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 182 | && OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 183 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 184 | O << "("; |
| 185 | emitConstantValueOnly(Op); |
| 186 | O << ")"; |
| 187 | break; |
| 188 | } |
| 189 | case Instruction::Add: |
| 190 | O << "("; |
| 191 | emitConstantValueOnly(CE->getOperand(0)); |
| 192 | O << ") + ("; |
| 193 | emitConstantValueOnly(CE->getOperand(1)); |
| 194 | O << ")"; |
| 195 | break; |
| 196 | default: |
| 197 | assert(0 && "Unsupported operator!"); |
| 198 | } |
| 199 | } else { |
| 200 | assert(0 && "Unknown constant value!"); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Print a constant value or values, with the appropriate storage class as a |
| 205 | // prefix. |
| 206 | void V8Printer::emitGlobalConstant(const Constant *CV) { |
| 207 | const TargetData &TD = TM.getTargetData(); |
| 208 | |
Brian Gaeke | 9d2427c | 2004-06-18 08:59:16 +0000 | [diff] [blame] | 209 | if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 210 | if (CVA->isString()) { |
| 211 | O << "\t.ascii\t"; |
| 212 | printAsCString(O, CVA); |
| 213 | O << "\n"; |
| 214 | } else { // Not a string. Print the values in successive locations |
| 215 | const std::vector<Use> &constValues = CVA->getValues(); |
| 216 | for (unsigned i=0; i < constValues.size(); i++) |
| 217 | emitGlobalConstant(cast<Constant>(constValues[i].get())); |
| 218 | } |
| 219 | return; |
| 220 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 221 | // Print the fields in successive locations. Pad to align if needed! |
| 222 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
| 223 | const std::vector<Use>& constValues = CVS->getValues(); |
| 224 | unsigned sizeSoFar = 0; |
| 225 | for (unsigned i=0, N = constValues.size(); i < N; i++) { |
| 226 | const Constant* field = cast<Constant>(constValues[i].get()); |
| 227 | |
| 228 | // Check if padding is needed and insert one or more 0s. |
| 229 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 230 | unsigned padSize = ((i == N-1? cvsLayout->StructSize |
| 231 | : cvsLayout->MemberOffsets[i+1]) |
| 232 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 233 | sizeSoFar += fieldSize + padSize; |
| 234 | |
| 235 | // Now print the actual field value |
| 236 | emitGlobalConstant(field); |
| 237 | |
| 238 | // Insert the field padding unless it's zero bytes... |
| 239 | if (padSize) |
Brian Gaeke | 9d2427c | 2004-06-18 08:59:16 +0000 | [diff] [blame] | 240 | O << "\t.skip\t " << padSize << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 241 | } |
| 242 | assert(sizeSoFar == cvsLayout->StructSize && |
| 243 | "Layout of constant struct may be incorrect!"); |
| 244 | return; |
| 245 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 246 | // FP Constants are printed as integer constants to avoid losing |
| 247 | // precision... |
| 248 | double Val = CFP->getValue(); |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 249 | switch (CFP->getType()->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 250 | default: assert(0 && "Unknown floating point type!"); |
| 251 | case Type::FloatTyID: { |
| 252 | union FU { // Abide by C TBAA rules |
| 253 | float FVal; |
| 254 | unsigned UVal; |
| 255 | } U; |
| 256 | U.FVal = Val; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 257 | O << ".long\t" << U.UVal << "\t! float " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | case Type::DoubleTyID: { |
| 261 | union DU { // Abide by C TBAA rules |
| 262 | double FVal; |
| 263 | uint64_t UVal; |
| 264 | } U; |
| 265 | U.FVal = Val; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 266 | O << ".quad\t" << U.UVal << "\t! double " << Val << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 267 | return; |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | const Type *type = CV->getType(); |
| 273 | O << "\t"; |
Chris Lattner | f70c22b | 2004-06-17 18:19:28 +0000 | [diff] [blame] | 274 | switch (type->getTypeID()) { |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 275 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 276 | O << ".byte"; |
| 277 | break; |
| 278 | case Type::UShortTyID: case Type::ShortTyID: |
| 279 | O << ".word"; |
| 280 | break; |
| 281 | case Type::FloatTyID: case Type::PointerTyID: |
| 282 | case Type::UIntTyID: case Type::IntTyID: |
| 283 | O << ".long"; |
| 284 | break; |
| 285 | case Type::DoubleTyID: |
| 286 | case Type::ULongTyID: case Type::LongTyID: |
| 287 | O << ".quad"; |
| 288 | break; |
| 289 | default: |
| 290 | assert (0 && "Can't handle printing this type of thing"); |
| 291 | break; |
| 292 | } |
| 293 | O << "\t"; |
| 294 | emitConstantValueOnly(CV); |
| 295 | O << "\n"; |
| 296 | } |
| 297 | |
| 298 | /// printConstantPool - Print to the current output stream assembly |
| 299 | /// representations of the constants in the constant pool MCP. This is |
| 300 | /// used to print out constants which have been "spilled to memory" by |
| 301 | /// the code generator. |
| 302 | /// |
| 303 | void V8Printer::printConstantPool(MachineConstantPool *MCP) { |
| 304 | const std::vector<Constant*> &CP = MCP->getConstants(); |
| 305 | const TargetData &TD = TM.getTargetData(); |
| 306 | |
| 307 | if (CP.empty()) return; |
| 308 | |
| 309 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 310 | O << "\t.section .rodata\n"; |
| 311 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
| 312 | << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 313 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t!" |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 314 | << *CP[i] << "\n"; |
| 315 | emitGlobalConstant(CP[i]); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 320 | /// method to print assembly for each instruction. |
| 321 | /// |
| 322 | bool V8Printer::runOnMachineFunction(MachineFunction &MF) { |
| 323 | // BBNumber is used here so that a given Printer will never give two |
| 324 | // BBs the same name. (If you have a better way, please let me know!) |
| 325 | static unsigned BBNumber = 0; |
| 326 | |
| 327 | O << "\n\n"; |
| 328 | // What's my mangled name? |
| 329 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 330 | |
| 331 | // Print out constants referenced by the function |
| 332 | printConstantPool(MF.getConstantPool()); |
| 333 | |
| 334 | // Print out labels for the function. |
| 335 | O << "\t.text\n"; |
| 336 | O << "\t.align 16\n"; |
| 337 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 338 | O << "\t.type\t" << CurrentFnName << ", #function\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 339 | O << CurrentFnName << ":\n"; |
| 340 | |
| 341 | // Number each basic block so that we can consistently refer to them |
| 342 | // in PC-relative references. |
| 343 | NumberForBB.clear(); |
| 344 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 345 | I != E; ++I) { |
| 346 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 347 | } |
| 348 | |
| 349 | // Print out code for the function. |
| 350 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 351 | I != E; ++I) { |
| 352 | // Print a label for the basic block. |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 353 | O << ".LBB" << Mang->getValueName(MF.getFunction ()) |
| 354 | << "_" << I->getNumber () << ":\t! " |
| 355 | << I->getBasicBlock ()->getName () << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 356 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 357 | II != E; ++II) { |
| 358 | // Print the assembly for the instruction. |
| 359 | O << "\t"; |
| 360 | printMachineInstruction(II); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // We didn't modify anything. |
| 365 | return false; |
| 366 | } |
| 367 | |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 368 | void V8Printer::printOperand(const MachineInstr *MI, int opNum) { |
| 369 | const MachineOperand &MO = MI->getOperand (opNum); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 370 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 371 | bool CloseParen = false; |
| 372 | if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) { |
| 373 | O << "%hi("; |
| 374 | CloseParen = true; |
Misha Brukman | f54ef97 | 2004-06-24 23:38:20 +0000 | [diff] [blame] | 375 | } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate()) |
| 376 | { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 377 | O << "%lo("; |
| 378 | CloseParen = true; |
| 379 | } |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 380 | switch (MO.getType()) { |
| 381 | case MachineOperand::MO_VirtualRegister: |
| 382 | if (Value *V = MO.getVRegValueOrNull()) { |
| 383 | O << "<" << V->getName() << ">"; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 384 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 385 | } |
| 386 | // FALLTHROUGH |
| 387 | case MachineOperand::MO_MachineRegister: |
| 388 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
Brian Gaeke | a8b00ca | 2004-03-06 05:30:21 +0000 | [diff] [blame] | 389 | O << "%" << LowercaseString (RI.get(MO.getReg()).Name); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 390 | else |
| 391 | O << "%reg" << MO.getReg(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 392 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 393 | |
| 394 | case MachineOperand::MO_SignExtendedImmed: |
| 395 | case MachineOperand::MO_UnextendedImmed: |
| 396 | O << (int)MO.getImmedValue(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 397 | break; |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 398 | case MachineOperand::MO_MachineBasicBlock: { |
| 399 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
| 400 | O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction()) |
| 401 | << "_" << MBBOp->getNumber () << "\t! " |
| 402 | << MBBOp->getBasicBlock ()->getName (); |
| 403 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 404 | } |
Brian Gaeke | 09c1309 | 2004-06-17 19:39:23 +0000 | [diff] [blame] | 405 | case MachineOperand::MO_PCRelativeDisp: |
| 406 | std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs"; |
| 407 | abort (); |
| 408 | return; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 409 | case MachineOperand::MO_GlobalAddress: |
| 410 | O << Mang->getValueName(MO.getGlobal()); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 411 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 412 | case MachineOperand::MO_ExternalSymbol: |
| 413 | O << MO.getSymbolName(); |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 414 | break; |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame^] | 415 | case MachineOperand::MO_ConstantPoolIndex: |
| 416 | O << ".CPI" << CurrentFnName << "_" << MO.getConstantPoolIndex(); |
| 417 | break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 418 | default: |
Brian Gaeke | 8a0ae9e | 2004-06-27 22:50:44 +0000 | [diff] [blame^] | 419 | O << "<unknown operand type>"; abort (); break; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 420 | } |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 421 | if (CloseParen) O << ")"; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 424 | static bool isLoadInstruction (const MachineInstr *MI) { |
| 425 | switch (MI->getOpcode ()) { |
Brian Gaeke | af0492e | 2004-06-24 07:37:12 +0000 | [diff] [blame] | 426 | case V8::LDSB: |
| 427 | case V8::LDSH: |
| 428 | case V8::LDUB: |
| 429 | case V8::LDUH: |
| 430 | case V8::LD: |
| 431 | case V8::LDD: |
| 432 | case V8::LDFrr: |
| 433 | case V8::LDFri: |
| 434 | case V8::LDDFrr: |
| 435 | case V8::LDDFri: |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 436 | return true; |
| 437 | default: |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static bool isStoreInstruction (const MachineInstr *MI) { |
| 443 | switch (MI->getOpcode ()) { |
Brian Gaeke | af0492e | 2004-06-24 07:37:12 +0000 | [diff] [blame] | 444 | case V8::STB: |
| 445 | case V8::STH: |
| 446 | case V8::ST: |
| 447 | case V8::STD: |
| 448 | case V8::STFrr: |
| 449 | case V8::STFri: |
| 450 | case V8::STDFrr: |
| 451 | case V8::STDFri: |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 452 | return true; |
| 453 | default: |
| 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | |
Brian Gaeke | ceb2241 | 2004-06-18 06:27:59 +0000 | [diff] [blame] | 458 | /// printBaseOffsetPair - Print two consecutive operands of MI, starting at #i, |
| 459 | /// which form a base + offset pair (which may have brackets around it, if |
| 460 | /// brackets is true, or may be in the form base - constant, if offset is a |
| 461 | /// negative constant). |
| 462 | /// |
| 463 | void V8Printer::printBaseOffsetPair (const MachineInstr *MI, int i, |
| 464 | bool brackets) { |
| 465 | if (brackets) O << "["; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 466 | printOperand (MI, i); |
Brian Gaeke | ceb2241 | 2004-06-18 06:27:59 +0000 | [diff] [blame] | 467 | if (MI->getOperand (i + 1).isImmediate()) { |
| 468 | int Val = (int) MI->getOperand (i + 1).getImmedValue (); |
| 469 | if (Val != 0) { |
| 470 | O << ((Val >= 0) ? " + " : " - "); |
| 471 | O << ((Val >= 0) ? Val : -Val); |
| 472 | } |
| 473 | } else { |
| 474 | O << " + "; |
| 475 | printOperand (MI, i + 1); |
Brian Gaeke | 8005ed3 | 2004-04-07 17:33:56 +0000 | [diff] [blame] | 476 | } |
Brian Gaeke | ceb2241 | 2004-06-18 06:27:59 +0000 | [diff] [blame] | 477 | if (brackets) O << "]"; |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 480 | /// printMachineInstruction -- Print out a single SparcV8 LLVM instruction |
| 481 | /// MI in GAS syntax to the current output stream. |
| 482 | /// |
| 483 | void V8Printer::printMachineInstruction(const MachineInstr *MI) { |
| 484 | unsigned Opcode = MI->getOpcode(); |
Chris Lattner | 143e0ea | 2004-06-02 05:47:26 +0000 | [diff] [blame] | 485 | const TargetInstrInfo &TII = *TM.getInstrInfo(); |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 486 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 487 | O << Desc.Name << " "; |
| 488 | |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 489 | // Printing memory instructions is a special case. |
Brian Gaeke | fa4bb09 | 2004-04-07 04:29:03 +0000 | [diff] [blame] | 490 | // for loads: %dest = op %base, offset --> op [%base + offset], %dest |
Brian Gaeke | 8308d04 | 2004-06-17 22:34:19 +0000 | [diff] [blame] | 491 | // for stores: op %base, offset, %src --> op %src, [%base + offset] |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 492 | if (isLoadInstruction (MI)) { |
Brian Gaeke | fa4bb09 | 2004-04-07 04:29:03 +0000 | [diff] [blame] | 493 | printBaseOffsetPair (MI, 1); |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 494 | O << ", "; |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 495 | printOperand (MI, 0); |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 496 | O << "\n"; |
| 497 | return; |
| 498 | } else if (isStoreInstruction (MI)) { |
Brian Gaeke | 8308d04 | 2004-06-17 22:34:19 +0000 | [diff] [blame] | 499 | printOperand (MI, 2); |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 500 | O << ", "; |
Brian Gaeke | 8308d04 | 2004-06-17 22:34:19 +0000 | [diff] [blame] | 501 | printBaseOffsetPair (MI, 0); |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 502 | O << "\n"; |
| 503 | return; |
Brian Gaeke | ceb2241 | 2004-06-18 06:27:59 +0000 | [diff] [blame] | 504 | } else if (Opcode == V8::JMPLrr) { |
| 505 | printBaseOffsetPair (MI, 1, false); |
| 506 | O << ", "; |
| 507 | printOperand (MI, 0); |
| 508 | O << "\n"; |
| 509 | return; |
Brian Gaeke | 1c38175 | 2004-04-06 22:10:11 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 512 | // print non-immediate, non-register-def operands |
| 513 | // then print immediate operands |
| 514 | // then print register-def operands. |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 515 | std::vector<int> print_order; |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 516 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 517 | if (!(MI->getOperand (i).isImmediate () |
| 518 | || (MI->getOperand (i).isRegister () |
| 519 | && MI->getOperand (i).isDef ()))) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 520 | print_order.push_back (i); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 521 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 522 | if (MI->getOperand (i).isImmediate ()) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 523 | print_order.push_back (i); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 524 | for (unsigned i = 0; i < MI->getNumOperands (); ++i) |
| 525 | if (MI->getOperand (i).isRegister () && MI->getOperand (i).isDef ()) |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 526 | print_order.push_back (i); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 527 | for (unsigned i = 0, e = print_order.size (); i != e; ++i) { |
Brian Gaeke | 446ae11 | 2004-06-15 19:52:59 +0000 | [diff] [blame] | 528 | printOperand (MI, print_order[i]); |
Brian Gaeke | 62aa28a | 2004-03-05 08:39:09 +0000 | [diff] [blame] | 529 | if (i != (print_order.size () - 1)) |
| 530 | O << ", "; |
| 531 | } |
| 532 | O << "\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | bool V8Printer::doInitialization(Module &M) { |
| 536 | Mang = new Mangler(M); |
| 537 | return false; // success |
| 538 | } |
| 539 | |
| 540 | // SwitchSection - Switch to the specified section of the executable if we are |
| 541 | // not already in it! |
| 542 | // |
| 543 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 544 | const char *NewSection) { |
| 545 | if (CurSection != NewSection) { |
| 546 | CurSection = NewSection; |
| 547 | if (!CurSection.empty()) |
| 548 | OS << "\t" << NewSection << "\n"; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | bool V8Printer::doFinalization(Module &M) { |
| 553 | const TargetData &TD = TM.getTargetData(); |
| 554 | std::string CurSection; |
| 555 | |
| 556 | // Print out module-level global variables here. |
| 557 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 558 | if (I->hasInitializer()) { // External global require no code |
| 559 | O << "\n\n"; |
| 560 | std::string name = Mang->getValueName(I); |
| 561 | Constant *C = I->getInitializer(); |
| 562 | unsigned Size = TD.getTypeSize(C->getType()); |
| 563 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 564 | |
| 565 | if (C->isNullValue() && |
| 566 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 567 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
| 568 | SwitchSection(O, CurSection, ".data"); |
| 569 | if (I->hasInternalLinkage()) |
| 570 | O << "\t.local " << name << "\n"; |
| 571 | |
| 572 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
| 573 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 574 | O << "\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 575 | WriteAsOperand(O, I, true, true, &M); |
| 576 | O << "\n"; |
| 577 | } else { |
| 578 | switch (I->getLinkage()) { |
| 579 | case GlobalValue::LinkOnceLinkage: |
| 580 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
| 581 | // Nonnull linkonce -> weak |
| 582 | O << "\t.weak " << name << "\n"; |
| 583 | SwitchSection(O, CurSection, ""); |
| 584 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 585 | break; |
| 586 | |
| 587 | case GlobalValue::AppendingLinkage: |
| 588 | // FIXME: appending linkage variables should go into a section of |
| 589 | // their name or something. For now, just emit them as external. |
| 590 | case GlobalValue::ExternalLinkage: |
| 591 | // If external or appending, declare as a global symbol |
| 592 | O << "\t.globl " << name << "\n"; |
| 593 | // FALL THROUGH |
| 594 | case GlobalValue::InternalLinkage: |
| 595 | if (C->isNullValue()) |
| 596 | SwitchSection(O, CurSection, ".bss"); |
| 597 | else |
| 598 | SwitchSection(O, CurSection, ".data"); |
| 599 | break; |
| 600 | } |
| 601 | |
| 602 | O << "\t.align " << Align << "\n"; |
Brian Gaeke | 54cc3c2 | 2004-03-16 22:52:04 +0000 | [diff] [blame] | 603 | O << "\t.type " << name << ",#object\n"; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 604 | O << "\t.size " << name << "," << Size << "\n"; |
Brian Gaeke | 79db740 | 2004-03-16 22:37:12 +0000 | [diff] [blame] | 605 | O << name << ":\t\t\t\t! "; |
Brian Gaeke | 4acfd03 | 2004-03-04 06:00:41 +0000 | [diff] [blame] | 606 | WriteAsOperand(O, I, true, true, &M); |
| 607 | O << " = "; |
| 608 | WriteAsOperand(O, C, false, false, &M); |
| 609 | O << "\n"; |
| 610 | emitGlobalConstant(C); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | delete Mang; |
| 615 | return false; // success |
| 616 | } |