blob: 4d586c01f5cbf8ccf28bac11fb2805dc2dfed9b1 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000034namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000035 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36
Chris Lattner93c1afa2003-08-11 19:35:26 +000037 // FIXME: This should be automatically picked up by autoconf from the C
38 // frontend
39 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
40 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
41
Chris Lattner0285a332002-12-28 20:25:38 +000042 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000043 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000044 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000045 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000046
47 /// Target machine description which we query for reg. names, data
48 /// layout, etc.
49 ///
50 TargetMachine &TM;
51
Brian Gaeked9fb37a2003-07-24 20:20:44 +000052 /// Name-mangler for global names.
53 ///
54 Mangler *Mang;
55
Brian Gaekede420ae2003-07-23 20:25:08 +000056 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000057
58 /// We name each basic block in a Function with a unique number, so
59 /// that we can consistently refer to them later. This is cleared
60 /// at the beginning of each call to runOnMachineFunction().
61 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000062 typedef std::map<const Value *, unsigned> ValueMapTy;
63 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000064
65 /// Cache of mangled name for current function. This is
66 /// recalculated at the beginning of each call to
67 /// runOnMachineFunction().
68 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000069 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000070
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000071 virtual const char *getPassName() const {
72 return "X86 Assembly Printer";
73 }
74
Brian Gaeke2a098772003-08-11 19:05:46 +000075 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000076 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000077 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000078 bool elideOffsetKeyword = false);
79 void printMemReference(const MachineInstr *MI, unsigned Op);
80 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000081 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +000082 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +000084 void emitGlobalConstant(const Constant* CV);
85 void emitConstantValueOnly(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/// isStringCompatible - Can we treat the specified array as a string?
99/// Only if it is an array of ubytes or non-negative sbytes.
100///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000101static bool isStringCompatible(const ConstantArray *CVA) {
102 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
103 if (ETy == Type::UByteTy) return true;
104 if (ETy != Type::SByteTy) return false;
105
106 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
107 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
108 return false;
109
110 return true;
111}
112
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000113/// toOctal - Convert the low order bits of X into an octal digit.
114///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000115static inline char toOctal(int X) {
116 return (X&7)+'0';
117}
118
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000119/// getAsCString - Return the specified array as a C compatible
120/// string, only if the predicate isStringCompatible is true.
121///
Chris Lattnerac662d12003-11-03 20:19:49 +0000122static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 assert(isStringCompatible(CVA) && "Array is not string compatible!");
124
Chris Lattnerac662d12003-11-03 20:19:49 +0000125 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000127 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000128
129 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000130 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000132 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000133 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000134 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000135 } else {
136 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000137 case '\b': O << "\\b"; break;
138 case '\f': O << "\\f"; break;
139 case '\n': O << "\\n"; break;
140 case '\r': O << "\\r"; break;
141 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000142 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000143 O << '\\';
144 O << toOctal(C >> 6);
145 O << toOctal(C >> 3);
146 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147 break;
148 }
149 }
150 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000151 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152}
153
Chris Lattnerac662d12003-11-03 20:19:49 +0000154// Print out the specified constant, without a storage class. Only the
155// constants valid in constant expressions can occur here.
156void Printer::emitConstantValueOnly(const Constant *CV) {
157 if (CV->isNullValue())
158 O << "0";
159 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
160 assert(CB == ConstantBool::True);
161 O << "1";
162 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
163 O << CI->getValue();
164 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
165 O << CI->getValue();
166 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
167 // This is a constant address for a global variable or function. Use the
168 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000169 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000170 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
171 const TargetData &TD = TM.getTargetData();
172 switch(CE->getOpcode()) {
173 case Instruction::GetElementPtr: {
174 // generate a symbolic expression for the byte address
175 const Constant *ptrVal = CE->getOperand(0);
176 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
177 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
178 O << "(";
179 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000180 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000181 } else {
182 emitConstantValueOnly(ptrVal);
183 }
184 break;
185 }
186 case Instruction::Cast: {
187 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000188 // that do not involve a change in value. This assertion is really gross,
189 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000190 Constant *Op = CE->getOperand(0);
191 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000192
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000193 // Remember, kids, pointers on x86 can be losslessly converted back and
194 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000195 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000196 && (Ty == Type::LongTy || Ty == Type::ULongTy
197 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000198 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000199 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
200 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000201 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
202 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000203 && "FIXME: Don't yet support this kind of constant cast expr");
204 O << "(";
205 emitConstantValueOnly(Op);
206 O << ")";
207 break;
208 }
209 case Instruction::Add:
210 O << "(";
211 emitConstantValueOnly(CE->getOperand(0));
212 O << ") + (";
213 emitConstantValueOnly(CE->getOperand(1));
214 O << ")";
215 break;
216 default:
217 assert(0 && "Unsupported operator!");
218 }
219 } else {
220 assert(0 && "Unknown constant value!");
221 }
222}
223
224// Print a constant value or values, with the appropriate storage class as a
225// prefix.
226void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000227 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000228
Chris Lattnerad200712003-09-09 16:23:36 +0000229 if (CV->isNullValue()) {
230 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000231 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000232 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
233 if (isStringCompatible(CVA)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000234 O << "\t.ascii\t";
235 printAsCString(O, CVA);
236 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000237 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000238 const std::vector<Use> &constValues = CVA->getValues();
239 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000240 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000241 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000242 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000243 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
244 // Print the fields in successive locations. Pad to align if needed!
245 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
246 const std::vector<Use>& constValues = CVS->getValues();
247 unsigned sizeSoFar = 0;
248 for (unsigned i=0, N = constValues.size(); i < N; i++) {
249 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000250
Chris Lattnerad200712003-09-09 16:23:36 +0000251 // Check if padding is needed and insert one or more 0s.
252 unsigned fieldSize = TD.getTypeSize(field->getType());
253 unsigned padSize = ((i == N-1? cvsLayout->StructSize
254 : cvsLayout->MemberOffsets[i+1])
255 - cvsLayout->MemberOffsets[i]) - fieldSize;
256 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000257
Chris Lattnerad200712003-09-09 16:23:36 +0000258 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000259 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000260
261 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000262 if (padSize)
263 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264 }
Chris Lattnerad200712003-09-09 16:23:36 +0000265 assert(sizeSoFar == cvsLayout->StructSize &&
266 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000267 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000268 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
269 // FP Constants are printed as integer constants to avoid losing
270 // precision...
271 double Val = CFP->getValue();
272 switch (CFP->getType()->getPrimitiveID()) {
273 default: assert(0 && "Unknown floating point type!");
274 case Type::FloatTyID: {
275 union FU { // Abide by C TBAA rules
276 float FVal;
277 unsigned UVal;
278 } U;
279 U.FVal = Val;
280 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
281 return;
282 }
283 case Type::DoubleTyID: {
284 union DU { // Abide by C TBAA rules
285 double FVal;
286 uint64_t UVal;
287 } U;
288 U.FVal = Val;
289 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
290 return;
291 }
292 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000293 }
294
295 const Type *type = CV->getType();
296 O << "\t";
297 switch (type->getPrimitiveID()) {
298 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
299 O << ".byte";
300 break;
301 case Type::UShortTyID: case Type::ShortTyID:
302 O << ".word";
303 break;
304 case Type::FloatTyID: case Type::PointerTyID:
305 case Type::UIntTyID: case Type::IntTyID:
306 O << ".long";
307 break;
308 case Type::DoubleTyID:
309 case Type::ULongTyID: case Type::LongTyID:
310 O << ".quad";
311 break;
312 default:
313 assert (0 && "Can't handle printing this type of thing");
314 break;
315 }
316 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000317 emitConstantValueOnly(CV);
318 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000319}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000320
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000321/// printConstantPool - Print to the current output stream assembly
322/// representations of the constants in the constant pool MCP. This is
323/// used to print out constants which have been "spilled to memory" by
324/// the code generator.
325///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000326void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000327 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000328 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000329
Chris Lattnerb7089442003-01-13 00:35:03 +0000330 if (CP.empty()) return;
331
332 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
333 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000334 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000335 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000336 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
337 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000338 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000339 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000340}
341
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000342/// runOnMachineFunction - This uses the printMachineInstruction()
343/// method to print assembly for each instruction.
344///
Chris Lattner0285a332002-12-28 20:25:38 +0000345bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000346 // BBNumber is used here so that a given Printer will never give two
347 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000348 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000349
Chris Lattnere0121322003-08-03 23:37:09 +0000350 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000351 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000352 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000353
Chris Lattnerb7089442003-01-13 00:35:03 +0000354 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000355 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000356
Brian Gaeke6559bb92002-11-14 22:32:30 +0000357 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000358 O << "\t.text\n";
359 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000360 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000361 if (!EmitCygwin)
362 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000363 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000364
Brian Gaeked7908f62003-06-27 00:00:48 +0000365 // Number each basic block so that we can consistently refer to them
366 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000367 NumberForBB.clear();
368 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
369 I != E; ++I) {
370 NumberForBB[I->getBasicBlock()] = BBNumber++;
371 }
372
Brian Gaeke6559bb92002-11-14 22:32:30 +0000373 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000374 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
375 I != E; ++I) {
376 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000377 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000378 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000379 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
380 II != E; ++II) {
381 // Print the assembly for the instruction.
382 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000383 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000384 }
Chris Lattner0285a332002-12-28 20:25:38 +0000385 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000386
387 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000388 return false;
389}
390
Chris Lattner3d3067b2002-11-21 20:44:15 +0000391static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000392 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000393 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
394 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000395}
396
397static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000398 if (MI->getOperand(Op).isFrameIndex()) return true;
399 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000400 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000401 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
402 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000403}
404
Brian Gaeke2a098772003-08-11 19:05:46 +0000405
406
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000407void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000408 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000409 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000410 switch (MO.getType()) {
411 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000412 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000413 O << "<" << V->getName() << ">";
414 return;
415 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000416 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000417 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000418 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000419 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000420 O << "%" << RI.get(MO.getReg()).Name;
421 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000422 O << "%reg" << MO.getReg();
423 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000424
425 case MachineOperand::MO_SignExtendedImmed:
426 case MachineOperand::MO_UnextendedImmed:
427 O << (int)MO.getImmedValue();
428 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000429 case MachineOperand::MO_PCRelativeDisp: {
430 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
431 assert (i != NumberForBB.end()
432 && "Could not find a BB in the NumberForBB map!");
433 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000434 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000435 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000436 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000437 if (!elideOffsetKeyword)
438 O << "OFFSET ";
439 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000440 return;
441 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000442 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000443 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000444 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000445 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000446 }
447}
448
Chris Lattner3501fea2003-01-14 22:00:31 +0000449static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000450 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000451 default: assert(0 && "Unknown arg size!");
452 case X86II::Arg8: return "BYTE PTR";
453 case X86II::Arg16: return "WORD PTR";
454 case X86II::Arg32: return "DWORD PTR";
455 case X86II::Arg64: return "QWORD PTR";
456 case X86II::ArgF32: return "DWORD PTR";
457 case X86II::ArgF64: return "QWORD PTR";
458 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000459 }
460}
461
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000462void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000463 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000464
465 if (MI->getOperand(Op).isFrameIndex()) {
466 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
467 if (MI->getOperand(Op+3).getImmedValue())
468 O << " + " << MI->getOperand(Op+3).getImmedValue();
469 O << "]";
470 return;
471 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000472 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000473 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000474 if (MI->getOperand(Op+3).getImmedValue())
475 O << " + " << MI->getOperand(Op+3).getImmedValue();
476 O << "]";
477 return;
478 }
479
Chris Lattner3d3067b2002-11-21 20:44:15 +0000480 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000481 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000482 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000483 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000484
485 O << "[";
486 bool NeedPlus = false;
487 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000488 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000489 NeedPlus = true;
490 }
491
492 if (IndexReg.getReg()) {
493 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000494 if (ScaleVal != 1)
495 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000496 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000497 NeedPlus = true;
498 }
499
Chris Lattner0285a332002-12-28 20:25:38 +0000500 if (DispVal) {
501 if (NeedPlus)
502 if (DispVal > 0)
503 O << " + ";
504 else {
505 O << " - ";
506 DispVal = -DispVal;
507 }
508 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000509 }
510 O << "]";
511}
512
Brian Gaeke2a098772003-08-11 19:05:46 +0000513/// checkImplUses - Emit the implicit-use registers for the
514/// instruction described by DESC, if its PrintImplUses flag is set.
515///
516void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
517 const MRegisterInfo &RI = *TM.getRegisterInfo();
518 if (Desc.TSFlags & X86II::PrintImplUses) {
519 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
520 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000521 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000522 }
523 }
524}
525
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000526/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000527/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000528///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000529void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000530 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000531 const TargetInstrInfo &TII = TM.getInstrInfo();
532 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000533
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000534 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000535 switch (Desc.TSFlags & X86II::FormMask) {
536 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000537 // Print pseudo-instructions as comments; either they should have been
538 // turned into real instructions by now, or they don't need to be
539 // seen by the assembler (e.g., IMPLICIT_USEs.)
540 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000541 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000542 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000543 O << " = phi ";
544 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
545 if (i != 1) O << ", ";
546 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000547 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000548 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000549 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000550 O << "]";
551 }
552 } else {
553 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000554 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000555 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000556 O << " = ";
557 ++i;
558 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000559 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000560
561 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
562 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000563 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000564 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000565 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000566 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000567 }
568 O << "\n";
569 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000570
Chris Lattnerf9f60882002-11-18 06:56:51 +0000571 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000572 // The accepted forms of Raw instructions are:
573 // 1. nop - No operand required
574 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000575 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000576 //
577 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000578 (MI->getNumOperands() == 1 &&
579 (MI->getOperand(0).isPCRelativeDisp() ||
580 MI->getOperand(0).isGlobalAddress() ||
581 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000582 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000583 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000584
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000585 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000586 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000587 }
588 O << "\n";
589 return;
590
Chris Lattner77875d82002-11-21 02:00:20 +0000591 case X86II::AddRegFrm: {
592 // There are currently two forms of acceptable AddRegFrm instructions.
593 // Either the instruction JUST takes a single register (like inc, dec, etc),
594 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000595 // (move immediate f.e.). Note that this immediate value might be stored as
596 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000597 // into a register. The initial register might be duplicated if this is a
598 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000599 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000600 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000601 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000602 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000603 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000604 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000605 MI->getOperand(1).isRegister() ||
606 MI->getOperand(1).isGlobalAddress() ||
607 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000608 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000609
Chris Lattner77875d82002-11-21 02:00:20 +0000610 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000611
Brian Gaeked7908f62003-06-27 00:00:48 +0000612 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000613 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000614 if (MI->getNumOperands() == 2 &&
615 (!MI->getOperand(1).isRegister() ||
616 MI->getOperand(1).getVRegValueOrNull() ||
617 MI->getOperand(1).isGlobalAddress() ||
618 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000619 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000620 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000621 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000622 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000623 O << "\n";
624 return;
625 }
Chris Lattner233ad712002-11-21 01:33:44 +0000626 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000627 // There are two acceptable forms of MRMDestReg instructions, those with 2,
628 // 3 and 4 operands:
629 //
630 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000631 //
632 // 3 Operands: in this form, the first two registers (the destination, and
633 // the first operand) should be the same, post register allocation. The 3rd
634 // operand is an additional input. This should be for things like add
635 // instructions.
636 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000637 // 4 Operands: This form is for instructions which are 3 operands forms, but
638 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000639 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000640 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000641 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000642 (MI->getNumOperands() == 2 ||
643 (isTwoAddr && MI->getOperand(1).isRegister() &&
644 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
645 (MI->getNumOperands() == 3 ||
646 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000647 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000648
Brian Gaeked7908f62003-06-27 00:00:48 +0000649 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000650 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000651 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000652 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000653 if (MI->getNumOperands() == 4) {
654 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000655 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000656 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000657 O << "\n";
658 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000659 }
Chris Lattner18042332002-11-21 21:03:39 +0000660
661 case X86II::MRMDestMem: {
662 // These instructions are the same as MRMDestReg, but instead of having a
663 // register reference for the mod/rm field, it's a memory reference.
664 //
665 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000666 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000667
Brian Gaeked7908f62003-06-27 00:00:48 +0000668 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000669 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000670 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000671 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000672 O << "\n";
673 return;
674 }
675
Chris Lattner233ad712002-11-21 01:33:44 +0000676 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000677 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000678 // those with 3 and 2 operands:
679 //
680 // 3 Operands: in this form, the last register (the second input) is the
681 // ModR/M input. The first two operands should be the same, post register
682 // allocation. This is for things like: add r32, r/m32
683 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000684 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
685 // instructions like the IMULri instructions.
686 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000687 // 2 Operands: this is for things like mov that do not read a second input
688 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000689 assert(MI->getOperand(0).isRegister() &&
690 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000691 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000692 (MI->getNumOperands() == 3 &&
693 (MI->getOperand(2).isRegister() ||
694 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000695 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000696 if (MI->getNumOperands() == 3 &&
697 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
698 O << "**";
699
Brian Gaeked7908f62003-06-27 00:00:48 +0000700 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000701 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000702
703 // If this is IMULri* instructions, print the non-two-address operand.
704 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
705 O << ", ";
706 printOp(MI->getOperand(1));
707 }
708
Chris Lattner644e1ab2002-11-21 00:30:01 +0000709 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000710 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000711 O << "\n";
712 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000713 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000714
Chris Lattner3d3067b2002-11-21 20:44:15 +0000715 case X86II::MRMSrcMem: {
716 // These instructions are the same as MRMSrcReg, but instead of having a
717 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000718 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000719 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000720 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000721 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000722 isMem(MI, 2))
723 && "Bad format for MRMDestReg!");
724 if (MI->getNumOperands() == 2+4 &&
725 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
726 O << "**";
727
Brian Gaeked7908f62003-06-27 00:00:48 +0000728 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000729 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000730 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000731 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000732 O << "\n";
733 return;
734 }
735
Chris Lattner675dd2c2002-11-21 17:09:01 +0000736 case X86II::MRMS0r: case X86II::MRMS1r:
737 case X86II::MRMS2r: case X86II::MRMS3r:
738 case X86II::MRMS4r: case X86II::MRMS5r:
739 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000740 // In this form, the following are valid formats:
741 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000742 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000743 // 2. shl rdest, rinput <implicit CL or 1>
744 // 3. sbb rdest, rinput, immediate [rdest = rinput]
745 //
746 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000747 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000748 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000749 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000750 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000751 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000752 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000753 "Bad MRMSxR format!");
754
Chris Lattnerd9096832002-12-15 08:01:39 +0000755 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000756 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
757 O << "**";
758
Brian Gaeked7908f62003-06-27 00:00:48 +0000759 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000760 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000761 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000762 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000763 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000764 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000765 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000766 O << "\n";
767
768 return;
769 }
770
Chris Lattnerb7089442003-01-13 00:35:03 +0000771 case X86II::MRMS0m: case X86II::MRMS1m:
772 case X86II::MRMS2m: case X86II::MRMS3m:
773 case X86II::MRMS4m: case X86II::MRMS5m:
774 case X86II::MRMS6m: case X86II::MRMS7m: {
775 // In this form, the following are valid formats:
776 // 1. sete [m]
777 // 2. cmp [m], immediate
778 // 2. shl [m], rinput <implicit CL or 1>
779 // 3. sbb [m], immediate
780 //
781 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
782 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000783 assert((MI->getNumOperands() != 5 ||
784 (MI->getOperand(4).isImmediate() ||
785 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000786 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000787
788 const MachineOperand &Op3 = MI->getOperand(3);
789
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000790 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
791 // is misassembled by gas in intel_syntax mode as its 32-bit
792 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
793 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000794 if (MI->getOpCode() == X86::FSTPr80) {
795 if ((MI->getOperand(0).getReg() == X86::ESP)
796 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000797 if (Op3.isImmediate() &&
798 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
799 // 1 byte disp.
800 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
801 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
802 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000803 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000804 O << ".long ";
805 printOp(Op3);
806 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000807 }
808 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000809 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000810
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000811 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
812 // misassembled by gas in intel_syntax mode as its 32-bit
813 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
814 // opcode bytes instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000815 if (MI->getOpCode() == X86::FLDr80 &&
816 MI->getOperand(0).getReg() == X86::ESP &&
817 MI->getOperand(1).getImmedValue() == 1) {
818 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
819 Op3.getImmedValue() <= 127) { // 1 byte displacement
820 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
821 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
822 } else {
823 O << ".byte 0xdb, 0xac, 0x24\n\t";
824 O << ".long ";
825 printOp(Op3);
826 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000827 }
828 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000829
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000830 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
831 // invalid opcode, saying "64 bit operations are only supported in
832 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
833 // [...]", which is wrong. Workaround: Output the raw opcode bytes
834 // instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000835 if (MI->getOpCode() == X86::FILDr64 &&
836 MI->getOperand(0).getReg() == X86::ESP &&
837 MI->getOperand(1).getImmedValue() == 1) {
838 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
839 Op3.getImmedValue() <= 127) { // 1 byte displacement
840 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
841 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
842 } else {
843 O << ".byte 0xdf, 0xac, 0x24\n\t";
844 O << ".long ";
845 printOp(Op3);
846 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000847 }
848 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000849
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000850 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
851 // an invalid opcode, saying "64 bit operations are only
852 // supported in 64 bit modes." libopcodes disassembles it as
853 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
854 // "fistpll DWORD PTR " instead, which is what libopcodes is
855 // expecting to see.
856 if (MI->getOpCode() == X86::FISTPr64) {
857 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000858 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000859 if (MI->getNumOperands() == 5) {
860 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000861 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000862 }
863 O << "\t# ";
864 }
865
Brian Gaeked7908f62003-06-27 00:00:48 +0000866 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000867 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000868 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000869 if (MI->getNumOperands() == 5) {
870 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000871 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000872 }
873 O << "\n";
874 return;
875 }
876
Chris Lattnerf9f60882002-11-18 06:56:51 +0000877 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000878 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000879 }
Chris Lattner72614082002-10-25 22:55:53 +0000880}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000881
Chris Lattner93c1afa2003-08-11 19:35:26 +0000882bool Printer::doInitialization(Module &M) {
883 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000884 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000885 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
886 // instruction as a reference to the register named sp, and if you try to
887 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
888 // before being looked up in the symbol table. This creates spurious
889 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
890 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000891 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000892 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000893 return false; // success
894}
895
Chris Lattnerad200712003-09-09 16:23:36 +0000896// SwitchSection - Switch to the specified section of the executable if we are
897// not already in it!
898//
899static void SwitchSection(std::ostream &OS, std::string &CurSection,
900 const char *NewSection) {
901 if (CurSection != NewSection) {
902 CurSection = NewSection;
903 if (!CurSection.empty())
904 OS << "\t" << NewSection << "\n";
905 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000906}
907
Chris Lattnerad200712003-09-09 16:23:36 +0000908bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000909 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000910 std::string CurSection;
911
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000912 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000913 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
914 if (I->hasInitializer()) { // External global require no code
915 O << "\n\n";
916 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000917 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000918 unsigned Size = TD.getTypeSize(C->getType());
919 unsigned Align = TD.getTypeAlignment(C->getType());
920
921 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000922 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
923 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000924 SwitchSection(O, CurSection, ".data");
925 if (I->hasInternalLinkage())
926 O << "\t.local " << name << "\n";
927
928 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000929 << "," << (unsigned)TD.getTypeAlignment(C->getType());
930 O << "\t\t# ";
931 WriteAsOperand(O, I, true, true, &M);
932 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000933 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000934 switch (I->getLinkage()) {
935 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000936 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000937 // Nonnull linkonce -> weak
938 O << "\t.weak " << name << "\n";
939 SwitchSection(O, CurSection, "");
940 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
941 break;
942
943 case GlobalValue::AppendingLinkage:
944 // FIXME: appending linkage variables should go into a section of
945 // their name or something. For now, just emit them as external.
946 case GlobalValue::ExternalLinkage:
947 // If external or appending, declare as a global symbol
948 O << "\t.globl " << name << "\n";
949 // FALL THROUGH
950 case GlobalValue::InternalLinkage:
951 if (C->isNullValue())
952 SwitchSection(O, CurSection, ".bss");
953 else
954 SwitchSection(O, CurSection, ".data");
955 break;
956 }
957
958 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000959 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000960 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000961 O << name << ":\t\t\t\t# ";
962 WriteAsOperand(O, I, true, true, &M);
963 O << " = ";
964 WriteAsOperand(O, C, false, false, &M);
965 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000966 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000967 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000968 }
Chris Lattnerad200712003-09-09 16:23:36 +0000969
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000970 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000971 return false; // success
972}
Brian Gaeked0fde302003-11-11 22:41:34 +0000973
974} // End llvm namespace