blob: 9890fddbecf1e6c7746e395246a7932477ec40ac [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:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000150 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000151 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 case Function::DLLExportLinkage:
153 DLLExportedFns.insert(CurrentFnName);
154 //FALLS THROUGH
155 case Function::ExternalLinkage:
156 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000157 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000158 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000160
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 // Print out code for the function.
164 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
165 I != E; ++I) {
166 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000167 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000168 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 O << '\n';
170 }
171 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
172 II != E; ++II) {
173 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 printMachineInstruction(II);
175 }
176 }
177
178 // Print out jump tables referenced by the function.
179 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
180
181 O << CurrentFnName << "\tendp\n";
182
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000183 O.flush();
184
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 // We didn't modify anything.
186 return false;
187}
188
189void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000190 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 assert(value <= 7 && "Invalid ssecc argument!");
192 switch (value) {
193 case 0: O << "eq"; break;
194 case 1: O << "lt"; break;
195 case 2: O << "le"; break;
196 case 3: O << "unord"; break;
197 case 4: O << "neq"; break;
198 case 5: O << "nlt"; break;
199 case 6: O << "nle"; break;
200 case 7: O << "ord"; break;
201 }
202}
203
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000204void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000207 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000208 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 unsigned Reg = MO.getReg();
210 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000211 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
213 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
214 Reg = getX86SubSuperRegister(Reg, VT);
215 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000216 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 } else
218 O << "reg" << MO.getReg();
219 return;
220 }
221 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000222 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 case MachineOperand::MO_JumpTableIndex: {
225 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
226 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000227 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000228 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000230 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 case MachineOperand::MO_ConstantPoolIndex: {
232 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
233 if (!isMemOp) O << "OFFSET ";
234 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000235 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000236 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 O << "]";
238 return;
239 }
240 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000242 GlobalValue *GV = MO.getGlobal();
Daniel Dunbarcc067252009-07-14 16:12:13 +0000243 std::string Name = Mang->getValueName(GV);
244
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();
Daniel Dunbarcc067252009-07-14 16:12:13 +0000281 std::string Name = Mang->getValueName(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()) {
Daniel Dunbarcc067252009-07-14 16:12:13 +0000449 std::string Name = Mang->getValueName(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()) {
Daniel Dunbarcc067252009-07-14 16:12:13 +0000464 std::string Name = Mang->getValueName(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
477bool X86IntelAsmPrinter::doFinalization(Module &M) {
478 const TargetData *TD = TM.getTargetData();
479
480 // Print out module-level global variables here.
481 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
482 I != E; ++I) {
483 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000484
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 // Check to see if this is a special global used by LLVM, if so, emit it.
486 if (EmitSpecialLLVMGlobal(I))
487 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000488
Daniel Dunbarcc067252009-07-14 16:12:13 +0000489 std::string name = Mang->getValueName(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 Constant *C = I->getInitializer();
491 unsigned Align = TD->getPreferredAlignmentLog(I);
492 bool bCustomSegment = false;
493
494 switch (I->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000495 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000496 case GlobalValue::LinkOnceAnyLinkage:
497 case GlobalValue::LinkOnceODRLinkage:
498 case GlobalValue::WeakAnyLinkage:
499 case GlobalValue::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000500 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000501 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 bCustomSegment = true;
503 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
504 // are also available.
505 break;
506 case GlobalValue::AppendingLinkage:
507 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000508 O << name << "?\tSEGMENT PARA public 'DATA'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 bCustomSegment = true;
510 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
511 // are also available.
512 break;
513 case GlobalValue::DLLExportLinkage:
514 DLLExportedGVs.insert(name);
515 // FALL THROUGH
516 case GlobalValue::ExternalLinkage:
517 O << "\tpublic " << name << "\n";
518 // FALL THROUGH
519 case GlobalValue::InternalLinkage:
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000520 SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 break;
522 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000523 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 }
525
526 if (!bCustomSegment)
527 EmitAlignment(Align, I);
528
Evan Cheng11db8142009-03-24 00:17:40 +0000529 O << name << ":";
530 if (VerboseAsm)
Evan Cheng4c7969e2009-03-25 01:08:42 +0000531 O << "\t\t\t\t" << TAI->getCommentString()
Evan Cheng11db8142009-03-24 00:17:40 +0000532 << " " << I->getName();
533 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534
535 EmitGlobalConstant(C);
536
537 if (bCustomSegment)
538 O << name << "?\tends\n";
539 }
540
541 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000542 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000544 O << "; WARNING: The following code is valid only with MASM v8.x"
545 << "and (possible) higher\n"
546 << "; This version of MASM is usually shipped with Microsoft "
547 << "Visual Studio 2005\n"
548 << "; or (possible) further versions. Unfortunately, there is no "
549 << "way to support\n"
550 << "; dllexported symbols in the earlier versions of MASM in fully "
551 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 O << "_drectve\t segment info alias('.drectve')\n";
553 }
554
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000555 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 e = DLLExportedGVs.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000557 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000558 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000560 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000562 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000563 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000565 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000566 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000569 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 SwitchToDataSection("");
571 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000572 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573}
574
575void 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++) {
583 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
584 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
635// Include the auto-generated portion of the assembly writer.
636#include "X86GenAsmWriter1.inc"