blob: 72ba1b098bc283cf397ec17ab432fc7e9350e3ac [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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000017#include "X86IntelAsmPrinter.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000018#include "X86TargetAsmInfo.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000019#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000020#include "llvm/CallingConv.h"
Jeff Cohenc884db42006-05-02 01:16:28 +000021#include "llvm/Constants.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
24#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner95b2c7d2006-12-19 22:59:26 +000027#include "llvm/ADT/Statistic.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000028using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000029
Chris Lattner95b2c7d2006-12-19 22:59:26 +000030STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
Chris Lattnerafbfded2006-10-05 02:43:52 +000032std::string X86IntelAsmPrinter::getSectionForFunction(const Function &F) const {
33 // Intel asm always emits functions to _text.
34 return "_text";
35}
36
Chris Lattnerb36cbd02005-07-01 22:44:09 +000037/// runOnMachineFunction - This uses the printMachineInstruction()
38/// method to print assembly for each instruction.
39///
40bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +000041 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +000042 O << "\n\n";
43
44 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +000045 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +000046
47 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +000048 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000049 unsigned CC = F->getCallingConv();
50
51 // Populate function information map. Actually, We don't want to populate
52 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +000053 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +000054 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +000055
56 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
57
Chris Lattnerafbfded2006-10-05 02:43:52 +000058 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
59
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000060 switch (F->getLinkage()) {
61 default: assert(0 && "Unsupported linkage type!");
62 case Function::InternalLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000063 EmitAlignment(4);
64 break;
65 case Function::DLLExportLinkage:
66 DLLExportedFns.insert(CurrentFnName);
67 //FALLS THROUGH
68 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +000069 O << "\tpublic " << CurrentFnName << "\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000070 EmitAlignment(4);
71 break;
72 }
73
Jeff Cohenc884db42006-05-02 01:16:28 +000074 O << CurrentFnName << "\tproc near\n";
Jim Laskey6b92b8e2006-04-07 20:44:42 +000075
Chris Lattnerb36cbd02005-07-01 22:44:09 +000076 // Print out code for the function.
77 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
78 I != E; ++I) {
79 // Print a label for the basic block if there are any predecessors.
Nate Begemancdf38c42006-05-02 05:37:32 +000080 if (I->pred_begin() != I->pred_end()) {
81 printBasicBlockLabel(I, true);
82 O << '\n';
83 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000084 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
85 II != E; ++II) {
86 // Print the assembly for the instruction.
87 O << "\t";
88 printMachineInstruction(II);
89 }
90 }
91
Chris Lattner1da31ee2006-10-05 03:01:21 +000092 // Print out jump tables referenced by the function.
93 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
94
Jeff Cohenc884db42006-05-02 01:16:28 +000095 O << CurrentFnName << "\tendp\n";
96
Chris Lattnerb36cbd02005-07-01 22:44:09 +000097 // We didn't modify anything.
98 return false;
99}
100
Nate Begeman391c5d22005-11-30 18:54:35 +0000101void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman6c7cb292005-07-14 22:52:25 +0000102 unsigned char value = MI->getOperand(Op).getImmedValue();
103 assert(value <= 7 && "Invalid ssecc argument!");
104 switch (value) {
105 case 0: O << "eq"; break;
106 case 1: O << "lt"; break;
107 case 2: O << "le"; break;
108 case 3: O << "unord"; break;
109 case 4: O << "neq"; break;
110 case 5: O << "nlt"; break;
111 case 6: O << "nle"; break;
112 case 7: O << "ord"; break;
113 }
114}
115
Chris Lattnera3b8c572006-02-06 23:41:19 +0000116void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
117 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000118 const MRegisterInfo &RI = *TM.getRegisterInfo();
119 switch (MO.getType()) {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000120 case MachineOperand::MO_Register: {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000121 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
122 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000123 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000124 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
125 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
126 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000127 Reg = getX86SubSuperRegister(Reg, VT);
128 }
129 O << RI.get(Reg).Name;
130 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000131 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000132 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000133 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000134 case MachineOperand::MO_Immediate:
Evan Cheng138a24e2006-05-26 08:04:31 +0000135 O << MO.getImmedValue();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000136 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000137 case MachineOperand::MO_MachineBasicBlock:
138 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000139 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000140 case MachineOperand::MO_JumpTableIndex: {
141 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
142 if (!isMemOp) O << "OFFSET ";
143 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
144 << "_" << MO.getJumpTableIndex();
145 return;
146 }
Evan Chenga09bd812006-02-26 08:28:12 +0000147 case MachineOperand::MO_ConstantPoolIndex: {
148 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
149 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000150 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
151 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
Evan Chenga09bd812006-02-26 08:28:12 +0000152 int Offset = MO.getOffset();
153 if (Offset > 0)
154 O << " + " << Offset;
155 else if (Offset < 0)
156 O << Offset;
157 O << "]";
158 return;
159 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000160 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000161 bool isCallOp = Modifier && !strcmp(Modifier, "call");
162 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000163 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000164 std::string Name = Mang->getValueName(GV);
165
166 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167
Evan Cheng7ccced62006-02-18 00:15:05 +0000168 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000169 if (GV->hasDLLImportLinkage()) {
170 // FIXME: This should be fixed with full support of stdcall & fastcall
171 // CC's
172 O << "__imp_";
173 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000174 O << Name;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175 int Offset = MO.getOffset();
176 if (Offset > 0)
177 O << " + " << Offset;
178 else if (Offset < 0)
Evan Cheng345c3f32005-11-30 01:59:00 +0000179 O << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000180 return;
181 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000182 case MachineOperand::MO_ExternalSymbol: {
183 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000184 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000185 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000187 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000188 default:
189 O << "<unknown operand type>"; return;
190 }
191}
192
Evan Cheng25ab6902006-09-08 06:48:29 +0000193void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
194 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000195 assert(isMem(MI, Op) && "Invalid memory reference!");
196
197 const MachineOperand &BaseReg = MI->getOperand(Op);
198 int ScaleVal = MI->getOperand(Op+1).getImmedValue();
199 const MachineOperand &IndexReg = MI->getOperand(Op+2);
200 const MachineOperand &DispSpec = MI->getOperand(Op+3);
201
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000202 O << "[";
203 bool NeedPlus = false;
204 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000205 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206 NeedPlus = true;
207 }
208
209 if (IndexReg.getReg()) {
210 if (NeedPlus) O << " + ";
211 if (ScaleVal != 1)
212 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000214 NeedPlus = true;
215 }
216
Chris Lattner0bb3af92006-12-19 19:29:58 +0000217 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
218 DispSpec.isJumpTableIndex()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000219 if (NeedPlus)
220 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000221 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 } else {
223 int DispVal = DispSpec.getImmedValue();
224 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
225 if (NeedPlus)
226 if (DispVal > 0)
227 O << " + ";
228 else {
229 O << " - ";
230 DispVal = -DispVal;
231 }
232 O << DispVal;
233 }
234 }
235 O << "]";
236}
237
Evan Cheng7ccced62006-02-18 00:15:05 +0000238void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
239 O << "\"L" << getFunctionNumber() << "$pb\"\n";
240 O << "\"L" << getFunctionNumber() << "$pb\":";
241}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000242
Evan Cheng55c25f22006-04-28 23:19:39 +0000243bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000244 const char Mode) {
245 const MRegisterInfo &RI = *TM.getRegisterInfo();
246 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000247 switch (Mode) {
248 default: return true; // Unknown mode.
249 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000250 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000251 break;
252 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000253 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000254 break;
255 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000256 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000257 break;
258 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000259 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000260 break;
261 }
262
Evan Cheng8f7f7122006-05-05 05:40:20 +0000263 O << '%' << RI.get(Reg).Name;
Evan Cheng62f27002006-04-28 23:11:40 +0000264 return false;
265}
266
Evan Cheng3d48a902006-04-28 21:19:05 +0000267/// PrintAsmOperand - Print out an operand for an inline asm expression.
268///
269bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
270 unsigned AsmVariant,
271 const char *ExtraCode) {
272 // Does this asm operand have a single letter operand modifier?
273 if (ExtraCode && ExtraCode[0]) {
274 if (ExtraCode[1] != 0) return true; // Unknown modifier.
275
276 switch (ExtraCode[0]) {
277 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000278 case 'b': // Print QImode register
279 case 'h': // Print QImode high register
280 case 'w': // Print HImode register
281 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000282 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000283 }
284 }
285
286 printOperand(MI, OpNo);
287 return false;
288}
289
290bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
291 unsigned OpNo,
292 unsigned AsmVariant,
293 const char *ExtraCode) {
294 if (ExtraCode && ExtraCode[0])
295 return true; // Unknown modifier.
296 printMemReference(MI, OpNo);
297 return false;
298}
299
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000300/// printMachineInstruction -- Print out a single X86 LLVM instruction
301/// MI in Intel syntax to the current output stream.
302///
303void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
304 ++EmittedInsts;
305
Evan Cheng8f7f7122006-05-05 05:40:20 +0000306 // See if a truncate instruction can be turned into a nop.
307 switch (MI->getOpcode()) {
308 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000309 case X86::PsMOVZX64rr32:
310 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
311 break;
Evan Cheng8f7f7122006-05-05 05:40:20 +0000312 }
313
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000314 // Call the autogenerated instruction printer routines.
315 printInstruction(MI);
316}
317
318bool X86IntelAsmPrinter::doInitialization(Module &M) {
Dan Gohmanb8275a32007-07-25 19:33:14 +0000319 bool Result = X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000320
Jim Laskey563321a2006-09-06 18:34:40 +0000321 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000322
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000323 O << "\t.686\n\t.model flat\n\n";
324
325 // Emit declarations for external functions.
326 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000327 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000328 std::string Name = Mang->getValueName(I);
329 X86SharedAsmPrinter::decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000330
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000331 O << "\textern " ;
332 if (I->hasDLLImportLinkage()) {
333 O << "__imp_";
334 }
335 O << Name << ":near\n";
336 }
337
Jeff Cohend43b18d2006-05-06 21:27:14 +0000338 // Emit declarations for external globals. Note that VC++ always declares
339 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000340 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
341 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000342 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000343 std::string Name = Mang->getValueName(I);
344
345 O << "\textern " ;
346 if (I->hasDLLImportLinkage()) {
347 O << "__imp_";
348 }
349 O << Name << ":byte\n";
350 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000351 }
352
Dan Gohmanb8275a32007-07-25 19:33:14 +0000353 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000354}
355
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000356bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000357 const TargetData *TD = TM.getTargetData();
358
359 // Print out module-level global variables here.
360 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
361 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000362 if (I->isDeclaration()) continue; // External global require no code
Jeff Cohend43b18d2006-05-06 21:27:14 +0000363
364 // Check to see if this is a special global used by LLVM, if so, emit it.
365 if (EmitSpecialLLVMGlobal(I))
366 continue;
367
368 std::string name = Mang->getValueName(I);
369 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000370 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000371 bool bCustomSegment = false;
372
373 switch (I->getLinkage()) {
374 case GlobalValue::LinkOnceLinkage:
375 case GlobalValue::WeakLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000376 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000377 O << name << "?\tsegment common 'COMMON'\n";
378 bCustomSegment = true;
379 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
380 // are also available.
381 break;
382 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000383 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000384 O << name << "?\tsegment public 'DATA'\n";
385 bCustomSegment = true;
386 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
387 // are also available.
388 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000389 case GlobalValue::DLLExportLinkage:
390 DLLExportedGVs.insert(name);
391 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000392 case GlobalValue::ExternalLinkage:
393 O << "\tpublic " << name << "\n";
394 // FALL THROUGH
395 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000396 SwitchToDataSection(TAI->getDataSection(), I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000397 break;
398 default:
399 assert(0 && "Unknown linkage type!");
400 }
401
402 if (!bCustomSegment)
403 EmitAlignment(Align, I);
404
Jim Laskey563321a2006-09-06 18:34:40 +0000405 O << name << ":\t\t\t\t" << TAI->getCommentString()
406 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000407
408 EmitGlobalConstant(C);
409
410 if (bCustomSegment)
411 O << name << "?\tends\n";
412 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000413
414 // Output linker support code for dllexported globals
415 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
416 (DLLExportedFns.begin() != DLLExportedFns.end())) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000417 SwitchToDataSection("");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000418 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
419 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
420 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
421 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
422 O << "_drectve\t segment info alias('.drectve')\n";
423 }
424
425 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
426 e = DLLExportedGVs.end();
427 i != e; ++i) {
428 O << "\t db ' /EXPORT:" << *i << ",data'\n";
429 }
430
431 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
432 e = DLLExportedFns.end();
433 i != e; ++i) {
434 O << "\t db ' /EXPORT:" << *i << "'\n";
435 }
436
437 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
438 (DLLExportedFns.begin() != DLLExportedFns.end())) {
439 O << "_drectve\t ends\n";
440 }
Jeff Cohend43b18d2006-05-06 21:27:14 +0000441
442 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000443 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000444 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000445 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000446 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000447}
448
Jeff Cohenc884db42006-05-02 01:16:28 +0000449void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
450 unsigned NumElts = CVA->getNumOperands();
451 if (NumElts) {
452 // ML does not have escape sequences except '' for '. It also has a maximum
453 // string length of 255.
454 unsigned len = 0;
455 bool inString = false;
456 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000457 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000458 if (len == 0)
459 O << "\tdb ";
460
461 if (n >= 32 && n <= 127) {
462 if (!inString) {
463 if (len > 0) {
464 O << ",'";
465 len += 2;
466 } else {
467 O << "'";
468 len++;
469 }
470 inString = true;
471 }
472 if (n == '\'') {
473 O << "'";
474 len++;
475 }
476 O << char(n);
477 } else {
478 if (inString) {
479 O << "'";
480 len++;
481 inString = false;
482 }
483 if (len > 0) {
484 O << ",";
485 len++;
486 }
487 O << n;
488 len += 1 + (n > 9) + (n > 99);
489 }
490
491 if (len > 60) {
492 if (inString) {
493 O << "'";
494 inString = false;
495 }
496 O << "\n";
497 len = 0;
498 }
499 }
500
501 if (len > 0) {
502 if (inString)
503 O << "'";
504 O << "\n";
505 }
506 }
507}
508
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000509// Include the auto-generated portion of the assembly writer.
510#include "X86GenAsmWriter1.inc"