blob: 4868906a041644d211515d43de358842f58a9aa6 [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
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
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!");
148 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;
223 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner6017d482007-12-30 23:10:15 +0000224 printBasicBlockLabel(MO.getMBB());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 return;
226 case MachineOperand::MO_JumpTableIndex: {
227 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
228 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000229 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000230 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000232 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 case MachineOperand::MO_ConstantPoolIndex: {
234 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
235 if (!isMemOp) O << "OFFSET ";
236 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000237 << getFunctionNumber() << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 int Offset = MO.getOffset();
239 if (Offset > 0)
240 O << " + " << Offset;
241 else if (Offset < 0)
242 O << Offset;
243 O << "]";
244 return;
245 }
246 case MachineOperand::MO_GlobalAddress: {
247 bool isCallOp = Modifier && !strcmp(Modifier, "call");
248 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
254 if (!isMemOp && !isCallOp) O << "OFFSET ";
255 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;
261 int Offset = MO.getOffset();
262 if (Offset > 0)
263 O << " + " << Offset;
264 else if (Offset < 0)
265 O << Offset;
266 return;
267 }
268 case MachineOperand::MO_ExternalSymbol: {
269 bool isCallOp = Modifier && !strcmp(Modifier, "call");
270 if (!isCallOp) O << "OFFSET ";
271 O << TAI->getGlobalPrefix() << MO.getSymbolName();
272 return;
273 }
274 default:
275 O << "<unknown operand type>"; return;
276 }
277}
278
279void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
280 const char *Modifier) {
281 assert(isMem(MI, Op) && "Invalid memory reference!");
282
283 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000284 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 const MachineOperand &IndexReg = MI->getOperand(Op+2);
286 const MachineOperand &DispSpec = MI->getOperand(Op+3);
287
288 O << "[";
289 bool NeedPlus = false;
290 if (BaseReg.getReg()) {
291 printOp(BaseReg, Modifier);
292 NeedPlus = true;
293 }
294
295 if (IndexReg.getReg()) {
296 if (NeedPlus) O << " + ";
297 if (ScaleVal != 1)
298 O << ScaleVal << "*";
299 printOp(IndexReg, Modifier);
300 NeedPlus = true;
301 }
302
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000303 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
304 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 if (NeedPlus)
306 O << " + ";
307 printOp(DispSpec, "mem");
308 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000309 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000311 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 if (DispVal > 0)
313 O << " + ";
314 else {
315 O << " - ";
316 DispVal = -DispVal;
317 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000318 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 O << DispVal;
320 }
321 }
322 O << "]";
323}
324
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000325void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000326 const MachineBasicBlock *MBB) const {
327 if (!TAI->getSetDirective())
328 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000329
Evan Cheng6fb06762007-11-09 01:32:10 +0000330 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
331 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000332 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000333 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
334}
335
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng477013c2007-10-14 05:57:21 +0000337 O << "\"L" << getFunctionNumber() << "$pb\"\n";
338 O << "\"L" << getFunctionNumber() << "$pb\":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339}
340
341bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
342 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 unsigned Reg = MO.getReg();
344 switch (Mode) {
345 default: return true; // Unknown mode.
346 case 'b': // Print QImode register
347 Reg = getX86SubSuperRegister(Reg, MVT::i8);
348 break;
349 case 'h': // Print QImode high register
350 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
351 break;
352 case 'w': // Print HImode register
353 Reg = getX86SubSuperRegister(Reg, MVT::i16);
354 break;
355 case 'k': // Print SImode register
356 Reg = getX86SubSuperRegister(Reg, MVT::i32);
357 break;
358 }
359
Evan Cheng00d04a72008-07-07 22:21:06 +0000360 O << '%' << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 return false;
362}
363
364/// PrintAsmOperand - Print out an operand for an inline asm expression.
365///
366bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000367 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 const char *ExtraCode) {
369 // Does this asm operand have a single letter operand modifier?
370 if (ExtraCode && ExtraCode[0]) {
371 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000372
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373 switch (ExtraCode[0]) {
374 default: return true; // Unknown modifier.
375 case 'b': // Print QImode register
376 case 'h': // Print QImode high register
377 case 'w': // Print HImode register
378 case 'k': // Print SImode register
379 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
380 }
381 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000382
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 printOperand(MI, OpNo);
384 return false;
385}
386
387bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
388 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000389 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 const char *ExtraCode) {
391 if (ExtraCode && ExtraCode[0])
392 return true; // Unknown modifier.
393 printMemReference(MI, OpNo);
394 return false;
395}
396
397/// printMachineInstruction -- Print out a single X86 LLVM instruction
398/// MI in Intel syntax to the current output stream.
399///
400void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
401 ++EmittedInsts;
402
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403 // Call the autogenerated instruction printer routines.
404 printInstruction(MI);
405}
406
407bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000408 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000409
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 Mang->markCharUnacceptable('.');
411
412 O << "\t.686\n\t.model flat\n\n";
413
414 // Emit declarations for external functions.
415 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
416 if (I->isDeclaration()) {
417 std::string Name = Mang->getValueName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000418 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
420 O << "\textern " ;
421 if (I->hasDLLImportLinkage()) {
422 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000423 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 O << Name << ":near\n";
425 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 // Emit declarations for external globals. Note that VC++ always declares
428 // external globals to have type byte, and if that's good enough for VC++...
429 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
430 I != E; ++I) {
431 if (I->isDeclaration()) {
432 std::string Name = Mang->getValueName(I);
433
434 O << "\textern " ;
435 if (I->hasDLLImportLinkage()) {
436 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000437 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 O << Name << ":byte\n";
439 }
440 }
441
Dan Gohman4a558a32007-07-25 19:33:14 +0000442 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443}
444
445bool X86IntelAsmPrinter::doFinalization(Module &M) {
446 const TargetData *TD = TM.getTargetData();
447
448 // Print out module-level global variables here.
449 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
450 I != E; ++I) {
451 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000452
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 // Check to see if this is a special global used by LLVM, if so, emit it.
454 if (EmitSpecialLLVMGlobal(I))
455 continue;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000456
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 std::string name = Mang->getValueName(I);
458 Constant *C = I->getInitializer();
459 unsigned Align = TD->getPreferredAlignmentLog(I);
460 bool bCustomSegment = false;
461
462 switch (I->getLinkage()) {
Dale Johannesen49c44122008-05-14 20:12:51 +0000463 case GlobalValue::CommonLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000464 case GlobalValue::LinkOnceLinkage:
465 case GlobalValue::WeakLinkage:
466 SwitchToDataSection("");
467 O << name << "?\tsegment common 'COMMON'\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::AppendingLinkage:
473 SwitchToDataSection("");
474 O << name << "?\tsegment public 'DATA'\n";
475 bCustomSegment = true;
476 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
477 // are also available.
478 break;
479 case GlobalValue::DLLExportLinkage:
480 DLLExportedGVs.insert(name);
481 // FALL THROUGH
482 case GlobalValue::ExternalLinkage:
483 O << "\tpublic " << name << "\n";
484 // FALL THROUGH
485 case GlobalValue::InternalLinkage:
Anton Korobeynikovcca60fa2008-09-24 22:16:16 +0000486 SwitchToSection(TAI->getDataSection());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000487 break;
488 default:
489 assert(0 && "Unknown linkage type!");
490 }
491
492 if (!bCustomSegment)
493 EmitAlignment(Align, I);
494
495 O << name << ":\t\t\t\t" << TAI->getCommentString()
496 << " " << I->getName() << '\n';
497
498 EmitGlobalConstant(C);
499
500 if (bCustomSegment)
501 O << name << "?\tends\n";
502 }
503
504 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000505 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 SwitchToDataSection("");
Evan Chengfb62bbc2008-09-20 00:13:08 +0000507 O << "; WARNING: The following code is valid only with MASM v8.x"
508 << "and (possible) higher\n"
509 << "; This version of MASM is usually shipped with Microsoft "
510 << "Visual Studio 2005\n"
511 << "; or (possible) further versions. Unfortunately, there is no "
512 << "way to support\n"
513 << "; dllexported symbols in the earlier versions of MASM in fully "
514 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 O << "_drectve\t segment info alias('.drectve')\n";
516 }
517
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000518 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 e = DLLExportedGVs.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000520 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000521 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000522
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000523 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 e = DLLExportedFns.end();
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000525 i != e; ++i)
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000526 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000528 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000529 O << "_drectve\t ends\n";
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000530
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000532 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 SwitchToDataSection("");
534 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000535 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536}
537
538void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
539 unsigned NumElts = CVA->getNumOperands();
540 if (NumElts) {
541 // ML does not have escape sequences except '' for '. It also has a maximum
542 // string length of 255.
543 unsigned len = 0;
544 bool inString = false;
545 for (unsigned i = 0; i < NumElts; i++) {
546 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
547 if (len == 0)
548 O << "\tdb ";
549
550 if (n >= 32 && n <= 127) {
551 if (!inString) {
552 if (len > 0) {
553 O << ",'";
554 len += 2;
555 } else {
556 O << "'";
557 len++;
558 }
559 inString = true;
560 }
561 if (n == '\'') {
562 O << "'";
563 len++;
564 }
565 O << char(n);
566 } else {
567 if (inString) {
568 O << "'";
569 len++;
570 inString = false;
571 }
572 if (len > 0) {
573 O << ",";
574 len++;
575 }
576 O << n;
577 len += 1 + (n > 9) + (n > 99);
578 }
579
580 if (len > 60) {
581 if (inString) {
582 O << "'";
583 inString = false;
584 }
585 O << "\n";
586 len = 0;
587 }
588 }
589
590 if (len > 0) {
591 if (inString)
592 O << "'";
593 O << "\n";
594 }
595 }
596}
597
598// Include the auto-generated portion of the assembly writer.
599#include "X86GenAsmWriter1.inc"