OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/IceGlobalInits.cpp - Global initializers ---------------===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This file implements the notion of global addresses and | |
11 // initializers in Subzero. | |
12 // | |
13 //===----------------------------------------------------------------------===// | |
14 | |
15 #include "IceDefs.h" | |
16 #include "IceGlobalInits.h" | |
17 #include "IceTypes.h" | |
18 #include "llvm/ADT/STLExtras.h" | |
jvoung (off chromium)
2014/10/03 16:15:29
nit: LLVM headers before Ice headers?
Karl
2014/10/04 16:28:34
Done.
| |
19 #include "llvm/IR/Function.h" | |
20 #include "llvm/IR/Value.h" | |
21 | |
22 namespace { | |
23 char hexdigit(unsigned X) { return X < 10 ? '0' + X : 'A' + X - 10; } | |
24 } | |
25 | |
26 namespace Ice { | |
27 | |
28 GlobalAddress::~GlobalAddress() { llvm::DeleteContainerPointers(Initializers); } | |
29 | |
30 void GlobalAddress::dumpType(Ostream &Stream) const { | |
31 if (Initializers.size() == 1) { | |
32 Initializers.front()->dumpType(Stream); | |
33 } else { | |
34 Stream << "<{ "; | |
35 bool IsFirst = true; | |
36 for (Initializer *Init : Initializers) { | |
37 if (IsFirst) { | |
38 IsFirst = false; | |
39 } else { | |
40 Stream << ", "; | |
41 } | |
42 Init->dumpType(Stream); | |
43 } | |
44 Stream << " }>"; | |
45 } | |
46 } | |
47 | |
48 void GlobalAddress::dump(Ostream &Stream) const { | |
49 Stream << "@" << getName() << " = internal " | |
50 << (IsConstant ? "constant" : "global") << " "; | |
51 | |
52 // Add initializer. | |
53 if (Initializers.size() == 1) { | |
54 Initializers.front()->dump(Stream); | |
55 } else { | |
56 dumpType(Stream); | |
57 Stream << " <{ "; | |
58 bool IsFirst = true; | |
59 for (Initializer *Init : Initializers) { | |
60 if (IsFirst) { | |
61 IsFirst = false; | |
62 } else { | |
63 Stream << ", "; | |
64 } | |
65 Init->dump(Stream); | |
66 } | |
67 Stream << " }>"; | |
68 } | |
69 | |
70 // Add alignment. | |
71 if (Alignment > 0) | |
72 Stream << ", align " << Alignment; | |
73 Stream << "\n"; | |
74 } | |
75 | |
76 void GlobalAddress::Initializer::dumpType(Ostream &Stream) const { | |
77 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; | |
78 } | |
79 | |
80 void GlobalAddress::DataInitializer::dump(Ostream &Stream) const { | |
81 dumpType(Stream); | |
82 Stream << " c\""; | |
83 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep | |
84 // the strings in the same format as the .ll file for practical | |
85 // diffing. | |
86 for (uint8_t C : Contents) { | |
87 if (isprint(C) && C != '\\' && C != '"') | |
88 Stream << C; | |
89 else | |
90 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); | |
91 } | |
92 Stream << "\""; | |
93 } | |
94 | |
95 void GlobalAddress::ZeroInitializer::dump(Ostream &Stream) const { | |
96 dumpType(Stream); | |
97 Stream << " zeroinitializer"; | |
98 } | |
99 | |
100 IceString GlobalAddress::RelocInitializer::getName() const { | |
101 switch (Address.Kind) { | |
102 case FunctionRelocation: | |
103 return Address.Address.Function->getName(); | |
104 case GlobalAddressRelocation: | |
105 return Address.Address.GlobalAddr->getName(); | |
106 default: | |
107 llvm::report_fatal_error("Malformed relocation address!"); | |
108 } | |
109 } | |
110 | |
111 void GlobalAddress::RelocInitializer::dumpType(Ostream &Stream) const { | |
112 Stream << Ice::IceType_i32; | |
113 } | |
114 | |
115 void GlobalAddress::RelocInitializer::dump(Ostream &Stream) const { | |
116 if (Offset != 0) { | |
117 dumpType(Stream); | |
118 Stream << " add ("; | |
119 } | |
120 dumpType(Stream); | |
121 Stream << " ptrtoint ("; | |
122 if (Address.Kind == FunctionRelocation) { | |
123 Stream << *Address.Address.Function->getType() << " @" | |
124 << Address.Address.Function->getName(); | |
125 } else { | |
126 Address.Address.GlobalAddr->dumpType(Stream); | |
127 Stream << "* @" << Address.Address.GlobalAddr->getName(); | |
128 } | |
129 Stream << " to "; | |
130 dumpType(Stream); | |
131 Stream << ")"; | |
132 if (Offset != 0) { | |
133 Stream << ", "; | |
134 dumpType(Stream); | |
135 Stream << " " << Offset << ")"; | |
136 } | |
137 } | |
138 } | |
OLD | NEW |