blob: adb296fdb6c9725f6f23858274d9fbf4d745aead [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"
Brian Gaeke6559bb92002-11-14 22:32:30 +000012#include "llvm/Function.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000013#include "llvm/Constant.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000014#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000016#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000017#include "llvm/CodeGen/MachineInstr.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000018#include "llvm/Type.h"
19#include "llvm/Constants.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/DerivedTypes.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000022#include "Support/StringExtras.h"
23#include "llvm/Module.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000024#include "llvm/Support/Mangler.h"
Chris Lattner72614082002-10-25 22:55:53 +000025
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000026namespace {
Chris Lattner0285a332002-12-28 20:25:38 +000027 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000028 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000029 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000030 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000031
32 /// Target machine description which we query for reg. names, data
33 /// layout, etc.
34 ///
35 TargetMachine &TM;
36
Brian Gaeked9fb37a2003-07-24 20:20:44 +000037 /// Name-mangler for global names.
38 ///
39 Mangler *Mang;
40
Brian Gaekede420ae2003-07-23 20:25:08 +000041 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000042
43 /// We name each basic block in a Function with a unique number, so
44 /// that we can consistently refer to them later. This is cleared
45 /// at the beginning of each call to runOnMachineFunction().
46 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000047 typedef std::map<const Value *, unsigned> ValueMapTy;
48 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000049
50 /// Cache of mangled name for current function. This is
51 /// recalculated at the beginning of each call to
52 /// runOnMachineFunction().
53 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000054 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000055
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000056 virtual const char *getPassName() const {
57 return "X86 Assembly Printer";
58 }
59
Brian Gaeked9fb37a2003-07-24 20:20:44 +000060 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000061 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000062 bool elideOffsetKeyword = false);
63 void printMemReference(const MachineInstr *MI, unsigned Op);
64 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000065 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000066 std::string ConstantExprToString(const ConstantExpr* CE);
67 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000068 bool doInitialization(Module &M);
69 bool doFinalization(Module &M);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000070 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
71 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000072 };
Brian Gaeked7908f62003-06-27 00:00:48 +000073} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000074
Brian Gaeke92bdfe62003-07-23 18:37:06 +000075/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000076/// assembly code for a MachineFunction to the given output stream,
77/// using the given target machine description. This should work
78/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000079///
Brian Gaekede420ae2003-07-23 20:25:08 +000080Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
81 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000082}
83
Brian Gaeke92bdfe62003-07-23 18:37:06 +000084/// valToExprString - Helper function for ConstantExprToString().
85/// Appends result to argument string S.
86///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000087std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000088 std::string S;
89 bool failed = false;
90 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
91 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
92 S += std::string(CB == ConstantBool::True ? "1" : "0");
93 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
94 S += itostr(CI->getValue());
95 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
96 S += utostr(CI->getValue());
97 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
98 S += ftostr(CFP->getValue());
99 else if (isa<ConstantPointerNull>(CV))
100 S += "0";
101 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
102 S += valToExprString(CPR->getValue());
103 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
104 S += ConstantExprToString(CE);
105 else
106 failed = true;
107 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000108 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000109 }
110 else
111 failed = true;
112
113 if (failed) {
114 assert(0 && "Cannot convert value to string");
115 S += "<illegal-value>";
116 }
117 return S;
118}
119
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000120/// ConstantExprToString - Convert a ConstantExpr to an asm expression
121/// and return this as a string.
122///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000123std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000124 std::string S;
Brian Gaekede420ae2003-07-23 20:25:08 +0000125 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126 switch(CE->getOpcode()) {
127 case Instruction::GetElementPtr:
128 { // generate a symbolic expression for the byte address
129 const Value* ptrVal = CE->getOperand(0);
130 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
131 S += "(" + valToExprString(ptrVal) + ") + ("
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000132 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000133 break;
134 }
135
136 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000137 // Support only non-converting or widening casts for now, that is,
138 // ones that do not involve a change in value. This assertion is
139 // not a complete check.
140 {
141 Constant *Op = CE->getOperand(0);
142 const Type *OpTy = Op->getType(), *Ty = CE->getType();
143 assert(((isa<PointerType>(OpTy)
144 && (Ty == Type::LongTy || Ty == Type::ULongTy))
145 || (isa<PointerType>(Ty)
146 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
147 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
148 && (OpTy-> isLosslesslyConvertibleTo(Ty))))
149 && "FIXME: Don't yet support this kind of constant cast expr");
150 S += "(" + valToExprString(Op) + ")";
151 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152 break;
153
154 case Instruction::Add:
155 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
156 + valToExprString(CE->getOperand(1)) + ")";
157 break;
158
159 default:
160 assert(0 && "Unsupported operator in ConstantExprToString()");
161 break;
162 }
163
164 return S;
165}
166
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000167/// printSingleConstantValue - Print a single constant value.
168///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000170Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000171{
172 assert(CV->getType() != Type::VoidTy &&
173 CV->getType() != Type::TypeTy &&
174 CV->getType() != Type::LabelTy &&
175 "Unexpected type for Constant");
176
177 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
178 && "Aggregate types should be handled outside this function");
179
180 const Type *type = CV->getType();
181 O << "\t";
182 switch(type->getPrimitiveID())
183 {
184 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
185 O << ".byte";
186 break;
187 case Type::UShortTyID: case Type::ShortTyID:
188 O << ".word";
189 break;
190 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
191 O << ".long";
192 break;
193 case Type::ULongTyID: case Type::LongTyID:
194 O << ".quad";
195 break;
196 case Type::FloatTyID:
197 O << ".long";
198 break;
199 case Type::DoubleTyID:
200 O << ".quad";
201 break;
202 case Type::ArrayTyID:
203 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
204 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
205 O << ".string";
206 else
207 assert (0 && "Can't handle printing this type of array");
208 break;
209 default:
210 assert (0 && "Can't handle printing this type of thing");
211 break;
212 }
213 O << "\t";
214
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000215 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
216 {
217 // Constant expression built from operators, constants, and
218 // symbolic addrs
219 O << ConstantExprToString(CE) << "\n";
220 }
221 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000222 {
223 if (type->isFloatingPoint()) {
224 // FP Constants are printed as integer constants to avoid losing
225 // precision...
226 double Val = cast<ConstantFP>(CV)->getValue();
227 if (type == Type::FloatTy) {
228 float FVal = (float)Val;
229 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
230 O << *(unsigned int*)ProxyPtr;
231 } else if (type == Type::DoubleTy) {
232 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
233 O << *(uint64_t*)ProxyPtr;
234 } else {
235 assert(0 && "Unknown floating point type!");
236 }
237
238 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
239 } else {
240 WriteAsOperand(O, CV, false, false) << "\n";
241 }
242 }
243 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
244 {
245 // This is a constant address for a global variable or method.
246 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000247 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248 }
249 else if (isa<ConstantPointerNull>(CV))
250 {
251 // Null pointer value
252 O << "0\n";
253 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000254 else
255 {
256 assert(0 && "Unknown elementary type for constant");
257 }
258}
259
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000260/// isStringCompatible - Can we treat the specified array as a string?
261/// Only if it is an array of ubytes or non-negative sbytes.
262///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000263static bool isStringCompatible(const ConstantArray *CVA) {
264 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
265 if (ETy == Type::UByteTy) return true;
266 if (ETy != Type::SByteTy) return false;
267
268 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
269 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
270 return false;
271
272 return true;
273}
274
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000275/// toOctal - Convert the low order bits of X into an octal digit.
276///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000277static inline char toOctal(int X) {
278 return (X&7)+'0';
279}
280
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000281/// getAsCString - Return the specified array as a C compatible
282/// string, only if the predicate isStringCompatible is true.
283///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000284static std::string getAsCString(const ConstantArray *CVA) {
285 assert(isStringCompatible(CVA) && "Array is not string compatible!");
286
287 std::string Result;
288 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
289 Result = "\"";
290 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000291 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000292
293 if (C == '"') {
294 Result += "\\\"";
295 } else if (C == '\\') {
296 Result += "\\\\";
297 } else if (isprint(C)) {
298 Result += C;
299 } else {
300 switch(C) {
301 case '\a': Result += "\\a"; break;
302 case '\b': Result += "\\b"; break;
303 case '\f': Result += "\\f"; break;
304 case '\n': Result += "\\n"; break;
305 case '\r': Result += "\\r"; break;
306 case '\t': Result += "\\t"; break;
307 case '\v': Result += "\\v"; break;
308 default:
309 Result += '\\';
310 Result += toOctal(C >> 6);
311 Result += toOctal(C >> 3);
312 Result += toOctal(C >> 0);
313 break;
314 }
315 }
316 }
317 Result += "\"";
318 return Result;
319}
320
321// Print a constant value or values (it may be an aggregate).
322// Uses printSingleConstantValue() to print each individual value.
323void
324Printer::printConstantValueOnly(const Constant* CV,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000325 int numPadBytesAfter /* = 0 */)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000326{
327 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000328 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000329
330 if (CVA && isStringCompatible(CVA))
331 { // print the string alone and return
332 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
333 }
334 else if (CVA)
335 { // Not a string. Print the values in successive locations
336 const std::vector<Use> &constValues = CVA->getValues();
337 for (unsigned i=0; i < constValues.size(); i++)
338 printConstantValueOnly(cast<Constant>(constValues[i].get()));
339 }
340 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
341 { // Print the fields in successive locations. Pad to align if needed!
342 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000343 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000344 const std::vector<Use>& constValues = CVS->getValues();
345 unsigned sizeSoFar = 0;
346 for (unsigned i=0, N = constValues.size(); i < N; i++)
347 {
348 const Constant* field = cast<Constant>(constValues[i].get());
349
350 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000351 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000352 int padSize = ((i == N-1? cvsLayout->StructSize
353 : cvsLayout->MemberOffsets[i+1])
354 - cvsLayout->MemberOffsets[i]) - fieldSize;
355 sizeSoFar += (fieldSize + padSize);
356
357 // Now print the actual field value
358 printConstantValueOnly(field, padSize);
359 }
360 assert(sizeSoFar == cvsLayout->StructSize &&
361 "Layout of constant struct may be incorrect!");
362 }
363 else
364 printSingleConstantValue(CV);
365
366 if (numPadBytesAfter) {
367 unsigned numBytes = numPadBytesAfter;
368 for ( ; numBytes >= 8; numBytes -= 8)
369 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
370 if (numBytes >= 4)
371 {
372 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
373 numBytes -= 4;
374 }
375 while (numBytes--)
376 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
377 }
378}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000379
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000380/// printConstantPool - Print to the current output stream assembly
381/// representations of the constants in the constant pool MCP. This is
382/// used to print out constants which have been "spilled to memory" by
383/// the code generator.
384///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000385void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000386 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000387 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000388
Chris Lattnerb7089442003-01-13 00:35:03 +0000389 if (CP.empty()) return;
390
391 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
392 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000393 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000394 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000395 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
396 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000397 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000398 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000399}
400
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000401/// runOnMachineFunction - This uses the printMachineInstruction()
402/// method to print assembly for each instruction.
403///
Chris Lattner0285a332002-12-28 20:25:38 +0000404bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000405 // BBNumber is used here so that a given Printer will never give two
406 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000407 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000408
Brian Gaeked7908f62003-06-27 00:00:48 +0000409 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000410 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000411
Chris Lattnerb7089442003-01-13 00:35:03 +0000412 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000413 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000414
Brian Gaeke6559bb92002-11-14 22:32:30 +0000415 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000416 O << "\t.text\n";
417 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000418 O << "\t.globl\t" << CurrentFnName << "\n";
419 O << "\t.type\t" << CurrentFnName << ", @function\n";
420 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000421
Brian Gaeked7908f62003-06-27 00:00:48 +0000422 // Number each basic block so that we can consistently refer to them
423 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000424 NumberForBB.clear();
425 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
426 I != E; ++I) {
427 NumberForBB[I->getBasicBlock()] = BBNumber++;
428 }
429
Brian Gaeke6559bb92002-11-14 22:32:30 +0000430 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000431 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
432 I != E; ++I) {
433 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000434 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000435 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000436 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
437 II != E; ++II) {
438 // Print the assembly for the instruction.
439 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000440 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000441 }
Chris Lattner0285a332002-12-28 20:25:38 +0000442 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000443
444 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000445 return false;
446}
447
Chris Lattner3d3067b2002-11-21 20:44:15 +0000448static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000449 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000450 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
451 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000452}
453
454static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000455 if (MI->getOperand(Op).isFrameIndex()) return true;
456 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000457 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000458 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
459 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000460}
461
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000462void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000463 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000464 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000465 switch (MO.getType()) {
466 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000467 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000468 O << "<" << V->getName() << ">";
469 return;
470 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000471 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000472 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000473 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
474 O << RI.get(MO.getReg()).Name;
475 else
476 O << "%reg" << MO.getReg();
477 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000478
479 case MachineOperand::MO_SignExtendedImmed:
480 case MachineOperand::MO_UnextendedImmed:
481 O << (int)MO.getImmedValue();
482 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000483 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000484 {
485 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
486 assert (i != NumberForBB.end()
487 && "Could not find a BB I previously put in the NumberForBB map!");
Brian Gaeke002a50a2003-07-31 17:38:52 +0000488 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Brian Gaeked7908f62003-06-27 00:00:48 +0000489 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000490 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000491 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000492 if (!elideOffsetKeyword)
493 O << "OFFSET ";
494 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000495 return;
496 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000497 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000498 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000499 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000500 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000501 }
502}
503
Chris Lattner3501fea2003-01-14 22:00:31 +0000504static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000505 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000506 default: assert(0 && "Unknown arg size!");
507 case X86II::Arg8: return "BYTE PTR";
508 case X86II::Arg16: return "WORD PTR";
509 case X86II::Arg32: return "DWORD PTR";
510 case X86II::Arg64: return "QWORD PTR";
511 case X86II::ArgF32: return "DWORD PTR";
512 case X86II::ArgF64: return "QWORD PTR";
513 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000514 }
515}
516
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000517void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000518 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000519 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000520
521 if (MI->getOperand(Op).isFrameIndex()) {
522 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
523 if (MI->getOperand(Op+3).getImmedValue())
524 O << " + " << MI->getOperand(Op+3).getImmedValue();
525 O << "]";
526 return;
527 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000528 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000529 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000530 if (MI->getOperand(Op+3).getImmedValue())
531 O << " + " << MI->getOperand(Op+3).getImmedValue();
532 O << "]";
533 return;
534 }
535
Chris Lattner3d3067b2002-11-21 20:44:15 +0000536 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000537 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000538 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000539 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000540
541 O << "[";
542 bool NeedPlus = false;
543 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000544 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000545 NeedPlus = true;
546 }
547
548 if (IndexReg.getReg()) {
549 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000550 if (ScaleVal != 1)
551 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000552 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000553 NeedPlus = true;
554 }
555
Chris Lattner0285a332002-12-28 20:25:38 +0000556 if (DispVal) {
557 if (NeedPlus)
558 if (DispVal > 0)
559 O << " + ";
560 else {
561 O << " - ";
562 DispVal = -DispVal;
563 }
564 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000565 }
566 O << "]";
567}
568
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000569/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000570/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000571///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000572void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000573 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000574 const TargetInstrInfo &TII = TM.getInstrInfo();
575 const TargetInstrDescriptor &Desc = TII.get(Opcode);
576 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000577
Chris Lattnereca1f632002-12-25 05:09:01 +0000578 switch (Desc.TSFlags & X86II::FormMask) {
579 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000580 // Print pseudo-instructions as comments; either they should have been
581 // turned into real instructions by now, or they don't need to be
582 // seen by the assembler (e.g., IMPLICIT_USEs.)
583 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000584 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000585 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000586 O << " = phi ";
587 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
588 if (i != 1) O << ", ";
589 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000590 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000591 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000592 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000593 O << "]";
594 }
595 } else {
596 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000597 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
598 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000599 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000600 O << " = ";
601 ++i;
602 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000603 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000604
605 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
606 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000607 if (MI->getOperand(i).opIsDefOnly() ||
608 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000609 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000610 if (MI->getOperand(i).opIsDefOnly() ||
611 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000612 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000613 }
614 O << "\n";
615 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000616
Chris Lattnerf9f60882002-11-18 06:56:51 +0000617 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000618 // The accepted forms of Raw instructions are:
619 // 1. nop - No operand required
620 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000621 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000622 //
623 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000624 (MI->getNumOperands() == 1 &&
625 (MI->getOperand(0).isPCRelativeDisp() ||
626 MI->getOperand(0).isGlobalAddress() ||
627 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000628 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000629 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000630
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000631 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000632 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000633 }
634 O << "\n";
635 return;
636
Chris Lattner77875d82002-11-21 02:00:20 +0000637 case X86II::AddRegFrm: {
638 // There are currently two forms of acceptable AddRegFrm instructions.
639 // Either the instruction JUST takes a single register (like inc, dec, etc),
640 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000641 // (move immediate f.e.). Note that this immediate value might be stored as
642 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000643 // into a register. The initial register might be duplicated if this is a
644 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000645 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000646 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000647 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000648 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000649 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000650 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000651 MI->getOperand(1).isRegister() ||
652 MI->getOperand(1).isGlobalAddress() ||
653 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000654 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000655
Chris Lattner77875d82002-11-21 02:00:20 +0000656 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000657
Brian Gaeked7908f62003-06-27 00:00:48 +0000658 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000659 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000660 if (MI->getNumOperands() == 2 &&
661 (!MI->getOperand(1).isRegister() ||
662 MI->getOperand(1).getVRegValueOrNull() ||
663 MI->getOperand(1).isGlobalAddress() ||
664 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000665 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000666 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000667 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000668 if (Desc.TSFlags & X86II::PrintImplUses) {
669 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
670 O << ", " << RI.get(*p).Name;
671 }
672 }
Chris Lattner77875d82002-11-21 02:00:20 +0000673 O << "\n";
674 return;
675 }
Chris Lattner233ad712002-11-21 01:33:44 +0000676 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 // There are two acceptable forms of MRMDestReg instructions, those with 2,
678 // 3 and 4 operands:
679 //
680 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000681 //
682 // 3 Operands: in this form, the first two registers (the destination, and
683 // the first operand) should be the same, post register allocation. The 3rd
684 // operand is an additional input. This should be for things like add
685 // instructions.
686 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000687 // 4 Operands: This form is for instructions which are 3 operands forms, but
688 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000690 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000691 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 (MI->getNumOperands() == 2 ||
693 (isTwoAddr && MI->getOperand(1).isRegister() &&
694 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
695 (MI->getNumOperands() == 3 ||
696 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000697 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000698
Brian Gaeked7908f62003-06-27 00:00:48 +0000699 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000700 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000701 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000702 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000703 if (MI->getNumOperands() == 4) {
704 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000705 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000706 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000707 O << "\n";
708 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000709 }
Chris Lattner18042332002-11-21 21:03:39 +0000710
711 case X86II::MRMDestMem: {
712 // These instructions are the same as MRMDestReg, but instead of having a
713 // register reference for the mod/rm field, it's a memory reference.
714 //
715 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000716 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000717
Brian Gaeked7908f62003-06-27 00:00:48 +0000718 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000719 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000720 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000722 O << "\n";
723 return;
724 }
725
Chris Lattner233ad712002-11-21 01:33:44 +0000726 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000727 // There is a two forms that are acceptable for MRMSrcReg instructions,
728 // those with 3 and 2 operands:
729 //
730 // 3 Operands: in this form, the last register (the second input) is the
731 // ModR/M input. The first two operands should be the same, post register
732 // allocation. This is for things like: add r32, r/m32
733 //
734 // 2 Operands: this is for things like mov that do not read a second input
735 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 assert(MI->getOperand(0).isRegister() &&
737 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000738 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000739 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000740 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000741 if (MI->getNumOperands() == 3 &&
742 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
743 O << "**";
744
Brian Gaeked7908f62003-06-27 00:00:48 +0000745 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000746 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000747 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000748 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000749 O << "\n";
750 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000751 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000752
Chris Lattner3d3067b2002-11-21 20:44:15 +0000753 case X86II::MRMSrcMem: {
754 // These instructions are the same as MRMSrcReg, but instead of having a
755 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000756 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000757 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000758 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000759 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000760 isMem(MI, 2))
761 && "Bad format for MRMDestReg!");
762 if (MI->getNumOperands() == 2+4 &&
763 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
764 O << "**";
765
Brian Gaeked7908f62003-06-27 00:00:48 +0000766 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000767 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000768 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000769 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000770 O << "\n";
771 return;
772 }
773
Chris Lattner675dd2c2002-11-21 17:09:01 +0000774 case X86II::MRMS0r: case X86II::MRMS1r:
775 case X86II::MRMS2r: case X86II::MRMS3r:
776 case X86II::MRMS4r: case X86II::MRMS5r:
777 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000778 // In this form, the following are valid formats:
779 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000780 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000781 // 2. shl rdest, rinput <implicit CL or 1>
782 // 3. sbb rdest, rinput, immediate [rdest = rinput]
783 //
784 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000785 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000786 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000787 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000788 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000789 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000790 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000791 "Bad MRMSxR format!");
792
Chris Lattnerd9096832002-12-15 08:01:39 +0000793 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000794 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
795 O << "**";
796
Brian Gaeked7908f62003-06-27 00:00:48 +0000797 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000798 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000799 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000801 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000802 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000803 if (Desc.TSFlags & X86II::PrintImplUses) {
804 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
805 O << ", " << RI.get(*p).Name;
806 }
807 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000808 O << "\n";
809
810 return;
811 }
812
Chris Lattnerb7089442003-01-13 00:35:03 +0000813 case X86II::MRMS0m: case X86II::MRMS1m:
814 case X86II::MRMS2m: case X86II::MRMS3m:
815 case X86II::MRMS4m: case X86II::MRMS5m:
816 case X86II::MRMS6m: case X86II::MRMS7m: {
817 // In this form, the following are valid formats:
818 // 1. sete [m]
819 // 2. cmp [m], immediate
820 // 2. shl [m], rinput <implicit CL or 1>
821 // 3. sbb [m], immediate
822 //
823 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
824 isMem(MI, 0) && "Bad MRMSxM format!");
825 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
826 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000827 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
828 // is misassembled by gas in intel_syntax mode as its 32-bit
829 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
830 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000831 if (MI->getOpCode() == X86::FSTPr80) {
832 if ((MI->getOperand(0).getReg() == X86::ESP)
833 && (MI->getOperand(1).getImmedValue() == 1)) {
834 int DispVal = MI->getOperand(3).getImmedValue();
835 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
836 unsigned int val = (unsigned int) DispVal;
837 O << ".byte 0xdb, 0xbc, 0x24\n\t";
838 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
839 } else { // 1 byte disp.
840 unsigned char val = (unsigned char) DispVal;
841 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
842 << std::dec << "\t# ";
843 }
844 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000845 }
846 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
847 // misassembled by gas in intel_syntax mode as its 32-bit
848 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
849 // opcode bytes instead of the instruction.
850 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000851 if ((MI->getOperand(0).getReg() == X86::ESP)
852 && (MI->getOperand(1).getImmedValue() == 1)) {
853 int DispVal = MI->getOperand(3).getImmedValue();
854 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
855 unsigned int val = (unsigned int) DispVal;
856 O << ".byte 0xdb, 0xac, 0x24\n\t";
857 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
858 } else { // 1 byte disp.
859 unsigned char val = (unsigned char) DispVal;
860 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
861 << std::dec << "\t# ";
862 }
863 }
864 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000865 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
866 // invalid opcode, saying "64 bit operations are only supported in
867 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
868 // [...]", which is wrong. Workaround: Output the raw opcode bytes
869 // instead of the instruction.
870 if (MI->getOpCode() == X86::FILDr64) {
871 if ((MI->getOperand(0).getReg() == X86::ESP)
872 && (MI->getOperand(1).getImmedValue() == 1)) {
873 int DispVal = MI->getOperand(3).getImmedValue();
874 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
875 unsigned int val = (unsigned int) DispVal;
876 O << ".byte 0xdf, 0xac, 0x24\n\t";
877 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
878 } else { // 1 byte disp.
879 unsigned char val = (unsigned char) DispVal;
880 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
881 << std::dec << "\t# ";
882 }
883 }
884 }
885 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
886 // an invalid opcode, saying "64 bit operations are only
887 // supported in 64 bit modes." libopcodes disassembles it as
888 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
889 // "fistpll DWORD PTR " instead, which is what libopcodes is
890 // expecting to see.
891 if (MI->getOpCode() == X86::FISTPr64) {
892 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000893 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000894 if (MI->getNumOperands() == 5) {
895 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000896 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000897 }
898 O << "\t# ";
899 }
900
Brian Gaeked7908f62003-06-27 00:00:48 +0000901 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000902 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000903 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000904 if (MI->getNumOperands() == 5) {
905 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000906 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000907 }
908 O << "\n";
909 return;
910 }
911
Chris Lattnerf9f60882002-11-18 06:56:51 +0000912 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000913 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000914 }
Chris Lattner72614082002-10-25 22:55:53 +0000915}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000916
917bool Printer::doInitialization(Module &M)
918{
919 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
920 // with no % decorations on register names.
921 O << "\t.intel_syntax noprefix\n";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000922 Mang = new Mangler(M);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000923 return false; // success
924}
925
Chris Lattnerc07736a2003-07-23 15:22:26 +0000926static const Function *isConstantFunctionPointerRef(const Constant *C) {
927 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
928 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000929 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000930 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000931}
932
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000933bool Printer::doFinalization(Module &M)
934{
Brian Gaekede420ae2003-07-23 20:25:08 +0000935 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000936 // Print out module-level global variables here.
937 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000938 std::string name(Mang->getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000939 if (I->hasInitializer()) {
940 Constant *C = I->getInitializer();
941 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000942 O << "\t.globl " << name << "\n";
943 O << "\t.type " << name << ",@object\n";
944 O << "\t.size " << name << ","
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000945 << (unsigned)TD.getTypeSize(I->getType()) << "\n";
946 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000947 O << name << ":\t\t\t\t\t#";
948 // If this is a constant function pointer, we only print out the
949 // name of the function in the comment (because printing the
950 // function means calling AsmWriter to print the whole LLVM
951 // assembly, which would corrupt the X86 assembly output.)
952 // Otherwise we print out the whole llvm value as a comment.
953 if (const Function *F = isConstantFunctionPointerRef (C)) {
954 O << " %" << F->getName() << "()\n";
955 } else {
956 O << *C << "\n";
957 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000958 printConstantValueOnly (C);
959 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000960 O << "\t.globl " << name << "\n";
961 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000962 << (unsigned)TD.getTypeSize(I->getType()) << ", "
963 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000964 }
965 }
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000966 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000967 return false; // success
968}