| 1 |
/* |
| 2 |
* obx.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 |
#ifndef _OBX_H |
| 32 |
#define _OBX_H 1 |
| 33 |
|
| 34 |
#ifdef TRACE |
| 35 |
#define DEBUG 1 |
| 36 |
#endif |
| 37 |
|
| 38 |
#include "config.h" |
| 39 |
#include <stdio.h> |
| 40 |
#include "obcommon.h" |
| 41 |
|
| 42 |
#define SLIMIT 256 /* Min stack space space left (bytes) */ |
| 43 |
|
| 44 |
typedef union value value; |
| 45 |
|
| 46 |
typedef value *primitive(value *sp); |
| 47 |
|
| 48 |
typedef uint32_t word; |
| 49 |
typedef uintptr_t ptrtype; |
| 50 |
|
| 51 |
union value { |
| 52 |
int i; |
| 53 |
float f; |
| 54 |
word a; |
| 55 |
}; |
| 56 |
|
| 57 |
#define valptr(v) ptrcast(value, (v).a) |
| 58 |
#define pointer(v) ptrcast(void, (v).a) |
| 59 |
|
| 60 |
#ifdef SEGMEM |
| 61 |
|
| 62 |
#define SEGBITS 20 |
| 63 |
#define SEGMENT (1 << SEGBITS) |
| 64 |
#define SEGMASK ((1 << SEGBITS)-1) |
| 65 |
#define NSEGMENTS 4096 |
| 66 |
|
| 67 |
#define stkaddr(p) (stack_vbase + ((uchar *) (p) - stack)) |
| 68 |
#define dsegaddr(p) (data_vbase + ((uchar *) (p) - dmem)) |
| 69 |
#define ptrcast(t, a) ((t *) physmap(a)) |
| 70 |
|
| 71 |
extern void *segmap[]; |
| 72 |
EXTERN word stack_vbase, data_vbase; |
| 73 |
|
| 74 |
#define physmap(a) \ |
| 75 |
((void *) ((char *) segmap[(a) >> SEGBITS] + ((a) & SEGMASK))) |
| 76 |
|
| 77 |
/* |
| 78 |
static inline void *physmap(unsigned a) { |
| 79 |
return (char *) segmap[a >> SEGBITS] + (a & SEGMASK); |
| 80 |
} |
| 81 |
*/ |
| 82 |
|
| 83 |
#else |
| 84 |
|
| 85 |
#define pun_memory(p) ((word) (ptrtype) (p)) |
| 86 |
#define stkaddr(p) pun_memory(p) |
| 87 |
#define dsegaddr(p) pun_memory(p) |
| 88 |
#define ptrcast(t, a) ((t *) (ptrtype) (a)) |
| 89 |
|
| 90 |
#endif |
| 91 |
|
| 92 |
#define codeaddr(p) ((p) - imem) |
| 93 |
#define codeptr(v) (imem + (v)) |
| 94 |
|
| 95 |
typedef struct _proc *proc; |
| 96 |
typedef struct _module *module; |
| 97 |
typedef struct _arc *arc; |
| 98 |
|
| 99 |
#ifdef PROFILE |
| 100 |
typedef uint64_t counter; |
| 101 |
#endif |
| 102 |
|
| 103 |
struct _proc { |
| 104 |
const char *p_name; /* Procedure name */ |
| 105 |
word p_addr; /* Address of descriptor in data space */ |
| 106 |
#ifdef PROFILE |
| 107 |
int p_index; /* Position in listing */ |
| 108 |
unsigned p_calls; /* Call count */ |
| 109 |
unsigned p_rec; /* Call count for recursion */ |
| 110 |
counter p_self; /* Tick count for self */ |
| 111 |
counter p_child; /* Tick count for children */ |
| 112 |
arc p_parents; /* List of callers */ |
| 113 |
arc p_children; /* List of procs we call */ |
| 114 |
#endif |
| 115 |
}; |
| 116 |
|
| 117 |
struct _module { |
| 118 |
char *m_name; /* Layout must match proc */ |
| 119 |
word m_addr; |
| 120 |
int m_length; |
| 121 |
#ifdef PROFILE |
| 122 |
int m_nlines; |
| 123 |
unsigned *m_lcount; |
| 124 |
#endif |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
/* Global variables */ |
| 129 |
EXTERN uchar *imem, *dmem; /* Instruction and data memory arrays */ |
| 130 |
EXTERN uchar *stack; /* Program stack */ |
| 131 |
EXTERN int code_size; /* Size of program code */ |
| 132 |
EXTERN int stack_size; /* Size of main stack */ |
| 133 |
EXTERN char *libpath; /* Path to dynamic library */ |
| 134 |
EXTERN value *entry; /* Program entry point */ |
| 135 |
EXTERN value *gcmap; /* Global pointer map */ |
| 136 |
EXTERN word interpreter, dyntrap; |
| 137 |
#ifdef USE_FFI |
| 138 |
EXTERN word dynstub; |
| 139 |
#endif |
| 140 |
|
| 141 |
#define interpreted(p) ((p)[CP_PRIM].a == interpreter) |
| 142 |
|
| 143 |
#ifndef M64X32 |
| 144 |
#define primcall(p, sp) ((primitive *) p[CP_PRIM].a)(sp) |
| 145 |
#else |
| 146 |
#ifdef JIT |
| 147 |
#define primcall(p, sp) (ptrcast(primitive, p[CP_PRIM].a))(sp) |
| 148 |
#else |
| 149 |
#define primcall(p, sp) (*ptrcast(primitive *, p[CP_PRIM].a))(sp) |
| 150 |
#endif |
| 151 |
#endif |
| 152 |
|
| 153 |
#define get1(p) ((int) ((signed char) (p)[0])) |
| 154 |
#define get2(p) ((int) ((short) (((p)[1]<<8) + (p)[0]))) |
| 155 |
|
| 156 |
EXTERN int nmods, nprocs, nsyms; |
| 157 |
EXTERN module *modtab; |
| 158 |
EXTERN proc *proctab; |
| 159 |
|
| 160 |
extern struct primdef { |
| 161 |
char *p_name; |
| 162 |
primitive *p_prim; |
| 163 |
} primtab[]; |
| 164 |
|
| 165 |
EXTERN int level; /* Recursion level in bytecode interp. */ |
| 166 |
#ifdef OBXDEB |
| 167 |
EXTERN value *prim_bp; /* Base pointer during primitive call */ |
| 168 |
#endif |
| 169 |
|
| 170 |
EXTERN int dflag; |
| 171 |
EXTERN mybool gflag; |
| 172 |
#ifdef PROFILE |
| 173 |
EXTERN mybool lflag; |
| 174 |
#endif |
| 175 |
#ifdef TRACE |
| 176 |
EXTERN int qflag; |
| 177 |
#endif |
| 178 |
#ifdef OBXDEB |
| 179 |
EXTERN char *debug_socket; |
| 180 |
#endif |
| 181 |
|
| 182 |
|
| 183 |
/* profile.c */ |
| 184 |
#ifdef PROFILE |
| 185 |
void prof_enter(word p, counter ticks, int why); |
| 186 |
void prof_exit(word p, counter ticks); |
| 187 |
void prof_init(void); |
| 188 |
void prof_reset(proc p); |
| 189 |
void profile(FILE *fp); |
| 190 |
|
| 191 |
#define PROF_CALL 1 |
| 192 |
#define PROF_TAIL 2 |
| 193 |
#define PROF_PRIM 3 |
| 194 |
#endif |
| 195 |
|
| 196 |
/* interp.c */ |
| 197 |
primitive interp, dltrap, dlstub; |
| 198 |
|
| 199 |
/* xmain.c */ |
| 200 |
EXTERN int saved_argc; |
| 201 |
EXTERN char **saved_argv; |
| 202 |
|
| 203 |
void xmain_exit(int status); |
| 204 |
void error_exit(int status); |
| 205 |
|
| 206 |
/* support.c */ |
| 207 |
int ob_div(int a, int b); |
| 208 |
int ob_mod(int a, int b); |
| 209 |
|
| 210 |
void int_div(value *sp); |
| 211 |
void int_mod(value *sp); |
| 212 |
|
| 213 |
void long_add(value *sp); |
| 214 |
void long_sub(value *sp); |
| 215 |
void long_mul(value *sp); |
| 216 |
void long_div(value *sp); |
| 217 |
void long_mod(value *sp); |
| 218 |
void long_neg(value *sp); |
| 219 |
void long_cmp(value *sp); |
| 220 |
void long_flo(value *sp); |
| 221 |
void long_ext(value *sp); |
| 222 |
void long_zcheck(value *sp); |
| 223 |
|
| 224 |
void flo_add(value *sp); |
| 225 |
void flo_sub(value *sp); |
| 226 |
void flo_mul(value *sp); |
| 227 |
void flo_div(value *sp); |
| 228 |
void flo_neg(value *sp); |
| 229 |
void flo_float(value *sp); |
| 230 |
void flo_fix(value *sp); |
| 231 |
void flo_cmpl(value *sp); |
| 232 |
void flo_cmpg(value *sp); |
| 233 |
void flo_trunc(value *sp); |
| 234 |
void dbl_add(value *sp); |
| 235 |
void dbl_sub(value *sp); |
| 236 |
void dbl_mul(value *sp); |
| 237 |
void dbl_div(value *sp); |
| 238 |
void dbl_neg(value *sp); |
| 239 |
void dbl_float(value *sp); |
| 240 |
void dbl_fix(value *sp); |
| 241 |
void dbl_cmpl(value *sp); |
| 242 |
void dbl_cmpg(value *sp); |
| 243 |
void dbl_widen(value *sp); |
| 244 |
void flo_zcheck(value *sp); |
| 245 |
void dbl_zcheck(value *sp); |
| 246 |
|
| 247 |
word wrap_prim(primitive *prim); |
| 248 |
|
| 249 |
/* load_file -- load a file of object code */ |
| 250 |
void load_file(FILE *bfp); |
| 251 |
void load_image(void); |
| 252 |
|
| 253 |
void make_module(char *name, word addr, int chsum, int nlines); |
| 254 |
void make_proc(char *name, word addr); |
| 255 |
void make_symbol(const char *kind, char *name, word addr); |
| 256 |
void fix_sizes(int dseg); |
| 257 |
|
| 258 |
void panic(const char *, ...); |
| 259 |
void obcopy(char *dst, int dlen, const char *src, int slen, value *bp); |
| 260 |
|
| 261 |
void error_stop(const char *msg, int val, int line, value *bp, uchar *pc); |
| 262 |
void runtime_error(int num, int line, value *bp, uchar *pc); |
| 263 |
void rterror(int num, int line, value *bp); |
| 264 |
void stkoflo(value *bp); |
| 265 |
#define liberror(msg) error_stop(msg, 0, 0, bp, NULL) |
| 266 |
|
| 267 |
proc find_symbol(word p, proc *table, int nelem); |
| 268 |
#define find_proc(cp) find_symbol(cp, proctab, nprocs) |
| 269 |
#define find_module(cp) ((module) find_symbol(cp, (proc *) modtab, nmods)) |
| 270 |
|
| 271 |
#ifdef TRACE |
| 272 |
char *fmt_inst(uchar *pc); |
| 273 |
void dump(void); |
| 274 |
const char *prim_name(value *p); |
| 275 |
#endif |
| 276 |
|
| 277 |
#ifdef UNALIGNED_MEM |
| 278 |
#define get_double(v) (* (double *) (v)) |
| 279 |
#define put_double(v, x) (* (double *) (v) = (x)) |
| 280 |
#define get_long(v) (* (longint *) (v)) |
| 281 |
#define put_long(v, x) (* (longint *) (v) = (x)) |
| 282 |
#else |
| 283 |
double get_double(value *v); |
| 284 |
void put_double(value *v, double x); |
| 285 |
longint get_long(value *v); |
| 286 |
void put_long(value *v, longint w); |
| 287 |
#endif |
| 288 |
|
| 289 |
double flo_conv(int); |
| 290 |
double flo_convq(longint); |
| 291 |
|
| 292 |
|
| 293 |
/* gc.c */ |
| 294 |
|
| 295 |
/* scratch_alloc -- allocate memory that will not be freed */ |
| 296 |
void *scratch_alloc(unsigned bytes); |
| 297 |
|
| 298 |
#ifdef USE_BOEHM |
| 299 |
/* scratch_alloc_atomic -- allocate memory that will not contain pointers */ |
| 300 |
void *scratch_alloc_atomic(unsigned bytes); |
| 301 |
#else |
| 302 |
#define scratch_alloc_atomic(bytes) scratch_alloc(bytes) |
| 303 |
#endif |
| 304 |
|
| 305 |
#ifdef SEGMEM |
| 306 |
|
| 307 |
word virtual_alloc(unsigned bytes); |
| 308 |
word map_segment(void *base, unsigned length); |
| 309 |
#define virtual_alloc_atomic(bytes) virtual_alloc(bytes) |
| 310 |
|
| 311 |
#else |
| 312 |
|
| 313 |
#define virtual_alloc(bytes) pun_memory(scratch_alloc(bytes)) |
| 314 |
#define virtual_alloc_atomic(bytes) pun_memory(scratch_alloc_atomic(bytes)) |
| 315 |
|
| 316 |
#endif |
| 317 |
|
| 318 |
/* gc_alloc -- allocate an object on the managed heap */ |
| 319 |
word gc_alloc(unsigned size, value *sp); |
| 320 |
|
| 321 |
/* gc_collect -- run the garbage collector */ |
| 322 |
value *gc_collect(value *sp); |
| 323 |
|
| 324 |
/* gc_heap_size -- return size of heap */ |
| 325 |
int gc_heap_size(void); |
| 326 |
|
| 327 |
extern mybool gcflag; |
| 328 |
void gc_init(void); |
| 329 |
void gc_dump(void); |
| 330 |
|
| 331 |
/* debug.c */ |
| 332 |
#ifdef OBXDEB |
| 333 |
extern mybool one_shot; |
| 334 |
extern mybool intflag; |
| 335 |
|
| 336 |
void debug_init(void); |
| 337 |
void debug_message(char *fmt, ...); |
| 338 |
void debug_break(value *cp, value *bp, uchar *pc, char *fmt, ...); |
| 339 |
#endif |
| 340 |
|
| 341 |
/* jit.c */ |
| 342 |
#ifdef JIT |
| 343 |
void jit_compile(value *cp); |
| 344 |
value *jit_trap(value *cp); |
| 345 |
#endif |
| 346 |
|
| 347 |
#ifdef __cplusplus |
| 348 |
#define PRIMDEF extern "C" |
| 349 |
#else |
| 350 |
#define PRIMDEF |
| 351 |
#endif |
| 352 |
|
| 353 |
#ifdef NEED_FPINIT |
| 354 |
/* On x86, each primitive re-initialises the FP unit, so that values |
| 355 |
left behind in registers by the caller do not cause stack overflow. */ |
| 356 |
#define FPINIT asm ("fninit") |
| 357 |
#else |
| 358 |
#define FPINIT |
| 359 |
#endif |
| 360 |
|
| 361 |
#endif |