BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
blitzlib.h
Go to the documentation of this file.
1/*
2 * blitzlib.h - write Blitz Basic 2 libraries in C.
3 *
4 * A BB2 library is an AmigaDOS executable with one code hunk that starts
5 * with a header and a table of token entries, followed by the code. The
6 * original libraries were Blitz programs: the macros of libmacs.res
7 * (bb2/cd/extras/blitzlibs/libmacs.bb) emitted header and table with `Dc`
8 * statements in front of the inline assembler code.
9 *
10 * The macros below emit exactly the same bytes as the libmacs macro of the
11 * same name, as static data in the section ".blitzlib.head". The linker
12 * script ld/blitzlib.ld puts that section at offset 0 of the single code
13 * hunk, followed by the code and data and, last, the debug-only error check
14 * routines (BLITZ_ERRCHK). Code addresses in the table (`Dc.l label` in
15 * the original) are relocated longs, as in the original.
16 *
17 * The objects must stay in definition order: compile with
18 * -fno-toplevel-reorder (the CMake function blitz_library() does that).
19 *
20 * libmacs here
21 * !syslibheader{n,f,l,i,r,fi,e,fl} BLITZ_SYSLIBHEADER(n,f,l,i,r,fi,e,fl)
22 * #ldivlib (libnums.bb) LIB_ldivlib (libnums.h)
23 * !libheader{n,init,ret,finit,err} BLITZ_LIBHEADER(n,init,ret,finit,err)
24 * !astatement BLITZ_ASTATEMENT()
25 * !afunction{type} BLITZ_AFUNCTION(type)
26 * !acommand{type} BLITZ_ACOMMAND(type)
27 * !ustatement / !ufunction BLITZ_USTATEMENT() / BLITZ_UFUNCTION()
28 * !args{t,...} BLITZ_ARGS(t,...) (no args: BLITZ_ARGS0())
29 * !repargs{first,num,t,...} BLITZ_REPARGS(first,num,t,...)
30 * !libs{lib,reg,...} BLITZ_LIBS(lib,reg,...) (none: BLITZ_LIBS0())
31 * !subs{code,err1,err2} BLITZ_SUBS(code,err1,err2)
32 * !name{"name","help"} BLITZ_NAME("name","help")
33 * !nullsub{code,err1,err2[,libs]} BLITZ_NULLSUB(code,err1,err2[,lib,reg...])
34 * !dumtoke{"name","help",label} BLITZ_DUMTOKE(label,"name","help")
35 * !libfin / !libfin{t,l,s,u,f,m,s} BLITZ_LIBFIN() / BLITZ_LIBFIN7(...)
36 *
37 * Library routines take their arguments in registers (args in d0-d5, the
38 * values requested with BLITZ_LIBS in the given registers) and return d0;
39 * declare them with GCC register parameters:
40 *
41 * LONG BLITZ_ROUTINE ldivide(LONG a BL_REG(d0), LONG b BL_REG(d1));
42 *
43 * a4 (locals) and a5 (globals) are fixed registers of the Blitz program;
44 * the build uses -ffixed-a4 -ffixed-a5, so C code can use them through the
45 * global register variables below.
46 *
47 * Small shared helpers (register arithmetic, a2 argument stack, mathffp
48 * LVOs, tokens of system lib routines) are in blitzhelp.h, included at the
49 * end of this file.
50 */
51#ifndef BLITZLIB_H
52#define BLITZLIB_H
53
54/* Library code has no C library: no memset/memcpy behind the scenes.
55 - built with -ffreestanding (checked here),
56 - loops must not become memset/memcpy calls (also set by CMake),
57 - linked with -nostdlib, so a call GCC still emits anyway (it may for
58 large struct copies) is a link error, never a hidden dependency. */
59/* BL_CODE_MODEL: the file is read by an IDE's code model, not compiled by
60 GCC - clangd (__clang__) and CLion, whose clangd engine takes GCC's
61 predefined macros (no __clang__) and defines __JETBRAINS_IDE__. The
62 GCC-only constructs (register parameters, global register variables,
63 asm with operands clang cannot check) are left out there. */
64#if defined(__clang__) || defined(__JETBRAINS_IDE__)
65#define BL_CODE_MODEL 1
66#endif
67
68#if !defined(BL_CODE_MODEL) && __STDC_HOSTED__
69#error "compile Blitz libraries with -ffreestanding (see CMakeLists.txt)"
70#endif
71#ifndef BL_CODE_MODEL
72#pragma GCC optimize ("no-tree-loop-distribute-patterns")
73#endif
74
75#include <exec/types.h>
76#include <exec/memory.h>
77#include <libnums.h> /* LIB_xxx = #xxx of libnums.bb; <>: a variant
78 (libs/variants) brings its own */
79
80/* a truth value as Blitz programs see it: -1 true, 0 false (exec's TRUE
81 is 1). Exactly a ULONG (C23 fixed underlying type): d0, BL_REG and the
82 code stay the same. gcc -Wc++-compat and clang -Wassign-enum warn
83 about an int assigned to it; an enum used as a ULONG is not caught */
84typedef enum : ULONG { BB_FALSE = 0, BB_TRUE = 0xffffffffUL } BB_BOOL;
85static_assert(sizeof(BB_BOOL) == sizeof(ULONG), "BB_BOOL must be a long");
86
87/* ----------------------------------------------------------- constants */
88
89/* types (!args / !afunction) */
90#define BL_BYTE 1
91#define BL_WORD 2
92#define BL_LONG 3
93#define BL_QUICK 4
94#define BL_FLOAT 5 /* Motorola FFP, not IEEE */
95#define BL_STRING 7
96/* argument flags */
97#define BL_USESIZE 0
98#define BL_UNKNOWN 8
99#define BL_ARRAYEND 16
100#define BL_ARRAY 32
101#define BL_PUSH 64
102#define BL_VARPTR 128
103
104/* init return (!libheader `return`) */
105#define BL_WORDRET (-1)
106#define BL_NORET 0
107#define BL_LONGRET 1
108
109/* Register codes for BLITZ_LIBS / BLITZ_NULLSUB (!libs{lib,code,...}):
110 what the compiler loads from library `lib` into a register before the
111 call. The names are the constants of the original's
112 archive/cd/ultimate-blitz-basic-2.1/EXTRAS/BLITZLIBS/LIBMACS.BB with
113 the prefix BL_ in capitals (#ld0 = BL_LD0, #ia0 = BL_IA0, #pd0 =
114 BL_PD0 ...). A code is a word, register | kind:
115 high byte the register: $00-$07 d0-d7 (#ld0..#ld7), $10-$13 and $16
116 a0-a3 and a6 (#la0..#la3, #la6); $FF00 = pushed onto the
117 stack (#lpush)
118 low byte the kind:
119 0 the library's variable (its init result, e.g. the library
120 base): #ld*, #la*
121 1 #base: the base item (item 0) of an object library's block
122 of objects: #bd* = #ld*|#base, #ba*
123 2 #used: the currently used object: #ud*, #ua*
124 3 #max: the maximum setting of the compiler options: #md*,
125 #ma*
126 $80|n #item: the object whose number is in dn (#pd0..#pd7 = n);
127 the pointer ends in an address register: #ia*|#pd*
128 Not in LIBMACS.BB, from the compiler (blitz2.s:11647 toreg; the
129 original sources write them in hex):
130 4 BL_USEDOFF: the a5 offset of the pointer to the used object
131 (move.w #offset,dn; $0604 = d6)
132 $8000 BL_IFBLITZ: loaded only in Blitz mode
133 $4000 BL_IFAMIGA: loaded only in Amiga mode ($5600 = a6)
134 E.g. $1080 = BL_IA0|BL_PD0 (a0 = the object of the number in d0),
135 $1201 = BL_BA2, $0303 = BL_MD3, $5600 = BL_IFAMIGA|BL_LA6.
136
137 The letters of the names (LIBMACS.BB):
138 L Library variable: the value the library's init returned (#ld*, #la*)
139 B Base: object 0 of the library's object block (#bd*, #ba*)
140 U Used: the current object, the one `Use` selected (#ud*, #ua*)
141 M Max: the maximum of the compiler options, as a number (#md*, #ma*)
142 I Item: the object whose number is an argument (#ia*), always
143 combined with
144 PD Parameter in D: the data register that holds that number
145 (#pd0..#pd7)
146 then D or A and the register number: BL_IA0|BL_PD0 = "item into a0,
147 its number from d0".
148
149 Object libraries. B, U, M and I work only for a library that ends in
150 BLITZ_LIBFIN7(toke, load, save, use, free, defmax, shift) instead of
151 BLITZ_LIBFIN:
152 * toke names the object type ("Bank", "Shape", ...). Under that name
153 the compiler options show the library's maximum; the program's
154 .xtra file stores it as a pair "library number, maximum"
155 (blitz2.s:15412 savextra, programs/blitz2/src/data.c savextra_c;
156 read back by the same file's loader). defmax is the maximum
157 until the user changes it.
158 * shift is log2 of the object size (banklib: 4 = 16 bytes).
159 * The compiler reserves an a5 offset for the library (LIBMAX_VAROFF,
160 programs/blitz2/src/structs.h): at offset+0 the pointer to the
161 object block, at offset+4 the pointer to the used object. For
162 every object library a program uses, it records "offset, maximum <<
163 shift" (blitz2.s:2750 calcmaxs, compile.c calcmaxs), and the
164 program's startup allocates each block through memlib.
165 What the compiler generates for BL_IA0|BL_PD0 (blitz2.s:11647 toreg,
166 libcall.c toreg):
167 move.l offset(a5),a0 ; B: the object block
168 cmp.w #max,d0 ; only with runtime error checking:
169 bcs ok ; number >= maximum is a
170 jsr maxerr ; runtime error
171 ok:
172 lsl.w #shift,d0 ; number << shift
173 adda.w d0,a0 ; a0 = the object
174 so the routine sees d0 = number << shift, not the number. BL_BA*
175 loads only the first line, BL_UA* loads offset+4(a5) (with checking: a
176 runtime error if it is 0), BL_MD* loads `move.w #max,dn`. */
177#define BL_LD(n) ((n) << 8) /* #ld0..#ld7 */
178#define BL_LA(n) (0x1000 | ((n) << 8)) /* #la0..#la3, #la6 */
179#define BL_LPUSH 0xff00 /* #lpush: onto the stack */
180#define BL_BASE 1 /* #base: item 0 of the objects */
181#define BL_USED 2 /* #used: current object */
182#define BL_MAX 3 /* #max: maximum setting */
183#define BL_USEDOFF 4 /* a5 offset of the used object */
184#define BL_ITEM 0x80 /* #item: object from param dN */
185#define BL_PD(n) (n) /* #pd0..#pd7 */
186#define BL_IFBLITZ 0x8000 /* only in Blitz mode */
187#define BL_IFAMIGA 0x4000 /* only in Amiga mode */
188
189#define BL_LD0 0x0000 /* #ld0..#ld7: L = Library variable into dN */
190#define BL_LD1 0x0100
191#define BL_LD2 0x0200
192#define BL_LD3 0x0300
193#define BL_LD4 0x0400
194#define BL_LD5 0x0500
195#define BL_LD6 0x0600
196#define BL_LD7 0x0700
197#define BL_LA0 0x1000 /* #la0..#la3, #la6: L = Library variable into aN */
198#define BL_LA1 0x1100
199#define BL_LA2 0x1200
200#define BL_LA3 0x1300
201#define BL_LA6 0x1600
202
203#define BL_UD0 (BL_LD0 | BL_USED) /* #ud*, #ua*: U = Used object (LIBFIN7) */
204#define BL_UD1 (BL_LD1 | BL_USED)
205#define BL_UD2 (BL_LD2 | BL_USED)
206#define BL_UD3 (BL_LD3 | BL_USED)
207#define BL_UD4 (BL_LD4 | BL_USED)
208#define BL_UD5 (BL_LD5 | BL_USED)
209#define BL_UD6 (BL_LD6 | BL_USED)
210#define BL_UD7 (BL_LD7 | BL_USED)
211#define BL_UA0 (BL_LA0 | BL_USED)
212#define BL_UA1 (BL_LA1 | BL_USED)
213#define BL_UA2 (BL_LA2 | BL_USED)
214#define BL_UA3 (BL_LA3 | BL_USED)
215#define BL_UA6 (BL_LA6 | BL_USED)
216
217#define BL_BD0 (BL_LD0 | BL_BASE) /* #bd*, #ba*: B = Base, object 0 (LIBFIN7) */
218#define BL_BD1 (BL_LD1 | BL_BASE)
219#define BL_BD2 (BL_LD2 | BL_BASE)
220#define BL_BD3 (BL_LD3 | BL_BASE)
221#define BL_BD4 (BL_LD4 | BL_BASE)
222#define BL_BD5 (BL_LD5 | BL_BASE)
223#define BL_BD6 (BL_LD6 | BL_BASE)
224#define BL_BD7 (BL_LD7 | BL_BASE)
225#define BL_BA0 (BL_LA0 | BL_BASE)
226#define BL_BA1 (BL_LA1 | BL_BASE)
227#define BL_BA2 (BL_LA2 | BL_BASE)
228#define BL_BA3 (BL_LA3 | BL_BASE)
229#define BL_BA6 (BL_LA6 | BL_BASE)
230
231#define BL_PD0 0 /* #pd0..#pd7: PD = Parameter in dN, the number of I */
232#define BL_PD1 1
233#define BL_PD2 2
234#define BL_PD3 3
235#define BL_PD4 4
236#define BL_PD5 5
237#define BL_PD6 6
238#define BL_PD7 7
239#define BL_IA0 (BL_LA0 | BL_ITEM) /* #ia*: I = Item, object number dN (| BL_PDn) */
240#define BL_IA1 (BL_LA1 | BL_ITEM)
241#define BL_IA2 (BL_LA2 | BL_ITEM)
242#define BL_IA3 (BL_LA3 | BL_ITEM)
243#define BL_IA6 (BL_LA6 | BL_ITEM)
244
245#define BL_MD0 (BL_LD0 | BL_MAX) /* #md*, #ma*: M = Max setting (LIBFIN7) */
246#define BL_MD1 (BL_LD1 | BL_MAX)
247#define BL_MD2 (BL_LD2 | BL_MAX)
248#define BL_MD3 (BL_LD3 | BL_MAX)
249#define BL_MD4 (BL_LD4 | BL_MAX)
250#define BL_MD5 (BL_LD5 | BL_MAX)
251#define BL_MD6 (BL_LD6 | BL_MAX)
252#define BL_MD7 (BL_LD7 | BL_MAX)
253#define BL_MA0 (BL_LA0 | BL_MAX)
254#define BL_MA1 (BL_LA1 | BL_MAX)
255#define BL_MA2 (BL_LA2 | BL_MAX)
256#define BL_MA3 (BL_LA3 | BL_MAX)
257#define BL_MA6 (BL_LA6 | BL_MAX)
258
259/* ------------------------------------------------------ fixed registers */
260
261/* a5 = base of the Blitz program's globals (init results of all libs are
262 globals at the header's $0A offset), a4 = locals of the current
263 procedure, a3 = string work pointer (string results are written at a3,
264 which then points to the terminating 0). Declared as GCC global
265 register variables; every unit that includes this header must be
266 compiled with -ffixed-a3 -ffixed-a4 -ffixed-a5. */
267#ifndef BL_CODE_MODEL
268register UBYTE *blitz_globals __asm("a5");
269register UBYTE *blitz_locals __asm("a4");
270register UBYTE *blitz_string __asm("a3");
271#else
272/* clang (clangd in the IDE) has no global register variables */
273extern UBYTE *blitz_globals, *blitz_locals, *blitz_string;
274#endif
275
276/* register parameter: LONG a BL_REG(d0) = GCC's `LONG a __asm("d0")`.
277 clang does not know register parameters (amigaos GCC extension), so
278 for clangd the macro is empty. */
279#ifndef BL_CODE_MODEL
280#define BL_REG(r) __asm(#r)
281#else
282#define BL_REG(r)
283#endif
284
285/* --------------------------------------------------------- helper macros */
286
287#define BL_CAT_(a, b) a##b
288#define BL_CAT(a, b) BL_CAT_(a, b)
289#define BL_ID(p) BL_CAT(p, __COUNTER__)
290
291#define BL_NARGS(...) BL_NARGS_(__VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, \
292 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
293#define BL_NARGS_(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, \
294 a15, a16, a17, a18, a19, a20, n, ...) n
295
296/* one object of the declaration table: packed (no padding inside),
297 word aligned and of even size (= `Even` after the libmacs macro) */
298#define BL_HEAD __attribute__((section(".blitzlib.head"), used, aligned(2)))
299#define BL_PACKED __attribute__((packed, aligned(2)))
300
301/* code address as `Dc.l label`: 0 or a relocated address */
302typedef void *bl_addr;
303#define BL_A(x) ((bl_addr)(x))
304
305/* debug-only error check routines: placed after `error`, cut away by the
306 compiler when the debugger is off. */
307#define BLITZ_ERRCHK __attribute__((section(".blitzlib.errchk"), used))
308/* start of the error check area, for the `error` header field */
309extern char blitz_errchk_start[];
310#define BLITZ_ERROR_AREA blitz_errchk_start
311
312/* a library routine: keep it even if nothing in C references it */
313#define BLITZ_ROUTINE __attribute__((used))
314
315/* ------------------------------------------------------------- header */
316
317/* !syslibheader{libnum,firsttoke,lasttoke+1,init,return,finit,error,flags}
318 MOVEQ #0,d0:RTS:Dc libnum:Dc.l 0:Dc 0,flags,first,last:Dc.l 0,init
319 Dc return:Dc.l finit,0,0,0,error */
320#define BLITZ_SYSLIBHEADER(libnum, first, last, init, ret, finit, error, flags) \
321 static const struct BL_PACKED { UWORD moveq, rts; UWORD num; ULONG z6; \
322 UWORD za, fl, ft, lt; ULONG z12; bl_addr in; WORD rt; bl_addr fi; \
323 ULONG z20, z24, z28; bl_addr er; } BL_ID(bl_header_) BL_HEAD = { \
324 0x7000, 0x4e75, (UWORD)(libnum), 0, 0, (flags), (first), (last), 0, \
325 BL_A(init), (ret), BL_A(finit), 0, 0, 0, BL_A(error) }
326
327/* !syslibheader with 7 parameters (no flags):
328 ... Dc libnum:Dc.l 0,0:Dc first,last ... */
329#define BLITZ_SYSLIBHEADER7(libnum, first, last, init, ret, finit, error) \
330 BLITZ_SYSLIBHEADER(libnum, first, last, init, ret, finit, error, 0)
331
332/* !libheader{libnum,init,return,finit,error}
333 MOVEQ #0,d0:RTS:Dc libnum:Dcb.l 4,0:Dc.l init:Dc return:Dc.l finit
334 Dcb.l 3,0:Dc.l error */
335#define BLITZ_LIBHEADER(libnum, init, ret, finit, error) \
336 BLITZ_SYSLIBHEADER(libnum, 0, 0, init, ret, finit, error, 0)
337
338/* ------------------------------------------------------- token entries */
339
340#define BL_KIND(v) static const UWORD BL_ID(bl_kind_)[3] BL_HEAD = { (v), 0, 0 }
341
342#define BLITZ_ASTATEMENT() BL_KIND(1)
343#define BLITZ_USTATEMENT() BL_KIND(8 + 1)
344#define BLITZ_AFUNCTION(t) BL_KIND(((t) << 8) + 2)
345#define BLITZ_UFUNCTION() BL_KIND(8 + 2)
346#define BLITZ_ACOMMAND(t) BL_KIND(((t) << 8) + 3)
347
348/* !args{...}: Dc count:Dc.b types:Even */
349#define BLITZ_ARGS(...) \
350 static const struct BL_PACKED { UWORD n; UBYTE t[BL_NARGS(__VA_ARGS__)]; } \
351 BL_ID(bl_args_) BL_HEAD = { BL_NARGS(__VA_ARGS__), { __VA_ARGS__ } }
352#define BLITZ_ARGS0() static const UWORD BL_ID(bl_args_) BL_HEAD = 0
353
354/* !repargs{first,num,...}: Dc first<<12|num<<8|count:Dc.b types:Even */
355#define BLITZ_REPARGS(first, num, ...) \
356 static const struct BL_PACKED { UWORD n; UBYTE t[BL_NARGS(__VA_ARGS__)]; } \
357 BL_ID(bl_repargs_) BL_HEAD = { \
358 ((first) << 12) | ((num) << 8) | BL_NARGS(__VA_ARGS__), { __VA_ARGS__ } }
359
360/* !libs{lib,reg,...}: Dc lib,reg,...:Dc 0 */
361#define BLITZ_LIBS(...) \
362 static const UWORD BL_ID(bl_libs_)[] BL_HEAD = { __VA_ARGS__, 0 }
363#define BLITZ_LIBS0() static const UWORD BL_ID(bl_libs_) BL_HEAD = 0
364
365/* !subs{code,error1,error2}: Dc.l error1,code,error2 */
366#define BLITZ_SUBS(code, err1, err2) \
367 static const struct BL_PACKED { bl_addr e1, c, e2; } \
368 BL_ID(bl_subs_) BL_HEAD = { BL_A(err1), BL_A(code), BL_A(err2) }
369
370/* !name{"name","help"}: Dc -1:Dc.l 0:Dc 0:Dc.b name,0:Dc.b help,0:Even */
371#define BLITZ_NAME(name, help) \
372 static const struct BL_PACKED { UWORD m1; ULONG next; UWORD num; \
373 char n[sizeof(name)]; char h[sizeof(help)]; } \
374 BL_ID(bl_name_) BL_HEAD = { 0xffff, 0, 0, name, help }
375
376/* label:!name{"name","help"} - name entry whose name string is referenced
377 elsewhere (e.g. as !libfin's toke of an object library): &label.n */
378#define BLITZ_NAME_AT(label, name, help) \
379 static const struct BL_PACKED { UWORD m1; ULONG next; UWORD num; \
380 char n[sizeof(name)]; char h[sizeof(help)]; } \
381 label BL_HEAD = { 0xffff, 0, 0, name, help }
382
383/* !nullsub{routine,error1,error2,lib,reg,...}:
384 Dc 0,0,0:!libs{...}:Dc.l error1,routine,error2 */
385#define BLITZ_NULLSUB(code, err1, ...) \
386 BL_KIND(0); BL_NULLSUB_LIBS(__VA_ARGS__); BLITZ_SUBS(code, err1, BL_FIRST(__VA_ARGS__))
387#define BL_FIRST(a, ...) a
388#define BL_NULLSUB_LIBS(err2, ...) \
389 static const UWORD BL_ID(bl_libs_)[] BL_HEAD = { __VA_ARGS__ __VA_OPT__(,) 0 }
390
391/* !dumtoke{"name","help",label}: Dc 8,0,0:Dc.l 0:Dc 0:label:Dc.b name,0
392 Dc.b help,0:Even. `label` (for BLITZ_LIBFIN7's toke) is &label.n */
393#define BLITZ_DUMTOKE(label, name, help) \
394 static const struct BL_PACKED { UWORD k[3]; ULONG next; UWORD num; \
395 char n[sizeof(name)]; char h[sizeof(help)]; } label BL_HEAD = { \
396 { 8, 0, 0 }, 0, 0, name, help }
397
398/* !libfin: Dc -1:Dc.l 0 */
399#define BLITZ_LIBFIN() \
400 static const struct BL_PACKED { UWORD m1; ULONG z; } \
401 BL_ID(bl_libfin_) BL_HEAD = { 0xffff, 0 }
402
403/* !libfin{toke,load,save,use,free,defmax,shift} (object libraries):
404 Dc -1:Dc.l toke,load,save,use,free:Dc 0,defmax,shift
405 toke: the object type's name (a BLITZ_NAME_AT / BLITZ_DUMTOKE label
406 .n), shown with its maximum in the compiler options and stored in the
407 .xtra file; load, save, use, free: the library's routines for the
408 object type that the compiler calls itself (blitz2.s libfin); defmax: the default maximum; shift:
409 log2 of the object size. See "Object libraries" at the register codes
410 above */
411#define BLITZ_LIBFIN7(toke, load, save, use, free, defmax, shift) \
412 static const struct BL_PACKED { UWORD m1; bl_addr t, l, s, u, f; \
413 UWORD z, mx, sh; } BL_ID(bl_libfin_) BL_HEAD = { 0xffff, \
414 BL_A(toke), BL_A(load), BL_A(save), BL_A(use), BL_A(free), 0, \
415 (defmax), (shift) }
416
417/* --------------------------------------- AmigaOS call libraries (deflibs) */
418
419/* The libraries of blitz2:deflibs declare AmigaOS library calls (`Open_`,
420 `AllocMem_` ...): the compiler loads the arguments into the registers
421 and calls the LVO with a6 = the base that the init nullsub of the
422 library `oslib` returned (blitz2.s stamigalib). The originals
423 (cd/extras/blitzlibs/amigalibs) were not built with libmacs; their
424 header is the one of !libheader, but starts with 4 zero bytes instead
425 of MOVEQ #0,d0:RTS:
426 Dc.l 0:Dc libnum:Dcb.l 4,0:Dc.l init:Dc return:Dc.l finit
427 Dcb.l 3,0:Dc.l 0 (error = 0) */
428#define BLITZ_OSLIBHEADER(libnum, init, ret, finit) \
429 static const struct BL_PACKED { UWORD z0, z2; UWORD num; ULONG z6; \
430 UWORD za, fl, ft, lt; ULONG z12; bl_addr in; WORD rt; bl_addr fi; \
431 ULONG z20, z24, z28; bl_addr er; } BL_ID(bl_header_) BL_HEAD = { \
432 0, 0, (UWORD)(libnum), 0, 0, 0, 0, 0, 0, \
433 BL_A(init), (ret), BL_A(finit), 0, 0, 0, 0 }
434
435/* register codes of BLITZ_OSCALL (stamigalib: bit 4 = address register,
436 bits 0-2 = register number) */
437#define BL_OSR_d0 0x00
438#define BL_OSR_d1 0x01
439#define BL_OSR_d2 0x02
440#define BL_OSR_d3 0x03
441#define BL_OSR_d4 0x04
442#define BL_OSR_d5 0x05
443#define BL_OSR_d6 0x06
444#define BL_OSR_d7 0x07
445#define BL_OSR_a0 0x10
446#define BL_OSR_a1 0x11
447#define BL_OSR_a2 0x12
448#define BL_OSR_a3 0x13
449#define BL_OSR_a4 0x14
450#define BL_OSR_a5 0x15
451#define BL_OSR_a6 0x16
452#define BL_OSR_a7 0x17
453
454/* BL_OSREGS(d1, a0) -> BL_OSR_d1, BL_OSR_a0, (up to 19 registers) */
455#define BL_OSREGS(...) BL_CAT(BL_OSR_N, BL_NARGS(__VA_ARGS__))(__VA_ARGS__)
456#define BL_OSR_N1(r) BL_OSR_##r,
457#define BL_OSR_N2(r, ...) BL_OSR_##r, BL_OSR_N1(__VA_ARGS__)
458#define BL_OSR_N3(r, ...) BL_OSR_##r, BL_OSR_N2(__VA_ARGS__)
459#define BL_OSR_N4(r, ...) BL_OSR_##r, BL_OSR_N3(__VA_ARGS__)
460#define BL_OSR_N5(r, ...) BL_OSR_##r, BL_OSR_N4(__VA_ARGS__)
461#define BL_OSR_N6(r, ...) BL_OSR_##r, BL_OSR_N5(__VA_ARGS__)
462#define BL_OSR_N7(r, ...) BL_OSR_##r, BL_OSR_N6(__VA_ARGS__)
463#define BL_OSR_N8(r, ...) BL_OSR_##r, BL_OSR_N7(__VA_ARGS__)
464#define BL_OSR_N9(r, ...) BL_OSR_##r, BL_OSR_N8(__VA_ARGS__)
465#define BL_OSR_N10(r, ...) BL_OSR_##r, BL_OSR_N9(__VA_ARGS__)
466#define BL_OSR_N11(r, ...) BL_OSR_##r, BL_OSR_N10(__VA_ARGS__)
467#define BL_OSR_N12(r, ...) BL_OSR_##r, BL_OSR_N11(__VA_ARGS__)
468#define BL_OSR_N13(r, ...) BL_OSR_##r, BL_OSR_N12(__VA_ARGS__)
469#define BL_OSR_N14(r, ...) BL_OSR_##r, BL_OSR_N13(__VA_ARGS__)
470#define BL_OSR_N15(r, ...) BL_OSR_##r, BL_OSR_N14(__VA_ARGS__)
471#define BL_OSR_N16(r, ...) BL_OSR_##r, BL_OSR_N15(__VA_ARGS__)
472#define BL_OSR_N17(r, ...) BL_OSR_##r, BL_OSR_N16(__VA_ARGS__)
473#define BL_OSR_N18(r, ...) BL_OSR_##r, BL_OSR_N17(__VA_ARGS__)
474#define BL_OSR_N19(r, ...) BL_OSR_##r, BL_OSR_N18(__VA_ARGS__)
475/* number of registers, 0 for none */
476#define BL_NOSREGS(...) (0 __VA_OPT__(+ BL_NARGS(__VA_ARGS__)))
477
478/* BLITZ_OSCALL(oslib, lvo, "Name_", "(help)", regs...) - one AmigaOS call
479 (token kind 6): `oslib` = number of the library whose init result is
480 the base (a6), `lvo` = negative offset, regs = the registers of the
481 arguments in order (d0..d7, a0..a6), none for a call without arguments:
482 Dc 6,0,0:Dc oslib,lvo:Dc.b regs...,$ff:Even
483 Dc.l 0:Dc 0:Dc.b name,0:Dc.b help,0:Even */
484#define BLITZ_OSCALL(oslib, lvo, name, help, ...) \
485 static const struct BL_PACKED { UWORD k[3]; UWORD lib; WORD off; \
486 UBYTE r[(BL_NOSREGS(__VA_ARGS__) + 2) & ~1]; ULONG next; UWORD num; \
487 char n[sizeof(name)]; char h[sizeof(help)]; } \
488 BL_ID(bl_oscall_) BL_HEAD = { { 6, 0, 0 }, (oslib), (lvo), \
489 { __VA_OPT__(BL_OSREGS(__VA_ARGS__)) 0xff }, 0, 0, name, help }
490
491/* ------------------------------------------------- labelled table entries */
492
493/* `label:!nullsub{...}` - a nullsub whose entry address is used in the
494 header (init/finit point to the ENTRY in the table, not to the code).
495 Declare the label before the header with BLITZ_ENTRY_DECL(label). */
496#define BLITZ_ENTRY_DECL(label) extern const UWORD label[]
497
498/* BLITZ_ENTRY_C(f) - a code label (routine, thunk, BLITZ_AB_ROUTINE ...)
499 as a char array `e_f`, for table entries that need address arithmetic
500 or an address that C cannot take from a function declaration in a
501 constant initialiser. */
502#define BLITZ_ENTRY_C(f) extern char e_##f[] __asm__("_" #f)
503
504/* Code / error check address + 1 in !subs / !nullsub (blitz2.s):
505 BLITZ_AMIGAONLY(code) code + 1: the routine exists in Amiga mode
506 only (a Blitz mode call is a compile error)
507 BLITZ_BLITZONLY err1 = 1: Blitz mode only, no check
508 BLITZ_BLITZONLY_CHK(chk) err1 = chk + 1: Blitz mode only, with the
509 error check `chk` */
510#define BLITZ_AMIGAONLY(code) ((char *)(code) + 1)
511#define BLITZ_BLITZONLY ((void *)1)
512#define BLITZ_BLITZONLY_CHK(chk) ((char *)(chk) + 1)
513#define BLITZ_NULLSUB_AT(label, code, err1, ...) \
514 const UWORD label[3] BL_HEAD = { 0, 0, 0 }; \
515 BL_NULLSUB_LIBS(__VA_ARGS__); BLITZ_SUBS(code, err1, BL_FIRST(__VA_ARGS__))
516
517/* --------------------------------------------- calling other libraries */
518
519/* Address of a library routine for ALibJsr/BLibJsr: the original compiler
520 writes `JSR <long>` with the placeholder ($8000|form)<<16 | token
521 (BLibJsr: $C000|form, Blitz mode) and a relocation; the compiler that
522 links the library replaces it by the real address (blitz2.s handlejsrs).
523 blitz_tok is a linker symbol at offset 0 of the library, so
524 blitz_tok+placeholder is exactly that relocated long. */
525extern char blitz_tok[];
526#define BL_TOKVAL(tok, form) (0x80000000UL | ((ULONG)(form) << 16) | (UWORD)(tok))
527#define BL_BTOKVAL(tok, form) (0xC0000000UL | ((ULONG)(form) << 16) | (UWORD)(tok))
528#define BLITZ_TOKE(tok) ((void *)(blitz_tok + BL_TOKVAL(tok, 0)))
529/* the same placeholder as text for inline assembler, from the library
530 number and the low part of the token (libnum<<7 + n):
531 "jsr _blitz_tok+" BL_ASMTOK(LIB_printlib, 1) */
532#define BL_STR_(x) #x
533#define BL_STR(x) BL_STR_(x)
534#define BL_ASMTOK(lib, n) "0x80000000+((" BL_STR(lib) "<<7)+" BL_STR(n) ")"
535
536/* Registers passed to / returned from a routine of another library. */
537struct bl_regs {
538 ULONG d0, d1, a0, a1;
539};
540
541/* ALibJsr tok[,form] - call a routine of another (possibly original
542 assembler) library. In: r.d0/d1/a0/a1, out: the same registers. The
543 called code may change every register except a4, a5, a7 (Blitz
544 convention), so all others are declared clobbered. a3 (blitz_string)
545 is an in/out operand of every ALibJsr/BLibJsr macro: a string function
546 (UStr$, Str$ ...) writes its result at a3 and leaves a3 behind it, and
547 any routine of another library may be one. Without the operand GCC
548 would take a3 as unchanged by the call and could use a value it knows
549 from before (e.g. a3 == the buffer it just stored there). a3 is a
550 fixed register (-ffixed-a3), so the operand stays in a3 and GCC never
551 saves or restores it: a routine that moves a3 for its caller still
552 does. */
553#define BL_A3_ "+a"(blitz_string)
554#define BLITZ_ALIBJSR(tok, r) BL_JSR_(BL_TOKVAL(tok, 0), r)
555#define BLITZ_ALIBJSR_FORM(tok, form, r) BL_JSR_(BL_TOKVAL(tok, form), r)
556#define BLITZ_BLIBJSR(tok, r) BL_JSR_(BL_BTOKVAL(tok, 0), r)
557#ifndef BL_CODE_MODEL
558#define BL_JSR_(val, r) ({ \
559 register ULONG _d0 __asm("d0") = (r).d0; \
560 register ULONG _d1 __asm("d1") = (r).d1; \
561 register ULONG _a0 __asm("a0") = (r).a0; \
562 register ULONG _a1 __asm("a1") = (r).a1; \
563 __asm volatile("jsr _blitz_tok+%c5" \
564 : "+d"(_d0), "+d"(_d1), "+a"(_a0), "+a"(_a1), BL_A3_ : "i"(val) \
565 : "d2", "d3", "d4", "d5", "d6", "d7", "a2", "a6", "cc", "memory"); \
566 (r).d0 = _d0; (r).d1 = _d1; (r).a0 = _a0; (r).a1 = _a1; _d0; })
567#else
568ULONG bl_jsr_clang(ULONG val, struct bl_regs *r);
569#define BL_JSR_(val, r) bl_jsr_clang(val, &(r))
570#endif
571
572/* BLITZ_BASEJSR(base, off, r) - `JSR off(a6)` with a6 = base: call
573 through a vector or a library base whose target has an open contract,
574 e.g. fontlib's previous input handler (any library's or program's
575 handler) or a routine of a third-party library the port does not rely
576 on. In/out: r.d0/d1/a0/a1 as with BLITZ_ALIBJSR; the callee may change
577 every register except a3-a5/a7 (a6 included), so all others are
578 declared clobbered. Unlike BLITZ_ALIBJSR, a3 is no operand: the
579 routines called this way keep a3. A library by the AmigaOS convention
580 and memlib's membase table take BLITZ_SYSJSR (below). */
581#ifndef BL_CODE_MODEL
582#define BLITZ_BASEJSR(base, off, r) ({ \
583 register ULONG _d0 __asm("d0") = (r).d0; \
584 register ULONG _d1 __asm("d1") = (r).d1; \
585 register ULONG _a0 __asm("a0") = (r).a0; \
586 register ULONG _a1 __asm("a1") = (r).a1; \
587 register const void *_a6 __asm("a6") = (base); \
588 __asm volatile("jsr %c5(a6)" \
589 : "+d"(_d0), "+d"(_d1), "+a"(_a0), "+a"(_a1), "+a"(_a6) : "i"(off) \
590 : "d2", "d3", "d4", "d5", "d6", "d7", "a2", "cc", "memory"); \
591 (r).d0 = _d0; (r).d1 = _d1; (r).a0 = _a0; (r).a1 = _a1; _d0; })
592#else
593ULONG bl_basejsr_clang(const void *base, int off, struct bl_regs *r);
594#define BLITZ_BASEJSR(base, off, r) bl_basejsr_clang(base, off, &(r))
595#endif
596
597/* BLITZ_SYSJSR(base, off, r), BLITZ_SYSJSR1(base, off, x0),
598 BLITZ_SYSJSR2(base, off, x0, x1) - `JSR off(a6)` with a6 = base to a
599 routine that keeps d2-d7 and a2-a6:
600 - a library by the AmigaOS convention (d0/d1/a0/a1 scratch, the rest
601 kept): exec, graphics, intuition, diskfont, mathffp, rexxsyslib,
602 translator, ...;
603 - memlib's membase table (blitzhelp.h MEMBASE_xxx: JSR (a6) localloc,
604 JSR 4(a6) locfree, +8 the global versions), by a tested Blitz
605 contract: the KEEPS cases of libs/tests/systest.c ("memlib localloc
606 (table)" and its siblings, recorded from the original) state that
607 localloc and globalloc change d0/d1 only, locfree and globfree d0/a1
608 only.
609 Only d0, d1, a0 and a1 are declared changed, so GCC keeps its values in
610 the other registers across the call instead of saving d2-d7/a2/a6
611 around it; a6 is an input and stays. `cc` and `memory` are clobbered
612 (the callee writes memory the caller reads: AllocMem, CloseFont, ...).
613 - BLITZ_SYSJSR: r.d0/d1/a0/a1 in and out through struct bl_regs, the
614 general form (also for a second result, e.g. a0 of rexxsyslib
615 ErrorMsg); result d0.
616 - BLITZ_SYSJSR1: d0 = x0 in, result d0; d1/a0/a1 changed.
617 - BLITZ_SYSJSR2: d0 = x0 and d1 = x1 in, result d0; a0/a1 changed.
618 The usual form for mathffp (a6 = the ffplib base, !libs
619 #ffplib,#la6): BLITZ_SYSJSR2(ffp, LVO_SPAdd, a, b).
620 A routine that may change more (a handler, "whatever a6 holds") takes
621 BLITZ_BASEJSR; a call with an argument in d2-d7 or a2-a5 goes through
622 the NDK inline (proto headers with the shadow headers of ndk/safe/). */
623#ifndef BL_CODE_MODEL
624#define BLITZ_SYSJSR(base, off, r) ({ \
625 register ULONG _d0 __asm("d0") = (r).d0; \
626 register ULONG _d1 __asm("d1") = (r).d1; \
627 register ULONG _a0 __asm("a0") = (r).a0; \
628 register ULONG _a1 __asm("a1") = (r).a1; \
629 register const void *_a6 __asm("a6") = (base); \
630 __asm volatile("jsr %c5(a6)" \
631 : "+d"(_d0), "+d"(_d1), "+a"(_a0), "+a"(_a1) : "a"(_a6), "i"(off) \
632 : "cc", "memory"); \
633 (r).d0 = _d0; (r).d1 = _d1; (r).a0 = _a0; (r).a1 = _a1; _d0; })
634#define BLITZ_SYSJSR1(base, off, x0) ({ \
635 register ULONG _d0 __asm("d0") = (x0); \
636 register const void *_a6 __asm("a6") = (base); \
637 __asm volatile("jsr %c2(a6)" \
638 : "+d"(_d0) : "a"(_a6), "i"(off) \
639 : "d1", "a0", "a1", "cc", "memory"); \
640 _d0; })
641#define BLITZ_SYSJSR2(base, off, x0, x1) ({ \
642 register ULONG _d0 __asm("d0") = (x0); \
643 register ULONG _d1 __asm("d1") = (x1); \
644 register const void *_a6 __asm("a6") = (base); \
645 __asm volatile("jsr %c3(a6)" \
646 : "+d"(_d0), "+d"(_d1) : "a"(_a6), "i"(off) \
647 : "a0", "a1", "cc", "memory"); \
648 _d0; })
649#else
650#define BLITZ_SYSJSR(base, off, r) bl_basejsr_clang(base, off, &(r))
651#define BLITZ_SYSJSR1(base, off, x0) ({ \
652 struct bl_regs r_ = { .d0 = (x0) }; BLITZ_SYSJSR(base, off, r_); })
653#define BLITZ_SYSJSR2(base, off, x0, x1) ({ \
654 struct bl_regs r_ = { .d0 = (x0), .d1 = (x1) }; \
655 BLITZ_SYSJSR(base, off, r_); })
656#endif
657
658/* BLITZ_ALIBJSR_A6(tok, base, x) - ALibJsr tok with a6 = `base`, in d0 =
659 x, result d0. BLITZ_ALIBJSR does not load a6 (and declares it
660 clobbered), but some routines need a library base there, e.g.
661 floatquicklib $D300 qutofl the ffplib base: the original calls it from
662 a routine with !libs #ffplib,#la6 and a6 still loaded. a6 is declared
663 changed; a2 (the compiler's argument stack) is kept like by every
664 library routine (see notes/cmdlibs-port.md), a4/a5/a7 anyway, a3 is
665 in/out (see BLITZ_ALIBJSR). (from
666 vallib Int) */
667#ifndef BL_CODE_MODEL
668#define BLITZ_ALIBJSR_A6(tok, base, x) ({ \
669 register ULONG _d0 __asm("d0") = (x); \
670 register const void *_a6 __asm("a6") = (base); \
671 __asm volatile("jsr _blitz_tok+%c3" \
672 : "+d"(_d0), "+a"(_a6), BL_A3_ : "i"(BL_TOKVAL(tok, 0)) \
673 : "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a0", "a1", "cc", "memory"); \
674 _d0; })
675#else
676#define BLITZ_ALIBJSR_A6(tok, base, x) ((void)(base), (ULONG)(x))
677#endif
678
679/* ALibJsr with d0 = x, a2 = a2v (argument stack for the callee, e.g. a
680 string length or type word) and a6 = a6v; result d0. The callee may
681 change every register except a4/a5/a7; a3 is in/out (see
682 BLITZ_ALIBJSR). */
683#ifndef BL_CODE_MODEL
684#define BLITZ_ALIBJSR_A2A6(tok, x, a2v, a6v) ({ \
685 register ULONG _d0 __asm("d0") = (ULONG)(x); \
686 register const void *_a2 __asm("a2") = (a2v); \
687 register const void *_a6 __asm("a6") = (a6v); \
688 __asm volatile("jsr _blitz_tok+%c4" \
689 : "+d"(_d0), "+a"(_a2), "+a"(_a6), BL_A3_ : "i"(BL_TOKVAL(tok, 0)) \
690 : "d1", "d2", "d3", "d4", "d5", "d6", "d7", "a0", "a1", "cc", "memory"); \
691 _d0; })
692#else
693#define BLITZ_ALIBJSR_A2A6(tok, x, a2v, a6v) ((void)(a2v), (void)(a6v), (ULONG)(x))
694#endif
695
696/* BLITZ_ALIBJSR_CCR(tok, r, "cc") - BLITZ_ALIBJSR for a routine that
697 answers in the condition codes (iffiolib: Z = ok): in/out r.d0/d1/a0/a1
698 like BLITZ_ALIBJSR, the result is `Scc` right after the call (0 or
699 0xff), e.g. BLITZ_ALIBJSR_CCR(TOK_iffopen, r, "ne") = 0xff on error.
700 (from audiolib/ilbmifflib IFFCALL/IFFJSR) */
701#ifndef BL_CODE_MODEL
702#define BLITZ_ALIBJSR_CCR(tok, r, cc) ({ \
703 register ULONG _d0 __asm("d0") = (r).d0; \
704 register ULONG _d1 __asm("d1") = (r).d1; \
705 register ULONG _a0 __asm("a0") = (r).a0; \
706 register ULONG _a1 __asm("a1") = (r).a1; \
707 UBYTE _cc; \
708 __asm volatile("jsr _blitz_tok+%c6\n\ts" cc " %4" \
709 : "+d"(_d0), "+d"(_d1), "+a"(_a0), "+a"(_a1), "=m"(_cc), BL_A3_ \
710 : "i"(BL_TOKVAL(tok, 0)) \
711 : "d2", "d3", "d4", "d5", "d6", "d7", "a2", "a6", "cc", "memory"); \
712 (r).d0 = _d0; (r).d1 = _d1; (r).a0 = _a0; (r).a1 = _a1; _cc; })
713#else
714#define BLITZ_ALIBJSR_CCR(tok, r, cc) ((void)(r), (UBYTE)0)
715#endif
716
717/* BLITZ_ALIBJSR_CCR_D0A0(tok, "cc", d0p, a0v) - the short form: in d0 =
718 *d0p, a0 = a0v (d1/a1 undefined), out *d0p = d0; result `Scc` after
719 the call. Same register contract as BLITZ_ALIBJSR. (from ilbmifflib
720 IFFJSR: BLITZ_ALIBJSR_CCR_D0A0(TOK_iffbyte, "eq", &d0, st) = ok) */
721#ifndef BL_CODE_MODEL
722#define BLITZ_ALIBJSR_CCR_D0A0(tok, cc, d0p, a0v) ({ \
723 register ULONG _d0 __asm("d0") = *(d0p); \
724 register ULONG _d1 __asm("d1"); \
725 register const void *_a0 __asm("a0") = (a0v); \
726 register ULONG _a1 __asm("a1"); \
727 UBYTE _cc; \
728 __asm volatile("jsr _blitz_tok+%c6\n\ts" cc " %4" \
729 : "+d"(_d0), "=d"(_d1), "+a"(_a0), "=a"(_a1), "=m"(_cc), BL_A3_ \
730 : "i"(BL_TOKVAL(tok, 0)) \
731 : "d2", "d3", "d4", "d5", "d6", "d7", "a2", "a6", "cc", "memory"); \
732 *(d0p) = _d0; _cc; })
733#else
734#define BLITZ_ALIBJSR_CCR_D0A0(tok, cc, d0p, a0v) ((void)(a0v), (UBYTE)*(d0p))
735#endif
736
737/* BLITZ_ALIBJSR_D4(tok, d0, d1, d2, d3) / BLITZ_BLIBJSR_D4 - a routine of
738 another library with its arguments in d0-d3 (BLITZ_ALIBJSR only passes
739 d0/d1/a0/a1), e.g. spriteslib ShowSprite (sprite, x, y, channel).
740 Result d0; the callee may change every register except a4/a5/a7; a3
741 is in/out (see BLITZ_ALIBJSR). */
742#ifndef BL_CODE_MODEL
743#define BL_JSR_D4_(val, x0, x1, x2, x3) ({ \
744 register ULONG _d0 __asm("d0") = (x0); \
745 register ULONG _d1 __asm("d1") = (x1); \
746 register ULONG _d2 __asm("d2") = (x2); \
747 register ULONG _d3 __asm("d3") = (x3); \
748 __asm volatile("jsr _blitz_tok+%c5" \
749 : "+d"(_d0), "+d"(_d1), "+d"(_d2), "+d"(_d3), BL_A3_ : "i"(val) \
750 : "d4", "d5", "d6", "d7", "a0", "a1", "a2", "a6", "cc", "memory"); \
751 _d0; })
752#else
753#define BL_JSR_D4_(val, x0, x1, x2, x3) \
754 ((void)(x1), (void)(x2), (void)(x3), (ULONG)(x0))
755#endif
756#define BLITZ_ALIBJSR_D4(tok, x0, x1, x2, x3) \
757 BL_JSR_D4_(BL_TOKVAL(tok, 0), x0, x1, x2, x3)
758#define BLITZ_BLIBJSR_D4(tok, x0, x1, x2, x3) \
759 BL_JSR_D4_(BL_BTOKVAL(tok, 0), x0, x1, x2, x3)
760
761/* ------------------------------------------------------------- thunks */
762
763/* BL_ASM_FUNC_(attrs, name, text) - the function `name` (void name(void),
764 with `attrs`, e.g. BLITZ_ERRCHK) whose code is only the assembler
765 `text`, which ends with its own return (rts, jmp) or is data: GCC adds
766 nothing but `.align 2` (= .even), `.globl` and the label; noipa: GCC
767 must not fold two routines with the same text into one (identical code
768 folding would make one a call of the other, which with
769 __builtin_unreachable has no return behind it). All
770 assembler routines of this file are such functions and not top-level
771 asm: GCC switches to their section itself and knows the section it is
772 in behind them. A section switch in top-level asm is invisible to GCC
773 (the assembler of this toolchain has no .pushsection/.popsection to
774 undo it), so a function behind it that GCC places in the section it
775 believes current gets no section directive and lands in the section
776 the asm left (#1056: check functions behind BLITZ_ERRCHK_THUNK in the
777 normal code). */
778#ifndef BL_CODE_MODEL
779#define BL_NOIPA_ __attribute__((noipa))
780#else
781#define BL_NOIPA_ /* clang has no noipa */
782#endif
783#define BL_ASM_FUNC_(attrs, name, text) \
784 attrs BL_NOIPA_ void name(void) \
785 { \
786 __asm volatile(text); \
787 __builtin_unreachable(); \
788 }
789
790/* Library entry with a register contract that a C function cannot express
791 itself. `entry` is the label used in the token table. The thunk saves
792 the registers `save` - the ones the original routine preserves but GCC
793 may change (d0, d1, a0, a1; GCC itself keeps d2-d7/a2-a6) - calls the C
794 function `cfunc` (its register parameters are passed through unchanged)
795 and restores them; movem does not change the CCR.
796 BLITZ_THUNK(entry, cfunc, "d0-d1/a0-a1")
797 BLITZ_THUNK_Z(entry, cfunc, save) CCR result: Z = (C result == 0);
798 the C function must return a 32 bit type (tst.l d0) - not BOOL,
799 which is a 16 bit type in exec/types.h
800 BLITZ_THUNK_PRE(entry, cfunc, save, pre)
801 `pre` = assembler run after saving, before the call, e.g. to load
802 a stack argument into a register parameter. The stack then
803 holds: saved registers (4 bytes each), the caller's return
804 address, the caller's stack arguments. */
805#define BLITZ_THUNK(entry, cfunc, save) BL_THUNK_(entry, cfunc, save, "", "")
806
807/* BLITZ_ALIAS(entry, cfunc) - the entry `entry` of the token table is the
808 C function `cfunc` itself, without a thunk: the entry of a user-level
809 statement or function, which keeps a4-a7 and (a function) a3 and
810 nothing else must survive it (the register rule, docs/libs/deviations.md
811 "Register preservation"); GCC keeps d2-d7/a2/a6 anyway, a3-a5 are
812 fixed. `cfunc` is a BLITZ_ROUTINE (used), so GCC emits it under its
813 own name with the register parameters it declares. An exception of
814 the rule that GCC's code keeps by nature is an alias too; its KEEPS
815 statement in the library tests states the registers. */
816#define BLITZ_ALIAS(entry, cfunc) \
817 __asm__(" .globl _" #entry "\n .set _" #entry ",_" #cfunc)
818
819/* BLITZ_ENTRYPOINT - in front of the C function of a user-level entry
820 (static BLITZ_ENTRYPOINT BLITZ_ROUTINE ULONG f_c(...)) that GCC is to
821 compile without saving the callee-saved registers d2-d7/a2/a6 (GCC's
822 __entrypoint): the register rule does not need d2-d7/a2 kept, and the
823 saves are the cost of a short routine. BLITZ_ALIAS_EP(entry, cfunc)
824 behind it makes it the entry and marks it for the build check
825 (blitzlib entrypoints, tools/lib/epcheck), which refuses:
826 - a6 written without a save at the routine's start (the rule keeps
827 a6; __entrypoint gives it free, so a routine that loads a library
828 base must not be one);
829 - a call or jump into the routine from other code: GCC does not know
830 at a call site that the routine keeps nothing, so it must never be
831 called from C;
832 - folding with a function of the same body: GCC's identical code
833 folding merges them (the plain one loses its saves); no_icf keeps
834 the routine apart.
835 Only for entries; never for internal tokens (they keep d2-d7). */
836#ifndef BL_CODE_MODEL
837#define BLITZ_ENTRYPOINT __entrypoint __attribute__((no_icf))
838#else
839#define BLITZ_ENTRYPOINT
840#endif
841#define BLITZ_ALIAS_EP(entry, cfunc) \
842 __asm__(" .globl _" #entry "\n .set _" #entry ",_" #cfunc \
843 "\n .globl ___blitz_entrypoint_" #entry \
844 "\n .set ___blitz_entrypoint_" #entry ",_" #cfunc)
845/* BLITZ_EP_MARK(cfunc) - the marker of BLITZ_ALIAS_EP alone, for an
846 __entrypoint routine that is the Blitz mode code of a
847 BLITZ_AB_ROUTINE (reached through its `Dc.l`, not a symbol of its own) */
848#define BLITZ_EP_MARK(cfunc) \
849 __asm__(" .globl ___blitz_entrypoint_" #cfunc \
850 "\n .set ___blitz_entrypoint_" #cfunc ",_" #cfunc)
851/* BLITZ_EP_KEEP_A6() / BLITZ_EP_KEEP_A6_END() - at the start and the end
852 of an __entrypoint routine that needs more address registers than GCC
853 finds in a0-a2: the caller's a6 is a live hard register from the one
854 to the other, so GCC does not take a6 (the build check refuses a
855 routine that writes it) and keeps its pointers in a0-a2 and d0-d7 */
856#ifndef BL_CODE_MODEL
857#define BLITZ_EP_KEEP_A6() \
858 register ULONG bl_keep_a6_ __asm("a6"); \
859 __asm__ volatile("" : "=a"(bl_keep_a6_))
860#define BLITZ_EP_KEEP_A6_END() __asm__ volatile("" : : "a"(bl_keep_a6_))
861#else
862#define BLITZ_EP_KEEP_A6()
863#define BLITZ_EP_KEEP_A6_END()
864#endif
865#define BLITZ_THUNK_Z(entry, cfunc, save) \
866 BL_THUNK_(entry, cfunc, save, "", " tst.l d0\n")
867#define BLITZ_THUNK_PRE(entry, cfunc, save, pre) \
868 BL_THUNK_(entry, cfunc, save, " " pre "\n", "")
869/* BLITZ_THUNK_POST(entry, cfunc, save, post)
870 `post` = assembler run after the call, before restoring `save`,
871 e.g. "move.l d0,a0" (result in a0: a0 not in `save`) or
872 "move.w d0,2(sp)" (word result, the caller's upper word of d0
873 kept: `save` must start with d0, its low word is at 2(sp)). */
874#define BLITZ_THUNK_POST(entry, cfunc, save, post) \
875 BL_THUNK_(entry, cfunc, save, "", " " post "\n")
876#define BL_THUNK_(entry, cfunc, save, pre, tail) \
877 BL_ASM_FUNC_(, entry, \
878 " movem.l " save ",-(sp)\n" pre \
879 " jsr _" #cfunc "\n" tail \
880 " movem.l (sp)+," save "\n" \
881 " rts\n")
882
883/* BLITZ_THUNK_POP(entry, cfunc, save, pre, tail, pop) - like the thunks
884 above for a routine that removes its own stack arguments (the original
885 ends with e.g. MOVE.l (a7)+,(a7):RTS): after restoring `save`, the
886 return address is moved up by `pop` bytes and the stack pointer
887 advanced - with movem/lea only, so the CCR set by `tail` survives.
888 save registers to keep, may be "" (nothing saved)
889 pre assembler before the call (stack: saved registers, return
890 address, arguments - as with BLITZ_THUNK_PRE), may be ""
891 tail assembler after the call, e.g. "tst.l d0" for a CCR result
892 pop number of argument bytes to remove (4, 8 ...; 0 = none, for a
893 routine with a CCR result but nothing to save)
894 (added by the mathstr group: strings2lib, strcomplib) */
895#define BLITZ_THUNK_POP(entry, cfunc, save, pre, tail, pop) \
896 BL_ASM_FUNC_(, entry, \
897 " .ifnc \"" save "\",\"\"\n" \
898 " movem.l " save ",-(sp)\n" \
899 " .endif\n" \
900 " " pre "\n" \
901 " jsr _" #cfunc "\n" \
902 " " tail "\n" \
903 " .ifnc \"" save "\",\"\"\n" \
904 " movem.l (sp)+," save "\n" \
905 " .endif\n" \
906 " .if " #pop "\n" \
907 " movem.l a0,-(sp)\n" \
908 " movem.l 4(sp),a0\n" \
909 " movem.l a0,4+" #pop "(sp)\n" \
910 " movem.l (sp)+,a0\n" \
911 " lea " #pop "(sp),sp\n" \
912 " .endif\n" \
913 " rts\n")
914
915/* ------------------------------------------- error checks (debugger only) */
916
917/* BLITZ_ERRCHK_THUNK(entry, cfunc, save) - BLITZ_THUNK for an error check
918 routine (!subs/!nullsub err1/err2): the thunk goes into the error check
919 area (.blitzlib.errchk) like the C function `cfunc`, which is declared
920 BLITZ_ERRCHK. An error check runs right before the routine, with the
921 routine's arguments in the registers; it must keep them and everything
922 else the original check keeps: `save` = the registers of d0-d1/a0-a1
923 the original does not change (GCC keeps d2-d7/a2-a6 itself), e.g.
924 "d0-d1/a0-a1" for a check that only compares (vallib intchk),
925 "d0-d1/a1" when the original changes a0 (linklistlib). The check
926 function can take its inputs as register parameters (d0, a2 ...);
927 they arrive unchanged. */
928#define BLITZ_ERRCHK_THUNK(entry, cfunc, save) \
929 BL_ASM_FUNC_(BLITZ_ERRCHK, entry, \
930 " movem.l " save ",-(sp)\n" \
931 " jsr _" #cfunc "\n" \
932 " movem.l (sp)+," save "\n" \
933 " rts\n")
934
935/* BLITZ_ERRCHK_MESSAGE(label, "text") - a runtime error message
936 (BLITZ_RUNERR(label)) in the error check area, i.e. cut away with the
937 checks when the debugger is off, as in the original. Assembler data
938 (in the function bl_errchk_message_label, BL_ASM_FUNC_): GCC refuses
939 code and data in the same named section, so as soon as a unit has C
940 check functions (BLITZ_ERRCHK) there, its messages cannot be C arrays
941 with BLITZ_ERRCHK (units without check code may do that, see memlib).
942 `text` is a C string literal without " or \ escapes. */
943#define BLITZ_ERRCHK_MESSAGE(label, text) \
944 extern const char label[]; \
945 BL_ASM_FUNC_(static BLITZ_ERRCHK, bl_errchk_message_##label, \
946 "_" #label ":\n .asciz \"" text "\"\n .even\n")
947
948/* BLITZ_ERRCHK_AB(entry, blitz, amiga) - an error check with Blitz/Amiga
949 variants in the error check area: `Dc $a001:Dc.l blitz` and then the
950 Amiga mode check (JMP amiga), see BLITZ_AB_ROUTINE. `blitz`/`amiga`
951 are check entries (BLITZ_ERRCHK_THUNK or asm). */
952#define BLITZ_ERRCHK_AB(entry, blitz, amiga) \
953 BL_ASM_FUNC_(BLITZ_ERRCHK, entry, \
954 " .word 0xa001\n" \
955 " .long _" #blitz "\n" \
956 " jmp _" #amiga "\n")
957
958/* BLITZ_ERRCHK_ASM(name, "text") - assembler in the error check area: check
959 routines that need assembler (a CCR input, the caller's return address,
960 a CPU trap) and their error branches. `text` brings its own labels
961 (with .globl for the entries of the header) and ends with a return or
962 a trap; `name` only names the block (the static function
963 bl_errchk_asm_name, BL_ASM_FUNC_, which starts at the block's first
964 byte). A library writes no section directive of its own (#1057, the
965 build checks it: cmake/check-asm-sections.cmake). */
966#define BLITZ_ERRCHK_ASM(name, text) \
967 BL_ASM_FUNC_(static BLITZ_ERRCHK, bl_errchk_asm_##name, text "\n")
968
969/* A routine in assembler, BLITZ_ASM_ROUTINE(entry, "text"): by nature (stack
970 pointer, exception frames, hardware) or for speed on a hot path measured
971 with the perf preset; say in a comment which. */
972#define BLITZ_ASM_ROUTINE(entry, text) BL_ASM_FUNC_(, entry, text "\n")
973
974/* BLITZ_ASM_BLOCK(name, "text") - assembler in the normal code that is not
975 one routine with its entry first: handlers with BRA entries in front
976 of their label, several routines, data in the code hunk. `text` brings
977 its own labels (and .globl); `name` only names the block (the static
978 function bl_asm_name, BL_ASM_FUNC_, which starts at the block's first
979 byte). Like BLITZ_ERRCHK_ASM it replaces top-level asm with a section
980 directive of its own (#1057). */
981#define BLITZ_ASM_BLOCK(name, text) \
982 BL_ASM_FUNC_(static BLITZ_ROUTINE, bl_asm_##name, text "\n")
983
984/* Inline routine (blitz2.s doajsr2): `Dc $a000,len` before the code;
985 the compiler copies the `len` bytes to the call instead of a JSR.
986 Inline code is by nature a few instructions, so it is given as
987 assembler text; the final RTS is for the JSR case (constant folding). */
988#define BLITZ_INLINE(entry, text) \
989 BL_ASM_FUNC_(, entry, \
990 " .word 0xa000,2f-1f\n" \
991 "1: " text "\n" \
992 "2: rts\n")
993
994/* Blitz/Amiga variants (`Dc $a001:Dc.l blitzcode` in front of the code):
995 the compiler (blitz2.s doajsr2 .isone) calls `blitzcode` in Blitz mode
996 and the code behind the 6 byte prefix (entry+6) in Amiga mode.
997 BLITZ_AB_ROUTINE(entry, blitzentry, "text")
998 `text` is the Amiga mode code, e.g. a thunk sequence that calls a C
999 function; `blitzentry` is an ordinary routine (BLITZ_THUNK, C, asm). */
1000#define BLITZ_AB_ROUTINE(entry, blitzentry, text) \
1001 BL_ASM_FUNC_(, entry, \
1002 " .word 0xa001\n" \
1003 " .long _" #blitzentry "\n" text "\n")
1004
1005/* Runtime error: `MOVE.l #message,d0:TRAP #0` - handled by the debugger
1006 (runtime error requester). */
1007#ifndef BL_CODE_MODEL
1008#define BLITZ_RUNERR(msg) do { \
1009 register const char *_m __asm("d0") = (msg); \
1010 __asm volatile("trap #0" : : "d"(_m) : "memory"); \
1011} while (0)
1012#else
1013#define BLITZ_RUNERR(msg) ((void)(msg))
1014#endif
1015
1016#include "blitzhelp.h" /* register arithmetic, a2 stack, mathffp LVOs, tokens */
1017
1018#endif
BB_BOOL
Definition blitzlib.h:84
@ BB_FALSE
Definition blitzlib.h:84
@ BB_TRUE
Definition blitzlib.h:84
char blitz_errchk_start[]
char blitz_tok[]
register UBYTE * blitz_locals
Definition blitzlib.h:269
register UBYTE * blitz_globals
Definition blitzlib.h:268
void * bl_addr
Definition blitzlib.h:302
register UBYTE * blitz_string
Definition blitzlib.h:270
Definition blitzlib.h:537
ULONG d0
Definition blitzlib.h:538
ULONG a1
Definition blitzlib.h:538
ULONG d1
Definition blitzlib.h:538
ULONG a0
Definition blitzlib.h:538