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