annotate keiko/primtab.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 * primtab.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 #include "obx.h"
mike@0 32 #include <math.h>
mike@0 33 #include <ctype.h>
mike@0 34
mike@0 35 /* Types for each kind of argument */
mike@0 36 typedef int type_C, type_I;
mike@0 37 typedef longint type_L;
mike@0 38 typedef float type_F;
mike@0 39 typedef double type_D;
mike@0 40 typedef void *type_P, *type_Q, *type_X;
mike@0 41 typedef void type_V;
mike@0 42
mike@0 43 /* Size of each kind in argument words */
mike@0 44 #define size_C 1
mike@0 45 #define size_I 1
mike@0 46 #define size_F 1
mike@0 47 #define size_P 1
mike@0 48 #define size_L 2
mike@0 49 #define size_D 2
mike@0 50 #define size_X 2
mike@0 51 #define size_Q 2
mike@0 52
mike@0 53 /* How to fetch each kind of argument */
mike@0 54 #define arg_I(j) bp[HEAD+j].i
mike@0 55 #define arg_C(j) align_byte(bp[HEAD+j].i)
mike@0 56 #define arg_L(j) get_long(&bp[HEAD+j])
mike@0 57 #define arg_F(j) bp[HEAD+j].f
mike@0 58 #define arg_D(j) get_double(&bp[HEAD+j])
mike@0 59 #define arg_P(j) pointer(bp[HEAD+j])
mike@0 60 #define arg_X(j) pointer(bp[HEAD+j])
mike@0 61 #define arg_Q(j) ptrcast(void, get_long(&bp[HEAD+j]))
mike@0 62
mike@0 63 /* How to return each kind of result via ob_res */
mike@0 64 #define res_I(v) ob_res.i = v
mike@0 65 #define res_C(v) ob_res.i = v
mike@0 66 #define res_F(v) ob_res.f = v
mike@0 67 #define res_P(v) ob_res.a = address(v)
mike@0 68 #define res_L(v) put_long(&ob_res, v)
mike@0 69 #define res_D(v) put_double(&ob_res, v)
mike@0 70 #define res_Q(v) put_long(&ob_res, (ptrtype) v)
mike@0 71 #define res_V(v) v
mike@0 72
mike@0 73 /* Three kinds of primitive:
mike@0 74 DIRECT -- defined by a function "void prim(value *bp)"
mike@0 75 WRAPPER -- defined by an foreign function prim that declared in one of
mike@0 76 the included header files. We generate a wrapper P_prim.
mike@0 77 INDIRECT -- defined by an internal function in some library module, with
mike@0 78 a natural type. We generate a wrapper that includes a
mike@0 79 declaration of the function.
mike@0 80
mike@0 81 Call WRAPPER(name, res, a1, a2, ..., an) where res and a1, ..., an are
mike@0 82 type letters for the result and arguments. */
mike@0 83
mike@0 84 #define DIRECT(name) void name(value *bp);
mike@0 85 #define WRAPPER(...) WRAP(_WRAP, 0, __VA_ARGS__)
mike@0 86 #define INDIRECT(...) WRAP(_INDIR, 0, __VA_ARGS__)
mike@0 87
mike@0 88 /* WRAP(mac, base, name, res, a1, ..., an) is
mike@0 89
mike@0 90 mac(name, res,
mike@0 91 (type_a1, ..., type_an),
mike@0 92 (arg_a1(base), arg_a2(base+s1), arg_a3(base+s1+s2), ...,
mike@0 93 arg_an(base+s1+s2+...s(n-1))))
mike@0 94
mike@0 95 where si = size_ai. */
mike@0 96
mike@0 97 #define WRAP(mac, base, ...) \
mike@0 98 SELECT(__VA_ARGS__, WRAP6, WRAP5, WRAP4, WRAP3, \
mike@0 99 WRAP2, WRAP1, WRAP0)(mac, base, __VA_ARGS__)
mike@0 100
mike@0 101 #define SELECT(n, r, a1, a2, a3, a4, a5, a6, t, ...) t
mike@0 102
mike@0 103 #define WRAP0(mac, base, name, res) \
mike@0 104 mac(name, res, (void), ())
mike@0 105 #define WRAP1(mac, base, name, res, a1) \
mike@0 106 mac(name, res, (type_##a1), (arg_##a1(base)))
mike@0 107 #define WRAP2(mac, base, name, res, a1, a2) \
mike@0 108 mac(name, res, (type_##a1, type_##a2), (args2(base, a1, a2)))
mike@0 109 #define WRAP3(mac, base, name, res, a1, a2, a3) \
mike@0 110 mac(name, res, \
mike@0 111 (type_##a1, type_##a2, type_##a3), \
mike@0 112 (args3(base, a1, a2, a3)))
mike@0 113 #define WRAP4(mac, base, name, res, a1, a2, a3, a4) \
mike@0 114 mac(name, res, \
mike@0 115 (type_##a1, type_##a2, type_##a3, type_##a4), \
mike@0 116 (args4(base, a1, a2, a3, a4)))
mike@0 117 #define WRAP5(mac, base, name, res, a1, a2, a3, a4, a5) \
mike@0 118 mac(name, res, \
mike@0 119 (type_##a1, type_##a2, type_##a3, type_##a4, type_##a5), \
mike@0 120 (args5(base, a1, a2, a3, a4, a5)))
mike@0 121 #define WRAP6(mac, base, name, res, a1, a2, a3, a4, a5, a6) \
mike@0 122 mac(name, res, \
mike@0 123 (type_##a1, type_##a2, type_##a3, type_##a4, type_##a5, type_##a6), \
mike@0 124 (args6(base, a1, a2, a3, a4, a5, a6)))
mike@0 125
mike@0 126 #define args2(j, a1, a2) \
mike@0 127 arg_##a1(j), arg_##a2(j+size_##a1)
mike@0 128 #define args3(j, a1, a2, a3) \
mike@0 129 arg_##a1(j), args2(j+size_##a1, a2, a3)
mike@0 130 #define args4(j, a1, a2, a3, a4) \
mike@0 131 arg_##a1(j), args3(j+size_##a1, a2, a3, a4)
mike@0 132 #define args5(j, a1, a2, a3, a4, a5) \
mike@0 133 arg_##a1(j), args4(j+size_##a1, a2, a3, a4, a5)
mike@0 134 #define args6(j, a1, a2, a3, a4, a5, a6) \
mike@0 135 arg_##a1(j), args5(j+size_##a1, a2, a3, a4, a5, a6)
mike@0 136
mike@0 137 /* How to generate a wrapper function with (_INDIR) or without (_WRAP)
mike@0 138 a declaration of the function being wrapped. */
mike@0 139 #define _WRAP(name, res, atypes, args) \
mike@0 140 __WRAP(, name, res, args)
mike@0 141 #define _INDIR(name, res, atypes, args) \
mike@0 142 __WRAP(type_##res name atypes;, name, res, args)
mike@0 143 #define __WRAP(decl, name, res, args) \
mike@0 144 void P_##name(value *bp) { decl FPINIT; res_##res(name args); }
mike@0 145
mike@0 146 #define WRAPPERS(prims) prims(DIRECT, INDIRECT, WRAPPER)
mike@0 147
mike@0 148 /* How to generate entries in the primitive table */
mike@0 149 #define DPRIM(name, ...) { #name, name },
mike@0 150 #define IPRIM(name, ...) DPRIM(P_##name)
mike@0 151
mike@0 152 #define TABLE(prims) \
mike@0 153 struct primdef primtab[] = { \
mike@0 154 prims(DPRIM, IPRIM, IPRIM) \
mike@0 155 { NULL, NULL } \
mike@0 156 };
mike@0 157
mike@0 158 /* If dynamic linking is enabled, we don't need a static table of
mike@0 159 primitives; if not, then we make a table and dltrap(dynlink.c)
mike@0 160 will search it. Note that we can have statically generated wrappers
mike@0 161 for speed even if FFI is available. */
mike@0 162
mike@0 163 #ifdef DYNLINK
mike@0 164 #define PRIMTAB(prims) WRAPPERS(prims)
mike@0 165 #else
mike@0 166 #define PRIMTAB(prims) WRAPPERS(prims) TABLE(prims)
mike@0 167 #endif
mike@0 168
mike@0 169 /* Variation for the compilers course with offset to compensate for
mike@0 170 dummy static link */
mike@0 171
mike@0 172 #define PWRAPPER(...) WRAP(_WRAP, 1, __VA_ARGS__)
mike@0 173 #define PINDIRECT(...) WRAP(_INDIR, 1, __VA_ARGS__)
mike@0 174 #define PWRAPPERS(prims) prims(DIRECT, PINDIRECT, PWRAPPER)
mike@0 175
mike@0 176 #ifdef DYNLINK
mike@0 177 #define PPRIMTAB(prims) PWRAPPERS(prims)
mike@0 178 #else
mike@0 179 #define PPRIMTAB(prims) PWRAPPERS(prims) TABLE(prims)
mike@0 180 #endif
mike@0 181