blob: bdfb8d56468709e3d286137d06a824ae66751fa0 [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"
Chris Lattnere0121322003-08-03 23:37:09 +000012#include "llvm/Module.h"
13#include "llvm/Type.h"
14#include "llvm/Constants.h"
15#include "llvm/DerivedTypes.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000016#include "llvm/Target/TargetMachine.h"
Chris Lattner0285a332002-12-28 20:25:38 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000018#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000019#include "llvm/CodeGen/MachineInstr.h"
Brian Gaeke01d79ff2003-06-25 18:01:07 +000020#include "llvm/Assembly/Writer.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000021#include "llvm/Support/Mangler.h"
Chris Lattnere0121322003-08-03 23:37:09 +000022#include "Support/StringExtras.h"
Chris Lattner72614082002-10-25 22:55:53 +000023
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000024namespace {
Chris Lattner0285a332002-12-28 20:25:38 +000025 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000026 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000027 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000028 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000029
30 /// Target machine description which we query for reg. names, data
31 /// layout, etc.
32 ///
33 TargetMachine &TM;
34
Brian Gaeked9fb37a2003-07-24 20:20:44 +000035 /// Name-mangler for global names.
36 ///
37 Mangler *Mang;
38
Brian Gaekede420ae2003-07-23 20:25:08 +000039 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000040
41 /// We name each basic block in a Function with a unique number, so
42 /// that we can consistently refer to them later. This is cleared
43 /// at the beginning of each call to runOnMachineFunction().
44 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000045 typedef std::map<const Value *, unsigned> ValueMapTy;
46 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000047
48 /// Cache of mangled name for current function. This is
49 /// recalculated at the beginning of each call to
50 /// runOnMachineFunction().
51 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000052 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000053
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000054 virtual const char *getPassName() const {
55 return "X86 Assembly Printer";
56 }
57
Brian Gaeked9fb37a2003-07-24 20:20:44 +000058 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000059 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000060 bool elideOffsetKeyword = false);
61 void printMemReference(const MachineInstr *MI, unsigned Op);
62 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000063 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000064 std::string ConstantExprToString(const ConstantExpr* CE);
65 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000066 bool doInitialization(Module &M);
67 bool doFinalization(Module &M);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000068 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
69 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000070 };
Brian Gaeked7908f62003-06-27 00:00:48 +000071} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000072
Brian Gaeke92bdfe62003-07-23 18:37:06 +000073/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000074/// assembly code for a MachineFunction to the given output stream,
75/// using the given target machine description. This should work
76/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000077///
Brian Gaekede420ae2003-07-23 20:25:08 +000078Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
79 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000080}
81
Brian Gaeke92bdfe62003-07-23 18:37:06 +000082/// valToExprString - Helper function for ConstantExprToString().
83/// Appends result to argument string S.
84///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000085std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000086 std::string S;
87 bool failed = false;
88 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
89 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
90 S += std::string(CB == ConstantBool::True ? "1" : "0");
91 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
92 S += itostr(CI->getValue());
93 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
94 S += utostr(CI->getValue());
95 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
96 S += ftostr(CFP->getValue());
97 else if (isa<ConstantPointerNull>(CV))
98 S += "0";
99 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
100 S += valToExprString(CPR->getValue());
101 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
102 S += ConstantExprToString(CE);
103 else
104 failed = true;
105 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000106 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000107 }
108 else
109 failed = true;
110
111 if (failed) {
112 assert(0 && "Cannot convert value to string");
113 S += "<illegal-value>";
114 }
115 return S;
116}
117
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000118/// ConstantExprToString - Convert a ConstantExpr to an asm expression
119/// and return this as a string.
120///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000121std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000122 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 switch(CE->getOpcode()) {
124 case Instruction::GetElementPtr:
125 { // generate a symbolic expression for the byte address
126 const Value* ptrVal = CE->getOperand(0);
127 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000128 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
129 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
130 else
131 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000132 }
133
134 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000135 // Support only non-converting or widening casts for now, that is,
136 // ones that do not involve a change in value. This assertion is
137 // not a complete check.
138 {
139 Constant *Op = CE->getOperand(0);
140 const Type *OpTy = Op->getType(), *Ty = CE->getType();
141 assert(((isa<PointerType>(OpTy)
142 && (Ty == Type::LongTy || Ty == Type::ULongTy))
143 || (isa<PointerType>(Ty)
144 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
145 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000146 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000147 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000148 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000149 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150
151 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000152 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
153 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000154
155 default:
156 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000157 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000158 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000159}
160
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000161/// printSingleConstantValue - Print a single constant value.
162///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000163void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000164Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165{
166 assert(CV->getType() != Type::VoidTy &&
167 CV->getType() != Type::TypeTy &&
168 CV->getType() != Type::LabelTy &&
169 "Unexpected type for Constant");
170
171 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
172 && "Aggregate types should be handled outside this function");
173
174 const Type *type = CV->getType();
175 O << "\t";
176 switch(type->getPrimitiveID())
177 {
178 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
179 O << ".byte";
180 break;
181 case Type::UShortTyID: case Type::ShortTyID:
182 O << ".word";
183 break;
184 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
185 O << ".long";
186 break;
187 case Type::ULongTyID: case Type::LongTyID:
188 O << ".quad";
189 break;
190 case Type::FloatTyID:
191 O << ".long";
192 break;
193 case Type::DoubleTyID:
194 O << ".quad";
195 break;
196 case Type::ArrayTyID:
197 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
198 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
199 O << ".string";
200 else
201 assert (0 && "Can't handle printing this type of array");
202 break;
203 default:
204 assert (0 && "Can't handle printing this type of thing");
205 break;
206 }
207 O << "\t";
208
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000209 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
210 {
211 // Constant expression built from operators, constants, and
212 // symbolic addrs
213 O << ConstantExprToString(CE) << "\n";
214 }
215 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000216 {
217 if (type->isFloatingPoint()) {
218 // FP Constants are printed as integer constants to avoid losing
219 // precision...
220 double Val = cast<ConstantFP>(CV)->getValue();
221 if (type == Type::FloatTy) {
222 float FVal = (float)Val;
223 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
224 O << *(unsigned int*)ProxyPtr;
225 } else if (type == Type::DoubleTy) {
226 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
227 O << *(uint64_t*)ProxyPtr;
228 } else {
229 assert(0 && "Unknown floating point type!");
230 }
231
232 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
233 } else {
234 WriteAsOperand(O, CV, false, false) << "\n";
235 }
236 }
237 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
238 {
239 // This is a constant address for a global variable or method.
240 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000241 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000242 }
243 else if (isa<ConstantPointerNull>(CV))
244 {
245 // Null pointer value
246 O << "0\n";
247 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248 else
249 {
250 assert(0 && "Unknown elementary type for constant");
251 }
252}
253
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000254/// isStringCompatible - Can we treat the specified array as a string?
255/// Only if it is an array of ubytes or non-negative sbytes.
256///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000257static bool isStringCompatible(const ConstantArray *CVA) {
258 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
259 if (ETy == Type::UByteTy) return true;
260 if (ETy != Type::SByteTy) return false;
261
262 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
263 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
264 return false;
265
266 return true;
267}
268
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000269/// toOctal - Convert the low order bits of X into an octal digit.
270///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000271static inline char toOctal(int X) {
272 return (X&7)+'0';
273}
274
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000275/// getAsCString - Return the specified array as a C compatible
276/// string, only if the predicate isStringCompatible is true.
277///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000278static std::string getAsCString(const ConstantArray *CVA) {
279 assert(isStringCompatible(CVA) && "Array is not string compatible!");
280
281 std::string Result;
282 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
283 Result = "\"";
284 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000285 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000286
287 if (C == '"') {
288 Result += "\\\"";
289 } else if (C == '\\') {
290 Result += "\\\\";
291 } else if (isprint(C)) {
292 Result += C;
293 } else {
294 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000295 case '\b': Result += "\\b"; break;
296 case '\f': Result += "\\f"; break;
297 case '\n': Result += "\\n"; break;
298 case '\r': Result += "\\r"; break;
299 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000300 default:
301 Result += '\\';
302 Result += toOctal(C >> 6);
303 Result += toOctal(C >> 3);
304 Result += toOctal(C >> 0);
305 break;
306 }
307 }
308 }
309 Result += "\"";
310 return Result;
311}
312
313// Print a constant value or values (it may be an aggregate).
314// Uses printSingleConstantValue() to print each individual value.
315void
316Printer::printConstantValueOnly(const Constant* CV,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000317 int numPadBytesAfter /* = 0 */)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000318{
319 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000320 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000321
322 if (CVA && isStringCompatible(CVA))
323 { // print the string alone and return
Chris Lattnere0121322003-08-03 23:37:09 +0000324 O << "\t.string\t" << getAsCString(CVA) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000325 }
326 else if (CVA)
327 { // Not a string. Print the values in successive locations
328 const std::vector<Use> &constValues = CVA->getValues();
329 for (unsigned i=0; i < constValues.size(); i++)
330 printConstantValueOnly(cast<Constant>(constValues[i].get()));
331 }
332 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
333 { // Print the fields in successive locations. Pad to align if needed!
334 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000335 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000336 const std::vector<Use>& constValues = CVS->getValues();
337 unsigned sizeSoFar = 0;
338 for (unsigned i=0, N = constValues.size(); i < N; i++)
339 {
340 const Constant* field = cast<Constant>(constValues[i].get());
341
342 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000343 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000344 int padSize = ((i == N-1? cvsLayout->StructSize
345 : cvsLayout->MemberOffsets[i+1])
346 - cvsLayout->MemberOffsets[i]) - fieldSize;
347 sizeSoFar += (fieldSize + padSize);
348
349 // Now print the actual field value
350 printConstantValueOnly(field, padSize);
351 }
352 assert(sizeSoFar == cvsLayout->StructSize &&
353 "Layout of constant struct may be incorrect!");
354 }
355 else
356 printSingleConstantValue(CV);
357
Chris Lattnere0121322003-08-03 23:37:09 +0000358 if (numPadBytesAfter) O << "\t.zero\t " << numPadBytesAfter << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000359}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000360
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000361/// printConstantPool - Print to the current output stream assembly
362/// representations of the constants in the constant pool MCP. This is
363/// used to print out constants which have been "spilled to memory" by
364/// the code generator.
365///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000366void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000367 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000368 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000369
Chris Lattnerb7089442003-01-13 00:35:03 +0000370 if (CP.empty()) return;
371
372 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
373 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000374 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000375 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000376 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
377 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000378 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000380}
381
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000382/// runOnMachineFunction - This uses the printMachineInstruction()
383/// method to print assembly for each instruction.
384///
Chris Lattner0285a332002-12-28 20:25:38 +0000385bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386 // BBNumber is used here so that a given Printer will never give two
387 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000388 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000389
Chris Lattnere0121322003-08-03 23:37:09 +0000390 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000391 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000392 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000393
Chris Lattnerb7089442003-01-13 00:35:03 +0000394 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000395 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000396
Brian Gaeke6559bb92002-11-14 22:32:30 +0000397 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000398 O << "\t.text\n";
399 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000400 O << "\t.globl\t" << CurrentFnName << "\n";
401 O << "\t.type\t" << CurrentFnName << ", @function\n";
402 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000403
Brian Gaeked7908f62003-06-27 00:00:48 +0000404 // Number each basic block so that we can consistently refer to them
405 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000406 NumberForBB.clear();
407 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
408 I != E; ++I) {
409 NumberForBB[I->getBasicBlock()] = BBNumber++;
410 }
411
Brian Gaeke6559bb92002-11-14 22:32:30 +0000412 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000413 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
414 I != E; ++I) {
415 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000416 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000417 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000418 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
419 II != E; ++II) {
420 // Print the assembly for the instruction.
421 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000422 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000423 }
Chris Lattner0285a332002-12-28 20:25:38 +0000424 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000425
426 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000427 return false;
428}
429
Chris Lattner3d3067b2002-11-21 20:44:15 +0000430static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000431 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000432 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
433 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000434}
435
436static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 if (MI->getOperand(Op).isFrameIndex()) return true;
438 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000439 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000440 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
441 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000442}
443
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000444void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000445 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000446 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000447 switch (MO.getType()) {
448 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000449 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000450 O << "<" << V->getName() << ">";
451 return;
452 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000453 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000454 case MachineOperand::MO_MachineRegister:
Chris Lattnerf9f60882002-11-18 06:56:51 +0000455 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
456 O << RI.get(MO.getReg()).Name;
457 else
458 O << "%reg" << MO.getReg();
459 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000460
461 case MachineOperand::MO_SignExtendedImmed:
462 case MachineOperand::MO_UnextendedImmed:
463 O << (int)MO.getImmedValue();
464 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000465 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000466 {
467 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
468 assert (i != NumberForBB.end()
469 && "Could not find a BB I previously put in the NumberForBB map!");
Brian Gaeke002a50a2003-07-31 17:38:52 +0000470 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Brian Gaeked7908f62003-06-27 00:00:48 +0000471 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000472 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000473 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000474 if (!elideOffsetKeyword)
475 O << "OFFSET ";
476 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000477 return;
478 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000479 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000481 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000482 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000483 }
484}
485
Chris Lattner3501fea2003-01-14 22:00:31 +0000486static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000487 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000488 default: assert(0 && "Unknown arg size!");
489 case X86II::Arg8: return "BYTE PTR";
490 case X86II::Arg16: return "WORD PTR";
491 case X86II::Arg32: return "DWORD PTR";
492 case X86II::Arg64: return "QWORD PTR";
493 case X86II::ArgF32: return "DWORD PTR";
494 case X86II::ArgF64: return "QWORD PTR";
495 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000496 }
497}
498
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000499void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000500 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000501 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000502
503 if (MI->getOperand(Op).isFrameIndex()) {
504 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
505 if (MI->getOperand(Op+3).getImmedValue())
506 O << " + " << MI->getOperand(Op+3).getImmedValue();
507 O << "]";
508 return;
509 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000510 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000511 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000512 if (MI->getOperand(Op+3).getImmedValue())
513 O << " + " << MI->getOperand(Op+3).getImmedValue();
514 O << "]";
515 return;
516 }
517
Chris Lattner3d3067b2002-11-21 20:44:15 +0000518 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000519 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000520 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000521 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000522
523 O << "[";
524 bool NeedPlus = false;
525 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000526 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000527 NeedPlus = true;
528 }
529
530 if (IndexReg.getReg()) {
531 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000532 if (ScaleVal != 1)
533 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000534 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000535 NeedPlus = true;
536 }
537
Chris Lattner0285a332002-12-28 20:25:38 +0000538 if (DispVal) {
539 if (NeedPlus)
540 if (DispVal > 0)
541 O << " + ";
542 else {
543 O << " - ";
544 DispVal = -DispVal;
545 }
546 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000547 }
548 O << "]";
549}
550
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000551/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000552/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000553///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000554void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000555 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000556 const TargetInstrInfo &TII = TM.getInstrInfo();
557 const TargetInstrDescriptor &Desc = TII.get(Opcode);
558 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000559
Chris Lattnereca1f632002-12-25 05:09:01 +0000560 switch (Desc.TSFlags & X86II::FormMask) {
561 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000562 // Print pseudo-instructions as comments; either they should have been
563 // turned into real instructions by now, or they don't need to be
564 // seen by the assembler (e.g., IMPLICIT_USEs.)
565 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000566 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000567 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000568 O << " = phi ";
569 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
570 if (i != 1) O << ", ";
571 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000572 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000573 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000574 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000575 O << "]";
576 }
577 } else {
578 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000579 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
580 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000581 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000582 O << " = ";
583 ++i;
584 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000585 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000586
587 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
588 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000589 if (MI->getOperand(i).opIsDefOnly() ||
590 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000591 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000592 if (MI->getOperand(i).opIsDefOnly() ||
593 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000594 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000595 }
596 O << "\n";
597 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000598
Chris Lattnerf9f60882002-11-18 06:56:51 +0000599 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000600 // The accepted forms of Raw instructions are:
601 // 1. nop - No operand required
602 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000603 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000604 //
605 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000606 (MI->getNumOperands() == 1 &&
607 (MI->getOperand(0).isPCRelativeDisp() ||
608 MI->getOperand(0).isGlobalAddress() ||
609 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000610 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000611 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000612
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000613 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000614 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000615 }
616 O << "\n";
617 return;
618
Chris Lattner77875d82002-11-21 02:00:20 +0000619 case X86II::AddRegFrm: {
620 // There are currently two forms of acceptable AddRegFrm instructions.
621 // Either the instruction JUST takes a single register (like inc, dec, etc),
622 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000623 // (move immediate f.e.). Note that this immediate value might be stored as
624 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000625 // into a register. The initial register might be duplicated if this is a
626 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000627 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000628 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000629 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000630 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000631 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000632 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000633 MI->getOperand(1).isRegister() ||
634 MI->getOperand(1).isGlobalAddress() ||
635 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000636 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000637
Chris Lattner77875d82002-11-21 02:00:20 +0000638 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000639
Brian Gaeked7908f62003-06-27 00:00:48 +0000640 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000641 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000642 if (MI->getNumOperands() == 2 &&
643 (!MI->getOperand(1).isRegister() ||
644 MI->getOperand(1).getVRegValueOrNull() ||
645 MI->getOperand(1).isGlobalAddress() ||
646 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000647 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000648 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000649 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000650 if (Desc.TSFlags & X86II::PrintImplUses) {
651 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
652 O << ", " << RI.get(*p).Name;
653 }
654 }
Chris Lattner77875d82002-11-21 02:00:20 +0000655 O << "\n";
656 return;
657 }
Chris Lattner233ad712002-11-21 01:33:44 +0000658 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000659 // There are two acceptable forms of MRMDestReg instructions, those with 2,
660 // 3 and 4 operands:
661 //
662 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000663 //
664 // 3 Operands: in this form, the first two registers (the destination, and
665 // the first operand) should be the same, post register allocation. The 3rd
666 // operand is an additional input. This should be for things like add
667 // instructions.
668 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000669 // 4 Operands: This form is for instructions which are 3 operands forms, but
670 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000671 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000672 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000673 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000674 (MI->getNumOperands() == 2 ||
675 (isTwoAddr && MI->getOperand(1).isRegister() &&
676 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
677 (MI->getNumOperands() == 3 ||
678 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000679 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000680
Brian Gaeked7908f62003-06-27 00:00:48 +0000681 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000682 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000683 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000684 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000685 if (MI->getNumOperands() == 4) {
686 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000687 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000688 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 O << "\n";
690 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000691 }
Chris Lattner18042332002-11-21 21:03:39 +0000692
693 case X86II::MRMDestMem: {
694 // These instructions are the same as MRMDestReg, but instead of having a
695 // register reference for the mod/rm field, it's a memory reference.
696 //
697 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000698 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000699
Brian Gaeked7908f62003-06-27 00:00:48 +0000700 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000701 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000702 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000703 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000704 O << "\n";
705 return;
706 }
707
Chris Lattner233ad712002-11-21 01:33:44 +0000708 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000709 // There is a two forms that are acceptable for MRMSrcReg instructions,
710 // those with 3 and 2 operands:
711 //
712 // 3 Operands: in this form, the last register (the second input) is the
713 // ModR/M input. The first two operands should be the same, post register
714 // allocation. This is for things like: add r32, r/m32
715 //
716 // 2 Operands: this is for things like mov that do not read a second input
717 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000718 assert(MI->getOperand(0).isRegister() &&
719 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000720 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000721 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000722 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000723 if (MI->getNumOperands() == 3 &&
724 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
725 O << "**";
726
Brian Gaeked7908f62003-06-27 00:00:48 +0000727 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000728 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000729 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000730 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000731 O << "\n";
732 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000733 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000734
Chris Lattner3d3067b2002-11-21 20:44:15 +0000735 case X86II::MRMSrcMem: {
736 // These instructions are the same as MRMSrcReg, but instead of having a
737 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000738 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000739 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000740 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000741 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000742 isMem(MI, 2))
743 && "Bad format for MRMDestReg!");
744 if (MI->getNumOperands() == 2+4 &&
745 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
746 O << "**";
747
Brian Gaeked7908f62003-06-27 00:00:48 +0000748 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000749 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000750 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000751 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000752 O << "\n";
753 return;
754 }
755
Chris Lattner675dd2c2002-11-21 17:09:01 +0000756 case X86II::MRMS0r: case X86II::MRMS1r:
757 case X86II::MRMS2r: case X86II::MRMS3r:
758 case X86II::MRMS4r: case X86II::MRMS5r:
759 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000760 // In this form, the following are valid formats:
761 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000762 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000763 // 2. shl rdest, rinput <implicit CL or 1>
764 // 3. sbb rdest, rinput, immediate [rdest = rinput]
765 //
766 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000767 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000768 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000769 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000770 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000771 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000772 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000773 "Bad MRMSxR format!");
774
Chris Lattnerd9096832002-12-15 08:01:39 +0000775 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000776 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
777 O << "**";
778
Brian Gaeked7908f62003-06-27 00:00:48 +0000779 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000780 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000781 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000782 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000783 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000784 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000785 if (Desc.TSFlags & X86II::PrintImplUses) {
786 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
787 O << ", " << RI.get(*p).Name;
788 }
789 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 O << "\n";
791
792 return;
793 }
794
Chris Lattnerb7089442003-01-13 00:35:03 +0000795 case X86II::MRMS0m: case X86II::MRMS1m:
796 case X86II::MRMS2m: case X86II::MRMS3m:
797 case X86II::MRMS4m: case X86II::MRMS5m:
798 case X86II::MRMS6m: case X86II::MRMS7m: {
799 // In this form, the following are valid formats:
800 // 1. sete [m]
801 // 2. cmp [m], immediate
802 // 2. shl [m], rinput <implicit CL or 1>
803 // 3. sbb [m], immediate
804 //
805 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
806 isMem(MI, 0) && "Bad MRMSxM format!");
807 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
808 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000809 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
810 // is misassembled by gas in intel_syntax mode as its 32-bit
811 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
812 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000813 if (MI->getOpCode() == X86::FSTPr80) {
814 if ((MI->getOperand(0).getReg() == X86::ESP)
815 && (MI->getOperand(1).getImmedValue() == 1)) {
816 int DispVal = MI->getOperand(3).getImmedValue();
817 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
818 unsigned int val = (unsigned int) DispVal;
819 O << ".byte 0xdb, 0xbc, 0x24\n\t";
820 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
821 } else { // 1 byte disp.
822 unsigned char val = (unsigned char) DispVal;
823 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
824 << std::dec << "\t# ";
825 }
826 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000827 }
828 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
829 // misassembled by gas in intel_syntax mode as its 32-bit
830 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
831 // opcode bytes instead of the instruction.
832 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000833 if ((MI->getOperand(0).getReg() == X86::ESP)
834 && (MI->getOperand(1).getImmedValue() == 1)) {
835 int DispVal = MI->getOperand(3).getImmedValue();
836 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
837 unsigned int val = (unsigned int) DispVal;
838 O << ".byte 0xdb, 0xac, 0x24\n\t";
839 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
840 } else { // 1 byte disp.
841 unsigned char val = (unsigned char) DispVal;
842 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
843 << std::dec << "\t# ";
844 }
845 }
846 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000847 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
848 // invalid opcode, saying "64 bit operations are only supported in
849 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
850 // [...]", which is wrong. Workaround: Output the raw opcode bytes
851 // instead of the instruction.
852 if (MI->getOpCode() == X86::FILDr64) {
853 if ((MI->getOperand(0).getReg() == X86::ESP)
854 && (MI->getOperand(1).getImmedValue() == 1)) {
855 int DispVal = MI->getOperand(3).getImmedValue();
856 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
857 unsigned int val = (unsigned int) DispVal;
858 O << ".byte 0xdf, 0xac, 0x24\n\t";
859 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
860 } else { // 1 byte disp.
861 unsigned char val = (unsigned char) DispVal;
862 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
863 << std::dec << "\t# ";
864 }
865 }
866 }
867 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
868 // an invalid opcode, saying "64 bit operations are only
869 // supported in 64 bit modes." libopcodes disassembles it as
870 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
871 // "fistpll DWORD PTR " instead, which is what libopcodes is
872 // expecting to see.
873 if (MI->getOpCode() == X86::FISTPr64) {
874 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000875 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000876 if (MI->getNumOperands() == 5) {
877 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000878 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000879 }
880 O << "\t# ";
881 }
882
Brian Gaeked7908f62003-06-27 00:00:48 +0000883 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000884 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000885 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000886 if (MI->getNumOperands() == 5) {
887 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000888 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000889 }
890 O << "\n";
891 return;
892 }
893
Chris Lattnerf9f60882002-11-18 06:56:51 +0000894 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000895 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000896 }
Chris Lattner72614082002-10-25 22:55:53 +0000897}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000898
899bool Printer::doInitialization(Module &M)
900{
901 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly,
902 // with no % decorations on register names.
903 O << "\t.intel_syntax noprefix\n";
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000904 Mang = new Mangler(M);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000905 return false; // success
906}
907
Chris Lattnerc07736a2003-07-23 15:22:26 +0000908static const Function *isConstantFunctionPointerRef(const Constant *C) {
909 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
910 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000911 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000912 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000913}
914
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000915bool Printer::doFinalization(Module &M)
916{
Brian Gaekede420ae2003-07-23 20:25:08 +0000917 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000918 // Print out module-level global variables here.
919 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000920 std::string name(Mang->getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000921 if (I->hasInitializer()) {
922 Constant *C = I->getInitializer();
Chris Lattnere0121322003-08-03 23:37:09 +0000923 if (C->isNullValue()) {
924 O << "\n\n\t.comm " << name << "," << TD.getTypeSize(C->getType())
925 << "," << (unsigned)TD.getTypeAlignment(C->getType());
926 O << "\t\t# ";
927 WriteAsOperand(O, I, true, true, &M);
928 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000929 } else {
Chris Lattnere0121322003-08-03 23:37:09 +0000930 O << "\n\n\t.data\n";
931 O << "\t.globl " << name << "\n";
932 O << "\t.type " << name << ",@object\n";
933 O << "\t.size " << name << "," << TD.getTypeSize(C->getType()) << "\n";
934 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
935 O << name << ":\t\t\t\t# ";
936 WriteAsOperand(O, I, true, true, &M);
937 O << " = ";
938 WriteAsOperand(O, C, false, false, &M);
939 O << "\n";
940 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000941 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000942 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000943 O << "\t.globl " << name << "\n";
944 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000945 << (unsigned)TD.getTypeSize(I->getType()) << ", "
946 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000947 }
948 }
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000949 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000950 return false; // success
951}