blob: 292a465e725e69c30c0db689d38f90ce9b1217ef [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/// isStringCompatible - Can we treat the specified array as a string?
99/// Only if it is an array of ubytes or non-negative sbytes.
100///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000101static bool isStringCompatible(const ConstantArray *CVA) {
102 const Type *ETy = cast<ArrayType>(CVA->getType())->getElementType();
103 if (ETy == Type::UByteTy) return true;
104 if (ETy != Type::SByteTy) return false;
105
106 for (unsigned i = 0; i < CVA->getNumOperands(); ++i)
107 if (cast<ConstantSInt>(CVA->getOperand(i))->getValue() < 0)
108 return false;
109
110 return true;
111}
112
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000113/// toOctal - Convert the low order bits of X into an octal digit.
114///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000115static inline char toOctal(int X) {
116 return (X&7)+'0';
117}
118
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000119/// getAsCString - Return the specified array as a C compatible
120/// string, only if the predicate isStringCompatible is true.
121///
Chris Lattnerac662d12003-11-03 20:19:49 +0000122static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000123 assert(isStringCompatible(CVA) && "Array is not string compatible!");
124
Chris Lattnerac662d12003-11-03 20:19:49 +0000125 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000126 for (unsigned i = 0; i < CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000127 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000128
129 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000130 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000131 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000132 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000133 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000134 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000135 } else {
136 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000137 case '\b': O << "\\b"; break;
138 case '\f': O << "\\f"; break;
139 case '\n': O << "\\n"; break;
140 case '\r': O << "\\r"; break;
141 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000142 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000143 O << '\\';
144 O << toOctal(C >> 6);
145 O << toOctal(C >> 3);
146 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147 break;
148 }
149 }
150 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000151 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000152}
153
Chris Lattnerac662d12003-11-03 20:19:49 +0000154// Print out the specified constant, without a storage class. Only the
155// constants valid in constant expressions can occur here.
156void Printer::emitConstantValueOnly(const Constant *CV) {
157 if (CV->isNullValue())
158 O << "0";
159 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
160 assert(CB == ConstantBool::True);
161 O << "1";
162 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
163 O << CI->getValue();
164 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
165 O << CI->getValue();
166 else if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(CV))
167 // This is a constant address for a global variable or function. Use the
168 // name of the variable or function as the address value.
Chris Lattner90533562003-11-04 16:04:32 +0000169 O << Mang->getValueName(CPR->getValue());
Chris Lattnerac662d12003-11-03 20:19:49 +0000170 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
171 const TargetData &TD = TM.getTargetData();
172 switch(CE->getOpcode()) {
173 case Instruction::GetElementPtr: {
174 // generate a symbolic expression for the byte address
175 const Constant *ptrVal = CE->getOperand(0);
176 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
177 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
178 O << "(";
179 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000180 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000181 } else {
182 emitConstantValueOnly(ptrVal);
183 }
184 break;
185 }
186 case Instruction::Cast: {
187 // Support only non-converting or widening casts for now, that is, ones
188 // that do not involve a change in value. This assertion is not a
189 // complete check.
190 Constant *Op = CE->getOperand(0);
191 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000192
Chris Lattnerac662d12003-11-03 20:19:49 +0000193 assert(((isa<PointerType>(OpTy)
194 && (Ty == Type::LongTy || Ty == Type::ULongTy))
195 || (isa<PointerType>(Ty)
Chris Lattner90533562003-11-04 16:04:32 +0000196 && (OpTy == Type::LongTy || OpTy == Type::ULongTy))
197 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
198 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000199 && "FIXME: Don't yet support this kind of constant cast expr");
200 O << "(";
201 emitConstantValueOnly(Op);
202 O << ")";
203 break;
204 }
205 case Instruction::Add:
206 O << "(";
207 emitConstantValueOnly(CE->getOperand(0));
208 O << ") + (";
209 emitConstantValueOnly(CE->getOperand(1));
210 O << ")";
211 break;
212 default:
213 assert(0 && "Unsupported operator!");
214 }
215 } else {
216 assert(0 && "Unknown constant value!");
217 }
218}
219
220// Print a constant value or values, with the appropriate storage class as a
221// prefix.
222void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000223 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000224
Chris Lattnerad200712003-09-09 16:23:36 +0000225 if (CV->isNullValue()) {
226 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000227 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000228 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
229 if (isStringCompatible(CVA)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000230 O << "\t.ascii\t";
231 printAsCString(O, CVA);
232 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000233 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000234 const std::vector<Use> &constValues = CVA->getValues();
235 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000236 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000237 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000238 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000239 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
240 // Print the fields in successive locations. Pad to align if needed!
241 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
242 const std::vector<Use>& constValues = CVS->getValues();
243 unsigned sizeSoFar = 0;
244 for (unsigned i=0, N = constValues.size(); i < N; i++) {
245 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000246
Chris Lattnerad200712003-09-09 16:23:36 +0000247 // Check if padding is needed and insert one or more 0s.
248 unsigned fieldSize = TD.getTypeSize(field->getType());
249 unsigned padSize = ((i == N-1? cvsLayout->StructSize
250 : cvsLayout->MemberOffsets[i+1])
251 - cvsLayout->MemberOffsets[i]) - fieldSize;
252 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000253
Chris Lattnerad200712003-09-09 16:23:36 +0000254 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000255 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000256
257 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000258 if (padSize)
259 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000260 }
Chris Lattnerad200712003-09-09 16:23:36 +0000261 assert(sizeSoFar == cvsLayout->StructSize &&
262 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000263 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000264 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
265 // FP Constants are printed as integer constants to avoid losing
266 // precision...
267 double Val = CFP->getValue();
268 switch (CFP->getType()->getPrimitiveID()) {
269 default: assert(0 && "Unknown floating point type!");
270 case Type::FloatTyID: {
271 union FU { // Abide by C TBAA rules
272 float FVal;
273 unsigned UVal;
274 } U;
275 U.FVal = Val;
276 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
277 return;
278 }
279 case Type::DoubleTyID: {
280 union DU { // Abide by C TBAA rules
281 double FVal;
282 uint64_t UVal;
283 } U;
284 U.FVal = Val;
285 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
286 return;
287 }
288 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000289 }
290
291 const Type *type = CV->getType();
292 O << "\t";
293 switch (type->getPrimitiveID()) {
294 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
295 O << ".byte";
296 break;
297 case Type::UShortTyID: case Type::ShortTyID:
298 O << ".word";
299 break;
300 case Type::FloatTyID: case Type::PointerTyID:
301 case Type::UIntTyID: case Type::IntTyID:
302 O << ".long";
303 break;
304 case Type::DoubleTyID:
305 case Type::ULongTyID: case Type::LongTyID:
306 O << ".quad";
307 break;
308 default:
309 assert (0 && "Can't handle printing this type of thing");
310 break;
311 }
312 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000313 emitConstantValueOnly(CV);
314 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000315}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000316
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000317/// printConstantPool - Print to the current output stream assembly
318/// representations of the constants in the constant pool MCP. This is
319/// used to print out constants which have been "spilled to memory" by
320/// the code generator.
321///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000322void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000323 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000324 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000325
Chris Lattnerb7089442003-01-13 00:35:03 +0000326 if (CP.empty()) return;
327
328 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
329 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000330 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000331 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000332 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
333 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000334 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000335 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000336}
337
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000338/// runOnMachineFunction - This uses the printMachineInstruction()
339/// method to print assembly for each instruction.
340///
Chris Lattner0285a332002-12-28 20:25:38 +0000341bool Printer::runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000342 // BBNumber is used here so that a given Printer will never give two
343 // BBs the same name. (If you have a better way, please let me know!)
Chris Lattner0285a332002-12-28 20:25:38 +0000344 static unsigned BBNumber = 0;
Brian Gaeke6559bb92002-11-14 22:32:30 +0000345
Chris Lattnere0121322003-08-03 23:37:09 +0000346 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000347 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000348 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000349
Chris Lattnerb7089442003-01-13 00:35:03 +0000350 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000351 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000352
Brian Gaeke6559bb92002-11-14 22:32:30 +0000353 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000354 O << "\t.text\n";
355 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000356 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000357 if (!EmitCygwin)
358 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000359 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000360
Brian Gaeked7908f62003-06-27 00:00:48 +0000361 // Number each basic block so that we can consistently refer to them
362 // in PC-relative references.
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000363 NumberForBB.clear();
364 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
365 I != E; ++I) {
366 NumberForBB[I->getBasicBlock()] = BBNumber++;
367 }
368
Brian Gaeke6559bb92002-11-14 22:32:30 +0000369 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000370 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
371 I != E; ++I) {
372 // Print a label for the basic block.
Brian Gaeke002a50a2003-07-31 17:38:52 +0000373 O << ".LBB" << NumberForBB[I->getBasicBlock()] << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000374 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000375 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
376 II != E; ++II) {
377 // Print the assembly for the instruction.
378 O << "\t";
Brian Gaekede420ae2003-07-23 20:25:08 +0000379 printMachineInstruction(*II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000380 }
Chris Lattner0285a332002-12-28 20:25:38 +0000381 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000382
383 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000384 return false;
385}
386
Chris Lattner3d3067b2002-11-21 20:44:15 +0000387static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000388 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000389 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
390 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000391}
392
393static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000394 if (MI->getOperand(Op).isFrameIndex()) return true;
395 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000396 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000397 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
398 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000399}
400
Brian Gaeke2a098772003-08-11 19:05:46 +0000401
402
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000403void Printer::printOp(const MachineOperand &MO,
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000404 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000405 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000406 switch (MO.getType()) {
407 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000408 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000409 O << "<" << V->getName() << ">";
410 return;
411 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000412 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000413 case MachineOperand::MO_MachineRegister:
Brian Gaeke9d99b432003-08-13 18:15:15 +0000414 if (MO.getReg() < MRegisterInfo::FirstVirtualRegister)
Brian Gaeke2a098772003-08-11 19:05:46 +0000415 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000416 O << "%" << RI.get(MO.getReg()).Name;
417 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000418 O << "%reg" << MO.getReg();
419 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000420
421 case MachineOperand::MO_SignExtendedImmed:
422 case MachineOperand::MO_UnextendedImmed:
423 O << (int)MO.getImmedValue();
424 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000425 case MachineOperand::MO_PCRelativeDisp: {
426 ValueMapTy::const_iterator i = NumberForBB.find(MO.getVRegValue());
427 assert (i != NumberForBB.end()
428 && "Could not find a BB in the NumberForBB map!");
429 O << ".LBB" << i->second << " # PC rel: " << MO.getVRegValue()->getName();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000430 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000431 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000432 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000433 if (!elideOffsetKeyword)
434 O << "OFFSET ";
435 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000436 return;
437 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000438 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000439 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000440 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000441 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000442 }
443}
444
Chris Lattner3501fea2003-01-14 22:00:31 +0000445static const std::string sizePtr(const TargetInstrDescriptor &Desc) {
Chris Lattnera0f38c82002-12-13 03:51:55 +0000446 switch (Desc.TSFlags & X86II::ArgMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000447 default: assert(0 && "Unknown arg size!");
448 case X86II::Arg8: return "BYTE PTR";
449 case X86II::Arg16: return "WORD PTR";
450 case X86II::Arg32: return "DWORD PTR";
451 case X86II::Arg64: return "QWORD PTR";
452 case X86II::ArgF32: return "DWORD PTR";
453 case X86II::ArgF64: return "QWORD PTR";
454 case X86II::ArgF80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000455 }
456}
457
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000458void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000459 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000460
461 if (MI->getOperand(Op).isFrameIndex()) {
462 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
463 if (MI->getOperand(Op+3).getImmedValue())
464 O << " + " << MI->getOperand(Op+3).getImmedValue();
465 O << "]";
466 return;
467 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000468 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000469 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000470 if (MI->getOperand(Op+3).getImmedValue())
471 O << " + " << MI->getOperand(Op+3).getImmedValue();
472 O << "]";
473 return;
474 }
475
Chris Lattner3d3067b2002-11-21 20:44:15 +0000476 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000477 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000478 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000479 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000480
481 O << "[";
482 bool NeedPlus = false;
483 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000484 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000485 NeedPlus = true;
486 }
487
488 if (IndexReg.getReg()) {
489 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000490 if (ScaleVal != 1)
491 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000492 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000493 NeedPlus = true;
494 }
495
Chris Lattner0285a332002-12-28 20:25:38 +0000496 if (DispVal) {
497 if (NeedPlus)
498 if (DispVal > 0)
499 O << " + ";
500 else {
501 O << " - ";
502 DispVal = -DispVal;
503 }
504 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000505 }
506 O << "]";
507}
508
Brian Gaeke2a098772003-08-11 19:05:46 +0000509/// checkImplUses - Emit the implicit-use registers for the
510/// instruction described by DESC, if its PrintImplUses flag is set.
511///
512void Printer::checkImplUses (const TargetInstrDescriptor &Desc) {
513 const MRegisterInfo &RI = *TM.getRegisterInfo();
514 if (Desc.TSFlags & X86II::PrintImplUses) {
515 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
516 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000517 O << ", %" << RI.get(*p).Name;
Brian Gaeke2a098772003-08-11 19:05:46 +0000518 }
519 }
520}
521
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000522/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000523/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000524///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000525void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000526 unsigned Opcode = MI->getOpcode();
Brian Gaeked7908f62003-06-27 00:00:48 +0000527 const TargetInstrInfo &TII = TM.getInstrInfo();
528 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000529
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000530 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000531 switch (Desc.TSFlags & X86II::FormMask) {
532 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000533 // Print pseudo-instructions as comments; either they should have been
534 // turned into real instructions by now, or they don't need to be
535 // seen by the assembler (e.g., IMPLICIT_USEs.)
536 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000537 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000538 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000539 O << " = phi ";
540 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
541 if (i != 1) O << ", ";
542 O << "[";
Brian Gaekede420ae2003-07-23 20:25:08 +0000543 printOp(MI->getOperand(i));
Chris Lattnereca1f632002-12-25 05:09:01 +0000544 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000545 printOp(MI->getOperand(i+1));
Chris Lattnereca1f632002-12-25 05:09:01 +0000546 O << "]";
547 }
548 } else {
549 unsigned i = 0;
Vikram S. Adve49cab032003-05-27 00:03:17 +0000550 if (MI->getNumOperands() && (MI->getOperand(0).opIsDefOnly() ||
551 MI->getOperand(0).opIsDefAndUse())) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000552 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000553 O << " = ";
554 ++i;
555 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000556 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000557
558 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
559 O << " ";
Vikram S. Adve49cab032003-05-27 00:03:17 +0000560 if (MI->getOperand(i).opIsDefOnly() ||
561 MI->getOperand(i).opIsDefAndUse()) O << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000562 printOp(MI->getOperand(i));
Vikram S. Adve49cab032003-05-27 00:03:17 +0000563 if (MI->getOperand(i).opIsDefOnly() ||
564 MI->getOperand(i).opIsDefAndUse()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000565 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000566 }
567 O << "\n";
568 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000569
Chris Lattnerf9f60882002-11-18 06:56:51 +0000570 case X86II::RawFrm:
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000571 // The accepted forms of Raw instructions are:
572 // 1. nop - No operand required
573 // 2. jmp foo - PC relative displacement operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000574 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000575 //
576 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000577 (MI->getNumOperands() == 1 &&
578 (MI->getOperand(0).isPCRelativeDisp() ||
579 MI->getOperand(0).isGlobalAddress() ||
580 MI->getOperand(0).isExternalSymbol())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000581 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000582 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000583
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000584 if (MI->getNumOperands() == 1) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000585 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
Chris Lattnerf9f60882002-11-18 06:56:51 +0000586 }
587 O << "\n";
588 return;
589
Chris Lattner77875d82002-11-21 02:00:20 +0000590 case X86II::AddRegFrm: {
591 // There are currently two forms of acceptable AddRegFrm instructions.
592 // Either the instruction JUST takes a single register (like inc, dec, etc),
593 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000594 // (move immediate f.e.). Note that this immediate value might be stored as
595 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000596 // into a register. The initial register might be duplicated if this is a
597 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000598 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000599 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000600 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000601 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000602 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000603 MI->getOperand(1).isImmediate() ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000604 MI->getOperand(1).isRegister() ||
605 MI->getOperand(1).isGlobalAddress() ||
606 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000607 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000608
Chris Lattner77875d82002-11-21 02:00:20 +0000609 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000610
Brian Gaeked7908f62003-06-27 00:00:48 +0000611 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000612 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000613 if (MI->getNumOperands() == 2 &&
614 (!MI->getOperand(1).isRegister() ||
615 MI->getOperand(1).getVRegValueOrNull() ||
616 MI->getOperand(1).isGlobalAddress() ||
617 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000618 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000619 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000620 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000621 checkImplUses(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000622 O << "\n";
623 return;
624 }
Chris Lattner233ad712002-11-21 01:33:44 +0000625 case X86II::MRMDestReg: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000626 // There are two acceptable forms of MRMDestReg instructions, those with 2,
627 // 3 and 4 operands:
628 //
629 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000630 //
631 // 3 Operands: in this form, the first two registers (the destination, and
632 // the first operand) should be the same, post register allocation. The 3rd
633 // operand is an additional input. This should be for things like add
634 // instructions.
635 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000636 // 4 Operands: This form is for instructions which are 3 operands forms, but
637 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000638 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000639 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000640 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000641 (MI->getNumOperands() == 2 ||
642 (isTwoAddr && MI->getOperand(1).isRegister() &&
643 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
644 (MI->getNumOperands() == 3 ||
645 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000646 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000647
Brian Gaeked7908f62003-06-27 00:00:48 +0000648 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000649 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000650 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000651 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000652 if (MI->getNumOperands() == 4) {
653 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000654 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000655 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000656 O << "\n";
657 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000658 }
Chris Lattner18042332002-11-21 21:03:39 +0000659
660 case X86II::MRMDestMem: {
661 // These instructions are the same as MRMDestReg, but instead of having a
662 // register reference for the mod/rm field, it's a memory reference.
663 //
664 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000665 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000666
Brian Gaeked7908f62003-06-27 00:00:48 +0000667 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000668 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000669 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000670 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000671 O << "\n";
672 return;
673 }
674
Chris Lattner233ad712002-11-21 01:33:44 +0000675 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000676 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000677 // those with 3 and 2 operands:
678 //
679 // 3 Operands: in this form, the last register (the second input) is the
680 // ModR/M input. The first two operands should be the same, post register
681 // allocation. This is for things like: add r32, r/m32
682 //
Chris Lattnerc01d1232003-10-20 03:42:58 +0000683 // 3 Operands: in this form, we can have 'INST R, R, imm', which is used for
684 // instructions like the IMULri instructions.
685 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000686 // 2 Operands: this is for things like mov that do not read a second input
687 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000688 assert(MI->getOperand(0).isRegister() &&
689 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000690 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000691 (MI->getNumOperands() == 3 &&
692 (MI->getOperand(2).isRegister() ||
693 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000694 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000695 if (MI->getNumOperands() == 3 &&
696 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
697 O << "**";
698
Brian Gaeked7908f62003-06-27 00:00:48 +0000699 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000700 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000701
702 // If this is IMULri* instructions, print the non-two-address operand.
703 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
704 O << ", ";
705 printOp(MI->getOperand(1));
706 }
707
Chris Lattner644e1ab2002-11-21 00:30:01 +0000708 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000709 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000710 O << "\n";
711 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000712 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000713
Chris Lattner3d3067b2002-11-21 20:44:15 +0000714 case X86II::MRMSrcMem: {
715 // These instructions are the same as MRMSrcReg, but instead of having a
716 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000717 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000718 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000719 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000720 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000721 isMem(MI, 2))
722 && "Bad format for MRMDestReg!");
723 if (MI->getNumOperands() == 2+4 &&
724 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
725 O << "**";
726
Brian Gaeked7908f62003-06-27 00:00:48 +0000727 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000728 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000729 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000730 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000731 O << "\n";
732 return;
733 }
734
Chris Lattner675dd2c2002-11-21 17:09:01 +0000735 case X86II::MRMS0r: case X86II::MRMS1r:
736 case X86II::MRMS2r: case X86II::MRMS3r:
737 case X86II::MRMS4r: case X86II::MRMS5r:
738 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000739 // In this form, the following are valid formats:
740 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000741 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000742 // 2. shl rdest, rinput <implicit CL or 1>
743 // 3. sbb rdest, rinput, immediate [rdest = rinput]
744 //
745 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000746 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000747 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000748 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000749 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000750 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000751 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000752 "Bad MRMSxR format!");
753
Chris Lattnerd9096832002-12-15 08:01:39 +0000754 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000755 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
756 O << "**";
757
Brian Gaeked7908f62003-06-27 00:00:48 +0000758 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000759 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000760 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000761 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000762 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000763 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000764 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000765 O << "\n";
766
767 return;
768 }
769
Chris Lattnerb7089442003-01-13 00:35:03 +0000770 case X86II::MRMS0m: case X86II::MRMS1m:
771 case X86II::MRMS2m: case X86II::MRMS3m:
772 case X86II::MRMS4m: case X86II::MRMS5m:
773 case X86II::MRMS6m: case X86II::MRMS7m: {
774 // In this form, the following are valid formats:
775 // 1. sete [m]
776 // 2. cmp [m], immediate
777 // 2. shl [m], rinput <implicit CL or 1>
778 // 3. sbb [m], immediate
779 //
780 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
781 isMem(MI, 0) && "Bad MRMSxM format!");
782 assert((MI->getNumOperands() != 5 || MI->getOperand(4).isImmediate()) &&
783 "Bad MRMSxM format!");
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000784 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
785 // is misassembled by gas in intel_syntax mode as its 32-bit
786 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
787 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000788 if (MI->getOpCode() == X86::FSTPr80) {
789 if ((MI->getOperand(0).getReg() == X86::ESP)
790 && (MI->getOperand(1).getImmedValue() == 1)) {
791 int DispVal = MI->getOperand(3).getImmedValue();
792 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
793 unsigned int val = (unsigned int) DispVal;
794 O << ".byte 0xdb, 0xbc, 0x24\n\t";
795 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
796 } else { // 1 byte disp.
797 unsigned char val = (unsigned char) DispVal;
798 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex << (unsigned) val
799 << std::dec << "\t# ";
800 }
801 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000802 }
803 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
804 // misassembled by gas in intel_syntax mode as its 32-bit
805 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
806 // opcode bytes instead of the instruction.
807 if (MI->getOpCode() == X86::FLDr80) {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000808 if ((MI->getOperand(0).getReg() == X86::ESP)
809 && (MI->getOperand(1).getImmedValue() == 1)) {
810 int DispVal = MI->getOperand(3).getImmedValue();
811 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
812 unsigned int val = (unsigned int) DispVal;
813 O << ".byte 0xdb, 0xac, 0x24\n\t";
814 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
815 } else { // 1 byte disp.
816 unsigned char val = (unsigned char) DispVal;
817 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
818 << std::dec << "\t# ";
819 }
820 }
821 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000822 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
823 // invalid opcode, saying "64 bit operations are only supported in
824 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
825 // [...]", which is wrong. Workaround: Output the raw opcode bytes
826 // instead of the instruction.
827 if (MI->getOpCode() == X86::FILDr64) {
828 if ((MI->getOperand(0).getReg() == X86::ESP)
829 && (MI->getOperand(1).getImmedValue() == 1)) {
830 int DispVal = MI->getOperand(3).getImmedValue();
831 if ((DispVal < -128) || (DispVal > 127)) { // 4 byte disp.
832 unsigned int val = (unsigned int) DispVal;
833 O << ".byte 0xdf, 0xac, 0x24\n\t";
834 O << ".long 0x" << std::hex << (unsigned) val << std::dec << "\t# ";
835 } else { // 1 byte disp.
836 unsigned char val = (unsigned char) DispVal;
837 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex << (unsigned) val
838 << std::dec << "\t# ";
839 }
840 }
841 }
842 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
843 // an invalid opcode, saying "64 bit operations are only
844 // supported in 64 bit modes." libopcodes disassembles it as
845 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
846 // "fistpll DWORD PTR " instead, which is what libopcodes is
847 // expecting to see.
848 if (MI->getOpCode() == X86::FISTPr64) {
849 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000850 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000851 if (MI->getNumOperands() == 5) {
852 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000853 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000854 }
855 O << "\t# ";
856 }
857
Brian Gaeked7908f62003-06-27 00:00:48 +0000858 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000859 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000860 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000861 if (MI->getNumOperands() == 5) {
862 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000863 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000864 }
865 O << "\n";
866 return;
867 }
868
Chris Lattnerf9f60882002-11-18 06:56:51 +0000869 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000870 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000871 }
Chris Lattner72614082002-10-25 22:55:53 +0000872}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000873
Chris Lattner93c1afa2003-08-11 19:35:26 +0000874bool Printer::doInitialization(Module &M) {
875 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000876 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000877 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
878 // instruction as a reference to the register named sp, and if you try to
879 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
880 // before being looked up in the symbol table. This creates spurious
881 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
882 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000883 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000884 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000885 return false; // success
886}
887
Chris Lattnerad200712003-09-09 16:23:36 +0000888// SwitchSection - Switch to the specified section of the executable if we are
889// not already in it!
890//
891static void SwitchSection(std::ostream &OS, std::string &CurSection,
892 const char *NewSection) {
893 if (CurSection != NewSection) {
894 CurSection = NewSection;
895 if (!CurSection.empty())
896 OS << "\t" << NewSection << "\n";
897 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000898}
899
Chris Lattnerad200712003-09-09 16:23:36 +0000900bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000901 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000902 std::string CurSection;
903
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000904 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000905 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
906 if (I->hasInitializer()) { // External global require no code
907 O << "\n\n";
908 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000909 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000910 unsigned Size = TD.getTypeSize(C->getType());
911 unsigned Align = TD.getTypeAlignment(C->getType());
912
913 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000914 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
915 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000916 SwitchSection(O, CurSection, ".data");
917 if (I->hasInternalLinkage())
918 O << "\t.local " << name << "\n";
919
920 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000921 << "," << (unsigned)TD.getTypeAlignment(C->getType());
922 O << "\t\t# ";
923 WriteAsOperand(O, I, true, true, &M);
924 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000925 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000926 switch (I->getLinkage()) {
927 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000928 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000929 // Nonnull linkonce -> weak
930 O << "\t.weak " << name << "\n";
931 SwitchSection(O, CurSection, "");
932 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
933 break;
934
935 case GlobalValue::AppendingLinkage:
936 // FIXME: appending linkage variables should go into a section of
937 // their name or something. For now, just emit them as external.
938 case GlobalValue::ExternalLinkage:
939 // If external or appending, declare as a global symbol
940 O << "\t.globl " << name << "\n";
941 // FALL THROUGH
942 case GlobalValue::InternalLinkage:
943 if (C->isNullValue())
944 SwitchSection(O, CurSection, ".bss");
945 else
946 SwitchSection(O, CurSection, ".data");
947 break;
948 }
949
950 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000951 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000952 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000953 O << name << ":\t\t\t\t# ";
954 WriteAsOperand(O, I, true, true, &M);
955 O << " = ";
956 WriteAsOperand(O, C, false, false, &M);
957 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000958 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000959 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000960 }
Chris Lattnerad200712003-09-09 16:23:36 +0000961
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000962 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000963 return false; // success
964}
Brian Gaeked0fde302003-11-11 22:41:34 +0000965
966} // End llvm namespace