blob: 9fd504d3f44b8ac948337fe7bd207ddb2b138b3f [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains a printer that converts from our internal representation
11// of machine-dependent LLVM code to Intel format assembly language.
12// This printer is the output mechanism used by `llc'.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86IntelAsmPrinter.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86InstrInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000019#include "X86MCAsmInfo.h"
Cedric Venetd85f51a2008-08-24 12:30:46 +000020#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000021#include "llvm/CallingConv.h"
Jeff Cohenc884db42006-05-02 01:16:28 +000022#include "llvm/Constants.h"
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringExtras.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027#include "llvm/Assembly/Writer.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000028#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000030#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/Mangler.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000036
Chris Lattner95b2c7d2006-12-19 22:59:26 +000037STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000039static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
40 const TargetData *TD) {
41 X86MachineFunctionInfo Info;
42 uint64_t Size = 0;
43
44 switch (F->getCallingConv()) {
45 case CallingConv::X86_StdCall:
46 Info.setDecorationStyle(StdCall);
47 break;
48 case CallingConv::X86_FastCall:
49 Info.setDecorationStyle(FastCall);
50 break;
51 default:
52 return Info;
53 }
54
55 unsigned argNum = 1;
56 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
57 AI != AE; ++AI, ++argNum) {
58 const Type* Ty = AI->getType();
59
60 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000061 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000062 Ty = cast<PointerType>(Ty)->getElementType();
63
64 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000065 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000066 }
67
68 // We're not supporting tooooo huge arguments :)
69 Info.setBytesToPopOnReturn((unsigned int)Size);
70 return Info;
71}
72
73
74/// decorateName - Query FunctionInfoMap and use this information for various
75/// name decoration.
76void X86IntelAsmPrinter::decorateName(std::string &Name,
77 const GlobalValue *GV) {
78 const Function *F = dyn_cast<Function>(GV);
79 if (!F) return;
80
81 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000082 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000083 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
84 return;
85
86 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
87
88 const X86MachineFunctionInfo *Info;
89 if (info_item == FunctionInfoMap.end()) {
90 // Calculate apropriate function info and populate map
91 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
92 Info = &FunctionInfoMap[F];
93 } else {
94 Info = &info_item->second;
95 }
96
97 const FunctionType *FT = F->getFunctionType();
98 switch (Info->getDecorationStyle()) {
99 case None:
100 break;
101 case StdCall:
102 // "Pure" variadic functions do not receive @0 suffix.
103 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
104 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
105 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
106 break;
107 case FastCall:
108 // "Pure" variadic functions do not receive @0 suffix.
109 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
110 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
111 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
112
113 if (Name[0] == '_')
114 Name[0] = '@';
115 else
116 Name = '@' + Name;
117
118 break;
119 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000120 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000121 }
122}
123
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000124/// runOnMachineFunction - This uses the printMachineInstruction()
125/// method to print assembly for each instruction.
126///
127bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000128 this->MF = &MF;
Chris Lattner8b8b9512005-11-21 07:51:23 +0000129 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130 O << "\n\n";
131
132 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000133 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000134
135 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000136 const Function *F = MF.getFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000137 CallingConv::ID CC = F->getCallingConv();
Bill Wendling20c568f2009-06-30 22:38:32 +0000138 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000139
140 // Populate function information map. Actually, We don't want to populate
141 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000142 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000143 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000144
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000145 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000146
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000147 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner17637662009-08-03 18:06:07 +0000148
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000149 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000150 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000151 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000152 case Function::LinkerPrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000153 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000154 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000155 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000156 case Function::DLLExportLinkage:
157 DLLExportedFns.insert(CurrentFnName);
158 //FALLS THROUGH
159 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000160 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000161 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000162 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000163 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000164
Jeff Cohenc884db42006-05-02 01:16:28 +0000165 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000166
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000167 // Print out code for the function.
168 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
169 I != E; ++I) {
170 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000171 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000172 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000173 O << '\n';
174 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
176 II != E; ++II) {
177 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000178 printMachineInstruction(II);
179 }
180 }
181
Chris Lattner1da31ee2006-10-05 03:01:21 +0000182 // Print out jump tables referenced by the function.
183 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
184
Jeff Cohenc884db42006-05-02 01:16:28 +0000185 O << CurrentFnName << "\tendp\n";
186
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187 // We didn't modify anything.
188 return false;
189}
190
Nate Begeman391c5d22005-11-30 18:54:35 +0000191void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000192 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000193 assert(value <= 7 && "Invalid ssecc argument!");
194 switch (value) {
195 case 0: O << "eq"; break;
196 case 1: O << "lt"; break;
197 case 2: O << "le"; break;
198 case 3: O << "unord"; break;
199 case 4: O << "neq"; break;
200 case 5: O << "nlt"; break;
201 case 6: O << "nle"; break;
202 case 7: O << "ord"; break;
203 }
204}
205
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000206void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000207 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000208 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000209 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000210 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000211 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000212 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000213 EVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000214 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
215 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000216 Reg = getX86SubSuperRegister(Reg, VT);
217 }
Evan Chengae270f62008-07-07 22:21:06 +0000218 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000219 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000220 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000221 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000222 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000223 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000224 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000225 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000226 case MachineOperand::MO_JumpTableIndex: {
227 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
228 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000229 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000230 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000231 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000232 }
Evan Chenga09bd812006-02-26 08:28:12 +0000233 case MachineOperand::MO_ConstantPoolIndex: {
234 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
235 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000236 O << "[" << MAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000237 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000238 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000239 O << "]";
240 return;
241 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000242 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000243 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000244 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000245 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000246 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000247
Chris Lattner7680e732009-06-20 19:34:09 +0000248 if (!isMemOp) O << "OFFSET ";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000249
250 // Handle dllimport linkage.
251 // FIXME: This should be fixed with full support of stdcall & fastcall
252 // CC's
253 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000254 O << "__imp_";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000255
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000256 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000257 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000258 return;
259 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000260 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000261 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000262 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000263 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000264 default:
265 O << "<unknown operand type>"; return;
266 }
267}
268
Chris Lattner7680e732009-06-20 19:34:09 +0000269void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
270 const MachineOperand &MO = MI->getOperand(OpNo);
271 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000272 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +0000273 case MachineOperand::MO_Immediate:
274 O << MO.getImm();
275 return;
276 case MachineOperand::MO_MachineBasicBlock:
Dan Gohmancf20ac42009-08-13 01:36:44 +0000277 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattner7680e732009-06-20 19:34:09 +0000278 return;
279
280 case MachineOperand::MO_GlobalAddress: {
281 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000282 std::string Name = Mang->getMangledName(GV);
Chris Lattner7680e732009-06-20 19:34:09 +0000283 decorateName(Name, GV);
284
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000285 // Handle dllimport linkage.
286 // FIXME: This should be fixed with full support of stdcall & fastcall
287 // CC's
288 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner7680e732009-06-20 19:34:09 +0000289 O << "__imp_";
Chris Lattner7680e732009-06-20 19:34:09 +0000290 O << Name;
291 printOffset(MO.getOffset());
292 return;
293 }
294
295 case MachineOperand::MO_ExternalSymbol:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000296 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattner7680e732009-06-20 19:34:09 +0000297 return;
298 }
299}
300
301
Rafael Espindola094fad32009-04-08 21:14:34 +0000302void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
303 unsigned Op,
304 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000306 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000307 const MachineOperand &IndexReg = MI->getOperand(Op+2);
308 const MachineOperand &DispSpec = MI->getOperand(Op+3);
309
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000310 O << "[";
311 bool NeedPlus = false;
312 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000313 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000314 NeedPlus = true;
315 }
316
317 if (IndexReg.getReg()) {
318 if (NeedPlus) O << " + ";
319 if (ScaleVal != 1)
320 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000321 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000322 NeedPlus = true;
323 }
324
Dan Gohmand735b802008-10-03 15:45:36 +0000325 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
326 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000327 if (NeedPlus)
328 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000329 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000330 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000331 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000332 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000333 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000334 if (DispVal > 0)
335 O << " + ";
336 else {
337 O << " - ";
338 DispVal = -DispVal;
339 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000340 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000341 O << DispVal;
342 }
343 }
344 O << "]";
345}
346
Rafael Espindola094fad32009-04-08 21:14:34 +0000347void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
348 const char *Modifier) {
349 assert(isMem(MI, Op) && "Invalid memory reference!");
350 MachineOperand Segment = MI->getOperand(Op+4);
351 if (Segment.getReg()) {
352 printOperand(MI, Op+4, Modifier);
353 O << ':';
354 }
355 printLeaMemReference(MI, Op, Modifier);
356}
357
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000358void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000359 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000360 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000361 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000362
Chris Lattner33adcfb2009-08-22 21:43:10 +0000363 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000364 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000365 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000366 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
367}
368
Evan Cheng7ccced62006-02-18 00:15:05 +0000369void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedmanaace4b12009-06-19 04:48:38 +0000370 O << "L" << getFunctionNumber() << "$pb\n";
371 O << "L" << getFunctionNumber() << "$pb:";
Evan Cheng7ccced62006-02-18 00:15:05 +0000372}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000373
Evan Cheng55c25f22006-04-28 23:19:39 +0000374bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000375 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000376 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000377 switch (Mode) {
378 default: return true; // Unknown mode.
379 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000380 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000381 break;
382 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000384 break;
385 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000387 break;
388 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000389 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000390 break;
391 }
392
Eli Friedmanaace4b12009-06-19 04:48:38 +0000393 O << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000394 return false;
395}
396
Evan Cheng3d48a902006-04-28 21:19:05 +0000397/// PrintAsmOperand - Print out an operand for an inline asm expression.
398///
399bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000400 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000401 const char *ExtraCode) {
402 // Does this asm operand have a single letter operand modifier?
403 if (ExtraCode && ExtraCode[0]) {
404 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000405
Evan Cheng3d48a902006-04-28 21:19:05 +0000406 switch (ExtraCode[0]) {
407 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000408 case 'b': // Print QImode register
409 case 'h': // Print QImode high register
410 case 'w': // Print HImode register
411 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000412 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000413 }
414 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000415
Evan Cheng3d48a902006-04-28 21:19:05 +0000416 printOperand(MI, OpNo);
417 return false;
418}
419
420bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
421 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000422 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000423 const char *ExtraCode) {
424 if (ExtraCode && ExtraCode[0])
425 return true; // Unknown modifier.
426 printMemReference(MI, OpNo);
427 return false;
428}
429
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000430/// printMachineInstruction -- Print out a single X86 LLVM instruction
431/// MI in Intel syntax to the current output stream.
432///
433void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
434 ++EmittedInsts;
435
Chris Lattner634cca32009-09-09 20:34:59 +0000436 processDebugLoc(MI->getDebugLoc());
437
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000438 // Call the autogenerated instruction printer routines.
439 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000440
441 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
442 EmitComments(*MI);
443 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000444}
445
446bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000447 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000448
Eli Friedmanaace4b12009-06-19 04:48:38 +0000449 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000450
451 // Emit declarations for external functions.
452 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000453 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000454 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000455 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000456
Eli Friedmanaace4b12009-06-19 04:48:38 +0000457 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000458 if (I->hasDLLImportLinkage()) {
459 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000460 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000461 O << Name << ":near\n";
462 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000463
Jeff Cohend43b18d2006-05-06 21:27:14 +0000464 // Emit declarations for external globals. Note that VC++ always declares
465 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000466 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
467 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000468 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000469 std::string Name = Mang->getMangledName(I);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000470
Eli Friedmanaace4b12009-06-19 04:48:38 +0000471 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000472 if (I->hasDLLImportLinkage()) {
473 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000474 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000475 O << Name << ":byte\n";
476 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000477 }
478
Dan Gohmanb8275a32007-07-25 19:33:14 +0000479 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000480}
481
Chris Lattner40bbebd2009-07-21 18:38:57 +0000482void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
483 // Check to see if this is a special global used by LLVM, if so, emit it.
484 if (GV->isDeclaration() ||
485 EmitSpecialLLVMGlobal(GV))
486 return;
487
Jeff Cohend43b18d2006-05-06 21:27:14 +0000488 const TargetData *TD = TM.getTargetData();
489
Chris Lattner40bbebd2009-07-21 18:38:57 +0000490 std::string name = Mang->getMangledName(GV);
491 Constant *C = GV->getInitializer();
492 unsigned Align = TD->getPreferredAlignmentLog(GV);
493 bool bCustomSegment = false;
494
495 switch (GV->getLinkage()) {
496 case GlobalValue::CommonLinkage:
497 case GlobalValue::LinkOnceAnyLinkage:
498 case GlobalValue::LinkOnceODRLinkage:
499 case GlobalValue::WeakAnyLinkage:
500 case GlobalValue::WeakODRLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000501 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000502 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
503 bCustomSegment = true;
504 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
505 // are also available.
506 break;
507 case GlobalValue::AppendingLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000508 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000509 O << name << "?\tSEGMENT PARA public 'DATA'\n";
510 bCustomSegment = true;
511 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
512 // are also available.
513 break;
514 case GlobalValue::DLLExportLinkage:
515 DLLExportedGVs.insert(name);
516 // FALL THROUGH
517 case GlobalValue::ExternalLinkage:
518 O << "\tpublic " << name << "\n";
519 // FALL THROUGH
520 case GlobalValue::InternalLinkage:
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000521 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner40bbebd2009-07-21 18:38:57 +0000522 break;
523 default:
524 llvm_unreachable("Unknown linkage type!");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000525 }
Chris Lattner40bbebd2009-07-21 18:38:57 +0000526
527 if (!bCustomSegment)
528 EmitAlignment(Align, GV);
529
530 O << name << ":";
531 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000532 O.PadToColumn(MAI->getCommentColumn());
533 O << MAI->getCommentString()
Chris Lattner40bbebd2009-07-21 18:38:57 +0000534 << " " << GV->getName();
535 O << '\n';
536
537 EmitGlobalConstant(C);
538
539 if (bCustomSegment)
540 O << name << "?\tends\n";
541}
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000542
Chris Lattner40bbebd2009-07-21 18:38:57 +0000543bool X86IntelAsmPrinter::doFinalization(Module &M) {
544 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000545 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Evan Cheng03339202008-09-20 00:13:08 +0000546 O << "; WARNING: The following code is valid only with MASM v8.x"
547 << "and (possible) higher\n"
548 << "; This version of MASM is usually shipped with Microsoft "
549 << "Visual Studio 2005\n"
550 << "; or (possible) further versions. Unfortunately, there is no "
551 << "way to support\n"
552 << "; dllexported symbols in the earlier versions of MASM in fully "
553 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000554 O << "_drectve\t segment info alias('.drectve')\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000555
Chris Lattner17637662009-08-03 18:06:07 +0000556 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
557 e = DLLExportedGVs.end();
558 i != e; ++i)
559 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000560
Chris Lattner17637662009-08-03 18:06:07 +0000561 for (StringSet<>::iterator i = DLLExportedFns.begin(),
562 e = DLLExportedFns.end();
563 i != e; ++i)
564 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000565
Anton Korobeynikov85739862008-06-27 21:22:49 +0000566 O << "_drectve\t ends\n";
Chris Lattner17637662009-08-03 18:06:07 +0000567 }
Anton Korobeynikov85739862008-06-27 21:22:49 +0000568
Jeff Cohend43b18d2006-05-06 21:27:14 +0000569 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000570 bool Result = AsmPrinter::doFinalization(M);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000571 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000572 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000573}
574
Jeff Cohenc884db42006-05-02 01:16:28 +0000575void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
576 unsigned NumElts = CVA->getNumOperands();
577 if (NumElts) {
578 // ML does not have escape sequences except '' for '. It also has a maximum
579 // string length of 255.
580 unsigned len = 0;
581 bool inString = false;
582 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000583 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000584 if (len == 0)
585 O << "\tdb ";
586
587 if (n >= 32 && n <= 127) {
588 if (!inString) {
589 if (len > 0) {
590 O << ",'";
591 len += 2;
592 } else {
593 O << "'";
594 len++;
595 }
596 inString = true;
597 }
598 if (n == '\'') {
599 O << "'";
600 len++;
601 }
602 O << char(n);
603 } else {
604 if (inString) {
605 O << "'";
606 len++;
607 inString = false;
608 }
609 if (len > 0) {
610 O << ",";
611 len++;
612 }
613 O << n;
614 len += 1 + (n > 9) + (n > 99);
615 }
616
617 if (len > 60) {
618 if (inString) {
619 O << "'";
620 inString = false;
621 }
622 O << "\n";
623 len = 0;
624 }
625 }
626
627 if (len > 0) {
628 if (inString)
629 O << "'";
630 O << "\n";
631 }
632 }
633}
634
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000635// Include the auto-generated portion of the assembly writer.
636#include "X86GenAsmWriter1.inc"