blob: fdec3bc71392786016afb9c2546cdb397a84c980 [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
61 Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
62 }
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 Patel2c9c3e72008-09-26 23:51:19 +0000144 if (!F->isDeclaration() && 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!");
148 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000149 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000150 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000151 case Function::DLLExportLinkage:
152 DLLExportedFns.insert(CurrentFnName);
153 //FALLS THROUGH
154 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000155 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000156 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000157 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000158 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000159
Jeff Cohenc884db42006-05-02 01:16:28 +0000160 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000161
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000162 // Print out code for the function.
163 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
164 I != E; ++I) {
165 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000166 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000167 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000168 O << '\n';
169 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000170 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
171 II != E; ++II) {
172 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000173 printMachineInstruction(II);
174 }
175 }
176
Chris Lattner1da31ee2006-10-05 03:01:21 +0000177 // Print out jump tables referenced by the function.
178 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
179
Jeff Cohenc884db42006-05-02 01:16:28 +0000180 O << CurrentFnName << "\tendp\n";
181
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000182 // We didn't modify anything.
183 return false;
184}
185
Nate Begeman391c5d22005-11-30 18:54:35 +0000186void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000187 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000188 assert(value <= 7 && "Invalid ssecc argument!");
189 switch (value) {
190 case 0: O << "eq"; break;
191 case 1: O << "lt"; break;
192 case 2: O << "le"; break;
193 case 3: O << "unord"; break;
194 case 4: O << "neq"; break;
195 case 5: O << "nlt"; break;
196 case 6: O << "nle"; break;
197 case 7: O << "ord"; break;
198 }
199}
200
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000201void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000202 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000203 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000204 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000205 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000206 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000207 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000208 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000209 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
210 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000211 Reg = getX86SubSuperRegister(Reg, VT);
212 }
Evan Chengae270f62008-07-07 22:21:06 +0000213 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000214 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000215 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000216 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000217 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000218 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000219 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000220 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000221 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000222 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000223 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000224 case MachineOperand::MO_JumpTableIndex: {
225 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
226 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000227 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000228 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000229 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000230 }
Evan Chenga09bd812006-02-26 08:28:12 +0000231 case MachineOperand::MO_ConstantPoolIndex: {
232 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
233 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000234 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000235 << getFunctionNumber() << "_" << MO.getIndex();
Evan Chenga09bd812006-02-26 08:28:12 +0000236 int Offset = MO.getOffset();
237 if (Offset > 0)
238 O << " + " << Offset;
239 else if (Offset < 0)
240 O << Offset;
241 O << "]";
242 return;
243 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000244 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000245 bool isCallOp = Modifier && !strcmp(Modifier, "call");
246 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000247 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000248 std::string Name = Mang->getValueName(GV);
249
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000250 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000251
Evan Cheng7ccced62006-02-18 00:15:05 +0000252 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000253 if (GV->hasDLLImportLinkage()) {
254 // FIXME: This should be fixed with full support of stdcall & fastcall
255 // CC's
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000256 O << "__imp_";
257 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000258 O << Name;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000259 int Offset = MO.getOffset();
260 if (Offset > 0)
261 O << " + " << Offset;
262 else if (Offset < 0)
Evan Cheng345c3f32005-11-30 01:59:00 +0000263 O << Offset;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000264 return;
265 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000266 case MachineOperand::MO_ExternalSymbol: {
267 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000268 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000269 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000270 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000271 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000272 default:
273 O << "<unknown operand type>"; return;
274 }
275}
276
Evan Cheng25ab6902006-09-08 06:48:29 +0000277void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
278 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000279 assert(isMem(MI, Op) && "Invalid memory reference!");
280
281 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000282 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000283 const MachineOperand &IndexReg = MI->getOperand(Op+2);
284 const MachineOperand &DispSpec = MI->getOperand(Op+3);
285
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000286 O << "[";
287 bool NeedPlus = false;
288 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000289 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000290 NeedPlus = true;
291 }
292
293 if (IndexReg.getReg()) {
294 if (NeedPlus) O << " + ";
295 if (ScaleVal != 1)
296 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000297 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000298 NeedPlus = true;
299 }
300
Chris Lattner0bb3af92006-12-19 19:29:58 +0000301 if (DispSpec.isGlobalAddress() || DispSpec.isConstantPoolIndex() ||
302 DispSpec.isJumpTableIndex()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000303 if (NeedPlus)
304 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000305 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000307 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000308 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000309 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000310 if (DispVal > 0)
311 O << " + ";
312 else {
313 O << " - ";
314 DispVal = -DispVal;
315 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000316 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000317 O << DispVal;
318 }
319 }
320 O << "]";
321}
322
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000323void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000324 const MachineBasicBlock *MBB) const {
325 if (!TAI->getSetDirective())
326 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000327
Evan Chengcc415862007-11-09 01:32:10 +0000328 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
329 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000330 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000331 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
332}
333
Evan Cheng7ccced62006-02-18 00:15:05 +0000334void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000335 O << "\"L" << getFunctionNumber() << "$pb\"\n";
336 O << "\"L" << getFunctionNumber() << "$pb\":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000337}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000338
Evan Cheng55c25f22006-04-28 23:19:39 +0000339bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000340 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000341 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000342 switch (Mode) {
343 default: return true; // Unknown mode.
344 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000345 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000346 break;
347 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000348 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000349 break;
350 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000351 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000352 break;
353 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000354 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000355 break;
356 }
357
Evan Chengae270f62008-07-07 22:21:06 +0000358 O << '%' << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000359 return false;
360}
361
Evan Cheng3d48a902006-04-28 21:19:05 +0000362/// PrintAsmOperand - Print out an operand for an inline asm expression.
363///
364bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000365 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000366 const char *ExtraCode) {
367 // Does this asm operand have a single letter operand modifier?
368 if (ExtraCode && ExtraCode[0]) {
369 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000370
Evan Cheng3d48a902006-04-28 21:19:05 +0000371 switch (ExtraCode[0]) {
372 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000373 case 'b': // Print QImode register
374 case 'h': // Print QImode high register
375 case 'w': // Print HImode register
376 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000377 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000378 }
379 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000380
Evan Cheng3d48a902006-04-28 21:19:05 +0000381 printOperand(MI, OpNo);
382 return false;
383}
384
385bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
386 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000387 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000388 const char *ExtraCode) {
389 if (ExtraCode && ExtraCode[0])
390 return true; // Unknown modifier.
391 printMemReference(MI, OpNo);
392 return false;
393}
394
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000395/// printMachineInstruction -- Print out a single X86 LLVM instruction
396/// MI in Intel syntax to the current output stream.
397///
398void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
399 ++EmittedInsts;
400
401 // Call the autogenerated instruction printer routines.
402 printInstruction(MI);
403}
404
405bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000406 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000407
Jim Laskey563321a2006-09-06 18:34:40 +0000408 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000409
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000410 O << "\t.686\n\t.model flat\n\n";
411
412 // Emit declarations for external functions.
413 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000414 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000415 std::string Name = Mang->getValueName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000416 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000417
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000418 O << "\textern " ;
419 if (I->hasDLLImportLinkage()) {
420 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000421 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000422 O << Name << ":near\n";
423 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000424
Jeff Cohend43b18d2006-05-06 21:27:14 +0000425 // Emit declarations for external globals. Note that VC++ always declares
426 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000427 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
428 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000429 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000430 std::string Name = Mang->getValueName(I);
431
432 O << "\textern " ;
433 if (I->hasDLLImportLinkage()) {
434 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000435 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000436 O << Name << ":byte\n";
437 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000438 }
439
Dan Gohmanb8275a32007-07-25 19:33:14 +0000440 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000441}
442
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000443bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000444 const TargetData *TD = TM.getTargetData();
445
446 // Print out module-level global variables here.
447 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
448 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000449 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000450
Jeff Cohend43b18d2006-05-06 21:27:14 +0000451 // Check to see if this is a special global used by LLVM, if so, emit it.
452 if (EmitSpecialLLVMGlobal(I))
453 continue;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000454
Jeff Cohend43b18d2006-05-06 21:27:14 +0000455 std::string name = Mang->getValueName(I);
456 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000457 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000458 bool bCustomSegment = false;
459
460 switch (I->getLinkage()) {
Dale Johannesenaafce772008-05-14 20:12:51 +0000461 case GlobalValue::CommonLinkage:
Jeff Cohend43b18d2006-05-06 21:27:14 +0000462 case GlobalValue::LinkOnceLinkage:
463 case GlobalValue::WeakLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000464 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000465 O << name << "?\tsegment common 'COMMON'\n";
466 bCustomSegment = true;
467 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
468 // are also available.
469 break;
470 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000471 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000472 O << name << "?\tsegment public 'DATA'\n";
473 bCustomSegment = true;
474 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
475 // are also available.
476 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000477 case GlobalValue::DLLExportLinkage:
478 DLLExportedGVs.insert(name);
479 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000480 case GlobalValue::ExternalLinkage:
481 O << "\tpublic " << name << "\n";
482 // FALL THROUGH
483 case GlobalValue::InternalLinkage:
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000484 SwitchToSection(TAI->getDataSection());
Jeff Cohend43b18d2006-05-06 21:27:14 +0000485 break;
486 default:
487 assert(0 && "Unknown linkage type!");
488 }
489
490 if (!bCustomSegment)
491 EmitAlignment(Align, I);
492
Jim Laskey563321a2006-09-06 18:34:40 +0000493 O << name << ":\t\t\t\t" << TAI->getCommentString()
494 << " " << I->getName() << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000495
496 EmitGlobalConstant(C);
497
498 if (bCustomSegment)
499 O << name << "?\tends\n";
500 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000501
502 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000503 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000504 SwitchToDataSection("");
Evan Cheng03339202008-09-20 00:13:08 +0000505 O << "; WARNING: The following code is valid only with MASM v8.x"
506 << "and (possible) higher\n"
507 << "; This version of MASM is usually shipped with Microsoft "
508 << "Visual Studio 2005\n"
509 << "; or (possible) further versions. Unfortunately, there is no "
510 << "way to support\n"
511 << "; dllexported symbols in the earlier versions of MASM in fully "
512 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000513 O << "_drectve\t segment info alias('.drectve')\n";
514 }
515
Anton Korobeynikov85739862008-06-27 21:22:49 +0000516 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000517 e = DLLExportedGVs.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() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000520
Anton Korobeynikov85739862008-06-27 21:22:49 +0000521 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000522 e = DLLExportedFns.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000523 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000524 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000525
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000526 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikov85739862008-06-27 21:22:49 +0000527 O << "_drectve\t ends\n";
Anton Korobeynikov85739862008-06-27 21:22:49 +0000528
Jeff Cohend43b18d2006-05-06 21:27:14 +0000529 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000530 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000531 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000532 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000533 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000534}
535
Jeff Cohenc884db42006-05-02 01:16:28 +0000536void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
537 unsigned NumElts = CVA->getNumOperands();
538 if (NumElts) {
539 // ML does not have escape sequences except '' for '. It also has a maximum
540 // string length of 255.
541 unsigned len = 0;
542 bool inString = false;
543 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000544 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000545 if (len == 0)
546 O << "\tdb ";
547
548 if (n >= 32 && n <= 127) {
549 if (!inString) {
550 if (len > 0) {
551 O << ",'";
552 len += 2;
553 } else {
554 O << "'";
555 len++;
556 }
557 inString = true;
558 }
559 if (n == '\'') {
560 O << "'";
561 len++;
562 }
563 O << char(n);
564 } else {
565 if (inString) {
566 O << "'";
567 len++;
568 inString = false;
569 }
570 if (len > 0) {
571 O << ",";
572 len++;
573 }
574 O << n;
575 len += 1 + (n > 9) + (n > 99);
576 }
577
578 if (len > 60) {
579 if (inString) {
580 O << "'";
581 inString = false;
582 }
583 O << "\n";
584 len = 0;
585 }
586 }
587
588 if (len > 0) {
589 if (inString)
590 O << "'";
591 O << "\n";
592 }
593 }
594}
595
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000596// Include the auto-generated portion of the assembly writer.
597#include "X86GenAsmWriter1.inc"