]> gitweb.factorcode.org Git - factor.git/blob - core/alien/alien-docs.factor
alien: Fix docs.
[factor.git] / core / alien / alien-docs.factor
1 USING: alien.accessors alien.c-types alien.libraries
2 alien.syntax arrays byte-arrays compiler cpu.x86
3 debugger definitions eval help.markup help.syntax io io.backend
4 kernel libc math parser quotations sequences system ;
5 IN: alien
6
7 HELP: cdecl
8 { $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." } ;
9
10 HELP: stdcall
11 { $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." } ;
12
13 HELP: fastcall
14 { $warning "In the current implementation this ABI only works for functions that take only integer and pointer arguments." }
15 { $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." } ;
16
17 HELP: thiscall
18 { $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." } ;
19
20 { cdecl stdcall fastcall thiscall } related-words
21
22 HELP: >c-ptr
23 { $values { "obj" object } { "c-ptr" c-ptr } }
24 { $contract "Outputs a pointer to the binary data of this object." } ;
25
26 HELP: byte-length
27 { $values { "obj" object } { "n" "a non-negative integer" } }
28 { $contract "Outputs the number of bytes of binary data that will be output by " { $link >c-ptr } "." } ;
29
30 HELP: element-size
31 { $values { "seq" sequence } { "n" "a non-negative integer" } }
32 { $contract "Outputs the number of bytes used for each element of the sequence." }
33 { $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." } ;
34
35 { >c-ptr element-size byte-length } related-words
36
37 HELP: alien
38 { $class-description "The class of alien pointers. See " { $link "syntax-aliens" } " for syntax and " { $link "c-data" } " for general information." } ;
39
40 HELP: dll
41 { $class-description "The class of native library handles. See " { $link "syntax-aliens" } " for syntax and " { $link "dll.private" } " for general information." } ;
42
43 HELP: dll-valid?
44 { $values { "dll" dll } { "?" "a boolean" } }
45 { $description "Returns true if the library exists and is loaded." } ;
46
47 HELP: expired?
48 { $values { "c-ptr" c-ptr } { "?" "a boolean" } }
49 { $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." } ;
50
51 HELP: <bad-alien>
52 { $values  { "alien" c-ptr } }
53 { $description "Constructs an invalid alien pointer that has expired." } ;
54
55 HELP: <displaced-alien>
56 { $values { "displacement" "an integer" } { "c-ptr" c-ptr } { "alien" "a new alien" } }
57 { $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" } "." }
58 { $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."
59 $nl
60 "Passing a zero absolute address does not construct a new alien object, but instead makes the word output " { $link f } "." } ;
61
62 { <alien> <displaced-alien> alien-address } related-words
63
64 HELP: alien-address
65 { $values { "c-ptr" c-ptr } { "addr" "a non-negative integer" } }
66 { $description "Outputs the address of an alien." }
67 { $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" } "." } ;
68
69 HELP: <alien>
70 { $values { "address" "a non-negative integer" } { "alien" "a new alien address" } }
71 { $description "Creates an alien object, wrapping a raw memory address." }
72 { $notes "Alien objects are invalidated between image saves and loads." } ;
73
74 HELP: c-ptr
75 { $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." } ;
76
77 HELP: alien-invoke-error
78 { $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:"
79     { $list
80         { "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." }
81         { "The return type or parameter list references an unknown C type." }
82         { "The symbol or library could not be found." }
83         { "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 } "." }
84     }
85 } ;
86
87 HELP: alien-invoke
88 { $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 } } }
89 { $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." }
90 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
91 { $errors "Throws an " { $link alien-invoke-error } " if the word calling " { $link alien-invoke } " was not compiled with the optimizing compiler." } ;
92
93 HELP: alien-indirect-error
94 { $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:"
95     { $list
96         { "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." }
97         { "One of the three inputs to " { $link alien-indirect } " is not a literal value." }
98     }
99 } ;
100
101 HELP: alien-indirect
102 { $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 } } }
103 { $description
104     "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."
105 }
106 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
107 { $errors "Throws an " { $link alien-indirect-error } " if the word calling " { $link alien-indirect } " is not compiled." } ;
108
109 HELP: alien-callback-error
110 { $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:"
111     { $list
112         { "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." }
113         { "One of the four inputs to " { $link alien-callback } " is not a literal value." }
114     }
115 } ;
116
117 HELP: alien-callback
118 { $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 } }
119 { $description
120     "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."
121     $nl
122     "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."
123     $nl
124     "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."
125 }
126 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
127 { $examples
128     "A simple example, showing a C function which returns the difference of two given integers:"
129     { $code
130         ": difference-callback ( -- alien )"
131         "    int { int int } cdecl [ - ] alien-callback ;"
132     }
133 }
134 { $errors "Throws an " { $link alien-callback-error } " if the word calling " { $link alien-callback } " is not compiled." } ;
135
136 HELP: alien-assembly-error
137 { $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:"
138     { $list
139         { "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." }
140         { "One of the four inputs to " { $link alien-assembly } " is not a literal value." }
141     }
142 } ;
143
144 HELP: alien-assembly
145 { $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 } } }
146 { $description
147     "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."
148     $nl
149     "The caller of this word must preserve the " { $link ds-reg } " and " { $link rs-reg } " registers. Note that this is not a " { $snippet "call" } " in the assembly sense, so there is no return address on the stack."
150     $nl
151     "It's important to mind the ABI. For instance, on x86.32, parameters are passed on the stack in " { $snippet "ESP" } ", while on x86.64 arguments are passed in " { $snippet "RDI" } ", " { $snippet "RSI" } ", " { $snippet "RDX" } ", and " { $snippet "RCX" } ". On Windows 64, integers and pointers are passed in " { $snippet "RCX" } ", " { $snippet "RDX" } ", " { $snippet "R8" } ", and " { $snippet "R9" } ", and then on the stack."
152     $nl
153     "There are Factor words for the input parameters, such as " { $snippet "param-reg-0" } " and " { $snippet "param-reg-1" } "."
154     $nl
155     "For output parameters, use " { $link return-reg } "."
156     $nl
157 }
158 { $notes "C type names are documented in " { $link "c-types-specs" } "." }
159 { $errors "Throws an " { $link alien-assembly-error } " if the word calling " { $link alien-assembly } " is not compiled." } ;
160
161 { alien-invoke alien-indirect alien-assembly alien-callback } related-words
162
163 ARTICLE: "alien-expiry" "Alien expiry"
164 "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."
165 $nl
166 "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 } "."
167 { $subsections expired? } ;
168
169 ARTICLE: "aliens" "Alien addresses"
170 "Instances of the " { $link alien } " class represent pointers to C data outside the Factor heap:"
171 { $subsections
172     <alien>
173     <displaced-alien>
174     alien-address
175 }
176 "Anywhere that a " { $link alien } " instance is accepted, the " { $link f } " singleton may be passed in to denote a null pointer."
177 $nl
178 "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."
179 { $subsections
180     "syntax-aliens"
181     "alien-expiry"
182 }
183 "When higher-level abstractions won't do:"
184 { $subsections "reading-writing-memory" }
185 { $see-also "c-data" "c-types-specs" } ;
186
187 ARTICLE: "reading-writing-memory" "Reading and writing memory directly"
188 "Numerical values can be read from memory addresses and converted to Factor objects using the various typed memory accessor words:"
189 { $subsections
190     alien-signed-1
191     alien-unsigned-1
192     alien-signed-2
193     alien-unsigned-2
194     alien-signed-4
195     alien-unsigned-4
196     alien-signed-cell
197     alien-unsigned-cell
198     alien-signed-8
199     alien-unsigned-8
200     alien-float
201     alien-double
202 }
203 "Factor numbers can also be converted to C values and stored to memory:"
204 { $subsections
205     set-alien-signed-1
206     set-alien-unsigned-1
207     set-alien-signed-2
208     set-alien-unsigned-2
209     set-alien-signed-4
210     set-alien-unsigned-4
211     set-alien-signed-cell
212     set-alien-unsigned-cell
213     set-alien-signed-8
214     set-alien-unsigned-8
215     set-alien-float
216     set-alien-double
217 } ;
218
219 ARTICLE: "alien-invoke" "Calling C from Factor"
220 "The easiest way to call into a C library is to define bindings using a pair of parsing words:"
221 { $subsections
222     POSTPONE: LIBRARY:
223     POSTPONE: FUNCTION:
224     POSTPONE: FUNCTION-ALIAS:
225 }
226 "The above parsing words create word definitions which call a lower-level word; you can use it directly, too:"
227 { $subsections alien-invoke }
228 "Sometimes it is necessary to invoke a C function pointer, rather than a named C function:"
229 { $subsections alien-indirect }
230 "There are some details concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "." ;
231
232 ARTICLE: "alien-callback" "Calling Factor from C"
233 "Callbacks can be defined and passed to C code as function pointers; the C code can then invoke the callback and run Factor code:"
234 { $subsections
235     alien-callback
236     POSTPONE: CALLBACK:
237 }
238 "There are some caveats concerning the conversion of Factor objects to C values, and vice versa. See " { $link "c-data" } "."
239 { $see-also "byte-arrays-gc" } ;
240
241 ARTICLE: "alien-globals" "Accessing C global variables"
242 "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."
243 { $subsections
244     POSTPONE: C-GLOBAL:
245     POSTPONE: &:
246 } ;
247
248 ARTICLE: "alien-assembly" "Calling arbitrary assembly code"
249 "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 } "."
250 $nl
251 "Assembler opcodes are defined in CPU-specific vocabularies:"
252 { $list
253     { $vocab-link "cpu.arm.assembler" }
254     { $vocab-link "cpu.ppc.assembler" }
255     { $vocab-link "cpu.x86.assembler" }
256 }
257 "The combinator for generating arbitrary assembly by calling a quotation at compile time:"
258 { $subsection alien-assembly } ;
259
260 ARTICLE: "dll.private" "DLL handles"
261 "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" } "."
262 $nl
263 "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:"
264 { $subsections
265     dlopen
266     dlsym
267     dlclose
268     dll-valid?
269 } ;
270
271 ARTICLE: "embedding-api" "Factor embedding API"
272 "The Factor embedding API is defined in " { $snippet "vm/master.h" } "."
273 $nl
274 "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."
275 $nl
276 "Including this header file into a C compilation unit will declare the following functions:"
277 { $table
278     { {
279         { $code "void init_factor_from_args("
280             "    F_CHAR *image, int argc, F_CHAR **argv, bool embedded"
281             ")" }
282         "Initializes Factor."
283         $nl
284         "If " { $snippet "image" } " is " { $snippet "NULL" } ", Factor will load an image file whose name is obtained by suffixing the executable name with " { $snippet ".image" } "."
285         $nl
286         "The " { $snippet "argc" } " and " { $snippet "argv" } " parameters are interpreted just like normal command line arguments when running Factor stand-alone; see " { $link "command-line" } "."
287         $nl
288         "The " { $snippet "embedded" } " flag ensures that this function returns as soon as Factor has been initialized. Otherwise, Factor will start up normally."
289     } }
290     { {
291         { $code "char *factor_eval_string(char *string)" }
292         "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" } "."
293     } }
294     { {
295         { $code "void factor_eval_free(char *result)" }
296         "Frees a string returned by " { $snippet "factor_eval_string()" } "."
297     } }
298     { {
299         { $code "void factor_yield(void)" }
300         "Gives all Factor threads a chance to run."
301     } }
302     { {
303         { $code "void factor_sleep(long us)" }
304         "Gives all Factor threads a chance to run for " { $snippet "us" } " microseconds."
305     } }
306 } ;
307
308 ARTICLE: "embedding-restrictions" "Embedding API restrictions"
309 "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." ;
310
311 ARTICLE: "embedding-factor" "What embedding looks like from Factor"
312 "Factor code will run inside an embedded instance in the same way it would run in a stand-alone instance."
313 $nl
314 "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."
315 $nl
316 "There is a word which can detect when Factor is embedded:"
317 { $subsections embedded? }
318 "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" } "." ;
319
320 ARTICLE: "embedding" "Embedding Factor into C applications"
321 "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:"
322 { $table
323     { "OS" "Library name" "Shared?" }
324     { "Windows XP/Vista" { $snippet "factor.dll" } "Yes" }
325     { "Mac OS X" { $snippet "libfactor.dylib" } "Yes" }
326     { "Other Unix" { $snippet "libfactor.a" } "No" }
327 }
328 "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" } ")."
329 { $subsections
330     "embedding-api"
331     "embedding-factor"
332     "embedding-restrictions"
333 } ;
334
335 ARTICLE: "alien" "C library interface"
336 "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."
337 $nl
338 "The C library interface is entirely self-contained; there is no C code which one must write in order to wrap a library."
339 $nl
340 "C library interface words are found in the " { $vocab-link "alien" } " vocabulary and its subvocabularies."
341 { $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." }
342 { $subsections
343     "loading-libs"
344     "alien-invoke"
345     "alien-callback"
346     "c-data"
347     "classes.struct"
348     "alien-globals"
349     "alien-assembly"
350     "dll.private"
351     "embedding"
352 } ;
353
354 ABOUT: "alien"