blob: 3d073f77c65901db3130e5894a6bf9786a51ad3a [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.cpp - Convert X86 LLVM code to Intel assembly ---------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Brian Gaeke92bdfe62003-07-23 18:37:06 +000010// This file contains a printer that converts from our internal
11// representation of machine-dependent LLVM code to Intel-format
12// assembly language. This printer is the output mechanism used
Chris Lattner93c1afa2003-08-11 19:35:26 +000013// by `llc' and `lli -print-machineinstrs' on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Chris Lattnere0121322003-08-03 23:37:09 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000021#include "llvm/Module.h"
22#include "llvm/Assembly/Writer.h"
Chris Lattner0285a332002-12-28 20:25:38 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000025#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000026#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000027#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000028#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000029#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000030#include "Support/CommandLine.h"
Chris Lattner72614082002-10-25 22:55:53 +000031
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000032namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000033 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
34
Chris Lattner93c1afa2003-08-11 19:35:26 +000035 // FIXME: This should be automatically picked up by autoconf from the C
36 // frontend
37 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
38 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
39
Chris Lattner0285a332002-12-28 20:25:38 +000040 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000041 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000042 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000043 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000044
45 /// Target machine description which we query for reg. names, data
46 /// layout, etc.
47 ///
48 TargetMachine &TM;
49
Brian Gaeked9fb37a2003-07-24 20:20:44 +000050 /// Name-mangler for global names.
51 ///
52 Mangler *Mang;
53
Brian Gaekede420ae2003-07-23 20:25:08 +000054 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000055
56 /// We name each basic block in a Function with a unique number, so
57 /// that we can consistently refer to them later. This is cleared
58 /// at the beginning of each call to runOnMachineFunction().
59 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000060 typedef std::map<const Value *, unsigned> ValueMapTy;
61 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000062
63 /// Cache of mangled name for current function. This is
64 /// recalculated at the beginning of each call to
65 /// runOnMachineFunction().
66 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000067 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000068
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000069 virtual const char *getPassName() const {
70 return "X86 Assembly Printer";
71 }
72
Brian Gaeke2a098772003-08-11 19:05:46 +000073 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000074 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000075 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000076 bool elideOffsetKeyword = false);
77 void printMemReference(const MachineInstr *MI, unsigned Op);
78 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000079 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +000080 bool doInitialization(Module &M);
81 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +000082 void emitGlobalConstant(const Constant* CV);
83 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000084 };
Brian Gaeked7908f62003-06-27 00:00:48 +000085} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000086
Brian Gaeke92bdfe62003-07-23 18:37:06 +000087/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000088/// assembly code for a MachineFunction to the given output stream,
89/// using the given target machine description. This should work
90/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000091///
Brian Gaeke9d99b432003-08-13 18:15:15 +000092FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000093 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000094}
95
Brian Gaeke92bdfe62003-07-23 18:37:06 +000096/// isStringCompatible - Can we treat the specified array as a string?
97/// Only if it is an array of ubytes or non-negative sbytes.
98///
Brian Gaeke01d79ff2003-06-25 18:01:07 +000099static bool isStringCompatible(const ConstantArray *CVA) {
100 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
101 if (ETy == Type::UByteTy) return true;
102 if (ETy != Type::SByteTy) return false;
103
104 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
105 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
106 return false;
107
108 return true;
109}
110
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000111/// toOctal - Convert the low order bits of X into an octal digit.
112///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000113static inline char toOctal(int X) {
114 return (X&7)+'0';
115}
116
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000117/// getAsCString - Return the specified array as a C compatible
118/// string, only if the predicate isStringCompatible is true.
119///
Chris Lattnerac662d12003-11-03 20:19:49 +0000120static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000121 assert(isStringCompatible(CVA) && "Array is not string compatible!");
122
Chris Lattnerac662d12003-11-03 20:19:49 +0000123 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000124 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000125 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126
127 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000128 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000129 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000130 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000132 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000133 } else {
134 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000135 case '\b': O << "\\b"; break;
136 case '\f': O << "\\f"; break;
137 case '\n': O << "\\n"; break;
138 case '\r': O << "\\r"; break;
139 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000140 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000141 O << '\\';
142 O << toOctal(C >> 6);
143 O << toOctal(C >> 3);
144 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000145 break;
146 }
147 }
148 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000149 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150}
151
Chris Lattnerac662d12003-11-03 20:19:49 +0000152// Print out the specified constant, without a storage class. Only the
153// constants valid in constant expressions can occur here.
154void Printer::emitConstantValueOnly(const Constant *CV) {
155 if (CV->isNullValue())
156 O << "0";
157 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
158 assert(CB == ConstantBool::True);
159 O << "1";
160 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
161 O << CI->getValue();
162 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
163 O << CI->getValue();
164 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
165 // This is a constant address for a global variable or function. Use the
166 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000167 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000168 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
169 const TargetData &TD = TM.getTargetData();
170 switch(CE->getOpcode()) {
171 case Instruction::GetElementPtr: {
172 // generate a symbolic expression for the byte address
173 const Constant *ptrVal = CE->getOperand(0);
174 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
175 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
176 O << "(";
177 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000178 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000179 } else {
180 emitConstantValueOnly(ptrVal);
181 }
182 break;
183 }
184 case Instruction::Cast: {
185 // Support only non-converting or widening casts for now, that is, ones
186 // that do not involve a change in value. This assertion is not a
187 // complete check.
188 Constant *Op = CE->getOperand(0);
189 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000190
Chris Lattnerac662d12003-11-03 20:19:49 +0000191 assert(((isa<PointerType>(OpTy)
192 && (Ty == Type::LongTy || Ty == Type::ULongTy))
193 || (isa<PointerType>(Ty)
Chris Lattner90533562003-11-04 16:04:32 +0000194 && (OpTy == Type::LongTy || OpTy == Type::ULongTy))
195 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
196 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000197 && "FIXME: Don't yet support this kind of constant cast expr");
198 O << "(";
199 emitConstantValueOnly(Op);
200 O << ")";
201 break;
202 }
203 case Instruction::Add:
204 O << "(";
205 emitConstantValueOnly(CE->getOperand(0));
206 O << ") + (";
207 emitConstantValueOnly(CE->getOperand(1));
208 O << ")";
209 break;
210 default:
211 assert(0 && "Unsupported operator!");
212 }
213 } else {
214 assert(0 && "Unknown constant value!");
215 }
216}
217
218// Print a constant value or values, with the appropriate storage class as a
219// prefix.
220void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000221 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000222
Chris Lattnerad200712003-09-09 16:23:36 +0000223 if (CV->isNullValue()) {
224 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000225 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000226 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
227 if (isStringCompatible(CVA)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000228 O << "\t.ascii\t";
229 printAsCString(O, CVA);
230 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000231 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000232 const std::vector<Use> &constValues = CVA->getValues();
233 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000234 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000235 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000236 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000237 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
238 // Print the fields in successive locations. Pad to align if needed!
239 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
240 const std::vector<Use>& constValues = CVS->getValues();
241 unsigned sizeSoFar = 0;
242 for (unsigned i=0, N = constValues.size(); i < N; i++) {
243 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000244
Chris Lattnerad200712003-09-09 16:23:36 +0000245 // Check if padding is needed and insert one or more 0s.
246 unsigned fieldSize = TD.getTypeSize(field->getType());
247 unsigned padSize = ((i == N-1? cvsLayout->StructSize
248 : cvsLayout->MemberOffsets[i+1])
249 - cvsLayout->MemberOffsets[i]) - fieldSize;
250 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000251
Chris Lattnerad200712003-09-09 16:23:36 +0000252 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000253 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000254
255 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000256 if (padSize)
257 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000258 }
Chris Lattnerad200712003-09-09 16:23:36 +0000259 assert(sizeSoFar == cvsLayout->StructSize &&
260 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000261 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000262 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
263 // FP Constants are printed as integer constants to avoid losing
264 // precision...
265 double Val = CFP->getValue();
266 switch (CFP->getType()->getPrimitiveID()) {
267 default: assert(0 && "Unknown floating point type!");
268 case Type::FloatTyID: {
269 union FU { // Abide by C TBAA rules
270 float FVal;
271 unsigned UVal;
272 } U;
273 U.FVal = Val;
274 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
275 return;
276 }
277 case Type::DoubleTyID: {
278 union DU { // Abide by C TBAA rules
279 double FVal;
280 uint64_t UVal;
281 } U;
282 U.FVal = Val;
283 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
284 return;
285 }
286 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000287 }
288
289 const Type *type = CV->getType();
290 O << "\t";
291 switch (type->getPrimitiveID()) {
292 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
293 O << ".byte";
294 break;
295 case Type::UShortTyID: case Type::ShortTyID:
296 O << ".word";
297 break;
298 case Type::FloatTyID: case Type::PointerTyID:
299 case Type::UIntTyID: case Type::IntTyID:
300 O << ".long";
301 break;
302 case Type::DoubleTyID:
303 case Type::ULongTyID: case Type::LongTyID:
304 O << ".quad";
305 break;
306 default:
307 assert (0 && "Can't handle printing this type of thing");
308 break;
309 }
310 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000311 emitConstantValueOnly(CV);
312 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000313}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000314
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000315/// printConstantPool - Print to the current output stream assembly
316/// representations of the constants in the constant pool MCP. This is
317/// used to print out constants which have been "spilled to memory" by
318/// the code generator.
319///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000320void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000321 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000322 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000323
Chris Lattnerb7089442003-01-13 00:35:03 +0000324 if (CP.empty()) return;
325
326 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
327 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000328 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000329 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000330 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
331 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000332 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000333 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000334}
335
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000336/// runOnMachineFunction - This uses the printMachineInstruction()
337/// method to print assembly for each instruction.
338///
Chris Lattner0285a332002-12-28 20:25:38 +0000339bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000340 // BBNumber is used here so that a given Printer will never give two
341 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000342 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000343
Chris Lattnere0121322003-08-03 23:37:09 +0000344 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000345 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000346 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000347
Chris Lattnerb7089442003-01-13 00:35:03 +0000348 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000349 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000350
Brian Gaeke6559bb92002-11-14 22:32:30 +0000351 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000352 O << "\t.text\n";
353 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000354 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000355 if (!EmitCygwin)
356 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000357 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000358
Brian Gaeked7908f62003-06-27 00:00:48 +0000359 // Number each basic block so that we can consistently refer to them
360 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000361 NumberForBB.clear();
362 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
363 I != E; ++I) {
364 NumberForBB[I->getBasicBlock()] = BBNumber++;
365 }
366
Brian Gaeke6559bb92002-11-14 22:32:30 +0000367 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000368 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
369 I != E; ++I) {
370 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000371 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000372 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000373 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
374 II != E; ++II) {
375 // Print the assembly for the instruction.
376 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000377 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000378 }
Chris Lattner0285a332002-12-28 20:25:38 +0000379 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000380
381 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000382 return false;
383}
384
Chris Lattner3d3067b2002-11-21 20:44:15 +0000385static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000386 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000387 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
388 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000389}
390
391static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000392 if (MI->getOperand(Op).isFrameIndex()) return true;
393 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000394 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000395 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
396 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000397}
398
Brian Gaeke2a098772003-08-11 19:05:46 +0000399
400
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000401void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000402 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000403 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000404 switch (MO.getType()) {
405 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000406 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000407 O << "<" << V->getName() << ">";
408 return;
409 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000410 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000411 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000412 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000413 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000414 O << "%" << RI.get(MO.getReg()).Name;
415 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000416 O << "%reg" << MO.getReg();
417 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000418
419 case MachineOperand::MO_SignExtendedImmed:
420 case MachineOperand::MO_UnextendedImmed:
421 O << (int)MO.getImmedValue();
422 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000423 case MachineOperand::MO_PCRelativeDisp: {
424 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
425 assert (i != NumberForBB.end()
426 && "Could not find a BB in the NumberForBB map!");
427 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000428 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000429 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000430 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000431 if (!elideOffsetKeyword)
432 O << "OFFSET ";
433 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000434 return;
435 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000436 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000438 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000439 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000440 }
441}
442
Chris Lattner3501fea2003-01-14 22:00:31 +0000443static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000444 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000445 default: assert(0 && "Unknown arg size!");
446 case X86II::Arg8: return "BYTE PTR";
447 case X86II::Arg16: return "WORD PTR";
448 case X86II::Arg32: return "DWORD PTR";
449 case X86II::Arg64: return "QWORD PTR";
450 case X86II::ArgF32: return "DWORD PTR";
451 case X86II::ArgF64: return "QWORD PTR";
452 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000453 }
454}
455
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000456void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000457 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000458
459 if (MI->getOperand(Op).isFrameIndex()) {
460 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
461 if (MI->getOperand(Op+3).getImmedValue())
462 O << " + " << MI->getOperand(Op+3).getImmedValue();
463 O << "]";
464 return;
465 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000466 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000467 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000468 if (MI->getOperand(Op+3).getImmedValue())
469 O << " + " << MI->getOperand(Op+3).getImmedValue();
470 O << "]";
471 return;
472 }
473
Chris Lattner3d3067b2002-11-21 20:44:15 +0000474 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000475 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000476 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000477 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000478
479 O << "[";
480 bool NeedPlus = false;
481 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000482 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000483 NeedPlus = true;
484 }
485
486 if (IndexReg.getReg()) {
487 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000488 if (ScaleVal != 1)
489 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000490 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000491 NeedPlus = true;
492 }
493
Chris Lattner0285a332002-12-28 20:25:38 +0000494 if (DispVal) {
495 if (NeedPlus)
496 if (DispVal > 0)
497 O << " + ";
498 else {
499 O << " - ";
500 DispVal = -DispVal;
501 }
502 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503 }
504 O << "]";
505}
506
Brian Gaeke2a098772003-08-11 19:05:46 +0000507/// checkImplUses - Emit the implicit-use registers for the
508/// instruction described by DESC, if its PrintImplUses flag is set.
509///
510void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
511 const MRegisterInfo &RI = *TM.getRegisterInfo();
512 if (Desc.TSFlags & X86II::PrintImplUses) {
513 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
514 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000515 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000516 }
517 }
518}
519
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000520/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000521/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000522///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000523void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000524 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000525 const TargetInstrInfo &TII = TM.getInstrInfo();
526 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000527
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000528 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000529 switch (Desc.TSFlags & X86II::FormMask) {
530 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000531 // Print pseudo-instructions as comments; either they should have been
532 // turned into real instructions by now, or they don't need to be
533 // seen by the assembler (e.g., IMPLICIT_USEs.)
534 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000535 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000536 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000537 O << " = phi ";
538 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
539 if (i != 1) O << ", ";
540 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000541 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000542 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000543 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000544 O << "]";
545 }
546 } else {
547 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000548 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
549 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000550 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000551 O << " = ";
552 ++i;
553 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000554 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000555
556 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
557 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000558 if (MI->getOperand(i).opIsDefOnly() ||
559 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000560 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000561 if (MI->getOperand(i).opIsDefOnly() ||
562 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000563 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000564 }
565 O << "\n";
566 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000567
Chris Lattnerf9f60882002-11-18 06:56:51 +0000568 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000569 // The accepted forms of Raw instructions are:
570 // 1. nop - No operand required
571 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000572 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000573 //
574 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000575 (MI->getNumOperands() == 1 &&
576 (MI->getOperand(0).isPCRelativeDisp() ||
577 MI->getOperand(0).isGlobalAddress() ||
578 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000579 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000580 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000581
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000582 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000583 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000584 }
585 O << "\n";
586 return;
587
Chris Lattner77875d82002-11-21 02:00:20 +0000588 case X86II::AddRegFrm: {
589 // There are currently two forms of acceptable AddRegFrm instructions.
590 // Either the instruction JUST takes a single register (like inc, dec, etc),
591 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000592 // (move immediate f.e.). Note that this immediate value might be stored as
593 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000594 // into a register. The initial register might be duplicated if this is a
595 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000596 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000597 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000598 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000599 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000600 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000601 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000602 MI->getOperand(1).isRegister() ||
603 MI->getOperand(1).isGlobalAddress() ||
604 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000605 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000606
Chris Lattner77875d82002-11-21 02:00:20 +0000607 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000608
Brian Gaeked7908f62003-06-27 00:00:48 +0000609 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000610 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000611 if (MI->getNumOperands() == 2 &&
612 (!MI->getOperand(1).isRegister() ||
613 MI->getOperand(1).getVRegValueOrNull() ||
614 MI->getOperand(1).isGlobalAddress() ||
615 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000616 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000617 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000618 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000619 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000620 O << "\n";
621 return;
622 }
Chris Lattner233ad712002-11-21 01:33:44 +0000623 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000624 // There are two acceptable forms of MRMDestReg instructions, those with 2,
625 // 3 and 4 operands:
626 //
627 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000628 //
629 // 3 Operands: in this form, the first two registers (the destination, and
630 // the first operand) should be the same, post register allocation. The 3rd
631 // operand is an additional input. This should be for things like add
632 // instructions.
633 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000634 // 4 Operands: This form is for instructions which are 3 operands forms, but
635 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000636 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000637 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000638 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000639 (MI->getNumOperands() == 2 ||
640 (isTwoAddr && MI->getOperand(1).isRegister() &&
641 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
642 (MI->getNumOperands() == 3 ||
643 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000644 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000645
Brian Gaeked7908f62003-06-27 00:00:48 +0000646 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000647 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000648 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000649 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000650 if (MI->getNumOperands() == 4) {
651 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000652 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000653 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000654 O << "\n";
655 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000656 }
Chris Lattner18042332002-11-21 21:03:39 +0000657
658 case X86II::MRMDestMem: {
659 // These instructions are the same as MRMDestReg, but instead of having a
660 // register reference for the mod/rm field, it's a memory reference.
661 //
662 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000663 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000664
Brian Gaeked7908f62003-06-27 00:00:48 +0000665 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000666 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000667 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000668 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000669 O << "\n";
670 return;
671 }
672
Chris Lattner233ad712002-11-21 01:33:44 +0000673 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000674 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000675 // those with 3 and 2 operands:
676 //
677 // 3 Operands: in this form, the last register (the second input) is the
678 // ModR/M input. The first two operands should be the same, post register
679 // allocation. This is for things like: add r32, r/m32
680 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000681 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
682 // instructions like the IMULri instructions.
683 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000684 // 2 Operands: this is for things like mov that do not read a second input
685 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000686 assert(MI->getOperand(0).isRegister() &&
687 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000688 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000689 (MI->getNumOperands() == 3 &&
690 (MI->getOperand(2).isRegister() ||
691 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000693 if (MI->getNumOperands() == 3 &&
694 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
695 O << "**";
696
Brian Gaeked7908f62003-06-27 00:00:48 +0000697 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000698 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000699
700 // If this is IMULri* instructions, print the non-two-address operand.
701 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
702 O << ", ";
703 printOp(MI->getOperand(1));
704 }
705
Chris Lattner644e1ab2002-11-21 00:30:01 +0000706 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000707 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000708 O << "\n";
709 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000710 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000711
Chris Lattner3d3067b2002-11-21 20:44:15 +0000712 case X86II::MRMSrcMem: {
713 // These instructions are the same as MRMSrcReg, but instead of having a
714 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000715 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000716 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000717 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000718 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000719 isMem(MI, 2))
720 && "Bad format for MRMDestReg!");
721 if (MI->getNumOperands() == 2+4 &&
722 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
723 O << "**";
724
Brian Gaeked7908f62003-06-27 00:00:48 +0000725 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000726 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000727 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000728 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000729 O << "\n";
730 return;
731 }
732
Chris Lattner675dd2c2002-11-21 17:09:01 +0000733 case X86II::MRMS0r: case X86II::MRMS1r:
734 case X86II::MRMS2r: case X86II::MRMS3r:
735 case X86II::MRMS4r: case X86II::MRMS5r:
736 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000737 // In this form, the following are valid formats:
738 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000739 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000740 // 2. shl rdest, rinput <implicit CL or 1>
741 // 3. sbb rdest, rinput, immediate [rdest = rinput]
742 //
743 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000744 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000745 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000746 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000747 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000748 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000749 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000750 "Bad MRMSxR format!");
751
Chris Lattnerd9096832002-12-15 08:01:39 +0000752 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000753 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
754 O << "**";
755
Brian Gaeked7908f62003-06-27 00:00:48 +0000756 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000757 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000758 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000759 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000760 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000761 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000762 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000763 O << "\n";
764
765 return;
766 }
767
Chris Lattnerb7089442003-01-13 00:35:03 +0000768 case X86II::MRMS0m: case X86II::MRMS1m:
769 case X86II::MRMS2m: case X86II::MRMS3m:
770 case X86II::MRMS4m: case X86II::MRMS5m:
771 case X86II::MRMS6m: case X86II::MRMS7m: {
772 // In this form, the following are valid formats:
773 // 1. sete [m]
774 // 2. cmp [m], immediate
775 // 2. shl [m], rinput <implicit CL or 1>
776 // 3. sbb [m], immediate
777 //
778 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
779 isMem(MI, 0) && "Bad MRMSxM format!");
780 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
781 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000782 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
783 // is misassembled by gas in intel_syntax mode as its 32-bit
784 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
785 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000786 if (MI->getOpCode() == X86::FSTPr80) {
787 if ((MI->getOperand(0).getReg() == X86::ESP)
788 && (MI->getOperand(1).getImmedValue() == 1)) {
789 int DispVal = MI->getOperand(3).getImmedValue();
790 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
791 unsigned int val = (unsigned int) DispVal;
792 O << ".byte 0xdb, 0xbc, 0x24\n\t";
793 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
794 } else { // 1 byte disp.
795 unsigned char val = (unsigned char) DispVal;
796 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
797 << std::dec << "\t# ";
798 }
799 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000800 }
801 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
802 // misassembled by gas in intel_syntax mode as its 32-bit
803 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
804 // opcode bytes instead of the instruction.
805 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000806 if ((MI->getOperand(0).getReg() == X86::ESP)
807 && (MI->getOperand(1).getImmedValue() == 1)) {
808 int DispVal = MI->getOperand(3).getImmedValue();
809 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
810 unsigned int val = (unsigned int) DispVal;
811 O << ".byte 0xdb, 0xac, 0x24\n\t";
812 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
813 } else { // 1 byte disp.
814 unsigned char val = (unsigned char) DispVal;
815 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
816 << std::dec << "\t# ";
817 }
818 }
819 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000820 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
821 // invalid opcode, saying "64 bit operations are only supported in
822 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
823 // [...]", which is wrong. Workaround: Output the raw opcode bytes
824 // instead of the instruction.
825 if (MI->getOpCode() == X86::FILDr64) {
826 if ((MI->getOperand(0).getReg() == X86::ESP)
827 && (MI->getOperand(1).getImmedValue() == 1)) {
828 int DispVal = MI->getOperand(3).getImmedValue();
829 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
830 unsigned int val = (unsigned int) DispVal;
831 O << ".byte 0xdf, 0xac, 0x24\n\t";
832 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
833 } else { // 1 byte disp.
834 unsigned char val = (unsigned char) DispVal;
835 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
836 << std::dec << "\t# ";
837 }
838 }
839 }
840 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
841 // an invalid opcode, saying "64 bit operations are only
842 // supported in 64 bit modes." libopcodes disassembles it as
843 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
844 // "fistpll DWORD PTR " instead, which is what libopcodes is
845 // expecting to see.
846 if (MI->getOpCode() == X86::FISTPr64) {
847 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000848 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000849 if (MI->getNumOperands() == 5) {
850 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000851 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000852 }
853 O << "\t# ";
854 }
855
Brian Gaeked7908f62003-06-27 00:00:48 +0000856 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000857 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000858 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000859 if (MI->getNumOperands() == 5) {
860 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000861 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000862 }
863 O << "\n";
864 return;
865 }
866
Chris Lattnerf9f60882002-11-18 06:56:51 +0000867 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000868 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000869 }
Chris Lattner72614082002-10-25 22:55:53 +0000870}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000871
Chris Lattner93c1afa2003-08-11 19:35:26 +0000872bool Printer::doInitialization(Module &M) {
873 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000874 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000875 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
876 // instruction as a reference to the register named sp, and if you try to
877 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
878 // before being looked up in the symbol table. This creates spurious
879 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
880 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000881 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000882 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000883 return false; // success
884}
885
Chris Lattnerad200712003-09-09 16:23:36 +0000886// SwitchSection - Switch to the specified section of the executable if we are
887// not already in it!
888//
889static void SwitchSection(std::ostream &OS, std::string &CurSection,
890 const char *NewSection) {
891 if (CurSection != NewSection) {
892 CurSection = NewSection;
893 if (!CurSection.empty())
894 OS << "\t" << NewSection << "\n";
895 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000896}
897
Chris Lattnerad200712003-09-09 16:23:36 +0000898bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000899 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000900 std::string CurSection;
901
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000902 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000903 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
904 if (I->hasInitializer()) { // External global require no code
905 O << "\n\n";
906 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000907 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000908 unsigned Size = TD.getTypeSize(C->getType());
909 unsigned Align = TD.getTypeAlignment(C->getType());
910
911 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000912 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
913 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000914 SwitchSection(O, CurSection, ".data");
915 if (I->hasInternalLinkage())
916 O << "\t.local " << name << "\n";
917
918 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000919 << "," << (unsigned)TD.getTypeAlignment(C->getType());
920 O << "\t\t# ";
921 WriteAsOperand(O, I, true, true, &M);
922 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000923 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000924 switch (I->getLinkage()) {
925 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000926 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000927 // Nonnull linkonce -> weak
928 O << "\t.weak " << name << "\n";
929 SwitchSection(O, CurSection, "");
930 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
931 break;
932
933 case GlobalValue::AppendingLinkage:
934 // FIXME: appending linkage variables should go into a section of
935 // their name or something. For now, just emit them as external.
936 case GlobalValue::ExternalLinkage:
937 // If external or appending, declare as a global symbol
938 O << "\t.globl " << name << "\n";
939 // FALL THROUGH
940 case GlobalValue::InternalLinkage:
941 if (C->isNullValue())
942 SwitchSection(O, CurSection, ".bss");
943 else
944 SwitchSection(O, CurSection, ".data");
945 break;
946 }
947
948 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000949 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000950 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000951 O << name << ":\t\t\t\t# ";
952 WriteAsOperand(O, I, true, true, &M);
953 O << " = ";
954 WriteAsOperand(O, C, false, false, &M);
955 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000956 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000957 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000958 }
Chris Lattnerad200712003-09-09 16:23:36 +0000959
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000960 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000961 return false; // success
962}