blob: 0afd399e299c6bc79de6cfad885beb8b3b7662da [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"
Chris Lattnere0121322003-08-03 23:37:09 +000021#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000022#include "Support/CommandLine.h"
Chris Lattner72614082002-10-25 22:55:53 +000023
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000024namespace {
Chris Lattner93c1afa2003-08-11 19:35:26 +000025 // FIXME: This should be automatically picked up by autoconf from the C
26 // frontend
27 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
28 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
29
Chris Lattner0285a332002-12-28 20:25:38 +000030 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000031 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000032 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000033 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000034
35 /// Target machine description which we query for reg. names, data
36 /// layout, etc.
37 ///
38 TargetMachine &TM;
39
Brian Gaeked9fb37a2003-07-24 20:20:44 +000040 /// Name-mangler for global names.
41 ///
42 Mangler *Mang;
43
Brian Gaekede420ae2003-07-23 20:25:08 +000044 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000045
46 /// We name each basic block in a Function with a unique number, so
47 /// that we can consistently refer to them later. This is cleared
48 /// at the beginning of each call to runOnMachineFunction().
49 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000050 typedef std::map<const Value *, unsigned> ValueMapTy;
51 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000052
53 /// Cache of mangled name for current function. This is
54 /// recalculated at the beginning of each call to
55 /// runOnMachineFunction().
56 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000057 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000058
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000059 virtual const char *getPassName() const {
60 return "X86 Assembly Printer";
61 }
62
Brian Gaeke2a098772003-08-11 19:05:46 +000063 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000064 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000065 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000066 bool elideOffsetKeyword = false);
67 void printMemReference(const MachineInstr *MI, unsigned Op);
68 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000069 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000070 std::string ConstantExprToString(const ConstantExpr* CE);
71 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000072 bool doInitialization(Module &M);
73 bool doFinalization(Module &M);
Chris Lattnerad200712003-09-09 16:23:36 +000074 void printConstantValueOnly(const Constant* CV);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000075 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000076 };
Brian Gaeked7908f62003-06-27 00:00:48 +000077} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078
Brian Gaeke92bdfe62003-07-23 18:37:06 +000079/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000080/// assembly code for a MachineFunction to the given output stream,
81/// using the given target machine description. This should work
82/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000083///
Brian Gaeke9d99b432003-08-13 18:15:15 +000084FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000085 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000086}
87
Brian Gaeke92bdfe62003-07-23 18:37:06 +000088/// valToExprString - Helper function for ConstantExprToString().
89/// Appends result to argument string S.
90///
Brian Gaeked9fb37a2003-07-24 20:20:44 +000091std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +000092 std::string S;
93 bool failed = false;
94 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
95 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
96 S += std::string(CB == ConstantBool::True ? "1" : "0");
97 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
98 S += itostr(CI->getValue());
99 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
100 S += utostr(CI->getValue());
101 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
102 S += ftostr(CFP->getValue());
103 else if (isa<ConstantPointerNull>(CV))
104 S += "0";
105 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
106 S += valToExprString(CPR->getValue());
107 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
108 S += ConstantExprToString(CE);
109 else
110 failed = true;
111 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000112 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000113 }
114 else
115 failed = true;
116
117 if (failed) {
118 assert(0 && "Cannot convert value to string");
119 S += "<illegal-value>";
120 }
121 return S;
122}
123
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000124/// ConstantExprToString - Convert a ConstantExpr to an asm expression
125/// and return this as a string.
126///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000127std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000128 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000129 switch(CE->getOpcode()) {
130 case Instruction::GetElementPtr:
131 { // generate a symbolic expression for the byte address
132 const Value* ptrVal = CE->getOperand(0);
133 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000134 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
135 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
136 else
137 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000138 }
139
140 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000141 // Support only non-converting or widening casts for now, that is,
142 // ones that do not involve a change in value. This assertion is
143 // not a complete check.
144 {
145 Constant *Op = CE->getOperand(0);
146 const Type *OpTy = Op->getType(), *Ty = CE->getType();
147 assert(((isa<PointerType>(OpTy)
148 && (Ty == Type::LongTy || Ty == Type::ULongTy))
149 || (isa<PointerType>(Ty)
150 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
151 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000152 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000153 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000154 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000155 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000156
157 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000158 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
159 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000160
161 default:
162 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000163 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000164 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165}
166
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000167/// printSingleConstantValue - Print a single constant value.
168///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000170Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000171{
172 assert(CV->getType() != Type::VoidTy &&
173 CV->getType() != Type::TypeTy &&
174 CV->getType() != Type::LabelTy &&
175 "Unexpected type for Constant");
176
177 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
178 && "Aggregate types should be handled outside this function");
179
180 const Type *type = CV->getType();
181 O << "\t";
182 switch(type->getPrimitiveID())
183 {
184 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
185 O << ".byte";
186 break;
187 case Type::UShortTyID: case Type::ShortTyID:
188 O << ".word";
189 break;
190 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
191 O << ".long";
192 break;
193 case Type::ULongTyID: case Type::LongTyID:
194 O << ".quad";
195 break;
196 case Type::FloatTyID:
197 O << ".long";
198 break;
199 case Type::DoubleTyID:
200 O << ".quad";
201 break;
202 case Type::ArrayTyID:
203 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
204 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
205 O << ".string";
206 else
207 assert (0 && "Can't handle printing this type of array");
208 break;
209 default:
210 assert (0 && "Can't handle printing this type of thing");
211 break;
212 }
213 O << "\t";
214
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000215 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
216 {
217 // Constant expression built from operators, constants, and
218 // symbolic addrs
219 O << ConstantExprToString(CE) << "\n";
220 }
221 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000222 {
223 if (type->isFloatingPoint()) {
224 // FP Constants are printed as integer constants to avoid losing
225 // precision...
226 double Val = cast<ConstantFP>(CV)->getValue();
227 if (type == Type::FloatTy) {
228 float FVal = (float)Val;
229 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
230 O << *(unsigned int*)ProxyPtr;
231 } else if (type == Type::DoubleTy) {
232 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
233 O << *(uint64_t*)ProxyPtr;
234 } else {
235 assert(0 && "Unknown floating point type!");
236 }
237
238 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
239 } else {
240 WriteAsOperand(O, CV, false, false) << "\n";
241 }
242 }
243 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
244 {
245 // This is a constant address for a global variable or method.
246 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000247 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248 }
249 else if (isa<ConstantPointerNull>(CV))
250 {
251 // Null pointer value
252 O << "0\n";
253 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000254 else
255 {
256 assert(0 && "Unknown elementary type for constant");
257 }
258}
259
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000260/// isStringCompatible - Can we treat the specified array as a string?
261/// Only if it is an array of ubytes or non-negative sbytes.
262///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000263static bool isStringCompatible(const ConstantArray *CVA) {
264 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
265 if (ETy == Type::UByteTy) return true;
266 if (ETy != Type::SByteTy) return false;
267
268 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
269 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
270 return false;
271
272 return true;
273}
274
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000275/// toOctal - Convert the low order bits of X into an octal digit.
276///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000277static inline char toOctal(int X) {
278 return (X&7)+'0';
279}
280
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000281/// getAsCString - Return the specified array as a C compatible
282/// string, only if the predicate isStringCompatible is true.
283///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000284static std::string getAsCString(const ConstantArray *CVA) {
285 assert(isStringCompatible(CVA) && "Array is not string compatible!");
286
287 std::string Result;
288 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
289 Result = "\"";
290 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000291 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000292
293 if (C == '"') {
294 Result += "\\\"";
295 } else if (C == '\\') {
296 Result += "\\\\";
297 } else if (isprint(C)) {
298 Result += C;
299 } else {
300 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000301 case '\b': Result += "\\b"; break;
302 case '\f': Result += "\\f"; break;
303 case '\n': Result += "\\n"; break;
304 case '\r': Result += "\\r"; break;
305 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000306 default:
307 Result += '\\';
308 Result += toOctal(C >> 6);
309 Result += toOctal(C >> 3);
310 Result += toOctal(C >> 0);
311 break;
312 }
313 }
314 }
315 Result += "\"";
316 return Result;
317}
318
319// Print a constant value or values (it may be an aggregate).
320// Uses printSingleConstantValue() to print each individual value.
Chris Lattnerad200712003-09-09 16:23:36 +0000321void Printer::printConstantValueOnly(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000322 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000323
Chris Lattnerad200712003-09-09 16:23:36 +0000324 if (CV->isNullValue()) {
325 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
326 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
327 if (isStringCompatible(CVA)) {
328 // print the string alone and return
Chris Lattnere0121322003-08-03 23:37:09 +0000329 O << "\t.string\t" << getAsCString(CVA) << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000330 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000331 const std::vector<Use> &constValues = CVA->getValues();
332 for (unsigned i=0; i < constValues.size(); i++)
333 printConstantValueOnly(cast<Constant>(constValues[i].get()));
334 }
Chris Lattnerad200712003-09-09 16:23:36 +0000335 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
336 // Print the fields in successive locations. Pad to align if needed!
337 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
338 const std::vector<Use>& constValues = CVS->getValues();
339 unsigned sizeSoFar = 0;
340 for (unsigned i=0, N = constValues.size(); i < N; i++) {
341 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000342
Chris Lattnerad200712003-09-09 16:23:36 +0000343 // Check if padding is needed and insert one or more 0s.
344 unsigned fieldSize = TD.getTypeSize(field->getType());
345 unsigned padSize = ((i == N-1? cvsLayout->StructSize
346 : cvsLayout->MemberOffsets[i+1])
347 - cvsLayout->MemberOffsets[i]) - fieldSize;
348 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000349
Chris Lattnerad200712003-09-09 16:23:36 +0000350 // Now print the actual field value
351 printConstantValueOnly(field);
352
353 // Insert the field padding unless it's zero bytes...
354 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000355 }
Chris Lattnerad200712003-09-09 16:23:36 +0000356 assert(sizeSoFar == cvsLayout->StructSize &&
357 "Layout of constant struct may be incorrect!");
358 } else
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000359 printSingleConstantValue(CV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000360}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000361
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000362/// printConstantPool - Print to the current output stream assembly
363/// representations of the constants in the constant pool MCP. This is
364/// used to print out constants which have been "spilled to memory" by
365/// the code generator.
366///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000367void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000368 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000369 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000370
Chris Lattnerb7089442003-01-13 00:35:03 +0000371 if (CP.empty()) return;
372
373 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
374 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000375 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000376 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000377 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
378 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000379 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000380 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000381}
382
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000383/// runOnMachineFunction - This uses the printMachineInstruction()
384/// method to print assembly for each instruction.
385///
Chris Lattner0285a332002-12-28 20:25:38 +0000386bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000387 // BBNumber is used here so that a given Printer will never give two
388 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000389 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000390
Chris Lattnere0121322003-08-03 23:37:09 +0000391 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000392 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000393 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000394
Chris Lattnerb7089442003-01-13 00:35:03 +0000395 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000396 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000397
Brian Gaeke6559bb92002-11-14 22:32:30 +0000398 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000399 O << "\t.text\n";
400 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000401 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000402 if (!EmitCygwin)
403 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000404 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405
Brian Gaeked7908f62003-06-27 00:00:48 +0000406 // Number each basic block so that we can consistently refer to them
407 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000408 NumberForBB.clear();
409 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
410 I != E; ++I) {
411 NumberForBB[I->getBasicBlock()] = BBNumber++;
412 }
413
Brian Gaeke6559bb92002-11-14 22:32:30 +0000414 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000415 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
416 I != E; ++I) {
417 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000418 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000419 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000420 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
421 II != E; ++II) {
422 // Print the assembly for the instruction.
423 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000424 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000425 }
Chris Lattner0285a332002-12-28 20:25:38 +0000426 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000427
428 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000429 return false;
430}
431
Chris Lattner3d3067b2002-11-21 20:44:15 +0000432static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000433 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000434 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
435 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000436}
437
438static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000439 if (MI->getOperand(Op).isFrameIndex()) return true;
440 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000441 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000442 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
443 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000444}
445
Brian Gaeke2a098772003-08-11 19:05:46 +0000446
447
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000448void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000449 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000450 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000451 switch (MO.getType()) {
452 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000453 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000454 O << "<" << V->getName() << ">";
455 return;
456 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000457 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000458 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000459 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000460 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000461 O << "%" << RI.get(MO.getReg()).Name;
462 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000463 O << "%reg" << MO.getReg();
464 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000465
466 case MachineOperand::MO_SignExtendedImmed:
467 case MachineOperand::MO_UnextendedImmed:
468 O << (int)MO.getImmedValue();
469 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000470 case MachineOperand::MO_PCRelativeDisp: {
471 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
472 assert (i != NumberForBB.end()
473 && "Could not find a BB in the NumberForBB map!");
474 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000475 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000476 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000477 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000478 if (!elideOffsetKeyword)
479 O << "OFFSET ";
480 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000481 return;
482 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000483 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000484 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000485 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000486 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000487 }
488}
489
Chris Lattner3501fea2003-01-14 22:00:31 +0000490static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000491 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000492 default: assert(0 && "Unknown arg size!");
493 case X86II::Arg8: return "BYTE PTR";
494 case X86II::Arg16: return "WORD PTR";
495 case X86II::Arg32: return "DWORD PTR";
496 case X86II::Arg64: return "QWORD PTR";
497 case X86II::ArgF32: return "DWORD PTR";
498 case X86II::ArgF64: return "QWORD PTR";
499 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000500 }
501}
502
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000503void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000504 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000505
506 if (MI->getOperand(Op).isFrameIndex()) {
507 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
508 if (MI->getOperand(Op+3).getImmedValue())
509 O << " + " << MI->getOperand(Op+3).getImmedValue();
510 O << "]";
511 return;
512 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000513 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000514 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000515 if (MI->getOperand(Op+3).getImmedValue())
516 O << " + " << MI->getOperand(Op+3).getImmedValue();
517 O << "]";
518 return;
519 }
520
Chris Lattner3d3067b2002-11-21 20:44:15 +0000521 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000522 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000523 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000524 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000525
526 O << "[";
527 bool NeedPlus = false;
528 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000529 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000530 NeedPlus = true;
531 }
532
533 if (IndexReg.getReg()) {
534 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000535 if (ScaleVal != 1)
536 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000537 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000538 NeedPlus = true;
539 }
540
Chris Lattner0285a332002-12-28 20:25:38 +0000541 if (DispVal) {
542 if (NeedPlus)
543 if (DispVal > 0)
544 O << " + ";
545 else {
546 O << " - ";
547 DispVal = -DispVal;
548 }
549 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000550 }
551 O << "]";
552}
553
Brian Gaeke2a098772003-08-11 19:05:46 +0000554/// checkImplUses - Emit the implicit-use registers for the
555/// instruction described by DESC, if its PrintImplUses flag is set.
556///
557void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
558 const MRegisterInfo &RI = *TM.getRegisterInfo();
559 if (Desc.TSFlags & X86II::PrintImplUses) {
560 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
561 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000562 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000563 }
564 }
565}
566
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000567/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000568/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000569///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000570void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000571 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000572 const TargetInstrInfo &TII = TM.getInstrInfo();
573 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000574
Chris Lattnereca1f632002-12-25 05:09:01 +0000575 switch (Desc.TSFlags & X86II::FormMask) {
576 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000577 // Print pseudo-instructions as comments; either they should have been
578 // turned into real instructions by now, or they don't need to be
579 // seen by the assembler (e.g., IMPLICIT_USEs.)
580 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000581 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000582 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000583 O << " = phi ";
584 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
585 if (i != 1) O << ", ";
586 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000587 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000589 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000590 O << "]";
591 }
592 } else {
593 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000594 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
595 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000596 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000597 O << " = ";
598 ++i;
599 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000600 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000601
602 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
603 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000604 if (MI->getOperand(i).opIsDefOnly() ||
605 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000606 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000607 if (MI->getOperand(i).opIsDefOnly() ||
608 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000609 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000610 }
611 O << "\n";
612 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000613
Chris Lattnerf9f60882002-11-18 06:56:51 +0000614 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000615 // The accepted forms of Raw instructions are:
616 // 1. nop - No operand required
617 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000618 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000619 //
620 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000621 (MI->getNumOperands() == 1 &&
622 (MI->getOperand(0).isPCRelativeDisp() ||
623 MI->getOperand(0).isGlobalAddress() ||
624 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000625 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000626 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000627
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000628 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000629 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000630 }
631 O << "\n";
632 return;
633
Chris Lattner77875d82002-11-21 02:00:20 +0000634 case X86II::AddRegFrm: {
635 // There are currently two forms of acceptable AddRegFrm instructions.
636 // Either the instruction JUST takes a single register (like inc, dec, etc),
637 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000638 // (move immediate f.e.). Note that this immediate value might be stored as
639 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000640 // into a register. The initial register might be duplicated if this is a
641 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000642 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000643 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000644 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000645 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000646 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000647 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000648 MI->getOperand(1).isRegister() ||
649 MI->getOperand(1).isGlobalAddress() ||
650 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000651 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000652
Chris Lattner77875d82002-11-21 02:00:20 +0000653 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000654
Brian Gaeked7908f62003-06-27 00:00:48 +0000655 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000656 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000657 if (MI->getNumOperands() == 2 &&
658 (!MI->getOperand(1).isRegister() ||
659 MI->getOperand(1).getVRegValueOrNull() ||
660 MI->getOperand(1).isGlobalAddress() ||
661 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000662 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000663 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000664 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000665 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000666 O << "\n";
667 return;
668 }
Chris Lattner233ad712002-11-21 01:33:44 +0000669 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000670 // There are two acceptable forms of MRMDestReg instructions, those with 2,
671 // 3 and 4 operands:
672 //
673 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000674 //
675 // 3 Operands: in this form, the first two registers (the destination, and
676 // the first operand) should be the same, post register allocation. The 3rd
677 // operand is an additional input. This should be for things like add
678 // instructions.
679 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000680 // 4 Operands: This form is for instructions which are 3 operands forms, but
681 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000683 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000684 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000685 (MI->getNumOperands() == 2 ||
686 (isTwoAddr && MI->getOperand(1).isRegister() &&
687 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
688 (MI->getNumOperands() == 3 ||
689 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000690 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000691
Brian Gaeked7908f62003-06-27 00:00:48 +0000692 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000693 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000694 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000695 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000696 if (MI->getNumOperands() == 4) {
697 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000698 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000699 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000700 O << "\n";
701 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000702 }
Chris Lattner18042332002-11-21 21:03:39 +0000703
704 case X86II::MRMDestMem: {
705 // These instructions are the same as MRMDestReg, but instead of having a
706 // register reference for the mod/rm field, it's a memory reference.
707 //
708 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000709 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000710
Brian Gaeked7908f62003-06-27 00:00:48 +0000711 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000712 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000713 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000714 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000715 O << "\n";
716 return;
717 }
718
Chris Lattner233ad712002-11-21 01:33:44 +0000719 case X86II::MRMSrcReg: {
Chris Lattner644e1ab2002-11-21 00:30:01 +0000720 // There is a two forms that are acceptable for MRMSrcReg instructions,
721 // those with 3 and 2 operands:
722 //
723 // 3 Operands: in this form, the last register (the second input) is the
724 // ModR/M input. The first two operands should be the same, post register
725 // allocation. This is for things like: add r32, r/m32
726 //
727 // 2 Operands: this is for things like mov that do not read a second input
728 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000729 assert(MI->getOperand(0).isRegister() &&
730 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000731 (MI->getNumOperands() == 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000732 (MI->getNumOperands() == 3 && MI->getOperand(2).isRegister()))
Chris Lattnerb7089442003-01-13 00:35:03 +0000733 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000734 if (MI->getNumOperands() == 3 &&
735 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
736 O << "**";
737
Brian Gaeked7908f62003-06-27 00:00:48 +0000738 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000739 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000740 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000741 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000742 O << "\n";
743 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000744 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000745
Chris Lattner3d3067b2002-11-21 20:44:15 +0000746 case X86II::MRMSrcMem: {
747 // These instructions are the same as MRMSrcReg, but instead of having a
748 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000749 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000750 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000751 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000752 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000753 isMem(MI, 2))
754 && "Bad format for MRMDestReg!");
755 if (MI->getNumOperands() == 2+4 &&
756 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
757 O << "**";
758
Brian Gaeked7908f62003-06-27 00:00:48 +0000759 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000760 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000761 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000762 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000763 O << "\n";
764 return;
765 }
766
Chris Lattner675dd2c2002-11-21 17:09:01 +0000767 case X86II::MRMS0r: case X86II::MRMS1r:
768 case X86II::MRMS2r: case X86II::MRMS3r:
769 case X86II::MRMS4r: case X86II::MRMS5r:
770 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000771 // In this form, the following are valid formats:
772 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000773 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000774 // 2. shl rdest, rinput <implicit CL or 1>
775 // 3. sbb rdest, rinput, immediate [rdest = rinput]
776 //
777 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000778 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000779 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000780 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000781 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000782 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000783 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000784 "Bad MRMSxR format!");
785
Chris Lattnerd9096832002-12-15 08:01:39 +0000786 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000787 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
788 O << "**";
789
Brian Gaeked7908f62003-06-27 00:00:48 +0000790 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000791 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000792 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000793 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000794 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000795 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000796 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000797 O << "\n";
798
799 return;
800 }
801
Chris Lattnerb7089442003-01-13 00:35:03 +0000802 case X86II::MRMS0m: case X86II::MRMS1m:
803 case X86II::MRMS2m: case X86II::MRMS3m:
804 case X86II::MRMS4m: case X86II::MRMS5m:
805 case X86II::MRMS6m: case X86II::MRMS7m: {
806 // In this form, the following are valid formats:
807 // 1. sete [m]
808 // 2. cmp [m], immediate
809 // 2. shl [m], rinput <implicit CL or 1>
810 // 3. sbb [m], immediate
811 //
812 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
813 isMem(MI, 0) && "Bad MRMSxM format!");
814 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
815 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000816 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
817 // is misassembled by gas in intel_syntax mode as its 32-bit
818 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
819 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000820 if (MI->getOpCode() == X86::FSTPr80) {
821 if ((MI->getOperand(0).getReg() == X86::ESP)
822 && (MI->getOperand(1).getImmedValue() == 1)) {
823 int DispVal = MI->getOperand(3).getImmedValue();
824 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
825 unsigned int val = (unsigned int) DispVal;
826 O << ".byte 0xdb, 0xbc, 0x24\n\t";
827 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
828 } else { // 1 byte disp.
829 unsigned char val = (unsigned char) DispVal;
830 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
831 << std::dec << "\t# ";
832 }
833 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000834 }
835 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
836 // misassembled by gas in intel_syntax mode as its 32-bit
837 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
838 // opcode bytes instead of the instruction.
839 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000840 if ((MI->getOperand(0).getReg() == X86::ESP)
841 && (MI->getOperand(1).getImmedValue() == 1)) {
842 int DispVal = MI->getOperand(3).getImmedValue();
843 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
844 unsigned int val = (unsigned int) DispVal;
845 O << ".byte 0xdb, 0xac, 0x24\n\t";
846 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
847 } else { // 1 byte disp.
848 unsigned char val = (unsigned char) DispVal;
849 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
850 << std::dec << "\t# ";
851 }
852 }
853 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000854 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
855 // invalid opcode, saying "64 bit operations are only supported in
856 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
857 // [...]", which is wrong. Workaround: Output the raw opcode bytes
858 // instead of the instruction.
859 if (MI->getOpCode() == X86::FILDr64) {
860 if ((MI->getOperand(0).getReg() == X86::ESP)
861 && (MI->getOperand(1).getImmedValue() == 1)) {
862 int DispVal = MI->getOperand(3).getImmedValue();
863 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
864 unsigned int val = (unsigned int) DispVal;
865 O << ".byte 0xdf, 0xac, 0x24\n\t";
866 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
867 } else { // 1 byte disp.
868 unsigned char val = (unsigned char) DispVal;
869 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
870 << std::dec << "\t# ";
871 }
872 }
873 }
874 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
875 // an invalid opcode, saying "64 bit operations are only
876 // supported in 64 bit modes." libopcodes disassembles it as
877 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
878 // "fistpll DWORD PTR " instead, which is what libopcodes is
879 // expecting to see.
880 if (MI->getOpCode() == X86::FISTPr64) {
881 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000882 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000883 if (MI->getNumOperands() == 5) {
884 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000885 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000886 }
887 O << "\t# ";
888 }
889
Brian Gaeked7908f62003-06-27 00:00:48 +0000890 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000891 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000892 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000893 if (MI->getNumOperands() == 5) {
894 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000895 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000896 }
897 O << "\n";
898 return;
899 }
900
Chris Lattnerf9f60882002-11-18 06:56:51 +0000901 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000902 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000903 }
Chris Lattner72614082002-10-25 22:55:53 +0000904}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000905
Chris Lattner93c1afa2003-08-11 19:35:26 +0000906bool Printer::doInitialization(Module &M) {
907 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000908 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000909 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
910 // instruction as a reference to the register named sp, and if you try to
911 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
912 // before being looked up in the symbol table. This creates spurious
913 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
914 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000915 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000916 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000917 return false; // success
918}
919
Chris Lattnerad200712003-09-09 16:23:36 +0000920// SwitchSection - Switch to the specified section of the executable if we are
921// not already in it!
922//
923static void SwitchSection(std::ostream &OS, std::string &CurSection,
924 const char *NewSection) {
925 if (CurSection != NewSection) {
926 CurSection = NewSection;
927 if (!CurSection.empty())
928 OS << "\t" << NewSection << "\n";
929 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000930}
931
Chris Lattnerad200712003-09-09 16:23:36 +0000932bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000933 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000934 std::string CurSection;
935
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000936 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000937 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
938 if (I->hasInitializer()) { // External global require no code
939 O << "\n\n";
940 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000941 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000942 unsigned Size = TD.getTypeSize(C->getType());
943 unsigned Align = TD.getTypeAlignment(C->getType());
944
945 if (C->isNullValue() &&
946 (I->hasLinkOnceLinkage() || I->hasInternalLinkage())) {
947 SwitchSection(O, CurSection, ".data");
948 if (I->hasInternalLinkage())
949 O << "\t.local " << name << "\n";
950
951 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000952 << "," << (unsigned)TD.getTypeAlignment(C->getType());
953 O << "\t\t# ";
954 WriteAsOperand(O, I, true, true, &M);
955 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000956 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000957 switch (I->getLinkage()) {
958 case GlobalValue::LinkOnceLinkage:
959 // Nonnull linkonce -> weak
960 O << "\t.weak " << name << "\n";
961 SwitchSection(O, CurSection, "");
962 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
963 break;
964
965 case GlobalValue::AppendingLinkage:
966 // FIXME: appending linkage variables should go into a section of
967 // their name or something. For now, just emit them as external.
968 case GlobalValue::ExternalLinkage:
969 // If external or appending, declare as a global symbol
970 O << "\t.globl " << name << "\n";
971 // FALL THROUGH
972 case GlobalValue::InternalLinkage:
973 if (C->isNullValue())
974 SwitchSection(O, CurSection, ".bss");
975 else
976 SwitchSection(O, CurSection, ".data");
977 break;
978 }
979
980 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000981 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000982 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000983 O << name << ":\t\t\t\t# ";
984 WriteAsOperand(O, I, true, true, &M);
985 O << " = ";
986 WriteAsOperand(O, C, false, false, &M);
987 O << "\n";
988 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000989 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000990 }
Chris Lattnerad200712003-09-09 16:23:36 +0000991
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000992 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000993 return false; // success
994}