blob: f8c582fe7998f90438f2a11646aed872d763664e [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 Lattnerb12ee502004-08-01 07:43:46 +000028#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000029#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000030#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000031#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000032#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000033#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000037 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Chris Lattner93c1afa2003-08-11 19:35:26 +000039 // FIXME: This should be automatically picked up by autoconf from the C
40 // frontend
41 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
42 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
43
Alkis Evlogimenos03090662004-03-09 03:35:34 +000044 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukman8606aea2004-07-26 18:48:58 +000045 GasBugWorkaroundEmitter(std::ostream& o)
46 : O(o), OldFlags(O.flags()), firstByte(true) {
47 O << std::hex;
48 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000049
Misha Brukman8606aea2004-07-26 18:48:58 +000050 ~GasBugWorkaroundEmitter() {
51 O.flags(OldFlags);
52 O << "\t# ";
53 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000054
Misha Brukman8606aea2004-07-26 18:48:58 +000055 virtual void emitByte(unsigned char B) {
56 if (!firstByte) O << "\n\t";
57 firstByte = false;
58 O << ".byte 0x" << (unsigned) B;
59 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000060
Misha Brukman8606aea2004-07-26 18:48:58 +000061 // These should never be called
62 virtual void emitWord(unsigned W) { assert(0); }
63 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
64 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
65 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
66 virtual uint64_t getCurrentPCValue() { abort(); }
67 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000068
69 private:
Misha Brukman8606aea2004-07-26 18:48:58 +000070 std::ostream& O;
71 std::ios::fmtflags OldFlags;
72 bool firstByte;
Alkis Evlogimenos03090662004-03-09 03:35:34 +000073 };
74
Chris Lattner3fa861a2004-08-01 06:02:08 +000075 struct X86AsmPrinter : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000076 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000077 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000079
80 /// Target machine description which we query for reg. names, data
81 /// layout, etc.
82 ///
83 TargetMachine &TM;
84
Brian Gaeked9fb37a2003-07-24 20:20:44 +000085 /// Name-mangler for global names.
86 ///
87 Mangler *Mang;
88
Chris Lattner3fa861a2004-08-01 06:02:08 +000089 X86AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000090
Brian Gaeke92bdfe62003-07-23 18:37:06 +000091 /// Cache of mangled name for current function. This is
92 /// recalculated at the beginning of each call to
93 /// runOnMachineFunction().
94 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000095 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000096
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000097 virtual const char *getPassName() const {
98 return "X86 Assembly Printer";
99 }
100
Chris Lattner3fa861a2004-08-01 06:02:08 +0000101 /// printInstruction - This method is automatically generated by tablegen
102 /// from the instruction set description. This method returns true if the
103 /// machine instruction was sufficiently described to print it, otherwise it
104 /// returns false.
105 bool printInstruction(const MachineInstr *MI);
106
Chris Lattnerb12ee502004-08-01 07:43:46 +0000107 // This method is used by the tablegen'erated instruction printer.
108 void printOperand(const MachineOperand &MO, MVT::ValueType VT) {
Chris Lattner25369cf2004-08-01 08:12:41 +0000109 if (MO.getType() == MachineOperand::MO_MachineRegister) {
110 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
111 // Bug Workaround: See note in Printer::doInitialization about %.
112 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
113 } else {
114 printOp(MO);
115 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000116 }
117
John Criswell4ffff9e2004-04-08 20:31:47 +0000118 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000119 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000120 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000121 void printMemReference(const MachineInstr *MI, unsigned Op);
122 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000124 bool doInitialization(Module &M);
125 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000126 void emitGlobalConstant(const Constant* CV);
127 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000128 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000129} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000130
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000131/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000132/// assembly code for a MachineFunction to the given output stream,
133/// using the given target machine description. This should work
134/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000135///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000136FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000137 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000138}
139
Chris Lattner3fa861a2004-08-01 06:02:08 +0000140
141// Include the auto-generated portion of the assembly writer.
142#include "X86GenAsmWriter.inc"
143
144
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000145/// toOctal - Convert the low order bits of X into an octal digit.
146///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147static inline char toOctal(int X) {
148 return (X&7)+'0';
149}
150
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000151/// getAsCString - Return the specified array as a C compatible
152/// string, only if the predicate isStringCompatible is true.
153///
Chris Lattnerac662d12003-11-03 20:19:49 +0000154static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000155 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000156
Chris Lattnerac662d12003-11-03 20:19:49 +0000157 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000158 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000159 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000160
161 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000162 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000163 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000164 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000166 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167 } else {
168 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000169 case '\b': O << "\\b"; break;
170 case '\f': O << "\\f"; break;
171 case '\n': O << "\\n"; break;
172 case '\r': O << "\\r"; break;
173 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000175 O << '\\';
176 O << toOctal(C >> 6);
177 O << toOctal(C >> 3);
178 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179 break;
180 }
181 }
182 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000183 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000184}
185
Chris Lattnerac662d12003-11-03 20:19:49 +0000186// Print out the specified constant, without a storage class. Only the
187// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000188void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000189 if (CV->isNullValue())
190 O << "0";
191 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
192 assert(CB == ConstantBool::True);
193 O << "1";
194 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000195 if (((CI->getValue() << 32) >> 32) == CI->getValue())
196 O << CI->getValue();
197 else
198 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000199 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
200 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000201 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 // This is a constant address for a global variable or function. Use the
203 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000204 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000205 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
206 const TargetData &TD = TM.getTargetData();
207 switch(CE->getOpcode()) {
208 case Instruction::GetElementPtr: {
209 // generate a symbolic expression for the byte address
210 const Constant *ptrVal = CE->getOperand(0);
211 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
212 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
213 O << "(";
214 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000215 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000216 } else {
217 emitConstantValueOnly(ptrVal);
218 }
219 break;
220 }
221 case Instruction::Cast: {
222 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000223 // that do not involve a change in value. This assertion is really gross,
224 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000225 Constant *Op = CE->getOperand(0);
226 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000227
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000228 // Remember, kids, pointers on x86 can be losslessly converted back and
229 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000230 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000231 && (Ty == Type::LongTy || Ty == Type::ULongTy
232 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000233 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000234 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
235 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000236 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
237 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000238 && "FIXME: Don't yet support this kind of constant cast expr");
239 O << "(";
240 emitConstantValueOnly(Op);
241 O << ")";
242 break;
243 }
244 case Instruction::Add:
245 O << "(";
246 emitConstantValueOnly(CE->getOperand(0));
247 O << ") + (";
248 emitConstantValueOnly(CE->getOperand(1));
249 O << ")";
250 break;
251 default:
252 assert(0 && "Unsupported operator!");
253 }
254 } else {
255 assert(0 && "Unknown constant value!");
256 }
257}
258
259// Print a constant value or values, with the appropriate storage class as a
260// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000261void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000262 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000263
Chris Lattnerad200712003-09-09 16:23:36 +0000264 if (CV->isNullValue()) {
265 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000266 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000267 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000268 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000269 O << "\t.ascii\t";
270 printAsCString(O, CVA);
271 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000272 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000273 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
274 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000275 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000276 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000277 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
278 // Print the fields in successive locations. Pad to align if needed!
279 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerad200712003-09-09 16:23:36 +0000280 unsigned sizeSoFar = 0;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000281 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
282 const Constant* field = CVS->getOperand(i);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000283
Chris Lattnerad200712003-09-09 16:23:36 +0000284 // Check if padding is needed and insert one or more 0s.
285 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000286 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattnerad200712003-09-09 16:23:36 +0000287 : cvsLayout->MemberOffsets[i+1])
288 - cvsLayout->MemberOffsets[i]) - fieldSize;
289 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000290
Chris Lattnerad200712003-09-09 16:23:36 +0000291 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000292 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000293
294 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000295 if (padSize)
296 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000297 }
Chris Lattnerad200712003-09-09 16:23:36 +0000298 assert(sizeSoFar == cvsLayout->StructSize &&
299 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000300 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000301 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
302 // FP Constants are printed as integer constants to avoid losing
303 // precision...
304 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000305 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000306 default: assert(0 && "Unknown floating point type!");
307 case Type::FloatTyID: {
308 union FU { // Abide by C TBAA rules
309 float FVal;
310 unsigned UVal;
311 } U;
312 U.FVal = Val;
313 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
314 return;
315 }
316 case Type::DoubleTyID: {
317 union DU { // Abide by C TBAA rules
318 double FVal;
319 uint64_t UVal;
320 } U;
321 U.FVal = Val;
322 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
323 return;
324 }
325 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000326 }
327
328 const Type *type = CV->getType();
329 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000330 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000331 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
332 O << ".byte";
333 break;
334 case Type::UShortTyID: case Type::ShortTyID:
335 O << ".word";
336 break;
337 case Type::FloatTyID: case Type::PointerTyID:
338 case Type::UIntTyID: case Type::IntTyID:
339 O << ".long";
340 break;
341 case Type::DoubleTyID:
342 case Type::ULongTyID: case Type::LongTyID:
343 O << ".quad";
344 break;
345 default:
346 assert (0 && "Can't handle printing this type of thing");
347 break;
348 }
349 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000350 emitConstantValueOnly(CV);
351 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000352}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000353
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000354/// printConstantPool - Print to the current output stream assembly
355/// representations of the constants in the constant pool MCP. This is
356/// used to print out constants which have been "spilled to memory" by
357/// the code generator.
358///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000359void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000360 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000361 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000362
Chris Lattnerb7089442003-01-13 00:35:03 +0000363 if (CP.empty()) return;
364
365 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
366 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000367 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000368 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000369 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
370 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000371 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000372 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000373}
374
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000375/// runOnMachineFunction - This uses the printMachineInstruction()
376/// method to print assembly for each instruction.
377///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000378bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000379 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000380 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000381 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000382
Chris Lattnerb7089442003-01-13 00:35:03 +0000383 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000384 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000385
Brian Gaeke6559bb92002-11-14 22:32:30 +0000386 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000387 O << "\t.text\n";
388 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000389 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000390 if (!EmitCygwin)
391 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000392 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000393
394 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000395 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
396 I != E; ++I) {
397 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000398 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000399 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000400 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000401 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000402 // Print the assembly for the instruction.
403 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000404 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405 }
Chris Lattner0285a332002-12-28 20:25:38 +0000406 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000407
408 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000409 return false;
410}
411
Chris Lattner3d3067b2002-11-21 20:44:15 +0000412static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000413 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
415 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000416}
417
418static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000419 if (MI->getOperand(Op).isFrameIndex()) return true;
420 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000421 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000422 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
423 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000424}
425
Brian Gaeke2a098772003-08-11 19:05:46 +0000426
427
Chris Lattner3fa861a2004-08-01 06:02:08 +0000428void X86AsmPrinter::printOp(const MachineOperand &MO,
429 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000430 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000431 switch (MO.getType()) {
432 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000433 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000434 O << "<" << V->getName() << ">";
435 return;
436 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000438 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000439 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000440 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000441 O << "%" << RI.get(MO.getReg()).Name;
442 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000443 O << "%reg" << MO.getReg();
444 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000445
446 case MachineOperand::MO_SignExtendedImmed:
447 case MachineOperand::MO_UnextendedImmed:
448 O << (int)MO.getImmedValue();
449 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000450 case MachineOperand::MO_MachineBasicBlock: {
451 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
452 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
453 << "_" << MBBOp->getNumber () << "\t# "
454 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000455 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000456 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000457 case MachineOperand::MO_PCRelativeDisp:
458 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
459 abort ();
460 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000462 if (!elideOffsetKeyword)
463 O << "OFFSET ";
464 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000465 return;
466 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000467 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000468 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000469 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000470 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000471 }
472}
473
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000474static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
475 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000476 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000477 case X86II::Mem8: return "BYTE PTR";
478 case X86II::Mem16: return "WORD PTR";
479 case X86II::Mem32: return "DWORD PTR";
480 case X86II::Mem64: return "QWORD PTR";
481 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000482 }
483}
484
Chris Lattner3fa861a2004-08-01 06:02:08 +0000485void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000486 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000487
488 if (MI->getOperand(Op).isFrameIndex()) {
489 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
490 if (MI->getOperand(Op+3).getImmedValue())
491 O << " + " << MI->getOperand(Op+3).getImmedValue();
492 O << "]";
493 return;
494 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000495 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000496 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000497 if (MI->getOperand(Op+3).getImmedValue())
498 O << " + " << MI->getOperand(Op+3).getImmedValue();
499 O << "]";
500 return;
501 }
502
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000504 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000506 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000507
508 O << "[";
509 bool NeedPlus = false;
510 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000511 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000512 NeedPlus = true;
513 }
514
515 if (IndexReg.getReg()) {
516 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000517 if (ScaleVal != 1)
518 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000519 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000520 NeedPlus = true;
521 }
522
Chris Lattner0285a332002-12-28 20:25:38 +0000523 if (DispVal) {
524 if (NeedPlus)
525 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000526 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000527 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000528 O << " - ";
529 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000530 }
531 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000532 }
533 O << "]";
534}
535
Chris Lattner30b2f722004-03-31 22:02:21 +0000536/// printImplUsesAfter - Emit the implicit-use registers for the instruction
537/// described by DESC, if its PrintImplUsesAfter flag is set.
538///
John Criswell4ffff9e2004-04-08 20:31:47 +0000539/// Inputs:
540/// Comma - List of registers will need a leading comma.
541/// Desc - Description of the Instruction.
542///
543/// Return value:
544/// true - Emitted one or more registers.
545/// false - Emitted no registers.
546///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000547bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
548 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000549 const MRegisterInfo &RI = *TM.getRegisterInfo();
550 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000551 bool emitted = false;
552 const unsigned *p = Desc.ImplicitUses;
553 if (*p) {
554 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
555 emitted = true;
556 ++p;
557 }
558 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000559 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000560 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000561 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000562 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000563 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000564 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000565 return false;
566}
567
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000568/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000569/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000570///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000571void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
572 ++EmittedInsts;
573 if (printInstruction(MI))
574 return; // Printer was automatically generated
575
Chris Lattnerf9f60882002-11-18 06:56:51 +0000576 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000577 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000578 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000579
Chris Lattnereca1f632002-12-25 05:09:01 +0000580 switch (Desc.TSFlags & X86II::FormMask) {
581 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000582 // Print pseudo-instructions as comments; either they should have been
583 // turned into real instructions by now, or they don't need to be
584 // seen by the assembler (e.g., IMPLICIT_USEs.)
585 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000586 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000587 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 O << " = phi ";
589 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000590 if (i != 1) O << ", ";
591 O << "[";
592 printOp(MI->getOperand(i));
593 O << ", ";
594 printOp(MI->getOperand(i+1));
595 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000596 }
597 } else {
598 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000599 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000600 printOp(MI->getOperand(0));
601 O << " = ";
602 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000603 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000604 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000605
606 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000607 O << " ";
608 if (MI->getOperand(i).isDef()) O << "*";
609 printOp(MI->getOperand(i));
610 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000611 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000612 }
613 O << "\n";
614 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000615
Chris Lattnerf9f60882002-11-18 06:56:51 +0000616 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000617 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000618 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000619 // 1. jmp foo - MachineBasicBlock operand
620 // 2. call bar - GlobalAddress Operand or External Symbol Operand
621 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000622 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000623 assert(MI->getNumOperands() == 1 &&
624 (MI->getOperand(0).isMachineBasicBlock() ||
625 MI->getOperand(0).isGlobalAddress() ||
626 MI->getOperand(0).isExternalSymbol() ||
627 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000628 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000629 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000630
Chris Lattner1626c502004-08-01 08:22:29 +0000631 bool LeadingComma = false;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000632 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000633 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000634 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000635 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000636 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000637 O << "\n";
638 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000639 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000640
Chris Lattner77875d82002-11-21 02:00:20 +0000641 case X86II::AddRegFrm: {
642 // There are currently two forms of acceptable AddRegFrm instructions.
643 // Either the instruction JUST takes a single register (like inc, dec, etc),
644 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000645 // (move immediate f.e.). Note that this immediate value might be stored as
646 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000647 // into a register. The initial register might be duplicated if this is a
648 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000649 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000650 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000651 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000652 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000653 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000654 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000655 MI->getOperand(1).isRegister() ||
656 MI->getOperand(1).isGlobalAddress() ||
657 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000658 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000659
Chris Lattner77875d82002-11-21 02:00:20 +0000660 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000661
Chris Lattnerb009c002004-02-11 19:26:28 +0000662 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000663
Brian Gaekede420ae2003-07-23 20:25:08 +0000664 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000665 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000666 (!MI->getOperand(1).isRegister() ||
667 MI->getOperand(1).getVRegValueOrNull() ||
668 MI->getOperand(1).isGlobalAddress() ||
669 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000670 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000671 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000672 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000673 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000674 O << "\n";
675 return;
676 }
Chris Lattner233ad712002-11-21 01:33:44 +0000677 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000678 // There are three forms of MRMDestReg instructions, those with 2
679 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000680 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000681 // 2 Operands: this is for things like mov that do not read a
682 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000683 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000684 // 2 Operands: two address instructions which def&use the first
685 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000686 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000687 // 3 Operands: in this form, two address instructions are the same
688 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000690 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000691 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000692 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000693 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000694 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000695
Chris Lattnerb009c002004-02-11 19:26:28 +0000696 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000697 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000698 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000699 printOp(MI->getOperand(1));
700 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000701 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000702 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000703 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000704 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000705 O << "\n";
706 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000707 }
Chris Lattner18042332002-11-21 21:03:39 +0000708
709 case X86II::MRMDestMem: {
710 // These instructions are the same as MRMDestReg, but instead of having a
711 // register reference for the mod/rm field, it's a memory reference.
712 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000713 assert(isMem(MI, 0) &&
714 (MI->getNumOperands() == 4+1 ||
715 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
716 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000717
Chris Lattnerb009c002004-02-11 19:26:28 +0000718 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000719 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000720 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000722 if (MI->getNumOperands() == 4+2) {
723 O << ", ";
724 printOp(MI->getOperand(5));
725 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000726 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000727 O << "\n";
728 return;
729 }
730
Chris Lattner233ad712002-11-21 01:33:44 +0000731 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000732 // There are three forms that are acceptable for MRMSrcReg
733 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000734 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000735 // 2 Operands: this is for things like mov that do not read a
736 // second input.
737 //
738 // 2 Operands: in this form, the last register is the ModR/M
739 // input. The first operand is a def&use. This is for things
740 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000741 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000742 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000743 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000744 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000745 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000746 assert(MI->getOperand(0).isRegister() &&
747 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000748 (MI->getNumOperands() == 2 ||
749 (MI->getNumOperands() == 3 &&
750 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000751 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000752
Chris Lattnerb009c002004-02-11 19:26:28 +0000753 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000754 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000755 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000756 printOp(MI->getOperand(1));
757 if (MI->getNumOperands() == 3) {
758 O << ", ";
759 printOp(MI->getOperand(2));
760 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000761 O << "\n";
762 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000763 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000764
Chris Lattner3d3067b2002-11-21 20:44:15 +0000765 case X86II::MRMSrcMem: {
766 // These instructions are the same as MRMSrcReg, but instead of having a
767 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000768 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000769 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000770 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
771 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
772 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000773 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000774 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000775 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000776 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000777 printMemReference(MI, 1);
778 if (MI->getNumOperands() == 2+4) {
779 O << ", ";
780 printOp(MI->getOperand(5));
781 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000782 O << "\n";
783 return;
784 }
785
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000786 case X86II::MRM0r: case X86II::MRM1r:
787 case X86II::MRM2r: case X86II::MRM3r:
788 case X86II::MRM4r: case X86II::MRM5r:
789 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 // In this form, the following are valid formats:
791 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000792 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000793 // 2. shl rdest, rinput <implicit CL or 1>
794 // 3. sbb rdest, rinput, immediate [rdest = rinput]
795 //
796 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000797 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000798 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000799 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000801 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000802 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000803 "Bad MRMSxR format!");
804
Chris Lattnerd9096832002-12-15 08:01:39 +0000805 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000806 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
807 O << "**";
808
Chris Lattnerb009c002004-02-11 19:26:28 +0000809 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000810 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000811 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000812 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000813 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000814 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000815 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000816 O << "\n";
817
818 return;
819 }
820
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000821 case X86II::MRM0m: case X86II::MRM1m:
822 case X86II::MRM2m: case X86II::MRM3m:
823 case X86II::MRM4m: case X86II::MRM5m:
824 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000825 // In this form, the following are valid formats:
826 // 1. sete [m]
827 // 2. cmp [m], immediate
828 // 2. shl [m], rinput <implicit CL or 1>
829 // 3. sbb [m], immediate
830 //
831 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
832 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000833 assert((MI->getNumOperands() != 5 ||
834 (MI->getOperand(4).isImmediate() ||
835 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000836 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000837
838 const MachineOperand &Op3 = MI->getOperand(3);
839
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000840 // gas bugs:
841 //
842 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000843 // is misassembled by gas in intel_syntax mode as its 32-bit
844 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
845 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000846 //
847 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000848 // misassembled by gas in intel_syntax mode as its 32-bit
849 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
850 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000851 //
852 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000853 // invalid opcode, saying "64 bit operations are only supported in
854 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
855 // [...]", which is wrong. Workaround: Output the raw opcode bytes
856 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000857 //
858 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
859 // invalid opcode, saying "64 bit operations are only supported in
860 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
861 // [...]", which is wrong. Workaround: Output the raw opcode bytes
862 // instead of the instruction.
863 if (MI->getOpcode() == X86::FSTP80m ||
864 MI->getOpcode() == X86::FLD80m ||
865 MI->getOpcode() == X86::FILD64m ||
866 MI->getOpcode() == X86::FISTP64m) {
Misha Brukman8606aea2004-07-26 18:48:58 +0000867 GasBugWorkaroundEmitter gwe(O);
868 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000869 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000870
Chris Lattnerb009c002004-02-11 19:26:28 +0000871 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000872 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000873 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000874 if (MI->getNumOperands() == 5) {
875 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000876 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000877 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000878 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000879 O << "\n";
880 return;
881 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000882 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000883 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000884 }
Chris Lattner72614082002-10-25 22:55:53 +0000885}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000886
Chris Lattner3fa861a2004-08-01 06:02:08 +0000887bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000888 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000889 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000890 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
891 // instruction as a reference to the register named sp, and if you try to
892 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
893 // before being looked up in the symbol table. This creates spurious
894 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
895 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000896 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000897 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000898 return false; // success
899}
900
Chris Lattnerad200712003-09-09 16:23:36 +0000901// SwitchSection - Switch to the specified section of the executable if we are
902// not already in it!
903//
904static void SwitchSection(std::ostream &OS, std::string &CurSection,
905 const char *NewSection) {
906 if (CurSection != NewSection) {
907 CurSection = NewSection;
908 if (!CurSection.empty())
909 OS << "\t" << NewSection << "\n";
910 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000911}
912
Chris Lattner3fa861a2004-08-01 06:02:08 +0000913bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000914 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000915 std::string CurSection;
916
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000917 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000918 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
919 if (I->hasInitializer()) { // External global require no code
920 O << "\n\n";
921 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000922 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000923 unsigned Size = TD.getTypeSize(C->getType());
924 unsigned Align = TD.getTypeAlignment(C->getType());
925
926 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000927 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
928 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000929 SwitchSection(O, CurSection, ".data");
930 if (I->hasInternalLinkage())
931 O << "\t.local " << name << "\n";
932
933 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000934 << "," << (unsigned)TD.getTypeAlignment(C->getType());
935 O << "\t\t# ";
936 WriteAsOperand(O, I, true, true, &M);
937 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000938 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000939 switch (I->getLinkage()) {
940 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000941 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000942 // Nonnull linkonce -> weak
943 O << "\t.weak " << name << "\n";
944 SwitchSection(O, CurSection, "");
945 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
946 break;
947
948 case GlobalValue::AppendingLinkage:
949 // FIXME: appending linkage variables should go into a section of
950 // their name or something. For now, just emit them as external.
951 case GlobalValue::ExternalLinkage:
952 // If external or appending, declare as a global symbol
953 O << "\t.globl " << name << "\n";
954 // FALL THROUGH
955 case GlobalValue::InternalLinkage:
956 if (C->isNullValue())
957 SwitchSection(O, CurSection, ".bss");
958 else
959 SwitchSection(O, CurSection, ".data");
960 break;
961 }
962
963 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000964 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000965 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000966 O << name << ":\t\t\t\t# ";
967 WriteAsOperand(O, I, true, true, &M);
968 O << " = ";
969 WriteAsOperand(O, C, false, false, &M);
970 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000971 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000972 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000973 }
Chris Lattnerad200712003-09-09 16:23:36 +0000974
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000975 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000976 return false; // success
977}