blob: 0caab87dd030e8ee29af90cf32b87dc2f774b85a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerb36cbd02005-07-01 22:44:09 +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
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.
Dan Gohmancb406c22007-10-03 19:26:29 +000080 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +000081 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +000082 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.
Chris Lattnerb36cbd02005-07-01 22:44:09 +000087 printMachineInstruction(II);
88 }
89 }
90
Chris Lattner1da31ee2006-10-05 03:01:21 +000091 // Print out jump tables referenced by the function.
92 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
93
Jeff Cohenc884db42006-05-02 01:16:28 +000094 O << CurrentFnName << "\tendp\n";
95
Chris Lattnerb36cbd02005-07-01 22:44:09 +000096 // We didn't modify anything.
97 return false;
98}
99
Nate Begeman391c5d22005-11-30 18:54:35 +0000100void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000101 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000102 assert(value <= 7 && "Invalid ssecc argument!");
103 switch (value) {
104 case 0: O << "eq"; break;
105 case 1: O << "lt"; break;
106 case 2: O << "le"; break;
107 case 3: O << "unord"; break;
108 case 4: O << "neq"; break;
109 case 5: O << "nlt"; break;
110 case 6: O << "nle"; break;
111 case 7: O << "ord"; break;
112 }
113}
114
Chris Lattnera3b8c572006-02-06 23:41:19 +0000115void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
116 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000117 switch (MO.getType()) {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000118 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000119 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000120 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000121 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000122 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
123 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
124 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000125 Reg = getX86SubSuperRegister(Reg, VT);
126 }
Evan Chengda47e6e2008-03-15 00:03:38 +0000127 O << TRI->getAsmName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000128 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000129 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000130 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000131 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000132 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000133 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000134 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000135 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000136 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000137 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000138 case MachineOperand::MO_JumpTableIndex: {
139 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
140 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000141 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000142 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000143 return;
144 }
Evan Chenga09bd812006-02-26 08:28:12 +0000145 case MachineOperand::MO_ConstantPoolIndex: {
146 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
147 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000148 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000149 << getFunctionNumber() << "_" << MO.getIndex();
Evan Chenga09bd812006-02-26 08:28:12 +0000150 int Offset = MO.getOffset();
151 if (Offset > 0)
152 O << " + " << Offset;
153 else if (Offset < 0)
154 O << Offset;
155 O << "]";
156 return;
157 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000158 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000159 bool isCallOp = Modifier && !strcmp(Modifier, "call");
160 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000161 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000162 std::string Name = Mang->getValueName(GV);
163
164 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000165
Evan Cheng7ccced62006-02-18 00:15:05 +0000166 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000167 if (GV->hasDLLImportLinkage()) {
168 // FIXME: This should be fixed with full support of stdcall & fastcall
169 // CC's
170 O << "__imp_";
171 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000172 O << Name;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000173 int Offset = MO.getOffset();
174 if (Offset > 0)
175 O << " + " << Offset;
176 else if (Offset < 0)
Evan Cheng345c3f32005-11-30 01:59:00 +0000177 O << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000178 return;
179 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000180 case MachineOperand::MO_ExternalSymbol: {
181 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000182 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000183 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000184 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000185 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186 default:
187 O << "<unknown operand type>"; return;
188 }
189}
190
Evan Cheng25ab6902006-09-08 06:48:29 +0000191void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
192 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000193 assert(isMem(MI, Op) && "Invalid memory reference!");
194
195 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000196 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000197 const MachineOperand &IndexReg = MI->getOperand(Op+2);
198 const MachineOperand &DispSpec = MI->getOperand(Op+3);
199
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000200 O << "[";
201 bool NeedPlus = false;
202 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000203 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000204 NeedPlus = true;
205 }
206
207 if (IndexReg.getReg()) {
208 if (NeedPlus) O << " + ";
209 if (ScaleVal != 1)
210 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000212 NeedPlus = true;
213 }
214
Chris Lattner0bb3af92006-12-19 19:29:58 +0000215 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
216 DispSpec.isJumpTableIndex()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000217 if (NeedPlus)
218 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000219 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000221 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000223 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000224 if (DispVal > 0)
225 O << " + ";
226 else {
227 O << " - ";
228 DispVal = -DispVal;
229 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000230 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000231 O << DispVal;
232 }
233 }
234 O << "]";
235}
236
Evan Chengcc415862007-11-09 01:32:10 +0000237void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
238 const MachineBasicBlock *MBB) const {
239 if (!TAI->getSetDirective())
240 return;
241
242 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
243 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000244 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000245 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
246}
247
Evan Cheng7ccced62006-02-18 00:15:05 +0000248void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000249 O << "\"L" << getFunctionNumber() << "$pb\"\n";
250 O << "\"L" << getFunctionNumber() << "$pb\":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000251}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000252
Evan Cheng55c25f22006-04-28 23:19:39 +0000253bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000254 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000255 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000256 switch (Mode) {
257 default: return true; // Unknown mode.
258 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000259 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000260 break;
261 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000262 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000263 break;
264 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000265 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000266 break;
267 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000268 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000269 break;
270 }
271
Evan Chengda47e6e2008-03-15 00:03:38 +0000272 O << '%' << TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000273 return false;
274}
275
Evan Cheng3d48a902006-04-28 21:19:05 +0000276/// PrintAsmOperand - Print out an operand for an inline asm expression.
277///
278bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279 unsigned AsmVariant,
280 const char *ExtraCode) {
281 // Does this asm operand have a single letter operand modifier?
282 if (ExtraCode && ExtraCode[0]) {
283 if (ExtraCode[1] != 0) return true; // Unknown modifier.
284
285 switch (ExtraCode[0]) {
286 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000287 case 'b': // Print QImode register
288 case 'h': // Print QImode high register
289 case 'w': // Print HImode register
290 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000291 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000292 }
293 }
294
295 printOperand(MI, OpNo);
296 return false;
297}
298
299bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
300 unsigned OpNo,
301 unsigned AsmVariant,
302 const char *ExtraCode) {
303 if (ExtraCode && ExtraCode[0])
304 return true; // Unknown modifier.
305 printMemReference(MI, OpNo);
306 return false;
307}
308
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000309/// printMachineInstruction -- Print out a single X86 LLVM instruction
310/// MI in Intel syntax to the current output stream.
311///
312void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
313 ++EmittedInsts;
314
315 // Call the autogenerated instruction printer routines.
316 printInstruction(MI);
317}
318
319bool X86IntelAsmPrinter::doInitialization(Module &M) {
Dan Gohmanb8275a32007-07-25 19:33:14 +0000320 bool Result = X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000321
Jim Laskey563321a2006-09-06 18:34:40 +0000322 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000323
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000324 O << "\t.686\n\t.model flat\n\n";
325
326 // Emit declarations for external functions.
327 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000328 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000329 std::string Name = Mang->getValueName(I);
330 X86SharedAsmPrinter::decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000331
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000332 O << "\textern " ;
333 if (I->hasDLLImportLinkage()) {
334 O << "__imp_";
335 }
336 O << Name << ":near\n";
337 }
338
Jeff Cohend43b18d2006-05-06 21:27:14 +0000339 // Emit declarations for external globals. Note that VC++ always declares
340 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000341 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
342 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000343 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000344 std::string Name = Mang->getValueName(I);
345
346 O << "\textern " ;
347 if (I->hasDLLImportLinkage()) {
348 O << "__imp_";
349 }
350 O << Name << ":byte\n";
351 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000352 }
353
Dan Gohmanb8275a32007-07-25 19:33:14 +0000354 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000355}
356
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000357bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000358 const TargetData *TD = TM.getTargetData();
359
360 // Print out module-level global variables here.
361 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
362 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000363 if (I->isDeclaration()) continue; // External global require no code
Jeff Cohend43b18d2006-05-06 21:27:14 +0000364
365 // Check to see if this is a special global used by LLVM, if so, emit it.
366 if (EmitSpecialLLVMGlobal(I))
367 continue;
368
369 std::string name = Mang->getValueName(I);
370 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000371 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000372 bool bCustomSegment = false;
373
374 switch (I->getLinkage()) {
375 case GlobalValue::LinkOnceLinkage:
376 case GlobalValue::WeakLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000377 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000378 O << name << "?\tsegment common 'COMMON'\n";
379 bCustomSegment = true;
380 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
381 // are also available.
382 break;
383 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000384 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000385 O << name << "?\tsegment public 'DATA'\n";
386 bCustomSegment = true;
387 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
388 // are also available.
389 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000390 case GlobalValue::DLLExportLinkage:
391 DLLExportedGVs.insert(name);
392 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000393 case GlobalValue::ExternalLinkage:
394 O << "\tpublic " << name << "\n";
395 // FALL THROUGH
396 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000397 SwitchToDataSection(TAI->getDataSection(), I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000398 break;
399 default:
400 assert(0 && "Unknown linkage type!");
401 }
402
403 if (!bCustomSegment)
404 EmitAlignment(Align, I);
405
Jim Laskey563321a2006-09-06 18:34:40 +0000406 O << name << ":\t\t\t\t" << TAI->getCommentString()
407 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000408
409 EmitGlobalConstant(C);
410
411 if (bCustomSegment)
412 O << name << "?\tends\n";
413 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000414
415 // Output linker support code for dllexported globals
Dan Gohmancb406c22007-10-03 19:26:29 +0000416 if (!DLLExportedGVs.empty() ||
417 !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000418 SwitchToDataSection("");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000419 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
420 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
421 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
422 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
423 O << "_drectve\t segment info alias('.drectve')\n";
424 }
425
426 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
427 e = DLLExportedGVs.end();
428 i != e; ++i) {
429 O << "\t db ' /EXPORT:" << *i << ",data'\n";
430 }
431
432 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
433 e = DLLExportedFns.end();
434 i != e; ++i) {
435 O << "\t db ' /EXPORT:" << *i << "'\n";
436 }
437
Dan Gohmancb406c22007-10-03 19:26:29 +0000438 if (!DLLExportedGVs.empty() ||
439 !DLLExportedFns.empty()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000440 O << "_drectve\t ends\n";
441 }
Jeff Cohend43b18d2006-05-06 21:27:14 +0000442
443 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000444 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000445 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000446 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000447 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000448}
449
Jeff Cohenc884db42006-05-02 01:16:28 +0000450void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
451 unsigned NumElts = CVA->getNumOperands();
452 if (NumElts) {
453 // ML does not have escape sequences except '' for '. It also has a maximum
454 // string length of 255.
455 unsigned len = 0;
456 bool inString = false;
457 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000458 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000459 if (len == 0)
460 O << "\tdb ";
461
462 if (n >= 32 && n <= 127) {
463 if (!inString) {
464 if (len > 0) {
465 O << ",'";
466 len += 2;
467 } else {
468 O << "'";
469 len++;
470 }
471 inString = true;
472 }
473 if (n == '\'') {
474 O << "'";
475 len++;
476 }
477 O << char(n);
478 } else {
479 if (inString) {
480 O << "'";
481 len++;
482 inString = false;
483 }
484 if (len > 0) {
485 O << ",";
486 len++;
487 }
488 O << n;
489 len += 1 + (n > 9) + (n > 99);
490 }
491
492 if (len > 60) {
493 if (inString) {
494 O << "'";
495 inString = false;
496 }
497 O << "\n";
498 len = 0;
499 }
500 }
501
502 if (len > 0) {
503 if (inString)
504 O << "'";
505 O << "\n";
506 }
507 }
508}
509
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000510// Include the auto-generated portion of the assembly writer.
511#include "X86GenAsmWriter1.inc"