blob: 705488a6c2f356e47704d692500a41897a948186 [file] [log] [blame]
Chris Lattnerb36cbd02005-07-01 22:44:09 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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#include "X86IntelAsmPrinter.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000017#include "X86TargetAsmInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000018#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000019#include "llvm/CallingConv.h"
Jeff Cohenc884db42006-05-02 01:16:28 +000020#include "llvm/Constants.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000021#include "llvm/Module.h"
22#include "llvm/Assembly/Writer.h"
23#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000024#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000026using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027
28/// runOnMachineFunction - This uses the printMachineInstruction()
29/// method to print assembly for each instruction.
30///
31bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000032 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033 O << "\n\n";
34
35 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000036 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037
38 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +000039 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000040 unsigned CC = F->getCallingConv();
41
42 // Populate function information map. Actually, We don't want to populate
43 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000044 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
45 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000046
47 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
48
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000049 switch (F->getLinkage()) {
50 default: assert(0 && "Unsupported linkage type!");
51 case Function::InternalLinkage:
52 SwitchToTextSection("_text", F);
53 EmitAlignment(4);
54 break;
55 case Function::DLLExportLinkage:
56 DLLExportedFns.insert(CurrentFnName);
57 //FALLS THROUGH
58 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +000059 O << "\tpublic " << CurrentFnName << "\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000060 SwitchToTextSection("_text", F);
61 EmitAlignment(4);
62 break;
63 }
64
Jeff Cohenc884db42006-05-02 01:16:28 +000065 O << CurrentFnName << "\tproc near\n";
Jim Laskey6b92b8e2006-04-07 20:44:42 +000066
Chris Lattnerb36cbd02005-07-01 22:44:09 +000067 // Print out code for the function.
68 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
69 I != E; ++I) {
70 // Print a label for the basic block if there are any predecessors.
Nate Begemancdf38c42006-05-02 05:37:32 +000071 if (I->pred_begin() != I->pred_end()) {
72 printBasicBlockLabel(I, true);
73 O << '\n';
74 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000075 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
76 II != E; ++II) {
77 // Print the assembly for the instruction.
78 O << "\t";
79 printMachineInstruction(II);
80 }
81 }
82
Jeff Cohenc884db42006-05-02 01:16:28 +000083 O << CurrentFnName << "\tendp\n";
84
Chris Lattnerb36cbd02005-07-01 22:44:09 +000085 // We didn't modify anything.
86 return false;
87}
88
Nate Begeman391c5d22005-11-30 18:54:35 +000089void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +000090 unsigned char value = MI->getOperand(Op).getImmedValue();
91 assert(value <= 7 && "Invalid ssecc argument!");
92 switch (value) {
93 case 0: O << "eq"; break;
94 case 1: O << "lt"; break;
95 case 2: O << "le"; break;
96 case 3: O << "unord"; break;
97 case 4: O << "neq"; break;
98 case 5: O << "nlt"; break;
99 case 6: O << "nle"; break;
100 case 7: O << "ord"; break;
101 }
102}
103
Chris Lattnera3b8c572006-02-06 23:41:19 +0000104void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
105 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000106 const MRegisterInfo &RI = *TM.getRegisterInfo();
107 switch (MO.getType()) {
Chris Lattner2d90ac72006-05-04 18:05:43 +0000108 case MachineOperand::MO_Register:
Evan Cheng8f7f7122006-05-05 05:40:20 +0000109 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
110 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000111 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000112 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
113 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
114 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000115 Reg = getX86SubSuperRegister(Reg, VT);
116 }
117 O << RI.get(Reg).Name;
118 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000119 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000120 return;
121
Chris Lattner63b3d712006-05-04 17:21:20 +0000122 case MachineOperand::MO_Immediate:
Evan Cheng138a24e2006-05-26 08:04:31 +0000123 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000124 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000125 case MachineOperand::MO_MachineBasicBlock:
126 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000127 return;
Evan Chenga09bd812006-02-26 08:28:12 +0000128 case MachineOperand::MO_ConstantPoolIndex: {
129 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
130 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000131 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
132 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
Evan Chenga09bd812006-02-26 08:28:12 +0000133 int Offset = MO.getOffset();
134 if (Offset > 0)
135 O << " + " << Offset;
136 else if (Offset < 0)
137 O << Offset;
138 O << "]";
139 return;
140 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000141 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000142 bool isCallOp = Modifier && !strcmp(Modifier, "call");
143 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000144 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000145 std::string Name = Mang->getValueName(GV);
146
147 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000148
Evan Cheng7ccced62006-02-18 00:15:05 +0000149 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000150 if (GV->hasDLLImportLinkage()) {
151 // FIXME: This should be fixed with full support of stdcall & fastcall
152 // CC's
153 O << "__imp_";
154 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000155 O << Name;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000156 int Offset = MO.getOffset();
157 if (Offset > 0)
158 O << " + " << Offset;
159 else if (Offset < 0)
Evan Cheng345c3f32005-11-30 01:59:00 +0000160 O << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000161 return;
162 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000163 case MachineOperand::MO_ExternalSymbol: {
164 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000165 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000166 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000167 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000168 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000169 default:
170 O << "<unknown operand type>"; return;
171 }
172}
173
Evan Cheng25ab6902006-09-08 06:48:29 +0000174void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
175 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000176 assert(isMem(MI, Op) && "Invalid memory reference!");
177
178 const MachineOperand &BaseReg = MI->getOperand(Op);
179 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
180 const MachineOperand &IndexReg = MI->getOperand(Op+2);
181 const MachineOperand &DispSpec = MI->getOperand(Op+3);
182
183 if (BaseReg.isFrameIndex()) {
184 O << "[frame slot #" << BaseReg.getFrameIndex();
185 if (DispSpec.getImmedValue())
186 O << " + " << DispSpec.getImmedValue();
187 O << "]";
188 return;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000189 }
190
191 O << "[";
192 bool NeedPlus = false;
193 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000194 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000195 NeedPlus = true;
196 }
197
198 if (IndexReg.getReg()) {
199 if (NeedPlus) O << " + ";
200 if (ScaleVal != 1)
201 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000203 NeedPlus = true;
204 }
205
Evan Chenga09bd812006-02-26 08:28:12 +0000206 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000207 if (NeedPlus)
208 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000209 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000210 } else {
211 int DispVal = DispSpec.getImmedValue();
212 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
213 if (NeedPlus)
214 if (DispVal > 0)
215 O << " + ";
216 else {
217 O << " - ";
218 DispVal = -DispVal;
219 }
220 O << DispVal;
221 }
222 }
223 O << "]";
224}
225
Evan Cheng7ccced62006-02-18 00:15:05 +0000226void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
227 O << "\"L" << getFunctionNumber() << "$pb\"\n";
228 O << "\"L" << getFunctionNumber() << "$pb\":";
229}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000230
Evan Cheng55c25f22006-04-28 23:19:39 +0000231bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000232 const char Mode) {
233 const MRegisterInfo &RI = *TM.getRegisterInfo();
234 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000235 switch (Mode) {
236 default: return true; // Unknown mode.
237 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000238 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000239 break;
240 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000241 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000242 break;
243 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000244 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000245 break;
246 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000247 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000248 break;
249 }
250
Evan Cheng8f7f7122006-05-05 05:40:20 +0000251 O << '%' << RI.get(Reg).Name;
Evan Cheng62f27002006-04-28 23:11:40 +0000252 return false;
253}
254
Evan Cheng3d48a902006-04-28 21:19:05 +0000255/// PrintAsmOperand - Print out an operand for an inline asm expression.
256///
257bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
258 unsigned AsmVariant,
259 const char *ExtraCode) {
260 // Does this asm operand have a single letter operand modifier?
261 if (ExtraCode && ExtraCode[0]) {
262 if (ExtraCode[1] != 0) return true; // Unknown modifier.
263
264 switch (ExtraCode[0]) {
265 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000266 case 'b': // Print QImode register
267 case 'h': // Print QImode high register
268 case 'w': // Print HImode register
269 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000270 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000271 }
272 }
273
274 printOperand(MI, OpNo);
275 return false;
276}
277
278bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
279 unsigned OpNo,
280 unsigned AsmVariant,
281 const char *ExtraCode) {
282 if (ExtraCode && ExtraCode[0])
283 return true; // Unknown modifier.
284 printMemReference(MI, OpNo);
285 return false;
286}
287
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000288/// printMachineInstruction -- Print out a single X86 LLVM instruction
289/// MI in Intel syntax to the current output stream.
290///
291void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
292 ++EmittedInsts;
293
Evan Cheng8f7f7122006-05-05 05:40:20 +0000294 // See if a truncate instruction can be turned into a nop.
295 switch (MI->getOpcode()) {
296 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000297 case X86::TRUNC_64to32:
298 case X86::TRUNC_64to16:
299 case X86::TRUNC_32to16:
300 case X86::TRUNC_32to8:
301 case X86::TRUNC_16to8:
302 case X86::TRUNC_32_to8:
303 case X86::TRUNC_16_to8: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000304 const MachineOperand &MO0 = MI->getOperand(0);
305 const MachineOperand &MO1 = MI->getOperand(1);
306 unsigned Reg0 = MO0.getReg();
307 unsigned Reg1 = MO1.getReg();
Evan Cheng25ab6902006-09-08 06:48:29 +0000308 unsigned Opc = MI->getOpcode();
309 if (Opc == X86::TRUNC_64to32)
310 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
311 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng403be7e2006-05-08 08:01:26 +0000312 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000313 else
Evan Cheng403be7e2006-05-08 08:01:26 +0000314 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskey563321a2006-09-06 18:34:40 +0000315 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng403be7e2006-05-08 08:01:26 +0000316 if (Reg0 != Reg1)
317 O << "\n\t";
Evan Cheng8f7f7122006-05-05 05:40:20 +0000318 break;
319 }
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 case X86::PsMOVZX64rr32:
321 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
322 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000323 }
324
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000325 // Call the autogenerated instruction printer routines.
326 printInstruction(MI);
327}
328
329bool X86IntelAsmPrinter::doInitialization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000330 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000331
Jim Laskey563321a2006-09-06 18:34:40 +0000332 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000333
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000334 O << "\t.686\n\t.model flat\n\n";
335
336 // Emit declarations for external functions.
337 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000338 if (I->isExternal()) {
339 std::string Name = Mang->getValueName(I);
340 X86SharedAsmPrinter::decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000341
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000342 O << "\textern " ;
343 if (I->hasDLLImportLinkage()) {
344 O << "__imp_";
345 }
346 O << Name << ":near\n";
347 }
348
Jeff Cohend43b18d2006-05-06 21:27:14 +0000349 // Emit declarations for external globals. Note that VC++ always declares
350 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000351 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
352 I != E; ++I) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000353 if (I->isExternal()) {
354 std::string Name = Mang->getValueName(I);
355
356 O << "\textern " ;
357 if (I->hasDLLImportLinkage()) {
358 O << "__imp_";
359 }
360 O << Name << ":byte\n";
361 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000362 }
363
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000364 return false;
365}
366
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000367bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000368 const TargetData *TD = TM.getTargetData();
369
370 // Print out module-level global variables here.
371 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
372 I != E; ++I) {
373 if (I->isExternal()) continue; // External global require no code
374
375 // Check to see if this is a special global used by LLVM, if so, emit it.
376 if (EmitSpecialLLVMGlobal(I))
377 continue;
378
379 std::string name = Mang->getValueName(I);
380 Constant *C = I->getInitializer();
381 unsigned Size = TD->getTypeSize(C->getType());
382 unsigned Align = getPreferredAlignmentLog(I);
383 bool bCustomSegment = false;
384
385 switch (I->getLinkage()) {
386 case GlobalValue::LinkOnceLinkage:
387 case GlobalValue::WeakLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000388 SwitchToDataSection("", 0);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000389 O << name << "?\tsegment common 'COMMON'\n";
390 bCustomSegment = true;
391 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
392 // are also available.
393 break;
394 case GlobalValue::AppendingLinkage:
Chris Lattner4632d7a2006-05-09 04:59:56 +0000395 SwitchToDataSection("", 0);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000396 O << name << "?\tsegment public 'DATA'\n";
397 bCustomSegment = true;
398 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
399 // are also available.
400 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000401 case GlobalValue::DLLExportLinkage:
402 DLLExportedGVs.insert(name);
403 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000404 case GlobalValue::ExternalLinkage:
405 O << "\tpublic " << name << "\n";
406 // FALL THROUGH
407 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000408 SwitchToDataSection(TAI->getDataSection(), I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000409 break;
410 default:
411 assert(0 && "Unknown linkage type!");
412 }
413
414 if (!bCustomSegment)
415 EmitAlignment(Align, I);
416
Jim Laskey563321a2006-09-06 18:34:40 +0000417 O << name << ":\t\t\t\t" << TAI->getCommentString()
418 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000419
420 EmitGlobalConstant(C);
421
422 if (bCustomSegment)
423 O << name << "?\tends\n";
424 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000425
426 // Output linker support code for dllexported globals
427 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
428 (DLLExportedFns.begin() != DLLExportedFns.end())) {
429 SwitchToDataSection("", 0);
430 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
431 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
432 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
433 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
434 O << "_drectve\t segment info alias('.drectve')\n";
435 }
436
437 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
438 e = DLLExportedGVs.end();
439 i != e; ++i) {
440 O << "\t db ' /EXPORT:" << *i << ",data'\n";
441 }
442
443 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
444 e = DLLExportedFns.end();
445 i != e; ++i) {
446 O << "\t db ' /EXPORT:" << *i << "'\n";
447 }
448
449 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
450 (DLLExportedFns.begin() != DLLExportedFns.end())) {
451 O << "_drectve\t ends\n";
452 }
Jeff Cohend43b18d2006-05-06 21:27:14 +0000453
454 // Bypass X86SharedAsmPrinter::doFinalization().
455 AsmPrinter::doFinalization(M);
Chris Lattnere7027d52006-05-09 05:15:24 +0000456 SwitchToDataSection("", 0);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000457 O << "\tend\n";
Jeff Cohend43b18d2006-05-06 21:27:14 +0000458 return false; // success
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000459}
460
Jeff Cohenc884db42006-05-02 01:16:28 +0000461void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
462 unsigned NumElts = CVA->getNumOperands();
463 if (NumElts) {
464 // ML does not have escape sequences except '' for '. It also has a maximum
465 // string length of 255.
466 unsigned len = 0;
467 bool inString = false;
468 for (unsigned i = 0; i < NumElts; i++) {
469 int n = cast<ConstantInt>(CVA->getOperand(i))->getRawValue() & 255;
470 if (len == 0)
471 O << "\tdb ";
472
473 if (n >= 32 && n <= 127) {
474 if (!inString) {
475 if (len > 0) {
476 O << ",'";
477 len += 2;
478 } else {
479 O << "'";
480 len++;
481 }
482 inString = true;
483 }
484 if (n == '\'') {
485 O << "'";
486 len++;
487 }
488 O << char(n);
489 } else {
490 if (inString) {
491 O << "'";
492 len++;
493 inString = false;
494 }
495 if (len > 0) {
496 O << ",";
497 len++;
498 }
499 O << n;
500 len += 1 + (n > 9) + (n > 99);
501 }
502
503 if (len > 60) {
504 if (inString) {
505 O << "'";
506 inString = false;
507 }
508 O << "\n";
509 len = 0;
510 }
511 }
512
513 if (len > 0) {
514 if (inString)
515 O << "'";
516 O << "\n";
517 }
518 }
519}
520
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000521// Include the auto-generated portion of the assembly writer.
522#include "X86GenAsmWriter1.inc"