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