blob: ad8d6adde7ea87ba5cebf19e0b5e8b48389aee64 [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029#include "llvm/Support/Mangler.h"
30#include "llvm/Target/TargetAsmInfo.h"
31#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032using namespace llvm;
33
34STATISTIC(EmittedInsts, "Number of machine instrs printed");
35
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000036static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
37 const TargetData *TD) {
38 X86MachineFunctionInfo Info;
39 uint64_t Size = 0;
40
41 switch (F->getCallingConv()) {
42 case CallingConv::X86_StdCall:
43 Info.setDecorationStyle(StdCall);
44 break;
45 case CallingConv::X86_FastCall:
46 Info.setDecorationStyle(FastCall);
47 break;
48 default:
49 return Info;
50 }
51
52 unsigned argNum = 1;
53 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
54 AI != AE; ++AI, ++argNum) {
55 const Type* Ty = AI->getType();
56
57 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000058 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000059 Ty = cast<PointerType>(Ty)->getElementType();
60
61 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000062 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000063 }
64
65 // We're not supporting tooooo huge arguments :)
66 Info.setBytesToPopOnReturn((unsigned int)Size);
67 return Info;
68}
69
70
71/// decorateName - Query FunctionInfoMap and use this information for various
72/// name decoration.
73void X86IntelAsmPrinter::decorateName(std::string &Name,
74 const GlobalValue *GV) {
75 const Function *F = dyn_cast<Function>(GV);
76 if (!F) return;
77
78 // We don't want to decorate non-stdcall or non-fastcall functions right now
79 unsigned CC = F->getCallingConv();
80 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
81 return;
82
83 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
84
85 const X86MachineFunctionInfo *Info;
86 if (info_item == FunctionInfoMap.end()) {
87 // Calculate apropriate function info and populate map
88 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
89 Info = &FunctionInfoMap[F];
90 } else {
91 Info = &info_item->second;
92 }
93
94 const FunctionType *FT = F->getFunctionType();
95 switch (Info->getDecorationStyle()) {
96 case None:
97 break;
98 case StdCall:
99 // "Pure" variadic functions do not receive @0 suffix.
100 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
101 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
102 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
103 break;
104 case FastCall:
105 // "Pure" variadic functions do not receive @0 suffix.
106 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
107 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
108 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
109
110 if (Name[0] == '_')
111 Name[0] = '@';
112 else
113 Name = '@' + Name;
114
115 break;
116 default:
117 assert(0 && "Unsupported DecorationStyle");
118 }
119}
120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121/// runOnMachineFunction - This uses the printMachineInstruction()
122/// method to print assembly for each instruction.
123///
124bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000125 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 SetupMachineFunction(MF);
127 O << "\n\n";
128
129 // Print out constants referenced by the function
130 EmitConstantPool(MF.getConstantPool());
131
132 // Print out labels for the function.
133 const Function *F = MF.getFunction();
134 unsigned CC = F->getCallingConv();
Bill Wendling25a8ae32009-06-30 22:38:32 +0000135 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136
137 // Populate function information map. Actually, We don't want to populate
138 // non-stdcall or non-fastcall functions' information right now.
139 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
140 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
141
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000142 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
Anton Korobeynikovcf87a3d2008-09-24 22:13:07 +0000144 SwitchToTextSection("_text", F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 switch (F->getLinkage()) {
146 default: assert(0 && "Unsupported linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000147 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000149 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000150 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 case Function::DLLExportLinkage:
152 DLLExportedFns.insert(CurrentFnName);
153 //FALLS THROUGH
154 case Function::ExternalLinkage:
155 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000156 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000157 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 // Print out code for the function.
163 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
164 I != E; ++I) {
165 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000166 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000167 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 O << '\n';
169 }
170 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
171 II != E; ++II) {
172 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 printMachineInstruction(II);
174 }
175 }
176
177 // Print out jump tables referenced by the function.
178 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
179
180 O << CurrentFnName << "\tendp\n";
181
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000182 O.flush();
183
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 // We didn't modify anything.
185 return false;
186}
187
188void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000189 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 assert(value <= 7 && "Invalid ssecc argument!");
191 switch (value) {
192 case 0: O << "eq"; break;
193 case 1: O << "lt"; break;
194 case 2: O << "le"; break;
195 case 3: O << "unord"; break;
196 case 4: O << "neq"; break;
197 case 5: O << "nlt"; break;
198 case 6: O << "nle"; break;
199 case 7: O << "ord"; break;
200 }
201}
202
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000203void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000206 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000207 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 unsigned Reg = MO.getReg();
209 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000210 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
212 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
213 Reg = getX86SubSuperRegister(Reg, VT);
214 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000215 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 } else
217 O << "reg" << MO.getReg();
218 return;
219 }
220 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000221 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 case MachineOperand::MO_JumpTableIndex: {
224 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
225 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000226 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000227 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000229 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 case MachineOperand::MO_ConstantPoolIndex: {
231 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
232 if (!isMemOp) O << "OFFSET ";
233 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000234 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000235 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 O << "]";
237 return;
238 }
239 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000241 GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 std::string Name = Mang->getValueName(GV);
243
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000244 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245
Chris Lattner357a0ca2009-06-20 19:34:09 +0000246 if (!isMemOp) O << "OFFSET ";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000247
248 // Handle dllimport linkage.
249 // FIXME: This should be fixed with full support of stdcall & fastcall
250 // CC's
251 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000252 O << "__imp_";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 O << Name;
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000255 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 return;
257 }
258 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 O << TAI->getGlobalPrefix() << MO.getSymbolName();
260 return;
261 }
262 default:
263 O << "<unknown operand type>"; return;
264 }
265}
266
Chris Lattner357a0ca2009-06-20 19:34:09 +0000267void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
268 const MachineOperand &MO = MI->getOperand(OpNo);
269 switch (MO.getType()) {
270 default: assert(0 && "Unknown pcrel immediate operand");
271 case MachineOperand::MO_Immediate:
272 O << MO.getImm();
273 return;
274 case MachineOperand::MO_MachineBasicBlock:
275 printBasicBlockLabel(MO.getMBB());
276 return;
277
278 case MachineOperand::MO_GlobalAddress: {
279 GlobalValue *GV = MO.getGlobal();
280 std::string Name = Mang->getValueName(GV);
281 decorateName(Name, GV);
282
Chris Lattner9ab4e662009-07-09 00:58:53 +0000283 // Handle dllimport linkage.
284 // FIXME: This should be fixed with full support of stdcall & fastcall
285 // CC's
286 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner357a0ca2009-06-20 19:34:09 +0000287 O << "__imp_";
Chris Lattner357a0ca2009-06-20 19:34:09 +0000288 O << Name;
289 printOffset(MO.getOffset());
290 return;
291 }
292
293 case MachineOperand::MO_ExternalSymbol:
294 O << TAI->getGlobalPrefix() << MO.getSymbolName();
295 return;
296 }
297}
298
299
Rafael Espindolabca99f72009-04-08 21:14:34 +0000300void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
301 unsigned Op,
302 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000304 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 const MachineOperand &IndexReg = MI->getOperand(Op+2);
306 const MachineOperand &DispSpec = MI->getOperand(Op+3);
307
308 O << "[";
309 bool NeedPlus = false;
310 if (BaseReg.getReg()) {
311 printOp(BaseReg, Modifier);
312 NeedPlus = true;
313 }
314
315 if (IndexReg.getReg()) {
316 if (NeedPlus) O << " + ";
317 if (ScaleVal != 1)
318 O << ScaleVal << "*";
319 printOp(IndexReg, Modifier);
320 NeedPlus = true;
321 }
322
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000323 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
324 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 if (NeedPlus)
326 O << " + ";
327 printOp(DispSpec, "mem");
328 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000329 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000331 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 if (DispVal > 0)
333 O << " + ";
334 else {
335 O << " - ";
336 DispVal = -DispVal;
337 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000338 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 O << DispVal;
340 }
341 }
342 O << "]";
343}
344
Rafael Espindolabca99f72009-04-08 21:14:34 +0000345void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
346 const char *Modifier) {
347 assert(isMem(MI, Op) && "Invalid memory reference!");
348 MachineOperand Segment = MI->getOperand(Op+4);
349 if (Segment.getReg()) {
350 printOperand(MI, Op+4, Modifier);
351 O << ':';
352 }
353 printLeaMemReference(MI, Op, Modifier);
354}
355
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000356void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000357 const MachineBasicBlock *MBB) const {
358 if (!TAI->getSetDirective())
359 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000360
Evan Cheng6fb06762007-11-09 01:32:10 +0000361 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
362 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000363 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000364 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
365}
366
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedman378ea832009-06-19 04:48:38 +0000368 O << "L" << getFunctionNumber() << "$pb\n";
369 O << "L" << getFunctionNumber() << "$pb:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370}
371
372bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
373 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 unsigned Reg = MO.getReg();
375 switch (Mode) {
376 default: return true; // Unknown mode.
377 case 'b': // Print QImode register
378 Reg = getX86SubSuperRegister(Reg, MVT::i8);
379 break;
380 case 'h': // Print QImode high register
381 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
382 break;
383 case 'w': // Print HImode register
384 Reg = getX86SubSuperRegister(Reg, MVT::i16);
385 break;
386 case 'k': // Print SImode register
387 Reg = getX86SubSuperRegister(Reg, MVT::i32);
388 break;
389 }
390
Eli Friedman378ea832009-06-19 04:48:38 +0000391 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 return false;
393}
394
395/// PrintAsmOperand - Print out an operand for an inline asm expression.
396///
397bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000398 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 const char *ExtraCode) {
400 // Does this asm operand have a single letter operand modifier?
401 if (ExtraCode && ExtraCode[0]) {
402 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000403
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 switch (ExtraCode[0]) {
405 default: return true; // Unknown modifier.
406 case 'b': // Print QImode register
407 case 'h': // Print QImode high register
408 case 'w': // Print HImode register
409 case 'k': // Print SImode register
410 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
411 }
412 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000413
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 printOperand(MI, OpNo);
415 return false;
416}
417
418bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
419 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000420 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421 const char *ExtraCode) {
422 if (ExtraCode && ExtraCode[0])
423 return true; // Unknown modifier.
424 printMemReference(MI, OpNo);
425 return false;
426}
427
428/// printMachineInstruction -- Print out a single X86 LLVM instruction
429/// MI in Intel syntax to the current output stream.
430///
431void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
432 ++EmittedInsts;
433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 // Call the autogenerated instruction printer routines.
435 printInstruction(MI);
436}
437
438bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000439 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441 Mang->markCharUnacceptable('.');
442
Eli Friedman378ea832009-06-19 04:48:38 +0000443 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444
445 // Emit declarations for external functions.
446 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
447 if (I->isDeclaration()) {
448 std::string Name = Mang->getValueName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000449 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450
Eli Friedman378ea832009-06-19 04:48:38 +0000451 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452 if (I->hasDLLImportLinkage()) {
453 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 O << Name << ":near\n";
456 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000457
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 // Emit declarations for external globals. Note that VC++ always declares
459 // external globals to have type byte, and if that's good enough for VC++...
460 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
461 I != E; ++I) {
462 if (I->isDeclaration()) {
463 std::string Name = Mang->getValueName(I);
464
Eli Friedman378ea832009-06-19 04:48:38 +0000465 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 if (I->hasDLLImportLinkage()) {
467 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000468 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000469 O << Name << ":byte\n";
470 }
471 }
472
Dan Gohman4a558a32007-07-25 19:33:14 +0000473 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474}
475
476bool X86IntelAsmPrinter::doFinalization(Module &M) {
477 const TargetData *TD = TM.getTargetData();
478
479 // Print out module-level global variables here.
480 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
481 I != E; ++I) {
482 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000483
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 // Check to see if this is a special global used by LLVM, if so, emit it.
485 if (EmitSpecialLLVMGlobal(I))
486 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000487
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000488 std::string name = Mang->getValueName(I);
489 Constant *C = I->getInitializer();
490 unsigned Align = TD->getPreferredAlignmentLog(I);
491 bool bCustomSegment = false;
492
493 switch (I->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000494 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000495 case GlobalValue::LinkOnceAnyLinkage:
496 case GlobalValue::LinkOnceODRLinkage:
497 case GlobalValue::WeakAnyLinkage:
498 case GlobalValue::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000500 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 bCustomSegment = true;
502 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
503 // are also available.
504 break;
505 case GlobalValue::AppendingLinkage:
506 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000507 O << name << "?\tSEGMENT PARA public 'DATA'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 bCustomSegment = true;
509 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
510 // are also available.
511 break;
512 case GlobalValue::DLLExportLinkage:
513 DLLExportedGVs.insert(name);
514 // FALL THROUGH
515 case GlobalValue::ExternalLinkage:
516 O << "\tpublic " << name << "\n";
517 // FALL THROUGH
518 case GlobalValue::InternalLinkage:
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000519 SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 break;
521 default:
522 assert(0 && "Unknown linkage type!");
523 }
524
525 if (!bCustomSegment)
526 EmitAlignment(Align, I);
527
Evan Cheng11db8142009-03-24 00:17:40 +0000528 O << name << ":";
529 if (VerboseAsm)
Evan Cheng4c7969e2009-03-25 01:08:42 +0000530 O << "\t\t\t\t" << TAI->getCommentString()
Evan Cheng11db8142009-03-24 00:17:40 +0000531 << " " << I->getName();
532 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533
534 EmitGlobalConstant(C);
535
536 if (bCustomSegment)
537 O << name << "?\tends\n";
538 }
539
540 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000541 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000543 O << "; WARNING: The following code is valid only with MASM v8.x"
544 << "and (possible) higher\n"
545 << "; This version of MASM is usually shipped with Microsoft "
546 << "Visual Studio 2005\n"
547 << "; or (possible) further versions. Unfortunately, there is no "
548 << "way to support\n"
549 << "; dllexported symbols in the earlier versions of MASM in fully "
550 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 O << "_drectve\t segment info alias('.drectve')\n";
552 }
553
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000554 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000555 e = DLLExportedGVs.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000556 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000557 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000559 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000561 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000562 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000564 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000565 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000566
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000568 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569 SwitchToDataSection("");
570 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000571 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572}
573
574void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
575 unsigned NumElts = CVA->getNumOperands();
576 if (NumElts) {
577 // ML does not have escape sequences except '' for '. It also has a maximum
578 // string length of 255.
579 unsigned len = 0;
580 bool inString = false;
581 for (unsigned i = 0; i < NumElts; i++) {
582 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
583 if (len == 0)
584 O << "\tdb ";
585
586 if (n >= 32 && n <= 127) {
587 if (!inString) {
588 if (len > 0) {
589 O << ",'";
590 len += 2;
591 } else {
592 O << "'";
593 len++;
594 }
595 inString = true;
596 }
597 if (n == '\'') {
598 O << "'";
599 len++;
600 }
601 O << char(n);
602 } else {
603 if (inString) {
604 O << "'";
605 len++;
606 inString = false;
607 }
608 if (len > 0) {
609 O << ",";
610 len++;
611 }
612 O << n;
613 len += 1 + (n > 9) + (n > 99);
614 }
615
616 if (len > 60) {
617 if (inString) {
618 O << "'";
619 inString = false;
620 }
621 O << "\n";
622 len = 0;
623 }
624 }
625
626 if (len > 0) {
627 if (inString)
628 O << "'";
629 O << "\n";
630 }
631 }
632}
633
634// Include the auto-generated portion of the assembly writer.
635#include "X86GenAsmWriter1.inc"