-
Notifications
You must be signed in to change notification settings - Fork 33
/
constant_pool.h
179 lines (157 loc) · 4.54 KB
/
constant_pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#ifndef IV_LV5_RAILGUN_CONSTANT_POOL_H_
#define IV_LV5_RAILGUN_CONSTANT_POOL_H_
#include <iv/detail/cstdint.h>
#include <iv/detail/unordered_map.h>
#include <iv/detail/functional.h>
#include <iv/ustring.h>
#include <iv/utils.h>
#include <iv/lv5/jsval_fwd.h>
#include <iv/lv5/jsstring_fwd.h>
#include <iv/lv5/error.h>
#include <iv/lv5/railgun/context_fwd.h>
#include <iv/lv5/railgun/code.h>
namespace iv {
namespace lv5 {
namespace railgun {
class ConstantPool {
// Bytecode register size is very limited.
// So probably, constant register may be overflown sometimes.
// When constant register is overflown, we can use LOAD_CONST instead of
// constant register.
public:
static const uint32_t kEmpty = UINT32_MAX;
typedef std::unordered_map<core::UString, int32_t> JSStringToIndexMap;
typedef std::unordered_map<
double,
int32_t,
std::hash<double>, JSDoubleEquals> JSDoubleToIndexMap;
ConstantPool(Context* ctx)
: ctx_(ctx),
code_(NULL),
jsstring_to_index_map_(),
double_to_index_map_(),
undefined_index_(),
null_index_(),
true_index_(),
false_index_(),
empty_index_() {
}
void Init(Code* code) {
code_ = code;
jsstring_to_index_map_.clear();
double_to_index_map_.clear();
undefined_index_ = kEmpty;
null_index_ = kEmpty;
true_index_ = kEmpty;
false_index_ = kEmpty;
empty_index_ = kEmpty;
}
uint32_t undefined_index() {
if (undefined_index_ != kEmpty) {
return undefined_index_;
}
undefined_index_ = AddConstant(JSUndefined);
return undefined_index_;
}
uint32_t null_index() {
if (null_index_ != kEmpty) {
return null_index_;
}
null_index_ = AddConstant(JSNull);
return null_index_;
}
uint32_t true_index() {
if (true_index_ != kEmpty) {
return true_index_;
}
true_index_ = AddConstant(JSTrue);
return true_index_;
}
uint32_t false_index() {
if (false_index_ != kEmpty) {
return false_index_;
}
false_index_ = AddConstant(JSFalse);
return false_index_;
}
uint32_t empty_index() {
if (empty_index_ != kEmpty) {
return empty_index_;
}
empty_index_ = AddConstant(JSEmpty);
return empty_index_;
}
uint32_t string_index(const core::UString& str) {
const JSStringToIndexMap::const_iterator it =
jsstring_to_index_map_.find(str);
if (it != jsstring_to_index_map_.end()) {
// duplicate constant
return it->second;
}
// new constant value
Error::Dummy dummy;
const uint32_t index = AddConstant(
JSString::New(
ctx_,
str.begin(),
str.end(),
core::character::IsASCII(str.begin(), str.end()), &dummy));
jsstring_to_index_map_.insert(std::make_pair(str, index));
return index;
}
uint32_t string_index(const StringLiteral* str) {
return string_index(core::ToUString(str->value()));
}
uint32_t string_index(const core::StringPiece& str) {
return string_index(core::ToUString(str));
}
uint32_t string_index(const core::UStringPiece& str) {
return string_index(core::UString(str));
}
uint32_t number_index(double val) {
const JSDoubleToIndexMap::const_iterator it =
double_to_index_map_.find(val);
if (it != double_to_index_map_.end()) {
// duplicate constant pool
return it->second;
}
// new constant value
const uint32_t index = AddConstant(val);
double_to_index_map_.insert(std::make_pair(val, index));
return index;
}
uint32_t Lookup(JSVal constant) {
if (constant.IsString()) {
return string_index(constant.string()->GetUString());
} else if (constant.IsNumber()) {
return number_index(constant.number());
} else if (constant.IsUndefined()) {
return undefined_index();
} else if (constant.IsNull()) {
return null_index();
} else if (constant.IsBoolean()) {
return (constant.boolean()) ? true_index() : false_index();
} else if (constant.IsEmpty()) {
return empty_index();
}
UNREACHABLE();
return 0; // makes compiler happy
}
private:
uint32_t AddConstant(JSVal value) {
const uint32_t result = code_->constants().size();
code_->constants_.push_back(value);
return result;
}
Context* ctx_;
Code* code_;
JSStringToIndexMap jsstring_to_index_map_;
JSDoubleToIndexMap double_to_index_map_;
uint32_t undefined_index_;
uint32_t null_index_;
uint32_t true_index_;
uint32_t false_index_;
uint32_t empty_index_;
};
} } } // namespace iv::lv5::railgun
#endif // IV_LV5_RAILGUN_CONSTANT_POOL_H_