-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_main.cpp
More file actions
165 lines (143 loc) · 4.58 KB
/
pz_main.cpp
File metadata and controls
165 lines (143 loc) · 4.58 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
/*
* Plasma bytecode execution
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*
* This program executes plasma bytecode.
*/
#include <stdio.h>
#include "pz_common.h"
#include "pz.h"
#include "pz_builtin.h"
#include "pz_gc.h"
#include "pz_gc.impl.h"
#include "pz_interp.h"
#include "pz_option.h"
#include "pz_read.h"
#include "pz_util.h"
using namespace pz;
static int run(Options & options);
static void help(const char * progname, FILE * stream);
static void version(void);
int main(int argc, char * const argv[])
{
Options options;
Options::Mode mode = options.parse(argc, argv);
switch (mode) {
case Options::Mode::HELP:
help(argv[0], stdout);
return EXIT_SUCCESS;
case Options::Mode::VERSION:
version();
return EXIT_SUCCESS;
case Options::Mode::ERROR:
if (options.error_message()) {
fprintf(stderr, "%s: %s\n", argv[0], options.error_message());
}
help(argv[0], stderr);
return EXIT_FAILURE;
case Options::Mode::NORMAL:
return run(options);
}
}
static bool setup_program(PZ & pz, Options & options, GCCapability & gc);
static int run(Options & options)
{
MemoryBase::init_statics();
Heap heap(options);
if (!heap.init()) {
fprintf(stderr, "Couldn't initialise memory.\n");
return PZ_EXIT_RUNTIME_ERROR;
}
int retcode = 0;
ScopeExit finalise([&heap, &options, &retcode] {
if (!heap.finalise(options.fast_exit())) {
if (retcode == 0) {
retcode = PZ_EXIT_RUNTIME_NONFATAL;
}
}
});
PZ pz(options, heap);
heap.set_roots_tracer(pz);
GCThreadHandle gc(heap);
if (setup_program(pz, options, gc)) {
int program_retcode = run(pz, options, gc);
retcode = program_retcode ? program_retcode : retcode;
} else {
retcode = PZ_EXIT_RUNTIME_ERROR;
}
return retcode;
}
static void split_filenames(const std::string & filenames,
std::string & bytecode, Optional<std::string> & native)
{
size_t pos = filenames.find_first_of(':');
if (pos == std::string::npos) {
bytecode = filenames;
native = Optional<std::string>();
} else {
bytecode = filenames.substr(0, pos);
native = Optional<std::string>(filenames.substr(pos+1));
}
}
static bool setup_program(PZ & pz, Options & options, GCCapability & gc0)
{
GCTracer gc(gc0);
Library * builtins = pz.new_library(String("Builtin"), gc);
setup_builtins(builtins, pz);
for (const std::string & filenames : options.pzlibs()) {
std::string bytecode_filename;
Optional<std::string> native_filename;
split_filenames(filenames, bytecode_filename, native_filename);
Root<Vector<String>> names(gc);
{
NoGCScope no_gc(gc);
names = new(no_gc) Vector<String>(no_gc);
no_gc.abort_if_oom("setup_program");
}
Root<Library> lib(gc);
if (!read(pz, bytecode_filename, native_filename, lib,
names.ptr(), gc))
{
return false;
}
for (auto& name : names.get()) {
pz.add_library(name, lib.ptr());
}
}
Root<Library> program(gc);
std::string bytecode_filename;
Optional<std::string> native_filename;
split_filenames(options.pzfile(), bytecode_filename, native_filename);
if (!read(pz, bytecode_filename, native_filename, program,
nullptr, gc))
{
return false;
}
pz.add_program_lib(program.ptr());
return true;
}
static void help(const char * progname, FILE * stream)
{
fprintf(stream, "Plasma runtime\n\n");
fprintf(stream, " Run plasma bytecode programs\n\n");
fprintf(stream, "Usage:\n\n");
fprintf(stream, " %s [-v] (-l <PZ LIB>) <PZ FILE> <program args>\n",
progname);
fprintf(stream, " %s -h\n", progname);
fprintf(stream, " %s -V\n\n", progname);
fprintf(stream, "Options:\n\n");
fprintf(stream, " -h Show the help message (this one).\n");
fprintf(stream, " -V Show version information.\n");
fprintf(stream, " -v Verbose bytecode loading.\n");
fprintf(stream, " -l Dynamic link this bytecode library.\n\n");
}
static void version(void)
{
printf("Plasma Runtime, " PLASMA_VERSION_STRING "\n");
printf("https://plasmalang.org\n");
printf("Copyright (C) 2015-2025 The Plasma Team\n");
printf("Distributed under the MIT License\n");
}