blob: b694a185100469d27f0c40372a1d20fdc928ea29 [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"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000029#include "llvm/Support/Mangler.h"
Jim Laskeya0f3d172006-09-07 22:06:40 +000030#include "llvm/Target/TargetAsmInfo.h"
Evan Cheng7ccced62006-02-18 00:15:05 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnerb36cbd02005-07-01 22:44:09 +000032using namespace llvm;
Chris Lattnerb36cbd02005-07-01 22:44:09 +000033
Chris Lattner95b2c7d2006-12-19 22:59:26 +000034STATISTIC(EmittedInsts, "Number of machine instrs printed");
35
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000036static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
37 const TargetData *TD) {
38 X86MachineFunctionInfo Info;
39 uint64_t Size = 0;
40
41 switch (F->getCallingConv()) {
42 case CallingConv::X86_StdCall:
43 Info.setDecorationStyle(StdCall);
44 break;
45 case CallingConv::X86_FastCall:
46 Info.setDecorationStyle(FastCall);
47 break;
48 default:
49 return Info;
50 }
51
52 unsigned argNum = 1;
53 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
54 AI != AE; ++AI, ++argNum) {
55 const Type* Ty = AI->getType();
56
57 // 'Dereference' type in case of byval parameter attribute
Devang Patel05988662008-09-25 21:00:45 +000058 if (F->paramHasAttr(argNum, Attribute::ByVal))
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000059 Ty = cast<PointerType>(Ty)->getElementType();
60
61 // Size should be aligned to DWORD boundary
Duncan Sandsceb4d1a2009-01-12 20:38:59 +000062 Size += ((TD->getTypePaddedSize(Ty) + 3)/4)*4;
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +000063 }
64
65 // We're not supporting tooooo huge arguments :)
66 Info.setBytesToPopOnReturn((unsigned int)Size);
67 return Info;
68}
69
70
71/// decorateName - Query FunctionInfoMap and use this information for various
72/// name decoration.
73void X86IntelAsmPrinter::decorateName(std::string &Name,
74 const GlobalValue *GV) {
75 const Function *F = dyn_cast<Function>(GV);
76 if (!F) return;
77
78 // We don't want to decorate non-stdcall or non-fastcall functions right now
79 unsigned CC = F->getCallingConv();
80 if (CC != CallingConv::X86_StdCall && CC != CallingConv::X86_FastCall)
81 return;
82
83 FMFInfoMap::const_iterator info_item = FunctionInfoMap.find(F);
84
85 const X86MachineFunctionInfo *Info;
86 if (info_item == FunctionInfoMap.end()) {
87 // Calculate apropriate function info and populate map
88 FunctionInfoMap[F] = calculateFunctionInfo(F, TM.getTargetData());
89 Info = &FunctionInfoMap[F];
90 } else {
91 Info = &info_item->second;
92 }
93
94 const FunctionType *FT = F->getFunctionType();
95 switch (Info->getDecorationStyle()) {
96 case None:
97 break;
98 case StdCall:
99 // "Pure" variadic functions do not receive @0 suffix.
100 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
101 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
102 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
103 break;
104 case FastCall:
105 // "Pure" variadic functions do not receive @0 suffix.
106 if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
107 (FT->getNumParams() == 1 && F->hasStructRetAttr()))
108 Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
109
110 if (Name[0] == '_')
111 Name[0] = '@';
112 else
113 Name = '@' + Name;
114
115 break;
116 default:
117 assert(0 && "Unsupported DecorationStyle");
118 }
119}
120
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000121/// runOnMachineFunction - This uses the printMachineInstruction()
122/// method to print assembly for each instruction.
123///
124bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Bill Wendling57f0db82009-02-24 08:30:20 +0000125 this->MF = &MF;
Chris Lattner8b8b9512005-11-21 07:51:23 +0000126 SetupMachineFunction(MF);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000127 O << "\n\n";
128
129 // Print out constants referenced by the function
Chris Lattnerd939f6c2005-11-21 08:32:23 +0000130 EmitConstantPool(MF.getConstantPool());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000131
132 // Print out labels for the function.
Chris Lattnere87e1152006-09-26 03:57:53 +0000133 const Function *F = MF.getFunction();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000134 unsigned CC = F->getCallingConv();
135
136 // Populate function information map. Actually, We don't want to populate
137 // non-stdcall or non-fastcall functions' information right now.
Chris Lattnere87e1152006-09-26 03:57:53 +0000138 if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall)
Chris Lattnerd15dff22007-04-17 17:21:52 +0000139 FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000140
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000141 decorateName(CurrentFnName, F);
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000142
Anton Korobeynikov0c8e8062008-09-24 22:13:07 +0000143 SwitchToTextSection("_text", F);
Chris Lattnerafbfded2006-10-05 02:43:52 +0000144
Devang Patel4ae641f2008-10-01 23:18:38 +0000145 unsigned FnAlign = 4;
Devang Patele4d4b8c2008-10-06 17:30:07 +0000146 if (F->hasFnAttr(Attribute::OptimizeForSize))
Devang Pateldb100332008-09-04 21:03:41 +0000147 FnAlign = 1;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000148 switch (F->getLinkage()) {
149 default: assert(0 && "Unsupported linkage type!");
Rafael Espindolabb46f522009-01-15 20:18:42 +0000150 case Function::PrivateLinkage:
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000151 case Function::InternalLinkage:
Evan Chenge22e62b2008-03-25 22:29:46 +0000152 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000153 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000154 case Function::DLLExportLinkage:
155 DLLExportedFns.insert(CurrentFnName);
156 //FALLS THROUGH
157 case Function::ExternalLinkage:
Jeff Cohenc884db42006-05-02 01:16:28 +0000158 O << "\tpublic " << CurrentFnName << "\n";
Evan Chenge22e62b2008-03-25 22:29:46 +0000159 EmitAlignment(FnAlign);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000160 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000161 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000162
Jeff Cohenc884db42006-05-02 01:16:28 +0000163 O << CurrentFnName << "\tproc near\n";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000164
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000165 // Print out code for the function.
166 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
167 I != E; ++I) {
168 // Print a label for the basic block if there are any predecessors.
Dan Gohmancb406c22007-10-03 19:26:29 +0000169 if (!I->pred_empty()) {
Evan Chengfb8075d2008-02-28 00:43:03 +0000170 printBasicBlockLabel(I, true, true);
Nate Begemancdf38c42006-05-02 05:37:32 +0000171 O << '\n';
172 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000173 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
174 II != E; ++II) {
175 // Print the assembly for the instruction.
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000176 printMachineInstruction(II);
177 }
178 }
179
Chris Lattner1da31ee2006-10-05 03:01:21 +0000180 // Print out jump tables referenced by the function.
181 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
182
Jeff Cohenc884db42006-05-02 01:16:28 +0000183 O << CurrentFnName << "\tendp\n";
184
Dan Gohmane5f4de42008-11-07 19:49:17 +0000185 O.flush();
186
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000187 // We didn't modify anything.
188 return false;
189}
190
Nate Begeman391c5d22005-11-30 18:54:35 +0000191void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000192 unsigned char value = MI->getOperand(Op).getImm();
Nate Begeman6c7cb292005-07-14 22:52:25 +0000193 assert(value <= 7 && "Invalid ssecc argument!");
194 switch (value) {
195 case 0: O << "eq"; break;
196 case 1: O << "lt"; break;
197 case 2: O << "le"; break;
198 case 3: O << "unord"; break;
199 case 4: O << "neq"; break;
200 case 5: O << "nlt"; break;
201 case 6: O << "nle"; break;
202 case 7: O << "ord"; break;
203 }
204}
205
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000206void X86IntelAsmPrinter::printOp(const MachineOperand &MO,
Chris Lattnera3b8c572006-02-06 23:41:19 +0000207 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000208 switch (MO.getType()) {
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000209 case MachineOperand::MO_Register: {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000210 if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) {
Evan Cheng8f7f7122006-05-05 05:40:20 +0000211 unsigned Reg = MO.getReg();
Evan Chengcbe70e12006-05-31 22:34:26 +0000212 if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000213 MVT VT = (strcmp(Modifier,"subreg64") == 0) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000214 MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 :
215 ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8));
Evan Cheng8f7f7122006-05-05 05:40:20 +0000216 Reg = getX86SubSuperRegister(Reg, VT);
217 }
Evan Chengae270f62008-07-07 22:21:06 +0000218 O << TRI->getName(Reg);
Evan Cheng8f7f7122006-05-05 05:40:20 +0000219 } else
Chris Lattner99f26322006-05-01 05:53:50 +0000220 O << "reg" << MO.getReg();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000221 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000222 }
Chris Lattner63b3d712006-05-04 17:21:20 +0000223 case MachineOperand::MO_Immediate:
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000224 O << MO.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000225 return;
Nate Begeman37efe672006-04-22 18:53:45 +0000226 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner8aa797a2007-12-30 23:10:15 +0000227 printBasicBlockLabel(MO.getMBB());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000228 return;
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000229 case MachineOperand::MO_JumpTableIndex: {
230 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
231 if (!isMemOp) O << "OFFSET ";
Evan Cheng347d39f2007-10-14 05:57:21 +0000232 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Chris Lattner8aa797a2007-12-30 23:10:15 +0000233 << "_" << MO.getIndex();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000234 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000235 }
Evan Chenga09bd812006-02-26 08:28:12 +0000236 case MachineOperand::MO_ConstantPoolIndex: {
237 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
238 if (!isMemOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000239 O << "[" << TAI->getPrivateGlobalPrefix() << "CPI"
Chris Lattner8aa797a2007-12-30 23:10:15 +0000240 << getFunctionNumber() << "_" << MO.getIndex();
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000241 printOffset(MO.getOffset());
Evan Chenga09bd812006-02-26 08:28:12 +0000242 O << "]";
243 return;
244 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000245 case MachineOperand::MO_GlobalAddress: {
Evan Cheng7ccced62006-02-18 00:15:05 +0000246 bool isCallOp = Modifier && !strcmp(Modifier, "call");
247 bool isMemOp = Modifier && !strcmp(Modifier, "mem");
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000248 GlobalValue *GV = MO.getGlobal();
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000249 std::string Name = Mang->getValueName(GV);
250
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000251 decorateName(Name, GV);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000252
Evan Cheng7ccced62006-02-18 00:15:05 +0000253 if (!isMemOp && !isCallOp) O << "OFFSET ";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000254 if (GV->hasDLLImportLinkage()) {
255 // FIXME: This should be fixed with full support of stdcall & fastcall
256 // CC's
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000257 O << "__imp_";
258 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000259 O << Name;
Anton Korobeynikov7751ad92008-11-22 16:15:34 +0000260 printOffset(MO.getOffset());
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000261 return;
262 }
Evan Cheng7ccced62006-02-18 00:15:05 +0000263 case MachineOperand::MO_ExternalSymbol: {
264 bool isCallOp = Modifier && !strcmp(Modifier, "call");
Evan Cheng4c1aa862006-02-22 20:19:42 +0000265 if (!isCallOp) O << "OFFSET ";
Jim Laskey563321a2006-09-06 18:34:40 +0000266 O << TAI->getGlobalPrefix() << MO.getSymbolName();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000267 return;
Evan Cheng7ccced62006-02-18 00:15:05 +0000268 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000269 default:
270 O << "<unknown operand type>"; return;
271 }
272}
273
Rafael Espindola2a6411b2009-04-07 21:37:46 +0000274void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI,
275 unsigned Op,
276 const char *Modifier) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000277 const MachineOperand &BaseReg = MI->getOperand(Op);
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000278 int ScaleVal = MI->getOperand(Op+1).getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000279 const MachineOperand &IndexReg = MI->getOperand(Op+2);
280 const MachineOperand &DispSpec = MI->getOperand(Op+3);
281
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000282 O << "[";
283 bool NeedPlus = false;
284 if (BaseReg.getReg()) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000285 printOp(BaseReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000286 NeedPlus = true;
287 }
288
289 if (IndexReg.getReg()) {
290 if (NeedPlus) O << " + ";
291 if (ScaleVal != 1)
292 O << ScaleVal << "*";
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 printOp(IndexReg, Modifier);
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000294 NeedPlus = true;
295 }
296
Dan Gohmand735b802008-10-03 15:45:36 +0000297 if (DispSpec.isGlobal() || DispSpec.isCPI() ||
298 DispSpec.isJTI()) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000299 if (NeedPlus)
300 O << " + ";
Evan Cheng2338c5c2006-02-07 08:38:37 +0000301 printOp(DispSpec, "mem");
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000302 } else {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000303 int DispVal = DispSpec.getImm();
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000304 if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000305 if (NeedPlus) {
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000306 if (DispVal > 0)
307 O << " + ";
308 else {
309 O << " - ";
310 DispVal = -DispVal;
311 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +0000312 }
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000313 O << DispVal;
314 }
315 }
316 O << "]";
317}
318
Rafael Espindola2a6411b2009-04-07 21:37:46 +0000319void X86IntelAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op,
320 const char *Modifier) {
321 assert(isMem(MI, Op) && "Invalid memory reference!");
322 MachineOperand Segment = MI->getOperand(Op+4);
323 if (Segment.getReg()) {
324 printOperand(MI, Op+4, Modifier);
325 O << ':';
326 }
327 printLeaMemReference(MI, Op, Modifier);
328}
329
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000330void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid,
Evan Chengcc415862007-11-09 01:32:10 +0000331 const MachineBasicBlock *MBB) const {
332 if (!TAI->getSetDirective())
333 return;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000334
Evan Chengcc415862007-11-09 01:32:10 +0000335 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
336 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Chengfb8075d2008-02-28 00:43:03 +0000337 printBasicBlockLabel(MBB, false, false, false);
Evan Chengcc415862007-11-09 01:32:10 +0000338 O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n";
339}
340
Evan Cheng7ccced62006-02-18 00:15:05 +0000341void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) {
Evan Cheng347d39f2007-10-14 05:57:21 +0000342 O << "\"L" << getFunctionNumber() << "$pb\"\n";
343 O << "\"L" << getFunctionNumber() << "$pb\":";
Evan Cheng7ccced62006-02-18 00:15:05 +0000344}
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000345
Evan Cheng55c25f22006-04-28 23:19:39 +0000346bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO,
Evan Cheng62f27002006-04-28 23:11:40 +0000347 const char Mode) {
Evan Cheng62f27002006-04-28 23:11:40 +0000348 unsigned Reg = MO.getReg();
Evan Cheng62f27002006-04-28 23:11:40 +0000349 switch (Mode) {
350 default: return true; // Unknown mode.
351 case 'b': // Print QImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000352 Reg = getX86SubSuperRegister(Reg, MVT::i8);
Evan Cheng62f27002006-04-28 23:11:40 +0000353 break;
354 case 'h': // Print QImode high register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000355 Reg = getX86SubSuperRegister(Reg, MVT::i8, true);
Evan Cheng62f27002006-04-28 23:11:40 +0000356 break;
357 case 'w': // Print HImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000358 Reg = getX86SubSuperRegister(Reg, MVT::i16);
Evan Cheng62f27002006-04-28 23:11:40 +0000359 break;
360 case 'k': // Print SImode register
Evan Cheng8f7f7122006-05-05 05:40:20 +0000361 Reg = getX86SubSuperRegister(Reg, MVT::i32);
Evan Cheng62f27002006-04-28 23:11:40 +0000362 break;
363 }
364
Evan Chengae270f62008-07-07 22:21:06 +0000365 O << '%' << TRI->getName(Reg);
Evan Cheng62f27002006-04-28 23:11:40 +0000366 return false;
367}
368
Evan Cheng3d48a902006-04-28 21:19:05 +0000369/// PrintAsmOperand - Print out an operand for an inline asm expression.
370///
371bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000372 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000373 const char *ExtraCode) {
374 // Does this asm operand have a single letter operand modifier?
375 if (ExtraCode && ExtraCode[0]) {
376 if (ExtraCode[1] != 0) return true; // Unknown modifier.
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000377
Evan Cheng3d48a902006-04-28 21:19:05 +0000378 switch (ExtraCode[0]) {
379 default: return true; // Unknown modifier.
Evan Cheng62f27002006-04-28 23:11:40 +0000380 case 'b': // Print QImode register
381 case 'h': // Print QImode high register
382 case 'w': // Print HImode register
383 case 'k': // Print SImode register
Evan Cheng55c25f22006-04-28 23:19:39 +0000384 return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]);
Evan Cheng3d48a902006-04-28 21:19:05 +0000385 }
386 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000387
Evan Cheng3d48a902006-04-28 21:19:05 +0000388 printOperand(MI, OpNo);
389 return false;
390}
391
392bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
393 unsigned OpNo,
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000394 unsigned AsmVariant,
Evan Cheng3d48a902006-04-28 21:19:05 +0000395 const char *ExtraCode) {
396 if (ExtraCode && ExtraCode[0])
397 return true; // Unknown modifier.
398 printMemReference(MI, OpNo);
399 return false;
400}
401
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000402/// printMachineInstruction -- Print out a single X86 LLVM instruction
403/// MI in Intel syntax to the current output stream.
404///
405void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
406 ++EmittedInsts;
407
408 // Call the autogenerated instruction printer routines.
409 printInstruction(MI);
410}
411
412bool X86IntelAsmPrinter::doInitialization(Module &M) {
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000413 bool Result = AsmPrinter::doInitialization(M);
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000414
Jim Laskey563321a2006-09-06 18:34:40 +0000415 Mang->markCharUnacceptable('.');
Jeff Cohen10efcfa2006-05-04 16:20:22 +0000416
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000417 O << "\t.686\n\t.model flat\n\n";
418
419 // Emit declarations for external functions.
420 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +0000421 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000422 std::string Name = Mang->getValueName(I);
Anton Korobeynikov1c4b5ea2008-06-28 11:07:54 +0000423 decorateName(Name, I);
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000424
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000425 O << "\textern " ;
426 if (I->hasDLLImportLinkage()) {
427 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000428 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000429 O << Name << ":near\n";
430 }
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000431
Jeff Cohend43b18d2006-05-06 21:27:14 +0000432 // Emit declarations for external globals. Note that VC++ always declares
433 // external globals to have type byte, and if that's good enough for VC++...
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000434 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
435 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000436 if (I->isDeclaration()) {
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000437 std::string Name = Mang->getValueName(I);
438
439 O << "\textern " ;
440 if (I->hasDLLImportLinkage()) {
441 O << "__imp_";
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000442 }
Anton Korobeynikovf8248682006-09-20 22:03:51 +0000443 O << Name << ":byte\n";
444 }
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000445 }
446
Dan Gohmanb8275a32007-07-25 19:33:14 +0000447 return Result;
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000448}
449
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000450bool X86IntelAsmPrinter::doFinalization(Module &M) {
Jeff Cohend43b18d2006-05-06 21:27:14 +0000451 const TargetData *TD = TM.getTargetData();
452
453 // Print out module-level global variables here.
454 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
455 I != E; ++I) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000456 if (I->isDeclaration()) continue; // External global require no code
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000457
Jeff Cohend43b18d2006-05-06 21:27:14 +0000458 // Check to see if this is a special global used by LLVM, if so, emit it.
459 if (EmitSpecialLLVMGlobal(I))
460 continue;
Anton Korobeynikovb131b442008-06-28 11:07:18 +0000461
Jeff Cohend43b18d2006-05-06 21:27:14 +0000462 std::string name = Mang->getValueName(I);
463 Constant *C = I->getInitializer();
Devang Patelf9c197e2006-10-24 20:32:14 +0000464 unsigned Align = TD->getPreferredAlignmentLog(I);
Jeff Cohend43b18d2006-05-06 21:27:14 +0000465 bool bCustomSegment = false;
466
467 switch (I->getLinkage()) {
Duncan Sands4dc2b392009-03-11 20:14:15 +0000468 case GlobalValue::CommonLinkage:
Duncan Sands667d4b82009-03-07 15:45:40 +0000469 case GlobalValue::LinkOnceAnyLinkage:
470 case GlobalValue::LinkOnceODRLinkage:
471 case GlobalValue::WeakAnyLinkage:
472 case GlobalValue::WeakODRLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000473 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000474 O << name << "?\tsegment common 'COMMON'\n";
475 bCustomSegment = true;
476 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
477 // are also available.
478 break;
479 case GlobalValue::AppendingLinkage:
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000480 SwitchToDataSection("");
Jeff Cohend43b18d2006-05-06 21:27:14 +0000481 O << name << "?\tsegment public 'DATA'\n";
482 bCustomSegment = true;
483 // FIXME: the default alignment is 16 bytes, but 1, 2, 4, and 256
484 // are also available.
485 break;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000486 case GlobalValue::DLLExportLinkage:
487 DLLExportedGVs.insert(name);
488 // FALL THROUGH
Jeff Cohend43b18d2006-05-06 21:27:14 +0000489 case GlobalValue::ExternalLinkage:
490 O << "\tpublic " << name << "\n";
491 // FALL THROUGH
492 case GlobalValue::InternalLinkage:
Anton Korobeynikov315690e2008-09-24 22:16:16 +0000493 SwitchToSection(TAI->getDataSection());
Jeff Cohend43b18d2006-05-06 21:27:14 +0000494 break;
495 default:
496 assert(0 && "Unknown linkage type!");
497 }
498
499 if (!bCustomSegment)
500 EmitAlignment(Align, I);
501
Evan Chengf1c0ae92009-03-24 00:17:40 +0000502 O << name << ":";
503 if (VerboseAsm)
Evan Cheng7db860d2009-03-25 01:08:42 +0000504 O << "\t\t\t\t" << TAI->getCommentString()
Evan Chengf1c0ae92009-03-24 00:17:40 +0000505 << " " << I->getName();
506 O << '\n';
Jeff Cohend43b18d2006-05-06 21:27:14 +0000507
508 EmitGlobalConstant(C);
509
510 if (bCustomSegment)
511 O << name << "?\tends\n";
512 }
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000513
514 // Output linker support code for dllexported globals
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000515 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) {
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000516 SwitchToDataSection("");
Evan Cheng03339202008-09-20 00:13:08 +0000517 O << "; WARNING: The following code is valid only with MASM v8.x"
518 << "and (possible) higher\n"
519 << "; This version of MASM is usually shipped with Microsoft "
520 << "Visual Studio 2005\n"
521 << "; or (possible) further versions. Unfortunately, there is no "
522 << "way to support\n"
523 << "; dllexported symbols in the earlier versions of MASM in fully "
524 << "automatic way\n\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000525 O << "_drectve\t segment info alias('.drectve')\n";
526 }
527
Anton Korobeynikov85739862008-06-27 21:22:49 +0000528 for (StringSet<>::iterator i = DLLExportedGVs.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000529 e = DLLExportedGVs.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000530 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000531 O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000532
Anton Korobeynikov85739862008-06-27 21:22:49 +0000533 for (StringSet<>::iterator i = DLLExportedFns.begin(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000534 e = DLLExportedFns.end();
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000535 i != e; ++i)
Anton Korobeynikov85739862008-06-27 21:22:49 +0000536 O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n";
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000537
Anton Korobeynikov271329a2008-06-28 11:07:35 +0000538 if (!DLLExportedGVs.empty() || !DLLExportedFns.empty())
Anton Korobeynikov85739862008-06-27 21:22:49 +0000539 O << "_drectve\t ends\n";
Anton Korobeynikov85739862008-06-27 21:22:49 +0000540
Jeff Cohend43b18d2006-05-06 21:27:14 +0000541 // Bypass X86SharedAsmPrinter::doFinalization().
Dan Gohmanb8275a32007-07-25 19:33:14 +0000542 bool Result = AsmPrinter::doFinalization(M);
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000543 SwitchToDataSection("");
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000544 O << "\tend\n";
Dan Gohmanb8275a32007-07-25 19:33:14 +0000545 return Result;
Jeff Cohen4f1ea1e2006-05-02 03:11:50 +0000546}
547
Jeff Cohenc884db42006-05-02 01:16:28 +0000548void X86IntelAsmPrinter::EmitString(const ConstantArray *CVA) const {
549 unsigned NumElts = CVA->getNumOperands();
550 if (NumElts) {
551 // ML does not have escape sequences except '' for '. It also has a maximum
552 // string length of 255.
553 unsigned len = 0;
554 bool inString = false;
555 for (unsigned i = 0; i < NumElts; i++) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000556 int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255;
Jeff Cohenc884db42006-05-02 01:16:28 +0000557 if (len == 0)
558 O << "\tdb ";
559
560 if (n >= 32 && n <= 127) {
561 if (!inString) {
562 if (len > 0) {
563 O << ",'";
564 len += 2;
565 } else {
566 O << "'";
567 len++;
568 }
569 inString = true;
570 }
571 if (n == '\'') {
572 O << "'";
573 len++;
574 }
575 O << char(n);
576 } else {
577 if (inString) {
578 O << "'";
579 len++;
580 inString = false;
581 }
582 if (len > 0) {
583 O << ",";
584 len++;
585 }
586 O << n;
587 len += 1 + (n > 9) + (n > 99);
588 }
589
590 if (len > 60) {
591 if (inString) {
592 O << "'";
593 inString = false;
594 }
595 O << "\n";
596 len = 0;
597 }
598 }
599
600 if (len > 0) {
601 if (inString)
602 O << "'";
603 O << "\n";
604 }
605 }
606}
607
Chris Lattnerb36cbd02005-07-01 22:44:09 +0000608// Include the auto-generated portion of the assembly writer.
609#include "X86GenAsmWriter1.inc"