blob: f8c006cfd1cd70d1637e72fc89b0dc08191ed283 [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/Constants.h"
13#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000014#include "llvm/Module.h"
15#include "llvm/Assembly/Writer.h"
Chris Lattner0285a332002-12-28 20:25:38 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000019#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000020#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000021#include "Support/Statistic.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 {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000026 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
27
Chris Lattner93c1afa2003-08-11 19:35:26 +000028 // FIXME: This should be automatically picked up by autoconf from the C
29 // frontend
30 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
31 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
32
Chris Lattner0285a332002-12-28 20:25:38 +000033 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000034 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000035 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000037
38 /// Target machine description which we query for reg. names, data
39 /// layout, etc.
40 ///
41 TargetMachine &TM;
42
Brian Gaeked9fb37a2003-07-24 20:20:44 +000043 /// Name-mangler for global names.
44 ///
45 Mangler *Mang;
46
Brian Gaekede420ae2003-07-23 20:25:08 +000047 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000048
49 /// We name each basic block in a Function with a unique number, so
50 /// that we can consistently refer to them later. This is cleared
51 /// at the beginning of each call to runOnMachineFunction().
52 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000053 typedef std::map<const Value *, unsigned> ValueMapTy;
54 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000055
56 /// Cache of mangled name for current function. This is
57 /// recalculated at the beginning of each call to
58 /// runOnMachineFunction().
59 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000060 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000061
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000062 virtual const char *getPassName() const {
63 return "X86 Assembly Printer";
64 }
65
Brian Gaeke2a098772003-08-11 19:05:46 +000066 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000067 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000068 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000069 bool elideOffsetKeyword = false);
70 void printMemReference(const MachineInstr *MI, unsigned Op);
71 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000072 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000073 std::string ConstantExprToString(const ConstantExpr* CE);
74 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000075 bool doInitialization(Module &M);
76 bool doFinalization(Module &M);
Chris Lattnerad200712003-09-09 16:23:36 +000077 void printConstantValueOnly(const Constant* CV);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000078 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000079 };
Brian Gaeked7908f62003-06-27 00:00:48 +000080} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000081
Brian Gaeke92bdfe62003-07-23 18:37:06 +000082/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000083/// assembly code for a MachineFunction to the given output stream,
84/// using the given target machine description. This should work
85/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000086///
Brian Gaeke9d99b432003-08-13 18:15:15 +000087FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000088 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000089}
90
Brian Gaeke92bdfe62003-07-23 18:37:06 +000091/// valToExprString - Helper function for ConstantExprToString().
92/// Appends result to argument string S.
93///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000094std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000095 std::string S;
96 bool failed = false;
97 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
98 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
99 S += std::string(CB == ConstantBool::True ? "1" : "0");
100 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
101 S += itostr(CI->getValue());
102 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
103 S += utostr(CI->getValue());
104 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
105 S += ftostr(CFP->getValue());
106 else if (isa<ConstantPointerNull>(CV))
107 S += "0";
108 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
109 S += valToExprString(CPR->getValue());
110 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
111 S += ConstantExprToString(CE);
112 else
113 failed = true;
114 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000115 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000116 }
117 else
118 failed = true;
119
120 if (failed) {
121 assert(0 && "Cannot convert value to string");
122 S += "<illegal-value>";
123 }
124 return S;
125}
126
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000127/// ConstantExprToString - Convert a ConstantExpr to an asm expression
128/// and return this as a string.
129///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000130std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000131 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000132 switch(CE->getOpcode()) {
133 case Instruction::GetElementPtr:
134 { // generate a symbolic expression for the byte address
135 const Value* ptrVal = CE->getOperand(0);
136 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000137 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
138 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
139 else
140 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000141 }
142
143 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000144 // Support only non-converting or widening casts for now, that is,
145 // ones that do not involve a change in value. This assertion is
146 // not a complete check.
147 {
148 Constant *Op = CE->getOperand(0);
149 const Type *OpTy = Op->getType(), *Ty = CE->getType();
150 assert(((isa<PointerType>(OpTy)
151 && (Ty == Type::LongTy || Ty == Type::ULongTy))
152 || (isa<PointerType>(Ty)
153 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
154 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000155 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000156 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000157 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000158 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000159
160 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000161 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
162 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000163
164 default:
165 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000166 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000168}
169
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000170/// printSingleConstantValue - Print a single constant value.
171///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000172void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000173Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174{
175 assert(CV->getType() != Type::VoidTy &&
176 CV->getType() != Type::TypeTy &&
177 CV->getType() != Type::LabelTy &&
178 "Unexpected type for Constant");
179
180 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
181 && "Aggregate types should be handled outside this function");
182
183 const Type *type = CV->getType();
184 O << "\t";
185 switch(type->getPrimitiveID())
186 {
187 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
188 O << ".byte";
189 break;
190 case Type::UShortTyID: case Type::ShortTyID:
191 O << ".word";
192 break;
193 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
194 O << ".long";
195 break;
196 case Type::ULongTyID: case Type::LongTyID:
197 O << ".quad";
198 break;
199 case Type::FloatTyID:
200 O << ".long";
201 break;
202 case Type::DoubleTyID:
203 O << ".quad";
204 break;
205 case Type::ArrayTyID:
206 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
207 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
208 O << ".string";
209 else
210 assert (0 && "Can't handle printing this type of array");
211 break;
212 default:
213 assert (0 && "Can't handle printing this type of thing");
214 break;
215 }
216 O << "\t";
217
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000218 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
219 {
220 // Constant expression built from operators, constants, and
221 // symbolic addrs
222 O << ConstantExprToString(CE) << "\n";
223 }
224 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000225 {
226 if (type->isFloatingPoint()) {
227 // FP Constants are printed as integer constants to avoid losing
228 // precision...
229 double Val = cast<ConstantFP>(CV)->getValue();
230 if (type == Type::FloatTy) {
231 float FVal = (float)Val;
232 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
233 O << *(unsigned int*)ProxyPtr;
234 } else if (type == Type::DoubleTy) {
235 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
236 O << *(uint64_t*)ProxyPtr;
237 } else {
238 assert(0 && "Unknown floating point type!");
239 }
240
241 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
242 } else {
243 WriteAsOperand(O, CV, false, false) << "\n";
244 }
245 }
246 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
247 {
248 // This is a constant address for a global variable or method.
249 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000250 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000251 }
252 else if (isa<ConstantPointerNull>(CV))
253 {
254 // Null pointer value
255 O << "0\n";
256 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000257 else
258 {
259 assert(0 && "Unknown elementary type for constant");
260 }
261}
262
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000263/// isStringCompatible - Can we treat the specified array as a string?
264/// Only if it is an array of ubytes or non-negative sbytes.
265///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000266static bool isStringCompatible(const ConstantArray *CVA) {
267 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
268 if (ETy == Type::UByteTy) return true;
269 if (ETy != Type::SByteTy) return false;
270
271 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
272 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
273 return false;
274
275 return true;
276}
277
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000278/// toOctal - Convert the low order bits of X into an octal digit.
279///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000280static inline char toOctal(int X) {
281 return (X&7)+'0';
282}
283
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000284/// getAsCString - Return the specified array as a C compatible
285/// string, only if the predicate isStringCompatible is true.
286///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000287static std::string getAsCString(const ConstantArray *CVA) {
288 assert(isStringCompatible(CVA) && "Array is not string compatible!");
289
290 std::string Result;
291 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
292 Result = "\"";
293 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000294 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000295
296 if (C == '"') {
297 Result += "\\\"";
298 } else if (C == '\\') {
299 Result += "\\\\";
300 } else if (isprint(C)) {
301 Result += C;
302 } else {
303 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000304 case '\b': Result += "\\b"; break;
305 case '\f': Result += "\\f"; break;
306 case '\n': Result += "\\n"; break;
307 case '\r': Result += "\\r"; break;
308 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000309 default:
310 Result += '\\';
311 Result += toOctal(C >> 6);
312 Result += toOctal(C >> 3);
313 Result += toOctal(C >> 0);
314 break;
315 }
316 }
317 }
318 Result += "\"";
319 return Result;
320}
321
322// Print a constant value or values (it may be an aggregate).
323// Uses printSingleConstantValue() to print each individual value.
Chris Lattnerad200712003-09-09 16:23:36 +0000324void Printer::printConstantValueOnly(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000325 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000326
Chris Lattnerad200712003-09-09 16:23:36 +0000327 if (CV->isNullValue()) {
328 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
329 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
330 if (isStringCompatible(CVA)) {
331 // print the string alone and return
Chris Lattnere0121322003-08-03 23:37:09 +0000332 O << "\t.string\t" << getAsCString(CVA) << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000333 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000334 const std::vector<Use> &constValues = CVA->getValues();
335 for (unsigned i=0; i < constValues.size(); i++)
336 printConstantValueOnly(cast<Constant>(constValues[i].get()));
337 }
Chris Lattnerad200712003-09-09 16:23:36 +0000338 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
339 // Print the fields in successive locations. Pad to align if needed!
340 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
341 const std::vector<Use>& constValues = CVS->getValues();
342 unsigned sizeSoFar = 0;
343 for (unsigned i=0, N = constValues.size(); i < N; i++) {
344 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000345
Chris Lattnerad200712003-09-09 16:23:36 +0000346 // Check if padding is needed and insert one or more 0s.
347 unsigned fieldSize = TD.getTypeSize(field->getType());
348 unsigned padSize = ((i == N-1? cvsLayout->StructSize
349 : cvsLayout->MemberOffsets[i+1])
350 - cvsLayout->MemberOffsets[i]) - fieldSize;
351 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000352
Chris Lattnerad200712003-09-09 16:23:36 +0000353 // Now print the actual field value
354 printConstantValueOnly(field);
355
356 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000357 if (padSize)
358 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000359 }
Chris Lattnerad200712003-09-09 16:23:36 +0000360 assert(sizeSoFar == cvsLayout->StructSize &&
361 "Layout of constant struct may be incorrect!");
362 } else
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000363 printSingleConstantValue(CV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000364}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000365
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000366/// printConstantPool - Print to the current output stream assembly
367/// representations of the constants in the constant pool MCP. This is
368/// used to print out constants which have been "spilled to memory" by
369/// the code generator.
370///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000371void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000372 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000373 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000374
Chris Lattnerb7089442003-01-13 00:35:03 +0000375 if (CP.empty()) return;
376
377 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
378 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000379 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000380 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000381 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
382 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000383 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000384 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000385}
386
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000387/// runOnMachineFunction - This uses the printMachineInstruction()
388/// method to print assembly for each instruction.
389///
Chris Lattner0285a332002-12-28 20:25:38 +0000390bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000391 // BBNumber is used here so that a given Printer will never give two
392 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000393 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000394
Chris Lattnere0121322003-08-03 23:37:09 +0000395 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000396 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000397 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000398
Chris Lattnerb7089442003-01-13 00:35:03 +0000399 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000400 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000401
Brian Gaeke6559bb92002-11-14 22:32:30 +0000402 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000403 O << "\t.text\n";
404 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000405 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000406 if (!EmitCygwin)
407 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000408 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409
Brian Gaeked7908f62003-06-27 00:00:48 +0000410 // Number each basic block so that we can consistently refer to them
411 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000412 NumberForBB.clear();
413 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
414 I != E; ++I) {
415 NumberForBB[I->getBasicBlock()] = BBNumber++;
416 }
417
Brian Gaeke6559bb92002-11-14 22:32:30 +0000418 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000419 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
420 I != E; ++I) {
421 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000422 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000423 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000424 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
425 II != E; ++II) {
426 // Print the assembly for the instruction.
427 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000428 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000429 }
Chris Lattner0285a332002-12-28 20:25:38 +0000430 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000431
432 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000433 return false;
434}
435
Chris Lattner3d3067b2002-11-21 20:44:15 +0000436static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000437 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000438 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
439 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000440}
441
442static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000443 if (MI->getOperand(Op).isFrameIndex()) return true;
444 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000445 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000446 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
447 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000448}
449
Brian Gaeke2a098772003-08-11 19:05:46 +0000450
451
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000452void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000453 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000454 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000455 switch (MO.getType()) {
456 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000457 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000458 O << "<" << V->getName() << ">";
459 return;
460 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000462 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000463 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000464 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000465 O << "%" << RI.get(MO.getReg()).Name;
466 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000467 O << "%reg" << MO.getReg();
468 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000469
470 case MachineOperand::MO_SignExtendedImmed:
471 case MachineOperand::MO_UnextendedImmed:
472 O << (int)MO.getImmedValue();
473 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000474 case MachineOperand::MO_PCRelativeDisp: {
475 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
476 assert (i != NumberForBB.end()
477 && "Could not find a BB in the NumberForBB map!");
478 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000479 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000480 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000481 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000482 if (!elideOffsetKeyword)
483 O << "OFFSET ";
484 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000485 return;
486 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000487 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000488 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000489 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000490 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000491 }
492}
493
Chris Lattner3501fea2003-01-14 22:00:31 +0000494static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000495 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000496 default: assert(0 && "Unknown arg size!");
497 case X86II::Arg8: return "BYTE PTR";
498 case X86II::Arg16: return "WORD PTR";
499 case X86II::Arg32: return "DWORD PTR";
500 case X86II::Arg64: return "QWORD PTR";
501 case X86II::ArgF32: return "DWORD PTR";
502 case X86II::ArgF64: return "QWORD PTR";
503 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000504 }
505}
506
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000507void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000508 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000509
510 if (MI->getOperand(Op).isFrameIndex()) {
511 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
512 if (MI->getOperand(Op+3).getImmedValue())
513 O << " + " << MI->getOperand(Op+3).getImmedValue();
514 O << "]";
515 return;
516 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000517 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000518 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000519 if (MI->getOperand(Op+3).getImmedValue())
520 O << " + " << MI->getOperand(Op+3).getImmedValue();
521 O << "]";
522 return;
523 }
524
Chris Lattner3d3067b2002-11-21 20:44:15 +0000525 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000526 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000527 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000528 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000529
530 O << "[";
531 bool NeedPlus = false;
532 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000533 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000534 NeedPlus = true;
535 }
536
537 if (IndexReg.getReg()) {
538 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000539 if (ScaleVal != 1)
540 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000541 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000542 NeedPlus = true;
543 }
544
Chris Lattner0285a332002-12-28 20:25:38 +0000545 if (DispVal) {
546 if (NeedPlus)
547 if (DispVal > 0)
548 O << " + ";
549 else {
550 O << " - ";
551 DispVal = -DispVal;
552 }
553 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000554 }
555 O << "]";
556}
557
Brian Gaeke2a098772003-08-11 19:05:46 +0000558/// checkImplUses - Emit the implicit-use registers for the
559/// instruction described by DESC, if its PrintImplUses flag is set.
560///
561void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
562 const MRegisterInfo &RI = *TM.getRegisterInfo();
563 if (Desc.TSFlags & X86II::PrintImplUses) {
564 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
565 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000566 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000567 }
568 }
569}
570
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000571/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000572/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000573///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000574void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000575 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000576 const TargetInstrInfo &TII = TM.getInstrInfo();
577 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000578
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000579 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000580 switch (Desc.TSFlags & X86II::FormMask) {
581 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000582 // Print pseudo-instructions as comments; either they should have been
583 // turned into real instructions by now, or they don't need to be
584 // seen by the assembler (e.g., IMPLICIT_USEs.)
585 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000586 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000587 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 O << " = phi ";
589 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
590 if (i != 1) O << ", ";
591 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000592 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000593 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000594 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000595 O << "]";
596 }
597 } else {
598 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000599 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
600 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000601 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000602 O << " = ";
603 ++i;
604 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000605 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000606
607 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
608 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000609 if (MI->getOperand(i).opIsDefOnly() ||
610 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000611 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000612 if (MI->getOperand(i).opIsDefOnly() ||
613 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000614 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000615 }
616 O << "\n";
617 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000618
Chris Lattnerf9f60882002-11-18 06:56:51 +0000619 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000620 // The accepted forms of Raw instructions are:
621 // 1. nop - No operand required
622 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000623 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000624 //
625 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000626 (MI->getNumOperands() == 1 &&
627 (MI->getOperand(0).isPCRelativeDisp() ||
628 MI->getOperand(0).isGlobalAddress() ||
629 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000630 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000631 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000632
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000633 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000634 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000635 }
636 O << "\n";
637 return;
638
Chris Lattner77875d82002-11-21 02:00:20 +0000639 case X86II::AddRegFrm: {
640 // There are currently two forms of acceptable AddRegFrm instructions.
641 // Either the instruction JUST takes a single register (like inc, dec, etc),
642 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000643 // (move immediate f.e.). Note that this immediate value might be stored as
644 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000645 // into a register. The initial register might be duplicated if this is a
646 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000647 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000648 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000649 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000650 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000651 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000652 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000653 MI->getOperand(1).isRegister() ||
654 MI->getOperand(1).isGlobalAddress() ||
655 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000656 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000657
Chris Lattner77875d82002-11-21 02:00:20 +0000658 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000659
Brian Gaeked7908f62003-06-27 00:00:48 +0000660 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000661 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000662 if (MI->getNumOperands() == 2 &&
663 (!MI->getOperand(1).isRegister() ||
664 MI->getOperand(1).getVRegValueOrNull() ||
665 MI->getOperand(1).isGlobalAddress() ||
666 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000667 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000668 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000669 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000670 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000671 O << "\n";
672 return;
673 }
Chris Lattner233ad712002-11-21 01:33:44 +0000674 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000675 // There are two acceptable forms of MRMDestReg instructions, those with 2,
676 // 3 and 4 operands:
677 //
678 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000679 //
680 // 3 Operands: in this form, the first two registers (the destination, and
681 // the first operand) should be the same, post register allocation. The 3rd
682 // operand is an additional input. This should be for things like add
683 // instructions.
684 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000685 // 4 Operands: This form is for instructions which are 3 operands forms, but
686 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000687 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000688 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000689 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000690 (MI->getNumOperands() == 2 ||
691 (isTwoAddr && MI->getOperand(1).isRegister() &&
692 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
693 (MI->getNumOperands() == 3 ||
694 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000695 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000696
Brian Gaeked7908f62003-06-27 00:00:48 +0000697 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000698 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000699 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000700 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000701 if (MI->getNumOperands() == 4) {
702 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000703 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000704 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000705 O << "\n";
706 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000707 }
Chris Lattner18042332002-11-21 21:03:39 +0000708
709 case X86II::MRMDestMem: {
710 // These instructions are the same as MRMDestReg, but instead of having a
711 // register reference for the mod/rm field, it's a memory reference.
712 //
713 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000714 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000715
Brian Gaeked7908f62003-06-27 00:00:48 +0000716 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000717 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000718 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000719 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000720 O << "\n";
721 return;
722 }
723
Chris Lattner233ad712002-11-21 01:33:44 +0000724 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000725 // There is a two forms that are acceptable for MRMSrcReg instructions,
726 // those with 3 and 2 operands:
727 //
728 // 3 Operands: in this form, the last register (the second input) is the
729 // ModR/M input. The first two operands should be the same, post register
730 // allocation. This is for things like: add r32, r/m32
731 //
732 // 2 Operands: this is for things like mov that do not read a second input
733 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000734 assert(MI->getOperand(0).isRegister() &&
735 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000736 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000737 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000738 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000739 if (MI->getNumOperands() == 3 &&
740 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
741 O << "**";
742
Brian Gaeked7908f62003-06-27 00:00:48 +0000743 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000744 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000745 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000746 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000747 O << "\n";
748 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000749 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000750
Chris Lattner3d3067b2002-11-21 20:44:15 +0000751 case X86II::MRMSrcMem: {
752 // These instructions are the same as MRMSrcReg, but instead of having a
753 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000754 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000755 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000756 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000757 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000758 isMem(MI, 2))
759 && "Bad format for MRMDestReg!");
760 if (MI->getNumOperands() == 2+4 &&
761 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
762 O << "**";
763
Brian Gaeked7908f62003-06-27 00:00:48 +0000764 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000765 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000766 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000767 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000768 O << "\n";
769 return;
770 }
771
Chris Lattner675dd2c2002-11-21 17:09:01 +0000772 case X86II::MRMS0r: case X86II::MRMS1r:
773 case X86II::MRMS2r: case X86II::MRMS3r:
774 case X86II::MRMS4r: case X86II::MRMS5r:
775 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000776 // In this form, the following are valid formats:
777 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000778 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000779 // 2. shl rdest, rinput <implicit CL or 1>
780 // 3. sbb rdest, rinput, immediate [rdest = rinput]
781 //
782 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000783 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000784 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000785 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000786 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000787 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000788 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000789 "Bad MRMSxR format!");
790
Chris Lattnerd9096832002-12-15 08:01:39 +0000791 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000792 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
793 O << "**";
794
Brian Gaeked7908f62003-06-27 00:00:48 +0000795 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000796 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000797 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000798 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000799 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000801 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000802 O << "\n";
803
804 return;
805 }
806
Chris Lattnerb7089442003-01-13 00:35:03 +0000807 case X86II::MRMS0m: case X86II::MRMS1m:
808 case X86II::MRMS2m: case X86II::MRMS3m:
809 case X86II::MRMS4m: case X86II::MRMS5m:
810 case X86II::MRMS6m: case X86II::MRMS7m: {
811 // In this form, the following are valid formats:
812 // 1. sete [m]
813 // 2. cmp [m], immediate
814 // 2. shl [m], rinput <implicit CL or 1>
815 // 3. sbb [m], immediate
816 //
817 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
818 isMem(MI, 0) && "Bad MRMSxM format!");
819 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
820 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000821 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
822 // is misassembled by gas in intel_syntax mode as its 32-bit
823 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
824 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000825 if (MI->getOpCode() == X86::FSTPr80) {
826 if ((MI->getOperand(0).getReg() == X86::ESP)
827 && (MI->getOperand(1).getImmedValue() == 1)) {
828 int DispVal = MI->getOperand(3).getImmedValue();
829 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
830 unsigned int val = (unsigned int) DispVal;
831 O << ".byte 0xdb, 0xbc, 0x24\n\t";
832 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
833 } else { // 1 byte disp.
834 unsigned char val = (unsigned char) DispVal;
835 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
836 << std::dec << "\t# ";
837 }
838 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000839 }
840 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
841 // misassembled by gas in intel_syntax mode as its 32-bit
842 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
843 // opcode bytes instead of the instruction.
844 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000845 if ((MI->getOperand(0).getReg() == X86::ESP)
846 && (MI->getOperand(1).getImmedValue() == 1)) {
847 int DispVal = MI->getOperand(3).getImmedValue();
848 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
849 unsigned int val = (unsigned int) DispVal;
850 O << ".byte 0xdb, 0xac, 0x24\n\t";
851 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
852 } else { // 1 byte disp.
853 unsigned char val = (unsigned char) DispVal;
854 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
855 << std::dec << "\t# ";
856 }
857 }
858 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000859 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
860 // invalid opcode, saying "64 bit operations are only supported in
861 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
862 // [...]", which is wrong. Workaround: Output the raw opcode bytes
863 // instead of the instruction.
864 if (MI->getOpCode() == X86::FILDr64) {
865 if ((MI->getOperand(0).getReg() == X86::ESP)
866 && (MI->getOperand(1).getImmedValue() == 1)) {
867 int DispVal = MI->getOperand(3).getImmedValue();
868 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
869 unsigned int val = (unsigned int) DispVal;
870 O << ".byte 0xdf, 0xac, 0x24\n\t";
871 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
872 } else { // 1 byte disp.
873 unsigned char val = (unsigned char) DispVal;
874 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
875 << std::dec << "\t# ";
876 }
877 }
878 }
879 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
880 // an invalid opcode, saying "64 bit operations are only
881 // supported in 64 bit modes." libopcodes disassembles it as
882 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
883 // "fistpll DWORD PTR " instead, which is what libopcodes is
884 // expecting to see.
885 if (MI->getOpCode() == X86::FISTPr64) {
886 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000887 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000888 if (MI->getNumOperands() == 5) {
889 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000890 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000891 }
892 O << "\t# ";
893 }
894
Brian Gaeked7908f62003-06-27 00:00:48 +0000895 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000896 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000897 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000898 if (MI->getNumOperands() == 5) {
899 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000900 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000901 }
902 O << "\n";
903 return;
904 }
905
Chris Lattnerf9f60882002-11-18 06:56:51 +0000906 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000907 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000908 }
Chris Lattner72614082002-10-25 22:55:53 +0000909}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000910
Chris Lattner93c1afa2003-08-11 19:35:26 +0000911bool Printer::doInitialization(Module &M) {
912 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000913 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000914 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
915 // instruction as a reference to the register named sp, and if you try to
916 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
917 // before being looked up in the symbol table. This creates spurious
918 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
919 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000920 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000921 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000922 return false; // success
923}
924
Chris Lattnerad200712003-09-09 16:23:36 +0000925// SwitchSection - Switch to the specified section of the executable if we are
926// not already in it!
927//
928static void SwitchSection(std::ostream &OS, std::string &CurSection,
929 const char *NewSection) {
930 if (CurSection != NewSection) {
931 CurSection = NewSection;
932 if (!CurSection.empty())
933 OS << "\t" << NewSection << "\n";
934 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000935}
936
Chris Lattnerad200712003-09-09 16:23:36 +0000937bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000938 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000939 std::string CurSection;
940
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000941 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000942 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
943 if (I->hasInitializer()) { // External global require no code
944 O << "\n\n";
945 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000946 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000947 unsigned Size = TD.getTypeSize(C->getType());
948 unsigned Align = TD.getTypeAlignment(C->getType());
949
950 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000951 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
952 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000953 SwitchSection(O, CurSection, ".data");
954 if (I->hasInternalLinkage())
955 O << "\t.local " << name << "\n";
956
957 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000958 << "," << (unsigned)TD.getTypeAlignment(C->getType());
959 O << "\t\t# ";
960 WriteAsOperand(O, I, true, true, &M);
961 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000962 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000963 switch (I->getLinkage()) {
964 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000965 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000966 // Nonnull linkonce -> weak
967 O << "\t.weak " << name << "\n";
968 SwitchSection(O, CurSection, "");
969 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
970 break;
971
972 case GlobalValue::AppendingLinkage:
973 // FIXME: appending linkage variables should go into a section of
974 // their name or something. For now, just emit them as external.
975 case GlobalValue::ExternalLinkage:
976 // If external or appending, declare as a global symbol
977 O << "\t.globl " << name << "\n";
978 // FALL THROUGH
979 case GlobalValue::InternalLinkage:
980 if (C->isNullValue())
981 SwitchSection(O, CurSection, ".bss");
982 else
983 SwitchSection(O, CurSection, ".data");
984 break;
985 }
986
987 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000988 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000989 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000990 O << name << ":\t\t\t\t# ";
991 WriteAsOperand(O, I, true, true, &M);
992 O << " = ";
993 WriteAsOperand(O, C, false, false, &M);
994 O << "\n";
995 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000996 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000997 }
Chris Lattnerad200712003-09-09 16:23:36 +0000998
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000999 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001000 return false; // success
1001}