blob: db55593ad7a7c731ddc8b7a38902a276cda012f9 [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
Chris Lattner93c1afa2003-08-11 19:35:26 +00006// by `llc' and `lli -print-machineinstrs' 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 Lattner93c1afa2003-08-11 19:35:26 +000023#include "Support/CommandLine.h"
Chris Lattner72614082002-10-25 22:55:53 +000024
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000025namespace {
Chris Lattner93c1afa2003-08-11 19:35:26 +000026 // FIXME: This should be automatically picked up by autoconf from the C
27 // frontend
28 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
29 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
30
Chris Lattner0285a332002-12-28 20:25:38 +000031 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000032 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000033 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000034 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000035
36 /// Target machine description which we query for reg. names, data
37 /// layout, etc.
38 ///
39 TargetMachine &TM;
40
Brian Gaeked9fb37a2003-07-24 20:20:44 +000041 /// Name-mangler for global names.
42 ///
43 Mangler *Mang;
44
Brian Gaekede420ae2003-07-23 20:25:08 +000045 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 Gaeke2a098772003-08-11 19:05:46 +000064 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000065 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000066 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000067 bool elideOffsetKeyword = false);
68 void printMemReference(const MachineInstr *MI, unsigned Op);
69 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000070 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000071 std::string ConstantExprToString(const ConstantExpr* CE);
72 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000073 bool doInitialization(Module &M);
74 bool doFinalization(Module &M);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000075 void printConstantValueOnly(const Constant* CV, int numPadBytesAfter = 0);
76 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 };
Brian Gaeked7908f62003-06-27 00:00:48 +000078} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000079
Brian Gaeke92bdfe62003-07-23 18:37:06 +000080/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000081/// assembly code for a MachineFunction to the given output stream,
82/// using the given target machine description. This should work
83/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000084///
Brian Gaekede420ae2003-07-23 20:25:08 +000085Pass *createX86CodePrinterPass(std::ostream &o, TargetMachine &tm) {
86 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000087}
88
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089/// valToExprString - Helper function for ConstantExprToString().
90/// Appends result to argument string S.
91///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000092std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000093 std::string S;
94 bool failed = false;
95 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
96 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
97 S += std::string(CB == ConstantBool::True ? "1" : "0");
98 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
99 S += itostr(CI->getValue());
100 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
101 S += utostr(CI->getValue());
102 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
103 S += ftostr(CFP->getValue());
104 else if (isa<ConstantPointerNull>(CV))
105 S += "0";
106 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
107 S += valToExprString(CPR->getValue());
108 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
109 S += ConstantExprToString(CE);
110 else
111 failed = true;
112 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000113 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000114 }
115 else
116 failed = true;
117
118 if (failed) {
119 assert(0 && "Cannot convert value to string");
120 S += "<illegal-value>";
121 }
122 return S;
123}
124
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000125/// ConstantExprToString - Convert a ConstantExpr to an asm expression
126/// and return this as a string.
127///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000128std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000129 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000130 switch(CE->getOpcode()) {
131 case Instruction::GetElementPtr:
132 { // generate a symbolic expression for the byte address
133 const Value* ptrVal = CE->getOperand(0);
134 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000135 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
136 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
137 else
138 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000139 }
140
141 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000142 // Support only non-converting or widening casts for now, that is,
143 // ones that do not involve a change in value. This assertion is
144 // not a complete check.
145 {
146 Constant *Op = CE->getOperand(0);
147 const Type *OpTy = Op->getType(), *Ty = CE->getType();
148 assert(((isa<PointerType>(OpTy)
149 && (Ty == Type::LongTy || Ty == Type::ULongTy))
150 || (isa<PointerType>(Ty)
151 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
152 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000153 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000154 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000155 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000156 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000157
158 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000159 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
160 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000161
162 default:
163 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000164 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166}
167
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000168/// printSingleConstantValue - Print a single constant value.
169///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000171Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000172{
173 assert(CV->getType() != Type::VoidTy &&
174 CV->getType() != Type::TypeTy &&
175 CV->getType() != Type::LabelTy &&
176 "Unexpected type for Constant");
177
178 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
179 && "Aggregate types should be handled outside this function");
180
181 const Type *type = CV->getType();
182 O << "\t";
183 switch(type->getPrimitiveID())
184 {
185 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
186 O << ".byte";
187 break;
188 case Type::UShortTyID: case Type::ShortTyID:
189 O << ".word";
190 break;
191 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
192 O << ".long";
193 break;
194 case Type::ULongTyID: case Type::LongTyID:
195 O << ".quad";
196 break;
197 case Type::FloatTyID:
198 O << ".long";
199 break;
200 case Type::DoubleTyID:
201 O << ".quad";
202 break;
203 case Type::ArrayTyID:
204 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
205 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
206 O << ".string";
207 else
208 assert (0 && "Can't handle printing this type of array");
209 break;
210 default:
211 assert (0 && "Can't handle printing this type of thing");
212 break;
213 }
214 O << "\t";
215
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000216 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
217 {
218 // Constant expression built from operators, constants, and
219 // symbolic addrs
220 O << ConstantExprToString(CE) << "\n";
221 }
222 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000223 {
224 if (type->isFloatingPoint()) {
225 // FP Constants are printed as integer constants to avoid losing
226 // precision...
227 double Val = cast<ConstantFP>(CV)->getValue();
228 if (type == Type::FloatTy) {
229 float FVal = (float)Val;
230 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
231 O << *(unsigned int*)ProxyPtr;
232 } else if (type == Type::DoubleTy) {
233 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
234 O << *(uint64_t*)ProxyPtr;
235 } else {
236 assert(0 && "Unknown floating point type!");
237 }
238
239 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
240 } else {
241 WriteAsOperand(O, CV, false, false) << "\n";
242 }
243 }
244 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
245 {
246 // This is a constant address for a global variable or method.
247 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000248 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000249 }
250 else if (isa<ConstantPointerNull>(CV))
251 {
252 // Null pointer value
253 O << "0\n";
254 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000255 else
256 {
257 assert(0 && "Unknown elementary type for constant");
258 }
259}
260
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000261/// isStringCompatible - Can we treat the specified array as a string?
262/// Only if it is an array of ubytes or non-negative sbytes.
263///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264static bool isStringCompatible(const ConstantArray *CVA) {
265 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
266 if (ETy == Type::UByteTy) return true;
267 if (ETy != Type::SByteTy) return false;
268
269 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
270 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
271 return false;
272
273 return true;
274}
275
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000276/// toOctal - Convert the low order bits of X into an octal digit.
277///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000278static inline char toOctal(int X) {
279 return (X&7)+'0';
280}
281
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000282/// getAsCString - Return the specified array as a C compatible
283/// string, only if the predicate isStringCompatible is true.
284///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000285static std::string getAsCString(const ConstantArray *CVA) {
286 assert(isStringCompatible(CVA) && "Array is not string compatible!");
287
288 std::string Result;
289 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
290 Result = "\"";
291 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000292 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000293
294 if (C == '"') {
295 Result += "\\\"";
296 } else if (C == '\\') {
297 Result += "\\\\";
298 } else if (isprint(C)) {
299 Result += C;
300 } else {
301 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000302 case '\b': Result += "\\b"; break;
303 case '\f': Result += "\\f"; break;
304 case '\n': Result += "\\n"; break;
305 case '\r': Result += "\\r"; break;
306 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000307 default:
308 Result += '\\';
309 Result += toOctal(C >> 6);
310 Result += toOctal(C >> 3);
311 Result += toOctal(C >> 0);
312 break;
313 }
314 }
315 }
316 Result += "\"";
317 return Result;
318}
319
320// Print a constant value or values (it may be an aggregate).
321// Uses printSingleConstantValue() to print each individual value.
322void
323Printer::printConstantValueOnly(const Constant* CV,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000324 int numPadBytesAfter /* = 0 */)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000325{
326 const ConstantArray *CVA = dyn_cast<ConstantArray>(CV);
Brian Gaekede420ae2003-07-23 20:25:08 +0000327 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000328
329 if (CVA && isStringCompatible(CVA))
330 { // print the string alone and return
Chris Lattnere0121322003-08-03 23:37:09 +0000331 O << "\t.string\t" << getAsCString(CVA) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000332 }
333 else if (CVA)
334 { // Not a string. Print the values in successive locations
335 const std::vector<Use> &constValues = CVA->getValues();
336 for (unsigned i=0; i < constValues.size(); i++)
337 printConstantValueOnly(cast<Constant>(constValues[i].get()));
338 }
339 else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
340 { // Print the fields in successive locations. Pad to align if needed!
341 const StructLayout *cvsLayout =
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000342 TD.getStructLayout(CVS->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000343 const std::vector<Use>& constValues = CVS->getValues();
344 unsigned sizeSoFar = 0;
345 for (unsigned i=0, N = constValues.size(); i < N; i++)
346 {
347 const Constant* field = cast<Constant>(constValues[i].get());
348
349 // Check if padding is needed and insert one or more 0s.
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000350 unsigned fieldSize = TD.getTypeSize(field->getType());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000351 int padSize = ((i == N-1? cvsLayout->StructSize
352 : cvsLayout->MemberOffsets[i+1])
353 - cvsLayout->MemberOffsets[i]) - fieldSize;
354 sizeSoFar += (fieldSize + padSize);
355
356 // Now print the actual field value
357 printConstantValueOnly(field, padSize);
358 }
359 assert(sizeSoFar == cvsLayout->StructSize &&
360 "Layout of constant struct may be incorrect!");
361 }
362 else
363 printSingleConstantValue(CV);
364
Chris Lattnere0121322003-08-03 23:37:09 +0000365 if (numPadBytesAfter) O << "\t.zero\t " << numPadBytesAfter << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000366}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000367
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000368/// printConstantPool - Print to the current output stream assembly
369/// representations of the constants in the constant pool MCP. This is
370/// used to print out constants which have been "spilled to memory" by
371/// the code generator.
372///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000373void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000374 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000375 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000376
Chris Lattnerb7089442003-01-13 00:35:03 +0000377 if (CP.empty()) return;
378
379 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
380 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000381 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000382 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000383 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
384 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000385 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000386 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000387}
388
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000389/// runOnMachineFunction - This uses the printMachineInstruction()
390/// method to print assembly for each instruction.
391///
Chris Lattner0285a332002-12-28 20:25:38 +0000392bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000393 // BBNumber is used here so that a given Printer will never give two
394 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000395 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000396
Chris Lattnere0121322003-08-03 23:37:09 +0000397 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000398 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000399 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000400
Chris Lattnerb7089442003-01-13 00:35:03 +0000401 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000402 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000403
Brian Gaeke6559bb92002-11-14 22:32:30 +0000404 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000405 O << "\t.text\n";
406 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000407 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000408 if (!EmitCygwin)
409 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000410 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000411
Brian Gaeked7908f62003-06-27 00:00:48 +0000412 // Number each basic block so that we can consistently refer to them
413 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 NumberForBB.clear();
415 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
416 I != E; ++I) {
417 NumberForBB[I->getBasicBlock()] = BBNumber++;
418 }
419
Brian Gaeke6559bb92002-11-14 22:32:30 +0000420 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000421 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
422 I != E; ++I) {
423 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000424 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000425 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000426 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
427 II != E; ++II) {
428 // Print the assembly for the instruction.
429 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000430 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000431 }
Chris Lattner0285a332002-12-28 20:25:38 +0000432 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000433
434 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000435 return false;
436}
437
Chris Lattner3d3067b2002-11-21 20:44:15 +0000438static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000439 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000440 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
441 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000442}
443
444static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000445 if (MI->getOperand(Op).isFrameIndex()) return true;
446 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000447 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000448 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
449 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000450}
451
Brian Gaeke2a098772003-08-11 19:05:46 +0000452
453
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000454void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000455 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000456 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000457 switch (MO.getType()) {
458 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000459 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000460 O << "<" << V->getName() << ">";
461 return;
462 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000463 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000464 case MachineOperand::MO_MachineRegister:
Chris Lattner93c1afa2003-08-11 19:35:26 +0000465 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000466 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner93c1afa2003-08-11 19:35:26 +0000467 O << RI.get(MO.getReg()).Name;
468 } else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000469 O << "%reg" << MO.getReg();
470 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000471
472 case MachineOperand::MO_SignExtendedImmed:
473 case MachineOperand::MO_UnextendedImmed:
474 O << (int)MO.getImmedValue();
475 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000476 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000477 {
478 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
479 assert (i != NumberForBB.end()
480 && "Could not find a BB I previously put in the NumberForBB map!");
Brian Gaeke002a50a2003-07-31 17:38:52 +0000481 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Brian Gaeked7908f62003-06-27 00:00:48 +0000482 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000483 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000484 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000485 if (!elideOffsetKeyword)
486 O << "OFFSET ";
487 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000488 return;
489 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000490 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000491 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000492 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000493 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000494 }
495}
496
Chris Lattner3501fea2003-01-14 22:00:31 +0000497static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000498 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000499 default: assert(0 && "Unknown arg size!");
500 case X86II::Arg8: return "BYTE PTR";
501 case X86II::Arg16: return "WORD PTR";
502 case X86II::Arg32: return "DWORD PTR";
503 case X86II::Arg64: return "QWORD PTR";
504 case X86II::ArgF32: return "DWORD PTR";
505 case X86II::ArgF64: return "QWORD PTR";
506 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000507 }
508}
509
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000510void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000511 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000512
513 if (MI->getOperand(Op).isFrameIndex()) {
514 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
515 if (MI->getOperand(Op+3).getImmedValue())
516 O << " + " << MI->getOperand(Op+3).getImmedValue();
517 O << "]";
518 return;
519 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000520 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000521 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000522 if (MI->getOperand(Op+3).getImmedValue())
523 O << " + " << MI->getOperand(Op+3).getImmedValue();
524 O << "]";
525 return;
526 }
527
Chris Lattner3d3067b2002-11-21 20:44:15 +0000528 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000529 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000530 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000531 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000532
533 O << "[";
534 bool NeedPlus = false;
535 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000536 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000537 NeedPlus = true;
538 }
539
540 if (IndexReg.getReg()) {
541 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000542 if (ScaleVal != 1)
543 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000544 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000545 NeedPlus = true;
546 }
547
Chris Lattner0285a332002-12-28 20:25:38 +0000548 if (DispVal) {
549 if (NeedPlus)
550 if (DispVal > 0)
551 O << " + ";
552 else {
553 O << " - ";
554 DispVal = -DispVal;
555 }
556 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000557 }
558 O << "]";
559}
560
Brian Gaeke2a098772003-08-11 19:05:46 +0000561/// checkImplUses - Emit the implicit-use registers for the
562/// instruction described by DESC, if its PrintImplUses flag is set.
563///
564void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
565 const MRegisterInfo &RI = *TM.getRegisterInfo();
566 if (Desc.TSFlags & X86II::PrintImplUses) {
567 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
568 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000569 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000570 }
571 }
572}
573
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000574/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000575/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000576///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000577void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000578 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000579 const TargetInstrInfo &TII = TM.getInstrInfo();
580 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000581
Chris Lattnereca1f632002-12-25 05:09:01 +0000582 switch (Desc.TSFlags & X86II::FormMask) {
583 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000584 // Print pseudo-instructions as comments; either they should have been
585 // turned into real instructions by now, or they don't need to be
586 // seen by the assembler (e.g., IMPLICIT_USEs.)
587 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000589 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000590 O << " = phi ";
591 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
592 if (i != 1) O << ", ";
593 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000594 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000595 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000596 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000597 O << "]";
598 }
599 } else {
600 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000601 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
602 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000603 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000604 O << " = ";
605 ++i;
606 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000607 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000608
609 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
610 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000611 if (MI->getOperand(i).opIsDefOnly() ||
612 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000613 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000614 if (MI->getOperand(i).opIsDefOnly() ||
615 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000616 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000617 }
618 O << "\n";
619 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000620
Chris Lattnerf9f60882002-11-18 06:56:51 +0000621 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000622 // The accepted forms of Raw instructions are:
623 // 1. nop - No operand required
624 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000625 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000626 //
627 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000628 (MI->getNumOperands() == 1 &&
629 (MI->getOperand(0).isPCRelativeDisp() ||
630 MI->getOperand(0).isGlobalAddress() ||
631 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000632 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000633 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000634
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000635 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000636 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000637 }
638 O << "\n";
639 return;
640
Chris Lattner77875d82002-11-21 02:00:20 +0000641 case X86II::AddRegFrm: {
642 // There are currently two forms of acceptable AddRegFrm instructions.
643 // Either the instruction JUST takes a single register (like inc, dec, etc),
644 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000645 // (move immediate f.e.). Note that this immediate value might be stored as
646 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000647 // into a register. The initial register might be duplicated if this is a
648 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000649 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000650 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000651 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000652 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000653 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000654 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000655 MI->getOperand(1).isRegister() ||
656 MI->getOperand(1).isGlobalAddress() ||
657 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000658 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000659
Chris Lattner77875d82002-11-21 02:00:20 +0000660 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000661
Brian Gaeked7908f62003-06-27 00:00:48 +0000662 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000663 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000664 if (MI->getNumOperands() == 2 &&
665 (!MI->getOperand(1).isRegister() ||
666 MI->getOperand(1).getVRegValueOrNull() ||
667 MI->getOperand(1).isGlobalAddress() ||
668 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000669 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000670 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000671 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000672 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000673 O << "\n";
674 return;
675 }
Chris Lattner233ad712002-11-21 01:33:44 +0000676 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 // There are two acceptable forms of MRMDestReg instructions, those with 2,
678 // 3 and 4 operands:
679 //
680 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000681 //
682 // 3 Operands: in this form, the first two registers (the destination, and
683 // the first operand) should be the same, post register allocation. The 3rd
684 // operand is an additional input. This should be for things like add
685 // instructions.
686 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000687 // 4 Operands: This form is for instructions which are 3 operands forms, but
688 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000690 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000691 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 (MI->getNumOperands() == 2 ||
693 (isTwoAddr && MI->getOperand(1).isRegister() &&
694 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
695 (MI->getNumOperands() == 3 ||
696 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000697 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +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 Lattnerf9f60882002-11-18 06:56:51 +0000701 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000702 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000703 if (MI->getNumOperands() == 4) {
704 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000705 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000706 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000707 O << "\n";
708 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000709 }
Chris Lattner18042332002-11-21 21:03:39 +0000710
711 case X86II::MRMDestMem: {
712 // These instructions are the same as MRMDestReg, but instead of having a
713 // register reference for the mod/rm field, it's a memory reference.
714 //
715 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000716 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000717
Brian Gaeked7908f62003-06-27 00:00:48 +0000718 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000719 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000720 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000722 O << "\n";
723 return;
724 }
725
Chris Lattner233ad712002-11-21 01:33:44 +0000726 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000727 // There is a two forms that are acceptable for MRMSrcReg instructions,
728 // those with 3 and 2 operands:
729 //
730 // 3 Operands: in this form, the last register (the second input) is the
731 // ModR/M input. The first two operands should be the same, post register
732 // allocation. This is for things like: add r32, r/m32
733 //
734 // 2 Operands: this is for things like mov that do not read a second input
735 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 assert(MI->getOperand(0).isRegister() &&
737 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000738 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000739 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000740 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000741 if (MI->getNumOperands() == 3 &&
742 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
743 O << "**";
744
Brian Gaeked7908f62003-06-27 00:00:48 +0000745 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000746 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000747 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000748 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000749 O << "\n";
750 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000751 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000752
Chris Lattner3d3067b2002-11-21 20:44:15 +0000753 case X86II::MRMSrcMem: {
754 // These instructions are the same as MRMSrcReg, but instead of having a
755 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000756 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000757 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000758 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000759 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000760 isMem(MI, 2))
761 && "Bad format for MRMDestReg!");
762 if (MI->getNumOperands() == 2+4 &&
763 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
764 O << "**";
765
Brian Gaeked7908f62003-06-27 00:00:48 +0000766 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000767 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000768 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000769 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000770 O << "\n";
771 return;
772 }
773
Chris Lattner675dd2c2002-11-21 17:09:01 +0000774 case X86II::MRMS0r: case X86II::MRMS1r:
775 case X86II::MRMS2r: case X86II::MRMS3r:
776 case X86II::MRMS4r: case X86II::MRMS5r:
777 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000778 // In this form, the following are valid formats:
779 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000780 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000781 // 2. shl rdest, rinput <implicit CL or 1>
782 // 3. sbb rdest, rinput, immediate [rdest = rinput]
783 //
784 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000785 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000786 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000787 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000788 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000789 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000790 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000791 "Bad MRMSxR format!");
792
Chris Lattnerd9096832002-12-15 08:01:39 +0000793 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000794 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
795 O << "**";
796
Brian Gaeked7908f62003-06-27 00:00:48 +0000797 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000798 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000799 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000801 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000802 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000803 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000804 O << "\n";
805
806 return;
807 }
808
Chris Lattnerb7089442003-01-13 00:35:03 +0000809 case X86II::MRMS0m: case X86II::MRMS1m:
810 case X86II::MRMS2m: case X86II::MRMS3m:
811 case X86II::MRMS4m: case X86II::MRMS5m:
812 case X86II::MRMS6m: case X86II::MRMS7m: {
813 // In this form, the following are valid formats:
814 // 1. sete [m]
815 // 2. cmp [m], immediate
816 // 2. shl [m], rinput <implicit CL or 1>
817 // 3. sbb [m], immediate
818 //
819 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
820 isMem(MI, 0) && "Bad MRMSxM format!");
821 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
822 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000823 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
824 // is misassembled by gas in intel_syntax mode as its 32-bit
825 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
826 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000827 if (MI->getOpCode() == X86::FSTPr80) {
828 if ((MI->getOperand(0).getReg() == X86::ESP)
829 && (MI->getOperand(1).getImmedValue() == 1)) {
830 int DispVal = MI->getOperand(3).getImmedValue();
831 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
832 unsigned int val = (unsigned int) DispVal;
833 O << ".byte 0xdb, 0xbc, 0x24\n\t";
834 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
835 } else { // 1 byte disp.
836 unsigned char val = (unsigned char) DispVal;
837 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
838 << std::dec << "\t# ";
839 }
840 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000841 }
842 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
843 // misassembled by gas in intel_syntax mode as its 32-bit
844 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
845 // opcode bytes instead of the instruction.
846 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000847 if ((MI->getOperand(0).getReg() == X86::ESP)
848 && (MI->getOperand(1).getImmedValue() == 1)) {
849 int DispVal = MI->getOperand(3).getImmedValue();
850 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
851 unsigned int val = (unsigned int) DispVal;
852 O << ".byte 0xdb, 0xac, 0x24\n\t";
853 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
854 } else { // 1 byte disp.
855 unsigned char val = (unsigned char) DispVal;
856 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
857 << std::dec << "\t# ";
858 }
859 }
860 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000861 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
862 // invalid opcode, saying "64 bit operations are only supported in
863 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
864 // [...]", which is wrong. Workaround: Output the raw opcode bytes
865 // instead of the instruction.
866 if (MI->getOpCode() == X86::FILDr64) {
867 if ((MI->getOperand(0).getReg() == X86::ESP)
868 && (MI->getOperand(1).getImmedValue() == 1)) {
869 int DispVal = MI->getOperand(3).getImmedValue();
870 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
871 unsigned int val = (unsigned int) DispVal;
872 O << ".byte 0xdf, 0xac, 0x24\n\t";
873 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
874 } else { // 1 byte disp.
875 unsigned char val = (unsigned char) DispVal;
876 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
877 << std::dec << "\t# ";
878 }
879 }
880 }
881 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
882 // an invalid opcode, saying "64 bit operations are only
883 // supported in 64 bit modes." libopcodes disassembles it as
884 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
885 // "fistpll DWORD PTR " instead, which is what libopcodes is
886 // expecting to see.
887 if (MI->getOpCode() == X86::FISTPr64) {
888 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000889 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000890 if (MI->getNumOperands() == 5) {
891 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000892 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000893 }
894 O << "\t# ";
895 }
896
Brian Gaeked7908f62003-06-27 00:00:48 +0000897 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000898 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000899 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000900 if (MI->getNumOperands() == 5) {
901 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000902 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000903 }
904 O << "\n";
905 return;
906 }
907
Chris Lattnerf9f60882002-11-18 06:56:51 +0000908 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000909 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000910 }
Chris Lattner72614082002-10-25 22:55:53 +0000911}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000912
Chris Lattner93c1afa2003-08-11 19:35:26 +0000913bool Printer::doInitialization(Module &M) {
914 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000915 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000916 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
917 // instruction as a reference to the register named sp, and if you try to
918 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
919 // before being looked up in the symbol table. This creates spurious
920 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
921 // mode, and decorate all register names with percent signs.
922 //
923 // Cygwin presumably doesn't have this problem, so drop the %'s.
924 //
Chris Lattner67488a92003-08-11 20:04:57 +0000925 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000926 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000927 return false; // success
928}
929
Chris Lattnerc07736a2003-07-23 15:22:26 +0000930static const Function *isConstantFunctionPointerRef(const Constant *C) {
931 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
932 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000933 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000934 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000935}
936
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000937bool Printer::doFinalization(Module &M)
938{
Brian Gaekede420ae2003-07-23 20:25:08 +0000939 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000940 // Print out module-level global variables here.
941 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000942 std::string name(Mang->getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000943 if (I->hasInitializer()) {
944 Constant *C = I->getInitializer();
Chris Lattnere0121322003-08-03 23:37:09 +0000945 if (C->isNullValue()) {
946 O << "\n\n\t.comm " << name << "," << TD.getTypeSize(C->getType())
947 << "," << (unsigned)TD.getTypeAlignment(C->getType());
948 O << "\t\t# ";
949 WriteAsOperand(O, I, true, true, &M);
950 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000951 } else {
Chris Lattnere0121322003-08-03 23:37:09 +0000952 O << "\n\n\t.data\n";
953 O << "\t.globl " << name << "\n";
954 O << "\t.type " << name << ",@object\n";
955 O << "\t.size " << name << "," << TD.getTypeSize(C->getType()) << "\n";
956 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
957 O << name << ":\t\t\t\t# ";
958 WriteAsOperand(O, I, true, true, &M);
959 O << " = ";
960 WriteAsOperand(O, C, false, false, &M);
961 O << "\n";
962 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000963 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000964 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000965 O << "\t.globl " << name << "\n";
966 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000967 << (unsigned)TD.getTypeSize(I->getType()) << ", "
968 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000969 }
970 }
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000971 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000972 return false; // success
973}