blob: e2f06846d95a770b9c26d3f0a5cd637b4fc23ef8 [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
90 /// We name each basic block in a Function with a unique number, so
91 /// that we can consistently refer to them later. This is cleared
92 /// at the beginning of each call to runOnMachineFunction().
93 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000094 typedef std::map<const Value *, unsigned> ValueMapTy;
95 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000096
97 /// Cache of mangled name for current function. This is
98 /// recalculated at the beginning of each call to
99 /// runOnMachineFunction().
100 ///
Brian Gaeked7908f62003-06-27 00:00:48 +0000101 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000102
Chris Lattnerf0eb7be2002-12-15 21:13:40 +0000103 virtual const char *getPassName() const {
104 return "X86 Assembly Printer";
105 }
106
Chris Lattner30b2f722004-03-31 22:02:21 +0000107 void printImplUsesBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000108 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
109 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000110 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000111 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000112 bool elideOffsetKeyword = false);
113 void printMemReference(const MachineInstr *MI, unsigned Op);
114 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000115 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000116 bool doInitialization(Module &M);
117 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000118 void emitGlobalConstant(const Constant* CV);
119 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000120 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000121} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000122
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000123/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000124/// assembly code for a MachineFunction to the given output stream,
125/// using the given target machine description. This should work
126/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000127///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000128FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000129 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000130}
131
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000132/// toOctal - Convert the low order bits of X into an octal digit.
133///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000134static inline char toOctal(int X) {
135 return (X&7)+'0';
136}
137
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000138/// getAsCString - Return the specified array as a C compatible
139/// string, only if the predicate isStringCompatible is true.
140///
Chris Lattnerac662d12003-11-03 20:19:49 +0000141static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000142 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000143
Chris Lattnerac662d12003-11-03 20:19:49 +0000144 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000145 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000146 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147
148 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000149 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000151 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000153 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000154 } else {
155 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000156 case '\b': O << "\\b"; break;
157 case '\f': O << "\\f"; break;
158 case '\n': O << "\\n"; break;
159 case '\r': O << "\\r"; break;
160 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000161 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000162 O << '\\';
163 O << toOctal(C >> 6);
164 O << toOctal(C >> 3);
165 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166 break;
167 }
168 }
169 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000170 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000171}
172
Chris Lattnerac662d12003-11-03 20:19:49 +0000173// Print out the specified constant, without a storage class. Only the
174// constants valid in constant expressions can occur here.
175void Printer::emitConstantValueOnly(const Constant *CV) {
176 if (CV->isNullValue())
177 O << "0";
178 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
179 assert(CB == ConstantBool::True);
180 O << "1";
181 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000182 if (((CI->getValue() << 32) >> 32) == CI->getValue())
183 O << CI->getValue();
184 else
185 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000186 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
187 O << CI->getValue();
188 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
189 // This is a constant address for a global variable or function. Use the
190 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000191 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000192 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
193 const TargetData &TD = TM.getTargetData();
194 switch(CE->getOpcode()) {
195 case Instruction::GetElementPtr: {
196 // generate a symbolic expression for the byte address
197 const Constant *ptrVal = CE->getOperand(0);
198 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
199 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
200 O << "(";
201 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000202 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000203 } else {
204 emitConstantValueOnly(ptrVal);
205 }
206 break;
207 }
208 case Instruction::Cast: {
209 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000210 // that do not involve a change in value. This assertion is really gross,
211 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000212 Constant *Op = CE->getOperand(0);
213 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000214
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000215 // Remember, kids, pointers on x86 can be losslessly converted back and
216 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000217 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000218 && (Ty == Type::LongTy || Ty == Type::ULongTy
219 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000220 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000221 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
222 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000223 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
224 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000225 && "FIXME: Don't yet support this kind of constant cast expr");
226 O << "(";
227 emitConstantValueOnly(Op);
228 O << ")";
229 break;
230 }
231 case Instruction::Add:
232 O << "(";
233 emitConstantValueOnly(CE->getOperand(0));
234 O << ") + (";
235 emitConstantValueOnly(CE->getOperand(1));
236 O << ")";
237 break;
238 default:
239 assert(0 && "Unsupported operator!");
240 }
241 } else {
242 assert(0 && "Unknown constant value!");
243 }
244}
245
246// Print a constant value or values, with the appropriate storage class as a
247// prefix.
248void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000249 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000250
Chris Lattnerad200712003-09-09 16:23:36 +0000251 if (CV->isNullValue()) {
252 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000253 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000254 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000255 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000256 O << "\t.ascii\t";
257 printAsCString(O, CVA);
258 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000259 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000260 const std::vector<Use> &constValues = CVA->getValues();
261 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000262 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000263 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000264 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000265 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
266 // Print the fields in successive locations. Pad to align if needed!
267 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
268 const std::vector<Use>& constValues = CVS->getValues();
269 unsigned sizeSoFar = 0;
270 for (unsigned i=0, N = constValues.size(); i < N; i++) {
271 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000272
Chris Lattnerad200712003-09-09 16:23:36 +0000273 // Check if padding is needed and insert one or more 0s.
274 unsigned fieldSize = TD.getTypeSize(field->getType());
275 unsigned padSize = ((i == N-1? cvsLayout->StructSize
276 : cvsLayout->MemberOffsets[i+1])
277 - cvsLayout->MemberOffsets[i]) - fieldSize;
278 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000279
Chris Lattnerad200712003-09-09 16:23:36 +0000280 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000281 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000282
283 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000284 if (padSize)
285 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000286 }
Chris Lattnerad200712003-09-09 16:23:36 +0000287 assert(sizeSoFar == cvsLayout->StructSize &&
288 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000289 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000290 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
291 // FP Constants are printed as integer constants to avoid losing
292 // precision...
293 double Val = CFP->getValue();
294 switch (CFP->getType()->getPrimitiveID()) {
295 default: assert(0 && "Unknown floating point type!");
296 case Type::FloatTyID: {
297 union FU { // Abide by C TBAA rules
298 float FVal;
299 unsigned UVal;
300 } U;
301 U.FVal = Val;
302 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
303 return;
304 }
305 case Type::DoubleTyID: {
306 union DU { // Abide by C TBAA rules
307 double FVal;
308 uint64_t UVal;
309 } U;
310 U.FVal = Val;
311 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
312 return;
313 }
314 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000315 }
316
317 const Type *type = CV->getType();
318 O << "\t";
319 switch (type->getPrimitiveID()) {
320 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
321 O << ".byte";
322 break;
323 case Type::UShortTyID: case Type::ShortTyID:
324 O << ".word";
325 break;
326 case Type::FloatTyID: case Type::PointerTyID:
327 case Type::UIntTyID: case Type::IntTyID:
328 O << ".long";
329 break;
330 case Type::DoubleTyID:
331 case Type::ULongTyID: case Type::LongTyID:
332 O << ".quad";
333 break;
334 default:
335 assert (0 && "Can't handle printing this type of thing");
336 break;
337 }
338 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000339 emitConstantValueOnly(CV);
340 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000341}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000342
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000343/// printConstantPool - Print to the current output stream assembly
344/// representations of the constants in the constant pool MCP. This is
345/// used to print out constants which have been "spilled to memory" by
346/// the code generator.
347///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000348void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000349 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000350 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000351
Chris Lattnerb7089442003-01-13 00:35:03 +0000352 if (CP.empty()) return;
353
354 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
355 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000356 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000357 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000358 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
359 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000360 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000361 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000362}
363
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000364/// runOnMachineFunction - This uses the printMachineInstruction()
365/// method to print assembly for each instruction.
366///
Chris Lattner0285a332002-12-28 20:25:38 +0000367bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000368 // BBNumber is used here so that a given Printer will never give two
369 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000370 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000371
Chris Lattnere0121322003-08-03 23:37:09 +0000372 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000373 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000374 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000375
Chris Lattnerb7089442003-01-13 00:35:03 +0000376 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000377 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000378
Brian Gaeke6559bb92002-11-14 22:32:30 +0000379 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000380 O << "\t.text\n";
381 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000382 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000383 if (!EmitCygwin)
384 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000385 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000386
Brian Gaeked7908f62003-06-27 00:00:48 +0000387 // Number each basic block so that we can consistently refer to them
388 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000389 NumberForBB.clear();
390 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
391 I != E; ++I) {
392 NumberForBB[I->getBasicBlock()] = BBNumber++;
393 }
394
Brian Gaeke6559bb92002-11-14 22:32:30 +0000395 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000396 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
397 I != E; ++I) {
398 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000399 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000400 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000401 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
402 II != E; ++II) {
403 // Print the assembly for the instruction.
404 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000405 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000406 }
Chris Lattner0285a332002-12-28 20:25:38 +0000407 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000408
409 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000410 return false;
411}
412
Chris Lattner3d3067b2002-11-21 20:44:15 +0000413static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000414 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000415 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
416 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000417}
418
419static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000420 if (MI->getOperand(Op).isFrameIndex()) return true;
421 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000422 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000423 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
424 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000425}
426
Brian Gaeke2a098772003-08-11 19:05:46 +0000427
428
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000429void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000430 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000431 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000432 switch (MO.getType()) {
433 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000434 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000435 O << "<" << V->getName() << ">";
436 return;
437 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000438 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000439 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000440 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000441 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000442 O << "%" << RI.get(MO.getReg()).Name;
443 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000444 O << "%reg" << MO.getReg();
445 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000446
447 case MachineOperand::MO_SignExtendedImmed:
448 case MachineOperand::MO_UnextendedImmed:
449 O << (int)MO.getImmedValue();
450 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000451 case MachineOperand::MO_PCRelativeDisp: {
452 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
453 assert (i != NumberForBB.end()
454 && "Could not find a BB in the NumberForBB map!");
455 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000456 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000457 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000458 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000459 if (!elideOffsetKeyword)
460 O << "OFFSET ";
461 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000462 return;
463 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000464 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000465 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000466 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000467 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000468 }
469}
470
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000471static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
472 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000473 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000474 case X86II::Mem8: return "BYTE PTR";
475 case X86II::Mem16: return "WORD PTR";
476 case X86II::Mem32: return "DWORD PTR";
477 case X86II::Mem64: return "QWORD PTR";
478 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000479 }
480}
481
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000482void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000483 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000484
485 if (MI->getOperand(Op).isFrameIndex()) {
486 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
487 if (MI->getOperand(Op+3).getImmedValue())
488 O << " + " << MI->getOperand(Op+3).getImmedValue();
489 O << "]";
490 return;
491 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000492 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000493 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000494 if (MI->getOperand(Op+3).getImmedValue())
495 O << " + " << MI->getOperand(Op+3).getImmedValue();
496 O << "]";
497 return;
498 }
499
Chris Lattner3d3067b2002-11-21 20:44:15 +0000500 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000501 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000502 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000503 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000504
505 O << "[";
506 bool NeedPlus = false;
507 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000508 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000509 NeedPlus = true;
510 }
511
512 if (IndexReg.getReg()) {
513 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000514 if (ScaleVal != 1)
515 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000516 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000517 NeedPlus = true;
518 }
519
Chris Lattner0285a332002-12-28 20:25:38 +0000520 if (DispVal) {
521 if (NeedPlus)
522 if (DispVal > 0)
523 O << " + ";
524 else {
525 O << " - ";
526 DispVal = -DispVal;
527 }
528 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000529 }
530 O << "]";
531}
532
Chris Lattner30b2f722004-03-31 22:02:21 +0000533
534/// printImplUsesBefore - Emit the implicit-use registers for the instruction
535/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000536///
Chris Lattner30b2f722004-03-31 22:02:21 +0000537void Printer::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000538 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000539 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
540 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
541 // Bug Workaround: See note in Printer::doInitialization about %.
542 O << "%" << RI.get(*p).Name << ", ";
543 }
544 }
545}
546
547/// printImplUsesAfter - Emit the implicit-use registers for the instruction
548/// described by DESC, if its PrintImplUsesAfter flag is set.
549///
John Criswell4ffff9e2004-04-08 20:31:47 +0000550/// Inputs:
551/// Comma - List of registers will need a leading comma.
552/// Desc - Description of the Instruction.
553///
554/// Return value:
555/// true - Emitted one or more registers.
556/// false - Emitted no registers.
557///
558bool Printer::printImplUsesAfter(const TargetInstrDescriptor &Desc,
559 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000560 const MRegisterInfo &RI = *TM.getRegisterInfo();
561 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000562 bool emitted = false;
563 const unsigned *p = Desc.ImplicitUses;
564 if (*p) {
565 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
566 emitted = true;
567 ++p;
568 }
569 while (*p) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000570 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000571 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000572 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000573 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000574 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000575 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000576 return false;
577}
578
579/// printImplDefsAfter - Emit the implicit-definition registers for the
580/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
581///
582/// 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///
590bool Printer::printImplDefsAfter(const TargetInstrDescriptor &Desc,
591 const bool Comma = true) {
592 const MRegisterInfo &RI = *TM.getRegisterInfo();
593 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
594 bool emitted = false;
595 const unsigned *p = Desc.ImplicitDefs;
596 if (*p) {
597 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
598 emitted = true;
599 ++p;
600 }
601 while (*p) {
602 // Bug Workaround: See note in Printer::doInitialization about %.
603 O << ", %" << RI.get(*p).Name;
604 ++p;
605 }
606 return emitted;
607 }
608 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000609}
610
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000611/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000612/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000613///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000614void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000615 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000616 const TargetInstrInfo &TII = TM.getInstrInfo();
617 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000618
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000619 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000620 switch (Desc.TSFlags & X86II::FormMask) {
621 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000622 // Print pseudo-instructions as comments; either they should have been
623 // turned into real instructions by now, or they don't need to be
624 // seen by the assembler (e.g., IMPLICIT_USEs.)
625 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000626 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000627 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000628 O << " = phi ";
629 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000630 if (i != 1) O << ", ";
631 O << "[";
632 printOp(MI->getOperand(i));
633 O << ", ";
634 printOp(MI->getOperand(i+1));
635 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000636 }
637 } else {
638 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000639 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000640 printOp(MI->getOperand(0));
641 O << " = ";
642 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000643 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000644 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000645
646 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000647 O << " ";
648 if (MI->getOperand(i).isDef()) O << "*";
649 printOp(MI->getOperand(i));
650 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000651 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000652 }
653 O << "\n";
654 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000655
Chris Lattnerf9f60882002-11-18 06:56:51 +0000656 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000657 {
658 bool LeadingComma = false;
659
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000660 // The accepted forms of Raw instructions are:
661 // 1. nop - No operand required
662 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000663 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000664 //
665 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000666 (MI->getNumOperands() == 1 &&
667 (MI->getOperand(0).isPCRelativeDisp() ||
668 MI->getOperand(0).isGlobalAddress() ||
669 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000670 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000671 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000672
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000673 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000674 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000675 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000676 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000677 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
678 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000679 O << "\n";
680 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000681 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000682
Chris Lattner77875d82002-11-21 02:00:20 +0000683 case X86II::AddRegFrm: {
684 // There are currently two forms of acceptable AddRegFrm instructions.
685 // Either the instruction JUST takes a single register (like inc, dec, etc),
686 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000687 // (move immediate f.e.). Note that this immediate value might be stored as
688 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000689 // into a register. The initial register might be duplicated if this is a
690 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000691 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000692 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000693 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000694 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000695 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000696 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000697 MI->getOperand(1).isRegister() ||
698 MI->getOperand(1).isGlobalAddress() ||
699 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000700 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000701
Chris Lattner77875d82002-11-21 02:00:20 +0000702 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000703
Chris Lattnerb009c002004-02-11 19:26:28 +0000704 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000705
706 printImplUsesBefore(Desc); // fcmov*
707
Brian Gaekede420ae2003-07-23 20:25:08 +0000708 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000709 if (MI->getNumOperands() == 2 &&
710 (!MI->getOperand(1).isRegister() ||
711 MI->getOperand(1).getVRegValueOrNull() ||
712 MI->getOperand(1).isGlobalAddress() ||
713 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000714 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000715 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000716 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000717 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000718 O << "\n";
719 return;
720 }
Chris Lattner233ad712002-11-21 01:33:44 +0000721 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000722 // There are three forms of MRMDestReg instructions, those with 2
723 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000724 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000725 // 2 Operands: this is for things like mov that do not read a
726 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000727 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000728 // 2 Operands: two address instructions which def&use the first
729 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000730 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000731 // 3 Operands: in this form, two address instructions are the same
732 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000733 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000734 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000735 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000736 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000737 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000738 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000739
Chris Lattnerb009c002004-02-11 19:26:28 +0000740 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000741 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000742 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000743 printOp(MI->getOperand(1));
744 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000745 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000746 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000747 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000748 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000749 O << "\n";
750 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000751 }
Chris Lattner18042332002-11-21 21:03:39 +0000752
753 case X86II::MRMDestMem: {
754 // These instructions are the same as MRMDestReg, but instead of having a
755 // register reference for the mod/rm field, it's a memory reference.
756 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000757 assert(isMem(MI, 0) &&
758 (MI->getNumOperands() == 4+1 ||
759 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
760 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000761
Chris Lattnerb009c002004-02-11 19:26:28 +0000762 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000763 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000764 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000765 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000766 if (MI->getNumOperands() == 4+2) {
767 O << ", ";
768 printOp(MI->getOperand(5));
769 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000770 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000771 O << "\n";
772 return;
773 }
774
Chris Lattner233ad712002-11-21 01:33:44 +0000775 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000776 // There are three forms that are acceptable for MRMSrcReg
777 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000778 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000779 // 2 Operands: this is for things like mov that do not read a
780 // second input.
781 //
782 // 2 Operands: in this form, the last register is the ModR/M
783 // input. The first operand is a def&use. This is for things
784 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000785 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000786 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000787 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000788 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000789 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000790 assert(MI->getOperand(0).isRegister() &&
791 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000792 (MI->getNumOperands() == 2 ||
793 (MI->getNumOperands() == 3 &&
794 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000795 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000796
Chris Lattnerb009c002004-02-11 19:26:28 +0000797 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000798 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000799 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000800 printOp(MI->getOperand(1));
801 if (MI->getNumOperands() == 3) {
802 O << ", ";
803 printOp(MI->getOperand(2));
804 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000805 O << "\n";
806 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000807 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000808
Chris Lattner3d3067b2002-11-21 20:44:15 +0000809 case X86II::MRMSrcMem: {
810 // These instructions are the same as MRMSrcReg, but instead of having a
811 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000812 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000813 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000814 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000815(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000816 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000817 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000818 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000819 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000820 printMemReference(MI, 1);
821 if (MI->getNumOperands() == 2+4) {
822 O << ", ";
823 printOp(MI->getOperand(5));
824 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000825 O << "\n";
826 return;
827 }
828
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000829 case X86II::MRM0r: case X86II::MRM1r:
830 case X86II::MRM2r: case X86II::MRM3r:
831 case X86II::MRM4r: case X86II::MRM5r:
832 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000833 // In this form, the following are valid formats:
834 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000835 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000836 // 2. shl rdest, rinput <implicit CL or 1>
837 // 3. sbb rdest, rinput, immediate [rdest = rinput]
838 //
839 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000840 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000841 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000842 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000843 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000844 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000845 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000846 "Bad MRMSxR format!");
847
Chris Lattnerd9096832002-12-15 08:01:39 +0000848 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000849 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
850 O << "**";
851
Chris Lattnerb009c002004-02-11 19:26:28 +0000852 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000853 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000854 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000855 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000856 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000857 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000858 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000859 O << "\n";
860
861 return;
862 }
863
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000864 case X86II::MRM0m: case X86II::MRM1m:
865 case X86II::MRM2m: case X86II::MRM3m:
866 case X86II::MRM4m: case X86II::MRM5m:
867 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000868 // In this form, the following are valid formats:
869 // 1. sete [m]
870 // 2. cmp [m], immediate
871 // 2. shl [m], rinput <implicit CL or 1>
872 // 3. sbb [m], immediate
873 //
874 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
875 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000876 assert((MI->getNumOperands() != 5 ||
877 (MI->getOperand(4).isImmediate() ||
878 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000879 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000880
881 const MachineOperand &Op3 = MI->getOperand(3);
882
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000883 // gas bugs:
884 //
885 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000886 // is misassembled by gas in intel_syntax mode as its 32-bit
887 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
888 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000889 //
890 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000891 // misassembled by gas in intel_syntax mode as its 32-bit
892 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
893 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000894 //
895 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000896 // invalid opcode, saying "64 bit operations are only supported in
897 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
898 // [...]", which is wrong. Workaround: Output the raw opcode bytes
899 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000900 //
901 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
902 // invalid opcode, saying "64 bit operations are only supported in
903 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
904 // [...]", which is wrong. Workaround: Output the raw opcode bytes
905 // instead of the instruction.
906 if (MI->getOpcode() == X86::FSTP80m ||
907 MI->getOpcode() == X86::FLD80m ||
908 MI->getOpcode() == X86::FILD64m ||
909 MI->getOpcode() == X86::FISTP64m) {
910 GasBugWorkaroundEmitter gwe(O);
911 X86::emitInstruction(gwe, (X86InstrInfo&)TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000912 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000913
Chris Lattnerb009c002004-02-11 19:26:28 +0000914 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000915 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000916 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000917 if (MI->getNumOperands() == 5) {
918 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000919 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000920 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000921 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000922 O << "\n";
923 return;
924 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000925 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000926 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000927 }
Chris Lattner72614082002-10-25 22:55:53 +0000928}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000929
Chris Lattner93c1afa2003-08-11 19:35:26 +0000930bool Printer::doInitialization(Module &M) {
931 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000932 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000933 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
934 // instruction as a reference to the register named sp, and if you try to
935 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
936 // before being looked up in the symbol table. This creates spurious
937 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
938 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000939 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000940 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000941 return false; // success
942}
943
Chris Lattnerad200712003-09-09 16:23:36 +0000944// SwitchSection - Switch to the specified section of the executable if we are
945// not already in it!
946//
947static void SwitchSection(std::ostream &OS, std::string &CurSection,
948 const char *NewSection) {
949 if (CurSection != NewSection) {
950 CurSection = NewSection;
951 if (!CurSection.empty())
952 OS << "\t" << NewSection << "\n";
953 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000954}
955
Chris Lattnerad200712003-09-09 16:23:36 +0000956bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000957 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000958 std::string CurSection;
959
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000960 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000961 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
962 if (I->hasInitializer()) { // External global require no code
963 O << "\n\n";
964 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000965 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000966 unsigned Size = TD.getTypeSize(C->getType());
967 unsigned Align = TD.getTypeAlignment(C->getType());
968
969 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000970 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
971 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000972 SwitchSection(O, CurSection, ".data");
973 if (I->hasInternalLinkage())
974 O << "\t.local " << name << "\n";
975
976 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000977 << "," << (unsigned)TD.getTypeAlignment(C->getType());
978 O << "\t\t# ";
979 WriteAsOperand(O, I, true, true, &M);
980 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000981 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000982 switch (I->getLinkage()) {
983 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000984 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000985 // Nonnull linkonce -> weak
986 O << "\t.weak " << name << "\n";
987 SwitchSection(O, CurSection, "");
988 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
989 break;
990
991 case GlobalValue::AppendingLinkage:
992 // FIXME: appending linkage variables should go into a section of
993 // their name or something. For now, just emit them as external.
994 case GlobalValue::ExternalLinkage:
995 // If external or appending, declare as a global symbol
996 O << "\t.globl " << name << "\n";
997 // FALL THROUGH
998 case GlobalValue::InternalLinkage:
999 if (C->isNullValue())
1000 SwitchSection(O, CurSection, ".bss");
1001 else
1002 SwitchSection(O, CurSection, ".data");
1003 break;
1004 }
1005
1006 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001007 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001008 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001009 O << name << ":\t\t\t\t# ";
1010 WriteAsOperand(O, I, true, true, &M);
1011 O << " = ";
1012 WriteAsOperand(O, C, false, false, &M);
1013 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001014 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001015 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001016 }
Chris Lattnerad200712003-09-09 16:23:36 +00001017
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001018 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001019 return false; // success
1020}