blob: 5bc4a488891a42536edc201f592a8b06fcd6aac2 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Misha Brukman538607f2004-03-01 23:53:11 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Intel-format assembly language. This
12// printer is the output mechanism used by `llc' and `lli -print-machineinstrs'
13// on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattnere0121322003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000027#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerb12ee502004-08-01 07:43:46 +000028#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000029#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000030#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000031#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000032#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000033#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000037 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Chris Lattner93c1afa2003-08-11 19:35:26 +000039 // FIXME: This should be automatically picked up by autoconf from the C
40 // frontend
41 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
42 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
43
Alkis Evlogimenos03090662004-03-09 03:35:34 +000044 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukman8606aea2004-07-26 18:48:58 +000045 GasBugWorkaroundEmitter(std::ostream& o)
46 : O(o), OldFlags(O.flags()), firstByte(true) {
47 O << std::hex;
48 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000049
Misha Brukman8606aea2004-07-26 18:48:58 +000050 ~GasBugWorkaroundEmitter() {
51 O.flags(OldFlags);
52 O << "\t# ";
53 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000054
Misha Brukman8606aea2004-07-26 18:48:58 +000055 virtual void emitByte(unsigned char B) {
56 if (!firstByte) O << "\n\t";
57 firstByte = false;
58 O << ".byte 0x" << (unsigned) B;
59 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000060
Misha Brukman8606aea2004-07-26 18:48:58 +000061 // These should never be called
62 virtual void emitWord(unsigned W) { assert(0); }
63 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
64 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
65 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
66 virtual uint64_t getCurrentPCValue() { abort(); }
67 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000068
69 private:
Misha Brukman8606aea2004-07-26 18:48:58 +000070 std::ostream& O;
71 std::ios::fmtflags OldFlags;
72 bool firstByte;
Alkis Evlogimenos03090662004-03-09 03:35:34 +000073 };
74
Chris Lattner3fa861a2004-08-01 06:02:08 +000075 struct X86AsmPrinter : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000076 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000077 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000078 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000079
80 /// Target machine description which we query for reg. names, data
81 /// layout, etc.
82 ///
83 TargetMachine &TM;
84
Brian Gaeked9fb37a2003-07-24 20:20:44 +000085 /// Name-mangler for global names.
86 ///
87 Mangler *Mang;
88
Chris Lattner3fa861a2004-08-01 06:02:08 +000089 X86AsmPrinter(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000090
Brian Gaeke92bdfe62003-07-23 18:37:06 +000091 /// Cache of mangled name for current function. This is
92 /// recalculated at the beginning of each call to
93 /// runOnMachineFunction().
94 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000095 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000096
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000097 virtual const char *getPassName() const {
98 return "X86 Assembly Printer";
99 }
100
Chris Lattner3fa861a2004-08-01 06:02:08 +0000101 /// printInstruction - This method is automatically generated by tablegen
102 /// from the instruction set description. This method returns true if the
103 /// machine instruction was sufficiently described to print it, otherwise it
104 /// returns false.
105 bool printInstruction(const MachineInstr *MI);
106
Chris Lattnerb12ee502004-08-01 07:43:46 +0000107 // This method is used by the tablegen'erated instruction printer.
108 void printOperand(const MachineOperand &MO, MVT::ValueType VT) {
Chris Lattner25369cf2004-08-01 08:12:41 +0000109 if (MO.getType() == MachineOperand::MO_MachineRegister) {
110 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
111 // Bug Workaround: See note in Printer::doInitialization about %.
112 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
113 } else {
114 printOp(MO);
115 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000116 }
117
John Criswell4ffff9e2004-04-08 20:31:47 +0000118 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000119 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000120 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000121 void printMemReference(const MachineInstr *MI, unsigned Op);
122 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000124 bool doInitialization(Module &M);
125 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000126 void emitGlobalConstant(const Constant* CV);
127 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000128 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000129} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000130
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000131/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000132/// assembly code for a MachineFunction to the given output stream,
133/// using the given target machine description. This should work
134/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000135///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000136FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000137 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000138}
139
Chris Lattner3fa861a2004-08-01 06:02:08 +0000140
141// Include the auto-generated portion of the assembly writer.
142#include "X86GenAsmWriter.inc"
143
144
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000145/// toOctal - Convert the low order bits of X into an octal digit.
146///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147static inline char toOctal(int X) {
148 return (X&7)+'0';
149}
150
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000151/// getAsCString - Return the specified array as a C compatible
152/// string, only if the predicate isStringCompatible is true.
153///
Chris Lattnerac662d12003-11-03 20:19:49 +0000154static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000155 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000156
Chris Lattnerac662d12003-11-03 20:19:49 +0000157 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000158 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000159 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000160
161 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000162 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000163 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000164 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000166 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167 } else {
168 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000169 case '\b': O << "\\b"; break;
170 case '\f': O << "\\f"; break;
171 case '\n': O << "\\n"; break;
172 case '\r': O << "\\r"; break;
173 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000175 O << '\\';
176 O << toOctal(C >> 6);
177 O << toOctal(C >> 3);
178 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179 break;
180 }
181 }
182 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000183 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000184}
185
Chris Lattnerac662d12003-11-03 20:19:49 +0000186// Print out the specified constant, without a storage class. Only the
187// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000188void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000189 if (CV->isNullValue())
190 O << "0";
191 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
192 assert(CB == ConstantBool::True);
193 O << "1";
194 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000195 if (((CI->getValue() << 32) >> 32) == CI->getValue())
196 O << CI->getValue();
197 else
198 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000199 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
200 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000201 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 // This is a constant address for a global variable or function. Use the
203 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000204 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000205 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
206 const TargetData &TD = TM.getTargetData();
207 switch(CE->getOpcode()) {
208 case Instruction::GetElementPtr: {
209 // generate a symbolic expression for the byte address
210 const Constant *ptrVal = CE->getOperand(0);
211 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
212 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
213 O << "(";
214 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000215 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000216 } else {
217 emitConstantValueOnly(ptrVal);
218 }
219 break;
220 }
221 case Instruction::Cast: {
222 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000223 // that do not involve a change in value. This assertion is really gross,
224 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000225 Constant *Op = CE->getOperand(0);
226 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000227
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000228 // Remember, kids, pointers on x86 can be losslessly converted back and
229 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000230 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000231 && (Ty == Type::LongTy || Ty == Type::ULongTy
232 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000233 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000234 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
235 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000236 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
237 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000238 && "FIXME: Don't yet support this kind of constant cast expr");
239 O << "(";
240 emitConstantValueOnly(Op);
241 O << ")";
242 break;
243 }
244 case Instruction::Add:
245 O << "(";
246 emitConstantValueOnly(CE->getOperand(0));
247 O << ") + (";
248 emitConstantValueOnly(CE->getOperand(1));
249 O << ")";
250 break;
251 default:
252 assert(0 && "Unsupported operator!");
253 }
254 } else {
255 assert(0 && "Unknown constant value!");
256 }
257}
258
259// Print a constant value or values, with the appropriate storage class as a
260// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000261void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000262 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000263
Chris Lattnerad200712003-09-09 16:23:36 +0000264 if (CV->isNullValue()) {
265 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000266 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000267 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000268 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000269 O << "\t.ascii\t";
270 printAsCString(O, CVA);
271 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000272 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000273 const std::vector<Use> &constValues = CVA->getValues();
274 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000275 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000276 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000277 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000278 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
279 // Print the fields in successive locations. Pad to align if needed!
280 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
281 const std::vector<Use>& constValues = CVS->getValues();
282 unsigned sizeSoFar = 0;
283 for (unsigned i=0, N = constValues.size(); i < N; i++) {
284 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000285
Chris Lattnerad200712003-09-09 16:23:36 +0000286 // Check if padding is needed and insert one or more 0s.
287 unsigned fieldSize = TD.getTypeSize(field->getType());
288 unsigned padSize = ((i == N-1? cvsLayout->StructSize
289 : cvsLayout->MemberOffsets[i+1])
290 - cvsLayout->MemberOffsets[i]) - fieldSize;
291 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000292
Chris Lattnerad200712003-09-09 16:23:36 +0000293 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000294 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000295
296 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000297 if (padSize)
298 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000299 }
Chris Lattnerad200712003-09-09 16:23:36 +0000300 assert(sizeSoFar == cvsLayout->StructSize &&
301 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000302 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000303 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
304 // FP Constants are printed as integer constants to avoid losing
305 // precision...
306 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000307 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000308 default: assert(0 && "Unknown floating point type!");
309 case Type::FloatTyID: {
310 union FU { // Abide by C TBAA rules
311 float FVal;
312 unsigned UVal;
313 } U;
314 U.FVal = Val;
315 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
316 return;
317 }
318 case Type::DoubleTyID: {
319 union DU { // Abide by C TBAA rules
320 double FVal;
321 uint64_t UVal;
322 } U;
323 U.FVal = Val;
324 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
325 return;
326 }
327 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000328 }
329
330 const Type *type = CV->getType();
331 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000332 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000333 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
334 O << ".byte";
335 break;
336 case Type::UShortTyID: case Type::ShortTyID:
337 O << ".word";
338 break;
339 case Type::FloatTyID: case Type::PointerTyID:
340 case Type::UIntTyID: case Type::IntTyID:
341 O << ".long";
342 break;
343 case Type::DoubleTyID:
344 case Type::ULongTyID: case Type::LongTyID:
345 O << ".quad";
346 break;
347 default:
348 assert (0 && "Can't handle printing this type of thing");
349 break;
350 }
351 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000352 emitConstantValueOnly(CV);
353 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000354}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000355
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000356/// printConstantPool - Print to the current output stream assembly
357/// representations of the constants in the constant pool MCP. This is
358/// used to print out constants which have been "spilled to memory" by
359/// the code generator.
360///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000361void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000362 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000363 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000364
Chris Lattnerb7089442003-01-13 00:35:03 +0000365 if (CP.empty()) return;
366
367 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
368 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000369 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000370 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000371 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
372 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000373 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000374 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000375}
376
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000377/// runOnMachineFunction - This uses the printMachineInstruction()
378/// method to print assembly for each instruction.
379///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000380bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000381 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000382 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000383 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000384
Chris Lattnerb7089442003-01-13 00:35:03 +0000385 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000386 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000387
Brian Gaeke6559bb92002-11-14 22:32:30 +0000388 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000389 O << "\t.text\n";
390 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000391 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000392 if (!EmitCygwin)
393 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000394 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000395
396 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000397 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
398 I != E; ++I) {
399 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000400 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000401 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000402 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000403 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000404 // Print the assembly for the instruction.
405 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000406 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000407 }
Chris Lattner0285a332002-12-28 20:25:38 +0000408 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409
410 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000411 return false;
412}
413
Chris Lattner3d3067b2002-11-21 20:44:15 +0000414static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000415 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000416 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
417 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000418}
419
420static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000421 if (MI->getOperand(Op).isFrameIndex()) return true;
422 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000423 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000424 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
425 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000426}
427
Brian Gaeke2a098772003-08-11 19:05:46 +0000428
429
Chris Lattner3fa861a2004-08-01 06:02:08 +0000430void X86AsmPrinter::printOp(const MachineOperand &MO,
431 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000432 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000433 switch (MO.getType()) {
434 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000435 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000436 O << "<" << V->getName() << ">";
437 return;
438 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000439 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000440 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000441 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000442 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000443 O << "%" << RI.get(MO.getReg()).Name;
444 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000445 O << "%reg" << MO.getReg();
446 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000447
448 case MachineOperand::MO_SignExtendedImmed:
449 case MachineOperand::MO_UnextendedImmed:
450 O << (int)MO.getImmedValue();
451 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000452 case MachineOperand::MO_MachineBasicBlock: {
453 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
454 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
455 << "_" << MBBOp->getNumber () << "\t# "
456 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000457 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000458 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000459 case MachineOperand::MO_PCRelativeDisp:
460 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
461 abort ();
462 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000463 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000464 if (!elideOffsetKeyword)
465 O << "OFFSET ";
466 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000467 return;
468 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000469 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000470 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000471 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000472 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000473 }
474}
475
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000476static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
477 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000478 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000479 case X86II::Mem8: return "BYTE PTR";
480 case X86II::Mem16: return "WORD PTR";
481 case X86II::Mem32: return "DWORD PTR";
482 case X86II::Mem64: return "QWORD PTR";
483 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000484 }
485}
486
Chris Lattner3fa861a2004-08-01 06:02:08 +0000487void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000488 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000489
490 if (MI->getOperand(Op).isFrameIndex()) {
491 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
492 if (MI->getOperand(Op+3).getImmedValue())
493 O << " + " << MI->getOperand(Op+3).getImmedValue();
494 O << "]";
495 return;
496 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000497 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000498 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000499 if (MI->getOperand(Op+3).getImmedValue())
500 O << " + " << MI->getOperand(Op+3).getImmedValue();
501 O << "]";
502 return;
503 }
504
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000506 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000507 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000508 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000509
510 O << "[";
511 bool NeedPlus = false;
512 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000513 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000514 NeedPlus = true;
515 }
516
517 if (IndexReg.getReg()) {
518 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000519 if (ScaleVal != 1)
520 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000521 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000522 NeedPlus = true;
523 }
524
Chris Lattner0285a332002-12-28 20:25:38 +0000525 if (DispVal) {
526 if (NeedPlus)
527 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000528 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000529 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000530 O << " - ";
531 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000532 }
533 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000534 }
535 O << "]";
536}
537
Chris Lattner30b2f722004-03-31 22:02:21 +0000538/// printImplUsesAfter - Emit the implicit-use registers for the instruction
539/// described by DESC, if its PrintImplUsesAfter flag is set.
540///
John Criswell4ffff9e2004-04-08 20:31:47 +0000541/// Inputs:
542/// Comma - List of registers will need a leading comma.
543/// Desc - Description of the Instruction.
544///
545/// Return value:
546/// true - Emitted one or more registers.
547/// false - Emitted no registers.
548///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000549bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
550 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000551 const MRegisterInfo &RI = *TM.getRegisterInfo();
552 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000553 bool emitted = false;
554 const unsigned *p = Desc.ImplicitUses;
555 if (*p) {
556 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
557 emitted = true;
558 ++p;
559 }
560 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000561 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000562 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000563 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000564 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000565 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000566 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000567 return false;
568}
569
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000570/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000571/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000572///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000573void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
574 ++EmittedInsts;
575 if (printInstruction(MI))
576 return; // Printer was automatically generated
577
Chris Lattnerf9f60882002-11-18 06:56:51 +0000578 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000579 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000580 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000581
Chris Lattnereca1f632002-12-25 05:09:01 +0000582 switch (Desc.TSFlags & X86II::FormMask) {
583 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000584 // Print pseudo-instructions as comments; either they should have been
585 // turned into real instructions by now, or they don't need to be
586 // seen by the assembler (e.g., IMPLICIT_USEs.)
587 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000588 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000589 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000590 O << " = phi ";
591 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000592 if (i != 1) O << ", ";
593 O << "[";
594 printOp(MI->getOperand(i));
595 O << ", ";
596 printOp(MI->getOperand(i+1));
597 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000598 }
599 } else {
600 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000601 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000602 printOp(MI->getOperand(0));
603 O << " = ";
604 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000605 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000606 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000607
608 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000609 O << " ";
610 if (MI->getOperand(i).isDef()) O << "*";
611 printOp(MI->getOperand(i));
612 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000613 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000614 }
615 O << "\n";
616 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000617
Chris Lattnerf9f60882002-11-18 06:56:51 +0000618 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000619 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000620 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000621 // 1. jmp foo - MachineBasicBlock operand
622 // 2. call bar - GlobalAddress Operand or External Symbol Operand
623 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000624 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000625 assert(MI->getNumOperands() == 1 &&
626 (MI->getOperand(0).isMachineBasicBlock() ||
627 MI->getOperand(0).isGlobalAddress() ||
628 MI->getOperand(0).isExternalSymbol() ||
629 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000630 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000631 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000632
Chris Lattner1626c502004-08-01 08:22:29 +0000633 bool LeadingComma = false;
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000634 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000635 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000636 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000637 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000638 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000639 O << "\n";
640 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000641 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642
Chris Lattner77875d82002-11-21 02:00:20 +0000643 case X86II::AddRegFrm: {
644 // There are currently two forms of acceptable AddRegFrm instructions.
645 // Either the instruction JUST takes a single register (like inc, dec, etc),
646 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000647 // (move immediate f.e.). Note that this immediate value might be stored as
648 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000649 // into a register. The initial register might be duplicated if this is a
650 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000651 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000652 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000653 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000654 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000655 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000656 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000657 MI->getOperand(1).isRegister() ||
658 MI->getOperand(1).isGlobalAddress() ||
659 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000660 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000661
Chris Lattner77875d82002-11-21 02:00:20 +0000662 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000663
Chris Lattnerb009c002004-02-11 19:26:28 +0000664 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000665
Brian Gaekede420ae2003-07-23 20:25:08 +0000666 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000667 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000668 (!MI->getOperand(1).isRegister() ||
669 MI->getOperand(1).getVRegValueOrNull() ||
670 MI->getOperand(1).isGlobalAddress() ||
671 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000672 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000673 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000674 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000675 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000676 O << "\n";
677 return;
678 }
Chris Lattner233ad712002-11-21 01:33:44 +0000679 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000680 // There are three forms of MRMDestReg instructions, those with 2
681 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000682 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000683 // 2 Operands: this is for things like mov that do not read a
684 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000685 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000686 // 2 Operands: two address instructions which def&use the first
687 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000688 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000689 // 3 Operands: in this form, two address instructions are the same
690 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000691 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000692 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000693 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000694 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000695 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000696 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000697
Chris Lattnerb009c002004-02-11 19:26:28 +0000698 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000699 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000700 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000701 printOp(MI->getOperand(1));
702 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000703 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000704 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000705 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000706 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000707 O << "\n";
708 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000709 }
Chris Lattner18042332002-11-21 21:03:39 +0000710
711 case X86II::MRMDestMem: {
712 // These instructions are the same as MRMDestReg, but instead of having a
713 // register reference for the mod/rm field, it's a memory reference.
714 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000715 assert(isMem(MI, 0) &&
716 (MI->getNumOperands() == 4+1 ||
717 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
718 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000719
Chris Lattnerb009c002004-02-11 19:26:28 +0000720 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000722 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000723 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000724 if (MI->getNumOperands() == 4+2) {
725 O << ", ";
726 printOp(MI->getOperand(5));
727 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000728 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000729 O << "\n";
730 return;
731 }
732
Chris Lattner233ad712002-11-21 01:33:44 +0000733 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000734 // There are three forms that are acceptable for MRMSrcReg
735 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000736 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000737 // 2 Operands: this is for things like mov that do not read a
738 // second input.
739 //
740 // 2 Operands: in this form, the last register is the ModR/M
741 // input. The first operand is a def&use. This is for things
742 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000743 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000744 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000745 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000746 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000747 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000748 assert(MI->getOperand(0).isRegister() &&
749 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000750 (MI->getNumOperands() == 2 ||
751 (MI->getNumOperands() == 3 &&
752 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000753 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000754
Chris Lattnerb009c002004-02-11 19:26:28 +0000755 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000756 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000757 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000758 printOp(MI->getOperand(1));
759 if (MI->getNumOperands() == 3) {
760 O << ", ";
761 printOp(MI->getOperand(2));
762 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000763 O << "\n";
764 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000765 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000766
Chris Lattner3d3067b2002-11-21 20:44:15 +0000767 case X86II::MRMSrcMem: {
768 // These instructions are the same as MRMSrcReg, but instead of having a
769 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000770 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000771 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000772 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
773 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
774 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000775 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000776 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000777 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000778 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000779 printMemReference(MI, 1);
780 if (MI->getNumOperands() == 2+4) {
781 O << ", ";
782 printOp(MI->getOperand(5));
783 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000784 O << "\n";
785 return;
786 }
787
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000788 case X86II::MRM0r: case X86II::MRM1r:
789 case X86II::MRM2r: case X86II::MRM3r:
790 case X86II::MRM4r: case X86II::MRM5r:
791 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000792 // In this form, the following are valid formats:
793 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000794 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000795 // 2. shl rdest, rinput <implicit CL or 1>
796 // 3. sbb rdest, rinput, immediate [rdest = rinput]
797 //
798 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000799 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000800 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000801 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000802 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000803 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000804 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000805 "Bad MRMSxR format!");
806
Chris Lattnerd9096832002-12-15 08:01:39 +0000807 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000808 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
809 O << "**";
810
Chris Lattnerb009c002004-02-11 19:26:28 +0000811 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000812 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000813 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000814 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000815 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000816 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000817 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000818 O << "\n";
819
820 return;
821 }
822
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000823 case X86II::MRM0m: case X86II::MRM1m:
824 case X86II::MRM2m: case X86II::MRM3m:
825 case X86II::MRM4m: case X86II::MRM5m:
826 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000827 // In this form, the following are valid formats:
828 // 1. sete [m]
829 // 2. cmp [m], immediate
830 // 2. shl [m], rinput <implicit CL or 1>
831 // 3. sbb [m], immediate
832 //
833 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
834 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000835 assert((MI->getNumOperands() != 5 ||
836 (MI->getOperand(4).isImmediate() ||
837 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000838 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000839
840 const MachineOperand &Op3 = MI->getOperand(3);
841
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000842 // gas bugs:
843 //
844 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000845 // is misassembled by gas in intel_syntax mode as its 32-bit
846 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
847 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000848 //
849 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000850 // misassembled by gas in intel_syntax mode as its 32-bit
851 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
852 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000853 //
854 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000855 // invalid opcode, saying "64 bit operations are only supported in
856 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
857 // [...]", which is wrong. Workaround: Output the raw opcode bytes
858 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000859 //
860 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
861 // invalid opcode, saying "64 bit operations are only supported in
862 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
863 // [...]", which is wrong. Workaround: Output the raw opcode bytes
864 // instead of the instruction.
865 if (MI->getOpcode() == X86::FSTP80m ||
866 MI->getOpcode() == X86::FLD80m ||
867 MI->getOpcode() == X86::FILD64m ||
868 MI->getOpcode() == X86::FISTP64m) {
Misha Brukman8606aea2004-07-26 18:48:58 +0000869 GasBugWorkaroundEmitter gwe(O);
870 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000871 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000872
Chris Lattnerb009c002004-02-11 19:26:28 +0000873 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000874 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000875 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000876 if (MI->getNumOperands() == 5) {
877 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000878 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000879 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000880 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000881 O << "\n";
882 return;
883 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000884 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000885 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000886 }
Chris Lattner72614082002-10-25 22:55:53 +0000887}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000888
Chris Lattner3fa861a2004-08-01 06:02:08 +0000889bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000890 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000891 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000892 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
893 // instruction as a reference to the register named sp, and if you try to
894 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
895 // before being looked up in the symbol table. This creates spurious
896 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
897 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000898 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000899 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000900 return false; // success
901}
902
Chris Lattnerad200712003-09-09 16:23:36 +0000903// SwitchSection - Switch to the specified section of the executable if we are
904// not already in it!
905//
906static void SwitchSection(std::ostream &OS, std::string &CurSection,
907 const char *NewSection) {
908 if (CurSection != NewSection) {
909 CurSection = NewSection;
910 if (!CurSection.empty())
911 OS << "\t" << NewSection << "\n";
912 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000913}
914
Chris Lattner3fa861a2004-08-01 06:02:08 +0000915bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000916 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000917 std::string CurSection;
918
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000919 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000920 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
921 if (I->hasInitializer()) { // External global require no code
922 O << "\n\n";
923 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000924 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000925 unsigned Size = TD.getTypeSize(C->getType());
926 unsigned Align = TD.getTypeAlignment(C->getType());
927
928 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000929 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
930 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000931 SwitchSection(O, CurSection, ".data");
932 if (I->hasInternalLinkage())
933 O << "\t.local " << name << "\n";
934
935 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000936 << "," << (unsigned)TD.getTypeAlignment(C->getType());
937 O << "\t\t# ";
938 WriteAsOperand(O, I, true, true, &M);
939 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000940 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000941 switch (I->getLinkage()) {
942 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000943 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000944 // Nonnull linkonce -> weak
945 O << "\t.weak " << name << "\n";
946 SwitchSection(O, CurSection, "");
947 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
948 break;
949
950 case GlobalValue::AppendingLinkage:
951 // FIXME: appending linkage variables should go into a section of
952 // their name or something. For now, just emit them as external.
953 case GlobalValue::ExternalLinkage:
954 // If external or appending, declare as a global symbol
955 O << "\t.globl " << name << "\n";
956 // FALL THROUGH
957 case GlobalValue::InternalLinkage:
958 if (C->isNullValue())
959 SwitchSection(O, CurSection, ".bss");
960 else
961 SwitchSection(O, CurSection, ".data");
962 break;
963 }
964
965 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000966 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000967 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000968 O << name << ":\t\t\t\t# ";
969 WriteAsOperand(O, I, true, true, &M);
970 O << " = ";
971 WriteAsOperand(O, C, false, false, &M);
972 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000973 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000974 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000975 }
Chris Lattnerad200712003-09-09 16:23:36 +0000976
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000977 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000978 return false; // success
979}