blob: 6f7f642208aebcc681bef81d401ecc0e7c3fb164 [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);
Misha Brukman8606aea2004-07-26 18:48:58 +000052 }
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 Lattnerb12ee502004-08-01 07:43:46 +0000106 // This method is used by the tablegen'erated instruction printer.
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000107 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT) {
108 const MachineOperand &MO = MI->getOperand(OpNo);
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
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000118 void printCallOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT) {
119 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
120 }
121
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000122 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
123 MVT::ValueType VT) {
124 switch (VT) {
125 default: assert(0 && "Unknown arg size!");
126 case MVT::i8: O << "BYTE PTR "; break;
127 case MVT::i16: O << "WORD PTR "; break;
128 case MVT::i32:
129 case MVT::f32: O << "DWORD PTR "; break;
130 case MVT::i64:
131 case MVT::f64: O << "QWORD PTR "; break;
132 case MVT::f80: O << "XWORD PTR "; break;
133 }
134 printMemReference(MI, OpNo);
135 }
136
John Criswell4ffff9e2004-04-08 20:31:47 +0000137 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000138 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000139 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000140 void printMemReference(const MachineInstr *MI, unsigned Op);
141 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000142 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000143 bool doInitialization(Module &M);
144 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000145 void emitGlobalConstant(const Constant* CV);
146 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000147 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000148} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000149
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000150/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000151/// assembly code for a MachineFunction to the given output stream,
152/// using the given target machine description. This should work
153/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000154///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000155FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000156 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000157}
158
Chris Lattner3fa861a2004-08-01 06:02:08 +0000159
160// Include the auto-generated portion of the assembly writer.
161#include "X86GenAsmWriter.inc"
162
163
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000164/// toOctal - Convert the low order bits of X into an octal digit.
165///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166static inline char toOctal(int X) {
167 return (X&7)+'0';
168}
169
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000170/// getAsCString - Return the specified array as a C compatible
171/// string, only if the predicate isStringCompatible is true.
172///
Chris Lattnerac662d12003-11-03 20:19:49 +0000173static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000174 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000175
Chris Lattnerac662d12003-11-03 20:19:49 +0000176 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000177 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000178 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179
180 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000181 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000182 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000183 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000184 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000185 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000186 } else {
187 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000188 case '\b': O << "\\b"; break;
189 case '\f': O << "\\f"; break;
190 case '\n': O << "\\n"; break;
191 case '\r': O << "\\r"; break;
192 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000193 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000194 O << '\\';
195 O << toOctal(C >> 6);
196 O << toOctal(C >> 3);
197 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000198 break;
199 }
200 }
201 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000203}
204
Chris Lattnerac662d12003-11-03 20:19:49 +0000205// Print out the specified constant, without a storage class. Only the
206// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000207void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000208 if (CV->isNullValue())
209 O << "0";
210 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
211 assert(CB == ConstantBool::True);
212 O << "1";
213 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000214 if (((CI->getValue() << 32) >> 32) == CI->getValue())
215 O << CI->getValue();
216 else
217 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
219 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000220 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000221 // This is a constant address for a global variable or function. Use the
222 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000223 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000224 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
225 const TargetData &TD = TM.getTargetData();
226 switch(CE->getOpcode()) {
227 case Instruction::GetElementPtr: {
228 // generate a symbolic expression for the byte address
229 const Constant *ptrVal = CE->getOperand(0);
230 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
231 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
232 O << "(";
233 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000234 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000235 } else {
236 emitConstantValueOnly(ptrVal);
237 }
238 break;
239 }
240 case Instruction::Cast: {
241 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000242 // that do not involve a change in value. This assertion is really gross,
243 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000244 Constant *Op = CE->getOperand(0);
245 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000246
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000247 // Remember, kids, pointers on x86 can be losslessly converted back and
248 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000249 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000250 && (Ty == Type::LongTy || Ty == Type::ULongTy
251 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000252 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000253 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
254 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000255 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
256 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000257 && "FIXME: Don't yet support this kind of constant cast expr");
258 O << "(";
259 emitConstantValueOnly(Op);
260 O << ")";
261 break;
262 }
263 case Instruction::Add:
264 O << "(";
265 emitConstantValueOnly(CE->getOperand(0));
266 O << ") + (";
267 emitConstantValueOnly(CE->getOperand(1));
268 O << ")";
269 break;
270 default:
271 assert(0 && "Unsupported operator!");
272 }
273 } else {
274 assert(0 && "Unknown constant value!");
275 }
276}
277
278// Print a constant value or values, with the appropriate storage class as a
279// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000280void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000281 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000282
Chris Lattnerad200712003-09-09 16:23:36 +0000283 if (CV->isNullValue()) {
284 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000285 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000286 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000287 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000288 O << "\t.ascii\t";
289 printAsCString(O, CVA);
290 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000291 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000292 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
293 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000294 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000295 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000296 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
297 // Print the fields in successive locations. Pad to align if needed!
298 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerad200712003-09-09 16:23:36 +0000299 unsigned sizeSoFar = 0;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000300 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
301 const Constant* field = CVS->getOperand(i);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000302
Chris Lattnerad200712003-09-09 16:23:36 +0000303 // Check if padding is needed and insert one or more 0s.
304 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000305 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattnerad200712003-09-09 16:23:36 +0000306 : cvsLayout->MemberOffsets[i+1])
307 - cvsLayout->MemberOffsets[i]) - fieldSize;
308 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000309
Chris Lattnerad200712003-09-09 16:23:36 +0000310 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000311 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000312
313 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000314 if (padSize)
315 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000316 }
Chris Lattnerad200712003-09-09 16:23:36 +0000317 assert(sizeSoFar == cvsLayout->StructSize &&
318 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000319 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000320 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
321 // FP Constants are printed as integer constants to avoid losing
322 // precision...
323 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000324 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000325 default: assert(0 && "Unknown floating point type!");
326 case Type::FloatTyID: {
327 union FU { // Abide by C TBAA rules
328 float FVal;
329 unsigned UVal;
330 } U;
331 U.FVal = Val;
332 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
333 return;
334 }
335 case Type::DoubleTyID: {
336 union DU { // Abide by C TBAA rules
337 double FVal;
338 uint64_t UVal;
339 } U;
340 U.FVal = Val;
341 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
342 return;
343 }
344 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000345 }
346
347 const Type *type = CV->getType();
348 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000349 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000350 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
351 O << ".byte";
352 break;
353 case Type::UShortTyID: case Type::ShortTyID:
354 O << ".word";
355 break;
356 case Type::FloatTyID: case Type::PointerTyID:
357 case Type::UIntTyID: case Type::IntTyID:
358 O << ".long";
359 break;
360 case Type::DoubleTyID:
361 case Type::ULongTyID: case Type::LongTyID:
362 O << ".quad";
363 break;
364 default:
365 assert (0 && "Can't handle printing this type of thing");
366 break;
367 }
368 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000369 emitConstantValueOnly(CV);
370 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000371}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000372
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000373/// printConstantPool - Print to the current output stream assembly
374/// representations of the constants in the constant pool MCP. This is
375/// used to print out constants which have been "spilled to memory" by
376/// the code generator.
377///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000378void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000380 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000381
Chris Lattnerb7089442003-01-13 00:35:03 +0000382 if (CP.empty()) return;
383
384 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
385 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000387 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000388 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
389 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000390 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000391 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000392}
393
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000394/// runOnMachineFunction - This uses the printMachineInstruction()
395/// method to print assembly for each instruction.
396///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000397bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000398 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000399 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000400 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000401
Chris Lattnerb7089442003-01-13 00:35:03 +0000402 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000403 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000404
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000406 O << "\t.text\n";
407 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000408 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000409 if (!EmitCygwin)
410 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000411 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000412
413 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000414 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
415 I != E; ++I) {
416 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000417 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000418 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000419 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000420 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000421 // Print the assembly for the instruction.
422 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000423 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000424 }
Chris Lattner0285a332002-12-28 20:25:38 +0000425 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000426
427 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000428 return false;
429}
430
Chris Lattner3d3067b2002-11-21 20:44:15 +0000431static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000432 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000433 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
434 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000435}
436
437static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000438 if (MI->getOperand(Op).isFrameIndex()) return true;
439 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000440 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000441 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
442 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000443}
444
Brian Gaeke2a098772003-08-11 19:05:46 +0000445
446
Chris Lattner3fa861a2004-08-01 06:02:08 +0000447void X86AsmPrinter::printOp(const MachineOperand &MO,
448 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000449 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000450 switch (MO.getType()) {
451 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000452 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000453 O << "<" << V->getName() << ">";
454 return;
455 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000456 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000457 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000458 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000459 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000460 O << "%" << RI.get(MO.getReg()).Name;
461 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000462 O << "%reg" << MO.getReg();
463 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000464
465 case MachineOperand::MO_SignExtendedImmed:
466 case MachineOperand::MO_UnextendedImmed:
467 O << (int)MO.getImmedValue();
468 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000469 case MachineOperand::MO_MachineBasicBlock: {
470 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
471 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
472 << "_" << MBBOp->getNumber () << "\t# "
473 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000474 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000475 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000476 case MachineOperand::MO_PCRelativeDisp:
477 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
478 abort ();
479 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000481 if (!elideOffsetKeyword)
482 O << "OFFSET ";
483 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000484 return;
485 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000486 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000487 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000488 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000489 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000490 }
491}
492
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000493static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
494 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000495 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000496 case X86II::Mem8: return "BYTE PTR";
497 case X86II::Mem16: return "WORD PTR";
498 case X86II::Mem32: return "DWORD PTR";
499 case X86II::Mem64: return "QWORD PTR";
500 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000501 }
502}
503
Chris Lattner3fa861a2004-08-01 06:02:08 +0000504void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000506
507 if (MI->getOperand(Op).isFrameIndex()) {
508 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
509 if (MI->getOperand(Op+3).getImmedValue())
510 O << " + " << MI->getOperand(Op+3).getImmedValue();
511 O << "]";
512 return;
513 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000514 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000515 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000516 if (MI->getOperand(Op+3).getImmedValue())
517 O << " + " << MI->getOperand(Op+3).getImmedValue();
518 O << "]";
519 return;
520 }
521
Chris Lattner3d3067b2002-11-21 20:44:15 +0000522 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000523 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000524 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000525 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000526
527 O << "[";
528 bool NeedPlus = false;
529 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000530 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000531 NeedPlus = true;
532 }
533
534 if (IndexReg.getReg()) {
535 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000536 if (ScaleVal != 1)
537 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000538 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000539 NeedPlus = true;
540 }
541
Chris Lattner0285a332002-12-28 20:25:38 +0000542 if (DispVal) {
543 if (NeedPlus)
544 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000545 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000546 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000547 O << " - ";
548 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000549 }
550 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000551 }
552 O << "]";
553}
554
Chris Lattner30b2f722004-03-31 22:02:21 +0000555/// printImplUsesAfter - Emit the implicit-use registers for the instruction
556/// described by DESC, if its PrintImplUsesAfter flag is set.
557///
John Criswell4ffff9e2004-04-08 20:31:47 +0000558/// Inputs:
559/// Comma - List of registers will need a leading comma.
560/// Desc - Description of the Instruction.
561///
562/// Return value:
563/// true - Emitted one or more registers.
564/// false - Emitted no registers.
565///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000566bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
567 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000568 const MRegisterInfo &RI = *TM.getRegisterInfo();
569 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000570 bool emitted = false;
571 const unsigned *p = Desc.ImplicitUses;
572 if (*p) {
573 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
574 emitted = true;
575 ++p;
576 }
577 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000578 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000579 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000580 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000581 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000582 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000583 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000584 return false;
585}
586
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000587/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000588/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000589///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000590void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
591 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000592
593 // gas bugs:
594 //
595 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
596 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
597 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
598 //
599 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
600 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
601 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
602 //
603 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
604 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
605 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
606 // Output the raw opcode bytes instead of the instruction.
607 //
608 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
609 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
610 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
611 // Output the raw opcode bytes instead of the instruction.
612 switch (MI->getOpcode()) {
613 case X86::FSTP80m:
614 case X86::FLD80m:
615 case X86::FILD64m:
616 case X86::FISTP64m:
617 GasBugWorkaroundEmitter gwe(O);
618 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
619 O << "\t# ";
620 }
621
Chris Lattner3fa861a2004-08-01 06:02:08 +0000622 if (printInstruction(MI))
623 return; // Printer was automatically generated
624
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000625 MI->dump();
626 abort();
627
Chris Lattnerf9f60882002-11-18 06:56:51 +0000628 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000629 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000630 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000631
Chris Lattnereca1f632002-12-25 05:09:01 +0000632 switch (Desc.TSFlags & X86II::FormMask) {
633 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000634 // Print pseudo-instructions as comments; either they should have been
635 // turned into real instructions by now, or they don't need to be
636 // seen by the assembler (e.g., IMPLICIT_USEs.)
637 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000638 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000639 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000640 O << " = phi ";
641 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000642 if (i != 1) O << ", ";
643 O << "[";
644 printOp(MI->getOperand(i));
645 O << ", ";
646 printOp(MI->getOperand(i+1));
647 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000648 }
649 } else {
650 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000651 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000652 printOp(MI->getOperand(0));
653 O << " = ";
654 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000655 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000656 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000657
658 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000659 O << " ";
660 if (MI->getOperand(i).isDef()) O << "*";
661 printOp(MI->getOperand(i));
662 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000663 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000664 }
665 O << "\n";
666 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000667
Chris Lattnerf9f60882002-11-18 06:56:51 +0000668 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000669 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000670 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000671 // 1. jmp foo - MachineBasicBlock operand
672 // 2. call bar - GlobalAddress Operand or External Symbol Operand
673 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000674 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000675 assert(MI->getNumOperands() == 1 &&
676 (MI->getOperand(0).isMachineBasicBlock() ||
677 MI->getOperand(0).isGlobalAddress() ||
678 MI->getOperand(0).isExternalSymbol() ||
679 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000680 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000681 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682
Chris Lattner1626c502004-08-01 08:22:29 +0000683 bool LeadingComma = false;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000684 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000685 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000686 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000687 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000688 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 O << "\n";
690 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000691 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000692
Chris Lattner77875d82002-11-21 02:00:20 +0000693 case X86II::AddRegFrm: {
694 // There are currently two forms of acceptable AddRegFrm instructions.
695 // Either the instruction JUST takes a single register (like inc, dec, etc),
696 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000697 // (move immediate f.e.). Note that this immediate value might be stored as
698 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000699 // into a register. The initial register might be duplicated if this is a
700 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000701 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000702 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000703 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000704 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000705 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000706 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000707 MI->getOperand(1).isRegister() ||
708 MI->getOperand(1).isGlobalAddress() ||
709 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000710 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000711
Chris Lattner77875d82002-11-21 02:00:20 +0000712 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000713
Chris Lattnerb009c002004-02-11 19:26:28 +0000714 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000715
Brian Gaekede420ae2003-07-23 20:25:08 +0000716 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000717 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000718 (!MI->getOperand(1).isRegister() ||
719 MI->getOperand(1).getVRegValueOrNull() ||
720 MI->getOperand(1).isGlobalAddress() ||
721 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000722 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000723 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000724 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000725 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000726 O << "\n";
727 return;
728 }
Chris Lattner233ad712002-11-21 01:33:44 +0000729 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000730 // There are three forms of MRMDestReg instructions, those with 2
731 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000732 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000733 // 2 Operands: this is for things like mov that do not read a
734 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000735 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000736 // 2 Operands: two address instructions which def&use the first
737 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000738 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000739 // 3 Operands: in this form, two address instructions are the same
740 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000741 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000742 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000743 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000744 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000745 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000746 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000747
Chris Lattnerb009c002004-02-11 19:26:28 +0000748 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000749 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000750 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000751 printOp(MI->getOperand(1));
752 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000753 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000754 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000755 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000756 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000757 O << "\n";
758 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000759 }
Chris Lattner18042332002-11-21 21:03:39 +0000760
761 case X86II::MRMDestMem: {
762 // These instructions are the same as MRMDestReg, but instead of having a
763 // register reference for the mod/rm field, it's a memory reference.
764 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000765 assert(isMem(MI, 0) &&
766 (MI->getNumOperands() == 4+1 ||
767 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
768 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000769
Chris Lattnerb009c002004-02-11 19:26:28 +0000770 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000771 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000772 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000773 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000774 if (MI->getNumOperands() == 4+2) {
775 O << ", ";
776 printOp(MI->getOperand(5));
777 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000778 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000779 O << "\n";
780 return;
781 }
782
Chris Lattner233ad712002-11-21 01:33:44 +0000783 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000784 // There are three forms that are acceptable for MRMSrcReg
785 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000786 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000787 // 2 Operands: this is for things like mov that do not read a
788 // second input.
789 //
790 // 2 Operands: in this form, the last register is the ModR/M
791 // input. The first operand is a def&use. This is for things
792 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000793 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000794 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000795 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000796 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000797 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000798 assert(MI->getOperand(0).isRegister() &&
799 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000800 (MI->getNumOperands() == 2 ||
801 (MI->getNumOperands() == 3 &&
802 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000803 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000804
Chris Lattnerb009c002004-02-11 19:26:28 +0000805 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000806 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000807 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000808 printOp(MI->getOperand(1));
809 if (MI->getNumOperands() == 3) {
810 O << ", ";
811 printOp(MI->getOperand(2));
812 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000813 O << "\n";
814 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000815 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000816
Chris Lattner3d3067b2002-11-21 20:44:15 +0000817 case X86II::MRMSrcMem: {
818 // These instructions are the same as MRMSrcReg, but instead of having a
819 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000820 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000821 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000822 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
823 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
824 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000825 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000826 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000827 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000828 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000829 printMemReference(MI, 1);
830 if (MI->getNumOperands() == 2+4) {
831 O << ", ";
832 printOp(MI->getOperand(5));
833 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000834 O << "\n";
835 return;
836 }
837
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000838 case X86II::MRM0r: case X86II::MRM1r:
839 case X86II::MRM2r: case X86II::MRM3r:
840 case X86II::MRM4r: case X86II::MRM5r:
841 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000842 // In this form, the following are valid formats:
843 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000844 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000845 // 2. shl rdest, rinput <implicit CL or 1>
846 // 3. sbb rdest, rinput, immediate [rdest = rinput]
847 //
848 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000849 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000850 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000851 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000852 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000853 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000854 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000855 "Bad MRMSxR format!");
856
Chris Lattnerd9096832002-12-15 08:01:39 +0000857 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000858 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
859 O << "**";
860
Chris Lattnerb009c002004-02-11 19:26:28 +0000861 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000862 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000863 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000864 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000865 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000866 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000867 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000868 O << "\n";
869
870 return;
871 }
872
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000873 case X86II::MRM0m: case X86II::MRM1m:
874 case X86II::MRM2m: case X86II::MRM3m:
875 case X86II::MRM4m: case X86II::MRM5m:
876 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000877 // In this form, the following are valid formats:
878 // 1. sete [m]
879 // 2. cmp [m], immediate
880 // 2. shl [m], rinput <implicit CL or 1>
881 // 3. sbb [m], immediate
882 //
883 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
884 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000885 assert((MI->getNumOperands() != 5 ||
886 (MI->getOperand(4).isImmediate() ||
887 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000888 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000889
890 const MachineOperand &Op3 = MI->getOperand(3);
891
Chris Lattnerb009c002004-02-11 19:26:28 +0000892 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000893 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000894 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000895 if (MI->getNumOperands() == 5) {
896 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000897 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000898 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000899 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000900 O << "\n";
901 return;
902 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000903 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000904 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000905 }
Chris Lattner72614082002-10-25 22:55:53 +0000906}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000907
Chris Lattner3fa861a2004-08-01 06:02:08 +0000908bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000909 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000910 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000911 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
912 // instruction as a reference to the register named sp, and if you try to
913 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
914 // before being looked up in the symbol table. This creates spurious
915 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
916 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000917 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000918 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000919 return false; // success
920}
921
Chris Lattnerad200712003-09-09 16:23:36 +0000922// SwitchSection - Switch to the specified section of the executable if we are
923// not already in it!
924//
925static void SwitchSection(std::ostream &OS, std::string &CurSection,
926 const char *NewSection) {
927 if (CurSection != NewSection) {
928 CurSection = NewSection;
929 if (!CurSection.empty())
930 OS << "\t" << NewSection << "\n";
931 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000932}
933
Chris Lattner3fa861a2004-08-01 06:02:08 +0000934bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000935 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000936 std::string CurSection;
937
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000938 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000939 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
940 if (I->hasInitializer()) { // External global require no code
941 O << "\n\n";
942 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000943 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000944 unsigned Size = TD.getTypeSize(C->getType());
945 unsigned Align = TD.getTypeAlignment(C->getType());
946
947 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000948 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
949 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000950 SwitchSection(O, CurSection, ".data");
951 if (I->hasInternalLinkage())
952 O << "\t.local " << name << "\n";
953
954 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000955 << "," << (unsigned)TD.getTypeAlignment(C->getType());
956 O << "\t\t# ";
957 WriteAsOperand(O, I, true, true, &M);
958 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000959 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000960 switch (I->getLinkage()) {
961 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000962 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000963 // Nonnull linkonce -> weak
964 O << "\t.weak " << name << "\n";
965 SwitchSection(O, CurSection, "");
966 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
967 break;
968
969 case GlobalValue::AppendingLinkage:
970 // FIXME: appending linkage variables should go into a section of
971 // their name or something. For now, just emit them as external.
972 case GlobalValue::ExternalLinkage:
973 // If external or appending, declare as a global symbol
974 O << "\t.globl " << name << "\n";
975 // FALL THROUGH
976 case GlobalValue::InternalLinkage:
977 if (C->isNullValue())
978 SwitchSection(O, CurSection, ".bss");
979 else
980 SwitchSection(O, CurSection, ".data");
981 break;
982 }
983
984 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000985 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000986 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000987 O << name << ":\t\t\t\t# ";
988 WriteAsOperand(O, I, true, true, &M);
989 O << " = ";
990 WriteAsOperand(O, C, false, false, &M);
991 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000992 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000993 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000994 }
Chris Lattnerad200712003-09-09 16:23:36 +0000995
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000996 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000997 return false; // success
998}