forked from perl11/potion
/
potion.c
249 lines (226 loc) · 7.33 KB
/
potion.c
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
//
// potion.c
// the Potion!
//
// (c) 2008 why the lucky stiff, the freelance professor
//
#include <stdio.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "potion.h"
#include "internal.h"
#include "opcodes.h"
#include "khash.h"
#include "table.h"
const char potion_banner[] = "potion " POTION_VERSION
" (date='" POTION_DATE "', commit='" POTION_COMMIT
"', platform='" POTION_PLATFORM "', jit=%d)\n";
const char potion_version[] = POTION_VERSION;
static void potion_cmd_usage() {
printf("usage: potion [options] [script] [arguments]\n"
" -B, --bytecode run with bytecode VM (slower, but cross-platform)\n"
" -X, --x86 run with x86 JIT VM (faster, x86 and x86-64)\n"
" -I, --inspect print only the return value\n"
" -V, --verbose show bytecode and ast info\n"
" -c, --compile compile the script to bytecode\n"
" -h, --help show this helpful stuff\n"
" -v, --version show version\n"
"(default: %s)\n",
#if POTION_JIT == 1
"-X"
#else
"-B"
#endif
);
}
static void potion_cmd_stats(void *sp) {
Potion *P = potion_create(sp);
printf("sizeof(PN=%d, PNObject=%d, PNTuple=%d, PNTuple+1=%d, PNTable=%d)\n",
(int)sizeof(PN), (int)sizeof(struct PNObject), (int)sizeof(struct PNTuple),
(int)(sizeof(PN) + sizeof(struct PNTuple)), (int)sizeof(struct PNTable));
printf("GC (fixed=%ld, actual=%ld, reserved=%ld)\n",
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
PN_INT(potion_gc_reserved(P, 0, 0)));
potion_destroy(P);
}
static void potion_cmd_version() {
printf(potion_banner, POTION_JIT);
}
static void potion_cmd_compile(char *filename, int exec, int verbose, void *sp) {
PN buf;
int fd = -1;
struct stat stats;
Potion *P = potion_create(sp);
if (stat(filename, &stats) == -1) {
fprintf(stderr, "** %s does not exist.", filename);
goto done;
}
fd = open(filename, O_RDONLY | O_BINARY);
if (fd == -1) {
fprintf(stderr, "** could not open %s. check permissions.", filename);
goto done;
}
buf = potion_bytes(P, stats.st_size + 1);
if (read(fd, PN_STR_PTR(buf), stats.st_size) == stats.st_size) {
PN code;
PN_STR_PTR(buf)[stats.st_size] = '\0';
code = potion_source_load(P, PN_NIL, buf);
if (PN_IS_PROTO(code)) {
if (verbose > 1)
printf("\n\n-- loaded --\n");
} else {
code = potion_parse(P, buf);
if (PN_TYPE(code) == PN_TERROR) {
potion_send(potion_send(code, PN_string), PN_print);
goto done;
}
if (verbose > 1) {
printf("\n-- parsed --\n");
potion_send(potion_send(code, PN_string), PN_print);
printf("\n");
}
code = potion_send(code, PN_compile, potion_str(P, filename), PN_NIL);
if (verbose > 1)
printf("\n-- compiled --\n");
}
if (verbose > 1) {
potion_send(potion_send(code, PN_string), PN_print);
printf("\n");
}
if (exec == 1) {
code = potion_vm(P, code, P->lobby, PN_NIL, 0, NULL);
if (verbose > 1)
printf("\n-- vm returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", (void *)code,
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
PN_INT(potion_gc_reserved(P, 0, 0)));
if (verbose) {
potion_send(potion_send(code, PN_string), PN_print);
printf("\n");
}
} else if (exec == 2) {
#if POTION_JIT == 1
PN val;
PN cl = potion_closure_new(P, (PN_F)potion_jit_proto(P, code, POTION_JIT_TARGET), PN_NIL, 1);
PN_CLOSURE(cl)->data[0] = code;
val = PN_PROTO(code)->jit(P, cl, P->lobby);
if (verbose > 1)
printf("\n-- jit returned %p (fixed=%ld, actual=%ld, reserved=%ld) --\n", PN_PROTO(code)->jit,
PN_INT(potion_gc_fixed(P, 0, 0)), PN_INT(potion_gc_actual(P, 0, 0)),
PN_INT(potion_gc_reserved(P, 0, 0)));
if (verbose) {
potion_send(potion_send(val, PN_string), PN_print);
printf("\n");
}
#else
fprintf(stderr, "** potion built without JIT support\n");
#endif
} else {
char pnbpath[255];
FILE *pnb;
sprintf(pnbpath, "%sb", filename);
pnb = fopen(pnbpath, "wb");
if (!pnb) {
fprintf(stderr, "** could not open %s for writing. check permissions.", pnbpath);
goto done;
}
code = potion_source_dump(P, PN_NIL, code);
if (fwrite(PN_STR_PTR(code), 1, PN_STR_LEN(code), pnb) == PN_STR_LEN(code)) {
printf("** compiled code saved to %s\n", pnbpath);
printf("** run it with: potion %s\n", pnbpath);
fclose(pnb);
} else {
fprintf(stderr, "** could not write all bytecode.");
}
}
#if 0
void *scanptr = (void *)((char *)P->mem->old_lo + (sizeof(PN) * 2));
while ((PN)scanptr < (PN)P->mem->old_cur) {
printf("%p.vt = %lx (%u)\n",
scanptr, ((struct PNObject *)scanptr)->vt,
potion_type_size(P, scanptr));
if (((struct PNFwd *)scanptr)->fwd != POTION_FWD && ((struct PNFwd *)scanptr)->fwd != POTION_COPIED) {
if (((struct PNObject *)scanptr)->vt < 0 || ((struct PNObject *)scanptr)->vt > PN_TUSER) {
printf("wrong type for allocated object: %p.vt = %lx\n",
scanptr, ((struct PNObject *)scanptr)->vt);
break;
}
}
scanptr = (void *)((char *)scanptr + potion_type_size(P, scanptr));
if ((PN)scanptr > (PN)P->mem->old_cur) {
printf("allocated object goes beyond GC pointer\n");
break;
}
}
#endif
} else {
fprintf(stderr, "** could not read entire file.");
}
done:
if (fd != -1)
close(fd);
if (P != NULL)
potion_destroy(P);
}
int main(int argc, char *argv[]) {
POTION_INIT_STACK(sp);
int i, verbose = 0, exec = 1 + POTION_JIT;
if (argc > 1) {
for (i = 0; i < argc; i++) {
if (strcmp(argv[i], "-I") == 0 ||
strcmp(argv[i], "--inspect") == 0) {
verbose = 1;
continue;
}
if (strcmp(argv[i], "-V") == 0 ||
strcmp(argv[i], "--verbose") == 0) {
verbose = 2;
continue;
}
if (strcmp(argv[i], "-v") == 0 ||
strcmp(argv[i], "--version") == 0) {
potion_cmd_version();
return 0;
}
if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "--help") == 0) {
potion_cmd_usage();
return 0;
}
if (strcmp(argv[i], "-s") == 0 ||
strcmp(argv[i], "--stats") == 0) {
potion_cmd_stats(sp);
return 0;
}
if (strcmp(argv[i], "-c") == 0 ||
strcmp(argv[i], "--compile") == 0) {
exec = 0;
}
if (strcmp(argv[i], "-B") == 0 ||
strcmp(argv[i], "--bytecode") == 0) {
exec = 1;
}
if (strcmp(argv[i], "-X") == 0 ||
strcmp(argv[i], "--x86") == 0) {
exec = 2;
}
}
potion_cmd_compile(argv[argc-1], exec, verbose, sp);
return 0;
}
Potion *P = potion_create(sp);
potion_eval(P, potion_byte_str(P,
"loop:\n" \
" '>> ' print\n" \
" code = read\n" \
" if (not code): break.\n" \
" if (code != ''):\n" \
" obj = code eval\n" \
" if (obj kind == Error):\n" \
" obj string print." \
" else: ('=> ', obj, \"\\n\") join print.\n" \
"_ loop"));
potion_destroy(P);
return 0;
}