blob: 289a68b7f7bbbea91ccd7b5914d266371d40c69a [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;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000554 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
555 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000556 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000557 O << " = ";
558 ++i;
559 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000560 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000561
562 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
563 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000564 if (MI->getOperand(i).opIsDefOnly() ||
565 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000566 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000567 if (MI->getOperand(i).opIsDefOnly() ||
568 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000569 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000570 }
571 O << "\n";
572 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000573
Chris Lattnerf9f60882002-11-18 06:56:51 +0000574 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000575 // The accepted forms of Raw instructions are:
576 // 1. nop - No operand required
577 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000578 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000579 //
580 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000581 (MI->getNumOperands() == 1 &&
582 (MI->getOperand(0).isPCRelativeDisp() ||
583 MI->getOperand(0).isGlobalAddress() ||
584 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000585 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000586 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000587
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000588 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000589 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000590 }
591 O << "\n";
592 return;
593
Chris Lattner77875d82002-11-21 02:00:20 +0000594 case X86II::AddRegFrm: {
595 // There are currently two forms of acceptable AddRegFrm instructions.
596 // Either the instruction JUST takes a single register (like inc, dec, etc),
597 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000598 // (move immediate f.e.). Note that this immediate value might be stored as
599 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000600 // into a register. The initial register might be duplicated if this is a
601 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000602 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000603 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000604 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000605 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000606 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000607 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000608 MI->getOperand(1).isRegister() ||
609 MI->getOperand(1).isGlobalAddress() ||
610 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000611 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000612
Chris Lattner77875d82002-11-21 02:00:20 +0000613 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000614
Brian Gaeked7908f62003-06-27 00:00:48 +0000615 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000616 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000617 if (MI->getNumOperands() == 2 &&
618 (!MI->getOperand(1).isRegister() ||
619 MI->getOperand(1).getVRegValueOrNull() ||
620 MI->getOperand(1).isGlobalAddress() ||
621 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000622 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000623 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000624 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000625 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000626 O << "\n";
627 return;
628 }
Chris Lattner233ad712002-11-21 01:33:44 +0000629 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000630 // There are two acceptable forms of MRMDestReg instructions, those with 2,
631 // 3 and 4 operands:
632 //
633 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000634 //
635 // 3 Operands: in this form, the first two registers (the destination, and
636 // the first operand) should be the same, post register allocation. The 3rd
637 // operand is an additional input. This should be for things like add
638 // instructions.
639 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000640 // 4 Operands: This form is for instructions which are 3 operands forms, but
641 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000643 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000644 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000645 (MI->getNumOperands() == 2 ||
646 (isTwoAddr && MI->getOperand(1).isRegister() &&
647 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
648 (MI->getNumOperands() == 3 ||
649 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000650 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000651
Brian Gaeked7908f62003-06-27 00:00:48 +0000652 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000653 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000654 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000655 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000656 if (MI->getNumOperands() == 4) {
657 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000658 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000659 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000660 O << "\n";
661 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000662 }
Chris Lattner18042332002-11-21 21:03:39 +0000663
664 case X86II::MRMDestMem: {
665 // These instructions are the same as MRMDestReg, but instead of having a
666 // register reference for the mod/rm field, it's a memory reference.
667 //
668 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000669 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000670
Brian Gaeked7908f62003-06-27 00:00:48 +0000671 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000672 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000673 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000674 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000675 O << "\n";
676 return;
677 }
678
Chris Lattner233ad712002-11-21 01:33:44 +0000679 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000680 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000681 // those with 3 and 2 operands:
682 //
683 // 3 Operands: in this form, the last register (the second input) is the
684 // ModR/M input. The first two operands should be the same, post register
685 // allocation. This is for things like: add r32, r/m32
686 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000687 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
688 // instructions like the IMULri instructions.
689 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000690 // 2 Operands: this is for things like mov that do not read a second input
691 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000692 assert(MI->getOperand(0).isRegister() &&
693 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000694 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000695 (MI->getNumOperands() == 3 &&
696 (MI->getOperand(2).isRegister() ||
697 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000698 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000699 if (MI->getNumOperands() == 3 &&
700 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
701 O << "**";
702
Brian Gaeked7908f62003-06-27 00:00:48 +0000703 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000704 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000705
706 // If this is IMULri* instructions, print the non-two-address operand.
707 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
708 O << ", ";
709 printOp(MI->getOperand(1));
710 }
711
Chris Lattner644e1ab2002-11-21 00:30:01 +0000712 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000713 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000714 O << "\n";
715 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000716 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000717
Chris Lattner3d3067b2002-11-21 20:44:15 +0000718 case X86II::MRMSrcMem: {
719 // These instructions are the same as MRMSrcReg, but instead of having a
720 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000721 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000722 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000723 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000724 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000725 isMem(MI, 2))
726 && "Bad format for MRMDestReg!");
727 if (MI->getNumOperands() == 2+4 &&
728 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
729 O << "**";
730
Brian Gaeked7908f62003-06-27 00:00:48 +0000731 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000732 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000733 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000734 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000735 O << "\n";
736 return;
737 }
738
Chris Lattner675dd2c2002-11-21 17:09:01 +0000739 case X86II::MRMS0r: case X86II::MRMS1r:
740 case X86II::MRMS2r: case X86II::MRMS3r:
741 case X86II::MRMS4r: case X86II::MRMS5r:
742 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000743 // In this form, the following are valid formats:
744 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000745 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000746 // 2. shl rdest, rinput <implicit CL or 1>
747 // 3. sbb rdest, rinput, immediate [rdest = rinput]
748 //
749 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000750 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000751 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000752 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000753 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000754 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000755 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000756 "Bad MRMSxR format!");
757
Chris Lattnerd9096832002-12-15 08:01:39 +0000758 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000759 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
760 O << "**";
761
Brian Gaeked7908f62003-06-27 00:00:48 +0000762 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000763 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000764 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000765 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000766 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000767 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000768 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000769 O << "\n";
770
771 return;
772 }
773
Chris Lattnerb7089442003-01-13 00:35:03 +0000774 case X86II::MRMS0m: case X86II::MRMS1m:
775 case X86II::MRMS2m: case X86II::MRMS3m:
776 case X86II::MRMS4m: case X86II::MRMS5m:
777 case X86II::MRMS6m: case X86II::MRMS7m: {
778 // In this form, the following are valid formats:
779 // 1. sete [m]
780 // 2. cmp [m], immediate
781 // 2. shl [m], rinput <implicit CL or 1>
782 // 3. sbb [m], immediate
783 //
784 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
785 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000786 assert((MI->getNumOperands() != 5 ||
787 (MI->getOperand(4).isImmediate() ||
788 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000789 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000790
791 const MachineOperand &Op3 = MI->getOperand(3);
792
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000793 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
794 // is misassembled by gas in intel_syntax mode as its 32-bit
795 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
796 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000797 if (MI->getOpCode() == X86::FSTPr80) {
798 if ((MI->getOperand(0).getReg() == X86::ESP)
799 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000800 if (Op3.isImmediate() &&
801 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
802 // 1 byte disp.
803 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
804 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
805 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000806 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000807 O << ".long ";
808 printOp(Op3);
809 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000810 }
811 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000812 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000813
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000814 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
815 // misassembled by gas in intel_syntax mode as its 32-bit
816 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
817 // opcode bytes instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000818 if (MI->getOpCode() == X86::FLDr80 &&
819 MI->getOperand(0).getReg() == X86::ESP &&
820 MI->getOperand(1).getImmedValue() == 1) {
821 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
822 Op3.getImmedValue() <= 127) { // 1 byte displacement
823 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
824 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
825 } else {
826 O << ".byte 0xdb, 0xac, 0x24\n\t";
827 O << ".long ";
828 printOp(Op3);
829 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000830 }
831 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000832
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000833 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
834 // invalid opcode, saying "64 bit operations are only supported in
835 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
836 // [...]", which is wrong. Workaround: Output the raw opcode bytes
837 // instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000838 if (MI->getOpCode() == X86::FILDr64 &&
839 MI->getOperand(0).getReg() == X86::ESP &&
840 MI->getOperand(1).getImmedValue() == 1) {
841 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
842 Op3.getImmedValue() <= 127) { // 1 byte displacement
843 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
844 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
845 } else {
846 O << ".byte 0xdf, 0xac, 0x24\n\t";
847 O << ".long ";
848 printOp(Op3);
849 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000850 }
851 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000852
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000853 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
854 // an invalid opcode, saying "64 bit operations are only
855 // supported in 64 bit modes." libopcodes disassembles it as
856 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
857 // "fistpll DWORD PTR " instead, which is what libopcodes is
858 // expecting to see.
859 if (MI->getOpCode() == X86::FISTPr64) {
860 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000861 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000862 if (MI->getNumOperands() == 5) {
863 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000864 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000865 }
866 O << "\t# ";
867 }
868
Brian Gaeked7908f62003-06-27 00:00:48 +0000869 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000870 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000871 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000872 if (MI->getNumOperands() == 5) {
873 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000874 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000875 }
876 O << "\n";
877 return;
878 }
879
Chris Lattnerf9f60882002-11-18 06:56:51 +0000880 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000881 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000882 }
Chris Lattner72614082002-10-25 22:55:53 +0000883}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000884
Chris Lattner93c1afa2003-08-11 19:35:26 +0000885bool Printer::doInitialization(Module &M) {
886 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000887 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000888 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
889 // instruction as a reference to the register named sp, and if you try to
890 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
891 // before being looked up in the symbol table. This creates spurious
892 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
893 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000894 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000895 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000896 return false; // success
897}
898
Chris Lattnerad200712003-09-09 16:23:36 +0000899// SwitchSection - Switch to the specified section of the executable if we are
900// not already in it!
901//
902static void SwitchSection(std::ostream &OS, std::string &CurSection,
903 const char *NewSection) {
904 if (CurSection != NewSection) {
905 CurSection = NewSection;
906 if (!CurSection.empty())
907 OS << "\t" << NewSection << "\n";
908 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000909}
910
Chris Lattnerad200712003-09-09 16:23:36 +0000911bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000912 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000913 std::string CurSection;
914
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000915 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000916 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
917 if (I->hasInitializer()) { // External global require no code
918 O << "\n\n";
919 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000920 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000921 unsigned Size = TD.getTypeSize(C->getType());
922 unsigned Align = TD.getTypeAlignment(C->getType());
923
924 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000925 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
926 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000927 SwitchSection(O, CurSection, ".data");
928 if (I->hasInternalLinkage())
929 O << "\t.local " << name << "\n";
930
931 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000932 << "," << (unsigned)TD.getTypeAlignment(C->getType());
933 O << "\t\t# ";
934 WriteAsOperand(O, I, true, true, &M);
935 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000936 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000937 switch (I->getLinkage()) {
938 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000939 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000940 // Nonnull linkonce -> weak
941 O << "\t.weak " << name << "\n";
942 SwitchSection(O, CurSection, "");
943 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
944 break;
945
946 case GlobalValue::AppendingLinkage:
947 // FIXME: appending linkage variables should go into a section of
948 // their name or something. For now, just emit them as external.
949 case GlobalValue::ExternalLinkage:
950 // If external or appending, declare as a global symbol
951 O << "\t.globl " << name << "\n";
952 // FALL THROUGH
953 case GlobalValue::InternalLinkage:
954 if (C->isNullValue())
955 SwitchSection(O, CurSection, ".bss");
956 else
957 SwitchSection(O, CurSection, ".data");
958 break;
959 }
960
961 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000962 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000963 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000964 O << name << ":\t\t\t\t# ";
965 WriteAsOperand(O, I, true, true, &M);
966 O << " = ";
967 WriteAsOperand(O, C, false, false, &M);
968 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000969 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000970 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000971 }
Chris Lattnerad200712003-09-09 16:23:36 +0000972
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000973 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000974 return false; // success
975}
Brian Gaeked0fde302003-11-11 22:41:34 +0000976
977} // End llvm namespace