blob: cf7c01f01b119f78c88b5644945276698e1c8d6c [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 Lattnerb12ee502004-08-01 07:43:46 +000028#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerd59414f2003-08-11 20:06:16 +000029#include "llvm/Target/TargetMachine.h"
Brian Gaeked9fb37a2003-07-24 20:20:44 +000030#include "llvm/Support/Mangler.h"
Brian Gaeke2c9b9132003-10-06 15:41:21 +000031#include "Support/Statistic.h"
Chris Lattnere0121322003-08-03 23:37:09 +000032#include "Support/StringExtras.h"
Chris Lattner93c1afa2003-08-11 19:35:26 +000033#include "Support/CommandLine.h"
Chris Lattner300d0ed2004-02-14 06:00:36 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattnerb4f68ed2002-10-29 22:37:54 +000036namespace {
Brian Gaeke2c9b9132003-10-06 15:41:21 +000037 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
38
Chris Lattner93c1afa2003-08-11 19:35:26 +000039 // FIXME: This should be automatically picked up by autoconf from the C
40 // frontend
41 cl::opt<bool> EmitCygwin("enable-cygwin-compatible-output", cl::Hidden,
42 cl::desc("Emit X86 assembly code suitable for consumption by cygwin"));
43
Alkis Evlogimenos03090662004-03-09 03:35:34 +000044 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukman8606aea2004-07-26 18:48:58 +000045 GasBugWorkaroundEmitter(std::ostream& o)
46 : O(o), OldFlags(O.flags()), firstByte(true) {
47 O << std::hex;
48 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000049
Misha Brukman8606aea2004-07-26 18:48:58 +000050 ~GasBugWorkaroundEmitter() {
51 O.flags(OldFlags);
Misha Brukman8606aea2004-07-26 18:48:58 +000052 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000053
Misha Brukman8606aea2004-07-26 18:48:58 +000054 virtual void emitByte(unsigned char B) {
55 if (!firstByte) O << "\n\t";
56 firstByte = false;
57 O << ".byte 0x" << (unsigned) B;
58 }
Alkis Evlogimenos03090662004-03-09 03:35:34 +000059
Misha Brukman8606aea2004-07-26 18:48:58 +000060 // These should never be called
61 virtual void emitWord(unsigned W) { assert(0); }
62 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:
Misha Brukman8606aea2004-07-26 18:48:58 +000069 std::ostream& O;
70 std::ios::fmtflags OldFlags;
71 bool firstByte;
Alkis Evlogimenos03090662004-03-09 03:35:34 +000072 };
73
Chris Lattner3fa861a2004-08-01 06:02:08 +000074 struct X86AsmPrinter : 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
Chris Lattner3fa861a2004-08-01 06:02:08 +000088 X86AsmPrinter(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 Lattner3fa861a2004-08-01 06:02:08 +0000100 /// printInstruction - This method is automatically generated by tablegen
101 /// from the instruction set description. This method returns true if the
102 /// machine instruction was sufficiently described to print it, otherwise it
103 /// returns false.
104 bool printInstruction(const MachineInstr *MI);
105
Chris Lattnerb12ee502004-08-01 07:43:46 +0000106 // This method is used by the tablegen'erated instruction printer.
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000107 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT) {
108 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner25369cf2004-08-01 08:12:41 +0000109 if (MO.getType() == MachineOperand::MO_MachineRegister) {
110 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
111 // Bug Workaround: See note in Printer::doInitialization about %.
112 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
113 } else {
114 printOp(MO);
115 }
Chris Lattnerb12ee502004-08-01 07:43:46 +0000116 }
117
Chris Lattnere4ead0c2004-08-11 06:59:12 +0000118 void printCallOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT) {
119 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
120 }
121
Chris Lattner66fa1dc2004-08-11 02:25:00 +0000122 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
123 MVT::ValueType VT) {
124 switch (VT) {
125 default: assert(0 && "Unknown arg size!");
126 case MVT::i8: O << "BYTE PTR "; break;
127 case MVT::i16: O << "WORD PTR "; break;
128 case MVT::i32:
129 case MVT::f32: O << "DWORD PTR "; break;
130 case MVT::i64:
131 case MVT::f64: O << "QWORD PTR "; break;
132 case MVT::f80: O << "XWORD PTR "; break;
133 }
134 printMemReference(MI, OpNo);
135 }
136
John Criswell4ffff9e2004-04-08 20:31:47 +0000137 bool printImplUsesAfter(const TargetInstrDescriptor &Desc, const bool LC);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000138 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000139 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000140 void printMemReference(const MachineInstr *MI, unsigned Op);
141 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000142 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000143 bool doInitialization(Module &M);
144 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000145 void emitGlobalConstant(const Constant* CV);
146 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000147 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000148} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000149
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000150/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000151/// assembly code for a MachineFunction to the given output stream,
152/// using the given target machine description. This should work
153/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000154///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000155FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000156 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000157}
158
Chris Lattner3fa861a2004-08-01 06:02:08 +0000159
160// Include the auto-generated portion of the assembly writer.
161#include "X86GenAsmWriter.inc"
162
163
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000164/// toOctal - Convert the low order bits of X into an octal digit.
165///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000166static inline char toOctal(int X) {
167 return (X&7)+'0';
168}
169
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000170/// getAsCString - Return the specified array as a C compatible
171/// string, only if the predicate isStringCompatible is true.
172///
Chris Lattnerac662d12003-11-03 20:19:49 +0000173static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000174 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000175
Chris Lattnerac662d12003-11-03 20:19:49 +0000176 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000177 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000178 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000179
180 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000181 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000182 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000183 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000184 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000185 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000186 } else {
187 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000188 case '\b': O << "\\b"; break;
189 case '\f': O << "\\f"; break;
190 case '\n': O << "\\n"; break;
191 case '\r': O << "\\r"; break;
192 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000193 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000194 O << '\\';
195 O << toOctal(C >> 6);
196 O << toOctal(C >> 3);
197 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000198 break;
199 }
200 }
201 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000202 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000203}
204
Chris Lattnerac662d12003-11-03 20:19:49 +0000205// Print out the specified constant, without a storage class. Only the
206// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000207void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000208 if (CV->isNullValue())
209 O << "0";
210 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
211 assert(CB == ConstantBool::True);
212 O << "1";
213 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000214 if (((CI->getValue() << 32) >> 32) == CI->getValue())
215 O << CI->getValue();
216 else
217 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000218 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
219 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000220 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000221 // This is a constant address for a global variable or function. Use the
222 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000223 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000224 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
225 const TargetData &TD = TM.getTargetData();
226 switch(CE->getOpcode()) {
227 case Instruction::GetElementPtr: {
228 // generate a symbolic expression for the byte address
229 const Constant *ptrVal = CE->getOperand(0);
230 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
231 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
232 O << "(";
233 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000234 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000235 } else {
236 emitConstantValueOnly(ptrVal);
237 }
238 break;
239 }
240 case Instruction::Cast: {
241 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000242 // that do not involve a change in value. This assertion is really gross,
243 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000244 Constant *Op = CE->getOperand(0);
245 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000246
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000247 // Remember, kids, pointers on x86 can be losslessly converted back and
248 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000249 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000250 && (Ty == Type::LongTy || Ty == Type::ULongTy
251 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000252 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000253 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
254 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000255 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
256 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000257 && "FIXME: Don't yet support this kind of constant cast expr");
258 O << "(";
259 emitConstantValueOnly(Op);
260 O << ")";
261 break;
262 }
263 case Instruction::Add:
264 O << "(";
265 emitConstantValueOnly(CE->getOperand(0));
266 O << ") + (";
267 emitConstantValueOnly(CE->getOperand(1));
268 O << ")";
269 break;
270 default:
271 assert(0 && "Unsupported operator!");
272 }
273 } else {
274 assert(0 && "Unknown constant value!");
275 }
276}
277
278// Print a constant value or values, with the appropriate storage class as a
279// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000280void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000281 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000282
Chris Lattnerad200712003-09-09 16:23:36 +0000283 if (CV->isNullValue()) {
284 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000285 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000286 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000287 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000288 O << "\t.ascii\t";
289 printAsCString(O, CVA);
290 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000291 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000292 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
293 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000294 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000295 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000296 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
297 // Print the fields in successive locations. Pad to align if needed!
298 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerad200712003-09-09 16:23:36 +0000299 unsigned sizeSoFar = 0;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000300 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
301 const Constant* field = CVS->getOperand(i);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000302
Chris Lattnerad200712003-09-09 16:23:36 +0000303 // Check if padding is needed and insert one or more 0s.
304 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000305 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattnerad200712003-09-09 16:23:36 +0000306 : cvsLayout->MemberOffsets[i+1])
307 - cvsLayout->MemberOffsets[i]) - fieldSize;
308 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000309
Chris Lattnerad200712003-09-09 16:23:36 +0000310 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000311 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000312
313 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000314 if (padSize)
315 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000316 }
Chris Lattnerad200712003-09-09 16:23:36 +0000317 assert(sizeSoFar == cvsLayout->StructSize &&
318 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000319 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000320 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
321 // FP Constants are printed as integer constants to avoid losing
322 // precision...
323 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000324 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000325 default: assert(0 && "Unknown floating point type!");
326 case Type::FloatTyID: {
327 union FU { // Abide by C TBAA rules
328 float FVal;
329 unsigned UVal;
330 } U;
331 U.FVal = Val;
332 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
333 return;
334 }
335 case Type::DoubleTyID: {
336 union DU { // Abide by C TBAA rules
337 double FVal;
338 uint64_t UVal;
339 } U;
340 U.FVal = Val;
341 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
342 return;
343 }
344 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000345 }
346
347 const Type *type = CV->getType();
348 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000349 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000350 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
351 O << ".byte";
352 break;
353 case Type::UShortTyID: case Type::ShortTyID:
354 O << ".word";
355 break;
356 case Type::FloatTyID: case Type::PointerTyID:
357 case Type::UIntTyID: case Type::IntTyID:
358 O << ".long";
359 break;
360 case Type::DoubleTyID:
361 case Type::ULongTyID: case Type::LongTyID:
362 O << ".quad";
363 break;
364 default:
365 assert (0 && "Can't handle printing this type of thing");
366 break;
367 }
368 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000369 emitConstantValueOnly(CV);
370 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000371}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000372
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000373/// printConstantPool - Print to the current output stream assembly
374/// representations of the constants in the constant pool MCP. This is
375/// used to print out constants which have been "spilled to memory" by
376/// the code generator.
377///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000378void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000379 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000380 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000381
Chris Lattnerb7089442003-01-13 00:35:03 +0000382 if (CP.empty()) return;
383
384 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
385 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000386 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000387 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000388 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
389 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000390 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000391 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000392}
393
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000394/// runOnMachineFunction - This uses the printMachineInstruction()
395/// method to print assembly for each instruction.
396///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000397bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000398 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000399 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000400 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000401
Chris Lattnerb7089442003-01-13 00:35:03 +0000402 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000403 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000404
Brian Gaeke6559bb92002-11-14 22:32:30 +0000405 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000406 O << "\t.text\n";
407 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000408 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000409 if (!EmitCygwin)
410 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000411 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000412
413 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000414 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
415 I != E; ++I) {
416 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000417 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000418 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000419 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000420 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000421 // Print the assembly for the instruction.
422 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000423 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000424 }
Chris Lattner0285a332002-12-28 20:25:38 +0000425 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000426
427 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000428 return false;
429}
430
Chris Lattner3d3067b2002-11-21 20:44:15 +0000431static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000432 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000433 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
434 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000435}
436
437static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000438 if (MI->getOperand(Op).isFrameIndex()) return true;
439 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000440 return Op+4 <= MI->getNumOperands() &&
Chris Lattner2a998bd2004-08-11 07:02:04 +0000441 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
442 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000443}
444
Brian Gaeke2a098772003-08-11 19:05:46 +0000445
446
Chris Lattner3fa861a2004-08-01 06:02:08 +0000447void X86AsmPrinter::printOp(const MachineOperand &MO,
448 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000449 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000450 switch (MO.getType()) {
451 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000452 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000453 O << "<" << V->getName() << ">";
454 return;
455 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000456 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000457 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000458 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000459 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000460 O << "%" << RI.get(MO.getReg()).Name;
461 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000462 O << "%reg" << MO.getReg();
463 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000464
465 case MachineOperand::MO_SignExtendedImmed:
466 case MachineOperand::MO_UnextendedImmed:
467 O << (int)MO.getImmedValue();
468 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000469 case MachineOperand::MO_MachineBasicBlock: {
470 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
471 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
472 << "_" << MBBOp->getNumber () << "\t# "
473 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000474 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000475 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000476 case MachineOperand::MO_PCRelativeDisp:
477 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
478 abort ();
479 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000480 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000481 if (!elideOffsetKeyword)
482 O << "OFFSET ";
483 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000484 return;
485 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000486 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000487 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000488 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000489 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000490 }
491}
492
Chris Lattner3fa861a2004-08-01 06:02:08 +0000493void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000494 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000495
496 if (MI->getOperand(Op).isFrameIndex()) {
497 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
498 if (MI->getOperand(Op+3).getImmedValue())
499 O << " + " << MI->getOperand(Op+3).getImmedValue();
500 O << "]";
501 return;
502 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000503 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000504 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000505 if (MI->getOperand(Op+3).getImmedValue())
506 O << " + " << MI->getOperand(Op+3).getImmedValue();
507 O << "]";
508 return;
509 }
510
Chris Lattner3d3067b2002-11-21 20:44:15 +0000511 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000512 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000513 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000514 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000515
516 O << "[";
517 bool NeedPlus = false;
518 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000519 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000520 NeedPlus = true;
521 }
522
523 if (IndexReg.getReg()) {
524 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000525 if (ScaleVal != 1)
526 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000527 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000528 NeedPlus = true;
529 }
530
Chris Lattner0285a332002-12-28 20:25:38 +0000531 if (DispVal) {
532 if (NeedPlus)
533 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000534 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000535 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000536 O << " - ";
537 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000538 }
539 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000540 }
541 O << "]";
542}
543
Chris Lattner30b2f722004-03-31 22:02:21 +0000544/// printImplUsesAfter - Emit the implicit-use registers for the instruction
545/// described by DESC, if its PrintImplUsesAfter flag is set.
546///
John Criswell4ffff9e2004-04-08 20:31:47 +0000547/// Inputs:
548/// Comma - List of registers will need a leading comma.
549/// Desc - Description of the Instruction.
550///
551/// Return value:
552/// true - Emitted one or more registers.
553/// false - Emitted no registers.
554///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000555bool X86AsmPrinter::printImplUsesAfter(const TargetInstrDescriptor &Desc,
556 const bool Comma = true) {
Chris Lattner30b2f722004-03-31 22:02:21 +0000557 const MRegisterInfo &RI = *TM.getRegisterInfo();
558 if (Desc.TSFlags & X86II::PrintImplUsesAfter) {
John Criswell4ffff9e2004-04-08 20:31:47 +0000559 bool emitted = false;
560 const unsigned *p = Desc.ImplicitUses;
561 if (*p) {
562 O << (Comma ? ", %" : "%") << RI.get (*p).Name;
563 emitted = true;
564 ++p;
565 }
566 while (*p) {
Chris Lattner3fa861a2004-08-01 06:02:08 +0000567 // Bug Workaround: See note in X86AsmPrinter::doInitialization about %.
Chris Lattner67488a92003-08-11 20:04:57 +0000568 O << ", %" << RI.get(*p).Name;
John Criswell4ffff9e2004-04-08 20:31:47 +0000569 ++p;
Brian Gaeke2a098772003-08-11 19:05:46 +0000570 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000571 return emitted;
Brian Gaeke2a098772003-08-11 19:05:46 +0000572 }
John Criswell4ffff9e2004-04-08 20:31:47 +0000573 return false;
574}
575
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000576/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000577/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000578///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000579void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
580 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000581
582 // gas bugs:
583 //
584 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
585 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
586 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
587 //
588 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
589 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
590 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
591 //
592 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
593 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
594 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
595 // Output the raw opcode bytes instead of the instruction.
596 //
597 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
598 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
599 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
600 // Output the raw opcode bytes instead of the instruction.
601 switch (MI->getOpcode()) {
602 case X86::FSTP80m:
603 case X86::FLD80m:
604 case X86::FILD64m:
605 case X86::FISTP64m:
606 GasBugWorkaroundEmitter gwe(O);
607 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
608 O << "\t# ";
609 }
610
Chris Lattner2a998bd2004-08-11 07:02:04 +0000611 // Call the autogenerated instruction printer routines.
612 bool Handled = printInstruction(MI);
613 if (!Handled) {
614 MI->dump();
615 assert(0 && "Do not know how to print this instruction!");
616 abort();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000617 }
Chris Lattner72614082002-10-25 22:55:53 +0000618}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000619
Chris Lattner3fa861a2004-08-01 06:02:08 +0000620bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000621 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000622 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000623 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
624 // instruction as a reference to the register named sp, and if you try to
625 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
626 // before being looked up in the symbol table. This creates spurious
627 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
628 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000629 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000630 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000631 return false; // success
632}
633
Chris Lattnerad200712003-09-09 16:23:36 +0000634// SwitchSection - Switch to the specified section of the executable if we are
635// not already in it!
636//
637static void SwitchSection(std::ostream &OS, std::string &CurSection,
638 const char *NewSection) {
639 if (CurSection != NewSection) {
640 CurSection = NewSection;
641 if (!CurSection.empty())
642 OS << "\t" << NewSection << "\n";
643 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000644}
645
Chris Lattner3fa861a2004-08-01 06:02:08 +0000646bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000647 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000648 std::string CurSection;
649
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000650 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000651 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
652 if (I->hasInitializer()) { // External global require no code
653 O << "\n\n";
654 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000655 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000656 unsigned Size = TD.getTypeSize(C->getType());
657 unsigned Align = TD.getTypeAlignment(C->getType());
658
659 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000660 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
661 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000662 SwitchSection(O, CurSection, ".data");
663 if (I->hasInternalLinkage())
664 O << "\t.local " << name << "\n";
665
666 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000667 << "," << (unsigned)TD.getTypeAlignment(C->getType());
668 O << "\t\t# ";
669 WriteAsOperand(O, I, true, true, &M);
670 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000671 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000672 switch (I->getLinkage()) {
673 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000674 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000675 // Nonnull linkonce -> weak
676 O << "\t.weak " << name << "\n";
677 SwitchSection(O, CurSection, "");
678 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
679 break;
680
681 case GlobalValue::AppendingLinkage:
682 // FIXME: appending linkage variables should go into a section of
683 // their name or something. For now, just emit them as external.
684 case GlobalValue::ExternalLinkage:
685 // If external or appending, declare as a global symbol
686 O << "\t.globl " << name << "\n";
687 // FALL THROUGH
688 case GlobalValue::InternalLinkage:
689 if (C->isNullValue())
690 SwitchSection(O, CurSection, ".bss");
691 else
692 SwitchSection(O, CurSection, ".data");
693 break;
694 }
695
696 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000697 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000698 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000699 O << name << ":\t\t\t\t# ";
700 WriteAsOperand(O, I, true, true, &M);
701 O << " = ";
702 WriteAsOperand(O, C, false, false, &M);
703 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000704 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000705 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000706 }
Chris Lattnerad200712003-09-09 16:23:36 +0000707
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000708 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000709 return false; // success
710}