blob: 822f6a2a037bedd86e8cf595aa438a6998d9eb9d [file] [log] [blame]
Brian Gaeke92bdfe62003-07-23 18:37:06 +00001//===-- X86/Printer.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 Lattnerd59414f2003-08-11 20:06:16 +000028#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000029#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000030#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000031#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000032#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000036 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37
Chris Lattner93c1afa2003-08-11 19:35:26 +000038 // FIXME: This should be automatically picked up by autoconf from the C
39 // frontend
40 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
41 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
42
Alkis Evlogimenos03090662004-03-09 03:35:34 +000043 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
44 GasBugWorkaroundEmitter(std::ostream& o)
45 : O(o), OldFlags(O.flags()), firstByte(true) {
46 O << std::hex;
47 }
48
49 ~GasBugWorkaroundEmitter() {
50 O.flags(OldFlags);
51 O << "\t# ";
52 }
53
54 virtual void emitByte(unsigned char B) {
55 if (!firstByte) O << "\n\t";
56 firstByte = false;
57 O << ".byte 0x" << (unsigned) B;
58 }
59
60 // These should never be called
61 virtual void emitWord(unsigned W) { assert(0); }
Misha Brukmandb760d02004-03-11 19:08:24 +000062 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
63 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
64 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
65 virtual uint64_t getCurrentPCValue() { abort(); }
66 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000067
68 private:
69 std::ostream& O;
70 std::ios::fmtflags OldFlags;
71 bool firstByte;
72 };
73
Chris Lattner0285a332002-12-28 20:25:38 +000074 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000075 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000076 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000078
79 /// Target machine description which we query for reg. names, data
80 /// layout, etc.
81 ///
82 TargetMachine &TM;
83
Brian Gaeked9fb37a2003-07-24 20:20:44 +000084 /// Name-mangler for global names.
85 ///
86 Mangler *Mang;
87
Brian Gaekede420ae2003-07-23 20:25:08 +000088 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089
Brian Gaeke92bdfe62003-07-23 18:37:06 +000090 /// Cache of mangled name for current function. This is
91 /// recalculated at the beginning of each call to
92 /// runOnMachineFunction().
93 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000094 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000095
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000096 virtual const char *getPassName() const {
97 return "X86 Assembly Printer";
98 }
99
Chris Lattner30b2f722004-03-31 22:02:21 +0000100 void printImplUsesBefore(const TargetInstrDescriptor &Desc);
Chris Lattner26653832004-04-13 17:18:39 +0000101 bool printImplDefsBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000102 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
103 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000104 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000105 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000106 bool elideOffsetKeyword = false);
107 void printMemReference(const MachineInstr *MI, unsigned Op);
108 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000109 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000110 bool doInitialization(Module &M);
111 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000112 void emitGlobalConstant(const Constant* CV);
113 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000114 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000115} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000116
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000117/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000118/// assembly code for a MachineFunction to the given output stream,
119/// using the given target machine description. This should work
120/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000121///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000122FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000123 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000124}
125
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000126/// toOctal - Convert the low order bits of X into an octal digit.
127///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000128static inline char toOctal(int X) {
129 return (X&7)+'0';
130}
131
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000132/// getAsCString - Return the specified array as a C compatible
133/// string, only if the predicate isStringCompatible is true.
134///
Chris Lattnerac662d12003-11-03 20:19:49 +0000135static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000136 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000137
Chris Lattnerac662d12003-11-03 20:19:49 +0000138 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000139 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000140 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000141
142 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000143 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000144 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000145 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000146 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000147 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000148 } else {
149 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000150 case '\b': O << "\\b"; break;
151 case '\f': O << "\\f"; break;
152 case '\n': O << "\\n"; break;
153 case '\r': O << "\\r"; break;
154 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000155 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000156 O << '\\';
157 O << toOctal(C >> 6);
158 O << toOctal(C >> 3);
159 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000160 break;
161 }
162 }
163 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000164 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165}
166
Chris Lattnerac662d12003-11-03 20:19:49 +0000167// Print out the specified constant, without a storage class. Only the
168// constants valid in constant expressions can occur here.
169void Printer::emitConstantValueOnly(const Constant *CV) {
170 if (CV->isNullValue())
171 O << "0";
172 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
173 assert(CB == ConstantBool::True);
174 O << "1";
175 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000176 if (((CI->getValue() << 32) >> 32) == CI->getValue())
177 O << CI->getValue();
178 else
179 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000180 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
181 O << CI->getValue();
182 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
183 // This is a constant address for a global variable or function. Use the
184 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000185 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000186 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
187 const TargetData &TD = TM.getTargetData();
188 switch(CE->getOpcode()) {
189 case Instruction::GetElementPtr: {
190 // generate a symbolic expression for the byte address
191 const Constant *ptrVal = CE->getOperand(0);
192 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
193 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
194 O << "(";
195 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000196 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000197 } else {
198 emitConstantValueOnly(ptrVal);
199 }
200 break;
201 }
202 case Instruction::Cast: {
203 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000204 // that do not involve a change in value. This assertion is really gross,
205 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000206 Constant *Op = CE->getOperand(0);
207 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000208
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000209 // Remember, kids, pointers on x86 can be losslessly converted back and
210 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000211 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000212 && (Ty == Type::LongTy || Ty == Type::ULongTy
213 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000214 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000215 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
216 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000217 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
218 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000219 && "FIXME: Don't yet support this kind of constant cast expr");
220 O << "(";
221 emitConstantValueOnly(Op);
222 O << ")";
223 break;
224 }
225 case Instruction::Add:
226 O << "(";
227 emitConstantValueOnly(CE->getOperand(0));
228 O << ") + (";
229 emitConstantValueOnly(CE->getOperand(1));
230 O << ")";
231 break;
232 default:
233 assert(0 && "Unsupported operator!");
234 }
235 } else {
236 assert(0 && "Unknown constant value!");
237 }
238}
239
240// Print a constant value or values, with the appropriate storage class as a
241// prefix.
242void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000243 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000244
Chris Lattnerad200712003-09-09 16:23:36 +0000245 if (CV->isNullValue()) {
246 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000247 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000248 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000249 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000250 O << "\t.ascii\t";
251 printAsCString(O, CVA);
252 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000253 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000254 const std::vector<Use> &constValues = CVA->getValues();
255 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000256 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000257 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000258 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000259 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
260 // Print the fields in successive locations. Pad to align if needed!
261 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
262 const std::vector<Use>& constValues = CVS->getValues();
263 unsigned sizeSoFar = 0;
264 for (unsigned i=0, N = constValues.size(); i < N; i++) {
265 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000266
Chris Lattnerad200712003-09-09 16:23:36 +0000267 // Check if padding is needed and insert one or more 0s.
268 unsigned fieldSize = TD.getTypeSize(field->getType());
269 unsigned padSize = ((i == N-1? cvsLayout->StructSize
270 : cvsLayout->MemberOffsets[i+1])
271 - cvsLayout->MemberOffsets[i]) - fieldSize;
272 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000273
Chris Lattnerad200712003-09-09 16:23:36 +0000274 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000275 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000276
277 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000278 if (padSize)
279 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000280 }
Chris Lattnerad200712003-09-09 16:23:36 +0000281 assert(sizeSoFar == cvsLayout->StructSize &&
282 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000283 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000284 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
285 // FP Constants are printed as integer constants to avoid losing
286 // precision...
287 double Val = CFP->getValue();
288 switch (CFP->getType()->getPrimitiveID()) {
289 default: assert(0 && "Unknown floating point type!");
290 case Type::FloatTyID: {
291 union FU { // Abide by C TBAA rules
292 float FVal;
293 unsigned UVal;
294 } U;
295 U.FVal = Val;
296 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
297 return;
298 }
299 case Type::DoubleTyID: {
300 union DU { // Abide by C TBAA rules
301 double FVal;
302 uint64_t UVal;
303 } U;
304 U.FVal = Val;
305 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
306 return;
307 }
308 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000309 }
310
311 const Type *type = CV->getType();
312 O << "\t";
313 switch (type->getPrimitiveID()) {
314 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
315 O << ".byte";
316 break;
317 case Type::UShortTyID: case Type::ShortTyID:
318 O << ".word";
319 break;
320 case Type::FloatTyID: case Type::PointerTyID:
321 case Type::UIntTyID: case Type::IntTyID:
322 O << ".long";
323 break;
324 case Type::DoubleTyID:
325 case Type::ULongTyID: case Type::LongTyID:
326 O << ".quad";
327 break;
328 default:
329 assert (0 && "Can't handle printing this type of thing");
330 break;
331 }
332 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000333 emitConstantValueOnly(CV);
334 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000335}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000336
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000337/// printConstantPool - Print to the current output stream assembly
338/// representations of the constants in the constant pool MCP. This is
339/// used to print out constants which have been "spilled to memory" by
340/// the code generator.
341///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000342void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000343 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000344 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000345
Chris Lattnerb7089442003-01-13 00:35:03 +0000346 if (CP.empty()) return;
347
348 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
349 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000350 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000351 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000352 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
353 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000354 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000355 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000356}
357
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000358/// runOnMachineFunction - This uses the printMachineInstruction()
359/// method to print assembly for each instruction.
360///
Chris Lattner0285a332002-12-28 20:25:38 +0000361bool Printer::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000362 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000363 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000364 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000365
Chris Lattnerb7089442003-01-13 00:35:03 +0000366 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000367 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000368
Brian Gaeke6559bb92002-11-14 22:32:30 +0000369 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000370 O << "\t.text\n";
371 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000372 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000373 if (!EmitCygwin)
374 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000375 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000376
377 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000378 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
379 I != E; ++I) {
380 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000381 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000382 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000383 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
384 II != E; ++II) {
385 // Print the assembly for the instruction.
386 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000387 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000388 }
Chris Lattner0285a332002-12-28 20:25:38 +0000389 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000390
391 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000392 return false;
393}
394
Chris Lattner3d3067b2002-11-21 20:44:15 +0000395static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000396 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000397 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
398 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000399}
400
401static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000402 if (MI->getOperand(Op).isFrameIndex()) return true;
403 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000404 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000405 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
406 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000407}
408
Brian Gaeke2a098772003-08-11 19:05:46 +0000409
410
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000411void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000412 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000413 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000414 switch (MO.getType()) {
415 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000416 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000417 O << "<" << V->getName() << ">";
418 return;
419 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000420 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000421 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000422 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000423 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000424 O << "%" << RI.get(MO.getReg()).Name;
425 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000426 O << "%reg" << MO.getReg();
427 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000428
429 case MachineOperand::MO_SignExtendedImmed:
430 case MachineOperand::MO_UnextendedImmed:
431 O << (int)MO.getImmedValue();
432 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000433 case MachineOperand::MO_MachineBasicBlock: {
434 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
435 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
436 << "_" << MBBOp->getNumber () << "\t# "
437 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000438 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000439 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000440 case MachineOperand::MO_PCRelativeDisp:
441 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
442 abort ();
443 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000444 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000445 if (!elideOffsetKeyword)
446 O << "OFFSET ";
447 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000448 return;
449 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000450 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000451 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000452 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000453 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000454 }
455}
456
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000457static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
458 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000459 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000460 case X86II::Mem8: return "BYTE PTR";
461 case X86II::Mem16: return "WORD PTR";
462 case X86II::Mem32: return "DWORD PTR";
463 case X86II::Mem64: return "QWORD PTR";
464 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000465 }
466}
467
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000468void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000469 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000470
471 if (MI->getOperand(Op).isFrameIndex()) {
472 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
473 if (MI->getOperand(Op+3).getImmedValue())
474 O << " + " << MI->getOperand(Op+3).getImmedValue();
475 O << "]";
476 return;
477 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000478 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000479 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 if (MI->getOperand(Op+3).getImmedValue())
481 O << " + " << MI->getOperand(Op+3).getImmedValue();
482 O << "]";
483 return;
484 }
485
Chris Lattner3d3067b2002-11-21 20:44:15 +0000486 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000487 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000488 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000489 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000490
491 O << "[";
492 bool NeedPlus = false;
493 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000494 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000495 NeedPlus = true;
496 }
497
498 if (IndexReg.getReg()) {
499 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000500 if (ScaleVal != 1)
501 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000502 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503 NeedPlus = true;
504 }
505
Chris Lattner0285a332002-12-28 20:25:38 +0000506 if (DispVal) {
507 if (NeedPlus)
508 if (DispVal > 0)
509 O << " + ";
510 else {
511 O << " - ";
512 DispVal = -DispVal;
513 }
514 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000515 }
516 O << "]";
517}
518
Chris Lattner30b2f722004-03-31 22:02:21 +0000519
520/// printImplUsesBefore - Emit the implicit-use registers for the instruction
521/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000522///
Chris Lattner30b2f722004-03-31 22:02:21 +0000523void Printer::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000524 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000525 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
526 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
527 // Bug Workaround: See note in Printer::doInitialization about %.
528 O << "%" << RI.get(*p).Name << ", ";
529 }
530 }
531}
532
Chris Lattner26653832004-04-13 17:18:39 +0000533/// printImplDefsBefore - Emit the implicit-def registers for the instruction
534/// described by DESC, if its PrintImplUsesBefore flag is set. Return true if
535/// we printed any registers.
536///
537bool Printer::printImplDefsBefore(const TargetInstrDescriptor &Desc) {
538 bool Printed = false;
539 const MRegisterInfo &RI = *TM.getRegisterInfo();
540 if (Desc.TSFlags & X86II::PrintImplDefsBefore) {
541 const unsigned *p = Desc.ImplicitDefs;
542 if (*p) {
543 O << (Printed ? ", %" : "%") << RI.get (*p).Name;
544 Printed = true;
545 ++p;
546 }
547 while (*p) {
548 // Bug Workaround: See note in Printer::doInitialization about %.
549 O << ", %" << RI.get(*p).Name;
550 ++p;
551 }
552 }
553 return Printed;
554}
555
556
Chris Lattner30b2f722004-03-31 22:02:21 +0000557/// printImplUsesAfter - Emit the implicit-use registers for the instruction
558/// described by DESC, if its PrintImplUsesAfter flag is set.
559///
John Criswell4ffff9e2004-04-08 20:31:47 +0000560/// Inputs:
561/// Comma - List of registers will need a leading comma.
562/// Desc - Description of the Instruction.
563///
564/// Return value:
565/// true - Emitted one or more registers.
566/// false - Emitted no registers.
567///
568bool Printer::printImplUsesAfter(const TargetInstrDescriptor &Desc,
569 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000570 const MRegisterInfo &RI = *TM.getRegisterInfo();
571 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000572 bool emitted = false;
573 const unsigned *p = Desc.ImplicitUses;
574 if (*p) {
575 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
576 emitted = true;
577 ++p;
578 }
579 while (*p) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000580 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000581 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000582 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000583 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000584 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000585 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000586 return false;
587}
588
589/// printImplDefsAfter - Emit the implicit-definition registers for the
590/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
591///
592/// Inputs:
593/// Comma - List of registers will need a leading comma.
594/// Desc - Description of the Instruction
595///
596/// Return value:
597/// true - Emitted one or more registers.
598/// false - Emitted no registers.
599///
600bool Printer::printImplDefsAfter(const TargetInstrDescriptor &Desc,
601 const bool Comma = true) {
602 const MRegisterInfo &RI = *TM.getRegisterInfo();
603 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
604 bool emitted = false;
605 const unsigned *p = Desc.ImplicitDefs;
606 if (*p) {
607 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
608 emitted = true;
609 ++p;
610 }
611 while (*p) {
612 // Bug Workaround: See note in Printer::doInitialization about %.
613 O << ", %" << RI.get(*p).Name;
614 ++p;
615 }
616 return emitted;
617 }
618 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000619}
620
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000621/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000622/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000623///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000624void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000625 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000626 const TargetInstrInfo &TII = TM.getInstrInfo();
627 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000628
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000629 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000630 switch (Desc.TSFlags & X86II::FormMask) {
631 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000632 // Print pseudo-instructions as comments; either they should have been
633 // turned into real instructions by now, or they don't need to be
634 // seen by the assembler (e.g., IMPLICIT_USEs.)
635 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000636 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000637 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000638 O << " = phi ";
639 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000640 if (i != 1) O << ", ";
641 O << "[";
642 printOp(MI->getOperand(i));
643 O << ", ";
644 printOp(MI->getOperand(i+1));
645 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000646 }
647 } else {
648 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000649 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000650 printOp(MI->getOperand(0));
651 O << " = ";
652 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000653 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000654 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000655
656 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000657 O << " ";
658 if (MI->getOperand(i).isDef()) O << "*";
659 printOp(MI->getOperand(i));
660 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000661 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000662 }
663 O << "\n";
664 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000665
Chris Lattnerf9f60882002-11-18 06:56:51 +0000666 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000667 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000668 // The accepted forms of Raw instructions are:
669 // 1. nop - No operand required
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000670 // 2. jmp foo - MachineBasicBlock operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000671 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattner26653832004-04-13 17:18:39 +0000672 // 4. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000673 //
674 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000675 (MI->getNumOperands() == 1 &&
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000676 (MI->getOperand(0).isMachineBasicBlock() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 MI->getOperand(0).isGlobalAddress() ||
Chris Lattner26653832004-04-13 17:18:39 +0000678 MI->getOperand(0).isExternalSymbol() ||
679 MI->getOperand(0).isImmediate())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000680 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000681 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682
Chris Lattner26653832004-04-13 17:18:39 +0000683 bool LeadingComma = printImplDefsBefore(Desc);
684
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000685 if (MI->getNumOperands() == 1) {
Chris Lattner26653832004-04-13 17:18:39 +0000686 if (LeadingComma) O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000687 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000688 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000689 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000690 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
691 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000692 O << "\n";
693 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000694 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000695
Chris Lattner77875d82002-11-21 02:00:20 +0000696 case X86II::AddRegFrm: {
697 // There are currently two forms of acceptable AddRegFrm instructions.
698 // Either the instruction JUST takes a single register (like inc, dec, etc),
699 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000700 // (move immediate f.e.). Note that this immediate value might be stored as
701 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000702 // into a register. The initial register might be duplicated if this is a
703 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000704 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000705 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000706 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000707 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000708 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000709 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000710 MI->getOperand(1).isRegister() ||
711 MI->getOperand(1).isGlobalAddress() ||
712 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000713 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000714
Chris Lattner77875d82002-11-21 02:00:20 +0000715 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000716
Chris Lattnerb009c002004-02-11 19:26:28 +0000717 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000718
719 printImplUsesBefore(Desc); // fcmov*
720
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000722 if (MI->getNumOperands() == 2 &&
723 (!MI->getOperand(1).isRegister() ||
724 MI->getOperand(1).getVRegValueOrNull() ||
725 MI->getOperand(1).isGlobalAddress() ||
726 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000727 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000728 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000729 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000730 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000731 O << "\n";
732 return;
733 }
Chris Lattner233ad712002-11-21 01:33:44 +0000734 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000735 // There are three forms of MRMDestReg instructions, those with 2
736 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000737 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000738 // 2 Operands: this is for things like mov that do not read a
739 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000740 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000741 // 2 Operands: two address instructions which def&use the first
742 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000743 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000744 // 3 Operands: in this form, two address instructions are the same
745 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000746 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000747 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000748 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000749 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000750 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000751 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000752
Chris Lattnerb009c002004-02-11 19:26:28 +0000753 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000754 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000755 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000756 printOp(MI->getOperand(1));
757 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000758 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000759 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000760 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000761 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000762 O << "\n";
763 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000764 }
Chris Lattner18042332002-11-21 21:03:39 +0000765
766 case X86II::MRMDestMem: {
767 // These instructions are the same as MRMDestReg, but instead of having a
768 // register reference for the mod/rm field, it's a memory reference.
769 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000770 assert(isMem(MI, 0) &&
771 (MI->getNumOperands() == 4+1 ||
772 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
773 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000774
Chris Lattnerb009c002004-02-11 19:26:28 +0000775 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000776 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000777 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000778 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000779 if (MI->getNumOperands() == 4+2) {
780 O << ", ";
781 printOp(MI->getOperand(5));
782 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000783 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000784 O << "\n";
785 return;
786 }
787
Chris Lattner233ad712002-11-21 01:33:44 +0000788 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000789 // There are three forms that are acceptable for MRMSrcReg
790 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000791 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000792 // 2 Operands: this is for things like mov that do not read a
793 // second input.
794 //
795 // 2 Operands: in this form, the last register is the ModR/M
796 // input. The first operand is a def&use. This is for things
797 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000798 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000799 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000800 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000801 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000802 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000803 assert(MI->getOperand(0).isRegister() &&
804 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000805 (MI->getNumOperands() == 2 ||
806 (MI->getNumOperands() == 3 &&
807 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000808 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000809
Chris Lattnerb009c002004-02-11 19:26:28 +0000810 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000811 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000812 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000813 printOp(MI->getOperand(1));
814 if (MI->getNumOperands() == 3) {
815 O << ", ";
816 printOp(MI->getOperand(2));
817 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000818 O << "\n";
819 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000820 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000821
Chris Lattner3d3067b2002-11-21 20:44:15 +0000822 case X86II::MRMSrcMem: {
823 // These instructions are the same as MRMSrcReg, but instead of having a
824 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000825 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000826 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000827 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000828(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000829 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000830 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000831 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000832 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000833 printMemReference(MI, 1);
834 if (MI->getNumOperands() == 2+4) {
835 O << ", ";
836 printOp(MI->getOperand(5));
837 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000838 O << "\n";
839 return;
840 }
841
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000842 case X86II::MRM0r: case X86II::MRM1r:
843 case X86II::MRM2r: case X86II::MRM3r:
844 case X86II::MRM4r: case X86II::MRM5r:
845 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000846 // In this form, the following are valid formats:
847 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000848 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000849 // 2. shl rdest, rinput <implicit CL or 1>
850 // 3. sbb rdest, rinput, immediate [rdest = rinput]
851 //
852 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000853 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000854 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000855 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000856 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000857 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000858 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000859 "Bad MRMSxR format!");
860
Chris Lattnerd9096832002-12-15 08:01:39 +0000861 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000862 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
863 O << "**";
864
Chris Lattnerb009c002004-02-11 19:26:28 +0000865 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000866 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000867 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000868 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000869 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000870 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000871 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000872 O << "\n";
873
874 return;
875 }
876
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000877 case X86II::MRM0m: case X86II::MRM1m:
878 case X86II::MRM2m: case X86II::MRM3m:
879 case X86II::MRM4m: case X86II::MRM5m:
880 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000881 // In this form, the following are valid formats:
882 // 1. sete [m]
883 // 2. cmp [m], immediate
884 // 2. shl [m], rinput <implicit CL or 1>
885 // 3. sbb [m], immediate
886 //
887 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
888 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000889 assert((MI->getNumOperands() != 5 ||
890 (MI->getOperand(4).isImmediate() ||
891 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000892 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000893
894 const MachineOperand &Op3 = MI->getOperand(3);
895
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000896 // gas bugs:
897 //
898 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000899 // is misassembled by gas in intel_syntax mode as its 32-bit
900 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
901 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000902 //
903 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000904 // misassembled by gas in intel_syntax mode as its 32-bit
905 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
906 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000907 //
908 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000909 // invalid opcode, saying "64 bit operations are only supported in
910 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
911 // [...]", which is wrong. Workaround: Output the raw opcode bytes
912 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000913 //
914 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
915 // invalid opcode, saying "64 bit operations are only supported in
916 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
917 // [...]", which is wrong. Workaround: Output the raw opcode bytes
918 // instead of the instruction.
919 if (MI->getOpcode() == X86::FSTP80m ||
920 MI->getOpcode() == X86::FLD80m ||
921 MI->getOpcode() == X86::FILD64m ||
922 MI->getOpcode() == X86::FISTP64m) {
923 GasBugWorkaroundEmitter gwe(O);
924 X86::emitInstruction(gwe, (X86InstrInfo&)TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000925 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000926
Chris Lattnerb009c002004-02-11 19:26:28 +0000927 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000928 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000929 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000930 if (MI->getNumOperands() == 5) {
931 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000932 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000933 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000934 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000935 O << "\n";
936 return;
937 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000938 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000939 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000940 }
Chris Lattner72614082002-10-25 22:55:53 +0000941}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000942
Chris Lattner93c1afa2003-08-11 19:35:26 +0000943bool Printer::doInitialization(Module &M) {
944 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000945 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000946 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
947 // instruction as a reference to the register named sp, and if you try to
948 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
949 // before being looked up in the symbol table. This creates spurious
950 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
951 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000952 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000953 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000954 return false; // success
955}
956
Chris Lattnerad200712003-09-09 16:23:36 +0000957// SwitchSection - Switch to the specified section of the executable if we are
958// not already in it!
959//
960static void SwitchSection(std::ostream &OS, std::string &CurSection,
961 const char *NewSection) {
962 if (CurSection != NewSection) {
963 CurSection = NewSection;
964 if (!CurSection.empty())
965 OS << "\t" << NewSection << "\n";
966 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000967}
968
Chris Lattnerad200712003-09-09 16:23:36 +0000969bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000970 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000971 std::string CurSection;
972
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000973 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000974 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
975 if (I->hasInitializer()) { // External global require no code
976 O << "\n\n";
977 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000978 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000979 unsigned Size = TD.getTypeSize(C->getType());
980 unsigned Align = TD.getTypeAlignment(C->getType());
981
982 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000983 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
984 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000985 SwitchSection(O, CurSection, ".data");
986 if (I->hasInternalLinkage())
987 O << "\t.local " << name << "\n";
988
989 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000990 << "," << (unsigned)TD.getTypeAlignment(C->getType());
991 O << "\t\t# ";
992 WriteAsOperand(O, I, true, true, &M);
993 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000994 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000995 switch (I->getLinkage()) {
996 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000997 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000998 // Nonnull linkonce -> weak
999 O << "\t.weak " << name << "\n";
1000 SwitchSection(O, CurSection, "");
1001 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
1002 break;
1003
1004 case GlobalValue::AppendingLinkage:
1005 // FIXME: appending linkage variables should go into a section of
1006 // their name or something. For now, just emit them as external.
1007 case GlobalValue::ExternalLinkage:
1008 // If external or appending, declare as a global symbol
1009 O << "\t.globl " << name << "\n";
1010 // FALL THROUGH
1011 case GlobalValue::InternalLinkage:
1012 if (C->isNullValue())
1013 SwitchSection(O, CurSection, ".bss");
1014 else
1015 SwitchSection(O, CurSection, ".data");
1016 break;
1017 }
1018
1019 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001020 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001021 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001022 O << name << ":\t\t\t\t# ";
1023 WriteAsOperand(O, I, true, true, &M);
1024 O << " = ";
1025 WriteAsOperand(O, C, false, false, &M);
1026 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001027 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001028 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001029 }
Chris Lattnerad200712003-09-09 16:23:36 +00001030
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001031 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001032 return false; // success
1033}