-
Notifications
You must be signed in to change notification settings - Fork 1
/
w3Stack.h
306 lines (257 loc) · 6.51 KB
/
w3Stack.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// A WebAssembly codebase by Jay Krell
//
// https://webassembly.github.io/spec/core/binary/index.html
// https://webassembly.github.io/spec/core/_download/WebAssembly.pdf
#pragma once
#include "w3StackBase.h"
#include "w3StackValue.h"
#include "w3Tag.h"
#include "w3Value.h"
// work in progress
struct w3Stack : private w3StackBase
{
w3Stack () { }
// old compilers lack using.
typedef w3StackBase base;
typedef base::iterator iterator;
void pop () { base::pop (); }
w3StackValue& top () { return base::top (); }
w3StackValue& back () { return base::back (); }
w3StackValue& front () { return base::front (); }
iterator begin () { return base::begin (); }
iterator end () { return base::end (); }
bool empty () const { return base::empty (); }
void resize (size_t newsize) { base::resize (newsize); }
size_t size () { return base::size (); }
w3StackValue& operator [ ] (size_t index) { return base::operator [ ] (index); }
void reserve (size_t n)
{
// TODO
}
// While ultimately a stack of values, labels, and frames, values dominate.
w3Tag& tag (w3Tag tag)
{
AssertTopIsValue ();
w3StackValue& t = top ();
AssertFormat (t.value.tag == tag, ("%X %X", t.value.tag, tag));
return t.value.tag;
}
w3Tag& tag ()
{
AssertTopIsValue ();
w3StackValue& t = top ();
return t.value.tag;
}
Value& value ()
{
AssertTopIsValue ();
w3StackValue& t = top ();
return t.value.value;
}
Value& value (w3Tag tag)
{
AssertTopIsValue ();
w3StackValue& t = top ();
AssertFormat (t.value.tag == tag, ("%X %X", t.value.tag, tag));
return t.value.value;
}
void pop_label ()
{
if (size () < 1 || top ().tag != Tag_Label)
DumpStack ("AssertTopIsValue");
AssertFormat (size () >= 1, ("%" FORMAT_SIZE "X", size ()));
AssertFormat (top ().tag == Tag_Label, ("%X %X", top ().tag, Tag_Label));
pop ();
}
void pop_value ()
{
AssertTopIsValue ();
//int t = tag ();
pop ();
//printf ("pop_value tag:%s depth:%" FORMAT_SIZE "X\n", TagToString (t), size ());
}
void push_value (const w3StackValue& value)
{
AssertFormat (value.tag == Tag_Value, ("%X %X", value.tag, Tag_Value));
push (value);
//printf ("push_value tag:%s value:%X depth:%" FORMAT_SIZE "X\n", TagToString (value.value.tag), value.value.value.i32, size ());
}
void push_label (const w3StackValue& value)
{
AssertFormat (value.tag == Tag_Label, ("%X %X", value.tag, Tag_Label));
push (value);
//printf ("push_label depth:%" FORMAT_SIZE "X\n", size ());
}
void push_frame (const w3StackValue& value)
{
AssertFormat (value.tag == Tag_Frame, ("%X %X", value.tag, Tag_Frame));
push (value);
//printf ("push_frame depth:%" FORMAT_SIZE "X\n", size ());
}
// type specific pushers
void push_i32 (int32_t i)
{
w3StackValue value (Tag_i32);
value.value.value.i32 = i;
push_value (value);
}
void push_i64 (int64_t i)
{
w3StackValue value (Tag_i64);
value.value.value.i64 = i;
push_value (value);
}
void push_u32 (uint32_t i)
{
push_i32 ((int32_t)i);
}
void push_u64 (uint64_t i)
{
push_i64 ((int64_t)i);
}
void push_f32 (float i)
{
w3StackValue value (Tag_f32);
value.value.value.f32 = i;
push_value (value);
}
void push_f64 (double i)
{
w3StackValue value (Tag_f64);
value.value.value.f64 = i;
push_value (value);
}
void push_bool (bool b)
{
push_i32 (b);
}
// accessors, check tag, return ref
int32_t& i32 ()
{
return value (Tag_i32).i32;
}
int64_t& i64 ()
{
return value (Tag_i64).i64;
}
uint32_t& u32 ()
{
return value (Tag_i32).u32;
}
uint64_t& u64 ()
{
return value (Tag_i64).u64;
}
float& f32 ()
{
return value (Tag_f32).f32;
}
double& f64 ()
{
return value (Tag_f64).f64;
}
void DumpStack (PCSTR prefix)
{
const size_t n = size ();
printf ("stack@%s: %" FORMAT_SIZE "X ", prefix, n);
for (size_t i = 0; i != n; ++i)
{
printf ("%s:", TagToString (begin () [(ssize_t)i].tag));
switch (begin () [(ssize_t)i].tag)
{
case Tag_Label:
break;
case Tag_Frame:
break;
case Tag_Value:
printf ("%s", TagToString (begin () [(ssize_t)i].value.tag));
break;
default:; //todo
}
printf (" ");
}
printf ("\n");
}
void AssertTopIsValue ()
{
if (size () < 1 || top ().tag != Tag_Value)
DumpStack ("AssertTopIsValue");
AssertFormat (size () >= 1, ("%" FORMAT_SIZE "X", size ()));
AssertFormat (top ().tag == Tag_Value, ("%X %X", top ().tag, Tag_Value));
}
// setter, changes tag, returns ref
Value& set (w3Tag tag)
{
AssertTopIsValue ();
w3StackValue& t = top ();
TaggedValue& v = t.value;
v.tag = tag;
return v.value;
}
// type-specific setters
void set_i32 (int32_t a)
{
set (Tag_i32).i32 = a;
}
void set_u32 (uint32_t a)
{
set (Tag_i32).u32 = a;
}
void set_bool (bool a)
{
set_i32 (a);
}
void set_i64 (int64_t a)
{
set (Tag_i64).i64 = a;
}
void set_u64 (uint64_t a)
{
set (Tag_i64).u64 = a;
}
void set_f32 (float a)
{
set (Tag_f32).f32 = a;
}
void set_f64 (double a)
{
set (Tag_f64).f64 = a;
}
// type specific poppers
int32_t pop_i32 ()
{
int32_t a = i32 ();
pop_value ();
return a;
}
uint32_t pop_u32 ()
{
uint32_t a = u32 ();
pop_value ();
return a;
}
int64_t pop_i64 ()
{
int64_t a = i64 ();
pop_value ();
return a;
}
uint64_t pop_u64 ()
{
uint64_t a = u64 ();
pop_value ();
return a;
}
float pop_f32 ()
{
float a = f32 ();
pop_value ();
return a;
}
double pop_f64 ()
{
double a = f64 ();
pop_value ();
return a;
}
};