blob: c6b581a5b41edb21fc30f45884f2a485e2989fdf [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
Brian Gaeke2a098772003-08-11 19:05:46 +0000107 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000108 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000109 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000110 bool elideOffsetKeyword = false);
111 void printMemReference(const MachineInstr *MI, unsigned Op);
112 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000113 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000114 bool doInitialization(Module &M);
115 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000116 void emitGlobalConstant(const Constant* CV);
117 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000118 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000119} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000120
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000121/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000122/// assembly code for a MachineFunction to the given output stream,
123/// using the given target machine description. This should work
124/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000125///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000126FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000127 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000128}
129
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000130/// toOctal - Convert the low order bits of X into an octal digit.
131///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000132static inline char toOctal(int X) {
133 return (X&7)+'0';
134}
135
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000136/// getAsCString - Return the specified array as a C compatible
137/// string, only if the predicate isStringCompatible is true.
138///
Chris Lattnerac662d12003-11-03 20:19:49 +0000139static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000140 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000141
Chris Lattnerac662d12003-11-03 20:19:49 +0000142 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000143 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000144 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000145
146 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000147 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000148 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000149 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000150 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000151 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152 } else {
153 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000154 case '\b': O << "\\b"; break;
155 case '\f': O << "\\f"; break;
156 case '\n': O << "\\n"; break;
157 case '\r': O << "\\r"; break;
158 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000159 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000160 O << '\\';
161 O << toOctal(C >> 6);
162 O << toOctal(C >> 3);
163 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000164 break;
165 }
166 }
167 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000168 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000169}
170
Chris Lattnerac662d12003-11-03 20:19:49 +0000171// Print out the specified constant, without a storage class. Only the
172// constants valid in constant expressions can occur here.
173void Printer::emitConstantValueOnly(const Constant *CV) {
174 if (CV->isNullValue())
175 O << "0";
176 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
177 assert(CB == ConstantBool::True);
178 O << "1";
179 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000180 if (((CI->getValue() << 32) >> 32) == CI->getValue())
181 O << CI->getValue();
182 else
183 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000184 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
185 O << CI->getValue();
186 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
187 // This is a constant address for a global variable or function. Use the
188 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000189 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000190 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
191 const TargetData &TD = TM.getTargetData();
192 switch(CE->getOpcode()) {
193 case Instruction::GetElementPtr: {
194 // generate a symbolic expression for the byte address
195 const Constant *ptrVal = CE->getOperand(0);
196 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
197 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
198 O << "(";
199 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000200 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000201 } else {
202 emitConstantValueOnly(ptrVal);
203 }
204 break;
205 }
206 case Instruction::Cast: {
207 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000208 // that do not involve a change in value. This assertion is really gross,
209 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000210 Constant *Op = CE->getOperand(0);
211 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000212
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000213 // Remember, kids, pointers on x86 can be losslessly converted back and
214 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000215 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000216 && (Ty == Type::LongTy || Ty == Type::ULongTy
217 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000219 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
220 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000221 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
222 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000223 && "FIXME: Don't yet support this kind of constant cast expr");
224 O << "(";
225 emitConstantValueOnly(Op);
226 O << ")";
227 break;
228 }
229 case Instruction::Add:
230 O << "(";
231 emitConstantValueOnly(CE->getOperand(0));
232 O << ") + (";
233 emitConstantValueOnly(CE->getOperand(1));
234 O << ")";
235 break;
236 default:
237 assert(0 && "Unsupported operator!");
238 }
239 } else {
240 assert(0 && "Unknown constant value!");
241 }
242}
243
244// Print a constant value or values, with the appropriate storage class as a
245// prefix.
246void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000247 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000248
Chris Lattnerad200712003-09-09 16:23:36 +0000249 if (CV->isNullValue()) {
250 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000251 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000252 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000253 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000254 O << "\t.ascii\t";
255 printAsCString(O, CVA);
256 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000257 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000258 const std::vector<Use> &constValues = CVA->getValues();
259 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000260 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000261 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000262 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000263 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
264 // Print the fields in successive locations. Pad to align if needed!
265 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
266 const std::vector<Use>& constValues = CVS->getValues();
267 unsigned sizeSoFar = 0;
268 for (unsigned i=0, N = constValues.size(); i < N; i++) {
269 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000270
Chris Lattnerad200712003-09-09 16:23:36 +0000271 // Check if padding is needed and insert one or more 0s.
272 unsigned fieldSize = TD.getTypeSize(field->getType());
273 unsigned padSize = ((i == N-1? cvsLayout->StructSize
274 : cvsLayout->MemberOffsets[i+1])
275 - cvsLayout->MemberOffsets[i]) - fieldSize;
276 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000277
Chris Lattnerad200712003-09-09 16:23:36 +0000278 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000279 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000280
281 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000282 if (padSize)
283 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000284 }
Chris Lattnerad200712003-09-09 16:23:36 +0000285 assert(sizeSoFar == cvsLayout->StructSize &&
286 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000287 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000288 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
289 // FP Constants are printed as integer constants to avoid losing
290 // precision...
291 double Val = CFP->getValue();
292 switch (CFP->getType()->getPrimitiveID()) {
293 default: assert(0 && "Unknown floating point type!");
294 case Type::FloatTyID: {
295 union FU { // Abide by C TBAA rules
296 float FVal;
297 unsigned UVal;
298 } U;
299 U.FVal = Val;
300 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
301 return;
302 }
303 case Type::DoubleTyID: {
304 union DU { // Abide by C TBAA rules
305 double FVal;
306 uint64_t UVal;
307 } U;
308 U.FVal = Val;
309 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
310 return;
311 }
312 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000313 }
314
315 const Type *type = CV->getType();
316 O << "\t";
317 switch (type->getPrimitiveID()) {
318 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
319 O << ".byte";
320 break;
321 case Type::UShortTyID: case Type::ShortTyID:
322 O << ".word";
323 break;
324 case Type::FloatTyID: case Type::PointerTyID:
325 case Type::UIntTyID: case Type::IntTyID:
326 O << ".long";
327 break;
328 case Type::DoubleTyID:
329 case Type::ULongTyID: case Type::LongTyID:
330 O << ".quad";
331 break;
332 default:
333 assert (0 && "Can't handle printing this type of thing");
334 break;
335 }
336 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000337 emitConstantValueOnly(CV);
338 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000339}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000340
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000341/// printConstantPool - Print to the current output stream assembly
342/// representations of the constants in the constant pool MCP. This is
343/// used to print out constants which have been "spilled to memory" by
344/// the code generator.
345///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000346void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000347 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000348 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000349
Chris Lattnerb7089442003-01-13 00:35:03 +0000350 if (CP.empty()) return;
351
352 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
353 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000354 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000355 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000356 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
357 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000358 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000359 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000360}
361
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000362/// runOnMachineFunction - This uses the printMachineInstruction()
363/// method to print assembly for each instruction.
364///
Chris Lattner0285a332002-12-28 20:25:38 +0000365bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000366 // BBNumber is used here so that a given Printer will never give two
367 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000368 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000369
Chris Lattnere0121322003-08-03 23:37:09 +0000370 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000371 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000372 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000373
Chris Lattnerb7089442003-01-13 00:35:03 +0000374 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000375 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000376
Brian Gaeke6559bb92002-11-14 22:32:30 +0000377 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000378 O << "\t.text\n";
379 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000380 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000381 if (!EmitCygwin)
382 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000383 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000384
Brian Gaeked7908f62003-06-27 00:00:48 +0000385 // Number each basic block so that we can consistently refer to them
386 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000387 NumberForBB.clear();
388 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
389 I != E; ++I) {
390 NumberForBB[I->getBasicBlock()] = BBNumber++;
391 }
392
Brian Gaeke6559bb92002-11-14 22:32:30 +0000393 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000394 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
395 I != E; ++I) {
396 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000397 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000398 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000399 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
400 II != E; ++II) {
401 // Print the assembly for the instruction.
402 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000403 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000404 }
Chris Lattner0285a332002-12-28 20:25:38 +0000405 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000406
407 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000408 return false;
409}
410
Chris Lattner3d3067b2002-11-21 20:44:15 +0000411static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000412 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000413 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
414 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000415}
416
417static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000418 if (MI->getOperand(Op).isFrameIndex()) return true;
419 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000420 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000421 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
422 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000423}
424
Brian Gaeke2a098772003-08-11 19:05:46 +0000425
426
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000427void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000428 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000429 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000430 switch (MO.getType()) {
431 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000432 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000433 O << "<" << V->getName() << ">";
434 return;
435 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000436 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000437 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000438 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000439 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000440 O << "%" << RI.get(MO.getReg()).Name;
441 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000442 O << "%reg" << MO.getReg();
443 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000444
445 case MachineOperand::MO_SignExtendedImmed:
446 case MachineOperand::MO_UnextendedImmed:
447 O << (int)MO.getImmedValue();
448 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000449 case MachineOperand::MO_PCRelativeDisp: {
450 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
451 assert (i != NumberForBB.end()
452 && "Could not find a BB in the NumberForBB map!");
453 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000454 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000455 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000456 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000457 if (!elideOffsetKeyword)
458 O << "OFFSET ";
459 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000460 return;
461 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000462 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000463 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000464 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000465 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000466 }
467}
468
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000469static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
470 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000471 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000472 case X86II::Mem8: return "BYTE PTR";
473 case X86II::Mem16: return "WORD PTR";
474 case X86II::Mem32: return "DWORD PTR";
475 case X86II::Mem64: return "QWORD PTR";
476 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000477 }
478}
479
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000480void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000481 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000482
483 if (MI->getOperand(Op).isFrameIndex()) {
484 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
485 if (MI->getOperand(Op+3).getImmedValue())
486 O << " + " << MI->getOperand(Op+3).getImmedValue();
487 O << "]";
488 return;
489 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000490 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000491 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000492 if (MI->getOperand(Op+3).getImmedValue())
493 O << " + " << MI->getOperand(Op+3).getImmedValue();
494 O << "]";
495 return;
496 }
497
Chris Lattner3d3067b2002-11-21 20:44:15 +0000498 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000499 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000500 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000501 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000502
503 O << "[";
504 bool NeedPlus = false;
505 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000506 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000507 NeedPlus = true;
508 }
509
510 if (IndexReg.getReg()) {
511 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000512 if (ScaleVal != 1)
513 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000514 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000515 NeedPlus = true;
516 }
517
Chris Lattner0285a332002-12-28 20:25:38 +0000518 if (DispVal) {
519 if (NeedPlus)
520 if (DispVal > 0)
521 O << " + ";
522 else {
523 O << " - ";
524 DispVal = -DispVal;
525 }
526 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000527 }
528 O << "]";
529}
530
Brian Gaeke2a098772003-08-11 19:05:46 +0000531/// checkImplUses - Emit the implicit-use registers for the
532/// instruction described by DESC, if its PrintImplUses flag is set.
533///
534void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
535 const MRegisterInfo &RI = *TM.getRegisterInfo();
536 if (Desc.TSFlags & X86II::PrintImplUses) {
537 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
538 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000539 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000540 }
541 }
542}
543
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000544/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000545/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000546///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000547void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000548 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000549 const TargetInstrInfo &TII = TM.getInstrInfo();
550 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000551
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000552 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000553 switch (Desc.TSFlags & X86II::FormMask) {
554 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000555 // Print pseudo-instructions as comments; either they should have been
556 // turned into real instructions by now, or they don't need to be
557 // seen by the assembler (e.g., IMPLICIT_USEs.)
558 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000559 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000560 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000561 O << " = phi ";
562 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
563 if (i != 1) O << ", ";
564 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000565 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000566 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000567 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000568 O << "]";
569 }
570 } else {
571 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000572 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000573 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000574 O << " = ";
575 ++i;
576 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000577 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000578
579 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
580 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000581 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000582 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000583 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000584 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000585 }
586 O << "\n";
587 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000588
Chris Lattnerf9f60882002-11-18 06:56:51 +0000589 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000590 // The accepted forms of Raw instructions are:
591 // 1. nop - No operand required
592 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000593 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000594 //
595 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000596 (MI->getNumOperands() == 1 &&
597 (MI->getOperand(0).isPCRelativeDisp() ||
598 MI->getOperand(0).isGlobalAddress() ||
599 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000600 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000601 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000602
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000603 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000604 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000605 }
606 O << "\n";
607 return;
608
Chris Lattner77875d82002-11-21 02:00:20 +0000609 case X86II::AddRegFrm: {
610 // There are currently two forms of acceptable AddRegFrm instructions.
611 // Either the instruction JUST takes a single register (like inc, dec, etc),
612 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000613 // (move immediate f.e.). Note that this immediate value might be stored as
614 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000615 // into a register. The initial register might be duplicated if this is a
616 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000617 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000618 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000619 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000620 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000621 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000622 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000623 MI->getOperand(1).isRegister() ||
624 MI->getOperand(1).isGlobalAddress() ||
625 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000626 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000627
Chris Lattner77875d82002-11-21 02:00:20 +0000628 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000629
Chris Lattnerb009c002004-02-11 19:26:28 +0000630 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000631 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000632 if (MI->getNumOperands() == 2 &&
633 (!MI->getOperand(1).isRegister() ||
634 MI->getOperand(1).getVRegValueOrNull() ||
635 MI->getOperand(1).isGlobalAddress() ||
636 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000637 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000638 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000639 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000640 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000641 O << "\n";
642 return;
643 }
Chris Lattner233ad712002-11-21 01:33:44 +0000644 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000645 // There are three forms of MRMDestReg instructions, those with 2
646 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000647 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000648 // 2 Operands: this is for things like mov that do not read a
649 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000650 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000651 // 2 Operands: two address instructions which def&use the first
652 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000653 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000654 // 3 Operands: in this form, two address instructions are the same
655 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000656 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000657 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000658 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000659 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000660 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000661 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000662
Chris Lattnerb009c002004-02-11 19:26:28 +0000663 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000664 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000665 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000666 printOp(MI->getOperand(1));
667 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000668 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000669 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000670 }
Alkis Evlogimenos519f4e72004-03-09 06:10:15 +0000671 checkImplUses(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000672 O << "\n";
673 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000674 }
Chris Lattner18042332002-11-21 21:03:39 +0000675
676 case X86II::MRMDestMem: {
677 // These instructions are the same as MRMDestReg, but instead of having a
678 // register reference for the mod/rm field, it's a memory reference.
679 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000680 assert(isMem(MI, 0) &&
681 (MI->getNumOperands() == 4+1 ||
682 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
683 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000684
Chris Lattnerb009c002004-02-11 19:26:28 +0000685 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000686 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000687 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000688 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000689 if (MI->getNumOperands() == 4+2) {
690 O << ", ";
691 printOp(MI->getOperand(5));
692 }
Alkis Evlogimenos519f4e72004-03-09 06:10:15 +0000693 checkImplUses(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000694 O << "\n";
695 return;
696 }
697
Chris Lattner233ad712002-11-21 01:33:44 +0000698 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000699 // There are three forms that are acceptable for MRMSrcReg
700 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000701 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000702 // 2 Operands: this is for things like mov that do not read a
703 // second input.
704 //
705 // 2 Operands: in this form, the last register is the ModR/M
706 // input. The first operand is a def&use. This is for things
707 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000708 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000709 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000710 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000711 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000712 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000713 assert(MI->getOperand(0).isRegister() &&
714 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000715 (MI->getNumOperands() == 2 ||
716 (MI->getNumOperands() == 3 &&
717 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000718 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000719
Chris Lattnerb009c002004-02-11 19:26:28 +0000720 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000721 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000722 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000723 printOp(MI->getOperand(1));
724 if (MI->getNumOperands() == 3) {
725 O << ", ";
726 printOp(MI->getOperand(2));
727 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000728 O << "\n";
729 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000730 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000731
Chris Lattner3d3067b2002-11-21 20:44:15 +0000732 case X86II::MRMSrcMem: {
733 // These instructions are the same as MRMSrcReg, but instead of having a
734 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000735 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000736 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000737 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000738(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000739 && "Bad format for MRMSrcMem!");
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 Lattnerb7089442003-01-13 00:35:03 +0000742 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000743 printMemReference(MI, 1);
744 if (MI->getNumOperands() == 2+4) {
745 O << ", ";
746 printOp(MI->getOperand(5));
747 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000748 O << "\n";
749 return;
750 }
751
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000752 case X86II::MRM0r: case X86II::MRM1r:
753 case X86II::MRM2r: case X86II::MRM3r:
754 case X86II::MRM4r: case X86II::MRM5r:
755 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000756 // In this form, the following are valid formats:
757 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000758 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000759 // 2. shl rdest, rinput <implicit CL or 1>
760 // 3. sbb rdest, rinput, immediate [rdest = rinput]
761 //
762 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000763 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000764 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000765 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000766 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000767 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000768 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000769 "Bad MRMSxR format!");
770
Chris Lattnerd9096832002-12-15 08:01:39 +0000771 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000772 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
773 O << "**";
774
Chris Lattnerb009c002004-02-11 19:26:28 +0000775 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000776 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000777 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000778 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000779 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000780 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000781 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000782 O << "\n";
783
784 return;
785 }
786
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000787 case X86II::MRM0m: case X86II::MRM1m:
788 case X86II::MRM2m: case X86II::MRM3m:
789 case X86II::MRM4m: case X86II::MRM5m:
790 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000791 // In this form, the following are valid formats:
792 // 1. sete [m]
793 // 2. cmp [m], immediate
794 // 2. shl [m], rinput <implicit CL or 1>
795 // 3. sbb [m], immediate
796 //
797 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
798 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000799 assert((MI->getNumOperands() != 5 ||
800 (MI->getOperand(4).isImmediate() ||
801 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000802 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000803
804 const MachineOperand &Op3 = MI->getOperand(3);
805
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000806 // gas bugs:
807 //
808 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000809 // is misassembled by gas in intel_syntax mode as its 32-bit
810 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
811 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000812 //
813 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000814 // misassembled by gas in intel_syntax mode as its 32-bit
815 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
816 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000817 //
818 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000819 // invalid opcode, saying "64 bit operations are only supported in
820 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
821 // [...]", which is wrong. Workaround: Output the raw opcode bytes
822 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000823 //
824 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
825 // invalid opcode, saying "64 bit operations are only supported in
826 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
827 // [...]", which is wrong. Workaround: Output the raw opcode bytes
828 // instead of the instruction.
829 if (MI->getOpcode() == X86::FSTP80m ||
830 MI->getOpcode() == X86::FLD80m ||
831 MI->getOpcode() == X86::FILD64m ||
832 MI->getOpcode() == X86::FISTP64m) {
833 GasBugWorkaroundEmitter gwe(O);
834 X86::emitInstruction(gwe, (X86InstrInfo&)TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000835 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000836
Chris Lattnerb009c002004-02-11 19:26:28 +0000837 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000838 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000839 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000840 if (MI->getNumOperands() == 5) {
841 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000842 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000843 }
Alkis Evlogimenos519f4e72004-03-09 06:10:15 +0000844 checkImplUses(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000845 O << "\n";
846 return;
847 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000848 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000849 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000850 }
Chris Lattner72614082002-10-25 22:55:53 +0000851}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000852
Chris Lattner93c1afa2003-08-11 19:35:26 +0000853bool Printer::doInitialization(Module &M) {
854 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000855 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000856 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
857 // instruction as a reference to the register named sp, and if you try to
858 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
859 // before being looked up in the symbol table. This creates spurious
860 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
861 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000862 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000863 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000864 return false; // success
865}
866
Chris Lattnerad200712003-09-09 16:23:36 +0000867// SwitchSection - Switch to the specified section of the executable if we are
868// not already in it!
869//
870static void SwitchSection(std::ostream &OS, std::string &CurSection,
871 const char *NewSection) {
872 if (CurSection != NewSection) {
873 CurSection = NewSection;
874 if (!CurSection.empty())
875 OS << "\t" << NewSection << "\n";
876 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000877}
878
Chris Lattnerad200712003-09-09 16:23:36 +0000879bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000880 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000881 std::string CurSection;
882
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000883 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000884 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
885 if (I->hasInitializer()) { // External global require no code
886 O << "\n\n";
887 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000888 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000889 unsigned Size = TD.getTypeSize(C->getType());
890 unsigned Align = TD.getTypeAlignment(C->getType());
891
892 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000893 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
894 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000895 SwitchSection(O, CurSection, ".data");
896 if (I->hasInternalLinkage())
897 O << "\t.local " << name << "\n";
898
899 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000900 << "," << (unsigned)TD.getTypeAlignment(C->getType());
901 O << "\t\t# ";
902 WriteAsOperand(O, I, true, true, &M);
903 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000904 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000905 switch (I->getLinkage()) {
906 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000907 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000908 // Nonnull linkonce -> weak
909 O << "\t.weak " << name << "\n";
910 SwitchSection(O, CurSection, "");
911 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
912 break;
913
914 case GlobalValue::AppendingLinkage:
915 // FIXME: appending linkage variables should go into a section of
916 // their name or something. For now, just emit them as external.
917 case GlobalValue::ExternalLinkage:
918 // If external or appending, declare as a global symbol
919 O << "\t.globl " << name << "\n";
920 // FALL THROUGH
921 case GlobalValue::InternalLinkage:
922 if (C->isNullValue())
923 SwitchSection(O, CurSection, ".bss");
924 else
925 SwitchSection(O, CurSection, ".data");
926 break;
927 }
928
929 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000930 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000931 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000932 O << name << ":\t\t\t\t# ";
933 WriteAsOperand(O, I, true, true, &M);
934 O << " = ";
935 WriteAsOperand(O, C, false, false, &M);
936 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000937 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000938 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000939 }
Chris Lattnerad200712003-09-09 16:23:36 +0000940
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000941 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000942 return false; // success
943}