Debian Codegraph @

utils/xml2pmx/xml2pmx-src/xmain.c

raw
1 /*
2 * xmain.c
3 *
4 * This file is part of the Oxford Oberon-2 compiler
5 * Copyright (c) 2006--2016 J. M. Spivey
6 * All rights reserved
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #define main xmain
32
33 #include <string.h>
34 #include <stdlib.h>
35
36 #define EXTERN
37 #include "obx.h"
38 #include "keiko.h"
39 #include "exec.h"
40
41 #ifdef JIT
42 #include "vm.h"
43 #ifdef DEBUG
44 #define JTEST 1
45 #endif
46 #endif
47
48 #ifdef PROFILE
49 #define MYNAME "profiler"
50 #else
51 #ifdef OBXDEB
52 #define MYNAME "debugging monitor"
53 #else
54 #define MYNAME "runtime system"
55 #endif
56 #endif
57
58 const char *version =
59 "Oxford Oberon-2 " MYNAME " version " PACKAGE_VERSION " [build " REVID "]"
60 #ifdef JIT
61 " (JIT)"
62 #else
63 ""
64 #endif
65 #ifdef DEBUG
66 " (debug)"
67 #else
68 ""
69 #endif
70 ;
71 extern const char *lib_version;
72 const char *copyright = "Copyright (C) 1999--2012 J. M. Spivey";
73
74 extern int vm_debug;
75
76 /* Helper functions for the loader */
77
78 void make_module(char *name, word addr, int chksum, int nlines) {
79 module m = scratch_alloc_atomic(sizeof(struct _module));
80 static int nm = 0;
81
82 if (modtab == NULL)
83 modtab = scratch_alloc_atomic(nmods * sizeof(module));
84
85 m->m_name = name;
86 m->m_addr =addr;
87 #ifdef PROFILE
88 m->m_nlines = nlines;
89 m->m_lcount = NULL;
90 if (lflag && nlines > 0) {
91 m->m_lcount = scratch_alloc_atomic(nlines * sizeof(unsigned));
92 memset(m->m_lcount, 0, nlines * sizeof(int));
93 }
94 #endif
95 #ifdef OBXDEB
96 debug_message("module %s %#x", name, chksum);
97 #endif
98 if (nm >= nmods) panic("Too many modules");
99 modtab[nm++] = m;
100 }
101
102 void make_proc(char *name, word addr) {
103 proc p = scratch_alloc_atomic(sizeof(struct _proc));
104 static int np = 0;
105
106 if (proctab == NULL)
107 proctab = scratch_alloc_atomic(nprocs * sizeof(proc));
108
109 p->p_name = name;
110 p->p_addr = addr;
111 #ifdef PROFILE
112 p->p_calls = p->p_rec = p->p_self = p->p_child = 0;
113 p->p_parents = p->p_children = NULL;
114 #endif
115 #ifdef OBXDEB
116 value *cp = ptrcast(value, p->p_addr);
117 debug_message("proc %s %#x %#x %d", name, addr,
118 cp[CP_CODE].a, cp[CP_SIZE].i);
119 #endif
120 if (np >= nprocs) panic("Too many procs");
121 proctab[np++] = p;
122 }
123
124 void make_symbol(const char *kind, char *name, word addr) {
125 #ifdef OBXDEB
126 debug_message("%s %s %#x", kind, name, addr);
127 #endif
128 }
129
130 /* fix_sizes -- calculate module lengths */
131 void fix_sizes(int dseg) {
132 word p = dsegaddr(dmem + dseg);
133 int i;
134
135 for (i = nmods-1; i >= 0; i--) {
136 modtab[i]->m_length = p - modtab[i]->m_addr;
137 p = modtab[i]->m_addr;
138 }
139 }
140
141
142 /* Runtime errors */
143
144 #ifndef OBXDEB
145 #define TOP 5 /* Number of frames shown at top and bottom */
146 #define BOT 5
147 #define GAP 10 /* Don't omit less than this many frames */
148 #define NBUF (BOT+GAP-1)
149
150 static void backtrace(value *bp) {
151 value *fp = bp, *cp = valptr(bp[CP]);
152 proc p = find_proc(dsegaddr(cp));
153 int n, j;
154 proc fbuf[NBUF];
155
156 fprintf(stderr, "In procedure %s\n", p->p_name);
157
158 /* Chain down the stack, printing the first TOP frames,
159 and saving the last NBUF in a circular buffer. */
160 for (n = 0;; n++) {
161 /* Each frame contains the cp and bp of its caller */
162 fp = valptr(fp[BP]); /* Base pointer of next frame */
163 if (fp == NULL) break;
164 cp = valptr(fp[CP]); /* Constant pool of next frame */
165 fbuf[n%NBUF] = p = find_proc(dsegaddr(cp));
166 if (n < TOP)
167 fprintf(stderr, " called from %s\n", p->p_name);
168 }
169
170 /* Now the last NBUF frames are f(n-NBUF), ..., f(n-1)
171 where f(i) = fbuf[i%NBUF] -- unless there are fewer
172 then NBUF frames in all. */
173
174 if (n < TOP+GAP+BOT)
175 /* Print the n-TOP frames not printed already */
176 j = TOP;
177 else {
178 /* Omit n-(TOP+BOT) frames (at least GAP) and print the
179 last BOT frames */
180 fprintf(stderr, " ... %d intervening frames omitted ...\n",
181 n-(TOP+BOT));
182 j = n-BOT;
183 }
184
185 /* Print frames j, ..., n-1 */
186 for (; j < n; j++)
187 fprintf(stderr, " called from %s\n", fbuf[j%NBUF]->p_name);
188 }
189 #endif
190
191 static const char *message(int code) {
192 switch (code) {
193 case E_CAST:
194 return "dynamic type error in cast";
195 case E_ASSIGN:
196 return "dynamic type error in record assignment";
197 case E_CASE:
198 return "no matching label in CASE statement";
199 case E_WITH:
200 return "no matching type guard in WITH statement";
201 case E_RETURN:
202 return "function failed to return a result";
203 case E_BOUND:
204 return "array bound error";
205 case E_NULL:
206 return "null pointer error";
207 case E_DIV:
208 return "DIV or MOD by zero";
209 case E_FDIV:
210 return "division by zero";
211 case E_STACK:
212 return "stack overflow";
213 case E_GLOB:
214 return "assignment of local procedure";
215 default:
216 return "the impossible has happened";
217 }
218 }
219
220 /* error_stop -- runtime error with explicit message text */
221 void error_stop(const char *msg, int val, int line, value *bp, uchar *pc) {
222 value *cp = valptr(bp[CP]);
223
224 #ifdef OBXDEB
225 char buf[256];
226 sprintf(buf, msg, val);
227 debug_break(cp, bp, pc, "error %d %s", line, buf);
228 #else
229 module mod = find_module(dsegaddr(cp));
230
231 fprintf(stderr, "Runtime error: ");
232 fprintf(stderr, msg, val);
233 if (line > 0) fprintf(stderr, " on line %d", line);
234 if (mod != NULL && strcmp(mod->m_name, "_Builtin") != 0)
235 fprintf(stderr, " in module %s", mod->m_name);
236 fprintf(stderr, "\n");
237 fflush(stderr);
238
239 if (nprocs == 0)
240 fprintf(stderr, "(No debugging information available)\n");
241 else if (bp != NULL)
242 backtrace(bp);
243
244 fflush(stderr);
245 #endif
246
247 exit(2);
248 }
249
250 /* runtime_error -- report a runtime error */
251 void runtime_error(int m, int line, value *bp, uchar *pc) {
252 error_stop(message(m), 0, line, bp, pc);
253 }
254
255 /* rterror -- simple version of runtime_error for JIT */
256 void rterror(int num, int line, value *bp) {
257 runtime_error(num, line, bp, NULL);
258 }
259
260 /* stkoflo -- stack overflow handler for JIT */
261 void stkoflo(value *bp) {
262 runtime_error(E_STACK, 0, bp, NULL);
263 }
264
265
266 /* Startup */
267
268 #define argc saved_argc
269 #define argv saved_argv
270
271 static void run(value *prog) {
272 value *sp;
273
274 /* Allow 32-word safety margin (for tracing) */
275 sp = (value *) (stack + stack_size) - 32;
276
277 sp -= HEAD;
278 sp[BP].a = 0;
279 sp[PC].a = 0;
280 sp[CP].a = dsegaddr(prog);
281 primcall(prog, sp);
282 }
283
284 #ifndef PRELOAD
285 mybool custom_file(char *name) {
286 char buf[4];
287 FILE *fp;
288 int nread;
289 mybool result;
290
291 fp = fopen(name, "rb");
292 if (fp == NULL) return FALSE;
293 fseek(fp, - (long) sizeof(trailer), SEEK_END);
294 nread = fread(buf, 1, 4, fp);
295 if (nread < 4 || strncmp(buf, MAGIC, 4) != 0)
296 result = FALSE;
297 else {
298 fseek(fp, 0, SEEK_SET);
299 nread = fread(buf, 1, 2, fp);
300 result = (nread == 2 && strncmp(buf, "#!", 2) != 0);
301 }
302 fclose(fp);
303 return result;
304 }
305
306 #ifdef WINDOWS
307 #include <windows.h>
308 #include <winbase.h>
309
310 char *search_path(char *name) {
311 static char buf[_MAX_PATH];
312 char *filepart;
313
314 if (SearchPath(NULL, name, ".exe", _MAX_PATH, buf, &filepart) == 0)
315 return NULL;
316
317 return buf;
318 }
319 #else
320 #include <sys/stat.h>
321 #ifdef HAVE_UNISTD_H
322 #include <unistd.h>
323 #endif
324
325 char *search_path(char *name) {
326 char *path;
327 static char buf[256];
328 struct stat stbuf;
329
330 if (name == NULL || strchr(name, '/') != NULL) return name;
331
332 path = getenv("PATH");
333 if (path == NULL) return NULL;
334
335 for (char *p = path, *q; p != NULL; p = q) {
336 q = strchr(p, ':');
337 char *r;
338 if (q == NULL) {
339 strcpy(buf, p);
340 r = buf + strlen(p);
341 } else {
342 strncpy(buf, p, q-p);
343 r = buf + (q-p); q++;
344 }
345 if (r > buf) *r++ = '/';
346 strcpy(r, name);
347
348 if (access(buf, R_OK) == 0 && stat(buf, &stbuf) == 0
349 && S_ISREG(stbuf.st_mode))
350 return buf;
351 }
352
353 return NULL;
354 }
355 #endif
356
357 static char *progname;
358 #ifdef PROFILE
359 static char *profout;
360 static const char *dumpname = "obprof.out";
361 #endif
362
363 static void usage(void) {
364 #ifdef PROFILE
365 fprintf(stderr,
366 "Usage: %s [-g] [-pl] [-o file] program [arg ...]\n",
367 progname);
368 #else
369 fprintf(stderr, "Usage: %s program [arg ...]\n", progname);
370 #endif
371 fflush(stderr);
372 _exit(1);
373 }
374
375 #ifdef JTEST
376 static mybool tflag = 0;
377 #endif
378
379 /* read_flags -- interpret flags */
380 static void read_flags(void) {
381 for (;;) {
382 argc--; argv++;
383 if (argc == 0 || argv[0][0] != '-') return;
384
385 if (strcmp(argv[0], "--") == 0) {
386 argc--; argv++;
387 return;
388 } else if (strcmp(argv[0], "-d") == 0) {
389 dflag++;
390 } else if (strcmp(argv[0], "-v") == 0) {
391 fprintf(stderr, "%s\n", version);
392 fprintf(stderr, "%s\n", lib_version);
393 exit(0);
394 }
395 #ifdef PROFILE
396 else if (argc >= 2 && strcmp(argv[0], "-o") == 0) {
397 profout = argv[1];
398 argc--; argv++;
399 } else if (strcmp(argv[0], "-g") == 0) {
400 gflag = TRUE;
401 } else if (strcmp(argv[0], "-l") == 0
402 || strcmp(argv[0], "-pl") == 0) {
403 lflag = TRUE;
404 }
405 #endif
406 #ifdef TRACE
407 else if (strcmp(argv[0], "-q") == 0) {
408 qflag++;
409 }
410 #endif
411 #ifdef OBXDEB
412 else if (argc >= 2 && strcmp(argv[0], "-p") == 0) {
413 debug_socket = argv[1];
414 argc--; argv++;
415 }
416 #endif
417 #ifdef JTEST
418 else if (strcmp(argv[0], "-t") == 0) {
419 tflag++;
420 }
421 #endif
422 else {
423 usage();
424 }
425 }
426 }
427
428 #ifdef PROFILE
429 static void dump_lcounts(void) {
430 FILE *fp = fopen(dumpname, "w");
431 if (fp == NULL) {
432 fprintf(stderr, "%s: cannot write\n", dumpname);
433 exit(1);
434 }
435
436 for (int m = 0; m < nmods; m++)
437 for (int n = 1; n <= modtab[m]->m_nlines; n++)
438 if (modtab[m]->m_lcount[n-1] > 0)
439 fprintf(fp, "%s %d %u\n", modtab[m]->m_name, n,
440 modtab[m]->m_lcount[n-1]);
441
442 fclose(fp);
443 }
444
445 static void print_profile(void) {
446 FILE *fp = stderr;
447
448 if (profout != NULL) {
449 fp = fopen(profout, "w");
450 if (fp == NULL) {
451 fprintf(stderr, "%s: cannot write\n", profout);
452 exit(1);
453 }
454
455 fprintf(fp, "Command line:\n\n");
456 fprintf(fp, " %s", saved_argv[0]);
457 for (int i = 1; i < saved_argc; i++)
458 fprintf(fp, " %s", saved_argv[i]);
459 fprintf(fp, "\n\n");
460 }
461
462 profile(fp);
463
464 if (fp != stderr) fclose(fp);
465 }
466 #endif
467 #endif
468
469 #ifdef JTEST
470 static void jit_test(void) {
471 dflag = vm_debug = 2; vm_aflag = 1;
472 if (nmods < 2) panic("Can't find main module");
473 module m = modtab[nmods-2];
474 for (int i = 0; i < nprocs; i++) {
475 proc p = proctab[i];
476 if (p->p_addr >= m->m_addr && p->p_addr < m->m_addr + m->m_length)
477 jit_compile(ptrcast(value, p->p_addr));
478 }
479 }
480 #endif
481
482 /* xmain_exit -- exit after program has finished */
483 void NORETURN xmain_exit(int status) {
484 #ifdef OBXDEB
485 debug_break(NULL, NULL, NULL, "exit");
486 #endif
487 #ifdef PROFILE
488 print_profile();
489 if (lflag) dump_lcounts();
490 #endif
491 exit(status);
492 }
493
494 /* error_exit -- exit after fatal error */
495 void NORETURN error_exit(int status) {
496 #ifdef OBXDEB
497 debug_message("quit");
498 #endif
499 exit(status);
500 }
501
502 /* The interpreter can be invoked in three ways:
503 (i) Explicitly as "obx [flags] bytefile args"
504
505 (ii) Via a #! script as "obx bytefile args"
506 or "bytefile bytefile args" under some Unixes
507
508 (iii) In a glued-together executable as "bytefile args"
509
510 Following the example of CAML Light, we recognize (iii) by seeing
511 if argv[0] names a bytefile that does not begin with #!. In that
512 case, we read that file for the bytecodes, and the program's args
513 follow immediately; otherwise, we look for flags and the name of
514 the bytefile before the program's args. In either case, we must be
515 prepared to search the shell path to find the bytefile.
516
517 These rules are modified a bit if a custom file is built for
518 profiling: in that case, we look for switches even in case (iii).
519
520 Actually, there's a fourth way: linking the interpreter with a
521 "preloaded" image. */
522
523 int main(int ac, char *av[]) {
524 #ifndef M64X32
525 if (sizeof(uchar *) != 4) panic("Bad pointer size");
526 #endif
527
528 argc = ac; argv = av;
529
530 #ifndef PRELOAD
531 progname = argv[0];
532
533 /* Read the command line first to handle -v */
534 char *codefile = search_path(argv[0]);
535 if (codefile != NULL && custom_file(codefile)) {
536 #ifdef PROFILE
537 char *prog = argv[0];
538 read_flags();
539 /* Fill the program name back in as argv[0] */
540 argc++; argv--;
541 argv[0] = prog;
542 #endif
543 } else {
544 read_flags();
545 if (argc < 1) usage();
546 codefile = search_path(argv[0]);
547 }
548
549 if (codefile == NULL) panic("can't find %s", argv[0]);
550 #endif
551
552 #ifdef OBXDEB
553 /* Now connect to the debugger process */
554 debug_init();
555 #endif
556
557 gc_init();
558
559 #ifdef JIT
560 vm_debug = dflag;
561 interpreter = wrap_prim(jit_trap);
562 #else
563 interpreter = wrap_prim(interp);
564 #endif
565 dyntrap = wrap_prim(dltrap);
566 #ifdef USE_FFI
567 dynstub = wrap_prim(dlstub);
568 #endif
569
570 #ifdef PRELOAD
571 load_image();
572 #else
573 FILE *fp = fopen(codefile, "rb");
574 if (fp == NULL) panic("can't open %s", codefile);
575 load_file(fp);
576 fclose(fp);
577 #endif
578
579 #ifdef TRACE
580 if (dflag) dump();
581 if (qflag) exit(0);
582 #endif
583
584 #ifdef JTEST
585 if (tflag) {
586 jit_test();
587 exit(0);
588 }
589 #endif
590
591 #ifdef PROFILE
592 if (nprocs == 0)
593 panic("no symbol table in object file");
594
595 prof_init();
596 #endif
597
598 #ifdef OBXDEB
599 debug_break(NULL, NULL, NULL, "ready");
600 #endif
601 #ifdef DEBUG
602 if (dflag)
603 printf("Starting program at address %ld\n",
604 (long) ((uchar *) entry - dmem));
605 #endif
606
607 run(entry);
608 xmain_exit(0);
609 }
610
611 #ifdef JIT
612 value *interp(value *bp) {
613 panic("dummy interp called");
614 return NULL;
615 }
616 #endif
617
618 word wrap_prim(primitive *prim) {
619 #ifdef JIT
620 return vm_wrap((funptr) prim);
621 #else
622 #ifndef M64X32
623 return (word) prim;
624 #else
625 word addr = virtual_alloc_atomic(sizeof(primitive *));
626 primitive **wrapper = ptrcast(primitive *, addr);
627 *wrapper = prim;
628 return addr;
629 #endif
630 #endif
631 }