blob: 73971bbe7f6b118fb252f9eb144a813091ca7049 [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);
108 void printImplUsesAfter(const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000109 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000110 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000111 bool elideOffsetKeyword = false);
112 void printMemReference(const MachineInstr *MI, unsigned Op);
113 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000114 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000115 bool doInitialization(Module &M);
116 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000117 void emitGlobalConstant(const Constant* CV);
118 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000119 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000120} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000121
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000122/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000123/// assembly code for a MachineFunction to the given output stream,
124/// using the given target machine description. This should work
125/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000126///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000127FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000128 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000129}
130
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000131/// toOctal - Convert the low order bits of X into an octal digit.
132///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000133static inline char toOctal(int X) {
134 return (X&7)+'0';
135}
136
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000137/// getAsCString - Return the specified array as a C compatible
138/// string, only if the predicate isStringCompatible is true.
139///
Chris Lattnerac662d12003-11-03 20:19:49 +0000140static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000141 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000142
Chris Lattnerac662d12003-11-03 20:19:49 +0000143 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000144 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000145 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000146
147 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000148 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000149 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000150 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000151 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000152 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000153 } else {
154 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000155 case '\b': O << "\\b"; break;
156 case '\f': O << "\\f"; break;
157 case '\n': O << "\\n"; break;
158 case '\r': O << "\\r"; break;
159 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000160 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000161 O << '\\';
162 O << toOctal(C >> 6);
163 O << toOctal(C >> 3);
164 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165 break;
166 }
167 }
168 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000169 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000170}
171
Chris Lattnerac662d12003-11-03 20:19:49 +0000172// Print out the specified constant, without a storage class. Only the
173// constants valid in constant expressions can occur here.
174void Printer::emitConstantValueOnly(const Constant *CV) {
175 if (CV->isNullValue())
176 O << "0";
177 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
178 assert(CB == ConstantBool::True);
179 O << "1";
180 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000181 if (((CI->getValue() << 32) >> 32) == CI->getValue())
182 O << CI->getValue();
183 else
184 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000185 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
186 O << CI->getValue();
187 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
188 // This is a constant address for a global variable or function. Use the
189 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000190 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000191 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
192 const TargetData &TD = TM.getTargetData();
193 switch(CE->getOpcode()) {
194 case Instruction::GetElementPtr: {
195 // generate a symbolic expression for the byte address
196 const Constant *ptrVal = CE->getOperand(0);
197 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
198 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
199 O << "(";
200 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000201 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 } else {
203 emitConstantValueOnly(ptrVal);
204 }
205 break;
206 }
207 case Instruction::Cast: {
208 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000209 // that do not involve a change in value. This assertion is really gross,
210 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000211 Constant *Op = CE->getOperand(0);
212 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000213
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000214 // Remember, kids, pointers on x86 can be losslessly converted back and
215 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000216 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000217 && (Ty == Type::LongTy || Ty == Type::ULongTy
218 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000219 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000220 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
221 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000222 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
223 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000224 && "FIXME: Don't yet support this kind of constant cast expr");
225 O << "(";
226 emitConstantValueOnly(Op);
227 O << ")";
228 break;
229 }
230 case Instruction::Add:
231 O << "(";
232 emitConstantValueOnly(CE->getOperand(0));
233 O << ") + (";
234 emitConstantValueOnly(CE->getOperand(1));
235 O << ")";
236 break;
237 default:
238 assert(0 && "Unsupported operator!");
239 }
240 } else {
241 assert(0 && "Unknown constant value!");
242 }
243}
244
245// Print a constant value or values, with the appropriate storage class as a
246// prefix.
247void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000248 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000249
Chris Lattnerad200712003-09-09 16:23:36 +0000250 if (CV->isNullValue()) {
251 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000252 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000253 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000254 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000255 O << "\t.ascii\t";
256 printAsCString(O, CVA);
257 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000258 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000259 const std::vector<Use> &constValues = CVA->getValues();
260 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000261 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000262 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000263 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000264 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
265 // Print the fields in successive locations. Pad to align if needed!
266 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
267 const std::vector<Use>& constValues = CVS->getValues();
268 unsigned sizeSoFar = 0;
269 for (unsigned i=0, N = constValues.size(); i < N; i++) {
270 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000271
Chris Lattnerad200712003-09-09 16:23:36 +0000272 // Check if padding is needed and insert one or more 0s.
273 unsigned fieldSize = TD.getTypeSize(field->getType());
274 unsigned padSize = ((i == N-1? cvsLayout->StructSize
275 : cvsLayout->MemberOffsets[i+1])
276 - cvsLayout->MemberOffsets[i]) - fieldSize;
277 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000278
Chris Lattnerad200712003-09-09 16:23:36 +0000279 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000280 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000281
282 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000283 if (padSize)
284 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000285 }
Chris Lattnerad200712003-09-09 16:23:36 +0000286 assert(sizeSoFar == cvsLayout->StructSize &&
287 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000288 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000289 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
290 // FP Constants are printed as integer constants to avoid losing
291 // precision...
292 double Val = CFP->getValue();
293 switch (CFP->getType()->getPrimitiveID()) {
294 default: assert(0 && "Unknown floating point type!");
295 case Type::FloatTyID: {
296 union FU { // Abide by C TBAA rules
297 float FVal;
298 unsigned UVal;
299 } U;
300 U.FVal = Val;
301 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
302 return;
303 }
304 case Type::DoubleTyID: {
305 union DU { // Abide by C TBAA rules
306 double FVal;
307 uint64_t UVal;
308 } U;
309 U.FVal = Val;
310 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
311 return;
312 }
313 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000314 }
315
316 const Type *type = CV->getType();
317 O << "\t";
318 switch (type->getPrimitiveID()) {
319 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
320 O << ".byte";
321 break;
322 case Type::UShortTyID: case Type::ShortTyID:
323 O << ".word";
324 break;
325 case Type::FloatTyID: case Type::PointerTyID:
326 case Type::UIntTyID: case Type::IntTyID:
327 O << ".long";
328 break;
329 case Type::DoubleTyID:
330 case Type::ULongTyID: case Type::LongTyID:
331 O << ".quad";
332 break;
333 default:
334 assert (0 && "Can't handle printing this type of thing");
335 break;
336 }
337 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000338 emitConstantValueOnly(CV);
339 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000340}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000341
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000342/// printConstantPool - Print to the current output stream assembly
343/// representations of the constants in the constant pool MCP. This is
344/// used to print out constants which have been "spilled to memory" by
345/// the code generator.
346///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000347void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000348 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000349 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000350
Chris Lattnerb7089442003-01-13 00:35:03 +0000351 if (CP.empty()) return;
352
353 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
354 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000355 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000356 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000357 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
358 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000359 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000360 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000361}
362
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000363/// runOnMachineFunction - This uses the printMachineInstruction()
364/// method to print assembly for each instruction.
365///
Chris Lattner0285a332002-12-28 20:25:38 +0000366bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000367 // BBNumber is used here so that a given Printer will never give two
368 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000369 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000370
Chris Lattnere0121322003-08-03 23:37:09 +0000371 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000372 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000373 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000374
Chris Lattnerb7089442003-01-13 00:35:03 +0000375 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000376 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000377
Brian Gaeke6559bb92002-11-14 22:32:30 +0000378 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 O << "\t.text\n";
380 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000381 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000382 if (!EmitCygwin)
383 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000384 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000385
Brian Gaeked7908f62003-06-27 00:00:48 +0000386 // Number each basic block so that we can consistently refer to them
387 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000388 NumberForBB.clear();
389 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
390 I != E; ++I) {
391 NumberForBB[I->getBasicBlock()] = BBNumber++;
392 }
393
Brian Gaeke6559bb92002-11-14 22:32:30 +0000394 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000395 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
396 I != E; ++I) {
397 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000398 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000399 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000400 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
401 II != E; ++II) {
402 // Print the assembly for the instruction.
403 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000404 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405 }
Chris Lattner0285a332002-12-28 20:25:38 +0000406 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000407
408 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000409 return false;
410}
411
Chris Lattner3d3067b2002-11-21 20:44:15 +0000412static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000413 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000414 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
415 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000416}
417
418static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000419 if (MI->getOperand(Op).isFrameIndex()) return true;
420 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000421 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000422 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
423 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000424}
425
Brian Gaeke2a098772003-08-11 19:05:46 +0000426
427
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000428void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000429 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000430 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000431 switch (MO.getType()) {
432 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000433 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000434 O << "<" << V->getName() << ">";
435 return;
436 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000438 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000439 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000440 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000441 O << "%" << RI.get(MO.getReg()).Name;
442 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000443 O << "%reg" << MO.getReg();
444 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000445
446 case MachineOperand::MO_SignExtendedImmed:
447 case MachineOperand::MO_UnextendedImmed:
448 O << (int)MO.getImmedValue();
449 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000450 case MachineOperand::MO_PCRelativeDisp: {
451 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
452 assert (i != NumberForBB.end()
453 && "Could not find a BB in the NumberForBB map!");
454 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000455 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000456 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000457 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000458 if (!elideOffsetKeyword)
459 O << "OFFSET ";
460 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000461 return;
462 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000463 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000464 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000465 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000466 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000467 }
468}
469
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000470static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
471 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000472 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000473 case X86II::Mem8: return "BYTE PTR";
474 case X86II::Mem16: return "WORD PTR";
475 case X86II::Mem32: return "DWORD PTR";
476 case X86II::Mem64: return "QWORD PTR";
477 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000478 }
479}
480
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000481void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000482 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000483
484 if (MI->getOperand(Op).isFrameIndex()) {
485 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
486 if (MI->getOperand(Op+3).getImmedValue())
487 O << " + " << MI->getOperand(Op+3).getImmedValue();
488 O << "]";
489 return;
490 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000491 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000492 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000493 if (MI->getOperand(Op+3).getImmedValue())
494 O << " + " << MI->getOperand(Op+3).getImmedValue();
495 O << "]";
496 return;
497 }
498
Chris Lattner3d3067b2002-11-21 20:44:15 +0000499 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000500 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000501 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000502 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000503
504 O << "[";
505 bool NeedPlus = false;
506 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000507 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000508 NeedPlus = true;
509 }
510
511 if (IndexReg.getReg()) {
512 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000513 if (ScaleVal != 1)
514 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000515 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000516 NeedPlus = true;
517 }
518
Chris Lattner0285a332002-12-28 20:25:38 +0000519 if (DispVal) {
520 if (NeedPlus)
521 if (DispVal > 0)
522 O << " + ";
523 else {
524 O << " - ";
525 DispVal = -DispVal;
526 }
527 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000528 }
529 O << "]";
530}
531
Chris Lattner30b2f722004-03-31 22:02:21 +0000532
533/// printImplUsesBefore - Emit the implicit-use registers for the instruction
534/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000535///
Chris Lattner30b2f722004-03-31 22:02:21 +0000536void Printer::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000537 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000538 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
539 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
540 // Bug Workaround: See note in Printer::doInitialization about %.
541 O << "%" << RI.get(*p).Name << ", ";
542 }
543 }
544}
545
546/// printImplUsesAfter - Emit the implicit-use registers for the instruction
547/// described by DESC, if its PrintImplUsesAfter flag is set.
548///
549void Printer::printImplUsesAfter(const TargetInstrDescriptor &Desc) {
550 const MRegisterInfo &RI = *TM.getRegisterInfo();
551 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000552 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
553 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000554 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000555 }
556 }
557}
558
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000559/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000560/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000561///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000562void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000563 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000564 const TargetInstrInfo &TII = TM.getInstrInfo();
565 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000566
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000567 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000568 switch (Desc.TSFlags & X86II::FormMask) {
569 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000570 // Print pseudo-instructions as comments; either they should have been
571 // turned into real instructions by now, or they don't need to be
572 // seen by the assembler (e.g., IMPLICIT_USEs.)
573 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000574 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000575 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000576 O << " = phi ";
577 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
578 if (i != 1) O << ", ";
579 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000580 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000581 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000582 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000583 O << "]";
584 }
585 } else {
586 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000587 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000588 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000589 O << " = ";
590 ++i;
591 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000592 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000593
594 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
595 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000596 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000597 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000598 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000599 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000600 }
601 O << "\n";
602 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000603
Chris Lattnerf9f60882002-11-18 06:56:51 +0000604 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000605 // The accepted forms of Raw instructions are:
606 // 1. nop - No operand required
607 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000608 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000609 //
610 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000611 (MI->getNumOperands() == 1 &&
612 (MI->getOperand(0).isPCRelativeDisp() ||
613 MI->getOperand(0).isGlobalAddress() ||
614 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000615 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000616 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000617
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000618 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000619 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000620 }
621 O << "\n";
622 return;
623
Chris Lattner77875d82002-11-21 02:00:20 +0000624 case X86II::AddRegFrm: {
625 // There are currently two forms of acceptable AddRegFrm instructions.
626 // Either the instruction JUST takes a single register (like inc, dec, etc),
627 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000628 // (move immediate f.e.). Note that this immediate value might be stored as
629 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000630 // into a register. The initial register might be duplicated if this is a
631 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000632 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000633 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000634 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000635 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000636 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000637 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000638 MI->getOperand(1).isRegister() ||
639 MI->getOperand(1).isGlobalAddress() ||
640 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000641 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642
Chris Lattner77875d82002-11-21 02:00:20 +0000643 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000644
Chris Lattnerb009c002004-02-11 19:26:28 +0000645 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000646
647 printImplUsesBefore(Desc); // fcmov*
648
Brian Gaekede420ae2003-07-23 20:25:08 +0000649 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000650 if (MI->getNumOperands() == 2 &&
651 (!MI->getOperand(1).isRegister() ||
652 MI->getOperand(1).getVRegValueOrNull() ||
653 MI->getOperand(1).isGlobalAddress() ||
654 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000655 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000656 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000657 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000658 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000659 O << "\n";
660 return;
661 }
Chris Lattner233ad712002-11-21 01:33:44 +0000662 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000663 // There are three forms of MRMDestReg instructions, those with 2
664 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000665 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000666 // 2 Operands: this is for things like mov that do not read a
667 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000668 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000669 // 2 Operands: two address instructions which def&use the first
670 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000671 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000672 // 3 Operands: in this form, two address instructions are the same
673 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000674 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000675 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000676 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000678 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000679 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000680
Chris Lattnerb009c002004-02-11 19:26:28 +0000681 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000682 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000683 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000684 printOp(MI->getOperand(1));
685 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000686 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000687 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000688 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000689 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000690 O << "\n";
691 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000692 }
Chris Lattner18042332002-11-21 21:03:39 +0000693
694 case X86II::MRMDestMem: {
695 // These instructions are the same as MRMDestReg, but instead of having a
696 // register reference for the mod/rm field, it's a memory reference.
697 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000698 assert(isMem(MI, 0) &&
699 (MI->getNumOperands() == 4+1 ||
700 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
701 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000702
Chris Lattnerb009c002004-02-11 19:26:28 +0000703 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000704 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000705 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000706 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000707 if (MI->getNumOperands() == 4+2) {
708 O << ", ";
709 printOp(MI->getOperand(5));
710 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000711 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000712 O << "\n";
713 return;
714 }
715
Chris Lattner233ad712002-11-21 01:33:44 +0000716 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000717 // There are three forms that are acceptable for MRMSrcReg
718 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000719 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000720 // 2 Operands: this is for things like mov that do not read a
721 // second input.
722 //
723 // 2 Operands: in this form, the last register is the ModR/M
724 // input. The first operand is a def&use. This is for things
725 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000726 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000727 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000728 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000729 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000730 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000731 assert(MI->getOperand(0).isRegister() &&
732 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000733 (MI->getNumOperands() == 2 ||
734 (MI->getNumOperands() == 3 &&
735 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000736 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000737
Chris Lattnerb009c002004-02-11 19:26:28 +0000738 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000739 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000740 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000741 printOp(MI->getOperand(1));
742 if (MI->getNumOperands() == 3) {
743 O << ", ";
744 printOp(MI->getOperand(2));
745 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000746 O << "\n";
747 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000748 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000749
Chris Lattner3d3067b2002-11-21 20:44:15 +0000750 case X86II::MRMSrcMem: {
751 // These instructions are the same as MRMSrcReg, but instead of having a
752 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000753 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000754 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000755 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattner5b672522004-02-17 07:40:44 +0000756(MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() && isMem(MI, 1))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000757 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000758 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000759 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000760 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000761 printMemReference(MI, 1);
762 if (MI->getNumOperands() == 2+4) {
763 O << ", ";
764 printOp(MI->getOperand(5));
765 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000766 O << "\n";
767 return;
768 }
769
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000770 case X86II::MRM0r: case X86II::MRM1r:
771 case X86II::MRM2r: case X86II::MRM3r:
772 case X86II::MRM4r: case X86II::MRM5r:
773 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000774 // In this form, the following are valid formats:
775 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000776 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000777 // 2. shl rdest, rinput <implicit CL or 1>
778 // 3. sbb rdest, rinput, immediate [rdest = rinput]
779 //
780 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000781 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000782 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000783 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000784 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000785 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000786 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000787 "Bad MRMSxR format!");
788
Chris Lattnerd9096832002-12-15 08:01:39 +0000789 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000790 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
791 O << "**";
792
Chris Lattnerb009c002004-02-11 19:26:28 +0000793 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000794 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000795 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000796 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000797 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000798 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000799 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000800 O << "\n";
801
802 return;
803 }
804
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000805 case X86II::MRM0m: case X86II::MRM1m:
806 case X86II::MRM2m: case X86II::MRM3m:
807 case X86II::MRM4m: case X86II::MRM5m:
808 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000809 // In this form, the following are valid formats:
810 // 1. sete [m]
811 // 2. cmp [m], immediate
812 // 2. shl [m], rinput <implicit CL or 1>
813 // 3. sbb [m], immediate
814 //
815 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
816 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000817 assert((MI->getNumOperands() != 5 ||
818 (MI->getOperand(4).isImmediate() ||
819 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000820 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000821
822 const MachineOperand &Op3 = MI->getOperand(3);
823
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000824 // gas bugs:
825 //
826 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000827 // is misassembled by gas in intel_syntax mode as its 32-bit
828 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
829 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000830 //
831 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000832 // misassembled by gas in intel_syntax mode as its 32-bit
833 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
834 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000835 //
836 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000837 // invalid opcode, saying "64 bit operations are only supported in
838 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
839 // [...]", which is wrong. Workaround: Output the raw opcode bytes
840 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000841 //
842 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
843 // invalid opcode, saying "64 bit operations are only supported in
844 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
845 // [...]", which is wrong. Workaround: Output the raw opcode bytes
846 // instead of the instruction.
847 if (MI->getOpcode() == X86::FSTP80m ||
848 MI->getOpcode() == X86::FLD80m ||
849 MI->getOpcode() == X86::FILD64m ||
850 MI->getOpcode() == X86::FISTP64m) {
851 GasBugWorkaroundEmitter gwe(O);
852 X86::emitInstruction(gwe, (X86InstrInfo&)TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000853 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000854
Chris Lattnerb009c002004-02-11 19:26:28 +0000855 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000856 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000857 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000858 if (MI->getNumOperands() == 5) {
859 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000860 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000861 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000862 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000863 O << "\n";
864 return;
865 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000866 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000867 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000868 }
Chris Lattner72614082002-10-25 22:55:53 +0000869}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000870
Chris Lattner93c1afa2003-08-11 19:35:26 +0000871bool Printer::doInitialization(Module &M) {
872 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000873 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000874 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
875 // instruction as a reference to the register named sp, and if you try to
876 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
877 // before being looked up in the symbol table. This creates spurious
878 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
879 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000880 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000881 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000882 return false; // success
883}
884
Chris Lattnerad200712003-09-09 16:23:36 +0000885// SwitchSection - Switch to the specified section of the executable if we are
886// not already in it!
887//
888static void SwitchSection(std::ostream &OS, std::string &CurSection,
889 const char *NewSection) {
890 if (CurSection != NewSection) {
891 CurSection = NewSection;
892 if (!CurSection.empty())
893 OS << "\t" << NewSection << "\n";
894 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000895}
896
Chris Lattnerad200712003-09-09 16:23:36 +0000897bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000898 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000899 std::string CurSection;
900
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000901 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000902 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
903 if (I->hasInitializer()) { // External global require no code
904 O << "\n\n";
905 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000906 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000907 unsigned Size = TD.getTypeSize(C->getType());
908 unsigned Align = TD.getTypeAlignment(C->getType());
909
910 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000911 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
912 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000913 SwitchSection(O, CurSection, ".data");
914 if (I->hasInternalLinkage())
915 O << "\t.local " << name << "\n";
916
917 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000918 << "," << (unsigned)TD.getTypeAlignment(C->getType());
919 O << "\t\t# ";
920 WriteAsOperand(O, I, true, true, &M);
921 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000922 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000923 switch (I->getLinkage()) {
924 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000925 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000926 // Nonnull linkonce -> weak
927 O << "\t.weak " << name << "\n";
928 SwitchSection(O, CurSection, "");
929 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
930 break;
931
932 case GlobalValue::AppendingLinkage:
933 // FIXME: appending linkage variables should go into a section of
934 // their name or something. For now, just emit them as external.
935 case GlobalValue::ExternalLinkage:
936 // If external or appending, declare as a global symbol
937 O << "\t.globl " << name << "\n";
938 // FALL THROUGH
939 case GlobalValue::InternalLinkage:
940 if (C->isNullValue())
941 SwitchSection(O, CurSection, ".bss");
942 else
943 SwitchSection(O, CurSection, ".data");
944 break;
945 }
946
947 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000948 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000949 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000950 O << name << ":\t\t\t\t# ";
951 WriteAsOperand(O, I, true, true, &M);
952 O << " = ";
953 WriteAsOperand(O, C, false, false, &M);
954 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000955 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000956 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000957 }
Chris Lattnerad200712003-09-09 16:23:36 +0000958
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000959 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000960 return false; // success
961}