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