blob: 6b09b64bf3d97047f393e030f407d00d8f3f6179 [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"
Evan Cheng7ccced62006-02-18 00:15:05 +000032#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000034
Chris Lattner95b2c7d2006-12-19 22:59:26 +000035STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000037static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
38 const TargetData *TD) {
39 X86MachineFunctionInfo Info;
40 uint64_t Size = 0;
41
42 switch (F->getCallingConv()) {
43 case CallingConv::X86_StdCall:
44 Info.setDecorationStyle(StdCall);
45 break;
46 case CallingConv::X86_FastCall:
47 Info.setDecorationStyle(FastCall);
48 break;
49 default:
50 return Info;
51 }
52
53 unsigned argNum = 1;
54 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
55 AI != AE; ++AI, ++argNum) {
56 const Type* Ty = AI->getType();
57
58 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000059 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000060 Ty = cast<PointerType>(Ty)->getElementType();
61
62 // Size should be aligned to DWORD boundary
Duncan Sands777d2302009-05-09 07:06:46 +000063 Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000064 }
65
66 // We're not supporting tooooo huge arguments :)
67 Info.setBytesToPopOnReturn((unsigned int)Size);
68 return Info;
69}
70
71
72/// decorateName - Query FunctionInfoMap and use this information for various
73/// name decoration.
74void X86IntelAsmPrinter::decorateName(std::string &Name,
75 const GlobalValue *GV) {
76 const Function *F = dyn_cast<Function>(GV);
77 if (!F) return;
78
79 // We don't want to decorate non-stdcall or non-fastcall functions right now
80 unsigned CC = F->getCallingConv();
81 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
82 return;
83
84 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
85
86 const X86MachineFunctionInfo *Info;
87 if (info_item == FunctionInfoMap.end()) {
88 // Calculate apropriate function info and populate map
89 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
90 Info = &FunctionInfoMap[F];
91 } else {
92 Info = &info_item->second;
93 }
94
95 const FunctionType *FT = F->getFunctionType();
96 switch (Info->getDecorationStyle()) {
97 case None:
98 break;
99 case StdCall:
100 // "Pure" variadic functions do not receive @0 suffix.
101 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
102 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
103 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
104 break;
105 case FastCall:
106 // "Pure" variadic functions do not receive @0 suffix.
107 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
108 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
109 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
110
111 if (Name[0] == '_')
112 Name[0] = '@';
113 else
114 Name = '@' + Name;
115
116 break;
117 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000118 llvm_unreachable("Unsupported DecorationStyle");
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000119 }
120}
121
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000122/// runOnMachineFunction - This uses the printMachineInstruction()
123/// method to print assembly for each instruction.
124///
125bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000126 this->MF = &MF;
Chris Lattner8b8b9512005-11-21 07:51:23 +0000127 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000128 O << "\n\n";
129
130 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000131 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000132
133 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000134 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000135 unsigned CC = F->getCallingConv();
Bill Wendling20c568f2009-06-30 22:38:32 +0000136 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000137
138 // Populate function information map. Actually, We don't want to populate
139 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000140 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000141 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000142
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000143 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000144
Anton Korobeynikov0c8e8062008-09-24 22:13:07 +0000145 SwitchToTextSection("_text", F);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000146 switch (F->getLinkage()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000147 default: llvm_unreachable("Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000148 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000149 case Function::LinkerPrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000150 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000151 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000152 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000153 case Function::DLLExportLinkage:
154 DLLExportedFns.insert(CurrentFnName);
155 //FALLS THROUGH
156 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000157 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000158 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000159 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000160 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000161
Jeff Cohenc884db42006-05-02 01:16:28 +0000162 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000163
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000164 // Print out code for the function.
165 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
166 I != E; ++I) {
167 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000168 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000169 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000170 O << '\n';
171 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000172 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
173 II != E; ++II) {
174 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000175 printMachineInstruction(II);
176 }
177 }
178
Chris Lattner1da31ee2006-10-05 03:01:21 +0000179 // Print out jump tables referenced by the function.
180 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
181
Jeff Cohenc884db42006-05-02 01:16:28 +0000182 O << CurrentFnName << "\tendp\n";
183
Dan Gohmane5f4de42008-11-07 19:49:17 +0000184 O.flush();
185
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000186 // We didn't modify anything.
187 return false;
188}
189
Nate Begeman391c5d22005-11-30 18:54:35 +0000190void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000191 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000192 assert(value <= 7 && "Invalid ssecc argument!");
193 switch (value) {
194 case 0: O << "eq"; break;
195 case 1: O << "lt"; break;
196 case 2: O << "le"; break;
197 case 3: O << "unord"; break;
198 case 4: O << "neq"; break;
199 case 5: O << "nlt"; break;
200 case 6: O << "nle"; break;
201 case 7: O << "ord"; break;
202 }
203}
204
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000205void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000206 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000207 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000208 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000209 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000210 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000211 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000212 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000213 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
214 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000215 Reg = getX86SubSuperRegister(Reg, VT);
216 }
Evan Chengae270f62008-07-07 22:21:06 +0000217 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000218 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000219 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000221 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000222 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000223 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000224 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000225 case MachineOperand::MO_JumpTableIndex: {
226 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
227 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000228 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000229 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000230 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000231 }
Evan Chenga09bd812006-02-26 08:28:12 +0000232 case MachineOperand::MO_ConstantPoolIndex: {
233 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
234 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000235 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000236 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000237 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000238 O << "]";
239 return;
240 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000241 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000242 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000243 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000244 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000245 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000246
Chris Lattner7680e732009-06-20 19:34:09 +0000247 if (!isMemOp) O << "OFFSET ";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000248
249 // Handle dllimport linkage.
250 // FIXME: This should be fixed with full support of stdcall & fastcall
251 // CC's
252 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000253 O << "__imp_";
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000254
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000255 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000256 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000257 return;
258 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000259 case MachineOperand::MO_ExternalSymbol: {
Jim Laskey563321a2006-09-06 18:34:40 +0000260 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000262 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000263 default:
264 O << "<unknown operand type>"; return;
265 }
266}
267
Chris Lattner7680e732009-06-20 19:34:09 +0000268void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){
269 const MachineOperand &MO = MI->getOperand(OpNo);
270 switch (MO.getType()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000271 default: llvm_unreachable("Unknown pcrel immediate operand");
Chris Lattner7680e732009-06-20 19:34:09 +0000272 case MachineOperand::MO_Immediate:
273 O << MO.getImm();
274 return;
275 case MachineOperand::MO_MachineBasicBlock:
276 printBasicBlockLabel(MO.getMBB());
277 return;
278
279 case MachineOperand::MO_GlobalAddress: {
280 GlobalValue *GV = MO.getGlobal();
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000281 std::string Name = Mang->getMangledName(GV);
Chris Lattner7680e732009-06-20 19:34:09 +0000282 decorateName(Name, GV);
283
Chris Lattner4aa21aa2009-07-09 00:58:53 +0000284 // Handle dllimport linkage.
285 // FIXME: This should be fixed with full support of stdcall & fastcall
286 // CC's
287 if (MO.getTargetFlags() == X86II::MO_DLLIMPORT)
Chris Lattner7680e732009-06-20 19:34:09 +0000288 O << "__imp_";
Chris Lattner7680e732009-06-20 19:34:09 +0000289 O << Name;
290 printOffset(MO.getOffset());
291 return;
292 }
293
294 case MachineOperand::MO_ExternalSymbol:
295 O << TAI->getGlobalPrefix() << MO.getSymbolName();
296 return;
297 }
298}
299
300
Rafael Espindola094fad32009-04-08 21:14:34 +0000301void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
302 unsigned Op,
303 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000304 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000305 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 const MachineOperand &IndexReg = MI->getOperand(Op+2);
307 const MachineOperand &DispSpec = MI->getOperand(Op+3);
308
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000309 O << "[";
310 bool NeedPlus = false;
311 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000313 NeedPlus = true;
314 }
315
316 if (IndexReg.getReg()) {
317 if (NeedPlus) O << " + ";
318 if (ScaleVal != 1)
319 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000320 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000321 NeedPlus = true;
322 }
323
Dan Gohmand735b802008-10-03 15:45:36 +0000324 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
325 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000326 if (NeedPlus)
327 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000328 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000329 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000330 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000331 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000332 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000333 if (DispVal > 0)
334 O << " + ";
335 else {
336 O << " - ";
337 DispVal = -DispVal;
338 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000339 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000340 O << DispVal;
341 }
342 }
343 O << "]";
344}
345
Rafael Espindola094fad32009-04-08 21:14:34 +0000346void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
347 const char *Modifier) {
348 assert(isMem(MI, Op) && "Invalid memory reference!");
349 MachineOperand Segment = MI->getOperand(Op+4);
350 if (Segment.getReg()) {
351 printOperand(MI, Op+4, Modifier);
352 O << ':';
353 }
354 printLeaMemReference(MI, Op, Modifier);
355}
356
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000357void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000358 const MachineBasicBlock *MBB) const {
359 if (!TAI->getSetDirective())
360 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000361
Evan Chengcc415862007-11-09 01:32:10 +0000362 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
363 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000364 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000365 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
366}
367
Evan Cheng7ccced62006-02-18 00:15:05 +0000368void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Eli Friedmanaace4b12009-06-19 04:48:38 +0000369 O << "L" << getFunctionNumber() << "$pb\n";
370 O << "L" << getFunctionNumber() << "$pb:";
Evan Cheng7ccced62006-02-18 00:15:05 +0000371}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000372
Evan Cheng55c25f22006-04-28 23:19:39 +0000373bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000374 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000375 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000376 switch (Mode) {
377 default: return true; // Unknown mode.
378 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000379 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000380 break;
381 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000382 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000383 break;
384 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000385 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000386 break;
387 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000388 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000389 break;
390 }
391
Eli Friedmanaace4b12009-06-19 04:48:38 +0000392 O << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000393 return false;
394}
395
Evan Cheng3d48a902006-04-28 21:19:05 +0000396/// PrintAsmOperand - Print out an operand for an inline asm expression.
397///
398bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000399 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000400 const char *ExtraCode) {
401 // Does this asm operand have a single letter operand modifier?
402 if (ExtraCode && ExtraCode[0]) {
403 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000404
Evan Cheng3d48a902006-04-28 21:19:05 +0000405 switch (ExtraCode[0]) {
406 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000407 case 'b': // Print QImode register
408 case 'h': // Print QImode high register
409 case 'w': // Print HImode register
410 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000411 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000412 }
413 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000414
Evan Cheng3d48a902006-04-28 21:19:05 +0000415 printOperand(MI, OpNo);
416 return false;
417}
418
419bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
420 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000421 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000422 const char *ExtraCode) {
423 if (ExtraCode && ExtraCode[0])
424 return true; // Unknown modifier.
425 printMemReference(MI, OpNo);
426 return false;
427}
428
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000429/// printMachineInstruction -- Print out a single X86 LLVM instruction
430/// MI in Intel syntax to the current output stream.
431///
432void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
433 ++EmittedInsts;
434
435 // Call the autogenerated instruction printer routines.
436 printInstruction(MI);
437}
438
439bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000440 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000441
Jim Laskey563321a2006-09-06 18:34:40 +0000442 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000443
Eli Friedmanaace4b12009-06-19 04:48:38 +0000444 O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n";
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000445
446 // Emit declarations for external functions.
447 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000448 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000449 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000450 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000451
Eli Friedmanaace4b12009-06-19 04:48:38 +0000452 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000453 if (I->hasDLLImportLinkage()) {
454 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000455 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000456 O << Name << ":near\n";
457 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000458
Jeff Cohend43b18d2006-05-06 21:27:14 +0000459 // Emit declarations for external globals. Note that VC++ always declares
460 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000461 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
462 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000463 if (I->isDeclaration()) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000464 std::string Name = Mang->getMangledName(I);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000465
Eli Friedmanaace4b12009-06-19 04:48:38 +0000466 O << "\tEXTERN " ;
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000467 if (I->hasDLLImportLinkage()) {
468 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000469 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000470 O << Name << ":byte\n";
471 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000472 }
473
Dan Gohmanb8275a32007-07-25 19:33:14 +0000474 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000475}
476
Chris Lattner40bbebd2009-07-21 18:38:57 +0000477void X86IntelAsmPrinter::PrintGlobalVariable(const GlobalVariable *GV) {
478 // Check to see if this is a special global used by LLVM, if so, emit it.
479 if (GV->isDeclaration() ||
480 EmitSpecialLLVMGlobal(GV))
481 return;
482
Jeff Cohend43b18d2006-05-06 21:27:14 +0000483 const TargetData *TD = TM.getTargetData();
484
Chris Lattner40bbebd2009-07-21 18:38:57 +0000485 std::string name = Mang->getMangledName(GV);
486 Constant *C = GV->getInitializer();
487 unsigned Align = TD->getPreferredAlignmentLog(GV);
488 bool bCustomSegment = false;
489
490 switch (GV->getLinkage()) {
491 case GlobalValue::CommonLinkage:
492 case GlobalValue::LinkOnceAnyLinkage:
493 case GlobalValue::LinkOnceODRLinkage:
494 case GlobalValue::WeakAnyLinkage:
495 case GlobalValue::WeakODRLinkage:
496 SwitchToDataSection("");
497 O << name << "?\tSEGEMNT PARA common 'COMMON'\n";
498 bCustomSegment = true;
499 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
500 // are also available.
501 break;
502 case GlobalValue::AppendingLinkage:
503 SwitchToDataSection("");
504 O << name << "?\tSEGMENT PARA public 'DATA'\n";
505 bCustomSegment = true;
506 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
507 // are also available.
508 break;
509 case GlobalValue::DLLExportLinkage:
510 DLLExportedGVs.insert(name);
511 // FALL THROUGH
512 case GlobalValue::ExternalLinkage:
513 O << "\tpublic " << name << "\n";
514 // FALL THROUGH
515 case GlobalValue::InternalLinkage:
516 SwitchToSection(TAI->getDataSection());
517 break;
518 default:
519 llvm_unreachable("Unknown linkage type!");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000520 }
Chris Lattner40bbebd2009-07-21 18:38:57 +0000521
522 if (!bCustomSegment)
523 EmitAlignment(Align, GV);
524
525 O << name << ":";
526 if (VerboseAsm)
527 O << "\t\t\t\t" << TAI->getCommentString()
528 << " " << GV->getName();
529 O << '\n';
530
531 EmitGlobalConstant(C);
532
533 if (bCustomSegment)
534 O << name << "?\tends\n";
535}
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000536
Chris Lattner40bbebd2009-07-21 18:38:57 +0000537bool X86IntelAsmPrinter::doFinalization(Module &M) {
538 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000539 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000540 SwitchToDataSection("");
Evan Cheng03339202008-09-20 00:13:08 +0000541 O << "; WARNING: The following code is valid only with MASM v8.x"
542 << "and (possible) higher\n"
543 << "; This version of MASM is usually shipped with Microsoft "
544 << "Visual Studio 2005\n"
545 << "; or (possible) further versions. Unfortunately, there is no "
546 << "way to support\n"
547 << "; dllexported symbols in the earlier versions of MASM in fully "
548 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000549 O << "_drectve\t segment info alias('.drectve')\n";
550 }
551
Anton Korobeynikov85739862008-06-27 21:22:49 +0000552 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000553 e = DLLExportedGVs.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000554 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000555 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000556
Anton Korobeynikov85739862008-06-27 21:22:49 +0000557 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000558 e = DLLExportedFns.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000559 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000560 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000561
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000562 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikov85739862008-06-27 21:22:49 +0000563 O << "_drectve\t ends\n";
Anton Korobeynikov85739862008-06-27 21:22:49 +0000564
Jeff Cohend43b18d2006-05-06 21:27:14 +0000565 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000566 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000567 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000568 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000569 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000570}
571
Jeff Cohenc884db42006-05-02 01:16:28 +0000572void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
573 unsigned NumElts = CVA->getNumOperands();
574 if (NumElts) {
575 // ML does not have escape sequences except '' for '. It also has a maximum
576 // string length of 255.
577 unsigned len = 0;
578 bool inString = false;
579 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000580 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000581 if (len == 0)
582 O << "\tdb ";
583
584 if (n >= 32 && n <= 127) {
585 if (!inString) {
586 if (len > 0) {
587 O << ",'";
588 len += 2;
589 } else {
590 O << "'";
591 len++;
592 }
593 inString = true;
594 }
595 if (n == '\'') {
596 O << "'";
597 len++;
598 }
599 O << char(n);
600 } else {
601 if (inString) {
602 O << "'";
603 len++;
604 inString = false;
605 }
606 if (len > 0) {
607 O << ",";
608 len++;
609 }
610 O << n;
611 len += 1 + (n > 9) + (n > 99);
612 }
613
614 if (len > 60) {
615 if (inString) {
616 O << "'";
617 inString = false;
618 }
619 O << "\n";
620 len = 0;
621 }
622 }
623
624 if (len > 0) {
625 if (inString)
626 O << "'";
627 O << "\n";
628 }
629 }
630}
631
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000632// Include the auto-generated portion of the assembly writer.
633#include "X86GenAsmWriter1.inc"