blob: be1f34d3bbe79b60fd31b69a1e295fab899c0d6a [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:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000402 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
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 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000645 assert(isMem(MI, 0) &&
646 (MI->getNumOperands() == 4+1 ||
647 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
648 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000649
Chris Lattnerb009c002004-02-11 19:26:28 +0000650 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000651 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000652 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000653 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000654 if (MI->getNumOperands() == 4+2) {
655 O << ", ";
656 printOp(MI->getOperand(5));
657 }
Chris Lattner18042332002-11-21 21:03:39 +0000658 O << "\n";
659 return;
660 }
661
Chris Lattner233ad712002-11-21 01:33:44 +0000662 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000663 // There are three forms that are acceptable for MRMSrcReg
664 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000665 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000666 // 2 Operands: this is for things like mov that do not read a
667 // second input.
668 //
669 // 2 Operands: in this form, the last register is the ModR/M
670 // input. The first operand is a def&use. This is for things
671 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000672 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000673 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000674 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000675 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000676 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000677 assert(MI->getOperand(0).isRegister() &&
678 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000679 (MI->getNumOperands() == 2 ||
680 (MI->getNumOperands() == 3 &&
681 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000682 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000683
Chris Lattnerb009c002004-02-11 19:26:28 +0000684 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000685 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000686 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000687 printOp(MI->getOperand(1));
688 if (MI->getNumOperands() == 3) {
689 O << ", ";
690 printOp(MI->getOperand(2));
691 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000692 O << "\n";
693 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000694 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000695
Chris Lattner3d3067b2002-11-21 20:44:15 +0000696 case X86II::MRMSrcMem: {
697 // These instructions are the same as MRMSrcReg, but instead of having a
698 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000699 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000700 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000701 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000702(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000703 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000704 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000705 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000706 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000707 printMemReference(MI, 1);
708 if (MI->getNumOperands() == 2+4) {
709 O << ", ";
710 printOp(MI->getOperand(5));
711 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000712 O << "\n";
713 return;
714 }
715
Chris Lattner675dd2c2002-11-21 17:09:01 +0000716 case X86II::MRMS0r: case X86II::MRMS1r:
717 case X86II::MRMS2r: case X86II::MRMS3r:
718 case X86II::MRMS4r: case X86II::MRMS5r:
719 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000720 // In this form, the following are valid formats:
721 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000722 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000723 // 2. shl rdest, rinput <implicit CL or 1>
724 // 3. sbb rdest, rinput, immediate [rdest = rinput]
725 //
726 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000727 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000728 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000729 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000730 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000731 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000732 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000733 "Bad MRMSxR format!");
734
Chris Lattnerd9096832002-12-15 08:01:39 +0000735 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000736 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
737 O << "**";
738
Chris Lattnerb009c002004-02-11 19:26:28 +0000739 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000740 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000741 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000742 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000743 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000744 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000745 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000746 O << "\n";
747
748 return;
749 }
750
Chris Lattnerb7089442003-01-13 00:35:03 +0000751 case X86II::MRMS0m: case X86II::MRMS1m:
752 case X86II::MRMS2m: case X86II::MRMS3m:
753 case X86II::MRMS4m: case X86II::MRMS5m:
754 case X86II::MRMS6m: case X86II::MRMS7m: {
755 // In this form, the following are valid formats:
756 // 1. sete [m]
757 // 2. cmp [m], immediate
758 // 2. shl [m], rinput <implicit CL or 1>
759 // 3. sbb [m], immediate
760 //
761 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
762 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000763 assert((MI->getNumOperands() != 5 ||
764 (MI->getOperand(4).isImmediate() ||
765 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000766 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000767
768 const MachineOperand &Op3 = MI->getOperand(3);
769
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000770 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
771 // is misassembled by gas in intel_syntax mode as its 32-bit
772 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
773 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000774 if (MI->getOpcode() == X86::FSTPr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000775 if ((MI->getOperand(0).getReg() == X86::ESP)
776 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000777 if (Op3.isImmediate() &&
778 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
779 // 1 byte disp.
780 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
781 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
782 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000783 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000784 O << ".long ";
785 printOp(Op3);
786 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000787 }
788 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000789 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000790
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000791 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
792 // misassembled by gas in intel_syntax mode as its 32-bit
793 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
794 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000795 if (MI->getOpcode() == X86::FLDr80 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000796 MI->getOperand(0).getReg() == X86::ESP &&
797 MI->getOperand(1).getImmedValue() == 1) {
798 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
799 Op3.getImmedValue() <= 127) { // 1 byte displacement
800 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
801 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
802 } else {
803 O << ".byte 0xdb, 0xac, 0x24\n\t";
804 O << ".long ";
805 printOp(Op3);
806 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000807 }
808 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000809
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000810 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
811 // invalid opcode, saying "64 bit operations are only supported in
812 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
813 // [...]", which is wrong. Workaround: Output the raw opcode bytes
814 // instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000815 if (MI->getOpcode() == X86::FILDr64 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000816 MI->getOperand(0).getReg() == X86::ESP &&
817 MI->getOperand(1).getImmedValue() == 1) {
818 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
819 Op3.getImmedValue() <= 127) { // 1 byte displacement
820 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
821 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
822 } else {
823 O << ".byte 0xdf, 0xac, 0x24\n\t";
824 O << ".long ";
825 printOp(Op3);
826 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000827 }
828 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000829
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000830 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
831 // an invalid opcode, saying "64 bit operations are only
832 // supported in 64 bit modes." libopcodes disassembles it as
833 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
834 // "fistpll DWORD PTR " instead, which is what libopcodes is
835 // expecting to see.
Chris Lattnerb009c002004-02-11 19:26:28 +0000836 if (MI->getOpcode() == X86::FISTPr64) {
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000837 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000838 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000839 if (MI->getNumOperands() == 5) {
840 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000841 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000842 }
843 O << "\t# ";
844 }
845
Chris Lattnerb009c002004-02-11 19:26:28 +0000846 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000847 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000848 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000849 if (MI->getNumOperands() == 5) {
850 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000851 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000852 }
853 O << "\n";
854 return;
855 }
856
Chris Lattnerf9f60882002-11-18 06:56:51 +0000857 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000858 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000859 }
Chris Lattner72614082002-10-25 22:55:53 +0000860}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000861
Chris Lattner93c1afa2003-08-11 19:35:26 +0000862bool Printer::doInitialization(Module &M) {
863 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000864 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000865 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
866 // instruction as a reference to the register named sp, and if you try to
867 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
868 // before being looked up in the symbol table. This creates spurious
869 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
870 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000871 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000872 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000873 return false; // success
874}
875
Chris Lattnerad200712003-09-09 16:23:36 +0000876// SwitchSection - Switch to the specified section of the executable if we are
877// not already in it!
878//
879static void SwitchSection(std::ostream &OS, std::string &CurSection,
880 const char *NewSection) {
881 if (CurSection != NewSection) {
882 CurSection = NewSection;
883 if (!CurSection.empty())
884 OS << "\t" << NewSection << "\n";
885 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000886}
887
Chris Lattnerad200712003-09-09 16:23:36 +0000888bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000889 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000890 std::string CurSection;
891
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000892 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000893 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
894 if (I->hasInitializer()) { // External global require no code
895 O << "\n\n";
896 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000897 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000898 unsigned Size = TD.getTypeSize(C->getType());
899 unsigned Align = TD.getTypeAlignment(C->getType());
900
901 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000902 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
903 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000904 SwitchSection(O, CurSection, ".data");
905 if (I->hasInternalLinkage())
906 O << "\t.local " << name << "\n";
907
908 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000909 << "," << (unsigned)TD.getTypeAlignment(C->getType());
910 O << "\t\t# ";
911 WriteAsOperand(O, I, true, true, &M);
912 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000913 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000914 switch (I->getLinkage()) {
915 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000916 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000917 // Nonnull linkonce -> weak
918 O << "\t.weak " << name << "\n";
919 SwitchSection(O, CurSection, "");
920 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
921 break;
922
923 case GlobalValue::AppendingLinkage:
924 // FIXME: appending linkage variables should go into a section of
925 // their name or something. For now, just emit them as external.
926 case GlobalValue::ExternalLinkage:
927 // If external or appending, declare as a global symbol
928 O << "\t.globl " << name << "\n";
929 // FALL THROUGH
930 case GlobalValue::InternalLinkage:
931 if (C->isNullValue())
932 SwitchSection(O, CurSection, ".bss");
933 else
934 SwitchSection(O, CurSection, ".data");
935 break;
936 }
937
938 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000939 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000940 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000941 O << name << ":\t\t\t\t# ";
942 WriteAsOperand(O, I, true, true, &M);
943 O << " = ";
944 WriteAsOperand(O, C, false, false, &M);
945 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000946 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000947 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000948 }
Chris Lattnerad200712003-09-09 16:23:36 +0000949
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000950 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000951 return false; // success
952}