blob: a17d5d5a72df1d1333d32630c056ac6ce679c5eb [file] [log] [blame]
Misha Brukmancf7d3af2004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswell482202a2003-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 Lattnerd92fb002002-10-25 22:55:53 +00009//
Misha Brukmana6025e62004-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 Lattnerd92fb002002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaekee7454352002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattner5dcb6542003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattner956e8372003-01-13 00:35:03 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner9289d7d2002-11-17 22:53:13 +000027#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd5540022004-08-01 07:43:46 +000028#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000029#include "llvm/Target/TargetMachine.h"
Brian Gaeke46f8b712003-07-24 20:20:44 +000030#include "llvm/Support/Mangler.h"
Brian Gaeke4547ab12003-10-06 15:41:21 +000031#include "Support/Statistic.h"
Chris Lattner5dcb6542003-08-03 23:37:09 +000032#include "Support/StringExtras.h"
Chris Lattner9a59f582003-08-11 19:35:26 +000033#include "Support/CommandLine.h"
Chris Lattner9f75a552004-02-14 06:00:36 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Chris Lattner02a3d832002-10-29 22:37:54 +000036namespace {
Brian Gaeke4547ab12003-10-06 15:41:21 +000037 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Chris Lattner9a59f582003-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 Evlogimenos8ac958b2004-03-09 03:35:34 +000044 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukmana332a6462004-07-26 18:48:58 +000045 GasBugWorkaroundEmitter(std::ostream& o)
46 : O(o), OldFlags(O.flags()), firstByte(true) {
47 O << std::hex;
48 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000049
Misha Brukmana332a6462004-07-26 18:48:58 +000050 ~GasBugWorkaroundEmitter() {
51 O.flags(OldFlags);
52 O << "\t# ";
53 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000054
Misha Brukmana332a6462004-07-26 18:48:58 +000055 virtual void emitByte(unsigned char B) {
56 if (!firstByte) O << "\n\t";
57 firstByte = false;
58 O << ".byte 0x" << (unsigned) B;
59 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000060
Misha Brukmana332a6462004-07-26 18:48:58 +000061 // These should never be called
62 virtual void emitWord(unsigned W) { assert(0); }
63 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
64 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
65 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
66 virtual uint64_t getCurrentPCValue() { abort(); }
67 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000068
69 private:
Misha Brukmana332a6462004-07-26 18:48:58 +000070 std::ostream& O;
71 std::ios::fmtflags OldFlags;
72 bool firstByte;
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000073 };
74
Chris Lattner9520d202004-08-01 06:02:08 +000075 struct X86AsmPrinter : public MachineFunctionPass {
Brian Gaekea92dce42003-07-23 20:25:08 +000076 /// Output stream on which we're printing assembly code.
Brian Gaekec3998cb2003-07-23 18:37:06 +000077 ///
Chris Lattner02a3d832002-10-29 22:37:54 +000078 std::ostream &O;
Brian Gaekea92dce42003-07-23 20:25:08 +000079
80 /// Target machine description which we query for reg. names, data
81 /// layout, etc.
82 ///
83 TargetMachine &TM;
84
Brian Gaeke46f8b712003-07-24 20:20:44 +000085 /// Name-mangler for global names.
86 ///
87 Mangler *Mang;
88
Chris Lattner9520d202004-08-01 06:02:08 +000089 X86AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaekec3998cb2003-07-23 18:37:06 +000090
Brian Gaekec3998cb2003-07-23 18:37:06 +000091 /// Cache of mangled name for current function. This is
92 /// recalculated at the beginning of each call to
93 /// runOnMachineFunction().
94 ///
Brian Gaekec1e4ee02003-06-27 00:00:48 +000095 std::string CurrentFnName;
Brian Gaekec3998cb2003-07-23 18:37:06 +000096
Chris Lattnerd06650a2002-12-15 21:13:40 +000097 virtual const char *getPassName() const {
98 return "X86 Assembly Printer";
99 }
100
Chris Lattner9520d202004-08-01 06:02:08 +0000101 /// printInstruction - This method is automatically generated by tablegen
102 /// from the instruction set description. This method returns true if the
103 /// machine instruction was sufficiently described to print it, otherwise it
104 /// returns false.
105 bool printInstruction(const MachineInstr *MI);
106
Chris Lattnerd5540022004-08-01 07:43:46 +0000107 // This method is used by the tablegen'erated instruction printer.
Chris Lattner09ee05b2004-08-11 02:25:00 +0000108 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT) {
109 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner06cf67e2004-08-01 08:12:41 +0000110 if (MO.getType() == MachineOperand::MO_MachineRegister) {
111 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
112 // Bug Workaround: See note in Printer::doInitialization about %.
113 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
114 } else {
115 printOp(MO);
116 }
Chris Lattnerd5540022004-08-01 07:43:46 +0000117 }
118
Chris Lattner09ee05b2004-08-11 02:25:00 +0000119 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
120 MVT::ValueType VT) {
121 switch (VT) {
122 default: assert(0 && "Unknown arg size!");
123 case MVT::i8: O << "BYTE PTR "; break;
124 case MVT::i16: O << "WORD PTR "; break;
125 case MVT::i32:
126 case MVT::f32: O << "DWORD PTR "; break;
127 case MVT::i64:
128 case MVT::f64: O << "QWORD PTR "; break;
129 case MVT::f80: O << "XWORD PTR "; break;
130 }
131 printMemReference(MI, OpNo);
132 }
133
John Criswell10db0622004-04-08 20:31:47 +0000134 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeke46f8b712003-07-24 20:20:44 +0000135 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanc968b872004-06-29 19:28:53 +0000136 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeke46f8b712003-07-24 20:20:44 +0000137 void printMemReference(const MachineInstr *MI, unsigned Op);
138 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000139 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000140 bool doInitialization(Module &M);
141 bool doFinalization(Module &M);
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000142 void emitGlobalConstant(const Constant* CV);
143 void emitConstantValueOnly(const Constant *CV);
Chris Lattner02a3d832002-10-29 22:37:54 +0000144 };
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000145} // end of anonymous namespace
Chris Lattner02a3d832002-10-29 22:37:54 +0000146
Brian Gaekec3998cb2003-07-23 18:37:06 +0000147/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekea92dce42003-07-23 20:25:08 +0000148/// assembly code for a MachineFunction to the given output stream,
149/// using the given target machine description. This should work
150/// regardless of whether the function is in SSA form.
Chris Lattner9289d7d2002-11-17 22:53:13 +0000151///
Chris Lattner9f75a552004-02-14 06:00:36 +0000152FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner9520d202004-08-01 06:02:08 +0000153 return new X86AsmPrinter(o, tm);
Chris Lattner9289d7d2002-11-17 22:53:13 +0000154}
155
Chris Lattner9520d202004-08-01 06:02:08 +0000156
157// Include the auto-generated portion of the assembly writer.
158#include "X86GenAsmWriter.inc"
159
160
Brian Gaekec3998cb2003-07-23 18:37:06 +0000161/// toOctal - Convert the low order bits of X into an octal digit.
162///
Brian Gaeke25e766a2003-06-25 18:01:07 +0000163static inline char toOctal(int X) {
164 return (X&7)+'0';
165}
166
Brian Gaekec3998cb2003-07-23 18:37:06 +0000167/// getAsCString - Return the specified array as a C compatible
168/// string, only if the predicate isStringCompatible is true.
169///
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000170static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattnerac2b1982004-01-14 17:14:42 +0000171 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke25e766a2003-06-25 18:01:07 +0000172
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000173 O << "\"";
Chris Lattnerac2b1982004-01-14 17:14:42 +0000174 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000175 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke25e766a2003-06-25 18:01:07 +0000176
177 if (C == '"') {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000178 O << "\\\"";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000179 } else if (C == '\\') {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000180 O << "\\\\";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000181 } else if (isprint(C)) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000182 O << C;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000183 } else {
184 switch(C) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000185 case '\b': O << "\\b"; break;
186 case '\f': O << "\\f"; break;
187 case '\n': O << "\\n"; break;
188 case '\r': O << "\\r"; break;
189 case '\t': O << "\\t"; break;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000190 default:
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000191 O << '\\';
192 O << toOctal(C >> 6);
193 O << toOctal(C >> 3);
194 O << toOctal(C >> 0);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000195 break;
196 }
197 }
198 }
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000199 O << "\"";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000200}
201
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000202// Print out the specified constant, without a storage class. Only the
203// constants valid in constant expressions can occur here.
Chris Lattner9520d202004-08-01 06:02:08 +0000204void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000205 if (CV->isNullValue())
206 O << "0";
207 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
208 assert(CB == ConstantBool::True);
209 O << "1";
210 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerabb91622004-02-23 03:27:05 +0000211 if (((CI->getValue() << 32) >> 32) == CI->getValue())
212 O << CI->getValue();
213 else
214 O << (unsigned long long)CI->getValue();
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000215 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
216 O << CI->getValue();
Reid Spencer87436872004-07-18 00:38:32 +0000217 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000218 // This is a constant address for a global variable or function. Use the
219 // name of the variable or function as the address value.
Reid Spencer87436872004-07-18 00:38:32 +0000220 O << Mang->getValueName(GV);
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000221 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
222 const TargetData &TD = TM.getTargetData();
223 switch(CE->getOpcode()) {
224 case Instruction::GetElementPtr: {
225 // generate a symbolic expression for the byte address
226 const Constant *ptrVal = CE->getOperand(0);
227 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
228 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
229 O << "(";
230 emitConstantValueOnly(ptrVal);
Chris Lattner47dfa642003-11-04 16:04:32 +0000231 O << ") + " << Offset;
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000232 } else {
233 emitConstantValueOnly(ptrVal);
234 }
235 break;
236 }
237 case Instruction::Cast: {
238 // Support only non-converting or widening casts for now, that is, ones
Brian Gaeke84c0efc2003-11-22 07:18:25 +0000239 // that do not involve a change in value. This assertion is really gross,
240 // and may not even be a complete check.
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000241 Constant *Op = CE->getOperand(0);
242 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner47dfa642003-11-04 16:04:32 +0000243
Brian Gaeke84c0efc2003-11-22 07:18:25 +0000244 // Remember, kids, pointers on x86 can be losslessly converted back and
245 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000246 assert(((isa<PointerType>(OpTy)
Brian Gaeke84c0efc2003-11-22 07:18:25 +0000247 && (Ty == Type::LongTy || Ty == Type::ULongTy
248 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000249 || (isa<PointerType>(Ty)
Brian Gaeke84c0efc2003-11-22 07:18:25 +0000250 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
251 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner47dfa642003-11-04 16:04:32 +0000252 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
253 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000254 && "FIXME: Don't yet support this kind of constant cast expr");
255 O << "(";
256 emitConstantValueOnly(Op);
257 O << ")";
258 break;
259 }
260 case Instruction::Add:
261 O << "(";
262 emitConstantValueOnly(CE->getOperand(0));
263 O << ") + (";
264 emitConstantValueOnly(CE->getOperand(1));
265 O << ")";
266 break;
267 default:
268 assert(0 && "Unsupported operator!");
269 }
270 } else {
271 assert(0 && "Unknown constant value!");
272 }
273}
274
275// Print a constant value or values, with the appropriate storage class as a
276// prefix.
Chris Lattner9520d202004-08-01 06:02:08 +0000277void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000278 const TargetData &TD = TM.getTargetData();
Brian Gaeke25e766a2003-06-25 18:01:07 +0000279
Chris Lattner230cffb2003-09-09 16:23:36 +0000280 if (CV->isNullValue()) {
281 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner469e4042003-11-03 19:44:05 +0000282 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000283 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerac2b1982004-01-14 17:14:42 +0000284 if (CVA->isString()) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000285 O << "\t.ascii\t";
286 printAsCString(O, CVA);
287 O << "\n";
Chris Lattner230cffb2003-09-09 16:23:36 +0000288 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000289 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
290 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke25e766a2003-06-25 18:01:07 +0000291 }
Chris Lattner469e4042003-11-03 19:44:05 +0000292 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000293 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
294 // Print the fields in successive locations. Pad to align if needed!
295 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner230cffb2003-09-09 16:23:36 +0000296 unsigned sizeSoFar = 0;
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000297 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
298 const Constant* field = CVS->getOperand(i);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000299
Chris Lattner230cffb2003-09-09 16:23:36 +0000300 // Check if padding is needed and insert one or more 0s.
301 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000302 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner230cffb2003-09-09 16:23:36 +0000303 : cvsLayout->MemberOffsets[i+1])
304 - cvsLayout->MemberOffsets[i]) - fieldSize;
305 sizeSoFar += fieldSize + padSize;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000306
Chris Lattner230cffb2003-09-09 16:23:36 +0000307 // Now print the actual field value
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000308 emitGlobalConstant(field);
Chris Lattner230cffb2003-09-09 16:23:36 +0000309
310 // Insert the field padding unless it's zero bytes...
Chris Lattner59068a02003-09-10 19:52:24 +0000311 if (padSize)
312 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000313 }
Chris Lattner230cffb2003-09-09 16:23:36 +0000314 assert(sizeSoFar == cvsLayout->StructSize &&
315 "Layout of constant struct may be incorrect!");
Chris Lattner469e4042003-11-03 19:44:05 +0000316 return;
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000317 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
318 // FP Constants are printed as integer constants to avoid losing
319 // precision...
320 double Val = CFP->getValue();
Chris Lattner6b727592004-06-17 18:19:28 +0000321 switch (CFP->getType()->getTypeID()) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000322 default: assert(0 && "Unknown floating point type!");
323 case Type::FloatTyID: {
324 union FU { // Abide by C TBAA rules
325 float FVal;
326 unsigned UVal;
327 } U;
328 U.FVal = Val;
329 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
330 return;
331 }
332 case Type::DoubleTyID: {
333 union DU { // Abide by C TBAA rules
334 double FVal;
335 uint64_t UVal;
336 } U;
337 U.FVal = Val;
338 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
339 return;
340 }
341 }
Chris Lattner469e4042003-11-03 19:44:05 +0000342 }
343
344 const Type *type = CV->getType();
345 O << "\t";
Chris Lattner6b727592004-06-17 18:19:28 +0000346 switch (type->getTypeID()) {
Chris Lattner469e4042003-11-03 19:44:05 +0000347 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
348 O << ".byte";
349 break;
350 case Type::UShortTyID: case Type::ShortTyID:
351 O << ".word";
352 break;
353 case Type::FloatTyID: case Type::PointerTyID:
354 case Type::UIntTyID: case Type::IntTyID:
355 O << ".long";
356 break;
357 case Type::DoubleTyID:
358 case Type::ULongTyID: case Type::LongTyID:
359 O << ".quad";
360 break;
361 default:
362 assert (0 && "Can't handle printing this type of thing");
363 break;
364 }
365 O << "\t";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000366 emitConstantValueOnly(CV);
367 O << "\n";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000368}
Chris Lattner9289d7d2002-11-17 22:53:13 +0000369
Brian Gaekec3998cb2003-07-23 18:37:06 +0000370/// printConstantPool - Print to the current output stream assembly
371/// representations of the constants in the constant pool MCP. This is
372/// used to print out constants which have been "spilled to memory" by
373/// the code generator.
374///
Chris Lattner9520d202004-08-01 06:02:08 +0000375void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattner956e8372003-01-13 00:35:03 +0000376 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekea92dce42003-07-23 20:25:08 +0000377 const TargetData &TD = TM.getTargetData();
Brian Gaekec3998cb2003-07-23 18:37:06 +0000378
Chris Lattner956e8372003-01-13 00:35:03 +0000379 if (CP.empty()) return;
380
381 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
382 O << "\t.section .rodata\n";
Brian Gaekec3998cb2003-07-23 18:37:06 +0000383 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke4ab22212003-06-26 18:02:30 +0000384 << "\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000385 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
386 << *CP[i] << "\n";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000387 emitGlobalConstant(CP[i]);
Chris Lattner956e8372003-01-13 00:35:03 +0000388 }
Chris Lattner956e8372003-01-13 00:35:03 +0000389}
390
Brian Gaekec3998cb2003-07-23 18:37:06 +0000391/// runOnMachineFunction - This uses the printMachineInstruction()
392/// method to print assembly for each instruction.
393///
Chris Lattner9520d202004-08-01 06:02:08 +0000394bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner5dcb6542003-08-03 23:37:09 +0000395 O << "\n\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000396 // What's my mangled name?
Brian Gaeke46f8b712003-07-24 20:20:44 +0000397 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000398
Chris Lattner956e8372003-01-13 00:35:03 +0000399 // Print out constants referenced by the function
Brian Gaeke25e766a2003-06-25 18:01:07 +0000400 printConstantPool(MF.getConstantPool());
Chris Lattner956e8372003-01-13 00:35:03 +0000401
Brian Gaekee7454352002-11-14 22:32:30 +0000402 // Print out labels for the function.
Chris Lattner956e8372003-01-13 00:35:03 +0000403 O << "\t.text\n";
404 O << "\t.align 16\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000405 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner9a59f582003-08-11 19:35:26 +0000406 if (!EmitCygwin)
407 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000408 O << CurrentFnName << ":\n";
Brian Gaekee7454352002-11-14 22:32:30 +0000409
410 // Print out code for the function.
Chris Lattner1520c5a2002-12-28 20:25:38 +0000411 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
412 I != E; ++I) {
413 // Print a label for the basic block.
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000414 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke25e766a2003-06-25 18:01:07 +0000415 << I->getBasicBlock()->getName() << "\n";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000416 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmanc968b872004-06-29 19:28:53 +0000417 II != E; ++II) {
Chris Lattner1520c5a2002-12-28 20:25:38 +0000418 // Print the assembly for the instruction.
419 O << "\t";
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000420 printMachineInstruction(II);
Brian Gaekee7454352002-11-14 22:32:30 +0000421 }
Chris Lattner1520c5a2002-12-28 20:25:38 +0000422 }
Brian Gaekee7454352002-11-14 22:32:30 +0000423
424 // We didn't modify anything.
Chris Lattner02a3d832002-10-29 22:37:54 +0000425 return false;
426}
427
Chris Lattner61fafd352002-11-21 20:44:15 +0000428static bool isScale(const MachineOperand &MO) {
Chris Lattnerce351082002-12-15 08:01:39 +0000429 return MO.isImmediate() &&
Brian Gaeke25e766a2003-06-25 18:01:07 +0000430 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
431 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner61fafd352002-11-21 20:44:15 +0000432}
433
434static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattner956e8372003-01-13 00:35:03 +0000435 if (MI->getOperand(Op).isFrameIndex()) return true;
436 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner61fafd352002-11-21 20:44:15 +0000437 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke25e766a2003-06-25 18:01:07 +0000438 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
439 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner61fafd352002-11-21 20:44:15 +0000440}
441
Brian Gaeke12b32532003-08-11 19:05:46 +0000442
443
Chris Lattner9520d202004-08-01 06:02:08 +0000444void X86AsmPrinter::printOp(const MachineOperand &MO,
445 bool elideOffsetKeyword /* = false */) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000446 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner5812f062002-11-18 06:56:51 +0000447 switch (MO.getType()) {
448 case MachineOperand::MO_VirtualRegister:
Chris Lattnerfb8032d2002-12-04 17:32:52 +0000449 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattner6425a502002-12-04 06:45:19 +0000450 O << "<" << V->getName() << ">";
451 return;
452 }
Chris Lattner956e8372003-01-13 00:35:03 +0000453 // FALLTHROUGH
Misha Brukman6e5d4932002-11-20 18:56:41 +0000454 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenosbbf53932004-02-15 21:37:17 +0000455 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke12b32532003-08-11 19:05:46 +0000456 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke1600c4a2003-08-13 18:15:15 +0000457 O << "%" << RI.get(MO.getReg()).Name;
458 else
Chris Lattner5812f062002-11-18 06:56:51 +0000459 O << "%reg" << MO.getReg();
460 return;
Chris Lattner177e9282002-11-21 02:00:20 +0000461
462 case MachineOperand::MO_SignExtendedImmed:
463 case MachineOperand::MO_UnextendedImmed:
464 O << (int)MO.getImmedValue();
465 return;
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000466 case MachineOperand::MO_MachineBasicBlock: {
467 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
468 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
469 << "_" << MBBOp->getNumber () << "\t# "
470 << MBBOp->getBasicBlock ()->getName ();
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000471 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000472 }
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000473 case MachineOperand::MO_PCRelativeDisp:
474 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
475 abort ();
476 return;
Chris Lattner956e8372003-01-13 00:35:03 +0000477 case MachineOperand::MO_GlobalAddress:
Brian Gaekea2d9f402003-07-31 17:38:52 +0000478 if (!elideOffsetKeyword)
479 O << "OFFSET ";
480 O << Mang->getValueName(MO.getGlobal());
Chris Lattner956e8372003-01-13 00:35:03 +0000481 return;
482 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000483 O << MO.getSymbolName();
Chris Lattner956e8372003-01-13 00:35:03 +0000484 return;
Chris Lattner5812f062002-11-18 06:56:51 +0000485 default:
Brian Gaeke25e766a2003-06-25 18:01:07 +0000486 O << "<unknown operand type>"; return;
Chris Lattner5812f062002-11-18 06:56:51 +0000487 }
488}
489
Alkis Evlogimenos19493902004-02-28 22:02:05 +0000490static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
491 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke25e766a2003-06-25 18:01:07 +0000492 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos19493902004-02-28 22:02:05 +0000493 case X86II::Mem8: return "BYTE PTR";
494 case X86II::Mem16: return "WORD PTR";
495 case X86II::Mem32: return "DWORD PTR";
496 case X86II::Mem64: return "QWORD PTR";
497 case X86II::Mem80: return "XWORD PTR";
Brian Gaekea4a10fe2002-12-05 08:30:40 +0000498 }
499}
500
Chris Lattner9520d202004-08-01 06:02:08 +0000501void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner61fafd352002-11-21 20:44:15 +0000502 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner956e8372003-01-13 00:35:03 +0000503
504 if (MI->getOperand(Op).isFrameIndex()) {
505 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
506 if (MI->getOperand(Op+3).getImmedValue())
507 O << " + " << MI->getOperand(Op+3).getImmedValue();
508 O << "]";
509 return;
510 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000511 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke4ab22212003-06-26 18:02:30 +0000512 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattner956e8372003-01-13 00:35:03 +0000513 if (MI->getOperand(Op+3).getImmedValue())
514 O << " + " << MI->getOperand(Op+3).getImmedValue();
515 O << "]";
516 return;
517 }
518
Chris Lattner61fafd352002-11-21 20:44:15 +0000519 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner1520c5a2002-12-28 20:25:38 +0000520 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner61fafd352002-11-21 20:44:15 +0000521 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner1520c5a2002-12-28 20:25:38 +0000522 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner61fafd352002-11-21 20:44:15 +0000523
524 O << "[";
525 bool NeedPlus = false;
526 if (BaseReg.getReg()) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000527 printOp(BaseReg);
Chris Lattner61fafd352002-11-21 20:44:15 +0000528 NeedPlus = true;
529 }
530
531 if (IndexReg.getReg()) {
532 if (NeedPlus) O << " + ";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000533 if (ScaleVal != 1)
534 O << ScaleVal << "*";
Brian Gaekea92dce42003-07-23 20:25:08 +0000535 printOp(IndexReg);
Chris Lattner61fafd352002-11-21 20:44:15 +0000536 NeedPlus = true;
537 }
538
Chris Lattner1520c5a2002-12-28 20:25:38 +0000539 if (DispVal) {
540 if (NeedPlus)
541 if (DispVal > 0)
Misha Brukmanc968b872004-06-29 19:28:53 +0000542 O << " + ";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000543 else {
Misha Brukmanc968b872004-06-29 19:28:53 +0000544 O << " - ";
545 DispVal = -DispVal;
Chris Lattner1520c5a2002-12-28 20:25:38 +0000546 }
547 O << DispVal;
Chris Lattner61fafd352002-11-21 20:44:15 +0000548 }
549 O << "]";
550}
551
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000552/// printImplUsesAfter - Emit the implicit-use registers for the instruction
553/// described by DESC, if its PrintImplUsesAfter flag is set.
554///
John Criswell10db0622004-04-08 20:31:47 +0000555/// Inputs:
556/// Comma - List of registers will need a leading comma.
557/// Desc - Description of the Instruction.
558///
559/// Return value:
560/// true - Emitted one or more registers.
561/// false - Emitted no registers.
562///
Chris Lattner9520d202004-08-01 06:02:08 +0000563bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
564 const bool Comma = true) {
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000565 const MRegisterInfo &RI = *TM.getRegisterInfo();
566 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell10db0622004-04-08 20:31:47 +0000567 bool emitted = false;
568 const unsigned *p = Desc.ImplicitUses;
569 if (*p) {
570 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
571 emitted = true;
572 ++p;
573 }
574 while (*p) {
Chris Lattner9520d202004-08-01 06:02:08 +0000575 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner262c8322003-08-11 20:04:57 +0000576 O << ", %" << RI.get(*p).Name;
John Criswell10db0622004-04-08 20:31:47 +0000577 ++p;
Brian Gaeke12b32532003-08-11 19:05:46 +0000578 }
John Criswell10db0622004-04-08 20:31:47 +0000579 return emitted;
Brian Gaeke12b32532003-08-11 19:05:46 +0000580 }
John Criswell10db0622004-04-08 20:31:47 +0000581 return false;
582}
583
Brian Gaekec3998cb2003-07-23 18:37:06 +0000584/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekea92dce42003-07-23 20:25:08 +0000585/// MI in Intel syntax to the current output stream.
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000586///
Chris Lattner9520d202004-08-01 06:02:08 +0000587void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
588 ++EmittedInsts;
589 if (printInstruction(MI))
590 return; // Printer was automatically generated
591
Chris Lattner5812f062002-11-18 06:56:51 +0000592 unsigned Opcode = MI->getOpcode();
Chris Lattner82baa9c2004-06-02 05:55:25 +0000593 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000594 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattner5812f062002-11-18 06:56:51 +0000595
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000596 switch (Desc.TSFlags & X86II::FormMask) {
597 case X86II::Pseudo:
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000598 // Print pseudo-instructions as comments; either they should have been
599 // turned into real instructions by now, or they don't need to be
600 // seen by the assembler (e.g., IMPLICIT_USEs.)
601 O << "# ";
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000602 if (Opcode == X86::PHI) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000603 printOp(MI->getOperand(0));
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000604 O << " = phi ";
605 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell10db0622004-04-08 20:31:47 +0000606 if (i != 1) O << ", ";
607 O << "[";
608 printOp(MI->getOperand(i));
609 O << ", ";
610 printOp(MI->getOperand(i+1));
611 O << "]";
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000612 }
613 } else {
614 unsigned i = 0;
Alkis Evlogimenosaaba4632003-12-14 13:24:17 +0000615 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell10db0622004-04-08 20:31:47 +0000616 printOp(MI->getOperand(0));
617 O << " = ";
618 ++i;
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000619 }
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000620 O << TII.getName(MI->getOpcode());
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000621
622 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell10db0622004-04-08 20:31:47 +0000623 O << " ";
624 if (MI->getOperand(i).isDef()) O << "*";
625 printOp(MI->getOperand(i));
626 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattner7c10f4f2002-12-25 05:09:01 +0000627 }
Chris Lattner2889d2e2002-12-13 09:59:26 +0000628 }
629 O << "\n";
630 return;
Chris Lattner2889d2e2002-12-13 09:59:26 +0000631
Chris Lattner5812f062002-11-18 06:56:51 +0000632 case X86II::RawFrm:
John Criswell10db0622004-04-08 20:31:47 +0000633 {
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000634 // The accepted forms of Raw instructions are:
Chris Lattner9520d202004-08-01 06:02:08 +0000635 // 1. jmp foo - MachineBasicBlock operand
636 // 2. call bar - GlobalAddress Operand or External Symbol Operand
637 // 3. in AL, imm - Immediate operand
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000638 //
Chris Lattner9520d202004-08-01 06:02:08 +0000639 assert(MI->getNumOperands() == 1 &&
640 (MI->getOperand(0).isMachineBasicBlock() ||
641 MI->getOperand(0).isGlobalAddress() ||
642 MI->getOperand(0).isExternalSymbol() ||
643 MI->getOperand(0).isImmediate()) &&
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000644 "Illegal raw instruction!");
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000645 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner5812f062002-11-18 06:56:51 +0000646
Chris Lattnere42675f2004-08-01 08:22:29 +0000647 bool LeadingComma = false;
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000648 if (MI->getNumOperands() == 1) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000649 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell10db0622004-04-08 20:31:47 +0000650 LeadingComma = true;
Chris Lattner5812f062002-11-18 06:56:51 +0000651 }
John Criswell10db0622004-04-08 20:31:47 +0000652 printImplUsesAfter(Desc, LeadingComma);
Chris Lattner5812f062002-11-18 06:56:51 +0000653 O << "\n";
654 return;
John Criswell10db0622004-04-08 20:31:47 +0000655 }
Chris Lattner5812f062002-11-18 06:56:51 +0000656
Chris Lattner177e9282002-11-21 02:00:20 +0000657 case X86II::AddRegFrm: {
658 // There are currently two forms of acceptable AddRegFrm instructions.
659 // Either the instruction JUST takes a single register (like inc, dec, etc),
660 // or it takes a register and an immediate of the same size as the register
Chris Lattner6425a502002-12-04 06:45:19 +0000661 // (move immediate f.e.). Note that this immediate value might be stored as
662 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnere218f792002-12-23 23:46:00 +0000663 // into a register. The initial register might be duplicated if this is a
664 // M_2_ADDR_REG instruction
Chris Lattner177e9282002-11-21 02:00:20 +0000665 //
Chris Lattnerce351082002-12-15 08:01:39 +0000666 assert(MI->getOperand(0).isRegister() &&
Chris Lattner177e9282002-11-21 02:00:20 +0000667 (MI->getNumOperands() == 1 ||
Chris Lattner6425a502002-12-04 06:45:19 +0000668 (MI->getNumOperands() == 2 &&
Chris Lattner8d79e5c2002-12-04 17:28:40 +0000669 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnere218f792002-12-23 23:46:00 +0000670 MI->getOperand(1).isImmediate() ||
Misha Brukmanc968b872004-06-29 19:28:53 +0000671 MI->getOperand(1).isRegister() ||
672 MI->getOperand(1).isGlobalAddress() ||
673 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner177e9282002-11-21 02:00:20 +0000674 "Illegal form for AddRegFrm instruction!");
Chris Lattner5812f062002-11-18 06:56:51 +0000675
Chris Lattner177e9282002-11-21 02:00:20 +0000676 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner177e9282002-11-21 02:00:20 +0000677
Chris Lattner0d10bf82004-02-11 19:26:28 +0000678 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000679
Brian Gaekea92dce42003-07-23 20:25:08 +0000680 printOp(MI->getOperand(0));
Chris Lattner956e8372003-01-13 00:35:03 +0000681 if (MI->getNumOperands() == 2 &&
Misha Brukmanc968b872004-06-29 19:28:53 +0000682 (!MI->getOperand(1).isRegister() ||
683 MI->getOperand(1).getVRegValueOrNull() ||
684 MI->getOperand(1).isGlobalAddress() ||
685 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner177e9282002-11-21 02:00:20 +0000686 O << ", ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000687 printOp(MI->getOperand(1));
Chris Lattner177e9282002-11-21 02:00:20 +0000688 }
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000689 printImplUsesAfter(Desc);
Chris Lattner177e9282002-11-21 02:00:20 +0000690 O << "\n";
691 return;
692 }
Chris Lattner6985c192002-11-21 01:33:44 +0000693 case X86II::MRMDestReg: {
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000694 // There are three forms of MRMDestReg instructions, those with 2
695 // or 3 operands:
Chris Lattner956e8372003-01-13 00:35:03 +0000696 //
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000697 // 2 Operands: this is for things like mov that do not read a
698 // second input.
Chris Lattner5812f062002-11-18 06:56:51 +0000699 //
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000700 // 2 Operands: two address instructions which def&use the first
701 // argument and use the second as input.
Chris Lattner5812f062002-11-18 06:56:51 +0000702 //
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000703 // 3 Operands: in this form, two address instructions are the same
704 // as in 2 but have a constant argument as well.
Chris Lattner5812f062002-11-18 06:56:51 +0000705 //
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000706 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerce351082002-12-15 08:01:39 +0000707 assert(MI->getOperand(0).isRegister() &&
Chris Lattner956e8372003-01-13 00:35:03 +0000708 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000709 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukman6e5d4932002-11-20 18:56:41 +0000710 && "Bad format for MRMDestReg!");
Chris Lattner5812f062002-11-18 06:56:51 +0000711
Chris Lattner0d10bf82004-02-11 19:26:28 +0000712 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000713 printOp(MI->getOperand(0));
Chris Lattner5812f062002-11-18 06:56:51 +0000714 O << ", ";
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000715 printOp(MI->getOperand(1));
716 if (MI->getNumOperands() == 3) {
Chris Lattner956e8372003-01-13 00:35:03 +0000717 O << ", ";
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000718 printOp(MI->getOperand(2));
Chris Lattner956e8372003-01-13 00:35:03 +0000719 }
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000720 printImplUsesAfter(Desc);
Chris Lattner5812f062002-11-18 06:56:51 +0000721 O << "\n";
722 return;
Chris Lattner6985c192002-11-21 01:33:44 +0000723 }
Chris Lattner4fbd8a22002-11-21 21:03:39 +0000724
725 case X86II::MRMDestMem: {
726 // These instructions are the same as MRMDestReg, but instead of having a
727 // register reference for the mod/rm field, it's a memory reference.
728 //
Chris Lattner288e0432004-02-17 06:16:44 +0000729 assert(isMem(MI, 0) &&
730 (MI->getNumOperands() == 4+1 ||
731 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
732 && "Bad format for MRMDestMem!");
Chris Lattner4fbd8a22002-11-21 21:03:39 +0000733
Chris Lattner0d10bf82004-02-11 19:26:28 +0000734 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000735 printMemReference(MI, 0);
Chris Lattner4fbd8a22002-11-21 21:03:39 +0000736 O << ", ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000737 printOp(MI->getOperand(4));
Chris Lattner288e0432004-02-17 06:16:44 +0000738 if (MI->getNumOperands() == 4+2) {
739 O << ", ";
740 printOp(MI->getOperand(5));
741 }
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000742 printImplUsesAfter(Desc);
Chris Lattner4fbd8a22002-11-21 21:03:39 +0000743 O << "\n";
744 return;
745 }
746
Chris Lattner6985c192002-11-21 01:33:44 +0000747 case X86II::MRMSrcReg: {
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000748 // There are three forms that are acceptable for MRMSrcReg
749 // instructions, those with 2 or 3 operands:
Chris Lattnerf03132f2002-11-21 00:30:01 +0000750 //
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000751 // 2 Operands: this is for things like mov that do not read a
752 // second input.
753 //
754 // 2 Operands: in this form, the last register is the ModR/M
755 // input. The first operand is a def&use. This is for things
756 // like: add r32, r/m32
Chris Lattnerf03132f2002-11-21 00:30:01 +0000757 //
Alkis Evlogimenosdbf4b422004-02-04 17:21:04 +0000758 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner818bcec2004-02-17 04:26:43 +0000759 // for instructions like the IMULrri instructions.
Chris Lattner97e1b552003-10-20 03:42:58 +0000760 //
Chris Lattnerf03132f2002-11-21 00:30:01 +0000761 //
Chris Lattnerce351082002-12-15 08:01:39 +0000762 assert(MI->getOperand(0).isRegister() &&
763 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000764 (MI->getNumOperands() == 2 ||
765 (MI->getNumOperands() == 3 &&
766 (MI->getOperand(2).isImmediate())))
Chris Lattner956e8372003-01-13 00:35:03 +0000767 && "Bad format for MRMSrcReg!");
Chris Lattnerf03132f2002-11-21 00:30:01 +0000768
Chris Lattner0d10bf82004-02-11 19:26:28 +0000769 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000770 printOp(MI->getOperand(0));
Chris Lattnerf03132f2002-11-21 00:30:01 +0000771 O << ", ";
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000772 printOp(MI->getOperand(1));
773 if (MI->getNumOperands() == 3) {
774 O << ", ";
775 printOp(MI->getOperand(2));
776 }
Chris Lattnerf03132f2002-11-21 00:30:01 +0000777 O << "\n";
778 return;
Chris Lattner6985c192002-11-21 01:33:44 +0000779 }
Chris Lattnerc8688412002-11-21 17:09:01 +0000780
Chris Lattner61fafd352002-11-21 20:44:15 +0000781 case X86II::MRMSrcMem: {
782 // These instructions are the same as MRMSrcReg, but instead of having a
783 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner4fbd8a22002-11-21 21:03:39 +0000784 //
Chris Lattnerce351082002-12-15 08:01:39 +0000785 assert(MI->getOperand(0).isRegister() &&
Misha Brukman887fd232004-06-29 19:43:20 +0000786 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
787 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
788 isMem(MI, 1)))
Alkis Evlogimenos32742642004-02-04 22:17:40 +0000789 && "Bad format for MRMSrcMem!");
Chris Lattner0d10bf82004-02-11 19:26:28 +0000790 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000791 printOp(MI->getOperand(0));
Chris Lattner956e8372003-01-13 00:35:03 +0000792 O << ", " << sizePtr(Desc) << " ";
Chris Lattnerc07eeae2004-02-17 07:40:44 +0000793 printMemReference(MI, 1);
794 if (MI->getNumOperands() == 2+4) {
795 O << ", ";
796 printOp(MI->getOperand(5));
797 }
Chris Lattner61fafd352002-11-21 20:44:15 +0000798 O << "\n";
799 return;
800 }
801
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000802 case X86II::MRM0r: case X86II::MRM1r:
803 case X86II::MRM2r: case X86II::MRM3r:
804 case X86II::MRM4r: case X86II::MRM5r:
805 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattnerc8688412002-11-21 17:09:01 +0000806 // In this form, the following are valid formats:
807 // 1. sete r
Chris Lattnere5330c42002-11-21 23:30:00 +0000808 // 2. cmp reg, immediate
Chris Lattnerc8688412002-11-21 17:09:01 +0000809 // 2. shl rdest, rinput <implicit CL or 1>
810 // 3. sbb rdest, rinput, immediate [rdest = rinput]
811 //
812 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerce351082002-12-15 08:01:39 +0000813 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattnere5330c42002-11-21 23:30:00 +0000814 assert((MI->getNumOperands() != 2 ||
Chris Lattnerce351082002-12-15 08:01:39 +0000815 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattnerc8688412002-11-21 17:09:01 +0000816 "Bad MRMSxR format!");
Chris Lattnere5330c42002-11-21 23:30:00 +0000817 assert((MI->getNumOperands() < 3 ||
Misha Brukmanc968b872004-06-29 19:28:53 +0000818 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattnerc8688412002-11-21 17:09:01 +0000819 "Bad MRMSxR format!");
820
Chris Lattnerce351082002-12-15 08:01:39 +0000821 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattnerc8688412002-11-21 17:09:01 +0000822 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
823 O << "**";
824
Chris Lattner0d10bf82004-02-11 19:26:28 +0000825 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000826 printOp(MI->getOperand(0));
Chris Lattnerce351082002-12-15 08:01:39 +0000827 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattnerc8688412002-11-21 17:09:01 +0000828 O << ", ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000829 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattnerc8688412002-11-21 17:09:01 +0000830 }
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000831 printImplUsesAfter(Desc);
Chris Lattnerc8688412002-11-21 17:09:01 +0000832 O << "\n";
833
834 return;
835 }
836
Alkis Evlogimenos58270fc2004-02-27 18:55:12 +0000837 case X86II::MRM0m: case X86II::MRM1m:
838 case X86II::MRM2m: case X86II::MRM3m:
839 case X86II::MRM4m: case X86II::MRM5m:
840 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattner956e8372003-01-13 00:35:03 +0000841 // In this form, the following are valid formats:
842 // 1. sete [m]
843 // 2. cmp [m], immediate
844 // 2. shl [m], rinput <implicit CL or 1>
845 // 3. sbb [m], immediate
846 //
847 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
848 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattner128937b2003-12-01 05:13:56 +0000849 assert((MI->getNumOperands() != 5 ||
850 (MI->getOperand(4).isImmediate() ||
851 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattner956e8372003-01-13 00:35:03 +0000852 "Bad MRMSxM format!");
Chris Lattner128937b2003-12-01 05:13:56 +0000853
854 const MachineOperand &Op3 = MI->getOperand(3);
855
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +0000856 // gas bugs:
857 //
858 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke5b049872003-07-11 18:18:35 +0000859 // is misassembled by gas in intel_syntax mode as its 32-bit
860 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
861 // opcode bytes instead of the instruction.
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +0000862 //
863 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke5b049872003-07-11 18:18:35 +0000864 // misassembled by gas in intel_syntax mode as its 32-bit
865 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
866 // opcode bytes instead of the instruction.
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +0000867 //
868 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke5b049872003-07-11 18:18:35 +0000869 // invalid opcode, saying "64 bit operations are only supported in
870 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
871 // [...]", which is wrong. Workaround: Output the raw opcode bytes
872 // instead of the instruction.
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +0000873 //
874 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
875 // invalid opcode, saying "64 bit operations are only supported in
876 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
877 // [...]", which is wrong. Workaround: Output the raw opcode bytes
878 // instead of the instruction.
879 if (MI->getOpcode() == X86::FSTP80m ||
880 MI->getOpcode() == X86::FLD80m ||
881 MI->getOpcode() == X86::FILD64m ||
882 MI->getOpcode() == X86::FISTP64m) {
Misha Brukmana332a6462004-07-26 18:48:58 +0000883 GasBugWorkaroundEmitter gwe(O);
884 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke5b049872003-07-11 18:18:35 +0000885 }
Chris Lattner128937b2003-12-01 05:13:56 +0000886
Chris Lattner0d10bf82004-02-11 19:26:28 +0000887 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner956e8372003-01-13 00:35:03 +0000888 O << sizePtr(Desc) << " ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000889 printMemReference(MI, 0);
Chris Lattner956e8372003-01-13 00:35:03 +0000890 if (MI->getNumOperands() == 5) {
891 O << ", ";
Brian Gaekea92dce42003-07-23 20:25:08 +0000892 printOp(MI->getOperand(4));
Chris Lattner956e8372003-01-13 00:35:03 +0000893 }
Chris Lattnercbb4ed92004-03-31 22:02:21 +0000894 printImplUsesAfter(Desc);
Chris Lattner956e8372003-01-13 00:35:03 +0000895 O << "\n";
896 return;
897 }
Chris Lattner5812f062002-11-18 06:56:51 +0000898 default:
Tanya Lattner23dbc812004-06-25 00:13:11 +0000899 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattner5812f062002-11-18 06:56:51 +0000900 }
Chris Lattnerd92fb002002-10-25 22:55:53 +0000901}
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000902
Chris Lattner9520d202004-08-01 06:02:08 +0000903bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner9a59f582003-08-11 19:35:26 +0000904 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke12b32532003-08-11 19:05:46 +0000905 //
Chris Lattner9a59f582003-08-11 19:35:26 +0000906 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
907 // instruction as a reference to the register named sp, and if you try to
908 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
909 // before being looked up in the symbol table. This creates spurious
910 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
911 // mode, and decorate all register names with percent signs.
Chris Lattner262c8322003-08-11 20:04:57 +0000912 O << "\t.intel_syntax\n";
Chris Lattner9a59f582003-08-11 19:35:26 +0000913 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000914 return false; // success
915}
916
Chris Lattner230cffb2003-09-09 16:23:36 +0000917// SwitchSection - Switch to the specified section of the executable if we are
918// not already in it!
919//
920static void SwitchSection(std::ostream &OS, std::string &CurSection,
921 const char *NewSection) {
922 if (CurSection != NewSection) {
923 CurSection = NewSection;
924 if (!CurSection.empty())
925 OS << "\t" << NewSection << "\n";
926 }
Brian Gaekeb99d6842003-07-11 21:57:01 +0000927}
928
Chris Lattner9520d202004-08-01 06:02:08 +0000929bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000930 const TargetData &TD = TM.getTargetData();
Chris Lattner230cffb2003-09-09 16:23:36 +0000931 std::string CurSection;
932
Brian Gaeke25e766a2003-06-25 18:01:07 +0000933 // Print out module-level global variables here.
Chris Lattner230cffb2003-09-09 16:23:36 +0000934 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
935 if (I->hasInitializer()) { // External global require no code
936 O << "\n\n";
937 std::string name = Mang->getValueName(I);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000938 Constant *C = I->getInitializer();
Chris Lattner230cffb2003-09-09 16:23:36 +0000939 unsigned Size = TD.getTypeSize(C->getType());
940 unsigned Align = TD.getTypeAlignment(C->getType());
941
942 if (C->isNullValue() &&
Chris Lattner068ad842003-10-16 18:29:00 +0000943 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
944 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner230cffb2003-09-09 16:23:36 +0000945 SwitchSection(O, CurSection, ".data");
946 if (I->hasInternalLinkage())
947 O << "\t.local " << name << "\n";
948
949 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner5dcb6542003-08-03 23:37:09 +0000950 << "," << (unsigned)TD.getTypeAlignment(C->getType());
951 O << "\t\t# ";
952 WriteAsOperand(O, I, true, true, &M);
953 O << "\n";
Brian Gaekeb99d6842003-07-11 21:57:01 +0000954 } else {
Chris Lattner230cffb2003-09-09 16:23:36 +0000955 switch (I->getLinkage()) {
956 case GlobalValue::LinkOnceLinkage:
Chris Lattner068ad842003-10-16 18:29:00 +0000957 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattner230cffb2003-09-09 16:23:36 +0000958 // Nonnull linkonce -> weak
959 O << "\t.weak " << name << "\n";
960 SwitchSection(O, CurSection, "");
961 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
962 break;
963
964 case GlobalValue::AppendingLinkage:
965 // FIXME: appending linkage variables should go into a section of
966 // their name or something. For now, just emit them as external.
967 case GlobalValue::ExternalLinkage:
968 // If external or appending, declare as a global symbol
969 O << "\t.globl " << name << "\n";
970 // FALL THROUGH
971 case GlobalValue::InternalLinkage:
972 if (C->isNullValue())
973 SwitchSection(O, CurSection, ".bss");
974 else
975 SwitchSection(O, CurSection, ".data");
976 break;
977 }
978
979 O << "\t.align " << Align << "\n";
Chris Lattner5dcb6542003-08-03 23:37:09 +0000980 O << "\t.type " << name << ",@object\n";
Chris Lattner230cffb2003-09-09 16:23:36 +0000981 O << "\t.size " << name << "," << Size << "\n";
Chris Lattner5dcb6542003-08-03 23:37:09 +0000982 O << name << ":\t\t\t\t# ";
983 WriteAsOperand(O, I, true, true, &M);
984 O << " = ";
985 WriteAsOperand(O, C, false, false, &M);
986 O << "\n";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000987 emitGlobalConstant(C);
Brian Gaekeb99d6842003-07-11 21:57:01 +0000988 }
Brian Gaeke25e766a2003-06-25 18:01:07 +0000989 }
Chris Lattner230cffb2003-09-09 16:23:36 +0000990
Brian Gaeke46f8b712003-07-24 20:20:44 +0000991 delete Mang;
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000992 return false; // success
993}