blob: 73cafce7f880ff38d22aa1a6b739686fb4a4c66e [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();
135
136 // Populate function information map. Actually, We don't want to populate
137 // non-stdcall or non-fastcall functions' information right now.
138 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
139 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
140
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000141 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
Anton Korobeynikovcf87a3d2008-09-24 22:13:07 +0000143 SwitchToTextSection("_text", F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144
Devang Patel93698d92008-10-01 23:18:38 +0000145 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000146 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000147 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 switch (F->getLinkage()) {
149 default: assert(0 && "Unsupported linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000150 case Function::PrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000152 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000153 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 case Function::DLLExportLinkage:
155 DLLExportedFns.insert(CurrentFnName);
156 //FALLS THROUGH
157 case Function::ExternalLinkage:
158 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000159 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000160 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000162
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 // Print out code for the function.
166 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
167 I != E; ++I) {
168 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000169 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000170 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 O << '\n';
172 }
173 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
174 II != E; ++II) {
175 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 printMachineInstruction(II);
177 }
178 }
179
180 // Print out jump tables referenced by the function.
181 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
182
183 O << CurrentFnName << "\tendp\n";
184
Dan Gohmaneb94abd2008-11-07 19:49:17 +0000185 O.flush();
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 // We didn't modify anything.
188 return false;
189}
190
191void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000192 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 assert(value <= 7 && "Invalid ssecc argument!");
194 switch (value) {
195 case 0: O << "eq"; break;
196 case 1: O << "lt"; break;
197 case 2: O << "le"; break;
198 case 3: O << "unord"; break;
199 case 4: O << "neq"; break;
200 case 5: O << "nlt"; break;
201 case 6: O << "nle"; break;
202 case 7: O << "ord"; break;
203 }
204}
205
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000206void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000209 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000210 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 unsigned Reg = MO.getReg();
212 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000213 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
215 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
216 Reg = getX86SubSuperRegister(Reg, VT);
217 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000218 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 } else
220 O << "reg" << MO.getReg();
221 return;
222 }
223 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000224 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 return;
226 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner357a0ca2009-06-20 19:34:09 +0000227 // FIXME: REMOVE
228 assert(0 && "labels should only be used as pc-relative values");
Chris Lattner6017d482007-12-30 23:10:15 +0000229 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 return;
231 case MachineOperand::MO_JumpTableIndex: {
232 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
233 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000234 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000235 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000237 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 case MachineOperand::MO_ConstantPoolIndex: {
239 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
240 if (!isMemOp) O << "OFFSET ";
241 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000242 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000243 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 O << "]";
245 return;
246 }
247 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000249 GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 std::string Name = Mang->getValueName(GV);
251
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000252 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
Chris Lattner357a0ca2009-06-20 19:34:09 +0000254 if (!isMemOp) O << "OFFSET ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (GV->hasDLLImportLinkage()) {
256 // FIXME: This should be fixed with full support of stdcall & fastcall
257 // CC's
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000258 O << "__imp_";
259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 O << Name;
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000261 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 return;
263 }
264 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 O << TAI->getGlobalPrefix() << MO.getSymbolName();
266 return;
267 }
268 default:
269 O << "<unknown operand type>"; return;
270 }
271}
272
Chris Lattner357a0ca2009-06-20 19:34:09 +0000273void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
274 const MachineOperand &MO = MI->getOperand(OpNo);
275 switch (MO.getType()) {
276 default: assert(0 && "Unknown pcrel immediate operand");
277 case MachineOperand::MO_Immediate:
278 O << MO.getImm();
279 return;
280 case MachineOperand::MO_MachineBasicBlock:
281 printBasicBlockLabel(MO.getMBB());
282 return;
283
284 case MachineOperand::MO_GlobalAddress: {
285 GlobalValue *GV = MO.getGlobal();
286 std::string Name = Mang->getValueName(GV);
287 decorateName(Name, GV);
288
289 if (GV->hasDLLImportLinkage()) {
290 // FIXME: This should be fixed with full support of stdcall & fastcall
291 // CC's
292 O << "__imp_";
293 }
294 O << Name;
295 printOffset(MO.getOffset());
296 return;
297 }
298
299 case MachineOperand::MO_ExternalSymbol:
300 O << TAI->getGlobalPrefix() << MO.getSymbolName();
301 return;
302 }
303}
304
305
Rafael Espindolabca99f72009-04-08 21:14:34 +0000306void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
307 unsigned Op,
308 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000310 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 const MachineOperand &IndexReg = MI->getOperand(Op+2);
312 const MachineOperand &DispSpec = MI->getOperand(Op+3);
313
314 O << "[";
315 bool NeedPlus = false;
316 if (BaseReg.getReg()) {
317 printOp(BaseReg, Modifier);
318 NeedPlus = true;
319 }
320
321 if (IndexReg.getReg()) {
322 if (NeedPlus) O << " + ";
323 if (ScaleVal != 1)
324 O << ScaleVal << "*";
325 printOp(IndexReg, Modifier);
326 NeedPlus = true;
327 }
328
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000329 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
330 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 if (NeedPlus)
332 O << " + ";
333 printOp(DispSpec, "mem");
334 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000335 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000337 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338 if (DispVal > 0)
339 O << " + ";
340 else {
341 O << " - ";
342 DispVal = -DispVal;
343 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000344 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 O << DispVal;
346 }
347 }
348 O << "]";
349}
350
Rafael Espindolabca99f72009-04-08 21:14:34 +0000351void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
352 const char *Modifier) {
353 assert(isMem(MI, Op) && "Invalid memory reference!");
354 MachineOperand Segment = MI->getOperand(Op+4);
355 if (Segment.getReg()) {
356 printOperand(MI, Op+4, Modifier);
357 O << ':';
358 }
359 printLeaMemReference(MI, Op, Modifier);
360}
361
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000362void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000363 const MachineBasicBlock *MBB) const {
364 if (!TAI->getSetDirective())
365 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000366
Evan Cheng6fb06762007-11-09 01:32:10 +0000367 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
368 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000369 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000370 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
371}
372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedman378ea832009-06-19 04:48:38 +0000374 O << "L" << getFunctionNumber() << "$pb\n";
375 O << "L" << getFunctionNumber() << "$pb:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376}
377
378bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
379 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380 unsigned Reg = MO.getReg();
381 switch (Mode) {
382 default: return true; // Unknown mode.
383 case 'b': // Print QImode register
384 Reg = getX86SubSuperRegister(Reg, MVT::i8);
385 break;
386 case 'h': // Print QImode high register
387 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
388 break;
389 case 'w': // Print HImode register
390 Reg = getX86SubSuperRegister(Reg, MVT::i16);
391 break;
392 case 'k': // Print SImode register
393 Reg = getX86SubSuperRegister(Reg, MVT::i32);
394 break;
395 }
396
Eli Friedman378ea832009-06-19 04:48:38 +0000397 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398 return false;
399}
400
401/// PrintAsmOperand - Print out an operand for an inline asm expression.
402///
403bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000404 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 const char *ExtraCode) {
406 // Does this asm operand have a single letter operand modifier?
407 if (ExtraCode && ExtraCode[0]) {
408 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000409
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 switch (ExtraCode[0]) {
411 default: return true; // Unknown modifier.
412 case 'b': // Print QImode register
413 case 'h': // Print QImode high register
414 case 'w': // Print HImode register
415 case 'k': // Print SImode register
416 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
417 }
418 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000419
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 printOperand(MI, OpNo);
421 return false;
422}
423
424bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
425 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000426 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 const char *ExtraCode) {
428 if (ExtraCode && ExtraCode[0])
429 return true; // Unknown modifier.
430 printMemReference(MI, OpNo);
431 return false;
432}
433
434/// printMachineInstruction -- Print out a single X86 LLVM instruction
435/// MI in Intel syntax to the current output stream.
436///
437void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
438 ++EmittedInsts;
439
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 // Call the autogenerated instruction printer routines.
441 printInstruction(MI);
442}
443
444bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000445 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000446
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 Mang->markCharUnacceptable('.');
448
Eli Friedman378ea832009-06-19 04:48:38 +0000449 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450
451 // Emit declarations for external functions.
452 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
453 if (I->isDeclaration()) {
454 std::string Name = Mang->getValueName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000455 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456
Eli Friedman378ea832009-06-19 04:48:38 +0000457 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458 if (I->hasDLLImportLinkage()) {
459 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000460 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 O << Name << ":near\n";
462 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000463
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 // Emit declarations for external globals. Note that VC++ always declares
465 // external globals to have type byte, and if that's good enough for VC++...
466 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
467 I != E; ++I) {
468 if (I->isDeclaration()) {
469 std::string Name = Mang->getValueName(I);
470
Eli Friedman378ea832009-06-19 04:48:38 +0000471 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 if (I->hasDLLImportLinkage()) {
473 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000474 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000475 O << Name << ":byte\n";
476 }
477 }
478
Dan Gohman4a558a32007-07-25 19:33:14 +0000479 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480}
481
482bool X86IntelAsmPrinter::doFinalization(Module &M) {
483 const TargetData *TD = TM.getTargetData();
484
485 // Print out module-level global variables here.
486 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
487 I != E; ++I) {
488 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000489
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 // Check to see if this is a special global used by LLVM, if so, emit it.
491 if (EmitSpecialLLVMGlobal(I))
492 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000493
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000494 std::string name = Mang->getValueName(I);
495 Constant *C = I->getInitializer();
496 unsigned Align = TD->getPreferredAlignmentLog(I);
497 bool bCustomSegment = false;
498
499 switch (I->getLinkage()) {
Duncan Sandsb95df792009-03-11 20:14:15 +0000500 case GlobalValue::CommonLinkage:
Duncan Sands19d161f2009-03-07 15:45:40 +0000501 case GlobalValue::LinkOnceAnyLinkage:
502 case GlobalValue::LinkOnceODRLinkage:
503 case GlobalValue::WeakAnyLinkage:
504 case GlobalValue::WeakODRLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000506 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507 bCustomSegment = true;
508 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
509 // are also available.
510 break;
511 case GlobalValue::AppendingLinkage:
512 SwitchToDataSection("");
Eli Friedman378ea832009-06-19 04:48:38 +0000513 O << name << "?\tSEGMENT PARA public 'DATA'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514 bCustomSegment = true;
515 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
516 // are also available.
517 break;
518 case GlobalValue::DLLExportLinkage:
519 DLLExportedGVs.insert(name);
520 // FALL THROUGH
521 case GlobalValue::ExternalLinkage:
522 O << "\tpublic " << name << "\n";
523 // FALL THROUGH
524 case GlobalValue::InternalLinkage:
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000525 SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 break;
527 default:
528 assert(0 && "Unknown linkage type!");
529 }
530
531 if (!bCustomSegment)
532 EmitAlignment(Align, I);
533
Evan Cheng11db8142009-03-24 00:17:40 +0000534 O << name << ":";
535 if (VerboseAsm)
Evan Cheng4c7969e2009-03-25 01:08:42 +0000536 O << "\t\t\t\t" << TAI->getCommentString()
Evan Cheng11db8142009-03-24 00:17:40 +0000537 << " " << I->getName();
538 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539
540 EmitGlobalConstant(C);
541
542 if (bCustomSegment)
543 O << name << "?\tends\n";
544 }
545
546 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000547 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000548 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000549 O << "; WARNING: The following code is valid only with MASM v8.x"
550 << "and (possible) higher\n"
551 << "; This version of MASM is usually shipped with Microsoft "
552 << "Visual Studio 2005\n"
553 << "; or (possible) further versions. Unfortunately, there is no "
554 << "way to support\n"
555 << "; dllexported symbols in the earlier versions of MASM in fully "
556 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 O << "_drectve\t segment info alias('.drectve')\n";
558 }
559
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000560 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 e = DLLExportedGVs.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() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000565 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000567 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000568 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000570 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000571 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000572
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000574 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 SwitchToDataSection("");
576 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000577 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578}
579
580void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
581 unsigned NumElts = CVA->getNumOperands();
582 if (NumElts) {
583 // ML does not have escape sequences except '' for '. It also has a maximum
584 // string length of 255.
585 unsigned len = 0;
586 bool inString = false;
587 for (unsigned i = 0; i < NumElts; i++) {
588 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
589 if (len == 0)
590 O << "\tdb ";
591
592 if (n >= 32 && n <= 127) {
593 if (!inString) {
594 if (len > 0) {
595 O << ",'";
596 len += 2;
597 } else {
598 O << "'";
599 len++;
600 }
601 inString = true;
602 }
603 if (n == '\'') {
604 O << "'";
605 len++;
606 }
607 O << char(n);
608 } else {
609 if (inString) {
610 O << "'";
611 len++;
612 inString = false;
613 }
614 if (len > 0) {
615 O << ",";
616 len++;
617 }
618 O << n;
619 len += 1 + (n > 9) + (n > 99);
620 }
621
622 if (len > 60) {
623 if (inString) {
624 O << "'";
625 inString = false;
626 }
627 O << "\n";
628 len = 0;
629 }
630 }
631
632 if (len > 0) {
633 if (inString)
634 O << "'";
635 O << "\n";
636 }
637 }
638}
639
640// Include the auto-generated portion of the assembly writer.
641#include "X86GenAsmWriter1.inc"