blob: d71715aa61693a6785a42fe8b0a99650f3350f82 [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 Lattner300d0ed2004-02-14 06:00:36 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000033namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000034 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
Chris Lattner93c1afa2003-08-11 19:35:26 +000036 // FIXME: This should be automatically picked up by autoconf from the C
37 // frontend
38 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
39 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
40
Chris Lattner0285a332002-12-28 20:25:38 +000041 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000042 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000043 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000044 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000045
46 /// Target machine description which we query for reg. names, data
47 /// layout, etc.
48 ///
49 TargetMachine &TM;
50
Brian Gaeked9fb37a2003-07-24 20:20:44 +000051 /// Name-mangler for global names.
52 ///
53 Mangler *Mang;
54
Brian Gaekede420ae2003-07-23 20:25:08 +000055 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000056
57 /// We name each basic block in a Function with a unique number, so
58 /// that we can consistently refer to them later. This is cleared
59 /// at the beginning of each call to runOnMachineFunction().
60 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000061 typedef std::map<const Value *, unsigned> ValueMapTy;
62 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000063
64 /// Cache of mangled name for current function. This is
65 /// recalculated at the beginning of each call to
66 /// runOnMachineFunction().
67 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000068 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000069
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000070 virtual const char *getPassName() const {
71 return "X86 Assembly Printer";
72 }
73
Brian Gaeke2a098772003-08-11 19:05:46 +000074 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000075 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000076 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000077 bool elideOffsetKeyword = false);
78 void printMemReference(const MachineInstr *MI, unsigned Op);
79 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000080 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +000081 bool doInitialization(Module &M);
82 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +000083 void emitGlobalConstant(const Constant* CV);
84 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000085 };
Brian Gaeked7908f62003-06-27 00:00:48 +000086} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000087
Brian Gaeke92bdfe62003-07-23 18:37:06 +000088/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000089/// assembly code for a MachineFunction to the given output stream,
90/// using the given target machine description. This should work
91/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000092///
Chris Lattner300d0ed2004-02-14 06:00:36 +000093FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000094 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000095}
96
Brian Gaeke92bdfe62003-07-23 18:37:06 +000097/// toOctal - Convert the low order bits of X into an octal digit.
98///
Brian Gaeke01d79ff2003-06-25 18:01:07 +000099static inline char toOctal(int X) {
100 return (X&7)+'0';
101}
102
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000103/// getAsCString - Return the specified array as a C compatible
104/// string, only if the predicate isStringCompatible is true.
105///
Chris Lattnerac662d12003-11-03 20:19:49 +0000106static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000107 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000108
Chris Lattnerac662d12003-11-03 20:19:49 +0000109 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000110 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000111 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000112
113 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000114 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000115 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000116 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000117 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000118 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000119 } else {
120 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000121 case '\b': O << "\\b"; break;
122 case '\f': O << "\\f"; break;
123 case '\n': O << "\\n"; break;
124 case '\r': O << "\\r"; break;
125 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000127 O << '\\';
128 O << toOctal(C >> 6);
129 O << toOctal(C >> 3);
130 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 break;
132 }
133 }
134 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000135 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000136}
137
Chris Lattnerac662d12003-11-03 20:19:49 +0000138// Print out the specified constant, without a storage class. Only the
139// constants valid in constant expressions can occur here.
140void Printer::emitConstantValueOnly(const Constant *CV) {
141 if (CV->isNullValue())
142 O << "0";
143 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
144 assert(CB == ConstantBool::True);
145 O << "1";
146 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000147 if (((CI->getValue() << 32) >> 32) == CI->getValue())
148 O << CI->getValue();
149 else
150 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000151 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
152 O << CI->getValue();
153 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
154 // This is a constant address for a global variable or function. Use the
155 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000156 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000157 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
158 const TargetData &TD = TM.getTargetData();
159 switch(CE->getOpcode()) {
160 case Instruction::GetElementPtr: {
161 // generate a symbolic expression for the byte address
162 const Constant *ptrVal = CE->getOperand(0);
163 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
164 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
165 O << "(";
166 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000167 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000168 } else {
169 emitConstantValueOnly(ptrVal);
170 }
171 break;
172 }
173 case Instruction::Cast: {
174 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000175 // that do not involve a change in value. This assertion is really gross,
176 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000177 Constant *Op = CE->getOperand(0);
178 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000179
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000180 // Remember, kids, pointers on x86 can be losslessly converted back and
181 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000182 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000183 && (Ty == Type::LongTy || Ty == Type::ULongTy
184 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000185 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000186 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
187 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000188 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
189 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000190 && "FIXME: Don't yet support this kind of constant cast expr");
191 O << "(";
192 emitConstantValueOnly(Op);
193 O << ")";
194 break;
195 }
196 case Instruction::Add:
197 O << "(";
198 emitConstantValueOnly(CE->getOperand(0));
199 O << ") + (";
200 emitConstantValueOnly(CE->getOperand(1));
201 O << ")";
202 break;
203 default:
204 assert(0 && "Unsupported operator!");
205 }
206 } else {
207 assert(0 && "Unknown constant value!");
208 }
209}
210
211// Print a constant value or values, with the appropriate storage class as a
212// prefix.
213void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000214 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000215
Chris Lattnerad200712003-09-09 16:23:36 +0000216 if (CV->isNullValue()) {
217 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000218 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000219 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000220 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000221 O << "\t.ascii\t";
222 printAsCString(O, CVA);
223 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000224 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000225 const std::vector<Use> &constValues = CVA->getValues();
226 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000227 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000228 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000229 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000230 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
231 // Print the fields in successive locations. Pad to align if needed!
232 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
233 const std::vector<Use>& constValues = CVS->getValues();
234 unsigned sizeSoFar = 0;
235 for (unsigned i=0, N = constValues.size(); i < N; i++) {
236 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000237
Chris Lattnerad200712003-09-09 16:23:36 +0000238 // Check if padding is needed and insert one or more 0s.
239 unsigned fieldSize = TD.getTypeSize(field->getType());
240 unsigned padSize = ((i == N-1? cvsLayout->StructSize
241 : cvsLayout->MemberOffsets[i+1])
242 - cvsLayout->MemberOffsets[i]) - fieldSize;
243 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000244
Chris Lattnerad200712003-09-09 16:23:36 +0000245 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000246 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000247
248 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000249 if (padSize)
250 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000251 }
Chris Lattnerad200712003-09-09 16:23:36 +0000252 assert(sizeSoFar == cvsLayout->StructSize &&
253 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000254 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000255 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
256 // FP Constants are printed as integer constants to avoid losing
257 // precision...
258 double Val = CFP->getValue();
259 switch (CFP->getType()->getPrimitiveID()) {
260 default: assert(0 && "Unknown floating point type!");
261 case Type::FloatTyID: {
262 union FU { // Abide by C TBAA rules
263 float FVal;
264 unsigned UVal;
265 } U;
266 U.FVal = Val;
267 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
268 return;
269 }
270 case Type::DoubleTyID: {
271 union DU { // Abide by C TBAA rules
272 double FVal;
273 uint64_t UVal;
274 } U;
275 U.FVal = Val;
276 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
277 return;
278 }
279 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000280 }
281
282 const Type *type = CV->getType();
283 O << "\t";
284 switch (type->getPrimitiveID()) {
285 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
286 O << ".byte";
287 break;
288 case Type::UShortTyID: case Type::ShortTyID:
289 O << ".word";
290 break;
291 case Type::FloatTyID: case Type::PointerTyID:
292 case Type::UIntTyID: case Type::IntTyID:
293 O << ".long";
294 break;
295 case Type::DoubleTyID:
296 case Type::ULongTyID: case Type::LongTyID:
297 O << ".quad";
298 break;
299 default:
300 assert (0 && "Can't handle printing this type of thing");
301 break;
302 }
303 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000304 emitConstantValueOnly(CV);
305 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000306}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000307
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000308/// printConstantPool - Print to the current output stream assembly
309/// representations of the constants in the constant pool MCP. This is
310/// used to print out constants which have been "spilled to memory" by
311/// the code generator.
312///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000313void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000314 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000315 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000316
Chris Lattnerb7089442003-01-13 00:35:03 +0000317 if (CP.empty()) return;
318
319 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
320 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000321 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000322 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000323 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
324 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000325 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000326 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000327}
328
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000329/// runOnMachineFunction - This uses the printMachineInstruction()
330/// method to print assembly for each instruction.
331///
Chris Lattner0285a332002-12-28 20:25:38 +0000332bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000333 // BBNumber is used here so that a given Printer will never give two
334 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000335 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000336
Chris Lattnere0121322003-08-03 23:37:09 +0000337 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000338 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000339 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000340
Chris Lattnerb7089442003-01-13 00:35:03 +0000341 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000342 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000343
Brian Gaeke6559bb92002-11-14 22:32:30 +0000344 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000345 O << "\t.text\n";
346 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000347 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000348 if (!EmitCygwin)
349 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000350 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000351
Brian Gaeked7908f62003-06-27 00:00:48 +0000352 // Number each basic block so that we can consistently refer to them
353 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000354 NumberForBB.clear();
355 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
356 I != E; ++I) {
357 NumberForBB[I->getBasicBlock()] = BBNumber++;
358 }
359
Brian Gaeke6559bb92002-11-14 22:32:30 +0000360 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000361 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
362 I != E; ++I) {
363 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000364 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000365 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000366 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
367 II != E; ++II) {
368 // Print the assembly for the instruction.
369 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000370 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000371 }
Chris Lattner0285a332002-12-28 20:25:38 +0000372 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000373
374 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000375 return false;
376}
377
Chris Lattner3d3067b2002-11-21 20:44:15 +0000378static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000379 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000380 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
381 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000382}
383
384static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000385 if (MI->getOperand(Op).isFrameIndex()) return true;
386 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000387 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000388 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
389 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000390}
391
Brian Gaeke2a098772003-08-11 19:05:46 +0000392
393
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000394void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000395 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000396 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000397 switch (MO.getType()) {
398 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000399 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000400 O << "<" << V->getName() << ">";
401 return;
402 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000403 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000404 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000405 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000406 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000407 O << "%" << RI.get(MO.getReg()).Name;
408 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000409 O << "%reg" << MO.getReg();
410 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000411
412 case MachineOperand::MO_SignExtendedImmed:
413 case MachineOperand::MO_UnextendedImmed:
414 O << (int)MO.getImmedValue();
415 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000416 case MachineOperand::MO_PCRelativeDisp: {
417 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
418 assert (i != NumberForBB.end()
419 && "Could not find a BB in the NumberForBB map!");
420 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000421 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000422 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000423 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000424 if (!elideOffsetKeyword)
425 O << "OFFSET ";
426 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000427 return;
428 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000429 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000430 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000431 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000432 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000433 }
434}
435
Chris Lattner3501fea2003-01-14 22:00:31 +0000436static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000437 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000438 default: assert(0 && "Unknown arg size!");
439 case X86II::Arg8: return "BYTE PTR";
440 case X86II::Arg16: return "WORD PTR";
441 case X86II::Arg32: return "DWORD PTR";
442 case X86II::Arg64: return "QWORD PTR";
443 case X86II::ArgF32: return "DWORD PTR";
444 case X86II::ArgF64: return "QWORD PTR";
445 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000446 }
447}
448
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000449void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000450 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000451
452 if (MI->getOperand(Op).isFrameIndex()) {
453 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
454 if (MI->getOperand(Op+3).getImmedValue())
455 O << " + " << MI->getOperand(Op+3).getImmedValue();
456 O << "]";
457 return;
458 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000459 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000460 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 if (MI->getOperand(Op+3).getImmedValue())
462 O << " + " << MI->getOperand(Op+3).getImmedValue();
463 O << "]";
464 return;
465 }
466
Chris Lattner3d3067b2002-11-21 20:44:15 +0000467 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000468 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000469 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000470 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000471
472 O << "[";
473 bool NeedPlus = false;
474 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000475 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000476 NeedPlus = true;
477 }
478
479 if (IndexReg.getReg()) {
480 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000481 if (ScaleVal != 1)
482 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000483 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000484 NeedPlus = true;
485 }
486
Chris Lattner0285a332002-12-28 20:25:38 +0000487 if (DispVal) {
488 if (NeedPlus)
489 if (DispVal > 0)
490 O << " + ";
491 else {
492 O << " - ";
493 DispVal = -DispVal;
494 }
495 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000496 }
497 O << "]";
498}
499
Brian Gaeke2a098772003-08-11 19:05:46 +0000500/// checkImplUses - Emit the implicit-use registers for the
501/// instruction described by DESC, if its PrintImplUses flag is set.
502///
503void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
504 const MRegisterInfo &RI = *TM.getRegisterInfo();
505 if (Desc.TSFlags & X86II::PrintImplUses) {
506 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
507 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000508 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000509 }
510 }
511}
512
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000513/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000514/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000515///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000516void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000517 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000518 const TargetInstrInfo &TII = TM.getInstrInfo();
519 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000520
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000521 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000522 switch (Desc.TSFlags & X86II::FormMask) {
523 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000524 // Print pseudo-instructions as comments; either they should have been
525 // turned into real instructions by now, or they don't need to be
526 // seen by the assembler (e.g., IMPLICIT_USEs.)
527 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000528 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000529 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000530 O << " = phi ";
531 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
532 if (i != 1) O << ", ";
533 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000534 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000535 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000536 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000537 O << "]";
538 }
539 } else {
540 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000541 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000542 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000543 O << " = ";
544 ++i;
545 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000546 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000547
548 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
549 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000550 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000551 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000552 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000553 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000554 }
555 O << "\n";
556 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000557
Chris Lattnerf9f60882002-11-18 06:56:51 +0000558 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000559 // The accepted forms of Raw instructions are:
560 // 1. nop - No operand required
561 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000562 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000563 //
564 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000565 (MI->getNumOperands() == 1 &&
566 (MI->getOperand(0).isPCRelativeDisp() ||
567 MI->getOperand(0).isGlobalAddress() ||
568 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000569 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000570 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000571
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000572 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000573 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000574 }
575 O << "\n";
576 return;
577
Chris Lattner77875d82002-11-21 02:00:20 +0000578 case X86II::AddRegFrm: {
579 // There are currently two forms of acceptable AddRegFrm instructions.
580 // Either the instruction JUST takes a single register (like inc, dec, etc),
581 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000582 // (move immediate f.e.). Note that this immediate value might be stored as
583 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000584 // into a register. The initial register might be duplicated if this is a
585 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000586 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000587 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000588 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000589 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000590 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000591 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000592 MI->getOperand(1).isRegister() ||
593 MI->getOperand(1).isGlobalAddress() ||
594 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000595 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000596
Chris Lattner77875d82002-11-21 02:00:20 +0000597 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000598
Chris Lattnerb009c002004-02-11 19:26:28 +0000599 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000600 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000601 if (MI->getNumOperands() == 2 &&
602 (!MI->getOperand(1).isRegister() ||
603 MI->getOperand(1).getVRegValueOrNull() ||
604 MI->getOperand(1).isGlobalAddress() ||
605 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000606 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000607 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000608 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000609 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000610 O << "\n";
611 return;
612 }
Chris Lattner233ad712002-11-21 01:33:44 +0000613 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000614 // There are three forms of MRMDestReg instructions, those with 2
615 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000616 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000617 // 2 Operands: this is for things like mov that do not read a
618 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000619 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000620 // 2 Operands: two address instructions which def&use the first
621 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000622 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000623 // 3 Operands: in this form, two address instructions are the same
624 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000625 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000626 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000627 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000628 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000629 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000630 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000631
Chris Lattnerb009c002004-02-11 19:26:28 +0000632 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000633 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000634 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000635 printOp(MI->getOperand(1));
636 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000637 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000638 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000639 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000640 O << "\n";
641 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000642 }
Chris Lattner18042332002-11-21 21:03:39 +0000643
644 case X86II::MRMDestMem: {
645 // These instructions are the same as MRMDestReg, but instead of having a
646 // register reference for the mod/rm field, it's a memory reference.
647 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000648 assert(isMem(MI, 0) &&
649 (MI->getNumOperands() == 4+1 ||
650 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
651 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000652
Chris Lattnerb009c002004-02-11 19:26:28 +0000653 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000654 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000655 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000656 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000657 if (MI->getNumOperands() == 4+2) {
658 O << ", ";
659 printOp(MI->getOperand(5));
660 }
Chris Lattner18042332002-11-21 21:03:39 +0000661 O << "\n";
662 return;
663 }
664
Chris Lattner233ad712002-11-21 01:33:44 +0000665 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000666 // There are three forms that are acceptable for MRMSrcReg
667 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000668 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000669 // 2 Operands: this is for things like mov that do not read a
670 // second input.
671 //
672 // 2 Operands: in this form, the last register is the ModR/M
673 // input. The first operand is a def&use. This is for things
674 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000675 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000676 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000677 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000678 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000679 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000680 assert(MI->getOperand(0).isRegister() &&
681 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000682 (MI->getNumOperands() == 2 ||
683 (MI->getNumOperands() == 3 &&
684 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000685 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000686
Chris Lattnerb009c002004-02-11 19:26:28 +0000687 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000688 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000689 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000690 printOp(MI->getOperand(1));
691 if (MI->getNumOperands() == 3) {
692 O << ", ";
693 printOp(MI->getOperand(2));
694 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000695 O << "\n";
696 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000697 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000698
Chris Lattner3d3067b2002-11-21 20:44:15 +0000699 case X86II::MRMSrcMem: {
700 // These instructions are the same as MRMSrcReg, but instead of having a
701 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000702 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000703 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000704 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000705(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000706 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000707 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000708 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000709 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000710 printMemReference(MI, 1);
711 if (MI->getNumOperands() == 2+4) {
712 O << ", ";
713 printOp(MI->getOperand(5));
714 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000715 O << "\n";
716 return;
717 }
718
Chris Lattner675dd2c2002-11-21 17:09:01 +0000719 case X86II::MRMS0r: case X86II::MRMS1r:
720 case X86II::MRMS2r: case X86II::MRMS3r:
721 case X86II::MRMS4r: case X86II::MRMS5r:
722 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000723 // In this form, the following are valid formats:
724 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000725 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000726 // 2. shl rdest, rinput <implicit CL or 1>
727 // 3. sbb rdest, rinput, immediate [rdest = rinput]
728 //
729 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000730 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000731 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000732 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000733 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000734 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000735 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000736 "Bad MRMSxR format!");
737
Chris Lattnerd9096832002-12-15 08:01:39 +0000738 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000739 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
740 O << "**";
741
Chris Lattnerb009c002004-02-11 19:26:28 +0000742 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000743 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000744 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000745 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000746 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000747 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000748 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000749 O << "\n";
750
751 return;
752 }
753
Chris Lattnerb7089442003-01-13 00:35:03 +0000754 case X86II::MRMS0m: case X86II::MRMS1m:
755 case X86II::MRMS2m: case X86II::MRMS3m:
756 case X86II::MRMS4m: case X86II::MRMS5m:
757 case X86II::MRMS6m: case X86II::MRMS7m: {
758 // In this form, the following are valid formats:
759 // 1. sete [m]
760 // 2. cmp [m], immediate
761 // 2. shl [m], rinput <implicit CL or 1>
762 // 3. sbb [m], immediate
763 //
764 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
765 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000766 assert((MI->getNumOperands() != 5 ||
767 (MI->getOperand(4).isImmediate() ||
768 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000769 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000770
771 const MachineOperand &Op3 = MI->getOperand(3);
772
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000773 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
774 // is misassembled by gas in intel_syntax mode as its 32-bit
775 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
776 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000777 if (MI->getOpcode() == X86::FSTPr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000778 if ((MI->getOperand(0).getReg() == X86::ESP)
779 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000780 if (Op3.isImmediate() &&
781 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
782 // 1 byte disp.
783 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
784 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
785 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000786 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000787 O << ".long ";
788 printOp(Op3);
789 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000790 }
791 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000792 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000793
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000794 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
795 // misassembled by gas in intel_syntax mode as its 32-bit
796 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
797 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000798 if (MI->getOpcode() == X86::FLDr80 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000799 MI->getOperand(0).getReg() == X86::ESP &&
800 MI->getOperand(1).getImmedValue() == 1) {
801 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
802 Op3.getImmedValue() <= 127) { // 1 byte displacement
803 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
804 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
805 } else {
806 O << ".byte 0xdb, 0xac, 0x24\n\t";
807 O << ".long ";
808 printOp(Op3);
809 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000810 }
811 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000812
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000813 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
814 // invalid opcode, saying "64 bit operations are only supported in
815 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
816 // [...]", which is wrong. Workaround: Output the raw opcode bytes
817 // instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000818 if (MI->getOpcode() == X86::FILDr64 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000819 MI->getOperand(0).getReg() == X86::ESP &&
820 MI->getOperand(1).getImmedValue() == 1) {
821 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
822 Op3.getImmedValue() <= 127) { // 1 byte displacement
823 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
824 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
825 } else {
826 O << ".byte 0xdf, 0xac, 0x24\n\t";
827 O << ".long ";
828 printOp(Op3);
829 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000830 }
831 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000832
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000833 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
834 // an invalid opcode, saying "64 bit operations are only
835 // supported in 64 bit modes." libopcodes disassembles it as
836 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
837 // "fistpll DWORD PTR " instead, which is what libopcodes is
838 // expecting to see.
Chris Lattnerb009c002004-02-11 19:26:28 +0000839 if (MI->getOpcode() == X86::FISTPr64) {
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000840 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000841 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000842 if (MI->getNumOperands() == 5) {
843 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000844 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000845 }
846 O << "\t# ";
847 }
848
Chris Lattnerb009c002004-02-11 19:26:28 +0000849 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000850 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000851 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000852 if (MI->getNumOperands() == 5) {
853 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000854 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000855 }
856 O << "\n";
857 return;
858 }
859
Chris Lattnerf9f60882002-11-18 06:56:51 +0000860 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000861 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000862 }
Chris Lattner72614082002-10-25 22:55:53 +0000863}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000864
Chris Lattner93c1afa2003-08-11 19:35:26 +0000865bool Printer::doInitialization(Module &M) {
866 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000867 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000868 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
869 // instruction as a reference to the register named sp, and if you try to
870 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
871 // before being looked up in the symbol table. This creates spurious
872 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
873 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000874 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000875 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000876 return false; // success
877}
878
Chris Lattnerad200712003-09-09 16:23:36 +0000879// SwitchSection - Switch to the specified section of the executable if we are
880// not already in it!
881//
882static void SwitchSection(std::ostream &OS, std::string &CurSection,
883 const char *NewSection) {
884 if (CurSection != NewSection) {
885 CurSection = NewSection;
886 if (!CurSection.empty())
887 OS << "\t" << NewSection << "\n";
888 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000889}
890
Chris Lattnerad200712003-09-09 16:23:36 +0000891bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000892 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000893 std::string CurSection;
894
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000895 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000896 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
897 if (I->hasInitializer()) { // External global require no code
898 O << "\n\n";
899 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000900 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000901 unsigned Size = TD.getTypeSize(C->getType());
902 unsigned Align = TD.getTypeAlignment(C->getType());
903
904 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000905 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
906 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000907 SwitchSection(O, CurSection, ".data");
908 if (I->hasInternalLinkage())
909 O << "\t.local " << name << "\n";
910
911 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000912 << "," << (unsigned)TD.getTypeAlignment(C->getType());
913 O << "\t\t# ";
914 WriteAsOperand(O, I, true, true, &M);
915 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000916 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000917 switch (I->getLinkage()) {
918 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000919 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000920 // Nonnull linkonce -> weak
921 O << "\t.weak " << name << "\n";
922 SwitchSection(O, CurSection, "");
923 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
924 break;
925
926 case GlobalValue::AppendingLinkage:
927 // FIXME: appending linkage variables should go into a section of
928 // their name or something. For now, just emit them as external.
929 case GlobalValue::ExternalLinkage:
930 // If external or appending, declare as a global symbol
931 O << "\t.globl " << name << "\n";
932 // FALL THROUGH
933 case GlobalValue::InternalLinkage:
934 if (C->isNullValue())
935 SwitchSection(O, CurSection, ".bss");
936 else
937 SwitchSection(O, CurSection, ".data");
938 break;
939 }
940
941 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000942 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000943 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000944 O << name << ":\t\t\t\t# ";
945 WriteAsOperand(O, I, true, true, &M);
946 O << " = ";
947 WriteAsOperand(O, C, false, false, &M);
948 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000949 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000950 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000951 }
Chris Lattnerad200712003-09-09 16:23:36 +0000952
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000953 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000954 return false; // success
955}