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 | // |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 7 | // This code largely consists of two LLVM Pass's: a MethodPass and a Pass. The |
| 8 | // MethodPass is pipelined together with all of the rest of the code generation |
| 9 | // stages, and the Pass runs at the end to emit code for global variables and |
| 10 | // such. |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SparcInternals.h" |
| 15 | #include "llvm/Analysis/SlotCalculator.h" |
| 16 | #include "llvm/CodeGen/MachineInstr.h" |
Chris Lattner | c019a17 | 2002-02-03 07:48:06 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 18 | #include "llvm/GlobalVariable.h" |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 19 | #include "llvm/ConstantVals.h" |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 20 | #include "llvm/DerivedTypes.h" |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 21 | #include "llvm/BasicBlock.h" |
| 22 | #include "llvm/Method.h" |
| 23 | #include "llvm/Module.h" |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 24 | #include "Support/StringExtras.h" |
| 25 | #include "Support/HashExtras.h" |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 26 | #include <ostream> |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 27 | using std::string; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Code Shared By the two printer passes, as a mixin |
| 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 34 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 35 | class AsmPrinter { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 36 | typedef std::hash_map<const Value*, int> ValIdMap; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 37 | typedef ValIdMap:: iterator ValIdMapIterator; |
| 38 | typedef ValIdMap::const_iterator ValIdMapConstIterator; |
| 39 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 40 | SlotCalculator *Table; // map anonymous values to unique integer IDs |
| 41 | ValIdMap valToIdMap; // used for values not handled by SlotCalculator |
| 42 | public: |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 43 | std::ostream &toAsm; |
Chris Lattner | 59ba109 | 2002-02-04 15:53:23 +0000 | [diff] [blame] | 44 | const TargetMachine &Target; |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 45 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 46 | enum Sections { |
| 47 | Unknown, |
| 48 | Text, |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 49 | ReadOnlyData, |
| 50 | InitRWData, |
| 51 | UninitRWData, |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 52 | } CurSection; |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 59ba109 | 2002-02-04 15:53:23 +0000 | [diff] [blame] | 54 | AsmPrinter(std::ostream &os, const TargetMachine &T) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 55 | : Table(0), toAsm(os), Target(T), CurSection(Unknown) {} |
| 56 | |
| 57 | |
| 58 | // (start|end)(Module|Method) - Callback methods to be invoked by subclasses |
| 59 | void startModule(Module *M) { |
| 60 | Table = new SlotCalculator(M, true); |
| 61 | } |
| 62 | void startMethod(Method *M) { |
| 63 | // Make sure the slot table has information about this method... |
| 64 | Table->incorporateMethod(M); |
| 65 | } |
| 66 | void endMethod(Method *M) { |
| 67 | Table->purgeMethod(); // Forget all about M. |
| 68 | } |
| 69 | void endModule() { |
| 70 | delete Table; Table = 0; |
| 71 | valToIdMap.clear(); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 74 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 75 | // enterSection - Use this method to enter a different section of the output |
| 76 | // executable. This is used to only output neccesary section transitions. |
| 77 | // |
| 78 | void enterSection(enum Sections S) { |
| 79 | if (S == CurSection) return; // Only switch section if neccesary |
| 80 | CurSection = S; |
| 81 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 82 | toAsm << "\n\t.section "; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 83 | switch (S) |
| 84 | { |
| 85 | default: assert(0 && "Bad section name!"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 86 | case Text: toAsm << "\".text\""; break; |
| 87 | case ReadOnlyData: toAsm << "\".rodata\",#alloc"; break; |
| 88 | case InitRWData: toAsm << "\".data\",#alloc,#write"; break; |
| 89 | case UninitRWData: toAsm << "\".bss\",#alloc,#write\nBbss.bss:"; break; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 90 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 91 | toAsm << "\n"; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 94 | static std::string getValidSymbolName(const string &S) { |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 95 | string Result; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 96 | |
| 97 | // Symbol names in Sparc assembly language have these rules: |
| 98 | // (a) Must match { letter | _ | . | $ } { letter | _ | . | $ | digit }* |
| 99 | // (b) A name beginning in "." is treated as a local name. |
| 100 | // (c) Names beginning with "_" are reserved by ANSI C and shd not be used. |
| 101 | // |
| 102 | if (S[0] == '_' || isdigit(S[0])) |
| 103 | Result += "ll"; |
| 104 | |
| 105 | for (unsigned i = 0; i < S.size(); ++i) |
| 106 | { |
| 107 | char C = S[i]; |
| 108 | if (C == '_' || C == '.' || C == '$' || isalpha(C) || isdigit(C)) |
| 109 | Result += C; |
| 110 | else |
| 111 | { |
| 112 | Result += '_'; |
| 113 | Result += char('0' + ((unsigned char)C >> 4)); |
| 114 | Result += char('0' + (C & 0xF)); |
| 115 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | c56d779 | 2001-09-28 15:07:24 +0000 | [diff] [blame] | 117 | return Result; |
| 118 | } |
| 119 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 120 | // getID - Return a valid identifier for the specified value. Base it on |
| 121 | // the name of the identifier if possible, use a numbered value based on |
| 122 | // prefix otherwise. FPrefix is always prepended to the output identifier. |
| 123 | // |
| 124 | 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] | 125 | string Result; |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 126 | string FP(FPrefix ? FPrefix : ""); // "Forced prefix" |
| 127 | if (V->hasName()) { |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 128 | Result = FP + V->getName(); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 129 | } else { |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 130 | int valId = Table->getValSlot(V); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 131 | if (valId == -1) { |
| 132 | ValIdMapConstIterator I = valToIdMap.find(V); |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 133 | if (I == valToIdMap.end()) |
| 134 | valId = valToIdMap[V] = valToIdMap.size(); |
| 135 | else |
| 136 | valId = I->second; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 137 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 138 | Result = FP + string(Prefix) + itostr(valId); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 139 | } |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 140 | return getValidSymbolName(Result); |
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 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 143 | // getID Wrappers - Ensure consistent usage... |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 144 | string getID(const Module *M) { |
| 145 | return getID(M, "LLVMModule_"); |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 146 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 147 | string getID(const Method *M) { |
| 148 | return getID(M, "LLVMMethod_"); |
| 149 | } |
| 150 | string getID(const BasicBlock *BB) { |
| 151 | return getID(BB, "LL", (".L_"+getID(BB->getParent())+"_").c_str()); |
| 152 | } |
| 153 | string getID(const GlobalVariable *GV) { |
| 154 | return getID(GV, "LLVMGlobal_", ".G_"); |
| 155 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 156 | string getID(const Constant *CV) { |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 157 | return getID(CV, "LLVMConst_", ".C_"); |
| 158 | } |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | |
| 163 | //===----------------------------------------------------------------------===// |
| 164 | // SparcMethodAsmPrinter Code |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | struct SparcMethodAsmPrinter : public MethodPass, public AsmPrinter { |
Chris Lattner | 59ba109 | 2002-02-04 15:53:23 +0000 | [diff] [blame] | 168 | inline SparcMethodAsmPrinter(std::ostream &os, const TargetMachine &t) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 169 | : AsmPrinter(os, t) {} |
| 170 | |
| 171 | virtual bool doInitialization(Module *M) { |
| 172 | startModule(M); |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | virtual bool runOnMethod(Method *M) { |
| 177 | startMethod(M); |
| 178 | emitMethod(M); |
| 179 | endMethod(M); |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | virtual bool doFinalization(Module *M) { |
| 184 | endModule(); |
| 185 | return false; |
| 186 | } |
| 187 | |
| 188 | void emitMethod(const Method *M); |
| 189 | private : |
| 190 | void emitBasicBlock(const BasicBlock *BB); |
| 191 | void emitMachineInst(const MachineInstr *MI); |
| 192 | |
| 193 | unsigned int printOperands(const MachineInstr *MI, unsigned int opNum); |
| 194 | void printOneOperand(const MachineOperand &Op); |
| 195 | |
| 196 | bool OpIsBranchTargetLabel(const MachineInstr *MI, unsigned int opNum); |
| 197 | bool OpIsMemoryAddressBase(const MachineInstr *MI, unsigned int opNum); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 198 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 199 | unsigned getOperandMask(unsigned Opcode) { |
| 200 | switch (Opcode) { |
| 201 | case SUBcc: return 1 << 3; // Remove CC argument |
Vikram S. Adve | 998cf0d | 2001-11-11 23:11:36 +0000 | [diff] [blame] | 202 | 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] | 203 | default: return 0; // By default, don't hack operands... |
| 204 | } |
| 205 | } |
| 206 | }; |
| 207 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 208 | inline bool |
| 209 | SparcMethodAsmPrinter::OpIsBranchTargetLabel(const MachineInstr *MI, |
| 210 | unsigned int opNum) { |
| 211 | switch (MI->getOpCode()) { |
| 212 | case JMPLCALL: |
| 213 | case JMPLRET: return (opNum == 0); |
| 214 | default: return false; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
| 219 | inline bool |
| 220 | SparcMethodAsmPrinter::OpIsMemoryAddressBase(const MachineInstr *MI, |
| 221 | unsigned int opNum) { |
| 222 | if (Target.getInstrInfo().isLoad(MI->getOpCode())) |
| 223 | return (opNum == 0); |
| 224 | else if (Target.getInstrInfo().isStore(MI->getOpCode())) |
| 225 | return (opNum == 1); |
| 226 | else |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | #define PrintOp1PlusOp2(Op1, Op2) \ |
| 232 | printOneOperand(Op1); \ |
| 233 | toAsm << "+"; \ |
| 234 | printOneOperand(Op2); |
| 235 | |
| 236 | unsigned int |
| 237 | SparcMethodAsmPrinter::printOperands(const MachineInstr *MI, |
| 238 | unsigned int opNum) |
| 239 | { |
| 240 | const MachineOperand& Op = MI->getOperand(opNum); |
| 241 | |
| 242 | if (OpIsBranchTargetLabel(MI, opNum)) |
| 243 | { |
| 244 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
| 245 | return 2; |
| 246 | } |
| 247 | else if (OpIsMemoryAddressBase(MI, opNum)) |
| 248 | { |
| 249 | toAsm << "["; |
| 250 | PrintOp1PlusOp2(Op, MI->getOperand(opNum+1)); |
| 251 | toAsm << "]"; |
| 252 | return 2; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | printOneOperand(Op); |
| 257 | return 1; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | void |
| 263 | SparcMethodAsmPrinter::printOneOperand(const MachineOperand &op) |
| 264 | { |
| 265 | switch (op.getOperandType()) |
| 266 | { |
| 267 | case MachineOperand::MO_VirtualRegister: |
| 268 | case MachineOperand::MO_CCRegister: |
| 269 | case MachineOperand::MO_MachineRegister: |
| 270 | { |
| 271 | int RegNum = (int)op.getAllocatedRegNum(); |
| 272 | |
| 273 | // ****this code is temporary till NULL Values are fixed |
| 274 | if (RegNum == Target.getRegInfo().getInvalidRegNum()) { |
| 275 | toAsm << "<NULL VALUE>"; |
| 276 | } else { |
| 277 | toAsm << "%" << Target.getRegInfo().getUnifiedRegName(RegNum); |
| 278 | } |
| 279 | break; |
| 280 | } |
| 281 | |
| 282 | case MachineOperand::MO_PCRelativeDisp: |
| 283 | { |
| 284 | const Value *Val = op.getVRegValue(); |
| 285 | if (!Val) |
| 286 | toAsm << "\t<*NULL Value*>"; |
| 287 | else if (const BasicBlock *BB = dyn_cast<const BasicBlock>(Val)) |
| 288 | toAsm << getID(BB); |
| 289 | else if (const Method *M = dyn_cast<const Method>(Val)) |
| 290 | toAsm << getID(M); |
| 291 | else if (const GlobalVariable *GV=dyn_cast<const GlobalVariable>(Val)) |
| 292 | toAsm << getID(GV); |
| 293 | else if (const Constant *CV = dyn_cast<const Constant>(Val)) |
| 294 | toAsm << getID(CV); |
| 295 | else |
| 296 | toAsm << "<unknown value=" << Val << ">"; |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | case MachineOperand::MO_SignExtendedImmed: |
| 301 | case MachineOperand::MO_UnextendedImmed: |
| 302 | toAsm << (long)op.getImmedValue(); |
| 303 | break; |
| 304 | |
| 305 | default: |
| 306 | toAsm << op; // use dump field |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | |
| 312 | void |
| 313 | SparcMethodAsmPrinter::emitMachineInst(const MachineInstr *MI) |
| 314 | { |
| 315 | unsigned Opcode = MI->getOpCode(); |
| 316 | |
| 317 | if (TargetInstrDescriptors[Opcode].iclass & M_DUMMY_PHI_FLAG) |
| 318 | return; // IGNORE PHI NODES |
| 319 | |
| 320 | toAsm << "\t" << TargetInstrDescriptors[Opcode].opCodeString << "\t"; |
| 321 | |
| 322 | unsigned Mask = getOperandMask(Opcode); |
| 323 | |
| 324 | bool NeedComma = false; |
| 325 | unsigned N = 1; |
| 326 | for (unsigned OpNum = 0; OpNum < MI->getNumOperands(); OpNum += N) |
| 327 | if (! ((1 << OpNum) & Mask)) { // Ignore this operand? |
| 328 | if (NeedComma) toAsm << ", "; // Handle comma outputing |
| 329 | NeedComma = true; |
| 330 | N = printOperands(MI, OpNum); |
| 331 | } |
| 332 | else |
| 333 | N = 1; |
| 334 | |
| 335 | toAsm << "\n"; |
| 336 | } |
| 337 | |
| 338 | void |
| 339 | SparcMethodAsmPrinter::emitBasicBlock(const BasicBlock *BB) |
| 340 | { |
| 341 | // Emit a label for the basic block |
| 342 | toAsm << getID(BB) << ":\n"; |
| 343 | |
| 344 | // Get the vector of machine instructions corresponding to this bb. |
| 345 | const MachineCodeForBasicBlock &MIs = BB->getMachineInstrVec(); |
| 346 | MachineCodeForBasicBlock::const_iterator MII = MIs.begin(), MIE = MIs.end(); |
| 347 | |
| 348 | // Loop over all of the instructions in the basic block... |
| 349 | for (; MII != MIE; ++MII) |
| 350 | emitMachineInst(*MII); |
| 351 | toAsm << "\n"; // Seperate BB's with newlines |
| 352 | } |
| 353 | |
| 354 | void |
| 355 | SparcMethodAsmPrinter::emitMethod(const Method *M) |
| 356 | { |
| 357 | string methName = getID(M); |
| 358 | toAsm << "!****** Outputing Method: " << methName << " ******\n"; |
| 359 | enterSection(AsmPrinter::Text); |
| 360 | toAsm << "\t.align\t4\n\t.global\t" << methName << "\n"; |
| 361 | //toAsm << "\t.type\t" << methName << ",#function\n"; |
| 362 | toAsm << "\t.type\t" << methName << ", 2\n"; |
| 363 | toAsm << methName << ":\n"; |
| 364 | |
| 365 | // Output code for all of the basic blocks in the method... |
| 366 | for (Method::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 367 | emitBasicBlock(*I); |
| 368 | |
| 369 | // Output a .size directive so the debugger knows the extents of the function |
| 370 | toAsm << ".EndOf_" << methName << ":\n\t.size " |
| 371 | << methName << ", .EndOf_" |
| 372 | << methName << "-" << methName << "\n"; |
| 373 | |
| 374 | // Put some spaces between the methods |
| 375 | toAsm << "\n\n"; |
| 376 | } |
| 377 | |
| 378 | } // End anonymous namespace |
| 379 | |
| 380 | Pass *UltraSparc::getMethodAsmPrinterPass(PassManager &PM, std::ostream &Out) { |
| 381 | return new SparcMethodAsmPrinter(Out, *this); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | // SparcMethodAsmPrinter Code |
| 390 | //===----------------------------------------------------------------------===// |
| 391 | |
| 392 | namespace { |
| 393 | |
| 394 | class SparcModuleAsmPrinter : public Pass, public AsmPrinter { |
| 395 | public: |
Chris Lattner | 49b8a9c | 2002-02-24 23:02:40 +0000 | [diff] [blame] | 396 | SparcModuleAsmPrinter(std::ostream &os, TargetMachine &t) |
| 397 | : AsmPrinter(os, t) {} |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 398 | |
| 399 | virtual bool run(Module *M) { |
| 400 | startModule(M); |
| 401 | emitGlobalsAndConstants(M); |
| 402 | endModule(); |
| 403 | return false; |
| 404 | } |
| 405 | |
| 406 | void emitGlobalsAndConstants(const Module *M); |
| 407 | |
| 408 | void printGlobalVariable(const GlobalVariable *GV); |
| 409 | void printSingleConstant( const Constant* CV); |
| 410 | void printConstantValueOnly(const Constant* CV); |
| 411 | void printConstant( const Constant* CV, std::string valID = ""); |
| 412 | |
| 413 | static void FoldConstants(const Module *M, |
| 414 | std::hash_set<const Constant*> &moduleConstants); |
| 415 | |
| 416 | }; |
| 417 | |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 418 | |
| 419 | // Can we treat the specified array as a string? Only if it is an array of |
| 420 | // ubytes or non-negative sbytes. |
| 421 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 422 | static bool isStringCompatible(ConstantArray *CPA) { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 423 | const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType(); |
| 424 | if (ETy == Type::UByteTy) return true; |
| 425 | if (ETy != Type::SByteTy) return false; |
| 426 | |
| 427 | for (unsigned i = 0; i < CPA->getNumOperands(); ++i) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 428 | if (cast<ConstantSInt>(CPA->getOperand(i))->getValue() < 0) |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 429 | return false; |
| 430 | |
| 431 | return true; |
| 432 | } |
| 433 | |
| 434 | // toOctal - Convert the low order bits of X into an octal letter |
| 435 | static inline char toOctal(int X) { |
| 436 | return (X&7)+'0'; |
| 437 | } |
| 438 | |
| 439 | // getAsCString - Return the specified array as a C compatible string, only if |
| 440 | // the predicate isStringCompatible is true. |
| 441 | // |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 442 | static string getAsCString(ConstantArray *CPA) { |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 443 | if (isStringCompatible(CPA)) { |
| 444 | string Result; |
| 445 | const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType(); |
| 446 | Result = "\""; |
| 447 | for (unsigned i = 0; i < CPA->getNumOperands(); ++i) { |
| 448 | unsigned char C = (ETy == Type::SByteTy) ? |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 449 | (unsigned char)cast<ConstantSInt>(CPA->getOperand(i))->getValue() : |
| 450 | (unsigned char)cast<ConstantUInt>(CPA->getOperand(i))->getValue(); |
Chris Lattner | cee8f9a | 2001-11-27 00:03:19 +0000 | [diff] [blame] | 451 | |
| 452 | if (isprint(C)) { |
| 453 | Result += C; |
| 454 | } else { |
| 455 | switch(C) { |
| 456 | case '\a': Result += "\\a"; break; |
| 457 | case '\b': Result += "\\b"; break; |
| 458 | case '\f': Result += "\\f"; break; |
| 459 | case '\n': Result += "\\n"; break; |
| 460 | case '\r': Result += "\\r"; break; |
| 461 | case '\t': Result += "\\t"; break; |
| 462 | case '\v': Result += "\\v"; break; |
| 463 | default: |
| 464 | Result += '\\'; |
| 465 | Result += toOctal(C >> 6); |
| 466 | Result += toOctal(C >> 3); |
| 467 | Result += toOctal(C >> 0); |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | Result += "\""; |
| 473 | |
| 474 | return Result; |
| 475 | } else { |
| 476 | return CPA->getStrValue(); |
| 477 | } |
| 478 | } |
| 479 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 480 | inline bool |
| 481 | ArrayTypeIsString(ArrayType* arrayType) |
| 482 | { |
| 483 | return (arrayType->getElementType() == Type::UByteTy || |
| 484 | arrayType->getElementType() == Type::SByteTy); |
| 485 | } |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 486 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 487 | inline const string |
| 488 | TypeToDataDirective(const Type* type) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 489 | { |
| 490 | switch(type->getPrimitiveID()) |
| 491 | { |
| 492 | case Type::BoolTyID: case Type::UByteTyID: case Type::SByteTyID: |
| 493 | return ".byte"; |
| 494 | case Type::UShortTyID: case Type::ShortTyID: |
| 495 | return ".half"; |
| 496 | case Type::UIntTyID: case Type::IntTyID: |
| 497 | return ".word"; |
| 498 | case Type::ULongTyID: case Type::LongTyID: case Type::PointerTyID: |
| 499 | return ".xword"; |
| 500 | case Type::FloatTyID: |
| 501 | return ".single"; |
| 502 | case Type::DoubleTyID: |
| 503 | return ".double"; |
| 504 | case Type::ArrayTyID: |
| 505 | if (ArrayTypeIsString((ArrayType*) type)) |
| 506 | return ".ascii"; |
| 507 | else |
| 508 | return "<InvaliDataTypeForPrinting>"; |
| 509 | default: |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 510 | return "<InvaliDataTypeForPrinting>"; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 511 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 514 | // Get the size of the constant for the given target. |
| 515 | // If this is an unsized array, return 0. |
| 516 | // |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 517 | inline unsigned int |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 518 | ConstantToSize(const Constant* CV, const TargetMachine& target) |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 519 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 520 | if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV)) |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 521 | { |
| 522 | ArrayType *aty = cast<ArrayType>(CPA->getType()); |
| 523 | if (ArrayTypeIsString(aty)) |
| 524 | return 1 + CPA->getNumOperands(); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 525 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 526 | |
| 527 | return target.findOptimalStorageSize(CV->getType()); |
| 528 | } |
| 529 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 530 | |
| 531 | |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 532 | // 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] | 533 | // Align all smaller data on the next higher 2^x boundary (4, 8, ...). |
| 534 | // |
| 535 | inline unsigned int |
| 536 | SizeToAlignment(unsigned int size, const TargetMachine& target) |
| 537 | { |
| 538 | unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1); |
| 539 | if (size > (unsigned) cacheLineSize / 2) |
| 540 | return cacheLineSize; |
| 541 | else |
| 542 | for (unsigned sz=1; /*no condition*/; sz *= 2) |
| 543 | if (sz >= size) |
| 544 | return sz; |
| 545 | } |
| 546 | |
| 547 | // Get the size of the type and then use SizeToAlignment. |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 548 | // |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 549 | inline unsigned int |
| 550 | TypeToAlignment(const Type* type, const TargetMachine& target) |
| 551 | { |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 552 | return SizeToAlignment(target.findOptimalStorageSize(type), target); |
| 553 | } |
| 554 | |
| 555 | // Get the size of the constant and then use SizeToAlignment. |
| 556 | // Handles strings as a special case; |
| 557 | inline unsigned int |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 558 | ConstantToAlignment(const Constant* CV, const TargetMachine& target) |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 559 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 560 | if (ConstantArray* CPA = dyn_cast<ConstantArray>(CV)) |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 561 | if (ArrayTypeIsString(cast<ArrayType>(CPA->getType()))) |
| 562 | return SizeToAlignment(1 + CPA->getNumOperands(), target); |
| 563 | |
| 564 | return TypeToAlignment(CV->getType(), target); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 568 | // Print a single constant value. |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 569 | void |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 570 | SparcModuleAsmPrinter::printSingleConstant(const Constant* CV) |
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 | assert(CV->getType() != Type::VoidTy && |
| 573 | CV->getType() != Type::TypeTy && |
| 574 | CV->getType() != Type::LabelTy && |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 575 | "Unexpected type for Constant"); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 576 | |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 577 | assert((! isa<ConstantArray>( CV) && ! isa<ConstantStruct>(CV)) |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 578 | && "Collective types should be handled outside this function"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 579 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 580 | toAsm << "\t" << TypeToDataDirective(CV->getType()) << "\t"; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 581 | |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 582 | if (CV->getType()->isPrimitiveType()) |
| 583 | { |
| 584 | if (CV->getType() == Type::FloatTy || CV->getType() == Type::DoubleTy) |
| 585 | toAsm << "0r"; // FP constants must have this prefix |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 586 | toAsm << CV->getStrValue() << "\n"; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 587 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 588 | else if (ConstantPointer* CPP = dyn_cast<ConstantPointer>(CV)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 589 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 590 | assert(CPP->isNullValue() && |
| 591 | "Cannot yet print non-null pointer constants to assembly"); |
| 592 | toAsm << "0\n"; |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 593 | } |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 594 | else if (isa<ConstantPointerRef>(CV)) |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 595 | { |
| 596 | assert(0 && "Cannot yet initialize pointer refs in assembly"); |
| 597 | } |
| 598 | else |
| 599 | { |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 600 | assert(0 && "Unknown elementary type for constant"); |
Vikram S. Adve | 29ff873 | 2001-11-08 05:12:37 +0000 | [diff] [blame] | 601 | } |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 604 | // Print a constant value or values (it may be an aggregate). |
| 605 | // Uses printSingleConstant() to print each individual value. |
| 606 | void |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 607 | SparcModuleAsmPrinter::printConstantValueOnly(const Constant* CV) |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 608 | { |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 609 | ConstantArray *CPA = dyn_cast<ConstantArray>(CV); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 610 | |
| 611 | if (CPA && isStringCompatible(CPA)) |
| 612 | { // print the string alone and return |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 613 | toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n"; |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 614 | } |
| 615 | else if (CPA) |
| 616 | { // Not a string. Print the values in successive locations |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 617 | const std::vector<Use> &constValues = CPA->getValues(); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 618 | for (unsigned i=1; i < constValues.size(); i++) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 619 | this->printConstantValueOnly(cast<Constant>(constValues[i].get())); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 620 | } |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 621 | else if (ConstantStruct *CPS = dyn_cast<ConstantStruct>(CV)) |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 622 | { // Print the fields in successive locations |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 623 | const std::vector<Use>& constValues = CPS->getValues(); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 624 | for (unsigned i=1; i < constValues.size(); i++) |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 625 | this->printConstantValueOnly(cast<Constant>(constValues[i].get())); |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 626 | } |
| 627 | else |
| 628 | this->printSingleConstant(CV); |
| 629 | } |
| 630 | |
| 631 | // Print a constant (which may be an aggregate) prefixed by all the |
| 632 | // appropriate directives. Uses printConstantValueOnly() to print the |
| 633 | // value or values. |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 634 | void |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 635 | SparcModuleAsmPrinter::printConstant(const Constant* CV, string valID) |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 636 | { |
| 637 | if (valID.length() == 0) |
| 638 | valID = getID(CV); |
| 639 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 640 | toAsm << "\t.align\t" << ConstantToAlignment(CV, Target) << "\n"; |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 641 | |
| 642 | // Print .size and .type only if it is not a string. |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 643 | ConstantArray *CPA = dyn_cast<ConstantArray>(CV); |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 644 | if (CPA && isStringCompatible(CPA)) |
| 645 | { // print it as a string and return |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 646 | toAsm << valID << ":\n"; |
| 647 | toAsm << "\t" << ".ascii" << "\t" << getAsCString(CPA) << "\n"; |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 648 | return; |
| 649 | } |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 650 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 651 | toAsm << "\t.type" << "\t" << valID << ",#object\n"; |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 652 | |
| 653 | unsigned int constSize = ConstantToSize(CV, Target); |
| 654 | if (constSize) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 655 | toAsm << "\t.size" << "\t" << valID << "," << constSize << "\n"; |
Vikram S. Adve | 2144722 | 2001-11-10 02:03:06 +0000 | [diff] [blame] | 656 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 657 | toAsm << valID << ":\n"; |
Vikram S. Adve | 915b58d | 2001-11-09 02:19:29 +0000 | [diff] [blame] | 658 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 659 | printConstantValueOnly(CV); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 663 | void SparcModuleAsmPrinter::FoldConstants(const Module *M, |
| 664 | std::hash_set<const Constant*> &MC) { |
| 665 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 666 | if (!(*I)->isExternal()) { |
| 667 | const std::hash_set<const Constant*> &pool = |
| 668 | MachineCodeForMethod::get(*I).getConstantPoolValues(); |
| 669 | MC.insert(pool.begin(), pool.end()); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | void SparcModuleAsmPrinter::printGlobalVariable(const GlobalVariable* GV) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 674 | { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 675 | toAsm << "\t.global\t" << getID(GV) << "\n"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 676 | |
| 677 | if (GV->hasInitializer()) |
| 678 | printConstant(GV->getInitializer(), getID(GV)); |
| 679 | else { |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 680 | toAsm << "\t.align\t" << TypeToAlignment(GV->getType()->getElementType(), |
| 681 | Target) << "\n"; |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 682 | toAsm << "\t.type\t" << getID(GV) << ",#object\n"; |
Vikram S. Adve | ffbba0f | 2001-11-08 14:29:57 +0000 | [diff] [blame] | 683 | toAsm << "\t.reserve\t" << getID(GV) << "," |
Chris Lattner | c019a17 | 2002-02-03 07:48:06 +0000 | [diff] [blame] | 684 | << Target.findOptimalStorageSize(GV->getType()->getElementType()) |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 685 | << "\n"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 690 | void SparcModuleAsmPrinter::emitGlobalsAndConstants(const Module *M) { |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 691 | // First, get the constants there were marked by the code generator for |
| 692 | // inclusion in the assembly code data area and fold them all into a |
| 693 | // single constant pool since there may be lots of duplicates. Also, |
| 694 | // lets force these constants into the slot table so that we can get |
| 695 | // unique names for unnamed constants also. |
| 696 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 697 | std::hash_set<const Constant*> moduleConstants; |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 698 | FoldConstants(M, moduleConstants); |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 699 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 700 | // Now, emit the three data sections separately; the cost of I/O should |
| 701 | // make up for the cost of extra passes over the globals list! |
| 702 | // |
| 703 | // Read-only data section (implies initialized) |
| 704 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 705 | { |
| 706 | const GlobalVariable* GV = *GI; |
| 707 | if (GV->hasInitializer() && GV->isConstant()) |
| 708 | { |
| 709 | if (GI == M->gbegin()) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 710 | enterSection(AsmPrinter::ReadOnlyData); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 711 | printGlobalVariable(GV); |
| 712 | } |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 713 | } |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 715 | for (std::hash_set<const Constant*>::const_iterator |
| 716 | I = moduleConstants.begin(), |
Chris Lattner | e9bb2df | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 717 | E = moduleConstants.end(); I != E; ++I) |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 718 | printConstant(*I); |
| 719 | |
| 720 | // Initialized read-write data section |
| 721 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 722 | { |
| 723 | const GlobalVariable* GV = *GI; |
| 724 | if (GV->hasInitializer() && ! GV->isConstant()) |
| 725 | { |
| 726 | if (GI == M->gbegin()) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 727 | enterSection(AsmPrinter::InitRWData); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 728 | printGlobalVariable(GV); |
| 729 | } |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 732 | // Uninitialized read-write data section |
| 733 | for (Module::const_giterator GI=M->gbegin(), GE=M->gend(); GI != GE; ++GI) |
| 734 | { |
| 735 | const GlobalVariable* GV = *GI; |
| 736 | if (! GV->hasInitializer()) |
| 737 | { |
| 738 | if (GI == M->gbegin()) |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 739 | enterSection(AsmPrinter::UninitRWData); |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 740 | printGlobalVariable(GV); |
| 741 | } |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 744 | toAsm << "\n"; |
Vikram S. Adve | 953c83e | 2001-10-28 21:38:52 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Lattner | e88f78c | 2001-09-19 13:47:27 +0000 | [diff] [blame] | 747 | } // End anonymous namespace |
| 748 | |
Chris Lattner | c19b8b1 | 2002-02-03 23:41:08 +0000 | [diff] [blame] | 749 | Pass *UltraSparc::getModuleAsmPrinterPass(PassManager &PM, std::ostream &Out) { |
| 750 | return new SparcModuleAsmPrinter(Out, *this); |
Chris Lattner | c019a17 | 2002-02-03 07:48:06 +0000 | [diff] [blame] | 751 | } |