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