comparison keiko/exec.h @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 /*
2 * exec.h
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 /*
32 The binary file output by the linker and input by the run-time
33 interpreter consists of several segments followed by a trailer that
34 describes the layout of the segments. The segments may be preceded
35 by a block of arbitrary data, but they must appear in the following order:
36
37 CODE, DATA, RELOC, SYMTAB
38
39 By using a trailer rather than a header, we allow for a binary file
40 also to be a valid file in another format that uses a header -- like
41 the executable format of most operating systems including various
42 Unixes and MS--DOS. In that format, our code is just junk beyond the
43 end of the file, and it is usually ignored. So we can make
44 self-contained executables by concatenating the interpreter with the
45 code for a program. This idea was stolen from CAML Light.
46 */
47
48 /* Codes for the segments: they must appear in the object file
49 in this order */
50 #define NSEGS 4
51 #define S_CODE 0 /* Bytecode */
52 #define S_DATA 1 /* Initialized data */
53 #define S_BSS 2 /* Below stack storage */
54 #define S_STACK 3 /* Stack */
55
56 typedef uchar word4[4];
57
58 typedef struct {
59 word4 magic; /* Magic number 'OBCX' */
60 word4 sig; /* Signature */
61 word4 primsig; /* Checksum of primitives */
62 word4 start; /* Offset of data start from end of file */
63 word4 entry; /* Entry point */
64 word4 gcmap; /* Global pointer map */
65 word4 libdir; /* Location of dynamic libraries */
66 word4 segment[NSEGS]; /* Segment sizes */
67 word4 nprocs, nmods, nsyms; /* No. of procs, modules, symbols */
68 } trailer;
69
70
71 /* Layout of relocation data */
72 #define WORD_SIZE 4
73 #define CODES_PER_WORD 16
74 #define BITS_PER_CODE 2
75 #define CODE_MASK ((1 << BITS_PER_CODE) - 1)
76
77 #define reloc_bits(buf, i) (buf[(i)/CODES_PER_WORD] >> \
78 ((i) % CODES_PER_WORD * BITS_PER_CODE) & CODE_MASK)
79
80 /* Relocation codes */
81 #define R_WORD 0
82 #define R_DATA 1
83 #define R_CODE 2
84 #define R_SUBR 3
85
86 /* Symbol tags */
87 #define X_NONE 0
88 #define X_MODULE 1
89 #define X_PROC 2
90 #define X_DATA 3
91 #define X_LINE 4
92 #define X_SYM 5
93
94 /* Fixed primitives */
95 #define INTERP 0 /* Index of interpreter as primitive */
96 #define DLTRAP 1 /* Index of dynlink trap */