blob: a709105c5e6aea685e6565553b7e4595c43610c9 [file] [log] [blame]
Misha Brukmancf7d3af2004-07-26 18:45:48 +00001//===-- X86AsmPrinter.cpp - Convert X86 LLVM code to Intel assembly -------===//
John Criswell482202a2003-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 Lattnerd92fb002002-10-25 22:55:53 +00009//
Misha Brukmana6025e62004-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 Lattnerd92fb002002-10-25 22:55:53 +000014//
15//===----------------------------------------------------------------------===//
16
17#include "X86.h"
Brian Gaekee7454352002-11-14 22:32:30 +000018#include "X86InstrInfo.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000019#include "X86TargetMachine.h"
Chris Lattner5dcb6542003-08-03 23:37:09 +000020#include "llvm/Constants.h"
21#include "llvm/DerivedTypes.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
Chris Lattner9cf46452004-08-16 23:16:06 +000024#include "llvm/CodeGen/AsmPrinter.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000025#include "llvm/CodeGen/MachineCodeEmitter.h"
Chris Lattner956e8372003-01-13 00:35:03 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000027#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner9289d7d2002-11-17 22:53:13 +000028#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnerd5540022004-08-01 07:43:46 +000029#include "llvm/CodeGen/ValueTypes.h"
Chris Lattner1a2e6f72003-08-11 20:06:16 +000030#include "llvm/Target/TargetMachine.h"
Brian Gaeke46f8b712003-07-24 20:20:44 +000031#include "llvm/Support/Mangler.h"
Brian Gaeke4547ab12003-10-06 15:41:21 +000032#include "Support/Statistic.h"
Chris Lattner5dcb6542003-08-03 23:37:09 +000033#include "Support/StringExtras.h"
Chris Lattner9a59f582003-08-11 19:35:26 +000034#include "Support/CommandLine.h"
Chris Lattner9f75a552004-02-14 06:00:36 +000035using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000036
Chris Lattner02a3d832002-10-29 22:37:54 +000037namespace {
Brian Gaeke4547ab12003-10-06 15:41:21 +000038 Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
39
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000040 struct GasBugWorkaroundEmitter : public MachineCodeEmitter {
Misha Brukmana332a6462004-07-26 18:48:58 +000041 GasBugWorkaroundEmitter(std::ostream& o)
42 : O(o), OldFlags(O.flags()), firstByte(true) {
43 O << std::hex;
44 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000045
Misha Brukmana332a6462004-07-26 18:48:58 +000046 ~GasBugWorkaroundEmitter() {
47 O.flags(OldFlags);
Misha Brukmana332a6462004-07-26 18:48:58 +000048 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000049
Misha Brukmana332a6462004-07-26 18:48:58 +000050 virtual void emitByte(unsigned char B) {
51 if (!firstByte) O << "\n\t";
52 firstByte = false;
53 O << ".byte 0x" << (unsigned) B;
54 }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000055
Misha Brukmana332a6462004-07-26 18:48:58 +000056 // These should never be called
57 virtual void emitWord(unsigned W) { assert(0); }
58 virtual uint64_t getGlobalValueAddress(GlobalValue *V) { abort(); }
59 virtual uint64_t getGlobalValueAddress(const std::string &Name) { abort(); }
60 virtual uint64_t getConstantPoolEntryAddress(unsigned Index) { abort(); }
61 virtual uint64_t getCurrentPCValue() { abort(); }
62 virtual uint64_t forceCompilationOf(Function *F) { abort(); }
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000063
64 private:
Misha Brukmana332a6462004-07-26 18:48:58 +000065 std::ostream& O;
66 std::ios::fmtflags OldFlags;
67 bool firstByte;
Alkis Evlogimenos8ac958b2004-03-09 03:35:34 +000068 };
69
Chris Lattner9cf46452004-08-16 23:16:06 +000070 struct X86AsmPrinter : public AsmPrinter {
71 X86AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) { }
Brian Gaekec3998cb2003-07-23 18:37:06 +000072
Chris Lattnerd06650a2002-12-15 21:13:40 +000073 virtual const char *getPassName() const {
74 return "X86 Assembly Printer";
75 }
76
Chris Lattner9520d202004-08-01 06:02:08 +000077 /// printInstruction - This method is automatically generated by tablegen
78 /// from the instruction set description. This method returns true if the
79 /// machine instruction was sufficiently described to print it, otherwise it
80 /// returns false.
81 bool printInstruction(const MachineInstr *MI);
82
Chris Lattnerd5540022004-08-01 07:43:46 +000083 // This method is used by the tablegen'erated instruction printer.
Chris Lattner9cf46452004-08-16 23:16:06 +000084 void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
Chris Lattner09ee05b2004-08-11 02:25:00 +000085 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattner06cf67e2004-08-01 08:12:41 +000086 if (MO.getType() == MachineOperand::MO_MachineRegister) {
87 assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physref??");
88 // Bug Workaround: See note in Printer::doInitialization about %.
89 O << "%" << TM.getRegisterInfo()->get(MO.getReg()).Name;
90 } else {
91 printOp(MO);
92 }
Chris Lattnerd5540022004-08-01 07:43:46 +000093 }
94
Chris Lattner9cf46452004-08-16 23:16:06 +000095 void printCallOperand(const MachineInstr *MI, unsigned OpNo,
96 MVT::ValueType VT) {
Chris Lattnera0bafce2004-08-11 06:59:12 +000097 printOp(MI->getOperand(OpNo), true); // Don't print "OFFSET".
98 }
99
Chris Lattner09ee05b2004-08-11 02:25:00 +0000100 void printMemoryOperand(const MachineInstr *MI, unsigned OpNo,
101 MVT::ValueType VT) {
102 switch (VT) {
103 default: assert(0 && "Unknown arg size!");
104 case MVT::i8: O << "BYTE PTR "; break;
105 case MVT::i16: O << "WORD PTR "; break;
106 case MVT::i32:
107 case MVT::f32: O << "DWORD PTR "; break;
108 case MVT::i64:
109 case MVT::f64: O << "QWORD PTR "; break;
110 case MVT::f80: O << "XWORD PTR "; break;
111 }
112 printMemReference(MI, OpNo);
113 }
114
Brian Gaeke46f8b712003-07-24 20:20:44 +0000115 void printMachineInstruction(const MachineInstr *MI);
Misha Brukmanc968b872004-06-29 19:28:53 +0000116 void printOp(const MachineOperand &MO, bool elideOffsetKeyword = false);
Brian Gaeke46f8b712003-07-24 20:20:44 +0000117 void printMemReference(const MachineInstr *MI, unsigned Op);
118 void printConstantPool(MachineConstantPool *MCP);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000119 bool runOnMachineFunction(MachineFunction &F);
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000120 bool doInitialization(Module &M);
121 bool doFinalization(Module &M);
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000122 void emitGlobalConstant(const Constant* CV);
Chris Lattner02a3d832002-10-29 22:37:54 +0000123 };
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000124} // end of anonymous namespace
Chris Lattner02a3d832002-10-29 22:37:54 +0000125
Brian Gaekec3998cb2003-07-23 18:37:06 +0000126/// createX86CodePrinterPass - Returns a pass that prints the X86
Brian Gaekea92dce42003-07-23 20:25:08 +0000127/// assembly code for a MachineFunction to the given output stream,
128/// using the given target machine description. This should work
129/// regardless of whether the function is in SSA form.
Chris Lattner9289d7d2002-11-17 22:53:13 +0000130///
Chris Lattner9f75a552004-02-14 06:00:36 +0000131FunctionPass *llvm::createX86CodePrinterPass(std::ostream &o,TargetMachine &tm){
Chris Lattner9520d202004-08-01 06:02:08 +0000132 return new X86AsmPrinter(o, tm);
Chris Lattner9289d7d2002-11-17 22:53:13 +0000133}
134
Chris Lattner9520d202004-08-01 06:02:08 +0000135
136// Include the auto-generated portion of the assembly writer.
137#include "X86GenAsmWriter.inc"
138
139
Brian Gaekec3998cb2003-07-23 18:37:06 +0000140/// toOctal - Convert the low order bits of X into an octal digit.
141///
Brian Gaeke25e766a2003-06-25 18:01:07 +0000142static inline char toOctal(int X) {
143 return (X&7)+'0';
144}
145
Brian Gaekec3998cb2003-07-23 18:37:06 +0000146/// getAsCString - Return the specified array as a C compatible
147/// string, only if the predicate isStringCompatible is true.
148///
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000149static void printAsCString(std::ostream &O, const ConstantArray *CVA) {
Chris Lattnerac2b1982004-01-14 17:14:42 +0000150 assert(CVA->isString() && "Array is not string compatible!");
Brian Gaeke25e766a2003-06-25 18:01:07 +0000151
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000152 O << "\"";
Chris Lattnerac2b1982004-01-14 17:14:42 +0000153 for (unsigned i = 0; i != CVA->getNumOperands(); ++i) {
Chris Lattner6077c312003-07-23 15:22:26 +0000154 unsigned char C = cast<ConstantInt>(CVA->getOperand(i))->getRawValue();
Brian Gaeke25e766a2003-06-25 18:01:07 +0000155
156 if (C == '"') {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000157 O << "\\\"";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000158 } else if (C == '\\') {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000159 O << "\\\\";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000160 } else if (isprint(C)) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000161 O << C;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000162 } else {
163 switch(C) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000164 case '\b': O << "\\b"; break;
165 case '\f': O << "\\f"; break;
166 case '\n': O << "\\n"; break;
167 case '\r': O << "\\r"; break;
168 case '\t': O << "\\t"; break;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000169 default:
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000170 O << '\\';
171 O << toOctal(C >> 6);
172 O << toOctal(C >> 3);
173 O << toOctal(C >> 0);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000174 break;
175 }
176 }
177 }
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000178 O << "\"";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000179}
180
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000181// Print a constant value or values, with the appropriate storage class as a
182// prefix.
Chris Lattner9520d202004-08-01 06:02:08 +0000183void X86AsmPrinter::emitGlobalConstant(const Constant *CV) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000184 const TargetData &TD = TM.getTargetData();
Brian Gaeke25e766a2003-06-25 18:01:07 +0000185
Chris Lattner230cffb2003-09-09 16:23:36 +0000186 if (CV->isNullValue()) {
187 O << "\t.zero\t " << TD.getTypeSize(CV->getType()) << "\n";
Chris Lattner469e4042003-11-03 19:44:05 +0000188 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000189 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Chris Lattnerac2b1982004-01-14 17:14:42 +0000190 if (CVA->isString()) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000191 O << "\t.ascii\t";
192 printAsCString(O, CVA);
193 O << "\n";
Chris Lattner230cffb2003-09-09 16:23:36 +0000194 } else { // Not a string. Print the values in successive locations
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000195 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
196 emitGlobalConstant(CVA->getOperand(i));
Brian Gaeke25e766a2003-06-25 18:01:07 +0000197 }
Chris Lattner469e4042003-11-03 19:44:05 +0000198 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000199 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
200 // Print the fields in successive locations. Pad to align if needed!
201 const StructLayout *cvsLayout = TD.getStructLayout(CVS->getType());
Chris Lattner230cffb2003-09-09 16:23:36 +0000202 unsigned sizeSoFar = 0;
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000203 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
204 const Constant* field = CVS->getOperand(i);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000205
Chris Lattner230cffb2003-09-09 16:23:36 +0000206 // Check if padding is needed and insert one or more 0s.
207 unsigned fieldSize = TD.getTypeSize(field->getType());
Alkis Evlogimenos83243722004-08-04 08:44:43 +0000208 unsigned padSize = ((i == e-1? cvsLayout->StructSize
Chris Lattner230cffb2003-09-09 16:23:36 +0000209 : cvsLayout->MemberOffsets[i+1])
210 - cvsLayout->MemberOffsets[i]) - fieldSize;
211 sizeSoFar += fieldSize + padSize;
Brian Gaeke25e766a2003-06-25 18:01:07 +0000212
Chris Lattner230cffb2003-09-09 16:23:36 +0000213 // Now print the actual field value
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000214 emitGlobalConstant(field);
Chris Lattner230cffb2003-09-09 16:23:36 +0000215
216 // Insert the field padding unless it's zero bytes...
Chris Lattner59068a02003-09-10 19:52:24 +0000217 if (padSize)
218 O << "\t.zero\t " << padSize << "\n";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000219 }
Chris Lattner230cffb2003-09-09 16:23:36 +0000220 assert(sizeSoFar == cvsLayout->StructSize &&
221 "Layout of constant struct may be incorrect!");
Chris Lattner469e4042003-11-03 19:44:05 +0000222 return;
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000223 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
224 // FP Constants are printed as integer constants to avoid losing
225 // precision...
226 double Val = CFP->getValue();
Chris Lattner6b727592004-06-17 18:19:28 +0000227 switch (CFP->getType()->getTypeID()) {
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000228 default: assert(0 && "Unknown floating point type!");
229 case Type::FloatTyID: {
230 union FU { // Abide by C TBAA rules
231 float FVal;
232 unsigned UVal;
233 } U;
234 U.FVal = Val;
235 O << ".long\t" << U.UVal << "\t# float " << Val << "\n";
236 return;
237 }
238 case Type::DoubleTyID: {
239 union DU { // Abide by C TBAA rules
240 double FVal;
241 uint64_t UVal;
242 } U;
243 U.FVal = Val;
244 O << ".quad\t" << U.UVal << "\t# double " << Val << "\n";
245 return;
246 }
247 }
Chris Lattner469e4042003-11-03 19:44:05 +0000248 }
249
250 const Type *type = CV->getType();
251 O << "\t";
Chris Lattner6b727592004-06-17 18:19:28 +0000252 switch (type->getTypeID()) {
Chris Lattner469e4042003-11-03 19:44:05 +0000253 case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID:
254 O << ".byte";
255 break;
256 case Type::UShortTyID: case Type::ShortTyID:
257 O << ".word";
258 break;
259 case Type::FloatTyID: case Type::PointerTyID:
260 case Type::UIntTyID: case Type::IntTyID:
261 O << ".long";
262 break;
263 case Type::DoubleTyID:
264 case Type::ULongTyID: case Type::LongTyID:
265 O << ".quad";
266 break;
267 default:
268 assert (0 && "Can't handle printing this type of thing");
269 break;
270 }
271 O << "\t";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000272 emitConstantValueOnly(CV);
273 O << "\n";
Brian Gaeke25e766a2003-06-25 18:01:07 +0000274}
Chris Lattner9289d7d2002-11-17 22:53:13 +0000275
Brian Gaekec3998cb2003-07-23 18:37:06 +0000276/// printConstantPool - Print to the current output stream assembly
277/// representations of the constants in the constant pool MCP. This is
278/// used to print out constants which have been "spilled to memory" by
279/// the code generator.
280///
Chris Lattner9520d202004-08-01 06:02:08 +0000281void X86AsmPrinter::printConstantPool(MachineConstantPool *MCP) {
Chris Lattner956e8372003-01-13 00:35:03 +0000282 const std::vector<Constant*> &CP = MCP->getConstants();
Brian Gaekea92dce42003-07-23 20:25:08 +0000283 const TargetData &TD = TM.getTargetData();
Brian Gaekec3998cb2003-07-23 18:37:06 +0000284
Chris Lattner956e8372003-01-13 00:35:03 +0000285 if (CP.empty()) return;
286
287 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
288 O << "\t.section .rodata\n";
Brian Gaekec3998cb2003-07-23 18:37:06 +0000289 O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
Brian Gaeke4ab22212003-06-26 18:02:30 +0000290 << "\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000291 O << ".CPI" << CurrentFnName << "_" << i << ":\t\t\t\t\t#"
292 << *CP[i] << "\n";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000293 emitGlobalConstant(CP[i]);
Chris Lattner956e8372003-01-13 00:35:03 +0000294 }
Chris Lattner956e8372003-01-13 00:35:03 +0000295}
296
Brian Gaekec3998cb2003-07-23 18:37:06 +0000297/// runOnMachineFunction - This uses the printMachineInstruction()
298/// method to print assembly for each instruction.
299///
Chris Lattner9520d202004-08-01 06:02:08 +0000300bool X86AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner9cf46452004-08-16 23:16:06 +0000301 setupMachineFunction(MF);
Chris Lattner5dcb6542003-08-03 23:37:09 +0000302 O << "\n\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000303
Chris Lattner956e8372003-01-13 00:35:03 +0000304 // Print out constants referenced by the function
Brian Gaeke25e766a2003-06-25 18:01:07 +0000305 printConstantPool(MF.getConstantPool());
Chris Lattner956e8372003-01-13 00:35:03 +0000306
Brian Gaekee7454352002-11-14 22:32:30 +0000307 // Print out labels for the function.
Chris Lattner956e8372003-01-13 00:35:03 +0000308 O << "\t.text\n";
309 O << "\t.align 16\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000310 O << "\t.globl\t" << CurrentFnName << "\n";
Chris Lattner9cf46452004-08-16 23:16:06 +0000311 O << "\t.type\t" << CurrentFnName << ", @function\n";
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000312 O << CurrentFnName << ":\n";
Brian Gaekee7454352002-11-14 22:32:30 +0000313
314 // Print out code for the function.
Chris Lattner1520c5a2002-12-28 20:25:38 +0000315 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
316 I != E; ++I) {
317 // Print a label for the basic block.
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000318 O << ".LBB" << CurrentFnName << "_" << I->getNumber() << ":\t# "
Brian Gaeke25e766a2003-06-25 18:01:07 +0000319 << I->getBasicBlock()->getName() << "\n";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000320 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
Misha Brukmanc968b872004-06-29 19:28:53 +0000321 II != E; ++II) {
Chris Lattner1520c5a2002-12-28 20:25:38 +0000322 // Print the assembly for the instruction.
323 O << "\t";
Alkis Evlogimenos80da8652004-02-12 02:27:10 +0000324 printMachineInstruction(II);
Brian Gaekee7454352002-11-14 22:32:30 +0000325 }
Chris Lattner1520c5a2002-12-28 20:25:38 +0000326 }
Brian Gaekee7454352002-11-14 22:32:30 +0000327
328 // We didn't modify anything.
Chris Lattner02a3d832002-10-29 22:37:54 +0000329 return false;
330}
331
Chris Lattner61fafd352002-11-21 20:44:15 +0000332static bool isScale(const MachineOperand &MO) {
Chris Lattnerce351082002-12-15 08:01:39 +0000333 return MO.isImmediate() &&
Brian Gaeke25e766a2003-06-25 18:01:07 +0000334 (MO.getImmedValue() == 1 || MO.getImmedValue() == 2 ||
335 MO.getImmedValue() == 4 || MO.getImmedValue() == 8);
Chris Lattner61fafd352002-11-21 20:44:15 +0000336}
337
338static bool isMem(const MachineInstr *MI, unsigned Op) {
Chris Lattner956e8372003-01-13 00:35:03 +0000339 if (MI->getOperand(Op).isFrameIndex()) return true;
340 if (MI->getOperand(Op).isConstantPoolIndex()) return true;
Chris Lattner61fafd352002-11-21 20:44:15 +0000341 return Op+4 <= MI->getNumOperands() &&
Chris Lattnerd1bee6e2004-08-11 07:02:04 +0000342 MI->getOperand(Op ).isRegister() && isScale(MI->getOperand(Op+1)) &&
343 MI->getOperand(Op+2).isRegister() && MI->getOperand(Op+3).isImmediate();
Chris Lattner61fafd352002-11-21 20:44:15 +0000344}
345
Brian Gaeke12b32532003-08-11 19:05:46 +0000346
347
Chris Lattner9520d202004-08-01 06:02:08 +0000348void X86AsmPrinter::printOp(const MachineOperand &MO,
349 bool elideOffsetKeyword /* = false */) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000350 const MRegisterInfo &RI = *TM.getRegisterInfo();
Chris Lattner5812f062002-11-18 06:56:51 +0000351 switch (MO.getType()) {
352 case MachineOperand::MO_VirtualRegister:
Chris Lattnerfb8032d2002-12-04 17:32:52 +0000353 if (Value *V = MO.getVRegValueOrNull()) {
Chris Lattner6425a502002-12-04 06:45:19 +0000354 O << "<" << V->getName() << ">";
355 return;
356 }
Chris Lattner956e8372003-01-13 00:35:03 +0000357 // FALLTHROUGH
Misha Brukman6e5d4932002-11-20 18:56:41 +0000358 case MachineOperand::MO_MachineRegister:
Alkis Evlogimenosbbf53932004-02-15 21:37:17 +0000359 if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
Brian Gaeke12b32532003-08-11 19:05:46 +0000360 // Bug Workaround: See note in Printer::doInitialization about %.
Brian Gaeke1600c4a2003-08-13 18:15:15 +0000361 O << "%" << RI.get(MO.getReg()).Name;
362 else
Chris Lattner5812f062002-11-18 06:56:51 +0000363 O << "%reg" << MO.getReg();
364 return;
Chris Lattner177e9282002-11-21 02:00:20 +0000365
366 case MachineOperand::MO_SignExtendedImmed:
367 case MachineOperand::MO_UnextendedImmed:
368 O << (int)MO.getImmedValue();
369 return;
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000370 case MachineOperand::MO_MachineBasicBlock: {
371 MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
372 O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
373 << "_" << MBBOp->getNumber () << "\t# "
374 << MBBOp->getBasicBlock ()->getName ();
Chris Lattner08cd1ed2002-12-01 23:25:59 +0000375 return;
Chris Lattner230cffb2003-09-09 16:23:36 +0000376 }
Brian Gaeke2b3a81c2004-05-14 06:54:57 +0000377 case MachineOperand::MO_PCRelativeDisp:
378 std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs";
379 abort ();
380 return;
Chris Lattner956e8372003-01-13 00:35:03 +0000381 case MachineOperand::MO_GlobalAddress:
Brian Gaekea2d9f402003-07-31 17:38:52 +0000382 if (!elideOffsetKeyword)
383 O << "OFFSET ";
384 O << Mang->getValueName(MO.getGlobal());
Chris Lattner956e8372003-01-13 00:35:03 +0000385 return;
386 case MachineOperand::MO_ExternalSymbol:
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000387 O << MO.getSymbolName();
Chris Lattner956e8372003-01-13 00:35:03 +0000388 return;
Chris Lattner5812f062002-11-18 06:56:51 +0000389 default:
Brian Gaeke25e766a2003-06-25 18:01:07 +0000390 O << "<unknown operand type>"; return;
Chris Lattner5812f062002-11-18 06:56:51 +0000391 }
392}
393
Chris Lattner9520d202004-08-01 06:02:08 +0000394void X86AsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op) {
Chris Lattner61fafd352002-11-21 20:44:15 +0000395 assert(isMem(MI, Op) && "Invalid memory reference!");
Chris Lattner956e8372003-01-13 00:35:03 +0000396
397 if (MI->getOperand(Op).isFrameIndex()) {
398 O << "[frame slot #" << MI->getOperand(Op).getFrameIndex();
399 if (MI->getOperand(Op+3).getImmedValue())
400 O << " + " << MI->getOperand(Op+3).getImmedValue();
401 O << "]";
402 return;
403 } else if (MI->getOperand(Op).isConstantPoolIndex()) {
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000404 O << "[.CPI" << CurrentFnName << "_"
Brian Gaeke4ab22212003-06-26 18:02:30 +0000405 << MI->getOperand(Op).getConstantPoolIndex();
Chris Lattner956e8372003-01-13 00:35:03 +0000406 if (MI->getOperand(Op+3).getImmedValue())
407 O << " + " << MI->getOperand(Op+3).getImmedValue();
408 O << "]";
409 return;
410 }
411
Chris Lattner61fafd352002-11-21 20:44:15 +0000412 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner1520c5a2002-12-28 20:25:38 +0000413 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
Chris Lattner61fafd352002-11-21 20:44:15 +0000414 const MachineOperand &IndexReg = MI->getOperand(Op+2);
Chris Lattner1520c5a2002-12-28 20:25:38 +0000415 int DispVal = MI->getOperand(Op+3).getImmedValue();
Chris Lattner61fafd352002-11-21 20:44:15 +0000416
417 O << "[";
418 bool NeedPlus = false;
419 if (BaseReg.getReg()) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000420 printOp(BaseReg);
Chris Lattner61fafd352002-11-21 20:44:15 +0000421 NeedPlus = true;
422 }
423
424 if (IndexReg.getReg()) {
425 if (NeedPlus) O << " + ";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000426 if (ScaleVal != 1)
427 O << ScaleVal << "*";
Brian Gaekea92dce42003-07-23 20:25:08 +0000428 printOp(IndexReg);
Chris Lattner61fafd352002-11-21 20:44:15 +0000429 NeedPlus = true;
430 }
431
Chris Lattner1520c5a2002-12-28 20:25:38 +0000432 if (DispVal) {
433 if (NeedPlus)
434 if (DispVal > 0)
Misha Brukmanc968b872004-06-29 19:28:53 +0000435 O << " + ";
Chris Lattner1520c5a2002-12-28 20:25:38 +0000436 else {
Misha Brukmanc968b872004-06-29 19:28:53 +0000437 O << " - ";
438 DispVal = -DispVal;
Chris Lattner1520c5a2002-12-28 20:25:38 +0000439 }
440 O << DispVal;
Chris Lattner61fafd352002-11-21 20:44:15 +0000441 }
442 O << "]";
443}
444
John Criswell10db0622004-04-08 20:31:47 +0000445
Brian Gaekec3998cb2003-07-23 18:37:06 +0000446/// printMachineInstruction -- Print out a single X86 LLVM instruction
Brian Gaekea92dce42003-07-23 20:25:08 +0000447/// MI in Intel syntax to the current output stream.
Brian Gaekec1e4ee02003-06-27 00:00:48 +0000448///
Chris Lattner9520d202004-08-01 06:02:08 +0000449void X86AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
450 ++EmittedInsts;
Chris Lattnerb9756362004-08-11 06:09:55 +0000451
452 // gas bugs:
453 //
454 // The 80-bit FP store-pop instruction "fstp XWORD PTR [...]" is misassembled
455 // by gas in intel_syntax mode as its 32-bit equivalent "fstp DWORD PTR
456 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
457 //
458 // The 80-bit FP load instruction "fld XWORD PTR [...]" is misassembled by gas
459 // in intel_syntax mode as its 32-bit equivalent "fld DWORD PTR
460 // [...]". Workaround: Output the raw opcode bytes instead of the instruction.
461 //
462 // gas intel_syntax mode treats "fild QWORD PTR [...]" as an invalid opcode,
463 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
464 // disassembles it as "fild DWORD PTR [...]", which is wrong. Workaround:
465 // Output the raw opcode bytes instead of the instruction.
466 //
467 // gas intel_syntax mode treats "fistp QWORD PTR [...]" as an invalid opcode,
468 // saying "64 bit operations are only supported in 64 bit modes." libopcodes
469 // disassembles it as "fistpll DWORD PTR [...]", which is wrong. Workaround:
470 // Output the raw opcode bytes instead of the instruction.
471 switch (MI->getOpcode()) {
472 case X86::FSTP80m:
473 case X86::FLD80m:
474 case X86::FILD64m:
475 case X86::FISTP64m:
476 GasBugWorkaroundEmitter gwe(O);
477 X86::emitInstruction(gwe, (X86InstrInfo&)*TM.getInstrInfo(), *MI);
478 O << "\t# ";
479 }
480
Chris Lattnerd1bee6e2004-08-11 07:02:04 +0000481 // Call the autogenerated instruction printer routines.
482 bool Handled = printInstruction(MI);
483 if (!Handled) {
484 MI->dump();
485 assert(0 && "Do not know how to print this instruction!");
486 abort();
Chris Lattner5812f062002-11-18 06:56:51 +0000487 }
Chris Lattnerd92fb002002-10-25 22:55:53 +0000488}
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000489
Chris Lattner9520d202004-08-01 06:02:08 +0000490bool X86AsmPrinter::doInitialization(Module &M) {
Chris Lattner9cf46452004-08-16 23:16:06 +0000491 AsmPrinter::doInitialization(M);
Chris Lattner9a59f582003-08-11 19:35:26 +0000492 // Tell gas we are outputting Intel syntax (not AT&T syntax) assembly.
Brian Gaeke12b32532003-08-11 19:05:46 +0000493 //
Chris Lattner9a59f582003-08-11 19:35:26 +0000494 // Bug: gas in `intel_syntax noprefix' mode interprets the symbol `Sp' in an
495 // instruction as a reference to the register named sp, and if you try to
496 // reference a symbol `Sp' (e.g. `mov ECX, OFFSET Sp') then it gets lowercased
497 // before being looked up in the symbol table. This creates spurious
498 // `undefined symbol' errors when linking. Workaround: Do not use `noprefix'
499 // mode, and decorate all register names with percent signs.
Chris Lattner262c8322003-08-11 20:04:57 +0000500 O << "\t.intel_syntax\n";
Chris Lattner9cf46452004-08-16 23:16:06 +0000501 return false;
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000502}
503
Chris Lattner230cffb2003-09-09 16:23:36 +0000504// SwitchSection - Switch to the specified section of the executable if we are
505// not already in it!
506//
507static void SwitchSection(std::ostream &OS, std::string &CurSection,
508 const char *NewSection) {
509 if (CurSection != NewSection) {
510 CurSection = NewSection;
511 if (!CurSection.empty())
512 OS << "\t" << NewSection << "\n";
513 }
Brian Gaekeb99d6842003-07-11 21:57:01 +0000514}
515
Chris Lattner9520d202004-08-01 06:02:08 +0000516bool X86AsmPrinter::doFinalization(Module &M) {
Brian Gaekea92dce42003-07-23 20:25:08 +0000517 const TargetData &TD = TM.getTargetData();
Chris Lattner230cffb2003-09-09 16:23:36 +0000518 std::string CurSection;
519
Brian Gaeke25e766a2003-06-25 18:01:07 +0000520 // Print out module-level global variables here.
Chris Lattner230cffb2003-09-09 16:23:36 +0000521 for (Module::const_giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
522 if (I->hasInitializer()) { // External global require no code
523 O << "\n\n";
524 std::string name = Mang->getValueName(I);
Brian Gaeke25e766a2003-06-25 18:01:07 +0000525 Constant *C = I->getInitializer();
Chris Lattner230cffb2003-09-09 16:23:36 +0000526 unsigned Size = TD.getTypeSize(C->getType());
527 unsigned Align = TD.getTypeAlignment(C->getType());
528
529 if (C->isNullValue() &&
Chris Lattner068ad842003-10-16 18:29:00 +0000530 (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
531 I->hasWeakLinkage() /* FIXME: Verify correct */)) {
Chris Lattner230cffb2003-09-09 16:23:36 +0000532 SwitchSection(O, CurSection, ".data");
533 if (I->hasInternalLinkage())
534 O << "\t.local " << name << "\n";
535
536 O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
Chris Lattner5dcb6542003-08-03 23:37:09 +0000537 << "," << (unsigned)TD.getTypeAlignment(C->getType());
538 O << "\t\t# ";
539 WriteAsOperand(O, I, true, true, &M);
540 O << "\n";
Brian Gaekeb99d6842003-07-11 21:57:01 +0000541 } else {
Chris Lattner230cffb2003-09-09 16:23:36 +0000542 switch (I->getLinkage()) {
543 case GlobalValue::LinkOnceLinkage:
Chris Lattner068ad842003-10-16 18:29:00 +0000544 case GlobalValue::WeakLinkage: // FIXME: Verify correct for weak.
Chris Lattner230cffb2003-09-09 16:23:36 +0000545 // Nonnull linkonce -> weak
546 O << "\t.weak " << name << "\n";
547 SwitchSection(O, CurSection, "");
548 O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
549 break;
550
551 case GlobalValue::AppendingLinkage:
552 // FIXME: appending linkage variables should go into a section of
553 // their name or something. For now, just emit them as external.
554 case GlobalValue::ExternalLinkage:
555 // If external or appending, declare as a global symbol
556 O << "\t.globl " << name << "\n";
557 // FALL THROUGH
558 case GlobalValue::InternalLinkage:
559 if (C->isNullValue())
560 SwitchSection(O, CurSection, ".bss");
561 else
562 SwitchSection(O, CurSection, ".data");
563 break;
564 }
565
566 O << "\t.align " << Align << "\n";
Chris Lattner5dcb6542003-08-03 23:37:09 +0000567 O << "\t.type " << name << ",@object\n";
Chris Lattner230cffb2003-09-09 16:23:36 +0000568 O << "\t.size " << name << "," << Size << "\n";
Chris Lattner5dcb6542003-08-03 23:37:09 +0000569 O << name << ":\t\t\t\t# ";
570 WriteAsOperand(O, I, true, true, &M);
571 O << " = ";
572 WriteAsOperand(O, C, false, false, &M);
573 O << "\n";
Chris Lattnerb71c3f72003-11-03 20:19:49 +0000574 emitGlobalConstant(C);
Brian Gaekeb99d6842003-07-11 21:57:01 +0000575 }
Brian Gaeke25e766a2003-06-25 18:01:07 +0000576 }
Chris Lattner230cffb2003-09-09 16:23:36 +0000577
Chris Lattner9cf46452004-08-16 23:16:06 +0000578 AsmPrinter::doFinalization(M);
Brian Gaeke259fdbc2003-06-19 19:32:32 +0000579 return false; // success
580}