blob: a75422a56d5c928076ce35954a400120a27674d5 [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"
28#include "llvm/Support/Mangler.h"
29#include "llvm/Target/TargetAsmInfo.h"
30#include "llvm/Target/TargetOptions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031using namespace llvm;
32
33STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000035static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
36 const TargetData *TD) {
37 X86MachineFunctionInfo Info;
38 uint64_t Size = 0;
39
40 switch (F->getCallingConv()) {
41 case CallingConv::X86_StdCall:
42 Info.setDecorationStyle(StdCall);
43 break;
44 case CallingConv::X86_FastCall:
45 Info.setDecorationStyle(FastCall);
46 break;
47 default:
48 return Info;
49 }
50
51 unsigned argNum = 1;
52 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
53 AI != AE; ++AI, ++argNum) {
54 const Type* Ty = AI->getType();
55
56 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000057 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000058 Ty = cast<PointerType>(Ty)->getElementType();
59
60 // Size should be aligned to DWORD boundary
Duncan Sandsd68f13b2009-01-12 20:38:59 +000061 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000062 }
63
64 // We're not supporting tooooo huge arguments :)
65 Info.setBytesToPopOnReturn((unsigned int)Size);
66 return Info;
67}
68
69
70/// decorateName - Query FunctionInfoMap and use this information for various
71/// name decoration.
72void X86IntelAsmPrinter::decorateName(std::string &Name,
73 const GlobalValue *GV) {
74 const Function *F = dyn_cast<Function>(GV);
75 if (!F) return;
76
77 // We don't want to decorate non-stdcall or non-fastcall functions right now
78 unsigned CC = F->getCallingConv();
79 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
80 return;
81
82 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
83
84 const X86MachineFunctionInfo *Info;
85 if (info_item == FunctionInfoMap.end()) {
86 // Calculate apropriate function info and populate map
87 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
88 Info = &FunctionInfoMap[F];
89 } else {
90 Info = &info_item->second;
91 }
92
93 const FunctionType *FT = F->getFunctionType();
94 switch (Info->getDecorationStyle()) {
95 case None:
96 break;
97 case StdCall:
98 // "Pure" variadic functions do not receive @0 suffix.
99 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
100 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
101 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
102 break;
103 case FastCall:
104 // "Pure" variadic functions do not receive @0 suffix.
105 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
106 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
107 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
108
109 if (Name[0] == '_')
110 Name[0] = '@';
111 else
112 Name = '@' + Name;
113
114 break;
115 default:
116 assert(0 && "Unsupported DecorationStyle");
117 }
118}
119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120/// runOnMachineFunction - This uses the printMachineInstruction()
121/// method to print assembly for each instruction.
122///
123bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
124 SetupMachineFunction(MF);
125 O << "\n\n";
126
127 // Print out constants referenced by the function
128 EmitConstantPool(MF.getConstantPool());
129
130 // Print out labels for the function.
131 const Function *F = MF.getFunction();
132 unsigned CC = F->getCallingConv();
133
134 // Populate function information map. Actually, We don't want to populate
135 // non-stdcall or non-fastcall functions' information right now.
136 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
137 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
138
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000139 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140
Anton Korobeynikovcf87a3d2008-09-24 22:13:07 +0000141 SwitchToTextSection("_text", F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142
Devang Patel93698d92008-10-01 23:18:38 +0000143 unsigned FnAlign = 4;
Devang Pateld3659412008-10-06 17:30:07 +0000144 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Patel009a8d12008-09-04 21:03:41 +0000145 FnAlign = 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 switch (F->getLinkage()) {
147 default: assert(0 && "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;
224 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000225 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 return;
227 case MachineOperand::MO_JumpTableIndex: {
228 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
229 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000230 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000231 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000233 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 case MachineOperand::MO_ConstantPoolIndex: {
235 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
236 if (!isMemOp) O << "OFFSET ";
237 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000238 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000239 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 O << "]";
241 return;
242 }
243 case MachineOperand::MO_GlobalAddress: {
244 bool isCallOp = Modifier && !strcmp(Modifier, "call");
245 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000246 GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 std::string Name = Mang->getValueName(GV);
248
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000249 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
251 if (!isMemOp && !isCallOp) O << "OFFSET ";
252 if (GV->hasDLLImportLinkage()) {
253 // FIXME: This should be fixed with full support of stdcall & fastcall
254 // CC's
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000255 O << "__imp_";
256 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 O << Name;
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000258 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 return;
260 }
261 case MachineOperand::MO_ExternalSymbol: {
262 bool isCallOp = Modifier && !strcmp(Modifier, "call");
263 if (!isCallOp) O << "OFFSET ";
264 O << TAI->getGlobalPrefix() << MO.getSymbolName();
265 return;
266 }
267 default:
268 O << "<unknown operand type>"; return;
269 }
270}
271
272void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
273 const char *Modifier) {
274 assert(isMem(MI, Op) && "Invalid memory reference!");
275
276 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000277 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 const MachineOperand &IndexReg = MI->getOperand(Op+2);
279 const MachineOperand &DispSpec = MI->getOperand(Op+3);
280
281 O << "[";
282 bool NeedPlus = false;
283 if (BaseReg.getReg()) {
284 printOp(BaseReg, Modifier);
285 NeedPlus = true;
286 }
287
288 if (IndexReg.getReg()) {
289 if (NeedPlus) O << " + ";
290 if (ScaleVal != 1)
291 O << ScaleVal << "*";
292 printOp(IndexReg, Modifier);
293 NeedPlus = true;
294 }
295
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000296 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
297 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 if (NeedPlus)
299 O << " + ";
300 printOp(DispSpec, "mem");
301 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000302 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000304 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 if (DispVal > 0)
306 O << " + ";
307 else {
308 O << " - ";
309 DispVal = -DispVal;
310 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000311 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 O << DispVal;
313 }
314 }
315 O << "]";
316}
317
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000318void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000319 const MachineBasicBlock *MBB) const {
320 if (!TAI->getSetDirective())
321 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000322
Evan Cheng6fb06762007-11-09 01:32:10 +0000323 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
324 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000325 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000326 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
327}
328
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng477013c2007-10-14 05:57:21 +0000330 O << "\"L" << getFunctionNumber() << "$pb\"\n";
331 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332}
333
334bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
335 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 unsigned Reg = MO.getReg();
337 switch (Mode) {
338 default: return true; // Unknown mode.
339 case 'b': // Print QImode register
340 Reg = getX86SubSuperRegister(Reg, MVT::i8);
341 break;
342 case 'h': // Print QImode high register
343 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
344 break;
345 case 'w': // Print HImode register
346 Reg = getX86SubSuperRegister(Reg, MVT::i16);
347 break;
348 case 'k': // Print SImode register
349 Reg = getX86SubSuperRegister(Reg, MVT::i32);
350 break;
351 }
352
Evan Cheng00d04a72008-07-07 22:21:06 +0000353 O << '%' << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 return false;
355}
356
357/// PrintAsmOperand - Print out an operand for an inline asm expression.
358///
359bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000360 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 const char *ExtraCode) {
362 // Does this asm operand have a single letter operand modifier?
363 if (ExtraCode && ExtraCode[0]) {
364 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000365
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366 switch (ExtraCode[0]) {
367 default: return true; // Unknown modifier.
368 case 'b': // Print QImode register
369 case 'h': // Print QImode high register
370 case 'w': // Print HImode register
371 case 'k': // Print SImode register
372 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
373 }
374 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 printOperand(MI, OpNo);
377 return false;
378}
379
380bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
381 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000382 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 const char *ExtraCode) {
384 if (ExtraCode && ExtraCode[0])
385 return true; // Unknown modifier.
386 printMemReference(MI, OpNo);
387 return false;
388}
389
390/// printMachineInstruction -- Print out a single X86 LLVM instruction
391/// MI in Intel syntax to the current output stream.
392///
393void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
394 ++EmittedInsts;
395
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 // Call the autogenerated instruction printer routines.
397 printInstruction(MI);
398}
399
400bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000401 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000402
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 Mang->markCharUnacceptable('.');
404
405 O << "\t.686\n\t.model flat\n\n";
406
407 // Emit declarations for external functions.
408 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
409 if (I->isDeclaration()) {
410 std::string Name = Mang->getValueName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000411 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
413 O << "\textern " ;
414 if (I->hasDLLImportLinkage()) {
415 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000416 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417 O << Name << ":near\n";
418 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000419
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 // Emit declarations for external globals. Note that VC++ always declares
421 // external globals to have type byte, and if that's good enough for VC++...
422 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
423 I != E; ++I) {
424 if (I->isDeclaration()) {
425 std::string Name = Mang->getValueName(I);
426
427 O << "\textern " ;
428 if (I->hasDLLImportLinkage()) {
429 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000430 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 O << Name << ":byte\n";
432 }
433 }
434
Dan Gohman4a558a32007-07-25 19:33:14 +0000435 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436}
437
438bool X86IntelAsmPrinter::doFinalization(Module &M) {
439 const TargetData *TD = TM.getTargetData();
440
441 // Print out module-level global variables here.
442 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
443 I != E; ++I) {
444 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000445
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 // Check to see if this is a special global used by LLVM, if so, emit it.
447 if (EmitSpecialLLVMGlobal(I))
448 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000449
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 std::string name = Mang->getValueName(I);
451 Constant *C = I->getInitializer();
452 unsigned Align = TD->getPreferredAlignmentLog(I);
453 bool bCustomSegment = false;
454
455 switch (I->getLinkage()) {
Dale Johannesen49c44122008-05-14 20:12:51 +0000456 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 case GlobalValue::LinkOnceLinkage:
458 case GlobalValue::WeakLinkage:
459 SwitchToDataSection("");
460 O << name << "?\tsegment common 'COMMON'\n";
461 bCustomSegment = true;
462 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
463 // are also available.
464 break;
465 case GlobalValue::AppendingLinkage:
466 SwitchToDataSection("");
467 O << name << "?\tsegment public 'DATA'\n";
468 bCustomSegment = true;
469 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
470 // are also available.
471 break;
472 case GlobalValue::DLLExportLinkage:
473 DLLExportedGVs.insert(name);
474 // FALL THROUGH
475 case GlobalValue::ExternalLinkage:
476 O << "\tpublic " << name << "\n";
477 // FALL THROUGH
478 case GlobalValue::InternalLinkage:
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000479 SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000480 break;
481 default:
482 assert(0 && "Unknown linkage type!");
483 }
484
485 if (!bCustomSegment)
486 EmitAlignment(Align, I);
487
488 O << name << ":\t\t\t\t" << TAI->getCommentString()
489 << " " << I->getName() << '\n';
490
491 EmitGlobalConstant(C);
492
493 if (bCustomSegment)
494 O << name << "?\tends\n";
495 }
496
497 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000498 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000500 O << "; WARNING: The following code is valid only with MASM v8.x"
501 << "and (possible) higher\n"
502 << "; This version of MASM is usually shipped with Microsoft "
503 << "Visual Studio 2005\n"
504 << "; or (possible) further versions. Unfortunately, there is no "
505 << "way to support\n"
506 << "; dllexported symbols in the earlier versions of MASM in fully "
507 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 O << "_drectve\t segment info alias('.drectve')\n";
509 }
510
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000511 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 e = DLLExportedGVs.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000513 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000514 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000516 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000518 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000519 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000521 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000522 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000525 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 SwitchToDataSection("");
527 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000528 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529}
530
531void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
532 unsigned NumElts = CVA->getNumOperands();
533 if (NumElts) {
534 // ML does not have escape sequences except '' for '. It also has a maximum
535 // string length of 255.
536 unsigned len = 0;
537 bool inString = false;
538 for (unsigned i = 0; i < NumElts; i++) {
539 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
540 if (len == 0)
541 O << "\tdb ";
542
543 if (n >= 32 && n <= 127) {
544 if (!inString) {
545 if (len > 0) {
546 O << ",'";
547 len += 2;
548 } else {
549 O << "'";
550 len++;
551 }
552 inString = true;
553 }
554 if (n == '\'') {
555 O << "'";
556 len++;
557 }
558 O << char(n);
559 } else {
560 if (inString) {
561 O << "'";
562 len++;
563 inString = false;
564 }
565 if (len > 0) {
566 O << ",";
567 len++;
568 }
569 O << n;
570 len += 1 + (n > 9) + (n > 99);
571 }
572
573 if (len > 60) {
574 if (inString) {
575 O << "'";
576 inString = false;
577 }
578 O << "\n";
579 len = 0;
580 }
581 }
582
583 if (len > 0) {
584 if (inString)
585 O << "'";
586 O << "\n";
587 }
588 }
589}
590
591// Include the auto-generated portion of the assembly writer.
592#include "X86GenAsmWriter1.inc"