blob: 89d0ad64e0b58be1db52e714ce394d3e9d954168 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86AsmPrinter.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//
Misha Brukman538607f2004-03-01 23:53:11 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Intel-format assembly language. This
12// printer is the output mechanism used by `llc' and `lli -print-machineinstrs'
13// 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"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattnere0121322003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000027#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000028#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000029#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000030#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000031#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000032#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000036 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37
Chris Lattner93c1afa2003-08-11 19:35:26 +000038 // FIXME: This should be automatically picked up by autoconf from the C
39 // frontend
40 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
41 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
42
Alkis Evlogimenos03090662004-03-09 03:35:34 +000043 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukman8606aea2004-07-26 18:48:58 +000044 GasBugWorkaroundEmitter(std::ostream& o)
45 : O(o), OldFlags(O.flags()), firstByte(true) {
46 O << std::hex;
47 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000048
Misha Brukman8606aea2004-07-26 18:48:58 +000049 ~GasBugWorkaroundEmitter() {
50 O.flags(OldFlags);
51 O << "\t# ";
52 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000053
Misha Brukman8606aea2004-07-26 18:48:58 +000054 virtual void emitByte(unsigned char B) {
55 if (!firstByte) O << "\n\t";
56 firstByte = false;
57 O << ".byte 0x" << (unsigned) B;
58 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000059
Misha Brukman8606aea2004-07-26 18:48:58 +000060 // These should never be called
61 virtual void emitWord(unsigned W) { assert(0); }
62 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
63 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
64 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
65 virtual uint64_t getCurrentPCValue() { abort(); }
66 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000067
68 private:
Misha Brukman8606aea2004-07-26 18:48:58 +000069 std::ostream& O;
70 std::ios::fmtflags OldFlags;
71 bool firstByte;
Alkis Evlogimenos03090662004-03-09 03:35:34 +000072 };
73
Chris Lattner3fa861a2004-08-01 06:02:08 +000074 struct X86AsmPrinter : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000075 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000076 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000078
79 /// Target machine description which we query for reg. names, data
80 /// layout, etc.
81 ///
82 TargetMachine &TM;
83
Brian Gaeked9fb37a2003-07-24 20:20:44 +000084 /// Name-mangler for global names.
85 ///
86 Mangler *Mang;
87
Chris Lattner3fa861a2004-08-01 06:02:08 +000088 X86AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089
Brian Gaeke92bdfe62003-07-23 18:37:06 +000090 /// Cache of mangled name for current function. This is
91 /// recalculated at the beginning of each call to
92 /// runOnMachineFunction().
93 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000094 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000095
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000096 virtual const char *getPassName() const {
97 return "X86 Assembly Printer";
98 }
99
Chris Lattner3fa861a2004-08-01 06:02:08 +0000100 /// printInstruction - This method is automatically generated by tablegen
101 /// from the instruction set description. This method returns true if the
102 /// machine instruction was sufficiently described to print it, otherwise it
103 /// returns false.
104 bool printInstruction(const MachineInstr *MI);
105
Chris Lattner30b2f722004-03-31 22:02:21 +0000106 void printImplUsesBefore(const TargetInstrDescriptor &Desc);
Chris Lattner26653832004-04-13 17:18:39 +0000107 bool printImplDefsBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000108 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
109 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000110 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000111 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000112 void printMemReference(const MachineInstr *MI, unsigned Op);
113 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000114 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000115 bool doInitialization(Module &M);
116 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000117 void emitGlobalConstant(const Constant* CV);
118 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000120} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000121
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000122/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000123/// assembly code for a MachineFunction to the given output stream,
124/// using the given target machine description. This should work
125/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000126///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000127FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000128 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000129}
130
Chris Lattner3fa861a2004-08-01 06:02:08 +0000131
132// Include the auto-generated portion of the assembly writer.
133#include "X86GenAsmWriter.inc"
134
135
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000136/// toOctal - Convert the low order bits of X into an octal digit.
137///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000138static inline char toOctal(int X) {
139 return (X&7)+'0';
140}
141
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000142/// getAsCString - Return the specified array as a C compatible
143/// string, only if the predicate isStringCompatible is true.
144///
Chris Lattnerac662d12003-11-03 20:19:49 +0000145static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000146 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147
Chris Lattnerac662d12003-11-03 20:19:49 +0000148 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000149 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000150 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000151
152 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000153 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000154 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000155 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000156 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000157 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000158 } else {
159 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000160 case '\b': O << "\\b"; break;
161 case '\f': O << "\\f"; break;
162 case '\n': O << "\\n"; break;
163 case '\r': O << "\\r"; break;
164 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000166 O << '\\';
167 O << toOctal(C >> 6);
168 O << toOctal(C >> 3);
169 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170 break;
171 }
172 }
173 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000174 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000175}
176
Chris Lattnerac662d12003-11-03 20:19:49 +0000177// Print out the specified constant, without a storage class. Only the
178// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000179void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000180 if (CV->isNullValue())
181 O << "0";
182 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
183 assert(CB == ConstantBool::True);
184 O << "1";
185 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000186 if (((CI->getValue() << 32) >> 32) == CI->getValue())
187 O << CI->getValue();
188 else
189 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000190 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
191 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000192 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000193 // This is a constant address for a global variable or function. Use the
194 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000195 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000196 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
197 const TargetData &TD = TM.getTargetData();
198 switch(CE->getOpcode()) {
199 case Instruction::GetElementPtr: {
200 // generate a symbolic expression for the byte address
201 const Constant *ptrVal = CE->getOperand(0);
202 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
203 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
204 O << "(";
205 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000206 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000207 } else {
208 emitConstantValueOnly(ptrVal);
209 }
210 break;
211 }
212 case Instruction::Cast: {
213 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000214 // that do not involve a change in value. This assertion is really gross,
215 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000216 Constant *Op = CE->getOperand(0);
217 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000218
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000219 // Remember, kids, pointers on x86 can be losslessly converted back and
220 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000221 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000222 && (Ty == Type::LongTy || Ty == Type::ULongTy
223 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000224 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000225 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
226 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000227 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
228 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000229 && "FIXME: Don't yet support this kind of constant cast expr");
230 O << "(";
231 emitConstantValueOnly(Op);
232 O << ")";
233 break;
234 }
235 case Instruction::Add:
236 O << "(";
237 emitConstantValueOnly(CE->getOperand(0));
238 O << ") + (";
239 emitConstantValueOnly(CE->getOperand(1));
240 O << ")";
241 break;
242 default:
243 assert(0 && "Unsupported operator!");
244 }
245 } else {
246 assert(0 && "Unknown constant value!");
247 }
248}
249
250// Print a constant value or values, with the appropriate storage class as a
251// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000252void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000253 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000254
Chris Lattnerad200712003-09-09 16:23:36 +0000255 if (CV->isNullValue()) {
256 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000257 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000258 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000259 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000260 O << "\t.ascii\t";
261 printAsCString(O, CVA);
262 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000263 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264 const std::vector<Use> &constValues = CVA->getValues();
265 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000266 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000267 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000268 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000269 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
270 // Print the fields in successive locations. Pad to align if needed!
271 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
272 const std::vector<Use>& constValues = CVS->getValues();
273 unsigned sizeSoFar = 0;
274 for (unsigned i=0, N = constValues.size(); i < N; i++) {
275 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000276
Chris Lattnerad200712003-09-09 16:23:36 +0000277 // Check if padding is needed and insert one or more 0s.
278 unsigned fieldSize = TD.getTypeSize(field->getType());
279 unsigned padSize = ((i == N-1? cvsLayout->StructSize
280 : cvsLayout->MemberOffsets[i+1])
281 - cvsLayout->MemberOffsets[i]) - fieldSize;
282 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000283
Chris Lattnerad200712003-09-09 16:23:36 +0000284 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000285 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000286
287 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000288 if (padSize)
289 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000290 }
Chris Lattnerad200712003-09-09 16:23:36 +0000291 assert(sizeSoFar == cvsLayout->StructSize &&
292 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000293 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000294 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
295 // FP Constants are printed as integer constants to avoid losing
296 // precision...
297 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000298 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000299 default: assert(0 && "Unknown floating point type!");
300 case Type::FloatTyID: {
301 union FU { // Abide by C TBAA rules
302 float FVal;
303 unsigned UVal;
304 } U;
305 U.FVal = Val;
306 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
307 return;
308 }
309 case Type::DoubleTyID: {
310 union DU { // Abide by C TBAA rules
311 double FVal;
312 uint64_t UVal;
313 } U;
314 U.FVal = Val;
315 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
316 return;
317 }
318 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000319 }
320
321 const Type *type = CV->getType();
322 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000323 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000324 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
325 O << ".byte";
326 break;
327 case Type::UShortTyID: case Type::ShortTyID:
328 O << ".word";
329 break;
330 case Type::FloatTyID: case Type::PointerTyID:
331 case Type::UIntTyID: case Type::IntTyID:
332 O << ".long";
333 break;
334 case Type::DoubleTyID:
335 case Type::ULongTyID: case Type::LongTyID:
336 O << ".quad";
337 break;
338 default:
339 assert (0 && "Can't handle printing this type of thing");
340 break;
341 }
342 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000343 emitConstantValueOnly(CV);
344 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000345}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000346
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000347/// printConstantPool - Print to the current output stream assembly
348/// representations of the constants in the constant pool MCP. This is
349/// used to print out constants which have been "spilled to memory" by
350/// the code generator.
351///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000352void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000353 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000354 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000355
Chris Lattnerb7089442003-01-13 00:35:03 +0000356 if (CP.empty()) return;
357
358 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
359 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000360 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000361 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000362 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
363 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000364 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000365 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000366}
367
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000368/// runOnMachineFunction - This uses the printMachineInstruction()
369/// method to print assembly for each instruction.
370///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000371bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000372 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000373 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000374 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000375
Chris Lattnerb7089442003-01-13 00:35:03 +0000376 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000377 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000378
Brian Gaeke6559bb92002-11-14 22:32:30 +0000379 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000380 O << "\t.text\n";
381 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000382 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000383 if (!EmitCygwin)
384 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000385 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000386
387 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000388 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
389 I != E; ++I) {
390 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000391 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000392 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000393 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000394 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000395 // Print the assembly for the instruction.
396 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000397 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000398 }
Chris Lattner0285a332002-12-28 20:25:38 +0000399 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000400
401 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000402 return false;
403}
404
Chris Lattner3d3067b2002-11-21 20:44:15 +0000405static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000406 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000407 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
408 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000409}
410
411static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000412 if (MI->getOperand(Op).isFrameIndex()) return true;
413 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000414 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000415 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
416 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000417}
418
Brian Gaeke2a098772003-08-11 19:05:46 +0000419
420
Chris Lattner3fa861a2004-08-01 06:02:08 +0000421void X86AsmPrinter::printOp(const MachineOperand &MO,
422 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000423 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000424 switch (MO.getType()) {
425 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000426 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000427 O << "<" << V->getName() << ">";
428 return;
429 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000430 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000431 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000432 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000433 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000434 O << "%" << RI.get(MO.getReg()).Name;
435 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000436 O << "%reg" << MO.getReg();
437 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000438
439 case MachineOperand::MO_SignExtendedImmed:
440 case MachineOperand::MO_UnextendedImmed:
441 O << (int)MO.getImmedValue();
442 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000443 case MachineOperand::MO_MachineBasicBlock: {
444 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
445 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
446 << "_" << MBBOp->getNumber () << "\t# "
447 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000448 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000449 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000450 case MachineOperand::MO_PCRelativeDisp:
451 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
452 abort ();
453 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000454 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000455 if (!elideOffsetKeyword)
456 O << "OFFSET ";
457 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000458 return;
459 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000460 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000462 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000463 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000464 }
465}
466
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000467static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
468 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000469 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000470 case X86II::Mem8: return "BYTE PTR";
471 case X86II::Mem16: return "WORD PTR";
472 case X86II::Mem32: return "DWORD PTR";
473 case X86II::Mem64: return "QWORD PTR";
474 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000475 }
476}
477
Chris Lattner3fa861a2004-08-01 06:02:08 +0000478void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000479 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000480
481 if (MI->getOperand(Op).isFrameIndex()) {
482 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
483 if (MI->getOperand(Op+3).getImmedValue())
484 O << " + " << MI->getOperand(Op+3).getImmedValue();
485 O << "]";
486 return;
487 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000488 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000489 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000490 if (MI->getOperand(Op+3).getImmedValue())
491 O << " + " << MI->getOperand(Op+3).getImmedValue();
492 O << "]";
493 return;
494 }
495
Chris Lattner3d3067b2002-11-21 20:44:15 +0000496 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000497 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000498 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000499 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000500
501 O << "[";
502 bool NeedPlus = false;
503 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000504 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505 NeedPlus = true;
506 }
507
508 if (IndexReg.getReg()) {
509 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000510 if (ScaleVal != 1)
511 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000512 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000513 NeedPlus = true;
514 }
515
Chris Lattner0285a332002-12-28 20:25:38 +0000516 if (DispVal) {
517 if (NeedPlus)
518 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000519 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000520 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000521 O << " - ";
522 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000523 }
524 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000525 }
526 O << "]";
527}
528
Chris Lattner30b2f722004-03-31 22:02:21 +0000529
530/// printImplUsesBefore - Emit the implicit-use registers for the instruction
531/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000532///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000533void X86AsmPrinter::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000534 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000535 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
536 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000537 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner30b2f722004-03-31 22:02:21 +0000538 O << "%" << RI.get(*p).Name << ", ";
539 }
540 }
541}
542
Chris Lattner26653832004-04-13 17:18:39 +0000543/// printImplDefsBefore - Emit the implicit-def registers for the instruction
544/// described by DESC, if its PrintImplUsesBefore flag is set. Return true if
545/// we printed any registers.
546///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000547bool X86AsmPrinter::printImplDefsBefore(const TargetInstrDescriptor &Desc) {
Chris Lattner26653832004-04-13 17:18:39 +0000548 bool Printed = false;
549 const MRegisterInfo &RI = *TM.getRegisterInfo();
550 if (Desc.TSFlags & X86II::PrintImplDefsBefore) {
551 const unsigned *p = Desc.ImplicitDefs;
552 if (*p) {
553 O << (Printed ? ", %" : "%") << RI.get (*p).Name;
554 Printed = true;
555 ++p;
556 }
557 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000558 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner26653832004-04-13 17:18:39 +0000559 O << ", %" << RI.get(*p).Name;
560 ++p;
561 }
562 }
563 return Printed;
564}
565
566
Chris Lattner30b2f722004-03-31 22:02:21 +0000567/// printImplUsesAfter - Emit the implicit-use registers for the instruction
568/// described by DESC, if its PrintImplUsesAfter flag is set.
569///
John Criswell4ffff9e2004-04-08 20:31:47 +0000570/// Inputs:
571/// Comma - List of registers will need a leading comma.
572/// Desc - Description of the Instruction.
573///
574/// Return value:
575/// true - Emitted one or more registers.
576/// false - Emitted no registers.
577///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000578bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
579 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000580 const MRegisterInfo &RI = *TM.getRegisterInfo();
581 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000582 bool emitted = false;
583 const unsigned *p = Desc.ImplicitUses;
584 if (*p) {
585 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
586 emitted = true;
587 ++p;
588 }
589 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000590 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000591 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000592 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000593 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000594 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000595 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000596 return false;
597}
598
599/// printImplDefsAfter - Emit the implicit-definition registers for the
600/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
601///
602/// Inputs:
603/// Comma - List of registers will need a leading comma.
604/// Desc - Description of the Instruction
605///
606/// Return value:
607/// true - Emitted one or more registers.
608/// false - Emitted no registers.
609///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000610bool X86AsmPrinter::printImplDefsAfter(const TargetInstrDescriptor &Desc,
611 const bool Comma = true) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000612 const MRegisterInfo &RI = *TM.getRegisterInfo();
613 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
614 bool emitted = false;
615 const unsigned *p = Desc.ImplicitDefs;
616 if (*p) {
617 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
618 emitted = true;
619 ++p;
620 }
621 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000622 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
John Criswell4ffff9e2004-04-08 20:31:47 +0000623 O << ", %" << RI.get(*p).Name;
624 ++p;
625 }
626 return emitted;
627 }
628 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000629}
630
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000631/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000632/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000633///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000634void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
635 ++EmittedInsts;
636 if (printInstruction(MI))
637 return; // Printer was automatically generated
638
Chris Lattnerf9f60882002-11-18 06:56:51 +0000639 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000640 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000641 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642
Chris Lattnereca1f632002-12-25 05:09:01 +0000643 switch (Desc.TSFlags & X86II::FormMask) {
644 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000645 // Print pseudo-instructions as comments; either they should have been
646 // turned into real instructions by now, or they don't need to be
647 // seen by the assembler (e.g., IMPLICIT_USEs.)
648 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000649 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000650 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000651 O << " = phi ";
652 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000653 if (i != 1) O << ", ";
654 O << "[";
655 printOp(MI->getOperand(i));
656 O << ", ";
657 printOp(MI->getOperand(i+1));
658 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000659 }
660 } else {
661 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000662 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000663 printOp(MI->getOperand(0));
664 O << " = ";
665 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000666 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000667 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000668
669 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000670 O << " ";
671 if (MI->getOperand(i).isDef()) O << "*";
672 printOp(MI->getOperand(i));
673 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000674 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000675 }
676 O << "\n";
677 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000678
Chris Lattnerf9f60882002-11-18 06:56:51 +0000679 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000680 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000681 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000682 // 1. jmp foo - MachineBasicBlock operand
683 // 2. call bar - GlobalAddress Operand or External Symbol Operand
684 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000685 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000686 assert(MI->getNumOperands() == 1 &&
687 (MI->getOperand(0).isMachineBasicBlock() ||
688 MI->getOperand(0).isGlobalAddress() ||
689 MI->getOperand(0).isExternalSymbol() ||
690 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000691 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000692 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000693
Chris Lattner26653832004-04-13 17:18:39 +0000694 bool LeadingComma = printImplDefsBefore(Desc);
695
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000696 if (MI->getNumOperands() == 1) {
Chris Lattner26653832004-04-13 17:18:39 +0000697 if (LeadingComma) O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000698 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000699 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000700 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000701 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
702 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000703 O << "\n";
704 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000705 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000706
Chris Lattner77875d82002-11-21 02:00:20 +0000707 case X86II::AddRegFrm: {
708 // There are currently two forms of acceptable AddRegFrm instructions.
709 // Either the instruction JUST takes a single register (like inc, dec, etc),
710 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000711 // (move immediate f.e.). Note that this immediate value might be stored as
712 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000713 // into a register. The initial register might be duplicated if this is a
714 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000715 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000716 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000717 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000718 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000719 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000720 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000721 MI->getOperand(1).isRegister() ||
722 MI->getOperand(1).isGlobalAddress() ||
723 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000724 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000725
Chris Lattner77875d82002-11-21 02:00:20 +0000726 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000727
Chris Lattnerb009c002004-02-11 19:26:28 +0000728 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000729
730 printImplUsesBefore(Desc); // fcmov*
731
Brian Gaekede420ae2003-07-23 20:25:08 +0000732 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000733 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000734 (!MI->getOperand(1).isRegister() ||
735 MI->getOperand(1).getVRegValueOrNull() ||
736 MI->getOperand(1).isGlobalAddress() ||
737 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000738 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000739 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000740 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000741 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000742 O << "\n";
743 return;
744 }
Chris Lattner233ad712002-11-21 01:33:44 +0000745 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000746 // There are three forms of MRMDestReg instructions, those with 2
747 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000748 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000749 // 2 Operands: this is for things like mov that do not read a
750 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000751 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000752 // 2 Operands: two address instructions which def&use the first
753 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000754 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000755 // 3 Operands: in this form, two address instructions are the same
756 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000757 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000758 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000759 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000760 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000761 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000762 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000763
Chris Lattnerb009c002004-02-11 19:26:28 +0000764 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000765 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000766 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000767 printOp(MI->getOperand(1));
768 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000769 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000770 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000771 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000772 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000773 O << "\n";
774 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000775 }
Chris Lattner18042332002-11-21 21:03:39 +0000776
777 case X86II::MRMDestMem: {
778 // These instructions are the same as MRMDestReg, but instead of having a
779 // register reference for the mod/rm field, it's a memory reference.
780 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000781 assert(isMem(MI, 0) &&
782 (MI->getNumOperands() == 4+1 ||
783 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
784 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000785
Chris Lattnerb009c002004-02-11 19:26:28 +0000786 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000787 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000788 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000789 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000790 if (MI->getNumOperands() == 4+2) {
791 O << ", ";
792 printOp(MI->getOperand(5));
793 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000794 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000795 O << "\n";
796 return;
797 }
798
Chris Lattner233ad712002-11-21 01:33:44 +0000799 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000800 // There are three forms that are acceptable for MRMSrcReg
801 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000802 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000803 // 2 Operands: this is for things like mov that do not read a
804 // second input.
805 //
806 // 2 Operands: in this form, the last register is the ModR/M
807 // input. The first operand is a def&use. This is for things
808 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000809 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000810 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000811 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000812 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000813 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000814 assert(MI->getOperand(0).isRegister() &&
815 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000816 (MI->getNumOperands() == 2 ||
817 (MI->getNumOperands() == 3 &&
818 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000819 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000820
Chris Lattnerb009c002004-02-11 19:26:28 +0000821 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000822 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000823 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000824 printOp(MI->getOperand(1));
825 if (MI->getNumOperands() == 3) {
826 O << ", ";
827 printOp(MI->getOperand(2));
828 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000829 O << "\n";
830 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000831 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000832
Chris Lattner3d3067b2002-11-21 20:44:15 +0000833 case X86II::MRMSrcMem: {
834 // These instructions are the same as MRMSrcReg, but instead of having a
835 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000836 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000837 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000838 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
839 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
840 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000841 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000842 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000843 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000844 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000845 printMemReference(MI, 1);
846 if (MI->getNumOperands() == 2+4) {
847 O << ", ";
848 printOp(MI->getOperand(5));
849 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000850 O << "\n";
851 return;
852 }
853
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000854 case X86II::MRM0r: case X86II::MRM1r:
855 case X86II::MRM2r: case X86II::MRM3r:
856 case X86II::MRM4r: case X86II::MRM5r:
857 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000858 // In this form, the following are valid formats:
859 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000860 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000861 // 2. shl rdest, rinput <implicit CL or 1>
862 // 3. sbb rdest, rinput, immediate [rdest = rinput]
863 //
864 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000865 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000866 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000867 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000868 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000869 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000870 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000871 "Bad MRMSxR format!");
872
Chris Lattnerd9096832002-12-15 08:01:39 +0000873 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000874 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
875 O << "**";
876
Chris Lattnerb009c002004-02-11 19:26:28 +0000877 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000878 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000879 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000880 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000881 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000882 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000883 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000884 O << "\n";
885
886 return;
887 }
888
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000889 case X86II::MRM0m: case X86II::MRM1m:
890 case X86II::MRM2m: case X86II::MRM3m:
891 case X86II::MRM4m: case X86II::MRM5m:
892 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000893 // In this form, the following are valid formats:
894 // 1. sete [m]
895 // 2. cmp [m], immediate
896 // 2. shl [m], rinput <implicit CL or 1>
897 // 3. sbb [m], immediate
898 //
899 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
900 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000901 assert((MI->getNumOperands() != 5 ||
902 (MI->getOperand(4).isImmediate() ||
903 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000904 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000905
906 const MachineOperand &Op3 = MI->getOperand(3);
907
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000908 // gas bugs:
909 //
910 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000911 // is misassembled by gas in intel_syntax mode as its 32-bit
912 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
913 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000914 //
915 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000916 // misassembled by gas in intel_syntax mode as its 32-bit
917 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
918 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000919 //
920 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000921 // invalid opcode, saying "64 bit operations are only supported in
922 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
923 // [...]", which is wrong. Workaround: Output the raw opcode bytes
924 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000925 //
926 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
927 // invalid opcode, saying "64 bit operations are only supported in
928 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
929 // [...]", which is wrong. Workaround: Output the raw opcode bytes
930 // instead of the instruction.
931 if (MI->getOpcode() == X86::FSTP80m ||
932 MI->getOpcode() == X86::FLD80m ||
933 MI->getOpcode() == X86::FILD64m ||
934 MI->getOpcode() == X86::FISTP64m) {
Misha Brukman8606aea2004-07-26 18:48:58 +0000935 GasBugWorkaroundEmitter gwe(O);
936 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000937 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000938
Chris Lattnerb009c002004-02-11 19:26:28 +0000939 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000940 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000941 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000942 if (MI->getNumOperands() == 5) {
943 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000944 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000945 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000946 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000947 O << "\n";
948 return;
949 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000950 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000951 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000952 }
Chris Lattner72614082002-10-25 22:55:53 +0000953}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000954
Chris Lattner3fa861a2004-08-01 06:02:08 +0000955bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000956 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000957 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000958 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
959 // instruction as a reference to the register named sp, and if you try to
960 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
961 // before being looked up in the symbol table. This creates spurious
962 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
963 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000964 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000965 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000966 return false; // success
967}
968
Chris Lattnerad200712003-09-09 16:23:36 +0000969// SwitchSection - Switch to the specified section of the executable if we are
970// not already in it!
971//
972static void SwitchSection(std::ostream &OS, std::string &CurSection,
973 const char *NewSection) {
974 if (CurSection != NewSection) {
975 CurSection = NewSection;
976 if (!CurSection.empty())
977 OS << "\t" << NewSection << "\n";
978 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000979}
980
Chris Lattner3fa861a2004-08-01 06:02:08 +0000981bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000982 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000983 std::string CurSection;
984
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000985 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000986 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
987 if (I->hasInitializer()) { // External global require no code
988 O << "\n\n";
989 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000990 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000991 unsigned Size = TD.getTypeSize(C->getType());
992 unsigned Align = TD.getTypeAlignment(C->getType());
993
994 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000995 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
996 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000997 SwitchSection(O, CurSection, ".data");
998 if (I->hasInternalLinkage())
999 O << "\t.local " << name << "\n";
1000
1001 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +00001002 << "," << (unsigned)TD.getTypeAlignment(C->getType());
1003 O << "\t\t# ";
1004 WriteAsOperand(O, I, true, true, &M);
1005 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001006 } else {
Chris Lattnerad200712003-09-09 16:23:36 +00001007 switch (I->getLinkage()) {
1008 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +00001009 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +00001010 // Nonnull linkonce -> weak
1011 O << "\t.weak " << name << "\n";
1012 SwitchSection(O, CurSection, "");
1013 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
1014 break;
1015
1016 case GlobalValue::AppendingLinkage:
1017 // FIXME: appending linkage variables should go into a section of
1018 // their name or something. For now, just emit them as external.
1019 case GlobalValue::ExternalLinkage:
1020 // If external or appending, declare as a global symbol
1021 O << "\t.globl " << name << "\n";
1022 // FALL THROUGH
1023 case GlobalValue::InternalLinkage:
1024 if (C->isNullValue())
1025 SwitchSection(O, CurSection, ".bss");
1026 else
1027 SwitchSection(O, CurSection, ".data");
1028 break;
1029 }
1030
1031 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001032 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001033 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001034 O << name << ":\t\t\t\t# ";
1035 WriteAsOperand(O, I, true, true, &M);
1036 O << " = ";
1037 WriteAsOperand(O, C, false, false, &M);
1038 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001039 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001040 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001041 }
Chris Lattnerad200712003-09-09 16:23:36 +00001042
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001043 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001044 return false; // success
1045}