blob: 141512e64c3a467c46a85b618ca29e5070824122 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
16#define DEBUG_TYPE "asm-printer"
17#include "X86IntelAsmPrinter.h"
Cédric Venet4fce6e22008-08-24 12:30:46 +000018#include "X86InstrInfo.h"
19#include "X86TargetAsmInfo.h"
20#include "X86.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CallingConv.h"
22#include "llvm/Constants.h"
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000023#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/Module.h"
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/Assembly/Writer.h"
Bill Wendling4ff1cdf2009-02-18 23:12:06 +000028#include "llvm/CodeGen/DwarfWriter.h"
Chris Lattner73266f92009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000031#include "llvm/Target/TargetLoweringObjectFile.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattner73266f92009-08-19 05:49:37 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000039static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
40 const TargetData *TD) {
41 X86MachineFunctionInfo Info;
42 uint64_t Size = 0;
43
44 switch (F->getCallingConv()) {
45 case CallingConv::X86_StdCall:
46 Info.setDecorationStyle(StdCall);
47 break;
48 case CallingConv::X86_FastCall:
49 Info.setDecorationStyle(FastCall);
50 break;
51 default:
52 return Info;
53 }
54
55 unsigned argNum = 1;
56 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
57 AI != AE; ++AI, ++argNum) {
58 const Type* Ty = AI->getType();
59
60 // 'Dereference' type in case of byval parameter attribute
Devang Pateld222f862008-09-25 21:00:45 +000061 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000062 Ty = cast<PointerType>(Ty)->getElementType();
63
64 // Size should be aligned to DWORD boundary
Duncan Sandsec4f97d2009-05-09 07:06:46 +000065 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +000066 }
67
68 // We're not supporting tooooo huge arguments :)
69 Info.setBytesToPopOnReturn((unsigned int)Size);
70 return Info;
71}
72
73
74/// decorateName - Query FunctionInfoMap and use this information for various
75/// name decoration.
76void X86IntelAsmPrinter::decorateName(std::string &Name,
77 const GlobalValue *GV) {
78 const Function *F = dyn_cast<Function>(GV);
79 if (!F) return;
80
81 // We don't want to decorate non-stdcall or non-fastcall functions right now
82 unsigned CC = F->getCallingConv();
83 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
84 return;
85
86 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
87
88 const X86MachineFunctionInfo *Info;
89 if (info_item == FunctionInfoMap.end()) {
90 // Calculate apropriate function info and populate map
91 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
92 Info = &FunctionInfoMap[F];
93 } else {
94 Info = &info_item->second;
95 }
96
97 const FunctionType *FT = F->getFunctionType();
98 switch (Info->getDecorationStyle()) {
99 case None:
100 break;
101 case StdCall:
102 // "Pure" variadic functions do not receive @0 suffix.
103 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
104 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
105 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
106 break;
107 case FastCall:
108 // "Pure" variadic functions do not receive @0 suffix.
109 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
110 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
111 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
112
113 if (Name[0] == '_')
114 Name[0] = '@';
115 else
116 Name = '@' + Name;
117
118 break;
119 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000120 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000121 }
122}
123
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124/// runOnMachineFunction - This uses the printMachineInstruction()
125/// method to print assembly for each instruction.
126///
127bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling4f405312009-02-24 08:30:20 +0000128 this->MF = &MF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 SetupMachineFunction(MF);
130 O << "\n\n";
131
132 // Print out constants referenced by the function
133 EmitConstantPool(MF.getConstantPool());
134
135 // Print out labels for the function.
136 const Function *F = MF.getFunction();
137 unsigned CC = F->getCallingConv();
Bill Wendling25a8ae32009-06-30 22:38:32 +0000138 unsigned FnAlign = MF.getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139
140 // Populate function information map. Actually, We don't want to populate
141 // non-stdcall or non-fastcall functions' information right now.
142 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
143 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
144
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000145 decorateName(CurrentFnName, F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146
Chris Lattner73266f92009-08-19 05:49:37 +0000147 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Chris Lattnera9208a12009-08-03 18:06:07 +0000148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 switch (F->getLinkage()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000150 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000151 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000152 case Function::LinkerPrivateLinkage:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 case Function::InternalLinkage:
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000154 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000155 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 case Function::DLLExportLinkage:
157 DLLExportedFns.insert(CurrentFnName);
158 //FALLS THROUGH
159 case Function::ExternalLinkage:
160 O << "\tpublic " << CurrentFnName << "\n";
Evan Cheng2e8d3d42008-03-25 22:29:46 +0000161 EmitAlignment(FnAlign);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000162 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // Print out code for the function.
168 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
169 I != E; ++I) {
170 // Print a label for the basic block if there are any predecessors.
Dan Gohman3f7d94b2007-10-03 19:26:29 +0000171 if (!I->pred_empty()) {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000172 printBasicBlockLabel(I, true, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 O << '\n';
174 }
175 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
176 II != E; ++II) {
177 // Print the assembly for the instruction.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 printMachineInstruction(II);
179 }
180 }
181
182 // Print out jump tables referenced by the function.
183 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
184
185 O << CurrentFnName << "\tendp\n";
186
187 // We didn't modify anything.
188 return false;
189}
190
191void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000192 unsigned char value = MI->getOperand(Op).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193 assert(value <= 7 && "Invalid ssecc argument!");
194 switch (value) {
195 case 0: O << "eq"; break;
196 case 1: O << "lt"; break;
197 case 2: O << "le"; break;
198 case 3: O << "unord"; break;
199 case 4: O << "neq"; break;
200 case 5: O << "nlt"; break;
201 case 6: O << "nle"; break;
202 case 7: O << "ord"; break;
203 }
204}
205
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000206void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 switch (MO.getType()) {
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000209 case MachineOperand::MO_Register: {
Dan Gohman1e57df32008-02-10 18:45:23 +0000210 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 unsigned Reg = MO.getReg();
212 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Owen Andersonac9de032009-08-10 22:56:29 +0000213 EVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000214 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
215 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 Reg = getX86SubSuperRegister(Reg, VT);
217 }
Evan Cheng00d04a72008-07-07 22:21:06 +0000218 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 } else
220 O << "reg" << MO.getReg();
221 return;
222 }
223 case MachineOperand::MO_Immediate:
Chris Lattnera96056a2007-12-30 20:49:49 +0000224 O << MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 return;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 case MachineOperand::MO_JumpTableIndex: {
227 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
228 if (!isMemOp) O << "OFFSET ";
Evan Cheng477013c2007-10-14 05:57:21 +0000229 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner6017d482007-12-30 23:10:15 +0000230 << "_" << MO.getIndex();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000232 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 case MachineOperand::MO_ConstantPoolIndex: {
234 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
235 if (!isMemOp) O << "OFFSET ";
236 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner6017d482007-12-30 23:10:15 +0000237 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000238 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 O << "]";
240 return;
241 }
242 case MachineOperand::MO_GlobalAddress: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000244 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000245 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000246 decorateName(Name, GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
Chris Lattner357a0ca2009-06-20 19:34:09 +0000248 if (!isMemOp) O << "OFFSET ";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000249
250 // Handle dllimport linkage.
251 // FIXME: This should be fixed with full support of stdcall & fastcall
252 // CC's
253 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000254 O << "__imp_";
Chris Lattner9ab4e662009-07-09 00:58:53 +0000255
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 O << Name;
Anton Korobeynikov440f23d2008-11-22 16:15:34 +0000257 printOffset(MO.getOffset());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 return;
259 }
260 case MachineOperand::MO_ExternalSymbol: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 O << TAI->getGlobalPrefix() << MO.getSymbolName();
262 return;
263 }
264 default:
265 O << "<unknown operand type>"; return;
266 }
267}
268
Chris Lattner357a0ca2009-06-20 19:34:09 +0000269void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
270 const MachineOperand &MO = MI->getOperand(OpNo);
271 switch (MO.getType()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000272 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner357a0ca2009-06-20 19:34:09 +0000273 case MachineOperand::MO_Immediate:
274 O << MO.getImm();
275 return;
276 case MachineOperand::MO_MachineBasicBlock:
Dan Gohman2aa282f2009-08-13 01:36:44 +0000277 printBasicBlockLabel(MO.getMBB(), false, false, false);
Chris Lattner357a0ca2009-06-20 19:34:09 +0000278 return;
279
280 case MachineOperand::MO_GlobalAddress: {
281 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000282 std::string Name = Mang->getMangledName(GV);
Chris Lattner357a0ca2009-06-20 19:34:09 +0000283 decorateName(Name, GV);
284
Chris Lattner9ab4e662009-07-09 00:58:53 +0000285 // Handle dllimport linkage.
286 // FIXME: This should be fixed with full support of stdcall & fastcall
287 // CC's
288 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner357a0ca2009-06-20 19:34:09 +0000289 O << "__imp_";
Chris Lattner357a0ca2009-06-20 19:34:09 +0000290 O << Name;
291 printOffset(MO.getOffset());
292 return;
293 }
294
295 case MachineOperand::MO_ExternalSymbol:
296 O << TAI->getGlobalPrefix() << MO.getSymbolName();
297 return;
298 }
299}
300
301
Rafael Espindolabca99f72009-04-08 21:14:34 +0000302void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
303 unsigned Op,
304 const char *Modifier) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattnera96056a2007-12-30 20:49:49 +0000306 int ScaleVal = MI->getOperand(Op+1).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 const MachineOperand &IndexReg = MI->getOperand(Op+2);
308 const MachineOperand &DispSpec = MI->getOperand(Op+3);
309
310 O << "[";
311 bool NeedPlus = false;
312 if (BaseReg.getReg()) {
313 printOp(BaseReg, Modifier);
314 NeedPlus = true;
315 }
316
317 if (IndexReg.getReg()) {
318 if (NeedPlus) O << " + ";
319 if (ScaleVal != 1)
320 O << ScaleVal << "*";
321 printOp(IndexReg, Modifier);
322 NeedPlus = true;
323 }
324
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000325 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
326 DispSpec.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327 if (NeedPlus)
328 O << " + ";
329 printOp(DispSpec, "mem");
330 } else {
Chris Lattnera96056a2007-12-30 20:49:49 +0000331 int DispVal = DispSpec.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000333 if (NeedPlus) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 if (DispVal > 0)
335 O << " + ";
336 else {
337 O << " - ";
338 DispVal = -DispVal;
339 }
Anton Korobeynikov8c90d2a2008-02-20 11:22:39 +0000340 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 O << DispVal;
342 }
343 }
344 O << "]";
345}
346
Rafael Espindolabca99f72009-04-08 21:14:34 +0000347void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
348 const char *Modifier) {
349 assert(isMem(MI, Op) && "Invalid memory reference!");
350 MachineOperand Segment = MI->getOperand(Op+4);
351 if (Segment.getReg()) {
352 printOperand(MI, Op+4, Modifier);
353 O << ':';
354 }
355 printLeaMemReference(MI, Op, Modifier);
356}
357
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000358void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Cheng6fb06762007-11-09 01:32:10 +0000359 const MachineBasicBlock *MBB) const {
360 if (!TAI->getSetDirective())
361 return;
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000362
Evan Cheng6fb06762007-11-09 01:32:10 +0000363 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
364 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +0000365 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng6fb06762007-11-09 01:32:10 +0000366 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
367}
368
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedman378ea832009-06-19 04:48:38 +0000370 O << "L" << getFunctionNumber() << "$pb\n";
371 O << "L" << getFunctionNumber() << "$pb:";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372}
373
374bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
375 const char Mode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 unsigned Reg = MO.getReg();
377 switch (Mode) {
378 default: return true; // Unknown mode.
379 case 'b': // Print QImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000380 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 break;
382 case 'h': // Print QImode high register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000383 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 break;
385 case 'w': // Print HImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000386 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 break;
388 case 'k': // Print SImode register
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000389 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 break;
391 }
392
Eli Friedman378ea832009-06-19 04:48:38 +0000393 O << TRI->getName(Reg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 return false;
395}
396
397/// PrintAsmOperand - Print out an operand for an inline asm expression.
398///
399bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000400 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 const char *ExtraCode) {
402 // Does this asm operand have a single letter operand modifier?
403 if (ExtraCode && ExtraCode[0]) {
404 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000405
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 switch (ExtraCode[0]) {
407 default: return true; // Unknown modifier.
408 case 'b': // Print QImode register
409 case 'h': // Print QImode high register
410 case 'w': // Print HImode register
411 case 'k': // Print SImode register
412 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
413 }
414 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000415
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 printOperand(MI, OpNo);
417 return false;
418}
419
420bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
421 unsigned OpNo,
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000422 unsigned AsmVariant,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000423 const char *ExtraCode) {
424 if (ExtraCode && ExtraCode[0])
425 return true; // Unknown modifier.
426 printMemReference(MI, OpNo);
427 return false;
428}
429
430/// printMachineInstruction -- Print out a single X86 LLVM instruction
431/// MI in Intel syntax to the current output stream.
432///
433void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
434 ++EmittedInsts;
435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 // Call the autogenerated instruction printer routines.
437 printInstruction(MI);
438}
439
440bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000441 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000442
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 Mang->markCharUnacceptable('.');
444
Eli Friedman378ea832009-06-19 04:48:38 +0000445 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446
447 // Emit declarations for external functions.
448 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
449 if (I->isDeclaration()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000450 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov2e7832f2008-06-28 11:07:54 +0000451 decorateName(Name, I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452
Eli Friedman378ea832009-06-19 04:48:38 +0000453 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 if (I->hasDLLImportLinkage()) {
455 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000456 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 O << Name << ":near\n";
458 }
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 // Emit declarations for external globals. Note that VC++ always declares
461 // external globals to have type byte, and if that's good enough for VC++...
462 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
463 I != E; ++I) {
464 if (I->isDeclaration()) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000465 std::string Name = Mang->getMangledName(I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466
Eli Friedman378ea832009-06-19 04:48:38 +0000467 O << "\tEXTERN " ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 if (I->hasDLLImportLinkage()) {
469 O << "__imp_";
Anton Korobeynikovd91d3602008-06-28 11:07:18 +0000470 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 O << Name << ":byte\n";
472 }
473 }
474
Dan Gohman4a558a32007-07-25 19:33:14 +0000475 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000476}
477
Chris Lattnerae982212009-07-21 18:38:57 +0000478void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
479 // Check to see if this is a special global used by LLVM, if so, emit it.
480 if (GV->isDeclaration() ||
481 EmitSpecialLLVMGlobal(GV))
482 return;
483
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 const TargetData *TD = TM.getTargetData();
485
Chris Lattnerae982212009-07-21 18:38:57 +0000486 std::string name = Mang->getMangledName(GV);
487 Constant *C = GV->getInitializer();
488 unsigned Align = TD->getPreferredAlignmentLog(GV);
489 bool bCustomSegment = false;
490
491 switch (GV->getLinkage()) {
492 case GlobalValue::CommonLinkage:
493 case GlobalValue::LinkOnceAnyLinkage:
494 case GlobalValue::LinkOnceODRLinkage:
495 case GlobalValue::WeakAnyLinkage:
496 case GlobalValue::WeakODRLinkage:
Chris Lattnerb43a6e32009-08-18 06:03:07 +0000497 // FIXME: make a MCSection.
Chris Lattnerae982212009-07-21 18:38:57 +0000498 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
499 bCustomSegment = true;
500 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
501 // are also available.
502 break;
503 case GlobalValue::AppendingLinkage:
Chris Lattnerb43a6e32009-08-18 06:03:07 +0000504 // FIXME: make a MCSection.
Chris Lattnerae982212009-07-21 18:38:57 +0000505 O << name << "?\tSEGMENT PARA public 'DATA'\n";
506 bCustomSegment = true;
507 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
508 // are also available.
509 break;
510 case GlobalValue::DLLExportLinkage:
511 DLLExportedGVs.insert(name);
512 // FALL THROUGH
513 case GlobalValue::ExternalLinkage:
514 O << "\tpublic " << name << "\n";
515 // FALL THROUGH
516 case GlobalValue::InternalLinkage:
Chris Lattner73266f92009-08-19 05:49:37 +0000517 OutStreamer.SwitchSection(getObjFileLowering().getDataSection());
Chris Lattnerae982212009-07-21 18:38:57 +0000518 break;
519 default:
520 llvm_unreachable("Unknown linkage type!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 }
Chris Lattnerae982212009-07-21 18:38:57 +0000522
523 if (!bCustomSegment)
524 EmitAlignment(Align, GV);
525
526 O << name << ":";
527 if (VerboseAsm)
Chris Lattner46148952009-08-17 15:48:08 +0000528 O.PadToColumn(TAI->getCommentColumn());
Dan Gohman167ff152009-08-12 18:55:32 +0000529 O << TAI->getCommentString()
Chris Lattnerae982212009-07-21 18:38:57 +0000530 << " " << GV->getName();
531 O << '\n';
532
533 EmitGlobalConstant(C);
534
535 if (bCustomSegment)
536 O << name << "?\tends\n";
537}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000538
Chris Lattnerae982212009-07-21 18:38:57 +0000539bool X86IntelAsmPrinter::doFinalization(Module &M) {
540 // Output linker support code for dllexported globals
Anton Korobeynikovb34a9a12008-06-28 11:07:35 +0000541 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Evan Chengfb62bbc2008-09-20 00:13:08 +0000542 O << "; WARNING: The following code is valid only with MASM v8.x"
543 << "and (possible) higher\n"
544 << "; This version of MASM is usually shipped with Microsoft "
545 << "Visual Studio 2005\n"
546 << "; or (possible) further versions. Unfortunately, there is no "
547 << "way to support\n"
548 << "; dllexported symbols in the earlier versions of MASM in fully "
549 << "automatic way\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 O << "_drectve\t segment info alias('.drectve')\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551
Chris Lattnera9208a12009-08-03 18:06:07 +0000552 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
553 e = DLLExportedGVs.end();
554 i != e; ++i)
555 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556
Chris Lattnera9208a12009-08-03 18:06:07 +0000557 for (StringSet<>::iterator i = DLLExportedFns.begin(),
558 e = DLLExportedFns.end();
559 i != e; ++i)
560 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000562 O << "_drectve\t ends\n";
Chris Lattnera9208a12009-08-03 18:06:07 +0000563 }
Anton Korobeynikovbafd3672008-06-27 21:22:49 +0000564
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohman4a558a32007-07-25 19:33:14 +0000566 bool Result = AsmPrinter::doFinalization(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 O << "\tend\n";
Dan Gohman4a558a32007-07-25 19:33:14 +0000568 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569}
570
571void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
572 unsigned NumElts = CVA->getNumOperands();
573 if (NumElts) {
574 // ML does not have escape sequences except '' for '. It also has a maximum
575 // string length of 255.
576 unsigned len = 0;
577 bool inString = false;
578 for (unsigned i = 0; i < NumElts; i++) {
579 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
580 if (len == 0)
581 O << "\tdb ";
582
583 if (n >= 32 && n <= 127) {
584 if (!inString) {
585 if (len > 0) {
586 O << ",'";
587 len += 2;
588 } else {
589 O << "'";
590 len++;
591 }
592 inString = true;
593 }
594 if (n == '\'') {
595 O << "'";
596 len++;
597 }
598 O << char(n);
599 } else {
600 if (inString) {
601 O << "'";
602 len++;
603 inString = false;
604 }
605 if (len > 0) {
606 O << ",";
607 len++;
608 }
609 O << n;
610 len += 1 + (n > 9) + (n > 99);
611 }
612
613 if (len > 60) {
614 if (inString) {
615 O << "'";
616 inString = false;
617 }
618 O << "\n";
619 len = 0;
620 }
621 }
622
623 if (len > 0) {
624 if (inString)
625 O << "'";
626 O << "\n";
627 }
628 }
629}
630
631// Include the auto-generated portion of the assembly writer.
632#include "X86GenAsmWriter1.inc"