blob: ddd90cd40a87beea53785e727e1cace1c7a3d8eb [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
Evan Chenge22e62b2008-03-25 22:29:46 +000060 unsigned FnAlign = OptimizeForSize ? 1 : 4;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000061 switch (F->getLinkage()) {
62 default: assert(0 && "Unsupported linkage type!");
63 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +000064 EmitAlignment(FnAlign);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000065 break;
66 case Function::DLLExportLinkage:
67 DLLExportedFns.insert(CurrentFnName);
68 //FALLS THROUGH
69 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +000070 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +000071 EmitAlignment(FnAlign);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +000072 break;
73 }
74
Jeff Cohenc884db42006-05-02 01:16:28 +000075 O << CurrentFnName << "\tproc near\n";
Jim Laskey6b92b8e2006-04-07 20:44:42 +000076
Chris Lattnerb36cbd02005-07-01 22:44:09 +000077 // Print out code for the function.
78 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
79 I != E; ++I) {
80 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +000081 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +000082 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +000083 O << '\n';
84 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +000085 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
86 II != E; ++II) {
87 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +000088 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) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000102 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000103 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 switch (MO.getType()) {
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000119 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000120 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000121 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000122 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000123 MVT::ValueType VT = (strcmp(Modifier,"subreg64") == 0) ?
124 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
125 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000126 Reg = getX86SubSuperRegister(Reg, VT);
127 }
Evan Chengda47e6e2008-03-15 00:03:38 +0000128 O << TRI->getAsmName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000129 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000130 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000132 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000133 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000134 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000135 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000136 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000137 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000138 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000139 case MachineOperand::MO_JumpTableIndex: {
140 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
141 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000142 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000143 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000144 return;
145 }
Evan Chenga09bd812006-02-26 08:28:12 +0000146 case MachineOperand::MO_ConstantPoolIndex: {
147 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
148 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000149 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000150 << getFunctionNumber() << "_" << MO.getIndex();
Evan Chenga09bd812006-02-26 08:28:12 +0000151 int Offset = MO.getOffset();
152 if (Offset > 0)
153 O << " + " << Offset;
154 else if (Offset < 0)
155 O << Offset;
156 O << "]";
157 return;
158 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000159 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000160 bool isCallOp = Modifier && !strcmp(Modifier, "call");
161 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000162 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000163 std::string Name = Mang->getValueName(GV);
164
165 X86SharedAsmPrinter::decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000166
Evan Cheng7ccced62006-02-18 00:15:05 +0000167 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000168 if (GV->hasDLLImportLinkage()) {
169 // FIXME: This should be fixed with full support of stdcall & fastcall
170 // CC's
171 O << "__imp_";
172 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000173 O << Name;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000174 int Offset = MO.getOffset();
175 if (Offset > 0)
176 O << " + " << Offset;
177 else if (Offset < 0)
Evan Cheng345c3f32005-11-30 01:59:00 +0000178 O << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000179 return;
180 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000181 case MachineOperand::MO_ExternalSymbol: {
182 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000183 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000184 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000185 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000186 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187 default:
188 O << "<unknown operand type>"; return;
189 }
190}
191
Evan Cheng25ab6902006-09-08 06:48:29 +0000192void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
193 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000194 assert(isMem(MI, Op) && "Invalid memory reference!");
195
196 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000197 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000198 const MachineOperand &IndexReg = MI->getOperand(Op+2);
199 const MachineOperand &DispSpec = MI->getOperand(Op+3);
200
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000201 O << "[";
202 bool NeedPlus = false;
203 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000205 NeedPlus = true;
206 }
207
208 if (IndexReg.getReg()) {
209 if (NeedPlus) O << " + ";
210 if (ScaleVal != 1)
211 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000213 NeedPlus = true;
214 }
215
Chris Lattner0bb3af92006-12-19 19:29:58 +0000216 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
217 DispSpec.isJumpTableIndex()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000218 if (NeedPlus)
219 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000220 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000221 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000222 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000223 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000224 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000225 if (DispVal > 0)
226 O << " + ";
227 else {
228 O << " - ";
229 DispVal = -DispVal;
230 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000231 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000232 O << DispVal;
233 }
234 }
235 O << "]";
236}
237
Evan Chengcc415862007-11-09 01:32:10 +0000238void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
239 const MachineBasicBlock *MBB) const {
240 if (!TAI->getSetDirective())
241 return;
242
243 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
244 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000245 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000246 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
247}
248
Evan Cheng7ccced62006-02-18 00:15:05 +0000249void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000250 O << "\"L" << getFunctionNumber() << "$pb\"\n";
251 O << "\"L" << getFunctionNumber() << "$pb\":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000252}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000253
Evan Cheng55c25f22006-04-28 23:19:39 +0000254bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000255 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000256 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000257 switch (Mode) {
258 default: return true; // Unknown mode.
259 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000260 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000261 break;
262 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000263 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000264 break;
265 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000266 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000267 break;
268 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000269 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000270 break;
271 }
272
Evan Chengda47e6e2008-03-15 00:03:38 +0000273 O << '%' << TRI->getAsmName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000274 return false;
275}
276
Evan Cheng3d48a902006-04-28 21:19:05 +0000277/// PrintAsmOperand - Print out an operand for an inline asm expression.
278///
279bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
280 unsigned AsmVariant,
281 const char *ExtraCode) {
282 // Does this asm operand have a single letter operand modifier?
283 if (ExtraCode && ExtraCode[0]) {
284 if (ExtraCode[1] != 0) return true; // Unknown modifier.
285
286 switch (ExtraCode[0]) {
287 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000288 case 'b': // Print QImode register
289 case 'h': // Print QImode high register
290 case 'w': // Print HImode register
291 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000292 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000293 }
294 }
295
296 printOperand(MI, OpNo);
297 return false;
298}
299
300bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
301 unsigned OpNo,
302 unsigned AsmVariant,
303 const char *ExtraCode) {
304 if (ExtraCode && ExtraCode[0])
305 return true; // Unknown modifier.
306 printMemReference(MI, OpNo);
307 return false;
308}
309
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000310/// printMachineInstruction -- Print out a single X86 LLVM instruction
311/// MI in Intel syntax to the current output stream.
312///
313void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
314 ++EmittedInsts;
315
316 // Call the autogenerated instruction printer routines.
317 printInstruction(MI);
318}
319
320bool X86IntelAsmPrinter::doInitialization(Module &M) {
Dan Gohmanb8275a32007-07-25 19:33:14 +0000321 bool Result = X86SharedAsmPrinter::doInitialization(M);
Chris Lattnerdad9c5a2006-05-09 05:12:53 +0000322
Jim Laskey563321a2006-09-06 18:34:40 +0000323 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000324
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000325 O << "\t.686\n\t.model flat\n\n";
326
327 // Emit declarations for external functions.
328 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000329 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000330 std::string Name = Mang->getValueName(I);
331 X86SharedAsmPrinter::decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000332
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000333 O << "\textern " ;
334 if (I->hasDLLImportLinkage()) {
335 O << "__imp_";
336 }
337 O << Name << ":near\n";
338 }
339
Jeff Cohend43b18d2006-05-06 21:27:14 +0000340 // Emit declarations for external globals. Note that VC++ always declares
341 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000342 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
343 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000344 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000345 std::string Name = Mang->getValueName(I);
346
347 O << "\textern " ;
348 if (I->hasDLLImportLinkage()) {
349 O << "__imp_";
350 }
351 O << Name << ":byte\n";
352 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000353 }
354
Dan Gohmanb8275a32007-07-25 19:33:14 +0000355 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000356}
357
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000358bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000359 const TargetData *TD = TM.getTargetData();
360
361 // Print out module-level global variables here.
362 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
363 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000364 if (I->isDeclaration()) continue; // External global require no code
Jeff Cohend43b18d2006-05-06 21:27:14 +0000365
366 // Check to see if this is a special global used by LLVM, if so, emit it.
367 if (EmitSpecialLLVMGlobal(I))
368 continue;
369
370 std::string name = Mang->getValueName(I);
371 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000372 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000373 bool bCustomSegment = false;
374
375 switch (I->getLinkage()) {
376 case GlobalValue::LinkOnceLinkage:
377 case GlobalValue::WeakLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000378 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000379 O << name << "?\tsegment common 'COMMON'\n";
380 bCustomSegment = true;
381 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
382 // are also available.
383 break;
384 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000385 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000386 O << name << "?\tsegment public 'DATA'\n";
387 bCustomSegment = true;
388 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
389 // are also available.
390 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000391 case GlobalValue::DLLExportLinkage:
392 DLLExportedGVs.insert(name);
393 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000394 case GlobalValue::ExternalLinkage:
395 O << "\tpublic " << name << "\n";
396 // FALL THROUGH
397 case GlobalValue::InternalLinkage:
Jim Laskey563321a2006-09-06 18:34:40 +0000398 SwitchToDataSection(TAI->getDataSection(), I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000399 break;
400 default:
401 assert(0 && "Unknown linkage type!");
402 }
403
404 if (!bCustomSegment)
405 EmitAlignment(Align, I);
406
Jim Laskey563321a2006-09-06 18:34:40 +0000407 O << name << ":\t\t\t\t" << TAI->getCommentString()
408 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000409
410 EmitGlobalConstant(C);
411
412 if (bCustomSegment)
413 O << name << "?\tends\n";
414 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000415
416 // Output linker support code for dllexported globals
Dan Gohmancb406c22007-10-03 19:26:29 +0000417 if (!DLLExportedGVs.empty() ||
418 !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000419 SwitchToDataSection("");
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000420 O << "; WARNING: The following code is valid only with MASM v8.x and (possible) higher\n"
421 << "; This version of MASM is usually shipped with Microsoft Visual Studio 2005\n"
422 << "; or (possible) further versions. Unfortunately, there is no way to support\n"
423 << "; dllexported symbols in the earlier versions of MASM in fully automatic way\n\n";
424 O << "_drectve\t segment info alias('.drectve')\n";
425 }
426
427 for (std::set<std::string>::iterator i = DLLExportedGVs.begin(),
428 e = DLLExportedGVs.end();
429 i != e; ++i) {
430 O << "\t db ' /EXPORT:" << *i << ",data'\n";
431 }
432
433 for (std::set<std::string>::iterator i = DLLExportedFns.begin(),
434 e = DLLExportedFns.end();
435 i != e; ++i) {
436 O << "\t db ' /EXPORT:" << *i << "'\n";
437 }
438
Dan Gohmancb406c22007-10-03 19:26:29 +0000439 if (!DLLExportedGVs.empty() ||
440 !DLLExportedFns.empty()) {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000441 O << "_drectve\t ends\n";
442 }
Jeff Cohend43b18d2006-05-06 21:27:14 +0000443
444 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000445 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000446 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000447 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000448 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000449}
450
Jeff Cohenc884db42006-05-02 01:16:28 +0000451void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
452 unsigned NumElts = CVA->getNumOperands();
453 if (NumElts) {
454 // ML does not have escape sequences except '' for '. It also has a maximum
455 // string length of 255.
456 unsigned len = 0;
457 bool inString = false;
458 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000459 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000460 if (len == 0)
461 O << "\tdb ";
462
463 if (n >= 32 && n <= 127) {
464 if (!inString) {
465 if (len > 0) {
466 O << ",'";
467 len += 2;
468 } else {
469 O << "'";
470 len++;
471 }
472 inString = true;
473 }
474 if (n == '\'') {
475 O << "'";
476 len++;
477 }
478 O << char(n);
479 } else {
480 if (inString) {
481 O << "'";
482 len++;
483 inString = false;
484 }
485 if (len > 0) {
486 O << ",";
487 len++;
488 }
489 O << n;
490 len += 1 + (n > 9) + (n > 99);
491 }
492
493 if (len > 60) {
494 if (inString) {
495 O << "'";
496 inString = false;
497 }
498 O << "\n";
499 len = 0;
500 }
501 }
502
503 if (len > 0) {
504 if (inString)
505 O << "'";
506 O << "\n";
507 }
508 }
509}
510
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000511// Include the auto-generated portion of the assembly writer.
512#include "X86GenAsmWriter1.inc"