blob: 23cf78d83bc7074771c71cf475f007e1a956f439 [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
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000137 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000138 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000139 void printMemReference(const MachineInstr *MI, unsigned Op);
140 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000141 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000142 bool doInitialization(Module &M);
143 bool doFinalization(Module &M);
Chris Lattnerac662d12003-11-03 20:19:49 +0000144 void emitGlobalConstant(const Constant* CV);
145 void emitConstantValueOnly(const Constant *CV);
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000146 };
Brian Gaeked7908f62003-06-27 00:00:48 +0000147} // end of anonymous namespace
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000148
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000149/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekede420ae2003-07-23 20:25:08 +0000150/// assembly code for a MachineFunction to the given output stream,
151/// using the given target machine description. This should work
152/// regardless of whether the function is in SSA form.
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000153///
Chris Lattner300d0ed2004-02-14 06:00:36 +0000154FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner3fa861a2004-08-01 06:02:08 +0000155 return new X86AsmPrinter(o, tm);
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000156}
157
Chris Lattner3fa861a2004-08-01 06:02:08 +0000158
159// Include the auto-generated portion of the assembly writer.
160#include "X86GenAsmWriter.inc"
161
162
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000163/// toOctal - Convert the low order bits of X into an octal digit.
164///
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000165static inline char toOctal(int X) {
166 return (X&7)+'0';
167}
168
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000169/// getAsCString - Return the specified array as a C compatible
170/// string, only if the predicate isStringCompatible is true.
171///
Chris Lattnerac662d12003-11-03 20:19:49 +0000172static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000173 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000174
Chris Lattnerac662d12003-11-03 20:19:49 +0000175 O << "\"";
Chris Lattneraa06d042004-01-14 17:14:42 +0000176 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattnerc07736a2003-07-23 15:22:26 +0000177 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000178
179 if (C == '"') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000180 O << "\\\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000181 } else if (C == '\\') {
Chris Lattnerac662d12003-11-03 20:19:49 +0000182 O << "\\\\";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000183 } else if (isprint(C)) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000184 O << C;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000185 } else {
186 switch(C) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000187 case '\b': O << "\\b"; break;
188 case '\f': O << "\\f"; break;
189 case '\n': O << "\\n"; break;
190 case '\r': O << "\\r"; break;
191 case '\t': O << "\\t"; break;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000192 default:
Chris Lattnerac662d12003-11-03 20:19:49 +0000193 O << '\\';
194 O << toOctal(C >> 6);
195 O << toOctal(C >> 3);
196 O << toOctal(C >> 0);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000197 break;
198 }
199 }
200 }
Chris Lattnerac662d12003-11-03 20:19:49 +0000201 O << "\"";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000202}
203
Chris Lattnerac662d12003-11-03 20:19:49 +0000204// Print out the specified constant, without a storage class. Only the
205// constants valid in constant expressions can occur here.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000206void X86AsmPrinter::emitConstantValueOnly(const Constant *CV) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000207 if (CV->isNullValue())
208 O << "0";
209 else if (const ConstantBool *CB = dyn_cast<ConstantBool>(CV)) {
210 assert(CB == ConstantBool::True);
211 O << "1";
212 } else if (const ConstantSInt *CI = dyn_cast<ConstantSInt>(CV))
Chris Lattnerf7b42252004-02-23 03:27:05 +0000213 if (((CI->getValue() << 32) >> 32) == CI->getValue())
214 O << CI->getValue();
215 else
216 O << (unsigned long long)CI->getValue();
Chris Lattnerac662d12003-11-03 20:19:49 +0000217 else if (const ConstantUInt *CI = dyn_cast<ConstantUInt>(CV))
218 O << CI->getValue();
Reid Spencer8863f182004-07-18 00:38:32 +0000219 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV))
Chris Lattnerac662d12003-11-03 20:19:49 +0000220 // This is a constant address for a global variable or function. Use the
221 // name of the variable or function as the address value.
Reid Spencer8863f182004-07-18 00:38:32 +0000222 O << Mang->getValueName(GV);
Chris Lattnerac662d12003-11-03 20:19:49 +0000223 else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
224 const TargetData &TD = TM.getTargetData();
225 switch(CE->getOpcode()) {
226 case Instruction::GetElementPtr: {
227 // generate a symbolic expression for the byte address
228 const Constant *ptrVal = CE->getOperand(0);
229 std::vector<Value*> idxVec(CE->op_begin()+1, CE->op_end());
230 if (unsigned Offset = TD.getIndexedOffset(ptrVal->getType(), idxVec)) {
231 O << "(";
232 emitConstantValueOnly(ptrVal);
Chris Lattner90533562003-11-04 16:04:32 +0000233 O << ") + " << Offset;
Chris Lattnerac662d12003-11-03 20:19:49 +0000234 } else {
235 emitConstantValueOnly(ptrVal);
236 }
237 break;
238 }
239 case Instruction::Cast: {
240 // Support only non-converting or widening casts for now, that is, ones
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000241 // that do not involve a change in value. This assertion is really gross,
242 // and may not even be a complete check.
Chris Lattnerac662d12003-11-03 20:19:49 +0000243 Constant *Op = CE->getOperand(0);
244 const Type *OpTy = Op->getType(), *Ty = CE->getType();
Chris Lattner90533562003-11-04 16:04:32 +0000245
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000246 // Remember, kids, pointers on x86 can be losslessly converted back and
247 // forth into 32-bit or wider integers, regardless of signedness. :-P
Chris Lattnerac662d12003-11-03 20:19:49 +0000248 assert(((isa<PointerType>(OpTy)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000249 && (Ty == Type::LongTy || Ty == Type::ULongTy
250 || Ty == Type::IntTy || Ty == Type::UIntTy))
Chris Lattnerac662d12003-11-03 20:19:49 +0000251 || (isa<PointerType>(Ty)
Brian Gaekef9c86cf2003-11-22 07:18:25 +0000252 && (OpTy == Type::LongTy || OpTy == Type::ULongTy
253 || OpTy == Type::IntTy || OpTy == Type::UIntTy))
Chris Lattner90533562003-11-04 16:04:32 +0000254 || (((TD.getTypeSize(Ty) >= TD.getTypeSize(OpTy))
255 && OpTy->isLosslesslyConvertibleTo(Ty))))
Chris Lattnerac662d12003-11-03 20:19:49 +0000256 && "FIXME: Don't yet support this kind of constant cast expr");
257 O << "(";
258 emitConstantValueOnly(Op);
259 O << ")";
260 break;
261 }
262 case Instruction::Add:
263 O << "(";
264 emitConstantValueOnly(CE->getOperand(0));
265 O << ") + (";
266 emitConstantValueOnly(CE->getOperand(1));
267 O << ")";
268 break;
269 default:
270 assert(0 && "Unsupported operator!");
271 }
272 } else {
273 assert(0 && "Unknown constant value!");
274 }
275}
276
277// Print a constant value or values, with the appropriate storage class as a
278// prefix.
Chris Lattner3fa861a2004-08-01 06:02:08 +0000279void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000280 const TargetData &TD = TM.getTargetData();
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000281
Chris Lattnerad200712003-09-09 16:23:36 +0000282 if (CV->isNullValue()) {
283 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner3e119c62003-11-03 19:44:05 +0000284 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000285 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattneraa06d042004-01-14 17:14:42 +0000286 if (CVA->isString()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000287 O << "\t.ascii\t";
288 printAsCString(O, CVA);
289 O << "\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000290 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000291 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
292 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000293 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000294 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000295 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
296 // Print the fields in successive locations. Pad to align if needed!
297 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattnerad200712003-09-09 16:23:36 +0000298 unsigned sizeSoFar = 0;
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000299 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
300 const Constant* field = CVS->getOperand(i);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000301
Chris Lattnerad200712003-09-09 16:23:36 +0000302 // Check if padding is needed and insert one or more 0s.
303 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos15876bb2004-08-04 08:44:43 +0000304 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattnerad200712003-09-09 16:23:36 +0000305 : cvsLayout->MemberOffsets[i+1])
306 - cvsLayout->MemberOffsets[i]) - fieldSize;
307 sizeSoFar += fieldSize + padSize;
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000308
Chris Lattnerad200712003-09-09 16:23:36 +0000309 // Now print the actual field value
Chris Lattnerac662d12003-11-03 20:19:49 +0000310 emitGlobalConstant(field);
Chris Lattnerad200712003-09-09 16:23:36 +0000311
312 // Insert the field padding unless it's zero bytes...
Chris Lattnerb3aad5d2003-09-10 19:52:24 +0000313 if (padSize)
314 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000315 }
Chris Lattnerad200712003-09-09 16:23:36 +0000316 assert(sizeSoFar == cvsLayout->StructSize &&
317 "Layout of constant struct may be incorrect!");
Chris Lattner3e119c62003-11-03 19:44:05 +0000318 return;
Chris Lattnerac662d12003-11-03 20:19:49 +0000319 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
320 // FP Constants are printed as integer constants to avoid losing
321 // precision...
322 double Val = CFP->getValue();
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000323 switch (CFP->getType()->getTypeID()) {
Chris Lattnerac662d12003-11-03 20:19:49 +0000324 default: assert(0 && "Unknown floating point type!");
325 case Type::FloatTyID: {
326 union FU { // Abide by C TBAA rules
327 float FVal;
328 unsigned UVal;
329 } U;
330 U.FVal = Val;
331 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
332 return;
333 }
334 case Type::DoubleTyID: {
335 union DU { // Abide by C TBAA rules
336 double FVal;
337 uint64_t UVal;
338 } U;
339 U.FVal = Val;
340 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
341 return;
342 }
343 }
Chris Lattner3e119c62003-11-03 19:44:05 +0000344 }
345
346 const Type *type = CV->getType();
347 O << "\t";
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000348 switch (type->getTypeID()) {
Chris Lattner3e119c62003-11-03 19:44:05 +0000349 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
350 O << ".byte";
351 break;
352 case Type::UShortTyID: case Type::ShortTyID:
353 O << ".word";
354 break;
355 case Type::FloatTyID: case Type::PointerTyID:
356 case Type::UIntTyID: case Type::IntTyID:
357 O << ".long";
358 break;
359 case Type::DoubleTyID:
360 case Type::ULongTyID: case Type::LongTyID:
361 O << ".quad";
362 break;
363 default:
364 assert (0 && "Can't handle printing this type of thing");
365 break;
366 }
367 O << "\t";
Chris Lattnerac662d12003-11-03 20:19:49 +0000368 emitConstantValueOnly(CV);
369 O << "\n";
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000370}
Chris Lattnerdbb61c62002-11-17 22:53:13 +0000371
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000372/// printConstantPool - Print to the current output stream assembly
373/// representations of the constants in the constant pool MCP. This is
374/// used to print out constants which have been "spilled to memory" by
375/// the code generator.
376///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000377void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000378 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekede420ae2003-07-23 20:25:08 +0000379 const TargetData &TD = TM.getTargetData();
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000380
Chris Lattnerb7089442003-01-13 00:35:03 +0000381 if (CP.empty()) return;
382
383 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
384 O << "\t.section .rodata\n";
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000385 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke5e001572003-06-26 18:02:30 +0000386 << "\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000387 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
388 << *CP[i] << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000389 emitGlobalConstant(CP[i]);
Chris Lattnerb7089442003-01-13 00:35:03 +0000390 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000391}
392
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000393/// runOnMachineFunction - This uses the printMachineInstruction()
394/// method to print assembly for each instruction.
395///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000396bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattnere0121322003-08-03 23:37:09 +0000397 O << "\n\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000398 // What's my mangled name?
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000399 CurrentFnName = Mang->getValueName(MF.getFunction());
Brian Gaeked7908f62003-06-27 00:00:48 +0000400
Chris Lattnerb7089442003-01-13 00:35:03 +0000401 // Print out constants referenced by the function
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000402 printConstantPool(MF.getConstantPool());
Chris Lattnerb7089442003-01-13 00:35:03 +0000403
Brian Gaeke6559bb92002-11-14 22:32:30 +0000404 // Print out labels for the function.
Chris Lattnerb7089442003-01-13 00:35:03 +0000405 O << "\t.text\n";
406 O << "\t.align 16\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000407 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000408 if (!EmitCygwin)
409 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaeked7908f62003-06-27 00:00:48 +0000410 O << CurrentFnName << ":\n";
Brian Gaeke6559bb92002-11-14 22:32:30 +0000411
412 // Print out code for the function.
Chris Lattner0285a332002-12-28 20:25:38 +0000413 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
414 I != E; ++I) {
415 // Print a label for the basic block.
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000416 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000417 << I->getBasicBlock()->getName() << "\n";
Chris Lattner0285a332002-12-28 20:25:38 +0000418 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000419 II != E; ++II) {
Chris Lattner0285a332002-12-28 20:25:38 +0000420 // Print the assembly for the instruction.
421 O << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000422 printMachineInstruction(II);
Brian Gaeke6559bb92002-11-14 22:32:30 +0000423 }
Chris Lattner0285a332002-12-28 20:25:38 +0000424 }
Brian Gaeke6559bb92002-11-14 22:32:30 +0000425
426 // We didn't modify anything.
Chris Lattnerb4f68ed2002-10-29 22:37:54 +0000427 return false;
428}
429
Chris Lattner3d3067b2002-11-21 20:44:15 +0000430static bool isScale(const MachineOperand &MO) {
Chris Lattnerd9096832002-12-15 08:01:39 +0000431 return MO.isImmediate() &&
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000432 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
433 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000434}
435
436static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattnerb7089442003-01-13 00:35:03 +0000437 if (MI->getOperand(Op).isFrameIndex()) return true;
438 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000439 return Op+4 <= MI->getNumOperands() &&
Chris Lattner2a998bd2004-08-11 07:02:04 +0000440 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
441 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000442}
443
Brian Gaeke2a098772003-08-11 19:05:46 +0000444
445
Chris Lattner3fa861a2004-08-01 06:02:08 +0000446void X86AsmPrinter::printOp(const MachineOperand &MO,
447 bool elideOffsetKeyword /* = false */) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000448 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000449 switch (MO.getType()) {
450 case MachineOperand::MO_VirtualRegister:
Chris Lattnerac573f62002-12-04 17:32:52 +0000451 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattnerdbf30f72002-12-04 06:45:19 +0000452 O << "<" << V->getName() << ">";
453 return;
454 }
Chris Lattnerb7089442003-01-13 00:35:03 +0000455 // FALLTHROUGH
Misha Brukmane1f0d812002-11-20 18:56:41 +0000456 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenos859a18b2004-02-15 21:37:17 +0000457 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke2a098772003-08-11 19:05:46 +0000458 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke9d99b432003-08-13 18:15:15 +0000459 O << "%" << RI.get(MO.getReg()).Name;
460 else
Chris Lattnerf9f60882002-11-18 06:56:51 +0000461 O << "%reg" << MO.getReg();
462 return;
Chris Lattner77875d82002-11-21 02:00:20 +0000463
464 case MachineOperand::MO_SignExtendedImmed:
465 case MachineOperand::MO_UnextendedImmed:
466 O << (int)MO.getImmedValue();
467 return;
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000468 case MachineOperand::MO_MachineBasicBlock: {
469 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
470 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
471 << "_" << MBBOp->getNumber () << "\t# "
472 << MBBOp->getBasicBlock ()->getName ();
Chris Lattnerf8bafe82002-12-01 23:25:59 +0000473 return;
Chris Lattnerad200712003-09-09 16:23:36 +0000474 }
Brian Gaeke3fb5d1a2004-05-14 06:54:57 +0000475 case MachineOperand::MO_PCRelativeDisp:
476 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
477 abort ();
478 return;
Chris Lattnerb7089442003-01-13 00:35:03 +0000479 case MachineOperand::MO_GlobalAddress:
Brian Gaeke002a50a2003-07-31 17:38:52 +0000480 if (!elideOffsetKeyword)
481 O << "OFFSET ";
482 O << Mang->getValueName(MO.getGlobal());
Chris Lattnerb7089442003-01-13 00:35:03 +0000483 return;
484 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke9e474c42003-06-19 19:32:32 +0000485 O << MO.getSymbolName();
Chris Lattnerb7089442003-01-13 00:35:03 +0000486 return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000487 default:
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000488 O << "<unknown operand type>"; return;
Chris Lattnerf9f60882002-11-18 06:56:51 +0000489 }
490}
491
Chris Lattner3fa861a2004-08-01 06:02:08 +0000492void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner3d3067b2002-11-21 20:44:15 +0000493 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattnerb7089442003-01-13 00:35:03 +0000494
495 if (MI->getOperand(Op).isFrameIndex()) {
496 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
497 if (MI->getOperand(Op+3).getImmedValue())
498 O << " + " << MI->getOperand(Op+3).getImmedValue();
499 O << "]";
500 return;
501 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaeked7908f62003-06-27 00:00:48 +0000502 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke5e001572003-06-26 18:02:30 +0000503 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattnerb7089442003-01-13 00:35:03 +0000504 if (MI->getOperand(Op+3).getImmedValue())
505 O << " + " << MI->getOperand(Op+3).getImmedValue();
506 O << "]";
507 return;
508 }
509
Chris Lattner3d3067b2002-11-21 20:44:15 +0000510 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner0285a332002-12-28 20:25:38 +0000511 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000512 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner0285a332002-12-28 20:25:38 +0000513 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner3d3067b2002-11-21 20:44:15 +0000514
515 O << "[";
516 bool NeedPlus = false;
517 if (BaseReg.getReg()) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000518 printOp(BaseReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000519 NeedPlus = true;
520 }
521
522 if (IndexReg.getReg()) {
523 if (NeedPlus) O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000524 if (ScaleVal != 1)
525 O << ScaleVal << "*";
Brian Gaekede420ae2003-07-23 20:25:08 +0000526 printOp(IndexReg);
Chris Lattner3d3067b2002-11-21 20:44:15 +0000527 NeedPlus = true;
528 }
529
Chris Lattner0285a332002-12-28 20:25:38 +0000530 if (DispVal) {
531 if (NeedPlus)
532 if (DispVal > 0)
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000533 O << " + ";
Chris Lattner0285a332002-12-28 20:25:38 +0000534 else {
Misha Brukmane8d8fb22004-06-29 19:28:53 +0000535 O << " - ";
536 DispVal = -DispVal;
Chris Lattner0285a332002-12-28 20:25:38 +0000537 }
538 O << DispVal;
Chris Lattner3d3067b2002-11-21 20:44:15 +0000539 }
540 O << "]";
541}
542
John Criswell4ffff9e2004-04-08 20:31:47 +0000543
Brian Gaeke92bdfe62003-07-23 18:37:06 +0000544/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekede420ae2003-07-23 20:25:08 +0000545/// MI in Intel syntax to the current output stream.
Brian Gaeked7908f62003-06-27 00:00:48 +0000546///
Chris Lattner3fa861a2004-08-01 06:02:08 +0000547void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
548 ++EmittedInsts;
Chris Lattner85494292004-08-11 06:09:55 +0000549
550 // gas bugs:
551 //
552 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
553 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
554 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
555 //
556 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
557 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
558 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
559 //
560 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
561 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
562 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
563 // Output the raw opcode bytes instead of the instruction.
564 //
565 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
566 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
567 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
568 // Output the raw opcode bytes instead of the instruction.
569 switch (MI->getOpcode()) {
570 case X86::FSTP80m:
571 case X86::FLD80m:
572 case X86::FILD64m:
573 case X86::FISTP64m:
574 GasBugWorkaroundEmitter gwe(O);
575 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
576 O << "\t# ";
577 }
578
Chris Lattner2a998bd2004-08-11 07:02:04 +0000579 // Call the autogenerated instruction printer routines.
580 bool Handled = printInstruction(MI);
581 if (!Handled) {
582 MI->dump();
583 assert(0 && "Do not know how to print this instruction!");
584 abort();
Chris Lattnerf9f60882002-11-18 06:56:51 +0000585 }
Chris Lattner72614082002-10-25 22:55:53 +0000586}
Brian Gaeke9e474c42003-06-19 19:32:32 +0000587
Chris Lattner3fa861a2004-08-01 06:02:08 +0000588bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner93c1afa2003-08-11 19:35:26 +0000589 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke2a098772003-08-11 19:05:46 +0000590 //
Chris Lattner93c1afa2003-08-11 19:35:26 +0000591 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
592 // instruction as a reference to the register named sp, and if you try to
593 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
594 // before being looked up in the symbol table. This creates spurious
595 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
596 // mode, and decorate all register names with percent signs.
Chris Lattner67488a92003-08-11 20:04:57 +0000597 O << "\t.intel_syntax\n";
Chris Lattner93c1afa2003-08-11 19:35:26 +0000598 Mang = new Mangler(M, EmitCygwin);
Brian Gaeke9e474c42003-06-19 19:32:32 +0000599 return false; // success
600}
601
Chris Lattnerad200712003-09-09 16:23:36 +0000602// SwitchSection - Switch to the specified section of the executable if we are
603// not already in it!
604//
605static void SwitchSection(std::ostream &OS, std::string &CurSection,
606 const char *NewSection) {
607 if (CurSection != NewSection) {
608 CurSection = NewSection;
609 if (!CurSection.empty())
610 OS << "\t" << NewSection << "\n";
611 }
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000612}
613
Chris Lattner3fa861a2004-08-01 06:02:08 +0000614bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekede420ae2003-07-23 20:25:08 +0000615 const TargetData &TD = TM.getTargetData();
Chris Lattnerad200712003-09-09 16:23:36 +0000616 std::string CurSection;
617
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000618 // Print out module-level global variables here.
Chris Lattnerad200712003-09-09 16:23:36 +0000619 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
620 if (I->hasInitializer()) { // External global require no code
621 O << "\n\n";
622 std::string name = Mang->getValueName(I);
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000623 Constant *C = I->getInitializer();
Chris Lattnerad200712003-09-09 16:23:36 +0000624 unsigned Size = TD.getTypeSize(C->getType());
625 unsigned Align = TD.getTypeAlignment(C->getType());
626
627 if (C->isNullValue() &&
Chris Lattner72ac148d2003-10-16 18:29:00 +0000628 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
629 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattnerad200712003-09-09 16:23:36 +0000630 SwitchSection(O, CurSection, ".data");
631 if (I->hasInternalLinkage())
632 O << "\t.local " << name << "\n";
633
634 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattnere0121322003-08-03 23:37:09 +0000635 << "," << (unsigned)TD.getTypeAlignment(C->getType());
636 O << "\t\t# ";
637 WriteAsOperand(O, I, true, true, &M);
638 O << "\n";
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000639 } else {
Chris Lattnerad200712003-09-09 16:23:36 +0000640 switch (I->getLinkage()) {
641 case GlobalValue::LinkOnceLinkage:
Chris Lattner72ac148d2003-10-16 18:29:00 +0000642 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattnerad200712003-09-09 16:23:36 +0000643 // Nonnull linkonce -> weak
644 O << "\t.weak " << name << "\n";
645 SwitchSection(O, CurSection, "");
646 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
647 break;
648
649 case GlobalValue::AppendingLinkage:
650 // FIXME: appending linkage variables should go into a section of
651 // their name or something. For now, just emit them as external.
652 case GlobalValue::ExternalLinkage:
653 // If external or appending, declare as a global symbol
654 O << "\t.globl " << name << "\n";
655 // FALL THROUGH
656 case GlobalValue::InternalLinkage:
657 if (C->isNullValue())
658 SwitchSection(O, CurSection, ".bss");
659 else
660 SwitchSection(O, CurSection, ".data");
661 break;
662 }
663
664 O << "\t.align " << Align << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000665 O << "\t.type " << name << ",@object\n";
Chris Lattnerad200712003-09-09 16:23:36 +0000666 O << "\t.size " << name << "," << Size << "\n";
Chris Lattnere0121322003-08-03 23:37:09 +0000667 O << name << ":\t\t\t\t# ";
668 WriteAsOperand(O, I, true, true, &M);
669 O << " = ";
670 WriteAsOperand(O, C, false, false, &M);
671 O << "\n";
Chris Lattnerac662d12003-11-03 20:19:49 +0000672 emitGlobalConstant(C);
Brian Gaeke0517c5a2003-07-11 21:57:01 +0000673 }
Brian Gaeke01d79ff2003-06-25 18:01:07 +0000674 }
Chris Lattnerad200712003-09-09 16:23:36 +0000675
Brian Gaeked9fb37a2003-07-24 20:20:44 +0000676 delete Mang;
Brian Gaeke9e474c42003-06-19 19:32:32 +0000677 return false; // success
678}