blob: 7cd4573ff66f1c7ee581cb5a5e0d9d1f0e2ca4b8 [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";
Brian Gaekede420ae2003-07-23 20:25:08 +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
Brian Gaeked7908f62003-06-27 00:00:48 +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: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000612 // There are two acceptable forms of MRMDestReg instructions, those with 2,
613 // 3 and 4 operands:
614 //
615 // 2 Operands: this is for things like mov that do not read a second input
Chris Lattnerf9f60882002-11-18 06:56:51 +0000616 //
617 // 3 Operands: in this form, the first two registers (the destination, and
618 // the first operand) should be the same, post register allocation. The 3rd
619 // operand is an additional input. This should be for things like add
620 // instructions.
621 //
Chris Lattnerb7089442003-01-13 00:35:03 +0000622 // 4 Operands: This form is for instructions which are 3 operands forms, but
623 // have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000624 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000625 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000626 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000627 (MI->getNumOperands() == 2 ||
628 (isTwoAddr && MI->getOperand(1).isRegister() &&
629 MI->getOperand(0).getReg() == MI->getOperand(1).getReg() &&
630 (MI->getNumOperands() == 3 ||
631 (MI->getNumOperands() == 4 && MI->getOperand(3).isImmediate()))))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000632 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000633
Brian Gaeked7908f62003-06-27 00:00:48 +0000634 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000635 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000636 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000637 printOp(MI->getOperand(1+isTwoAddr));
Chris Lattnerb7089442003-01-13 00:35:03 +0000638 if (MI->getNumOperands() == 4) {
639 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000640 printOp(MI->getOperand(3));
Chris Lattnerb7089442003-01-13 00:35:03 +0000641 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000642 O << "\n";
643 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000644 }
Chris Lattner18042332002-11-21 21:03:39 +0000645
646 case X86II::MRMDestMem: {
647 // These instructions are the same as MRMDestReg, but instead of having a
648 // register reference for the mod/rm field, it's a memory reference.
649 //
650 assert(isMem(MI, 0) && MI->getNumOperands() == 4+1 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000651 MI->getOperand(4).isRegister() && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000652
Brian Gaeked7908f62003-06-27 00:00:48 +0000653 O << TII.getName(MI->getOpCode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000654 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000655 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000656 printOp(MI->getOperand(4));
Chris Lattner18042332002-11-21 21:03:39 +0000657 O << "\n";
658 return;
659 }
660
Chris Lattner233ad712002-11-21 01:33:44 +0000661 case X86II::MRMSrcReg: {
Misha Brukman44ffd5a2003-10-20 04:03:10 +0000662 // There are three forms that are acceptable for MRMSrcReg instructions,
Chris Lattner644e1ab2002-11-21 00:30:01 +0000663 // those with 3 and 2 operands:
664 //
665 // 3 Operands: in this form, the last register (the second input) is the
666 // ModR/M input. The first two operands should be the same, post register
667 // allocation. This is for things like: add r32, r/m32
668 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000669 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
670 // for instructions like the IMULri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000671 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000672 // 2 Operands: this is for things like mov that do not read a second input
673 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000674 assert(MI->getOperand(0).isRegister() &&
675 MI->getOperand(1).isRegister() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000676 (MI->getNumOperands() == 2 ||
Chris Lattnerc01d1232003-10-20 03:42:58 +0000677 (MI->getNumOperands() == 3 &&
678 (MI->getOperand(2).isRegister() ||
679 MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000680 && "Bad format for MRMSrcReg!");
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000681 if (MI->getNumOperands() == 3 && !MI->getOperand(2).isImmediate() &&
Chris Lattner644e1ab2002-11-21 00:30:01 +0000682 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
683 O << "**";
684
Brian Gaeked7908f62003-06-27 00:00:48 +0000685 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000686 printOp(MI->getOperand(0));
Chris Lattnerc01d1232003-10-20 03:42:58 +0000687
688 // If this is IMULri* instructions, print the non-two-address operand.
689 if (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()) {
690 O << ", ";
691 printOp(MI->getOperand(1));
692 }
693
Chris Lattner644e1ab2002-11-21 00:30:01 +0000694 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000695 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000696 O << "\n";
697 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000698 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000699
Chris Lattner3d3067b2002-11-21 20:44:15 +0000700 case X86II::MRMSrcMem: {
701 // These instructions are the same as MRMSrcReg, but instead of having a
702 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000703 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000704 assert(MI->getOperand(0).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000705 (MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000706 (MI->getNumOperands() == 2+4 && MI->getOperand(1).isRegister() &&
Chris Lattner3d3067b2002-11-21 20:44:15 +0000707 isMem(MI, 2))
708 && "Bad format for MRMDestReg!");
709 if (MI->getNumOperands() == 2+4 &&
710 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
711 O << "**";
712
Brian Gaeked7908f62003-06-27 00:00:48 +0000713 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000714 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000715 O << ", " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000716 printMemReference(MI, MI->getNumOperands()-4);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000717 O << "\n";
718 return;
719 }
720
Chris Lattner675dd2c2002-11-21 17:09:01 +0000721 case X86II::MRMS0r: case X86II::MRMS1r:
722 case X86II::MRMS2r: case X86II::MRMS3r:
723 case X86II::MRMS4r: case X86II::MRMS5r:
724 case X86II::MRMS6r: case X86II::MRMS7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000725 // In this form, the following are valid formats:
726 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000727 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000728 // 2. shl rdest, rinput <implicit CL or 1>
729 // 3. sbb rdest, rinput, immediate [rdest = rinput]
730 //
731 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000732 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000733 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000734 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000735 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000736 assert((MI->getNumOperands() < 3 ||
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000737 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000738 "Bad MRMSxR format!");
739
Chris Lattnerd9096832002-12-15 08:01:39 +0000740 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000741 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
742 O << "**";
743
Brian Gaeked7908f62003-06-27 00:00:48 +0000744 O << TII.getName(MI->getOpCode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000745 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000746 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000747 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000748 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000749 }
Brian Gaeke2a098772003-08-11 19:05:46 +0000750 checkImplUses(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000751 O << "\n";
752
753 return;
754 }
755
Chris Lattnerb7089442003-01-13 00:35:03 +0000756 case X86II::MRMS0m: case X86II::MRMS1m:
757 case X86II::MRMS2m: case X86II::MRMS3m:
758 case X86II::MRMS4m: case X86II::MRMS5m:
759 case X86II::MRMS6m: case X86II::MRMS7m: {
760 // In this form, the following are valid formats:
761 // 1. sete [m]
762 // 2. cmp [m], immediate
763 // 2. shl [m], rinput <implicit CL or 1>
764 // 3. sbb [m], immediate
765 //
766 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
767 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000768 assert((MI->getNumOperands() != 5 ||
769 (MI->getOperand(4).isImmediate() ||
770 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000771 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000772
773 const MachineOperand &Op3 = MI->getOperand(3);
774
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000775 // Bug: The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
776 // is misassembled by gas in intel_syntax mode as its 32-bit
777 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
778 // opcode bytes instead of the instruction.
Brian Gaekeb44210d2003-07-07 18:34:20 +0000779 if (MI->getOpCode() == X86::FSTPr80) {
780 if ((MI->getOperand(0).getReg() == X86::ESP)
781 && (MI->getOperand(1).getImmedValue() == 1)) {
Chris Lattnerf2d29252003-12-01 05:13:56 +0000782 if (Op3.isImmediate() &&
783 Op3.getImmedValue() >= -128 && Op3.getImmedValue() <= 127) {
784 // 1 byte disp.
785 O << ".byte 0xdb, 0x7c, 0x24, 0x" << std::hex
786 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
787 } else {
Brian Gaekeb44210d2003-07-07 18:34:20 +0000788 O << ".byte 0xdb, 0xbc, 0x24\n\t";
Chris Lattnerf2d29252003-12-01 05:13:56 +0000789 O << ".long ";
790 printOp(Op3);
791 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000792 }
793 }
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000794 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000795
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000796 // Bug: The 80-bit FP load instruction "fld XWORD PTR [...]" is
797 // misassembled by gas in intel_syntax mode as its 32-bit
798 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
799 // opcode bytes instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000800 if (MI->getOpCode() == X86::FLDr80 &&
801 MI->getOperand(0).getReg() == X86::ESP &&
802 MI->getOperand(1).getImmedValue() == 1) {
803 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
804 Op3.getImmedValue() <= 127) { // 1 byte displacement
805 O << ".byte 0xdb, 0x6c, 0x24, 0x" << std::hex
806 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
807 } else {
808 O << ".byte 0xdb, 0xac, 0x24\n\t";
809 O << ".long ";
810 printOp(Op3);
811 O << "\t# ";
Brian Gaekeb44210d2003-07-07 18:34:20 +0000812 }
813 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000814
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000815 // Bug: gas intel_syntax mode treats "fild QWORD PTR [...]" as an
816 // invalid opcode, saying "64 bit operations are only supported in
817 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
818 // [...]", which is wrong. Workaround: Output the raw opcode bytes
819 // instead of the instruction.
Chris Lattnerf2d29252003-12-01 05:13:56 +0000820 if (MI->getOpCode() == X86::FILDr64 &&
821 MI->getOperand(0).getReg() == X86::ESP &&
822 MI->getOperand(1).getImmedValue() == 1) {
823 if (Op3.isImmediate() && Op3.getImmedValue() >= -128 &&
824 Op3.getImmedValue() <= 127) { // 1 byte displacement
825 O << ".byte 0xdf, 0x6c, 0x24, 0x" << std::hex
826 << ((unsigned)Op3.getImmedValue() & 255) << std::dec << "\t# ";
827 } else {
828 O << ".byte 0xdf, 0xac, 0x24\n\t";
829 O << ".long ";
830 printOp(Op3);
831 O << std::dec << "\t# ";
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000832 }
833 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000834
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000835 // Bug: gas intel_syntax mode treats "fistp QWORD PTR [...]" as
836 // an invalid opcode, saying "64 bit operations are only
837 // supported in 64 bit modes." libopcodes disassembles it as
838 // "fistpll DWORD PTR [...]", which is wrong. Workaround: Output
839 // "fistpll DWORD PTR " instead, which is what libopcodes is
840 // expecting to see.
841 if (MI->getOpCode() == X86::FISTPr64) {
842 O << "fistpll DWORD PTR ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000843 printMemReference(MI, 0);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000844 if (MI->getNumOperands() == 5) {
845 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000846 printOp(MI->getOperand(4));
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000847 }
848 O << "\t# ";
849 }
850
Brian Gaeked7908f62003-06-27 00:00:48 +0000851 O << TII.getName(MI->getOpCode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000852 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000853 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000854 if (MI->getNumOperands() == 5) {
855 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000856 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000857 }
858 O << "\n";
859 return;
860 }
861
Chris Lattnerf9f60882002-11-18 06:56:51 +0000862 default:
Chris Lattnerb7089442003-01-13 00:35:03 +0000863 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000864 }
Chris Lattner72614082002-10-25 22:55:53 +0000865}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000866
Chris Lattner93c1afa2003-08-11 19:35:26 +0000867bool Printer::doInitialization(Module &M) {
868 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000869 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000870 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
871 // instruction as a reference to the register named sp, and if you try to
872 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
873 // before being looked up in the symbol table. This creates spurious
874 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
875 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000876 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000877 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000878 return false; // success
879}
880
Chris Lattnerad200712003-09-09 16:23:36 +0000881// SwitchSection - Switch to the specified section of the executable if we are
882// not already in it!
883//
884static void SwitchSection(std::ostream &OS, std::string &CurSection,
885 const char *NewSection) {
886 if (CurSection != NewSection) {
887 CurSection = NewSection;
888 if (!CurSection.empty())
889 OS << "\t" << NewSection << "\n";
890 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000891}
892
Chris Lattnerad200712003-09-09 16:23:36 +0000893bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000894 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000895 std::string CurSection;
896
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000897 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000898 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
899 if (I->hasInitializer()) { // External global require no code
900 O << "\n\n";
901 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000902 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000903 unsigned Size = TD.getTypeSize(C->getType());
904 unsigned Align = TD.getTypeAlignment(C->getType());
905
906 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000907 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
908 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000909 SwitchSection(O, CurSection, ".data");
910 if (I->hasInternalLinkage())
911 O << "\t.local " << name << "\n";
912
913 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000914 << "," << (unsigned)TD.getTypeAlignment(C->getType());
915 O << "\t\t# ";
916 WriteAsOperand(O, I, true, true, &M);
917 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000918 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000919 switch (I->getLinkage()) {
920 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000921 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000922 // Nonnull linkonce -> weak
923 O << "\t.weak " << name << "\n";
924 SwitchSection(O, CurSection, "");
925 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
926 break;
927
928 case GlobalValue::AppendingLinkage:
929 // FIXME: appending linkage variables should go into a section of
930 // their name or something. For now, just emit them as external.
931 case GlobalValue::ExternalLinkage:
932 // If external or appending, declare as a global symbol
933 O << "\t.globl " << name << "\n";
934 // FALL THROUGH
935 case GlobalValue::InternalLinkage:
936 if (C->isNullValue())
937 SwitchSection(O, CurSection, ".bss");
938 else
939 SwitchSection(O, CurSection, ".data");
940 break;
941 }
942
943 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000944 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000945 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000946 O << name << ":\t\t\t\t# ";
947 WriteAsOperand(O, I, true, true, &M);
948 O << " = ";
949 WriteAsOperand(O, C, false, false, &M);
950 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000951 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000952 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000953 }
Chris Lattnerad200712003-09-09 16:23:36 +0000954
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000955 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000956 return false; // success
957}
Brian Gaeked0fde302003-11-11 22:41:34 +0000958
959} // End llvm namespace