blob: 10a468ff18d06162c386dc98781f6ac886439d14 [file] [log] [blame]
Misha Brukman91b5ca82004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Misha Brukman538607f2004-03-01 23:53:11 +000010// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Intel-format assembly language. This
12// printer is the output mechanism used by `llc' and `lli -print-machineinstrs'
13// on X86.
Chris Lattner72614082002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaeke6559bb92002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattnere0121322003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000024#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattnerb7089442003-01-13 00:35:03 +000025#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos03090662004-03-09 03:35:34 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnerdbb61c62002-11-17 22:53:13 +000027#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000028#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000029#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000030#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000031#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000032#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000033using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000035namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000036 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
37
Chris Lattner93c1afa2003-08-11 19:35:26 +000038 // FIXME: This should be automatically picked up by autoconf from the C
39 // frontend
40 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
41 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
42
Alkis Evlogimenos03090662004-03-09 03:35:34 +000043 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
44 GasBugWorkaroundEmitter(std::ostream& o)
45 : O(o), OldFlags(O.flags()), firstByte(true) {
46 O << std::hex;
47 }
48
49 ~GasBugWorkaroundEmitter() {
50 O.flags(OldFlags);
51 O << "\t# ";
52 }
53
54 virtual void emitByte(unsigned char B) {
55 if (!firstByte) O << "\n\t";
56 firstByte = false;
57 O << ".byte 0x" << (unsigned) B;
58 }
59
60 // These should never be called
61 virtual void emitWord(unsigned W) { assert(0); }
Misha Brukmandb760d02004-03-11 19:08:24 +000062 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
63 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
64 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
65 virtual uint64_t getCurrentPCValue() { abort(); }
66 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000067
68 private:
69 std::ostream& O;
70 std::ios::fmtflags OldFlags;
71 bool firstByte;
72 };
73
Chris Lattner0285a332002-12-28 20:25:38 +000074 struct Printer : public MachineFunctionPass {
Brian Gaekede420ae2003-07-23 20:25:08 +000075 /// Output stream on which we're printing assembly code.
Brian Gaeke92bdfe62003-07-23 18:37:06 +000076 ///
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000077 std::ostream &O;
Brian Gaekede420ae2003-07-23 20:25:08 +000078
79 /// Target machine description which we query for reg. names, data
80 /// layout, etc.
81 ///
82 TargetMachine &TM;
83
Brian Gaeked9fb37a2003-07-24 20:20:44 +000084 /// Name-mangler for global names.
85 ///
86 Mangler *Mang;
87
Brian Gaekede420ae2003-07-23 20:25:08 +000088 Printer(std::ostream &o, TargetMachine &tm) : O(o), TM(tm) { }
Brian Gaeke92bdfe62003-07-23 18:37:06 +000089
Brian Gaeke92bdfe62003-07-23 18:37:06 +000090 /// Cache of mangled name for current function. This is
91 /// recalculated at the beginning of each call to
92 /// runOnMachineFunction().
93 ///
Brian Gaeked7908f62003-06-27 00:00:48 +000094 std::string CurrentFnName;
Brian Gaeke92bdfe62003-07-23 18:37:06 +000095
Chris Lattnerf0eb7be2002-12-15 21:13:40 +000096 virtual const char *getPassName() const {
97 return "X86 Assembly Printer";
98 }
99
Chris Lattner30b2f722004-03-31 22:02:21 +0000100 void printImplUsesBefore(const TargetInstrDescriptor &Desc);
Chris Lattner26653832004-04-13 17:18:39 +0000101 bool printImplDefsBefore(const TargetInstrDescriptor &Desc);
John Criswell4ffff9e2004-04-08 20:31:47 +0000102 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
103 bool printImplDefsAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000104 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000105 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000106 void printMemReference(const MachineInstr *MI, unsigned Op);
107 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000108 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000109 bool doInitialization(Module &M);
110 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000111 void emitGlobalConstant(const Constant* CV);
112 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000113 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000114} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000115
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000116/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000117/// assembly code for a MachineFunction to the given output stream,
118/// using the given target machine description. This should work
119/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000120///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000121FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Brian Gaekede420ae2003-07-23 20:25:08 +0000122 return new Printer(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000123}
124
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000125/// toOctal - Convert the low order bits of X into an octal digit.
126///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000127static inline char toOctal(int X) {
128 return (X&7)+'0';
129}
130
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000131/// getAsCString - Return the specified array as a C compatible
132/// string, only if the predicate isStringCompatible is true.
133///
Chris Lattnerac662d12003-11-03 20:19:49 +0000134static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000135 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000136
Chris Lattnerac662d12003-11-03 20:19:49 +0000137 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000138 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000139 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000140
141 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000142 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000143 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000144 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000145 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000146 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000147 } else {
148 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000149 case '\b': O << "\\b"; break;
150 case '\f': O << "\\f"; break;
151 case '\n': O << "\\n"; break;
152 case '\r': O << "\\r"; break;
153 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000154 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000155 O << '\\';
156 O << toOctal(C >> 6);
157 O << toOctal(C >> 3);
158 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000159 break;
160 }
161 }
162 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000163 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000164}
165
Chris Lattnerac662d12003-11-03 20:19:49 +0000166// Print out the specified constant, without a storage class. Only the
167// constants valid in constant expressions can occur here.
168void Printer::emitConstantValueOnly(const Constant *CV) {
169 if (CV->isNullValue())
170 O << "0";
171 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
172 assert(CB == ConstantBool::True);
173 O << "1";
174 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000175 if (((CI->getValue() << 32) >> 32) == CI->getValue())
176 O << CI->getValue();
177 else
178 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000179 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
180 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000181 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000182 // This is a constant address for a global variable or function. Use the
183 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000184 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000185 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
186 const TargetData &TD = TM.getTargetData();
187 switch(CE->getOpcode()) {
188 case Instruction::GetElementPtr: {
189 // generate a symbolic expression for the byte address
190 const Constant *ptrVal = CE->getOperand(0);
191 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
192 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
193 O << "(";
194 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000195 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000196 } else {
197 emitConstantValueOnly(ptrVal);
198 }
199 break;
200 }
201 case Instruction::Cast: {
202 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000203 // that do not involve a change in value. This assertion is really gross,
204 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000205 Constant *Op = CE->getOperand(0);
206 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000207
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000208 // Remember, kids, pointers on x86 can be losslessly converted back and
209 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000210 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000211 && (Ty == Type::LongTy || Ty == Type::ULongTy
212 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000213 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000214 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
215 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000216 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
217 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 && "FIXME: Don't yet support this kind of constant cast expr");
219 O << "(";
220 emitConstantValueOnly(Op);
221 O << ")";
222 break;
223 }
224 case Instruction::Add:
225 O << "(";
226 emitConstantValueOnly(CE->getOperand(0));
227 O << ") + (";
228 emitConstantValueOnly(CE->getOperand(1));
229 O << ")";
230 break;
231 default:
232 assert(0 && "Unsupported operator!");
233 }
234 } else {
235 assert(0 && "Unknown constant value!");
236 }
237}
238
239// Print a constant value or values, with the appropriate storage class as a
240// prefix.
241void Printer::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000242 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000243
Chris Lattnerad200712003-09-09 16:23:36 +0000244 if (CV->isNullValue()) {
245 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000246 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000247 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000248 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000249 O << "\t.ascii\t";
250 printAsCString(O, CVA);
251 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000252 } else { // Not a string. Print the values in successive locations
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000253 const std::vector<Use> &constValues = CVA->getValues();
254 for (unsigned i=0; i < constValues.size(); i++)
Chris Lattnerac662d12003-11-03 20:19:49 +0000255 emitGlobalConstant(cast<Constant>(constValues[i].get()));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000256 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000257 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000258 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
259 // Print the fields in successive locations. Pad to align if needed!
260 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
261 const std::vector<Use>& constValues = CVS->getValues();
262 unsigned sizeSoFar = 0;
263 for (unsigned i=0, N = constValues.size(); i < N; i++) {
264 const Constant* field = cast<Constant>(constValues[i].get());
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000265
Chris Lattnerad200712003-09-09 16:23:36 +0000266 // Check if padding is needed and insert one or more 0s.
267 unsigned fieldSize = TD.getTypeSize(field->getType());
268 unsigned padSize = ((i == N-1? cvsLayout->StructSize
269 : cvsLayout->MemberOffsets[i+1])
270 - cvsLayout->MemberOffsets[i]) - fieldSize;
271 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000272
Chris Lattnerad200712003-09-09 16:23:36 +0000273 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000274 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000275
276 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000277 if (padSize)
278 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000279 }
Chris Lattnerad200712003-09-09 16:23:36 +0000280 assert(sizeSoFar == cvsLayout->StructSize &&
281 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000282 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000283 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
284 // FP Constants are printed as integer constants to avoid losing
285 // precision...
286 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000287 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000288 default: assert(0 && "Unknown floating point type!");
289 case Type::FloatTyID: {
290 union FU { // Abide by C TBAA rules
291 float FVal;
292 unsigned UVal;
293 } U;
294 U.FVal = Val;
295 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
296 return;
297 }
298 case Type::DoubleTyID: {
299 union DU { // Abide by C TBAA rules
300 double FVal;
301 uint64_t UVal;
302 } U;
303 U.FVal = Val;
304 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
305 return;
306 }
307 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000308 }
309
310 const Type *type = CV->getType();
311 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000312 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000313 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
314 O << ".byte";
315 break;
316 case Type::UShortTyID: case Type::ShortTyID:
317 O << ".word";
318 break;
319 case Type::FloatTyID: case Type::PointerTyID:
320 case Type::UIntTyID: case Type::IntTyID:
321 O << ".long";
322 break;
323 case Type::DoubleTyID:
324 case Type::ULongTyID: case Type::LongTyID:
325 O << ".quad";
326 break;
327 default:
328 assert (0 && "Can't handle printing this type of thing");
329 break;
330 }
331 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000332 emitConstantValueOnly(CV);
333 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000334}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000335
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000336/// printConstantPool - Print to the current output stream assembly
337/// representations of the constants in the constant pool MCP. This is
338/// used to print out constants which have been "spilled to memory" by
339/// the code generator.
340///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000341void Printer::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000342 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000343 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000344
Chris Lattnerb7089442003-01-13 00:35:03 +0000345 if (CP.empty()) return;
346
347 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
348 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000349 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000350 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000351 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
352 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000353 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000354 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000355}
356
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000357/// runOnMachineFunction - This uses the printMachineInstruction()
358/// method to print assembly for each instruction.
359///
Chris Lattner0285a332002-12-28 20:25:38 +0000360bool Printer::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000361 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000362 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000363 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000364
Chris Lattnerb7089442003-01-13 00:35:03 +0000365 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000366 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000367
Brian Gaeke6559bb92002-11-14 22:32:30 +0000368 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000369 O << "\t.text\n";
370 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000371 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000372 if (!EmitCygwin)
373 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000374 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000375
376 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000377 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
378 I != E; ++I) {
379 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000380 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000381 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000382 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000383 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000384 // Print the assembly for the instruction.
385 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000386 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000387 }
Chris Lattner0285a332002-12-28 20:25:38 +0000388 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000389
390 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000391 return false;
392}
393
Chris Lattner3d3067b2002-11-21 20:44:15 +0000394static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000395 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000396 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
397 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000398}
399
400static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000401 if (MI->getOperand(Op).isFrameIndex()) return true;
402 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000403 return Op+4 <= MI->getNumOperands() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000404 MI->getOperand(Op ).isRegister() &&isScale(MI->getOperand(Op+1)) &&
405 MI->getOperand(Op+2).isRegister() &&MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000406}
407
Brian Gaeke2a098772003-08-11 19:05:46 +0000408
409
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000410void Printer::printOp(const MachineOperand &MO,
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000411 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000412 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000413 switch (MO.getType()) {
414 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000415 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000416 O << "<" << V->getName() << ">";
417 return;
418 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000419 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000420 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000421 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000422 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000423 O << "%" << RI.get(MO.getReg()).Name;
424 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000425 O << "%reg" << MO.getReg();
426 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000427
428 case MachineOperand::MO_SignExtendedImmed:
429 case MachineOperand::MO_UnextendedImmed:
430 O << (int)MO.getImmedValue();
431 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000432 case MachineOperand::MO_MachineBasicBlock: {
433 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
434 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
435 << "_" << MBBOp->getNumber () << "\t# "
436 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000437 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000438 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000439 case MachineOperand::MO_PCRelativeDisp:
440 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
441 abort ();
442 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000443 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000444 if (!elideOffsetKeyword)
445 O << "OFFSET ";
446 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000447 return;
448 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000449 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000450 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000451 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000452 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000453 }
454}
455
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000456static const char* const sizePtr(const TargetInstrDescriptor &Desc) {
457 switch (Desc.TSFlags & X86II::MemMask) {
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000458 default: assert(0 && "Unknown arg size!");
Alkis Evlogimenos5ab29b52004-02-28 22:02:05 +0000459 case X86II::Mem8: return "BYTE PTR";
460 case X86II::Mem16: return "WORD PTR";
461 case X86II::Mem32: return "DWORD PTR";
462 case X86II::Mem64: return "QWORD PTR";
463 case X86II::Mem80: return "XWORD PTR";
Brian Gaeke86764d72002-12-05 08:30:40 +0000464 }
465}
466
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000467void Printer::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000468 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000469
470 if (MI->getOperand(Op).isFrameIndex()) {
471 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
472 if (MI->getOperand(Op+3).getImmedValue())
473 O << " + " << MI->getOperand(Op+3).getImmedValue();
474 O << "]";
475 return;
476 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000477 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000478 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000479 if (MI->getOperand(Op+3).getImmedValue())
480 O << " + " << MI->getOperand(Op+3).getImmedValue();
481 O << "]";
482 return;
483 }
484
Chris Lattner3d3067b2002-11-21 20:44:15 +0000485 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000486 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000487 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000488 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000489
490 O << "[";
491 bool NeedPlus = false;
492 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000493 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000494 NeedPlus = true;
495 }
496
497 if (IndexReg.getReg()) {
498 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000499 if (ScaleVal != 1)
500 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000501 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000502 NeedPlus = true;
503 }
504
Chris Lattner0285a332002-12-28 20:25:38 +0000505 if (DispVal) {
506 if (NeedPlus)
507 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000508 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000509 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000510 O << " - ";
511 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000512 }
513 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000514 }
515 O << "]";
516}
517
Chris Lattner30b2f722004-03-31 22:02:21 +0000518
519/// printImplUsesBefore - Emit the implicit-use registers for the instruction
520/// described by DESC, if its PrintImplUsesBefore flag is set.
Brian Gaeke2a098772003-08-11 19:05:46 +0000521///
Chris Lattner30b2f722004-03-31 22:02:21 +0000522void Printer::printImplUsesBefore(const TargetInstrDescriptor &Desc) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000523 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner30b2f722004-03-31 22:02:21 +0000524 if (Desc.TSFlags & X86II::PrintImplUsesBefore) {
525 for (const unsigned *p = Desc.ImplicitUses; *p; ++p) {
526 // Bug Workaround: See note in Printer::doInitialization about %.
527 O << "%" << RI.get(*p).Name << ", ";
528 }
529 }
530}
531
Chris Lattner26653832004-04-13 17:18:39 +0000532/// printImplDefsBefore - Emit the implicit-def registers for the instruction
533/// described by DESC, if its PrintImplUsesBefore flag is set. Return true if
534/// we printed any registers.
535///
536bool Printer::printImplDefsBefore(const TargetInstrDescriptor &Desc) {
537 bool Printed = false;
538 const MRegisterInfo &RI = *TM.getRegisterInfo();
539 if (Desc.TSFlags & X86II::PrintImplDefsBefore) {
540 const unsigned *p = Desc.ImplicitDefs;
541 if (*p) {
542 O << (Printed ? ", %" : "%") << RI.get (*p).Name;
543 Printed = true;
544 ++p;
545 }
546 while (*p) {
547 // Bug Workaround: See note in Printer::doInitialization about %.
548 O << ", %" << RI.get(*p).Name;
549 ++p;
550 }
551 }
552 return Printed;
553}
554
555
Chris Lattner30b2f722004-03-31 22:02:21 +0000556/// printImplUsesAfter - Emit the implicit-use registers for the instruction
557/// described by DESC, if its PrintImplUsesAfter flag is set.
558///
John Criswell4ffff9e2004-04-08 20:31:47 +0000559/// Inputs:
560/// Comma - List of registers will need a leading comma.
561/// Desc - Description of the Instruction.
562///
563/// Return value:
564/// true - Emitted one or more registers.
565/// false - Emitted no registers.
566///
567bool Printer::printImplUsesAfter(const TargetInstrDescriptor &Desc,
568 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000569 const MRegisterInfo &RI = *TM.getRegisterInfo();
570 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000571 bool emitted = false;
572 const unsigned *p = Desc.ImplicitUses;
573 if (*p) {
574 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
575 emitted = true;
576 ++p;
577 }
578 while (*p) {
Brian Gaeke2a098772003-08-11 19:05:46 +0000579 // Bug Workaround: See note in Printer::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000580 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000581 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000582 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000583 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000584 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000585 return false;
586}
587
588/// printImplDefsAfter - Emit the implicit-definition registers for the
589/// instruction described by DESC, if its PrintImplDefsAfter flag is set.
590///
591/// Inputs:
592/// Comma - List of registers will need a leading comma.
593/// Desc - Description of the Instruction
594///
595/// Return value:
596/// true - Emitted one or more registers.
597/// false - Emitted no registers.
598///
599bool Printer::printImplDefsAfter(const TargetInstrDescriptor &Desc,
600 const bool Comma = true) {
601 const MRegisterInfo &RI = *TM.getRegisterInfo();
602 if (Desc.TSFlags & X86II::PrintImplDefsAfter) {
603 bool emitted = false;
604 const unsigned *p = Desc.ImplicitDefs;
605 if (*p) {
606 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
607 emitted = true;
608 ++p;
609 }
610 while (*p) {
611 // Bug Workaround: See note in Printer::doInitialization about %.
612 O << ", %" << RI.get(*p).Name;
613 ++p;
614 }
615 return emitted;
616 }
617 return false;
Brian Gaeke2a098772003-08-11 19:05:46 +0000618}
619
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000620/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000621/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000622///
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000623void Printer::printMachineInstruction(const MachineInstr *MI) {
Chris Lattnerf9f60882002-11-18 06:56:51 +0000624 unsigned Opcode = MI->getOpcode();
Chris Lattnerd029cd22004-06-02 05:55:25 +0000625 const TargetInstrInfo &TII = *TM.getInstrInfo();
Brian Gaeked7908f62003-06-27 00:00:48 +0000626 const TargetInstrDescriptor &Desc = TII.get(Opcode);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000627
Brian Gaeke2c9b9132003-10-06 15:41:21 +0000628 ++EmittedInsts;
Chris Lattnereca1f632002-12-25 05:09:01 +0000629 switch (Desc.TSFlags & X86II::FormMask) {
630 case X86II::Pseudo:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000631 // Print pseudo-instructions as comments; either they should have been
632 // turned into real instructions by now, or they don't need to be
633 // seen by the assembler (e.g., IMPLICIT_USEs.)
634 O << "# ";
Chris Lattnereca1f632002-12-25 05:09:01 +0000635 if (Opcode == X86::PHI) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000636 printOp(MI->getOperand(0));
Chris Lattnereca1f632002-12-25 05:09:01 +0000637 O << " = phi ";
638 for (unsigned i = 1, e = MI->getNumOperands(); i != e; i+=2) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000639 if (i != 1) O << ", ";
640 O << "[";
641 printOp(MI->getOperand(i));
642 O << ", ";
643 printOp(MI->getOperand(i+1));
644 O << "]";
Chris Lattnereca1f632002-12-25 05:09:01 +0000645 }
646 } else {
647 unsigned i = 0;
Alkis Evlogimenos4d7af652003-12-14 13:24:17 +0000648 if (MI->getNumOperands() && MI->getOperand(0).isDef()) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000649 printOp(MI->getOperand(0));
650 O << " = ";
651 ++i;
Chris Lattnereca1f632002-12-25 05:09:01 +0000652 }
Brian Gaeked7908f62003-06-27 00:00:48 +0000653 O << TII.getName(MI->getOpcode());
Chris Lattnereca1f632002-12-25 05:09:01 +0000654
655 for (unsigned e = MI->getNumOperands(); i != e; ++i) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000656 O << " ";
657 if (MI->getOperand(i).isDef()) O << "*";
658 printOp(MI->getOperand(i));
659 if (MI->getOperand(i).isDef()) O << "*";
Chris Lattnereca1f632002-12-25 05:09:01 +0000660 }
Chris Lattner3faae2d2002-12-13 09:59:26 +0000661 }
662 O << "\n";
663 return;
Chris Lattner3faae2d2002-12-13 09:59:26 +0000664
Chris Lattnerf9f60882002-11-18 06:56:51 +0000665 case X86II::RawFrm:
John Criswell4ffff9e2004-04-08 20:31:47 +0000666 {
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000667 // The accepted forms of Raw instructions are:
668 // 1. nop - No operand required
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000669 // 2. jmp foo - MachineBasicBlock operand
Chris Lattnerb7089442003-01-13 00:35:03 +0000670 // 3. call bar - GlobalAddress Operand or External Symbol Operand
Chris Lattner26653832004-04-13 17:18:39 +0000671 // 4. in AL, imm - Immediate operand
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000672 //
673 assert(MI->getNumOperands() == 0 ||
Chris Lattnerb7089442003-01-13 00:35:03 +0000674 (MI->getNumOperands() == 1 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000675 (MI->getOperand(0).isMachineBasicBlock() ||
676 MI->getOperand(0).isGlobalAddress() ||
677 MI->getOperand(0).isExternalSymbol() ||
Chris Lattner26653832004-04-13 17:18:39 +0000678 MI->getOperand(0).isImmediate())) &&
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000679 "Illegal raw instruction!");
Brian Gaeked7908f62003-06-27 00:00:48 +0000680 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerf9f60882002-11-18 06:56:51 +0000681
Chris Lattner26653832004-04-13 17:18:39 +0000682 bool LeadingComma = printImplDefsBefore(Desc);
683
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000684 if (MI->getNumOperands() == 1) {
Chris Lattner26653832004-04-13 17:18:39 +0000685 if (LeadingComma) O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000686 printOp(MI->getOperand(0), true); // Don't print "OFFSET"...
John Criswell4ffff9e2004-04-08 20:31:47 +0000687 LeadingComma = true;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000688 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000689 LeadingComma = printImplDefsAfter(Desc, LeadingComma) || LeadingComma;
690 printImplUsesAfter(Desc, LeadingComma);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000691 O << "\n";
692 return;
John Criswell4ffff9e2004-04-08 20:31:47 +0000693 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000694
Chris Lattner77875d82002-11-21 02:00:20 +0000695 case X86II::AddRegFrm: {
696 // There are currently two forms of acceptable AddRegFrm instructions.
697 // Either the instruction JUST takes a single register (like inc, dec, etc),
698 // or it takes a register and an immediate of the same size as the register
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000699 // (move immediate f.e.). Note that this immediate value might be stored as
700 // an LLVM value, to represent, for example, loading the address of a global
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000701 // into a register. The initial register might be duplicated if this is a
702 // M_2_ADDR_REG instruction
Chris Lattner77875d82002-11-21 02:00:20 +0000703 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000704 assert(MI->getOperand(0).isRegister() &&
Chris Lattner77875d82002-11-21 02:00:20 +0000705 (MI->getNumOperands() == 1 ||
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000706 (MI->getNumOperands() == 2 &&
Chris Lattner6d669442002-12-04 17:28:40 +0000707 (MI->getOperand(1).getVRegValueOrNull() ||
Chris Lattnerfacc9fb2002-12-23 23:46:00 +0000708 MI->getOperand(1).isImmediate() ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000709 MI->getOperand(1).isRegister() ||
710 MI->getOperand(1).isGlobalAddress() ||
711 MI->getOperand(1).isExternalSymbol()))) &&
Chris Lattner77875d82002-11-21 02:00:20 +0000712 "Illegal form for AddRegFrm instruction!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000713
Chris Lattner77875d82002-11-21 02:00:20 +0000714 unsigned Reg = MI->getOperand(0).getReg();
Chris Lattner77875d82002-11-21 02:00:20 +0000715
Chris Lattnerb009c002004-02-11 19:26:28 +0000716 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattner30b2f722004-03-31 22:02:21 +0000717
718 printImplUsesBefore(Desc); // fcmov*
719
Brian Gaekede420ae2003-07-23 20:25:08 +0000720 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000721 if (MI->getNumOperands() == 2 &&
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000722 (!MI->getOperand(1).isRegister() ||
723 MI->getOperand(1).getVRegValueOrNull() ||
724 MI->getOperand(1).isGlobalAddress() ||
725 MI->getOperand(1).isExternalSymbol())) {
Chris Lattner77875d82002-11-21 02:00:20 +0000726 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000727 printOp(MI->getOperand(1));
Chris Lattner77875d82002-11-21 02:00:20 +0000728 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000729 printImplUsesAfter(Desc);
Chris Lattner77875d82002-11-21 02:00:20 +0000730 O << "\n";
731 return;
732 }
Chris Lattner233ad712002-11-21 01:33:44 +0000733 case X86II::MRMDestReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000734 // There are three forms of MRMDestReg instructions, those with 2
735 // or 3 operands:
Chris Lattnerb7089442003-01-13 00:35:03 +0000736 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000737 // 2 Operands: this is for things like mov that do not read a
738 // second input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000739 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000740 // 2 Operands: two address instructions which def&use the first
741 // argument and use the second as input.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000742 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000743 // 3 Operands: in this form, two address instructions are the same
744 // as in 2 but have a constant argument as well.
Chris Lattnerf9f60882002-11-18 06:56:51 +0000745 //
Brian Gaeked7908f62003-06-27 00:00:48 +0000746 bool isTwoAddr = TII.isTwoAddrInstr(Opcode);
Chris Lattnerd9096832002-12-15 08:01:39 +0000747 assert(MI->getOperand(0).isRegister() &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000748 (MI->getNumOperands() == 2 ||
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000749 (MI->getNumOperands() == 3 && MI->getOperand(2).isImmediate()))
Misha Brukmane1f0d812002-11-20 18:56:41 +0000750 && "Bad format for MRMDestReg!");
Chris Lattnerf9f60882002-11-18 06:56:51 +0000751
Chris Lattnerb009c002004-02-11 19:26:28 +0000752 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000753 printOp(MI->getOperand(0));
Chris Lattnerf9f60882002-11-18 06:56:51 +0000754 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000755 printOp(MI->getOperand(1));
756 if (MI->getNumOperands() == 3) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000757 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000758 printOp(MI->getOperand(2));
Chris Lattnerb7089442003-01-13 00:35:03 +0000759 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000760 printImplUsesAfter(Desc);
Chris Lattnerf9f60882002-11-18 06:56:51 +0000761 O << "\n";
762 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000763 }
Chris Lattner18042332002-11-21 21:03:39 +0000764
765 case X86II::MRMDestMem: {
766 // These instructions are the same as MRMDestReg, but instead of having a
767 // register reference for the mod/rm field, it's a memory reference.
768 //
Chris Lattner6e173a02004-02-17 06:16:44 +0000769 assert(isMem(MI, 0) &&
770 (MI->getNumOperands() == 4+1 ||
771 (MI->getNumOperands() == 4+2 && MI->getOperand(5).isImmediate()))
772 && "Bad format for MRMDestMem!");
Chris Lattner18042332002-11-21 21:03:39 +0000773
Chris Lattnerb009c002004-02-11 19:26:28 +0000774 O << TII.getName(MI->getOpcode()) << " " << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000775 printMemReference(MI, 0);
Chris Lattner18042332002-11-21 21:03:39 +0000776 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000777 printOp(MI->getOperand(4));
Chris Lattner6e173a02004-02-17 06:16:44 +0000778 if (MI->getNumOperands() == 4+2) {
779 O << ", ";
780 printOp(MI->getOperand(5));
781 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000782 printImplUsesAfter(Desc);
Chris Lattner18042332002-11-21 21:03:39 +0000783 O << "\n";
784 return;
785 }
786
Chris Lattner233ad712002-11-21 01:33:44 +0000787 case X86II::MRMSrcReg: {
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000788 // There are three forms that are acceptable for MRMSrcReg
789 // instructions, those with 2 or 3 operands:
Chris Lattner644e1ab2002-11-21 00:30:01 +0000790 //
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000791 // 2 Operands: this is for things like mov that do not read a
792 // second input.
793 //
794 // 2 Operands: in this form, the last register is the ModR/M
795 // input. The first operand is a def&use. This is for things
796 // like: add r32, r/m32
Chris Lattner644e1ab2002-11-21 00:30:01 +0000797 //
Alkis Evlogimenosf0339392004-02-04 17:21:04 +0000798 // 3 Operands: in this form, we can have 'INST R1, R2, imm', which is used
Chris Lattner55b54812004-02-17 04:26:43 +0000799 // for instructions like the IMULrri instructions.
Chris Lattnerc01d1232003-10-20 03:42:58 +0000800 //
Chris Lattner644e1ab2002-11-21 00:30:01 +0000801 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000802 assert(MI->getOperand(0).isRegister() &&
803 MI->getOperand(1).isRegister() &&
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000804 (MI->getNumOperands() == 2 ||
805 (MI->getNumOperands() == 3 &&
806 (MI->getOperand(2).isImmediate())))
Chris Lattnerb7089442003-01-13 00:35:03 +0000807 && "Bad format for MRMSrcReg!");
Chris Lattner644e1ab2002-11-21 00:30:01 +0000808
Chris Lattnerb009c002004-02-11 19:26:28 +0000809 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000810 printOp(MI->getOperand(0));
Chris Lattner644e1ab2002-11-21 00:30:01 +0000811 O << ", ";
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000812 printOp(MI->getOperand(1));
813 if (MI->getNumOperands() == 3) {
814 O << ", ";
815 printOp(MI->getOperand(2));
816 }
Chris Lattner644e1ab2002-11-21 00:30:01 +0000817 O << "\n";
818 return;
Chris Lattner233ad712002-11-21 01:33:44 +0000819 }
Chris Lattner675dd2c2002-11-21 17:09:01 +0000820
Chris Lattner3d3067b2002-11-21 20:44:15 +0000821 case X86II::MRMSrcMem: {
822 // These instructions are the same as MRMSrcReg, but instead of having a
823 // register reference for the mod/rm field, it's a memory reference.
Chris Lattner18042332002-11-21 21:03:39 +0000824 //
Chris Lattnerd9096832002-12-15 08:01:39 +0000825 assert(MI->getOperand(0).isRegister() &&
Misha Brukmanc1f901c2004-06-29 19:43:20 +0000826 ((MI->getNumOperands() == 1+4 && isMem(MI, 1)) ||
827 (MI->getNumOperands() == 2+4 && MI->getOperand(5).isImmediate() &&
828 isMem(MI, 1)))
Alkis Evlogimenos14be6402004-02-04 22:17:40 +0000829 && "Bad format for MRMSrcMem!");
Chris Lattnerb009c002004-02-11 19:26:28 +0000830 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000831 printOp(MI->getOperand(0));
Chris Lattnerb7089442003-01-13 00:35:03 +0000832 O << ", " << sizePtr(Desc) << " ";
Chris Lattner5b672522004-02-17 07:40:44 +0000833 printMemReference(MI, 1);
834 if (MI->getNumOperands() == 2+4) {
835 O << ", ";
836 printOp(MI->getOperand(5));
837 }
Chris Lattner3d3067b2002-11-21 20:44:15 +0000838 O << "\n";
839 return;
840 }
841
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000842 case X86II::MRM0r: case X86II::MRM1r:
843 case X86II::MRM2r: case X86II::MRM3r:
844 case X86II::MRM4r: case X86II::MRM5r:
845 case X86II::MRM6r: case X86II::MRM7r: {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000846 // In this form, the following are valid formats:
847 // 1. sete r
Chris Lattner1d53ce42002-11-21 23:30:00 +0000848 // 2. cmp reg, immediate
Chris Lattner675dd2c2002-11-21 17:09:01 +0000849 // 2. shl rdest, rinput <implicit CL or 1>
850 // 3. sbb rdest, rinput, immediate [rdest = rinput]
851 //
852 assert(MI->getNumOperands() > 0 && MI->getNumOperands() < 4 &&
Chris Lattnerd9096832002-12-15 08:01:39 +0000853 MI->getOperand(0).isRegister() && "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000854 assert((MI->getNumOperands() != 2 ||
Chris Lattnerd9096832002-12-15 08:01:39 +0000855 MI->getOperand(1).isRegister() || MI->getOperand(1).isImmediate())&&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000856 "Bad MRMSxR format!");
Chris Lattner1d53ce42002-11-21 23:30:00 +0000857 assert((MI->getNumOperands() < 3 ||
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000858 (MI->getOperand(1).isRegister() && MI->getOperand(2).isImmediate())) &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000859 "Bad MRMSxR format!");
860
Chris Lattnerd9096832002-12-15 08:01:39 +0000861 if (MI->getNumOperands() > 1 && MI->getOperand(1).isRegister() &&
Chris Lattner675dd2c2002-11-21 17:09:01 +0000862 MI->getOperand(0).getReg() != MI->getOperand(1).getReg())
863 O << "**";
864
Chris Lattnerb009c002004-02-11 19:26:28 +0000865 O << TII.getName(MI->getOpcode()) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000866 printOp(MI->getOperand(0));
Chris Lattnerd9096832002-12-15 08:01:39 +0000867 if (MI->getOperand(MI->getNumOperands()-1).isImmediate()) {
Chris Lattner675dd2c2002-11-21 17:09:01 +0000868 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000869 printOp(MI->getOperand(MI->getNumOperands()-1));
Chris Lattner675dd2c2002-11-21 17:09:01 +0000870 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000871 printImplUsesAfter(Desc);
Chris Lattner675dd2c2002-11-21 17:09:01 +0000872 O << "\n";
873
874 return;
875 }
876
Alkis Evlogimenos169584e2004-02-27 18:55:12 +0000877 case X86II::MRM0m: case X86II::MRM1m:
878 case X86II::MRM2m: case X86II::MRM3m:
879 case X86II::MRM4m: case X86II::MRM5m:
880 case X86II::MRM6m: case X86II::MRM7m: {
Chris Lattnerb7089442003-01-13 00:35:03 +0000881 // In this form, the following are valid formats:
882 // 1. sete [m]
883 // 2. cmp [m], immediate
884 // 2. shl [m], rinput <implicit CL or 1>
885 // 3. sbb [m], immediate
886 //
887 assert(MI->getNumOperands() >= 4 && MI->getNumOperands() <= 5 &&
888 isMem(MI, 0) && "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000889 assert((MI->getNumOperands() != 5 ||
890 (MI->getOperand(4).isImmediate() ||
891 MI->getOperand(4).isGlobalAddress())) &&
Chris Lattnerb7089442003-01-13 00:35:03 +0000892 "Bad MRMSxM format!");
Chris Lattnerf2d29252003-12-01 05:13:56 +0000893
894 const MachineOperand &Op3 = MI->getOperand(3);
895
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000896 // gas bugs:
897 //
898 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]"
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000899 // is misassembled by gas in intel_syntax mode as its 32-bit
900 // equivalent "fstp DWORD PTR [...]". Workaround: Output the raw
901 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000902 //
903 // The 80-bit FP load instruction "fld XWORD PTR [...]" is
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000904 // misassembled by gas in intel_syntax mode as its 32-bit
905 // equivalent "fld DWORD PTR [...]". Workaround: Output the raw
906 // opcode bytes instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000907 //
908 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000909 // invalid opcode, saying "64 bit operations are only supported in
910 // 64 bit modes." libopcodes disassembles it as "fild DWORD PTR
911 // [...]", which is wrong. Workaround: Output the raw opcode bytes
912 // instead of the instruction.
Alkis Evlogimenos03090662004-03-09 03:35:34 +0000913 //
914 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an
915 // invalid opcode, saying "64 bit operations are only supported in
916 // 64 bit modes." libopcodes disassembles it as "fistpll DWORD PTR
917 // [...]", which is wrong. Workaround: Output the raw opcode bytes
918 // instead of the instruction.
919 if (MI->getOpcode() == X86::FSTP80m ||
920 MI->getOpcode() == X86::FLD80m ||
921 MI->getOpcode() == X86::FILD64m ||
922 MI->getOpcode() == X86::FISTP64m) {
923 GasBugWorkaroundEmitter gwe(O);
Chris Lattnerd029cd22004-06-02 05:55:25 +0000924 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
Brian Gaeke1aa476e2003-07-11 18:18:35 +0000925 }
Chris Lattnerf2d29252003-12-01 05:13:56 +0000926
Chris Lattnerb009c002004-02-11 19:26:28 +0000927 O << TII.getName(MI->getOpcode()) << " ";
Chris Lattnerb7089442003-01-13 00:35:03 +0000928 O << sizePtr(Desc) << " ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000929 printMemReference(MI, 0);
Chris Lattnerb7089442003-01-13 00:35:03 +0000930 if (MI->getNumOperands() == 5) {
931 O << ", ";
Brian Gaekede420ae2003-07-23 20:25:08 +0000932 printOp(MI->getOperand(4));
Chris Lattnerb7089442003-01-13 00:35:03 +0000933 }
Chris Lattner30b2f722004-03-31 22:02:21 +0000934 printImplUsesAfter(Desc);
Chris Lattnerb7089442003-01-13 00:35:03 +0000935 O << "\n";
936 return;
937 }
Chris Lattnerf9f60882002-11-18 06:56:51 +0000938 default:
Tanya Lattnerb1407622004-06-25 00:13:11 +0000939 O << "\tUNKNOWN FORM:\t\t-"; MI->print(O, &TM); break;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000940 }
Chris Lattner72614082002-10-25 22:55:53 +0000941}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000942
Chris Lattner93c1afa2003-08-11 19:35:26 +0000943bool Printer::doInitialization(Module &M) {
944 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000945 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000946 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
947 // instruction as a reference to the register named sp, and if you try to
948 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
949 // before being looked up in the symbol table. This creates spurious
950 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
951 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000952 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000953 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000954 return false; // success
955}
956
Chris Lattnerad200712003-09-09 16:23:36 +0000957// SwitchSection - Switch to the specified section of the executable if we are
958// not already in it!
959//
960static void SwitchSection(std::ostream &OS, std::string &CurSection,
961 const char *NewSection) {
962 if (CurSection != NewSection) {
963 CurSection = NewSection;
964 if (!CurSection.empty())
965 OS << "\t" << NewSection << "\n";
966 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000967}
968
Chris Lattnerad200712003-09-09 16:23:36 +0000969bool Printer::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000970 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000971 std::string CurSection;
972
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000973 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000974 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
975 if (I->hasInitializer()) { // External global require no code
976 O << "\n\n";
977 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000978 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000979 unsigned Size = TD.getTypeSize(C->getType());
980 unsigned Align = TD.getTypeAlignment(C->getType());
981
982 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000983 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
984 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000985 SwitchSection(O, CurSection, ".data");
986 if (I->hasInternalLinkage())
987 O << "\t.local " << name << "\n";
988
989 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000990 << "," << (unsigned)TD.getTypeAlignment(C->getType());
991 O << "\t\t# ";
992 WriteAsOperand(O, I, true, true, &M);
993 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000994 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000995 switch (I->getLinkage()) {
996 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000997 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000998 // Nonnull linkonce -> weak
999 O << "\t.weak " << name << "\n";
1000 SwitchSection(O, CurSection, "");
1001 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
1002 break;
1003
1004 case GlobalValue::AppendingLinkage:
1005 // FIXME: appending linkage variables should go into a section of
1006 // their name or something. For now, just emit them as external.
1007 case GlobalValue::ExternalLinkage:
1008 // If external or appending, declare as a global symbol
1009 O << "\t.globl " << name << "\n";
1010 // FALL THROUGH
1011 case GlobalValue::InternalLinkage:
1012 if (C->isNullValue())
1013 SwitchSection(O, CurSection, ".bss");
1014 else
1015 SwitchSection(O, CurSection, ".data");
1016 break;
1017 }
1018
1019 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001020 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +00001021 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +00001022 O << name << ":\t\t\t\t# ";
1023 WriteAsOperand(O, I, true, true, &M);
1024 O << " = ";
1025 WriteAsOperand(O, C, false, false, &M);
1026 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +00001027 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +00001028 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +00001029 }
Chris Lattnerad200712003-09-09 16:23:36 +00001030
Brian Gaeked9fb37a2003-07-24 20:20:44 +00001031 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +00001032 return false; // success
1033}