]> gitweb.factorcode.org Git - factor.git/blob - core/alien/alien-docs.factor
Remove Windows CE from core/ basis/ and build-support/
[factor.git] / core / alien / alien-docs.factor
1 USING: byte-arrays arrays help.syntax help.markup
2 alien.syntax compiler definitions math libc eval
3 debugger parser io io.backend system alien.accessors
4 alien.libraries alien.c-types quotations kernel
5 sequences ;
6 IN: alien
7
8 HELP: cdecl
9 { $description "This symbol is passed as the " { $snippet "abi" } " argument to " { $link alien-indirect } ", " { $link alien-callback } ", " { $link alien-assembly } ", and " { $link add-library } " to indicate that the standard C calling convention should be used, where the caller cleans up the stack frame after calling the function. This symbol only has meaning on 32-bit x86 platforms." } ;
10
11 HELP: stdcall
12 { $description "This symbol is passed as the " { $snippet "abi" } " argument to " { $link alien-indirect } ", " { $link alien-callback } ", " { $link alien-assembly } ", and " { $link add-library } " to indicate that the Windows API calling convention should be used, where the called function cleans up its own stack frame before returning to the caller. This symbol only has meaning on 32-bit x86 platforms." } ;
13
14 HELP: fastcall
15 { $warning "In the current implementation this ABI only works for functions that take only integer and pointer arguments." }
16 { $description "This symbol is passed as the " { $snippet "abi" } " argument to " { $link alien-indirect } ", " { $link alien-callback } ", " { $link alien-assembly } ", and " { $link add-library } " to indicate that the \"fast call\" calling convention should be used, where the first two integer or pointer arguments are passed in registers and the function cleans up its own stack frame before returning to the caller. This symbol only has meaning on 32-bit x86 platforms." } ;
17
18 HELP: thiscall
19 { $description "This symbol is passed as the " { $snippet "abi" } " argument to " { $link alien-indirect } ", " { $link alien-callback } ", " { $link alien-assembly } ", and " { $link add-library } " to indicate that Microsoft Visual C++ calling convention should be used, where the first argument (which must be a \"this\" pointer) is passed in a register and the function cleans up its own stack frame before returning to the caller. This symbol only has meaning on 32-bit x86 platforms." } ;
20
21 { cdecl stdcall fastcall thiscall } related-words
22
23 HELP: >c-ptr
24 { $values { "obj" object } { "c-ptr" c-ptr } }
25 { $contract "Outputs a pointer to the binary data of this object." } ;
26
27 HELP: byte-length
28 { $values { "obj" object } { "n" "a non-negative integer" } }
29 { $contract "Outputs the number of bytes of binary data that will be output by " { $link >c-ptr } "." } ;
30
31 HELP: element-size
32 { $values { "seq" sequence } { "n" "a non-negative integer" } }
33 { $contract "Outputs the number of bytes used for each element of the sequence." }
34 { $notes "If a sequence class implements " { $link element-size } " and " { $link >c-ptr } ", then instances of this sequence, as well as slices of this sequence, can be used as binary objects." } ;
35
36 { >c-ptr element-size byte-length } related-words
37
38 HELP: alien
39 { $class-description "The class of alien pointers. See " { $link "syntax-aliens" } " for syntax and " { $link "c-data" } " for general information." } ;
40
41 HELP: dll
42 { $class-description "The class of native library handles. See " { $link "syntax-aliens" } " for syntax and " { $link "dll.private" } " for general information." } ;
43
44 HELP: dll-valid? ( dll -- ? )
45 { $values { "dll" dll } { "?" "a boolean" } }
46 { $description "Returns true if the library exists and is loaded." } ;
47
48 HELP: expired?
49 { $values { "c-ptr" c-ptr } { "?" "a boolean" } }
50 { $description "Tests if the alien is a relic from an earlier session. A byte array is never considered to have expired, whereas passing " { $link f } " always yields true." } ;
51
52 HELP: <bad-alien>
53 { $values  { "alien" c-ptr } }
54 { $description "Constructs an invalid alien pointer that has expired." } ;
55
56 HELP: <displaced-alien> ( displacement c-ptr -- alien )
57 { $values { "displacement" "an integer" } { "c-ptr" c-ptr } { "alien" "a new alien" } }
58 { $description "Creates a new alien address object, wrapping a raw memory address. The alien points to a location in memory which is offset by " { $snippet "displacement" } " from the address of " { $snippet "c-ptr" } "." }
59 { $notes "Passing a value of " { $link f } " for " { $snippet "c-ptr" } " creates an alien with an absolute address; this is how " { $link <alien> } " is implemented."
60 $nl
61 "Passing a zero absolute address does not construct a new alien object, but instead makes the word output " { $link f } "." } ;
62
63 { <alien> <displaced-alien> alien-address } related-words
64
65 HELP: alien-address ( c-ptr -- addr )
66 { $values { "c-ptr" c-ptr } { "addr" "a non-negative integer" } }
67 { $description "Outputs the address of an alien." }
68 { $notes "Taking the address of a " { $link byte-array } " is explicitly prohibited since byte arrays can be moved by the garbage collector between the time the address is taken, and when it is accessed. If you need to pass pointers to C functions which will persist across alien calls, you must allocate unmanaged memory instead. See " { $link "malloc" } "." } ;
69
70 HELP: <alien>
71 { $values { "address" "a non-negative integer" } { "alien" "a new alien address" } }
72 { $description "Creates an alien object, wrapping a raw memory address." }
73 { $notes "Alien objects are invalidated between image saves and loads." } ;
74
75 HELP: c-ptr
76 { $class-description "Class of objects consisting of aliens, byte arrays and " { $link f } ". These objects all can be used as values of " { $link pointer } " C types." } ;
77
78 HELP: alien-invoke-error
79 { $error-description "Thrown if the word calling " { $link alien-invoke } " was not compiled with the optimizing compiler. This may be a result of one of several failure conditions:"
80     { $list
81         { "This can happen when experimenting with " { $link alien-invoke } " in this listener. To fix the problem, place the " { $link alien-invoke } " call in a word; word definitions are automatically compiled with the optimizing compiler." }
82         { "The return type or parameter list references an unknown C type." }
83         { "The symbol or library could not be found." }
84         { "One of the four inputs to " { $link alien-invoke } " is not a literal value. To call functions which are not known at compile-time, use " { $link alien-indirect } "." }
85     }
86 } ;
87
88 HELP: alien-invoke
89 { $values { "args..." "zero or more objects passed to the C function" } { "return" "a C return type" } { "library" "a logical library name" } { "function" "a C function name" } { "parameters" "a sequence of C parameter types" } { "return..." "the return value of the function, if not " { $link void } } }
90 { $description "Calls a C library function with the given name. Input parameters are taken from the data stack, and the return value is pushed on the data stack after the function returns. A return type of " { $link void } " indicates that no value is to be expected." }
91 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
92 { $errors "Throws an " { $link alien-invoke-error } " if the word calling " { $link alien-invoke } " was not compiled with the optimizing compiler." } ;
93
94 HELP: alien-indirect-error
95 { $error-description "Thrown if the word calling " { $link alien-indirect } " was not compiled with the optimizing compiler. This may be a result of one of two failure conditions:"
96     { $list
97         { "This can happen when experimenting with " { $link alien-indirect } " in this listener. To fix the problem, place the " { $link alien-indirect } " call in a word; word definitions are automatically compiled with the optimizing compiler." }
98         { "One of the three inputs to " { $link alien-indirect } " is not a literal value." }
99     }
100 } ;
101
102 HELP: alien-indirect
103 { $values { "args..." "zero or more objects passed to the C function" } { "funcptr" "a C function pointer" } { "return" "a C return type" } { "parameters" "a sequence of C parameter types" } { "abi" "one of " { $link cdecl } " or " { $link stdcall } } { "return..." "the return value of the function, if not " { $link void } } }
104 { $description
105     "Invokes a C function pointer passed on the data stack. Input parameters are taken from the data stack following the function pointer, and the return value is pushed on the data stack after the function returns. A return type of " { $link void } " indicates that no value is to be expected."
106 }
107 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
108 { $errors "Throws an " { $link alien-indirect-error } " if the word calling " { $link alien-indirect } " is not compiled." } ;
109
110 HELP: alien-callback-error
111 { $error-description "Thrown if the word calling " { $link alien-callback } " was not compiled with the optimizing compiler. This may be a result of one of two failure conditions:"
112     { $list
113         { "This can happen when experimenting with " { $link alien-callback } " in this listener. To fix the problem, place the " { $link alien-callback } " call in a word; word definitions are automatically compiled with the optimizing compiler." }
114         { "One of the four inputs to " { $link alien-callback } " is not a literal value." }
115     }
116 } ;
117
118 HELP: alien-callback
119 { $values { "return" "a C return type" } { "parameters" "a sequence of C parameter types" } { "abi" "one of " { $link cdecl } " or " { $link stdcall } } { "quot" quotation } { "alien" alien } }
120 { $description
121     "Defines a callback from C to Factor which accepts the given set of parameters from the C caller, pushes them on the data stack, calls the quotation, and passes a return value back to the C caller. A return type of " { $snippet "void" } " indicates that no value is to be returned."
122     $nl
123     "When a compiled reference to this word is called, it pushes the callback's alien address on the data stack. This address can be passed to any C function expecting a C function pointer with the correct signature. The callback is actually generated when the word calling " { $link alien-callback } " is compiled."
124     $nl
125     "Callback quotations run with freshly-allocated stacks. This means the data stack contains the values passed by the C function, and nothing else. It also means that if the callback throws an error which is not caught, the Factor runtime will halt. See " { $link "errors" } " for error handling options."
126 }
127 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
128 { $examples
129     "A simple example, showing a C function which returns the difference of two given integers:"
130     { $code
131         ": difference-callback ( -- alien )"
132         "    int { int int } cdecl [ - ] alien-callback ;"
133     }
134 }
135 { $errors "Throws an " { $link alien-callback-error } " if the word calling " { $link alien-callback } " is not compiled." } ;
136
137 HELP: alien-assembly-error
138 { $error-description "Thrown if the word calling " { $link alien-assembly } " was not compiled with the optimizing compiler. This may be a result of one of two failure conditions:"
139     { $list
140         { "This can happen when experimenting with " { $link alien-assembly } " in this listener. To fix the problem, place the " { $link alien-assembly } " call in a word; word definitions are automatically compiled with the optimizing compiler." }
141         { "One of the four inputs to " { $link alien-assembly } " is not a literal value." }
142     }
143 } ;
144
145 HELP: alien-assembly
146 { $values { "args..." "zero or more objects passed to the C function" } { "return" "a C return type" } { "parameters" "a sequence of C parameter types" } { "abi" "one of " { $link cdecl } " or " { $link stdcall } } { "quot" quotation } { "return..." "the return value of the function, if not " { $link void } } }
147 { $description
148     "Invokes arbitrary machine code, generated at compile-time by the quotation. Input parameters are taken from the data stack, and the return value is pushed on the data stack after the function returns. A return type of " { $link void } " indicates that no value is to be expected."
149 }
150 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
151 { $errors "Throws an " { $link alien-assembly-error } " if the word calling " { $link alien-assembly } " is not compiled." } ;
152
153 { alien-invoke alien-indirect alien-assembly alien-callback } related-words
154
155 ARTICLE: "alien-expiry" "Alien expiry"
156 "When an image is loaded, any alien objects which persisted from the previous session are marked as having expired. This is because the C pointers they contain are almost certainly no longer valid."
157 $nl
158 "For this reason, the " { $link POSTPONE: ALIEN: } " word should not be used in source files, since loading the source file then saving the image will result in the literal becoming expired. Use " { $link <alien> } " instead, and ensure the word calling " { $link <alien> } " is not declared " { $link POSTPONE: flushable } "."
159 { $subsections expired? } ;
160
161 ARTICLE: "aliens" "Alien addresses"
162 "Instances of the " { $link alien } " class represent pointers to C data outside the Factor heap:"
163 { $subsections
164     <alien>
165     <displaced-alien>
166     alien-address
167 }
168 "Anywhere that a " { $link alien } " instance is accepted, the " { $link f } " singleton may be passed in to denote a null pointer."
169 $nl
170 "Usually alien objects do not have to created and dereferenced directly; instead declaring C function parameters and return values as having a " { $link pointer } " type such as " { $snippet "void*" } " takes care of the details."
171 { $subsections
172     "syntax-aliens"
173     "alien-expiry"
174 }
175 "When higher-level abstractions won't do:"
176 { $subsections "reading-writing-memory" }
177 { $see-also "c-data" "c-types-specs" } ;
178
179 ARTICLE: "reading-writing-memory" "Reading and writing memory directly"
180 "Numerical values can be read from memory addresses and converted to Factor objects using the various typed memory accessor words:"
181 { $subsections
182     alien-signed-1
183     alien-unsigned-1
184     alien-signed-2
185     alien-unsigned-2
186     alien-signed-4
187     alien-unsigned-4
188     alien-signed-cell
189     alien-unsigned-cell
190     alien-signed-8
191     alien-unsigned-8
192     alien-float
193     alien-double
194 }
195 "Factor numbers can also be converted to C values and stored to memory:"
196 { $subsections
197     set-alien-signed-1
198     set-alien-unsigned-1
199     set-alien-signed-2
200     set-alien-unsigned-2
201     set-alien-signed-4
202     set-alien-unsigned-4
203     set-alien-signed-cell
204     set-alien-unsigned-cell
205     set-alien-signed-8
206     set-alien-unsigned-8
207     set-alien-float
208     set-alien-double
209 } ;
210
211 ARTICLE: "alien-invoke" "Calling C from Factor"
212 "The easiest way to call into a C library is to define bindings using a pair of parsing words:"
213 { $subsections
214     POSTPONE: LIBRARY:
215     POSTPONE: FUNCTION:
216     POSTPONE: FUNCTION-ALIAS:
217 }
218 "The above parsing words create word definitions which call a lower-level word; you can use it directly, too:"
219 { $subsections alien-invoke }
220 "Sometimes it is necessary to invoke a C function pointer, rather than a named C function:"
221 { $subsections alien-indirect }
222 "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "." ;
223
224 ARTICLE: "alien-callback" "Calling Factor from C"
225 "Callbacks can be defined and passed to C code as function pointers; the C code can then invoke the callback and run Factor code:"
226 { $subsections
227     alien-callback
228     POSTPONE: CALLBACK:
229 }
230 "There are some caveats concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "."
231 { $see-also "byte-arrays-gc" } ;
232
233 ARTICLE: "alien-globals" "Accessing C global variables"
234 "The " { $vocab-link "alien.syntax" } " vocabulary defines two parsing words for accessing the value of a global variable, and get the address of a global variable, respectively."
235 { $subsections
236     POSTPONE: C-GLOBAL:
237     POSTPONE: &:
238 } ;
239
240 ARTICLE: "alien-assembly" "Calling arbitrary assembly code"
241 "It is possible to write a word whose body consists of arbitrary assembly code. The assembly receives parameters and returns values as per the platform's ABI; marshalling and unmarshalling Factor values is taken care of by the C library interface, as with " { $link alien-invoke } "."
242 $nl
243 "Assembler opcodes are defined in CPU-specific vocabularies:"
244 { $list
245     { $vocab-link "cpu.arm.assembler" }
246     { $vocab-link "cpu.ppc.assembler" }
247     { $vocab-link "cpu.x86.assembler" }
248 }
249 "The combinator for generating arbitrary assembly by calling a quotation at compile time:"
250 { $subsection alien-assembly } ;
251
252 ARTICLE: "dll.private" "DLL handles"
253 "DLL handles are a built-in class of objects which represent loaded native libraries. DLL handles are instances of the " { $link dll } " class, and have a literal syntax used for debugging printouts; see " { $link "syntax-aliens" } "."
254 $nl
255 "Usually one never has to deal with DLL handles directly; the C library interface creates them as required. However if direct access to these operating system facilities is required, the following primitives can be used:"
256 { $subsections
257     dlopen
258     dlsym
259     dlclose
260     dll-valid?
261 } ;
262
263 ARTICLE: "embedding-api" "Factor embedding API"
264 "The Factor embedding API is defined in " { $snippet "vm/master.h" } "."
265 $nl
266 "The " { $snippet "F_CHAR" } " type is an alias for the character type used for path names by the operating system; " { $snippet "char" } " on Unix and " { $snippet "wchar_t" } " on Windows."
267 $nl
268 "Including this header file into a C compilation unit will declare the following functions:"
269 { $table
270     { {
271         { $code "void init_factor_from_args("
272             "    F_CHAR *image, int argc, F_CHAR **argv, bool embedded"
273             ")" }
274         "Initializes Factor."
275         $nl
276         "If " { $snippet "image" } " is " { $snippet "NULL" } ", Factor will load an image file whose name is obtained by suffixing the executable name with " { $snippet ".image" } "."
277         $nl
278         "The " { $snippet "argc" } " and " { $snippet "argv" } " parameters are interpreted just like normal command line arguments when running Factor stand-alone; see " { $link "cli" } "."
279         $nl
280         "The " { $snippet "embedded" } " flag ensures that this function returns as soon as Factor has been initialized. Otherwise, Factor will start up normally."
281     } }
282     { {
283         { $code "char *factor_eval_string(char *string)" }
284         "Evaluates a piece of code in the embedded Factor instance by passing the string to " { $link eval>string } " and returning the result. The result must be explicitly freed by a call to " { $snippet "factor_eval_free" } "."
285     } }
286     { {
287         { $code "void factor_eval_free(char *result)" }
288         "Frees a string returned by " { $snippet "factor_eval_string()" } "."
289     } }
290     { {
291         { $code "void factor_yield(void)" }
292         "Gives all Factor threads a chance to run."
293     } }
294     { {
295         { $code "void factor_sleep(long us)" }
296         "Gives all Factor threads a chance to run for " { $snippet "us" } " microseconds."
297     } }
298 } ;
299
300 ARTICLE: "embedding-restrictions" "Embedding API restrictions"
301 "The Factor VM is not thread safe, and does not support multiple instances. There must only be one Factor instance per process, and this instance must be consistently accessed from the same thread for its entire lifetime. Once initialized, a Factor instance cannot be destroyed other than by exiting the process." ;
302
303 ARTICLE: "embedding-factor" "What embedding looks like from Factor"
304 "Factor code will run inside an embedded instance in the same way it would run in a stand-alone instance."
305 $nl
306 "One exception is that the global " { $link input-stream } " and " { $link output-stream } " streams are not bound by default, to avoid conflicting with any I/O the host process might perform. The " { $link init-stdio } " words must be called explicitly to initialize terminal streams."
307 $nl
308 "There is a word which can detect when Factor is embedded:"
309 { $subsections embedded? }
310 "No special support is provided for calling out from Factor into the owner process. The C library interface works fine for this task - see " { $link "alien" } "." ;
311
312 ARTICLE: "embedding" "Embedding Factor into C applications"
313 "The Factor " { $snippet "Makefile" } " builds the Factor VM both as an executable and a library. The library can be used by other applications. File names for the library on various operating systems:"
314 { $table
315     { "OS" "Library name" "Shared?" }
316     { "Windows XP/Vista" { $snippet "factor.dll" } "Yes" }
317     { "Mac OS X" { $snippet "libfactor.dylib" } "Yes" }
318     { "Other Unix" { $snippet "libfactor.a" } "No" }
319 }
320 "An image file must be supplied; a minimal image can be built, however the compiler must be included for the embedding API to work (see " { $link "bootstrap-cli-args" } ")."
321 { $subsections
322     "embedding-api"
323     "embedding-factor"
324     "embedding-restrictions"
325 } ;
326
327 ARTICLE: "alien" "C library interface"
328 "Factor can directly call C functions in native libraries. It is also possible to compile callbacks which run Factor code, and pass them to native libraries as function pointers."
329 $nl
330 "The C library interface is entirely self-contained; there is no C code which one must write in order to wrap a library."
331 $nl
332 "C library interface words are found in the " { $vocab-link "alien" } " vocabulary and its subvocabularies."
333 { $warning "C does not perform runtime type checking, automatic memory management or array bounds checks. Incorrect usage of C library functions can lead to crashes, data corruption, and security exploits." }
334 { $subsections
335     "loading-libs"
336     "alien-invoke"
337     "alien-callback"
338     "c-data"
339     "classes.struct"
340     "alien-globals"
341     "alien-assembly"
342     "dll.private"
343     "embedding"
344 } ;
345
346 ABOUT: "alien"