blob: 3fc6c6f44646d047e26a598053f1823e4940c3b2 [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 if (!EmitCygwin) O << "%";
468 O << RI.get(MO.getReg()).Name;
469 } else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000470 O << "%reg" << MO.getReg();
471 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000472
473 case MachineOperand::MO_SignExtendedImmed:
474 case MachineOperand::MO_UnextendedImmed:
475 O << (int)MO.getImmedValue();
476 return;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000477 case MachineOperand::MO_PCRelativeDisp:
Brian Gaeked7908f62003-06-27 00:00:48 +0000478 {
479 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
480 assert (i != NumberForBB.end()
481 && "Could not find a BB I previously put in the NumberForBB map!");
Brian Gaeke002a50a2003-07-31 17:38:52 +0000482 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Brian Gaeked7908f62003-06-27 00:00:48 +0000483 }
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000484 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000485 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000486 if (!elideOffsetKeyword)
487 O << "OFFSET ";
488 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000489 return;
490 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000491 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000492 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000493 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000494 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000495 }
496}
497
Chris Lattner3501fea2003-01-14 22:00:31 +0000498static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000499 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000500 default: assert(0 && "Unknown arg size!");
501 case X86II::Arg8: return "BYTE PTR";
502 case X86II::Arg16: return "WORD PTR";
503 case X86II::Arg32: return "DWORD PTR";
504 case X86II::Arg64: return "QWORD PTR";
505 case X86II::ArgF32: return "DWORD PTR";
506 case X86II::ArgF64: return "QWORD PTR";
507 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000508 }
509}
510
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000511void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000512 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000513
514 if (MI->getOperand(Op).isFrameIndex()) {
515 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
516 if (MI->getOperand(Op+3).getImmedValue())
517 O << " + " << MI->getOperand(Op+3).getImmedValue();
518 O << "]";
519 return;
520 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000521 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000522 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000523 if (MI->getOperand(Op+3).getImmedValue())
524 O << " + " << MI->getOperand(Op+3).getImmedValue();
525 O << "]";
526 return;
527 }
528
Chris Lattner3d3067b2002-11-21 20:44:15 +0000529 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000530 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000531 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000532 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000533
534 O << "[";
535 bool NeedPlus = false;
536 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000537 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000538 NeedPlus = true;
539 }
540
541 if (IndexReg.getReg()) {
542 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000543 if (ScaleVal != 1)
544 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000545 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000546 NeedPlus = true;
547 }
548
Chris Lattner0285a332002-12-28 20:25:38 +0000549 if (DispVal) {
550 if (NeedPlus)
551 if (DispVal > 0)
552 O << " + ";
553 else {
554 O << " - ";
555 DispVal = -DispVal;
556 }
557 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000558 }
559 O << "]";
560}
561
Brian Gaeke2a098772003-08-11 19:05:46 +0000562/// checkImplUses - Emit the implicit-use registers for the
563/// instruction described by DESC, if its PrintImplUses flag is set.
564///
565void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
566 const MRegisterInfo &RI = *TM.getRegisterInfo();
567 if (Desc.TSFlags & X86II::PrintImplUses) {
568 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
569 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner93c1afa2003-08-11 19:35:26 +0000570 O << ", " << (EmitCygwin ? "" : "%") << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000571 }
572 }
573}
574
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000575/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000576/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000577///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000578void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000579 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000580 const TargetInstrInfo &TII = TM.getInstrInfo();
581 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000582
Chris Lattnereca1f632002-12-25 05:09:01 +0000583 switch (Desc.TSFlags & X86II::FormMask) {
584 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000585 // Print pseudo-instructions as comments; either they should have been
586 // turned into real instructions by now, or they don't need to be
587 // seen by the assembler (e.g., IMPLICIT_USEs.)
588 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000589 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000590 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000591 O << " = phi ";
592 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
593 if (i != 1) O << ", ";
594 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000595 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000596 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000597 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000598 O << "]";
599 }
600 } else {
601 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000602 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
603 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000604 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000605 O << " = ";
606 ++i;
607 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000608 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000609
610 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
611 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000612 if (MI->getOperand(i).opIsDefOnly() ||
613 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000614 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000615 if (MI->getOperand(i).opIsDefOnly() ||
616 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000617 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000618 }
619 O << "\n";
620 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000621
Chris Lattnerf9f60882002-11-18 06:56:51 +0000622 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000623 // The accepted forms of Raw instructions are:
624 // 1. nop - No operand required
625 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000626 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000627 //
628 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000629 (MI->getNumOperands() == 1 &&
630 (MI->getOperand(0).isPCRelativeDisp() ||
631 MI->getOperand(0).isGlobalAddress() ||
632 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000633 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000634 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000635
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000636 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000637 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000638 }
639 O << "\n";
640 return;
641
Chris Lattner77875d82002-11-21 02:00:20 +0000642 case X86II::AddRegFrm: {
643 // There are currently two forms of acceptable AddRegFrm instructions.
644 // Either the instruction JUST takes a single register (like inc, dec, etc),
645 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000646 // (move immediate f.e.). Note that this immediate value might be stored as
647 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000648 // into a register. The initial register might be duplicated if this is a
649 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000650 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000651 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000652 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000653 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000654 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000655 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000656 MI->getOperand(1).isRegister() ||
657 MI->getOperand(1).isGlobalAddress() ||
658 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000659 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000660
Chris Lattner77875d82002-11-21 02:00:20 +0000661 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000662
Brian Gaeked7908f62003-06-27 00:00:48 +0000663 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000664 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000665 if (MI->getNumOperands() == 2 &&
666 (!MI->getOperand(1).isRegister() ||
667 MI->getOperand(1).getVRegValueOrNull() ||
668 MI->getOperand(1).isGlobalAddress() ||
669 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000670 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000671 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000672 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000673 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000674 O << "\n";
675 return;
676 }
Chris Lattner233ad712002-11-21 01:33:44 +0000677 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000678 // There are two acceptable forms of MRMDestReg instructions, those with 2,
679 // 3 and 4 operands:
680 //
681 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682 //
683 // 3 Operands: in this form, the first two registers (the destination, and
684 // the first operand) should be the same, post register allocation. The 3rd
685 // operand is an additional input. This should be for things like add
686 // instructions.
687 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000688 // 4 Operands: This form is for instructions which are 3 operands forms, but
689 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000690 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000691 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000692 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000693 (MI->getNumOperands() == 2 ||
694 (isTwoAddr && MI->getOperand(1).isRegister() &&
695 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
696 (MI->getNumOperands() == 3 ||
697 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000698 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000699
Brian Gaeked7908f62003-06-27 00:00:48 +0000700 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000701 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000702 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000703 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000704 if (MI->getNumOperands() == 4) {
705 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000706 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000707 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000708 O << "\n";
709 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000710 }
Chris Lattner18042332002-11-21 21:03:39 +0000711
712 case X86II::MRMDestMem: {
713 // These instructions are the same as MRMDestReg, but instead of having a
714 // register reference for the mod/rm field, it's a memory reference.
715 //
716 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000717 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000718
Brian Gaeked7908f62003-06-27 00:00:48 +0000719 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000720 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000721 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000722 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000723 O << "\n";
724 return;
725 }
726
Chris Lattner233ad712002-11-21 01:33:44 +0000727 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000728 // There is a two forms that are acceptable for MRMSrcReg instructions,
729 // those with 3 and 2 operands:
730 //
731 // 3 Operands: in this form, the last register (the second input) is the
732 // ModR/M input. The first two operands should be the same, post register
733 // allocation. This is for things like: add r32, r/m32
734 //
735 // 2 Operands: this is for things like mov that do not read a second input
736 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000737 assert(MI->getOperand(0).isRegister() &&
738 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000739 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000740 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000741 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000742 if (MI->getNumOperands() == 3 &&
743 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
744 O << "**";
745
Brian Gaeked7908f62003-06-27 00:00:48 +0000746 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000747 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000748 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000749 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000750 O << "\n";
751 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000752 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000753
Chris Lattner3d3067b2002-11-21 20:44:15 +0000754 case X86II::MRMSrcMem: {
755 // These instructions are the same as MRMSrcReg, but instead of having a
756 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000757 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000758 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000759 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000760 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000761 isMem(MI, 2))
762 && "Bad format for MRMDestReg!");
763 if (MI->getNumOperands() == 2+4 &&
764 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
765 O << "**";
766
Brian Gaeked7908f62003-06-27 00:00:48 +0000767 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000768 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000769 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000770 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000771 O << "\n";
772 return;
773 }
774
Chris Lattner675dd2c2002-11-21 17:09:01 +0000775 case X86II::MRMS0r: case X86II::MRMS1r:
776 case X86II::MRMS2r: case X86II::MRMS3r:
777 case X86II::MRMS4r: case X86II::MRMS5r:
778 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000779 // In this form, the following are valid formats:
780 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000781 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000782 // 2. shl rdest, rinput <implicit CL or 1>
783 // 3. sbb rdest, rinput, immediate [rdest = rinput]
784 //
785 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000786 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000787 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000788 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000789 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000790 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000791 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000792 "Bad MRMSxR format!");
793
Chris Lattnerd9096832002-12-15 08:01:39 +0000794 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000795 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
796 O << "**";
797
Brian Gaeked7908f62003-06-27 00:00:48 +0000798 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000799 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000800 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000801 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000802 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000803 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000804 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000805 O << "\n";
806
807 return;
808 }
809
Chris Lattnerb7089442003-01-13 00:35:03 +0000810 case X86II::MRMS0m: case X86II::MRMS1m:
811 case X86II::MRMS2m: case X86II::MRMS3m:
812 case X86II::MRMS4m: case X86II::MRMS5m:
813 case X86II::MRMS6m: case X86II::MRMS7m: {
814 // In this form, the following are valid formats:
815 // 1. sete [m]
816 // 2. cmp [m], immediate
817 // 2. shl [m], rinput <implicit CL or 1>
818 // 3. sbb [m], immediate
819 //
820 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
821 isMem(MI, 0) && "Bad MRMSxM format!");
822 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
823 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000824 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
825 // is misassembled by gas in intel_syntax mode as its 32-bit
826 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
827 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000828 if (MI->getOpCode() == X86::FSTPr80) {
829 if ((MI->getOperand(0).getReg() == X86::ESP)
830 && (MI->getOperand(1).getImmedValue() == 1)) {
831 int DispVal = MI->getOperand(3).getImmedValue();
832 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
833 unsigned int val = (unsigned int) DispVal;
834 O << ".byte 0xdb, 0xbc, 0x24\n\t";
835 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
836 } else { // 1 byte disp.
837 unsigned char val = (unsigned char) DispVal;
838 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
839 << std::dec << "\t# ";
840 }
841 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000842 }
843 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
844 // misassembled by gas in intel_syntax mode as its 32-bit
845 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
846 // opcode bytes instead of the instruction.
847 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000848 if ((MI->getOperand(0).getReg() == X86::ESP)
849 && (MI->getOperand(1).getImmedValue() == 1)) {
850 int DispVal = MI->getOperand(3).getImmedValue();
851 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
852 unsigned int val = (unsigned int) DispVal;
853 O << ".byte 0xdb, 0xac, 0x24\n\t";
854 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
855 } else { // 1 byte disp.
856 unsigned char val = (unsigned char) DispVal;
857 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
858 << std::dec << "\t# ";
859 }
860 }
861 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000862 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
863 // invalid opcode, saying "64 bit operations are only supported in
864 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
865 // [...]", which is wrong. Workaround: Output the raw opcode bytes
866 // instead of the instruction.
867 if (MI->getOpCode() == X86::FILDr64) {
868 if ((MI->getOperand(0).getReg() == X86::ESP)
869 && (MI->getOperand(1).getImmedValue() == 1)) {
870 int DispVal = MI->getOperand(3).getImmedValue();
871 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
872 unsigned int val = (unsigned int) DispVal;
873 O << ".byte 0xdf, 0xac, 0x24\n\t";
874 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
875 } else { // 1 byte disp.
876 unsigned char val = (unsigned char) DispVal;
877 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
878 << std::dec << "\t# ";
879 }
880 }
881 }
882 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
883 // an invalid opcode, saying "64 bit operations are only
884 // supported in 64 bit modes." libopcodes disassembles it as
885 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
886 // "fistpll DWORD PTR " instead, which is what libopcodes is
887 // expecting to see.
888 if (MI->getOpCode() == X86::FISTPr64) {
889 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000890 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000891 if (MI->getNumOperands() == 5) {
892 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000893 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000894 }
895 O << "\t# ";
896 }
897
Brian Gaeked7908f62003-06-27 00:00:48 +0000898 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000899 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000900 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000901 if (MI->getNumOperands() == 5) {
902 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000903 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000904 }
905 O << "\n";
906 return;
907 }
908
Chris Lattnerf9f60882002-11-18 06:56:51 +0000909 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000910 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000911 }
Chris Lattner72614082002-10-25 22:55:53 +0000912}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000913
Chris Lattner93c1afa2003-08-11 19:35:26 +0000914bool Printer::doInitialization(Module &M) {
915 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000916 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000917 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
918 // instruction as a reference to the register named sp, and if you try to
919 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
920 // before being looked up in the symbol table. This creates spurious
921 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
922 // mode, and decorate all register names with percent signs.
923 //
924 // Cygwin presumably doesn't have this problem, so drop the %'s.
925 //
926 O << "\t.intel_syntax";
927 if (EmitCygwin) O << " noprefix";
928 O << "\n";
929 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000930 return false; // success
931}
932
Chris Lattnerc07736a2003-07-23 15:22:26 +0000933static const Function *isConstantFunctionPointerRef(const Constant *C) {
934 if (const ConstantPointerRef *R = dyn_cast<ConstantPointerRef>(C))
935 if (const Function *F = dyn_cast<Function>(R->getValue()))
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000936 return F;
Chris Lattnerc07736a2003-07-23 15:22:26 +0000937 return 0;
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000938}
939
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000940bool Printer::doFinalization(Module &M)
941{
Brian Gaekede420ae2003-07-23 20:25:08 +0000942 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000943 // Print out module-level global variables here.
944 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000945 std::string name(Mang->getValueName(I));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000946 if (I->hasInitializer()) {
947 Constant *C = I->getInitializer();
Chris Lattnere0121322003-08-03 23:37:09 +0000948 if (C->isNullValue()) {
949 O << "\n\n\t.comm " << name << "," << TD.getTypeSize(C->getType())
950 << "," << (unsigned)TD.getTypeAlignment(C->getType());
951 O << "\t\t# ";
952 WriteAsOperand(O, I, true, true, &M);
953 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000954 } else {
Chris Lattnere0121322003-08-03 23:37:09 +0000955 O << "\n\n\t.data\n";
956 O << "\t.globl " << name << "\n";
957 O << "\t.type " << name << ",@object\n";
958 O << "\t.size " << name << "," << TD.getTypeSize(C->getType()) << "\n";
959 O << "\t.align " << (unsigned)TD.getTypeAlignment(C->getType()) << "\n";
960 O << name << ":\t\t\t\t# ";
961 WriteAsOperand(O, I, true, true, &M);
962 O << " = ";
963 WriteAsOperand(O, C, false, false, &M);
964 O << "\n";
965 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000966 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000967 } else {
Brian Gaekebc601fe2003-06-25 22:00:39 +0000968 O << "\t.globl " << name << "\n";
969 O << "\t.comm " << name << ", "
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000970 << (unsigned)TD.getTypeSize(I->getType()) << ", "
971 << (unsigned)TD.getTypeAlignment(I->getType()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000972 }
973 }
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000974 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000975 return false; // success
976}