blob: 75cc6e9e6fe09b0799deed6fef784155312ca18b [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
Chris Lattner72614082002-10-25 22:55:53 +00002//
Brian Gaeke92bdfe62003-07-23 18:37:06 +00003// This file contains a printer that converts from our internal
4// representation of machine-dependent LLVM code to Intel-format
5// assembly language. This printer is the output mechanism used
6// by `llc' and `lli -printmachineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "X86InstrInfo.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000012#include "llvm/Function.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000013#include "llvm/Constant.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000014#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000016#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000017#include "llvm/CodeGen/MachineInstr.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000018#include "llvm/Type.h"
19#include "llvm/Constants.h"
20#include "llvm/Assembly/Writer.h"
21#include "llvm/DerivedTypes.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000022#include "Support/StringExtras.h"
23#include "llvm/Module.h"
Chris Lattner72614082002-10-25 22:55:53 +000024
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000025namespace {
Brian Gaeke92bdfe62003-07-23 18:37:06 +000026 /// This is properly part of the name mangler; it keeps track of
27 /// which global values have had their names mangled. It is cleared
28 /// at the end of every module by doFinalization().
29 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000030 std::set<const Value *> MangledGlobals;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000031
Chris Lattner0285a332002-12-28 20:25:38 +000032 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000033 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000034 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000036
37 /// Target machine description which we query for reg. names, data
38 /// layout, etc.
39 ///
40 TargetMachine &TM;
41
42 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000043
44 /// We name each basic block in a Function with a unique number, so
45 /// that we can consistently refer to them later. This is cleared
46 /// at the beginning of each call to runOnMachineFunction().
47 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000048 typedef std::map<const Value *, unsigned> ValueMapTy;
49 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000050
51 /// Cache of mangled name for current function. This is
52 /// recalculated at the beginning of each call to
53 /// runOnMachineFunction().
54 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000055 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000056
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000057 virtual const char *getPassName() const {
58 return "X86 Assembly Printer";
59 }
60
Brian Gaekede420ae2003-07-23 20:25:08 +000061 void printMachineInstruction(const MachineInstr *MI) const;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000062 void printOp(const MachineOperand &MO,
Brian Gaekede420ae2003-07-23 20:25:08 +000063 bool elideOffsetKeyword = false) const;
64 void printMemReference(const MachineInstr *MI, unsigned Op) const;
Brian Gaekeb3011a02003-07-24 17:30:45 +000065 void printConstantPool(MachineConstantPool *MCP) const;
Brian Gaeke01d79ff2003-06-25 18:01:07 +000066 bool runOnMachineFunction(MachineFunction &F);
Brian Gaekeb3011a02003-07-24 17:30:45 +000067 std::string ConstantExprToString(const ConstantExpr* CE) const;
68 std::string valToExprString(const Value* V) const;
Brian Gaeke9e474c42003-06-19 19:32:32 +000069 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
Brian Gaekeb3011a02003-07-24 17:30:45 +000071 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0) const;
72 void printSingleConstantValue(const Constant* CV) const;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000073 };
Brian Gaeked7908f62003-06-27 00:00:48 +000074} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000075
Brian Gaeke92bdfe62003-07-23 18:37:06 +000076/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000077/// assembly code for a MachineFunction to the given output stream,
78/// using the given target machine description. This should work
79/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000080///
Brian Gaekede420ae2003-07-23 20:25:08 +000081Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
82 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000083}
84
Brian Gaeke92bdfe62003-07-23 18:37:06 +000085/// makeNameProper - We don't want identifier names with ., space, or
86/// - in them, so we mangle these characters into the strings "d_",
87/// "s_", and "D_", respectively.
88///
Brian Gaekebc601fe2003-06-25 22:00:39 +000089static std::string makeNameProper(std::string x) {
90 std::string tmp;
91 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
92 switch (*sI) {
93 case '.': tmp += "d_"; break;
94 case ' ': tmp += "s_"; break;
95 case '-': tmp += "D_"; break;
96 default: tmp += *sI;
97 }
Brian Gaekebc601fe2003-06-25 22:00:39 +000098 return tmp;
99}
100
Brian Gaeked7908f62003-06-27 00:00:48 +0000101static std::string getValueName(const Value *V) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000102 if (V->hasName()) { // Print out the label if it exists...
Brian Gaekebc601fe2003-06-25 22:00:39 +0000103 // Name mangling occurs as follows:
104 // - If V is not a global, mangling always occurs.
105 // - Otherwise, mangling occurs when any of the following are true:
106 // 1) V has internal linkage
107 // 2) V's name would collide if it is not mangled.
108 //
Brian Gaekebc601fe2003-06-25 22:00:39 +0000109 if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
110 if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
111 // No internal linkage, name will not collide -> no mangling.
112 return makeNameProper(gv->getName());
113 }
114 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000115 // Non-global, or global with internal linkage / colliding name -> mangle.
116 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
117 makeNameProper(V->getName());
118 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000119 static int Count = 0;
120 Count++;
121 return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
122}
123
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000124/// valToExprString - Helper function for ConstantExprToString().
125/// Appends result to argument string S.
126///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000127std::string Printer::valToExprString(const Value* V) const {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000128 std::string S;
129 bool failed = false;
130 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
131 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
132 S += std::string(CB == ConstantBool::True ? "1" : "0");
133 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
134 S += itostr(CI->getValue());
135 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
136 S += utostr(CI->getValue());
137 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
138 S += ftostr(CFP->getValue());
139 else if (isa<ConstantPointerNull>(CV))
140 S += "0";
141 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
142 S += valToExprString(CPR->getValue());
143 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
144 S += ConstantExprToString(CE);
145 else
146 failed = true;
147 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000148 S += getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000149 }
150 else
151 failed = true;
152
153 if (failed) {
154 assert(0 && "Cannot convert value to string");
155 S += "<illegal-value>";
156 }
157 return S;
158}
159
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000160/// ConstantExprToString - Convert a ConstantExpr to an asm expression
161/// and return this as a string.
162///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000163std::string Printer::ConstantExprToString(const ConstantExpr* CE) const {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000164 std::string S;
Brian Gaekede420ae2003-07-23 20:25:08 +0000165 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166 switch(CE->getOpcode()) {
167 case Instruction::GetElementPtr:
168 { // generate a symbolic expression for the byte address
169 const Value* ptrVal = CE->getOperand(0);
170 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
171 S += "(" + valToExprString(ptrVal) + ") + ("
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000172 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000173 break;
174 }
175
176 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000177 // Support only non-converting or widening casts for now, that is,
178 // ones that do not involve a change in value. This assertion is
179 // not a complete check.
180 {
181 Constant *Op = CE->getOperand(0);
182 const Type *OpTy = Op->getType(), *Ty = CE->getType();
183 assert(((isa<PointerType>(OpTy)
184 && (Ty == Type::LongTy || Ty == Type::ULongTy))
185 || (isa<PointerType>(Ty)
186 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
187 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
188 && (OpTy-> isLosslesslyConvertibleTo(Ty))))
189 && "FIXME: Don't yet support this kind of constant cast expr");
190 S += "(" + valToExprString(Op) + ")";
191 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000192 break;
193
194 case Instruction::Add:
195 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
196 + valToExprString(CE->getOperand(1)) + ")";
197 break;
198
199 default:
200 assert(0 && "Unsupported operator in ConstantExprToString()");
201 break;
202 }
203
204 return S;
205}
206
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000207/// printSingleConstantValue - Print a single constant value.
208///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000209void
Brian Gaekeb3011a02003-07-24 17:30:45 +0000210Printer::printSingleConstantValue(const Constant* CV) const
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000211{
212 assert(CV->getType() != Type::VoidTy &&
213 CV->getType() != Type::TypeTy &&
214 CV->getType() != Type::LabelTy &&
215 "Unexpected type for Constant");
216
217 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
218 && "Aggregate types should be handled outside this function");
219
220 const Type *type = CV->getType();
221 O << "\t";
222 switch(type->getPrimitiveID())
223 {
224 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
225 O << ".byte";
226 break;
227 case Type::UShortTyID: case Type::ShortTyID:
228 O << ".word";
229 break;
230 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
231 O << ".long";
232 break;
233 case Type::ULongTyID: case Type::LongTyID:
234 O << ".quad";
235 break;
236 case Type::FloatTyID:
237 O << ".long";
238 break;
239 case Type::DoubleTyID:
240 O << ".quad";
241 break;
242 case Type::ArrayTyID:
243 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
244 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
245 O << ".string";
246 else
247 assert (0 && "Can't handle printing this type of array");
248 break;
249 default:
250 assert (0 && "Can't handle printing this type of thing");
251 break;
252 }
253 O << "\t";
254
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000255 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
256 {
257 // Constant expression built from operators, constants, and
258 // symbolic addrs
259 O << ConstantExprToString(CE) << "\n";
260 }
261 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000262 {
263 if (type->isFloatingPoint()) {
264 // FP Constants are printed as integer constants to avoid losing
265 // precision...
266 double Val = cast<ConstantFP>(CV)->getValue();
267 if (type == Type::FloatTy) {
268 float FVal = (float)Val;
269 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
270 O << *(unsigned int*)ProxyPtr;
271 } else if (type == Type::DoubleTy) {
272 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
273 O << *(uint64_t*)ProxyPtr;
274 } else {
275 assert(0 && "Unknown floating point type!");
276 }
277
278 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
279 } else {
280 WriteAsOperand(O, CV, false, false) << "\n";
281 }
282 }
283 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
284 {
285 // This is a constant address for a global variable or method.
286 // Use the name of the variable or method as the address value.
Brian Gaekebc601fe2003-06-25 22:00:39 +0000287 O << getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000288 }
289 else if (isa<ConstantPointerNull>(CV))
290 {
291 // Null pointer value
292 O << "0\n";
293 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000294 else
295 {
296 assert(0 && "Unknown elementary type for constant");
297 }
298}
299
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000300/// isStringCompatible - Can we treat the specified array as a string?
301/// Only if it is an array of ubytes or non-negative sbytes.
302///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000303static bool isStringCompatible(const ConstantArray *CVA) {
304 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
305 if (ETy == Type::UByteTy) return true;
306 if (ETy != Type::SByteTy) return false;
307
308 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
309 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
310 return false;
311
312 return true;
313}
314
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000315/// toOctal - Convert the low order bits of X into an octal digit.
316///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000317static inline char toOctal(int X) {
318 return (X&7)+'0';
319}
320
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000321/// getAsCString - Return the specified array as a C compatible
322/// string, only if the predicate isStringCompatible is true.
323///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000324static std::string getAsCString(const ConstantArray *CVA) {
325 assert(isStringCompatible(CVA) && "Array is not string compatible!");
326
327 std::string Result;
328 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
329 Result = "\"";
330 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000331 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000332
333 if (C == '"') {
334 Result += "\\\"";
335 } else if (C == '\\') {
336 Result += "\\\\";
337 } else if (isprint(C)) {
338 Result += C;
339 } else {
340 switch(C) {
341 case '\a': Result += "\\a"; break;
342 case '\b': Result += "\\b"; break;
343 case '\f': Result += "\\f"; break;
344 case '\n': Result += "\\n"; break;
345 case '\r': Result += "\\r"; break;
346 case '\t': Result += "\\t"; break;
347 case '\v': Result += "\\v"; break;
348 default:
349 Result += '\\';
350 Result += toOctal(C >> 6);
351 Result += toOctal(C >> 3);
352 Result += toOctal(C >> 0);
353 break;
354 }
355 }
356 }
357 Result += "\"";
358 return Result;
359}
360
361// Print a constant value or values (it may be an aggregate).
362// Uses printSingleConstantValue() to print each individual value.
363void
364Printer::printConstantValueOnly(const Constant* CV,
Brian Gaekeb3011a02003-07-24 17:30:45 +0000365 int numPadBytesAfter /* = 0 */) const
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000366{
367 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000368 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000369
370 if (CVA && isStringCompatible(CVA))
371 { // print the string alone and return
372 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
373 }
374 else if (CVA)
375 { // Not a string. Print the values in successive locations
376 const std::vector<Use> &constValues = CVA->getValues();
377 for (unsigned i=0; i < constValues.size(); i++)
378 printConstantValueOnly(cast<Constant>(constValues[i].get()));
379 }
380 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
381 { // Print the fields in successive locations. Pad to align if needed!
382 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000383 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000384 const std::vector<Use>& constValues = CVS->getValues();
385 unsigned sizeSoFar = 0;
386 for (unsigned i=0, N = constValues.size(); i < N; i++)
387 {
388 const Constant* field = cast<Constant>(constValues[i].get());
389
390 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000391 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000392 int padSize = ((i == N-1? cvsLayout->StructSize
393 : cvsLayout->MemberOffsets[i+1])
394 - cvsLayout->MemberOffsets[i]) - fieldSize;
395 sizeSoFar += (fieldSize + padSize);
396
397 // Now print the actual field value
398 printConstantValueOnly(field, padSize);
399 }
400 assert(sizeSoFar == cvsLayout->StructSize &&
401 "Layout of constant struct may be incorrect!");
402 }
403 else
404 printSingleConstantValue(CV);
405
406 if (numPadBytesAfter) {
407 unsigned numBytes = numPadBytesAfter;
408 for ( ; numBytes >= 8; numBytes -= 8)
409 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
410 if (numBytes >= 4)
411 {
412 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
413 numBytes -= 4;
414 }
415 while (numBytes--)
416 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
417 }
418}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000419
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000420/// printConstantPool - Print to the current output stream assembly
421/// representations of the constants in the constant pool MCP. This is
422/// used to print out constants which have been "spilled to memory" by
423/// the code generator.
424///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000425void Printer::printConstantPool(MachineConstantPool *MCP) const {
Chris Lattnerb7089442003-01-13 00:35:03 +0000426 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000427 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000428
Chris Lattnerb7089442003-01-13 00:35:03 +0000429 if (CP.empty()) return;
430
431 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
432 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000433 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000434 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000435 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
436 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000437 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000438 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000439}
440
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000441/// runOnMachineFunction - This uses the printMachineInstruction()
442/// method to print assembly for each instruction.
443///
Chris Lattner0285a332002-12-28 20:25:38 +0000444bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000445 // BBNumber is used here so that a given Printer will never give two
446 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000447 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000448
Brian Gaeked7908f62003-06-27 00:00:48 +0000449 // What's my mangled name?
450 CurrentFnName = getValueName(MF.getFunction());
451
Chris Lattnerb7089442003-01-13 00:35:03 +0000452 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000453 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000454
Brian Gaeke6559bb92002-11-14 22:32:30 +0000455 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000456 O << "\t.text\n";
457 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000458 O << "\t.globl\t" << CurrentFnName << "\n";
459 O << "\t.type\t" << CurrentFnName << ", @function\n";
460 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000461
Brian Gaeked7908f62003-06-27 00:00:48 +0000462 // Number each basic block so that we can consistently refer to them
463 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000464 NumberForBB.clear();
465 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
466 I != E; ++I) {
467 NumberForBB[I->getBasicBlock()] = BBNumber++;
468 }
469
Brian Gaeke6559bb92002-11-14 22:32:30 +0000470 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000471 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
472 I != E; ++I) {
473 // Print a label for the basic block.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000474 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
475 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000476 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
477 II != E; ++II) {
478 // Print the assembly for the instruction.
479 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000480 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000481 }
Chris Lattner0285a332002-12-28 20:25:38 +0000482 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000483
484 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000485 return false;
486}
487
Chris Lattner3d3067b2002-11-21 20:44:15 +0000488static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000489 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000490 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
491 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000492}
493
494static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000495 if (MI->getOperand(Op).isFrameIndex()) return true;
496 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000497 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000498 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
499 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000500}
501
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000502void Printer::printOp(const MachineOperand &MO,
Brian Gaeked7908f62003-06-27 00:00:48 +0000503 bool elideOffsetKeyword /* = false */) const {
Brian Gaekede420ae2003-07-23 20:25:08 +0000504 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000505 switch (MO.getType()) {
506 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000507 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000508 O << "<" << V->getName() << ">";
509 return;
510 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000511 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000512 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000513 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
514 O << RI.get(MO.getReg()).Name;
515 else
516 O << "%reg" << MO.getReg();
517 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000518
519 case MachineOperand::MO_SignExtendedImmed:
520 case MachineOperand::MO_UnextendedImmed:
521 O << (int)MO.getImmedValue();
522 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000523 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000524 {
525 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
526 assert (i != NumberForBB.end()
527 && "Could not find a BB I previously put in the NumberForBB map!");
528 O << ".BB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
529 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000530 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000531 case MachineOperand::MO_GlobalAddress:
Brian Gaekebc601fe2003-06-25 22:00:39 +0000532 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000533 return;
534 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000535 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000536 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000537 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000538 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000539 }
540}
541
Chris Lattner3501fea2003-01-14 22:00:31 +0000542static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000543 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000544 default: assert(0 && "Unknown arg size!");
545 case X86II::Arg8: return "BYTE PTR";
546 case X86II::Arg16: return "WORD PTR";
547 case X86II::Arg32: return "DWORD PTR";
548 case X86II::Arg64: return "QWORD PTR";
549 case X86II::ArgF32: return "DWORD PTR";
550 case X86II::ArgF64: return "QWORD PTR";
551 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000552 }
553}
554
Brian Gaekede420ae2003-07-23 20:25:08 +0000555void Printer::printMemReference(const MachineInstr *MI, unsigned Op) const {
556 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000557 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000558
559 if (MI->getOperand(Op).isFrameIndex()) {
560 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
561 if (MI->getOperand(Op+3).getImmedValue())
562 O << " + " << MI->getOperand(Op+3).getImmedValue();
563 O << "]";
564 return;
565 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000566 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000567 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000568 if (MI->getOperand(Op+3).getImmedValue())
569 O << " + " << MI->getOperand(Op+3).getImmedValue();
570 O << "]";
571 return;
572 }
573
Chris Lattner3d3067b2002-11-21 20:44:15 +0000574 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000575 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000576 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000577 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000578
579 O << "[";
580 bool NeedPlus = false;
581 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000582 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000583 NeedPlus = true;
584 }
585
586 if (IndexReg.getReg()) {
587 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000588 if (ScaleVal != 1)
589 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000590 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000591 NeedPlus = true;
592 }
593
Chris Lattner0285a332002-12-28 20:25:38 +0000594 if (DispVal) {
595 if (NeedPlus)
596 if (DispVal > 0)
597 O << " + ";
598 else {
599 O << " - ";
600 DispVal = -DispVal;
601 }
602 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000603 }
604 O << "]";
605}
606
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000607/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000608/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000609///
Brian Gaekede420ae2003-07-23 20:25:08 +0000610void Printer::printMachineInstruction(const MachineInstr *MI) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000611 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000612 const TargetInstrInfo &TII = TM.getInstrInfo();
613 const TargetInstrDescriptor &Desc = TII.get(Opcode);
614 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000615
Chris Lattnereca1f632002-12-25 05:09:01 +0000616 switch (Desc.TSFlags & X86II::FormMask) {
617 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000618 // Print pseudo-instructions as comments; either they should have been
619 // turned into real instructions by now, or they don't need to be
620 // seen by the assembler (e.g., IMPLICIT_USEs.)
621 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000622 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000623 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000624 O << " = phi ";
625 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
626 if (i != 1) O << ", ";
627 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000628 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000629 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000630 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000631 O << "]";
632 }
633 } else {
634 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000635 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
636 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000637 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000638 O << " = ";
639 ++i;
640 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000641 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000642
643 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
644 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000645 if (MI->getOperand(i).opIsDefOnly() ||
646 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000647 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000648 if (MI->getOperand(i).opIsDefOnly() ||
649 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000650 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000651 }
652 O << "\n";
653 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000654
Chris Lattnerf9f60882002-11-18 06:56:51 +0000655 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000656 // The accepted forms of Raw instructions are:
657 // 1. nop - No operand required
658 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000659 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000660 //
661 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000662 (MI->getNumOperands() == 1 &&
663 (MI->getOperand(0).isPCRelativeDisp() ||
664 MI->getOperand(0).isGlobalAddress() ||
665 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000666 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000667 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000668
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000669 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000670 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000671 }
672 O << "\n";
673 return;
674
Chris Lattner77875d82002-11-21 02:00:20 +0000675 case X86II::AddRegFrm: {
676 // There are currently two forms of acceptable AddRegFrm instructions.
677 // Either the instruction JUST takes a single register (like inc, dec, etc),
678 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000679 // (move immediate f.e.). Note that this immediate value might be stored as
680 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000681 // into a register. The initial register might be duplicated if this is a
682 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000683 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000684 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000685 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000686 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000687 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000688 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000689 MI->getOperand(1).isRegister() ||
690 MI->getOperand(1).isGlobalAddress() ||
691 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000692 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000693
Chris Lattner77875d82002-11-21 02:00:20 +0000694 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000695
Brian Gaeked7908f62003-06-27 00:00:48 +0000696 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000697 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000698 if (MI->getNumOperands() == 2 &&
699 (!MI->getOperand(1).isRegister() ||
700 MI->getOperand(1).getVRegValueOrNull() ||
701 MI->getOperand(1).isGlobalAddress() ||
702 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000703 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000704 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000705 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000706 if (Desc.TSFlags & X86II::PrintImplUses) {
707 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
708 O << ", " << RI.get(*p).Name;
709 }
710 }
Chris Lattner77875d82002-11-21 02:00:20 +0000711 O << "\n";
712 return;
713 }
Chris Lattner233ad712002-11-21 01:33:44 +0000714 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000715 // There are two acceptable forms of MRMDestReg instructions, those with 2,
716 // 3 and 4 operands:
717 //
718 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000719 //
720 // 3 Operands: in this form, the first two registers (the destination, and
721 // the first operand) should be the same, post register allocation. The 3rd
722 // operand is an additional input. This should be for things like add
723 // instructions.
724 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000725 // 4 Operands: This form is for instructions which are 3 operands forms, but
726 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000727 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000728 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000729 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000730 (MI->getNumOperands() == 2 ||
731 (isTwoAddr && MI->getOperand(1).isRegister() &&
732 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
733 (MI->getNumOperands() == 3 ||
734 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000735 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000736
Brian Gaeked7908f62003-06-27 00:00:48 +0000737 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000738 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000739 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000740 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000741 if (MI->getNumOperands() == 4) {
742 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000743 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000744 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000745 O << "\n";
746 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000747 }
Chris Lattner18042332002-11-21 21:03:39 +0000748
749 case X86II::MRMDestMem: {
750 // These instructions are the same as MRMDestReg, but instead of having a
751 // register reference for the mod/rm field, it's a memory reference.
752 //
753 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000754 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000755
Brian Gaeked7908f62003-06-27 00:00:48 +0000756 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000757 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000758 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000759 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000760 O << "\n";
761 return;
762 }
763
Chris Lattner233ad712002-11-21 01:33:44 +0000764 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000765 // There is a two forms that are acceptable for MRMSrcReg instructions,
766 // those with 3 and 2 operands:
767 //
768 // 3 Operands: in this form, the last register (the second input) is the
769 // ModR/M input. The first two operands should be the same, post register
770 // allocation. This is for things like: add r32, r/m32
771 //
772 // 2 Operands: this is for things like mov that do not read a second input
773 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000774 assert(MI->getOperand(0).isRegister() &&
775 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000776 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000777 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000778 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000779 if (MI->getNumOperands() == 3 &&
780 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
781 O << "**";
782
Brian Gaeked7908f62003-06-27 00:00:48 +0000783 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000784 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000785 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000786 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000787 O << "\n";
788 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000789 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790
Chris Lattner3d3067b2002-11-21 20:44:15 +0000791 case X86II::MRMSrcMem: {
792 // These instructions are the same as MRMSrcReg, but instead of having a
793 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000794 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000795 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000796 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000797 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000798 isMem(MI, 2))
799 && "Bad format for MRMDestReg!");
800 if (MI->getNumOperands() == 2+4 &&
801 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
802 O << "**";
803
Brian Gaeked7908f62003-06-27 00:00:48 +0000804 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000805 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000806 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000807 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000808 O << "\n";
809 return;
810 }
811
Chris Lattner675dd2c2002-11-21 17:09:01 +0000812 case X86II::MRMS0r: case X86II::MRMS1r:
813 case X86II::MRMS2r: case X86II::MRMS3r:
814 case X86II::MRMS4r: case X86II::MRMS5r:
815 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000816 // In this form, the following are valid formats:
817 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000818 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000819 // 2. shl rdest, rinput <implicit CL or 1>
820 // 3. sbb rdest, rinput, immediate [rdest = rinput]
821 //
822 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000823 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000824 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000825 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000826 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000827 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000828 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000829 "Bad MRMSxR format!");
830
Chris Lattnerd9096832002-12-15 08:01:39 +0000831 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000832 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
833 O << "**";
834
Brian Gaeked7908f62003-06-27 00:00:48 +0000835 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000836 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000837 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000838 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000839 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000840 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000841 if (Desc.TSFlags & X86II::PrintImplUses) {
842 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
843 O << ", " << RI.get(*p).Name;
844 }
845 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000846 O << "\n";
847
848 return;
849 }
850
Chris Lattnerb7089442003-01-13 00:35:03 +0000851 case X86II::MRMS0m: case X86II::MRMS1m:
852 case X86II::MRMS2m: case X86II::MRMS3m:
853 case X86II::MRMS4m: case X86II::MRMS5m:
854 case X86II::MRMS6m: case X86II::MRMS7m: {
855 // In this form, the following are valid formats:
856 // 1. sete [m]
857 // 2. cmp [m], immediate
858 // 2. shl [m], rinput <implicit CL or 1>
859 // 3. sbb [m], immediate
860 //
861 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
862 isMem(MI, 0) && "Bad MRMSxM format!");
863 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
864 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000865 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
866 // is misassembled by gas in intel_syntax mode as its 32-bit
867 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
868 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000869 if (MI->getOpCode() == X86::FSTPr80) {
870 if ((MI->getOperand(0).getReg() == X86::ESP)
871 && (MI->getOperand(1).getImmedValue() == 1)) {
872 int DispVal = MI->getOperand(3).getImmedValue();
873 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
874 unsigned int val = (unsigned int) DispVal;
875 O << ".byte 0xdb, 0xbc, 0x24\n\t";
876 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
877 } else { // 1 byte disp.
878 unsigned char val = (unsigned char) DispVal;
879 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
880 << std::dec << "\t# ";
881 }
882 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000883 }
884 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
885 // misassembled by gas in intel_syntax mode as its 32-bit
886 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
887 // opcode bytes instead of the instruction.
888 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000889 if ((MI->getOperand(0).getReg() == X86::ESP)
890 && (MI->getOperand(1).getImmedValue() == 1)) {
891 int DispVal = MI->getOperand(3).getImmedValue();
892 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
893 unsigned int val = (unsigned int) DispVal;
894 O << ".byte 0xdb, 0xac, 0x24\n\t";
895 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
896 } else { // 1 byte disp.
897 unsigned char val = (unsigned char) DispVal;
898 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
899 << std::dec << "\t# ";
900 }
901 }
902 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000903 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
904 // invalid opcode, saying "64 bit operations are only supported in
905 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
906 // [...]", which is wrong. Workaround: Output the raw opcode bytes
907 // instead of the instruction.
908 if (MI->getOpCode() == X86::FILDr64) {
909 if ((MI->getOperand(0).getReg() == X86::ESP)
910 && (MI->getOperand(1).getImmedValue() == 1)) {
911 int DispVal = MI->getOperand(3).getImmedValue();
912 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
913 unsigned int val = (unsigned int) DispVal;
914 O << ".byte 0xdf, 0xac, 0x24\n\t";
915 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
916 } else { // 1 byte disp.
917 unsigned char val = (unsigned char) DispVal;
918 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
919 << std::dec << "\t# ";
920 }
921 }
922 }
923 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
924 // an invalid opcode, saying "64 bit operations are only
925 // supported in 64 bit modes." libopcodes disassembles it as
926 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
927 // "fistpll DWORD PTR " instead, which is what libopcodes is
928 // expecting to see.
929 if (MI->getOpCode() == X86::FISTPr64) {
930 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000931 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000932 if (MI->getNumOperands() == 5) {
933 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000934 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000935 }
936 O << "\t# ";
937 }
938
Brian Gaeked7908f62003-06-27 00:00:48 +0000939 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000940 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000941 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000942 if (MI->getNumOperands() == 5) {
943 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000944 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000945 }
946 O << "\n";
947 return;
948 }
949
Chris Lattnerf9f60882002-11-18 06:56:51 +0000950 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000951 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000952 }
Chris Lattner72614082002-10-25 22:55:53 +0000953}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000954
955bool Printer::doInitialization(Module &M)
956{
957 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
958 // with no % decorations on register names.
959 O << "\t.intel_syntax noprefix\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000960
961 // Ripped from CWriter:
962 // Calculate which global values have names that will collide when we throw
963 // away type information.
964 { // Scope to delete the FoundNames set when we are done with it...
965 std::set<std::string> FoundNames;
966 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
967 if (I->hasName()) // If the global has a name...
968 if (FoundNames.count(I->getName())) // And the name is already used
969 MangledGlobals.insert(I); // Mangle the name
970 else
971 FoundNames.insert(I->getName()); // Otherwise, keep track of name
972
973 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
974 if (I->hasName()) // If the global has a name...
975 if (FoundNames.count(I->getName())) // And the name is already used
976 MangledGlobals.insert(I); // Mangle the name
977 else
978 FoundNames.insert(I->getName()); // Otherwise, keep track of name
979 }
980
Brian Gaeke9e474c42003-06-19 19:32:32 +0000981 return false; // success
982}
983
Chris Lattnerc07736a2003-07-23 15:22:26 +0000984static const Function *isConstantFunctionPointerRef(const Constant *C) {
985 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
986 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000987 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000988 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000989}
990
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000991bool Printer::doFinalization(Module &M)
992{
Brian Gaekede420ae2003-07-23 20:25:08 +0000993 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000994 // Print out module-level global variables here.
995 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000996 std::string name(getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000997 if (I->hasInitializer()) {
998 Constant *C = I->getInitializer();
999 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +00001000 O << "\t.globl " << name << "\n";
1001 O << "\t.type " << name << ",@object\n";
1002 O << "\t.size " << name << ","
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001003 << (unsigned)TD.getTypeSize(I->getType()) << "\n";
1004 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001005 O << name << ":\t\t\t\t\t#";
1006 // If this is a constant function pointer, we only print out the
1007 // name of the function in the comment (because printing the
1008 // function means calling AsmWriter to print the whole LLVM
1009 // assembly, which would corrupt the X86 assembly output.)
1010 // Otherwise we print out the whole llvm value as a comment.
1011 if (const Function *F = isConstantFunctionPointerRef (C)) {
1012 O << " %" << F->getName() << "()\n";
1013 } else {
1014 O << *C << "\n";
1015 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001016 printConstantValueOnly (C);
1017 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +00001018 O << "\t.globl " << name << "\n";
1019 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001020 << (unsigned)TD.getTypeSize(I->getType()) << ", "
1021 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001022 }
1023 }
Brian Gaekebc601fe2003-06-25 22:00:39 +00001024 MangledGlobals.clear();
Brian Gaeke9e474c42003-06-19 19:32:32 +00001025 return false; // success
1026}