blob: 6cfc1c0eaf94799f75bb5f74a9aae77212ae206a [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
Chris Lattner72614082002-10-25 22:55:53 +00002//
Brian Gaeke92bdfe62003-07-23 18:37:06 +00003// This file contains a printer that converts from our internal
4// representation of machine-dependent LLVM code to Intel-format
5// assembly language. This printer is the output mechanism used
6// by `llc' and `lli -printmachineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000011#include "X86InstrInfo.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000012#include "llvm/Function.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000013#include "llvm/Constant.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000014#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000015#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000016#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner233ad712002-11-21 01:33:44 +000018#include "Support/Statistic.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000019#include "Support/hash_map"
20#include "llvm/Type.h"
21#include "llvm/Constants.h"
22#include "llvm/Assembly/Writer.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/SlotCalculator.h"
25#include "Support/StringExtras.h"
26#include "llvm/Module.h"
Chris Lattner72614082002-10-25 22:55:53 +000027
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000028namespace {
Brian Gaeke92bdfe62003-07-23 18:37:06 +000029 /// This is properly part of the name mangler; it keeps track of
30 /// which global values have had their names mangled. It is cleared
31 /// at the end of every module by doFinalization().
32 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000033 std::set<const Value *> MangledGlobals;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000034
Chris Lattner0285a332002-12-28 20:25:38 +000035 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000036 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000037 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000038 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000039
40 /// Target machine description which we query for reg. names, data
41 /// layout, etc.
42 ///
43 TargetMachine &TM;
44
45 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000046
47 /// We name each basic block in a Function with a unique number, so
48 /// that we can consistently refer to them later. This is cleared
49 /// at the beginning of each call to runOnMachineFunction().
50 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000051 typedef std::map<const Value *, unsigned> ValueMapTy;
52 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000053
54 /// Cache of mangled name for current function. This is
55 /// recalculated at the beginning of each call to
56 /// runOnMachineFunction().
57 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000058 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000059
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000060 virtual const char *getPassName() const {
61 return "X86 Assembly Printer";
62 }
63
Brian Gaekede420ae2003-07-23 20:25:08 +000064 void printMachineInstruction(const MachineInstr *MI) const;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000065 void printOp(const MachineOperand &MO,
Brian Gaekede420ae2003-07-23 20:25:08 +000066 bool elideOffsetKeyword = false) const;
67 void printMemReference(const MachineInstr *MI, unsigned Op) const;
Brian Gaekeb3011a02003-07-24 17:30:45 +000068 void printConstantPool(MachineConstantPool *MCP) const;
Brian Gaeke01d79ff2003-06-25 18:01:07 +000069 bool runOnMachineFunction(MachineFunction &F);
Brian Gaekeb3011a02003-07-24 17:30:45 +000070 std::string ConstantExprToString(const ConstantExpr* CE) const;
71 std::string valToExprString(const Value* V) const;
Brian Gaeke9e474c42003-06-19 19:32:32 +000072 bool doInitialization(Module &M);
73 bool doFinalization(Module &M);
Brian Gaekeb3011a02003-07-24 17:30:45 +000074 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0) const;
75 void printSingleConstantValue(const Constant* CV) const;
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000076 };
Brian Gaeked7908f62003-06-27 00:00:48 +000077} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078
Brian Gaeke92bdfe62003-07-23 18:37:06 +000079/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000080/// assembly code for a MachineFunction to the given output stream,
81/// using the given target machine description. This should work
82/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000083///
Brian Gaekede420ae2003-07-23 20:25:08 +000084Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
85 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000086}
87
Brian Gaeke92bdfe62003-07-23 18:37:06 +000088/// makeNameProper - We don't want identifier names with ., space, or
89/// - in them, so we mangle these characters into the strings "d_",
90/// "s_", and "D_", respectively.
91///
Brian Gaekebc601fe2003-06-25 22:00:39 +000092static std::string makeNameProper(std::string x) {
93 std::string tmp;
94 for (std::string::iterator sI = x.begin(), sEnd = x.end(); sI != sEnd; sI++)
95 switch (*sI) {
96 case '.': tmp += "d_"; break;
97 case ' ': tmp += "s_"; break;
98 case '-': tmp += "D_"; break;
99 default: tmp += *sI;
100 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000101 return tmp;
102}
103
Brian Gaeked7908f62003-06-27 00:00:48 +0000104static std::string getValueName(const Value *V) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000105 if (V->hasName()) { // Print out the label if it exists...
Brian Gaekebc601fe2003-06-25 22:00:39 +0000106 // Name mangling occurs as follows:
107 // - If V is not a global, mangling always occurs.
108 // - Otherwise, mangling occurs when any of the following are true:
109 // 1) V has internal linkage
110 // 2) V's name would collide if it is not mangled.
111 //
Brian Gaekebc601fe2003-06-25 22:00:39 +0000112 if(const GlobalValue* gv = dyn_cast<GlobalValue>(V)) {
113 if(!gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
114 // No internal linkage, name will not collide -> no mangling.
115 return makeNameProper(gv->getName());
116 }
117 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000118 // Non-global, or global with internal linkage / colliding name -> mangle.
119 return "l" + utostr(V->getType()->getUniqueID()) + "_" +
120 makeNameProper(V->getName());
121 }
Brian Gaekebc601fe2003-06-25 22:00:39 +0000122 static int Count = 0;
123 Count++;
124 return "ltmp_" + itostr(Count) + "_" + utostr(V->getType()->getUniqueID());
125}
126
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000127/// valToExprString - Helper function for ConstantExprToString().
128/// Appends result to argument string S.
129///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000130std::string Printer::valToExprString(const Value* V) const {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 std::string S;
132 bool failed = false;
133 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
134 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
135 S += std::string(CB == ConstantBool::True ? "1" : "0");
136 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
137 S += itostr(CI->getValue());
138 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
139 S += utostr(CI->getValue());
140 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
141 S += ftostr(CFP->getValue());
142 else if (isa<ConstantPointerNull>(CV))
143 S += "0";
144 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
145 S += valToExprString(CPR->getValue());
146 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
147 S += ConstantExprToString(CE);
148 else
149 failed = true;
150 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000151 S += getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152 }
153 else
154 failed = true;
155
156 if (failed) {
157 assert(0 && "Cannot convert value to string");
158 S += "<illegal-value>";
159 }
160 return S;
161}
162
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000163/// ConstantExprToString - Convert a ConstantExpr to an asm expression
164/// and return this as a string.
165///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000166std::string Printer::ConstantExprToString(const ConstantExpr* CE) const {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167 std::string S;
Brian Gaekede420ae2003-07-23 20:25:08 +0000168 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169 switch(CE->getOpcode()) {
170 case Instruction::GetElementPtr:
171 { // generate a symbolic expression for the byte address
172 const Value* ptrVal = CE->getOperand(0);
173 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
174 S += "(" + valToExprString(ptrVal) + ") + ("
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000175 + utostr(TD.getIndexedOffset(ptrVal->getType(),idxVec)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000176 break;
177 }
178
179 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000180 // Support only non-converting or widening casts for now, that is,
181 // ones that do not involve a change in value. This assertion is
182 // not a complete check.
183 {
184 Constant *Op = CE->getOperand(0);
185 const Type *OpTy = Op->getType(), *Ty = CE->getType();
186 assert(((isa<PointerType>(OpTy)
187 && (Ty == Type::LongTy || Ty == Type::ULongTy))
188 || (isa<PointerType>(Ty)
189 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
190 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
191 && (OpTy-> isLosslesslyConvertibleTo(Ty))))
192 && "FIXME: Don't yet support this kind of constant cast expr");
193 S += "(" + valToExprString(Op) + ")";
194 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000195 break;
196
197 case Instruction::Add:
198 S += "(" + valToExprString(CE->getOperand(0)) + ") + ("
199 + valToExprString(CE->getOperand(1)) + ")";
200 break;
201
202 default:
203 assert(0 && "Unsupported operator in ConstantExprToString()");
204 break;
205 }
206
207 return S;
208}
209
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000210/// printSingleConstantValue - Print a single constant value.
211///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000212void
Brian Gaekeb3011a02003-07-24 17:30:45 +0000213Printer::printSingleConstantValue(const Constant* CV) const
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000214{
215 assert(CV->getType() != Type::VoidTy &&
216 CV->getType() != Type::TypeTy &&
217 CV->getType() != Type::LabelTy &&
218 "Unexpected type for Constant");
219
220 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
221 && "Aggregate types should be handled outside this function");
222
223 const Type *type = CV->getType();
224 O << "\t";
225 switch(type->getPrimitiveID())
226 {
227 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
228 O << ".byte";
229 break;
230 case Type::UShortTyID: case Type::ShortTyID:
231 O << ".word";
232 break;
233 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
234 O << ".long";
235 break;
236 case Type::ULongTyID: case Type::LongTyID:
237 O << ".quad";
238 break;
239 case Type::FloatTyID:
240 O << ".long";
241 break;
242 case Type::DoubleTyID:
243 O << ".quad";
244 break;
245 case Type::ArrayTyID:
246 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
247 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
248 O << ".string";
249 else
250 assert (0 && "Can't handle printing this type of array");
251 break;
252 default:
253 assert (0 && "Can't handle printing this type of thing");
254 break;
255 }
256 O << "\t";
257
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000258 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
259 {
260 // Constant expression built from operators, constants, and
261 // symbolic addrs
262 O << ConstantExprToString(CE) << "\n";
263 }
264 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000265 {
266 if (type->isFloatingPoint()) {
267 // FP Constants are printed as integer constants to avoid losing
268 // precision...
269 double Val = cast<ConstantFP>(CV)->getValue();
270 if (type == Type::FloatTy) {
271 float FVal = (float)Val;
272 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
273 O << *(unsigned int*)ProxyPtr;
274 } else if (type == Type::DoubleTy) {
275 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
276 O << *(uint64_t*)ProxyPtr;
277 } else {
278 assert(0 && "Unknown floating point type!");
279 }
280
281 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
282 } else {
283 WriteAsOperand(O, CV, false, false) << "\n";
284 }
285 }
286 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
287 {
288 // This is a constant address for a global variable or method.
289 // Use the name of the variable or method as the address value.
Brian Gaekebc601fe2003-06-25 22:00:39 +0000290 O << getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000291 }
292 else if (isa<ConstantPointerNull>(CV))
293 {
294 // Null pointer value
295 O << "0\n";
296 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000297 else
298 {
299 assert(0 && "Unknown elementary type for constant");
300 }
301}
302
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000303/// isStringCompatible - Can we treat the specified array as a string?
304/// Only if it is an array of ubytes or non-negative sbytes.
305///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000306static bool isStringCompatible(const ConstantArray *CVA) {
307 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
308 if (ETy == Type::UByteTy) return true;
309 if (ETy != Type::SByteTy) return false;
310
311 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
312 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
313 return false;
314
315 return true;
316}
317
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000318/// toOctal - Convert the low order bits of X into an octal digit.
319///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000320static inline char toOctal(int X) {
321 return (X&7)+'0';
322}
323
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000324/// getAsCString - Return the specified array as a C compatible
325/// string, only if the predicate isStringCompatible is true.
326///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000327static std::string getAsCString(const ConstantArray *CVA) {
328 assert(isStringCompatible(CVA) && "Array is not string compatible!");
329
330 std::string Result;
331 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
332 Result = "\"";
333 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000334 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000335
336 if (C == '"') {
337 Result += "\\\"";
338 } else if (C == '\\') {
339 Result += "\\\\";
340 } else if (isprint(C)) {
341 Result += C;
342 } else {
343 switch(C) {
344 case '\a': Result += "\\a"; break;
345 case '\b': Result += "\\b"; break;
346 case '\f': Result += "\\f"; break;
347 case '\n': Result += "\\n"; break;
348 case '\r': Result += "\\r"; break;
349 case '\t': Result += "\\t"; break;
350 case '\v': Result += "\\v"; break;
351 default:
352 Result += '\\';
353 Result += toOctal(C >> 6);
354 Result += toOctal(C >> 3);
355 Result += toOctal(C >> 0);
356 break;
357 }
358 }
359 }
360 Result += "\"";
361 return Result;
362}
363
364// Print a constant value or values (it may be an aggregate).
365// Uses printSingleConstantValue() to print each individual value.
366void
367Printer::printConstantValueOnly(const Constant* CV,
Brian Gaekeb3011a02003-07-24 17:30:45 +0000368 int numPadBytesAfter /* = 0 */) const
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000369{
370 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000371 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000372
373 if (CVA && isStringCompatible(CVA))
374 { // print the string alone and return
375 O << "\t" << ".string" << "\t" << getAsCString(CVA) << "\n";
376 }
377 else if (CVA)
378 { // Not a string. Print the values in successive locations
379 const std::vector<Use> &constValues = CVA->getValues();
380 for (unsigned i=0; i < constValues.size(); i++)
381 printConstantValueOnly(cast<Constant>(constValues[i].get()));
382 }
383 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
384 { // Print the fields in successive locations. Pad to align if needed!
385 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000387 const std::vector<Use>& constValues = CVS->getValues();
388 unsigned sizeSoFar = 0;
389 for (unsigned i=0, N = constValues.size(); i < N; i++)
390 {
391 const Constant* field = cast<Constant>(constValues[i].get());
392
393 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000394 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000395 int padSize = ((i == N-1? cvsLayout->StructSize
396 : cvsLayout->MemberOffsets[i+1])
397 - cvsLayout->MemberOffsets[i]) - fieldSize;
398 sizeSoFar += (fieldSize + padSize);
399
400 // Now print the actual field value
401 printConstantValueOnly(field, padSize);
402 }
403 assert(sizeSoFar == cvsLayout->StructSize &&
404 "Layout of constant struct may be incorrect!");
405 }
406 else
407 printSingleConstantValue(CV);
408
409 if (numPadBytesAfter) {
410 unsigned numBytes = numPadBytesAfter;
411 for ( ; numBytes >= 8; numBytes -= 8)
412 printSingleConstantValue(Constant::getNullValue(Type::ULongTy));
413 if (numBytes >= 4)
414 {
415 printSingleConstantValue(Constant::getNullValue(Type::UIntTy));
416 numBytes -= 4;
417 }
418 while (numBytes--)
419 printSingleConstantValue(Constant::getNullValue(Type::UByteTy));
420 }
421}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000422
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000423/// printConstantPool - Print to the current output stream assembly
424/// representations of the constants in the constant pool MCP. This is
425/// used to print out constants which have been "spilled to memory" by
426/// the code generator.
427///
Brian Gaekeb3011a02003-07-24 17:30:45 +0000428void Printer::printConstantPool(MachineConstantPool *MCP) const {
Chris Lattnerb7089442003-01-13 00:35:03 +0000429 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000430 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000431
Chris Lattnerb7089442003-01-13 00:35:03 +0000432 if (CP.empty()) return;
433
434 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
435 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000436 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000437 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000438 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
439 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000440 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000441 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000442}
443
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000444/// runOnMachineFunction - This uses the printMachineInstruction()
445/// method to print assembly for each instruction.
446///
Chris Lattner0285a332002-12-28 20:25:38 +0000447bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000448 // BBNumber is used here so that a given Printer will never give two
449 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000450 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000451
Brian Gaeked7908f62003-06-27 00:00:48 +0000452 // What's my mangled name?
453 CurrentFnName = getValueName(MF.getFunction());
454
Chris Lattnerb7089442003-01-13 00:35:03 +0000455 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000456 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000457
Brian Gaeke6559bb92002-11-14 22:32:30 +0000458 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000459 O << "\t.text\n";
460 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000461 O << "\t.globl\t" << CurrentFnName << "\n";
462 O << "\t.type\t" << CurrentFnName << ", @function\n";
463 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000464
Brian Gaeked7908f62003-06-27 00:00:48 +0000465 // Number each basic block so that we can consistently refer to them
466 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000467 NumberForBB.clear();
468 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
469 I != E; ++I) {
470 NumberForBB[I->getBasicBlock()] = BBNumber++;
471 }
472
Brian Gaeke6559bb92002-11-14 22:32:30 +0000473 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000474 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
475 I != E; ++I) {
476 // Print a label for the basic block.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000477 O << ".BB" << NumberForBB[I->getBasicBlock()] << ":\t# "
478 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000479 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
480 II != E; ++II) {
481 // Print the assembly for the instruction.
482 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000483 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000484 }
Chris Lattner0285a332002-12-28 20:25:38 +0000485 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000486
487 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000488 return false;
489}
490
Chris Lattner3d3067b2002-11-21 20:44:15 +0000491static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000492 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000493 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
494 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000495}
496
497static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000498 if (MI->getOperand(Op).isFrameIndex()) return true;
499 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000500 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000501 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
502 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503}
504
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000505void Printer::printOp(const MachineOperand &MO,
Brian Gaeked7908f62003-06-27 00:00:48 +0000506 bool elideOffsetKeyword /* = false */) const {
Brian Gaekede420ae2003-07-23 20:25:08 +0000507 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000508 switch (MO.getType()) {
509 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000510 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000511 O << "<" << V->getName() << ">";
512 return;
513 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000514 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000515 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000516 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
517 O << RI.get(MO.getReg()).Name;
518 else
519 O << "%reg" << MO.getReg();
520 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000521
522 case MachineOperand::MO_SignExtendedImmed:
523 case MachineOperand::MO_UnextendedImmed:
524 O << (int)MO.getImmedValue();
525 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000526 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000527 {
528 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
529 assert (i != NumberForBB.end()
530 && "Could not find a BB I previously put in the NumberForBB map!");
531 O << ".BB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
532 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000533 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000534 case MachineOperand::MO_GlobalAddress:
Brian Gaekebc601fe2003-06-25 22:00:39 +0000535 if (!elideOffsetKeyword) O << "OFFSET "; O << getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000536 return;
537 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000538 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000539 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000540 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000541 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000542 }
543}
544
Chris Lattner3501fea2003-01-14 22:00:31 +0000545static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000546 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000547 default: assert(0 && "Unknown arg size!");
548 case X86II::Arg8: return "BYTE PTR";
549 case X86II::Arg16: return "WORD PTR";
550 case X86II::Arg32: return "DWORD PTR";
551 case X86II::Arg64: return "QWORD PTR";
552 case X86II::ArgF32: return "DWORD PTR";
553 case X86II::ArgF64: return "QWORD PTR";
554 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000555 }
556}
557
Brian Gaekede420ae2003-07-23 20:25:08 +0000558void Printer::printMemReference(const MachineInstr *MI, unsigned Op) const {
559 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000560 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000561
562 if (MI->getOperand(Op).isFrameIndex()) {
563 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
564 if (MI->getOperand(Op+3).getImmedValue())
565 O << " + " << MI->getOperand(Op+3).getImmedValue();
566 O << "]";
567 return;
568 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000569 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000570 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000571 if (MI->getOperand(Op+3).getImmedValue())
572 O << " + " << MI->getOperand(Op+3).getImmedValue();
573 O << "]";
574 return;
575 }
576
Chris Lattner3d3067b2002-11-21 20:44:15 +0000577 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000578 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000579 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000580 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000581
582 O << "[";
583 bool NeedPlus = false;
584 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000585 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000586 NeedPlus = true;
587 }
588
589 if (IndexReg.getReg()) {
590 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000591 if (ScaleVal != 1)
592 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000593 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000594 NeedPlus = true;
595 }
596
Chris Lattner0285a332002-12-28 20:25:38 +0000597 if (DispVal) {
598 if (NeedPlus)
599 if (DispVal > 0)
600 O << " + ";
601 else {
602 O << " - ";
603 DispVal = -DispVal;
604 }
605 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000606 }
607 O << "]";
608}
609
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000610/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000611/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000612///
Brian Gaekede420ae2003-07-23 20:25:08 +0000613void Printer::printMachineInstruction(const MachineInstr *MI) const {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000614 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000615 const TargetInstrInfo &TII = TM.getInstrInfo();
616 const TargetInstrDescriptor &Desc = TII.get(Opcode);
617 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000618
Chris Lattnereca1f632002-12-25 05:09:01 +0000619 switch (Desc.TSFlags & X86II::FormMask) {
620 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000621 // Print pseudo-instructions as comments; either they should have been
622 // turned into real instructions by now, or they don't need to be
623 // seen by the assembler (e.g., IMPLICIT_USEs.)
624 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000625 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000626 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000627 O << " = phi ";
628 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
629 if (i != 1) O << ", ";
630 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000631 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000632 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000633 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000634 O << "]";
635 }
636 } else {
637 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000638 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
639 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000640 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000641 O << " = ";
642 ++i;
643 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000644 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000645
646 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
647 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000648 if (MI->getOperand(i).opIsDefOnly() ||
649 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000650 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000651 if (MI->getOperand(i).opIsDefOnly() ||
652 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000653 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000654 }
655 O << "\n";
656 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000657
Chris Lattnerf9f60882002-11-18 06:56:51 +0000658 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000659 // The accepted forms of Raw instructions are:
660 // 1. nop - No operand required
661 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000662 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000663 //
664 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000665 (MI->getNumOperands() == 1 &&
666 (MI->getOperand(0).isPCRelativeDisp() ||
667 MI->getOperand(0).isGlobalAddress() ||
668 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000669 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000670 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000671
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000672 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000673 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000674 }
675 O << "\n";
676 return;
677
Chris Lattner77875d82002-11-21 02:00:20 +0000678 case X86II::AddRegFrm: {
679 // There are currently two forms of acceptable AddRegFrm instructions.
680 // Either the instruction JUST takes a single register (like inc, dec, etc),
681 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000682 // (move immediate f.e.). Note that this immediate value might be stored as
683 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000684 // into a register. The initial register might be duplicated if this is a
685 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000686 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000687 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000688 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000689 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000690 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000691 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 MI->getOperand(1).isRegister() ||
693 MI->getOperand(1).isGlobalAddress() ||
694 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000695 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000696
Chris Lattner77875d82002-11-21 02:00:20 +0000697 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000698
Brian Gaeked7908f62003-06-27 00:00:48 +0000699 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000700 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000701 if (MI->getNumOperands() == 2 &&
702 (!MI->getOperand(1).isRegister() ||
703 MI->getOperand(1).getVRegValueOrNull() ||
704 MI->getOperand(1).isGlobalAddress() ||
705 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000706 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000707 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000708 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000709 if (Desc.TSFlags & X86II::PrintImplUses) {
710 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
711 O << ", " << RI.get(*p).Name;
712 }
713 }
Chris Lattner77875d82002-11-21 02:00:20 +0000714 O << "\n";
715 return;
716 }
Chris Lattner233ad712002-11-21 01:33:44 +0000717 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000718 // There are two acceptable forms of MRMDestReg instructions, those with 2,
719 // 3 and 4 operands:
720 //
721 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000722 //
723 // 3 Operands: in this form, the first two registers (the destination, and
724 // the first operand) should be the same, post register allocation. The 3rd
725 // operand is an additional input. This should be for things like add
726 // instructions.
727 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000728 // 4 Operands: This form is for instructions which are 3 operands forms, but
729 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000730 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000731 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000732 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000733 (MI->getNumOperands() == 2 ||
734 (isTwoAddr && MI->getOperand(1).isRegister() &&
735 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
736 (MI->getNumOperands() == 3 ||
737 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000738 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000739
Brian Gaeked7908f62003-06-27 00:00:48 +0000740 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000741 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000742 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000743 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000744 if (MI->getNumOperands() == 4) {
745 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000746 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000747 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000748 O << "\n";
749 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000750 }
Chris Lattner18042332002-11-21 21:03:39 +0000751
752 case X86II::MRMDestMem: {
753 // These instructions are the same as MRMDestReg, but instead of having a
754 // register reference for the mod/rm field, it's a memory reference.
755 //
756 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000757 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000758
Brian Gaeked7908f62003-06-27 00:00:48 +0000759 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000760 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000761 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000762 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000763 O << "\n";
764 return;
765 }
766
Chris Lattner233ad712002-11-21 01:33:44 +0000767 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000768 // There is a two forms that are acceptable for MRMSrcReg instructions,
769 // those with 3 and 2 operands:
770 //
771 // 3 Operands: in this form, the last register (the second input) is the
772 // ModR/M input. The first two operands should be the same, post register
773 // allocation. This is for things like: add r32, r/m32
774 //
775 // 2 Operands: this is for things like mov that do not read a second input
776 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000777 assert(MI->getOperand(0).isRegister() &&
778 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000779 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000780 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000781 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000782 if (MI->getNumOperands() == 3 &&
783 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
784 O << "**";
785
Brian Gaeked7908f62003-06-27 00:00:48 +0000786 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000787 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000788 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000789 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000790 O << "\n";
791 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000792 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000793
Chris Lattner3d3067b2002-11-21 20:44:15 +0000794 case X86II::MRMSrcMem: {
795 // These instructions are the same as MRMSrcReg, but instead of having a
796 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000797 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000798 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000799 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000800 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000801 isMem(MI, 2))
802 && "Bad format for MRMDestReg!");
803 if (MI->getNumOperands() == 2+4 &&
804 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
805 O << "**";
806
Brian Gaeked7908f62003-06-27 00:00:48 +0000807 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000808 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000809 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000810 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000811 O << "\n";
812 return;
813 }
814
Chris Lattner675dd2c2002-11-21 17:09:01 +0000815 case X86II::MRMS0r: case X86II::MRMS1r:
816 case X86II::MRMS2r: case X86II::MRMS3r:
817 case X86II::MRMS4r: case X86II::MRMS5r:
818 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000819 // In this form, the following are valid formats:
820 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000821 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000822 // 2. shl rdest, rinput <implicit CL or 1>
823 // 3. sbb rdest, rinput, immediate [rdest = rinput]
824 //
825 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000826 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000827 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000828 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000829 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000830 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000831 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000832 "Bad MRMSxR format!");
833
Chris Lattnerd9096832002-12-15 08:01:39 +0000834 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000835 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
836 O << "**";
837
Brian Gaeked7908f62003-06-27 00:00:48 +0000838 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000839 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000840 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000841 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000842 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000843 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000844 if (Desc.TSFlags & X86II::PrintImplUses) {
845 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
846 O << ", " << RI.get(*p).Name;
847 }
848 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000849 O << "\n";
850
851 return;
852 }
853
Chris Lattnerb7089442003-01-13 00:35:03 +0000854 case X86II::MRMS0m: case X86II::MRMS1m:
855 case X86II::MRMS2m: case X86II::MRMS3m:
856 case X86II::MRMS4m: case X86II::MRMS5m:
857 case X86II::MRMS6m: case X86II::MRMS7m: {
858 // In this form, the following are valid formats:
859 // 1. sete [m]
860 // 2. cmp [m], immediate
861 // 2. shl [m], rinput <implicit CL or 1>
862 // 3. sbb [m], immediate
863 //
864 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
865 isMem(MI, 0) && "Bad MRMSxM format!");
866 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
867 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000868 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
869 // is misassembled by gas in intel_syntax mode as its 32-bit
870 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
871 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000872 if (MI->getOpCode() == X86::FSTPr80) {
873 if ((MI->getOperand(0).getReg() == X86::ESP)
874 && (MI->getOperand(1).getImmedValue() == 1)) {
875 int DispVal = MI->getOperand(3).getImmedValue();
876 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
877 unsigned int val = (unsigned int) DispVal;
878 O << ".byte 0xdb, 0xbc, 0x24\n\t";
879 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
880 } else { // 1 byte disp.
881 unsigned char val = (unsigned char) DispVal;
882 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
883 << std::dec << "\t# ";
884 }
885 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000886 }
887 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
888 // misassembled by gas in intel_syntax mode as its 32-bit
889 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
890 // opcode bytes instead of the instruction.
891 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000892 if ((MI->getOperand(0).getReg() == X86::ESP)
893 && (MI->getOperand(1).getImmedValue() == 1)) {
894 int DispVal = MI->getOperand(3).getImmedValue();
895 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
896 unsigned int val = (unsigned int) DispVal;
897 O << ".byte 0xdb, 0xac, 0x24\n\t";
898 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
899 } else { // 1 byte disp.
900 unsigned char val = (unsigned char) DispVal;
901 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
902 << std::dec << "\t# ";
903 }
904 }
905 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000906 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
907 // invalid opcode, saying "64 bit operations are only supported in
908 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
909 // [...]", which is wrong. Workaround: Output the raw opcode bytes
910 // instead of the instruction.
911 if (MI->getOpCode() == X86::FILDr64) {
912 if ((MI->getOperand(0).getReg() == X86::ESP)
913 && (MI->getOperand(1).getImmedValue() == 1)) {
914 int DispVal = MI->getOperand(3).getImmedValue();
915 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
916 unsigned int val = (unsigned int) DispVal;
917 O << ".byte 0xdf, 0xac, 0x24\n\t";
918 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
919 } else { // 1 byte disp.
920 unsigned char val = (unsigned char) DispVal;
921 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
922 << std::dec << "\t# ";
923 }
924 }
925 }
926 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
927 // an invalid opcode, saying "64 bit operations are only
928 // supported in 64 bit modes." libopcodes disassembles it as
929 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
930 // "fistpll DWORD PTR " instead, which is what libopcodes is
931 // expecting to see.
932 if (MI->getOpCode() == X86::FISTPr64) {
933 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000934 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000935 if (MI->getNumOperands() == 5) {
936 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000937 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000938 }
939 O << "\t# ";
940 }
941
Brian Gaeked7908f62003-06-27 00:00:48 +0000942 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000943 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000944 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000945 if (MI->getNumOperands() == 5) {
946 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000947 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000948 }
949 O << "\n";
950 return;
951 }
952
Chris Lattnerf9f60882002-11-18 06:56:51 +0000953 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000954 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000955 }
Chris Lattner72614082002-10-25 22:55:53 +0000956}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000957
958bool Printer::doInitialization(Module &M)
959{
960 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
961 // with no % decorations on register names.
962 O << "\t.intel_syntax noprefix\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +0000963
964 // Ripped from CWriter:
965 // Calculate which global values have names that will collide when we throw
966 // away type information.
967 { // Scope to delete the FoundNames set when we are done with it...
968 std::set<std::string> FoundNames;
969 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
970 if (I->hasName()) // If the global has a name...
971 if (FoundNames.count(I->getName())) // And the name is already used
972 MangledGlobals.insert(I); // Mangle the name
973 else
974 FoundNames.insert(I->getName()); // Otherwise, keep track of name
975
976 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
977 if (I->hasName()) // If the global has a name...
978 if (FoundNames.count(I->getName())) // And the name is already used
979 MangledGlobals.insert(I); // Mangle the name
980 else
981 FoundNames.insert(I->getName()); // Otherwise, keep track of name
982 }
983
Brian Gaeke9e474c42003-06-19 19:32:32 +0000984 return false; // success
985}
986
Chris Lattnerc07736a2003-07-23 15:22:26 +0000987static const Function *isConstantFunctionPointerRef(const Constant *C) {
988 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
989 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000990 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000991 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000992}
993
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000994bool Printer::doFinalization(Module &M)
995{
Brian Gaekede420ae2003-07-23 20:25:08 +0000996 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000997 // Print out module-level global variables here.
998 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000999 std::string name(getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001000 if (I->hasInitializer()) {
1001 Constant *C = I->getInitializer();
1002 O << "\t.data\n";
Brian Gaekebc601fe2003-06-25 22:00:39 +00001003 O << "\t.globl " << name << "\n";
1004 O << "\t.type " << name << ",@object\n";
1005 O << "\t.size " << name << ","
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001006 << (unsigned)TD.getTypeSize(I->getType()) << "\n";
1007 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001008 O << name << ":\t\t\t\t\t#";
1009 // If this is a constant function pointer, we only print out the
1010 // name of the function in the comment (because printing the
1011 // function means calling AsmWriter to print the whole LLVM
1012 // assembly, which would corrupt the X86 assembly output.)
1013 // Otherwise we print out the whole llvm value as a comment.
1014 if (const Function *F = isConstantFunctionPointerRef (C)) {
1015 O << " %" << F->getName() << "()\n";
1016 } else {
1017 O << *C << "\n";
1018 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001019 printConstantValueOnly (C);
1020 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +00001021 O << "\t.globl " << name << "\n";
1022 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001023 << (unsigned)TD.getTypeSize(I->getType()) << ", "
1024 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001025 }
1026 }
Brian Gaekebc601fe2003-06-25 22:00:39 +00001027 MangledGlobals.clear();
Brian Gaeke9e474c42003-06-19 19:32:32 +00001028 return false; // success
1029}