blob: 305d72421bf35fc21130ffd71b4a25c1a0fefacc [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 Lattner66fa1dc2004-08-11 02:25:00 +0000118 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
119 MVT::ValueType VT) {
120 switch (VT) {
121 default: assert(0 && "Unknown arg size!");
122 case MVT::i8: O << "BYTE PTR "; break;
123 case MVT::i16: O << "WORD PTR "; break;
124 case MVT::i32:
125 case MVT::f32: O << "DWORD PTR "; break;
126 case MVT::i64:
127 case MVT::f64: O << "QWORD PTR "; break;
128 case MVT::f80: O << "XWORD PTR "; break;
129 }
130 printMemReference(MI, OpNo);
131 }
132
John Criswell4ffff9e2004-04-08 20:31:47 +0000133 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000134 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000135 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000136 void printMemReference(const MachineInstr *MI, unsigned Op);
137 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000138 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000139 bool doInitialization(Module &M);
140 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000141 void emitGlobalConstant(const Constant* CV);
142 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000143 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000144} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000145
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000146/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000147/// assembly code for a MachineFunction to the given output stream,
148/// using the given target machine description. This should work
149/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000150///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000151FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000152 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000153}
154
Chris Lattner3fa861a2004-08-01 06:02:08 +0000155
156// Include the auto-generated portion of the assembly writer.
157#include "X86GenAsmWriter.inc"
158
159
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000160/// toOctal - Convert the low order bits of X into an octal digit.
161///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000162static inline char toOctal(int X) {
163 return (X&7)+'0';
164}
165
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000166/// getAsCString - Return the specified array as a C compatible
167/// string, only if the predicate isStringCompatible is true.
168///
Chris Lattnerac662d12003-11-03 20:19:49 +0000169static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000170 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000171
Chris Lattnerac662d12003-11-03 20:19:49 +0000172 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000173 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000174 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000175
176 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000177 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000178 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000179 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000180 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000181 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000182 } else {
183 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000184 case '\b': O << "\\b"; break;
185 case '\f': O << "\\f"; break;
186 case '\n': O << "\\n"; break;
187 case '\r': O << "\\r"; break;
188 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000189 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000190 O << '\\';
191 O << toOctal(C >> 6);
192 O << toOctal(C >> 3);
193 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000194 break;
195 }
196 }
197 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000198 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000199}
200
Chris Lattnerac662d12003-11-03 20:19:49 +0000201// Print out the specified constant, without a storage class. Only the
202// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000203void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000204 if (CV->isNullValue())
205 O << "0";
206 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
207 assert(CB == ConstantBool::True);
208 O << "1";
209 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000210 if (((CI->getValue() << 32) >> 32) == CI->getValue())
211 O << CI->getValue();
212 else
213 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000214 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
215 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000216 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000217 // This is a constant address for a global variable or function. Use the
218 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000219 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000220 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
221 const TargetData &TD = TM.getTargetData();
222 switch(CE->getOpcode()) {
223 case Instruction::GetElementPtr: {
224 // generate a symbolic expression for the byte address
225 const Constant *ptrVal = CE->getOperand(0);
226 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
227 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
228 O << "(";
229 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000230 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000231 } else {
232 emitConstantValueOnly(ptrVal);
233 }
234 break;
235 }
236 case Instruction::Cast: {
237 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000238 // that do not involve a change in value. This assertion is really gross,
239 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000240 Constant *Op = CE->getOperand(0);
241 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000242
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000243 // Remember, kids, pointers on x86 can be losslessly converted back and
244 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000245 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000246 && (Ty == Type::LongTy || Ty == Type::ULongTy
247 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000248 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000249 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
250 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000251 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
252 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000253 && "FIXME: Don't yet support this kind of constant cast expr");
254 O << "(";
255 emitConstantValueOnly(Op);
256 O << ")";
257 break;
258 }
259 case Instruction::Add:
260 O << "(";
261 emitConstantValueOnly(CE->getOperand(0));
262 O << ") + (";
263 emitConstantValueOnly(CE->getOperand(1));
264 O << ")";
265 break;
266 default:
267 assert(0 && "Unsupported operator!");
268 }
269 } else {
270 assert(0 && "Unknown constant value!");
271 }
272}
273
274// Print a constant value or values, with the appropriate storage class as a
275// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000276void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000277 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000278
Chris Lattnerad200712003-09-09 16:23:36 +0000279 if (CV->isNullValue()) {
280 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000281 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000282 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000283 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000284 O << "\t.ascii\t";
285 printAsCString(O, CVA);
286 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000287 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000288 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
289 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000290 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000291 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000292 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
293 // Print the fields in successive locations. Pad to align if needed!
294 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerad200712003-09-09 16:23:36 +0000295 unsigned sizeSoFar = 0;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000296 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
297 const Constant* field = CVS->getOperand(i);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000298
Chris Lattnerad200712003-09-09 16:23:36 +0000299 // Check if padding is needed and insert one or more 0s.
300 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000301 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattnerad200712003-09-09 16:23:36 +0000302 : cvsLayout->MemberOffsets[i+1])
303 - cvsLayout->MemberOffsets[i]) - fieldSize;
304 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000305
Chris Lattnerad200712003-09-09 16:23:36 +0000306 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000307 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000308
309 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000310 if (padSize)
311 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000312 }
Chris Lattnerad200712003-09-09 16:23:36 +0000313 assert(sizeSoFar == cvsLayout->StructSize &&
314 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000315 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000316 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
317 // FP Constants are printed as integer constants to avoid losing
318 // precision...
319 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000320 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000321 default: assert(0 && "Unknown floating point type!");
322 case Type::FloatTyID: {
323 union FU { // Abide by C TBAA rules
324 float FVal;
325 unsigned UVal;
326 } U;
327 U.FVal = Val;
328 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
329 return;
330 }
331 case Type::DoubleTyID: {
332 union DU { // Abide by C TBAA rules
333 double FVal;
334 uint64_t UVal;
335 } U;
336 U.FVal = Val;
337 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
338 return;
339 }
340 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000341 }
342
343 const Type *type = CV->getType();
344 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000345 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000346 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
347 O << ".byte";
348 break;
349 case Type::UShortTyID: case Type::ShortTyID:
350 O << ".word";
351 break;
352 case Type::FloatTyID: case Type::PointerTyID:
353 case Type::UIntTyID: case Type::IntTyID:
354 O << ".long";
355 break;
356 case Type::DoubleTyID:
357 case Type::ULongTyID: case Type::LongTyID:
358 O << ".quad";
359 break;
360 default:
361 assert (0 && "Can't handle printing this type of thing");
362 break;
363 }
364 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000365 emitConstantValueOnly(CV);
366 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000367}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000368
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000369/// printConstantPool - Print to the current output stream assembly
370/// representations of the constants in the constant pool MCP. This is
371/// used to print out constants which have been "spilled to memory" by
372/// the code generator.
373///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000374void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000375 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000376 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000377
Chris Lattnerb7089442003-01-13 00:35:03 +0000378 if (CP.empty()) return;
379
380 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
381 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000382 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000383 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000384 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
385 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000386 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000387 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000388}
389
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000390/// runOnMachineFunction - This uses the printMachineInstruction()
391/// method to print assembly for each instruction.
392///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000393bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000394 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000395 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000396 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000397
Chris Lattnerb7089442003-01-13 00:35:03 +0000398 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000399 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000400
Brian Gaeke6559bb92002-11-14 22:32:30 +0000401 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000402 O << "\t.text\n";
403 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000404 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000405 if (!EmitCygwin)
406 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000407 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000408
409 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000410 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
411 I != E; ++I) {
412 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000413 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000415 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000416 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000417 // Print the assembly for the instruction.
418 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000419 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000420 }
Chris Lattner0285a332002-12-28 20:25:38 +0000421 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000422
423 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000424 return false;
425}
426
Chris Lattner3d3067b2002-11-21 20:44:15 +0000427static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000428 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000429 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
430 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000431}
432
433static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000434 if (MI->getOperand(Op).isFrameIndex()) return true;
435 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000436 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000437 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
438 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000439}
440
Brian Gaeke2a098772003-08-11 19:05:46 +0000441
442
Chris Lattner3fa861a2004-08-01 06:02:08 +0000443void X86AsmPrinter::printOp(const MachineOperand &MO,
444 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000445 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000446 switch (MO.getType()) {
447 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000448 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000449 O << "<" << V->getName() << ">";
450 return;
451 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000452 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000453 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000454 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000455 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000456 O << "%" << RI.get(MO.getReg()).Name;
457 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000458 O << "%reg" << MO.getReg();
459 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000460
461 case MachineOperand::MO_SignExtendedImmed:
462 case MachineOperand::MO_UnextendedImmed:
463 O << (int)MO.getImmedValue();
464 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000465 case MachineOperand::MO_MachineBasicBlock: {
466 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
467 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
468 << "_" << MBBOp->getNumber () << "\t# "
469 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000470 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000471 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000472 case MachineOperand::MO_PCRelativeDisp:
473 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
474 abort ();
475 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000476 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000477 if (!elideOffsetKeyword)
478 O << "OFFSET ";
479 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 return;
481 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000482 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000483 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000484 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000485 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000486 }
487}
488
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000489static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
490 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000491 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000492 case X86II::Mem8: return "BYTE PTR";
493 case X86II::Mem16: return "WORD PTR";
494 case X86II::Mem32: return "DWORD PTR";
495 case X86II::Mem64: return "QWORD PTR";
496 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000497 }
498}
499
Chris Lattner3fa861a2004-08-01 06:02:08 +0000500void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000501 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000502
503 if (MI->getOperand(Op).isFrameIndex()) {
504 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
505 if (MI->getOperand(Op+3).getImmedValue())
506 O << " + " << MI->getOperand(Op+3).getImmedValue();
507 O << "]";
508 return;
509 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000510 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000511 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000512 if (MI->getOperand(Op+3).getImmedValue())
513 O << " + " << MI->getOperand(Op+3).getImmedValue();
514 O << "]";
515 return;
516 }
517
Chris Lattner3d3067b2002-11-21 20:44:15 +0000518 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000519 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000520 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000521 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000522
523 O << "[";
524 bool NeedPlus = false;
525 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000526 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000527 NeedPlus = true;
528 }
529
530 if (IndexReg.getReg()) {
531 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000532 if (ScaleVal != 1)
533 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000534 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000535 NeedPlus = true;
536 }
537
Chris Lattner0285a332002-12-28 20:25:38 +0000538 if (DispVal) {
539 if (NeedPlus)
540 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000541 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000542 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000543 O << " - ";
544 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000545 }
546 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000547 }
548 O << "]";
549}
550
Chris Lattner30b2f722004-03-31 22:02:21 +0000551/// printImplUsesAfter - Emit the implicit-use registers for the instruction
552/// described by DESC, if its PrintImplUsesAfter flag is set.
553///
John Criswell4ffff9e2004-04-08 20:31:47 +0000554/// Inputs:
555/// Comma - List of registers will need a leading comma.
556/// Desc - Description of the Instruction.
557///
558/// Return value:
559/// true - Emitted one or more registers.
560/// false - Emitted no registers.
561///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000562bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
563 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000564 const MRegisterInfo &RI = *TM.getRegisterInfo();
565 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000566 bool emitted = false;
567 const unsigned *p = Desc.ImplicitUses;
568 if (*p) {
569 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
570 emitted = true;
571 ++p;
572 }
573 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000574 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000575 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000576 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000577 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000578 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000579 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000580 return false;
581}
582
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000583/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000584/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000585///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000586void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
587 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000588
589 // gas bugs:
590 //
591 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
592 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
593 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
594 //
595 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
596 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
597 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
598 //
599 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
600 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
601 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
602 // Output the raw opcode bytes instead of the instruction.
603 //
604 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
605 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
606 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
607 // Output the raw opcode bytes instead of the instruction.
608 switch (MI->getOpcode()) {
609 case X86::FSTP80m:
610 case X86::FLD80m:
611 case X86::FILD64m:
612 case X86::FISTP64m:
613 GasBugWorkaroundEmitter gwe(O);
614 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
615 O << "\t# ";
616 }
617
Chris Lattner3fa861a2004-08-01 06:02:08 +0000618 if (printInstruction(MI))
619 return; // Printer was automatically generated
620
Chris Lattnerf9f60882002-11-18 06:56:51 +0000621 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000622 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000623 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000624
Chris Lattnereca1f632002-12-25 05:09:01 +0000625 switch (Desc.TSFlags & X86II::FormMask) {
626 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000627 // Print pseudo-instructions as comments; either they should have been
628 // turned into real instructions by now, or they don't need to be
629 // seen by the assembler (e.g., IMPLICIT_USEs.)
630 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000631 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000632 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000633 O << " = phi ";
634 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000635 if (i != 1) O << ", ";
636 O << "[";
637 printOp(MI->getOperand(i));
638 O << ", ";
639 printOp(MI->getOperand(i+1));
640 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000641 }
642 } else {
643 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000644 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000645 printOp(MI->getOperand(0));
646 O << " = ";
647 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000648 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000649 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000650
651 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000652 O << " ";
653 if (MI->getOperand(i).isDef()) O << "*";
654 printOp(MI->getOperand(i));
655 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000656 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000657 }
658 O << "\n";
659 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000660
Chris Lattnerf9f60882002-11-18 06:56:51 +0000661 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000662 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000663 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000664 // 1. jmp foo - MachineBasicBlock operand
665 // 2. call bar - GlobalAddress Operand or External Symbol Operand
666 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000667 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000668 assert(MI->getNumOperands() == 1 &&
669 (MI->getOperand(0).isMachineBasicBlock() ||
670 MI->getOperand(0).isGlobalAddress() ||
671 MI->getOperand(0).isExternalSymbol() ||
672 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000673 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000674 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000675
Chris Lattner1626c502004-08-01 08:22:29 +0000676 bool LeadingComma = false;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000677 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000678 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000679 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000680 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000681 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682 O << "\n";
683 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000684 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000685
Chris Lattner77875d82002-11-21 02:00:20 +0000686 case X86II::AddRegFrm: {
687 // There are currently two forms of acceptable AddRegFrm instructions.
688 // Either the instruction JUST takes a single register (like inc, dec, etc),
689 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000690 // (move immediate f.e.). Note that this immediate value might be stored as
691 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000692 // into a register. The initial register might be duplicated if this is a
693 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000694 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000695 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000696 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000697 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000698 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000699 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000700 MI->getOperand(1).isRegister() ||
701 MI->getOperand(1).isGlobalAddress() ||
702 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000703 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000704
Chris Lattner77875d82002-11-21 02:00:20 +0000705 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000706
Chris Lattnerb009c002004-02-11 19:26:28 +0000707 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000708
Brian Gaekede420ae2003-07-23 20:25:08 +0000709 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000710 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000711 (!MI->getOperand(1).isRegister() ||
712 MI->getOperand(1).getVRegValueOrNull() ||
713 MI->getOperand(1).isGlobalAddress() ||
714 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000715 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000716 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000717 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000718 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000719 O << "\n";
720 return;
721 }
Chris Lattner233ad712002-11-21 01:33:44 +0000722 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000723 // There are three forms of MRMDestReg instructions, those with 2
724 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000725 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000726 // 2 Operands: this is for things like mov that do not read a
727 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000728 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000729 // 2 Operands: two address instructions which def&use the first
730 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000731 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000732 // 3 Operands: in this form, two address instructions are the same
733 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000734 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000735 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000737 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000738 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000739 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000740
Chris Lattnerb009c002004-02-11 19:26:28 +0000741 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000742 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000743 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000744 printOp(MI->getOperand(1));
745 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000746 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000747 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000748 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000749 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000750 O << "\n";
751 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000752 }
Chris Lattner18042332002-11-21 21:03:39 +0000753
754 case X86II::MRMDestMem: {
755 // These instructions are the same as MRMDestReg, but instead of having a
756 // register reference for the mod/rm field, it's a memory reference.
757 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000758 assert(isMem(MI, 0) &&
759 (MI->getNumOperands() == 4+1 ||
760 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
761 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000762
Chris Lattnerb009c002004-02-11 19:26:28 +0000763 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000764 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000765 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000766 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000767 if (MI->getNumOperands() == 4+2) {
768 O << ", ";
769 printOp(MI->getOperand(5));
770 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000771 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000772 O << "\n";
773 return;
774 }
775
Chris Lattner233ad712002-11-21 01:33:44 +0000776 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000777 // There are three forms that are acceptable for MRMSrcReg
778 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000779 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000780 // 2 Operands: this is for things like mov that do not read a
781 // second input.
782 //
783 // 2 Operands: in this form, the last register is the ModR/M
784 // input. The first operand is a def&use. This is for things
785 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000786 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000787 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000788 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000789 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000790 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000791 assert(MI->getOperand(0).isRegister() &&
792 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000793 (MI->getNumOperands() == 2 ||
794 (MI->getNumOperands() == 3 &&
795 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000796 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000797
Chris Lattnerb009c002004-02-11 19:26:28 +0000798 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000799 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000800 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000801 printOp(MI->getOperand(1));
802 if (MI->getNumOperands() == 3) {
803 O << ", ";
804 printOp(MI->getOperand(2));
805 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000806 O << "\n";
807 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000808 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000809
Chris Lattner3d3067b2002-11-21 20:44:15 +0000810 case X86II::MRMSrcMem: {
811 // These instructions are the same as MRMSrcReg, but instead of having a
812 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000813 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000814 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000815 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
816 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
817 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000818 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000819 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000820 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000821 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000822 printMemReference(MI, 1);
823 if (MI->getNumOperands() == 2+4) {
824 O << ", ";
825 printOp(MI->getOperand(5));
826 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000827 O << "\n";
828 return;
829 }
830
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000831 case X86II::MRM0r: case X86II::MRM1r:
832 case X86II::MRM2r: case X86II::MRM3r:
833 case X86II::MRM4r: case X86II::MRM5r:
834 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000835 // In this form, the following are valid formats:
836 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000837 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000838 // 2. shl rdest, rinput <implicit CL or 1>
839 // 3. sbb rdest, rinput, immediate [rdest = rinput]
840 //
841 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000842 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000843 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000844 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000845 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000846 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000847 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000848 "Bad MRMSxR format!");
849
Chris Lattnerd9096832002-12-15 08:01:39 +0000850 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000851 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
852 O << "**";
853
Chris Lattnerb009c002004-02-11 19:26:28 +0000854 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000855 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000856 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000857 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000858 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000859 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000860 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000861 O << "\n";
862
863 return;
864 }
865
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000866 case X86II::MRM0m: case X86II::MRM1m:
867 case X86II::MRM2m: case X86II::MRM3m:
868 case X86II::MRM4m: case X86II::MRM5m:
869 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000870 // In this form, the following are valid formats:
871 // 1. sete [m]
872 // 2. cmp [m], immediate
873 // 2. shl [m], rinput <implicit CL or 1>
874 // 3. sbb [m], immediate
875 //
876 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
877 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000878 assert((MI->getNumOperands() != 5 ||
879 (MI->getOperand(4).isImmediate() ||
880 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000881 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000882
883 const MachineOperand &Op3 = MI->getOperand(3);
884
Chris Lattnerb009c002004-02-11 19:26:28 +0000885 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000886 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000887 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000888 if (MI->getNumOperands() == 5) {
889 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000890 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000891 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000892 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000893 O << "\n";
894 return;
895 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000896 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000897 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000898 }
Chris Lattner72614082002-10-25 22:55:53 +0000899}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000900
Chris Lattner3fa861a2004-08-01 06:02:08 +0000901bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000902 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000903 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000904 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
905 // instruction as a reference to the register named sp, and if you try to
906 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
907 // before being looked up in the symbol table. This creates spurious
908 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
909 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000910 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000911 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000912 return false; // success
913}
914
Chris Lattnerad200712003-09-09 16:23:36 +0000915// SwitchSection - Switch to the specified section of the executable if we are
916// not already in it!
917//
918static void SwitchSection(std::ostream &OS, std::string &CurSection,
919 const char *NewSection) {
920 if (CurSection != NewSection) {
921 CurSection = NewSection;
922 if (!CurSection.empty())
923 OS << "\t" << NewSection << "\n";
924 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000925}
926
Chris Lattner3fa861a2004-08-01 06:02:08 +0000927bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000928 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000929 std::string CurSection;
930
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000931 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000932 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
933 if (I->hasInitializer()) { // External global require no code
934 O << "\n\n";
935 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000936 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000937 unsigned Size = TD.getTypeSize(C->getType());
938 unsigned Align = TD.getTypeAlignment(C->getType());
939
940 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000941 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
942 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000943 SwitchSection(O, CurSection, ".data");
944 if (I->hasInternalLinkage())
945 O << "\t.local " << name << "\n";
946
947 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000948 << "," << (unsigned)TD.getTypeAlignment(C->getType());
949 O << "\t\t# ";
950 WriteAsOperand(O, I, true, true, &M);
951 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000952 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000953 switch (I->getLinkage()) {
954 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000955 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000956 // Nonnull linkonce -> weak
957 O << "\t.weak " << name << "\n";
958 SwitchSection(O, CurSection, "");
959 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
960 break;
961
962 case GlobalValue::AppendingLinkage:
963 // FIXME: appending linkage variables should go into a section of
964 // their name or something. For now, just emit them as external.
965 case GlobalValue::ExternalLinkage:
966 // If external or appending, declare as a global symbol
967 O << "\t.globl " << name << "\n";
968 // FALL THROUGH
969 case GlobalValue::InternalLinkage:
970 if (C->isNullValue())
971 SwitchSection(O, CurSection, ".bss");
972 else
973 SwitchSection(O, CurSection, ".data");
974 break;
975 }
976
977 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000978 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000979 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000980 O << name << ":\t\t\t\t# ";
981 WriteAsOperand(O, I, true, true, &M);
982 O << " = ";
983 WriteAsOperand(O, C, false, false, &M);
984 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000985 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000986 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000987 }
Chris Lattnerad200712003-09-09 16:23:36 +0000988
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000989 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000990 return false; // success
991}