blob: 6b09b64bf3d97047f393e030f407d00d8f3f6179 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
16#define DEBUG_TYPE "asm-printer"
17#include "X86IntelAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86InstrInfo.h"
19#include "X86TargetAsmInfo.h"
20#include "X86.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CallingConv.h"
22#include "llvm/Constants.h"
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000023#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Module.h"
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Assembly/Writer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000028#include "llvm/CodeGen/DwarfWriter.h"
Edwin Török675d5622009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Mangler.h"
31#include "llvm/Target/TargetAsmInfo.h"
32#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000037static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
38 const TargetData *TD) {
39 X86MachineFunctionInfo Info;
40 uint64_t Size = 0;
41
42 switch (F->getCallingConv()) {
43 case CallingConv::X86_StdCall:
44 Info.setDecorationStyle(StdCall);
45 break;
46 case CallingConv::X86_FastCall:
47 Info.setDecorationStyle(FastCall);
48 break;
49 default:
50 return Info;
51 }
52
53 unsigned argNum = 1;
54 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
55 AI != AE; ++AI, ++argNum) {
56 const Type* Ty = AI->getType();
57
58 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000059 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000060 Ty = cast<PointerType>(Ty)->getElementType();
61
62 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000063 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000064 }
65
66 // We're not supporting tooooo huge arguments :)
67 Info.setBytesToPopOnReturn((unsigned int)Size);
68 return Info;
69}
70
71
72/// decorateName - Query FunctionInfoMap and use this information for various
73/// name decoration.
74void X86IntelAsmPrinter::decorateName(std::string &Name,
75 const GlobalValue *GV) {
76 const Function *F = dyn_cast<Function>(GV);
77 if (!F) return;
78
79 // We don't want to decorate non-stdcall or non-fastcall functions right now
80 unsigned CC = F->getCallingConv();
81 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
82 return;
83
84 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
85
86 const X86MachineFunctionInfo *Info;
87 if (info_item == FunctionInfoMap.end()) {
88 // Calculate apropriate function info and populate map
89 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
90 Info = &FunctionInfoMap[F];
91 } else {
92 Info = &info_item->second;
93 }
94
95 const FunctionType *FT = F->getFunctionType();
96 switch (Info->getDecorationStyle()) {
97 case None:
98 break;
99 case StdCall:
100 // "Pure" variadic functions do not receive @0 suffix.
101 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
102 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
103 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
104 break;
105 case FastCall:
106 // "Pure" variadic functions do not receive @0 suffix.
107 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
108 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
109 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
110
111 if (Name[0] == '_')
112 Name[0] = '@';
113 else
114 Name = '@' + Name;
115
116 break;
117 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000118 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000119 }
120}
121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122/// runOnMachineFunction - This uses the printMachineInstruction()
123/// method to print assembly for each instruction.
124///
125bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000126 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 SetupMachineFunction(MF);
128 O << "\n\n";
129
130 // Print out constants referenced by the function
131 EmitConstantPool(MF.getConstantPool());
132
133 // Print out labels for the function.
134 const Function *F = MF.getFunction();
135 unsigned CC = F->getCallingConv();
Bill Wendling25a8ae32009-06-30 22:38:32 +0000136 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 // Populate function information map. Actually, We don't want to populate
139 // non-stdcall or non-fastcall functions' information right now.
140 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
141 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
142
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000143 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144
Anton Korobeynikovcf87a3d2008-09-24 22:13:07 +0000145 SwitchToTextSection("_text", F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000147 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000148 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000149 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000151 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000152 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 case Function::DLLExportLinkage:
154 DLLExportedFns.insert(CurrentFnName);
155 //FALLS THROUGH
156 case Function::ExternalLinkage:
157 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000158 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000159 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 // Print out code for the function.
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
166 I != E; ++I) {
167 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000168 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000169 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 O << '\n';
171 }
172 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
173 II != E; ++II) {
174 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 printMachineInstruction(II);
176 }
177 }
178
179 // Print out jump tables referenced by the function.
180 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
181
182 O << CurrentFnName << "\tendp\n";
183
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000184 O.flush();
185
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 // We didn't modify anything.
187 return false;
188}
189
190void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000191 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 assert(value <= 7 && "Invalid ssecc argument!");
193 switch (value) {
194 case 0: O << "eq"; break;
195 case 1: O << "lt"; break;
196 case 2: O << "le"; break;
197 case 3: O << "unord"; break;
198 case 4: O << "neq"; break;
199 case 5: O << "nlt"; break;
200 case 6: O << "nle"; break;
201 case 7: O << "ord"; break;
202 }
203}
204
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000205void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000208 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000209 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 unsigned Reg = MO.getReg();
211 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000212 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
214 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
215 Reg = getX86SubSuperRegister(Reg, VT);
216 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000217 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 } else
219 O << "reg" << MO.getReg();
220 return;
221 }
222 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000223 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 case MachineOperand::MO_JumpTableIndex: {
226 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
227 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000228 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000229 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000231 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 case MachineOperand::MO_ConstantPoolIndex: {
233 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
234 if (!isMemOp) O << "OFFSET ";
235 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000236 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000237 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 O << "]";
239 return;
240 }
241 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000243 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000244 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000245 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
Chris Lattner357a0ca2009-06-20 19:34:09 +0000247 if (!isMemOp) O << "OFFSET ";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000248
249 // Handle dllimport linkage.
250 // FIXME: This should be fixed with full support of stdcall & fastcall
251 // CC's
252 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000253 O << "__imp_";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 O << Name;
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000256 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 return;
258 }
259 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 O << TAI->getGlobalPrefix() << MO.getSymbolName();
261 return;
262 }
263 default:
264 O << "<unknown operand type>"; return;
265 }
266}
267
Chris Lattner357a0ca2009-06-20 19:34:09 +0000268void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
269 const MachineOperand &MO = MI->getOperand(OpNo);
270 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000271 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000272 case MachineOperand::MO_Immediate:
273 O << MO.getImm();
274 return;
275 case MachineOperand::MO_MachineBasicBlock:
276 printBasicBlockLabel(MO.getMBB());
277 return;
278
279 case MachineOperand::MO_GlobalAddress: {
280 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000281 std::string Name = Mang->getMangledName(GV);
Chris Lattner357a0ca2009-06-20 19:34:09 +0000282 decorateName(Name, GV);
283
Chris Lattner9ab4e662009-07-09 00:58:53 +0000284 // Handle dllimport linkage.
285 // FIXME: This should be fixed with full support of stdcall & fastcall
286 // CC's
287 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner357a0ca2009-06-20 19:34:09 +0000288 O << "__imp_";
Chris Lattner357a0ca2009-06-20 19:34:09 +0000289 O << Name;
290 printOffset(MO.getOffset());
291 return;
292 }
293
294 case MachineOperand::MO_ExternalSymbol:
295 O << TAI->getGlobalPrefix() << MO.getSymbolName();
296 return;
297 }
298}
299
300
Rafael Espindolabca99f72009-04-08 21:14:34 +0000301void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
302 unsigned Op,
303 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000305 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 const MachineOperand &IndexReg = MI->getOperand(Op+2);
307 const MachineOperand &DispSpec = MI->getOperand(Op+3);
308
309 O << "[";
310 bool NeedPlus = false;
311 if (BaseReg.getReg()) {
312 printOp(BaseReg, Modifier);
313 NeedPlus = true;
314 }
315
316 if (IndexReg.getReg()) {
317 if (NeedPlus) O << " + ";
318 if (ScaleVal != 1)
319 O << ScaleVal << "*";
320 printOp(IndexReg, Modifier);
321 NeedPlus = true;
322 }
323
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000324 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
325 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 if (NeedPlus)
327 O << " + ";
328 printOp(DispSpec, "mem");
329 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000330 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000332 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333 if (DispVal > 0)
334 O << " + ";
335 else {
336 O << " - ";
337 DispVal = -DispVal;
338 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000339 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 O << DispVal;
341 }
342 }
343 O << "]";
344}
345
Rafael Espindolabca99f72009-04-08 21:14:34 +0000346void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
347 const char *Modifier) {
348 assert(isMem(MI, Op) && "Invalid memory reference!");
349 MachineOperand Segment = MI->getOperand(Op+4);
350 if (Segment.getReg()) {
351 printOperand(MI, Op+4, Modifier);
352 O << ':';
353 }
354 printLeaMemReference(MI, Op, Modifier);
355}
356
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000357void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000358 const MachineBasicBlock *MBB) const {
359 if (!TAI->getSetDirective())
360 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000361
Evan Cheng6fb06762007-11-09 01:32:10 +0000362 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
363 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000364 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000365 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
366}
367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedman378ea832009-06-19 04:48:38 +0000369 O << "L" << getFunctionNumber() << "$pb\n";
370 O << "L" << getFunctionNumber() << "$pb:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371}
372
373bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
374 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 unsigned Reg = MO.getReg();
376 switch (Mode) {
377 default: return true; // Unknown mode.
378 case 'b': // Print QImode register
379 Reg = getX86SubSuperRegister(Reg, MVT::i8);
380 break;
381 case 'h': // Print QImode high register
382 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
383 break;
384 case 'w': // Print HImode register
385 Reg = getX86SubSuperRegister(Reg, MVT::i16);
386 break;
387 case 'k': // Print SImode register
388 Reg = getX86SubSuperRegister(Reg, MVT::i32);
389 break;
390 }
391
Eli Friedman378ea832009-06-19 04:48:38 +0000392 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393 return false;
394}
395
396/// PrintAsmOperand - Print out an operand for an inline asm expression.
397///
398bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000399 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000400 const char *ExtraCode) {
401 // Does this asm operand have a single letter operand modifier?
402 if (ExtraCode && ExtraCode[0]) {
403 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 switch (ExtraCode[0]) {
406 default: return true; // Unknown modifier.
407 case 'b': // Print QImode register
408 case 'h': // Print QImode high register
409 case 'w': // Print HImode register
410 case 'k': // Print SImode register
411 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
412 }
413 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000414
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 printOperand(MI, OpNo);
416 return false;
417}
418
419bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
420 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000421 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 const char *ExtraCode) {
423 if (ExtraCode && ExtraCode[0])
424 return true; // Unknown modifier.
425 printMemReference(MI, OpNo);
426 return false;
427}
428
429/// printMachineInstruction -- Print out a single X86 LLVM instruction
430/// MI in Intel syntax to the current output stream.
431///
432void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
433 ++EmittedInsts;
434
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000435 // Call the autogenerated instruction printer routines.
436 printInstruction(MI);
437}
438
439bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000440 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000441
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 Mang->markCharUnacceptable('.');
443
Eli Friedman378ea832009-06-19 04:48:38 +0000444 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445
446 // Emit declarations for external functions.
447 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
448 if (I->isDeclaration()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000449 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000450 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451
Eli Friedman378ea832009-06-19 04:48:38 +0000452 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 if (I->hasDLLImportLinkage()) {
454 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 O << Name << ":near\n";
457 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 // Emit declarations for external globals. Note that VC++ always declares
460 // external globals to have type byte, and if that's good enough for VC++...
461 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
462 I != E; ++I) {
463 if (I->isDeclaration()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000464 std::string Name = Mang->getMangledName(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465
Eli Friedman378ea832009-06-19 04:48:38 +0000466 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 if (I->hasDLLImportLinkage()) {
468 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000469 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 O << Name << ":byte\n";
471 }
472 }
473
Dan Gohman4a558a32007-07-25 19:33:14 +0000474 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475}
476
Chris Lattnerae982212009-07-21 18:38:57 +0000477void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
478 // Check to see if this is a special global used by LLVM, if so, emit it.
479 if (GV->isDeclaration() ||
480 EmitSpecialLLVMGlobal(GV))
481 return;
482
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000483 const TargetData *TD = TM.getTargetData();
484
Chris Lattnerae982212009-07-21 18:38:57 +0000485 std::string name = Mang->getMangledName(GV);
486 Constant *C = GV->getInitializer();
487 unsigned Align = TD->getPreferredAlignmentLog(GV);
488 bool bCustomSegment = false;
489
490 switch (GV->getLinkage()) {
491 case GlobalValue::CommonLinkage:
492 case GlobalValue::LinkOnceAnyLinkage:
493 case GlobalValue::LinkOnceODRLinkage:
494 case GlobalValue::WeakAnyLinkage:
495 case GlobalValue::WeakODRLinkage:
496 SwitchToDataSection("");
497 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
498 bCustomSegment = true;
499 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
500 // are also available.
501 break;
502 case GlobalValue::AppendingLinkage:
503 SwitchToDataSection("");
504 O << name << "?\tSEGMENT PARA public 'DATA'\n";
505 bCustomSegment = true;
506 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
507 // are also available.
508 break;
509 case GlobalValue::DLLExportLinkage:
510 DLLExportedGVs.insert(name);
511 // FALL THROUGH
512 case GlobalValue::ExternalLinkage:
513 O << "\tpublic " << name << "\n";
514 // FALL THROUGH
515 case GlobalValue::InternalLinkage:
516 SwitchToSection(TAI->getDataSection());
517 break;
518 default:
519 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
Chris Lattnerae982212009-07-21 18:38:57 +0000521
522 if (!bCustomSegment)
523 EmitAlignment(Align, GV);
524
525 O << name << ":";
526 if (VerboseAsm)
527 O << "\t\t\t\t" << TAI->getCommentString()
528 << " " << GV->getName();
529 O << '\n';
530
531 EmitGlobalConstant(C);
532
533 if (bCustomSegment)
534 O << name << "?\tends\n";
535}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536
Chris Lattnerae982212009-07-21 18:38:57 +0000537bool X86IntelAsmPrinter::doFinalization(Module &M) {
538 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000539 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000541 O << "; WARNING: The following code is valid only with MASM v8.x"
542 << "and (possible) higher\n"
543 << "; This version of MASM is usually shipped with Microsoft "
544 << "Visual Studio 2005\n"
545 << "; or (possible) further versions. Unfortunately, there is no "
546 << "way to support\n"
547 << "; dllexported symbols in the earlier versions of MASM in fully "
548 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 O << "_drectve\t segment info alias('.drectve')\n";
550 }
551
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000552 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553 e = DLLExportedGVs.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000554 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000555 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000557 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000559 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000560 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000562 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000563 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000564
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000566 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 SwitchToDataSection("");
568 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000569 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570}
571
572void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
573 unsigned NumElts = CVA->getNumOperands();
574 if (NumElts) {
575 // ML does not have escape sequences except '' for '. It also has a maximum
576 // string length of 255.
577 unsigned len = 0;
578 bool inString = false;
579 for (unsigned i = 0; i < NumElts; i++) {
580 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
581 if (len == 0)
582 O << "\tdb ";
583
584 if (n >= 32 && n <= 127) {
585 if (!inString) {
586 if (len > 0) {
587 O << ",'";
588 len += 2;
589 } else {
590 O << "'";
591 len++;
592 }
593 inString = true;
594 }
595 if (n == '\'') {
596 O << "'";
597 len++;
598 }
599 O << char(n);
600 } else {
601 if (inString) {
602 O << "'";
603 len++;
604 inString = false;
605 }
606 if (len > 0) {
607 O << ",";
608 len++;
609 }
610 O << n;
611 len += 1 + (n > 9) + (n > 99);
612 }
613
614 if (len > 60) {
615 if (inString) {
616 O << "'";
617 inString = false;
618 }
619 O << "\n";
620 len = 0;
621 }
622 }
623
624 if (len > 0) {
625 if (inString)
626 O << "'";
627 O << "\n";
628 }
629 }
630}
631
632// Include the auto-generated portion of the assembly writer.
633#include "X86GenAsmWriter1.inc"