blob: 75af19debcab4d002fba07c8aaa1d1ef3c041b77 [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
Chris Lattner72614082002-10-25 22:55:53 +00002//
Brian Gaeke92bdfe62003-07-23 18:37:06 +00003// 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
6// by `llc' and `lli -printmachineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "X86InstrInfo.h"
Chris Lattnere0121322003-08-03 23:37:09 +000012#include "llvm/Module.h"
13#include "llvm/Type.h"
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000019#include "llvm/CodeGen/MachineInstr.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000020#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000021#include "llvm/Support/Mangler.h"
Chris Lattnere0121322003-08-03 23:37:09 +000022#include "Support/StringExtras.h"
Chris Lattner72614082002-10-25 22:55:53 +000023
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000024namespace {
Chris Lattner0285a332002-12-28 20:25:38 +000025 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000026 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000027 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000028 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000029
30 /// Target machine description which we query for reg. names, data
31 /// layout, etc.
32 ///
33 TargetMachine &TM;
34
Brian Gaeked9fb37a2003-07-24 20:20:44 +000035 /// Name-mangler for global names.
36 ///
37 Mangler *Mang;
38
Brian Gaekede420ae2003-07-23 20:25:08 +000039 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000040
41 /// We name each basic block in a Function with a unique number, so
42 /// that we can consistently refer to them later. This is cleared
43 /// at the beginning of each call to runOnMachineFunction().
44 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000045 typedef std::map<const Value *, unsigned> ValueMapTy;
46 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000047
48 /// Cache of mangled name for current function. This is
49 /// recalculated at the beginning of each call to
50 /// runOnMachineFunction().
51 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000052 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000053
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000054 virtual const char *getPassName() const {
55 return "X86 Assembly Printer";
56 }
57
Brian Gaeked9fb37a2003-07-24 20:20:44 +000058 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000059 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000060 bool elideOffsetKeyword = false);
61 void printMemReference(const MachineInstr *MI, unsigned Op);
62 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000063 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000064 std::string ConstantExprToString(const ConstantExpr* CE);
65 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000066 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000068 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
69 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000070 };
Brian Gaeked7908f62003-06-27 00:00:48 +000071} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000072
Brian Gaeke92bdfe62003-07-23 18:37:06 +000073/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000074/// assembly code for a MachineFunction to the given output stream,
75/// using the given target machine description. This should work
76/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000077///
Brian Gaekede420ae2003-07-23 20:25:08 +000078Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
79 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000080}
81
Brian Gaeke92bdfe62003-07-23 18:37:06 +000082/// valToExprString - Helper function for ConstantExprToString().
83/// Appends result to argument string S.
84///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000085std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000086 std::string S;
87 bool failed = false;
88 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
89 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
90 S += std::string(CB == ConstantBool::True ? "1" : "0");
91 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
92 S += itostr(CI->getValue());
93 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
94 S += utostr(CI->getValue());
95 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
96 S += ftostr(CFP->getValue());
97 else if (isa<ConstantPointerNull>(CV))
98 S += "0";
99 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
100 S += valToExprString(CPR->getValue());
101 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
102 S += ConstantExprToString(CE);
103 else
104 failed = true;
105 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000106 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000107 }
108 else
109 failed = true;
110
111 if (failed) {
112 assert(0 && "Cannot convert value to string");
113 S += "<illegal-value>";
114 }
115 return S;
116}
117
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000118/// ConstantExprToString - Convert a ConstantExpr to an asm expression
119/// and return this as a string.
120///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000121std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000122 std::string S;
Brian Gaekede420ae2003-07-23 20:25:08 +0000123 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000124 switch(CE->getOpcode()) {
125 case Instruction::GetElementPtr:
126 { // generate a symbolic expression for the byte address
127 const Value* ptrVal = CE->getOperand(0);
128 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
129 S += "(" + valToExprString(ptrVal) + ") + ("
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000130 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 break;
132 }
133
134 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000135 // Support only non-converting or widening casts for now, that is,
136 // ones that do not involve a change in value. This assertion is
137 // not a complete check.
138 {
139 Constant *Op = CE->getOperand(0);
140 const Type *OpTy = Op->getType(), *Ty = CE->getType();
141 assert(((isa<PointerType>(OpTy)
142 && (Ty == Type::LongTy || Ty == Type::ULongTy))
143 || (isa<PointerType>(Ty)
144 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
145 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
146 && (OpTy-> isLosslesslyConvertibleTo(Ty))))
147 && "FIXME: Don't yet support this kind of constant cast expr");
148 S += "(" + valToExprString(Op) + ")";
149 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150 break;
151
152 case Instruction::Add:
153 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
154 + valToExprString(CE->getOperand(1)) + ")";
155 break;
156
157 default:
158 assert(0 && "Unsupported operator in ConstantExprToString()");
159 break;
160 }
161
162 return S;
163}
164
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000165/// printSingleConstantValue - Print a single constant value.
166///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000168Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169{
170 assert(CV->getType() != Type::VoidTy &&
171 CV->getType() != Type::TypeTy &&
172 CV->getType() != Type::LabelTy &&
173 "Unexpected type for Constant");
174
175 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
176 && "Aggregate types should be handled outside this function");
177
178 const Type *type = CV->getType();
179 O << "\t";
180 switch(type->getPrimitiveID())
181 {
182 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
183 O << ".byte";
184 break;
185 case Type::UShortTyID: case Type::ShortTyID:
186 O << ".word";
187 break;
188 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
189 O << ".long";
190 break;
191 case Type::ULongTyID: case Type::LongTyID:
192 O << ".quad";
193 break;
194 case Type::FloatTyID:
195 O << ".long";
196 break;
197 case Type::DoubleTyID:
198 O << ".quad";
199 break;
200 case Type::ArrayTyID:
201 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
202 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
203 O << ".string";
204 else
205 assert (0 && "Can't handle printing this type of array");
206 break;
207 default:
208 assert (0 && "Can't handle printing this type of thing");
209 break;
210 }
211 O << "\t";
212
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000213 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
214 {
215 // Constant expression built from operators, constants, and
216 // symbolic addrs
217 O << ConstantExprToString(CE) << "\n";
218 }
219 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000220 {
221 if (type->isFloatingPoint()) {
222 // FP Constants are printed as integer constants to avoid losing
223 // precision...
224 double Val = cast<ConstantFP>(CV)->getValue();
225 if (type == Type::FloatTy) {
226 float FVal = (float)Val;
227 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
228 O << *(unsigned int*)ProxyPtr;
229 } else if (type == Type::DoubleTy) {
230 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
231 O << *(uint64_t*)ProxyPtr;
232 } else {
233 assert(0 && "Unknown floating point type!");
234 }
235
236 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
237 } else {
238 WriteAsOperand(O, CV, false, false) << "\n";
239 }
240 }
241 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
242 {
243 // This is a constant address for a global variable or method.
244 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000245 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000246 }
247 else if (isa<ConstantPointerNull>(CV))
248 {
249 // Null pointer value
250 O << "0\n";
251 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000252 else
253 {
254 assert(0 && "Unknown elementary type for constant");
255 }
256}
257
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000258/// isStringCompatible - Can we treat the specified array as a string?
259/// Only if it is an array of ubytes or non-negative sbytes.
260///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000261static bool isStringCompatible(const ConstantArray *CVA) {
262 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
263 if (ETy == Type::UByteTy) return true;
264 if (ETy != Type::SByteTy) return false;
265
266 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
267 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
268 return false;
269
270 return true;
271}
272
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000273/// toOctal - Convert the low order bits of X into an octal digit.
274///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000275static inline char toOctal(int X) {
276 return (X&7)+'0';
277}
278
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000279/// getAsCString - Return the specified array as a C compatible
280/// string, only if the predicate isStringCompatible is true.
281///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000282static std::string getAsCString(const ConstantArray *CVA) {
283 assert(isStringCompatible(CVA) && "Array is not string compatible!");
284
285 std::string Result;
286 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
287 Result = "\"";
288 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000289 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000290
291 if (C == '"') {
292 Result += "\\\"";
293 } else if (C == '\\') {
294 Result += "\\\\";
295 } else if (isprint(C)) {
296 Result += C;
297 } else {
298 switch(C) {
299 case '\a': Result += "\\a"; break;
300 case '\b': Result += "\\b"; break;
301 case '\f': Result += "\\f"; break;
302 case '\n': Result += "\\n"; break;
303 case '\r': Result += "\\r"; break;
304 case '\t': Result += "\\t"; break;
305 case '\v': Result += "\\v"; break;
306 default:
307 Result += '\\';
308 Result += toOctal(C >> 6);
309 Result += toOctal(C >> 3);
310 Result += toOctal(C >> 0);
311 break;
312 }
313 }
314 }
315 Result += "\"";
316 return Result;
317}
318
319// Print a constant value or values (it may be an aggregate).
320// Uses printSingleConstantValue() to print each individual value.
321void
322Printer::printConstantValueOnly(const Constant* CV,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000323 int numPadBytesAfter /* = 0 */)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000324{
325 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000326 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000327
328 if (CVA && isStringCompatible(CVA))
329 { // print the string alone and return
Chris Lattnere0121322003-08-03 23:37:09 +0000330 O << "\t.string\t" << getAsCString(CVA) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000331 }
332 else if (CVA)
333 { // Not a string. Print the values in successive locations
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 }
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 =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000341 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000342 const std::vector<Use>& constValues = CVS->getValues();
343 unsigned sizeSoFar = 0;
344 for (unsigned i=0, N = constValues.size(); i < N; i++)
345 {
346 const Constant* field = cast<Constant>(constValues[i].get());
347
348 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000349 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000350 int padSize = ((i == N-1? cvsLayout->StructSize
351 : cvsLayout->MemberOffsets[i+1])
352 - cvsLayout->MemberOffsets[i]) - fieldSize;
353 sizeSoFar += (fieldSize + padSize);
354
355 // Now print the actual field value
356 printConstantValueOnly(field, padSize);
357 }
358 assert(sizeSoFar == cvsLayout->StructSize &&
359 "Layout of constant struct may be incorrect!");
360 }
361 else
362 printSingleConstantValue(CV);
363
Chris Lattnere0121322003-08-03 23:37:09 +0000364 if (numPadBytesAfter) O << "\t.zero\t " << numPadBytesAfter << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000365}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000366
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000367/// printConstantPool - Print to the current output stream assembly
368/// representations of the constants in the constant pool MCP. This is
369/// used to print out constants which have been "spilled to memory" by
370/// the code generator.
371///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000372void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000373 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000374 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000375
Chris Lattnerb7089442003-01-13 00:35:03 +0000376 if (CP.empty()) return;
377
378 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
379 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000380 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000381 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000382 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
383 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000384 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000385 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000386}
387
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000388/// runOnMachineFunction - This uses the printMachineInstruction()
389/// method to print assembly for each instruction.
390///
Chris Lattner0285a332002-12-28 20:25:38 +0000391bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000392 // BBNumber is used here so that a given Printer will never give two
393 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000394 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000395
Chris Lattnere0121322003-08-03 23:37:09 +0000396 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000397 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000398 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000399
Chris Lattnerb7089442003-01-13 00:35:03 +0000400 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000401 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000402
Brian Gaeke6559bb92002-11-14 22:32:30 +0000403 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000404 O << "\t.text\n";
405 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000406 O << "\t.globl\t" << CurrentFnName << "\n";
407 O << "\t.type\t" << CurrentFnName << ", @function\n";
408 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409
Brian Gaeked7908f62003-06-27 00:00:48 +0000410 // Number each basic block so that we can consistently refer to them
411 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000412 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 Gaeke6559bb92002-11-14 22:32:30 +0000418 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000419 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
420 I != E; ++I) {
421 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000422 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000423 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000424 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 Gaekede420ae2003-07-23 20:25:08 +0000428 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000429 }
Chris Lattner0285a332002-12-28 20:25:38 +0000430 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000431
432 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000433 return false;
434}
435
Chris Lattner3d3067b2002-11-21 20:44:15 +0000436static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000437 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000438 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
439 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000440}
441
442static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000443 if (MI->getOperand(Op).isFrameIndex()) return true;
444 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000445 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000446 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
447 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000448}
449
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000450void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000451 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000452 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000453 switch (MO.getType()) {
454 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000455 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000456 O << "<" << V->getName() << ">";
457 return;
458 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000459 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000460 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000461 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
462 O << RI.get(MO.getReg()).Name;
463 else
464 O << "%reg" << MO.getReg();
465 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000466
467 case MachineOperand::MO_SignExtendedImmed:
468 case MachineOperand::MO_UnextendedImmed:
469 O << (int)MO.getImmedValue();
470 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000471 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000472 {
473 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
474 assert (i != NumberForBB.end()
475 && "Could not find a BB I previously put in the NumberForBB map!");
Brian Gaeke002a50a2003-07-31 17:38:52 +0000476 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Brian Gaeked7908f62003-06-27 00:00:48 +0000477 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000478 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000479 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000480 if (!elideOffsetKeyword)
481 O << "OFFSET ";
482 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000483 return;
484 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000485 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000486 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000487 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000488 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000489 }
490}
491
Chris Lattner3501fea2003-01-14 22:00:31 +0000492static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000493 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000494 default: assert(0 && "Unknown arg size!");
495 case X86II::Arg8: return "BYTE PTR";
496 case X86II::Arg16: return "WORD PTR";
497 case X86II::Arg32: return "DWORD PTR";
498 case X86II::Arg64: return "QWORD PTR";
499 case X86II::ArgF32: return "DWORD PTR";
500 case X86II::ArgF64: return "QWORD PTR";
501 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000502 }
503}
504
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000505void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000506 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000507 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000508
509 if (MI->getOperand(Op).isFrameIndex()) {
510 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
511 if (MI->getOperand(Op+3).getImmedValue())
512 O << " + " << MI->getOperand(Op+3).getImmedValue();
513 O << "]";
514 return;
515 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000516 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000517 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000518 if (MI->getOperand(Op+3).getImmedValue())
519 O << " + " << MI->getOperand(Op+3).getImmedValue();
520 O << "]";
521 return;
522 }
523
Chris Lattner3d3067b2002-11-21 20:44:15 +0000524 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000525 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000526 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000527 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000528
529 O << "[";
530 bool NeedPlus = false;
531 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000532 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000533 NeedPlus = true;
534 }
535
536 if (IndexReg.getReg()) {
537 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000538 if (ScaleVal != 1)
539 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000540 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000541 NeedPlus = true;
542 }
543
Chris Lattner0285a332002-12-28 20:25:38 +0000544 if (DispVal) {
545 if (NeedPlus)
546 if (DispVal > 0)
547 O << " + ";
548 else {
549 O << " - ";
550 DispVal = -DispVal;
551 }
552 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000553 }
554 O << "]";
555}
556
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000557/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000558/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000559///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000560void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000561 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000562 const TargetInstrInfo &TII = TM.getInstrInfo();
563 const TargetInstrDescriptor &Desc = TII.get(Opcode);
564 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000565
Chris Lattnereca1f632002-12-25 05:09:01 +0000566 switch (Desc.TSFlags & X86II::FormMask) {
567 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000568 // Print pseudo-instructions as comments; either they should have been
569 // turned into real instructions by now, or they don't need to be
570 // seen by the assembler (e.g., IMPLICIT_USEs.)
571 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000572 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000573 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000574 O << " = phi ";
575 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
576 if (i != 1) O << ", ";
577 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000578 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000579 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000580 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000581 O << "]";
582 }
583 } else {
584 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000585 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
586 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000587 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 O << " = ";
589 ++i;
590 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000591 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000592
593 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
594 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000595 if (MI->getOperand(i).opIsDefOnly() ||
596 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000597 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000598 if (MI->getOperand(i).opIsDefOnly() ||
599 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000600 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000601 }
602 O << "\n";
603 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000604
Chris Lattnerf9f60882002-11-18 06:56:51 +0000605 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000606 // The accepted forms of Raw instructions are:
607 // 1. nop - No operand required
608 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000609 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000610 //
611 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000612 (MI->getNumOperands() == 1 &&
613 (MI->getOperand(0).isPCRelativeDisp() ||
614 MI->getOperand(0).isGlobalAddress() ||
615 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000616 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000617 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000618
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000619 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000620 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000621 }
622 O << "\n";
623 return;
624
Chris Lattner77875d82002-11-21 02:00:20 +0000625 case X86II::AddRegFrm: {
626 // There are currently two forms of acceptable AddRegFrm instructions.
627 // Either the instruction JUST takes a single register (like inc, dec, etc),
628 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000629 // (move immediate f.e.). Note that this immediate value might be stored as
630 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000631 // into a register. The initial register might be duplicated if this is a
632 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000633 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000634 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000635 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000636 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000637 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000638 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000639 MI->getOperand(1).isRegister() ||
640 MI->getOperand(1).isGlobalAddress() ||
641 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000642 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000643
Chris Lattner77875d82002-11-21 02:00:20 +0000644 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000645
Brian Gaeked7908f62003-06-27 00:00:48 +0000646 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000647 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000648 if (MI->getNumOperands() == 2 &&
649 (!MI->getOperand(1).isRegister() ||
650 MI->getOperand(1).getVRegValueOrNull() ||
651 MI->getOperand(1).isGlobalAddress() ||
652 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000653 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000654 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000655 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000656 if (Desc.TSFlags & X86II::PrintImplUses) {
657 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
658 O << ", " << RI.get(*p).Name;
659 }
660 }
Chris Lattner77875d82002-11-21 02:00:20 +0000661 O << "\n";
662 return;
663 }
Chris Lattner233ad712002-11-21 01:33:44 +0000664 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000665 // There are two acceptable forms of MRMDestReg instructions, those with 2,
666 // 3 and 4 operands:
667 //
668 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000669 //
670 // 3 Operands: in this form, the first two registers (the destination, and
671 // the first operand) should be the same, post register allocation. The 3rd
672 // operand is an additional input. This should be for things like add
673 // instructions.
674 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000675 // 4 Operands: This form is for instructions which are 3 operands forms, but
676 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000677 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000678 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000679 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000680 (MI->getNumOperands() == 2 ||
681 (isTwoAddr && MI->getOperand(1).isRegister() &&
682 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
683 (MI->getNumOperands() == 3 ||
684 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000685 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000686
Brian Gaeked7908f62003-06-27 00:00:48 +0000687 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000688 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000690 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000691 if (MI->getNumOperands() == 4) {
692 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000693 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000694 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000695 O << "\n";
696 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000697 }
Chris Lattner18042332002-11-21 21:03:39 +0000698
699 case X86II::MRMDestMem: {
700 // These instructions are the same as MRMDestReg, but instead of having a
701 // register reference for the mod/rm field, it's a memory reference.
702 //
703 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000704 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000705
Brian Gaeked7908f62003-06-27 00:00:48 +0000706 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000707 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000708 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000709 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000710 O << "\n";
711 return;
712 }
713
Chris Lattner233ad712002-11-21 01:33:44 +0000714 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000715 // There is a two forms that are acceptable for MRMSrcReg instructions,
716 // those with 3 and 2 operands:
717 //
718 // 3 Operands: in this form, the last register (the second input) is the
719 // ModR/M input. The first two operands should be the same, post register
720 // allocation. This is for things like: add r32, r/m32
721 //
722 // 2 Operands: this is for things like mov that do not read a second input
723 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000724 assert(MI->getOperand(0).isRegister() &&
725 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000726 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000727 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000728 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000729 if (MI->getNumOperands() == 3 &&
730 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
731 O << "**";
732
Brian Gaeked7908f62003-06-27 00:00:48 +0000733 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000734 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000735 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000736 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000737 O << "\n";
738 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000739 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000740
Chris Lattner3d3067b2002-11-21 20:44:15 +0000741 case X86II::MRMSrcMem: {
742 // These instructions are the same as MRMSrcReg, but instead of having a
743 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000744 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000745 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000746 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000747 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000748 isMem(MI, 2))
749 && "Bad format for MRMDestReg!");
750 if (MI->getNumOperands() == 2+4 &&
751 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
752 O << "**";
753
Brian Gaeked7908f62003-06-27 00:00:48 +0000754 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000755 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000756 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000757 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000758 O << "\n";
759 return;
760 }
761
Chris Lattner675dd2c2002-11-21 17:09:01 +0000762 case X86II::MRMS0r: case X86II::MRMS1r:
763 case X86II::MRMS2r: case X86II::MRMS3r:
764 case X86II::MRMS4r: case X86II::MRMS5r:
765 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000766 // In this form, the following are valid formats:
767 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000768 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000769 // 2. shl rdest, rinput <implicit CL or 1>
770 // 3. sbb rdest, rinput, immediate [rdest = rinput]
771 //
772 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000773 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000774 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000775 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000776 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000777 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000778 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000779 "Bad MRMSxR format!");
780
Chris Lattnerd9096832002-12-15 08:01:39 +0000781 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000782 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
783 O << "**";
784
Brian Gaeked7908f62003-06-27 00:00:48 +0000785 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000786 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000787 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000788 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000789 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000791 if (Desc.TSFlags & X86II::PrintImplUses) {
792 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
793 O << ", " << RI.get(*p).Name;
794 }
795 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000796 O << "\n";
797
798 return;
799 }
800
Chris Lattnerb7089442003-01-13 00:35:03 +0000801 case X86II::MRMS0m: case X86II::MRMS1m:
802 case X86II::MRMS2m: case X86II::MRMS3m:
803 case X86II::MRMS4m: case X86II::MRMS5m:
804 case X86II::MRMS6m: case X86II::MRMS7m: {
805 // In this form, the following are valid formats:
806 // 1. sete [m]
807 // 2. cmp [m], immediate
808 // 2. shl [m], rinput <implicit CL or 1>
809 // 3. sbb [m], immediate
810 //
811 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
812 isMem(MI, 0) && "Bad MRMSxM format!");
813 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
814 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000815 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
816 // is misassembled by gas in intel_syntax mode as its 32-bit
817 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
818 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000819 if (MI->getOpCode() == X86::FSTPr80) {
820 if ((MI->getOperand(0).getReg() == X86::ESP)
821 && (MI->getOperand(1).getImmedValue() == 1)) {
822 int DispVal = MI->getOperand(3).getImmedValue();
823 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
824 unsigned int val = (unsigned int) DispVal;
825 O << ".byte 0xdb, 0xbc, 0x24\n\t";
826 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
827 } else { // 1 byte disp.
828 unsigned char val = (unsigned char) DispVal;
829 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
830 << std::dec << "\t# ";
831 }
832 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000833 }
834 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
835 // misassembled by gas in intel_syntax mode as its 32-bit
836 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
837 // opcode bytes instead of the instruction.
838 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000839 if ((MI->getOperand(0).getReg() == X86::ESP)
840 && (MI->getOperand(1).getImmedValue() == 1)) {
841 int DispVal = MI->getOperand(3).getImmedValue();
842 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
843 unsigned int val = (unsigned int) DispVal;
844 O << ".byte 0xdb, 0xac, 0x24\n\t";
845 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
846 } else { // 1 byte disp.
847 unsigned char val = (unsigned char) DispVal;
848 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
849 << std::dec << "\t# ";
850 }
851 }
852 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000853 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
854 // invalid opcode, saying "64 bit operations are only supported in
855 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
856 // [...]", which is wrong. Workaround: Output the raw opcode bytes
857 // instead of the instruction.
858 if (MI->getOpCode() == X86::FILDr64) {
859 if ((MI->getOperand(0).getReg() == X86::ESP)
860 && (MI->getOperand(1).getImmedValue() == 1)) {
861 int DispVal = MI->getOperand(3).getImmedValue();
862 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
863 unsigned int val = (unsigned int) DispVal;
864 O << ".byte 0xdf, 0xac, 0x24\n\t";
865 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
866 } else { // 1 byte disp.
867 unsigned char val = (unsigned char) DispVal;
868 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
869 << std::dec << "\t# ";
870 }
871 }
872 }
873 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
874 // an invalid opcode, saying "64 bit operations are only
875 // supported in 64 bit modes." libopcodes disassembles it as
876 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
877 // "fistpll DWORD PTR " instead, which is what libopcodes is
878 // expecting to see.
879 if (MI->getOpCode() == X86::FISTPr64) {
880 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000881 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000882 if (MI->getNumOperands() == 5) {
883 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000884 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000885 }
886 O << "\t# ";
887 }
888
Brian Gaeked7908f62003-06-27 00:00:48 +0000889 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000890 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000891 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000892 if (MI->getNumOperands() == 5) {
893 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000894 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000895 }
896 O << "\n";
897 return;
898 }
899
Chris Lattnerf9f60882002-11-18 06:56:51 +0000900 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000901 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000902 }
Chris Lattner72614082002-10-25 22:55:53 +0000903}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000904
905bool Printer::doInitialization(Module &M)
906{
907 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
908 // with no % decorations on register names.
909 O << "\t.intel_syntax noprefix\n";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000910 Mang = new Mangler(M);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000911 return false; // success
912}
913
Chris Lattnerc07736a2003-07-23 15:22:26 +0000914static const Function *isConstantFunctionPointerRef(const Constant *C) {
915 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
916 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000917 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000918 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000919}
920
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000921bool Printer::doFinalization(Module &M)
922{
Brian Gaekede420ae2003-07-23 20:25:08 +0000923 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000924 // Print out module-level global variables here.
925 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000926 std::string name(Mang->getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000927 if (I->hasInitializer()) {
928 Constant *C = I->getInitializer();
Chris Lattnere0121322003-08-03 23:37:09 +0000929 if (C->isNullValue()) {
930 O << "\n\n\t.comm " << name << "," << TD.getTypeSize(C->getType())
931 << "," << (unsigned)TD.getTypeAlignment(C->getType());
932 O << "\t\t# ";
933 WriteAsOperand(O, I, true, true, &M);
934 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000935 } else {
Chris Lattnere0121322003-08-03 23:37:09 +0000936 O << "\n\n\t.data\n";
937 O << "\t.globl " << name << "\n";
938 O << "\t.type " << name << ",@object\n";
939 O << "\t.size " << name << "," << TD.getTypeSize(C->getType()) << "\n";
940 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
941 O << name << ":\t\t\t\t# ";
942 WriteAsOperand(O, I, true, true, &M);
943 O << " = ";
944 WriteAsOperand(O, C, false, false, &M);
945 O << "\n";
946 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000947 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000948 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000949 O << "\t.globl " << name << "\n";
950 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000951 << (unsigned)TD.getTypeSize(I->getType()) << ", "
952 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000953 }
954 }
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000955 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000956 return false; // success
957}