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