blob: 1822c1f914a42eed9c820761a7251187532d5998 [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"
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000018#include "X86InstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "X86TargetAsmInfo.h"
20#include "X86.h"
21#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
57 if (F->paramHasAttr(argNum, ParamAttr::ByVal))
58 Ty = cast<PointerType>(Ty)->getElementType();
59
60 // Size should be aligned to DWORD boundary
61 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
62 }
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
120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const {
122 // Intel asm always emits functions to _text.
123 return "_text";
124}
125
126/// runOnMachineFunction - This uses the printMachineInstruction()
127/// method to print assembly for each instruction.
128///
129bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
130 SetupMachineFunction(MF);
131 O << "\n\n";
132
133 // Print out constants referenced by the function
134 EmitConstantPool(MF.getConstantPool());
135
136 // Print out labels for the function.
137 const Function *F = MF.getFunction();
138 unsigned CC = F->getCallingConv();
139
140 // Populate function information map. Actually, We don't want to populate
141 // non-stdcall or non-fastcall functions' information right now.
142 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
143 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
144
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000145 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
147 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
148
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000149 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 switch (F->getLinkage()) {
151 default: assert(0 && "Unsupported linkage type!");
152 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000153 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000154 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 case Function::DLLExportLinkage:
156 DLLExportedFns.insert(CurrentFnName);
157 //FALLS THROUGH
158 case Function::ExternalLinkage:
159 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000160 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000161 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000163
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 // Print out code for the function.
167 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
168 I != E; ++I) {
169 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000170 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000171 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 O << '\n';
173 }
174 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
175 II != E; ++II) {
176 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 printMachineInstruction(II);
178 }
179 }
180
181 // Print out jump tables referenced by the function.
182 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
183
184 O << CurrentFnName << "\tendp\n";
185
186 // We didn't modify anything.
187 return false;
188}
189
190void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000191 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 assert(value <= 7 && "Invalid ssecc argument!");
193 switch (value) {
194 case 0: O << "eq"; break;
195 case 1: O << "lt"; break;
196 case 2: O << "le"; break;
197 case 3: O << "unord"; break;
198 case 4: O << "neq"; break;
199 case 5: O << "nlt"; break;
200 case 6: O << "nle"; break;
201 case 7: O << "ord"; break;
202 }
203}
204
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000205void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000208 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000209 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 unsigned Reg = MO.getReg();
211 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands92c43912008-06-06 12:08:01 +0000212 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
214 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
215 Reg = getX86SubSuperRegister(Reg, VT);
216 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000217 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 } else
219 O << "reg" << MO.getReg();
220 return;
221 }
222 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000223 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 return;
225 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000226 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 return;
228 case MachineOperand::MO_JumpTableIndex: {
229 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
230 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000231 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000232 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000234 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 case MachineOperand::MO_ConstantPoolIndex: {
236 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
237 if (!isMemOp) O << "OFFSET ";
238 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000239 << getFunctionNumber() << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 int Offset = MO.getOffset();
241 if (Offset > 0)
242 O << " + " << Offset;
243 else if (Offset < 0)
244 O << Offset;
245 O << "]";
246 return;
247 }
248 case MachineOperand::MO_GlobalAddress: {
249 bool isCallOp = Modifier && !strcmp(Modifier, "call");
250 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000251 GlobalValue *GV = MO.getGlobal();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 std::string Name = Mang->getValueName(GV);
253
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000254 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
256 if (!isMemOp && !isCallOp) O << "OFFSET ";
257 if (GV->hasDLLImportLinkage()) {
258 // FIXME: This should be fixed with full support of stdcall & fastcall
259 // CC's
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000260 O << "__imp_";
261 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 O << Name;
263 int Offset = MO.getOffset();
264 if (Offset > 0)
265 O << " + " << Offset;
266 else if (Offset < 0)
267 O << Offset;
268 return;
269 }
270 case MachineOperand::MO_ExternalSymbol: {
271 bool isCallOp = Modifier && !strcmp(Modifier, "call");
272 if (!isCallOp) O << "OFFSET ";
273 O << TAI->getGlobalPrefix() << MO.getSymbolName();
274 return;
275 }
276 default:
277 O << "<unknown operand type>"; return;
278 }
279}
280
281void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
282 const char *Modifier) {
283 assert(isMem(MI, Op) && "Invalid memory reference!");
284
285 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000286 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 const MachineOperand &IndexReg = MI->getOperand(Op+2);
288 const MachineOperand &DispSpec = MI->getOperand(Op+3);
289
290 O << "[";
291 bool NeedPlus = false;
292 if (BaseReg.getReg()) {
293 printOp(BaseReg, Modifier);
294 NeedPlus = true;
295 }
296
297 if (IndexReg.getReg()) {
298 if (NeedPlus) O << " + ";
299 if (ScaleVal != 1)
300 O << ScaleVal << "*";
301 printOp(IndexReg, Modifier);
302 NeedPlus = true;
303 }
304
305 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
306 DispSpec.isJumpTableIndex()) {
307 if (NeedPlus)
308 O << " + ";
309 printOp(DispSpec, "mem");
310 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000311 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000313 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 if (DispVal > 0)
315 O << " + ";
316 else {
317 O << " - ";
318 DispVal = -DispVal;
319 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000320 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321 O << DispVal;
322 }
323 }
324 O << "]";
325}
326
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000327void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000328 const MachineBasicBlock *MBB) const {
329 if (!TAI->getSetDirective())
330 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000331
Evan Cheng6fb06762007-11-09 01:32:10 +0000332 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
333 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000334 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000335 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
336}
337
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng477013c2007-10-14 05:57:21 +0000339 O << "\"L" << getFunctionNumber() << "$pb\"\n";
340 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341}
342
343bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
344 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 unsigned Reg = MO.getReg();
346 switch (Mode) {
347 default: return true; // Unknown mode.
348 case 'b': // Print QImode register
349 Reg = getX86SubSuperRegister(Reg, MVT::i8);
350 break;
351 case 'h': // Print QImode high register
352 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
353 break;
354 case 'w': // Print HImode register
355 Reg = getX86SubSuperRegister(Reg, MVT::i16);
356 break;
357 case 'k': // Print SImode register
358 Reg = getX86SubSuperRegister(Reg, MVT::i32);
359 break;
360 }
361
Evan Cheng00d04a72008-07-07 22:21:06 +0000362 O << '%' << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 return false;
364}
365
366/// PrintAsmOperand - Print out an operand for an inline asm expression.
367///
368bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000369 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370 const char *ExtraCode) {
371 // Does this asm operand have a single letter operand modifier?
372 if (ExtraCode && ExtraCode[0]) {
373 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000374
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 switch (ExtraCode[0]) {
376 default: return true; // Unknown modifier.
377 case 'b': // Print QImode register
378 case 'h': // Print QImode high register
379 case 'w': // Print HImode register
380 case 'k': // Print SImode register
381 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
382 }
383 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 printOperand(MI, OpNo);
386 return false;
387}
388
389bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
390 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000391 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 const char *ExtraCode) {
393 if (ExtraCode && ExtraCode[0])
394 return true; // Unknown modifier.
395 printMemReference(MI, OpNo);
396 return false;
397}
398
399/// printMachineInstruction -- Print out a single X86 LLVM instruction
400/// MI in Intel syntax to the current output stream.
401///
402void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
403 ++EmittedInsts;
404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 // Call the autogenerated instruction printer routines.
406 printInstruction(MI);
407}
408
409bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000410 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 Mang->markCharUnacceptable('.');
413
414 O << "\t.686\n\t.model flat\n\n";
415
416 // Emit declarations for external functions.
417 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
418 if (I->isDeclaration()) {
419 std::string Name = Mang->getValueName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000420 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000421
422 O << "\textern " ;
423 if (I->hasDLLImportLinkage()) {
424 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000425 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 O << Name << ":near\n";
427 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000428
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000429 // Emit declarations for external globals. Note that VC++ always declares
430 // external globals to have type byte, and if that's good enough for VC++...
431 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
432 I != E; ++I) {
433 if (I->isDeclaration()) {
434 std::string Name = Mang->getValueName(I);
435
436 O << "\textern " ;
437 if (I->hasDLLImportLinkage()) {
438 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000439 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000440 O << Name << ":byte\n";
441 }
442 }
443
Dan Gohman4a558a32007-07-25 19:33:14 +0000444 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445}
446
447bool X86IntelAsmPrinter::doFinalization(Module &M) {
448 const TargetData *TD = TM.getTargetData();
449
450 // Print out module-level global variables here.
451 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
452 I != E; ++I) {
453 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000454
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 // Check to see if this is a special global used by LLVM, if so, emit it.
456 if (EmitSpecialLLVMGlobal(I))
457 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000458
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 std::string name = Mang->getValueName(I);
460 Constant *C = I->getInitializer();
461 unsigned Align = TD->getPreferredAlignmentLog(I);
462 bool bCustomSegment = false;
463
464 switch (I->getLinkage()) {
Dale Johannesen49c44122008-05-14 20:12:51 +0000465 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 case GlobalValue::LinkOnceLinkage:
467 case GlobalValue::WeakLinkage:
468 SwitchToDataSection("");
469 O << name << "?\tsegment common 'COMMON'\n";
470 bCustomSegment = true;
471 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
472 // are also available.
473 break;
474 case GlobalValue::AppendingLinkage:
475 SwitchToDataSection("");
476 O << name << "?\tsegment public 'DATA'\n";
477 bCustomSegment = true;
478 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
479 // are also available.
480 break;
481 case GlobalValue::DLLExportLinkage:
482 DLLExportedGVs.insert(name);
483 // FALL THROUGH
484 case GlobalValue::ExternalLinkage:
485 O << "\tpublic " << name << "\n";
486 // FALL THROUGH
487 case GlobalValue::InternalLinkage:
488 SwitchToDataSection(TAI->getDataSection(), I);
489 break;
490 default:
491 assert(0 && "Unknown linkage type!");
492 }
493
494 if (!bCustomSegment)
495 EmitAlignment(Align, I);
496
497 O << name << ":\t\t\t\t" << TAI->getCommentString()
498 << " " << I->getName() << '\n';
499
500 EmitGlobalConstant(C);
501
502 if (bCustomSegment)
503 O << name << "?\tends\n";
504 }
505
506 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000507 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 SwitchToDataSection("");
509 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
510 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
511 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
512 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
513 O << "_drectve\t segment info alias('.drectve')\n";
514 }
515
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000516 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 e = DLLExportedGVs.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() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000521 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000523 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000524 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000526 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000527 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000528
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000530 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 SwitchToDataSection("");
532 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000533 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534}
535
536void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
537 unsigned NumElts = CVA->getNumOperands();
538 if (NumElts) {
539 // ML does not have escape sequences except '' for '. It also has a maximum
540 // string length of 255.
541 unsigned len = 0;
542 bool inString = false;
543 for (unsigned i = 0; i < NumElts; i++) {
544 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
545 if (len == 0)
546 O << "\tdb ";
547
548 if (n >= 32 && n <= 127) {
549 if (!inString) {
550 if (len > 0) {
551 O << ",'";
552 len += 2;
553 } else {
554 O << "'";
555 len++;
556 }
557 inString = true;
558 }
559 if (n == '\'') {
560 O << "'";
561 len++;
562 }
563 O << char(n);
564 } else {
565 if (inString) {
566 O << "'";
567 len++;
568 inString = false;
569 }
570 if (len > 0) {
571 O << ",";
572 len++;
573 }
574 O << n;
575 len += 1 + (n > 9) + (n > 99);
576 }
577
578 if (len > 60) {
579 if (inString) {
580 O << "'";
581 inString = false;
582 }
583 O << "\n";
584 len = 0;
585 }
586 }
587
588 if (len > 0) {
589 if (inString)
590 O << "'";
591 O << "\n";
592 }
593 }
594}
595
596// Include the auto-generated portion of the assembly writer.
597#include "X86GenAsmWriter1.inc"