Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 1 | //===-- EmitAssembly.cpp - Emit Sparc Specific .s File ---------------------==// |
| 2 | // |
| 3 | // This file implements all of the stuff neccesary to output a .s file from |
| 4 | // LLVM. The code in this file assumes that the specified module has already |
| 5 | // been compiled into the internal data structures of the Module. |
| 6 | // |
| 7 | // The entry point of this file is the UltraSparc::emitAssembly method. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #include "SparcInternals.h" |
| 12 | #include "llvm/Analysis/SlotCalculator.h" |
| 13 | #include "llvm/CodeGen/MachineInstr.h" |
| 14 | #include "llvm/BasicBlock.h" |
| 15 | #include "llvm/Method.h" |
| 16 | #include "llvm/Module.h" |
| 17 | #include "llvm/Support/StringExtras.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class SparcAsmPrinter { |
| 22 | ostream &Out; |
| 23 | SlotCalculator Table; |
Chris Lattner | ec0a95f | 2001-10-15 15:54:43 +0000 | [diff] [blame] | 24 | const UltraSparc &Target; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 25 | |
| 26 | enum Sections { |
| 27 | Unknown, |
| 28 | Text, |
| 29 | Data, |
| 30 | ReadOnly, |
| 31 | } CurSection; |
| 32 | public: |
Chris Lattner | ec0a95f | 2001-10-15 15:54:43 +0000 | [diff] [blame] | 33 | inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t) |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 34 | : Out(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) { |
| 35 | emitModule(M); |
| 36 | } |
| 37 | |
| 38 | private : |
| 39 | void emitModule(const Module *M); |
| 40 | /* |
| 41 | void processSymbolTable(const SymbolTable &ST); |
| 42 | void processConstant(const ConstPoolVal *CPV); |
| 43 | void processGlobal(const GlobalVariable *GV); |
| 44 | */ |
| 45 | void emitMethod(const Method *M); |
| 46 | //void processMethodArgument(const MethodArgument *MA); |
| 47 | void emitBasicBlock(const BasicBlock *BB); |
| 48 | void emitMachineInst(const MachineInstr *MI); |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 49 | void printOperand(const MachineOperand &Op); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 50 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 51 | |
| 52 | // enterSection - Use this method to enter a different section of the output |
| 53 | // executable. This is used to only output neccesary section transitions. |
| 54 | // |
| 55 | void enterSection(enum Sections S) { |
| 56 | if (S == CurSection) return; // Only switch section if neccesary |
| 57 | CurSection = S; |
| 58 | |
| 59 | Out << ".section \"."; |
| 60 | switch (S) { |
| 61 | default: assert(0 && "Bad section name!"); |
| 62 | case Text: Out << "text"; break; |
| 63 | case Data: Out << "data"; break; |
| 64 | case ReadOnly: Out << "rodata"; break; |
| 65 | } |
| 66 | Out << "\"\n"; |
| 67 | } |
| 68 | |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 69 | string getEscapedString(const string &S) { |
| 70 | string Result; |
| 71 | |
| 72 | for (unsigned i = 0; i < S.size(); ++i) { |
| 73 | char C = S[i]; |
| 74 | if ((C >= 'a' && C <= 'z') || (C >= 'A' && C <= 'Z') || |
| 75 | (C >= '0' && C <= '9')) { |
| 76 | Result += C; |
| 77 | } else { |
| 78 | Result += '$'; |
| 79 | Result += char('0' + ((unsigned char)C >> 4)); |
| 80 | Result += char('0' + (C & 0xF)); |
| 81 | } |
| 82 | } |
| 83 | return Result; |
| 84 | } |
| 85 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 86 | // getID - Return a valid identifier for the specified value. Base it on |
| 87 | // the name of the identifier if possible, use a numbered value based on |
| 88 | // prefix otherwise. FPrefix is always prepended to the output identifier. |
| 89 | // |
| 90 | string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) { |
| 91 | string FP(FPrefix ? FPrefix : ""); // "Forced prefix" |
| 92 | if (V->hasName()) { |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 93 | return FP + getEscapedString(V->getName()); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 94 | } else { |
| 95 | assert(Table.getValSlot(V) != -1 && "Value not in value table!"); |
| 96 | return FP + string(Prefix) + itostr(Table.getValSlot(V)); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // getID Wrappers - Ensure consistent usage... |
| 101 | string getID(const Method *M) { return getID(M, "anon_method$"); } |
| 102 | string getID(const BasicBlock *BB) { |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 103 | return getID(BB, "LL", (".L$"+getID(BB->getParent())+"$").c_str()); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | unsigned getOperandMask(unsigned Opcode) { |
| 107 | switch (Opcode) { |
| 108 | case SUBcc: return 1 << 3; // Remove CC argument |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 109 | case BA: case BRZ: // Remove Arg #0, which is always null or xcc |
| 110 | case BRLEZ: case BRLZ: |
| 111 | case BRNZ: case BRGZ: |
| 112 | case BRGEZ: return 1 << 0; |
Vikram S. Adve | 2827d52 | 2001-10-20 21:33:50 +0000 | [diff] [blame] | 113 | // case RETURN: return 1 << 1; // Remove Arg #2 which is zero |
Chris Lattner | 39f501c | 2001-10-01 02:32:34 +0000 | [diff] [blame] | 114 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 115 | default: return 0; // By default, don't hack operands... |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 121 | void SparcAsmPrinter::printOperand(const MachineOperand &Op) { |
| 122 | switch (Op.getOperandType()) { |
| 123 | case MachineOperand::MO_VirtualRegister: |
| 124 | case MachineOperand::MO_CCRegister: |
| 125 | case MachineOperand::MO_MachineRegister: { |
| 126 | int RegNum = (int)Op.getAllocatedRegNum(); |
| 127 | |
| 128 | // ****this code is temporary till NULL Values are fixed |
| 129 | if (RegNum == 10000) { |
| 130 | Out << "<NULL VALUE>"; |
| 131 | } else { |
| 132 | Out << "%" << Target.getRegInfo().getUnifiedRegName(RegNum); |
| 133 | } |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | case MachineOperand::MO_PCRelativeDisp: { |
| 138 | const Value *Val = Op.getVRegValue(); |
| 139 | if (!Val) { |
| 140 | Out << "\t<*NULL Value*>"; |
| 141 | } else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) { |
| 142 | Out << getID(BB); |
Vikram S. Adve | 2827d52 | 2001-10-20 21:33:50 +0000 | [diff] [blame] | 143 | } else if (const Method *M = dyn_cast<const Method>(Val)) { |
| 144 | Out << getID(M); |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 145 | } else { |
| 146 | Out << "<unknown value=" << Val << ">"; |
| 147 | } |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | default: |
| 152 | Out << Op; // use dump field |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 158 | void SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) { |
| 159 | unsigned Opcode = MI->getOpCode(); |
| 160 | |
| 161 | if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG) |
| 162 | return; // IGNORE PHI NODES |
| 163 | |
| 164 | Out << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t"; |
| 165 | |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 166 | switch (Opcode) { // Some opcodes have special syntax... |
Vikram S. Adve | 578f99c | 2001-10-22 13:44:53 +0000 | [diff] [blame] | 167 | case JMPLCALL: |
| 168 | case JMPLRET: |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 169 | assert(MI->getNumOperands() == 3 && "Unexpected JMPL instr!"); |
| 170 | printOperand(MI->getOperand(0)); |
| 171 | Out << "+"; |
Vikram S. Adve | 4353444 | 2001-10-20 20:56:40 +0000 | [diff] [blame] | 172 | printOperand(MI->getOperand(1)); |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 173 | Out << ", "; |
Vikram S. Adve | 4353444 | 2001-10-20 20:56:40 +0000 | [diff] [blame] | 174 | printOperand(MI->getOperand(2)); |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 175 | Out << endl; |
| 176 | return; |
Vikram S. Adve | 2827d52 | 2001-10-20 21:33:50 +0000 | [diff] [blame] | 177 | |
| 178 | case RETURN: |
| 179 | assert(MI->getNumOperands() == 2 && "Unexpected RETURN instr!"); |
| 180 | printOperand(MI->getOperand(0)); |
| 181 | Out << "+"; |
| 182 | printOperand(MI->getOperand(1)); |
| 183 | Out << endl; |
| 184 | return; |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 185 | |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 186 | default: break; |
| 187 | } |
| 188 | |
Ruchira Sasanka | 7dcd612 | 2001-10-24 22:05:34 +0000 | [diff] [blame] | 189 | if( Target.getInstrInfo().isLoad(Opcode) ) { // if Load |
| 190 | assert(MI->getNumOperands() == 3 && "Loads must have 3 operands"); |
| 191 | Out << "["; |
| 192 | printOperand(MI->getOperand(0)); |
| 193 | |
| 194 | const MachineOperand& ImmOp = MI->getOperand(1); |
| 195 | if( ImmOp.getImmedValue() >= 0) |
| 196 | Out << "+"; |
| 197 | printOperand(ImmOp); |
| 198 | Out << "]"; |
| 199 | Out << ", "; |
| 200 | |
| 201 | printOperand(MI->getOperand(2)); |
| 202 | Out << endl; |
| 203 | return; |
| 204 | |
| 205 | } |
| 206 | |
| 207 | if( Target.getInstrInfo().isStore(Opcode) ) { // if Store |
| 208 | assert(MI->getNumOperands() == 3 && "Stores must have 3 operands"); |
| 209 | printOperand(MI->getOperand(0)); |
| 210 | Out << ", "; |
| 211 | Out << "["; |
| 212 | printOperand(MI->getOperand(1)); |
| 213 | |
| 214 | const MachineOperand& ImmOp = MI->getOperand(2); |
| 215 | if( ImmOp.getImmedValue() >= 0) |
| 216 | Out << "+"; |
| 217 | printOperand(ImmOp); |
| 218 | Out << "]"; |
| 219 | Out << endl; |
| 220 | return; |
| 221 | |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 226 | unsigned Mask = getOperandMask(Opcode); |
| 227 | |
| 228 | bool NeedComma = false; |
| 229 | for(unsigned OpNum = 0; OpNum < MI->getNumOperands(); ++OpNum) { |
| 230 | if ((1 << OpNum) & Mask) continue; // Ignore this operand? |
| 231 | |
| 232 | const MachineOperand &Op = MI->getOperand(OpNum); |
| 233 | if (NeedComma) Out << ", "; // Handle comma outputing |
| 234 | NeedComma = true; |
| 235 | |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 236 | printOperand(Op); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 237 | } |
| 238 | Out << endl; |
| 239 | } |
| 240 | |
| 241 | void SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) { |
| 242 | // Emit a label for the basic block |
| 243 | Out << getID(BB) << ":\n"; |
| 244 | |
| 245 | // Get the vector of machine instructions corresponding to this bb. |
| 246 | const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec(); |
| 247 | MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end(); |
| 248 | |
| 249 | // Loop over all of the instructions in the basic block... |
| 250 | for (; MII != MIE; ++MII) |
| 251 | emitMachineInst(*MII); |
| 252 | Out << "\n"; // Seperate BB's with newlines |
| 253 | } |
| 254 | |
| 255 | void SparcAsmPrinter::emitMethod(const Method *M) { |
| 256 | if (M->isExternal()) return; |
| 257 | |
| 258 | // Make sure the slot table has information about this method... |
| 259 | Table.incorporateMethod(M); |
| 260 | |
| 261 | string MethName = getID(M); |
| 262 | Out << "!****** Outputing Method: " << MethName << " ******\n"; |
| 263 | enterSection(Text); |
| 264 | Out << "\t.align 4\n\t.global\t" << MethName << "\n"; |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 265 | //Out << "\t.type\t" << MethName << ",#function\n"; |
Chris Lattner | f927bb4 | 2001-10-15 19:34:17 +0000 | [diff] [blame] | 266 | Out << "\t.type\t" << MethName << ", 2\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 267 | Out << MethName << ":\n"; |
| 268 | |
| 269 | // Output code for all of the basic blocks in the method... |
| 270 | for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 271 | emitBasicBlock(*I); |
| 272 | |
| 273 | // Output a .size directive so the debugger knows the extents of the function |
| 274 | Out << ".EndOf$" << MethName << ":\n\t.size " << MethName << ", .EndOf$" |
| 275 | << MethName << "-" << MethName << endl; |
| 276 | |
| 277 | // Put some spaces between the methods |
| 278 | Out << "\n\n"; |
| 279 | |
| 280 | // Forget all about M. |
| 281 | Table.purgeMethod(); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | void SparcAsmPrinter::emitModule(const Module *M) { |
| 286 | // TODO: Look for a filename annotation on M to emit a .file directive |
| 287 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 288 | emitMethod(*I); |
| 289 | } |
| 290 | |
| 291 | } // End anonymous namespace |
| 292 | |
| 293 | // |
| 294 | // emitAssembly - Output assembly language code (a .s file) for the specified |
| 295 | // method. The specified method must have been compiled before this may be |
| 296 | // used. |
| 297 | // |
Chris Lattner | ec0a95f | 2001-10-15 15:54:43 +0000 | [diff] [blame] | 298 | void UltraSparc::emitAssembly(const Module *M, ostream &Out) const { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 299 | SparcAsmPrinter Print(Out, M, *this); |
| 300 | } |