blob: 64037aec10a4255572669e157282edf1a2a15d61 [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"
Cedric Venetd85f51a2008-08-24 12:30:46 +000018#include "X86InstrInfo.h"
19#include "X86TargetAsmInfo.h"
20#include "X86.h"
Anton Korobeynikovf8248682006-09-20 22:03:51 +000021#include "llvm/CallingConv.h"
Jeff Cohenc884db42006-05-02 01:16:28 +000022#include "llvm/Constants.h"
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000023#include "llvm/DerivedTypes.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000024#include "llvm/Module.h"
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000025#include "llvm/ADT/Statistic.h"
26#include "llvm/ADT/StringExtras.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000027#include "llvm/Assembly/Writer.h"
Bill Wendlingcb819f12009-02-18 23:12:06 +000028#include "llvm/CodeGen/DwarfWriter.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000030#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000031#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000033#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000035
Chris Lattner95b2c7d2006-12-19 22:59:26 +000036STATISTIC(EmittedInsts, "Number of machine instrs printed");
37
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000038static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
39 const TargetData *TD) {
40 X86MachineFunctionInfo Info;
41 uint64_t Size = 0;
42
43 switch (F->getCallingConv()) {
44 case CallingConv::X86_StdCall:
45 Info.setDecorationStyle(StdCall);
46 break;
47 case CallingConv::X86_FastCall:
48 Info.setDecorationStyle(FastCall);
49 break;
50 default:
51 return Info;
52 }
53
54 unsigned argNum = 1;
55 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
56 AI != AE; ++AI, ++argNum) {
57 const Type* Ty = AI->getType();
58
59 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000060 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000061 Ty = cast<PointerType>(Ty)->getElementType();
62
63 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000064 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000065 }
66
67 // We're not supporting tooooo huge arguments :)
68 Info.setBytesToPopOnReturn((unsigned int)Size);
69 return Info;
70}
71
72
73/// decorateName - Query FunctionInfoMap and use this information for various
74/// name decoration.
75void X86IntelAsmPrinter::decorateName(std::string &Name,
76 const GlobalValue *GV) {
77 const Function *F = dyn_cast<Function>(GV);
78 if (!F) return;
79
80 // We don't want to decorate non-stdcall or non-fastcall functions right now
81 unsigned CC = F->getCallingConv();
82 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
83 return;
84
85 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
86
87 const X86MachineFunctionInfo *Info;
88 if (info_item == FunctionInfoMap.end()) {
89 // Calculate apropriate function info and populate map
90 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
91 Info = &FunctionInfoMap[F];
92 } else {
93 Info = &info_item->second;
94 }
95
96 const FunctionType *FT = F->getFunctionType();
97 switch (Info->getDecorationStyle()) {
98 case None:
99 break;
100 case StdCall:
101 // "Pure" variadic functions do not receive @0 suffix.
102 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
103 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
104 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
105 break;
106 case FastCall:
107 // "Pure" variadic functions do not receive @0 suffix.
108 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
109 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
110 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
111
112 if (Name[0] == '_')
113 Name[0] = '@';
114 else
115 Name = '@' + Name;
116
117 break;
118 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000119 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000120 }
121}
122
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000123/// runOnMachineFunction - This uses the printMachineInstruction()
124/// method to print assembly for each instruction.
125///
126bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000127 this->MF = &MF;
Chris Lattner8b8b9512005-11-21 07:51:23 +0000128 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000129 O << "\n\n";
130
131 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000132 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000133
134 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000135 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000136 unsigned CC = F->getCallingConv();
Bill Wendling20c568f2009-06-30 22:38:32 +0000137 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000138
139 // Populate function information map. Actually, We don't want to populate
140 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000141 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000142 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000143
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000144 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000145
Chris Lattner17637662009-08-03 18:06:07 +0000146 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
147
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000148 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000149 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000150 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000151 case Function::LinkerPrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000152 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000153 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000154 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000155 case Function::DLLExportLinkage:
156 DLLExportedFns.insert(CurrentFnName);
157 //FALLS THROUGH
158 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000159 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000160 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000161 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000162 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000163
Jeff Cohenc884db42006-05-02 01:16:28 +0000164 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000165
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000166 // Print out code for the function.
167 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
168 I != E; ++I) {
169 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000170 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000171 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000172 O << '\n';
173 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000174 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
175 II != E; ++II) {
176 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000177 printMachineInstruction(II);
178 }
179 }
180
Chris Lattner1da31ee2006-10-05 03:01:21 +0000181 // Print out jump tables referenced by the function.
182 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
183
Jeff Cohenc884db42006-05-02 01:16:28 +0000184 O << CurrentFnName << "\tendp\n";
185
Dan Gohmane5f4de42008-11-07 19:49:17 +0000186 O.flush();
187
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000188 // We didn't modify anything.
189 return false;
190}
191
Nate Begeman391c5d22005-11-30 18:54:35 +0000192void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000193 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000194 assert(value <= 7 && "Invalid ssecc argument!");
195 switch (value) {
196 case 0: O << "eq"; break;
197 case 1: O << "lt"; break;
198 case 2: O << "le"; break;
199 case 3: O << "unord"; break;
200 case 4: O << "neq"; break;
201 case 5: O << "nlt"; break;
202 case 6: O << "nle"; break;
203 case 7: O << "ord"; break;
204 }
205}
206
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000207void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000208 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000209 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000210 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000211 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000212 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000213 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000214 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000215 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
216 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000217 Reg = getX86SubSuperRegister(Reg, VT);
218 }
Evan Chengae270f62008-07-07 22:21:06 +0000219 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000220 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000221 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000222 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000223 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000224 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000225 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000226 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000227 case MachineOperand::MO_JumpTableIndex: {
228 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
229 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000230 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000231 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000232 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000233 }
Evan Chenga09bd812006-02-26 08:28:12 +0000234 case MachineOperand::MO_ConstantPoolIndex: {
235 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
236 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000237 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000238 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000239 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000240 O << "]";
241 return;
242 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000243 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000244 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000245 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000246 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000247 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000248
Chris Lattner7680e732009-06-20 19:34:09 +0000249 if (!isMemOp) O << "OFFSET ";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000250
251 // Handle dllimport linkage.
252 // FIXME: This should be fixed with full support of stdcall & fastcall
253 // CC's
254 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000255 O << "__imp_";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000256
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000257 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000258 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000259 return;
260 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000261 case MachineOperand::MO_ExternalSymbol: {
Jim Laskey563321a2006-09-06 18:34:40 +0000262 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000264 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 default:
266 O << "<unknown operand type>"; return;
267 }
268}
269
Chris Lattner7680e732009-06-20 19:34:09 +0000270void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
271 const MachineOperand &MO = MI->getOperand(OpNo);
272 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000273 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +0000274 case MachineOperand::MO_Immediate:
275 O << MO.getImm();
276 return;
277 case MachineOperand::MO_MachineBasicBlock:
278 printBasicBlockLabel(MO.getMBB());
279 return;
280
281 case MachineOperand::MO_GlobalAddress: {
282 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000283 std::string Name = Mang->getMangledName(GV);
Chris Lattner7680e732009-06-20 19:34:09 +0000284 decorateName(Name, GV);
285
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000286 // Handle dllimport linkage.
287 // FIXME: This should be fixed with full support of stdcall & fastcall
288 // CC's
289 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner7680e732009-06-20 19:34:09 +0000290 O << "__imp_";
Chris Lattner7680e732009-06-20 19:34:09 +0000291 O << Name;
292 printOffset(MO.getOffset());
293 return;
294 }
295
296 case MachineOperand::MO_ExternalSymbol:
297 O << TAI->getGlobalPrefix() << MO.getSymbolName();
298 return;
299 }
300}
301
302
Rafael Espindola094fad32009-04-08 21:14:34 +0000303void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
304 unsigned Op,
305 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000307 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000308 const MachineOperand &IndexReg = MI->getOperand(Op+2);
309 const MachineOperand &DispSpec = MI->getOperand(Op+3);
310
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000311 O << "[";
312 bool NeedPlus = false;
313 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000314 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000315 NeedPlus = true;
316 }
317
318 if (IndexReg.getReg()) {
319 if (NeedPlus) O << " + ";
320 if (ScaleVal != 1)
321 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000322 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000323 NeedPlus = true;
324 }
325
Dan Gohmand735b802008-10-03 15:45:36 +0000326 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
327 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000328 if (NeedPlus)
329 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000330 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000331 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000332 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000333 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000334 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000335 if (DispVal > 0)
336 O << " + ";
337 else {
338 O << " - ";
339 DispVal = -DispVal;
340 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000341 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000342 O << DispVal;
343 }
344 }
345 O << "]";
346}
347
Rafael Espindola094fad32009-04-08 21:14:34 +0000348void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
349 const char *Modifier) {
350 assert(isMem(MI, Op) && "Invalid memory reference!");
351 MachineOperand Segment = MI->getOperand(Op+4);
352 if (Segment.getReg()) {
353 printOperand(MI, Op+4, Modifier);
354 O << ':';
355 }
356 printLeaMemReference(MI, Op, Modifier);
357}
358
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000359void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000360 const MachineBasicBlock *MBB) const {
361 if (!TAI->getSetDirective())
362 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000363
Evan Chengcc415862007-11-09 01:32:10 +0000364 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
365 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000366 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000367 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
368}
369
Evan Cheng7ccced62006-02-18 00:15:05 +0000370void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedmanaace4b12009-06-19 04:48:38 +0000371 O << "L" << getFunctionNumber() << "$pb\n";
372 O << "L" << getFunctionNumber() << "$pb:";
Evan Cheng7ccced62006-02-18 00:15:05 +0000373}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000374
Evan Cheng55c25f22006-04-28 23:19:39 +0000375bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000376 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000377 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000378 switch (Mode) {
379 default: return true; // Unknown mode.
380 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000381 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000382 break;
383 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000384 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000385 break;
386 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000387 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000388 break;
389 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000390 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000391 break;
392 }
393
Eli Friedmanaace4b12009-06-19 04:48:38 +0000394 O << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000395 return false;
396}
397
Evan Cheng3d48a902006-04-28 21:19:05 +0000398/// PrintAsmOperand - Print out an operand for an inline asm expression.
399///
400bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000401 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000402 const char *ExtraCode) {
403 // Does this asm operand have a single letter operand modifier?
404 if (ExtraCode && ExtraCode[0]) {
405 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000406
Evan Cheng3d48a902006-04-28 21:19:05 +0000407 switch (ExtraCode[0]) {
408 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000409 case 'b': // Print QImode register
410 case 'h': // Print QImode high register
411 case 'w': // Print HImode register
412 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000413 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000414 }
415 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000416
Evan Cheng3d48a902006-04-28 21:19:05 +0000417 printOperand(MI, OpNo);
418 return false;
419}
420
421bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
422 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000423 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000424 const char *ExtraCode) {
425 if (ExtraCode && ExtraCode[0])
426 return true; // Unknown modifier.
427 printMemReference(MI, OpNo);
428 return false;
429}
430
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000431/// printMachineInstruction -- Print out a single X86 LLVM instruction
432/// MI in Intel syntax to the current output stream.
433///
434void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
435 ++EmittedInsts;
436
437 // Call the autogenerated instruction printer routines.
438 printInstruction(MI);
439}
440
441bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000442 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000443
Jim Laskey563321a2006-09-06 18:34:40 +0000444 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000445
Eli Friedmanaace4b12009-06-19 04:48:38 +0000446 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000447
448 // Emit declarations for external functions.
449 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000450 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000451 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000452 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000453
Eli Friedmanaace4b12009-06-19 04:48:38 +0000454 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000455 if (I->hasDLLImportLinkage()) {
456 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000457 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000458 O << Name << ":near\n";
459 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000460
Jeff Cohend43b18d2006-05-06 21:27:14 +0000461 // Emit declarations for external globals. Note that VC++ always declares
462 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000463 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
464 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000465 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000466 std::string Name = Mang->getMangledName(I);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000467
Eli Friedmanaace4b12009-06-19 04:48:38 +0000468 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000469 if (I->hasDLLImportLinkage()) {
470 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000471 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000472 O << Name << ":byte\n";
473 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000474 }
475
Dan Gohmanb8275a32007-07-25 19:33:14 +0000476 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000477}
478
Chris Lattner40bbebd2009-07-21 18:38:57 +0000479void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
480 // Check to see if this is a special global used by LLVM, if so, emit it.
481 if (GV->isDeclaration() ||
482 EmitSpecialLLVMGlobal(GV))
483 return;
484
Jeff Cohend43b18d2006-05-06 21:27:14 +0000485 const TargetData *TD = TM.getTargetData();
486
Chris Lattner40bbebd2009-07-21 18:38:57 +0000487 std::string name = Mang->getMangledName(GV);
488 Constant *C = GV->getInitializer();
489 unsigned Align = TD->getPreferredAlignmentLog(GV);
490 bool bCustomSegment = false;
491
492 switch (GV->getLinkage()) {
493 case GlobalValue::CommonLinkage:
494 case GlobalValue::LinkOnceAnyLinkage:
495 case GlobalValue::LinkOnceODRLinkage:
496 case GlobalValue::WeakAnyLinkage:
497 case GlobalValue::WeakODRLinkage:
Chris Lattner17637662009-08-03 18:06:07 +0000498 SwitchToSection(0);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000499 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
500 bCustomSegment = true;
501 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
502 // are also available.
503 break;
504 case GlobalValue::AppendingLinkage:
Chris Lattner17637662009-08-03 18:06:07 +0000505 SwitchToSection(0);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000506 O << name << "?\tSEGMENT PARA public 'DATA'\n";
507 bCustomSegment = true;
508 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
509 // are also available.
510 break;
511 case GlobalValue::DLLExportLinkage:
512 DLLExportedGVs.insert(name);
513 // FALL THROUGH
514 case GlobalValue::ExternalLinkage:
515 O << "\tpublic " << name << "\n";
516 // FALL THROUGH
517 case GlobalValue::InternalLinkage:
Chris Lattnerf0144122009-07-28 03:13:23 +0000518 SwitchToSection(getObjFileLowering().getDataSection());
Chris Lattner40bbebd2009-07-21 18:38:57 +0000519 break;
520 default:
521 llvm_unreachable("Unknown linkage type!");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000522 }
Chris Lattner40bbebd2009-07-21 18:38:57 +0000523
524 if (!bCustomSegment)
525 EmitAlignment(Align, GV);
526
527 O << name << ":";
528 if (VerboseAsm)
529 O << "\t\t\t\t" << TAI->getCommentString()
530 << " " << GV->getName();
531 O << '\n';
532
533 EmitGlobalConstant(C);
534
535 if (bCustomSegment)
536 O << name << "?\tends\n";
537}
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000538
Chris Lattner40bbebd2009-07-21 18:38:57 +0000539bool X86IntelAsmPrinter::doFinalization(Module &M) {
540 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000541 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Chris Lattner17637662009-08-03 18:06:07 +0000542 SwitchToSection(0);
Evan Cheng03339202008-09-20 00:13:08 +0000543 O << "; WARNING: The following code is valid only with MASM v8.x"
544 << "and (possible) higher\n"
545 << "; This version of MASM is usually shipped with Microsoft "
546 << "Visual Studio 2005\n"
547 << "; or (possible) further versions. Unfortunately, there is no "
548 << "way to support\n"
549 << "; dllexported symbols in the earlier versions of MASM in fully "
550 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000551 O << "_drectve\t segment info alias('.drectve')\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000552
Chris Lattner17637662009-08-03 18:06:07 +0000553 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
554 e = DLLExportedGVs.end();
555 i != e; ++i)
556 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000557
Chris Lattner17637662009-08-03 18:06:07 +0000558 for (StringSet<>::iterator i = DLLExportedFns.begin(),
559 e = DLLExportedFns.end();
560 i != e; ++i)
561 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000562
Anton Korobeynikov85739862008-06-27 21:22:49 +0000563 O << "_drectve\t ends\n";
Chris Lattner17637662009-08-03 18:06:07 +0000564 }
Anton Korobeynikov85739862008-06-27 21:22:49 +0000565
Jeff Cohend43b18d2006-05-06 21:27:14 +0000566 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000567 bool Result = AsmPrinter::doFinalization(M);
Chris Lattner17637662009-08-03 18:06:07 +0000568 SwitchToSection(0);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000569 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000570 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000571}
572
Jeff Cohenc884db42006-05-02 01:16:28 +0000573void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
574 unsigned NumElts = CVA->getNumOperands();
575 if (NumElts) {
576 // ML does not have escape sequences except '' for '. It also has a maximum
577 // string length of 255.
578 unsigned len = 0;
579 bool inString = false;
580 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000581 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000582 if (len == 0)
583 O << "\tdb ";
584
585 if (n >= 32 && n <= 127) {
586 if (!inString) {
587 if (len > 0) {
588 O << ",'";
589 len += 2;
590 } else {
591 O << "'";
592 len++;
593 }
594 inString = true;
595 }
596 if (n == '\'') {
597 O << "'";
598 len++;
599 }
600 O << char(n);
601 } else {
602 if (inString) {
603 O << "'";
604 len++;
605 inString = false;
606 }
607 if (len > 0) {
608 O << ",";
609 len++;
610 }
611 O << n;
612 len += 1 + (n > 9) + (n > 99);
613 }
614
615 if (len > 60) {
616 if (inString) {
617 O << "'";
618 inString = false;
619 }
620 O << "\n";
621 len = 0;
622 }
623 }
624
625 if (len > 0) {
626 if (inString)
627 O << "'";
628 O << "\n";
629 }
630 }
631}
632
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000633// Include the auto-generated portion of the assembly writer.
634#include "X86GenAsmWriter1.inc"