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" |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 13 | #include "llvm/Transforms/Linker.h" |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalVariable.h" |
| 16 | #include "llvm/GlobalValue.h" |
| 17 | #include "llvm/ConstPoolVals.h" |
| 18 | #include "llvm/DerivedTypes.h" |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 19 | #include "llvm/BasicBlock.h" |
| 20 | #include "llvm/Method.h" |
| 21 | #include "llvm/Module.h" |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 22 | #include "llvm/Support/HashExtras.h" |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 23 | #include "llvm/Support/StringExtras.h" |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 24 | #include <locale.h> |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 25 | |
| 26 | namespace { |
| 27 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 28 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 29 | class SparcAsmPrinter { |
| 30 | typedef hash_map<const Value*, int> ValIdMap; |
| 31 | typedef ValIdMap:: iterator ValIdMapIterator; |
| 32 | typedef ValIdMap::const_iterator ValIdMapConstIterator; |
| 33 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 34 | ostream &toAsm; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 35 | SlotCalculator Table; // map anonymous values to unique integer IDs |
| 36 | ValIdMap valToIdMap; // used for values not handled by SlotCalculator |
| 37 | const UltraSparc &Target; |
| 38 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 39 | enum Sections { |
| 40 | Unknown, |
| 41 | Text, |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 42 | ReadOnlyData, |
| 43 | InitRWData, |
| 44 | UninitRWData, |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 45 | } CurSection; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 46 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 47 | public: |
Chris Lattner | ec0a95f | 2001-10-15 15:54:43 +0000 | [diff] [blame] | 48 | inline SparcAsmPrinter(ostream &o, const Module *M, const UltraSparc &t) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 49 | : toAsm(o), Table(SlotCalculator(M, true)), Target(t), CurSection(Unknown) { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 50 | emitModule(M); |
| 51 | } |
| 52 | |
| 53 | private : |
| 54 | void emitModule(const Module *M); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 55 | void emitMethod(const Method *M); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 56 | void emitGlobalsAndConstants(const Module* module); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 57 | //void processMethodArgument(const MethodArgument *MA); |
| 58 | void emitBasicBlock(const BasicBlock *BB); |
| 59 | void emitMachineInst(const MachineInstr *MI); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 60 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 61 | void printGlobalVariable(const GlobalVariable* GV); |
| 62 | void printConstant(const ConstPoolVal* CV, string valID = string("")); |
| 63 | |
| 64 | unsigned int printOperands(const MachineInstr *MI, unsigned int opNum); |
| 65 | void printOneOperand(const MachineOperand &Op); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 66 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 67 | bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum); |
| 68 | bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum); |
| 69 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 70 | // enterSection - Use this method to enter a different section of the output |
| 71 | // executable. This is used to only output neccesary section transitions. |
| 72 | // |
| 73 | void enterSection(enum Sections S) { |
| 74 | if (S == CurSection) return; // Only switch section if neccesary |
| 75 | CurSection = S; |
| 76 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 77 | toAsm << "\n\t.section "; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 78 | switch (S) |
| 79 | { |
| 80 | default: assert(0 && "Bad section name!"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 81 | case Text: toAsm << "\".text\""; break; |
| 82 | case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break; |
| 83 | case InitRWData: toAsm << "\".data\",#alloc,#write"; break; |
| 84 | case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 85 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 86 | toAsm << "\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 89 | string getValidSymbolName(const string &S) { |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 90 | string Result; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 91 | |
| 92 | // Symbol names in Sparc assembly language have these rules: |
| 93 | // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }* |
| 94 | // (b) A name beginning in "." is treated as a local name. |
| 95 | // (c) Names beginning with "_" are reserved by ANSI C and shd not be used. |
| 96 | // |
| 97 | if (S[0] == '_' || isdigit(S[0])) |
| 98 | Result += "ll"; |
| 99 | |
| 100 | for (unsigned i = 0; i < S.size(); ++i) |
| 101 | { |
| 102 | char C = S[i]; |
| 103 | if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C)) |
| 104 | Result += C; |
| 105 | else |
| 106 | { |
| 107 | Result += '_'; |
| 108 | Result += char('0' + ((unsigned char)C >> 4)); |
| 109 | Result += char('0' + (C & 0xF)); |
| 110 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 111 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 112 | return Result; |
| 113 | } |
| 114 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 115 | // getID - Return a valid identifier for the specified value. Base it on |
| 116 | // the name of the identifier if possible, use a numbered value based on |
| 117 | // prefix otherwise. FPrefix is always prepended to the output identifier. |
| 118 | // |
| 119 | string getID(const Value *V, const char *Prefix, const char *FPrefix = 0) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 120 | string Result; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 121 | string FP(FPrefix ? FPrefix : ""); // "Forced prefix" |
| 122 | if (V->hasName()) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 123 | Result = FP + V->getName(); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 124 | } else { |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 125 | int valId = Table.getValSlot(V); |
| 126 | if (valId == -1) { |
| 127 | ValIdMapConstIterator I = valToIdMap.find(V); |
| 128 | valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size()) |
| 129 | : (*I).second; |
| 130 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 131 | Result = FP + string(Prefix) + itostr(valId); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 132 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 133 | return getValidSymbolName(Result); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 134 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 135 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 136 | // getID Wrappers - Ensure consistent usage... |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 137 | string getID(const Module *M) { |
| 138 | return getID(M, "LLVMModule_"); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 139 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 140 | string getID(const Method *M) { |
| 141 | return getID(M, "LLVMMethod_"); |
| 142 | } |
| 143 | string getID(const BasicBlock *BB) { |
| 144 | return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str()); |
| 145 | } |
| 146 | string getID(const GlobalVariable *GV) { |
| 147 | return getID(GV, "LLVMGlobal_", ".G_"); |
| 148 | } |
| 149 | string getID(const ConstPoolVal *CV) { |
| 150 | return getID(CV, "LLVMConst_", ".C_"); |
| 151 | } |
| 152 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 153 | unsigned getOperandMask(unsigned Opcode) { |
| 154 | switch (Opcode) { |
| 155 | case SUBcc: return 1 << 3; // Remove CC argument |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 156 | case BA: case BRZ: // Remove Arg #0, which is always null or xcc |
| 157 | case BRLEZ: case BRLZ: |
| 158 | case BRNZ: case BRGZ: |
| 159 | case BRGEZ: return 1 << 0; |
Chris Lattner | 39f501c | 2001-10-01 02:32:34 +0000 | [diff] [blame] | 160 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 161 | default: return 0; // By default, don't hack operands... |
| 162 | } |
| 163 | } |
| 164 | }; |
| 165 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 166 | inline bool |
| 167 | SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI, |
| 168 | unsigned int opNum) { |
| 169 | switch (MI->getOpCode()) { |
| 170 | case JMPLCALL: |
| 171 | case JMPLRET: return (opNum == 0); |
| 172 | default: return false; |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
| 176 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 177 | inline bool |
| 178 | SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI, |
| 179 | unsigned int opNum) { |
| 180 | if (Target.getInstrInfo().isLoad(MI->getOpCode())) |
| 181 | return (opNum == 0); |
| 182 | else if (Target.getInstrInfo().isStore(MI->getOpCode())) |
| 183 | return (opNum == 1); |
| 184 | else |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | #define PrintOp1PlusOp2(Op1, Op2) \ |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 190 | printOneOperand(Op1); toAsm << "+"; printOneOperand(Op2); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 191 | |
| 192 | |
| 193 | unsigned int |
| 194 | SparcAsmPrinter::printOperands(const MachineInstr *MI, |
| 195 | unsigned int opNum) |
| 196 | { |
| 197 | const MachineOperand& Op = MI->getOperand(opNum); |
| 198 | |
| 199 | if (OpIsBranchTargetLabel(MI, opNum)) |
| 200 | { |
| 201 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
| 202 | return 2; |
| 203 | } |
| 204 | else if (OpIsMemoryAddressBase(MI, opNum)) |
| 205 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 206 | toAsm << "["; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 207 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 208 | toAsm << "]"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 209 | return 2; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | printOneOperand(Op); |
| 214 | return 1; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | void |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 220 | SparcAsmPrinter::printOneOperand(const MachineOperand &op) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 221 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 222 | switch (op.getOperandType()) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 223 | { |
| 224 | case MachineOperand::MO_VirtualRegister: |
| 225 | case MachineOperand::MO_CCRegister: |
| 226 | case MachineOperand::MO_MachineRegister: |
| 227 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 228 | int RegNum = (int)op.getAllocatedRegNum(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 229 | |
| 230 | // ****this code is temporary till NULL Values are fixed |
| 231 | if (RegNum == 10000) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 232 | toAsm << "<NULL VALUE>"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 233 | } else { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 234 | toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 235 | } |
| 236 | break; |
| 237 | } |
| 238 | |
| 239 | case MachineOperand::MO_PCRelativeDisp: |
| 240 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 241 | const Value *Val = op.getVRegValue(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 242 | if (!Val) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 243 | toAsm << "\t<*NULL Value*>"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 244 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 245 | toAsm << getID(BB); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 246 | else if (const Method *M = dyn_cast<const Method>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 247 | toAsm << getID(M); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 248 | else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 249 | toAsm << getID(GV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 250 | else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 251 | toAsm << getID(CV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 252 | else |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 253 | toAsm << "<unknown value=" << Val << ">"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 254 | break; |
| 255 | } |
| 256 | |
| 257 | case MachineOperand::MO_SignExtendedImmed: |
| 258 | case MachineOperand::MO_UnextendedImmed: |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 259 | toAsm << op.getImmedValue(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
| 262 | default: |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 263 | toAsm << op; // use dump field |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | void |
| 270 | SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) |
| 271 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 272 | unsigned Opcode = MI->getOpCode(); |
| 273 | |
| 274 | if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG) |
| 275 | return; // IGNORE PHI NODES |
| 276 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 277 | toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 278 | |
| 279 | unsigned Mask = getOperandMask(Opcode); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 280 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 281 | bool NeedComma = false; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 282 | unsigned N = 1; |
| 283 | for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N) |
| 284 | if (! ((1 << OpNum) & Mask)) { // Ignore this operand? |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 285 | if (NeedComma) toAsm << ", "; // Handle comma outputing |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 286 | NeedComma = true; |
| 287 | N = printOperands(MI, OpNum); |
| 288 | } |
| 289 | else |
| 290 | N = 1; |
| 291 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 292 | toAsm << endl; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 295 | void |
| 296 | SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) |
| 297 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 298 | // Emit a label for the basic block |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 299 | toAsm << getID(BB) << ":\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 300 | |
| 301 | // Get the vector of machine instructions corresponding to this bb. |
| 302 | const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec(); |
| 303 | MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end(); |
| 304 | |
| 305 | // Loop over all of the instructions in the basic block... |
| 306 | for (; MII != MIE; ++MII) |
| 307 | emitMachineInst(*MII); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 308 | toAsm << "\n"; // Seperate BB's with newlines |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 311 | void |
| 312 | SparcAsmPrinter::emitMethod(const Method *M) |
| 313 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 314 | if (M->isExternal()) return; |
| 315 | |
| 316 | // Make sure the slot table has information about this method... |
| 317 | Table.incorporateMethod(M); |
| 318 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 319 | string methName = getID(M); |
| 320 | toAsm << "!****** Outputing Method: " << methName << " ******\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 321 | enterSection(Text); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 322 | toAsm << "\t.align\t4\n\t.global\t" << methName << "\n"; |
| 323 | //toAsm << "\t.type\t" << methName << ",#function\n"; |
| 324 | toAsm << "\t.type\t" << methName << ", 2\n"; |
| 325 | toAsm << methName << ":\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 326 | |
| 327 | // Output code for all of the basic blocks in the method... |
| 328 | for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 329 | emitBasicBlock(*I); |
| 330 | |
| 331 | // Output a .size directive so the debugger knows the extents of the function |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 332 | toAsm << ".EndOf_" << methName << ":\n\t.size " |
| 333 | << methName << ", .EndOf_" |
| 334 | << methName << "-" << methName << endl; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 335 | |
| 336 | // Put some spaces between the methods |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 337 | toAsm << "\n\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 338 | |
| 339 | // Forget all about M. |
| 340 | Table.purgeMethod(); |
| 341 | } |
| 342 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 343 | inline bool |
| 344 | ArrayTypeIsString(ArrayType* arrayType) |
| 345 | { |
| 346 | return (arrayType->getElementType() == Type::UByteTy || |
| 347 | arrayType->getElementType() == Type::SByteTy); |
| 348 | } |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 349 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 350 | inline const string TypeToDataDirective(const Type* type) |
| 351 | { |
| 352 | switch(type->getPrimitiveID()) |
| 353 | { |
| 354 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 355 | return ".byte"; |
| 356 | case Type::UShortTyID: case Type::ShortTyID: |
| 357 | return ".half"; |
| 358 | case Type::UIntTyID: case Type::IntTyID: |
| 359 | return ".word"; |
| 360 | case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID: |
| 361 | return ".xword"; |
| 362 | case Type::FloatTyID: |
| 363 | return ".single"; |
| 364 | case Type::DoubleTyID: |
| 365 | return ".double"; |
| 366 | case Type::ArrayTyID: |
| 367 | if (ArrayTypeIsString((ArrayType*) type)) |
| 368 | return ".ascii"; |
| 369 | else |
| 370 | return "<InvaliDataTypeForPrinting>"; |
| 371 | default: |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 372 | return "<InvaliDataTypeForPrinting>"; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 373 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | inline unsigned int ConstantToSize(const ConstPoolVal* CV, |
| 377 | const TargetMachine& target) { |
| 378 | if (ConstPoolArray* AV = dyn_cast<ConstPoolArray>(CV)) |
| 379 | if (ArrayTypeIsString((ArrayType*) CV->getType())) |
| 380 | return 1 + AV->getNumOperands(); |
| 381 | |
| 382 | return target.findOptimalStorageSize(CV->getType()); |
| 383 | } |
| 384 | |
| 385 | |
| 386 | inline |
| 387 | unsigned int TypeToSize(const Type* type, const TargetMachine& target) |
| 388 | { |
| 389 | return target.findOptimalStorageSize(type); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | inline unsigned int |
| 394 | TypeToAlignment(const Type* type, const TargetMachine& target) |
| 395 | { |
| 396 | if (type->getPrimitiveID() == Type::ArrayTyID && |
| 397 | ArrayTypeIsString((ArrayType*) type)) |
| 398 | return target.findOptimalStorageSize(Type::LongTy); |
| 399 | |
| 400 | return target.findOptimalStorageSize(type); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | void |
| 405 | SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID) |
| 406 | { |
| 407 | if (valID.length() == 0) |
| 408 | valID = getID(CV); |
| 409 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 410 | assert(CV->getType() != Type::VoidTy && |
| 411 | CV->getType() != Type::TypeTy && |
| 412 | CV->getType() != Type::LabelTy && |
| 413 | "Unexpected type for ConstPoolVal"); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 414 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 415 | toAsm << "\t.align\t" << TypeToAlignment(CV->getType(), Target) |
| 416 | << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 417 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 418 | toAsm << valID << ":" << endl; |
| 419 | |
| 420 | toAsm << "\t" |
| 421 | << TypeToDataDirective(CV->getType()) << "\t"; |
| 422 | |
| 423 | if (ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV)) |
Chris Lattner | 24d3a8b | 2001-10-29 13:39:38 +0000 | [diff] [blame] | 424 | if (isStringCompatible(CPA)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 425 | { |
| 426 | toAsm << getAsCString(CPA) << endl; |
| 427 | return; |
| 428 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 429 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 430 | if (CV->getType()->isPrimitiveType()) |
| 431 | { |
| 432 | if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy) |
| 433 | toAsm << "0r"; // FP constants must have this prefix |
| 434 | toAsm << CV->getStrValue() << endl; |
| 435 | } |
| 436 | else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV)) |
| 437 | { |
| 438 | if (! CPP->isNullValue()) |
| 439 | assert(0 && "Cannot yet print non-null pointer constants to assembly"); |
| 440 | else |
| 441 | toAsm << (void*) NULL; |
| 442 | } |
| 443 | else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV)) |
| 444 | { |
| 445 | assert(0 && "Cannot yet initialize pointer refs in assembly"); |
| 446 | } |
| 447 | else |
| 448 | { |
| 449 | assert(0 && "Cannot yet print non-primitive constants to assembly"); |
| 450 | // toAsm << CV->getStrValue() << endl; |
| 451 | } |
| 452 | |
| 453 | toAsm << "\t.type" << "\t" << valID << ",#object" << endl; |
| 454 | toAsm << "\t.size" << "\t" << valID << "," |
| 455 | << ConstantToSize(CV, Target) << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | |
| 459 | void |
| 460 | SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) |
| 461 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 462 | toAsm << "\t.global\t" << getID(GV) << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 463 | |
| 464 | if (GV->hasInitializer()) |
| 465 | printConstant(GV->getInitializer(), getID(GV)); |
| 466 | else { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 467 | toAsm << "\t.align" << TypeToAlignment(GV->getType()->getValueType(), Target) |
| 468 | << getID(GV) << ":" << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 469 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 470 | toAsm << "\t.type" << "\t" << getID(GV) << ",#object" << endl; |
| 471 | toAsm << "\t.size" << "\t" << getID(GV) << "," |
| 472 | << TypeToSize(GV->getType()->getValueType(), Target) |
| 473 | << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | |
| 478 | static void |
| 479 | FoldConstPools(const Module *M, |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 480 | hash_set<const ConstPoolVal*>& moduleConstPool) |
| 481 | { |
| 482 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 483 | if (! (*I)->isExternal()) |
| 484 | { |
| 485 | const hash_set<const ConstPoolVal*>& pool = |
| 486 | MachineCodeForMethod::get(*I).getConstantPoolValues(); |
| 487 | moduleConstPool.insert(pool.begin(), pool.end()); |
| 488 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | |
| 492 | void |
| 493 | SparcAsmPrinter::emitGlobalsAndConstants(const Module *M) |
| 494 | { |
| 495 | // First, get the constants there were marked by the code generator for |
| 496 | // inclusion in the assembly code data area and fold them all into a |
| 497 | // single constant pool since there may be lots of duplicates. Also, |
| 498 | // lets force these constants into the slot table so that we can get |
| 499 | // unique names for unnamed constants also. |
| 500 | // |
| 501 | hash_set<const ConstPoolVal*> moduleConstPool; |
| 502 | FoldConstPools(M, moduleConstPool); |
| 503 | |
| 504 | // Now, emit the three data sections separately; the cost of I/O should |
| 505 | // make up for the cost of extra passes over the globals list! |
| 506 | // |
| 507 | // Read-only data section (implies initialized) |
| 508 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 509 | { |
| 510 | const GlobalVariable* GV = *GI; |
| 511 | if (GV->hasInitializer() && GV->isConstant()) |
| 512 | { |
| 513 | if (GI == M->gbegin()) |
| 514 | enterSection(ReadOnlyData); |
| 515 | printGlobalVariable(GV); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(), |
| 520 | E = moduleConstPool.end(); I != E; ++I) |
| 521 | printConstant(*I); |
| 522 | |
| 523 | // Initialized read-write data section |
| 524 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 525 | { |
| 526 | const GlobalVariable* GV = *GI; |
| 527 | if (GV->hasInitializer() && ! GV->isConstant()) |
| 528 | { |
| 529 | if (GI == M->gbegin()) |
| 530 | enterSection(InitRWData); |
| 531 | printGlobalVariable(GV); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // Uninitialized read-write data section |
| 536 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 537 | { |
| 538 | const GlobalVariable* GV = *GI; |
| 539 | if (! GV->hasInitializer()) |
| 540 | { |
| 541 | if (GI == M->gbegin()) |
| 542 | enterSection(UninitRWData); |
| 543 | printGlobalVariable(GV); |
| 544 | } |
| 545 | } |
| 546 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 547 | toAsm << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | |
| 551 | void |
| 552 | SparcAsmPrinter::emitModule(const Module *M) |
| 553 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 554 | // TODO: Look for a filename annotation on M to emit a .file directive |
| 555 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 556 | emitMethod(*I); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 557 | |
| 558 | emitGlobalsAndConstants(M); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | } // End anonymous namespace |
| 562 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 563 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 564 | // |
| 565 | // emitAssembly - Output assembly language code (a .s file) for the specified |
| 566 | // method. The specified method must have been compiled before this may be |
| 567 | // used. |
| 568 | // |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 569 | void |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 570 | UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 571 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame^] | 572 | SparcAsmPrinter Print(toAsm, M, *this); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 573 | } |