-
Notifications
You must be signed in to change notification settings - Fork 4
/
mcompile.c
504 lines (436 loc) · 13.8 KB
/
mcompile.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
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
/*
* Copyright (c) 1993-2012 David Gay and Gustav Hållberg
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose, without fee, and without written agreement is hereby granted,
* provided that the above copyright notice and the following two paragraphs
* appear in all copies of this software.
*
* IN NO EVENT SHALL DAVID GAY OR GUSTAV HALLBERG BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF DAVID GAY OR
* GUSTAV HALLBERG HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* DAVID GAY AND GUSTAV HALLBERG SPECIFICALLY DISCLAIM ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
* "AS IS" BASIS, AND DAVID GAY AND GUSTAV HALLBERG HAVE NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*/
#include "mudlle-config.h"
#include <stdlib.h>
#include <string.h>
#include "alloc.h"
#include "calloc.h"
#include "code.h"
#include "global.h"
#include "ins.h"
#include "lexer.h"
#include "mcompile.h"
#include "module.h"
#include "mvalues.h"
#include "print.h"
#include "strbuf.h"
#include "tree.h"
#include "utils.h"
/* A list of global variable indexes */
struct glist {
struct glist *next;
ulong n;
bool used;
struct loc loc;
};
static struct glist *new_glist(struct alloc_block *heap, ulong n,
const struct loc *loc, struct glist *next)
{
struct glist *newp = allocate(heap, sizeof *newp);
*newp = (struct glist){
.next = next,
.n = n,
.used = false,
.loc = *loc
};
return newp;
}
static bool in_glist(ulong n, struct glist *l, bool do_mark)
{
for (; l; l = l->next)
if (n == l->n)
{
if (do_mark)
l->used = true;
return true;
}
return false;
}
static struct glist *readable;
static bool all_readable;
static struct glist *writable;
static bool all_writable;
static struct glist *definable;
static struct string *this_module;
/* A list of imported modules */
struct mlist {
struct mlist *next;
struct vlist *var;
enum module_status status;
bool used;
};
static struct mlist *imported_modules;
static struct mlist *new_mlist(struct alloc_block *heap, struct vlist *var,
enum module_status status, struct mlist *next)
{
struct mlist *newp = allocate(heap, sizeof *newp);
*newp = (struct mlist){
.next = next,
.var = var,
.status = status,
.used = false
};
return newp;
}
static enum module_status imported(const char *name, int do_mark)
/* Returns: status of name in imported_modules, module_unloaded if absent
*/
{
for (struct mlist *mods = imported_modules; mods; mods = mods->next)
if (strcasecmp(mods->var->var, name) == 0)
{
if (do_mark)
mods->used = 1;
return mods->status;
}
return module_unloaded;
}
static struct mfile *current_mfile;
bool mstart(struct alloc_block *heap, struct mfile *f, seclev_t seclev)
/* Effects: Start processing module f:
- unload f
- load required modules
- change status of variables of f (defines, writes)
- setup information for mrecall/massign/mexecute
Sends error/warning messages.
Returns: true if compilation can proceed
*/
{
assert(current_mfile == NULL);
erred = false;
struct mlist *lmodules = NULL;
if (f->name)
{
if (module_status(f->name) == module_loaded
&& module_seclevel(f->name) > seclev)
return false;
if (!module_unload(f->name))
return false;
module_set(f->name, module_loading, seclev);
}
/* Load all modules */
for (struct vlist *mods = f->requires; mods; mods = mods->next)
{
enum module_status mstatus = module_require(mods->var);
if (mstatus < module_loaded)
{
if (mstatus == module_loading)
compile_error(&mods->loc, "loop in requires of %s", mods->var);
else
compile_error(&mods->loc, "failed to load %s", mods->var);
goto early_errout;
}
lmodules = new_mlist(heap, mods, mstatus, lmodules);
}
current_mfile = f;
all_writable = f->vclass == f_plain;
all_readable = f->vclass == f_plain;
readable = writable = definable = NULL;
if (f->name)
this_module = make_readonly(alloc_string(f->name));
else
this_module = NULL;
imported_modules = lmodules;
/* Change status of variables */
for (struct vlist *defines = f->defines; defines; defines = defines->next)
{
ulong n = global_lookup(defines->var);
struct string *omod;
enum vstatus ostatus = module_vstatus(n, &omod);
if (ostatus == var_system_write)
compile_error(&defines->loc,
"cannot define %s: cannot be written from mudlle",
defines->var);
else if ((ostatus == var_write && seclev < SECLEVEL_GLOBALS)
|| ostatus == var_system_write)
compile_error(&defines->loc,
"cannot define %s: exists and is writable", defines->var);
else if (!module_vset(n, var_module, this_module))
compile_error(&defines->loc,
"cannot define %s: belongs to module %s", defines->var,
omod->str);
else if (ostatus == var_write)
compile_warning(&defines->loc, "%s was writable", defines->var);
definable = new_glist(heap, n, &defines->loc, definable);
}
for (struct vlist *writes = f->writes; writes; writes = writes->next)
{
ulong n = global_lookup(writes->var);
if (in_glist(n, definable, false))
compile_error(&writes->loc, "cannot write and define %s", writes->var);
if (!module_vset(n, var_write, NULL))
{
struct string *belongs;
enum vstatus vs = module_vstatus(n, &belongs);
switch (vs)
{
case var_system_write:
compile_error(&writes->loc,
"cannot write %s from mudlle", writes->var);
break;
case var_system_mutable:
compile_warning(&writes->loc, "%s is always writable",
writes->var);
break;
case var_module:
assert(TYPE(belongs, string));
compile_error(&writes->loc,
"cannot write %s: belongs to module %s", writes->var,
belongs->str);
break;
case var_normal:
case var_write:
abort();
}
continue;
}
writable = new_glist(heap, n, &f->loc, writable);
}
for (struct vlist *reads = f->reads; reads; reads = reads->next)
{
ulong n = global_lookup(reads->var);
if (in_glist(n, definable, false))
compile_error(&reads->loc, "cannot read and define %s", reads->var);
readable = new_glist(heap, n, &reads->loc, readable);
}
for (struct vlist *statics = f->statics; statics; statics = statics->next)
{
ulong n;
if (!global_exists(statics->var, &n))
continue;
if (in_glist(n, definable, false))
compile_error(&statics->loc, "cannot define static %s", statics->var);
if (in_glist(n, writable, false))
compile_error(&statics->loc, "cannot write static %s", statics->var);
if (in_glist(n, readable, false))
compile_error(&statics->loc, "cannot read static %s", statics->var);
}
if (erred)
goto errout;
return true;
errout:
mstop(f);
early_errout:
if (f->name)
module_set(f->name, module_error, seclev);
return false;
}
void mstop(struct mfile *f)
{
assert(current_mfile == f);
current_mfile = NULL;
}
void mrecall(const struct loc *loc, ulong n, const char *name,
struct fncode *fn)
/* Effects: Generate code to recall variable n
*/
{
assert(current_mfile != NULL);
struct string *mod;
enum vstatus status = module_vstatus(n, &mod);
if (in_glist(n, definable, false)
|| in_glist(n, readable, true)
|| in_glist(n, writable, false)
|| status == var_system_write
|| status == var_system_mutable)
;
else if (status == var_module)
{
/* Implicitly import protected modules */
if (module_status(mod->str) == module_protected)
{
imported(mod->str, 1);
if (immutablep(GVAR(n))) /* Use value */
{
ins_constant(GVAR(n), fn);
return;
}
}
else if (!all_readable && imported(mod->str, 1) == module_unloaded)
compile_error(loc, "read of global %s (module %s)", name, mod->str);
}
else if (!all_readable)
compile_error(loc, "read of global %s", name);
ins2(op_recall_global, n, fn);
}
void mexecute(const struct loc *loc, ulong n, const char *name, int count,
struct fncode *fn)
/* Effects: Generates code to call function in variable n, with count
arguments
*/
{
assert(current_mfile != NULL);
if (name == NULL)
goto skip_checks;
if (in_glist(n, definable, false) || in_glist(n, readable, true)
|| in_glist(n, writable, false))
goto skip_checks;
struct string *mod;
enum vstatus status = module_vstatus(n, &mod);
if (status == var_system_write || status == var_system_mutable)
;
else if (status == var_module)
{
/* Implicitly import protected modules */
if (module_status(mod->str) == module_protected)
{
value gvar = GVAR(n);
imported(mod->str, 1);
if (TYPE(gvar, primitive))
{
if (count >= 1 && count <= 2
&& ((struct primitive *)gvar)->op->nargs == count)
{
if (count == 1)
ins2(op_execute_primitive_1arg, n, fn);
else
ins2(op_execute_primitive_2arg, n, fn);
}
else
{
/* Could merge, but can't be bothered... */
ins2(op_recall_global, n, fn);
if (count <= ARG1_MAX)
ins1(op_execute_primitive, count, fn);
else
ins2(op_execute_primitive2, count, fn);
}
return;
}
if (TYPE(gvar, secure))
{
/* Could merge, but can't be bothered... */
ins2(op_recall_global, n, fn);
if (count <= ARG1_MAX)
ins1(op_execute_secure, count, fn);
else
ins2(op_execute_secure2, count, fn);
return;
}
if (TYPE(gvar, varargs))
{
/* Could merge, but can't be bothered... */
ins2(op_recall_global, n, fn);
if (count <= ARG1_MAX)
ins1(op_execute_varargs, count, fn);
else
ins2(op_execute_varargs2, count, fn);
return;
}
}
else if (!all_readable && imported(mod->str, 1) == module_unloaded)
compile_error(loc, "read of global %s (module %s)", name, mod->str);
}
else if (!all_readable)
compile_error(loc,"read of global %s", name);
skip_checks:
if (count == 1)
ins2(op_execute_global_1arg, n, fn);
else if (count == 2)
ins2(op_execute_global_2arg, n, fn);
else
{
/* Could have an op_execute_global */
ins2(op_recall_global, n, fn);
if (count <= ARG1_MAX)
ins1(op_execute, count, fn);
else
ins2(op_execute2, count, fn);
}
}
bool mwritable(const struct loc *loc, ulong n, const char *name)
{
if (all_writable || in_glist(n, writable, true))
{
struct string *mod;
if (module_vstatus(n, &mod) != var_write)
module_vset(n, var_write, NULL);
return true;
}
if (GMUTABLE(n))
return true;
compile_error(loc, "write of global %s", name);
return false;
}
void massign(const struct loc *loc, ulong n, const char *name,
struct fncode *fn)
/* Effects: Generate code to assign to variable n
*/
{
assert(current_mfile != NULL);
struct string *mod;
enum vstatus status = module_vstatus(n, &mod);
if (status == var_module)
if (mod == this_module && fntoplevel(fn))
{
if (!in_glist(n, definable, true))
abort();
/* defined here */
ins2(op_define, n, fn);
}
else
compile_error(loc, "write of global %s (module %s)", name, mod->str);
else if (mwritable(loc, n, name))
ins2(op_assign_global, n, fn);
}
void mwarn_module(seclev_t seclev, struct block *b)
{
assert(current_mfile != NULL);
for (struct mlist *ml = imported_modules; ml; ml = ml->next)
if (!ml->used)
compile_warning(&ml->var->loc,
"symbols from required module %s were never used",
ml->var->var);
for (struct glist *gl = readable; gl; gl = gl->next)
{
if (!gl->used)
compile_warning(&gl->loc, "readable global %s was never read",
GNAME(gl->n)->str);
struct string *belongs;
if (module_vstatus(gl->n, &belongs) == var_module)
{
int mlev = module_seclevel(belongs->str);
struct strbuf sblev = SBNULL;
if (mlev < seclev)
{
sb_addstr(&sblev, " (lvl ");
sb_add_seclevel(&sblev, mlev);
sb_addstr(&sblev, ")");
}
compile_warning(&gl->loc, "reads global variable %s defined in %s%s",
GNAME(gl->n)->str, belongs->str, sb_str(&sblev));
sb_free(&sblev);
}
}
for (struct glist *gl = writable; gl; gl = gl->next)
if (!gl->used)
compile_warning(&gl->loc, "writable global %s was never written",
GNAME(gl->n)->str);
for (struct glist *gl = definable; gl; gl = gl->next)
if (!gl->used)
compile_warning(&gl->loc,
"definable variable %s was never defined",
GNAME(gl->n)->str);
}
void mcompile_init(void)
{
staticpro(&this_module);
}