annotate keiko/oblink.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
0
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
1 /*
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
2 * oblink.h
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
3 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
4 * This file is part of the Oxford Oberon-2 compiler
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
5 * Copyright (c) 2006--2016 J. M. Spivey
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
6 * All rights reserved
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
7 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions are met:
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
10 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
12 * this list of conditions and the following disclaimer.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
14 * this list of conditions and the following disclaimer in the documentation
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
15 * and/or other materials provided with the distribution.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
16 * 3. The name of the author may not be used to endorse or promote products
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
17 * derived from this software without specific prior written permission.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
18 *
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
29 */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
30
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
31 #include "config.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
32 #include <stdio.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
33 #include <stdlib.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
34 #include <string.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
35 #include <stdarg.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
36 #include <assert.h>
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
37 #include "obcommon.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
38 #include "exec.h"
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
39
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
40 #ifdef DEBUG
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
41 /* Small tables to help test auto-growth */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
42 #define INIT_PMEM 8 /* Init space for procedures */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
43 #define INIT_XMEM 128 /* Same for data buffer */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
44 #define INIT_LMEM 16 /* Same for local labels */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
45 #define INIT_SMEM 16 /* Same for symbol table */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
46 #define INIT_MODS 10 /* Init space for modules */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
47 #else
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
48 #define INIT_PMEM 256 /* Init space for procedures */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
49 #define INIT_XMEM 2048 /* Same for data buffer */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
50 #define INIT_LMEM 1024 /* Same for local labels */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
51 #define INIT_SMEM 1024 /* Same for symbol table */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
52 #define INIT_MODS 100 /* Init space for modules */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
53 #endif
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
54
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
55 #define STACK_SIZE (1024 * 1024 - 32)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
56 /* Default stack size (bytes) */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
57 #define MIN_STACK 4096 /* Min stack size (bytes) */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
58
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
59 typedef enum { ABS, DATA, BSS, CODE, UNDEFINED } segment;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
60
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
61 typedef struct _phrase *phrase;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
62
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
63 #ifdef __cplusplus
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
64 #define template xyzzy
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
65 #endif
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
66
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
67 typedef struct _template *template;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
68
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
69 #define MAXMAC 6
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
70
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
71 struct _template { /* An encoding of an instruction */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
72 const char *t_name; /* The instruction */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
73 const char *t_pattern; /* Argument formats */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
74 int t_lo, t_hi, t_step; /* Pattern of values for 'N' format */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
75 int t_size; /* Total length of instruction */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
76 int t_oplen; /* Length of opcode */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
77 uchar t_op; /* Opcode */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
78 const char *t_macro[MAXMAC]; /* Macro expansion */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
79 };
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
80
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
81 #define put1(buf, x) put_int(1, buf, x)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
82 #define put2(buf, x) put_int(2, buf, x)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
83 #define put4(buf, x) put_int(4, buf, x)
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
84
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
85 EXTERN int dflag;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
86 EXTERN int zflag; /* Whether to compress the bytecode */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
87 EXTERN mybool sflag; /* Whether to suppress symbol table */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
88 EXTERN mybool gflag; /* Whether to output extra debugging info */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
89 EXTERN mybool custom; /* Whether to output a primitive table */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
90 EXTERN mybool linecount;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
91 EXTERN int stack_size;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
92
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
93 /* template.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
94 extern struct _template templates[];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
95 extern short templ_trie[];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
96 extern uchar templ_check[];
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
97
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
98 /* symtab.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
99 typedef struct _symbol *symbol;
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
100
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
101 symbol make_symbol(const char *name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
102 symbol find_symbol(const char *name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
103 const char *sym_name(symbol s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
104 void def_global(symbol s, segment seg, int off, int kind);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
105 void use_global(symbol s, uchar *base, int offset);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
106 int sym_value(symbol s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
107 mybool known(const char *name);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
108 void fix_data(uchar *base, int bss);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
109 int write_symtab(void);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
110 void module_data(symbol s, unsigned checksum, int nlines);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
111
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
112 void init_labels(void);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
113 int make_label(symbol s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
114 const char *label_name(int n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
115 void def_label(symbol s, phrase val);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
116 phrase find_label(int n);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
117
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
118 /* linker.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
119 void init_linker(char *outname, char *interp);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
120 void put_inst(const char *name, char *rands[], unsigned nrands);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
121 void gen_inst(const char *fmt, ...);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
122 void save_string(const char *label, char *string);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
123 void open_output(char *name, char *interp);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
124 void end_linking(void);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
125 void dump_prims(void);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
126
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
127 void relocate(int loc, int bits);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
128
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
129 void put_int(int n, uchar *buf, int x);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
130 int get4(uchar *buf);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
131 void write_string(const char *s);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
132 void write_int(int n, int x);
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
133
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
134 /* util.c */
Mike Spivey <mike@cs.ox.ac.uk>
parents:
diff changeset
135 #include "util.h"