1 \
FLK "Foreign" language
support using shared objects
3 \
Since the shared objects
have to be
loaded on startup
and closed on shutdown
4 \ we
have to maitain a storage
of the libraries. This is done in the words
5 \ using a linked list
which is checked on startup
and shutdown
. To maintain
6 \
the correct order
of the libraries all
declared libraries are appended
to
12 0x100 CONSTANT RTLD_GLOBAL
14 \
Head of the library list. The end-of-list pointer is IMAGE-BASE.
15 IMAGE-BASE RVALUE libraries
17 \
The tail of the library list.
18 #? libraries RVALUE last-lib
20 macro getlib
( addr len
-- lib
)
22 RTLD_LAZY RTLD_GLOBAL OR OPENLIB \ addr len lib
24 DROP ." Error loading " q
-TYPE
25 ." : " LIBERROR strlen
TYPE CR
31 macro get
-symbol
( lib addr len
-- sym
)
33 LIBERROR strlen
TYPE CR ABORT
36 \
Format of the data area
of a
library word:
39 \
1 cell
library handle
40 macro lib
>handle
CELL+
42 \
2 cells
head of symbol
chain
48 \
Format of the data area
of a symbol
word.
58 \
Create a named
library from
the filename given
. If the filename is not a
59 \ global
one (i
.e
. starting
with a slash
) the default library search path
is
60 \ used
. See also: man page
of dlopen
.
61 : lib
( addr len
-<name>- )
62 2DUP getlib
CREATE HERE \ addr len lib link
69 IMAGE-BASE r
, \ addr len
75 macro
(push
-param
-reg
) ( regs
-- )
84 macro
(push
-param
-mem
) ( mems
-- )
86 DWORD offs
-ebp
[ebp
] push
,
92 macro
(link
-sym
) ( -- )
94 ABORT" No library declared to load from."
95 lib
>sym
DUP @ \ sh lsh
106 ALSO C-CALLS DEFINITIONS PREVIOUS
109 \
Define an integer parameter
for the use in a shared
library function.
117 \
Define some integer parameters
for the use in a shared
library function.
118 : ints
( n ints
-- n
+1)
120 TUCK #USEREGS MIN DUP \ ints n regs regs
121 (push
-param
-reg
) \ ints
n regs
122 PLUCK SWAP - \ ints
n mems
123 (push
-param
-mem
) \ ints
n
126 \
Define a float parameter for the use in a shared
library function.
130 #? FSP #[] free0 mov,
131 B/FLOAT ## free0 sub,
135 FSTACK +relocate
[free0
] fldf
,
136 free0
#? FSP #[] mov,
143 \
Define a double
parameter for the use in a shared
library function.
149 #? FSP #[] free0 mov,
150 B/FLOAT ## free0 sub,
154 FSTACK +relocate
[free0
] fldf
,
155 free0
#? FSP #[] mov,
162 \
Define an ignored
parameter for the use in a shared
library function.
168 \
Create a word name as a FORTH front
end to the library function named
169 \ libname
. returns
is one of the constants above
and determines
the return
170 \
type of the function.
171 : fct
: ( returns
<name params> -- )
173 currentlib lib
>handle
@
175 >IN @ SWAP \ ret
ind lib
176 BL PARSE \ ret
ind lib addr len
178 get
-symbol \ ret
ind sym
181 regalloc
-reset \ ret
sym
182 DWORD HA-SAVE-F-EBP #[] push,
185 0 INTERPRET \ ret
sym cells
-esp
189 ebp
HA-SAVE-F-EBP #[] mov,
190 SWAP ## call, \ ret cells-esp
198 DWORD HA-SAVE-F-EBP #[] pop,
204 double
OF POSTPONE fpush
ENDOF
210 \
Create a variable
found in lib
.
213 currentlib lib
>handle
@
216 BL PARSE \ lib
ind ln
#ln
217 2DUP 2>R \ lib
ind ln
#ln
218 ROT >IN ! : \ lib ln
#ln
226 ALSO CB-PARAMS DEFINITIONS PREVIOUS
229 \
Define an integer
parameter to be
used in a callback
function.
230 : int ( ind -- ind+1 )
235 DUP CELLS [free0
] free0 mov
,
243 \
Define word that
's execution semantics can only be used as a callback
244 \ function. A callback must be ended by ;callback
245 : callback: ( returns -<name params>- ret cs)
252 -ROT (namedHeader) \ xt
253 CFT-callback (new-cs-item)
254 (curr-cf-item) 3 CELLS + !
259 HA-SAVE-F-EBP #[] ebp mov,
265 OVER int <> IF eax push, 8 ELSE 7 THEN
266 ALSO CB-PARAMS INTERPRET
268 (curr-cf-item) 4 CELLS + !
272 \ End a callback definition.
275 CFT-callback (check-cs-item)
276 (curr-cf-item) 4 CELLS + @
277 DUP double = IF POSTPONE fpop THEN
291 FALSE STATE ! \ cs=xt
292 (curr-cf-item) 3 CELLS + @
294 DUP C@ HF-VISIBLE OR SWAP C!
299 \ Get the CFA of a word.
300 : callback ( -<name>- )
305 \
Correct all symbols
in the chain.
306 : (init
-lib
-syms
) ( lib
sym-dict
-- )
310 DUP sym>name $
COUNT \ lib sd addr len
311 FLOCK -ROT get
-symbol \ lib sd
sym
312 OVER sym>fix @ \ lib sd
sym fix
313 TUCK - SWAP CELL- ! \ lib sd
317 \
Load all libraries but
not the symbols to be order independed
.
319 libraries BEGIN \ lib
-dict
323 DUP lib
>name $
COUNT \ lib
-dict
addr len
324 getlib \ lib
-dict
lib
325 OVER lib>handle
! \
lib-dict
lib
329 \
Load all declared symbols from all libraries which have to be loaded
331 : (load
-symbols) ( -- )
332 libraries BEGIN \
lib-dict
336 DUP lib>handle
@ \
lib-dict
lib
337 OVER lib>sym @ (init
-lib-syms
)
341 \
Load all libraries in the chain and fix their
symbols.
349 \ close
all libraries in the chain
356 DUP lib>handle
@ CLOSELIB
360 \
Find the first library in the chain below
xt and store
its address
into
361 \
libraries. All above xt are closed
.
362 : forget
-lib ( xt -- xt )
365 libraries BEGIN \
xt lib-dict
369 DUP lib>handle
@ CLOSELIB
371 REPEAT TO libraries DROP ;
373 ' init-lib TO initializer
374 ' done-lib TO finisher
375 ' forget-lib TO forgetter
377 \ show all loaded libs
380 DUP IMAGE-BASE = IF ." no libs loaded." CR THEN
385 DUP lib>name $COUNT TYPE SPACE