annotate 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
rev   line source
mike@0 1 /*
mike@0 2 * exec.h
mike@0 3 *
mike@0 4 * This file is part of the Oxford Oberon-2 compiler
mike@0 5 * Copyright (c) 2006--2016 J. M. Spivey
mike@0 6 * All rights reserved
mike@0 7 *
mike@0 8 * Redistribution and use in source and binary forms, with or without
mike@0 9 * modification, are permitted provided that the following conditions are met:
mike@0 10 *
mike@0 11 * 1. Redistributions of source code must retain the above copyright notice,
mike@0 12 * this list of conditions and the following disclaimer.
mike@0 13 * 2. Redistributions in binary form must reproduce the above copyright notice,
mike@0 14 * this list of conditions and the following disclaimer in the documentation
mike@0 15 * and/or other materials provided with the distribution.
mike@0 16 * 3. The name of the author may not be used to endorse or promote products
mike@0 17 * derived from this software without specific prior written permission.
mike@0 18 *
mike@0 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
mike@0 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
mike@0 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
mike@0 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
mike@0 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
mike@0 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
mike@0 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
mike@0 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
mike@0 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
mike@0 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mike@0 29 */
mike@0 30
mike@0 31 /*
mike@0 32 The binary file output by the linker and input by the run-time
mike@0 33 interpreter consists of several segments followed by a trailer that
mike@0 34 describes the layout of the segments. The segments may be preceded
mike@0 35 by a block of arbitrary data, but they must appear in the following order:
mike@0 36
mike@0 37 CODE, DATA, RELOC, SYMTAB
mike@0 38
mike@0 39 By using a trailer rather than a header, we allow for a binary file
mike@0 40 also to be a valid file in another format that uses a header -- like
mike@0 41 the executable format of most operating systems including various
mike@0 42 Unixes and MS--DOS. In that format, our code is just junk beyond the
mike@0 43 end of the file, and it is usually ignored. So we can make
mike@0 44 self-contained executables by concatenating the interpreter with the
mike@0 45 code for a program. This idea was stolen from CAML Light.
mike@0 46 */
mike@0 47
mike@0 48 /* Codes for the segments: they must appear in the object file
mike@0 49 in this order */
mike@0 50 #define NSEGS 4
mike@0 51 #define S_CODE 0 /* Bytecode */
mike@0 52 #define S_DATA 1 /* Initialized data */
mike@0 53 #define S_BSS 2 /* Below stack storage */
mike@0 54 #define S_STACK 3 /* Stack */
mike@0 55
mike@0 56 typedef uchar word4[4];
mike@0 57
mike@0 58 typedef struct {
mike@0 59 word4 magic; /* Magic number 'OBCX' */
mike@0 60 word4 sig; /* Signature */
mike@0 61 word4 primsig; /* Checksum of primitives */
mike@0 62 word4 start; /* Offset of data start from end of file */
mike@0 63 word4 entry; /* Entry point */
mike@0 64 word4 gcmap; /* Global pointer map */
mike@0 65 word4 libdir; /* Location of dynamic libraries */
mike@0 66 word4 segment[NSEGS]; /* Segment sizes */
mike@0 67 word4 nprocs, nmods, nsyms; /* No. of procs, modules, symbols */
mike@0 68 } trailer;
mike@0 69
mike@0 70
mike@0 71 /* Layout of relocation data */
mike@0 72 #define WORD_SIZE 4
mike@0 73 #define CODES_PER_WORD 16
mike@0 74 #define BITS_PER_CODE 2
mike@0 75 #define CODE_MASK ((1 << BITS_PER_CODE) - 1)
mike@0 76
mike@0 77 #define reloc_bits(buf, i) (buf[(i)/CODES_PER_WORD] >> \
mike@0 78 ((i) % CODES_PER_WORD * BITS_PER_CODE) & CODE_MASK)
mike@0 79
mike@0 80 /* Relocation codes */
mike@0 81 #define R_WORD 0
mike@0 82 #define R_DATA 1
mike@0 83 #define R_CODE 2
mike@0 84 #define R_SUBR 3
mike@0 85
mike@0 86 /* Symbol tags */
mike@0 87 #define X_NONE 0
mike@0 88 #define X_MODULE 1
mike@0 89 #define X_PROC 2
mike@0 90 #define X_DATA 3
mike@0 91 #define X_LINE 4
mike@0 92 #define X_SYM 5
mike@0 93
mike@0 94 /* Fixed primitives */
mike@0 95 #define INTERP 0 /* Index of interpreter as primitive */
mike@0 96 #define DLTRAP 1 /* Index of dynlink trap */