blob: c8072da052cff839a1ccf2c36d53d618d0425d75 [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
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000225 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV))
226 {
227 // Constant expression built from operators, constants, and
228 // symbolic addrs
229 O << ConstantExprToString(CE) << "\n";
230 }
231 else if (type->isPrimitiveType())
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000232 {
233 if (type->isFloatingPoint()) {
234 // FP Constants are printed as integer constants to avoid losing
235 // precision...
236 double Val = cast<ConstantFP>(CV)->getValue();
237 if (type == Type::FloatTy) {
238 float FVal = (float)Val;
239 char *ProxyPtr = (char*)&FVal; // Abide by C TBAA rules
240 O << *(unsigned int*)ProxyPtr;
241 } else if (type == Type::DoubleTy) {
242 char *ProxyPtr = (char*)&Val; // Abide by C TBAA rules
243 O << *(uint64_t*)ProxyPtr;
244 } else {
245 assert(0 && "Unknown floating point type!");
246 }
247
248 O << "\t# " << type->getDescription() << " value: " << Val << "\n";
249 } else {
250 WriteAsOperand(O, CV, false, false) << "\n";
251 }
252 }
253 else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV))
254 {
255 // This is a constant address for a global variable or method.
256 // Use the name of the variable or method as the address value.
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000257 O << Mang->getValueName(CPR->getValue()) << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000258 }
259 else if (isa<ConstantPointerNull>(CV))
260 {
261 // Null pointer value
262 O << "0\n";
263 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264 else
265 {
266 assert(0 && "Unknown elementary type for constant");
267 }
268}
269
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000270/// isStringCompatible - Can we treat the specified array as a string?
271/// Only if it is an array of ubytes or non-negative sbytes.
272///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000273static bool isStringCompatible(const ConstantArray *CVA) {
274 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
275 if (ETy == Type::UByteTy) return true;
276 if (ETy != Type::SByteTy) return false;
277
278 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
279 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
280 return false;
281
282 return true;
283}
284
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000285/// toOctal - Convert the low order bits of X into an octal digit.
286///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000287static inline char toOctal(int X) {
288 return (X&7)+'0';
289}
290
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000291/// getAsCString - Return the specified array as a C compatible
292/// string, only if the predicate isStringCompatible is true.
293///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000294static std::string getAsCString(const ConstantArray *CVA) {
295 assert(isStringCompatible(CVA) && "Array is not string compatible!");
296
297 std::string Result;
298 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
299 Result = "\"";
300 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000301 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000302
303 if (C == '"') {
304 Result += "\\\"";
305 } else if (C == '\\') {
306 Result += "\\\\";
307 } else if (isprint(C)) {
308 Result += C;
309 } else {
310 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000311 case '\b': Result += "\\b"; break;
312 case '\f': Result += "\\f"; break;
313 case '\n': Result += "\\n"; break;
314 case '\r': Result += "\\r"; break;
315 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000316 default:
317 Result += '\\';
318 Result += toOctal(C >> 6);
319 Result += toOctal(C >> 3);
320 Result += toOctal(C >> 0);
321 break;
322 }
323 }
324 }
325 Result += "\"";
326 return Result;
327}
328
329// Print a constant value or values (it may be an aggregate).
330// Uses printSingleConstantValue() to print each individual value.
Chris Lattnerad200712003-09-09 16:23:36 +0000331void Printer::printConstantValueOnly(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000332 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000333
Chris Lattnerad200712003-09-09 16:23:36 +0000334 if (CV->isNullValue()) {
335 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
336 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
337 if (isStringCompatible(CVA)) {
338 // print the string alone and return
Chris Lattnerb1698412003-10-19 02:51:01 +0000339 O << "\t.ascii\t" << getAsCString(CVA) << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000340 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000341 const std::vector<Use> &constValues = CVA->getValues();
342 for (unsigned i=0; i < constValues.size(); i++)
343 printConstantValueOnly(cast<Constant>(constValues[i].get()));
344 }
Chris Lattnerad200712003-09-09 16:23:36 +0000345 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
346 // Print the fields in successive locations. Pad to align if needed!
347 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
348 const std::vector<Use>& constValues = CVS->getValues();
349 unsigned sizeSoFar = 0;
350 for (unsigned i=0, N = constValues.size(); i < N; i++) {
351 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000352
Chris Lattnerad200712003-09-09 16:23:36 +0000353 // Check if padding is needed and insert one or more 0s.
354 unsigned fieldSize = TD.getTypeSize(field->getType());
355 unsigned padSize = ((i == N-1? cvsLayout->StructSize
356 : cvsLayout->MemberOffsets[i+1])
357 - cvsLayout->MemberOffsets[i]) - fieldSize;
358 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000359
Chris Lattnerad200712003-09-09 16:23:36 +0000360 // Now print the actual field value
361 printConstantValueOnly(field);
362
363 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000364 if (padSize)
365 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000366 }
Chris Lattnerad200712003-09-09 16:23:36 +0000367 assert(sizeSoFar == cvsLayout->StructSize &&
368 "Layout of constant struct may be incorrect!");
369 } else
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000370 printSingleConstantValue(CV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000371}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000372
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000373/// printConstantPool - Print to the current output stream assembly
374/// representations of the constants in the constant pool MCP. This is
375/// used to print out constants which have been "spilled to memory" by
376/// the code generator.
377///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000378void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000380 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000381
Chris Lattnerb7089442003-01-13 00:35:03 +0000382 if (CP.empty()) return;
383
384 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
385 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000387 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000388 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
389 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000390 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000391 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000392}
393
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000394/// runOnMachineFunction - This uses the printMachineInstruction()
395/// method to print assembly for each instruction.
396///
Chris Lattner0285a332002-12-28 20:25:38 +0000397bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000398 // BBNumber is used here so that a given Printer will never give two
399 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000400 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000401
Chris Lattnere0121322003-08-03 23:37:09 +0000402 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000403 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000404 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000405
Chris Lattnerb7089442003-01-13 00:35:03 +0000406 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000407 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000408
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000410 O << "\t.text\n";
411 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000412 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000413 if (!EmitCygwin)
414 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000415 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000416
Brian Gaeked7908f62003-06-27 00:00:48 +0000417 // Number each basic block so that we can consistently refer to them
418 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000419 NumberForBB.clear();
420 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
421 I != E; ++I) {
422 NumberForBB[I->getBasicBlock()] = BBNumber++;
423 }
424
Brian Gaeke6559bb92002-11-14 22:32:30 +0000425 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000426 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
427 I != E; ++I) {
428 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000429 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000430 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000431 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
432 II != E; ++II) {
433 // Print the assembly for the instruction.
434 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000435 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000436 }
Chris Lattner0285a332002-12-28 20:25:38 +0000437 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000438
439 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000440 return false;
441}
442
Chris Lattner3d3067b2002-11-21 20:44:15 +0000443static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000444 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000445 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
446 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000447}
448
449static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000450 if (MI->getOperand(Op).isFrameIndex()) return true;
451 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000452 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000453 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
454 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000455}
456
Brian Gaeke2a098772003-08-11 19:05:46 +0000457
458
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000459void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000460 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000461 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000462 switch (MO.getType()) {
463 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000464 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000465 O << "<" << V->getName() << ">";
466 return;
467 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000468 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000469 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000470 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000471 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000472 O << "%" << RI.get(MO.getReg()).Name;
473 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000474 O << "%reg" << MO.getReg();
475 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000476
477 case MachineOperand::MO_SignExtendedImmed:
478 case MachineOperand::MO_UnextendedImmed:
479 O << (int)MO.getImmedValue();
480 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000481 case MachineOperand::MO_PCRelativeDisp: {
482 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
483 assert (i != NumberForBB.end()
484 && "Could not find a BB in the NumberForBB map!");
485 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000486 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000487 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000488 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000489 if (!elideOffsetKeyword)
490 O << "OFFSET ";
491 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000492 return;
493 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000494 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000495 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000496 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000497 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000498 }
499}
500
Chris Lattner3501fea2003-01-14 22:00:31 +0000501static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000502 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000503 default: assert(0 && "Unknown arg size!");
504 case X86II::Arg8: return "BYTE PTR";
505 case X86II::Arg16: return "WORD PTR";
506 case X86II::Arg32: return "DWORD PTR";
507 case X86II::Arg64: return "QWORD PTR";
508 case X86II::ArgF32: return "DWORD PTR";
509 case X86II::ArgF64: return "QWORD PTR";
510 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000511 }
512}
513
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000514void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000515 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000516
517 if (MI->getOperand(Op).isFrameIndex()) {
518 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
519 if (MI->getOperand(Op+3).getImmedValue())
520 O << " + " << MI->getOperand(Op+3).getImmedValue();
521 O << "]";
522 return;
523 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000524 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000525 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000526 if (MI->getOperand(Op+3).getImmedValue())
527 O << " + " << MI->getOperand(Op+3).getImmedValue();
528 O << "]";
529 return;
530 }
531
Chris Lattner3d3067b2002-11-21 20:44:15 +0000532 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000533 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000534 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000535 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000536
537 O << "[";
538 bool NeedPlus = false;
539 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000540 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000541 NeedPlus = true;
542 }
543
544 if (IndexReg.getReg()) {
545 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000546 if (ScaleVal != 1)
547 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000548 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000549 NeedPlus = true;
550 }
551
Chris Lattner0285a332002-12-28 20:25:38 +0000552 if (DispVal) {
553 if (NeedPlus)
554 if (DispVal > 0)
555 O << " + ";
556 else {
557 O << " - ";
558 DispVal = -DispVal;
559 }
560 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000561 }
562 O << "]";
563}
564
Brian Gaeke2a098772003-08-11 19:05:46 +0000565/// checkImplUses - Emit the implicit-use registers for the
566/// instruction described by DESC, if its PrintImplUses flag is set.
567///
568void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
569 const MRegisterInfo &RI = *TM.getRegisterInfo();
570 if (Desc.TSFlags & X86II::PrintImplUses) {
571 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
572 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000573 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000574 }
575 }
576}
577
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000578/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000579/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000580///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000581void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000582 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000583 const TargetInstrInfo &TII = TM.getInstrInfo();
584 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000585
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000586 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000587 switch (Desc.TSFlags & X86II::FormMask) {
588 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000589 // Print pseudo-instructions as comments; either they should have been
590 // turned into real instructions by now, or they don't need to be
591 // seen by the assembler (e.g., IMPLICIT_USEs.)
592 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000593 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000594 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000595 O << " = phi ";
596 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
597 if (i != 1) O << ", ";
598 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000599 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000600 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000601 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000602 O << "]";
603 }
604 } else {
605 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000606 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
607 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000608 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000609 O << " = ";
610 ++i;
611 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000612 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000613
614 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
615 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000616 if (MI->getOperand(i).opIsDefOnly() ||
617 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000618 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000619 if (MI->getOperand(i).opIsDefOnly() ||
620 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000621 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000622 }
623 O << "\n";
624 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000625
Chris Lattnerf9f60882002-11-18 06:56:51 +0000626 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000627 // The accepted forms of Raw instructions are:
628 // 1. nop - No operand required
629 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000630 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000631 //
632 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000633 (MI->getNumOperands() == 1 &&
634 (MI->getOperand(0).isPCRelativeDisp() ||
635 MI->getOperand(0).isGlobalAddress() ||
636 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000637 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000638 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000639
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000640 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000641 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642 }
643 O << "\n";
644 return;
645
Chris Lattner77875d82002-11-21 02:00:20 +0000646 case X86II::AddRegFrm: {
647 // There are currently two forms of acceptable AddRegFrm instructions.
648 // Either the instruction JUST takes a single register (like inc, dec, etc),
649 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000650 // (move immediate f.e.). Note that this immediate value might be stored as
651 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000652 // into a register. The initial register might be duplicated if this is a
653 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000654 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000655 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000656 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000657 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000658 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000659 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000660 MI->getOperand(1).isRegister() ||
661 MI->getOperand(1).isGlobalAddress() ||
662 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000663 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000664
Chris Lattner77875d82002-11-21 02:00:20 +0000665 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000666
Brian Gaeked7908f62003-06-27 00:00:48 +0000667 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000668 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000669 if (MI->getNumOperands() == 2 &&
670 (!MI->getOperand(1).isRegister() ||
671 MI->getOperand(1).getVRegValueOrNull() ||
672 MI->getOperand(1).isGlobalAddress() ||
673 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000674 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000675 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000676 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000677 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000678 O << "\n";
679 return;
680 }
Chris Lattner233ad712002-11-21 01:33:44 +0000681 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000682 // There are two acceptable forms of MRMDestReg instructions, those with 2,
683 // 3 and 4 operands:
684 //
685 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000686 //
687 // 3 Operands: in this form, the first two registers (the destination, and
688 // the first operand) should be the same, post register allocation. The 3rd
689 // operand is an additional input. This should be for things like add
690 // instructions.
691 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 // 4 Operands: This form is for instructions which are 3 operands forms, but
693 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000694 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000695 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000696 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000697 (MI->getNumOperands() == 2 ||
698 (isTwoAddr && MI->getOperand(1).isRegister() &&
699 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
700 (MI->getNumOperands() == 3 ||
701 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000702 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000703
Brian Gaeked7908f62003-06-27 00:00:48 +0000704 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000705 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000706 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000707 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000708 if (MI->getNumOperands() == 4) {
709 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000710 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000711 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000712 O << "\n";
713 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000714 }
Chris Lattner18042332002-11-21 21:03:39 +0000715
716 case X86II::MRMDestMem: {
717 // These instructions are the same as MRMDestReg, but instead of having a
718 // register reference for the mod/rm field, it's a memory reference.
719 //
720 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000721 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000722
Brian Gaeked7908f62003-06-27 00:00:48 +0000723 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000724 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000725 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000726 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000727 O << "\n";
728 return;
729 }
730
Chris Lattner233ad712002-11-21 01:33:44 +0000731 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000732 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000733 // those with 3 and 2 operands:
734 //
735 // 3 Operands: in this form, the last register (the second input) is the
736 // ModR/M input. The first two operands should be the same, post register
737 // allocation. This is for things like: add r32, r/m32
738 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000739 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
740 // instructions like the IMULri instructions.
741 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000742 // 2 Operands: this is for things like mov that do not read a second input
743 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000744 assert(MI->getOperand(0).isRegister() &&
745 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000746 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000747 (MI->getNumOperands() == 3 &&
748 (MI->getOperand(2).isRegister() ||
749 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000750 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000751 if (MI->getNumOperands() == 3 &&
752 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
753 O << "**";
754
Brian Gaeked7908f62003-06-27 00:00:48 +0000755 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000756 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000757
758 // If this is IMULri* instructions, print the non-two-address operand.
759 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
760 O << ", ";
761 printOp(MI->getOperand(1));
762 }
763
Chris Lattner644e1ab2002-11-21 00:30:01 +0000764 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000765 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000766 O << "\n";
767 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000768 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000769
Chris Lattner3d3067b2002-11-21 20:44:15 +0000770 case X86II::MRMSrcMem: {
771 // These instructions are the same as MRMSrcReg, but instead of having a
772 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000773 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000774 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000775 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000776 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000777 isMem(MI, 2))
778 && "Bad format for MRMDestReg!");
779 if (MI->getNumOperands() == 2+4 &&
780 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
781 O << "**";
782
Brian Gaeked7908f62003-06-27 00:00:48 +0000783 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000784 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000785 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000786 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000787 O << "\n";
788 return;
789 }
790
Chris Lattner675dd2c2002-11-21 17:09:01 +0000791 case X86II::MRMS0r: case X86II::MRMS1r:
792 case X86II::MRMS2r: case X86II::MRMS3r:
793 case X86II::MRMS4r: case X86II::MRMS5r:
794 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000795 // In this form, the following are valid formats:
796 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000797 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000798 // 2. shl rdest, rinput <implicit CL or 1>
799 // 3. sbb rdest, rinput, immediate [rdest = rinput]
800 //
801 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000802 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000803 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000804 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000805 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000806 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000807 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000808 "Bad MRMSxR format!");
809
Chris Lattnerd9096832002-12-15 08:01:39 +0000810 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000811 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
812 O << "**";
813
Brian Gaeked7908f62003-06-27 00:00:48 +0000814 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000815 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000816 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000817 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000818 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000819 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000820 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000821 O << "\n";
822
823 return;
824 }
825
Chris Lattnerb7089442003-01-13 00:35:03 +0000826 case X86II::MRMS0m: case X86II::MRMS1m:
827 case X86II::MRMS2m: case X86II::MRMS3m:
828 case X86II::MRMS4m: case X86II::MRMS5m:
829 case X86II::MRMS6m: case X86II::MRMS7m: {
830 // In this form, the following are valid formats:
831 // 1. sete [m]
832 // 2. cmp [m], immediate
833 // 2. shl [m], rinput <implicit CL or 1>
834 // 3. sbb [m], immediate
835 //
836 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
837 isMem(MI, 0) && "Bad MRMSxM format!");
838 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
839 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000840 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
841 // is misassembled by gas in intel_syntax mode as its 32-bit
842 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
843 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000844 if (MI->getOpCode() == X86::FSTPr80) {
845 if ((MI->getOperand(0).getReg() == X86::ESP)
846 && (MI->getOperand(1).getImmedValue() == 1)) {
847 int DispVal = MI->getOperand(3).getImmedValue();
848 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
849 unsigned int val = (unsigned int) DispVal;
850 O << ".byte 0xdb, 0xbc, 0x24\n\t";
851 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
852 } else { // 1 byte disp.
853 unsigned char val = (unsigned char) DispVal;
854 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
855 << std::dec << "\t# ";
856 }
857 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000858 }
859 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
860 // misassembled by gas in intel_syntax mode as its 32-bit
861 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
862 // opcode bytes instead of the instruction.
863 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000864 if ((MI->getOperand(0).getReg() == X86::ESP)
865 && (MI->getOperand(1).getImmedValue() == 1)) {
866 int DispVal = MI->getOperand(3).getImmedValue();
867 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
868 unsigned int val = (unsigned int) DispVal;
869 O << ".byte 0xdb, 0xac, 0x24\n\t";
870 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
871 } else { // 1 byte disp.
872 unsigned char val = (unsigned char) DispVal;
873 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
874 << std::dec << "\t# ";
875 }
876 }
877 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000878 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
879 // invalid opcode, saying "64 bit operations are only supported in
880 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
881 // [...]", which is wrong. Workaround: Output the raw opcode bytes
882 // instead of the instruction.
883 if (MI->getOpCode() == X86::FILDr64) {
884 if ((MI->getOperand(0).getReg() == X86::ESP)
885 && (MI->getOperand(1).getImmedValue() == 1)) {
886 int DispVal = MI->getOperand(3).getImmedValue();
887 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
888 unsigned int val = (unsigned int) DispVal;
889 O << ".byte 0xdf, 0xac, 0x24\n\t";
890 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
891 } else { // 1 byte disp.
892 unsigned char val = (unsigned char) DispVal;
893 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
894 << std::dec << "\t# ";
895 }
896 }
897 }
898 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
899 // an invalid opcode, saying "64 bit operations are only
900 // supported in 64 bit modes." libopcodes disassembles it as
901 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
902 // "fistpll DWORD PTR " instead, which is what libopcodes is
903 // expecting to see.
904 if (MI->getOpCode() == X86::FISTPr64) {
905 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000906 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000907 if (MI->getNumOperands() == 5) {
908 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000909 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000910 }
911 O << "\t# ";
912 }
913
Brian Gaeked7908f62003-06-27 00:00:48 +0000914 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000915 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000916 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000917 if (MI->getNumOperands() == 5) {
918 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000919 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000920 }
921 O << "\n";
922 return;
923 }
924
Chris Lattnerf9f60882002-11-18 06:56:51 +0000925 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000926 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000927 }
Chris Lattner72614082002-10-25 22:55:53 +0000928}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000929
Chris Lattner93c1afa2003-08-11 19:35:26 +0000930bool Printer::doInitialization(Module &M) {
931 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000932 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000933 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
934 // instruction as a reference to the register named sp, and if you try to
935 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
936 // before being looked up in the symbol table. This creates spurious
937 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
938 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000939 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000940 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000941 return false; // success
942}
943
Chris Lattnerad200712003-09-09 16:23:36 +0000944// SwitchSection - Switch to the specified section of the executable if we are
945// not already in it!
946//
947static void SwitchSection(std::ostream &OS, std::string &CurSection,
948 const char *NewSection) {
949 if (CurSection != NewSection) {
950 CurSection = NewSection;
951 if (!CurSection.empty())
952 OS << "\t" << NewSection << "\n";
953 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000954}
955
Chris Lattnerad200712003-09-09 16:23:36 +0000956bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000957 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000958 std::string CurSection;
959
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000960 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000961 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
962 if (I->hasInitializer()) { // External global require no code
963 O << "\n\n";
964 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000965 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000966 unsigned Size = TD.getTypeSize(C->getType());
967 unsigned Align = TD.getTypeAlignment(C->getType());
968
969 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000970 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
971 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000972 SwitchSection(O, CurSection, ".data");
973 if (I->hasInternalLinkage())
974 O << "\t.local " << name << "\n";
975
976 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000977 << "," << (unsigned)TD.getTypeAlignment(C->getType());
978 O << "\t\t# ";
979 WriteAsOperand(O, I, true, true, &M);
980 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000981 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000982 switch (I->getLinkage()) {
983 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000984 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000985 // Nonnull linkonce -> weak
986 O << "\t.weak " << name << "\n";
987 SwitchSection(O, CurSection, "");
988 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
989 break;
990
991 case GlobalValue::AppendingLinkage:
992 // FIXME: appending linkage variables should go into a section of
993 // their name or something. For now, just emit them as external.
994 case GlobalValue::ExternalLinkage:
995 // If external or appending, declare as a global symbol
996 O << "\t.globl " << name << "\n";
997 // FALL THROUGH
998 case GlobalValue::InternalLinkage:
999 if (C->isNullValue())
1000 SwitchSection(O, CurSection, ".bss");
1001 else
1002 SwitchSection(O, CurSection, ".data");
1003 break;
1004 }
1005
1006 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001007 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001008 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001009 O << name << ":\t\t\t\t# ";
1010 WriteAsOperand(O, I, true, true, &M);
1011 O << " = ";
1012 WriteAsOperand(O, C, false, false, &M);
1013 O << "\n";
1014 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001015 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001016 }
Chris Lattnerad200712003-09-09 16:23:36 +00001017
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001018 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001019 return false; // success
1020}