Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 1 | //===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===// |
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 | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 10 | // This file contains a printer that converts from our internal |
| 11 | // representation of machine-dependent LLVM code to Intel-format |
| 12 | // assembly language. This printer is the output mechanism used |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 13 | // by `llc' and `lli -print-machineinstrs' on X86. |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "X86.h" |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 18 | #include "X86InstrInfo.h" |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d59414f | 2003-08-11 20:06:16 +0000 | [diff] [blame] | 21 | #include "llvm/Module.h" |
| 22 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | d59414f | 2003-08-11 20:06:16 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Mangler.h" |
Brian Gaeke | 2c9b913 | 2003-10-06 15:41:21 +0000 | [diff] [blame] | 28 | #include "Support/Statistic.h" |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 29 | #include "Support/StringExtras.h" |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 30 | #include "Support/CommandLine.h" |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 31 | |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 32 | namespace { |
Brian Gaeke | 2c9b913 | 2003-10-06 15:41:21 +0000 | [diff] [blame] | 33 | Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed"); |
| 34 | |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 35 | // FIXME: This should be automatically picked up by autoconf from the C |
| 36 | // frontend |
| 37 | cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden, |
| 38 | cl::desc("Emit X86 assembly code suitable for consumption by cygwin")); |
| 39 | |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 40 | struct Printer : public MachineFunctionPass { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 41 | /// Output stream on which we're printing assembly code. |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 42 | /// |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 43 | std::ostream &O; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 44 | |
| 45 | /// Target machine description which we query for reg. names, data |
| 46 | /// layout, etc. |
| 47 | /// |
| 48 | TargetMachine &TM; |
| 49 | |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 50 | /// Name-mangler for global names. |
| 51 | /// |
| 52 | Mangler *Mang; |
| 53 | |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 54 | Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { } |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 55 | |
| 56 | /// We name each basic block in a Function with a unique number, so |
| 57 | /// that we can consistently refer to them later. This is cleared |
| 58 | /// at the beginning of each call to runOnMachineFunction(). |
| 59 | /// |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 60 | typedef std::map<const Value *, unsigned> ValueMapTy; |
| 61 | ValueMapTy NumberForBB; |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 62 | |
| 63 | /// Cache of mangled name for current function. This is |
| 64 | /// recalculated at the beginning of each call to |
| 65 | /// runOnMachineFunction(). |
| 66 | /// |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 67 | std::string CurrentFnName; |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 68 | |
Chris Lattner | f0eb7be | 2002-12-15 21:13:40 +0000 | [diff] [blame] | 69 | virtual const char *getPassName() const { |
| 70 | return "X86 Assembly Printer"; |
| 71 | } |
| 72 | |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 73 | void checkImplUses (const TargetInstrDescriptor &Desc); |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 74 | void printMachineInstruction(const MachineInstr *MI); |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 75 | void printOp(const MachineOperand &MO, |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 76 | bool elideOffsetKeyword = false); |
| 77 | void printMemReference(const MachineInstr *MI, unsigned Op); |
| 78 | void printConstantPool(MachineConstantPool *MCP); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 79 | bool runOnMachineFunction(MachineFunction &F); |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 80 | bool doInitialization(Module &M); |
| 81 | bool doFinalization(Module &M); |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 82 | void emitGlobalConstant(const Constant* CV); |
| 83 | void emitConstantValueOnly(const Constant *CV); |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 84 | }; |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 85 | } // end of anonymous namespace |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 86 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 87 | /// createX86CodePrinterPass - Returns a pass that prints the X86 |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 88 | /// assembly code for a MachineFunction to the given output stream, |
| 89 | /// using the given target machine description. This should work |
| 90 | /// regardless of whether the function is in SSA form. |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 91 | /// |
Brian Gaeke | 9d99b43 | 2003-08-13 18:15:15 +0000 | [diff] [blame] | 92 | FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){ |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 93 | return new Printer(o, tm); |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 96 | /// isStringCompatible - Can we treat the specified array as a string? |
| 97 | /// Only if it is an array of ubytes or non-negative sbytes. |
| 98 | /// |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 99 | static bool isStringCompatible(const ConstantArray *CVA) { |
| 100 | const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType(); |
| 101 | if (ETy == Type::UByteTy) return true; |
| 102 | if (ETy != Type::SByteTy) return false; |
| 103 | |
| 104 | for (unsigned i = 0; i < CVA->getNumOperands(); ++i) |
| 105 | if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0) |
| 106 | return false; |
| 107 | |
| 108 | return true; |
| 109 | } |
| 110 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 111 | /// toOctal - Convert the low order bits of X into an octal digit. |
| 112 | /// |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 113 | static inline char toOctal(int X) { |
| 114 | return (X&7)+'0'; |
| 115 | } |
| 116 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 117 | /// getAsCString - Return the specified array as a C compatible |
| 118 | /// string, only if the predicate isStringCompatible is true. |
| 119 | /// |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 120 | static void printAsCString(std::ostream &O, const ConstantArray *CVA) { |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 121 | assert(isStringCompatible(CVA) && "Array is not string compatible!"); |
| 122 | |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 123 | O << "\""; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 124 | for (unsigned i = 0; i < CVA->getNumOperands(); ++i) { |
Chris Lattner | c07736a | 2003-07-23 15:22:26 +0000 | [diff] [blame] | 125 | unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue(); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 126 | |
| 127 | if (C == '"') { |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 128 | O << "\\\""; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 129 | } else if (C == '\\') { |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 130 | O << "\\\\"; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 131 | } else if (isprint(C)) { |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 132 | O << C; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 133 | } else { |
| 134 | switch(C) { |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 135 | case '\b': O << "\\b"; break; |
| 136 | case '\f': O << "\\f"; break; |
| 137 | case '\n': O << "\\n"; break; |
| 138 | case '\r': O << "\\r"; break; |
| 139 | case '\t': O << "\\t"; break; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 140 | default: |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 141 | O << '\\'; |
| 142 | O << toOctal(C >> 6); |
| 143 | O << toOctal(C >> 3); |
| 144 | O << toOctal(C >> 0); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | } |
| 148 | } |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 149 | O << "\""; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 152 | // Print out the specified constant, without a storage class. Only the |
| 153 | // constants valid in constant expressions can occur here. |
| 154 | void Printer::emitConstantValueOnly(const Constant *CV) { |
| 155 | if (CV->isNullValue()) |
| 156 | O << "0"; |
| 157 | else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) { |
| 158 | assert(CB == ConstantBool::True); |
| 159 | O << "1"; |
| 160 | } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV)) |
| 161 | O << CI->getValue(); |
| 162 | else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV)) |
| 163 | O << CI->getValue(); |
| 164 | else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV)) |
| 165 | // This is a constant address for a global variable or function. Use the |
| 166 | // name of the variable or function as the address value. |
| 167 | O << Mang->getValueName(CPR->getValue()) << "\n"; |
| 168 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { |
| 169 | const TargetData &TD = TM.getTargetData(); |
| 170 | switch(CE->getOpcode()) { |
| 171 | case Instruction::GetElementPtr: { |
| 172 | // generate a symbolic expression for the byte address |
| 173 | const Constant *ptrVal = CE->getOperand(0); |
| 174 | std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end()); |
| 175 | if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) { |
| 176 | O << "("; |
| 177 | emitConstantValueOnly(ptrVal); |
| 178 | O << ") + " + Offset; |
| 179 | } else { |
| 180 | emitConstantValueOnly(ptrVal); |
| 181 | } |
| 182 | break; |
| 183 | } |
| 184 | case Instruction::Cast: { |
| 185 | // Support only non-converting or widening casts for now, that is, ones |
| 186 | // that do not involve a change in value. This assertion is not a |
| 187 | // complete check. |
| 188 | Constant *Op = CE->getOperand(0); |
| 189 | const Type *OpTy = Op->getType(), *Ty = CE->getType(); |
| 190 | assert(((isa<PointerType>(OpTy) |
| 191 | && (Ty == Type::LongTy || Ty == Type::ULongTy)) |
| 192 | || (isa<PointerType>(Ty) |
| 193 | && (OpTy == Type::LongTy || OpTy == Type::ULongTy))) |
| 194 | || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy)) |
| 195 | && (OpTy->isLosslesslyConvertibleTo(Ty)))) |
| 196 | && "FIXME: Don't yet support this kind of constant cast expr"); |
| 197 | O << "("; |
| 198 | emitConstantValueOnly(Op); |
| 199 | O << ")"; |
| 200 | break; |
| 201 | } |
| 202 | case Instruction::Add: |
| 203 | O << "("; |
| 204 | emitConstantValueOnly(CE->getOperand(0)); |
| 205 | O << ") + ("; |
| 206 | emitConstantValueOnly(CE->getOperand(1)); |
| 207 | O << ")"; |
| 208 | break; |
| 209 | default: |
| 210 | assert(0 && "Unsupported operator!"); |
| 211 | } |
| 212 | } else { |
| 213 | assert(0 && "Unknown constant value!"); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // Print a constant value or values, with the appropriate storage class as a |
| 218 | // prefix. |
| 219 | void Printer::emitGlobalConstant(const Constant *CV) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 220 | const TargetData &TD = TM.getTargetData(); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 221 | |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 222 | if (CV->isNullValue()) { |
| 223 | O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n"; |
Chris Lattner | 3e119c6 | 2003-11-03 19:44:05 +0000 | [diff] [blame] | 224 | return; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 225 | } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) { |
| 226 | if (isStringCompatible(CVA)) { |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 227 | O << "\t.ascii\t"; |
| 228 | printAsCString(O, CVA); |
| 229 | O << "\n"; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 230 | } else { // Not a string. Print the values in successive locations |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 231 | const std::vector<Use> &constValues = CVA->getValues(); |
| 232 | for (unsigned i=0; i < constValues.size(); i++) |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 233 | emitGlobalConstant(cast<Constant>(constValues[i].get())); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 234 | } |
Chris Lattner | 3e119c6 | 2003-11-03 19:44:05 +0000 | [diff] [blame] | 235 | return; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 236 | } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) { |
| 237 | // Print the fields in successive locations. Pad to align if needed! |
| 238 | const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType()); |
| 239 | const std::vector<Use>& constValues = CVS->getValues(); |
| 240 | unsigned sizeSoFar = 0; |
| 241 | for (unsigned i=0, N = constValues.size(); i < N; i++) { |
| 242 | const Constant* field = cast<Constant>(constValues[i].get()); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 243 | |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 244 | // Check if padding is needed and insert one or more 0s. |
| 245 | unsigned fieldSize = TD.getTypeSize(field->getType()); |
| 246 | unsigned padSize = ((i == N-1? cvsLayout->StructSize |
| 247 | : cvsLayout->MemberOffsets[i+1]) |
| 248 | - cvsLayout->MemberOffsets[i]) - fieldSize; |
| 249 | sizeSoFar += fieldSize + padSize; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 250 | |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 251 | // Now print the actual field value |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 252 | emitGlobalConstant(field); |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 253 | |
| 254 | // Insert the field padding unless it's zero bytes... |
Chris Lattner | b3aad5d | 2003-09-10 19:52:24 +0000 | [diff] [blame] | 255 | if (padSize) |
| 256 | O << "\t.zero\t " << padSize << "\n"; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 257 | } |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 258 | assert(sizeSoFar == cvsLayout->StructSize && |
| 259 | "Layout of constant struct may be incorrect!"); |
Chris Lattner | 3e119c6 | 2003-11-03 19:44:05 +0000 | [diff] [blame] | 260 | return; |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 261 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
| 262 | // FP Constants are printed as integer constants to avoid losing |
| 263 | // precision... |
| 264 | double Val = CFP->getValue(); |
| 265 | switch (CFP->getType()->getPrimitiveID()) { |
| 266 | default: assert(0 && "Unknown floating point type!"); |
| 267 | case Type::FloatTyID: { |
| 268 | union FU { // Abide by C TBAA rules |
| 269 | float FVal; |
| 270 | unsigned UVal; |
| 271 | } U; |
| 272 | U.FVal = Val; |
| 273 | O << ".long\t" << U.UVal << "\t# float " << Val << "\n"; |
| 274 | return; |
| 275 | } |
| 276 | case Type::DoubleTyID: { |
| 277 | union DU { // Abide by C TBAA rules |
| 278 | double FVal; |
| 279 | uint64_t UVal; |
| 280 | } U; |
| 281 | U.FVal = Val; |
| 282 | O << ".quad\t" << U.UVal << "\t# double " << Val << "\n"; |
| 283 | return; |
| 284 | } |
| 285 | } |
Chris Lattner | 3e119c6 | 2003-11-03 19:44:05 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | const Type *type = CV->getType(); |
| 289 | O << "\t"; |
| 290 | switch (type->getPrimitiveID()) { |
| 291 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 292 | O << ".byte"; |
| 293 | break; |
| 294 | case Type::UShortTyID: case Type::ShortTyID: |
| 295 | O << ".word"; |
| 296 | break; |
| 297 | case Type::FloatTyID: case Type::PointerTyID: |
| 298 | case Type::UIntTyID: case Type::IntTyID: |
| 299 | O << ".long"; |
| 300 | break; |
| 301 | case Type::DoubleTyID: |
| 302 | case Type::ULongTyID: case Type::LongTyID: |
| 303 | O << ".quad"; |
| 304 | break; |
| 305 | default: |
| 306 | assert (0 && "Can't handle printing this type of thing"); |
| 307 | break; |
| 308 | } |
| 309 | O << "\t"; |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 310 | emitConstantValueOnly(CV); |
| 311 | O << "\n"; |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 312 | } |
Chris Lattner | dbb61c6 | 2002-11-17 22:53:13 +0000 | [diff] [blame] | 313 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 314 | /// printConstantPool - Print to the current output stream assembly |
| 315 | /// representations of the constants in the constant pool MCP. This is |
| 316 | /// used to print out constants which have been "spilled to memory" by |
| 317 | /// the code generator. |
| 318 | /// |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 319 | void Printer::printConstantPool(MachineConstantPool *MCP) { |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 320 | const std::vector<Constant*> &CP = MCP->getConstants(); |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 321 | const TargetData &TD = TM.getTargetData(); |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 322 | |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 323 | if (CP.empty()) return; |
| 324 | |
| 325 | for (unsigned i = 0, e = CP.size(); i != e; ++i) { |
| 326 | O << "\t.section .rodata\n"; |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 327 | O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType()) |
Brian Gaeke | 5e00157 | 2003-06-26 18:02:30 +0000 | [diff] [blame] | 328 | << "\n"; |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 329 | O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#" |
| 330 | << *CP[i] << "\n"; |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 331 | emitGlobalConstant(CP[i]); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 332 | } |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 335 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 336 | /// method to print assembly for each instruction. |
| 337 | /// |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 338 | bool Printer::runOnMachineFunction(MachineFunction &MF) { |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 339 | // BBNumber is used here so that a given Printer will never give two |
| 340 | // BBs the same name. (If you have a better way, please let me know!) |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 341 | static unsigned BBNumber = 0; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 342 | |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 343 | O << "\n\n"; |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 344 | // What's my mangled name? |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 345 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 346 | |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 347 | // Print out constants referenced by the function |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 348 | printConstantPool(MF.getConstantPool()); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 349 | |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 350 | // Print out labels for the function. |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 351 | O << "\t.text\n"; |
| 352 | O << "\t.align 16\n"; |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 353 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 354 | if (!EmitCygwin) |
| 355 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 356 | O << CurrentFnName << ":\n"; |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 357 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 358 | // Number each basic block so that we can consistently refer to them |
| 359 | // in PC-relative references. |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 360 | NumberForBB.clear(); |
| 361 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 362 | I != E; ++I) { |
| 363 | NumberForBB[I->getBasicBlock()] = BBNumber++; |
| 364 | } |
| 365 | |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 366 | // Print out code for the function. |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 367 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 368 | I != E; ++I) { |
| 369 | // Print a label for the basic block. |
Brian Gaeke | 002a50a | 2003-07-31 17:38:52 +0000 | [diff] [blame] | 370 | O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# " |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 371 | << I->getBasicBlock()->getName() << "\n"; |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 372 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 373 | II != E; ++II) { |
| 374 | // Print the assembly for the instruction. |
| 375 | O << "\t"; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 376 | printMachineInstruction(*II); |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 378 | } |
Brian Gaeke | 6559bb9 | 2002-11-14 22:32:30 +0000 | [diff] [blame] | 379 | |
| 380 | // We didn't modify anything. |
Chris Lattner | b4f68ed | 2002-10-29 22:37:54 +0000 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 384 | static bool isScale(const MachineOperand &MO) { |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 385 | return MO.isImmediate() && |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 386 | (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 || |
| 387 | MO.getImmedValue() == 4 || MO.getImmedValue() == 8); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | static bool isMem(const MachineInstr *MI, unsigned Op) { |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 391 | if (MI->getOperand(Op).isFrameIndex()) return true; |
| 392 | if (MI->getOperand(Op).isConstantPoolIndex()) return true; |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 393 | return Op+4 <= MI->getNumOperands() && |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 394 | MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) && |
| 395 | MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate(); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 398 | |
| 399 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 400 | void Printer::printOp(const MachineOperand &MO, |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 401 | bool elideOffsetKeyword /* = false */) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 402 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 403 | switch (MO.getType()) { |
| 404 | case MachineOperand::MO_VirtualRegister: |
Chris Lattner | ac573f6 | 2002-12-04 17:32:52 +0000 | [diff] [blame] | 405 | if (Value *V = MO.getVRegValueOrNull()) { |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 406 | O << "<" << V->getName() << ">"; |
| 407 | return; |
| 408 | } |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 409 | // FALLTHROUGH |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 410 | case MachineOperand::MO_MachineRegister: |
Brian Gaeke | 9d99b43 | 2003-08-13 18:15:15 +0000 | [diff] [blame] | 411 | if (MO.getReg() < MRegisterInfo::FirstVirtualRegister) |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 412 | // Bug Workaround: See note in Printer::doInitialization about %. |
Brian Gaeke | 9d99b43 | 2003-08-13 18:15:15 +0000 | [diff] [blame] | 413 | O << "%" << RI.get(MO.getReg()).Name; |
| 414 | else |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 415 | O << "%reg" << MO.getReg(); |
| 416 | return; |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 417 | |
| 418 | case MachineOperand::MO_SignExtendedImmed: |
| 419 | case MachineOperand::MO_UnextendedImmed: |
| 420 | O << (int)MO.getImmedValue(); |
| 421 | return; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 422 | case MachineOperand::MO_PCRelativeDisp: { |
| 423 | ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue()); |
| 424 | assert (i != NumberForBB.end() |
| 425 | && "Could not find a BB in the NumberForBB map!"); |
| 426 | O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName(); |
Chris Lattner | f8bafe8 | 2002-12-01 23:25:59 +0000 | [diff] [blame] | 427 | return; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 428 | } |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 429 | case MachineOperand::MO_GlobalAddress: |
Brian Gaeke | 002a50a | 2003-07-31 17:38:52 +0000 | [diff] [blame] | 430 | if (!elideOffsetKeyword) |
| 431 | O << "OFFSET "; |
| 432 | O << Mang->getValueName(MO.getGlobal()); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 433 | return; |
| 434 | case MachineOperand::MO_ExternalSymbol: |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 435 | O << MO.getSymbolName(); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 436 | return; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 437 | default: |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 438 | O << "<unknown operand type>"; return; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Chris Lattner | 3501fea | 2003-01-14 22:00:31 +0000 | [diff] [blame] | 442 | static const std::string sizePtr(const TargetInstrDescriptor &Desc) { |
Chris Lattner | a0f38c8 | 2002-12-13 03:51:55 +0000 | [diff] [blame] | 443 | switch (Desc.TSFlags & X86II::ArgMask) { |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 444 | default: assert(0 && "Unknown arg size!"); |
| 445 | case X86II::Arg8: return "BYTE PTR"; |
| 446 | case X86II::Arg16: return "WORD PTR"; |
| 447 | case X86II::Arg32: return "DWORD PTR"; |
| 448 | case X86II::Arg64: return "QWORD PTR"; |
| 449 | case X86II::ArgF32: return "DWORD PTR"; |
| 450 | case X86II::ArgF64: return "QWORD PTR"; |
| 451 | case X86II::ArgF80: return "XWORD PTR"; |
Brian Gaeke | 86764d7 | 2002-12-05 08:30:40 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 455 | void Printer::printMemReference(const MachineInstr *MI, unsigned Op) { |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 456 | assert(isMem(MI, Op) && "Invalid memory reference!"); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 457 | |
| 458 | if (MI->getOperand(Op).isFrameIndex()) { |
| 459 | O << "[frame slot #" << MI->getOperand(Op).getFrameIndex(); |
| 460 | if (MI->getOperand(Op+3).getImmedValue()) |
| 461 | O << " + " << MI->getOperand(Op+3).getImmedValue(); |
| 462 | O << "]"; |
| 463 | return; |
| 464 | } else if (MI->getOperand(Op).isConstantPoolIndex()) { |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 465 | O << "[.CPI" << CurrentFnName << "_" |
Brian Gaeke | 5e00157 | 2003-06-26 18:02:30 +0000 | [diff] [blame] | 466 | << MI->getOperand(Op).getConstantPoolIndex(); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 467 | if (MI->getOperand(Op+3).getImmedValue()) |
| 468 | O << " + " << MI->getOperand(Op+3).getImmedValue(); |
| 469 | O << "]"; |
| 470 | return; |
| 471 | } |
| 472 | |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 473 | const MachineOperand &BaseReg = MI->getOperand(Op); |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 474 | int ScaleVal = MI->getOperand(Op+1).getImmedValue(); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 475 | const MachineOperand &IndexReg = MI->getOperand(Op+2); |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 476 | int DispVal = MI->getOperand(Op+3).getImmedValue(); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 477 | |
| 478 | O << "["; |
| 479 | bool NeedPlus = false; |
| 480 | if (BaseReg.getReg()) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 481 | printOp(BaseReg); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 482 | NeedPlus = true; |
| 483 | } |
| 484 | |
| 485 | if (IndexReg.getReg()) { |
| 486 | if (NeedPlus) O << " + "; |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 487 | if (ScaleVal != 1) |
| 488 | O << ScaleVal << "*"; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 489 | printOp(IndexReg); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 490 | NeedPlus = true; |
| 491 | } |
| 492 | |
Chris Lattner | 0285a33 | 2002-12-28 20:25:38 +0000 | [diff] [blame] | 493 | if (DispVal) { |
| 494 | if (NeedPlus) |
| 495 | if (DispVal > 0) |
| 496 | O << " + "; |
| 497 | else { |
| 498 | O << " - "; |
| 499 | DispVal = -DispVal; |
| 500 | } |
| 501 | O << DispVal; |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 502 | } |
| 503 | O << "]"; |
| 504 | } |
| 505 | |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 506 | /// checkImplUses - Emit the implicit-use registers for the |
| 507 | /// instruction described by DESC, if its PrintImplUses flag is set. |
| 508 | /// |
| 509 | void Printer::checkImplUses (const TargetInstrDescriptor &Desc) { |
| 510 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 511 | if (Desc.TSFlags & X86II::PrintImplUses) { |
| 512 | for (const unsigned *p = Desc.ImplicitUses; *p; ++p) { |
| 513 | // Bug Workaround: See note in Printer::doInitialization about %. |
Chris Lattner | 67488a9 | 2003-08-11 20:04:57 +0000 | [diff] [blame] | 514 | O << ", %" << RI.get(*p).Name; |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
Brian Gaeke | 92bdfe6 | 2003-07-23 18:37:06 +0000 | [diff] [blame] | 519 | /// printMachineInstruction -- Print out a single X86 LLVM instruction |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 520 | /// MI in Intel syntax to the current output stream. |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 521 | /// |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 522 | void Printer::printMachineInstruction(const MachineInstr *MI) { |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 523 | unsigned Opcode = MI->getOpcode(); |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 524 | const TargetInstrInfo &TII = TM.getInstrInfo(); |
| 525 | const TargetInstrDescriptor &Desc = TII.get(Opcode); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 526 | |
Brian Gaeke | 2c9b913 | 2003-10-06 15:41:21 +0000 | [diff] [blame] | 527 | ++EmittedInsts; |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 528 | switch (Desc.TSFlags & X86II::FormMask) { |
| 529 | case X86II::Pseudo: |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 530 | // Print pseudo-instructions as comments; either they should have been |
| 531 | // turned into real instructions by now, or they don't need to be |
| 532 | // seen by the assembler (e.g., IMPLICIT_USEs.) |
| 533 | O << "# "; |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 534 | if (Opcode == X86::PHI) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 535 | printOp(MI->getOperand(0)); |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 536 | O << " = phi "; |
| 537 | for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) { |
| 538 | if (i != 1) O << ", "; |
| 539 | O << "["; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 540 | printOp(MI->getOperand(i)); |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 541 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 542 | printOp(MI->getOperand(i+1)); |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 543 | O << "]"; |
| 544 | } |
| 545 | } else { |
| 546 | unsigned i = 0; |
Vikram S. Adve | 49cab03 | 2003-05-27 00:03:17 +0000 | [diff] [blame] | 547 | if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() || |
| 548 | MI->getOperand(0).opIsDefAndUse())) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 549 | printOp(MI->getOperand(0)); |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 550 | O << " = "; |
| 551 | ++i; |
| 552 | } |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 553 | O << TII.getName(MI->getOpcode()); |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 554 | |
| 555 | for (unsigned e = MI->getNumOperands(); i != e; ++i) { |
| 556 | O << " "; |
Vikram S. Adve | 49cab03 | 2003-05-27 00:03:17 +0000 | [diff] [blame] | 557 | if (MI->getOperand(i).opIsDefOnly() || |
| 558 | MI->getOperand(i).opIsDefAndUse()) O << "*"; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 559 | printOp(MI->getOperand(i)); |
Vikram S. Adve | 49cab03 | 2003-05-27 00:03:17 +0000 | [diff] [blame] | 560 | if (MI->getOperand(i).opIsDefOnly() || |
| 561 | MI->getOperand(i).opIsDefAndUse()) O << "*"; |
Chris Lattner | eca1f63 | 2002-12-25 05:09:01 +0000 | [diff] [blame] | 562 | } |
Chris Lattner | 3faae2d | 2002-12-13 09:59:26 +0000 | [diff] [blame] | 563 | } |
| 564 | O << "\n"; |
| 565 | return; |
Chris Lattner | 3faae2d | 2002-12-13 09:59:26 +0000 | [diff] [blame] | 566 | |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 567 | case X86II::RawFrm: |
Chris Lattner | f8bafe8 | 2002-12-01 23:25:59 +0000 | [diff] [blame] | 568 | // The accepted forms of Raw instructions are: |
| 569 | // 1. nop - No operand required |
| 570 | // 2. jmp foo - PC relative displacement operand |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 571 | // 3. call bar - GlobalAddress Operand or External Symbol Operand |
Chris Lattner | f8bafe8 | 2002-12-01 23:25:59 +0000 | [diff] [blame] | 572 | // |
| 573 | assert(MI->getNumOperands() == 0 || |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 574 | (MI->getNumOperands() == 1 && |
| 575 | (MI->getOperand(0).isPCRelativeDisp() || |
| 576 | MI->getOperand(0).isGlobalAddress() || |
| 577 | MI->getOperand(0).isExternalSymbol())) && |
Chris Lattner | f8bafe8 | 2002-12-01 23:25:59 +0000 | [diff] [blame] | 578 | "Illegal raw instruction!"); |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 579 | O << TII.getName(MI->getOpcode()) << " "; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 580 | |
Chris Lattner | f8bafe8 | 2002-12-01 23:25:59 +0000 | [diff] [blame] | 581 | if (MI->getNumOperands() == 1) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 582 | printOp(MI->getOperand(0), true); // Don't print "OFFSET"... |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 583 | } |
| 584 | O << "\n"; |
| 585 | return; |
| 586 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 587 | case X86II::AddRegFrm: { |
| 588 | // There are currently two forms of acceptable AddRegFrm instructions. |
| 589 | // Either the instruction JUST takes a single register (like inc, dec, etc), |
| 590 | // or it takes a register and an immediate of the same size as the register |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 591 | // (move immediate f.e.). Note that this immediate value might be stored as |
| 592 | // an LLVM value, to represent, for example, loading the address of a global |
Chris Lattner | facc9fb | 2002-12-23 23:46:00 +0000 | [diff] [blame] | 593 | // into a register. The initial register might be duplicated if this is a |
| 594 | // M_2_ADDR_REG instruction |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 595 | // |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 596 | assert(MI->getOperand(0).isRegister() && |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 597 | (MI->getNumOperands() == 1 || |
Chris Lattner | dbf30f7 | 2002-12-04 06:45:19 +0000 | [diff] [blame] | 598 | (MI->getNumOperands() == 2 && |
Chris Lattner | 6d66944 | 2002-12-04 17:28:40 +0000 | [diff] [blame] | 599 | (MI->getOperand(1).getVRegValueOrNull() || |
Chris Lattner | facc9fb | 2002-12-23 23:46:00 +0000 | [diff] [blame] | 600 | MI->getOperand(1).isImmediate() || |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 601 | MI->getOperand(1).isRegister() || |
| 602 | MI->getOperand(1).isGlobalAddress() || |
| 603 | MI->getOperand(1).isExternalSymbol()))) && |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 604 | "Illegal form for AddRegFrm instruction!"); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 606 | unsigned Reg = MI->getOperand(0).getReg(); |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 607 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 608 | O << TII.getName(MI->getOpCode()) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 609 | printOp(MI->getOperand(0)); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 610 | if (MI->getNumOperands() == 2 && |
| 611 | (!MI->getOperand(1).isRegister() || |
| 612 | MI->getOperand(1).getVRegValueOrNull() || |
| 613 | MI->getOperand(1).isGlobalAddress() || |
| 614 | MI->getOperand(1).isExternalSymbol())) { |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 615 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 616 | printOp(MI->getOperand(1)); |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 617 | } |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 618 | checkImplUses(Desc); |
Chris Lattner | 77875d8 | 2002-11-21 02:00:20 +0000 | [diff] [blame] | 619 | O << "\n"; |
| 620 | return; |
| 621 | } |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 622 | case X86II::MRMDestReg: { |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 623 | // There are two acceptable forms of MRMDestReg instructions, those with 2, |
| 624 | // 3 and 4 operands: |
| 625 | // |
| 626 | // 2 Operands: this is for things like mov that do not read a second input |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 627 | // |
| 628 | // 3 Operands: in this form, the first two registers (the destination, and |
| 629 | // the first operand) should be the same, post register allocation. The 3rd |
| 630 | // operand is an additional input. This should be for things like add |
| 631 | // instructions. |
| 632 | // |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 633 | // 4 Operands: This form is for instructions which are 3 operands forms, but |
| 634 | // have a constant argument as well. |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 635 | // |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 636 | bool isTwoAddr = TII.isTwoAddrInstr(Opcode); |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 637 | assert(MI->getOperand(0).isRegister() && |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 638 | (MI->getNumOperands() == 2 || |
| 639 | (isTwoAddr && MI->getOperand(1).isRegister() && |
| 640 | MI->getOperand(0).getReg() == MI->getOperand(1).getReg() && |
| 641 | (MI->getNumOperands() == 3 || |
| 642 | (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate())))) |
Misha Brukman | e1f0d81 | 2002-11-20 18:56:41 +0000 | [diff] [blame] | 643 | && "Bad format for MRMDestReg!"); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 644 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 645 | O << TII.getName(MI->getOpCode()) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 646 | printOp(MI->getOperand(0)); |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 647 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 648 | printOp(MI->getOperand(1+isTwoAddr)); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 649 | if (MI->getNumOperands() == 4) { |
| 650 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 651 | printOp(MI->getOperand(3)); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 652 | } |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 653 | O << "\n"; |
| 654 | return; |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 655 | } |
Chris Lattner | 1804233 | 2002-11-21 21:03:39 +0000 | [diff] [blame] | 656 | |
| 657 | case X86II::MRMDestMem: { |
| 658 | // These instructions are the same as MRMDestReg, but instead of having a |
| 659 | // register reference for the mod/rm field, it's a memory reference. |
| 660 | // |
| 661 | assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 && |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 662 | MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!"); |
Chris Lattner | 1804233 | 2002-11-21 21:03:39 +0000 | [diff] [blame] | 663 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 664 | O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 665 | printMemReference(MI, 0); |
Chris Lattner | 1804233 | 2002-11-21 21:03:39 +0000 | [diff] [blame] | 666 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 667 | printOp(MI->getOperand(4)); |
Chris Lattner | 1804233 | 2002-11-21 21:03:39 +0000 | [diff] [blame] | 668 | O << "\n"; |
| 669 | return; |
| 670 | } |
| 671 | |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 672 | case X86II::MRMSrcReg: { |
Misha Brukman | 44ffd5a | 2003-10-20 04:03:10 +0000 | [diff] [blame] | 673 | // There are three forms that are acceptable for MRMSrcReg instructions, |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 674 | // those with 3 and 2 operands: |
| 675 | // |
| 676 | // 3 Operands: in this form, the last register (the second input) is the |
| 677 | // ModR/M input. The first two operands should be the same, post register |
| 678 | // allocation. This is for things like: add r32, r/m32 |
| 679 | // |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 680 | // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for |
| 681 | // instructions like the IMULri instructions. |
| 682 | // |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 683 | // 2 Operands: this is for things like mov that do not read a second input |
| 684 | // |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 685 | assert(MI->getOperand(0).isRegister() && |
| 686 | MI->getOperand(1).isRegister() && |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 687 | (MI->getNumOperands() == 2 || |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 688 | (MI->getNumOperands() == 3 && |
| 689 | (MI->getOperand(2).isRegister() || |
| 690 | MI->getOperand(2).isImmediate()))) |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 691 | && "Bad format for MRMSrcReg!"); |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 692 | if (MI->getNumOperands() == 3 && |
| 693 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 694 | O << "**"; |
| 695 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 696 | O << TII.getName(MI->getOpCode()) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 697 | printOp(MI->getOperand(0)); |
Chris Lattner | c01d123 | 2003-10-20 03:42:58 +0000 | [diff] [blame] | 698 | |
| 699 | // If this is IMULri* instructions, print the non-two-address operand. |
| 700 | if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) { |
| 701 | O << ", "; |
| 702 | printOp(MI->getOperand(1)); |
| 703 | } |
| 704 | |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 705 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 706 | printOp(MI->getOperand(MI->getNumOperands()-1)); |
Chris Lattner | 644e1ab | 2002-11-21 00:30:01 +0000 | [diff] [blame] | 707 | O << "\n"; |
| 708 | return; |
Chris Lattner | 233ad71 | 2002-11-21 01:33:44 +0000 | [diff] [blame] | 709 | } |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 711 | case X86II::MRMSrcMem: { |
| 712 | // These instructions are the same as MRMSrcReg, but instead of having a |
| 713 | // register reference for the mod/rm field, it's a memory reference. |
Chris Lattner | 1804233 | 2002-11-21 21:03:39 +0000 | [diff] [blame] | 714 | // |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 715 | assert(MI->getOperand(0).isRegister() && |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 716 | (MI->getNumOperands() == 1+4 && isMem(MI, 1)) || |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 717 | (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() && |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 718 | isMem(MI, 2)) |
| 719 | && "Bad format for MRMDestReg!"); |
| 720 | if (MI->getNumOperands() == 2+4 && |
| 721 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 722 | O << "**"; |
| 723 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 724 | O << TII.getName(MI->getOpCode()) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 725 | printOp(MI->getOperand(0)); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 726 | O << ", " << sizePtr(Desc) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 727 | printMemReference(MI, MI->getNumOperands()-4); |
Chris Lattner | 3d3067b | 2002-11-21 20:44:15 +0000 | [diff] [blame] | 728 | O << "\n"; |
| 729 | return; |
| 730 | } |
| 731 | |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 732 | case X86II::MRMS0r: case X86II::MRMS1r: |
| 733 | case X86II::MRMS2r: case X86II::MRMS3r: |
| 734 | case X86II::MRMS4r: case X86II::MRMS5r: |
| 735 | case X86II::MRMS6r: case X86II::MRMS7r: { |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 736 | // In this form, the following are valid formats: |
| 737 | // 1. sete r |
Chris Lattner | 1d53ce4 | 2002-11-21 23:30:00 +0000 | [diff] [blame] | 738 | // 2. cmp reg, immediate |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 739 | // 2. shl rdest, rinput <implicit CL or 1> |
| 740 | // 3. sbb rdest, rinput, immediate [rdest = rinput] |
| 741 | // |
| 742 | assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 && |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 743 | MI->getOperand(0).isRegister() && "Bad MRMSxR format!"); |
Chris Lattner | 1d53ce4 | 2002-11-21 23:30:00 +0000 | [diff] [blame] | 744 | assert((MI->getNumOperands() != 2 || |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 745 | MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&& |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 746 | "Bad MRMSxR format!"); |
Chris Lattner | 1d53ce4 | 2002-11-21 23:30:00 +0000 | [diff] [blame] | 747 | assert((MI->getNumOperands() < 3 || |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 748 | (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) && |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 749 | "Bad MRMSxR format!"); |
| 750 | |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 751 | if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() && |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 752 | MI->getOperand(0).getReg() != MI->getOperand(1).getReg()) |
| 753 | O << "**"; |
| 754 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 755 | O << TII.getName(MI->getOpCode()) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 756 | printOp(MI->getOperand(0)); |
Chris Lattner | d909683 | 2002-12-15 08:01:39 +0000 | [diff] [blame] | 757 | if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) { |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 758 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 759 | printOp(MI->getOperand(MI->getNumOperands()-1)); |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 760 | } |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 761 | checkImplUses(Desc); |
Chris Lattner | 675dd2c | 2002-11-21 17:09:01 +0000 | [diff] [blame] | 762 | O << "\n"; |
| 763 | |
| 764 | return; |
| 765 | } |
| 766 | |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 767 | case X86II::MRMS0m: case X86II::MRMS1m: |
| 768 | case X86II::MRMS2m: case X86II::MRMS3m: |
| 769 | case X86II::MRMS4m: case X86II::MRMS5m: |
| 770 | case X86II::MRMS6m: case X86II::MRMS7m: { |
| 771 | // In this form, the following are valid formats: |
| 772 | // 1. sete [m] |
| 773 | // 2. cmp [m], immediate |
| 774 | // 2. shl [m], rinput <implicit CL or 1> |
| 775 | // 3. sbb [m], immediate |
| 776 | // |
| 777 | assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 && |
| 778 | isMem(MI, 0) && "Bad MRMSxM format!"); |
| 779 | assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) && |
| 780 | "Bad MRMSxM format!"); |
Brian Gaeke | 1aa476e | 2003-07-11 18:18:35 +0000 | [diff] [blame] | 781 | // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" |
| 782 | // is misassembled by gas in intel_syntax mode as its 32-bit |
| 783 | // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw |
| 784 | // opcode bytes instead of the instruction. |
Brian Gaeke | b44210d | 2003-07-07 18:34:20 +0000 | [diff] [blame] | 785 | if (MI->getOpCode() == X86::FSTPr80) { |
| 786 | if ((MI->getOperand(0).getReg() == X86::ESP) |
| 787 | && (MI->getOperand(1).getImmedValue() == 1)) { |
| 788 | int DispVal = MI->getOperand(3).getImmedValue(); |
| 789 | if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp. |
| 790 | unsigned int val = (unsigned int) DispVal; |
| 791 | O << ".byte 0xdb, 0xbc, 0x24\n\t"; |
| 792 | O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# "; |
| 793 | } else { // 1 byte disp. |
| 794 | unsigned char val = (unsigned char) DispVal; |
| 795 | O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val |
| 796 | << std::dec << "\t# "; |
| 797 | } |
| 798 | } |
Brian Gaeke | 1aa476e | 2003-07-11 18:18:35 +0000 | [diff] [blame] | 799 | } |
| 800 | // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is |
| 801 | // misassembled by gas in intel_syntax mode as its 32-bit |
| 802 | // equivalent "fld DWORD PTR [...]". Workaround: Output the raw |
| 803 | // opcode bytes instead of the instruction. |
| 804 | if (MI->getOpCode() == X86::FLDr80) { |
Brian Gaeke | b44210d | 2003-07-07 18:34:20 +0000 | [diff] [blame] | 805 | if ((MI->getOperand(0).getReg() == X86::ESP) |
| 806 | && (MI->getOperand(1).getImmedValue() == 1)) { |
| 807 | int DispVal = MI->getOperand(3).getImmedValue(); |
| 808 | if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp. |
| 809 | unsigned int val = (unsigned int) DispVal; |
| 810 | O << ".byte 0xdb, 0xac, 0x24\n\t"; |
| 811 | O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# "; |
| 812 | } else { // 1 byte disp. |
| 813 | unsigned char val = (unsigned char) DispVal; |
| 814 | O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val |
| 815 | << std::dec << "\t# "; |
| 816 | } |
| 817 | } |
| 818 | } |
Brian Gaeke | 1aa476e | 2003-07-11 18:18:35 +0000 | [diff] [blame] | 819 | // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an |
| 820 | // invalid opcode, saying "64 bit operations are only supported in |
| 821 | // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR |
| 822 | // [...]", which is wrong. Workaround: Output the raw opcode bytes |
| 823 | // instead of the instruction. |
| 824 | if (MI->getOpCode() == X86::FILDr64) { |
| 825 | if ((MI->getOperand(0).getReg() == X86::ESP) |
| 826 | && (MI->getOperand(1).getImmedValue() == 1)) { |
| 827 | int DispVal = MI->getOperand(3).getImmedValue(); |
| 828 | if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp. |
| 829 | unsigned int val = (unsigned int) DispVal; |
| 830 | O << ".byte 0xdf, 0xac, 0x24\n\t"; |
| 831 | O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# "; |
| 832 | } else { // 1 byte disp. |
| 833 | unsigned char val = (unsigned char) DispVal; |
| 834 | O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val |
| 835 | << std::dec << "\t# "; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as |
| 840 | // an invalid opcode, saying "64 bit operations are only |
| 841 | // supported in 64 bit modes." libopcodes disassembles it as |
| 842 | // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output |
| 843 | // "fistpll DWORD PTR " instead, which is what libopcodes is |
| 844 | // expecting to see. |
| 845 | if (MI->getOpCode() == X86::FISTPr64) { |
| 846 | O << "fistpll DWORD PTR "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 847 | printMemReference(MI, 0); |
Brian Gaeke | 1aa476e | 2003-07-11 18:18:35 +0000 | [diff] [blame] | 848 | if (MI->getNumOperands() == 5) { |
| 849 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 850 | printOp(MI->getOperand(4)); |
Brian Gaeke | 1aa476e | 2003-07-11 18:18:35 +0000 | [diff] [blame] | 851 | } |
| 852 | O << "\t# "; |
| 853 | } |
| 854 | |
Brian Gaeke | d7908f6 | 2003-06-27 00:00:48 +0000 | [diff] [blame] | 855 | O << TII.getName(MI->getOpCode()) << " "; |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 856 | O << sizePtr(Desc) << " "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 857 | printMemReference(MI, 0); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 858 | if (MI->getNumOperands() == 5) { |
| 859 | O << ", "; |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 860 | printOp(MI->getOperand(4)); |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 861 | } |
| 862 | O << "\n"; |
| 863 | return; |
| 864 | } |
| 865 | |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 866 | default: |
Chris Lattner | b708944 | 2003-01-13 00:35:03 +0000 | [diff] [blame] | 867 | O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break; |
Chris Lattner | f9f6088 | 2002-11-18 06:56:51 +0000 | [diff] [blame] | 868 | } |
Chris Lattner | 7261408 | 2002-10-25 22:55:53 +0000 | [diff] [blame] | 869 | } |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 870 | |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 871 | bool Printer::doInitialization(Module &M) { |
| 872 | // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly. |
Brian Gaeke | 2a09877 | 2003-08-11 19:05:46 +0000 | [diff] [blame] | 873 | // |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 874 | // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an |
| 875 | // instruction as a reference to the register named sp, and if you try to |
| 876 | // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased |
| 877 | // before being looked up in the symbol table. This creates spurious |
| 878 | // `undefined symbol' errors when linking. Workaround: Do not use `noprefix' |
| 879 | // mode, and decorate all register names with percent signs. |
Chris Lattner | 67488a9 | 2003-08-11 20:04:57 +0000 | [diff] [blame] | 880 | O << "\t.intel_syntax\n"; |
Chris Lattner | 93c1afa | 2003-08-11 19:35:26 +0000 | [diff] [blame] | 881 | Mang = new Mangler(M, EmitCygwin); |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 882 | return false; // success |
| 883 | } |
| 884 | |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 885 | // SwitchSection - Switch to the specified section of the executable if we are |
| 886 | // not already in it! |
| 887 | // |
| 888 | static void SwitchSection(std::ostream &OS, std::string &CurSection, |
| 889 | const char *NewSection) { |
| 890 | if (CurSection != NewSection) { |
| 891 | CurSection = NewSection; |
| 892 | if (!CurSection.empty()) |
| 893 | OS << "\t" << NewSection << "\n"; |
| 894 | } |
Brian Gaeke | 0517c5a | 2003-07-11 21:57:01 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 897 | bool Printer::doFinalization(Module &M) { |
Brian Gaeke | de420ae | 2003-07-23 20:25:08 +0000 | [diff] [blame] | 898 | const TargetData &TD = TM.getTargetData(); |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 899 | std::string CurSection; |
| 900 | |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 901 | // Print out module-level global variables here. |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 902 | for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) |
| 903 | if (I->hasInitializer()) { // External global require no code |
| 904 | O << "\n\n"; |
| 905 | std::string name = Mang->getValueName(I); |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 906 | Constant *C = I->getInitializer(); |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 907 | unsigned Size = TD.getTypeSize(C->getType()); |
| 908 | unsigned Align = TD.getTypeAlignment(C->getType()); |
| 909 | |
| 910 | if (C->isNullValue() && |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 911 | (I->hasLinkOnceLinkage() || I->hasInternalLinkage() || |
| 912 | I->hasWeakLinkage() /* FIXME: Verify correct */)) { |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 913 | SwitchSection(O, CurSection, ".data"); |
| 914 | if (I->hasInternalLinkage()) |
| 915 | O << "\t.local " << name << "\n"; |
| 916 | |
| 917 | O << "\t.comm " << name << "," << TD.getTypeSize(C->getType()) |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 918 | << "," << (unsigned)TD.getTypeAlignment(C->getType()); |
| 919 | O << "\t\t# "; |
| 920 | WriteAsOperand(O, I, true, true, &M); |
| 921 | O << "\n"; |
Brian Gaeke | 0517c5a | 2003-07-11 21:57:01 +0000 | [diff] [blame] | 922 | } else { |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 923 | switch (I->getLinkage()) { |
| 924 | case GlobalValue::LinkOnceLinkage: |
Chris Lattner | 72ac148d | 2003-10-16 18:29:00 +0000 | [diff] [blame] | 925 | case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak. |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 926 | // Nonnull linkonce -> weak |
| 927 | O << "\t.weak " << name << "\n"; |
| 928 | SwitchSection(O, CurSection, ""); |
| 929 | O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n"; |
| 930 | break; |
| 931 | |
| 932 | case GlobalValue::AppendingLinkage: |
| 933 | // FIXME: appending linkage variables should go into a section of |
| 934 | // their name or something. For now, just emit them as external. |
| 935 | case GlobalValue::ExternalLinkage: |
| 936 | // If external or appending, declare as a global symbol |
| 937 | O << "\t.globl " << name << "\n"; |
| 938 | // FALL THROUGH |
| 939 | case GlobalValue::InternalLinkage: |
| 940 | if (C->isNullValue()) |
| 941 | SwitchSection(O, CurSection, ".bss"); |
| 942 | else |
| 943 | SwitchSection(O, CurSection, ".data"); |
| 944 | break; |
| 945 | } |
| 946 | |
| 947 | O << "\t.align " << Align << "\n"; |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 948 | O << "\t.type " << name << ",@object\n"; |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 949 | O << "\t.size " << name << "," << Size << "\n"; |
Chris Lattner | e012132 | 2003-08-03 23:37:09 +0000 | [diff] [blame] | 950 | O << name << ":\t\t\t\t# "; |
| 951 | WriteAsOperand(O, I, true, true, &M); |
| 952 | O << " = "; |
| 953 | WriteAsOperand(O, C, false, false, &M); |
| 954 | O << "\n"; |
Chris Lattner | ac662d1 | 2003-11-03 20:19:49 +0000 | [diff] [blame^] | 955 | emitGlobalConstant(C); |
Brian Gaeke | 0517c5a | 2003-07-11 21:57:01 +0000 | [diff] [blame] | 956 | } |
Brian Gaeke | 01d79ff | 2003-06-25 18:01:07 +0000 | [diff] [blame] | 957 | } |
Chris Lattner | ad20071 | 2003-09-09 16:23:36 +0000 | [diff] [blame] | 958 | |
Brian Gaeke | d9fb37a | 2003-07-24 20:20:44 +0000 | [diff] [blame] | 959 | delete Mang; |
Brian Gaeke | 9e474c4 | 2003-06-19 19:32:32 +0000 | [diff] [blame] | 960 | return false; // success |
| 961 | } |