blob: 09913c271891d1cfaaada67da3e6c71600f2906e [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
Chris Lattner30b2f722004-03-31 22:02:21 +0000118 void printImplUsesBefore(const TargetInstrDescriptor &Desc);
Chris Lattner26653832004-04-13 17:18:39 +0000119 bool printImplDefsBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000120 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
121 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000122 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000123 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000124 void printMemReference(const MachineInstr *MI, unsigned Op);
125 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000127 bool doInitialization(Module &M);
128 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000129 void emitGlobalConstant(const Constant* CV);
130 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000131 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000132} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000133
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000134/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000135/// assembly code for a MachineFunction to the given output stream,
136/// using the given target machine description. This should work
137/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000138///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000139FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000140 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000141}
142
Chris Lattner3fa861a2004-08-01 06:02:08 +0000143
144// Include the auto-generated portion of the assembly writer.
145#include "X86GenAsmWriter.inc"
146
147
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000148/// toOctal - Convert the low order bits of X into an octal digit.
149///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150static inline char toOctal(int X) {
151 return (X&7)+'0';
152}
153
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000154/// getAsCString - Return the specified array as a C compatible
155/// string, only if the predicate isStringCompatible is true.
156///
Chris Lattnerac662d12003-11-03 20:19:49 +0000157static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000158 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000159
Chris Lattnerac662d12003-11-03 20:19:49 +0000160 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000161 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000162 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000163
164 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000165 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000167 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000168 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000169 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170 } else {
171 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000172 case '\b': O << "\\b"; break;
173 case '\f': O << "\\f"; break;
174 case '\n': O << "\\n"; break;
175 case '\r': O << "\\r"; break;
176 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000177 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000178 O << '\\';
179 O << toOctal(C >> 6);
180 O << toOctal(C >> 3);
181 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000182 break;
183 }
184 }
185 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000186 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000187}
188
Chris Lattnerac662d12003-11-03 20:19:49 +0000189// Print out the specified constant, without a storage class. Only the
190// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000191void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000192 if (CV->isNullValue())
193 O << "0";
194 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
195 assert(CB == ConstantBool::True);
196 O << "1";
197 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000198 if (((CI->getValue() << 32) >> 32) == CI->getValue())
199 O << CI->getValue();
200 else
201 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
203 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000204 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000205 // This is a constant address for a global variable or function. Use the
206 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000207 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000208 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
209 const TargetData &TD = TM.getTargetData();
210 switch(CE->getOpcode()) {
211 case Instruction::GetElementPtr: {
212 // generate a symbolic expression for the byte address
213 const Constant *ptrVal = CE->getOperand(0);
214 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
215 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
216 O << "(";
217 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000218 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000219 } else {
220 emitConstantValueOnly(ptrVal);
221 }
222 break;
223 }
224 case Instruction::Cast: {
225 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000226 // that do not involve a change in value. This assertion is really gross,
227 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000228 Constant *Op = CE->getOperand(0);
229 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000230
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000231 // Remember, kids, pointers on x86 can be losslessly converted back and
232 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000233 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000234 && (Ty == Type::LongTy || Ty == Type::ULongTy
235 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000236 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000237 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
238 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000239 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
240 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000241 && "FIXME: Don't yet support this kind of constant cast expr");
242 O << "(";
243 emitConstantValueOnly(Op);
244 O << ")";
245 break;
246 }
247 case Instruction::Add:
248 O << "(";
249 emitConstantValueOnly(CE->getOperand(0));
250 O << ") + (";
251 emitConstantValueOnly(CE->getOperand(1));
252 O << ")";
253 break;
254 default:
255 assert(0 && "Unsupported operator!");
256 }
257 } else {
258 assert(0 && "Unknown constant value!");
259 }
260}
261
262// Print a constant value or values, with the appropriate storage class as a
263// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000264void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000265 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000266
Chris Lattnerad200712003-09-09 16:23:36 +0000267 if (CV->isNullValue()) {
268 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000269 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000270 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000271 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000272 O << "\t.ascii\t";
273 printAsCString(O, CVA);
274 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000275 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000276 const std::vector<Use> &constValues = CVA->getValues();
277 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000278 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000279 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000280 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000281 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
282 // Print the fields in successive locations. Pad to align if needed!
283 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
284 const std::vector<Use>& constValues = CVS->getValues();
285 unsigned sizeSoFar = 0;
286 for (unsigned i=0, N = constValues.size(); i < N; i++) {
287 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000288
Chris Lattnerad200712003-09-09 16:23:36 +0000289 // Check if padding is needed and insert one or more 0s.
290 unsigned fieldSize = TD.getTypeSize(field->getType());
291 unsigned padSize = ((i == N-1? cvsLayout->StructSize
292 : cvsLayout->MemberOffsets[i+1])
293 - cvsLayout->MemberOffsets[i]) - fieldSize;
294 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000295
Chris Lattnerad200712003-09-09 16:23:36 +0000296 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000297 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000298
299 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000300 if (padSize)
301 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000302 }
Chris Lattnerad200712003-09-09 16:23:36 +0000303 assert(sizeSoFar == cvsLayout->StructSize &&
304 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000305 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000306 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
307 // FP Constants are printed as integer constants to avoid losing
308 // precision...
309 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000310 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000311 default: assert(0 && "Unknown floating point type!");
312 case Type::FloatTyID: {
313 union FU { // Abide by C TBAA rules
314 float FVal;
315 unsigned UVal;
316 } U;
317 U.FVal = Val;
318 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
319 return;
320 }
321 case Type::DoubleTyID: {
322 union DU { // Abide by C TBAA rules
323 double FVal;
324 uint64_t UVal;
325 } U;
326 U.FVal = Val;
327 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
328 return;
329 }
330 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000331 }
332
333 const Type *type = CV->getType();
334 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000335 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000336 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
337 O << ".byte";
338 break;
339 case Type::UShortTyID: case Type::ShortTyID:
340 O << ".word";
341 break;
342 case Type::FloatTyID: case Type::PointerTyID:
343 case Type::UIntTyID: case Type::IntTyID:
344 O << ".long";
345 break;
346 case Type::DoubleTyID:
347 case Type::ULongTyID: case Type::LongTyID:
348 O << ".quad";
349 break;
350 default:
351 assert (0 && "Can't handle printing this type of thing");
352 break;
353 }
354 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000355 emitConstantValueOnly(CV);
356 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000357}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000358
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000359/// printConstantPool - Print to the current output stream assembly
360/// representations of the constants in the constant pool MCP. This is
361/// used to print out constants which have been "spilled to memory" by
362/// the code generator.
363///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000364void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000365 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000366 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000367
Chris Lattnerb7089442003-01-13 00:35:03 +0000368 if (CP.empty()) return;
369
370 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
371 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000372 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000373 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000374 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
375 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000376 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000377 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000378}
379
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000380/// runOnMachineFunction - This uses the printMachineInstruction()
381/// method to print assembly for each instruction.
382///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000383bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000384 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000385 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000386 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000387
Chris Lattnerb7089442003-01-13 00:35:03 +0000388 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000389 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000390
Brian Gaeke6559bb92002-11-14 22:32:30 +0000391 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000392 O << "\t.text\n";
393 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000394 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000395 if (!EmitCygwin)
396 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000397 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000398
399 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000400 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
401 I != E; ++I) {
402 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000403 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000404 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000405 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000406 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000407 // Print the assembly for the instruction.
408 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000409 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000410 }
Chris Lattner0285a332002-12-28 20:25:38 +0000411 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000412
413 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000414 return false;
415}
416
Chris Lattner3d3067b2002-11-21 20:44:15 +0000417static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000418 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000419 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
420 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000421}
422
423static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000424 if (MI->getOperand(Op).isFrameIndex()) return true;
425 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000426 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000427 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
428 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000429}
430
Brian Gaeke2a098772003-08-11 19:05:46 +0000431
432
Chris Lattner3fa861a2004-08-01 06:02:08 +0000433void X86AsmPrinter::printOp(const MachineOperand &MO,
434 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000435 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000436 switch (MO.getType()) {
437 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000438 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000439 O << "<" << V->getName() << ">";
440 return;
441 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000442 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000443 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000444 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000445 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000446 O << "%" << RI.get(MO.getReg()).Name;
447 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000448 O << "%reg" << MO.getReg();
449 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000450
451 case MachineOperand::MO_SignExtendedImmed:
452 case MachineOperand::MO_UnextendedImmed:
453 O << (int)MO.getImmedValue();
454 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000455 case MachineOperand::MO_MachineBasicBlock: {
456 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
457 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
458 << "_" << MBBOp->getNumber () << "\t# "
459 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000460 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000461 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000462 case MachineOperand::MO_PCRelativeDisp:
463 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
464 abort ();
465 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000466 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000467 if (!elideOffsetKeyword)
468 O << "OFFSET ";
469 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000470 return;
471 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000472 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000473 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000474 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000475 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000476 }
477}
478
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000479static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
480 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000481 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000482 case X86II::Mem8: return "BYTE PTR";
483 case X86II::Mem16: return "WORD PTR";
484 case X86II::Mem32: return "DWORD PTR";
485 case X86II::Mem64: return "QWORD PTR";
486 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000487 }
488}
489
Chris Lattner3fa861a2004-08-01 06:02:08 +0000490void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000491 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000492
493 if (MI->getOperand(Op).isFrameIndex()) {
494 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
495 if (MI->getOperand(Op+3).getImmedValue())
496 O << " + " << MI->getOperand(Op+3).getImmedValue();
497 O << "]";
498 return;
499 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000500 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000501 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000502 if (MI->getOperand(Op+3).getImmedValue())
503 O << " + " << MI->getOperand(Op+3).getImmedValue();
504 O << "]";
505 return;
506 }
507
Chris Lattner3d3067b2002-11-21 20:44:15 +0000508 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000509 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000510 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000511 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000512
513 O << "[";
514 bool NeedPlus = false;
515 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000516 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000517 NeedPlus = true;
518 }
519
520 if (IndexReg.getReg()) {
521 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000522 if (ScaleVal != 1)
523 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000524 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000525 NeedPlus = true;
526 }
527
Chris Lattner0285a332002-12-28 20:25:38 +0000528 if (DispVal) {
529 if (NeedPlus)
530 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000531 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000532 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000533 O << " - ";
534 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000535 }
536 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000537 }
538 O << "]";
539}
540
Chris Lattner30b2f722004-03-31 22:02:21 +0000541
542/// printImplUsesBefore - Emit the implicit-use registers for the instruction
543/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000544///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000545void X86AsmPrinter::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000546 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000547 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
548 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000549 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner30b2f722004-03-31 22:02:21 +0000550 O << "%" << RI.get(*p).Name << ", ";
551 }
552 }
553}
554
Chris Lattner26653832004-04-13 17:18:39 +0000555/// printImplDefsBefore - Emit the implicit-def registers for the instruction
556/// described by DESC, if its PrintImplUsesBefore flag is set. Return true if
557/// we printed any registers.
558///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000559bool X86AsmPrinter::printImplDefsBefore(const TargetInstrDescriptor &Desc) {
Chris Lattner26653832004-04-13 17:18:39 +0000560 bool Printed = false;
561 const MRegisterInfo &RI = *TM.getRegisterInfo();
562 if (Desc.TSFlags & X86II::PrintImplDefsBefore) {
563 const unsigned *p = Desc.ImplicitDefs;
564 if (*p) {
565 O << (Printed ? ", %" : "%") << RI.get (*p).Name;
566 Printed = true;
567 ++p;
568 }
569 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000570 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner26653832004-04-13 17:18:39 +0000571 O << ", %" << RI.get(*p).Name;
572 ++p;
573 }
574 }
575 return Printed;
576}
577
578
Chris Lattner30b2f722004-03-31 22:02:21 +0000579/// printImplUsesAfter - Emit the implicit-use registers for the instruction
580/// described by DESC, if its PrintImplUsesAfter flag is set.
581///
John Criswell4ffff9e2004-04-08 20:31:47 +0000582/// Inputs:
583/// Comma - List of registers will need a leading comma.
584/// Desc - Description of the Instruction.
585///
586/// Return value:
587/// true - Emitted one or more registers.
588/// false - Emitted no registers.
589///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000590bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
591 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000592 const MRegisterInfo &RI = *TM.getRegisterInfo();
593 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000594 bool emitted = false;
595 const unsigned *p = Desc.ImplicitUses;
596 if (*p) {
597 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
598 emitted = true;
599 ++p;
600 }
601 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000602 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000603 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000604 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000605 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000606 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000607 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000608 return false;
609}
610
611/// printImplDefsAfter - Emit the implicit-definition registers for the
612/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
613///
614/// Inputs:
615/// Comma - List of registers will need a leading comma.
616/// Desc - Description of the Instruction
617///
618/// Return value:
619/// true - Emitted one or more registers.
620/// false - Emitted no registers.
621///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000622bool X86AsmPrinter::printImplDefsAfter(const TargetInstrDescriptor &Desc,
623 const bool Comma = true) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000624 const MRegisterInfo &RI = *TM.getRegisterInfo();
625 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
626 bool emitted = false;
627 const unsigned *p = Desc.ImplicitDefs;
628 if (*p) {
629 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
630 emitted = true;
631 ++p;
632 }
633 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000634 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
John Criswell4ffff9e2004-04-08 20:31:47 +0000635 O << ", %" << RI.get(*p).Name;
636 ++p;
637 }
638 return emitted;
639 }
640 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000641}
642
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000643/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000644/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000645///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000646void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
647 ++EmittedInsts;
648 if (printInstruction(MI))
649 return; // Printer was automatically generated
650
Chris Lattnerf9f60882002-11-18 06:56:51 +0000651 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000652 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000653 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000654
Chris Lattnereca1f632002-12-25 05:09:01 +0000655 switch (Desc.TSFlags & X86II::FormMask) {
656 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000657 // Print pseudo-instructions as comments; either they should have been
658 // turned into real instructions by now, or they don't need to be
659 // seen by the assembler (e.g., IMPLICIT_USEs.)
660 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000661 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000662 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000663 O << " = phi ";
664 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000665 if (i != 1) O << ", ";
666 O << "[";
667 printOp(MI->getOperand(i));
668 O << ", ";
669 printOp(MI->getOperand(i+1));
670 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000671 }
672 } else {
673 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000674 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000675 printOp(MI->getOperand(0));
676 O << " = ";
677 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000678 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000679 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000680
681 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000682 O << " ";
683 if (MI->getOperand(i).isDef()) O << "*";
684 printOp(MI->getOperand(i));
685 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000686 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000687 }
688 O << "\n";
689 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000690
Chris Lattnerf9f60882002-11-18 06:56:51 +0000691 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000692 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000693 // The accepted forms of Raw instructions are:
Chris Lattner3fa861a2004-08-01 06:02:08 +0000694 // 1. jmp foo - MachineBasicBlock operand
695 // 2. call bar - GlobalAddress Operand or External Symbol Operand
696 // 3. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000697 //
Chris Lattner3fa861a2004-08-01 06:02:08 +0000698 assert(MI->getNumOperands() == 1 &&
699 (MI->getOperand(0).isMachineBasicBlock() ||
700 MI->getOperand(0).isGlobalAddress() ||
701 MI->getOperand(0).isExternalSymbol() ||
702 MI->getOperand(0).isImmediate()) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000703 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000704 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000705
Chris Lattner26653832004-04-13 17:18:39 +0000706 bool LeadingComma = printImplDefsBefore(Desc);
707
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000708 if (MI->getNumOperands() == 1) {
Chris Lattner26653832004-04-13 17:18:39 +0000709 if (LeadingComma) O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000710 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000711 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000712 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000713 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
714 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000715 O << "\n";
716 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000717 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000718
Chris Lattner77875d82002-11-21 02:00:20 +0000719 case X86II::AddRegFrm: {
720 // There are currently two forms of acceptable AddRegFrm instructions.
721 // Either the instruction JUST takes a single register (like inc, dec, etc),
722 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000723 // (move immediate f.e.). Note that this immediate value might be stored as
724 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000725 // into a register. The initial register might be duplicated if this is a
726 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000727 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000728 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000729 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000730 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000731 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000732 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000733 MI->getOperand(1).isRegister() ||
734 MI->getOperand(1).isGlobalAddress() ||
735 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000736 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000737
Chris Lattner77875d82002-11-21 02:00:20 +0000738 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000739
Chris Lattnerb009c002004-02-11 19:26:28 +0000740 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000741
742 printImplUsesBefore(Desc); // fcmov*
743
Brian Gaekede420ae2003-07-23 20:25:08 +0000744 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000745 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000746 (!MI->getOperand(1).isRegister() ||
747 MI->getOperand(1).getVRegValueOrNull() ||
748 MI->getOperand(1).isGlobalAddress() ||
749 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000750 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000751 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000752 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000753 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000754 O << "\n";
755 return;
756 }
Chris Lattner233ad712002-11-21 01:33:44 +0000757 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000758 // There are three forms of MRMDestReg instructions, those with 2
759 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000760 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000761 // 2 Operands: this is for things like mov that do not read a
762 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000763 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000764 // 2 Operands: two address instructions which def&use the first
765 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000766 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000767 // 3 Operands: in this form, two address instructions are the same
768 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000769 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000770 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000771 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000772 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000773 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000774 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000775
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 Lattnerf9f60882002-11-18 06:56:51 +0000778 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000779 printOp(MI->getOperand(1));
780 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000781 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000782 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000783 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000784 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000785 O << "\n";
786 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000787 }
Chris Lattner18042332002-11-21 21:03:39 +0000788
789 case X86II::MRMDestMem: {
790 // These instructions are the same as MRMDestReg, but instead of having a
791 // register reference for the mod/rm field, it's a memory reference.
792 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000793 assert(isMem(MI, 0) &&
794 (MI->getNumOperands() == 4+1 ||
795 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
796 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000797
Chris Lattnerb009c002004-02-11 19:26:28 +0000798 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000799 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000800 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000801 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000802 if (MI->getNumOperands() == 4+2) {
803 O << ", ";
804 printOp(MI->getOperand(5));
805 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000806 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000807 O << "\n";
808 return;
809 }
810
Chris Lattner233ad712002-11-21 01:33:44 +0000811 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000812 // There are three forms that are acceptable for MRMSrcReg
813 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000814 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000815 // 2 Operands: this is for things like mov that do not read a
816 // second input.
817 //
818 // 2 Operands: in this form, the last register is the ModR/M
819 // input. The first operand is a def&use. This is for things
820 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000821 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000822 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000823 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000824 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000825 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000826 assert(MI->getOperand(0).isRegister() &&
827 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000828 (MI->getNumOperands() == 2 ||
829 (MI->getNumOperands() == 3 &&
830 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000831 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000832
Chris Lattnerb009c002004-02-11 19:26:28 +0000833 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000834 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000835 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000836 printOp(MI->getOperand(1));
837 if (MI->getNumOperands() == 3) {
838 O << ", ";
839 printOp(MI->getOperand(2));
840 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000841 O << "\n";
842 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000843 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000844
Chris Lattner3d3067b2002-11-21 20:44:15 +0000845 case X86II::MRMSrcMem: {
846 // These instructions are the same as MRMSrcReg, but instead of having a
847 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000848 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000849 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000850 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
851 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
852 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000853 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000854 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000855 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000856 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000857 printMemReference(MI, 1);
858 if (MI->getNumOperands() == 2+4) {
859 O << ", ";
860 printOp(MI->getOperand(5));
861 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000862 O << "\n";
863 return;
864 }
865
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000866 case X86II::MRM0r: case X86II::MRM1r:
867 case X86II::MRM2r: case X86II::MRM3r:
868 case X86II::MRM4r: case X86II::MRM5r:
869 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000870 // In this form, the following are valid formats:
871 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000872 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000873 // 2. shl rdest, rinput <implicit CL or 1>
874 // 3. sbb rdest, rinput, immediate [rdest = rinput]
875 //
876 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000877 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000878 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000879 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000880 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000881 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000882 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000883 "Bad MRMSxR format!");
884
Chris Lattnerd9096832002-12-15 08:01:39 +0000885 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000886 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
887 O << "**";
888
Chris Lattnerb009c002004-02-11 19:26:28 +0000889 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000890 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000891 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000892 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000893 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000894 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000895 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000896 O << "\n";
897
898 return;
899 }
900
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000901 case X86II::MRM0m: case X86II::MRM1m:
902 case X86II::MRM2m: case X86II::MRM3m:
903 case X86II::MRM4m: case X86II::MRM5m:
904 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000905 // In this form, the following are valid formats:
906 // 1. sete [m]
907 // 2. cmp [m], immediate
908 // 2. shl [m], rinput <implicit CL or 1>
909 // 3. sbb [m], immediate
910 //
911 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
912 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000913 assert((MI->getNumOperands() != 5 ||
914 (MI->getOperand(4).isImmediate() ||
915 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000916 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000917
918 const MachineOperand &Op3 = MI->getOperand(3);
919
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000920 // gas bugs:
921 //
922 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000923 // is misassembled by gas in intel_syntax mode as its 32-bit
924 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
925 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000926 //
927 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000928 // misassembled by gas in intel_syntax mode as its 32-bit
929 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
930 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000931 //
932 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000933 // invalid opcode, saying "64 bit operations are only supported in
934 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
935 // [...]", which is wrong. Workaround: Output the raw opcode bytes
936 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000937 //
938 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
939 // invalid opcode, saying "64 bit operations are only supported in
940 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
941 // [...]", which is wrong. Workaround: Output the raw opcode bytes
942 // instead of the instruction.
943 if (MI->getOpcode() == X86::FSTP80m ||
944 MI->getOpcode() == X86::FLD80m ||
945 MI->getOpcode() == X86::FILD64m ||
946 MI->getOpcode() == X86::FISTP64m) {
Misha Brukman8606aea2004-07-26 18:48:58 +0000947 GasBugWorkaroundEmitter gwe(O);
948 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000949 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000950
Chris Lattnerb009c002004-02-11 19:26:28 +0000951 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000952 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000953 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000954 if (MI->getNumOperands() == 5) {
955 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000956 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000957 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000958 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000959 O << "\n";
960 return;
961 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000962 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000963 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000964 }
Chris Lattner72614082002-10-25 22:55:53 +0000965}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000966
Chris Lattner3fa861a2004-08-01 06:02:08 +0000967bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000968 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000969 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000970 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
971 // instruction as a reference to the register named sp, and if you try to
972 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
973 // before being looked up in the symbol table. This creates spurious
974 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
975 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000976 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000977 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000978 return false; // success
979}
980
Chris Lattnerad200712003-09-09 16:23:36 +0000981// SwitchSection - Switch to the specified section of the executable if we are
982// not already in it!
983//
984static void SwitchSection(std::ostream &OS, std::string &CurSection,
985 const char *NewSection) {
986 if (CurSection != NewSection) {
987 CurSection = NewSection;
988 if (!CurSection.empty())
989 OS << "\t" << NewSection << "\n";
990 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000991}
992
Chris Lattner3fa861a2004-08-01 06:02:08 +0000993bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000994 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000995 std::string CurSection;
996
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000997 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000998 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
999 if (I->hasInitializer()) { // External global require no code
1000 O << "\n\n";
1001 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001002 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +00001003 unsigned Size = TD.getTypeSize(C->getType());
1004 unsigned Align = TD.getTypeAlignment(C->getType());
1005
1006 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +00001007 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
1008 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +00001009 SwitchSection(O, CurSection, ".data");
1010 if (I->hasInternalLinkage())
1011 O << "\t.local " << name << "\n";
1012
1013 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +00001014 << "," << (unsigned)TD.getTypeAlignment(C->getType());
1015 O << "\t\t# ";
1016 WriteAsOperand(O, I, true, true, &M);
1017 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001018 } else {
Chris Lattnerad200712003-09-09 16:23:36 +00001019 switch (I->getLinkage()) {
1020 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +00001021 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +00001022 // Nonnull linkonce -> weak
1023 O << "\t.weak " << name << "\n";
1024 SwitchSection(O, CurSection, "");
1025 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
1026 break;
1027
1028 case GlobalValue::AppendingLinkage:
1029 // FIXME: appending linkage variables should go into a section of
1030 // their name or something. For now, just emit them as external.
1031 case GlobalValue::ExternalLinkage:
1032 // If external or appending, declare as a global symbol
1033 O << "\t.globl " << name << "\n";
1034 // FALL THROUGH
1035 case GlobalValue::InternalLinkage:
1036 if (C->isNullValue())
1037 SwitchSection(O, CurSection, ".bss");
1038 else
1039 SwitchSection(O, CurSection, ".data");
1040 break;
1041 }
1042
1043 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001044 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001045 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001046 O << name << ":\t\t\t\t# ";
1047 WriteAsOperand(O, I, true, true, &M);
1048 O << " = ";
1049 WriteAsOperand(O, C, false, false, &M);
1050 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001051 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001052 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001053 }
Chris Lattnerad200712003-09-09 16:23:36 +00001054
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001055 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001056 return false; // success
1057}