-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_generic.cpp
More file actions
164 lines (140 loc) · 4.34 KB
/
pz_generic.cpp
File metadata and controls
164 lines (140 loc) · 4.34 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
/*
* Plasma bytecode exection (generic portable version)
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include "pz_common.h"
#include <stdio.h>
#include <string.h>
#include "pz.h"
#include "pz_code.h"
#include "pz_cxx_future.h"
#include "pz_interp.h"
#include "pz_trace.h"
#include "pz_util.h"
#include "pz_generic_closure.h"
#include "pz_generic_run.h"
namespace pz {
/* Must match or exceed ptag_bits from src/core.types.m */
const unsigned num_tag_bits = 2;
const uintptr_t tag_bits = 0x3;
/*
* Run the program
*
******************/
int run(PZ & pz, const Options & options, GCCapability &gc)
{
uint8_t * wrapper_proc = nullptr;
unsigned wrapper_proc_size;
int retcode = 0;
ImmediateValue imv_none;
assert(PZT_LAST_TOKEN < 256);
Context context(gc);
if (!context.allocate()) {
fprintf(stderr, "Could not allocate context\n");
return PZ_EXIT_RUNTIME_ERROR;
}
ScopeExit finalise([&context, &retcode, &options]{
if (!context.release(options.fast_exit())) {
fprintf(stderr, "Error releasing memory\n");
if (retcode == 0) {
retcode = PZ_EXIT_RUNTIME_NONFATAL;
}
}
});
/*
* Assemble a special procedure that exits the interpreter and put its
* address on the call stack.
*/
memset(&imv_none, 0, sizeof(imv_none));
wrapper_proc_size = write_instr(nullptr, 0, PZI_END);
wrapper_proc =
static_cast<uint8_t *>(context.alloc_bytes(wrapper_proc_size, META));
heap_set_meta_info(&context.heap(), wrapper_proc, nullptr);
write_instr(wrapper_proc, 0, PZI_END);
context.return_stack[0] = nullptr;
// Wrapper proc is tracablo here.
context.return_stack[1] = wrapper_proc;
context.rsp = 1;
// Determine the entry procedure.
Library * program = pz.program_lib();
Closure * entry_closure = program ? program->entry_closure() : nullptr;
if (!entry_closure) {
fprintf(stderr, "No entry closure\n");
return PZ_EXIT_RUNTIME_ERROR;
}
PZOptEntrySignature entry_signature = program->entry_signature();
switch (entry_signature) {
case PZ_OPT_ENTRY_SIG_PLAIN:
break;
case PZ_OPT_ENTRY_SIG_ARGS:
fprintf(stderr,
"Unsupported, cannot execute programs that "
"accept command line arguments. (Bug #283)\n");
return PZ_EXIT_RUNTIME_ERROR;
}
#ifdef PZ_DEV
trace_enabled = options.interp_trace();
#endif
int program_retcode =
generic_main_loop(context, &pz.heap(), entry_closure, pz);
retcode = program_retcode ? program_retcode : retcode;
return retcode;
}
Context::Context(GCCapability & gc)
: AbstractGCTracer(gc)
, ip(nullptr)
, env(nullptr)
, return_stack("return stack")
, rsp(0)
, expr_stack("expression stack")
, esp(0)
{}
Context::~Context()
{
assert(!return_stack.is_mapped());
assert(!expr_stack.is_mapped());
}
bool Context::allocate() {
if (!return_stack.allocate_guarded(RETURN_STACK_SIZE * sizeof(uint8_t*))) {
return false;
}
if (!expr_stack.allocate_guarded(EXPR_STACK_SIZE * sizeof(StackValue))) {
return false;
}
return true;
}
bool Context::release(bool fast) {
if (fast) {
return_stack.forget();
expr_stack.forget();
return true;
}
bool result = true;
if (!return_stack.release()) {
result = false;
}
if (!expr_stack.release()) {
result = false;
}
return result;
}
void Context::do_trace(HeapMarkState * state) const
{
/*
* The +1 is required here because the callee will only mark the first N
* bytes in these memory areas, and esp and rsp are zero-based indexes,
* So if esp is 2, which means the 3rd (0-based) index is the
* top-of-stack. Then we need (2+1)*sizeof(...) to ensure we mark all
* three items.
*/
state->mark_root_conservative((void*)expr_stack.ptr(),
(esp + 1) * sizeof(StackValue));
state->mark_root_conservative_interior((void*)return_stack.ptr(),
(rsp + 1) * WORDSIZE_BYTES);
state->mark_root_interior(ip);
state->mark_root(env);
}
} // namespace pz