blob: 840aa4a9415e7600408023a8c0eb810acbc27205 [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//
Brian Gaeke92bdfe62003-07-23 18:37:06 +000010// This file contains a printer that converts from our internal
11// representation of machine-dependent LLVM code to Intel-format
12// assembly language. This printer is the output mechanism used
Chris Lattner93c1afa2003-08-11 19:35:26 +000013// by `llc' and `lli -print-machineinstrs' 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"
Chris Lattnere0121322003-08-03 23:37:09 +000019#include "llvm/Constants.h"
20#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000021#include "llvm/Module.h"
22#include "llvm/Assembly/Writer.h"
Chris Lattner0285a332002-12-28 20:25:38 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000025#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000026#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000027#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000028#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000029#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000030#include "Support/CommandLine.h"
Chris Lattner72614082002-10-25 22:55:53 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000034namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000035 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36
Chris Lattner93c1afa2003-08-11 19:35:26 +000037 // FIXME: This should be automatically picked up by autoconf from the C
38 // frontend
39 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
40 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
41
Chris Lattner0285a332002-12-28 20:25:38 +000042 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000043 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000044 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000045 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000046
47 /// Target machine description which we query for reg. names, data
48 /// layout, etc.
49 ///
50 TargetMachine &TM;
51
Brian Gaeked9fb37a2003-07-24 20:20:44 +000052 /// Name-mangler for global names.
53 ///
54 Mangler *Mang;
55
Brian Gaekede420ae2003-07-23 20:25:08 +000056 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000057
58 /// We name each basic block in a Function with a unique number, so
59 /// that we can consistently refer to them later. This is cleared
60 /// at the beginning of each call to runOnMachineFunction().
61 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000062 typedef std::map<const Value *, unsigned> ValueMapTy;
63 ValueMapTy NumberForBB;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000064
65 /// Cache of mangled name for current function. This is
66 /// recalculated at the beginning of each call to
67 /// runOnMachineFunction().
68 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000069 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000070
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000071 virtual const char *getPassName() const {
72 return "X86 Assembly Printer";
73 }
74
Brian Gaeke2a098772003-08-11 19:05:46 +000075 void checkImplUses (const TargetInstrDescriptor &Desc);
Brian Gaeked9fb37a2003-07-24 20:20:44 +000076 void printMachineInstruction(const MachineInstr *MI);
Brian Gaeke92bdfe62003-07-23 18:37:06 +000077 void printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +000078 bool elideOffsetKeyword = false);
79 void printMemReference(const MachineInstr *MI, unsigned Op);
80 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +000081 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +000082 bool doInitialization(Module &M);
83 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +000084 void emitGlobalConstant(const Constant* CV);
85 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000086 };
Brian Gaeked7908f62003-06-27 00:00:48 +000087} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000088
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +000090/// assembly code for a MachineFunction to the given output stream,
91/// using the given target machine description. This should work
92/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +000093///
Brian Gaeke9d99b432003-08-13 18:15:15 +000094FunctionPass *createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +000095 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +000096}
97
Brian Gaeke92bdfe62003-07-23 18:37:06 +000098/// toOctal - Convert the low order bits of X into an octal digit.
99///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000100static inline char toOctal(int X) {
101 return (X&7)+'0';
102}
103
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000104/// getAsCString - Return the specified array as a C compatible
105/// string, only if the predicate isStringCompatible is true.
106///
Chris Lattnerac662d12003-11-03 20:19:49 +0000107static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000108 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000109
Chris Lattnerac662d12003-11-03 20:19:49 +0000110 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000111 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000112 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000113
114 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000115 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000116 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000117 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000118 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000119 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000120 } else {
121 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000122 case '\b': O << "\\b"; break;
123 case '\f': O << "\\f"; break;
124 case '\n': O << "\\n"; break;
125 case '\r': O << "\\r"; break;
126 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000127 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000128 O << '\\';
129 O << toOctal(C >> 6);
130 O << toOctal(C >> 3);
131 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000132 break;
133 }
134 }
135 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000136 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000137}
138
Chris Lattnerac662d12003-11-03 20:19:49 +0000139// Print out the specified constant, without a storage class. Only the
140// constants valid in constant expressions can occur here.
141void Printer::emitConstantValueOnly(const Constant *CV) {
142 if (CV->isNullValue())
143 O << "0";
144 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
145 assert(CB == ConstantBool::True);
146 O << "1";
147 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
148 O << CI->getValue();
149 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
150 O << CI->getValue();
151 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
152 // This is a constant address for a global variable or function. Use the
153 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000154 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000155 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
156 const TargetData &TD = TM.getTargetData();
157 switch(CE->getOpcode()) {
158 case Instruction::GetElementPtr: {
159 // generate a symbolic expression for the byte address
160 const Constant *ptrVal = CE->getOperand(0);
161 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
162 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
163 O << "(";
164 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000165 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000166 } else {
167 emitConstantValueOnly(ptrVal);
168 }
169 break;
170 }
171 case Instruction::Cast: {
172 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000173 // that do not involve a change in value. This assertion is really gross,
174 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000175 Constant *Op = CE->getOperand(0);
176 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000177
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000178 // Remember, kids, pointers on x86 can be losslessly converted back and
179 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000180 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000181 && (Ty == Type::LongTy || Ty == Type::ULongTy
182 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000183 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000184 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
185 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000186 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
187 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000188 && "FIXME: Don't yet support this kind of constant cast expr");
189 O << "(";
190 emitConstantValueOnly(Op);
191 O << ")";
192 break;
193 }
194 case Instruction::Add:
195 O << "(";
196 emitConstantValueOnly(CE->getOperand(0));
197 O << ") + (";
198 emitConstantValueOnly(CE->getOperand(1));
199 O << ")";
200 break;
201 default:
202 assert(0 && "Unsupported operator!");
203 }
204 } else {
205 assert(0 && "Unknown constant value!");
206 }
207}
208
209// Print a constant value or values, with the appropriate storage class as a
210// prefix.
211void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000212 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000213
Chris Lattnerad200712003-09-09 16:23:36 +0000214 if (CV->isNullValue()) {
215 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000216 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000217 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000218 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000219 O << "\t.ascii\t";
220 printAsCString(O, CVA);
221 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000222 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000223 const std::vector<Use> &constValues = CVA->getValues();
224 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000225 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000226 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000227 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000228 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
229 // Print the fields in successive locations. Pad to align if needed!
230 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
231 const std::vector<Use>& constValues = CVS->getValues();
232 unsigned sizeSoFar = 0;
233 for (unsigned i=0, N = constValues.size(); i < N; i++) {
234 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000235
Chris Lattnerad200712003-09-09 16:23:36 +0000236 // Check if padding is needed and insert one or more 0s.
237 unsigned fieldSize = TD.getTypeSize(field->getType());
238 unsigned padSize = ((i == N-1? cvsLayout->StructSize
239 : cvsLayout->MemberOffsets[i+1])
240 - cvsLayout->MemberOffsets[i]) - fieldSize;
241 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000242
Chris Lattnerad200712003-09-09 16:23:36 +0000243 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000244 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000245
246 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000247 if (padSize)
248 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000249 }
Chris Lattnerad200712003-09-09 16:23:36 +0000250 assert(sizeSoFar == cvsLayout->StructSize &&
251 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000252 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000253 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
254 // FP Constants are printed as integer constants to avoid losing
255 // precision...
256 double Val = CFP->getValue();
257 switch (CFP->getType()->getPrimitiveID()) {
258 default: assert(0 && "Unknown floating point type!");
259 case Type::FloatTyID: {
260 union FU { // Abide by C TBAA rules
261 float FVal;
262 unsigned UVal;
263 } U;
264 U.FVal = Val;
265 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
266 return;
267 }
268 case Type::DoubleTyID: {
269 union DU { // Abide by C TBAA rules
270 double FVal;
271 uint64_t UVal;
272 } U;
273 U.FVal = Val;
274 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
275 return;
276 }
277 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000278 }
279
280 const Type *type = CV->getType();
281 O << "\t";
282 switch (type->getPrimitiveID()) {
283 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
284 O << ".byte";
285 break;
286 case Type::UShortTyID: case Type::ShortTyID:
287 O << ".word";
288 break;
289 case Type::FloatTyID: case Type::PointerTyID:
290 case Type::UIntTyID: case Type::IntTyID:
291 O << ".long";
292 break;
293 case Type::DoubleTyID:
294 case Type::ULongTyID: case Type::LongTyID:
295 O << ".quad";
296 break;
297 default:
298 assert (0 && "Can't handle printing this type of thing");
299 break;
300 }
301 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000302 emitConstantValueOnly(CV);
303 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000304}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000305
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000306/// printConstantPool - Print to the current output stream assembly
307/// representations of the constants in the constant pool MCP. This is
308/// used to print out constants which have been "spilled to memory" by
309/// the code generator.
310///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000311void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000312 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000313 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000314
Chris Lattnerb7089442003-01-13 00:35:03 +0000315 if (CP.empty()) return;
316
317 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
318 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000319 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000320 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000321 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
322 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000323 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000324 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000325}
326
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000327/// runOnMachineFunction - This uses the printMachineInstruction()
328/// method to print assembly for each instruction.
329///
Chris Lattner0285a332002-12-28 20:25:38 +0000330bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000331 // BBNumber is used here so that a given Printer will never give two
332 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000333 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000334
Chris Lattnere0121322003-08-03 23:37:09 +0000335 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000336 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000337 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000338
Chris Lattnerb7089442003-01-13 00:35:03 +0000339 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000340 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000341
Brian Gaeke6559bb92002-11-14 22:32:30 +0000342 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000343 O << "\t.text\n";
344 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000345 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000346 if (!EmitCygwin)
347 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000348 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000349
Brian Gaeked7908f62003-06-27 00:00:48 +0000350 // Number each basic block so that we can consistently refer to them
351 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000352 NumberForBB.clear();
353 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
354 I != E; ++I) {
355 NumberForBB[I->getBasicBlock()] = BBNumber++;
356 }
357
Brian Gaeke6559bb92002-11-14 22:32:30 +0000358 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000359 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
360 I != E; ++I) {
361 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000362 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000363 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000364 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
365 II != E; ++II) {
366 // Print the assembly for the instruction.
367 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000368 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000369 }
Chris Lattner0285a332002-12-28 20:25:38 +0000370 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000371
372 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000373 return false;
374}
375
Chris Lattner3d3067b2002-11-21 20:44:15 +0000376static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000377 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000378 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
379 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000380}
381
382static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000383 if (MI->getOperand(Op).isFrameIndex()) return true;
384 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000385 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000386 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
387 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000388}
389
Brian Gaeke2a098772003-08-11 19:05:46 +0000390
391
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000392void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000393 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000394 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000395 switch (MO.getType()) {
396 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000397 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000398 O << "<" << V->getName() << ">";
399 return;
400 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000401 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000402 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000403 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000404 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000405 O << "%" << RI.get(MO.getReg()).Name;
406 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000407 O << "%reg" << MO.getReg();
408 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000409
410 case MachineOperand::MO_SignExtendedImmed:
411 case MachineOperand::MO_UnextendedImmed:
412 O << (int)MO.getImmedValue();
413 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000414 case MachineOperand::MO_PCRelativeDisp: {
415 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
416 assert (i != NumberForBB.end()
417 && "Could not find a BB in the NumberForBB map!");
418 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000419 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000420 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000421 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000422 if (!elideOffsetKeyword)
423 O << "OFFSET ";
424 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000425 return;
426 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000427 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000428 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000429 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000430 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000431 }
432}
433
Chris Lattner3501fea2003-01-14 22:00:31 +0000434static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000435 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000436 default: assert(0 && "Unknown arg size!");
437 case X86II::Arg8: return "BYTE PTR";
438 case X86II::Arg16: return "WORD PTR";
439 case X86II::Arg32: return "DWORD PTR";
440 case X86II::Arg64: return "QWORD PTR";
441 case X86II::ArgF32: return "DWORD PTR";
442 case X86II::ArgF64: return "QWORD PTR";
443 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000444 }
445}
446
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000447void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000448 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000449
450 if (MI->getOperand(Op).isFrameIndex()) {
451 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
452 if (MI->getOperand(Op+3).getImmedValue())
453 O << " + " << MI->getOperand(Op+3).getImmedValue();
454 O << "]";
455 return;
456 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000457 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000458 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000459 if (MI->getOperand(Op+3).getImmedValue())
460 O << " + " << MI->getOperand(Op+3).getImmedValue();
461 O << "]";
462 return;
463 }
464
Chris Lattner3d3067b2002-11-21 20:44:15 +0000465 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000466 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000467 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000468 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000469
470 O << "[";
471 bool NeedPlus = false;
472 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000473 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000474 NeedPlus = true;
475 }
476
477 if (IndexReg.getReg()) {
478 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000479 if (ScaleVal != 1)
480 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000481 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000482 NeedPlus = true;
483 }
484
Chris Lattner0285a332002-12-28 20:25:38 +0000485 if (DispVal) {
486 if (NeedPlus)
487 if (DispVal > 0)
488 O << " + ";
489 else {
490 O << " - ";
491 DispVal = -DispVal;
492 }
493 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000494 }
495 O << "]";
496}
497
Brian Gaeke2a098772003-08-11 19:05:46 +0000498/// checkImplUses - Emit the implicit-use registers for the
499/// instruction described by DESC, if its PrintImplUses flag is set.
500///
501void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
502 const MRegisterInfo &RI = *TM.getRegisterInfo();
503 if (Desc.TSFlags & X86II::PrintImplUses) {
504 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
505 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000506 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000507 }
508 }
509}
510
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000511/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000512/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000513///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000514void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000515 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000516 const TargetInstrInfo &TII = TM.getInstrInfo();
517 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000518
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000519 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000520 switch (Desc.TSFlags & X86II::FormMask) {
521 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000522 // Print pseudo-instructions as comments; either they should have been
523 // turned into real instructions by now, or they don't need to be
524 // seen by the assembler (e.g., IMPLICIT_USEs.)
525 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000526 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000527 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000528 O << " = phi ";
529 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
530 if (i != 1) O << ", ";
531 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000532 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000533 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000534 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000535 O << "]";
536 }
537 } else {
538 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000539 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000540 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000541 O << " = ";
542 ++i;
543 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000544 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000545
546 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
547 O << " ";
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000548 if (MI->getOperand(i).isDef()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000549 printOp(MI->getOperand(i));
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000550 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000551 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000552 }
553 O << "\n";
554 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000555
Chris Lattnerf9f60882002-11-18 06:56:51 +0000556 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000557 // The accepted forms of Raw instructions are:
558 // 1. nop - No operand required
559 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000560 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000561 //
562 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000563 (MI->getNumOperands() == 1 &&
564 (MI->getOperand(0).isPCRelativeDisp() ||
565 MI->getOperand(0).isGlobalAddress() ||
566 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000567 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000568 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000569
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000570 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000571 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000572 }
573 O << "\n";
574 return;
575
Chris Lattner77875d82002-11-21 02:00:20 +0000576 case X86II::AddRegFrm: {
577 // There are currently two forms of acceptable AddRegFrm instructions.
578 // Either the instruction JUST takes a single register (like inc, dec, etc),
579 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000580 // (move immediate f.e.). Note that this immediate value might be stored as
581 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000582 // into a register. The initial register might be duplicated if this is a
583 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000584 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000585 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000586 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000587 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000588 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000589 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000590 MI->getOperand(1).isRegister() ||
591 MI->getOperand(1).isGlobalAddress() ||
592 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000593 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000594
Chris Lattner77875d82002-11-21 02:00:20 +0000595 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000596
Chris Lattnerb009c002004-02-11 19:26:28 +0000597 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000598 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000599 if (MI->getNumOperands() == 2 &&
600 (!MI->getOperand(1).isRegister() ||
601 MI->getOperand(1).getVRegValueOrNull() ||
602 MI->getOperand(1).isGlobalAddress() ||
603 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000604 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000605 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000606 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000607 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000608 O << "\n";
609 return;
610 }
Chris Lattner233ad712002-11-21 01:33:44 +0000611 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000612 // There are three forms of MRMDestReg instructions, those with 2
613 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000614 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000615 // 2 Operands: this is for things like mov that do not read a
616 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000617 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000618 // 2 Operands: two address instructions which def&use the first
619 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000620 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000621 // 3 Operands: in this form, two address instructions are the same
622 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000623 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000624 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000625 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000626 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000627 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000628 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +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 Lattnerf9f60882002-11-18 06:56:51 +0000632 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000633 printOp(MI->getOperand(1));
634 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000635 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000636 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000637 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000638 O << "\n";
639 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000640 }
Chris Lattner18042332002-11-21 21:03:39 +0000641
642 case X86II::MRMDestMem: {
643 // These instructions are the same as MRMDestReg, but instead of having a
644 // register reference for the mod/rm field, it's a memory reference.
645 //
646 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000647 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000648
Chris Lattnerb009c002004-02-11 19:26:28 +0000649 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000650 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000651 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000652 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000653 O << "\n";
654 return;
655 }
656
Chris Lattner233ad712002-11-21 01:33:44 +0000657 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000658 // There are three forms that are acceptable for MRMSrcReg
659 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000660 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000661 // 2 Operands: this is for things like mov that do not read a
662 // second input.
663 //
664 // 2 Operands: in this form, the last register is the ModR/M
665 // input. The first operand is a def&use. This is for things
666 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000667 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000668 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
669 // for instructions like the IMULri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000670 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000671 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000672 assert(MI->getOperand(0).isRegister() &&
673 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000674 (MI->getNumOperands() == 2 ||
675 (MI->getNumOperands() == 3 &&
676 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000677 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000678
Chris Lattnerb009c002004-02-11 19:26:28 +0000679 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000680 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000681 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000682 printOp(MI->getOperand(1));
683 if (MI->getNumOperands() == 3) {
684 O << ", ";
685 printOp(MI->getOperand(2));
686 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000687 O << "\n";
688 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000689 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000690
Chris Lattner3d3067b2002-11-21 20:44:15 +0000691 case X86II::MRMSrcMem: {
692 // These instructions are the same as MRMSrcReg, but instead of having a
693 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000694 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000695 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000696 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000697 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000698 isMem(MI, 2))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000699 && "Bad format for MRMSrcMem!");
Chris Lattner3d3067b2002-11-21 20:44:15 +0000700 if (MI->getNumOperands() == 2+4 &&
701 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
702 O << "**";
703
Chris Lattnerb009c002004-02-11 19:26:28 +0000704 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000705 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000706 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000707 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000708 O << "\n";
709 return;
710 }
711
Chris Lattner675dd2c2002-11-21 17:09:01 +0000712 case X86II::MRMS0r: case X86II::MRMS1r:
713 case X86II::MRMS2r: case X86II::MRMS3r:
714 case X86II::MRMS4r: case X86II::MRMS5r:
715 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000716 // In this form, the following are valid formats:
717 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000718 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000719 // 2. shl rdest, rinput <implicit CL or 1>
720 // 3. sbb rdest, rinput, immediate [rdest = rinput]
721 //
722 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000723 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000724 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000725 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000726 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000727 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000728 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000729 "Bad MRMSxR format!");
730
Chris Lattnerd9096832002-12-15 08:01:39 +0000731 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000732 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
733 O << "**";
734
Chris Lattnerb009c002004-02-11 19:26:28 +0000735 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000736 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000737 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000738 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000739 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000740 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000741 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000742 O << "\n";
743
744 return;
745 }
746
Chris Lattnerb7089442003-01-13 00:35:03 +0000747 case X86II::MRMS0m: case X86II::MRMS1m:
748 case X86II::MRMS2m: case X86II::MRMS3m:
749 case X86II::MRMS4m: case X86II::MRMS5m:
750 case X86II::MRMS6m: case X86II::MRMS7m: {
751 // In this form, the following are valid formats:
752 // 1. sete [m]
753 // 2. cmp [m], immediate
754 // 2. shl [m], rinput <implicit CL or 1>
755 // 3. sbb [m], immediate
756 //
757 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
758 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000759 assert((MI->getNumOperands() != 5 ||
760 (MI->getOperand(4).isImmediate() ||
761 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000762 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000763
764 const MachineOperand &Op3 = MI->getOperand(3);
765
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000766 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
767 // is misassembled by gas in intel_syntax mode as its 32-bit
768 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
769 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000770 if (MI->getOpcode() == X86::FSTPr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000771 if ((MI->getOperand(0).getReg() == X86::ESP)
772 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000773 if (Op3.isImmediate() &&
774 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
775 // 1 byte disp.
776 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
777 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
778 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000779 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000780 O << ".long ";
781 printOp(Op3);
782 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000783 }
784 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000785 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000786
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000787 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
788 // misassembled by gas in intel_syntax mode as its 32-bit
789 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
790 // opcode bytes instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000791 if (MI->getOpcode() == X86::FLDr80 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000792 MI->getOperand(0).getReg() == X86::ESP &&
793 MI->getOperand(1).getImmedValue() == 1) {
794 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
795 Op3.getImmedValue() <= 127) { // 1 byte displacement
796 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
797 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
798 } else {
799 O << ".byte 0xdb, 0xac, 0x24\n\t";
800 O << ".long ";
801 printOp(Op3);
802 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000803 }
804 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000805
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000806 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
807 // invalid opcode, saying "64 bit operations are only supported in
808 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
809 // [...]", which is wrong. Workaround: Output the raw opcode bytes
810 // instead of the instruction.
Chris Lattnerb009c002004-02-11 19:26:28 +0000811 if (MI->getOpcode() == X86::FILDr64 &&
Chris Lattnerf2d29252003-12-01 05:13:56 +0000812 MI->getOperand(0).getReg() == X86::ESP &&
813 MI->getOperand(1).getImmedValue() == 1) {
814 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
815 Op3.getImmedValue() <= 127) { // 1 byte displacement
816 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
817 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
818 } else {
819 O << ".byte 0xdf, 0xac, 0x24\n\t";
820 O << ".long ";
821 printOp(Op3);
822 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000823 }
824 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000825
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000826 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
827 // an invalid opcode, saying "64 bit operations are only
828 // supported in 64 bit modes." libopcodes disassembles it as
829 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
830 // "fistpll DWORD PTR " instead, which is what libopcodes is
831 // expecting to see.
Chris Lattnerb009c002004-02-11 19:26:28 +0000832 if (MI->getOpcode() == X86::FISTPr64) {
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000833 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000834 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000835 if (MI->getNumOperands() == 5) {
836 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000837 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000838 }
839 O << "\t# ";
840 }
841
Chris Lattnerb009c002004-02-11 19:26:28 +0000842 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000843 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000844 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000845 if (MI->getNumOperands() == 5) {
846 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000847 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000848 }
849 O << "\n";
850 return;
851 }
852
Chris Lattnerf9f60882002-11-18 06:56:51 +0000853 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000854 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000855 }
Chris Lattner72614082002-10-25 22:55:53 +0000856}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000857
Chris Lattner93c1afa2003-08-11 19:35:26 +0000858bool Printer::doInitialization(Module &M) {
859 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000860 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000861 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
862 // instruction as a reference to the register named sp, and if you try to
863 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
864 // before being looked up in the symbol table. This creates spurious
865 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
866 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000867 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000868 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000869 return false; // success
870}
871
Chris Lattnerad200712003-09-09 16:23:36 +0000872// SwitchSection - Switch to the specified section of the executable if we are
873// not already in it!
874//
875static void SwitchSection(std::ostream &OS, std::string &CurSection,
876 const char *NewSection) {
877 if (CurSection != NewSection) {
878 CurSection = NewSection;
879 if (!CurSection.empty())
880 OS << "\t" << NewSection << "\n";
881 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000882}
883
Chris Lattnerad200712003-09-09 16:23:36 +0000884bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000885 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000886 std::string CurSection;
887
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000888 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000889 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
890 if (I->hasInitializer()) { // External global require no code
891 O << "\n\n";
892 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000893 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000894 unsigned Size = TD.getTypeSize(C->getType());
895 unsigned Align = TD.getTypeAlignment(C->getType());
896
897 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000898 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
899 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000900 SwitchSection(O, CurSection, ".data");
901 if (I->hasInternalLinkage())
902 O << "\t.local " << name << "\n";
903
904 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000905 << "," << (unsigned)TD.getTypeAlignment(C->getType());
906 O << "\t\t# ";
907 WriteAsOperand(O, I, true, true, &M);
908 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000909 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000910 switch (I->getLinkage()) {
911 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000912 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000913 // Nonnull linkonce -> weak
914 O << "\t.weak " << name << "\n";
915 SwitchSection(O, CurSection, "");
916 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
917 break;
918
919 case GlobalValue::AppendingLinkage:
920 // FIXME: appending linkage variables should go into a section of
921 // their name or something. For now, just emit them as external.
922 case GlobalValue::ExternalLinkage:
923 // If external or appending, declare as a global symbol
924 O << "\t.globl " << name << "\n";
925 // FALL THROUGH
926 case GlobalValue::InternalLinkage:
927 if (C->isNullValue())
928 SwitchSection(O, CurSection, ".bss");
929 else
930 SwitchSection(O, CurSection, ".data");
931 break;
932 }
933
934 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000935 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000936 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000937 O << name << ":\t\t\t\t# ";
938 WriteAsOperand(O, I, true, true, &M);
939 O << " = ";
940 WriteAsOperand(O, C, false, false, &M);
941 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000942 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000943 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000944 }
Chris Lattnerad200712003-09-09 16:23:36 +0000945
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000946 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000947 return false; // success
948}
Brian Gaeked0fde302003-11-11 22:41:34 +0000949
950} // End llvm namespace