blob: a3e5286d8eeec9d34c7dec5e50167e0c3558ccc0 [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 Lattneraf76e592009-08-22 20:48:53 +000029#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000030#include "llvm/MC/MCStreamer.h"
31#include "llvm/MC/MCSymbol.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000034#include "llvm/Support/ErrorHandling.h"
35#include "llvm/Support/Mangler.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000036using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037
Chris Lattner95b2c7d2006-12-19 22:59:26 +000038STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000040static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
41 const TargetData *TD) {
42 X86MachineFunctionInfo Info;
43 uint64_t Size = 0;
44
45 switch (F->getCallingConv()) {
46 case CallingConv::X86_StdCall:
47 Info.setDecorationStyle(StdCall);
48 break;
49 case CallingConv::X86_FastCall:
50 Info.setDecorationStyle(FastCall);
51 break;
52 default:
53 return Info;
54 }
55
56 unsigned argNum = 1;
57 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
58 AI != AE; ++AI, ++argNum) {
59 const Type* Ty = AI->getType();
60
61 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000062 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000063 Ty = cast<PointerType>(Ty)->getElementType();
64
65 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000066 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000067 }
68
69 // We're not supporting tooooo huge arguments :)
70 Info.setBytesToPopOnReturn((unsigned int)Size);
71 return Info;
72}
73
74
75/// decorateName - Query FunctionInfoMap and use this information for various
76/// name decoration.
77void X86IntelAsmPrinter::decorateName(std::string &Name,
78 const GlobalValue *GV) {
79 const Function *F = dyn_cast<Function>(GV);
80 if (!F) return;
81
82 // We don't want to decorate non-stdcall or non-fastcall functions right now
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000083 CallingConv::ID CC = F->getCallingConv();
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000084 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
85 return;
86
87 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
88
89 const X86MachineFunctionInfo *Info;
90 if (info_item == FunctionInfoMap.end()) {
91 // Calculate apropriate function info and populate map
92 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
93 Info = &FunctionInfoMap[F];
94 } else {
95 Info = &info_item->second;
96 }
97
98 const FunctionType *FT = F->getFunctionType();
99 switch (Info->getDecorationStyle()) {
100 case None:
101 break;
102 case StdCall:
103 // "Pure" variadic functions do not receive @0 suffix.
104 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
105 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
106 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
107 break;
108 case FastCall:
109 // "Pure" variadic functions do not receive @0 suffix.
110 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
111 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
112 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
113
114 if (Name[0] == '_')
115 Name[0] = '@';
116 else
117 Name = '@' + Name;
118
119 break;
120 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000121 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000122 }
123}
124
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000125/// runOnMachineFunction - This uses the printMachineInstruction()
126/// method to print assembly for each instruction.
127///
128bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000129 this->MF = &MF;
Chris Lattner8b8b9512005-11-21 07:51:23 +0000130 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131 O << "\n\n";
132
133 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000134 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000135
136 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000137 const Function *F = MF.getFunction();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000138 CallingConv::ID CC = F->getCallingConv();
Bill Wendling20c568f2009-06-30 22:38:32 +0000139 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000140
141 // Populate function information map. Actually, We don't want to populate
142 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000143 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000144 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000145
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000146 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000147
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000148 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattner17637662009-08-03 18:06:07 +0000149
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000150 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000151 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000152 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000153 case Function::LinkerPrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000154 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000155 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000156 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000157 case Function::DLLExportLinkage:
158 DLLExportedFns.insert(CurrentFnName);
159 //FALLS THROUGH
160 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000161 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000162 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000163 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000164 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000165
Jeff Cohenc884db42006-05-02 01:16:28 +0000166 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000167
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000168 // Print out code for the function.
169 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
170 I != E; ++I) {
171 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000172 if (!I->pred_empty()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000173 EmitBasicBlockStart(I);
Nate Begemancdf38c42006-05-02 05:37:32 +0000174 O << '\n';
175 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000176 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
177 II != E; ++II) {
178 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000179 printMachineInstruction(II);
180 }
181 }
182
Chris Lattner1da31ee2006-10-05 03:01:21 +0000183 // Print out jump tables referenced by the function.
184 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
185
Jeff Cohenc884db42006-05-02 01:16:28 +0000186 O << CurrentFnName << "\tendp\n";
187
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000188 // We didn't modify anything.
189 return false;
190}
191
Nate Begeman391c5d22005-11-30 18:54:35 +0000192void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000193 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000194 assert(value <= 7 && "Invalid ssecc argument!");
195 switch (value) {
196 case 0: O << "eq"; break;
197 case 1: O << "lt"; break;
198 case 2: O << "le"; break;
199 case 3: O << "unord"; break;
200 case 4: O << "neq"; break;
201 case 5: O << "nlt"; break;
202 case 6: O << "nle"; break;
203 case 7: O << "ord"; break;
204 }
205}
206
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000207void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000208 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000210 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000211 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000212 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000213 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersone50ed302009-08-10 22:56:29 +0000214 EVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Owen Anderson825b72b2009-08-11 20:47:22 +0000215 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
216 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000217 Reg = getX86SubSuperRegister(Reg, VT);
218 }
Evan Chengae270f62008-07-07 22:21:06 +0000219 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000220 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000221 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000223 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000224 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000225 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000226 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000227 case MachineOperand::MO_JumpTableIndex: {
228 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
229 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000230 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000231 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000232 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000233 }
Evan Chenga09bd812006-02-26 08:28:12 +0000234 case MachineOperand::MO_ConstantPoolIndex: {
235 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
236 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000237 O << "[" << MAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000238 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000239 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000240 O << "]";
241 return;
242 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000244 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000245 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000246 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000247 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000248
Chris Lattner7680e732009-06-20 19:34:09 +0000249 if (!isMemOp) O << "OFFSET ";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000250
251 // Handle dllimport linkage.
252 // FIXME: This should be fixed with full support of stdcall & fastcall
253 // CC's
254 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000255 O << "__imp_";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000256
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000257 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000258 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000259 return;
260 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000261 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000262 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000264 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 default:
266 O << "<unknown operand type>"; return;
267 }
268}
269
Chris Lattner7680e732009-06-20 19:34:09 +0000270void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
271 const MachineOperand &MO = MI->getOperand(OpNo);
272 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000273 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +0000274 case MachineOperand::MO_Immediate:
275 O << MO.getImm();
276 return;
277 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000278 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Chris Lattner7680e732009-06-20 19:34:09 +0000279 return;
280
281 case MachineOperand::MO_GlobalAddress: {
282 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000283 std::string Name = Mang->getMangledName(GV);
Chris Lattner7680e732009-06-20 19:34:09 +0000284 decorateName(Name, GV);
285
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000286 // Handle dllimport linkage.
287 // FIXME: This should be fixed with full support of stdcall & fastcall
288 // CC's
289 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner7680e732009-06-20 19:34:09 +0000290 O << "__imp_";
Chris Lattner7680e732009-06-20 19:34:09 +0000291 O << Name;
292 printOffset(MO.getOffset());
293 return;
294 }
295
296 case MachineOperand::MO_ExternalSymbol:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000297 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattner7680e732009-06-20 19:34:09 +0000298 return;
299 }
300}
301
302
Rafael Espindola094fad32009-04-08 21:14:34 +0000303void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
304 unsigned Op,
305 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000307 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000308 const MachineOperand &IndexReg = MI->getOperand(Op+2);
309 const MachineOperand &DispSpec = MI->getOperand(Op+3);
310
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000311 O << "[";
312 bool NeedPlus = false;
313 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000315 NeedPlus = true;
316 }
317
318 if (IndexReg.getReg()) {
319 if (NeedPlus) O << " + ";
320 if (ScaleVal != 1)
321 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000322 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000323 NeedPlus = true;
324 }
325
Dan Gohmand735b802008-10-03 15:45:36 +0000326 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
327 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000328 if (NeedPlus)
329 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000330 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000331 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000332 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000333 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000334 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335 if (DispVal > 0)
336 O << " + ";
337 else {
338 O << " - ";
339 DispVal = -DispVal;
340 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000341 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000342 O << DispVal;
343 }
344 }
345 O << "]";
346}
347
Rafael Espindola094fad32009-04-08 21:14:34 +0000348void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
349 const char *Modifier) {
350 assert(isMem(MI, Op) && "Invalid memory reference!");
351 MachineOperand Segment = MI->getOperand(Op+4);
352 if (Segment.getReg()) {
353 printOperand(MI, Op+4, Modifier);
354 O << ':';
355 }
356 printLeaMemReference(MI, Op, Modifier);
357}
358
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000359void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000360 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000361 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000362 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000363
Chris Lattner33adcfb2009-08-22 21:43:10 +0000364 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000365 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +0000366 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Evan Chengcc415862007-11-09 01:32:10 +0000367 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
368}
369
Evan Cheng7ccced62006-02-18 00:15:05 +0000370void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedmanaace4b12009-06-19 04:48:38 +0000371 O << "L" << getFunctionNumber() << "$pb\n";
372 O << "L" << getFunctionNumber() << "$pb:";
Evan Cheng7ccced62006-02-18 00:15:05 +0000373}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000374
Evan Cheng55c25f22006-04-28 23:19:39 +0000375bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000376 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000377 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000378 switch (Mode) {
379 default: return true; // Unknown mode.
380 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000381 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000382 break;
383 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000385 break;
386 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000387 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000388 break;
389 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000391 break;
392 }
393
Eli Friedmanaace4b12009-06-19 04:48:38 +0000394 O << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000395 return false;
396}
397
Evan Cheng3d48a902006-04-28 21:19:05 +0000398/// PrintAsmOperand - Print out an operand for an inline asm expression.
399///
400bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000401 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000402 const char *ExtraCode) {
403 // Does this asm operand have a single letter operand modifier?
404 if (ExtraCode && ExtraCode[0]) {
405 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000406
Evan Cheng3d48a902006-04-28 21:19:05 +0000407 switch (ExtraCode[0]) {
408 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000409 case 'b': // Print QImode register
410 case 'h': // Print QImode high register
411 case 'w': // Print HImode register
412 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000413 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000414 }
415 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000416
Evan Cheng3d48a902006-04-28 21:19:05 +0000417 printOperand(MI, OpNo);
418 return false;
419}
420
421bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
422 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000423 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000424 const char *ExtraCode) {
425 if (ExtraCode && ExtraCode[0])
426 return true; // Unknown modifier.
427 printMemReference(MI, OpNo);
428 return false;
429}
430
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000431/// printMachineInstruction -- Print out a single X86 LLVM instruction
432/// MI in Intel syntax to the current output stream.
433///
434void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
435 ++EmittedInsts;
436
Chris Lattner634cca32009-09-09 20:34:59 +0000437 processDebugLoc(MI->getDebugLoc());
438
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000439 // Call the autogenerated instruction printer routines.
440 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000441
442 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
443 EmitComments(*MI);
444 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000445}
446
447bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000448 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000449
Eli Friedmanaace4b12009-06-19 04:48:38 +0000450 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000451
452 // Emit declarations for external functions.
453 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000454 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000455 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000456 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000457
Eli Friedmanaace4b12009-06-19 04:48:38 +0000458 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000459 if (I->hasDLLImportLinkage()) {
460 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000461 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000462 O << Name << ":near\n";
463 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000464
Jeff Cohend43b18d2006-05-06 21:27:14 +0000465 // Emit declarations for external globals. Note that VC++ always declares
466 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000467 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
468 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000469 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000470 std::string Name = Mang->getMangledName(I);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000471
Eli Friedmanaace4b12009-06-19 04:48:38 +0000472 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000473 if (I->hasDLLImportLinkage()) {
474 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000475 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000476 O << Name << ":byte\n";
477 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000478 }
479
Dan Gohmanb8275a32007-07-25 19:33:14 +0000480 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000481}
482
Chris Lattner40bbebd2009-07-21 18:38:57 +0000483void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
484 // Check to see if this is a special global used by LLVM, if so, emit it.
485 if (GV->isDeclaration() ||
486 EmitSpecialLLVMGlobal(GV))
487 return;
488
Jeff Cohend43b18d2006-05-06 21:27:14 +0000489 const TargetData *TD = TM.getTargetData();
490
Chris Lattner40bbebd2009-07-21 18:38:57 +0000491 std::string name = Mang->getMangledName(GV);
492 Constant *C = GV->getInitializer();
493 unsigned Align = TD->getPreferredAlignmentLog(GV);
494 bool bCustomSegment = false;
495
496 switch (GV->getLinkage()) {
497 case GlobalValue::CommonLinkage:
498 case GlobalValue::LinkOnceAnyLinkage:
499 case GlobalValue::LinkOnceODRLinkage:
500 case GlobalValue::WeakAnyLinkage:
501 case GlobalValue::WeakODRLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000502 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000503 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
504 bCustomSegment = true;
505 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
506 // are also available.
507 break;
508 case GlobalValue::AppendingLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000509 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000510 O << name << "?\tSEGMENT PARA public 'DATA'\n";
511 bCustomSegment = true;
512 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
513 // are also available.
514 break;
515 case GlobalValue::DLLExportLinkage:
516 DLLExportedGVs.insert(name);
517 // FALL THROUGH
518 case GlobalValue::ExternalLinkage:
519 O << "\tpublic " << name << "\n";
520 // FALL THROUGH
521 case GlobalValue::InternalLinkage:
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000522 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner40bbebd2009-07-21 18:38:57 +0000523 break;
524 default:
525 llvm_unreachable("Unknown linkage type!");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000526 }
Chris Lattner40bbebd2009-07-21 18:38:57 +0000527
528 if (!bCustomSegment)
529 EmitAlignment(Align, GV);
530
531 O << name << ":";
532 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000533 O.PadToColumn(MAI->getCommentColumn());
534 O << MAI->getCommentString()
Chris Lattner40bbebd2009-07-21 18:38:57 +0000535 << " " << GV->getName();
536 O << '\n';
537
538 EmitGlobalConstant(C);
539
540 if (bCustomSegment)
541 O << name << "?\tends\n";
542}
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000543
Chris Lattner40bbebd2009-07-21 18:38:57 +0000544bool X86IntelAsmPrinter::doFinalization(Module &M) {
545 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000546 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Evan Cheng03339202008-09-20 00:13:08 +0000547 O << "; WARNING: The following code is valid only with MASM v8.x"
548 << "and (possible) higher\n"
549 << "; This version of MASM is usually shipped with Microsoft "
550 << "Visual Studio 2005\n"
551 << "; or (possible) further versions. Unfortunately, there is no "
552 << "way to support\n"
553 << "; dllexported symbols in the earlier versions of MASM in fully "
554 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000555 O << "_drectve\t segment info alias('.drectve')\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000556
Chris Lattner17637662009-08-03 18:06:07 +0000557 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
558 e = DLLExportedGVs.end();
559 i != e; ++i)
560 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000561
Chris Lattner17637662009-08-03 18:06:07 +0000562 for (StringSet<>::iterator i = DLLExportedFns.begin(),
563 e = DLLExportedFns.end();
564 i != e; ++i)
565 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000566
Anton Korobeynikov85739862008-06-27 21:22:49 +0000567 O << "_drectve\t ends\n";
Chris Lattner17637662009-08-03 18:06:07 +0000568 }
Anton Korobeynikov85739862008-06-27 21:22:49 +0000569
Jeff Cohend43b18d2006-05-06 21:27:14 +0000570 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000571 bool Result = AsmPrinter::doFinalization(M);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000572 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000573 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000574}
575
Jeff Cohenc884db42006-05-02 01:16:28 +0000576void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
577 unsigned NumElts = CVA->getNumOperands();
Chris Lattner73d28f42009-09-13 19:10:08 +0000578 if (NumElts == 0) return;
579
580 // ML does not have escape sequences except '' for '. It also has a maximum
581 // string length of 255.
582 unsigned len = 0;
583 bool inString = false;
584 for (unsigned i = 0; i < NumElts; i++) {
585 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
586 if (len == 0)
587 O << "\tdb ";
Jeff Cohenc884db42006-05-02 01:16:28 +0000588
Chris Lattner73d28f42009-09-13 19:10:08 +0000589 if (n >= 32 && n <= 127) {
590 if (!inString) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000591 if (len > 0) {
Chris Lattner73d28f42009-09-13 19:10:08 +0000592 O << ",'";
593 len += 2;
594 } else {
595 O << "'";
Jeff Cohenc884db42006-05-02 01:16:28 +0000596 len++;
597 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000598 inString = true;
Jeff Cohenc884db42006-05-02 01:16:28 +0000599 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000600 if (n == '\'') {
Jeff Cohenc884db42006-05-02 01:16:28 +0000601 O << "'";
Chris Lattner73d28f42009-09-13 19:10:08 +0000602 len++;
603 }
604 O << char(n);
605 } else {
606 if (inString) {
607 O << "'";
608 len++;
609 inString = false;
610 }
611 if (len > 0) {
612 O << ",";
613 len++;
614 }
615 O << n;
616 len += 1 + (n > 9) + (n > 99);
Jeff Cohenc884db42006-05-02 01:16:28 +0000617 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000618
619 if (len > 60) {
620 if (inString) {
621 O << "'";
622 inString = false;
623 }
624 O << "\n";
625 len = 0;
626 }
627 }
628
629 if (len > 0) {
630 if (inString)
631 O << "'";
632 O << "\n";
Jeff Cohenc884db42006-05-02 01:16:28 +0000633 }
634}
635
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000636// Include the auto-generated portion of the assembly writer.
637#include "X86GenAsmWriter1.inc"