Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 1 | //===-- X86ATTAsmPrinter.cpp - Convert X86 LLVM code to Intel assembly ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to AT&T format assembly |
| 12 | // language. This printer is the output mechanism used by `llc'. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "X86ATTAsmPrinter.h" |
| 17 | #include "X86.h" |
| 18 | #include "X86TargetMachine.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Support/Mangler.h" |
Chris Lattner | 2c2c6c6 | 2006-01-22 23:41:00 +0000 | [diff] [blame] | 21 | #include <iostream> |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | using namespace x86; |
| 24 | |
| 25 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 26 | /// method to print assembly for each instruction. |
| 27 | /// |
| 28 | bool X86ATTAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 29 | SetupMachineFunction(MF); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 30 | O << "\n\n"; |
| 31 | |
| 32 | // Print out constants referenced by the function |
Chris Lattner | d939f6c | 2005-11-21 08:32:23 +0000 | [diff] [blame] | 33 | EmitConstantPool(MF.getConstantPool()); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 34 | |
| 35 | // Print out labels for the function. |
Chris Lattner | 7b6e53c | 2005-11-21 07:16:34 +0000 | [diff] [blame] | 36 | SwitchSection("\t.text\n", MF.getFunction()); |
Chris Lattner | 8b8b951 | 2005-11-21 07:51:23 +0000 | [diff] [blame] | 37 | EmitAlignment(4); // FIXME: This should be parameterized somewhere. |
Chris Lattner | 272f998 | 2005-12-16 00:07:30 +0000 | [diff] [blame] | 38 | if (!MF.getFunction()->hasInternalLinkage()) |
| 39 | O << "\t.globl\t" << CurrentFnName << "\n"; |
Chris Lattner | ac2902b | 2005-11-21 23:06:54 +0000 | [diff] [blame] | 40 | if (HasDotTypeDotSizeDirective) |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 41 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 42 | O << CurrentFnName << ":\n"; |
| 43 | |
| 44 | // Print out code for the function. |
| 45 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 46 | I != E; ++I) { |
| 47 | // Print a label for the basic block. |
| 48 | if (I->pred_begin() != I->pred_end()) |
Chris Lattner | 64965ba | 2005-11-21 07:43:59 +0000 | [diff] [blame] | 49 | O << PrivateGlobalPrefix << "BB" << CurrentFnName << "_" << I->getNumber() |
| 50 | << ":\t" << CommentString << " " << I->getBasicBlock()->getName() |
| 51 | << "\n"; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 52 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 53 | II != E; ++II) { |
| 54 | // Print the assembly for the instruction. |
| 55 | O << "\t"; |
| 56 | printMachineInstruction(II); |
| 57 | } |
| 58 | } |
Chris Lattner | ac2902b | 2005-11-21 23:06:54 +0000 | [diff] [blame] | 59 | if (HasDotTypeDotSizeDirective) |
Nate Begeman | 73213f6 | 2005-07-12 01:37:28 +0000 | [diff] [blame] | 60 | O << "\t.size " << CurrentFnName << ", .-" << CurrentFnName << "\n"; |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 61 | |
| 62 | // We didn't modify anything. |
| 63 | return false; |
| 64 | } |
| 65 | |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 66 | void X86ATTAsmPrinter::printOperand(const MachineInstr *MI, unsigned OpNo, |
| 67 | const char *Modifier) { |
| 68 | const MachineOperand &MO = MI->getOperand(OpNo); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 69 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 70 | switch (MO.getType()) { |
| 71 | case MachineOperand::MO_VirtualRegister: |
| 72 | case MachineOperand::MO_MachineRegister: |
| 73 | assert(MRegisterInfo::isPhysicalRegister(MO.getReg()) && |
| 74 | "Virtual registers should not make it this far!"); |
| 75 | O << '%'; |
| 76 | for (const char *Name = RI.get(MO.getReg()).Name; *Name; ++Name) |
| 77 | O << (char)tolower(*Name); |
| 78 | return; |
| 79 | |
| 80 | case MachineOperand::MO_SignExtendedImmed: |
| 81 | case MachineOperand::MO_UnextendedImmed: |
| 82 | O << '$' << (int)MO.getImmedValue(); |
| 83 | return; |
| 84 | case MachineOperand::MO_MachineBasicBlock: { |
| 85 | MachineBasicBlock *MBBOp = MO.getMachineBasicBlock(); |
Chris Lattner | 64965ba | 2005-11-21 07:43:59 +0000 | [diff] [blame] | 86 | O << PrivateGlobalPrefix << "BB" |
| 87 | << Mang->getValueName(MBBOp->getParent()->getFunction()) |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 88 | << "_" << MBBOp->getNumber () << "\t# " |
| 89 | << MBBOp->getBasicBlock ()->getName (); |
| 90 | return; |
| 91 | } |
| 92 | case MachineOperand::MO_PCRelativeDisp: |
| 93 | std::cerr << "Shouldn't use addPCDisp() when building X86 MachineInstrs"; |
| 94 | abort (); |
| 95 | return; |
| 96 | case MachineOperand::MO_GlobalAddress: { |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 97 | bool isCallOp = Modifier && !strcmp(Modifier, "call"); |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 98 | // Darwin block shameless ripped from PowerPCAsmPrinter.cpp |
| 99 | if (forDarwin) { |
| 100 | if (!isCallOp) O << '$'; |
| 101 | GlobalValue *GV = MO.getGlobal(); |
| 102 | std::string Name = Mang->getValueName(GV); |
| 103 | |
| 104 | // Dynamically-resolved functions need a stub for the function. Be |
| 105 | // wary however not to output $stub for external functions whose addresses |
| 106 | // are taken. Those should be emitted as $non_lazy_ptr below. |
| 107 | Function *F = dyn_cast<Function>(GV); |
| 108 | if (F && isCallOp && F->isExternal()) { |
| 109 | FnStubs.insert(Name); |
| 110 | O << "L" << Name << "$stub"; |
Nate Begeman | d3a490a | 2005-07-12 18:34:58 +0000 | [diff] [blame] | 111 | } else if (GV->hasLinkOnceLinkage()) { |
Jeff Cohen | 00b16889 | 2005-07-27 06:12:32 +0000 | [diff] [blame] | 112 | // Link-once, External, or Weakly-linked global variables need |
Nate Begeman | d3a490a | 2005-07-12 18:34:58 +0000 | [diff] [blame] | 113 | // non-lazily-resolved stubs |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 114 | LinkOnceStubs.insert(Name); |
| 115 | O << "L" << Name << "$non_lazy_ptr"; |
Nate Begeman | d3a490a | 2005-07-12 18:34:58 +0000 | [diff] [blame] | 116 | } else if (GV->isExternal() || GV->hasWeakLinkage()) { |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 117 | GVStubs.insert(Name); |
| 118 | O << "L" << Name << "$non_lazy_ptr"; |
Nate Begeman | d3a490a | 2005-07-12 18:34:58 +0000 | [diff] [blame] | 119 | } else { |
| 120 | O << Mang->getValueName(GV); |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 121 | } |
Nate Begeman | d3a490a | 2005-07-12 18:34:58 +0000 | [diff] [blame] | 122 | int Offset = MO.getOffset(); |
| 123 | if (Offset > 0) |
| 124 | O << "+" << Offset; |
| 125 | else if (Offset < 0) |
| 126 | O << Offset; |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 127 | return; |
| 128 | } |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 129 | if (!isCallOp) O << '$'; |
| 130 | O << Mang->getValueName(MO.getGlobal()); |
| 131 | int Offset = MO.getOffset(); |
| 132 | if (Offset > 0) |
| 133 | O << "+" << Offset; |
| 134 | else if (Offset < 0) |
| 135 | O << Offset; |
| 136 | return; |
| 137 | } |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 138 | case MachineOperand::MO_ExternalSymbol: { |
| 139 | bool isCallOp = Modifier && !strcmp(Modifier, "call"); |
Nate Begeman | 72b286b | 2005-07-08 00:23:26 +0000 | [diff] [blame] | 140 | if (isCallOp && forDarwin) { |
| 141 | std::string Name(GlobalPrefix); Name += MO.getSymbolName(); |
| 142 | FnStubs.insert(Name); |
| 143 | O << "L" << Name << "$stub"; |
| 144 | return; |
| 145 | } |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 146 | if (!isCallOp) O << '$'; |
| 147 | O << GlobalPrefix << MO.getSymbolName(); |
| 148 | return; |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 149 | } |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 150 | default: |
| 151 | O << "<unknown operand type>"; return; |
| 152 | } |
| 153 | } |
| 154 | |
Nate Begeman | 391c5d2 | 2005-11-30 18:54:35 +0000 | [diff] [blame] | 155 | void X86ATTAsmPrinter::printSSECC(const MachineInstr *MI, unsigned Op) { |
Nate Begeman | 6c7cb29 | 2005-07-14 22:52:25 +0000 | [diff] [blame] | 156 | unsigned char value = MI->getOperand(Op).getImmedValue(); |
| 157 | assert(value <= 7 && "Invalid ssecc argument!"); |
| 158 | switch (value) { |
| 159 | case 0: O << "eq"; break; |
| 160 | case 1: O << "lt"; break; |
| 161 | case 2: O << "le"; break; |
| 162 | case 3: O << "unord"; break; |
| 163 | case 4: O << "neq"; break; |
| 164 | case 5: O << "nlt"; break; |
| 165 | case 6: O << "nle"; break; |
| 166 | case 7: O << "ord"; break; |
| 167 | } |
| 168 | } |
| 169 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 170 | void X86ATTAsmPrinter::printMemReference(const MachineInstr *MI, unsigned Op){ |
| 171 | assert(isMem(MI, Op) && "Invalid memory reference!"); |
| 172 | |
| 173 | const MachineOperand &BaseReg = MI->getOperand(Op); |
| 174 | int ScaleVal = MI->getOperand(Op+1).getImmedValue(); |
| 175 | const MachineOperand &IndexReg = MI->getOperand(Op+2); |
| 176 | const MachineOperand &DispSpec = MI->getOperand(Op+3); |
| 177 | |
| 178 | if (BaseReg.isFrameIndex()) { |
| 179 | O << "[frame slot #" << BaseReg.getFrameIndex(); |
| 180 | if (DispSpec.getImmedValue()) |
| 181 | O << " + " << DispSpec.getImmedValue(); |
| 182 | O << "]"; |
| 183 | return; |
| 184 | } else if (BaseReg.isConstantPoolIndex()) { |
Chris Lattner | d939f6c | 2005-11-21 08:32:23 +0000 | [diff] [blame] | 185 | O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_" |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 186 | << BaseReg.getConstantPoolIndex(); |
| 187 | if (DispSpec.getImmedValue()) |
| 188 | O << "+" << DispSpec.getImmedValue(); |
| 189 | if (IndexReg.getReg()) { |
| 190 | O << "(,"; |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 191 | printOperand(MI, Op+2); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 192 | if (ScaleVal != 1) |
| 193 | O << "," << ScaleVal; |
| 194 | O << ")"; |
| 195 | } |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (DispSpec.isGlobalAddress()) { |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 200 | printOperand(MI, Op+3, "call"); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 201 | } else { |
| 202 | int DispVal = DispSpec.getImmedValue(); |
| 203 | if (DispVal || (!IndexReg.getReg() && !BaseReg.getReg())) |
| 204 | O << DispVal; |
| 205 | } |
| 206 | |
| 207 | if (IndexReg.getReg() || BaseReg.getReg()) { |
| 208 | O << "("; |
| 209 | if (BaseReg.getReg()) |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 210 | printOperand(MI, Op); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 211 | |
| 212 | if (IndexReg.getReg()) { |
| 213 | O << ","; |
Chris Lattner | a3b8c57 | 2006-02-06 23:41:19 +0000 | [diff] [blame^] | 214 | printOperand(MI, Op+2); |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 215 | if (ScaleVal != 1) |
| 216 | O << "," << ScaleVal; |
| 217 | } |
| 218 | |
| 219 | O << ")"; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /// printMachineInstruction -- Print out a single X86 LLVM instruction |
| 224 | /// MI in Intel syntax to the current output stream. |
| 225 | /// |
| 226 | void X86ATTAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 227 | ++EmittedInsts; |
Evan Cheng | 67caa39 | 2006-01-26 02:27:43 +0000 | [diff] [blame] | 228 | // This works around some Darwin assembler bugs. |
| 229 | if (forDarwin) { |
| 230 | switch (MI->getOpcode()) { |
| 231 | case X86::REP_MOVSB: |
| 232 | O << "rep/movsb (%esi),(%edi)\n"; |
| 233 | return; |
| 234 | case X86::REP_MOVSD: |
| 235 | O << "rep/movsl (%esi),(%edi)\n"; |
| 236 | return; |
| 237 | case X86::REP_MOVSW: |
| 238 | O << "rep/movsw (%esi),(%edi)\n"; |
| 239 | return; |
| 240 | case X86::REP_STOSB: |
| 241 | O << "rep/stosb\n"; |
| 242 | return; |
| 243 | case X86::REP_STOSD: |
| 244 | O << "rep/stosl\n"; |
| 245 | return; |
| 246 | case X86::REP_STOSW: |
| 247 | O << "rep/stosw\n"; |
| 248 | return; |
| 249 | default: |
| 250 | break; |
| 251 | } |
| 252 | } |
| 253 | |
Chris Lattner | b36cbd0 | 2005-07-01 22:44:09 +0000 | [diff] [blame] | 254 | // Call the autogenerated instruction printer routines. |
| 255 | printInstruction(MI); |
| 256 | } |
| 257 | |
| 258 | // Include the auto-generated portion of the assembly writer. |
| 259 | #include "X86GenAsmWriter.inc" |
| 260 | |