Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(494)

Side by Side Diff: src/IceGlobalInits.cpp

Issue 641193002: Introduce the notion of function addresses in Subzero. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Clean up code and fix nits. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 //===- subzero/src/IceGlobalInits.cpp - Global initializers ---------------===// 1 //===- subzero/src/IceGlobalInits.cpp - Global initializers ---------------===//
2 // 2 //
3 // The Subzero Code Generator 3 // The Subzero Code Generator
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 // 9 //
10 // This file implements the notion of global addresses and 10 // This file implements the notion of global addresses and
11 // initializers in Subzero. 11 // initializers in Subzero.
12 // 12 //
13 //===----------------------------------------------------------------------===// 13 //===----------------------------------------------------------------------===//
14 14
15 #include "llvm/ADT/STLExtras.h" 15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Function.h" 16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Value.h" 17 #include "llvm/IR/Value.h"
18 18
19 #include "IceDefs.h" 19 #include "IceDefs.h"
20 #include "IceGlobalContext.h"
20 #include "IceGlobalInits.h" 21 #include "IceGlobalInits.h"
21 #include "IceTypes.h" 22 #include "IceTypes.h"
22 23
23 namespace { 24 namespace {
24 char hexdigit(unsigned X) { return X < 10 ? '0' + X : 'A' + X - 10; } 25 char hexdigit(unsigned X) { return X < 10 ? '0' + X : 'A' + X - 10; }
26
27 void dumpLinkage(Ice::Ostream &Stream,
28 llvm::GlobalValue::LinkageTypes Linkage) {
29 switch (Linkage) {
30 case llvm::GlobalValue::ExternalLinkage:
31 Stream << "external";
32 return;
33 case llvm::GlobalValue::InternalLinkage:
34 Stream << "internal";
35 return;
36 default:
37 break;
38 }
39 std::string Buffer;
40 llvm::raw_string_ostream StrBuf(Buffer);
41 StrBuf << "Unknown linkage value: " << Linkage;
42 llvm::report_fatal_error(StrBuf.str());
43 }
44
45 void dumpCallingConv(Ice::Ostream &, llvm::CallingConv::ID CallingConv) {
46 if (CallingConv == llvm::CallingConv::C)
47 return;
48 std::string Buffer;
49 llvm::raw_string_ostream StrBuf(Buffer);
50 StrBuf << "Unknown calling convention: " << CallingConv;
51 llvm::report_fatal_error(StrBuf.str());
52 }
25 } 53 }
Jim Stichnoth 2014/10/10 13:15:00 // end of anonymous namespace
Karl 2014/10/10 20:17:30 Done.
26 54
27 namespace Ice { 55 namespace Ice {
28 56
29 GlobalAddress::~GlobalAddress() { llvm::DeleteContainerPointers(Initializers); } 57 IceString GlobalAddress::mangleName(GlobalContext *Ctx) const {
58 return (getIsExternal() && !hasInitializer()) ? Name : Ctx->mangleName(Name);
59 }
30 60
31 void GlobalAddress::dumpType(Ostream &Stream) const { 61 Function *Function::create(GlobalContext *Ctx, const FuncSigType &Signature,
62 llvm::CallingConv::ID CallingConv,
63 llvm::GlobalValue::LinkageTypes Linkage,
64 bool IsProto) {
65 return Ctx->newFunction(&Signature, CallingConv, Linkage, IsProto);
66 }
67
68 void Function::dumpType(Ostream &Stream) const { Stream << Signature; }
69
70 void Function::dump(Ostream &Stream) const {
71 if (IsProto)
72 Stream << "declare ";
73 ::dumpLinkage(Stream, Linkage);
74 ::dumpCallingConv(Stream, CallingConv);
75 Stream << Signature.getReturnType() << " @" << Name << "(";
76 bool IsFirst = true;
77 for (Type ArgTy : Signature.getArgList()) {
78 if (IsFirst)
79 IsFirst = false;
80 else
81 Stream << ", ";
82 Stream << ArgTy;
83 }
84 Stream << ")";
85 }
86
87 GlobalVariable *GlobalVariable::create(GlobalContext *Ctx) {
88 return Ctx->newGlobalVariable();
89 }
90
91 GlobalVariable::~GlobalVariable() {
92 llvm::DeleteContainerPointers(Initializers);
93 }
94
95 void GlobalVariable::dumpType(Ostream &Stream) const {
32 if (Initializers.size() == 1) { 96 if (Initializers.size() == 1) {
33 Initializers.front()->dumpType(Stream); 97 Initializers.front()->dumpType(Stream);
34 } else { 98 } else {
35 Stream << "<{ "; 99 Stream << "<{ ";
36 bool IsFirst = true; 100 bool IsFirst = true;
37 for (Initializer *Init : Initializers) { 101 for (Initializer *Init : Initializers) {
38 if (IsFirst) { 102 if (IsFirst) {
39 IsFirst = false; 103 IsFirst = false;
40 } else { 104 } else {
41 Stream << ", "; 105 Stream << ", ";
42 } 106 }
43 Init->dumpType(Stream); 107 Init->dumpType(Stream);
44 } 108 }
45 Stream << " }>"; 109 Stream << " }>";
46 } 110 }
47 } 111 }
48 112
49 void GlobalAddress::dump(Ostream &Stream) const { 113 void GlobalVariable::dump(Ostream &Stream) const {
50 Stream << "@" << getName() << " = internal " 114 Stream << "@" << Name << " = internal "
51 << (IsConstant ? "constant" : "global") << " "; 115 << (IsConstant ? "constant" : "global") << " ";
52 116
53 // Add initializer. 117 // Add initializer.
54 if (Initializers.size() == 1) { 118 if (Initializers.size() == 1) {
55 Initializers.front()->dump(Stream); 119 Initializers.front()->dump(Stream);
56 } else { 120 } else {
57 dumpType(Stream); 121 dumpType(Stream);
58 Stream << " <{ "; 122 Stream << " <{ ";
59 bool IsFirst = true; 123 bool IsFirst = true;
60 for (Initializer *Init : Initializers) { 124 for (Initializer *Init : Initializers) {
61 if (IsFirst) { 125 if (IsFirst) {
62 IsFirst = false; 126 IsFirst = false;
63 } else { 127 } else {
64 Stream << ", "; 128 Stream << ", ";
65 } 129 }
66 Init->dump(Stream); 130 Init->dump(Stream);
67 } 131 }
68 Stream << " }>"; 132 Stream << " }>";
69 } 133 }
70 134
71 // Add alignment. 135 // Add alignment.
72 if (Alignment > 0) 136 if (Alignment > 0)
73 Stream << ", align " << Alignment; 137 Stream << ", align " << Alignment;
74 Stream << "\n"; 138 Stream << "\n";
75 } 139 }
76 140
77 void GlobalAddress::Initializer::dumpType(Ostream &Stream) const { 141 void GlobalVariable::Initializer::dumpType(Ostream &Stream) const {
78 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]"; 142 Stream << "[" << getNumBytes() << " x " << Ice::IceType_i8 << "]";
79 } 143 }
80 144
81 void GlobalAddress::DataInitializer::dump(Ostream &Stream) const { 145 void GlobalVariable::DataInitializer::dump(Ostream &Stream) const {
82 dumpType(Stream); 146 dumpType(Stream);
83 Stream << " c\""; 147 Stream << " c\"";
84 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep 148 // Code taken from PrintEscapedString() in AsmWriter.cpp. Keep
85 // the strings in the same format as the .ll file for practical 149 // the strings in the same format as the .ll file for practical
86 // diffing. 150 // diffing.
87 for (uint8_t C : Contents) { 151 for (uint8_t C : Contents) {
88 if (isprint(C) && C != '\\' && C != '"') 152 if (isprint(C) && C != '\\' && C != '"')
89 Stream << C; 153 Stream << C;
90 else 154 else
91 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); 155 Stream << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
92 } 156 }
93 Stream << "\""; 157 Stream << "\"";
94 } 158 }
95 159
96 void GlobalAddress::ZeroInitializer::dump(Ostream &Stream) const { 160 void GlobalVariable::ZeroInitializer::dump(Ostream &Stream) const {
97 dumpType(Stream); 161 dumpType(Stream);
98 Stream << " zeroinitializer"; 162 Stream << " zeroinitializer";
99 } 163 }
100 164
101 IceString GlobalAddress::RelocInitializer::getName() const { 165 void GlobalVariable::RelocInitializer::dumpType(Ostream &Stream) const {
102 switch (Address.getKind()) {
103 case FunctionRelocation:
104 return Address.getFunction()->getName();
105 case GlobalAddressRelocation:
106 return Address.getGlobalAddr()->getName();
107 default:
108 llvm::report_fatal_error("Malformed relocation address!");
109 }
110 }
111
112 void GlobalAddress::RelocInitializer::dumpType(Ostream &Stream) const {
113 Stream << Ice::IceType_i32; 166 Stream << Ice::IceType_i32;
114 } 167 }
115 168
116 void GlobalAddress::RelocInitializer::dump(Ostream &Stream) const { 169 void GlobalVariable::RelocInitializer::dump(Ostream &Stream) const {
117 if (Offset != 0) { 170 if (Offset != 0) {
118 dumpType(Stream); 171 dumpType(Stream);
119 Stream << " add ("; 172 Stream << " add (";
120 } 173 }
121 dumpType(Stream); 174 dumpType(Stream);
122 Stream << " ptrtoint ("; 175 Stream << " ptrtoint (";
123 if (Address.getKind() == FunctionRelocation) { 176 Address->dumpType(Stream);
124 Stream << *Address.getFunction()->getType() << " @" 177 Stream << "* @" << Address->getName() << " to ";
125 << Address.getFunction()->getName();
126 } else {
127 Address.getGlobalAddr()->dumpType(Stream);
128 Stream << "* @" << Address.getGlobalAddr()->getName();
129 }
130 Stream << " to ";
131 dumpType(Stream); 178 dumpType(Stream);
132 Stream << ")"; 179 Stream << ")";
133 if (Offset != 0) { 180 if (Offset != 0) {
134 Stream << ", "; 181 Stream << ", ";
135 dumpType(Stream); 182 dumpType(Stream);
136 Stream << " " << Offset << ")"; 183 Stream << " " << Offset << ")";
137 } 184 }
138 } 185 }
139 } 186 }
Jim Stichnoth 2014/10/10 13:15:00 // end of namespace Ice
Karl 2014/10/10 20:17:30 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698