blob: 19430cd9f62bff68d9a3dec0dd21d9acd97b79ff [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"
Chris Lattner233ad712002-11-21 01:33:44 +000018#include "Support/Statistic.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000019#include "Support/hash_map"
20#include "llvm/Type.h"
21#include "llvm/Constants.h"
22#include "llvm/Assembly/Writer.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/SlotCalculator.h"
25#include "Support/StringExtras.h"
26#include "llvm/Module.h"
Chris Lattner72614082002-10-25 22:55:53 +000027
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000028namespace {
Brian Gaeke92bdfe62003-07-23 18:37:06 +000029 /// This is properly part of the name mangler; it keeps track of
30 /// which global values have had their names mangled. It is cleared
31 /// at the end of every module by doFinalization().
32 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000033 std::set<const Value *> MangledGlobals;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000034
Chris Lattner0285a332002-12-28 20:25:38 +000035 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000036 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000037 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000038 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000039
40 /// Target machine description which we query for reg. names, data
41 /// layout, etc.
42 ///
43 TargetMachine &TM;
44
45 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000046
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
50 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000051 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000053
54 /// Cache of mangled name for current function. This is
55 /// recalculated at the beginning of each call to
56 /// runOnMachineFunction().
57 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000058 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000059
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000060 virtual const char *getPassName() const {
61 return "X86 Assembly Printer";
62 }
63
Brian Gaekede420ae2003-07-23 20:25:08 +000064 void printMachineInstruction(const MachineInstr *MI) const;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000065 void printOp(const MachineOperand &MO,
Brian Gaekede420ae2003-07-23 20:25:08 +000066 bool elideOffsetKeyword = false) const;
67 void printMemReference(const MachineInstr *MI, unsigned Op) const;
Brian Gaeke01d79ff2003-06-25 18:01:07 +000068 void printConstantPool(MachineConstantPool *MCP);
69 bool runOnMachineFunction(MachineFunction &F);
70 std::string ConstantExprToString(const ConstantExpr* CE);
71 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000072 bool doInitialization(Module &M);
73 bool doFinalization(Module &M);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000074 void PrintZeroBytesToPad(int numBytes);
75 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
76 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 };
Brian Gaeked7908f62003-06-27 00:00:48 +000078} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000079
Brian Gaeke92bdfe62003-07-23 18:37:06 +000080/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000081/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000084///
Brian Gaekede420ae2003-07-23 20:25:08 +000085Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
86 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000087}
88
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089/// makeNameProper - We don't want identifier names with ., space, or
90/// - in them, so we mangle these characters into the strings "d_",
91/// "s_", and "D_", respectively.
92///
Brian Gaekebc601fe2003-06-25 22:00:39 +000093static std::string makeNameProper(std::string x) {
94 std::string tmp;
95 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
96 switch (*sI) {
97 case '.': tmp += "d_"; break;
98 case ' ': tmp += "s_"; break;
99 case '-': tmp += "D_"; break;
100 default: tmp += *sI;
101 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000102 return tmp;
103}
104
Brian Gaeked7908f62003-06-27 00:00:48 +0000105static std::string getValueName(const Value *V) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000106 if (V->hasName()) { // Print out the label if it exists...
Brian Gaekebc601fe2003-06-25 22:00:39 +0000107 // Name mangling occurs as follows:
108 // - If V is not a global, mangling always occurs.
109 // - Otherwise, mangling occurs when any of the following are true:
110 // 1) V has internal linkage
111 // 2) V's name would collide if it is not mangled.
112 //
Brian Gaekebc601fe2003-06-25 22:00:39 +0000113 if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
114 if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
115 // No internal linkage, name will not collide -> no mangling.
116 return makeNameProper(gv->getName());
117 }
118 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000119 // Non-global, or global with internal linkage / colliding name -> mangle.
120 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
121 makeNameProper(V->getName());
122 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000123 static int Count = 0;
124 Count++;
125 return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
126}
127
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000128/// valToExprString - Helper function for ConstantExprToString().
129/// Appends result to argument string S.
130///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131std::string Printer::valToExprString(const Value* V) {
132 std::string S;
133 bool failed = false;
134 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
135 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
136 S += std::string(CB == ConstantBool::True ? "1" : "0");
137 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
138 S += itostr(CI->getValue());
139 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
140 S += utostr(CI->getValue());
141 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
142 S += ftostr(CFP->getValue());
143 else if (isa<ConstantPointerNull>(CV))
144 S += "0";
145 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
146 S += valToExprString(CPR->getValue());
147 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
148 S += ConstantExprToString(CE);
149 else
150 failed = true;
151 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000152 S += getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000153 }
154 else
155 failed = true;
156
157 if (failed) {
158 assert(0 && "Cannot convert value to string");
159 S += "<illegal-value>";
160 }
161 return S;
162}
163
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000164/// ConstantExprToString - Convert a ConstantExpr to an asm expression
165/// and return this as a string.
166///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
168 std::string S;
Brian Gaekede420ae2003-07-23 20:25:08 +0000169 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170 switch(CE->getOpcode()) {
171 case Instruction::GetElementPtr:
172 { // generate a symbolic expression for the byte address
173 const Value* ptrVal = CE->getOperand(0);
174 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
175 S += "(" + valToExprString(ptrVal) + ") + ("
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000176 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000177 break;
178 }
179
180 case Instruction::Cast:
181 // Support only non-converting casts for now, i.e., a no-op.
182 // This assertion is not a complete check.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000183 assert(TD.getTypeSize(CE->getType()) ==
184 TD.getTypeSize(CE->getOperand(0)->getType()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000185 S += "(" + valToExprString(CE->getOperand(0)) + ")";
186 break;
187
188 case Instruction::Add:
189 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
190 + valToExprString(CE->getOperand(1)) + ")";
191 break;
192
193 default:
194 assert(0 && "Unsupported operator in ConstantExprToString()");
195 break;
196 }
197
198 return S;
199}
200
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000201/// printSingleConstantValue - Print a single constant value.
202///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000203void
204Printer::printSingleConstantValue(const Constant* CV)
205{
206 assert(CV->getType() != Type::VoidTy &&
207 CV->getType() != Type::TypeTy &&
208 CV->getType() != Type::LabelTy &&
209 "Unexpected type for Constant");
210
211 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
212 && "Aggregate types should be handled outside this function");
213
214 const Type *type = CV->getType();
215 O << "\t";
216 switch(type->getPrimitiveID())
217 {
218 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
219 O << ".byte";
220 break;
221 case Type::UShortTyID: case Type::ShortTyID:
222 O << ".word";
223 break;
224 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
225 O << ".long";
226 break;
227 case Type::ULongTyID: case Type::LongTyID:
228 O << ".quad";
229 break;
230 case Type::FloatTyID:
231 O << ".long";
232 break;
233 case Type::DoubleTyID:
234 O << ".quad";
235 break;
236 case Type::ArrayTyID:
237 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
238 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
239 O << ".string";
240 else
241 assert (0 && "Can't handle printing this type of array");
242 break;
243 default:
244 assert (0 && "Can't handle printing this type of thing");
245 break;
246 }
247 O << "\t";
248
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000249 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
250 {
251 // Constant expression built from operators, constants, and
252 // symbolic addrs
253 O << ConstantExprToString(CE) << "\n";
254 }
255 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000256 {
257 if (type->isFloatingPoint()) {
258 // FP Constants are printed as integer constants to avoid losing
259 // precision...
260 double Val = cast<ConstantFP>(CV)->getValue();
261 if (type == Type::FloatTy) {
262 float FVal = (float)Val;
263 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
264 O << *(unsigned int*)ProxyPtr;
265 } else if (type == Type::DoubleTy) {
266 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
267 O << *(uint64_t*)ProxyPtr;
268 } else {
269 assert(0 && "Unknown floating point type!");
270 }
271
272 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
273 } else {
274 WriteAsOperand(O, CV, false, false) << "\n";
275 }
276 }
277 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
278 {
279 // This is a constant address for a global variable or method.
280 // Use the name of the variable or method as the address value.
Brian Gaekebc601fe2003-06-25 22:00:39 +0000281 O << getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000282 }
283 else if (isa<ConstantPointerNull>(CV))
284 {
285 // Null pointer value
286 O << "0\n";
287 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000288 else
289 {
290 assert(0 && "Unknown elementary type for constant");
291 }
292}
293
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000294/// isStringCompatible - Can we treat the specified array as a string?
295/// Only if it is an array of ubytes or non-negative sbytes.
296///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000297static bool isStringCompatible(const ConstantArray *CVA) {
298 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
299 if (ETy == Type::UByteTy) return true;
300 if (ETy != Type::SByteTy) return false;
301
302 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
303 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
304 return false;
305
306 return true;
307}
308
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000309/// toOctal - Convert the low order bits of X into an octal digit.
310///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000311static inline char toOctal(int X) {
312 return (X&7)+'0';
313}
314
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000315/// getAsCString - Return the specified array as a C compatible
316/// string, only if the predicate isStringCompatible is true.
317///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000318static std::string getAsCString(const ConstantArray *CVA) {
319 assert(isStringCompatible(CVA) && "Array is not string compatible!");
320
321 std::string Result;
322 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
323 Result = "\"";
324 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000325 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000326
327 if (C == '"') {
328 Result += "\\\"";
329 } else if (C == '\\') {
330 Result += "\\\\";
331 } else if (isprint(C)) {
332 Result += C;
333 } else {
334 switch(C) {
335 case '\a': Result += "\\a"; break;
336 case '\b': Result += "\\b"; break;
337 case '\f': Result += "\\f"; break;
338 case '\n': Result += "\\n"; break;
339 case '\r': Result += "\\r"; break;
340 case '\t': Result += "\\t"; break;
341 case '\v': Result += "\\v"; break;
342 default:
343 Result += '\\';
344 Result += toOctal(C >> 6);
345 Result += toOctal(C >> 3);
346 Result += toOctal(C >> 0);
347 break;
348 }
349 }
350 }
351 Result += "\"";
352 return Result;
353}
354
355// Print a constant value or values (it may be an aggregate).
356// Uses printSingleConstantValue() to print each individual value.
357void
358Printer::printConstantValueOnly(const Constant* CV,
359 int numPadBytesAfter /* = 0 */)
360{
361 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000362 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000363
364 if (CVA && isStringCompatible(CVA))
365 { // print the string alone and return
366 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
367 }
368 else if (CVA)
369 { // Not a string. Print the values in successive locations
370 const std::vector<Use> &constValues = CVA->getValues();
371 for (unsigned i=0; i < constValues.size(); i++)
372 printConstantValueOnly(cast<Constant>(constValues[i].get()));
373 }
374 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
375 { // Print the fields in successive locations. Pad to align if needed!
376 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000377 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000378 const std::vector<Use>& constValues = CVS->getValues();
379 unsigned sizeSoFar = 0;
380 for (unsigned i=0, N = constValues.size(); i < N; i++)
381 {
382 const Constant* field = cast<Constant>(constValues[i].get());
383
384 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000385 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000386 int padSize = ((i == N-1? cvsLayout->StructSize
387 : cvsLayout->MemberOffsets[i+1])
388 - cvsLayout->MemberOffsets[i]) - fieldSize;
389 sizeSoFar += (fieldSize + padSize);
390
391 // Now print the actual field value
392 printConstantValueOnly(field, padSize);
393 }
394 assert(sizeSoFar == cvsLayout->StructSize &&
395 "Layout of constant struct may be incorrect!");
396 }
397 else
398 printSingleConstantValue(CV);
399
400 if (numPadBytesAfter) {
401 unsigned numBytes = numPadBytesAfter;
402 for ( ; numBytes >= 8; numBytes -= 8)
403 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
404 if (numBytes >= 4)
405 {
406 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
407 numBytes -= 4;
408 }
409 while (numBytes--)
410 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
411 }
412}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000413
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000414/// printConstantPool - Print to the current output stream assembly
415/// representations of the constants in the constant pool MCP. This is
416/// used to print out constants which have been "spilled to memory" by
417/// the code generator.
418///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000419void Printer::printConstantPool(MachineConstantPool *MCP){
Chris Lattnerb7089442003-01-13 00:35:03 +0000420 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000421 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000422
Chris Lattnerb7089442003-01-13 00:35:03 +0000423 if (CP.empty()) return;
424
425 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
426 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000427 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000428 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000429 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
430 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000431 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000432 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000433}
434
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000435/// runOnMachineFunction - This uses the printMachineInstruction()
436/// method to print assembly for each instruction.
437///
Chris Lattner0285a332002-12-28 20:25:38 +0000438bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000439 // BBNumber is used here so that a given Printer will never give two
440 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000441 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000442
Brian Gaeked7908f62003-06-27 00:00:48 +0000443 // What's my mangled name?
444 CurrentFnName = getValueName(MF.getFunction());
445
Chris Lattnerb7089442003-01-13 00:35:03 +0000446 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000447 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000448
Brian Gaeke6559bb92002-11-14 22:32:30 +0000449 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000450 O << "\t.text\n";
451 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000452 O << "\t.globl\t" << CurrentFnName << "\n";
453 O << "\t.type\t" << CurrentFnName << ", @function\n";
454 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000455
Brian Gaeked7908f62003-06-27 00:00:48 +0000456 // Number each basic block so that we can consistently refer to them
457 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000458 NumberForBB.clear();
459 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
460 I != E; ++I) {
461 NumberForBB[I->getBasicBlock()] = BBNumber++;
462 }
463
Brian Gaeke6559bb92002-11-14 22:32:30 +0000464 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000465 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
466 I != E; ++I) {
467 // Print a label for the basic block.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000468 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
469 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000470 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
471 II != E; ++II) {
472 // Print the assembly for the instruction.
473 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000474 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000475 }
Chris Lattner0285a332002-12-28 20:25:38 +0000476 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000477
478 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000479 return false;
480}
481
Chris Lattner3d3067b2002-11-21 20:44:15 +0000482static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000483 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000484 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
485 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000486}
487
488static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000489 if (MI->getOperand(Op).isFrameIndex()) return true;
490 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000491 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000492 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
493 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000494}
495
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000496void Printer::printOp(const MachineOperand &MO,
Brian Gaeked7908f62003-06-27 00:00:48 +0000497 bool elideOffsetKeyword /* = false */) const {
Brian Gaekede420ae2003-07-23 20:25:08 +0000498 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000499 switch (MO.getType()) {
500 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000501 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000502 O << "<" << V->getName() << ">";
503 return;
504 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000505 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000506 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000507 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
508 O << RI.get(MO.getReg()).Name;
509 else
510 O << "%reg" << MO.getReg();
511 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000512
513 case MachineOperand::MO_SignExtendedImmed:
514 case MachineOperand::MO_UnextendedImmed:
515 O << (int)MO.getImmedValue();
516 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000517 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000518 {
519 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
520 assert (i != NumberForBB.end()
521 && "Could not find a BB I previously put in the NumberForBB map!");
522 O << ".BB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
523 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000524 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000525 case MachineOperand::MO_GlobalAddress:
Brian Gaekebc601fe2003-06-25 22:00:39 +0000526 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000527 return;
528 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000529 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000530 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000531 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000532 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000533 }
534}
535
Chris Lattner3501fea2003-01-14 22:00:31 +0000536static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000537 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000538 default: assert(0 && "Unknown arg size!");
539 case X86II::Arg8: return "BYTE PTR";
540 case X86II::Arg16: return "WORD PTR";
541 case X86II::Arg32: return "DWORD PTR";
542 case X86II::Arg64: return "QWORD PTR";
543 case X86II::ArgF32: return "DWORD PTR";
544 case X86II::ArgF64: return "QWORD PTR";
545 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000546 }
547}
548
Brian Gaekede420ae2003-07-23 20:25:08 +0000549void Printer::printMemReference(const MachineInstr *MI, unsigned Op) const {
550 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000551 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000552
553 if (MI->getOperand(Op).isFrameIndex()) {
554 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
555 if (MI->getOperand(Op+3).getImmedValue())
556 O << " + " << MI->getOperand(Op+3).getImmedValue();
557 O << "]";
558 return;
559 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000560 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000561 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000562 if (MI->getOperand(Op+3).getImmedValue())
563 O << " + " << MI->getOperand(Op+3).getImmedValue();
564 O << "]";
565 return;
566 }
567
Chris Lattner3d3067b2002-11-21 20:44:15 +0000568 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000569 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000570 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000571 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000572
573 O << "[";
574 bool NeedPlus = false;
575 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000576 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000577 NeedPlus = true;
578 }
579
580 if (IndexReg.getReg()) {
581 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000582 if (ScaleVal != 1)
583 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000584 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000585 NeedPlus = true;
586 }
587
Chris Lattner0285a332002-12-28 20:25:38 +0000588 if (DispVal) {
589 if (NeedPlus)
590 if (DispVal > 0)
591 O << " + ";
592 else {
593 O << " - ";
594 DispVal = -DispVal;
595 }
596 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000597 }
598 O << "]";
599}
600
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000601/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000602/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000603///
Brian Gaekede420ae2003-07-23 20:25:08 +0000604void Printer::printMachineInstruction(const MachineInstr *MI) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000605 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000606 const TargetInstrInfo &TII = TM.getInstrInfo();
607 const TargetInstrDescriptor &Desc = TII.get(Opcode);
608 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000609
Chris Lattnereca1f632002-12-25 05:09:01 +0000610 switch (Desc.TSFlags & X86II::FormMask) {
611 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000612 // Print pseudo-instructions as comments; either they should have been
613 // turned into real instructions by now, or they don't need to be
614 // seen by the assembler (e.g., IMPLICIT_USEs.)
615 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000616 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000617 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000618 O << " = phi ";
619 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
620 if (i != 1) O << ", ";
621 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000622 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000623 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000624 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000625 O << "]";
626 }
627 } else {
628 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000629 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
630 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000631 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000632 O << " = ";
633 ++i;
634 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000635 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000636
637 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
638 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000639 if (MI->getOperand(i).opIsDefOnly() ||
640 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000641 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000642 if (MI->getOperand(i).opIsDefOnly() ||
643 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000644 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000645 }
646 O << "\n";
647 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000648
Chris Lattnerf9f60882002-11-18 06:56:51 +0000649 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000650 // The accepted forms of Raw instructions are:
651 // 1. nop - No operand required
652 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000653 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000654 //
655 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000656 (MI->getNumOperands() == 1 &&
657 (MI->getOperand(0).isPCRelativeDisp() ||
658 MI->getOperand(0).isGlobalAddress() ||
659 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000660 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000661 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000662
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000663 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000664 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000665 }
666 O << "\n";
667 return;
668
Chris Lattner77875d82002-11-21 02:00:20 +0000669 case X86II::AddRegFrm: {
670 // There are currently two forms of acceptable AddRegFrm instructions.
671 // Either the instruction JUST takes a single register (like inc, dec, etc),
672 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000673 // (move immediate f.e.). Note that this immediate value might be stored as
674 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000675 // into a register. The initial register might be duplicated if this is a
676 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000677 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000678 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000679 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000680 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000681 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000682 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000683 MI->getOperand(1).isRegister() ||
684 MI->getOperand(1).isGlobalAddress() ||
685 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000686 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000687
Chris Lattner77875d82002-11-21 02:00:20 +0000688 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000689
Brian Gaeked7908f62003-06-27 00:00:48 +0000690 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000691 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 if (MI->getNumOperands() == 2 &&
693 (!MI->getOperand(1).isRegister() ||
694 MI->getOperand(1).getVRegValueOrNull() ||
695 MI->getOperand(1).isGlobalAddress() ||
696 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000697 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000698 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000699 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000700 if (Desc.TSFlags & X86II::PrintImplUses) {
701 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
702 O << ", " << RI.get(*p).Name;
703 }
704 }
Chris Lattner77875d82002-11-21 02:00:20 +0000705 O << "\n";
706 return;
707 }
Chris Lattner233ad712002-11-21 01:33:44 +0000708 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000709 // There are two acceptable forms of MRMDestReg instructions, those with 2,
710 // 3 and 4 operands:
711 //
712 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000713 //
714 // 3 Operands: in this form, the first two registers (the destination, and
715 // the first operand) should be the same, post register allocation. The 3rd
716 // operand is an additional input. This should be for things like add
717 // instructions.
718 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000719 // 4 Operands: This form is for instructions which are 3 operands forms, but
720 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000721 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000722 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000723 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000724 (MI->getNumOperands() == 2 ||
725 (isTwoAddr && MI->getOperand(1).isRegister() &&
726 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
727 (MI->getNumOperands() == 3 ||
728 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000729 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000730
Brian Gaeked7908f62003-06-27 00:00:48 +0000731 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000732 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000733 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000734 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000735 if (MI->getNumOperands() == 4) {
736 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000737 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000738 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000739 O << "\n";
740 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000741 }
Chris Lattner18042332002-11-21 21:03:39 +0000742
743 case X86II::MRMDestMem: {
744 // These instructions are the same as MRMDestReg, but instead of having a
745 // register reference for the mod/rm field, it's a memory reference.
746 //
747 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000748 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000749
Brian Gaeked7908f62003-06-27 00:00:48 +0000750 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000751 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000752 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000753 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000754 O << "\n";
755 return;
756 }
757
Chris Lattner233ad712002-11-21 01:33:44 +0000758 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000759 // There is a two forms that are acceptable for MRMSrcReg instructions,
760 // those with 3 and 2 operands:
761 //
762 // 3 Operands: in this form, the last register (the second input) is the
763 // ModR/M input. The first two operands should be the same, post register
764 // allocation. This is for things like: add r32, r/m32
765 //
766 // 2 Operands: this is for things like mov that do not read a second input
767 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000768 assert(MI->getOperand(0).isRegister() &&
769 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000770 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000771 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000772 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000773 if (MI->getNumOperands() == 3 &&
774 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
775 O << "**";
776
Brian Gaeked7908f62003-06-27 00:00:48 +0000777 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000778 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000779 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000780 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000781 O << "\n";
782 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000783 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000784
Chris Lattner3d3067b2002-11-21 20:44:15 +0000785 case X86II::MRMSrcMem: {
786 // These instructions are the same as MRMSrcReg, but instead of having a
787 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000788 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000789 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000790 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000791 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000792 isMem(MI, 2))
793 && "Bad format for MRMDestReg!");
794 if (MI->getNumOperands() == 2+4 &&
795 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
796 O << "**";
797
Brian Gaeked7908f62003-06-27 00:00:48 +0000798 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000799 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000800 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000801 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000802 O << "\n";
803 return;
804 }
805
Chris Lattner675dd2c2002-11-21 17:09:01 +0000806 case X86II::MRMS0r: case X86II::MRMS1r:
807 case X86II::MRMS2r: case X86II::MRMS3r:
808 case X86II::MRMS4r: case X86II::MRMS5r:
809 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000810 // In this form, the following are valid formats:
811 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000812 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000813 // 2. shl rdest, rinput <implicit CL or 1>
814 // 3. sbb rdest, rinput, immediate [rdest = rinput]
815 //
816 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000817 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000818 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000819 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000820 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000821 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000822 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000823 "Bad MRMSxR format!");
824
Chris Lattnerd9096832002-12-15 08:01:39 +0000825 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000826 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
827 O << "**";
828
Brian Gaeked7908f62003-06-27 00:00:48 +0000829 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000830 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000831 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000832 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000833 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000834 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000835 if (Desc.TSFlags & X86II::PrintImplUses) {
836 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
837 O << ", " << RI.get(*p).Name;
838 }
839 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000840 O << "\n";
841
842 return;
843 }
844
Chris Lattnerb7089442003-01-13 00:35:03 +0000845 case X86II::MRMS0m: case X86II::MRMS1m:
846 case X86II::MRMS2m: case X86II::MRMS3m:
847 case X86II::MRMS4m: case X86II::MRMS5m:
848 case X86II::MRMS6m: case X86II::MRMS7m: {
849 // In this form, the following are valid formats:
850 // 1. sete [m]
851 // 2. cmp [m], immediate
852 // 2. shl [m], rinput <implicit CL or 1>
853 // 3. sbb [m], immediate
854 //
855 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
856 isMem(MI, 0) && "Bad MRMSxM format!");
857 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
858 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000859 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
860 // is misassembled by gas in intel_syntax mode as its 32-bit
861 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
862 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000863 if (MI->getOpCode() == X86::FSTPr80) {
864 if ((MI->getOperand(0).getReg() == X86::ESP)
865 && (MI->getOperand(1).getImmedValue() == 1)) {
866 int DispVal = MI->getOperand(3).getImmedValue();
867 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
868 unsigned int val = (unsigned int) DispVal;
869 O << ".byte 0xdb, 0xbc, 0x24\n\t";
870 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
871 } else { // 1 byte disp.
872 unsigned char val = (unsigned char) DispVal;
873 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
874 << std::dec << "\t# ";
875 }
876 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000877 }
878 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
879 // misassembled by gas in intel_syntax mode as its 32-bit
880 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
881 // opcode bytes instead of the instruction.
882 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000883 if ((MI->getOperand(0).getReg() == X86::ESP)
884 && (MI->getOperand(1).getImmedValue() == 1)) {
885 int DispVal = MI->getOperand(3).getImmedValue();
886 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
887 unsigned int val = (unsigned int) DispVal;
888 O << ".byte 0xdb, 0xac, 0x24\n\t";
889 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
890 } else { // 1 byte disp.
891 unsigned char val = (unsigned char) DispVal;
892 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
893 << std::dec << "\t# ";
894 }
895 }
896 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000897 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
898 // invalid opcode, saying "64 bit operations are only supported in
899 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
900 // [...]", which is wrong. Workaround: Output the raw opcode bytes
901 // instead of the instruction.
902 if (MI->getOpCode() == X86::FILDr64) {
903 if ((MI->getOperand(0).getReg() == X86::ESP)
904 && (MI->getOperand(1).getImmedValue() == 1)) {
905 int DispVal = MI->getOperand(3).getImmedValue();
906 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
907 unsigned int val = (unsigned int) DispVal;
908 O << ".byte 0xdf, 0xac, 0x24\n\t";
909 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
910 } else { // 1 byte disp.
911 unsigned char val = (unsigned char) DispVal;
912 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
913 << std::dec << "\t# ";
914 }
915 }
916 }
917 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
918 // an invalid opcode, saying "64 bit operations are only
919 // supported in 64 bit modes." libopcodes disassembles it as
920 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
921 // "fistpll DWORD PTR " instead, which is what libopcodes is
922 // expecting to see.
923 if (MI->getOpCode() == X86::FISTPr64) {
924 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000925 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000926 if (MI->getNumOperands() == 5) {
927 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000928 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000929 }
930 O << "\t# ";
931 }
932
Brian Gaeked7908f62003-06-27 00:00:48 +0000933 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000934 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000935 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000936 if (MI->getNumOperands() == 5) {
937 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000938 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000939 }
940 O << "\n";
941 return;
942 }
943
Chris Lattnerf9f60882002-11-18 06:56:51 +0000944 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000945 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000946 }
Chris Lattner72614082002-10-25 22:55:53 +0000947}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000948
949bool Printer::doInitialization(Module &M)
950{
951 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
952 // with no % decorations on register names.
953 O << "\t.intel_syntax noprefix\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000954
955 // Ripped from CWriter:
956 // Calculate which global values have names that will collide when we throw
957 // away type information.
958 { // Scope to delete the FoundNames set when we are done with it...
959 std::set<std::string> FoundNames;
960 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
961 if (I->hasName()) // If the global has a name...
962 if (FoundNames.count(I->getName())) // And the name is already used
963 MangledGlobals.insert(I); // Mangle the name
964 else
965 FoundNames.insert(I->getName()); // Otherwise, keep track of name
966
967 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
968 if (I->hasName()) // If the global has a name...
969 if (FoundNames.count(I->getName())) // And the name is already used
970 MangledGlobals.insert(I); // Mangle the name
971 else
972 FoundNames.insert(I->getName()); // Otherwise, keep track of name
973 }
974
Brian Gaeke9e474c42003-06-19 19:32:32 +0000975 return false; // success
976}
977
Chris Lattnerc07736a2003-07-23 15:22:26 +0000978static const Function *isConstantFunctionPointerRef(const Constant *C) {
979 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
980 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000981 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000982 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000983}
984
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000985bool Printer::doFinalization(Module &M)
986{
Brian Gaekede420ae2003-07-23 20:25:08 +0000987 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000988 // Print out module-level global variables here.
989 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000990 std::string name(getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000991 if (I->hasInitializer()) {
992 Constant *C = I->getInitializer();
993 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000994 O << "\t.globl " << name << "\n";
995 O << "\t.type " << name << ",@object\n";
996 O << "\t.size " << name << ","
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000997 << (unsigned)TD.getTypeSize(I->getType()) << "\n";
998 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000999 O << name << ":\t\t\t\t\t#";
1000 // If this is a constant function pointer, we only print out the
1001 // name of the function in the comment (because printing the
1002 // function means calling AsmWriter to print the whole LLVM
1003 // assembly, which would corrupt the X86 assembly output.)
1004 // Otherwise we print out the whole llvm value as a comment.
1005 if (const Function *F = isConstantFunctionPointerRef (C)) {
1006 O << " %" << F->getName() << "()\n";
1007 } else {
1008 O << *C << "\n";
1009 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001010 printConstantValueOnly (C);
1011 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +00001012 O << "\t.globl " << name << "\n";
1013 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001014 << (unsigned)TD.getTypeSize(I->getType()) << ", "
1015 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001016 }
1017 }
Brian Gaekebc601fe2003-06-25 22:00:39 +00001018 MangledGlobals.clear();
Brian Gaeke9e474c42003-06-19 19:32:32 +00001019 return false; // success
1020}