blob: a75422a56d5c928076ce35954a400120a27674d5 [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"
28#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000029#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000030#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000031using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000032
Chris Lattner95b2c7d2006-12-19 22:59:26 +000033STATISTIC(EmittedInsts, "Number of machine instrs printed");
34
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000035static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
36 const TargetData *TD) {
37 X86MachineFunctionInfo Info;
38 uint64_t Size = 0;
39
40 switch (F->getCallingConv()) {
41 case CallingConv::X86_StdCall:
42 Info.setDecorationStyle(StdCall);
43 break;
44 case CallingConv::X86_FastCall:
45 Info.setDecorationStyle(FastCall);
46 break;
47 default:
48 return Info;
49 }
50
51 unsigned argNum = 1;
52 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
53 AI != AE; ++AI, ++argNum) {
54 const Type* Ty = AI->getType();
55
56 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000057 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000058 Ty = cast<PointerType>(Ty)->getElementType();
59
60 // Size should be aligned to DWORD boundary
Duncan Sandsceb4d1a2009-01-12 20:38:59 +000061 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000062 }
63
64 // We're not supporting tooooo huge arguments :)
65 Info.setBytesToPopOnReturn((unsigned int)Size);
66 return Info;
67}
68
69
70/// decorateName - Query FunctionInfoMap and use this information for various
71/// name decoration.
72void X86IntelAsmPrinter::decorateName(std::string &Name,
73 const GlobalValue *GV) {
74 const Function *F = dyn_cast<Function>(GV);
75 if (!F) return;
76
77 // We don't want to decorate non-stdcall or non-fastcall functions right now
78 unsigned CC = F->getCallingConv();
79 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
80 return;
81
82 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
83
84 const X86MachineFunctionInfo *Info;
85 if (info_item == FunctionInfoMap.end()) {
86 // Calculate apropriate function info and populate map
87 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
88 Info = &FunctionInfoMap[F];
89 } else {
90 Info = &info_item->second;
91 }
92
93 const FunctionType *FT = F->getFunctionType();
94 switch (Info->getDecorationStyle()) {
95 case None:
96 break;
97 case StdCall:
98 // "Pure" variadic functions do not receive @0 suffix.
99 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
100 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
101 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
102 break;
103 case FastCall:
104 // "Pure" variadic functions do not receive @0 suffix.
105 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
106 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
107 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
108
109 if (Name[0] == '_')
110 Name[0] = '@';
111 else
112 Name = '@' + Name;
113
114 break;
115 default:
116 assert(0 && "Unsupported DecorationStyle");
117 }
118}
119
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000120/// runOnMachineFunction - This uses the printMachineInstruction()
121/// method to print assembly for each instruction.
122///
123bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Chris Lattner8b8b9512005-11-21 07:51:23 +0000124 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000125 O << "\n\n";
126
127 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000128 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000129
130 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000131 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000132 unsigned CC = F->getCallingConv();
133
134 // Populate function information map. Actually, We don't want to populate
135 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000136 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000137 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000138
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000139 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000140
Anton Korobeynikov0c8e8062008-09-24 22:13:07 +0000141 SwitchToTextSection("_text", F);
Chris Lattnerafbfded2006-10-05 02:43:52 +0000142
Devang Patel4ae641f2008-10-01 23:18:38 +0000143 unsigned FnAlign = 4;
Devang Patele4d4b8c2008-10-06 17:30:07 +0000144 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Pateldb100332008-09-04 21:03:41 +0000145 FnAlign = 1;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000146 switch (F->getLinkage()) {
147 default: assert(0 && "Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000148 case Function::PrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000149 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000150 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000151 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000152 case Function::DLLExportLinkage:
153 DLLExportedFns.insert(CurrentFnName);
154 //FALLS THROUGH
155 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000156 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000157 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000158 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000159 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000160
Jeff Cohenc884db42006-05-02 01:16:28 +0000161 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000162
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000163 // Print out code for the function.
164 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
165 I != E; ++I) {
166 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000167 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000168 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000169 O << '\n';
170 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000171 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
172 II != E; ++II) {
173 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000174 printMachineInstruction(II);
175 }
176 }
177
Chris Lattner1da31ee2006-10-05 03:01:21 +0000178 // Print out jump tables referenced by the function.
179 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
180
Jeff Cohenc884db42006-05-02 01:16:28 +0000181 O << CurrentFnName << "\tendp\n";
182
Dan Gohmane5f4de42008-11-07 19:49:17 +0000183 O.flush();
184
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000185 // We didn't modify anything.
186 return false;
187}
188
Nate Begeman391c5d22005-11-30 18:54:35 +0000189void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000190 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000191 assert(value <= 7 && "Invalid ssecc argument!");
192 switch (value) {
193 case 0: O << "eq"; break;
194 case 1: O << "lt"; break;
195 case 2: O << "le"; break;
196 case 3: O << "unord"; break;
197 case 4: O << "neq"; break;
198 case 5: O << "nlt"; break;
199 case 6: O << "nle"; break;
200 case 7: O << "ord"; break;
201 }
202}
203
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000204void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000205 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000206 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000207 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000208 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000209 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000210 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000211 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
213 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000214 Reg = getX86SubSuperRegister(Reg, VT);
215 }
Evan Chengae270f62008-07-07 22:21:06 +0000216 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000217 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000218 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000219 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000220 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000221 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000222 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000223 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000224 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000225 printBasicBlockLabel(MO.getMBB());
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 isCallOp = Modifier && !strcmp(Modifier, "call");
245 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000246 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000247 std::string Name = Mang->getValueName(GV);
248
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000249 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000250
Evan Cheng7ccced62006-02-18 00:15:05 +0000251 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252 if (GV->hasDLLImportLinkage()) {
253 // FIXME: This should be fixed with full support of stdcall & fastcall
254 // CC's
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000255 O << "__imp_";
256 }
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: {
262 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000263 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000264 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000265 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000266 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267 default:
268 O << "<unknown operand type>"; return;
269 }
270}
271
Evan Cheng25ab6902006-09-08 06:48:29 +0000272void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
273 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000274 assert(isMem(MI, Op) && "Invalid memory reference!");
275
276 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000277 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000278 const MachineOperand &IndexReg = MI->getOperand(Op+2);
279 const MachineOperand &DispSpec = MI->getOperand(Op+3);
280
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000281 O << "[";
282 bool NeedPlus = false;
283 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000284 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000285 NeedPlus = true;
286 }
287
288 if (IndexReg.getReg()) {
289 if (NeedPlus) O << " + ";
290 if (ScaleVal != 1)
291 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000293 NeedPlus = true;
294 }
295
Dan Gohmand735b802008-10-03 15:45:36 +0000296 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
297 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000298 if (NeedPlus)
299 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000300 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000301 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000302 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000304 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000305 if (DispVal > 0)
306 O << " + ";
307 else {
308 O << " - ";
309 DispVal = -DispVal;
310 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000311 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000312 O << DispVal;
313 }
314 }
315 O << "]";
316}
317
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000318void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000319 const MachineBasicBlock *MBB) const {
320 if (!TAI->getSetDirective())
321 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000322
Evan Chengcc415862007-11-09 01:32:10 +0000323 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
324 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000325 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000326 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
327}
328
Evan Cheng7ccced62006-02-18 00:15:05 +0000329void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000330 O << "\"L" << getFunctionNumber() << "$pb\"\n";
331 O << "\"L" << getFunctionNumber() << "$pb\":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000332}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000333
Evan Cheng55c25f22006-04-28 23:19:39 +0000334bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000335 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000336 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000337 switch (Mode) {
338 default: return true; // Unknown mode.
339 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000340 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000341 break;
342 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000343 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000344 break;
345 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000346 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000347 break;
348 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000349 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000350 break;
351 }
352
Evan Chengae270f62008-07-07 22:21:06 +0000353 O << '%' << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000354 return false;
355}
356
Evan Cheng3d48a902006-04-28 21:19:05 +0000357/// PrintAsmOperand - Print out an operand for an inline asm expression.
358///
359bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000360 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000361 const char *ExtraCode) {
362 // Does this asm operand have a single letter operand modifier?
363 if (ExtraCode && ExtraCode[0]) {
364 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000365
Evan Cheng3d48a902006-04-28 21:19:05 +0000366 switch (ExtraCode[0]) {
367 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000368 case 'b': // Print QImode register
369 case 'h': // Print QImode high register
370 case 'w': // Print HImode register
371 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000372 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000373 }
374 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000375
Evan Cheng3d48a902006-04-28 21:19:05 +0000376 printOperand(MI, OpNo);
377 return false;
378}
379
380bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
381 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000382 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000383 const char *ExtraCode) {
384 if (ExtraCode && ExtraCode[0])
385 return true; // Unknown modifier.
386 printMemReference(MI, OpNo);
387 return false;
388}
389
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000390/// printMachineInstruction -- Print out a single X86 LLVM instruction
391/// MI in Intel syntax to the current output stream.
392///
393void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
394 ++EmittedInsts;
395
396 // Call the autogenerated instruction printer routines.
397 printInstruction(MI);
398}
399
400bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000401 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000402
Jim Laskey563321a2006-09-06 18:34:40 +0000403 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000404
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000405 O << "\t.686\n\t.model flat\n\n";
406
407 // Emit declarations for external functions.
408 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000409 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000410 std::string Name = Mang->getValueName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000411 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000412
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000413 O << "\textern " ;
414 if (I->hasDLLImportLinkage()) {
415 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000416 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000417 O << Name << ":near\n";
418 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000419
Jeff Cohend43b18d2006-05-06 21:27:14 +0000420 // Emit declarations for external globals. Note that VC++ always declares
421 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000422 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
423 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000424 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000425 std::string Name = Mang->getValueName(I);
426
427 O << "\textern " ;
428 if (I->hasDLLImportLinkage()) {
429 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000430 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000431 O << Name << ":byte\n";
432 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000433 }
434
Dan Gohmanb8275a32007-07-25 19:33:14 +0000435 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000436}
437
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000438bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000439 const TargetData *TD = TM.getTargetData();
440
441 // Print out module-level global variables here.
442 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
443 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000444 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000445
Jeff Cohend43b18d2006-05-06 21:27:14 +0000446 // Check to see if this is a special global used by LLVM, if so, emit it.
447 if (EmitSpecialLLVMGlobal(I))
448 continue;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000449
Jeff Cohend43b18d2006-05-06 21:27:14 +0000450 std::string name = Mang->getValueName(I);
451 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000452 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000453 bool bCustomSegment = false;
454
455 switch (I->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000456 case GlobalValue::CommonLinkage:
Jeff Cohend43b18d2006-05-06 21:27:14 +0000457 case GlobalValue::LinkOnceLinkage:
458 case GlobalValue::WeakLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000459 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000460 O << name << "?\tsegment common 'COMMON'\n";
461 bCustomSegment = true;
462 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
463 // are also available.
464 break;
465 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000466 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000467 O << name << "?\tsegment public 'DATA'\n";
468 bCustomSegment = true;
469 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
470 // are also available.
471 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000472 case GlobalValue::DLLExportLinkage:
473 DLLExportedGVs.insert(name);
474 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000475 case GlobalValue::ExternalLinkage:
476 O << "\tpublic " << name << "\n";
477 // FALL THROUGH
478 case GlobalValue::InternalLinkage:
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000479 SwitchToSection(TAI->getDataSection());
Jeff Cohend43b18d2006-05-06 21:27:14 +0000480 break;
481 default:
482 assert(0 && "Unknown linkage type!");
483 }
484
485 if (!bCustomSegment)
486 EmitAlignment(Align, I);
487
Jim Laskey563321a2006-09-06 18:34:40 +0000488 O << name << ":\t\t\t\t" << TAI->getCommentString()
489 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000490
491 EmitGlobalConstant(C);
492
493 if (bCustomSegment)
494 O << name << "?\tends\n";
495 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000496
497 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000498 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000499 SwitchToDataSection("");
Evan Cheng03339202008-09-20 00:13:08 +0000500 O << "; WARNING: The following code is valid only with MASM v8.x"
501 << "and (possible) higher\n"
502 << "; This version of MASM is usually shipped with Microsoft "
503 << "Visual Studio 2005\n"
504 << "; or (possible) further versions. Unfortunately, there is no "
505 << "way to support\n"
506 << "; dllexported symbols in the earlier versions of MASM in fully "
507 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000508 O << "_drectve\t segment info alias('.drectve')\n";
509 }
510
Anton Korobeynikov85739862008-06-27 21:22:49 +0000511 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000512 e = DLLExportedGVs.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000513 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000514 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000515
Anton Korobeynikov85739862008-06-27 21:22:49 +0000516 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000517 e = DLLExportedFns.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000518 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000519 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000520
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000521 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikov85739862008-06-27 21:22:49 +0000522 O << "_drectve\t ends\n";
Anton Korobeynikov85739862008-06-27 21:22:49 +0000523
Jeff Cohend43b18d2006-05-06 21:27:14 +0000524 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000525 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000526 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000527 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000528 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000529}
530
Jeff Cohenc884db42006-05-02 01:16:28 +0000531void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
532 unsigned NumElts = CVA->getNumOperands();
533 if (NumElts) {
534 // ML does not have escape sequences except '' for '. It also has a maximum
535 // string length of 255.
536 unsigned len = 0;
537 bool inString = false;
538 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000539 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000540 if (len == 0)
541 O << "\tdb ";
542
543 if (n >= 32 && n <= 127) {
544 if (!inString) {
545 if (len > 0) {
546 O << ",'";
547 len += 2;
548 } else {
549 O << "'";
550 len++;
551 }
552 inString = true;
553 }
554 if (n == '\'') {
555 O << "'";
556 len++;
557 }
558 O << char(n);
559 } else {
560 if (inString) {
561 O << "'";
562 len++;
563 inString = false;
564 }
565 if (len > 0) {
566 O << ",";
567 len++;
568 }
569 O << n;
570 len += 1 + (n > 9) + (n > 99);
571 }
572
573 if (len > 60) {
574 if (inString) {
575 O << "'";
576 inString = false;
577 }
578 O << "\n";
579 len = 0;
580 }
581 }
582
583 if (len > 0) {
584 if (inString)
585 O << "'";
586 O << "\n";
587 }
588 }
589}
590
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000591// Include the auto-generated portion of the assembly writer.
592#include "X86GenAsmWriter1.inc"