blob: baa87a04932e5c5f3fe709e9ed702161e3b815b8 [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);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000085 };
Brian Gaeked7908f62003-06-27 00:00:48 +000086} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000087
Brian Gaeke92bdfe62003-07-23 18:37:06 +000088/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000089/// assembly code for a MachineFunction to the given output stream,
90/// using the given target machine description. This should work
91/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000092///
Brian Gaeke9d99b432003-08-13 18:15:15 +000093FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000094 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000095}
96
Brian Gaeke92bdfe62003-07-23 18:37:06 +000097/// valToExprString - Helper function for ConstantExprToString().
98/// Appends result to argument string S.
99///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000100std::string Printer::valToExprString(const Value* V) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000101 std::string S;
102 bool failed = false;
103 if (const Constant* CV = dyn_cast<Constant>(V)) { // symbolic or known
104 if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV))
105 S += std::string(CB == ConstantBool::True ? "1" : "0");
106 else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
107 S += itostr(CI->getValue());
108 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
109 S += utostr(CI->getValue());
110 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
111 S += ftostr(CFP->getValue());
112 else if (isa<ConstantPointerNull>(CV))
113 S += "0";
114 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
115 S += valToExprString(CPR->getValue());
116 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV))
117 S += ConstantExprToString(CE);
118 else
119 failed = true;
120 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(V)) {
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000121 S += Mang->getValueName(GV);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000122 }
123 else
124 failed = true;
125
126 if (failed) {
127 assert(0 && "Cannot convert value to string");
128 S += "<illegal-value>";
129 }
130 return S;
131}
132
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000133/// ConstantExprToString - Convert a ConstantExpr to an asm expression
134/// and return this as a string.
135///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000136std::string Printer::ConstantExprToString(const ConstantExpr* CE) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000137 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000138 switch(CE->getOpcode()) {
139 case Instruction::GetElementPtr:
140 { // generate a symbolic expression for the byte address
141 const Value* ptrVal = CE->getOperand(0);
142 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
Chris Lattner72feb152003-08-04 01:04:59 +0000143 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec))
144 return "(" + valToExprString(ptrVal) + ") + " + utostr(Offset);
145 else
146 return valToExprString(ptrVal);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147 }
148
149 case Instruction::Cast:
Brian Gaekeb3011a02003-07-24 17:30:45 +0000150 // Support only non-converting or widening casts for now, that is,
151 // ones that do not involve a change in value. This assertion is
152 // not a complete check.
153 {
154 Constant *Op = CE->getOperand(0);
155 const Type *OpTy = Op->getType(), *Ty = CE->getType();
156 assert(((isa<PointerType>(OpTy)
157 && (Ty == Type::LongTy || Ty == Type::ULongTy))
158 || (isa<PointerType>(Ty)
159 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
160 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
Chris Lattner72feb152003-08-04 01:04:59 +0000161 && (OpTy->isLosslesslyConvertibleTo(Ty))))
Brian Gaekeb3011a02003-07-24 17:30:45 +0000162 && "FIXME: Don't yet support this kind of constant cast expr");
Chris Lattner72feb152003-08-04 01:04:59 +0000163 return "(" + valToExprString(Op) + ")";
Brian Gaekeb3011a02003-07-24 17:30:45 +0000164 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165
166 case Instruction::Add:
Chris Lattner72feb152003-08-04 01:04:59 +0000167 return "(" + valToExprString(CE->getOperand(0)) + ") + ("
168 + valToExprString(CE->getOperand(1)) + ")";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169
170 default:
171 assert(0 && "Unsupported operator in ConstantExprToString()");
Chris Lattner72feb152003-08-04 01:04:59 +0000172 return "";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000173 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174}
175
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000176/// isStringCompatible - Can we treat the specified array as a string?
177/// Only if it is an array of ubytes or non-negative sbytes.
178///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179static bool isStringCompatible(const ConstantArray *CVA) {
180 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
181 if (ETy == Type::UByteTy) return true;
182 if (ETy != Type::SByteTy) return false;
183
184 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
185 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
186 return false;
187
188 return true;
189}
190
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000191/// toOctal - Convert the low order bits of X into an octal digit.
192///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000193static inline char toOctal(int X) {
194 return (X&7)+'0';
195}
196
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000197/// getAsCString - Return the specified array as a C compatible
198/// string, only if the predicate isStringCompatible is true.
199///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000200static std::string getAsCString(const ConstantArray *CVA) {
201 assert(isStringCompatible(CVA) && "Array is not string compatible!");
202
203 std::string Result;
204 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
205 Result = "\"";
206 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000207 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000208
209 if (C == '"') {
210 Result += "\\\"";
211 } else if (C == '\\') {
212 Result += "\\\\";
213 } else if (isprint(C)) {
214 Result += C;
215 } else {
216 switch(C) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000217 case '\b': Result += "\\b"; break;
218 case '\f': Result += "\\f"; break;
219 case '\n': Result += "\\n"; break;
220 case '\r': Result += "\\r"; break;
221 case '\t': Result += "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000222 default:
223 Result += '\\';
224 Result += toOctal(C >> 6);
225 Result += toOctal(C >> 3);
226 Result += toOctal(C >> 0);
227 break;
228 }
229 }
230 }
231 Result += "\"";
232 return Result;
233}
234
235// Print a constant value or values (it may be an aggregate).
Chris Lattnerad200712003-09-09 16:23:36 +0000236void Printer::printConstantValueOnly(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000237 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000238
Chris Lattnerad200712003-09-09 16:23:36 +0000239 if (CV->isNullValue()) {
240 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000241 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000242 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
243 if (isStringCompatible(CVA)) {
244 // print the string alone and return
Chris Lattnerb1698412003-10-19 02:51:01 +0000245 O << "\t.ascii\t" << getAsCString(CVA) << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000246 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000247 const std::vector<Use> &constValues = CVA->getValues();
248 for (unsigned i=0; i < constValues.size(); i++)
249 printConstantValueOnly(cast<Constant>(constValues[i].get()));
250 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000251 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000252 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
253 // Print the fields in successive locations. Pad to align if needed!
254 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
255 const std::vector<Use>& constValues = CVS->getValues();
256 unsigned sizeSoFar = 0;
257 for (unsigned i=0, N = constValues.size(); i < N; i++) {
258 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000259
Chris Lattnerad200712003-09-09 16:23:36 +0000260 // Check if padding is needed and insert one or more 0s.
261 unsigned fieldSize = TD.getTypeSize(field->getType());
262 unsigned padSize = ((i == N-1? cvsLayout->StructSize
263 : cvsLayout->MemberOffsets[i+1])
264 - cvsLayout->MemberOffsets[i]) - fieldSize;
265 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000266
Chris Lattnerad200712003-09-09 16:23:36 +0000267 // Now print the actual field value
268 printConstantValueOnly(field);
269
270 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000271 if (padSize)
272 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000273 }
Chris Lattnerad200712003-09-09 16:23:36 +0000274 assert(sizeSoFar == cvsLayout->StructSize &&
275 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000276 return;
277 }
278
279 const Type *type = CV->getType();
280 O << "\t";
281 switch (type->getPrimitiveID()) {
282 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
283 O << ".byte";
284 break;
285 case Type::UShortTyID: case Type::ShortTyID:
286 O << ".word";
287 break;
288 case Type::FloatTyID: case Type::PointerTyID:
289 case Type::UIntTyID: case Type::IntTyID:
290 O << ".long";
291 break;
292 case Type::DoubleTyID:
293 case Type::ULongTyID: case Type::LongTyID:
294 O << ".quad";
295 break;
296 default:
297 assert (0 && "Can't handle printing this type of thing");
298 break;
299 }
300 O << "\t";
301
302 if (const ConstantExpr* CE = dyn_cast<ConstantExpr>(CV)) {
303 // Constant expression built from operators, constants, and
304 // symbolic addrs
305 O << ConstantExprToString(CE) << "\n";
306 } else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
307 O << (CB->getValue() ? "1\n" : "0\n");
308 } else if (type->isFloatingPoint()) {
309 // FP Constants are printed as integer constants to avoid losing
310 // precision...
311 double Val = cast<ConstantFP>(CV)->getValue();
312 if (type == Type::FloatTy) {
313 union FU { // Abide by C TBAA rules
314 float FVal;
315 unsigned UVal;
316 } U;
317 U.FVal = Val;
318 O << U.UVal;
319 } else if (type == Type::DoubleTy) {
320 union DU { // Abide by C TBAA rules
321 double FVal;
322 uint64_t UVal;
323 } U;
324 U.FVal = Val;
325 O << U.UVal;
326 } else {
327 assert(0 && "Unknown floating point type!");
328 }
329
330 O << "\t# " << *type << " value: " << Val << "\n";
331 } else if (type->isPrimitiveType()) {
332 WriteAsOperand(O, CV, false, false) << "\n";
333 } else if (const ConstantPointerRef* CPR = dyn_cast<ConstantPointerRef>(CV)) {
334 // This is a constant address for a global variable or method.
335 // Use the name of the variable or method as the address value.
336 O << Mang->getValueName(CPR->getValue()) << "\n";
337 } else {
338 assert(0 && "Unknown elementary type for constant");
339 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000340}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000341
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000342/// printConstantPool - Print to the current output stream assembly
343/// representations of the constants in the constant pool MCP. This is
344/// used to print out constants which have been "spilled to memory" by
345/// the code generator.
346///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000347void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000348 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000349 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000350
Chris Lattnerb7089442003-01-13 00:35:03 +0000351 if (CP.empty()) return;
352
353 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
354 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000355 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000356 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000357 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
358 << *CP[i] << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000359 printConstantValueOnly (CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000360 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000361}
362
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000363/// runOnMachineFunction - This uses the printMachineInstruction()
364/// method to print assembly for each instruction.
365///
Chris Lattner0285a332002-12-28 20:25:38 +0000366bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000367 // BBNumber is used here so that a given Printer will never give two
368 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000369 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000370
Chris Lattnere0121322003-08-03 23:37:09 +0000371 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000372 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000373 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000374
Chris Lattnerb7089442003-01-13 00:35:03 +0000375 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000376 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000377
Brian Gaeke6559bb92002-11-14 22:32:30 +0000378 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 O << "\t.text\n";
380 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000381 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000382 if (!EmitCygwin)
383 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000384 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000385
Brian Gaeked7908f62003-06-27 00:00:48 +0000386 // Number each basic block so that we can consistently refer to them
387 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000388 NumberForBB.clear();
389 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
390 I != E; ++I) {
391 NumberForBB[I->getBasicBlock()] = BBNumber++;
392 }
393
Brian Gaeke6559bb92002-11-14 22:32:30 +0000394 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000395 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
396 I != E; ++I) {
397 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000398 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000399 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000400 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
401 II != E; ++II) {
402 // Print the assembly for the instruction.
403 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000404 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405 }
Chris Lattner0285a332002-12-28 20:25:38 +0000406 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000407
408 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000409 return false;
410}
411
Chris Lattner3d3067b2002-11-21 20:44:15 +0000412static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000413 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
415 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000416}
417
418static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000419 if (MI->getOperand(Op).isFrameIndex()) return true;
420 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000421 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000422 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
423 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000424}
425
Brian Gaeke2a098772003-08-11 19:05:46 +0000426
427
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000428void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000429 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000430 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000431 switch (MO.getType()) {
432 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000433 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000434 O << "<" << V->getName() << ">";
435 return;
436 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000438 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000439 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000440 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000441 O << "%" << RI.get(MO.getReg()).Name;
442 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000443 O << "%reg" << MO.getReg();
444 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000445
446 case MachineOperand::MO_SignExtendedImmed:
447 case MachineOperand::MO_UnextendedImmed:
448 O << (int)MO.getImmedValue();
449 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000450 case MachineOperand::MO_PCRelativeDisp: {
451 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
452 assert (i != NumberForBB.end()
453 && "Could not find a BB in the NumberForBB map!");
454 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000455 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000456 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000457 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000458 if (!elideOffsetKeyword)
459 O << "OFFSET ";
460 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 return;
462 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000463 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000464 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000465 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000466 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000467 }
468}
469
Chris Lattner3501fea2003-01-14 22:00:31 +0000470static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000471 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000472 default: assert(0 && "Unknown arg size!");
473 case X86II::Arg8: return "BYTE PTR";
474 case X86II::Arg16: return "WORD PTR";
475 case X86II::Arg32: return "DWORD PTR";
476 case X86II::Arg64: return "QWORD PTR";
477 case X86II::ArgF32: return "DWORD PTR";
478 case X86II::ArgF64: return "QWORD PTR";
479 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000480 }
481}
482
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000483void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000484 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000485
486 if (MI->getOperand(Op).isFrameIndex()) {
487 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
488 if (MI->getOperand(Op+3).getImmedValue())
489 O << " + " << MI->getOperand(Op+3).getImmedValue();
490 O << "]";
491 return;
492 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000493 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000494 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000495 if (MI->getOperand(Op+3).getImmedValue())
496 O << " + " << MI->getOperand(Op+3).getImmedValue();
497 O << "]";
498 return;
499 }
500
Chris Lattner3d3067b2002-11-21 20:44:15 +0000501 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000502 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000504 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505
506 O << "[";
507 bool NeedPlus = false;
508 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000509 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000510 NeedPlus = true;
511 }
512
513 if (IndexReg.getReg()) {
514 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000515 if (ScaleVal != 1)
516 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000517 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000518 NeedPlus = true;
519 }
520
Chris Lattner0285a332002-12-28 20:25:38 +0000521 if (DispVal) {
522 if (NeedPlus)
523 if (DispVal > 0)
524 O << " + ";
525 else {
526 O << " - ";
527 DispVal = -DispVal;
528 }
529 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000530 }
531 O << "]";
532}
533
Brian Gaeke2a098772003-08-11 19:05:46 +0000534/// checkImplUses - Emit the implicit-use registers for the
535/// instruction described by DESC, if its PrintImplUses flag is set.
536///
537void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
538 const MRegisterInfo &RI = *TM.getRegisterInfo();
539 if (Desc.TSFlags & X86II::PrintImplUses) {
540 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
541 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000542 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000543 }
544 }
545}
546
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000547/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000548/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000549///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000550void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000551 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000552 const TargetInstrInfo &TII = TM.getInstrInfo();
553 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000554
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000555 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000556 switch (Desc.TSFlags & X86II::FormMask) {
557 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000558 // Print pseudo-instructions as comments; either they should have been
559 // turned into real instructions by now, or they don't need to be
560 // seen by the assembler (e.g., IMPLICIT_USEs.)
561 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000562 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000563 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000564 O << " = phi ";
565 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
566 if (i != 1) O << ", ";
567 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000568 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000569 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000570 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000571 O << "]";
572 }
573 } else {
574 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000575 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
576 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000577 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000578 O << " = ";
579 ++i;
580 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000581 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000582
583 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
584 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000585 if (MI->getOperand(i).opIsDefOnly() ||
586 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000587 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000588 if (MI->getOperand(i).opIsDefOnly() ||
589 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000590 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000591 }
592 O << "\n";
593 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000594
Chris Lattnerf9f60882002-11-18 06:56:51 +0000595 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000596 // The accepted forms of Raw instructions are:
597 // 1. nop - No operand required
598 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000599 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000600 //
601 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000602 (MI->getNumOperands() == 1 &&
603 (MI->getOperand(0).isPCRelativeDisp() ||
604 MI->getOperand(0).isGlobalAddress() ||
605 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000606 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000607 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000608
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000609 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000610 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000611 }
612 O << "\n";
613 return;
614
Chris Lattner77875d82002-11-21 02:00:20 +0000615 case X86II::AddRegFrm: {
616 // There are currently two forms of acceptable AddRegFrm instructions.
617 // Either the instruction JUST takes a single register (like inc, dec, etc),
618 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000619 // (move immediate f.e.). Note that this immediate value might be stored as
620 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000621 // into a register. The initial register might be duplicated if this is a
622 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000623 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000624 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000625 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000626 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000627 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000628 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000629 MI->getOperand(1).isRegister() ||
630 MI->getOperand(1).isGlobalAddress() ||
631 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000632 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000633
Chris Lattner77875d82002-11-21 02:00:20 +0000634 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000635
Brian Gaeked7908f62003-06-27 00:00:48 +0000636 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000637 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000638 if (MI->getNumOperands() == 2 &&
639 (!MI->getOperand(1).isRegister() ||
640 MI->getOperand(1).getVRegValueOrNull() ||
641 MI->getOperand(1).isGlobalAddress() ||
642 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000643 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000644 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000645 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000646 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000647 O << "\n";
648 return;
649 }
Chris Lattner233ad712002-11-21 01:33:44 +0000650 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000651 // There are two acceptable forms of MRMDestReg instructions, those with 2,
652 // 3 and 4 operands:
653 //
654 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000655 //
656 // 3 Operands: in this form, the first two registers (the destination, and
657 // the first operand) should be the same, post register allocation. The 3rd
658 // operand is an additional input. This should be for things like add
659 // instructions.
660 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000661 // 4 Operands: This form is for instructions which are 3 operands forms, but
662 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000663 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000664 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000665 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000666 (MI->getNumOperands() == 2 ||
667 (isTwoAddr && MI->getOperand(1).isRegister() &&
668 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
669 (MI->getNumOperands() == 3 ||
670 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000671 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000672
Brian Gaeked7908f62003-06-27 00:00:48 +0000673 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000674 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000675 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000676 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 if (MI->getNumOperands() == 4) {
678 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000679 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000680 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000681 O << "\n";
682 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000683 }
Chris Lattner18042332002-11-21 21:03:39 +0000684
685 case X86II::MRMDestMem: {
686 // These instructions are the same as MRMDestReg, but instead of having a
687 // register reference for the mod/rm field, it's a memory reference.
688 //
689 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000690 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000691
Brian Gaeked7908f62003-06-27 00:00:48 +0000692 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000693 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000694 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000695 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000696 O << "\n";
697 return;
698 }
699
Chris Lattner233ad712002-11-21 01:33:44 +0000700 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000701 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000702 // those with 3 and 2 operands:
703 //
704 // 3 Operands: in this form, the last register (the second input) is the
705 // ModR/M input. The first two operands should be the same, post register
706 // allocation. This is for things like: add r32, r/m32
707 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000708 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
709 // instructions like the IMULri instructions.
710 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000711 // 2 Operands: this is for things like mov that do not read a second input
712 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000713 assert(MI->getOperand(0).isRegister() &&
714 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000715 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000716 (MI->getNumOperands() == 3 &&
717 (MI->getOperand(2).isRegister() ||
718 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000719 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000720 if (MI->getNumOperands() == 3 &&
721 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
722 O << "**";
723
Brian Gaeked7908f62003-06-27 00:00:48 +0000724 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000725 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000726
727 // If this is IMULri* instructions, print the non-two-address operand.
728 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
729 O << ", ";
730 printOp(MI->getOperand(1));
731 }
732
Chris Lattner644e1ab2002-11-21 00:30:01 +0000733 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000734 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000735 O << "\n";
736 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000737 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000738
Chris Lattner3d3067b2002-11-21 20:44:15 +0000739 case X86II::MRMSrcMem: {
740 // These instructions are the same as MRMSrcReg, but instead of having a
741 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000742 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000743 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000744 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000745 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000746 isMem(MI, 2))
747 && "Bad format for MRMDestReg!");
748 if (MI->getNumOperands() == 2+4 &&
749 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
750 O << "**";
751
Brian Gaeked7908f62003-06-27 00:00:48 +0000752 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000753 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000754 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000755 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000756 O << "\n";
757 return;
758 }
759
Chris Lattner675dd2c2002-11-21 17:09:01 +0000760 case X86II::MRMS0r: case X86II::MRMS1r:
761 case X86II::MRMS2r: case X86II::MRMS3r:
762 case X86II::MRMS4r: case X86II::MRMS5r:
763 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000764 // In this form, the following are valid formats:
765 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000766 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000767 // 2. shl rdest, rinput <implicit CL or 1>
768 // 3. sbb rdest, rinput, immediate [rdest = rinput]
769 //
770 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000771 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000772 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000773 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000774 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000775 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000776 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000777 "Bad MRMSxR format!");
778
Chris Lattnerd9096832002-12-15 08:01:39 +0000779 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000780 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 Lattnerd9096832002-12-15 08:01:39 +0000785 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000786 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000787 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000788 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000789 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 O << "\n";
791
792 return;
793 }
794
Chris Lattnerb7089442003-01-13 00:35:03 +0000795 case X86II::MRMS0m: case X86II::MRMS1m:
796 case X86II::MRMS2m: case X86II::MRMS3m:
797 case X86II::MRMS4m: case X86II::MRMS5m:
798 case X86II::MRMS6m: case X86II::MRMS7m: {
799 // In this form, the following are valid formats:
800 // 1. sete [m]
801 // 2. cmp [m], immediate
802 // 2. shl [m], rinput <implicit CL or 1>
803 // 3. sbb [m], immediate
804 //
805 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
806 isMem(MI, 0) && "Bad MRMSxM format!");
807 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
808 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000809 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
810 // is misassembled by gas in intel_syntax mode as its 32-bit
811 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
812 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000813 if (MI->getOpCode() == X86::FSTPr80) {
814 if ((MI->getOperand(0).getReg() == X86::ESP)
815 && (MI->getOperand(1).getImmedValue() == 1)) {
816 int DispVal = MI->getOperand(3).getImmedValue();
817 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
818 unsigned int val = (unsigned int) DispVal;
819 O << ".byte 0xdb, 0xbc, 0x24\n\t";
820 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
821 } else { // 1 byte disp.
822 unsigned char val = (unsigned char) DispVal;
823 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
824 << std::dec << "\t# ";
825 }
826 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000827 }
828 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
829 // misassembled by gas in intel_syntax mode as its 32-bit
830 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
831 // opcode bytes instead of the instruction.
832 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000833 if ((MI->getOperand(0).getReg() == X86::ESP)
834 && (MI->getOperand(1).getImmedValue() == 1)) {
835 int DispVal = MI->getOperand(3).getImmedValue();
836 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
837 unsigned int val = (unsigned int) DispVal;
838 O << ".byte 0xdb, 0xac, 0x24\n\t";
839 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
840 } else { // 1 byte disp.
841 unsigned char val = (unsigned char) DispVal;
842 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
843 << std::dec << "\t# ";
844 }
845 }
846 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000847 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
848 // invalid opcode, saying "64 bit operations are only supported in
849 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
850 // [...]", which is wrong. Workaround: Output the raw opcode bytes
851 // instead of the instruction.
852 if (MI->getOpCode() == X86::FILDr64) {
853 if ((MI->getOperand(0).getReg() == X86::ESP)
854 && (MI->getOperand(1).getImmedValue() == 1)) {
855 int DispVal = MI->getOperand(3).getImmedValue();
856 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
857 unsigned int val = (unsigned int) DispVal;
858 O << ".byte 0xdf, 0xac, 0x24\n\t";
859 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
860 } else { // 1 byte disp.
861 unsigned char val = (unsigned char) DispVal;
862 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
863 << std::dec << "\t# ";
864 }
865 }
866 }
867 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
868 // an invalid opcode, saying "64 bit operations are only
869 // supported in 64 bit modes." libopcodes disassembles it as
870 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
871 // "fistpll DWORD PTR " instead, which is what libopcodes is
872 // expecting to see.
873 if (MI->getOpCode() == X86::FISTPr64) {
874 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000875 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000876 if (MI->getNumOperands() == 5) {
877 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000878 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000879 }
880 O << "\t# ";
881 }
882
Brian Gaeked7908f62003-06-27 00:00:48 +0000883 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000884 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000885 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000886 if (MI->getNumOperands() == 5) {
887 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000888 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000889 }
890 O << "\n";
891 return;
892 }
893
Chris Lattnerf9f60882002-11-18 06:56:51 +0000894 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000895 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000896 }
Chris Lattner72614082002-10-25 22:55:53 +0000897}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000898
Chris Lattner93c1afa2003-08-11 19:35:26 +0000899bool Printer::doInitialization(Module &M) {
900 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000901 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000902 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
903 // instruction as a reference to the register named sp, and if you try to
904 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
905 // before being looked up in the symbol table. This creates spurious
906 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
907 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000908 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000909 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000910 return false; // success
911}
912
Chris Lattnerad200712003-09-09 16:23:36 +0000913// SwitchSection - Switch to the specified section of the executable if we are
914// not already in it!
915//
916static void SwitchSection(std::ostream &OS, std::string &CurSection,
917 const char *NewSection) {
918 if (CurSection != NewSection) {
919 CurSection = NewSection;
920 if (!CurSection.empty())
921 OS << "\t" << NewSection << "\n";
922 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000923}
924
Chris Lattnerad200712003-09-09 16:23:36 +0000925bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000926 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000927 std::string CurSection;
928
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000929 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000930 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
931 if (I->hasInitializer()) { // External global require no code
932 O << "\n\n";
933 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000934 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000935 unsigned Size = TD.getTypeSize(C->getType());
936 unsigned Align = TD.getTypeAlignment(C->getType());
937
938 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000939 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
940 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000941 SwitchSection(O, CurSection, ".data");
942 if (I->hasInternalLinkage())
943 O << "\t.local " << name << "\n";
944
945 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000946 << "," << (unsigned)TD.getTypeAlignment(C->getType());
947 O << "\t\t# ";
948 WriteAsOperand(O, I, true, true, &M);
949 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000950 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000951 switch (I->getLinkage()) {
952 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000953 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000954 // Nonnull linkonce -> weak
955 O << "\t.weak " << name << "\n";
956 SwitchSection(O, CurSection, "");
957 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
958 break;
959
960 case GlobalValue::AppendingLinkage:
961 // FIXME: appending linkage variables should go into a section of
962 // their name or something. For now, just emit them as external.
963 case GlobalValue::ExternalLinkage:
964 // If external or appending, declare as a global symbol
965 O << "\t.globl " << name << "\n";
966 // FALL THROUGH
967 case GlobalValue::InternalLinkage:
968 if (C->isNullValue())
969 SwitchSection(O, CurSection, ".bss");
970 else
971 SwitchSection(O, CurSection, ".data");
972 break;
973 }
974
975 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000976 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000977 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000978 O << name << ":\t\t\t\t# ";
979 WriteAsOperand(O, I, true, true, &M);
980 O << " = ";
981 WriteAsOperand(O, C, false, false, &M);
982 O << "\n";
983 printConstantValueOnly(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000984 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000985 }
Chris Lattnerad200712003-09-09 16:23:36 +0000986
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000987 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000988 return false; // success
989}