blob: cfeac4c0f7b5530fba2b3979bc55b2238808eaba [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.
167 O << Mang->getValueName(CPR->getValue()) << "\n";
168 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);
178 O << ") + " + Offset;
179 } 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();
190 assert(((isa<PointerType>(OpTy)
191 && (Ty == Type::LongTy || Ty == Type::ULongTy))
192 || (isa<PointerType>(Ty)
193 && (OpTy == Type::LongTy || OpTy == Type::ULongTy)))
194 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
195 && (OpTy->isLosslesslyConvertibleTo(Ty))))
196 && "FIXME: Don't yet support this kind of constant cast expr");
197 O << "(";
198 emitConstantValueOnly(Op);
199 O << ")";
200 break;
201 }
202 case Instruction::Add:
203 O << "(";
204 emitConstantValueOnly(CE->getOperand(0));
205 O << ") + (";
206 emitConstantValueOnly(CE->getOperand(1));
207 O << ")";
208 break;
209 default:
210 assert(0 && "Unsupported operator!");
211 }
212 } else {
213 assert(0 && "Unknown constant value!");
214 }
215}
216
217// Print a constant value or values, with the appropriate storage class as a
218// prefix.
219void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000220 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000221
Chris Lattnerad200712003-09-09 16:23:36 +0000222 if (CV->isNullValue()) {
223 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000224 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000225 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
226 if (isStringCompatible(CVA)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000227 O << "\t.ascii\t";
228 printAsCString(O, CVA);
229 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000230 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000231 const std::vector<Use> &constValues = CVA->getValues();
232 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000233 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000234 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000235 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000236 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
237 // Print the fields in successive locations. Pad to align if needed!
238 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
239 const std::vector<Use>& constValues = CVS->getValues();
240 unsigned sizeSoFar = 0;
241 for (unsigned i=0, N = constValues.size(); i < N; i++) {
242 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000243
Chris Lattnerad200712003-09-09 16:23:36 +0000244 // Check if padding is needed and insert one or more 0s.
245 unsigned fieldSize = TD.getTypeSize(field->getType());
246 unsigned padSize = ((i == N-1? cvsLayout->StructSize
247 : cvsLayout->MemberOffsets[i+1])
248 - cvsLayout->MemberOffsets[i]) - fieldSize;
249 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000250
Chris Lattnerad200712003-09-09 16:23:36 +0000251 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000252 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000253
254 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000255 if (padSize)
256 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000257 }
Chris Lattnerad200712003-09-09 16:23:36 +0000258 assert(sizeSoFar == cvsLayout->StructSize &&
259 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000260 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000261 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
262 // FP Constants are printed as integer constants to avoid losing
263 // precision...
264 double Val = CFP->getValue();
265 switch (CFP->getType()->getPrimitiveID()) {
266 default: assert(0 && "Unknown floating point type!");
267 case Type::FloatTyID: {
268 union FU { // Abide by C TBAA rules
269 float FVal;
270 unsigned UVal;
271 } U;
272 U.FVal = Val;
273 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
274 return;
275 }
276 case Type::DoubleTyID: {
277 union DU { // Abide by C TBAA rules
278 double FVal;
279 uint64_t UVal;
280 } U;
281 U.FVal = Val;
282 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
283 return;
284 }
285 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000286 }
287
288 const Type *type = CV->getType();
289 O << "\t";
290 switch (type->getPrimitiveID()) {
291 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
292 O << ".byte";
293 break;
294 case Type::UShortTyID: case Type::ShortTyID:
295 O << ".word";
296 break;
297 case Type::FloatTyID: case Type::PointerTyID:
298 case Type::UIntTyID: case Type::IntTyID:
299 O << ".long";
300 break;
301 case Type::DoubleTyID:
302 case Type::ULongTyID: case Type::LongTyID:
303 O << ".quad";
304 break;
305 default:
306 assert (0 && "Can't handle printing this type of thing");
307 break;
308 }
309 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000310 emitConstantValueOnly(CV);
311 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000312}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000313
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000314/// printConstantPool - Print to the current output stream assembly
315/// representations of the constants in the constant pool MCP. This is
316/// used to print out constants which have been "spilled to memory" by
317/// the code generator.
318///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000319void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000320 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000321 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000322
Chris Lattnerb7089442003-01-13 00:35:03 +0000323 if (CP.empty()) return;
324
325 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
326 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000327 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000328 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000329 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
330 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000331 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000332 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000333}
334
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000335/// runOnMachineFunction - This uses the printMachineInstruction()
336/// method to print assembly for each instruction.
337///
Chris Lattner0285a332002-12-28 20:25:38 +0000338bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000339 // BBNumber is used here so that a given Printer will never give two
340 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000341 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000342
Chris Lattnere0121322003-08-03 23:37:09 +0000343 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000344 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000345 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000346
Chris Lattnerb7089442003-01-13 00:35:03 +0000347 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000348 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000349
Brian Gaeke6559bb92002-11-14 22:32:30 +0000350 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000351 O << "\t.text\n";
352 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000353 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000354 if (!EmitCygwin)
355 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000356 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000357
Brian Gaeked7908f62003-06-27 00:00:48 +0000358 // Number each basic block so that we can consistently refer to them
359 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000360 NumberForBB.clear();
361 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
362 I != E; ++I) {
363 NumberForBB[I->getBasicBlock()] = BBNumber++;
364 }
365
Brian Gaeke6559bb92002-11-14 22:32:30 +0000366 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000367 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
368 I != E; ++I) {
369 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000370 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000371 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000372 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
373 II != E; ++II) {
374 // Print the assembly for the instruction.
375 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000376 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000377 }
Chris Lattner0285a332002-12-28 20:25:38 +0000378 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000379
380 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000381 return false;
382}
383
Chris Lattner3d3067b2002-11-21 20:44:15 +0000384static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000385 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000386 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
387 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000388}
389
390static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000391 if (MI->getOperand(Op).isFrameIndex()) return true;
392 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000393 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000394 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
395 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000396}
397
Brian Gaeke2a098772003-08-11 19:05:46 +0000398
399
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000400void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000401 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000402 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000403 switch (MO.getType()) {
404 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000405 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000406 O << "<" << V->getName() << ">";
407 return;
408 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000409 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000410 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000411 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000412 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000413 O << "%" << RI.get(MO.getReg()).Name;
414 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000415 O << "%reg" << MO.getReg();
416 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000417
418 case MachineOperand::MO_SignExtendedImmed:
419 case MachineOperand::MO_UnextendedImmed:
420 O << (int)MO.getImmedValue();
421 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000422 case MachineOperand::MO_PCRelativeDisp: {
423 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
424 assert (i != NumberForBB.end()
425 && "Could not find a BB in the NumberForBB map!");
426 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000427 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000428 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000429 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000430 if (!elideOffsetKeyword)
431 O << "OFFSET ";
432 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000433 return;
434 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000435 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000436 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000437 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000438 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000439 }
440}
441
Chris Lattner3501fea2003-01-14 22:00:31 +0000442static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000443 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000444 default: assert(0 && "Unknown arg size!");
445 case X86II::Arg8: return "BYTE PTR";
446 case X86II::Arg16: return "WORD PTR";
447 case X86II::Arg32: return "DWORD PTR";
448 case X86II::Arg64: return "QWORD PTR";
449 case X86II::ArgF32: return "DWORD PTR";
450 case X86II::ArgF64: return "QWORD PTR";
451 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000452 }
453}
454
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000455void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000456 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000457
458 if (MI->getOperand(Op).isFrameIndex()) {
459 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
460 if (MI->getOperand(Op+3).getImmedValue())
461 O << " + " << MI->getOperand(Op+3).getImmedValue();
462 O << "]";
463 return;
464 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000465 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000466 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000467 if (MI->getOperand(Op+3).getImmedValue())
468 O << " + " << MI->getOperand(Op+3).getImmedValue();
469 O << "]";
470 return;
471 }
472
Chris Lattner3d3067b2002-11-21 20:44:15 +0000473 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000474 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000475 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000476 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000477
478 O << "[";
479 bool NeedPlus = false;
480 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000481 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000482 NeedPlus = true;
483 }
484
485 if (IndexReg.getReg()) {
486 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000487 if (ScaleVal != 1)
488 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000489 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000490 NeedPlus = true;
491 }
492
Chris Lattner0285a332002-12-28 20:25:38 +0000493 if (DispVal) {
494 if (NeedPlus)
495 if (DispVal > 0)
496 O << " + ";
497 else {
498 O << " - ";
499 DispVal = -DispVal;
500 }
501 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000502 }
503 O << "]";
504}
505
Brian Gaeke2a098772003-08-11 19:05:46 +0000506/// checkImplUses - Emit the implicit-use registers for the
507/// instruction described by DESC, if its PrintImplUses flag is set.
508///
509void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
510 const MRegisterInfo &RI = *TM.getRegisterInfo();
511 if (Desc.TSFlags & X86II::PrintImplUses) {
512 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
513 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000514 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000515 }
516 }
517}
518
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000519/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000520/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000521///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000522void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000523 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000524 const TargetInstrInfo &TII = TM.getInstrInfo();
525 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000526
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000527 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000528 switch (Desc.TSFlags & X86II::FormMask) {
529 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000530 // Print pseudo-instructions as comments; either they should have been
531 // turned into real instructions by now, or they don't need to be
532 // seen by the assembler (e.g., IMPLICIT_USEs.)
533 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000534 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000535 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000536 O << " = phi ";
537 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
538 if (i != 1) O << ", ";
539 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000540 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000541 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000542 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000543 O << "]";
544 }
545 } else {
546 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000547 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
548 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000549 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000550 O << " = ";
551 ++i;
552 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000553 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000554
555 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
556 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000557 if (MI->getOperand(i).opIsDefOnly() ||
558 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000559 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000560 if (MI->getOperand(i).opIsDefOnly() ||
561 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000562 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000563 }
564 O << "\n";
565 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000566
Chris Lattnerf9f60882002-11-18 06:56:51 +0000567 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000568 // The accepted forms of Raw instructions are:
569 // 1. nop - No operand required
570 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000571 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000572 //
573 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000574 (MI->getNumOperands() == 1 &&
575 (MI->getOperand(0).isPCRelativeDisp() ||
576 MI->getOperand(0).isGlobalAddress() ||
577 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000578 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000579 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000580
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000581 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000582 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000583 }
584 O << "\n";
585 return;
586
Chris Lattner77875d82002-11-21 02:00:20 +0000587 case X86II::AddRegFrm: {
588 // There are currently two forms of acceptable AddRegFrm instructions.
589 // Either the instruction JUST takes a single register (like inc, dec, etc),
590 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000591 // (move immediate f.e.). Note that this immediate value might be stored as
592 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000593 // into a register. The initial register might be duplicated if this is a
594 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000595 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000596 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000597 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000598 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000599 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000600 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000601 MI->getOperand(1).isRegister() ||
602 MI->getOperand(1).isGlobalAddress() ||
603 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000604 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000605
Chris Lattner77875d82002-11-21 02:00:20 +0000606 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000607
Brian Gaeked7908f62003-06-27 00:00:48 +0000608 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000609 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000610 if (MI->getNumOperands() == 2 &&
611 (!MI->getOperand(1).isRegister() ||
612 MI->getOperand(1).getVRegValueOrNull() ||
613 MI->getOperand(1).isGlobalAddress() ||
614 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000615 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000616 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000617 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000618 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000619 O << "\n";
620 return;
621 }
Chris Lattner233ad712002-11-21 01:33:44 +0000622 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000623 // There are two acceptable forms of MRMDestReg instructions, those with 2,
624 // 3 and 4 operands:
625 //
626 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000627 //
628 // 3 Operands: in this form, the first two registers (the destination, and
629 // the first operand) should be the same, post register allocation. The 3rd
630 // operand is an additional input. This should be for things like add
631 // instructions.
632 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000633 // 4 Operands: This form is for instructions which are 3 operands forms, but
634 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000635 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000636 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000637 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000638 (MI->getNumOperands() == 2 ||
639 (isTwoAddr && MI->getOperand(1).isRegister() &&
640 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
641 (MI->getNumOperands() == 3 ||
642 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000643 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000644
Brian Gaeked7908f62003-06-27 00:00:48 +0000645 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000646 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000647 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000648 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000649 if (MI->getNumOperands() == 4) {
650 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000651 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000652 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000653 O << "\n";
654 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000655 }
Chris Lattner18042332002-11-21 21:03:39 +0000656
657 case X86II::MRMDestMem: {
658 // These instructions are the same as MRMDestReg, but instead of having a
659 // register reference for the mod/rm field, it's a memory reference.
660 //
661 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000662 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000663
Brian Gaeked7908f62003-06-27 00:00:48 +0000664 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000665 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000666 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000667 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000668 O << "\n";
669 return;
670 }
671
Chris Lattner233ad712002-11-21 01:33:44 +0000672 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000673 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000674 // those with 3 and 2 operands:
675 //
676 // 3 Operands: in this form, the last register (the second input) is the
677 // ModR/M input. The first two operands should be the same, post register
678 // allocation. This is for things like: add r32, r/m32
679 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000680 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
681 // instructions like the IMULri instructions.
682 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000683 // 2 Operands: this is for things like mov that do not read a second input
684 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000685 assert(MI->getOperand(0).isRegister() &&
686 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000687 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000688 (MI->getNumOperands() == 3 &&
689 (MI->getOperand(2).isRegister() ||
690 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000691 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000692 if (MI->getNumOperands() == 3 &&
693 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
694 O << "**";
695
Brian Gaeked7908f62003-06-27 00:00:48 +0000696 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000697 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000698
699 // If this is IMULri* instructions, print the non-two-address operand.
700 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
701 O << ", ";
702 printOp(MI->getOperand(1));
703 }
704
Chris Lattner644e1ab2002-11-21 00:30:01 +0000705 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000706 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000707 O << "\n";
708 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000709 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000710
Chris Lattner3d3067b2002-11-21 20:44:15 +0000711 case X86II::MRMSrcMem: {
712 // These instructions are the same as MRMSrcReg, but instead of having a
713 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000714 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000715 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000716 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000717 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000718 isMem(MI, 2))
719 && "Bad format for MRMDestReg!");
720 if (MI->getNumOperands() == 2+4 &&
721 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
722 O << "**";
723
Brian Gaeked7908f62003-06-27 00:00:48 +0000724 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000725 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000726 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000727 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000728 O << "\n";
729 return;
730 }
731
Chris Lattner675dd2c2002-11-21 17:09:01 +0000732 case X86II::MRMS0r: case X86II::MRMS1r:
733 case X86II::MRMS2r: case X86II::MRMS3r:
734 case X86II::MRMS4r: case X86II::MRMS5r:
735 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000736 // In this form, the following are valid formats:
737 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000738 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000739 // 2. shl rdest, rinput <implicit CL or 1>
740 // 3. sbb rdest, rinput, immediate [rdest = rinput]
741 //
742 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000743 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000744 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000745 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000746 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000747 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000748 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000749 "Bad MRMSxR format!");
750
Chris Lattnerd9096832002-12-15 08:01:39 +0000751 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000752 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
753 O << "**";
754
Brian Gaeked7908f62003-06-27 00:00:48 +0000755 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000756 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000757 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000758 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000759 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000760 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000761 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000762 O << "\n";
763
764 return;
765 }
766
Chris Lattnerb7089442003-01-13 00:35:03 +0000767 case X86II::MRMS0m: case X86II::MRMS1m:
768 case X86II::MRMS2m: case X86II::MRMS3m:
769 case X86II::MRMS4m: case X86II::MRMS5m:
770 case X86II::MRMS6m: case X86II::MRMS7m: {
771 // In this form, the following are valid formats:
772 // 1. sete [m]
773 // 2. cmp [m], immediate
774 // 2. shl [m], rinput <implicit CL or 1>
775 // 3. sbb [m], immediate
776 //
777 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
778 isMem(MI, 0) && "Bad MRMSxM format!");
779 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
780 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000781 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
782 // is misassembled by gas in intel_syntax mode as its 32-bit
783 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
784 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000785 if (MI->getOpCode() == X86::FSTPr80) {
786 if ((MI->getOperand(0).getReg() == X86::ESP)
787 && (MI->getOperand(1).getImmedValue() == 1)) {
788 int DispVal = MI->getOperand(3).getImmedValue();
789 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
790 unsigned int val = (unsigned int) DispVal;
791 O << ".byte 0xdb, 0xbc, 0x24\n\t";
792 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
793 } else { // 1 byte disp.
794 unsigned char val = (unsigned char) DispVal;
795 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
796 << std::dec << "\t# ";
797 }
798 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000799 }
800 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
801 // misassembled by gas in intel_syntax mode as its 32-bit
802 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
803 // opcode bytes instead of the instruction.
804 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000805 if ((MI->getOperand(0).getReg() == X86::ESP)
806 && (MI->getOperand(1).getImmedValue() == 1)) {
807 int DispVal = MI->getOperand(3).getImmedValue();
808 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
809 unsigned int val = (unsigned int) DispVal;
810 O << ".byte 0xdb, 0xac, 0x24\n\t";
811 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
812 } else { // 1 byte disp.
813 unsigned char val = (unsigned char) DispVal;
814 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
815 << std::dec << "\t# ";
816 }
817 }
818 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000819 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
820 // invalid opcode, saying "64 bit operations are only supported in
821 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
822 // [...]", which is wrong. Workaround: Output the raw opcode bytes
823 // instead of the instruction.
824 if (MI->getOpCode() == X86::FILDr64) {
825 if ((MI->getOperand(0).getReg() == X86::ESP)
826 && (MI->getOperand(1).getImmedValue() == 1)) {
827 int DispVal = MI->getOperand(3).getImmedValue();
828 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
829 unsigned int val = (unsigned int) DispVal;
830 O << ".byte 0xdf, 0xac, 0x24\n\t";
831 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
832 } else { // 1 byte disp.
833 unsigned char val = (unsigned char) DispVal;
834 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
835 << std::dec << "\t# ";
836 }
837 }
838 }
839 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
840 // an invalid opcode, saying "64 bit operations are only
841 // supported in 64 bit modes." libopcodes disassembles it as
842 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
843 // "fistpll DWORD PTR " instead, which is what libopcodes is
844 // expecting to see.
845 if (MI->getOpCode() == X86::FISTPr64) {
846 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000847 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000848 if (MI->getNumOperands() == 5) {
849 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000850 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000851 }
852 O << "\t# ";
853 }
854
Brian Gaeked7908f62003-06-27 00:00:48 +0000855 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000856 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000857 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000858 if (MI->getNumOperands() == 5) {
859 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000860 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000861 }
862 O << "\n";
863 return;
864 }
865
Chris Lattnerf9f60882002-11-18 06:56:51 +0000866 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000867 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000868 }
Chris Lattner72614082002-10-25 22:55:53 +0000869}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000870
Chris Lattner93c1afa2003-08-11 19:35:26 +0000871bool Printer::doInitialization(Module &M) {
872 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000873 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000874 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
875 // instruction as a reference to the register named sp, and if you try to
876 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
877 // before being looked up in the symbol table. This creates spurious
878 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
879 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000880 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000881 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000882 return false; // success
883}
884
Chris Lattnerad200712003-09-09 16:23:36 +0000885// SwitchSection - Switch to the specified section of the executable if we are
886// not already in it!
887//
888static void SwitchSection(std::ostream &OS, std::string &CurSection,
889 const char *NewSection) {
890 if (CurSection != NewSection) {
891 CurSection = NewSection;
892 if (!CurSection.empty())
893 OS << "\t" << NewSection << "\n";
894 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000895}
896
Chris Lattnerad200712003-09-09 16:23:36 +0000897bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000898 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000899 std::string CurSection;
900
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000901 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000902 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
903 if (I->hasInitializer()) { // External global require no code
904 O << "\n\n";
905 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000906 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000907 unsigned Size = TD.getTypeSize(C->getType());
908 unsigned Align = TD.getTypeAlignment(C->getType());
909
910 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000911 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
912 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000913 SwitchSection(O, CurSection, ".data");
914 if (I->hasInternalLinkage())
915 O << "\t.local " << name << "\n";
916
917 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000918 << "," << (unsigned)TD.getTypeAlignment(C->getType());
919 O << "\t\t# ";
920 WriteAsOperand(O, I, true, true, &M);
921 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000922 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000923 switch (I->getLinkage()) {
924 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000925 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000926 // Nonnull linkonce -> weak
927 O << "\t.weak " << name << "\n";
928 SwitchSection(O, CurSection, "");
929 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
930 break;
931
932 case GlobalValue::AppendingLinkage:
933 // FIXME: appending linkage variables should go into a section of
934 // their name or something. For now, just emit them as external.
935 case GlobalValue::ExternalLinkage:
936 // If external or appending, declare as a global symbol
937 O << "\t.globl " << name << "\n";
938 // FALL THROUGH
939 case GlobalValue::InternalLinkage:
940 if (C->isNullValue())
941 SwitchSection(O, CurSection, ".bss");
942 else
943 SwitchSection(O, CurSection, ".data");
944 break;
945 }
946
947 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000948 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000949 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000950 O << name << ":\t\t\t\t# ";
951 WriteAsOperand(O, I, true, true, &M);
952 O << " = ";
953 WriteAsOperand(O, C, false, false, &M);
954 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000955 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000956 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000957 }
Chris Lattnerad200712003-09-09 16:23:36 +0000958
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000959 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000960 return false; // success
961}