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 | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 61 | void printGlobalVariable( const GlobalVariable* GV); |
| 62 | void printSingleConstant( const ConstPoolVal* CV); |
| 63 | void printConstantValueOnly(const ConstPoolVal* CV); |
| 64 | void printConstant( const ConstPoolVal* CV, string valID=string("")); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned int printOperands(const MachineInstr *MI, unsigned int opNum); |
| 67 | void printOneOperand(const MachineOperand &Op); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 68 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 69 | bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum); |
| 70 | bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum); |
| 71 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 72 | // enterSection - Use this method to enter a different section of the output |
| 73 | // executable. This is used to only output neccesary section transitions. |
| 74 | // |
| 75 | void enterSection(enum Sections S) { |
| 76 | if (S == CurSection) return; // Only switch section if neccesary |
| 77 | CurSection = S; |
| 78 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 79 | toAsm << "\n\t.section "; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 80 | switch (S) |
| 81 | { |
| 82 | default: assert(0 && "Bad section name!"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 83 | case Text: toAsm << "\".text\""; break; |
| 84 | case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break; |
| 85 | case InitRWData: toAsm << "\".data\",#alloc,#write"; break; |
| 86 | case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 87 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 88 | toAsm << "\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 91 | string getValidSymbolName(const string &S) { |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 92 | string Result; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 93 | |
| 94 | // Symbol names in Sparc assembly language have these rules: |
| 95 | // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }* |
| 96 | // (b) A name beginning in "." is treated as a local name. |
| 97 | // (c) Names beginning with "_" are reserved by ANSI C and shd not be used. |
| 98 | // |
| 99 | if (S[0] == '_' || isdigit(S[0])) |
| 100 | Result += "ll"; |
| 101 | |
| 102 | for (unsigned i = 0; i < S.size(); ++i) |
| 103 | { |
| 104 | char C = S[i]; |
| 105 | if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C)) |
| 106 | Result += C; |
| 107 | else |
| 108 | { |
| 109 | Result += '_'; |
| 110 | Result += char('0' + ((unsigned char)C >> 4)); |
| 111 | Result += char('0' + (C & 0xF)); |
| 112 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 113 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 114 | return Result; |
| 115 | } |
| 116 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 117 | // getID - Return a valid identifier for the specified value. Base it on |
| 118 | // the name of the identifier if possible, use a numbered value based on |
| 119 | // prefix otherwise. FPrefix is always prepended to the output identifier. |
| 120 | // |
| 121 | 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] | 122 | string Result; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 123 | string FP(FPrefix ? FPrefix : ""); // "Forced prefix" |
| 124 | if (V->hasName()) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 125 | Result = FP + V->getName(); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 126 | } else { |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 127 | int valId = Table.getValSlot(V); |
| 128 | if (valId == -1) { |
| 129 | ValIdMapConstIterator I = valToIdMap.find(V); |
| 130 | valId = (I == valToIdMap.end())? (valToIdMap[V] = valToIdMap.size()) |
| 131 | : (*I).second; |
| 132 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 133 | Result = FP + string(Prefix) + itostr(valId); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 134 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 135 | return getValidSymbolName(Result); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 136 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 137 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 138 | // getID Wrappers - Ensure consistent usage... |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 139 | string getID(const Module *M) { |
| 140 | return getID(M, "LLVMModule_"); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 141 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 142 | string getID(const Method *M) { |
| 143 | return getID(M, "LLVMMethod_"); |
| 144 | } |
| 145 | string getID(const BasicBlock *BB) { |
| 146 | return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str()); |
| 147 | } |
| 148 | string getID(const GlobalVariable *GV) { |
| 149 | return getID(GV, "LLVMGlobal_", ".G_"); |
| 150 | } |
| 151 | string getID(const ConstPoolVal *CV) { |
| 152 | return getID(CV, "LLVMConst_", ".C_"); |
| 153 | } |
| 154 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 155 | unsigned getOperandMask(unsigned Opcode) { |
| 156 | switch (Opcode) { |
| 157 | case SUBcc: return 1 << 3; // Remove CC argument |
Vikram S. Adve | 998cf0d | 2001-11-11 23:11:36 +0000 | [diff] [blame] | 158 | case BA: return 1 << 0; // Remove Arg #0, which is always null or xcc |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 159 | default: return 0; // By default, don't hack operands... |
| 160 | } |
| 161 | } |
| 162 | }; |
| 163 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 164 | inline bool |
| 165 | SparcAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI, |
| 166 | unsigned int opNum) { |
| 167 | switch (MI->getOpCode()) { |
| 168 | case JMPLCALL: |
| 169 | case JMPLRET: return (opNum == 0); |
| 170 | default: return false; |
Chris Lattner | c28f6d6 | 2001-10-15 19:21:31 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 175 | inline bool |
| 176 | SparcAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI, |
| 177 | unsigned int opNum) { |
| 178 | if (Target.getInstrInfo().isLoad(MI->getOpCode())) |
| 179 | return (opNum == 0); |
| 180 | else if (Target.getInstrInfo().isStore(MI->getOpCode())) |
| 181 | return (opNum == 1); |
| 182 | else |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | #define PrintOp1PlusOp2(Op1, Op2) \ |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 188 | printOneOperand(Op1); \ |
| 189 | toAsm << "+"; \ |
| 190 | printOneOperand(Op2); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 191 | |
| 192 | unsigned int |
| 193 | SparcAsmPrinter::printOperands(const MachineInstr *MI, |
| 194 | unsigned int opNum) |
| 195 | { |
| 196 | const MachineOperand& Op = MI->getOperand(opNum); |
| 197 | |
| 198 | if (OpIsBranchTargetLabel(MI, opNum)) |
| 199 | { |
| 200 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
| 201 | return 2; |
| 202 | } |
| 203 | else if (OpIsMemoryAddressBase(MI, opNum)) |
| 204 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 205 | toAsm << "["; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 206 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 207 | toAsm << "]"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 208 | return 2; |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | printOneOperand(Op); |
| 213 | return 1; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | |
| 218 | void |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 219 | SparcAsmPrinter::printOneOperand(const MachineOperand &op) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 220 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 221 | switch (op.getOperandType()) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 222 | { |
| 223 | case MachineOperand::MO_VirtualRegister: |
| 224 | case MachineOperand::MO_CCRegister: |
| 225 | case MachineOperand::MO_MachineRegister: |
| 226 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 227 | int RegNum = (int)op.getAllocatedRegNum(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 228 | |
| 229 | // ****this code is temporary till NULL Values are fixed |
Ruchira Sasanka | 5f98aca | 2001-11-13 23:08:19 +0000 | [diff] [blame] | 230 | if (RegNum == Target.getRegInfo().getInvalidRegNum()) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 231 | toAsm << "<NULL VALUE>"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 232 | } else { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 233 | toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 234 | } |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | case MachineOperand::MO_PCRelativeDisp: |
| 239 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 240 | const Value *Val = op.getVRegValue(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 241 | if (!Val) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 242 | toAsm << "\t<*NULL Value*>"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 243 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 244 | toAsm << getID(BB); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 245 | else if (const Method *M = dyn_cast<const Method>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 246 | toAsm << getID(M); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 247 | else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 248 | toAsm << getID(GV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 249 | else if (const ConstPoolVal *CV = dyn_cast<const ConstPoolVal>(Val)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 250 | toAsm << getID(CV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 251 | else |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 252 | toAsm << "<unknown value=" << Val << ">"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | |
| 256 | case MachineOperand::MO_SignExtendedImmed: |
| 257 | case MachineOperand::MO_UnextendedImmed: |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 258 | toAsm << op.getImmedValue(); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 259 | break; |
| 260 | |
| 261 | default: |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 262 | toAsm << op; // use dump field |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | void |
| 269 | SparcAsmPrinter::emitMachineInst(const MachineInstr *MI) |
| 270 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 271 | unsigned Opcode = MI->getOpCode(); |
| 272 | |
| 273 | if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG) |
| 274 | return; // IGNORE PHI NODES |
| 275 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 276 | toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 277 | |
| 278 | unsigned Mask = getOperandMask(Opcode); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 279 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 280 | bool NeedComma = false; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 281 | unsigned N = 1; |
| 282 | for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N) |
| 283 | if (! ((1 << OpNum) & Mask)) { // Ignore this operand? |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 284 | if (NeedComma) toAsm << ", "; // Handle comma outputing |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 285 | NeedComma = true; |
| 286 | N = printOperands(MI, OpNum); |
| 287 | } |
| 288 | else |
| 289 | N = 1; |
| 290 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 291 | toAsm << endl; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 294 | void |
| 295 | SparcAsmPrinter::emitBasicBlock(const BasicBlock *BB) |
| 296 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 297 | // Emit a label for the basic block |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 298 | toAsm << getID(BB) << ":\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 299 | |
| 300 | // Get the vector of machine instructions corresponding to this bb. |
| 301 | const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec(); |
| 302 | MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end(); |
| 303 | |
| 304 | // Loop over all of the instructions in the basic block... |
| 305 | for (; MII != MIE; ++MII) |
| 306 | emitMachineInst(*MII); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 307 | toAsm << "\n"; // Seperate BB's with newlines |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 310 | void |
| 311 | SparcAsmPrinter::emitMethod(const Method *M) |
| 312 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 313 | if (M->isExternal()) return; |
| 314 | |
| 315 | // Make sure the slot table has information about this method... |
| 316 | Table.incorporateMethod(M); |
| 317 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 318 | string methName = getID(M); |
| 319 | toAsm << "!****** Outputing Method: " << methName << " ******\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 320 | enterSection(Text); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 321 | toAsm << "\t.align\t4\n\t.global\t" << methName << "\n"; |
| 322 | //toAsm << "\t.type\t" << methName << ",#function\n"; |
| 323 | toAsm << "\t.type\t" << methName << ", 2\n"; |
| 324 | toAsm << methName << ":\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 325 | |
| 326 | // Output code for all of the basic blocks in the method... |
| 327 | for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 328 | emitBasicBlock(*I); |
| 329 | |
| 330 | // 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] | 331 | toAsm << ".EndOf_" << methName << ":\n\t.size " |
| 332 | << methName << ", .EndOf_" |
| 333 | << methName << "-" << methName << endl; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 334 | |
| 335 | // Put some spaces between the methods |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 336 | toAsm << "\n\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 337 | |
| 338 | // Forget all about M. |
| 339 | Table.purgeMethod(); |
| 340 | } |
| 341 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 342 | inline bool |
| 343 | ArrayTypeIsString(ArrayType* arrayType) |
| 344 | { |
| 345 | return (arrayType->getElementType() == Type::UByteTy || |
| 346 | arrayType->getElementType() == Type::SByteTy); |
| 347 | } |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 348 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 349 | inline const string |
| 350 | TypeToDataDirective(const Type* type) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 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 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 376 | // Get the size of the constant for the given target. |
| 377 | // If this is an unsized array, return 0. |
| 378 | // |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 379 | inline unsigned int |
| 380 | ConstantToSize(const ConstPoolVal* CV, const TargetMachine& target) |
| 381 | { |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 382 | if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV)) |
| 383 | { |
| 384 | ArrayType *aty = cast<ArrayType>(CPA->getType()); |
| 385 | if (ArrayTypeIsString(aty)) |
| 386 | return 1 + CPA->getNumOperands(); |
| 387 | else if (! aty->isSized()) |
| 388 | return 0; |
| 389 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 390 | |
| 391 | return target.findOptimalStorageSize(CV->getType()); |
| 392 | } |
| 393 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 394 | inline |
| 395 | unsigned int TypeToSize(const Type* type, const TargetMachine& target) |
| 396 | { |
| 397 | return target.findOptimalStorageSize(type); |
| 398 | } |
| 399 | |
| 400 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 401 | // Align data larger than one L1 cache line on L1 cache line boundaries. |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 402 | // Align all smaller data on the next higher 2^x boundary (4, 8, ...). |
| 403 | // |
| 404 | inline unsigned int |
| 405 | SizeToAlignment(unsigned int size, const TargetMachine& target) |
| 406 | { |
| 407 | unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); |
| 408 | if (size > (unsigned) cacheLineSize / 2) |
| 409 | return cacheLineSize; |
| 410 | else |
| 411 | for (unsigned sz=1; /*no condition*/; sz *= 2) |
| 412 | if (sz >= size) |
| 413 | return sz; |
| 414 | } |
| 415 | |
| 416 | // Get the size of the type and then use SizeToAlignment. |
| 417 | // If this is an unsized array, just return the L1 cache line size |
| 418 | // (viz., the default behavior for large global objects). |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 419 | // |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 420 | inline unsigned int |
| 421 | TypeToAlignment(const Type* type, const TargetMachine& target) |
| 422 | { |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 423 | if (ArrayType* aty = dyn_cast<ArrayType>(type)) |
| 424 | if (! aty->isSized()) |
| 425 | return target.getCacheInfo().getCacheLineSize(1); |
| 426 | |
| 427 | return SizeToAlignment(target.findOptimalStorageSize(type), target); |
| 428 | } |
| 429 | |
| 430 | // Get the size of the constant and then use SizeToAlignment. |
| 431 | // Handles strings as a special case; |
| 432 | inline unsigned int |
| 433 | ConstantToAlignment(const ConstPoolVal* CV, const TargetMachine& target) |
| 434 | { |
| 435 | unsigned int constantSize; |
| 436 | if (ConstPoolArray* CPA = dyn_cast<ConstPoolArray>(CV)) |
| 437 | if (ArrayTypeIsString(cast<ArrayType>(CPA->getType()))) |
| 438 | return SizeToAlignment(1 + CPA->getNumOperands(), target); |
| 439 | |
| 440 | return TypeToAlignment(CV->getType(), target); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 444 | // Print a single constant value. |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 445 | void |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 446 | SparcAsmPrinter::printSingleConstant(const ConstPoolVal* CV) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 447 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 448 | assert(CV->getType() != Type::VoidTy && |
| 449 | CV->getType() != Type::TypeTy && |
| 450 | CV->getType() != Type::LabelTy && |
| 451 | "Unexpected type for ConstPoolVal"); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 452 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 453 | assert((! isa<ConstPoolArray>( CV) && ! isa<ConstPoolStruct>(CV)) |
| 454 | && "Collective types should be handled outside this function"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 455 | |
| 456 | toAsm << "\t" |
| 457 | << TypeToDataDirective(CV->getType()) << "\t"; |
| 458 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 459 | if (CV->getType()->isPrimitiveType()) |
| 460 | { |
| 461 | if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy) |
| 462 | toAsm << "0r"; // FP constants must have this prefix |
| 463 | toAsm << CV->getStrValue() << endl; |
| 464 | } |
| 465 | else if (ConstPoolPointer* CPP = dyn_cast<ConstPoolPointer>(CV)) |
| 466 | { |
| 467 | if (! CPP->isNullValue()) |
| 468 | assert(0 && "Cannot yet print non-null pointer constants to assembly"); |
| 469 | else |
Vikram S. Adve | ffbba0f | 2001-11-08 14:29:57 +0000 | [diff] [blame] | 470 | toAsm << (void*) NULL << endl; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 471 | } |
| 472 | else if (ConstPoolPointerRef* CPRef = dyn_cast<ConstPoolPointerRef>(CV)) |
| 473 | { |
| 474 | assert(0 && "Cannot yet initialize pointer refs in assembly"); |
| 475 | } |
| 476 | else |
| 477 | { |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 478 | assert(0 && "Unknown elementary type for constant"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 479 | } |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 482 | // Print a constant value or values (it may be an aggregate). |
| 483 | // Uses printSingleConstant() to print each individual value. |
| 484 | void |
| 485 | SparcAsmPrinter::printConstantValueOnly(const ConstPoolVal* CV) |
| 486 | { |
| 487 | ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV); |
| 488 | |
| 489 | if (CPA && isStringCompatible(CPA)) |
| 490 | { // print the string alone and return |
| 491 | toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl; |
| 492 | } |
| 493 | else if (CPA) |
| 494 | { // Not a string. Print the values in successive locations |
| 495 | const vector<Use>& constValues = CPA->getValues(); |
| 496 | for (unsigned i=1; i < constValues.size(); i++) |
| 497 | this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get())); |
| 498 | } |
| 499 | else if (ConstPoolStruct *CPS = dyn_cast<ConstPoolStruct>(CV)) |
| 500 | { // Print the fields in successive locations |
| 501 | const vector<Use>& constValues = CPS->getValues(); |
| 502 | for (unsigned i=1; i < constValues.size(); i++) |
| 503 | this->printConstantValueOnly(cast<ConstPoolVal>(constValues[i].get())); |
| 504 | } |
| 505 | else |
| 506 | this->printSingleConstant(CV); |
| 507 | } |
| 508 | |
| 509 | // Print a constant (which may be an aggregate) prefixed by all the |
| 510 | // appropriate directives. Uses printConstantValueOnly() to print the |
| 511 | // value or values. |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 512 | void |
| 513 | SparcAsmPrinter::printConstant(const ConstPoolVal* CV, string valID) |
| 514 | { |
| 515 | if (valID.length() == 0) |
| 516 | valID = getID(CV); |
| 517 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 518 | toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 519 | << endl; |
| 520 | |
| 521 | // Print .size and .type only if it is not a string. |
| 522 | ConstPoolArray *CPA = dyn_cast<ConstPoolArray>(CV); |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 523 | if (CPA && isStringCompatible(CPA)) |
| 524 | { // print it as a string and return |
| 525 | toAsm << valID << ":" << endl; |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 526 | toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << endl; |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 527 | return; |
| 528 | } |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 529 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 530 | toAsm << "\t.type" << "\t" << valID << ",#object" << endl; |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 531 | |
| 532 | unsigned int constSize = ConstantToSize(CV, Target); |
| 533 | if (constSize) |
| 534 | toAsm << "\t.size" << "\t" << valID << "," |
| 535 | << constSize << endl; |
| 536 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 537 | toAsm << valID << ":" << endl; |
| 538 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 539 | this->printConstantValueOnly(CV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | void |
| 544 | SparcAsmPrinter::printGlobalVariable(const GlobalVariable* GV) |
| 545 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 546 | toAsm << "\t.global\t" << getID(GV) << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 547 | |
| 548 | if (GV->hasInitializer()) |
| 549 | printConstant(GV->getInitializer(), getID(GV)); |
| 550 | else { |
Vikram S. Adve | ffbba0f | 2001-11-08 14:29:57 +0000 | [diff] [blame] | 551 | toAsm << "\t.align\t" |
| 552 | << TypeToAlignment(GV->getType()->getValueType(), Target) << endl; |
| 553 | toAsm << "\t.type\t" << getID(GV) << ",#object" << endl; |
| 554 | toAsm << "\t.reserve\t" << getID(GV) << "," |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 555 | << TypeToSize(GV->getType()->getValueType(), Target) |
| 556 | << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | static void |
| 562 | FoldConstPools(const Module *M, |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 563 | hash_set<const ConstPoolVal*>& moduleConstPool) |
| 564 | { |
| 565 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 566 | if (! (*I)->isExternal()) |
| 567 | { |
| 568 | const hash_set<const ConstPoolVal*>& pool = |
| 569 | MachineCodeForMethod::get(*I).getConstantPoolValues(); |
| 570 | moduleConstPool.insert(pool.begin(), pool.end()); |
| 571 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | |
| 575 | void |
| 576 | SparcAsmPrinter::emitGlobalsAndConstants(const Module *M) |
| 577 | { |
| 578 | // First, get the constants there were marked by the code generator for |
| 579 | // inclusion in the assembly code data area and fold them all into a |
| 580 | // single constant pool since there may be lots of duplicates. Also, |
| 581 | // lets force these constants into the slot table so that we can get |
| 582 | // unique names for unnamed constants also. |
| 583 | // |
| 584 | hash_set<const ConstPoolVal*> moduleConstPool; |
| 585 | FoldConstPools(M, moduleConstPool); |
| 586 | |
| 587 | // Now, emit the three data sections separately; the cost of I/O should |
| 588 | // make up for the cost of extra passes over the globals list! |
| 589 | // |
| 590 | // Read-only data section (implies initialized) |
| 591 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 592 | { |
| 593 | const GlobalVariable* GV = *GI; |
| 594 | if (GV->hasInitializer() && GV->isConstant()) |
| 595 | { |
| 596 | if (GI == M->gbegin()) |
| 597 | enterSection(ReadOnlyData); |
| 598 | printGlobalVariable(GV); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | for (hash_set<const ConstPoolVal*>::const_iterator I=moduleConstPool.begin(), |
| 603 | E = moduleConstPool.end(); I != E; ++I) |
| 604 | printConstant(*I); |
| 605 | |
| 606 | // Initialized read-write data section |
| 607 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 608 | { |
| 609 | const GlobalVariable* GV = *GI; |
| 610 | if (GV->hasInitializer() && ! GV->isConstant()) |
| 611 | { |
| 612 | if (GI == M->gbegin()) |
| 613 | enterSection(InitRWData); |
| 614 | printGlobalVariable(GV); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | // Uninitialized read-write data section |
| 619 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 620 | { |
| 621 | const GlobalVariable* GV = *GI; |
| 622 | if (! GV->hasInitializer()) |
| 623 | { |
| 624 | if (GI == M->gbegin()) |
| 625 | enterSection(UninitRWData); |
| 626 | printGlobalVariable(GV); |
| 627 | } |
| 628 | } |
| 629 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 630 | toAsm << endl; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | |
| 634 | void |
| 635 | SparcAsmPrinter::emitModule(const Module *M) |
| 636 | { |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 637 | // TODO: Look for a filename annotation on M to emit a .file directive |
| 638 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 639 | emitMethod(*I); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 640 | |
| 641 | emitGlobalsAndConstants(M); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | } // End anonymous namespace |
| 645 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 646 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 647 | // |
| 648 | // emitAssembly - Output assembly language code (a .s file) for the specified |
| 649 | // method. The specified method must have been compiled before this may be |
| 650 | // used. |
| 651 | // |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 652 | void |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 653 | UltraSparc::emitAssembly(const Module *M, ostream &toAsm) const |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 654 | { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 655 | SparcAsmPrinter Print(toAsm, M, *this); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 656 | } |