blob: 93faa531ba4e30f661c0df5dfcc44e4720d4ebe6 [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!");
Chris Lattnera1cb09e2009-09-13 19:44:38 +0000152 case Function::LinkOnceAnyLinkage:
153 case Function::LinkOnceODRLinkage:
154 case Function::WeakAnyLinkage:
155 case Function::WeakODRLinkage:
156
Rafael Espindolabb46f522009-01-15 20:18:42 +0000157 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000158 case Function::LinkerPrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000159 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000160 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000161 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000162 case Function::DLLExportLinkage:
163 DLLExportedFns.insert(CurrentFnName);
164 //FALLS THROUGH
165 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000166 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000167 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000168 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000169 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000170
Jeff Cohenc884db42006-05-02 01:16:28 +0000171 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000172
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000173 // Print out code for the function.
174 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
175 I != E; ++I) {
176 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000177 if (!I->pred_empty()) {
Chris Lattner70a54c02009-09-13 18:25:37 +0000178 EmitBasicBlockStart(I);
Nate Begemancdf38c42006-05-02 05:37:32 +0000179 O << '\n';
180 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000181 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182 II != E; ++II) {
183 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 printMachineInstruction(II);
185 }
186 }
187
Chris Lattner1da31ee2006-10-05 03:01:21 +0000188 // Print out jump tables referenced by the function.
189 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
190
Jeff Cohenc884db42006-05-02 01:16:28 +0000191 O << CurrentFnName << "\tendp\n";
192
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000193 // We didn't modify anything.
194 return false;
195}
196
Nate Begeman391c5d22005-11-30 18:54:35 +0000197void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000198 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000199 assert(value <= 7 && "Invalid ssecc argument!");
200 switch (value) {
201 case 0: O << "eq"; break;
202 case 1: O << "lt"; break;
203 case 2: O << "le"; break;
204 case 3: O << "unord"; break;
205 case 4: O << "neq"; break;
206 case 5: O << "nlt"; break;
207 case 6: O << "nle"; break;
208 case 7: O << "ord"; break;
209 }
210}
211
Chris Lattnera1cb09e2009-09-13 19:44:38 +0000212static void PrintRegName(raw_ostream &O, StringRef RegName) {
213 for (unsigned i = 0, e = RegName.size(); i != e; ++i)
214 O << (char)toupper(RegName[i]);
215}
216
Chris Lattnerc510f4c2009-09-13 20:15:16 +0000217void X86IntelAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo,
218 const char *Modifier) {
219 printOp(MI->getOperand(OpNo), Modifier);
220}
221
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000222void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000223 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000224 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000225 case MachineOperand::MO_Register: {
Chris Lattnera1cb09e2009-09-13 19:44:38 +0000226 unsigned Reg = MO.getReg();
227 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
228 EVT VT = (strcmp(Modifier,"subreg64") == 0) ?
229 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
230 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
231 Reg = getX86SubSuperRegister(Reg, VT);
232 }
Chris Lattnerc510f4c2009-09-13 20:15:16 +0000233 PrintRegName(O, getRegisterName(Reg));
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000234 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000235 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000236 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000237 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000238 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000239 case MachineOperand::MO_JumpTableIndex: {
240 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
241 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000242 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000243 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000244 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000245 }
Evan Chenga09bd812006-02-26 08:28:12 +0000246 case MachineOperand::MO_ConstantPoolIndex: {
247 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
248 if (!isMemOp) O << "OFFSET ";
Chris Lattner33adcfb2009-08-22 21:43:10 +0000249 O << "[" << MAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000250 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000251 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000252 O << "]";
253 return;
254 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000255 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000256 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000257 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000258 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000259 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000260
Chris Lattner7680e732009-06-20 19:34:09 +0000261 if (!isMemOp) O << "OFFSET ";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000262
263 // Handle dllimport linkage.
264 // FIXME: This should be fixed with full support of stdcall & fastcall
265 // CC's
266 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000267 O << "__imp_";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000268
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000269 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000270 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000271 return;
272 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000273 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000274 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000275 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000276 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277 default:
278 O << "<unknown operand type>"; return;
279 }
280}
281
Chris Lattner7680e732009-06-20 19:34:09 +0000282void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
283 const MachineOperand &MO = MI->getOperand(OpNo);
284 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000285 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +0000286 case MachineOperand::MO_Immediate:
287 O << MO.getImm();
288 return;
289 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000290 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Chris Lattner7680e732009-06-20 19:34:09 +0000291 return;
292
293 case MachineOperand::MO_GlobalAddress: {
294 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000295 std::string Name = Mang->getMangledName(GV);
Chris Lattner7680e732009-06-20 19:34:09 +0000296 decorateName(Name, GV);
297
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000298 // Handle dllimport linkage.
299 // FIXME: This should be fixed with full support of stdcall & fastcall
300 // CC's
301 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner7680e732009-06-20 19:34:09 +0000302 O << "__imp_";
Chris Lattner7680e732009-06-20 19:34:09 +0000303 O << Name;
304 printOffset(MO.getOffset());
305 return;
306 }
307
308 case MachineOperand::MO_ExternalSymbol:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000309 O << MAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattner7680e732009-06-20 19:34:09 +0000310 return;
311 }
312}
313
314
Rafael Espindola094fad32009-04-08 21:14:34 +0000315void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
316 unsigned Op,
317 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000318 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000319 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000320 const MachineOperand &IndexReg = MI->getOperand(Op+2);
321 const MachineOperand &DispSpec = MI->getOperand(Op+3);
322
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000323 O << "[";
324 bool NeedPlus = false;
325 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000326 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000327 NeedPlus = true;
328 }
329
330 if (IndexReg.getReg()) {
331 if (NeedPlus) O << " + ";
332 if (ScaleVal != 1)
333 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000334 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335 NeedPlus = true;
336 }
337
Dan Gohmand735b802008-10-03 15:45:36 +0000338 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
339 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000340 if (NeedPlus)
341 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000342 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000343 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000344 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000345 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000346 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000347 if (DispVal > 0)
348 O << " + ";
349 else {
350 O << " - ";
351 DispVal = -DispVal;
352 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000353 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000354 O << DispVal;
355 }
356 }
357 O << "]";
358}
359
Rafael Espindola094fad32009-04-08 21:14:34 +0000360void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
361 const char *Modifier) {
362 assert(isMem(MI, Op) && "Invalid memory reference!");
363 MachineOperand Segment = MI->getOperand(Op+4);
364 if (Segment.getReg()) {
365 printOperand(MI, Op+4, Modifier);
366 O << ':';
367 }
368 printLeaMemReference(MI, Op, Modifier);
369}
370
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000371void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000372 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000373 if (!MAI->getSetDirective())
Evan Chengcc415862007-11-09 01:32:10 +0000374 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000375
Chris Lattner33adcfb2009-08-22 21:43:10 +0000376 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Chengcc415862007-11-09 01:32:10 +0000377 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +0000378 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Evan Chengcc415862007-11-09 01:32:10 +0000379 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
380}
381
Evan Cheng7ccced62006-02-18 00:15:05 +0000382void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedmanaace4b12009-06-19 04:48:38 +0000383 O << "L" << getFunctionNumber() << "$pb\n";
384 O << "L" << getFunctionNumber() << "$pb:";
Evan Cheng7ccced62006-02-18 00:15:05 +0000385}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000386
Evan Cheng55c25f22006-04-28 23:19:39 +0000387bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000388 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000389 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000390 switch (Mode) {
391 default: return true; // Unknown mode.
392 case 'b': // Print QImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000394 break;
395 case 'h': // Print QImode high register
Owen Anderson825b72b2009-08-11 20:47:22 +0000396 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000397 break;
398 case 'w': // Print HImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000399 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000400 break;
401 case 'k': // Print SImode register
Owen Anderson825b72b2009-08-11 20:47:22 +0000402 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000403 break;
404 }
405
Chris Lattnerc510f4c2009-09-13 20:15:16 +0000406 PrintRegName(O, getRegisterName(Reg));
Evan Cheng62f27002006-04-28 23:11:40 +0000407 return false;
408}
409
Evan Cheng3d48a902006-04-28 21:19:05 +0000410/// PrintAsmOperand - Print out an operand for an inline asm expression.
411///
412bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000413 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000414 const char *ExtraCode) {
415 // Does this asm operand have a single letter operand modifier?
416 if (ExtraCode && ExtraCode[0]) {
417 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000418
Evan Cheng3d48a902006-04-28 21:19:05 +0000419 switch (ExtraCode[0]) {
420 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000421 case 'b': // Print QImode register
422 case 'h': // Print QImode high register
423 case 'w': // Print HImode register
424 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000425 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000426 }
427 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000428
Evan Cheng3d48a902006-04-28 21:19:05 +0000429 printOperand(MI, OpNo);
430 return false;
431}
432
433bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
434 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000435 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000436 const char *ExtraCode) {
437 if (ExtraCode && ExtraCode[0])
438 return true; // Unknown modifier.
439 printMemReference(MI, OpNo);
440 return false;
441}
442
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000443/// printMachineInstruction -- Print out a single X86 LLVM instruction
444/// MI in Intel syntax to the current output stream.
445///
446void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
447 ++EmittedInsts;
448
Chris Lattner634cca32009-09-09 20:34:59 +0000449 processDebugLoc(MI->getDebugLoc());
450
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000451 // Call the autogenerated instruction printer routines.
452 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000453
454 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
455 EmitComments(*MI);
456 O << '\n';
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000457}
458
459bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000460 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000461
Eli Friedmanaace4b12009-06-19 04:48:38 +0000462 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000463
464 // Emit declarations for external functions.
465 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000466 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000467 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000468 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000469
Eli Friedmanaace4b12009-06-19 04:48:38 +0000470 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000471 if (I->hasDLLImportLinkage()) {
472 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000473 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000474 O << Name << ":near\n";
475 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000476
Jeff Cohend43b18d2006-05-06 21:27:14 +0000477 // Emit declarations for external globals. Note that VC++ always declares
478 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000479 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
480 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000481 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000482 std::string Name = Mang->getMangledName(I);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000483
Eli Friedmanaace4b12009-06-19 04:48:38 +0000484 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000485 if (I->hasDLLImportLinkage()) {
486 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000487 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000488 O << Name << ":byte\n";
489 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000490 }
491
Dan Gohmanb8275a32007-07-25 19:33:14 +0000492 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000493}
494
Chris Lattner40bbebd2009-07-21 18:38:57 +0000495void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
496 // Check to see if this is a special global used by LLVM, if so, emit it.
497 if (GV->isDeclaration() ||
498 EmitSpecialLLVMGlobal(GV))
499 return;
500
Jeff Cohend43b18d2006-05-06 21:27:14 +0000501 const TargetData *TD = TM.getTargetData();
502
Chris Lattner40bbebd2009-07-21 18:38:57 +0000503 std::string name = Mang->getMangledName(GV);
504 Constant *C = GV->getInitializer();
505 unsigned Align = TD->getPreferredAlignmentLog(GV);
506 bool bCustomSegment = false;
507
508 switch (GV->getLinkage()) {
509 case GlobalValue::CommonLinkage:
510 case GlobalValue::LinkOnceAnyLinkage:
511 case GlobalValue::LinkOnceODRLinkage:
512 case GlobalValue::WeakAnyLinkage:
513 case GlobalValue::WeakODRLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000514 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000515 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
516 bCustomSegment = true;
517 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
518 // are also available.
519 break;
520 case GlobalValue::AppendingLinkage:
Chris Lattner090d73c2009-08-18 06:03:07 +0000521 // FIXME: make a MCSection.
Chris Lattner40bbebd2009-07-21 18:38:57 +0000522 O << name << "?\tSEGMENT PARA public 'DATA'\n";
523 bCustomSegment = true;
524 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
525 // are also available.
526 break;
527 case GlobalValue::DLLExportLinkage:
528 DLLExportedGVs.insert(name);
529 // FALL THROUGH
530 case GlobalValue::ExternalLinkage:
531 O << "\tpublic " << name << "\n";
532 // FALL THROUGH
533 case GlobalValue::InternalLinkage:
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000534 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattner40bbebd2009-07-21 18:38:57 +0000535 break;
536 default:
537 llvm_unreachable("Unknown linkage type!");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000538 }
Chris Lattner40bbebd2009-07-21 18:38:57 +0000539
540 if (!bCustomSegment)
541 EmitAlignment(Align, GV);
542
543 O << name << ":";
544 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000545 O.PadToColumn(MAI->getCommentColumn());
546 O << MAI->getCommentString()
Chris Lattner40bbebd2009-07-21 18:38:57 +0000547 << " " << GV->getName();
548 O << '\n';
549
550 EmitGlobalConstant(C);
551
552 if (bCustomSegment)
553 O << name << "?\tends\n";
554}
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000555
Chris Lattner40bbebd2009-07-21 18:38:57 +0000556bool X86IntelAsmPrinter::doFinalization(Module &M) {
557 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000558 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Evan Cheng03339202008-09-20 00:13:08 +0000559 O << "; WARNING: The following code is valid only with MASM v8.x"
560 << "and (possible) higher\n"
561 << "; This version of MASM is usually shipped with Microsoft "
562 << "Visual Studio 2005\n"
563 << "; or (possible) further versions. Unfortunately, there is no "
564 << "way to support\n"
565 << "; dllexported symbols in the earlier versions of MASM in fully "
566 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000567 O << "_drectve\t segment info alias('.drectve')\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000568
Chris Lattner17637662009-08-03 18:06:07 +0000569 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
570 e = DLLExportedGVs.end();
571 i != e; ++i)
572 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000573
Chris Lattner17637662009-08-03 18:06:07 +0000574 for (StringSet<>::iterator i = DLLExportedFns.begin(),
575 e = DLLExportedFns.end();
576 i != e; ++i)
577 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000578
Anton Korobeynikov85739862008-06-27 21:22:49 +0000579 O << "_drectve\t ends\n";
Chris Lattner17637662009-08-03 18:06:07 +0000580 }
Anton Korobeynikov85739862008-06-27 21:22:49 +0000581
Jeff Cohend43b18d2006-05-06 21:27:14 +0000582 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000583 bool Result = AsmPrinter::doFinalization(M);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000584 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000585 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000586}
587
Jeff Cohenc884db42006-05-02 01:16:28 +0000588void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
589 unsigned NumElts = CVA->getNumOperands();
Chris Lattner73d28f42009-09-13 19:10:08 +0000590 if (NumElts == 0) return;
591
592 // ML does not have escape sequences except '' for '. It also has a maximum
593 // string length of 255.
594 unsigned len = 0;
595 bool inString = false;
596 for (unsigned i = 0; i < NumElts; i++) {
597 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
598 if (len == 0)
599 O << "\tdb ";
Jeff Cohenc884db42006-05-02 01:16:28 +0000600
Chris Lattner73d28f42009-09-13 19:10:08 +0000601 if (n >= 32 && n <= 127) {
602 if (!inString) {
Jeff Cohenc884db42006-05-02 01:16:28 +0000603 if (len > 0) {
Chris Lattner73d28f42009-09-13 19:10:08 +0000604 O << ",'";
605 len += 2;
606 } else {
607 O << "'";
Jeff Cohenc884db42006-05-02 01:16:28 +0000608 len++;
609 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000610 inString = true;
Jeff Cohenc884db42006-05-02 01:16:28 +0000611 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000612 if (n == '\'') {
Jeff Cohenc884db42006-05-02 01:16:28 +0000613 O << "'";
Chris Lattner73d28f42009-09-13 19:10:08 +0000614 len++;
615 }
616 O << char(n);
617 } else {
618 if (inString) {
619 O << "'";
620 len++;
621 inString = false;
622 }
623 if (len > 0) {
624 O << ",";
625 len++;
626 }
627 O << n;
628 len += 1 + (n > 9) + (n > 99);
Jeff Cohenc884db42006-05-02 01:16:28 +0000629 }
Chris Lattner73d28f42009-09-13 19:10:08 +0000630
631 if (len > 60) {
632 if (inString) {
633 O << "'";
634 inString = false;
635 }
636 O << "\n";
637 len = 0;
638 }
639 }
640
641 if (len > 0) {
642 if (inString)
643 O << "'";
644 O << "\n";
Jeff Cohenc884db42006-05-02 01:16:28 +0000645 }
646}
647
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000648// Include the auto-generated portion of the assembly writer.
649#include "X86GenAsmWriter1.inc"