Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- X86IntelAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 | |
| 16 | #define DEBUG_TYPE "asm-printer" |
| 17 | #include "X86IntelAsmPrinter.h" |
Cédric Venet | 4fce6e2 | 2008-08-24 12:30:46 +0000 | [diff] [blame] | 18 | #include "X86InstrInfo.h" |
| 19 | #include "X86TargetAsmInfo.h" |
| 20 | #include "X86.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/CallingConv.h" |
| 22 | #include "llvm/Constants.h" |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 23 | #include "llvm/DerivedTypes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/Statistic.h" |
| 26 | #include "llvm/ADT/StringExtras.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | #include "llvm/Assembly/Writer.h" |
Bill Wendling | 4ff1cdf | 2009-02-18 23:12:06 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/DwarfWriter.h" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Mangler.h" |
| 31 | #include "llvm/Target/TargetAsmInfo.h" |
| 32 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
| 35 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 36 | |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 37 | static 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 Patel | d222f86 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 59 | if (F->paramHasAttr(argNum, Attribute::ByVal)) |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 60 | Ty = cast<PointerType>(Ty)->getElementType(); |
| 61 | |
| 62 | // Size should be aligned to DWORD boundary |
Duncan Sands | ec4f97d | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 63 | Size += ((TD->getTypeAllocSize(Ty) + 3)/4)*4; |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 64 | } |
| 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. |
| 74 | void 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: |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 118 | llvm_unreachable("Unsupported DecorationStyle"); |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 123 | /// method to print assembly for each instruction. |
| 124 | /// |
| 125 | bool X86IntelAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Bill Wendling | 4f40531 | 2009-02-24 08:30:20 +0000 | [diff] [blame] | 126 | this->MF = &MF; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 127 | SetupMachineFunction(MF); |
| 128 | O << "\n\n"; |
| 129 | |
| 130 | // Print out constants referenced by the function |
| 131 | EmitConstantPool(MF.getConstantPool()); |
| 132 | |
| 133 | // Print out labels for the function. |
| 134 | const Function *F = MF.getFunction(); |
| 135 | unsigned CC = F->getCallingConv(); |
Bill Wendling | 25a8ae3 | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 136 | unsigned FnAlign = MF.getAlignment(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | |
| 138 | // Populate function information map. Actually, We don't want to populate |
| 139 | // non-stdcall or non-fastcall functions' information right now. |
| 140 | if (CC == CallingConv::X86_StdCall || CC == CallingConv::X86_FastCall) |
| 141 | FunctionInfoMap[F] = *MF.getInfo<X86MachineFunctionInfo>(); |
| 142 | |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 143 | decorateName(CurrentFnName, F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 144 | |
Anton Korobeynikov | cf87a3d | 2008-09-24 22:13:07 +0000 | [diff] [blame] | 145 | SwitchToTextSection("_text", F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 146 | switch (F->getLinkage()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 147 | default: llvm_unreachable("Unsupported linkage type!"); |
Rafael Espindola | a168fc9 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 148 | case Function::PrivateLinkage: |
Bill Wendling | 41a0785 | 2009-07-20 01:03:30 +0000 | [diff] [blame] | 149 | case Function::LinkerPrivateLinkage: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 150 | case Function::InternalLinkage: |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 151 | EmitAlignment(FnAlign); |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 152 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 153 | case Function::DLLExportLinkage: |
| 154 | DLLExportedFns.insert(CurrentFnName); |
| 155 | //FALLS THROUGH |
| 156 | case Function::ExternalLinkage: |
| 157 | O << "\tpublic " << CurrentFnName << "\n"; |
Evan Cheng | 2e8d3d4 | 2008-03-25 22:29:46 +0000 | [diff] [blame] | 158 | EmitAlignment(FnAlign); |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 159 | break; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | } |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 161 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | O << CurrentFnName << "\tproc near\n"; |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 163 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 164 | // 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 Gohman | 3f7d94b | 2007-10-03 19:26:29 +0000 | [diff] [blame] | 168 | if (!I->pred_empty()) { |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 169 | printBasicBlockLabel(I, true, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | O << '\n'; |
| 171 | } |
| 172 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 173 | II != E; ++II) { |
| 174 | // Print the assembly for the instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 175 | printMachineInstruction(II); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Print out jump tables referenced by the function. |
| 180 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 181 | |
| 182 | O << CurrentFnName << "\tendp\n"; |
| 183 | |
Dan Gohman | eb94abd | 2008-11-07 19:49:17 +0000 | [diff] [blame] | 184 | O.flush(); |
| 185 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 186 | // We didn't modify anything. |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | void X86IntelAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 191 | unsigned char value = MI->getOperand(Op).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | 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 Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 205 | void X86IntelAsmPrinter::printOp(const MachineOperand &MO, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | const char *Modifier) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 207 | switch (MO.getType()) { |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 208 | case MachineOperand::MO_Register: { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 209 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | unsigned Reg = MO.getReg(); |
| 211 | if (Modifier && strncmp(Modifier, "subreg", strlen("subreg")) == 0) { |
Duncan Sands | 92c4391 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 212 | MVT VT = (strcmp(Modifier,"subreg64") == 0) ? |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 213 | MVT::i64 : ((strcmp(Modifier, "subreg32") == 0) ? MVT::i32 : |
| 214 | ((strcmp(Modifier,"subreg16") == 0) ? MVT::i16 :MVT::i8)); |
| 215 | Reg = getX86SubSuperRegister(Reg, VT); |
| 216 | } |
Evan Cheng | 00d04a7 | 2008-07-07 22:21:06 +0000 | [diff] [blame] | 217 | O << TRI->getName(Reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 218 | } else |
| 219 | O << "reg" << MO.getReg(); |
| 220 | return; |
| 221 | } |
| 222 | case MachineOperand::MO_Immediate: |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 223 | O << MO.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 224 | return; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | case MachineOperand::MO_JumpTableIndex: { |
| 226 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 227 | if (!isMemOp) O << "OFFSET "; |
Evan Cheng | 477013c | 2007-10-14 05:57:21 +0000 | [diff] [blame] | 228 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 229 | << "_" << MO.getIndex(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 230 | return; |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 231 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 232 | case MachineOperand::MO_ConstantPoolIndex: { |
| 233 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
| 234 | if (!isMemOp) O << "OFFSET "; |
| 235 | O << "[" << TAI->getPrivateGlobalPrefix() << "CPI" |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 236 | << getFunctionNumber() << "_" << MO.getIndex(); |
Anton Korobeynikov | 440f23d | 2008-11-22 16:15:34 +0000 | [diff] [blame] | 237 | printOffset(MO.getOffset()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | O << "]"; |
| 239 | return; |
| 240 | } |
| 241 | case MachineOperand::MO_GlobalAddress: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 242 | bool isMemOp = Modifier && !strcmp(Modifier, "mem"); |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 243 | GlobalValue *GV = MO.getGlobal(); |
Chris Lattner | b3cdde6 | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 244 | std::string Name = Mang->getMangledName(GV); |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 245 | decorateName(Name, GV); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 246 | |
Chris Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 247 | if (!isMemOp) O << "OFFSET "; |
Chris Lattner | 9ab4e66 | 2009-07-09 00:58:53 +0000 | [diff] [blame] | 248 | |
| 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 Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 253 | O << "__imp_"; |
Chris Lattner | 9ab4e66 | 2009-07-09 00:58:53 +0000 | [diff] [blame] | 254 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 255 | O << Name; |
Anton Korobeynikov | 440f23d | 2008-11-22 16:15:34 +0000 | [diff] [blame] | 256 | printOffset(MO.getOffset()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 257 | return; |
| 258 | } |
| 259 | case MachineOperand::MO_ExternalSymbol: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | O << TAI->getGlobalPrefix() << MO.getSymbolName(); |
| 261 | return; |
| 262 | } |
| 263 | default: |
| 264 | O << "<unknown operand type>"; return; |
| 265 | } |
| 266 | } |
| 267 | |
Chris Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 268 | void X86IntelAsmPrinter::print_pcrel_imm(const MachineInstr *MI, unsigned OpNo){ |
| 269 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 270 | switch (MO.getType()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 271 | default: llvm_unreachable("Unknown pcrel immediate operand"); |
Chris Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 272 | 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 Lattner | b3cdde6 | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 281 | std::string Name = Mang->getMangledName(GV); |
Chris Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 282 | decorateName(Name, GV); |
| 283 | |
Chris Lattner | 9ab4e66 | 2009-07-09 00:58:53 +0000 | [diff] [blame] | 284 | // 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 Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 288 | O << "__imp_"; |
Chris Lattner | 357a0ca | 2009-06-20 19:34:09 +0000 | [diff] [blame] | 289 | 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 Espindola | bca99f7 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 301 | void X86IntelAsmPrinter::printLeaMemReference(const MachineInstr *MI, |
| 302 | unsigned Op, |
| 303 | const char *Modifier) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 304 | const MachineOperand &BaseReg = MI->getOperand(Op); |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 305 | int ScaleVal = MI->getOperand(Op+1).getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 306 | const MachineOperand &IndexReg = MI->getOperand(Op+2); |
| 307 | const MachineOperand &DispSpec = MI->getOperand(Op+3); |
| 308 | |
| 309 | O << "["; |
| 310 | bool NeedPlus = false; |
| 311 | if (BaseReg.getReg()) { |
| 312 | printOp(BaseReg, Modifier); |
| 313 | NeedPlus = true; |
| 314 | } |
| 315 | |
| 316 | if (IndexReg.getReg()) { |
| 317 | if (NeedPlus) O << " + "; |
| 318 | if (ScaleVal != 1) |
| 319 | O << ScaleVal << "*"; |
| 320 | printOp(IndexReg, Modifier); |
| 321 | NeedPlus = true; |
| 322 | } |
| 323 | |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 324 | if (DispSpec.isGlobal() || DispSpec.isCPI() || |
| 325 | DispSpec.isJTI()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | if (NeedPlus) |
| 327 | O << " + "; |
| 328 | printOp(DispSpec, "mem"); |
| 329 | } else { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 330 | int DispVal = DispSpec.getImm(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 331 | if (DispVal || (!BaseReg.getReg() && !IndexReg.getReg())) { |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 332 | if (NeedPlus) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 333 | if (DispVal > 0) |
| 334 | O << " + "; |
| 335 | else { |
| 336 | O << " - "; |
| 337 | DispVal = -DispVal; |
| 338 | } |
Anton Korobeynikov | 8c90d2a | 2008-02-20 11:22:39 +0000 | [diff] [blame] | 339 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 340 | O << DispVal; |
| 341 | } |
| 342 | } |
| 343 | O << "]"; |
| 344 | } |
| 345 | |
Rafael Espindola | bca99f7 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 346 | void 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 Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 357 | void X86IntelAsmPrinter::printPICJumpTableSetLabel(unsigned uid, |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 358 | const MachineBasicBlock *MBB) const { |
| 359 | if (!TAI->getSetDirective()) |
| 360 | return; |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 361 | |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 362 | O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix() |
| 363 | << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','; |
Evan Cheng | 45c1edb | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 364 | printBasicBlockLabel(MBB, false, false, false); |
Evan Cheng | 6fb0676 | 2007-11-09 01:32:10 +0000 | [diff] [blame] | 365 | O << '-' << "\"L" << getFunctionNumber() << "$pb\"'\n"; |
| 366 | } |
| 367 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 368 | void X86IntelAsmPrinter::printPICLabel(const MachineInstr *MI, unsigned Op) { |
Eli Friedman | 378ea83 | 2009-06-19 04:48:38 +0000 | [diff] [blame] | 369 | O << "L" << getFunctionNumber() << "$pb\n"; |
| 370 | O << "L" << getFunctionNumber() << "$pb:"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | bool X86IntelAsmPrinter::printAsmMRegister(const MachineOperand &MO, |
| 374 | const char Mode) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 375 | unsigned Reg = MO.getReg(); |
| 376 | switch (Mode) { |
| 377 | default: return true; // Unknown mode. |
| 378 | case 'b': // Print QImode register |
| 379 | Reg = getX86SubSuperRegister(Reg, MVT::i8); |
| 380 | break; |
| 381 | case 'h': // Print QImode high register |
| 382 | Reg = getX86SubSuperRegister(Reg, MVT::i8, true); |
| 383 | break; |
| 384 | case 'w': // Print HImode register |
| 385 | Reg = getX86SubSuperRegister(Reg, MVT::i16); |
| 386 | break; |
| 387 | case 'k': // Print SImode register |
| 388 | Reg = getX86SubSuperRegister(Reg, MVT::i32); |
| 389 | break; |
| 390 | } |
| 391 | |
Eli Friedman | 378ea83 | 2009-06-19 04:48:38 +0000 | [diff] [blame] | 392 | O << TRI->getName(Reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 393 | return false; |
| 394 | } |
| 395 | |
| 396 | /// PrintAsmOperand - Print out an operand for an inline asm expression. |
| 397 | /// |
| 398 | bool X86IntelAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 399 | unsigned AsmVariant, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 400 | 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 Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 404 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 405 | switch (ExtraCode[0]) { |
| 406 | default: return true; // Unknown modifier. |
| 407 | case 'b': // Print QImode register |
| 408 | case 'h': // Print QImode high register |
| 409 | case 'w': // Print HImode register |
| 410 | case 'k': // Print SImode register |
| 411 | return printAsmMRegister(MI->getOperand(OpNo), ExtraCode[0]); |
| 412 | } |
| 413 | } |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 414 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 415 | printOperand(MI, OpNo); |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool X86IntelAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, |
| 420 | unsigned OpNo, |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 421 | unsigned AsmVariant, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 422 | const char *ExtraCode) { |
| 423 | if (ExtraCode && ExtraCode[0]) |
| 424 | return true; // Unknown modifier. |
| 425 | printMemReference(MI, OpNo); |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | /// printMachineInstruction -- Print out a single X86 LLVM instruction |
| 430 | /// MI in Intel syntax to the current output stream. |
| 431 | /// |
| 432 | void X86IntelAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 433 | ++EmittedInsts; |
| 434 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 435 | // Call the autogenerated instruction printer routines. |
| 436 | printInstruction(MI); |
| 437 | } |
| 438 | |
| 439 | bool X86IntelAsmPrinter::doInitialization(Module &M) { |
Anton Korobeynikov | b34a9a1 | 2008-06-28 11:07:35 +0000 | [diff] [blame] | 440 | bool Result = AsmPrinter::doInitialization(M); |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 441 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 442 | Mang->markCharUnacceptable('.'); |
| 443 | |
Eli Friedman | 378ea83 | 2009-06-19 04:48:38 +0000 | [diff] [blame] | 444 | O << "\t.686\n\t.MMX\n\t.XMM\n\t.model flat\n\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 445 | |
| 446 | // Emit declarations for external functions. |
| 447 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 448 | if (I->isDeclaration()) { |
Chris Lattner | b3cdde6 | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 449 | std::string Name = Mang->getMangledName(I); |
Anton Korobeynikov | 2e7832f | 2008-06-28 11:07:54 +0000 | [diff] [blame] | 450 | decorateName(Name, I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 451 | |
Eli Friedman | 378ea83 | 2009-06-19 04:48:38 +0000 | [diff] [blame] | 452 | O << "\tEXTERN " ; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 453 | if (I->hasDLLImportLinkage()) { |
| 454 | O << "__imp_"; |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 455 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 456 | O << Name << ":near\n"; |
| 457 | } |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 458 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 459 | // 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++... |
| 461 | for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); |
| 462 | I != E; ++I) { |
| 463 | if (I->isDeclaration()) { |
Chris Lattner | b3cdde6 | 2009-07-14 18:17:16 +0000 | [diff] [blame] | 464 | std::string Name = Mang->getMangledName(I); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 465 | |
Eli Friedman | 378ea83 | 2009-06-19 04:48:38 +0000 | [diff] [blame] | 466 | O << "\tEXTERN " ; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 467 | if (I->hasDLLImportLinkage()) { |
| 468 | O << "__imp_"; |
Anton Korobeynikov | d91d360 | 2008-06-28 11:07:18 +0000 | [diff] [blame] | 469 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 470 | O << Name << ":byte\n"; |
| 471 | } |
| 472 | } |
| 473 | |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 474 | return Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 475 | } |
| 476 | |
Chris Lattner | ae98221 | 2009-07-21 18:38:57 +0000 | [diff] [blame^] | 477 | void 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 483 | const TargetData *TD = TM.getTargetData(); |
| 484 | |
Chris Lattner | ae98221 | 2009-07-21 18:38:57 +0000 | [diff] [blame^] | 485 | 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!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 520 | } |
Chris Lattner | ae98221 | 2009-07-21 18:38:57 +0000 | [diff] [blame^] | 521 | |
| 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 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 536 | |
Chris Lattner | ae98221 | 2009-07-21 18:38:57 +0000 | [diff] [blame^] | 537 | bool X86IntelAsmPrinter::doFinalization(Module &M) { |
| 538 | // Output linker support code for dllexported globals |
Anton Korobeynikov | b34a9a1 | 2008-06-28 11:07:35 +0000 | [diff] [blame] | 539 | if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 540 | SwitchToDataSection(""); |
Evan Cheng | fb62bbc | 2008-09-20 00:13:08 +0000 | [diff] [blame] | 541 | 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"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 549 | O << "_drectve\t segment info alias('.drectve')\n"; |
| 550 | } |
| 551 | |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 552 | for (StringSet<>::iterator i = DLLExportedGVs.begin(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 553 | e = DLLExportedGVs.end(); |
Anton Korobeynikov | b34a9a1 | 2008-06-28 11:07:35 +0000 | [diff] [blame] | 554 | i != e; ++i) |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 555 | O << "\t db ' /EXPORT:" << i->getKeyData() << ",data'\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 556 | |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 557 | for (StringSet<>::iterator i = DLLExportedFns.begin(), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 558 | e = DLLExportedFns.end(); |
Anton Korobeynikov | b34a9a1 | 2008-06-28 11:07:35 +0000 | [diff] [blame] | 559 | i != e; ++i) |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 560 | O << "\t db ' /EXPORT:" << i->getKeyData() << "'\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 561 | |
Anton Korobeynikov | b34a9a1 | 2008-06-28 11:07:35 +0000 | [diff] [blame] | 562 | if (!DLLExportedGVs.empty() || !DLLExportedFns.empty()) |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 563 | O << "_drectve\t ends\n"; |
Anton Korobeynikov | bafd367 | 2008-06-27 21:22:49 +0000 | [diff] [blame] | 564 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 565 | // Bypass X86SharedAsmPrinter::doFinalization(). |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 566 | bool Result = AsmPrinter::doFinalization(M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 567 | SwitchToDataSection(""); |
| 568 | O << "\tend\n"; |
Dan Gohman | 4a558a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 569 | return Result; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | void 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++) { |
| 580 | int n = cast<ConstantInt>(CVA->getOperand(i))->getZExtValue() & 255; |
| 581 | 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 | |
| 632 | // Include the auto-generated portion of the assembly writer. |
| 633 | #include "X86GenAsmWriter1.inc" |