blob: 76968f5d1998ba4f3bbcfe1d32ce5d4fbb4fb0b4 [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);
Chris Lattner26653832004-04-13 17:18:39 +0000108 bool printImplDefsBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000109 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
110 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000111 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000112 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000113 bool elideOffsetKeyword = false);
114 void printMemReference(const MachineInstr *MI, unsigned Op);
115 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000116 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000117 bool doInitialization(Module &M);
118 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000119 void emitGlobalConstant(const Constant* CV);
120 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000121 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000122} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000123
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000124/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000125/// assembly code for a MachineFunction to the given output stream,
126/// using the given target machine description. This should work
127/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000128///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000129FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000130 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000131}
132
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000133/// toOctal - Convert the low order bits of X into an octal digit.
134///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000135static inline char toOctal(int X) {
136 return (X&7)+'0';
137}
138
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000139/// getAsCString - Return the specified array as a C compatible
140/// string, only if the predicate isStringCompatible is true.
141///
Chris Lattnerac662d12003-11-03 20:19:49 +0000142static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000143 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000144
Chris Lattnerac662d12003-11-03 20:19:49 +0000145 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000146 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000147 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000148
149 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000150 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000151 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000152 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000153 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000154 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000155 } else {
156 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000157 case '\b': O << "\\b"; break;
158 case '\f': O << "\\f"; break;
159 case '\n': O << "\\n"; break;
160 case '\r': O << "\\r"; break;
161 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000162 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000163 O << '\\';
164 O << toOctal(C >> 6);
165 O << toOctal(C >> 3);
166 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000167 break;
168 }
169 }
170 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000171 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000172}
173
Chris Lattnerac662d12003-11-03 20:19:49 +0000174// Print out the specified constant, without a storage class. Only the
175// constants valid in constant expressions can occur here.
176void Printer::emitConstantValueOnly(const Constant *CV) {
177 if (CV->isNullValue())
178 O << "0";
179 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
180 assert(CB == ConstantBool::True);
181 O << "1";
182 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000183 if (((CI->getValue() << 32) >> 32) == CI->getValue())
184 O << CI->getValue();
185 else
186 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000187 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
188 O << CI->getValue();
189 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
190 // This is a constant address for a global variable or function. Use the
191 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000192 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000193 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
194 const TargetData &TD = TM.getTargetData();
195 switch(CE->getOpcode()) {
196 case Instruction::GetElementPtr: {
197 // generate a symbolic expression for the byte address
198 const Constant *ptrVal = CE->getOperand(0);
199 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
200 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
201 O << "(";
202 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000203 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000204 } else {
205 emitConstantValueOnly(ptrVal);
206 }
207 break;
208 }
209 case Instruction::Cast: {
210 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000211 // that do not involve a change in value. This assertion is really gross,
212 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000213 Constant *Op = CE->getOperand(0);
214 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000215
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000216 // Remember, kids, pointers on x86 can be losslessly converted back and
217 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000219 && (Ty == Type::LongTy || Ty == Type::ULongTy
220 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000221 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000222 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
223 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000224 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
225 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000226 && "FIXME: Don't yet support this kind of constant cast expr");
227 O << "(";
228 emitConstantValueOnly(Op);
229 O << ")";
230 break;
231 }
232 case Instruction::Add:
233 O << "(";
234 emitConstantValueOnly(CE->getOperand(0));
235 O << ") + (";
236 emitConstantValueOnly(CE->getOperand(1));
237 O << ")";
238 break;
239 default:
240 assert(0 && "Unsupported operator!");
241 }
242 } else {
243 assert(0 && "Unknown constant value!");
244 }
245}
246
247// Print a constant value or values, with the appropriate storage class as a
248// prefix.
249void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000250 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000251
Chris Lattnerad200712003-09-09 16:23:36 +0000252 if (CV->isNullValue()) {
253 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000254 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000255 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000256 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000257 O << "\t.ascii\t";
258 printAsCString(O, CVA);
259 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000260 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000261 const std::vector<Use> &constValues = CVA->getValues();
262 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000263 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000264 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000265 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000266 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
267 // Print the fields in successive locations. Pad to align if needed!
268 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
269 const std::vector<Use>& constValues = CVS->getValues();
270 unsigned sizeSoFar = 0;
271 for (unsigned i=0, N = constValues.size(); i < N; i++) {
272 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000273
Chris Lattnerad200712003-09-09 16:23:36 +0000274 // Check if padding is needed and insert one or more 0s.
275 unsigned fieldSize = TD.getTypeSize(field->getType());
276 unsigned padSize = ((i == N-1? cvsLayout->StructSize
277 : cvsLayout->MemberOffsets[i+1])
278 - cvsLayout->MemberOffsets[i]) - fieldSize;
279 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000280
Chris Lattnerad200712003-09-09 16:23:36 +0000281 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000282 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000283
284 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000285 if (padSize)
286 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000287 }
Chris Lattnerad200712003-09-09 16:23:36 +0000288 assert(sizeSoFar == cvsLayout->StructSize &&
289 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000290 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000291 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
292 // FP Constants are printed as integer constants to avoid losing
293 // precision...
294 double Val = CFP->getValue();
295 switch (CFP->getType()->getPrimitiveID()) {
296 default: assert(0 && "Unknown floating point type!");
297 case Type::FloatTyID: {
298 union FU { // Abide by C TBAA rules
299 float FVal;
300 unsigned UVal;
301 } U;
302 U.FVal = Val;
303 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
304 return;
305 }
306 case Type::DoubleTyID: {
307 union DU { // Abide by C TBAA rules
308 double FVal;
309 uint64_t UVal;
310 } U;
311 U.FVal = Val;
312 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
313 return;
314 }
315 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000316 }
317
318 const Type *type = CV->getType();
319 O << "\t";
320 switch (type->getPrimitiveID()) {
321 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
322 O << ".byte";
323 break;
324 case Type::UShortTyID: case Type::ShortTyID:
325 O << ".word";
326 break;
327 case Type::FloatTyID: case Type::PointerTyID:
328 case Type::UIntTyID: case Type::IntTyID:
329 O << ".long";
330 break;
331 case Type::DoubleTyID:
332 case Type::ULongTyID: case Type::LongTyID:
333 O << ".quad";
334 break;
335 default:
336 assert (0 && "Can't handle printing this type of thing");
337 break;
338 }
339 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000340 emitConstantValueOnly(CV);
341 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000342}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000343
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000344/// printConstantPool - Print to the current output stream assembly
345/// representations of the constants in the constant pool MCP. This is
346/// used to print out constants which have been "spilled to memory" by
347/// the code generator.
348///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000349void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000350 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000351 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000352
Chris Lattnerb7089442003-01-13 00:35:03 +0000353 if (CP.empty()) return;
354
355 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
356 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000357 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000358 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000359 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
360 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000361 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000362 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000363}
364
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000365/// runOnMachineFunction - This uses the printMachineInstruction()
366/// method to print assembly for each instruction.
367///
Chris Lattner0285a332002-12-28 20:25:38 +0000368bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000369 // BBNumber is used here so that a given Printer will never give two
370 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000371 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000372
Chris Lattnere0121322003-08-03 23:37:09 +0000373 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000374 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000375 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000376
Chris Lattnerb7089442003-01-13 00:35:03 +0000377 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000378 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000379
Brian Gaeke6559bb92002-11-14 22:32:30 +0000380 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000381 O << "\t.text\n";
382 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000383 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000384 if (!EmitCygwin)
385 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000386 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000387
Brian Gaeked7908f62003-06-27 00:00:48 +0000388 // Number each basic block so that we can consistently refer to them
389 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000390 NumberForBB.clear();
391 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
392 I != E; ++I) {
393 NumberForBB[I->getBasicBlock()] = BBNumber++;
394 }
395
Brian Gaeke6559bb92002-11-14 22:32:30 +0000396 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000397 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
398 I != E; ++I) {
399 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000400 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000401 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000402 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
403 II != E; ++II) {
404 // Print the assembly for the instruction.
405 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000406 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000407 }
Chris Lattner0285a332002-12-28 20:25:38 +0000408 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000409
410 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000411 return false;
412}
413
Chris Lattner3d3067b2002-11-21 20:44:15 +0000414static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000415 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000416 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
417 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000418}
419
420static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000421 if (MI->getOperand(Op).isFrameIndex()) return true;
422 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000423 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000424 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
425 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000426}
427
Brian Gaeke2a098772003-08-11 19:05:46 +0000428
429
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000430void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000431 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000432 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000433 switch (MO.getType()) {
434 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000435 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000436 O << "<" << V->getName() << ">";
437 return;
438 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000439 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000440 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000441 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000442 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000443 O << "%" << RI.get(MO.getReg()).Name;
444 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000445 O << "%reg" << MO.getReg();
446 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000447
448 case MachineOperand::MO_SignExtendedImmed:
449 case MachineOperand::MO_UnextendedImmed:
450 O << (int)MO.getImmedValue();
451 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000452 case MachineOperand::MO_PCRelativeDisp: {
453 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
454 assert (i != NumberForBB.end()
455 && "Could not find a BB in the NumberForBB map!");
456 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000457 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000458 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000459 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000460 if (!elideOffsetKeyword)
461 O << "OFFSET ";
462 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000463 return;
464 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000465 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000466 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000467 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000468 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000469 }
470}
471
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000472static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
473 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000474 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000475 case X86II::Mem8: return "BYTE PTR";
476 case X86II::Mem16: return "WORD PTR";
477 case X86II::Mem32: return "DWORD PTR";
478 case X86II::Mem64: return "QWORD PTR";
479 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000480 }
481}
482
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000483void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000484 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000485
486 if (MI->getOperand(Op).isFrameIndex()) {
487 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
488 if (MI->getOperand(Op+3).getImmedValue())
489 O << " + " << MI->getOperand(Op+3).getImmedValue();
490 O << "]";
491 return;
492 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000493 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000494 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000495 if (MI->getOperand(Op+3).getImmedValue())
496 O << " + " << MI->getOperand(Op+3).getImmedValue();
497 O << "]";
498 return;
499 }
500
Chris Lattner3d3067b2002-11-21 20:44:15 +0000501 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000502 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000504 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505
506 O << "[";
507 bool NeedPlus = false;
508 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000509 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000510 NeedPlus = true;
511 }
512
513 if (IndexReg.getReg()) {
514 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000515 if (ScaleVal != 1)
516 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000517 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000518 NeedPlus = true;
519 }
520
Chris Lattner0285a332002-12-28 20:25:38 +0000521 if (DispVal) {
522 if (NeedPlus)
523 if (DispVal > 0)
524 O << " + ";
525 else {
526 O << " - ";
527 DispVal = -DispVal;
528 }
529 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000530 }
531 O << "]";
532}
533
Chris Lattner30b2f722004-03-31 22:02:21 +0000534
535/// printImplUsesBefore - Emit the implicit-use registers for the instruction
536/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000537///
Chris Lattner30b2f722004-03-31 22:02:21 +0000538void Printer::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000539 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000540 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
541 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
542 // Bug Workaround: See note in Printer::doInitialization about %.
543 O << "%" << RI.get(*p).Name << ", ";
544 }
545 }
546}
547
Chris Lattner26653832004-04-13 17:18:39 +0000548/// printImplDefsBefore - Emit the implicit-def registers for the instruction
549/// described by DESC, if its PrintImplUsesBefore flag is set. Return true if
550/// we printed any registers.
551///
552bool Printer::printImplDefsBefore(const TargetInstrDescriptor &Desc) {
553 bool Printed = false;
554 const MRegisterInfo &RI = *TM.getRegisterInfo();
555 if (Desc.TSFlags & X86II::PrintImplDefsBefore) {
556 const unsigned *p = Desc.ImplicitDefs;
557 if (*p) {
558 O << (Printed ? ", %" : "%") << RI.get (*p).Name;
559 Printed = true;
560 ++p;
561 }
562 while (*p) {
563 // Bug Workaround: See note in Printer::doInitialization about %.
564 O << ", %" << RI.get(*p).Name;
565 ++p;
566 }
567 }
568 return Printed;
569}
570
571
Chris Lattner30b2f722004-03-31 22:02:21 +0000572/// printImplUsesAfter - Emit the implicit-use registers for the instruction
573/// described by DESC, if its PrintImplUsesAfter flag is set.
574///
John Criswell4ffff9e2004-04-08 20:31:47 +0000575/// Inputs:
576/// Comma - List of registers will need a leading comma.
577/// Desc - Description of the Instruction.
578///
579/// Return value:
580/// true - Emitted one or more registers.
581/// false - Emitted no registers.
582///
583bool Printer::printImplUsesAfter(const TargetInstrDescriptor &Desc,
584 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000585 const MRegisterInfo &RI = *TM.getRegisterInfo();
586 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000587 bool emitted = false;
588 const unsigned *p = Desc.ImplicitUses;
589 if (*p) {
590 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
591 emitted = true;
592 ++p;
593 }
594 while (*p) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000595 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000596 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000597 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000598 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000599 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000600 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000601 return false;
602}
603
604/// printImplDefsAfter - Emit the implicit-definition registers for the
605/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
606///
607/// Inputs:
608/// Comma - List of registers will need a leading comma.
609/// Desc - Description of the Instruction
610///
611/// Return value:
612/// true - Emitted one or more registers.
613/// false - Emitted no registers.
614///
615bool Printer::printImplDefsAfter(const TargetInstrDescriptor &Desc,
616 const bool Comma = true) {
617 const MRegisterInfo &RI = *TM.getRegisterInfo();
618 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
619 bool emitted = false;
620 const unsigned *p = Desc.ImplicitDefs;
621 if (*p) {
622 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
623 emitted = true;
624 ++p;
625 }
626 while (*p) {
627 // Bug Workaround: See note in Printer::doInitialization about %.
628 O << ", %" << RI.get(*p).Name;
629 ++p;
630 }
631 return emitted;
632 }
633 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000634}
635
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000636/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000637/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000638///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000639void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000640 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000641 const TargetInstrInfo &TII = TM.getInstrInfo();
642 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000643
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000644 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000645 switch (Desc.TSFlags & X86II::FormMask) {
646 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000647 // Print pseudo-instructions as comments; either they should have been
648 // turned into real instructions by now, or they don't need to be
649 // seen by the assembler (e.g., IMPLICIT_USEs.)
650 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000651 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000652 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000653 O << " = phi ";
654 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000655 if (i != 1) O << ", ";
656 O << "[";
657 printOp(MI->getOperand(i));
658 O << ", ";
659 printOp(MI->getOperand(i+1));
660 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000661 }
662 } else {
663 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000664 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000665 printOp(MI->getOperand(0));
666 O << " = ";
667 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000668 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000669 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000670
671 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000672 O << " ";
673 if (MI->getOperand(i).isDef()) O << "*";
674 printOp(MI->getOperand(i));
675 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000676 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000677 }
678 O << "\n";
679 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000680
Chris Lattnerf9f60882002-11-18 06:56:51 +0000681 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000682 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000683 // The accepted forms of Raw instructions are:
684 // 1. nop - No operand required
685 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000686 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattner26653832004-04-13 17:18:39 +0000687 // 4. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000688 //
689 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000690 (MI->getNumOperands() == 1 &&
691 (MI->getOperand(0).isPCRelativeDisp() ||
692 MI->getOperand(0).isGlobalAddress() ||
Chris Lattner26653832004-04-13 17:18:39 +0000693 MI->getOperand(0).isExternalSymbol() ||
694 MI->getOperand(0).isImmediate())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000695 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000696 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000697
Chris Lattner26653832004-04-13 17:18:39 +0000698 bool LeadingComma = printImplDefsBefore(Desc);
699
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000700 if (MI->getNumOperands() == 1) {
Chris Lattner26653832004-04-13 17:18:39 +0000701 if (LeadingComma) O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000702 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000703 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000704 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000705 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
706 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000707 O << "\n";
708 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000709 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000710
Chris Lattner77875d82002-11-21 02:00:20 +0000711 case X86II::AddRegFrm: {
712 // There are currently two forms of acceptable AddRegFrm instructions.
713 // Either the instruction JUST takes a single register (like inc, dec, etc),
714 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000715 // (move immediate f.e.). Note that this immediate value might be stored as
716 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000717 // into a register. The initial register might be duplicated if this is a
718 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000719 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000720 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000721 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000722 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000723 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000724 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000725 MI->getOperand(1).isRegister() ||
726 MI->getOperand(1).isGlobalAddress() ||
727 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000728 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000729
Chris Lattner77875d82002-11-21 02:00:20 +0000730 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000731
Chris Lattnerb009c002004-02-11 19:26:28 +0000732 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000733
734 printImplUsesBefore(Desc); // fcmov*
735
Brian Gaekede420ae2003-07-23 20:25:08 +0000736 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000737 if (MI->getNumOperands() == 2 &&
738 (!MI->getOperand(1).isRegister() ||
739 MI->getOperand(1).getVRegValueOrNull() ||
740 MI->getOperand(1).isGlobalAddress() ||
741 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000742 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000743 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000744 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000745 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000746 O << "\n";
747 return;
748 }
Chris Lattner233ad712002-11-21 01:33:44 +0000749 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000750 // There are three forms of MRMDestReg instructions, those with 2
751 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000752 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000753 // 2 Operands: this is for things like mov that do not read a
754 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000755 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000756 // 2 Operands: two address instructions which def&use the first
757 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000758 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000759 // 3 Operands: in this form, two address instructions are the same
760 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000761 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000762 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000763 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000764 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000765 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000766 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000767
Chris Lattnerb009c002004-02-11 19:26:28 +0000768 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000769 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000770 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000771 printOp(MI->getOperand(1));
772 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000773 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000774 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000775 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000776 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000777 O << "\n";
778 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000779 }
Chris Lattner18042332002-11-21 21:03:39 +0000780
781 case X86II::MRMDestMem: {
782 // These instructions are the same as MRMDestReg, but instead of having a
783 // register reference for the mod/rm field, it's a memory reference.
784 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000785 assert(isMem(MI, 0) &&
786 (MI->getNumOperands() == 4+1 ||
787 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
788 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000789
Chris Lattnerb009c002004-02-11 19:26:28 +0000790 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000791 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000792 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000793 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000794 if (MI->getNumOperands() == 4+2) {
795 O << ", ";
796 printOp(MI->getOperand(5));
797 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000798 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000799 O << "\n";
800 return;
801 }
802
Chris Lattner233ad712002-11-21 01:33:44 +0000803 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000804 // There are three forms that are acceptable for MRMSrcReg
805 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000806 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000807 // 2 Operands: this is for things like mov that do not read a
808 // second input.
809 //
810 // 2 Operands: in this form, the last register is the ModR/M
811 // input. The first operand is a def&use. This is for things
812 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000813 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000814 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000815 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000816 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000817 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000818 assert(MI->getOperand(0).isRegister() &&
819 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000820 (MI->getNumOperands() == 2 ||
821 (MI->getNumOperands() == 3 &&
822 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000823 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000824
Chris Lattnerb009c002004-02-11 19:26:28 +0000825 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000826 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000827 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000828 printOp(MI->getOperand(1));
829 if (MI->getNumOperands() == 3) {
830 O << ", ";
831 printOp(MI->getOperand(2));
832 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000833 O << "\n";
834 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000835 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000836
Chris Lattner3d3067b2002-11-21 20:44:15 +0000837 case X86II::MRMSrcMem: {
838 // These instructions are the same as MRMSrcReg, but instead of having a
839 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000840 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000841 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000842 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000843(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000844 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000845 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000846 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000847 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000848 printMemReference(MI, 1);
849 if (MI->getNumOperands() == 2+4) {
850 O << ", ";
851 printOp(MI->getOperand(5));
852 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000853 O << "\n";
854 return;
855 }
856
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000857 case X86II::MRM0r: case X86II::MRM1r:
858 case X86II::MRM2r: case X86II::MRM3r:
859 case X86II::MRM4r: case X86II::MRM5r:
860 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000861 // In this form, the following are valid formats:
862 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000863 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000864 // 2. shl rdest, rinput <implicit CL or 1>
865 // 3. sbb rdest, rinput, immediate [rdest = rinput]
866 //
867 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000868 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000869 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000870 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000871 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000872 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000873 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000874 "Bad MRMSxR format!");
875
Chris Lattnerd9096832002-12-15 08:01:39 +0000876 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000877 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
878 O << "**";
879
Chris Lattnerb009c002004-02-11 19:26:28 +0000880 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000881 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000882 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000883 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000884 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000885 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000886 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000887 O << "\n";
888
889 return;
890 }
891
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000892 case X86II::MRM0m: case X86II::MRM1m:
893 case X86II::MRM2m: case X86II::MRM3m:
894 case X86II::MRM4m: case X86II::MRM5m:
895 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000896 // In this form, the following are valid formats:
897 // 1. sete [m]
898 // 2. cmp [m], immediate
899 // 2. shl [m], rinput <implicit CL or 1>
900 // 3. sbb [m], immediate
901 //
902 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
903 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000904 assert((MI->getNumOperands() != 5 ||
905 (MI->getOperand(4).isImmediate() ||
906 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000907 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000908
909 const MachineOperand &Op3 = MI->getOperand(3);
910
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000911 // gas bugs:
912 //
913 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000914 // is misassembled by gas in intel_syntax mode as its 32-bit
915 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
916 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000917 //
918 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000919 // misassembled by gas in intel_syntax mode as its 32-bit
920 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
921 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000922 //
923 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000924 // invalid opcode, saying "64 bit operations are only supported in
925 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
926 // [...]", which is wrong. Workaround: Output the raw opcode bytes
927 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000928 //
929 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
930 // invalid opcode, saying "64 bit operations are only supported in
931 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
932 // [...]", which is wrong. Workaround: Output the raw opcode bytes
933 // instead of the instruction.
934 if (MI->getOpcode() == X86::FSTP80m ||
935 MI->getOpcode() == X86::FLD80m ||
936 MI->getOpcode() == X86::FILD64m ||
937 MI->getOpcode() == X86::FISTP64m) {
938 GasBugWorkaroundEmitter gwe(O);
939 X86::emitInstruction(gwe, (X86InstrInfo&)TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000940 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000941
Chris Lattnerb009c002004-02-11 19:26:28 +0000942 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000943 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000944 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000945 if (MI->getNumOperands() == 5) {
946 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000947 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000948 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000949 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000950 O << "\n";
951 return;
952 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000953 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000954 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000955 }
Chris Lattner72614082002-10-25 22:55:53 +0000956}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000957
Chris Lattner93c1afa2003-08-11 19:35:26 +0000958bool Printer::doInitialization(Module &M) {
959 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000960 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000961 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
962 // instruction as a reference to the register named sp, and if you try to
963 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
964 // before being looked up in the symbol table. This creates spurious
965 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
966 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000967 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000968 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000969 return false; // success
970}
971
Chris Lattnerad200712003-09-09 16:23:36 +0000972// SwitchSection - Switch to the specified section of the executable if we are
973// not already in it!
974//
975static void SwitchSection(std::ostream &OS, std::string &CurSection,
976 const char *NewSection) {
977 if (CurSection != NewSection) {
978 CurSection = NewSection;
979 if (!CurSection.empty())
980 OS << "\t" << NewSection << "\n";
981 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000982}
983
Chris Lattnerad200712003-09-09 16:23:36 +0000984bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000985 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000986 std::string CurSection;
987
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000988 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000989 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
990 if (I->hasInitializer()) { // External global require no code
991 O << "\n\n";
992 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000993 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000994 unsigned Size = TD.getTypeSize(C->getType());
995 unsigned Align = TD.getTypeAlignment(C->getType());
996
997 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000998 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
999 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +00001000 SwitchSection(O, CurSection, ".data");
1001 if (I->hasInternalLinkage())
1002 O << "\t.local " << name << "\n";
1003
1004 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +00001005 << "," << (unsigned)TD.getTypeAlignment(C->getType());
1006 O << "\t\t# ";
1007 WriteAsOperand(O, I, true, true, &M);
1008 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001009 } else {
Chris Lattnerad200712003-09-09 16:23:36 +00001010 switch (I->getLinkage()) {
1011 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +00001012 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +00001013 // Nonnull linkonce -> weak
1014 O << "\t.weak " << name << "\n";
1015 SwitchSection(O, CurSection, "");
1016 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
1017 break;
1018
1019 case GlobalValue::AppendingLinkage:
1020 // FIXME: appending linkage variables should go into a section of
1021 // their name or something. For now, just emit them as external.
1022 case GlobalValue::ExternalLinkage:
1023 // If external or appending, declare as a global symbol
1024 O << "\t.globl " << name << "\n";
1025 // FALL THROUGH
1026 case GlobalValue::InternalLinkage:
1027 if (C->isNullValue())
1028 SwitchSection(O, CurSection, ".bss");
1029 else
1030 SwitchSection(O, CurSection, ".data");
1031 break;
1032 }
1033
1034 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001035 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001036 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001037 O << name << ":\t\t\t\t# ";
1038 WriteAsOperand(O, I, true, true, &M);
1039 O << " = ";
1040 WriteAsOperand(O, C, false, false, &M);
1041 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001042 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001043 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001044 }
Chris Lattnerad200712003-09-09 16:23:36 +00001045
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001046 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001047 return false; // success
1048}