blob: f591612aea30ea58e7c63fc1643dc03f10d876f2 [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))
147 O << CI->getValue();
148 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
149 O << CI->getValue();
150 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
151 // This is a constant address for a global variable or function. Use the
152 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000153 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000154 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
155 const TargetData &TD = TM.getTargetData();
156 switch(CE->getOpcode()) {
157 case Instruction::GetElementPtr: {
158 // generate a symbolic expression for the byte address
159 const Constant *ptrVal = CE->getOperand(0);
160 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
161 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
162 O << "(";
163 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000164 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000165 } else {
166 emitConstantValueOnly(ptrVal);
167 }
168 break;
169 }
170 case Instruction::Cast: {
171 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000172 // that do not involve a change in value. This assertion is really gross,
173 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000174 Constant *Op = CE->getOperand(0);
175 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000176
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000177 // Remember, kids, pointers on x86 can be losslessly converted back and
178 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000179 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000180 && (Ty == Type::LongTy || Ty == Type::ULongTy
181 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000182 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000183 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
184 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000185 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
186 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000187 && "FIXME: Don't yet support this kind of constant cast expr");
188 O << "(";
189 emitConstantValueOnly(Op);
190 O << ")";
191 break;
192 }
193 case Instruction::Add:
194 O << "(";
195 emitConstantValueOnly(CE->getOperand(0));
196 O << ") + (";
197 emitConstantValueOnly(CE->getOperand(1));
198 O << ")";
199 break;
200 default:
201 assert(0 && "Unsupported operator!");
202 }
203 } else {
204 assert(0 && "Unknown constant value!");
205 }
206}
207
208// Print a constant value or values, with the appropriate storage class as a
209// prefix.
210void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000211 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000212
Chris Lattnerad200712003-09-09 16:23:36 +0000213 if (CV->isNullValue()) {
214 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000215 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000216 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000217 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 O << "\t.ascii\t";
219 printAsCString(O, CVA);
220 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000221 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000222 const std::vector<Use> &constValues = CVA->getValues();
223 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000224 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000225 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000226 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000227 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
228 // Print the fields in successive locations. Pad to align if needed!
229 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
230 const std::vector<Use>& constValues = CVS->getValues();
231 unsigned sizeSoFar = 0;
232 for (unsigned i=0, N = constValues.size(); i < N; i++) {
233 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000234
Chris Lattnerad200712003-09-09 16:23:36 +0000235 // Check if padding is needed and insert one or more 0s.
236 unsigned fieldSize = TD.getTypeSize(field->getType());
237 unsigned padSize = ((i == N-1? cvsLayout->StructSize
238 : cvsLayout->MemberOffsets[i+1])
239 - cvsLayout->MemberOffsets[i]) - fieldSize;
240 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000241
Chris Lattnerad200712003-09-09 16:23:36 +0000242 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000243 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000244
245 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000246 if (padSize)
247 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248 }
Chris Lattnerad200712003-09-09 16:23:36 +0000249 assert(sizeSoFar == cvsLayout->StructSize &&
250 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000251 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000252 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
253 // FP Constants are printed as integer constants to avoid losing
254 // precision...
255 double Val = CFP->getValue();
256 switch (CFP->getType()->getPrimitiveID()) {
257 default: assert(0 && "Unknown floating point type!");
258 case Type::FloatTyID: {
259 union FU { // Abide by C TBAA rules
260 float FVal;
261 unsigned UVal;
262 } U;
263 U.FVal = Val;
264 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
265 return;
266 }
267 case Type::DoubleTyID: {
268 union DU { // Abide by C TBAA rules
269 double FVal;
270 uint64_t UVal;
271 } U;
272 U.FVal = Val;
273 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
274 return;
275 }
276 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000277 }
278
279 const Type *type = CV->getType();
280 O << "\t";
281 switch (type->getPrimitiveID()) {
282 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
283 O << ".byte";
284 break;
285 case Type::UShortTyID: case Type::ShortTyID:
286 O << ".word";
287 break;
288 case Type::FloatTyID: case Type::PointerTyID:
289 case Type::UIntTyID: case Type::IntTyID:
290 O << ".long";
291 break;
292 case Type::DoubleTyID:
293 case Type::ULongTyID: case Type::LongTyID:
294 O << ".quad";
295 break;
296 default:
297 assert (0 && "Can't handle printing this type of thing");
298 break;
299 }
300 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000301 emitConstantValueOnly(CV);
302 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000303}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000304
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000305/// printConstantPool - Print to the current output stream assembly
306/// representations of the constants in the constant pool MCP. This is
307/// used to print out constants which have been "spilled to memory" by
308/// the code generator.
309///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000310void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000311 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000312 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000313
Chris Lattnerb7089442003-01-13 00:35:03 +0000314 if (CP.empty()) return;
315
316 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
317 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000318 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000319 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000320 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
321 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000322 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000323 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000324}
325
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000326/// runOnMachineFunction - This uses the printMachineInstruction()
327/// method to print assembly for each instruction.
328///
Chris Lattner0285a332002-12-28 20:25:38 +0000329bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000330 // BBNumber is used here so that a given Printer will never give two
331 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000332 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000333
Chris Lattnere0121322003-08-03 23:37:09 +0000334 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000335 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000336 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000337
Chris Lattnerb7089442003-01-13 00:35:03 +0000338 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000339 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000340
Brian Gaeke6559bb92002-11-14 22:32:30 +0000341 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000342 O << "\t.text\n";
343 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000344 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000345 if (!EmitCygwin)
346 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000347 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000348
Brian Gaeked7908f62003-06-27 00:00:48 +0000349 // Number each basic block so that we can consistently refer to them
350 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000351 NumberForBB.clear();
352 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
353 I != E; ++I) {
354 NumberForBB[I->getBasicBlock()] = BBNumber++;
355 }
356
Brian Gaeke6559bb92002-11-14 22:32:30 +0000357 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000358 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
359 I != E; ++I) {
360 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000361 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000362 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000363 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
364 II != E; ++II) {
365 // Print the assembly for the instruction.
366 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000367 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000368 }
Chris Lattner0285a332002-12-28 20:25:38 +0000369 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000370
371 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000372 return false;
373}
374
Chris Lattner3d3067b2002-11-21 20:44:15 +0000375static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000376 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000377 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
378 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000379}
380
381static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000382 if (MI->getOperand(Op).isFrameIndex()) return true;
383 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000384 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000385 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
386 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000387}
388
Brian Gaeke2a098772003-08-11 19:05:46 +0000389
390
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000391void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000392 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000393 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000394 switch (MO.getType()) {
395 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000396 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000397 O << "<" << V->getName() << ">";
398 return;
399 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000400 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000401 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000402 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000403 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000404 O << "%" << RI.get(MO.getReg()).Name;
405 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000406 O << "%reg" << MO.getReg();
407 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000408
409 case MachineOperand::MO_SignExtendedImmed:
410 case MachineOperand::MO_UnextendedImmed:
411 O << (int)MO.getImmedValue();
412 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000413 case MachineOperand::MO_PCRelativeDisp: {
414 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
415 assert (i != NumberForBB.end()
416 && "Could not find a BB in the NumberForBB map!");
417 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000418 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000419 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000420 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000421 if (!elideOffsetKeyword)
422 O << "OFFSET ";
423 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000424 return;
425 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000426 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000427 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000428 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000429 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000430 }
431}
432
Chris Lattner3501fea2003-01-14 22:00:31 +0000433static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000434 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000435 default: assert(0 && "Unknown arg size!");
436 case X86II::Arg8: return "BYTE PTR";
437 case X86II::Arg16: return "WORD PTR";
438 case X86II::Arg32: return "DWORD PTR";
439 case X86II::Arg64: return "QWORD PTR";
440 case X86II::ArgF32: return "DWORD PTR";
441 case X86II::ArgF64: return "QWORD PTR";
442 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000443 }
444}
445
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000446void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000447 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000448
449 if (MI->getOperand(Op).isFrameIndex()) {
450 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
451 if (MI->getOperand(Op+3).getImmedValue())
452 O << " + " << MI->getOperand(Op+3).getImmedValue();
453 O << "]";
454 return;
455 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000456 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000457 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000458 if (MI->getOperand(Op+3).getImmedValue())
459 O << " + " << MI->getOperand(Op+3).getImmedValue();
460 O << "]";
461 return;
462 }
463
Chris Lattner3d3067b2002-11-21 20:44:15 +0000464 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000465 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000466 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000467 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000468
469 O << "[";
470 bool NeedPlus = false;
471 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000472 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000473 NeedPlus = true;
474 }
475
476 if (IndexReg.getReg()) {
477 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000478 if (ScaleVal != 1)
479 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000480 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000481 NeedPlus = true;
482 }
483
Chris Lattner0285a332002-12-28 20:25:38 +0000484 if (DispVal) {
485 if (NeedPlus)
486 if (DispVal > 0)
487 O << " + ";
488 else {
489 O << " - ";
490 DispVal = -DispVal;
491 }
492 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000493 }
494 O << "]";
495}
496
Brian Gaeke2a098772003-08-11 19:05:46 +0000497/// checkImplUses - Emit the implicit-use registers for the
498/// instruction described by DESC, if its PrintImplUses flag is set.
499///
500void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
501 const MRegisterInfo &RI = *TM.getRegisterInfo();
502 if (Desc.TSFlags & X86II::PrintImplUses) {
503 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
504 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000505 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000506 }
507 }
508}
509
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000510/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000511/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000512///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000513void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000514 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000515 const TargetInstrInfo &TII = TM.getInstrInfo();
516 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000517
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000518 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000519 switch (Desc.TSFlags & X86II::FormMask) {
520 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000521 // Print pseudo-instructions as comments; either they should have been
522 // turned into real instructions by now, or they don't need to be
523 // seen by the assembler (e.g., IMPLICIT_USEs.)
524 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000525 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000526 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000527 O << " = phi ";
528 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
529 if (i != 1) O << ", ";
530 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000531 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000532 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000533 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000534 O << "]";
535 }
536 } else {
537 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000538 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000539 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000540 O << " = ";
541 ++i;
542 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000543 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000544
545 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
546 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000547 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000548 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000549 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000550 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000551 }
552 O << "\n";
553 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000554
Chris Lattnerf9f60882002-11-18 06:56:51 +0000555 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000556 // The accepted forms of Raw instructions are:
557 // 1. nop - No operand required
558 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000559 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000560 //
561 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000562 (MI->getNumOperands() == 1 &&
563 (MI->getOperand(0).isPCRelativeDisp() ||
564 MI->getOperand(0).isGlobalAddress() ||
565 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000566 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000567 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000568
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000569 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000570 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000571 }
572 O << "\n";
573 return;
574
Chris Lattner77875d82002-11-21 02:00:20 +0000575 case X86II::AddRegFrm: {
576 // There are currently two forms of acceptable AddRegFrm instructions.
577 // Either the instruction JUST takes a single register (like inc, dec, etc),
578 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000579 // (move immediate f.e.). Note that this immediate value might be stored as
580 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000581 // into a register. The initial register might be duplicated if this is a
582 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000583 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000584 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000585 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000586 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000587 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000588 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000589 MI->getOperand(1).isRegister() ||
590 MI->getOperand(1).isGlobalAddress() ||
591 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000592 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000593
Chris Lattner77875d82002-11-21 02:00:20 +0000594 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000595
Chris Lattnerb009c002004-02-11 19:26:28 +0000596 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000597 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000598 if (MI->getNumOperands() == 2 &&
599 (!MI->getOperand(1).isRegister() ||
600 MI->getOperand(1).getVRegValueOrNull() ||
601 MI->getOperand(1).isGlobalAddress() ||
602 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000603 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000604 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000605 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000606 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000607 O << "\n";
608 return;
609 }
Chris Lattner233ad712002-11-21 01:33:44 +0000610 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000611 // There are three forms of MRMDestReg instructions, those with 2
612 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000613 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000614 // 2 Operands: this is for things like mov that do not read a
615 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000616 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000617 // 2 Operands: two address instructions which def&use the first
618 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000619 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000620 // 3 Operands: in this form, two address instructions are the same
621 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000622 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000623 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000624 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000625 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000626 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000627 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000628
Chris Lattnerb009c002004-02-11 19:26:28 +0000629 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000630 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000631 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000632 printOp(MI->getOperand(1));
633 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000634 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000635 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000636 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000637 O << "\n";
638 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000639 }
Chris Lattner18042332002-11-21 21:03:39 +0000640
641 case X86II::MRMDestMem: {
642 // These instructions are the same as MRMDestReg, but instead of having a
643 // register reference for the mod/rm field, it's a memory reference.
644 //
645 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000646 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000647
Chris Lattnerb009c002004-02-11 19:26:28 +0000648 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000649 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000650 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000651 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000652 O << "\n";
653 return;
654 }
655
Chris Lattner233ad712002-11-21 01:33:44 +0000656 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000657 // There are three forms that are acceptable for MRMSrcReg
658 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000659 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000660 // 2 Operands: this is for things like mov that do not read a
661 // second input.
662 //
663 // 2 Operands: in this form, the last register is the ModR/M
664 // input. The first operand is a def&use. This is for things
665 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000666 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000667 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
668 // for instructions like the IMULri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000669 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000670 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000671 assert(MI->getOperand(0).isRegister() &&
672 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000673 (MI->getNumOperands() == 2 ||
674 (MI->getNumOperands() == 3 &&
675 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000676 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000677
Chris Lattnerb009c002004-02-11 19:26:28 +0000678 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000679 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000680 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000681 printOp(MI->getOperand(1));
682 if (MI->getNumOperands() == 3) {
683 O << ", ";
684 printOp(MI->getOperand(2));
685 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000686 O << "\n";
687 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000688 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000689
Chris Lattner3d3067b2002-11-21 20:44:15 +0000690 case X86II::MRMSrcMem: {
691 // These instructions are the same as MRMSrcReg, but instead of having a
692 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000693 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000694 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000695 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000696 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000697 isMem(MI, 2))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000698 && "Bad format for MRMSrcMem!");
Chris Lattner3d3067b2002-11-21 20:44:15 +0000699 if (MI->getNumOperands() == 2+4 &&
700 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
701 O << "**";
702
Chris Lattnerb009c002004-02-11 19:26:28 +0000703 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000704 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000705 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000706 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000707 O << "\n";
708 return;
709 }
710
Chris Lattner675dd2c2002-11-21 17:09:01 +0000711 case X86II::MRMS0r: case X86II::MRMS1r:
712 case X86II::MRMS2r: case X86II::MRMS3r:
713 case X86II::MRMS4r: case X86II::MRMS5r:
714 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000715 // In this form, the following are valid formats:
716 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000717 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000718 // 2. shl rdest, rinput <implicit CL or 1>
719 // 3. sbb rdest, rinput, immediate [rdest = rinput]
720 //
721 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000722 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000723 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000724 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000725 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000726 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000727 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000728 "Bad MRMSxR format!");
729
Chris Lattnerd9096832002-12-15 08:01:39 +0000730 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000731 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
732 O << "**";
733
Chris Lattnerb009c002004-02-11 19:26:28 +0000734 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000735 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000737 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000738 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000739 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000740 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000741 O << "\n";
742
743 return;
744 }
745
Chris Lattnerb7089442003-01-13 00:35:03 +0000746 case X86II::MRMS0m: case X86II::MRMS1m:
747 case X86II::MRMS2m: case X86II::MRMS3m:
748 case X86II::MRMS4m: case X86II::MRMS5m:
749 case X86II::MRMS6m: case X86II::MRMS7m: {
750 // In this form, the following are valid formats:
751 // 1. sete [m]
752 // 2. cmp [m], immediate
753 // 2. shl [m], rinput <implicit CL or 1>
754 // 3. sbb [m], immediate
755 //
756 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
757 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000758 assert((MI->getNumOperands() != 5 ||
759 (MI->getOperand(4).isImmediate() ||
760 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000761 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000762
763 const MachineOperand &Op3 = MI->getOperand(3);
764
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000765 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
766 // is misassembled by gas in intel_syntax mode as its 32-bit
767 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
768 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000769 if (MI->getOpcode() == X86::FSTPr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000770 if ((MI->getOperand(0).getReg() == X86::ESP)
771 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000772 if (Op3.isImmediate() &&
773 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
774 // 1 byte disp.
775 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
776 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
777 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000778 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000779 O << ".long ";
780 printOp(Op3);
781 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000782 }
783 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000784 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000785
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000786 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
787 // misassembled by gas in intel_syntax mode as its 32-bit
788 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
789 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000790 if (MI->getOpcode() == X86::FLDr80 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000791 MI->getOperand(0).getReg() == X86::ESP &&
792 MI->getOperand(1).getImmedValue() == 1) {
793 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
794 Op3.getImmedValue() <= 127) { // 1 byte displacement
795 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
796 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
797 } else {
798 O << ".byte 0xdb, 0xac, 0x24\n\t";
799 O << ".long ";
800 printOp(Op3);
801 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000802 }
803 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000804
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000805 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
806 // invalid opcode, saying "64 bit operations are only supported in
807 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
808 // [...]", which is wrong. Workaround: Output the raw opcode bytes
809 // instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000810 if (MI->getOpcode() == X86::FILDr64 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000811 MI->getOperand(0).getReg() == X86::ESP &&
812 MI->getOperand(1).getImmedValue() == 1) {
813 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
814 Op3.getImmedValue() <= 127) { // 1 byte displacement
815 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
816 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
817 } else {
818 O << ".byte 0xdf, 0xac, 0x24\n\t";
819 O << ".long ";
820 printOp(Op3);
821 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000822 }
823 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000824
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000825 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
826 // an invalid opcode, saying "64 bit operations are only
827 // supported in 64 bit modes." libopcodes disassembles it as
828 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
829 // "fistpll DWORD PTR " instead, which is what libopcodes is
830 // expecting to see.
Chris Lattnerb009c002004-02-11 19:26:28 +0000831 if (MI->getOpcode() == X86::FISTPr64) {
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000832 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000833 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000834 if (MI->getNumOperands() == 5) {
835 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000836 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000837 }
838 O << "\t# ";
839 }
840
Chris Lattnerb009c002004-02-11 19:26:28 +0000841 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000842 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000843 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000844 if (MI->getNumOperands() == 5) {
845 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000846 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000847 }
848 O << "\n";
849 return;
850 }
851
Chris Lattnerf9f60882002-11-18 06:56:51 +0000852 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000853 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000854 }
Chris Lattner72614082002-10-25 22:55:53 +0000855}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000856
Chris Lattner93c1afa2003-08-11 19:35:26 +0000857bool Printer::doInitialization(Module &M) {
858 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000859 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000860 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
861 // instruction as a reference to the register named sp, and if you try to
862 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
863 // before being looked up in the symbol table. This creates spurious
864 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
865 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000866 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000867 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000868 return false; // success
869}
870
Chris Lattnerad200712003-09-09 16:23:36 +0000871// SwitchSection - Switch to the specified section of the executable if we are
872// not already in it!
873//
874static void SwitchSection(std::ostream &OS, std::string &CurSection,
875 const char *NewSection) {
876 if (CurSection != NewSection) {
877 CurSection = NewSection;
878 if (!CurSection.empty())
879 OS << "\t" << NewSection << "\n";
880 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000881}
882
Chris Lattnerad200712003-09-09 16:23:36 +0000883bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000884 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000885 std::string CurSection;
886
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000887 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000888 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
889 if (I->hasInitializer()) { // External global require no code
890 O << "\n\n";
891 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000892 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000893 unsigned Size = TD.getTypeSize(C->getType());
894 unsigned Align = TD.getTypeAlignment(C->getType());
895
896 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000897 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
898 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000899 SwitchSection(O, CurSection, ".data");
900 if (I->hasInternalLinkage())
901 O << "\t.local " << name << "\n";
902
903 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000904 << "," << (unsigned)TD.getTypeAlignment(C->getType());
905 O << "\t\t# ";
906 WriteAsOperand(O, I, true, true, &M);
907 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000908 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000909 switch (I->getLinkage()) {
910 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000911 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000912 // Nonnull linkonce -> weak
913 O << "\t.weak " << name << "\n";
914 SwitchSection(O, CurSection, "");
915 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
916 break;
917
918 case GlobalValue::AppendingLinkage:
919 // FIXME: appending linkage variables should go into a section of
920 // their name or something. For now, just emit them as external.
921 case GlobalValue::ExternalLinkage:
922 // If external or appending, declare as a global symbol
923 O << "\t.globl " << name << "\n";
924 // FALL THROUGH
925 case GlobalValue::InternalLinkage:
926 if (C->isNullValue())
927 SwitchSection(O, CurSection, ".bss");
928 else
929 SwitchSection(O, CurSection, ".data");
930 break;
931 }
932
933 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000934 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000935 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000936 O << name << ":\t\t\t\t# ";
937 WriteAsOperand(O, I, true, true, &M);
938 O << " = ";
939 WriteAsOperand(O, C, false, false, &M);
940 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000941 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000942 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000943 }
Chris Lattnerad200712003-09-09 16:23:36 +0000944
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000945 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000946 return false; // success
947}