blob: 541af61fd961d9acfb50d439d0b5d8056c49c66c [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 Gaeke92bdfe62003-07-23 18:37:06 +000036 /// Output stream on which we're printing assembly code. This is
37 /// assigned by the constructor and never changes.
38 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000039 std::ostream &O;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000040 Printer(std::ostream &o) : O(o) { }
41
42 /// We name each basic block in a Function with a unique number, so
43 /// that we can consistently refer to them later. This is cleared
44 /// at the beginning of each call to runOnMachineFunction().
45 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000046 typedef std::map<const Value *, unsigned> ValueMapTy;
47 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000048
49 /// Cache of mangled name for current function. This is
50 /// recalculated at the beginning of each call to
51 /// runOnMachineFunction().
52 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000053 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000054
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000055 virtual const char *getPassName() const {
56 return "X86 Assembly Printer";
57 }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000058 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.addRequired<TargetData>();
60 }
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000061
Brian Gaeke92bdfe62003-07-23 18:37:06 +000062 void printMachineInstruction(const MachineInstr *MI,
Brian Gaeked7908f62003-06-27 00:00:48 +000063 const TargetMachine &TM) const;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000064 void printOp(const MachineOperand &MO,
Brian Gaeked7908f62003-06-27 00:00:48 +000065 const MRegisterInfo &RI, bool elideOffsetKeyword = false) const;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000066 void printMemReference(const MachineInstr *MI,
Brian Gaeked7908f62003-06-27 00:00:48 +000067 unsigned Op,
68 const MRegisterInfo &RI) const;
Brian Gaeke01d79ff2003-06-25 18:01:07 +000069 void printConstantPool(MachineConstantPool *MCP);
70 bool runOnMachineFunction(MachineFunction &F);
71 std::string ConstantExprToString(const ConstantExpr* CE);
72 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000073 bool doInitialization(Module &M);
74 bool doFinalization(Module &M);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000075 void PrintZeroBytesToPad(int numBytes);
76 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
77 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 };
Brian Gaeked7908f62003-06-27 00:00:48 +000079} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000080
Brian Gaeke92bdfe62003-07-23 18:37:06 +000081/// createX86CodePrinterPass - Returns a pass that prints the X86
82/// assembly code for a MachineFunction to the specified stream. This
83/// should work regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000084///
Chris Lattner0285a332002-12-28 20:25:38 +000085Pass *createX86CodePrinterPass(std::ostream &O) {
86 return new Printer(O);
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 Gaeke92bdfe62003-07-23 18:37:06 +0000169 TargetData &TD = getAnalysis<TargetData>();
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 Gaeke92bdfe62003-07-23 18:37:06 +0000362 TargetData &TD = getAnalysis<TargetData>();
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 Gaeke92bdfe62003-07-23 18:37:06 +0000421 TargetData &TD = getAnalysis<TargetData>();
422
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;
442 const TargetMachine &TM = MF.getTarget();
Brian Gaeke6559bb92002-11-14 22:32:30 +0000443
Brian Gaeked7908f62003-06-27 00:00:48 +0000444 // What's my mangled name?
445 CurrentFnName = getValueName(MF.getFunction());
446
Chris Lattnerb7089442003-01-13 00:35:03 +0000447 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000448 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000449
Brian Gaeke6559bb92002-11-14 22:32:30 +0000450 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000451 O << "\t.text\n";
452 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000453 O << "\t.globl\t" << CurrentFnName << "\n";
454 O << "\t.type\t" << CurrentFnName << ", @function\n";
455 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000456
Brian Gaeked7908f62003-06-27 00:00:48 +0000457 // Number each basic block so that we can consistently refer to them
458 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000459 NumberForBB.clear();
460 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
461 I != E; ++I) {
462 NumberForBB[I->getBasicBlock()] = BBNumber++;
463 }
464
Brian Gaeke6559bb92002-11-14 22:32:30 +0000465 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000466 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
467 I != E; ++I) {
468 // Print a label for the basic block.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000469 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
470 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000471 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
472 II != E; ++II) {
473 // Print the assembly for the instruction.
474 O << "\t";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000475 printMachineInstruction(*II, TM);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000476 }
Chris Lattner0285a332002-12-28 20:25:38 +0000477 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000478
479 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000480 return false;
481}
482
Chris Lattner3d3067b2002-11-21 20:44:15 +0000483static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000484 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000485 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
486 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000487}
488
489static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000490 if (MI->getOperand(Op).isFrameIndex()) return true;
491 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000492 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000493 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
494 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000495}
496
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000497void Printer::printOp(const MachineOperand &MO,
Brian Gaeked7908f62003-06-27 00:00:48 +0000498 const MRegisterInfo &RI,
499 bool elideOffsetKeyword /* = false */) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000500 switch (MO.getType()) {
501 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000502 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000503 O << "<" << V->getName() << ">";
504 return;
505 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000506 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000507 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000508 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
509 O << RI.get(MO.getReg()).Name;
510 else
511 O << "%reg" << MO.getReg();
512 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000513
514 case MachineOperand::MO_SignExtendedImmed:
515 case MachineOperand::MO_UnextendedImmed:
516 O << (int)MO.getImmedValue();
517 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000518 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000519 {
520 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
521 assert (i != NumberForBB.end()
522 && "Could not find a BB I previously put in the NumberForBB map!");
523 O << ".BB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
524 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000525 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000526 case MachineOperand::MO_GlobalAddress:
Brian Gaekebc601fe2003-06-25 22:00:39 +0000527 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000528 return;
529 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000530 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000531 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000532 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000533 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000534 }
535}
536
Chris Lattner3501fea2003-01-14 22:00:31 +0000537static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000538 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000539 default: assert(0 && "Unknown arg size!");
540 case X86II::Arg8: return "BYTE PTR";
541 case X86II::Arg16: return "WORD PTR";
542 case X86II::Arg32: return "DWORD PTR";
543 case X86II::Arg64: return "QWORD PTR";
544 case X86II::ArgF32: return "DWORD PTR";
545 case X86II::ArgF64: return "QWORD PTR";
546 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000547 }
548}
549
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000550void Printer::printMemReference(const MachineInstr *MI,
Brian Gaeked7908f62003-06-27 00:00:48 +0000551 unsigned Op,
552 const MRegisterInfo &RI) const {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000553 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000554
555 if (MI->getOperand(Op).isFrameIndex()) {
556 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
557 if (MI->getOperand(Op+3).getImmedValue())
558 O << " + " << MI->getOperand(Op+3).getImmedValue();
559 O << "]";
560 return;
561 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000562 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000563 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000564 if (MI->getOperand(Op+3).getImmedValue())
565 O << " + " << MI->getOperand(Op+3).getImmedValue();
566 O << "]";
567 return;
568 }
569
Chris Lattner3d3067b2002-11-21 20:44:15 +0000570 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000571 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000572 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000573 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000574
575 O << "[";
576 bool NeedPlus = false;
577 if (BaseReg.getReg()) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000578 printOp(BaseReg, RI);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000579 NeedPlus = true;
580 }
581
582 if (IndexReg.getReg()) {
583 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000584 if (ScaleVal != 1)
585 O << ScaleVal << "*";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000586 printOp(IndexReg, RI);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000587 NeedPlus = true;
588 }
589
Chris Lattner0285a332002-12-28 20:25:38 +0000590 if (DispVal) {
591 if (NeedPlus)
592 if (DispVal > 0)
593 O << " + ";
594 else {
595 O << " - ";
596 DispVal = -DispVal;
597 }
598 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000599 }
600 O << "]";
601}
602
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000603/// printMachineInstruction -- Print out a single X86 LLVM instruction
604/// MI in Intel syntax to the current output stream, using the given
605/// TargetMachine.
Brian Gaeked7908f62003-06-27 00:00:48 +0000606///
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000607void Printer::printMachineInstruction(const MachineInstr *MI,
Brian Gaeked7908f62003-06-27 00:00:48 +0000608 const TargetMachine &TM) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000609 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000610 const TargetInstrInfo &TII = TM.getInstrInfo();
611 const TargetInstrDescriptor &Desc = TII.get(Opcode);
612 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000613
Chris Lattnereca1f632002-12-25 05:09:01 +0000614 switch (Desc.TSFlags & X86II::FormMask) {
615 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000616 // Print pseudo-instructions as comments; either they should have been
617 // turned into real instructions by now, or they don't need to be
618 // seen by the assembler (e.g., IMPLICIT_USEs.)
619 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000620 if (Opcode == X86::PHI) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000621 printOp(MI->getOperand(0), RI);
Chris Lattnereca1f632002-12-25 05:09:01 +0000622 O << " = phi ";
623 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
624 if (i != 1) O << ", ";
625 O << "[";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000626 printOp(MI->getOperand(i), RI);
Chris Lattnereca1f632002-12-25 05:09:01 +0000627 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000628 printOp(MI->getOperand(i+1), RI);
Chris Lattnereca1f632002-12-25 05:09:01 +0000629 O << "]";
630 }
631 } else {
632 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000633 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
634 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000635 printOp(MI->getOperand(0), RI);
Chris Lattnereca1f632002-12-25 05:09:01 +0000636 O << " = ";
637 ++i;
638 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000639 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000640
641 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
642 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000643 if (MI->getOperand(i).opIsDefOnly() ||
644 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000645 printOp(MI->getOperand(i), RI);
Vikram S. Adve49cab032003-05-27 00:03:17 +0000646 if (MI->getOperand(i).opIsDefOnly() ||
647 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000648 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000649 }
650 O << "\n";
651 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000652
Chris Lattnerf9f60882002-11-18 06:56:51 +0000653 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000654 // The accepted forms of Raw instructions are:
655 // 1. nop - No operand required
656 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000657 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000658 //
659 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000660 (MI->getNumOperands() == 1 &&
661 (MI->getOperand(0).isPCRelativeDisp() ||
662 MI->getOperand(0).isGlobalAddress() ||
663 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000664 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000665 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000666
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000667 if (MI->getNumOperands() == 1) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000668 printOp(MI->getOperand(0), RI, true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000669 }
670 O << "\n";
671 return;
672
Chris Lattner77875d82002-11-21 02:00:20 +0000673 case X86II::AddRegFrm: {
674 // There are currently two forms of acceptable AddRegFrm instructions.
675 // Either the instruction JUST takes a single register (like inc, dec, etc),
676 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000677 // (move immediate f.e.). Note that this immediate value might be stored as
678 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000679 // into a register. The initial register might be duplicated if this is a
680 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000681 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000682 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000683 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000684 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000685 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000686 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000687 MI->getOperand(1).isRegister() ||
688 MI->getOperand(1).isGlobalAddress() ||
689 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000690 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000691
Chris Lattner77875d82002-11-21 02:00:20 +0000692 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000693
Brian Gaeked7908f62003-06-27 00:00:48 +0000694 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000695 printOp(MI->getOperand(0), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000696 if (MI->getNumOperands() == 2 &&
697 (!MI->getOperand(1).isRegister() ||
698 MI->getOperand(1).getVRegValueOrNull() ||
699 MI->getOperand(1).isGlobalAddress() ||
700 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000701 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000702 printOp(MI->getOperand(1), RI);
Chris Lattner77875d82002-11-21 02:00:20 +0000703 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000704 if (Desc.TSFlags & X86II::PrintImplUses) {
705 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
706 O << ", " << RI.get(*p).Name;
707 }
708 }
Chris Lattner77875d82002-11-21 02:00:20 +0000709 O << "\n";
710 return;
711 }
Chris Lattner233ad712002-11-21 01:33:44 +0000712 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000713 // There are two acceptable forms of MRMDestReg instructions, those with 2,
714 // 3 and 4 operands:
715 //
716 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000717 //
718 // 3 Operands: in this form, the first two registers (the destination, and
719 // the first operand) should be the same, post register allocation. The 3rd
720 // operand is an additional input. This should be for things like add
721 // instructions.
722 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000723 // 4 Operands: This form is for instructions which are 3 operands forms, but
724 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000725 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000726 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000727 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000728 (MI->getNumOperands() == 2 ||
729 (isTwoAddr && MI->getOperand(1).isRegister() &&
730 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
731 (MI->getNumOperands() == 3 ||
732 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000733 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000734
Brian Gaeked7908f62003-06-27 00:00:48 +0000735 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000736 printOp(MI->getOperand(0), RI);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000737 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000738 printOp(MI->getOperand(1+isTwoAddr), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000739 if (MI->getNumOperands() == 4) {
740 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000741 printOp(MI->getOperand(3), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000742 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000743 O << "\n";
744 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000745 }
Chris Lattner18042332002-11-21 21:03:39 +0000746
747 case X86II::MRMDestMem: {
748 // These instructions are the same as MRMDestReg, but instead of having a
749 // register reference for the mod/rm field, it's a memory reference.
750 //
751 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000752 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000753
Brian Gaeked7908f62003-06-27 00:00:48 +0000754 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000755 printMemReference(MI, 0, RI);
Chris Lattner18042332002-11-21 21:03:39 +0000756 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000757 printOp(MI->getOperand(4), RI);
Chris Lattner18042332002-11-21 21:03:39 +0000758 O << "\n";
759 return;
760 }
761
Chris Lattner233ad712002-11-21 01:33:44 +0000762 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000763 // There is a two forms that are acceptable for MRMSrcReg instructions,
764 // those with 3 and 2 operands:
765 //
766 // 3 Operands: in this form, the last register (the second input) is the
767 // ModR/M input. The first two operands should be the same, post register
768 // allocation. This is for things like: add r32, r/m32
769 //
770 // 2 Operands: this is for things like mov that do not read a second input
771 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000772 assert(MI->getOperand(0).isRegister() &&
773 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000774 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000775 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000776 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000777 if (MI->getNumOperands() == 3 &&
778 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
779 O << "**";
780
Brian Gaeked7908f62003-06-27 00:00:48 +0000781 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000782 printOp(MI->getOperand(0), RI);
Chris Lattner644e1ab2002-11-21 00:30:01 +0000783 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000784 printOp(MI->getOperand(MI->getNumOperands()-1), RI);
Chris Lattner644e1ab2002-11-21 00:30:01 +0000785 O << "\n";
786 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000787 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000788
Chris Lattner3d3067b2002-11-21 20:44:15 +0000789 case X86II::MRMSrcMem: {
790 // These instructions are the same as MRMSrcReg, but instead of having a
791 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000792 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000793 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000794 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000795 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000796 isMem(MI, 2))
797 && "Bad format for MRMDestReg!");
798 if (MI->getNumOperands() == 2+4 &&
799 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
800 O << "**";
801
Brian Gaeked7908f62003-06-27 00:00:48 +0000802 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000803 printOp(MI->getOperand(0), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000804 O << ", " << sizePtr(Desc) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000805 printMemReference(MI, MI->getNumOperands()-4, RI);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000806 O << "\n";
807 return;
808 }
809
Chris Lattner675dd2c2002-11-21 17:09:01 +0000810 case X86II::MRMS0r: case X86II::MRMS1r:
811 case X86II::MRMS2r: case X86II::MRMS3r:
812 case X86II::MRMS4r: case X86II::MRMS5r:
813 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000814 // In this form, the following are valid formats:
815 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000816 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000817 // 2. shl rdest, rinput <implicit CL or 1>
818 // 3. sbb rdest, rinput, immediate [rdest = rinput]
819 //
820 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000821 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000822 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000823 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000824 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000825 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000826 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000827 "Bad MRMSxR format!");
828
Chris Lattnerd9096832002-12-15 08:01:39 +0000829 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000830 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
831 O << "**";
832
Brian Gaeked7908f62003-06-27 00:00:48 +0000833 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000834 printOp(MI->getOperand(0), RI);
Chris Lattnerd9096832002-12-15 08:01:39 +0000835 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000836 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000837 printOp(MI->getOperand(MI->getNumOperands()-1), RI);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000838 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000839 if (Desc.TSFlags & X86II::PrintImplUses) {
840 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
841 O << ", " << RI.get(*p).Name;
842 }
843 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000844 O << "\n";
845
846 return;
847 }
848
Chris Lattnerb7089442003-01-13 00:35:03 +0000849 case X86II::MRMS0m: case X86II::MRMS1m:
850 case X86II::MRMS2m: case X86II::MRMS3m:
851 case X86II::MRMS4m: case X86II::MRMS5m:
852 case X86II::MRMS6m: case X86II::MRMS7m: {
853 // In this form, the following are valid formats:
854 // 1. sete [m]
855 // 2. cmp [m], immediate
856 // 2. shl [m], rinput <implicit CL or 1>
857 // 3. sbb [m], immediate
858 //
859 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
860 isMem(MI, 0) && "Bad MRMSxM format!");
861 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
862 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000863 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
864 // is misassembled by gas in intel_syntax mode as its 32-bit
865 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
866 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000867 if (MI->getOpCode() == X86::FSTPr80) {
868 if ((MI->getOperand(0).getReg() == X86::ESP)
869 && (MI->getOperand(1).getImmedValue() == 1)) {
870 int DispVal = MI->getOperand(3).getImmedValue();
871 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
872 unsigned int val = (unsigned int) DispVal;
873 O << ".byte 0xdb, 0xbc, 0x24\n\t";
874 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
875 } else { // 1 byte disp.
876 unsigned char val = (unsigned char) DispVal;
877 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
878 << std::dec << "\t# ";
879 }
880 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000881 }
882 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
883 // misassembled by gas in intel_syntax mode as its 32-bit
884 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
885 // opcode bytes instead of the instruction.
886 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000887 if ((MI->getOperand(0).getReg() == X86::ESP)
888 && (MI->getOperand(1).getImmedValue() == 1)) {
889 int DispVal = MI->getOperand(3).getImmedValue();
890 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
891 unsigned int val = (unsigned int) DispVal;
892 O << ".byte 0xdb, 0xac, 0x24\n\t";
893 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
894 } else { // 1 byte disp.
895 unsigned char val = (unsigned char) DispVal;
896 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
897 << std::dec << "\t# ";
898 }
899 }
900 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000901 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
902 // invalid opcode, saying "64 bit operations are only supported in
903 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
904 // [...]", which is wrong. Workaround: Output the raw opcode bytes
905 // instead of the instruction.
906 if (MI->getOpCode() == X86::FILDr64) {
907 if ((MI->getOperand(0).getReg() == X86::ESP)
908 && (MI->getOperand(1).getImmedValue() == 1)) {
909 int DispVal = MI->getOperand(3).getImmedValue();
910 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
911 unsigned int val = (unsigned int) DispVal;
912 O << ".byte 0xdf, 0xac, 0x24\n\t";
913 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
914 } else { // 1 byte disp.
915 unsigned char val = (unsigned char) DispVal;
916 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
917 << std::dec << "\t# ";
918 }
919 }
920 }
921 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
922 // an invalid opcode, saying "64 bit operations are only
923 // supported in 64 bit modes." libopcodes disassembles it as
924 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
925 // "fistpll DWORD PTR " instead, which is what libopcodes is
926 // expecting to see.
927 if (MI->getOpCode() == X86::FISTPr64) {
928 O << "fistpll DWORD PTR ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000929 printMemReference(MI, 0, RI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000930 if (MI->getNumOperands() == 5) {
931 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000932 printOp(MI->getOperand(4), RI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000933 }
934 O << "\t# ";
935 }
936
Brian Gaeked7908f62003-06-27 00:00:48 +0000937 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000938 O << sizePtr(Desc) << " ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000939 printMemReference(MI, 0, RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000940 if (MI->getNumOperands() == 5) {
941 O << ", ";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000942 printOp(MI->getOperand(4), RI);
Chris Lattnerb7089442003-01-13 00:35:03 +0000943 }
944 O << "\n";
945 return;
946 }
947
Chris Lattnerf9f60882002-11-18 06:56:51 +0000948 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000949 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000950 }
Chris Lattner72614082002-10-25 22:55:53 +0000951}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000952
953bool Printer::doInitialization(Module &M)
954{
955 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
956 // with no % decorations on register names.
957 O << "\t.intel_syntax noprefix\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000958
959 // Ripped from CWriter:
960 // Calculate which global values have names that will collide when we throw
961 // away type information.
962 { // Scope to delete the FoundNames set when we are done with it...
963 std::set<std::string> FoundNames;
964 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
965 if (I->hasName()) // If the global has a name...
966 if (FoundNames.count(I->getName())) // And the name is already used
967 MangledGlobals.insert(I); // Mangle the name
968 else
969 FoundNames.insert(I->getName()); // Otherwise, keep track of name
970
971 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
972 if (I->hasName()) // If the global has a name...
973 if (FoundNames.count(I->getName())) // And the name is already used
974 MangledGlobals.insert(I); // Mangle the name
975 else
976 FoundNames.insert(I->getName()); // Otherwise, keep track of name
977 }
978
Brian Gaeke9e474c42003-06-19 19:32:32 +0000979 return false; // success
980}
981
Chris Lattnerc07736a2003-07-23 15:22:26 +0000982static const Function *isConstantFunctionPointerRef(const Constant *C) {
983 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
984 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000985 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000986 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000987}
988
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000989bool Printer::doFinalization(Module &M)
990{
991 TargetData &TD = getAnalysis<TargetData>();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000992 // Print out module-level global variables here.
993 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000994 std::string name(getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000995 if (I->hasInitializer()) {
996 Constant *C = I->getInitializer();
997 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000998 O << "\t.globl " << name << "\n";
999 O << "\t.type " << name << ",@object\n";
1000 O << "\t.size " << name << ","
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001001 << (unsigned)TD.getTypeSize(I->getType()) << "\n";
1002 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001003 O << name << ":\t\t\t\t\t#";
1004 // If this is a constant function pointer, we only print out the
1005 // name of the function in the comment (because printing the
1006 // function means calling AsmWriter to print the whole LLVM
1007 // assembly, which would corrupt the X86 assembly output.)
1008 // Otherwise we print out the whole llvm value as a comment.
1009 if (const Function *F = isConstantFunctionPointerRef (C)) {
1010 O << " %" << F->getName() << "()\n";
1011 } else {
1012 O << *C << "\n";
1013 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001014 printConstantValueOnly (C);
1015 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +00001016 O << "\t.globl " << name << "\n";
1017 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001018 << (unsigned)TD.getTypeSize(I->getType()) << ", "
1019 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001020 }
1021 }
Brian Gaekebc601fe2003-06-25 22:00:39 +00001022 MangledGlobals.clear();
Brian Gaeke9e474c42003-06-19 19:32:32 +00001023 return false; // success
1024}