blob: fa3529575025d1e9d4d57f1343449e5fd0d6f1c3 [file] [log] [blame]
Chris Lattnerb9740462005-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 Lattner1ef9cd42006-12-19 22:59:26 +000016#define DEBUG_TYPE "asm-printer"
Chris Lattnerb9740462005-07-01 22:44:09 +000017#include "X86IntelAsmPrinter.h"
Jim Laskey261779b2006-09-07 22:06:40 +000018#include "X86TargetAsmInfo.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000019#include "X86.h"
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +000020#include "llvm/CallingConv.h"
Jeff Cohen24a62a92006-05-02 01:16:28 +000021#include "llvm/Constants.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000022#include "llvm/Module.h"
23#include "llvm/Assembly/Writer.h"
24#include "llvm/Support/Mangler.h"
Jim Laskey261779b2006-09-07 22:06:40 +000025#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng5588de92006-02-18 00:15:05 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattner1ef9cd42006-12-19 22:59:26 +000027#include "llvm/ADT/Statistic.h"
Chris Lattnerb9740462005-07-01 22:44:09 +000028using namespace llvm;
Chris Lattnerb9740462005-07-01 22:44:09 +000029
Chris Lattner1ef9cd42006-12-19 22:59:26 +000030STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
Chris Lattnerb82247b2006-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 Lattnerb9740462005-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 Lattner99946fb2005-11-21 07:51:23 +000041 SetupMachineFunction(MF);
Chris Lattnerb9740462005-07-01 22:44:09 +000042 O << "\n\n";
43
44 // Print out constants referenced by the function
Chris Lattner8a5f3c12005-11-21 08:32:23 +000045 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb9740462005-07-01 22:44:09 +000046
47 // Print out labels for the function.
Chris Lattner104aa5d2006-09-26 03:57:53 +000048 const Function *F = MF.getFunction();
Anton Korobeynikov3c5b3df2006-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 Lattner104aa5d2006-09-26 03:57:53 +000053 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
54 FunctionInfoMap[F] = *MF.getInfo<X86FunctionInfo>();
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +000055
56 X86SharedAsmPrinter::decorateName(CurrentFnName, F);
57
Chris Lattnerb82247b2006-10-05 02:43:52 +000058 SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
59
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000060 switch (F->getLinkage()) {
61 default: assert(0 && "Unsupported linkage type!");
62 case Function::InternalLinkage:
Anton Korobeynikovd61d39e2006-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 Cohen24a62a92006-05-02 01:16:28 +000069 O << "\tpublic " << CurrentFnName << "\n";
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +000070 EmitAlignment(4);
71 break;
72 }
73
Jeff Cohen24a62a92006-05-02 01:16:28 +000074 O << CurrentFnName << "\tproc near\n";
Jim Laskeyc0d65182006-04-07 20:44:42 +000075
Chris Lattnerb9740462005-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 Begemanb9d4f832006-05-02 05:37:32 +000080 if (I->pred_begin() != I->pred_end()) {
81 printBasicBlockLabel(I, true);
82 O << '\n';
83 }
Chris Lattnerb9740462005-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 Lattnera6a570e2006-10-05 03:01:21 +000092 // Print out jump tables referenced by the function.
93 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
94
Jeff Cohen24a62a92006-05-02 01:16:28 +000095 O << CurrentFnName << "\tendp\n";
96
Chris Lattnerb9740462005-07-01 22:44:09 +000097 // We didn't modify anything.
98 return false;
99}
100
Nate Begeman6f8c1ac2005-11-30 18:54:35 +0000101void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Nate Begeman0f38dc42005-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 Lattnerd62a3bf2006-02-06 23:41:19 +0000116void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
117 const char *Modifier) {
Chris Lattnerb9740462005-07-01 22:44:09 +0000118 const MRegisterInfo &RI = *TM.getRegisterInfo();
119 switch (MO.getType()) {
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000120 case MachineOperand::MO_Register: {
Evan Chengddb6cc12006-05-05 05:40:20 +0000121 if (MRegisterInfo::isPhysicalRegister(MO.getReg())) {
122 unsigned Reg = MO.getReg();
Evan Chengcfaffdd2006-05-31 22:34:26 +0000123 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng11b0a5d2006-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 Chengddb6cc12006-05-05 05:40:20 +0000127 Reg = getX86SubSuperRegister(Reg, VT);
128 }
129 O << RI.get(Reg).Name;
130 } else
Chris Lattner563f0412006-05-01 05:53:50 +0000131 O << "reg" << MO.getReg();
Chris Lattnerb9740462005-07-01 22:44:09 +0000132 return;
Anton Korobeynikov44ef9342006-12-19 21:04:20 +0000133 }
Chris Lattnerfef7a2d2006-05-04 17:21:20 +0000134 case MachineOperand::MO_Immediate:
Evan Cheng70145f22006-05-26 08:04:31 +0000135 O << MO.getImmedValue();
Chris Lattnerb9740462005-07-01 22:44:09 +0000136 return;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000137 case MachineOperand::MO_MachineBasicBlock:
138 printBasicBlockLabel(MO.getMachineBasicBlock());
Chris Lattnerb9740462005-07-01 22:44:09 +0000139 return;
Anton Korobeynikov44ef9342006-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 Cheng75b87832006-02-26 08:28:12 +0000147 case MachineOperand::MO_ConstantPoolIndex: {
148 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
149 if (!isMemOp) O << "OFFSET ";
Jim Laskeya6211dc2006-09-06 18:34:40 +0000150 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
151 << getFunctionNumber() << "_" << MO.getConstantPoolIndex();
Evan Cheng75b87832006-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 Lattnerb9740462005-07-01 22:44:09 +0000160 case MachineOperand::MO_GlobalAddress: {
Evan Cheng5588de92006-02-18 00:15:05 +0000161 bool isCallOp = Modifier && !strcmp(Modifier, "call");
162 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000163 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000164 std::string Name = Mang->getValueName(GV);
165
166 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000167
Evan Cheng5588de92006-02-18 00:15:05 +0000168 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovd61d39e2006-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 Korobeynikov3c5b3df2006-09-20 22:03:51 +0000174 O << Name;
Chris Lattnerb9740462005-07-01 22:44:09 +0000175 int Offset = MO.getOffset();
176 if (Offset > 0)
177 O << " + " << Offset;
178 else if (Offset < 0)
Evan Chengd2cb7052005-11-30 01:59:00 +0000179 O << Offset;
Chris Lattnerb9740462005-07-01 22:44:09 +0000180 return;
181 }
Evan Cheng5588de92006-02-18 00:15:05 +0000182 case MachineOperand::MO_ExternalSymbol: {
183 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng73136df2006-02-22 20:19:42 +0000184 if (!isCallOp) O << "OFFSET ";
Jim Laskeya6211dc2006-09-06 18:34:40 +0000185 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb9740462005-07-01 22:44:09 +0000186 return;
Evan Cheng5588de92006-02-18 00:15:05 +0000187 }
Chris Lattnerb9740462005-07-01 22:44:09 +0000188 default:
189 O << "<unknown operand type>"; return;
190 }
191}
192
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000193void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
194 const char *Modifier) {
Chris Lattnerb9740462005-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
202 if (BaseReg.isFrameIndex()) {
203 O << "[frame slot #" << BaseReg.getFrameIndex();
204 if (DispSpec.getImmedValue())
205 O << " + " << DispSpec.getImmedValue();
206 O << "]";
207 return;
Chris Lattnerb9740462005-07-01 22:44:09 +0000208 }
209
210 O << "[";
211 bool NeedPlus = false;
212 if (BaseReg.getReg()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000213 printOp(BaseReg, Modifier);
Chris Lattnerb9740462005-07-01 22:44:09 +0000214 NeedPlus = true;
215 }
216
217 if (IndexReg.getReg()) {
218 if (NeedPlus) O << " + ";
219 if (ScaleVal != 1)
220 O << ScaleVal << "*";
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000221 printOp(IndexReg, Modifier);
Chris Lattnerb9740462005-07-01 22:44:09 +0000222 NeedPlus = true;
223 }
224
Chris Lattnera7f95902006-12-19 19:29:58 +0000225 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
226 DispSpec.isJumpTableIndex()) {
Chris Lattnerb9740462005-07-01 22:44:09 +0000227 if (NeedPlus)
228 O << " + ";
Evan Cheng5a766802006-02-07 08:38:37 +0000229 printOp(DispSpec, "mem");
Chris Lattnerb9740462005-07-01 22:44:09 +0000230 } else {
231 int DispVal = DispSpec.getImmedValue();
232 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
233 if (NeedPlus)
234 if (DispVal > 0)
235 O << " + ";
236 else {
237 O << " - ";
238 DispVal = -DispVal;
239 }
240 O << DispVal;
241 }
242 }
243 O << "]";
244}
245
Evan Cheng5588de92006-02-18 00:15:05 +0000246void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
247 O << "\"L" << getFunctionNumber() << "$pb\"\n";
248 O << "\"L" << getFunctionNumber() << "$pb\":";
249}
Chris Lattnerb9740462005-07-01 22:44:09 +0000250
Evan Chengd3696032006-04-28 23:19:39 +0000251bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Chengb244b802006-04-28 23:11:40 +0000252 const char Mode) {
253 const MRegisterInfo &RI = *TM.getRegisterInfo();
254 unsigned Reg = MO.getReg();
Evan Chengb244b802006-04-28 23:11:40 +0000255 switch (Mode) {
256 default: return true; // Unknown mode.
257 case 'b': // Print QImode register
Evan Chengddb6cc12006-05-05 05:40:20 +0000258 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Chengb244b802006-04-28 23:11:40 +0000259 break;
260 case 'h': // Print QImode high register
Evan Chengddb6cc12006-05-05 05:40:20 +0000261 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Chengb244b802006-04-28 23:11:40 +0000262 break;
263 case 'w': // Print HImode register
Evan Chengddb6cc12006-05-05 05:40:20 +0000264 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Chengb244b802006-04-28 23:11:40 +0000265 break;
266 case 'k': // Print SImode register
Evan Chengddb6cc12006-05-05 05:40:20 +0000267 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Chengb244b802006-04-28 23:11:40 +0000268 break;
269 }
270
Evan Chengddb6cc12006-05-05 05:40:20 +0000271 O << '%' << RI.get(Reg).Name;
Evan Chengb244b802006-04-28 23:11:40 +0000272 return false;
273}
274
Evan Cheng68a44dc2006-04-28 21:19:05 +0000275/// PrintAsmOperand - Print out an operand for an inline asm expression.
276///
277bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
278 unsigned AsmVariant,
279 const char *ExtraCode) {
280 // Does this asm operand have a single letter operand modifier?
281 if (ExtraCode && ExtraCode[0]) {
282 if (ExtraCode[1] != 0) return true; // Unknown modifier.
283
284 switch (ExtraCode[0]) {
285 default: return true; // Unknown modifier.
Evan Chengb244b802006-04-28 23:11:40 +0000286 case 'b': // Print QImode register
287 case 'h': // Print QImode high register
288 case 'w': // Print HImode register
289 case 'k': // Print SImode register
Evan Chengd3696032006-04-28 23:19:39 +0000290 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng68a44dc2006-04-28 21:19:05 +0000291 }
292 }
293
294 printOperand(MI, OpNo);
295 return false;
296}
297
298bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
299 unsigned OpNo,
300 unsigned AsmVariant,
301 const char *ExtraCode) {
302 if (ExtraCode && ExtraCode[0])
303 return true; // Unknown modifier.
304 printMemReference(MI, OpNo);
305 return false;
306}
307
Chris Lattnerb9740462005-07-01 22:44:09 +0000308/// printMachineInstruction -- Print out a single X86 LLVM instruction
309/// MI in Intel syntax to the current output stream.
310///
311void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
312 ++EmittedInsts;
313
Evan Chengddb6cc12006-05-05 05:40:20 +0000314 // See if a truncate instruction can be turned into a nop.
315 switch (MI->getOpcode()) {
316 default: break;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000317 case X86::TRUNC_64to32:
318 case X86::TRUNC_64to16:
319 case X86::TRUNC_32to16:
320 case X86::TRUNC_32to8:
321 case X86::TRUNC_16to8:
322 case X86::TRUNC_32_to8:
323 case X86::TRUNC_16_to8: {
Evan Chengddb6cc12006-05-05 05:40:20 +0000324 const MachineOperand &MO0 = MI->getOperand(0);
325 const MachineOperand &MO1 = MI->getOperand(1);
326 unsigned Reg0 = MO0.getReg();
327 unsigned Reg1 = MO1.getReg();
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000328 unsigned Opc = MI->getOpcode();
329 if (Opc == X86::TRUNC_64to32)
330 Reg1 = getX86SubSuperRegister(Reg1, MVT::i32);
331 else if (Opc == X86::TRUNC_32to16 || Opc == X86::TRUNC_64to16)
Evan Cheng9733bde2006-05-08 08:01:26 +0000332 Reg1 = getX86SubSuperRegister(Reg1, MVT::i16);
Evan Chengddb6cc12006-05-05 05:40:20 +0000333 else
Evan Cheng9733bde2006-05-08 08:01:26 +0000334 Reg1 = getX86SubSuperRegister(Reg1, MVT::i8);
Jim Laskeya6211dc2006-09-06 18:34:40 +0000335 O << TAI->getCommentString() << " TRUNCATE ";
Evan Cheng9733bde2006-05-08 08:01:26 +0000336 if (Reg0 != Reg1)
337 O << "\n\t";
Evan Chengddb6cc12006-05-05 05:40:20 +0000338 break;
339 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000340 case X86::PsMOVZX64rr32:
341 O << TAI->getCommentString() << " ZERO-EXTEND " << "\n\t";
342 break;
Evan Chengddb6cc12006-05-05 05:40:20 +0000343 }
344
Chris Lattnerb9740462005-07-01 22:44:09 +0000345 // Call the autogenerated instruction printer routines.
346 printInstruction(MI);
347}
348
349bool X86IntelAsmPrinter::doInitialization(Module &M) {
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000350 X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerd0201942006-05-09 05:12:53 +0000351
Jim Laskeya6211dc2006-09-06 18:34:40 +0000352 Mang->markCharUnacceptable('.');
Jeff Cohen06041ab2006-05-04 16:20:22 +0000353
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000354 O << "\t.686\n\t.model flat\n\n";
355
356 // Emit declarations for external functions.
357 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000358 if (I->isExternal()) {
359 std::string Name = Mang->getValueName(I);
360 X86SharedAsmPrinter::decorateName(Name, I);
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000361
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000362 O << "\textern " ;
363 if (I->hasDLLImportLinkage()) {
364 O << "__imp_";
365 }
366 O << Name << ":near\n";
367 }
368
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000369 // Emit declarations for external globals. Note that VC++ always declares
370 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000371 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
372 I != E; ++I) {
Anton Korobeynikov3c5b3df2006-09-20 22:03:51 +0000373 if (I->isExternal()) {
374 std::string Name = Mang->getValueName(I);
375
376 O << "\textern " ;
377 if (I->hasDLLImportLinkage()) {
378 O << "__imp_";
379 }
380 O << Name << ":byte\n";
381 }
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000382 }
383
Chris Lattnerb9740462005-07-01 22:44:09 +0000384 return false;
385}
386
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000387bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000388 const TargetData *TD = TM.getTargetData();
389
390 // Print out module-level global variables here.
391 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
392 I != E; ++I) {
393 if (I->isExternal()) continue; // External global require no code
394
395 // Check to see if this is a special global used by LLVM, if so, emit it.
396 if (EmitSpecialLLVMGlobal(I))
397 continue;
398
399 std::string name = Mang->getValueName(I);
400 Constant *C = I->getInitializer();
Devang Patel71b99292006-10-24 20:32:14 +0000401 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000402 bool bCustomSegment = false;
403
404 switch (I->getLinkage()) {
405 case GlobalValue::LinkOnceLinkage:
406 case GlobalValue::WeakLinkage:
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000407 SwitchToDataSection("");
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000408 O << name << "?\tsegment common 'COMMON'\n";
409 bCustomSegment = true;
410 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
411 // are also available.
412 break;
413 case GlobalValue::AppendingLinkage:
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000414 SwitchToDataSection("");
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000415 O << name << "?\tsegment public 'DATA'\n";
416 bCustomSegment = true;
417 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
418 // are also available.
419 break;
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000420 case GlobalValue::DLLExportLinkage:
421 DLLExportedGVs.insert(name);
422 // FALL THROUGH
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000423 case GlobalValue::ExternalLinkage:
424 O << "\tpublic " << name << "\n";
425 // FALL THROUGH
426 case GlobalValue::InternalLinkage:
Jim Laskeya6211dc2006-09-06 18:34:40 +0000427 SwitchToDataSection(TAI->getDataSection(), I);
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000428 break;
429 default:
430 assert(0 && "Unknown linkage type!");
431 }
432
433 if (!bCustomSegment)
434 EmitAlignment(Align, I);
435
Jim Laskeya6211dc2006-09-06 18:34:40 +0000436 O << name << ":\t\t\t\t" << TAI->getCommentString()
437 << " " << I->getName() << '\n';
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000438
439 EmitGlobalConstant(C);
440
441 if (bCustomSegment)
442 O << name << "?\tends\n";
443 }
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000444
445 // Output linker support code for dllexported globals
446 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
447 (DLLExportedFns.begin() != DLLExportedFns.end())) {
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000448 SwitchToDataSection("");
Anton Korobeynikovd61d39e2006-09-14 18:23:27 +0000449 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
450 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
451 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
452 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
453 O << "_drectve\t segment info alias('.drectve')\n";
454 }
455
456 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
457 e = DLLExportedGVs.end();
458 i != e; ++i) {
459 O << "\t db ' /EXPORT:" << *i << ",data'\n";
460 }
461
462 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
463 e = DLLExportedFns.end();
464 i != e; ++i) {
465 O << "\t db ' /EXPORT:" << *i << "'\n";
466 }
467
468 if ((DLLExportedGVs.begin() != DLLExportedGVs.end()) ||
469 (DLLExportedFns.begin() != DLLExportedFns.end())) {
470 O << "_drectve\t ends\n";
471 }
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000472
473 // Bypass X86SharedAsmPrinter::doFinalization().
474 AsmPrinter::doFinalization(M);
Anton Korobeynikovaa4c0f92006-10-31 08:31:24 +0000475 SwitchToDataSection("");
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000476 O << "\tend\n";
Jeff Cohence9b9fe2006-05-06 21:27:14 +0000477 return false; // success
Jeff Cohenbfe9ffb2006-05-02 03:11:50 +0000478}
479
Jeff Cohen24a62a92006-05-02 01:16:28 +0000480void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
481 unsigned NumElts = CVA->getNumOperands();
482 if (NumElts) {
483 // ML does not have escape sequences except '' for '. It also has a maximum
484 // string length of 255.
485 unsigned len = 0;
486 bool inString = false;
487 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencere0fc4df2006-10-20 07:07:24 +0000488 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohen24a62a92006-05-02 01:16:28 +0000489 if (len == 0)
490 O << "\tdb ";
491
492 if (n >= 32 && n <= 127) {
493 if (!inString) {
494 if (len > 0) {
495 O << ",'";
496 len += 2;
497 } else {
498 O << "'";
499 len++;
500 }
501 inString = true;
502 }
503 if (n == '\'') {
504 O << "'";
505 len++;
506 }
507 O << char(n);
508 } else {
509 if (inString) {
510 O << "'";
511 len++;
512 inString = false;
513 }
514 if (len > 0) {
515 O << ",";
516 len++;
517 }
518 O << n;
519 len += 1 + (n > 9) + (n > 99);
520 }
521
522 if (len > 60) {
523 if (inString) {
524 O << "'";
525 inString = false;
526 }
527 O << "\n";
528 len = 0;
529 }
530 }
531
532 if (len > 0) {
533 if (inString)
534 O << "'";
535 O << "\n";
536 }
537 }
538}
539
Chris Lattnerb9740462005-07-01 22:44:09 +0000540// Include the auto-generated portion of the assembly writer.
541#include "X86GenAsmWriter1.inc"