blob: 85ae8388993ff8f64e00c29b5997413fe5deec45 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===//
2//
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//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to AT&T format assembly
12// language. This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
16#include "X86ATTAsmPrinter.h"
17#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000019#include "X86TargetMachine.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000020#include "X86TargetAsmInfo.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000021#include "llvm/CallingConv.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000022#include "llvm/Module.h"
23#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000026using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027
Chris Lattnerafbfded2006-10-05 02:43:52 +000028/// getSectionForFunction - Return the section that we should emit the
29/// specified function body into.
30std::string X86ATTAsmPrinter::getSectionForFunction(const Function &F) const {
31 switch (F.getLinkage()) {
32 default: assert(0 && "Unknown linkage type!");
33 case Function::InternalLinkage:
34 case Function::DLLExportLinkage:
35 case Function::ExternalLinkage:
36 return TAI->getTextSection();
37 case Function::WeakLinkage:
38 case Function::LinkOnceLinkage:
39 if (Subtarget->isTargetDarwin()) {
40 return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
41 } else if (Subtarget->isTargetCygwin()) {
Anton Korobeynikovb1c88022006-10-18 09:12:29 +000042 return "\t.section\t.text$linkonce." + CurrentFnName + ",\"ax\"\n";
Chris Lattnerafbfded2006-10-05 02:43:52 +000043 } else {
44 return "\t.section\t.llvm.linkonce.t." + CurrentFnName +
45 ",\"ax\",@progbits\n";
46 }
47 }
48}
49
Chris Lattnerb36cbd02005-07-01 22:44:09 +000050/// runOnMachineFunction - This uses the printMachineInstruction()
51/// method to print assembly for each instruction.
52///
53bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +000054 if (Subtarget->isTargetDarwin() ||
55 Subtarget->isTargetELF() ||
56 Subtarget->isTargetCygwin()) {
Jim Laskeye29c2f52006-07-19 11:54:50 +000057 // Let PassManager know we need debug information and relay
58 // the MachineDebugInfo address on to DwarfWriter.
59 DW.SetDebugInfo(&getAnalysis<MachineDebugInfo>());
60 }
Evan Cheng3c992d22006-03-07 02:02:57 +000061
Chris Lattner8b8b9512005-11-21 07:51:23 +000062 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000063 O << "\n\n";
64
65 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000066 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000067
68 // Print out labels for the function.
Evan Cheng2338c5c2006-02-07 08:38:37 +000069 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000070 unsigned CC = F->getCallingConv();
71
72 // Populate function information map. Actually, We don't want to populate
73 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000074 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
75 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000076
77 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
78
Chris Lattnerafbfded2006-10-05 02:43:52 +000079 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
80
Evan Cheng2338c5c2006-02-07 08:38:37 +000081 switch (F->getLinkage()) {
82 default: assert(0 && "Unknown linkage type!");
83 case Function::InternalLinkage: // Symbols default to internal.
Evan Cheng2338c5c2006-02-07 08:38:37 +000084 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
85 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000086 case Function::DLLExportLinkage:
87 DLLExportedFns.insert(Mang->makeNameProper(F->getName(), ""));
88 //FALLS THROUGH
Evan Cheng2338c5c2006-02-07 08:38:37 +000089 case Function::ExternalLinkage:
Evan Cheng2338c5c2006-02-07 08:38:37 +000090 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000091 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000092 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +000093 case Function::LinkOnceLinkage:
Anton Korobeynikovb1c88022006-10-18 09:12:29 +000094 case Function::WeakLinkage:
Jim Laskeyea348582006-07-27 02:05:13 +000095 if (Subtarget->isTargetDarwin()) {
Evan Chengf1616da2006-02-22 23:59:57 +000096 O << "\t.globl\t" << CurrentFnName << "\n";
Evan Cheng2338c5c2006-02-07 08:38:37 +000097 O << "\t.weak_definition\t" << CurrentFnName << "\n";
Anton Korobeynikovbcb97702006-09-17 20:25:45 +000098 } else if (Subtarget->isTargetCygwin()) {
Evan Cheng932ad512006-05-25 21:59:08 +000099 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
Anton Korobeynikovb7188b12006-10-17 20:29:49 +0000100 O << "\t.linkonce discard\n";
101 O << "\t.globl " << CurrentFnName << "\n";
102 } else {
103 EmitAlignment(4, F); // FIXME: This should be parameterized somewhere.
104 O << "\t.weak " << CurrentFnName << "\n";
105 }
106 break;
Evan Cheng2338c5c2006-02-07 08:38:37 +0000107 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000108 O << CurrentFnName << ":\n";
Anton Korobeynikovb1c88022006-10-18 09:12:29 +0000109 // Add some workaround for linkonce linkage on Cygwin\MinGW
110 if (Subtarget->isTargetCygwin() &&
111 (F->getLinkage() == Function::LinkOnceLinkage ||
112 F->getLinkage() == Function::WeakLinkage))
113 O << "_llvm$workaround$fake$stub_" << CurrentFnName << ":\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000114
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000115 if (Subtarget->isTargetDarwin() ||
116 Subtarget->isTargetELF() ||
117 Subtarget->isTargetCygwin()) {
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000118 // Emit pre-function debug information.
Jim Laskey89d67fa2006-06-23 12:51:53 +0000119 DW.BeginFunction(&MF);
Jim Laskey6b92b8e2006-04-07 20:44:42 +0000120 }
121
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122 // Print out code for the function.
123 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
124 I != E; ++I) {
125 // Print a label for the basic block.
Nate Begemancdf38c42006-05-02 05:37:32 +0000126 if (I->pred_begin() != I->pred_end()) {
127 printBasicBlockLabel(I, true);
128 O << '\n';
129 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
131 II != E; ++II) {
132 // Print the assembly for the instruction.
133 O << "\t";
134 printMachineInstruction(II);
135 }
136 }
Evan Cheng67afece2006-08-28 22:14:16 +0000137
Chris Lattner1da31ee2006-10-05 03:01:21 +0000138 // Print out jump tables referenced by the function.
139
140 // Mac OS X requires that the jump table follow the function, so that the jump
141 // table is part of the same atom that the function is in.
142 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
Evan Cheng67afece2006-08-28 22:14:16 +0000143
Jim Laskey563321a2006-09-06 18:34:40 +0000144 if (TAI->hasDotTypeDotSizeDirective())
Nate Begeman73213f62005-07-12 01:37:28 +0000145 O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n";
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000146
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000147 if (Subtarget->isTargetDarwin() ||
148 Subtarget->isTargetELF() ||
149 Subtarget->isTargetCygwin()) {
Evan Chengd5948812006-03-07 02:23:26 +0000150 // Emit post-function debug information.
Jim Laskey99db0442006-03-23 18:09:44 +0000151 DW.EndFunction();
Evan Chengd5948812006-03-07 02:23:26 +0000152 }
Evan Cheng3c992d22006-03-07 02:02:57 +0000153
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000154 // We didn't modify anything.
155 return false;
156}
157
Chris Lattnera3b8c572006-02-06 23:41:19 +0000158void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
159 const char *Modifier) {
160 const MachineOperand &MO = MI->getOperand(OpNo);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000161 const MRegisterInfo &RI = *TM.getRegisterInfo();
162 switch (MO.getType()) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000163 case MachineOperand::MO_Register: {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164 assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) &&
165 "Virtual registers should not make it this far!");
166 O << '%';
Evan Cheng8f7f7122006-05-05 05:40:20 +0000167 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000168 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000169 MVT::ValueType VT = (strcmp(Modifier+6,"64") == 0) ?
170 MVT::i64 : ((strcmp(Modifier+6, "32") == 0) ? MVT::i32 :
171 ((strcmp(Modifier+6,"16") == 0) ? MVT::i16 : MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000172 Reg = getX86SubSuperRegister(Reg, VT);
173 }
174 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175 O << (char)tolower(*Name);
176 return;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000177 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000178
Chris Lattner63b3d712006-05-04 17:21:20 +0000179 case MachineOperand::MO_Immediate:
Evan Cheng3c992d22006-03-07 02:02:57 +0000180 if (!Modifier || strcmp(Modifier, "debug") != 0)
181 O << '$';
Evan Cheng138a24e2006-05-26 08:04:31 +0000182 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000183 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000184 case MachineOperand::MO_MachineBasicBlock:
185 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000187 case MachineOperand::MO_JumpTableIndex: {
188 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
189 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000190 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << "_"
Nate Begeman37efe672006-04-22 18:53:45 +0000191 << MO.getJumpTableIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000192 if (X86PICStyle == PICStyle::Stub &&
Nate Begeman2f1ae882006-07-27 01:13:04 +0000193 TM.getRelocationModel() == Reloc::PIC_)
194 O << "-\"L" << getFunctionNumber() << "$pb\"";
Evan Cheng25ab6902006-09-08 06:48:29 +0000195 if (Subtarget->is64Bit())
196 O << "(%rip)";
Nate Begeman37efe672006-04-22 18:53:45 +0000197 return;
198 }
Evan Chenga09bd812006-02-26 08:28:12 +0000199 case MachineOperand::MO_ConstantPoolIndex: {
200 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
201 if (!isMemOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000202 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
Evan Chenga09bd812006-02-26 08:28:12 +0000203 << MO.getConstantPoolIndex();
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 if (X86PICStyle == PICStyle::Stub &&
Chris Lattner35d86fe2006-07-26 21:12:04 +0000205 TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga09bd812006-02-26 08:28:12 +0000206 O << "-\"L" << getFunctionNumber() << "$pb\"";
207 int Offset = MO.getOffset();
208 if (Offset > 0)
209 O << "+" << Offset;
210 else if (Offset < 0)
211 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000212
213 if (Subtarget->is64Bit())
214 O << "(%rip)";
Evan Chenga09bd812006-02-26 08:28:12 +0000215 return;
216 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000217 case MachineOperand::MO_GlobalAddress: {
Chris Lattnera3b8c572006-02-06 23:41:19 +0000218 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng2338c5c2006-02-07 08:38:37 +0000219 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Evan Cheng7ccced62006-02-18 00:15:05 +0000220 if (!isMemOp && !isCallOp) O << '$';
Evan Cheng25ab6902006-09-08 06:48:29 +0000221
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000222 GlobalValue *GV = MO.getGlobal();
Evan Cheng25ab6902006-09-08 06:48:29 +0000223 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000224
Evan Cheng25ab6902006-09-08 06:48:29 +0000225 bool isExt = (GV->isExternal() || GV->hasWeakLinkage() ||
226 GV->hasLinkOnceLinkage());
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000227
Chris Lattnere87e1152006-09-26 03:57:53 +0000228 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000229
Evan Cheng25ab6902006-09-08 06:48:29 +0000230 if (X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000231 TM.getRelocationModel() != Reloc::Static) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000232 // Link-once, External, or Weakly-linked global variables need
233 // non-lazily-resolved stubs
Evan Cheng25ab6902006-09-08 06:48:29 +0000234 if (isExt) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000235 // Dynamically-resolved functions need a stub for the function.
Evan Cheng25ab6902006-09-08 06:48:29 +0000236 if (isCallOp && isa<Function>(GV)) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000237 FnStubs.insert(Name);
238 O << "L" << Name << "$stub";
239 } else {
240 GVStubs.insert(Name);
241 O << "L" << Name << "$non_lazy_ptr";
242 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000243 } else {
244 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000245 O << "__imp_";
246 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000247 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000248 }
249
Chris Lattner35d86fe2006-07-26 21:12:04 +0000250 if (!isCallOp && TM.getRelocationModel() == Reloc::PIC_)
Evan Chenga0ea0532006-02-23 02:43:52 +0000251 O << "-\"L" << getFunctionNumber() << "$pb\"";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 } else {
253 if (GV->hasDLLImportLinkage()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000254 O << "__imp_";
255 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 O << Name;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000257 }
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000258
Evan Cheng6d7d3102006-12-01 07:38:23 +0000259 if (GV->hasExternalWeakLinkage())
Anton Korobeynikov78ee7b72006-12-01 00:25:12 +0000260 ExtWeakSymbols.insert(Name);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000261
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000262 int Offset = MO.getOffset();
263 if (Offset > 0)
264 O << "+" << Offset;
265 else if (Offset < 0)
266 O << Offset;
Evan Cheng25ab6902006-09-08 06:48:29 +0000267
Evan Chengd0ff02c2006-11-29 23:19:46 +0000268 if (isMemOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 Subtarget->is64Bit()) {
270 if (isExt && TM.getRelocationModel() != Reloc::Static)
271 O << "@GOTPCREL";
272 O << "(%rip)";
273 }
274
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000275 return;
276 }
Chris Lattnera3b8c572006-02-06 23:41:19 +0000277 case MachineOperand::MO_ExternalSymbol: {
278 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng932ad512006-05-25 21:59:08 +0000279 if (isCallOp &&
Evan Cheng25ab6902006-09-08 06:48:29 +0000280 X86PICStyle == PICStyle::Stub &&
Evan Cheng932ad512006-05-25 21:59:08 +0000281 TM.getRelocationModel() != Reloc::Static) {
Jim Laskey563321a2006-09-06 18:34:40 +0000282 std::string Name(TAI->getGlobalPrefix());
Evan Cheng4c1aa862006-02-22 20:19:42 +0000283 Name += MO.getSymbolName();
Nate Begeman72b286b2005-07-08 00:23:26 +0000284 FnStubs.insert(Name);
285 O << "L" << Name << "$stub";
286 return;
287 }
Evan Cheng4c1aa862006-02-22 20:19:42 +0000288 if (!isCallOp) O << '$';
Jim Laskey563321a2006-09-06 18:34:40 +0000289 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Evan Cheng25ab6902006-09-08 06:48:29 +0000290
291 if (!isCallOp &&
292 Subtarget->is64Bit())
293 O << "(%rip)";
294
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000295 return;
Chris Lattnera3b8c572006-02-06 23:41:19 +0000296 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000297 default:
298 O << "<unknown operand type>"; return;
299 }
300}
301
Nate Begeman391c5d22005-11-30 18:54:35 +0000302void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000303 unsigned char value = MI->getOperand(Op).getImmedValue();
304 assert(value <= 7 && "Invalid ssecc argument!");
305 switch (value) {
306 case 0: O << "eq"; break;
307 case 1: O << "lt"; break;
308 case 2: O << "le"; break;
309 case 3: O << "unord"; break;
310 case 4: O << "neq"; break;
311 case 5: O << "nlt"; break;
312 case 6: O << "nle"; break;
313 case 7: O << "ord"; break;
314 }
315}
316
Evan Cheng25ab6902006-09-08 06:48:29 +0000317void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
318 const char *Modifier){
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000319 assert(isMem(MI, Op) && "Invalid memory reference!");
320
321 const MachineOperand &BaseReg = MI->getOperand(Op);
322 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
323 const MachineOperand &IndexReg = MI->getOperand(Op+2);
324 const MachineOperand &DispSpec = MI->getOperand(Op+3);
325
326 if (BaseReg.isFrameIndex()) {
327 O << "[frame slot #" << BaseReg.getFrameIndex();
328 if (DispSpec.getImmedValue())
329 O << " + " << DispSpec.getImmedValue();
330 O << "]";
331 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000332 }
333
Evan Chengc9676de2006-08-29 22:14:48 +0000334 if (DispSpec.isGlobalAddress() ||
335 DispSpec.isConstantPoolIndex() ||
336 DispSpec.isJumpTableIndex()) {
Evan Cheng2338c5c2006-02-07 08:38:37 +0000337 printOperand(MI, Op+3, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000338 } else {
339 int DispVal = DispSpec.getImmedValue();
340 if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg()))
341 O << DispVal;
342 }
343
344 if (IndexReg.getReg() || BaseReg.getReg()) {
345 O << "(";
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (BaseReg.getReg()) {
347 printOperand(MI, Op, Modifier);
348 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000349
350 if (IndexReg.getReg()) {
351 O << ",";
Evan Cheng25ab6902006-09-08 06:48:29 +0000352 printOperand(MI, Op+2, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000353 if (ScaleVal != 1)
354 O << "," << ScaleVal;
355 }
356
357 O << ")";
358 }
359}
360
Evan Cheng7ccced62006-02-18 00:15:05 +0000361void X86ATTAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
362 O << "\"L" << getFunctionNumber() << "$pb\"\n";
363 O << "\"L" << getFunctionNumber() << "$pb\":";
364}
365
Evan Cheng62f27002006-04-28 23:11:40 +0000366
Evan Cheng55c25f22006-04-28 23:19:39 +0000367bool X86ATTAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000368 const char Mode) {
369 const MRegisterInfo &RI = *TM.getRegisterInfo();
370 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000371 switch (Mode) {
372 default: return true; // Unknown mode.
373 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000374 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000375 break;
376 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000377 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000378 break;
379 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000380 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000381 break;
382 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000383 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000384 break;
385 }
386
Evan Cheng8f7f7122006-05-05 05:40:20 +0000387 O << '%';
388 for (const char *Name = RI.get(Reg).Name; *Name; ++Name)
389 O << (char)tolower(*Name);
Evan Cheng62f27002006-04-28 23:11:40 +0000390 return false;
391}
392
Evan Cheng3d48a902006-04-28 21:19:05 +0000393/// PrintAsmOperand - Print out an operand for an inline asm expression.
394///
395bool X86ATTAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
396 unsigned AsmVariant,
397 const char *ExtraCode) {
398 // Does this asm operand have a single letter operand modifier?
399 if (ExtraCode && ExtraCode[0]) {
400 if (ExtraCode[1] != 0) return true; // Unknown modifier.
401
402 switch (ExtraCode[0]) {
403 default: return true; // Unknown modifier.
Chris Lattner0d924992006-10-31 20:12:30 +0000404 case 'c': // Don't print "$" before a global var name.
405 printOperand(MI, OpNo, "mem");
406 return false;
Evan Cheng62f27002006-04-28 23:11:40 +0000407 case 'b': // Print QImode register
408 case 'h': // Print QImode high register
409 case 'w': // Print HImode register
410 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000411 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000412 }
413 }
414
415 printOperand(MI, OpNo);
416 return false;
417}
418
419bool X86ATTAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
420 unsigned OpNo,
421 unsigned AsmVariant,
422 const char *ExtraCode) {
423 if (ExtraCode && ExtraCode[0])
424 return true; // Unknown modifier.
425 printMemReference(MI, OpNo);
426 return false;
427}
428
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000429/// printMachineInstruction -- Print out a single X86 LLVM instruction
430/// MI in Intel syntax to the current output stream.
431///
432void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
433 ++EmittedInsts;
Evan Cheng67caa392006-01-26 02:27:43 +0000434
Evan Cheng8f7f7122006-05-05 05:40:20 +0000435 // See if a truncate instruction can be turned into a nop.
436 switch (MI->getOpcode()) {
437 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000438 case X86::TRUNC_64to32:
439 case X86::TRUNC_64to16:
440 case X86::TRUNC_32to16:
441 case X86::TRUNC_32to8:
442 case X86::TRUNC_16to8:
443 case X86::TRUNC_32_to8:
444 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000445 const MachineOperand &MO0 = MI->getOperand(0);
446 const MachineOperand &MO1 = MI->getOperand(1);
447 unsigned Reg0 = MO0.getReg();
448 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000449 unsigned Opc = MI->getOpcode();
450 if (Opc == X86::TRUNC_64to32)
451 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
452 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000453 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000454 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000455 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000456 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000457 if (Reg0 != Reg1)
458 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000459 break;
460 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000461 case X86::PsMOVZX64rr32:
462 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
463 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000464 }
465
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000466 // Call the autogenerated instruction printer routines.
467 printInstruction(MI);
468}
469
470// Include the auto-generated portion of the assembly writer.
471#include "X86GenAsmWriter.inc"
472