blob: f1e29c5579575d7bfae7d8b34002f320bd50a6f4 [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Brian Gaeke92bdfe62003-07-23 18:37:06 +000010// This file contains a printer that converts from our internal
11// representation of machine-dependent LLVM code to Intel-format
12// assembly language. This printer is the output mechanism used
Chris Lattner93c1afa2003-08-11 19:35:26 +000013// by `llc' and `lli -print-machineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Chris Lattnere0121322003-08-03 23:37:09 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000021#include "llvm/Module.h"
22#include "llvm/Assembly/Writer.h"
Chris Lattner0285a332002-12-28 20:25:38 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000025#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000026#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000027#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000028#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000029#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000030#include "Support/CommandLine.h"
Chris Lattner72614082002-10-25 22:55:53 +000031
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000032namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000033 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34
Chris Lattner93c1afa2003-08-11 19:35:26 +000035 // FIXME: This should be automatically picked up by autoconf from the C
36 // frontend
37 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
38 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
39
Chris Lattner0285a332002-12-28 20:25:38 +000040 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000041 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000042 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000043 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000044
45 /// Target machine description which we query for reg. names, data
46 /// layout, etc.
47 ///
48 TargetMachine &TM;
49
Brian Gaeked9fb37a2003-07-24 20:20:44 +000050 /// Name-mangler for global names.
51 ///
52 Mangler *Mang;
53
Brian Gaekede420ae2003-07-23 20:25:08 +000054 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000055
56 /// We name each basic block in a Function with a unique number, so
57 /// that we can consistently refer to them later. This is cleared
58 /// at the beginning of each call to runOnMachineFunction().
59 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000060 typedef std::map<const Value *, unsigned> ValueMapTy;
61 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000062
63 /// Cache of mangled name for current function. This is
64 /// recalculated at the beginning of each call to
65 /// runOnMachineFunction().
66 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000067 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000068
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000069 virtual const char *getPassName() const {
70 return "X86 Assembly Printer";
71 }
72
Brian Gaeke2a098772003-08-11 19:05:46 +000073 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000074 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000075 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000076 bool elideOffsetKeyword = false);
77 void printMemReference(const MachineInstr *MI, unsigned Op);
78 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000079 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000080 std::string ConstantExprToString(const ConstantExpr* CE);
81 std::string valToExprString(const Value* V);
Brian Gaeke9e474c42003-06-19 19:32:32 +000082 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
Chris Lattnerad200712003-09-09 16:23:36 +000084 void printConstantValueOnly(const Constant* CV);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000085 void printSingleConstantValue(const Constant* CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000086 };
Brian Gaeked7908f62003-06-27 00:00:48 +000087} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000088
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000090/// assembly code for a MachineFunction to the given output stream,
91/// using the given target machine description. This should work
92/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000093///
Brian Gaeke9d99b432003-08-13 18:15:15 +000094FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000095 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000096}
97
Brian Gaeke92bdfe62003-07-23 18:37:06 +000098/// valToExprString - Helper function for ConstantExprToString().
99/// Appends result to argument string S.
100///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000101std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000102 std::string S;
103 bool failed = false;
104 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
105 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
106 S += std::string(CB == ConstantBool::True ? "1" : "0");
107 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
108 S += itostr(CI->getValue());
109 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
110 S += utostr(CI->getValue());
111 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
112 S += ftostr(CFP->getValue());
113 else if (isa<ConstantPointerNull>(CV))
114 S += "0";
115 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
116 S += valToExprString(CPR->getValue());
117 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
118 S += ConstantExprToString(CE);
119 else
120 failed = true;
121 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000122 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 }
124 else
125 failed = true;
126
127 if (failed) {
128 assert(0 && "Cannot convert value to string");
129 S += "<illegal-value>";
130 }
131 return S;
132}
133
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000134/// ConstantExprToString - Convert a ConstantExpr to an asm expression
135/// and return this as a string.
136///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000137std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000138 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000139 switch(CE->getOpcode()) {
140 case Instruction::GetElementPtr:
141 { // generate a symbolic expression for the byte address
142 const Value* ptrVal = CE->getOperand(0);
143 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000144 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
145 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
146 else
147 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000148 }
149
150 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000151 // Support only non-converting or widening casts for now, that is,
152 // ones that do not involve a change in value. This assertion is
153 // not a complete check.
154 {
155 Constant *Op = CE->getOperand(0);
156 const Type *OpTy = Op->getType(), *Ty = CE->getType();
157 assert(((isa<PointerType>(OpTy)
158 && (Ty == Type::LongTy || Ty == Type::ULongTy))
159 || (isa<PointerType>(Ty)
160 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
161 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000162 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000163 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000164 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000165 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166
167 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000168 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
169 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170
171 default:
172 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000173 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000175}
176
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000177/// printSingleConstantValue - Print a single constant value.
178///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179void
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000180Printer::printSingleConstantValue(const Constant* CV)
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000181{
182 assert(CV->getType() != Type::VoidTy &&
183 CV->getType() != Type::TypeTy &&
184 CV->getType() != Type::LabelTy &&
185 "Unexpected type for Constant");
186
187 assert((!isa<ConstantArray>(CV) && ! isa<ConstantStruct>(CV))
188 && "Aggregate types should be handled outside this function");
189
190 const Type *type = CV->getType();
191 O << "\t";
192 switch(type->getPrimitiveID())
193 {
194 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
195 O << ".byte";
196 break;
197 case Type::UShortTyID: case Type::ShortTyID:
198 O << ".word";
199 break;
200 case Type::UIntTyID: case Type::IntTyID: case Type::PointerTyID:
201 O << ".long";
202 break;
203 case Type::ULongTyID: case Type::LongTyID:
204 O << ".quad";
205 break;
206 case Type::FloatTyID:
207 O << ".long";
208 break;
209 case Type::DoubleTyID:
210 O << ".quad";
211 break;
212 case Type::ArrayTyID:
213 if ((cast<ArrayType>(type)->getElementType() == Type::UByteTy) ||
214 (cast<ArrayType>(type)->getElementType() == Type::SByteTy))
215 O << ".string";
216 else
217 assert (0 && "Can't handle printing this type of array");
218 break;
219 default:
220 assert (0 && "Can't handle printing this type of thing");
221 break;
222 }
223 O << "\t";
224
Chris Lattner603c5622003-11-03 19:33:45 +0000225 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
226 // Constant expression built from operators, constants, and
227 // symbolic addrs
228 O << ConstantExprToString(CE) << "\n";
229 } else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
230 O << (CB->getValue() ? "1\n" : "0\n");
231 } else if (type->isFloatingPoint()) {
232 // FP Constants are printed as integer constants to avoid losing
233 // precision...
234 double Val = cast<ConstantFP>(CV)->getValue();
235 if (type == Type::FloatTy) {
236 float FVal = (float)Val;
237 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
238 O << *(unsigned int*)ProxyPtr;
239 } else if (type == Type::DoubleTy) {
240 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
241 O << *(uint64_t*)ProxyPtr;
242 } else {
243 assert(0 && "Unknown floating point type!");
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000244 }
Chris Lattner603c5622003-11-03 19:33:45 +0000245
246 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
247 } else if (type->isPrimitiveType()) {
248
249 WriteAsOperand(O, CV, false, false) << "\n";
250 } else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV)) {
251 // This is a constant address for a global variable or method.
252 // Use the name of the variable or method as the address value.
253 O << Mang->getValueName(CPR->getValue()) << "\n";
254 } else if (isa<ConstantPointerNull>(CV)) {
255 // Null pointer value
256 O << "0\n";
257 } else {
258 assert(0 && "Unknown elementary type for constant");
259 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000260}
261
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000262/// isStringCompatible - Can we treat the specified array as a string?
263/// Only if it is an array of ubytes or non-negative sbytes.
264///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000265static bool isStringCompatible(const ConstantArray *CVA) {
266 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
267 if (ETy == Type::UByteTy) return true;
268 if (ETy != Type::SByteTy) return false;
269
270 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
271 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
272 return false;
273
274 return true;
275}
276
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000277/// toOctal - Convert the low order bits of X into an octal digit.
278///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000279static inline char toOctal(int X) {
280 return (X&7)+'0';
281}
282
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000283/// getAsCString - Return the specified array as a C compatible
284/// string, only if the predicate isStringCompatible is true.
285///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000286static std::string getAsCString(const ConstantArray *CVA) {
287 assert(isStringCompatible(CVA) && "Array is not string compatible!");
288
289 std::string Result;
290 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
291 Result = "\"";
292 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000293 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000294
295 if (C == '"') {
296 Result += "\\\"";
297 } else if (C == '\\') {
298 Result += "\\\\";
299 } else if (isprint(C)) {
300 Result += C;
301 } else {
302 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000303 case '\b': Result += "\\b"; break;
304 case '\f': Result += "\\f"; break;
305 case '\n': Result += "\\n"; break;
306 case '\r': Result += "\\r"; break;
307 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000308 default:
309 Result += '\\';
310 Result += toOctal(C >> 6);
311 Result += toOctal(C >> 3);
312 Result += toOctal(C >> 0);
313 break;
314 }
315 }
316 }
317 Result += "\"";
318 return Result;
319}
320
321// Print a constant value or values (it may be an aggregate).
322// Uses printSingleConstantValue() to print each individual value.
Chris Lattnerad200712003-09-09 16:23:36 +0000323void Printer::printConstantValueOnly(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000324 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000325
Chris Lattnerad200712003-09-09 16:23:36 +0000326 if (CV->isNullValue()) {
327 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
328 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
329 if (isStringCompatible(CVA)) {
330 // print the string alone and return
Chris Lattnerb1698412003-10-19 02:51:01 +0000331 O << "\t.ascii\t" << getAsCString(CVA) << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000332 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000333 const std::vector<Use> &constValues = CVA->getValues();
334 for (unsigned i=0; i < constValues.size(); i++)
335 printConstantValueOnly(cast<Constant>(constValues[i].get()));
336 }
Chris Lattnerad200712003-09-09 16:23:36 +0000337 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
338 // Print the fields in successive locations. Pad to align if needed!
339 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
340 const std::vector<Use>& constValues = CVS->getValues();
341 unsigned sizeSoFar = 0;
342 for (unsigned i=0, N = constValues.size(); i < N; i++) {
343 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000344
Chris Lattnerad200712003-09-09 16:23:36 +0000345 // Check if padding is needed and insert one or more 0s.
346 unsigned fieldSize = TD.getTypeSize(field->getType());
347 unsigned padSize = ((i == N-1? cvsLayout->StructSize
348 : cvsLayout->MemberOffsets[i+1])
349 - cvsLayout->MemberOffsets[i]) - fieldSize;
350 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000351
Chris Lattnerad200712003-09-09 16:23:36 +0000352 // Now print the actual field value
353 printConstantValueOnly(field);
354
355 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000356 if (padSize)
357 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000358 }
Chris Lattnerad200712003-09-09 16:23:36 +0000359 assert(sizeSoFar == cvsLayout->StructSize &&
360 "Layout of constant struct may be incorrect!");
361 } else
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000362 printSingleConstantValue(CV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000363}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000364
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000365/// printConstantPool - Print to the current output stream assembly
366/// representations of the constants in the constant pool MCP. This is
367/// used to print out constants which have been "spilled to memory" by
368/// the code generator.
369///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000370void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000371 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000372 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000373
Chris Lattnerb7089442003-01-13 00:35:03 +0000374 if (CP.empty()) return;
375
376 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
377 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000378 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000379 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000380 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
381 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000382 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000383 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000384}
385
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386/// runOnMachineFunction - This uses the printMachineInstruction()
387/// method to print assembly for each instruction.
388///
Chris Lattner0285a332002-12-28 20:25:38 +0000389bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000390 // BBNumber is used here so that a given Printer will never give two
391 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000392 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000393
Chris Lattnere0121322003-08-03 23:37:09 +0000394 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000395 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000396 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000397
Chris Lattnerb7089442003-01-13 00:35:03 +0000398 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000399 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000400
Brian Gaeke6559bb92002-11-14 22:32:30 +0000401 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000402 O << "\t.text\n";
403 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000404 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000405 if (!EmitCygwin)
406 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000407 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000408
Brian Gaeked7908f62003-06-27 00:00:48 +0000409 // Number each basic block so that we can consistently refer to them
410 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000411 NumberForBB.clear();
412 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
413 I != E; ++I) {
414 NumberForBB[I->getBasicBlock()] = BBNumber++;
415 }
416
Brian Gaeke6559bb92002-11-14 22:32:30 +0000417 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000418 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
419 I != E; ++I) {
420 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000421 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000422 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000423 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
424 II != E; ++II) {
425 // Print the assembly for the instruction.
426 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000427 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000428 }
Chris Lattner0285a332002-12-28 20:25:38 +0000429 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000430
431 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000432 return false;
433}
434
Chris Lattner3d3067b2002-11-21 20:44:15 +0000435static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000436 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000437 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
438 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000439}
440
441static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000442 if (MI->getOperand(Op).isFrameIndex()) return true;
443 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000444 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000445 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
446 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000447}
448
Brian Gaeke2a098772003-08-11 19:05:46 +0000449
450
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000451void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000452 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000453 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000454 switch (MO.getType()) {
455 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000456 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000457 O << "<" << V->getName() << ">";
458 return;
459 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000460 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000461 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000462 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000463 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000464 O << "%" << RI.get(MO.getReg()).Name;
465 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000466 O << "%reg" << MO.getReg();
467 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000468
469 case MachineOperand::MO_SignExtendedImmed:
470 case MachineOperand::MO_UnextendedImmed:
471 O << (int)MO.getImmedValue();
472 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000473 case MachineOperand::MO_PCRelativeDisp: {
474 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
475 assert (i != NumberForBB.end()
476 && "Could not find a BB in the NumberForBB map!");
477 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000478 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000479 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000481 if (!elideOffsetKeyword)
482 O << "OFFSET ";
483 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000484 return;
485 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000486 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000487 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000488 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000489 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000490 }
491}
492
Chris Lattner3501fea2003-01-14 22:00:31 +0000493static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000494 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000495 default: assert(0 && "Unknown arg size!");
496 case X86II::Arg8: return "BYTE PTR";
497 case X86II::Arg16: return "WORD PTR";
498 case X86II::Arg32: return "DWORD PTR";
499 case X86II::Arg64: return "QWORD PTR";
500 case X86II::ArgF32: return "DWORD PTR";
501 case X86II::ArgF64: return "QWORD PTR";
502 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000503 }
504}
505
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000506void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000507 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000508
509 if (MI->getOperand(Op).isFrameIndex()) {
510 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
511 if (MI->getOperand(Op+3).getImmedValue())
512 O << " + " << MI->getOperand(Op+3).getImmedValue();
513 O << "]";
514 return;
515 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000516 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000517 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000518 if (MI->getOperand(Op+3).getImmedValue())
519 O << " + " << MI->getOperand(Op+3).getImmedValue();
520 O << "]";
521 return;
522 }
523
Chris Lattner3d3067b2002-11-21 20:44:15 +0000524 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000525 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000526 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000527 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000528
529 O << "[";
530 bool NeedPlus = false;
531 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000532 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000533 NeedPlus = true;
534 }
535
536 if (IndexReg.getReg()) {
537 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000538 if (ScaleVal != 1)
539 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000540 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000541 NeedPlus = true;
542 }
543
Chris Lattner0285a332002-12-28 20:25:38 +0000544 if (DispVal) {
545 if (NeedPlus)
546 if (DispVal > 0)
547 O << " + ";
548 else {
549 O << " - ";
550 DispVal = -DispVal;
551 }
552 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000553 }
554 O << "]";
555}
556
Brian Gaeke2a098772003-08-11 19:05:46 +0000557/// checkImplUses - Emit the implicit-use registers for the
558/// instruction described by DESC, if its PrintImplUses flag is set.
559///
560void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
561 const MRegisterInfo &RI = *TM.getRegisterInfo();
562 if (Desc.TSFlags & X86II::PrintImplUses) {
563 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
564 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000565 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000566 }
567 }
568}
569
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000570/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000571/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000572///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000573void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000574 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000575 const TargetInstrInfo &TII = TM.getInstrInfo();
576 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000577
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000578 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000579 switch (Desc.TSFlags & X86II::FormMask) {
580 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000581 // Print pseudo-instructions as comments; either they should have been
582 // turned into real instructions by now, or they don't need to be
583 // seen by the assembler (e.g., IMPLICIT_USEs.)
584 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000585 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000586 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000587 O << " = phi ";
588 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
589 if (i != 1) O << ", ";
590 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000591 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000592 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000593 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000594 O << "]";
595 }
596 } else {
597 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000598 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
599 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000600 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000601 O << " = ";
602 ++i;
603 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000604 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000605
606 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
607 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000608 if (MI->getOperand(i).opIsDefOnly() ||
609 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000610 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000611 if (MI->getOperand(i).opIsDefOnly() ||
612 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000613 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000614 }
615 O << "\n";
616 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000617
Chris Lattnerf9f60882002-11-18 06:56:51 +0000618 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000619 // The accepted forms of Raw instructions are:
620 // 1. nop - No operand required
621 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000622 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000623 //
624 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000625 (MI->getNumOperands() == 1 &&
626 (MI->getOperand(0).isPCRelativeDisp() ||
627 MI->getOperand(0).isGlobalAddress() ||
628 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000629 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000630 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000631
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000632 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000633 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000634 }
635 O << "\n";
636 return;
637
Chris Lattner77875d82002-11-21 02:00:20 +0000638 case X86II::AddRegFrm: {
639 // There are currently two forms of acceptable AddRegFrm instructions.
640 // Either the instruction JUST takes a single register (like inc, dec, etc),
641 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000642 // (move immediate f.e.). Note that this immediate value might be stored as
643 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000644 // into a register. The initial register might be duplicated if this is a
645 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000646 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000647 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000648 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000649 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000650 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000651 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000652 MI->getOperand(1).isRegister() ||
653 MI->getOperand(1).isGlobalAddress() ||
654 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000655 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000656
Chris Lattner77875d82002-11-21 02:00:20 +0000657 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000658
Brian Gaeked7908f62003-06-27 00:00:48 +0000659 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000660 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000661 if (MI->getNumOperands() == 2 &&
662 (!MI->getOperand(1).isRegister() ||
663 MI->getOperand(1).getVRegValueOrNull() ||
664 MI->getOperand(1).isGlobalAddress() ||
665 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000666 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000667 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000668 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000669 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000670 O << "\n";
671 return;
672 }
Chris Lattner233ad712002-11-21 01:33:44 +0000673 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000674 // There are two acceptable forms of MRMDestReg instructions, those with 2,
675 // 3 and 4 operands:
676 //
677 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000678 //
679 // 3 Operands: in this form, the first two registers (the destination, and
680 // the first operand) should be the same, post register allocation. The 3rd
681 // operand is an additional input. This should be for things like add
682 // instructions.
683 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000684 // 4 Operands: This form is for instructions which are 3 operands forms, but
685 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000686 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000687 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000688 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000689 (MI->getNumOperands() == 2 ||
690 (isTwoAddr && MI->getOperand(1).isRegister() &&
691 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
692 (MI->getNumOperands() == 3 ||
693 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000694 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000695
Brian Gaeked7908f62003-06-27 00:00:48 +0000696 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000697 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000698 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000699 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000700 if (MI->getNumOperands() == 4) {
701 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000702 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000703 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000704 O << "\n";
705 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000706 }
Chris Lattner18042332002-11-21 21:03:39 +0000707
708 case X86II::MRMDestMem: {
709 // These instructions are the same as MRMDestReg, but instead of having a
710 // register reference for the mod/rm field, it's a memory reference.
711 //
712 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000713 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000714
Brian Gaeked7908f62003-06-27 00:00:48 +0000715 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000716 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000717 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000718 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000719 O << "\n";
720 return;
721 }
722
Chris Lattner233ad712002-11-21 01:33:44 +0000723 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000724 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000725 // those with 3 and 2 operands:
726 //
727 // 3 Operands: in this form, the last register (the second input) is the
728 // ModR/M input. The first two operands should be the same, post register
729 // allocation. This is for things like: add r32, r/m32
730 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000731 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
732 // instructions like the IMULri instructions.
733 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000734 // 2 Operands: this is for things like mov that do not read a second input
735 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 assert(MI->getOperand(0).isRegister() &&
737 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000738 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000739 (MI->getNumOperands() == 3 &&
740 (MI->getOperand(2).isRegister() ||
741 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000742 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000743 if (MI->getNumOperands() == 3 &&
744 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
745 O << "**";
746
Brian Gaeked7908f62003-06-27 00:00:48 +0000747 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000748 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000749
750 // If this is IMULri* instructions, print the non-two-address operand.
751 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
752 O << ", ";
753 printOp(MI->getOperand(1));
754 }
755
Chris Lattner644e1ab2002-11-21 00:30:01 +0000756 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000757 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000758 O << "\n";
759 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000760 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000761
Chris Lattner3d3067b2002-11-21 20:44:15 +0000762 case X86II::MRMSrcMem: {
763 // These instructions are the same as MRMSrcReg, but instead of having a
764 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000765 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000766 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000767 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000768 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000769 isMem(MI, 2))
770 && "Bad format for MRMDestReg!");
771 if (MI->getNumOperands() == 2+4 &&
772 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
773 O << "**";
774
Brian Gaeked7908f62003-06-27 00:00:48 +0000775 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000776 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000777 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000778 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000779 O << "\n";
780 return;
781 }
782
Chris Lattner675dd2c2002-11-21 17:09:01 +0000783 case X86II::MRMS0r: case X86II::MRMS1r:
784 case X86II::MRMS2r: case X86II::MRMS3r:
785 case X86II::MRMS4r: case X86II::MRMS5r:
786 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000787 // In this form, the following are valid formats:
788 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000789 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 // 2. shl rdest, rinput <implicit CL or 1>
791 // 3. sbb rdest, rinput, immediate [rdest = rinput]
792 //
793 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000794 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000795 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000796 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000797 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000798 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000799 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 "Bad MRMSxR format!");
801
Chris Lattnerd9096832002-12-15 08:01:39 +0000802 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000803 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
804 O << "**";
805
Brian Gaeked7908f62003-06-27 00:00:48 +0000806 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000807 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000808 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000809 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000810 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000811 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000812 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000813 O << "\n";
814
815 return;
816 }
817
Chris Lattnerb7089442003-01-13 00:35:03 +0000818 case X86II::MRMS0m: case X86II::MRMS1m:
819 case X86II::MRMS2m: case X86II::MRMS3m:
820 case X86II::MRMS4m: case X86II::MRMS5m:
821 case X86II::MRMS6m: case X86II::MRMS7m: {
822 // In this form, the following are valid formats:
823 // 1. sete [m]
824 // 2. cmp [m], immediate
825 // 2. shl [m], rinput <implicit CL or 1>
826 // 3. sbb [m], immediate
827 //
828 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
829 isMem(MI, 0) && "Bad MRMSxM format!");
830 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
831 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000832 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
833 // is misassembled by gas in intel_syntax mode as its 32-bit
834 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
835 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000836 if (MI->getOpCode() == X86::FSTPr80) {
837 if ((MI->getOperand(0).getReg() == X86::ESP)
838 && (MI->getOperand(1).getImmedValue() == 1)) {
839 int DispVal = MI->getOperand(3).getImmedValue();
840 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
841 unsigned int val = (unsigned int) DispVal;
842 O << ".byte 0xdb, 0xbc, 0x24\n\t";
843 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
844 } else { // 1 byte disp.
845 unsigned char val = (unsigned char) DispVal;
846 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
847 << std::dec << "\t# ";
848 }
849 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000850 }
851 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
852 // misassembled by gas in intel_syntax mode as its 32-bit
853 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
854 // opcode bytes instead of the instruction.
855 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000856 if ((MI->getOperand(0).getReg() == X86::ESP)
857 && (MI->getOperand(1).getImmedValue() == 1)) {
858 int DispVal = MI->getOperand(3).getImmedValue();
859 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
860 unsigned int val = (unsigned int) DispVal;
861 O << ".byte 0xdb, 0xac, 0x24\n\t";
862 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
863 } else { // 1 byte disp.
864 unsigned char val = (unsigned char) DispVal;
865 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
866 << std::dec << "\t# ";
867 }
868 }
869 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000870 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
871 // invalid opcode, saying "64 bit operations are only supported in
872 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
873 // [...]", which is wrong. Workaround: Output the raw opcode bytes
874 // instead of the instruction.
875 if (MI->getOpCode() == X86::FILDr64) {
876 if ((MI->getOperand(0).getReg() == X86::ESP)
877 && (MI->getOperand(1).getImmedValue() == 1)) {
878 int DispVal = MI->getOperand(3).getImmedValue();
879 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
880 unsigned int val = (unsigned int) DispVal;
881 O << ".byte 0xdf, 0xac, 0x24\n\t";
882 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
883 } else { // 1 byte disp.
884 unsigned char val = (unsigned char) DispVal;
885 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
886 << std::dec << "\t# ";
887 }
888 }
889 }
890 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
891 // an invalid opcode, saying "64 bit operations are only
892 // supported in 64 bit modes." libopcodes disassembles it as
893 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
894 // "fistpll DWORD PTR " instead, which is what libopcodes is
895 // expecting to see.
896 if (MI->getOpCode() == X86::FISTPr64) {
897 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000898 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000899 if (MI->getNumOperands() == 5) {
900 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000901 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000902 }
903 O << "\t# ";
904 }
905
Brian Gaeked7908f62003-06-27 00:00:48 +0000906 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000907 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000908 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000909 if (MI->getNumOperands() == 5) {
910 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000911 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000912 }
913 O << "\n";
914 return;
915 }
916
Chris Lattnerf9f60882002-11-18 06:56:51 +0000917 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000918 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000919 }
Chris Lattner72614082002-10-25 22:55:53 +0000920}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000921
Chris Lattner93c1afa2003-08-11 19:35:26 +0000922bool Printer::doInitialization(Module &M) {
923 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000924 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000925 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
926 // instruction as a reference to the register named sp, and if you try to
927 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
928 // before being looked up in the symbol table. This creates spurious
929 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
930 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000931 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000932 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000933 return false; // success
934}
935
Chris Lattnerad200712003-09-09 16:23:36 +0000936// SwitchSection - Switch to the specified section of the executable if we are
937// not already in it!
938//
939static void SwitchSection(std::ostream &OS, std::string &CurSection,
940 const char *NewSection) {
941 if (CurSection != NewSection) {
942 CurSection = NewSection;
943 if (!CurSection.empty())
944 OS << "\t" << NewSection << "\n";
945 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000946}
947
Chris Lattnerad200712003-09-09 16:23:36 +0000948bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000949 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000950 std::string CurSection;
951
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000952 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000953 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
954 if (I->hasInitializer()) { // External global require no code
955 O << "\n\n";
956 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000957 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000958 unsigned Size = TD.getTypeSize(C->getType());
959 unsigned Align = TD.getTypeAlignment(C->getType());
960
961 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000962 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
963 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000964 SwitchSection(O, CurSection, ".data");
965 if (I->hasInternalLinkage())
966 O << "\t.local " << name << "\n";
967
968 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000969 << "," << (unsigned)TD.getTypeAlignment(C->getType());
970 O << "\t\t# ";
971 WriteAsOperand(O, I, true, true, &M);
972 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000973 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000974 switch (I->getLinkage()) {
975 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000976 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000977 // Nonnull linkonce -> weak
978 O << "\t.weak " << name << "\n";
979 SwitchSection(O, CurSection, "");
980 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
981 break;
982
983 case GlobalValue::AppendingLinkage:
984 // FIXME: appending linkage variables should go into a section of
985 // their name or something. For now, just emit them as external.
986 case GlobalValue::ExternalLinkage:
987 // If external or appending, declare as a global symbol
988 O << "\t.globl " << name << "\n";
989 // FALL THROUGH
990 case GlobalValue::InternalLinkage:
991 if (C->isNullValue())
992 SwitchSection(O, CurSection, ".bss");
993 else
994 SwitchSection(O, CurSection, ".data");
995 break;
996 }
997
998 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000999 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001000 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001001 O << name << ":\t\t\t\t# ";
1002 WriteAsOperand(O, I, true, true, &M);
1003 O << " = ";
1004 WriteAsOperand(O, C, false, false, &M);
1005 O << "\n";
1006 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001007 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001008 }
Chris Lattnerad200712003-09-09 16:23:36 +00001009
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001010 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001011 return false; // success
1012}