-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathstack.cpp
More file actions
628 lines (528 loc) · 16.9 KB
/
stack.cpp
File metadata and controls
628 lines (528 loc) · 16.9 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/* Copyright (c) 2024 Julian Benda
*
* This file is part of inkCPP which is released under MIT license.
* See file LICENSE.txt or go to
* https://github.com/JBenda/inkcpp for full license details.
*/
#include "stack.h"
#include "string_table.h"
namespace ink::runtime::internal
{
basic_stack::basic_stack(entry* data, size_t size)
: base(data, size)
{
}
void basic_stack::set(hash_t name, const value& val)
{
// If we have a save point, always add no matter what
if (base::is_saved()) {
add(name, val);
return;
}
// Either set an existing variable or add it to the stack
value* existing = const_cast<value*>(get(name));
if (existing == nullptr)
add(name, val);
else
*existing = val;
}
bool reverse_find_predicat(hash_t name, thread_t& skip, uint32_t& jumping, entry& e)
{
// Jumping
if (jumping > 0) {
jumping--;
return false;
}
// If this is an end thread marker, skip over it
if (skip == ~0U && e.data.type() == value_type::thread_end) {
skip = e.data.get<value_type::thread_end>();
}
// If we're skipping
if (skip != ~0U) {
// Stop if we get to the start of the thread block
if (e.data.type() == value_type::thread_start
&& skip == e.data.get<value_type::thread_start>().jump) {
skip = ~0U;
}
// Don't return anything in the hidden thread block
return false;
}
// Is it a thread start or a jump marker
if (e.name == InvalidHash
&& (e.data.type() == value_type::thread_start || e.data.type() == value_type::jump_marker)) {
// If this thread start has a jump value
uint32_t jump = e.data.get<value_type::jump_marker>().thread_id;
// Then we need to do some jumping. Skip
if (jump > 0) {
jumping = jump;
return false;
}
}
return e.name == name || e.name == InvalidHash;
}
class reverse_find_predicat_operator
{
public:
explicit reverse_find_predicat_operator(hash_t name)
: _name{name}
{
}
bool operator()(entry& e) { return reverse_find_predicat(_name, _skip, _jumping, e); }
private:
hash_t _name;
thread_t _skip = ~0U;
uint32_t _jumping = 0;
};
class reverse_find_from_frame_predicat_operator
{
public:
reverse_find_from_frame_predicat_operator(int ci, hash_t name)
: _ci{ci}
, _name{name}
{
inkAssert(ci == -1 || ci == 0, "only support ci == -1, for now!");
}
bool operator()(entry& e)
{
if (reverse_find_predicat(_name, _skip, _jumping, e)) {
if (_ci == _current_frame) {
return true;
}
_current_frame -= 1;
}
return false;
}
private:
int _ci;
int _current_frame = 0;
hash_t _name;
thread_t _skip = ~0U;
uint32_t _jumping = 0;
};
const value* basic_stack::get(hash_t name) const
{
// Find whatever comes first: a matching entry or a stack frame entry
const entry* found = base::reverse_find(reverse_find_predicat_operator(name));
// If nothing found, no value
if (found == nullptr)
return nullptr;
// If we found something of that name, return the value
if (found->name == name)
return &found->data;
// Otherwise, nothing in this stack frame
return nullptr;
}
value* basic_stack::get(hash_t name)
{
// Find whatever comes first: a matching entry or a stack frame entry
entry* found = base::reverse_find(reverse_find_predicat_operator(name));
// If nothing found, no value
if (found == nullptr)
return nullptr;
// If we found something of that name, return the value
if (found->name == name)
return &found->data;
// Otherwise, nothing in this stack frame
return nullptr;
}
value* basic_stack::get_from_frame(int ci, hash_t name)
{
entry* found = base::reverse_find(reverse_find_from_frame_predicat_operator(ci, name));
if (found == nullptr && ci == -1) {
found = base::reverse_find(reverse_find_from_frame_predicat_operator(0, name));
}
if (found == nullptr) {
return nullptr;
}
if (found->name == name) {
return &found->data;
}
return nullptr;
}
template<>
void basic_stack::push_frame<frame_type::function>(offset_t return_to, bool eval)
{
add(InvalidHash, value{}.set<value_type::function_frame>(return_to, eval));
}
template<>
void basic_stack::push_frame<frame_type::tunnel>(offset_t return_to, bool eval)
{
add(InvalidHash, value{}.set<value_type::tunnel_frame>(return_to, eval));
}
template<>
void basic_stack::push_frame<frame_type::thread>(offset_t return_to, bool eval)
{
add(InvalidHash, value{}.set<value_type::thread_frame>(return_to, eval));
}
const entry* basic_stack::pop()
{
return &base::pop([](const entry& elem) { return elem.name == ~0U; });
}
entry* basic_stack::do_thread_jump_pop(const basic_stack::iterator& jumpStart)
{
// Start an iterator right after the jumping marker (might be a thread_start or a jump_marker)
iterator threadIter = jumpStart;
// Get a reference to its jump count
value& start = threadIter.get()->data;
value_type vt = start.type();
auto jump = start.get<value_type::jump_marker>();
// Move over it
threadIter.next();
// Move back over the current jump value
for (uint32_t i = 0; i < jump.thread_id; i++)
threadIter.next();
// Now keep iterating back until we get to a frame marker
// FIXME: meta types or subtypes?
while (! threadIter.done()
&& (threadIter.get()->name != InvalidHash
|| threadIter.get()->data.type() == value_type::thread_start
|| threadIter.get()->data.type() == value_type::thread_end)) {
// If we've hit an end of thread marker
auto e = threadIter.get();
if (e->data.type() == value_type::thread_end) {
// We basically want to skip until we get to the start of this thread (leave the block alone)
thread_t tid = e->data.get<value_type::thread_end>();
while (threadIter.get()->data.type() != value_type::thread_start
|| threadIter.get()->data.get<value_type::thread_start>().jump != tid) {
jump.thread_id++;
threadIter.next();
}
// Now let us skip over the thread start
}
threadIter.next();
jump.thread_id++;
}
// Move us over the frame marker
jump.thread_id++;
// Now that thread marker is set to the correct jump value.
if (vt == value_type::jump_marker) {
start.set<value_type::jump_marker>(jump);
} else if (vt == value_type::thread_start) {
start.set<value_type::thread_start>(jump);
} else {
inkFail("unknown jump type");
}
return threadIter.get();
}
frame_type get_frame_type(value_type type)
{
switch (type) {
case value_type::tunnel_frame: return frame_type::tunnel;
case value_type::function_frame: return frame_type::function;
case value_type::thread_frame: return frame_type::thread;
default: inkAssert(false, "Unknown frame type detected"); return ( frame_type ) -1;
}
}
offset_t basic_stack::pop_frame(frame_type* type, bool& eval)
{
inkAssert(! base::is_empty(), "Can not pop frame from empty callstack.");
const entry* returnedFrame = nullptr;
auto isNull = [](const entry& e) {
return e.name == ~0U;
};
// Start iterating backwards
iterator iter = base::begin();
if (isNull(*iter.get())) {
iter.next(isNull);
}
while (! iter.done()) {
// Keep popping if it's not a frame marker or thread marker of some kind
entry* frame = iter.get();
if (frame->name != InvalidHash) {
pop();
iter = base::begin();
if (isNull(*iter.get())) {
iter.next(isNull);
}
continue;
}
// We now have a frame marker. Check if it's a thread
// Thread handling
if (
// FIXME: is_tghead_marker, is_jump_marker
frame->data.type() == value_type::thread_start
|| frame->data.type() == value_type::thread_end
|| frame->data.type() == value_type::jump_marker
)
{
// End of thread marker, we need to create a jump marker
if (frame->data.type() == value_type::thread_end) {
// Push a new jump marker after the thread end
push({InvalidHash, value{}.set<value_type::jump_marker>(0u, 0u)});
// Do a pop back
returnedFrame = do_thread_jump_pop(base::begin());
break;
}
// If this is a jump marker, we actually want to extend it to the next frame
if (frame->data.type() == value_type::jump_marker) {
// Use the thread jump pop method using this jump marker
returnedFrame = do_thread_jump_pop(iter);
break;
}
// Popping past thread start
if (frame->data.type() == value_type::thread_start) {
returnedFrame = do_thread_jump_pop(iter);
break;
}
}
// Otherwise, pop the frame marker off and return it
returnedFrame = pop();
break;
}
// If we didn't find a frame entry, we never had a frame to return from
inkAssert(returnedFrame, "Attempting to pop_frame when no frames exist! Stack reset.");
// Make sure we're not somehow trying to "return" from a thread
inkAssert(
returnedFrame->data.type() != value_type::thread_start
&& returnedFrame->data.type() != value_type::thread_end,
"Can not return from a thread! How did this happen?"
);
// Store frame type
if (type != nullptr) {
*type = get_frame_type(returnedFrame->data.type());
}
// Return the offset stored in the frame record
// FIXME: correct type?
const auto& frame = returnedFrame->data.get<value_type::function_frame>();
eval = frame.eval;
return frame.addr;
}
bool basic_stack::has_frame(frame_type* returnType) const
{
// Empty case
if (base::is_empty())
return false;
uint32_t jumping = 0;
uint32_t thread = ~0U;
// Search in reverse for a stack frame
const entry* frame = base::reverse_find([&jumping, &thread](const entry& elem) {
// If we're jumping over data, just keep returning false until we're done
if (jumping > 0) {
jumping--;
return false;
}
// We only care about elements with InvalidHash
if (elem.name != InvalidHash)
return false;
// If we're skipping over a thread, wait until we hit its start before checking
if (thread != ~0U) {
if (elem.data.type() == value_type::thread_start
&& elem.data.get<value_type::thread_start>().jump == thread)
thread = ~0U;
return false;
}
// If it's a jump marker or a thread start
if (elem.data.type() == value_type::jump_marker
|| elem.data.type() == value_type::thread_start) {
jumping = elem.data.get<value_type::jump_marker>().thread_id;
return false;
}
// If it's a thread end, we need to skip to the matching thread start
if (elem.data.type() == value_type::thread_end) {
thread = elem.data.get<value_type::thread_end>();
return false;
}
return elem.name == InvalidHash;
});
if (frame != nullptr && returnType != nullptr)
*returnType = get_frame_type(frame->data.type());
// Return true if a frame was found
return frame != nullptr;
}
void basic_stack::clear() { base::clear(); }
void basic_stack::mark_used(string_table& strings, list_table& lists) const
{
// Mark all strings
base::for_each_all([&strings, &lists](const entry& elem) {
if (elem.data.type() == value_type::string) {
strings.mark_used(elem.data.get<value_type::string>());
} else if (elem.data.type() == value_type::list) {
lists.mark_used(elem.data.get<value_type::list>());
}
});
}
thread_t basic_stack::fork_thread()
{
// TODO create unique thread ID
thread_t new_thread = _next_thread++;
// Push a thread start marker here
add(InvalidHash, value{}.set<value_type::thread_start>(new_thread, 0u));
// Set stack jump counter for thread to 0. This number is used if the thread ever
// tries to pop past its origin. It keeps track of how much of the preceeding stack it's popped
// back
return new_thread;
}
void basic_stack::complete_thread(thread_t thread)
{
// Add a thread complete marker
add(InvalidHash, value{}.set<value_type::thread_end>(thread));
}
void basic_stack::collapse_to_thread(thread_t thread)
{
// Reset thread counter
_next_thread = 0;
// If we're restoring a specific thread (and not the main thread)
if (thread != ~0U) {
// Keep popping until we find the requested thread's end marker
const entry* top = pop();
while (
! (top->data.type() == value_type::thread_end
&& top->data.get<value_type::thread_end>() == thread)
) {
inkAssert(
! is_empty(),
"Ran out of stack while searching for end of thread marker. Did you call complete_thread?"
);
top = pop();
}
}
// Now, start iterating backwards
thread_t nulling = ~0U;
uint32_t jumping = 0;
base::reverse_for_each(
[&nulling, &jumping](entry& elem) {
if (jumping > 0) {
// delete data
elem.name = NulledHashId;
// Move on
jumping--;
return;
}
// Thread end. We just need to delete this whole block
if (nulling == ~0U && elem.data.type() == value_type::thread_end
&& elem.name == InvalidHash) {
nulling = elem.data.get<value_type::thread_end>();
}
// If we're deleting a useless thread block
if (nulling != ~0U) {
// If this is the start of the block, stop deleting
if (elem.name == InvalidHash && elem.data.type() == value_type::thread_start
&& elem.data.get<value_type::thread_start>().jump == nulling) {
nulling = ~0U;
}
// delete data
elem.name = NulledHashId;
} else {
// Clear thread start markers. We don't need or want them anymore
if (elem.name == InvalidHash
&& (elem.data.type() == value_type::thread_start
|| elem.data.type() == value_type::jump_marker)) {
// Clear it out
elem.name = NulledHashId;
// Check if this is a jump, if so we need to ignore even more data
jumping = elem.data.get<value_type::jump_marker>().thread_id;
}
// Clear thread frame markers. We can't use them anymore
if (elem.name == InvalidHash && elem.data.type() == value_type::thread_frame) {
elem.name = NulledHashId;
}
}
},
[](entry& elem) { return elem.name == NulledHashId; }
);
// No more threads. Clear next thread counter
_next_thread = 0;
}
void basic_stack::save()
{
base::save();
// Save thread counter
_backup_next_thread = _next_thread;
}
void basic_stack::restore()
{
base::restore();
// Restore thread counter
_next_thread = _backup_next_thread;
}
void basic_stack::forget()
{
base::forget([](entry& elem) { elem.name = ~0U; });
}
entry& basic_stack::add(hash_t name, const value& val) { return base::push({name, val}); }
basic_eval_stack::basic_eval_stack(value* data, size_t size)
: base(data, size)
{
}
void basic_eval_stack::push(const value& val) { base::push(val); }
value basic_eval_stack::pop()
{
return base::pop([](const value& v) { return v.type() == value_type::none; });
}
const value& basic_eval_stack::top() const
{
return base::top([](const value&) { return false; });
}
const value& basic_eval_stack::top_value() const
{
return base::top([](const value& v) { return v.type() == value_type::none; });
}
bool basic_eval_stack::is_empty() const { return base::is_empty(); }
void basic_eval_stack::clear() { base::clear(); }
void basic_eval_stack::mark_used(string_table& strings, list_table& lists) const
{
// Iterate everything (including what we have saved) and mark strings
base::for_each_all([&strings, &lists](const value& elem) {
if (elem.type() == value_type::string) {
string_type str = elem.get<value_type::string>();
if (str.allocated) {
strings.mark_used(str.str);
}
} else if (elem.type() == value_type::list) {
lists.mark_used(elem.get<value_type::list>());
}
});
}
void basic_eval_stack::save() { base::save(); }
void basic_eval_stack::restore() { base::restore(); }
void basic_eval_stack::forget()
{
// Clear out
value x;
x.set<value_type::none>();
value none = value(x);
base::forget([&none](value& elem) { elem = none; });
}
void basic_stack::fetch_values(basic_stack& stack)
{
auto itr = base::begin();
auto predicat = [](entry& e) {
return ! (e.name == InvalidHash || e.data.type() == value_type::value_pointer);
};
if (! itr.done() && predicat(*itr.get())) {
itr.next(predicat);
}
for (; ! itr.done() && itr.get()->name != InvalidHash; itr.next(predicat)) {
auto [name, ci] = itr.get()->data.get<value_type::value_pointer>();
inkAssert(ci != 0, "Global refs should not exists on ref stack!");
inkAssert(ci == -1, "only support ci = -1 for now!");
if (ci == -1) {
set(name, *stack.get(itr.get()->name));
}
}
}
void basic_stack::push_values(basic_stack& stack)
{
for (auto itr = base::begin();
itr.get()->name != InvalidHash && itr.get()->data.type() != value_type::value_pointer;
itr.next()) {
stack.set(itr.get()->name, itr.get()->data);
}
}
size_t basic_stack::snap(unsigned char* data, const snapper& snapper) const
{
unsigned char* ptr = data;
bool should_write = data != nullptr;
ptr = snap_write(ptr, _next_thread, should_write);
ptr = snap_write(ptr, _backup_next_thread, should_write);
ptr += base::snap(data ? ptr : nullptr, snapper);
return static_cast<size_t>(ptr - data);
}
const unsigned char* basic_stack::snap_load(const unsigned char* ptr, const loader& loader)
{
ptr = snap_read(ptr, _next_thread);
ptr = snap_read(ptr, _backup_next_thread);
ptr = base::snap_load(ptr, loader);
return ptr;
}
} // namespace ink::runtime::internal