Sigh. Manual synching of 2 svn repos. sucks.
[cake.git] / tools / fd2inline / fd2inline.texi
blob3234f60f809bd96515d895765ae88e5afabc7f31
1 \input texinfo.tex  @c -*-texinfo-*-
2 @c %**start of header
3 @setfilename fd2inline.info
4 @settitle FD2InLine User's Guide
5 @set lastupdate March 22nd, 1998
6 @include version.texi
7 @c %**end of header
9 @dircategory GeekGadgets programming tools
10 @direntry
11 * FD2Inline: (fd2inline).        Creates @samp{GNU CC} @samp{inlines}.
12 @end direntry
13                               
14 @ifinfo
15 This is a user's guide to FD2Inline @value{version}, a parser
16 converting FD files to GCC ``inlines''.
18 Copyright @copyright{} 1996, 1997, 1998 Kamil Iskra
20 Permission is granted to make and distribute verbatim copies of this
21 manual provided the copyright notice and this permission notice are
22 preserved on all copies.
23 @end ifinfo
25 @titlepage
26 @sp 10
27 @center @titlefont{FD2InLine}
28 @subtitle User's Guide to FD2InLine parser
29 @subtitle @value{lastupdate}
30 @author Kamil Iskra
31 @page
32 @end titlepage
34 @ifinfo
35 @node Top, Introduction, (dir), (dir)
36 This is a user's guide to @samp{FD2Inline} @value{version}, a parser
37 that converts the AmigaOS and p.OS shared library @samp{FD} files to
38 format accepted by the @samp{GNU CC}.
40 See file @file{COPYING} for the GNU General Public License.
42 Last updated @value{lastupdate}.
43 @end ifinfo
45 @menu
46 * Introduction::  What is this program for?
47 * Installation::  How to install it?
48 * Usage::         How to use @samp{inlines} and @samp{FD2InLine}?
49 * Rebuilding::    How to recompile it?
50 * Internals::     How do @samp{inlines} work?
51 * History::       What has changed?
52 * Authors::       Who wrote it?
54 * Index::         Concept index.
55 @end menu
57 @node Introduction, Installation, Top, Top
58 @chapter Introduction
59 @cindex Introduction
60 @cindex What FD2InLine is
62 @samp{FD2InLine} is useful if you want to use @samp{GCC} for AmigaOS
63 and p.OS specific development and would like to call the functions in
64 the shared libraries efficiently.
66 The format of calls to the AmigaOS shared library functions differs
67 substantially from the default function call format of C compilers
68 (@pxref{Background}). Therefore, some tricks are necessary if you want
69 to use these functions.
71 The format of calls used in the current development versions of p.OS
72 is much more similar to the call format of C compilers
73 (@pxref{Internals}), nevertheless some tricks are still necessary.
75 @samp{FD2InLine} is a parser that converts @samp{fd} files and
76 @samp{clib} files to @samp{GCC inlines}.
78 @samp{fd} and @samp{clib} files contain information about functions in
79 shared libraries (@pxref{Background}).
81 @samp{FD2InLine} reads these two files and merges the information
82 contained therein, producing an output file suitable for use with the
83 @samp{GCC} compiler.
85 This output file contains so-called @dfn{inlines} --- one for each
86 function entry. Using them, @samp{GCC} can produce very efficient code
87 for making function calls to the AmigaOS and p.OS shared libraries.
89 @quotation
90 @emph{Note:} the term @samp{inlines} is misleading ---
91 @samp{FD2InLine} no longer uses the @code{__inline} feature of
92 @samp{GCC} (@pxref{New format}).
93 @end quotation
95 @node Installation, Usage, Introduction, Top
96 @chapter Installation
97 @cindex Installation
98 @cindex Where to put it
100 The following assumes you have the
101 @file{fd2inline-@value{version}-bin.lha} archive.
103 If you use a recent release of @samp{GCC}, you might not need to
104 install anything. Starting with @samp{GCC} 2.7.2, the new format
105 (@pxref{New format}) of @samp{inlines} should be available with the
106 compiler. However, the separate
107 @file{fd2inline-@value{version}-bin.lha} archive will always contain
108 the latest version of @samp{FD2InLine} and the @samp{inlines}, which
109 might not be the true for the @samp{GeekGadgets} or @samp{Aminet}
110 distributions. @samp{GeekGadgets} distribution might not contain some
111 of the supported 3rd party libraries' @samp{inlines}.
113 The installation is very easy, so there is no Installer script :-).
115 If you have an older version of the @samp{inlines} installed, please
116 remove it now, or you might encounter problems later. Typically, you
117 will have to remove the following subdirectories of the
118 @file{os-include} directory: @file{inline}, @file{pragmas} and
119 @file{proto}.
121 Next, please change your current directory to @file{GG:} (or
122 @file{ADE:} or @file{GNU:}, if you use older distributions) and simply
123 unpack the @file{fd2inline-@value{version}-bin.lha} archive. This
124 should install everything in the right place. More precisely, the
125 headers will go to the @file{include} directory, the libraries to
126 @file{lib}, @file{fd2inline} executable to @file{bin} directory and
127 the @samp{AmigaGuide} documentation to the @file{guide} directory.
129 @node Usage, Rebuilding, Installation, Top
130 @chapter Usage
131 @cindex Usage
133 This chapter describes two aspects of using @samp{FD2InLine}:
135 @menu
136 * Using inlines::    Making efficient function calls.
137 * Using fd2inline::  Creating @samp{inlines}.
138 @end menu
140 @node Using inlines, Using fd2inline, Usage, Usage
141 @section Using inlines
142 @cindex Using inlines
143 @cindex Making efficient calls
145 @menu
146 * Using inlines under AmigaOS::
147 * Using inlines under p.OS::
148 @end menu
150 @node Using inlines under AmigaOS, Using inlines under p.OS, Using inlines, Using inlines
151 @subsection Using inlines under AmigaOS
152 @cindex Using inlines under AmigaOS
154 Using @samp{inlines} under AmigaOS is very simple. If you want to use
155 a library called @samp{foo.library} (or a device called
156 @samp{bar.device}), simply include file @samp{<proto/foo.h>}
157 (@samp{<proto/bar.h>}) and that's it. For example:
159 @example
160 #include <proto/dos.h>
162 int main(void)
164    Delay(100); /* Wait for 2 seconds */
166 @end example
168 Please @emph{always} include @file{proto} files, @emph{not}
169 @file{inline} files --- @file{proto} files often fix some
170 incompatibilities between system headers and @samp{GCC}. Besides, this
171 technique makes your code more portable across various AmigaOS
172 @samp{C} compilers.
174 @cindex Preprocessor symbols under AmigaOS
175 There are a few preprocessor symbols which alter the behaviour of the
176 @samp{proto} and @samp{inline} files:
178 @table @code
179 @item __NOLIBBASE__
180 By default, the @samp{proto} files make external declarations of the
181 library base pointers. You can disable this behaviour by defining
182 @code{__NOLIBBASE__} before including a @samp{proto} file.
184 @cindex __CONSTLIBBASEDECL__
185 @item __CONSTLIBBASEDECL__
186 The external declarations described above declare plain pointer
187 variables. The disadvantage of this is that the library base variable
188 has to be reloaded every time some function is called. If you define
189 @code{__CONSTLIBBASEDECL__} to @code{const}, less reloading will be
190 necessary, and better code will be produced. However, declaring a
191 variable as @code{const} makes altering it impossible, so some dirty
192 hacks are necessary (like defining the variable as plain in one file
193 and altering it only there. However, this will @emph{not} work with
194 base relative code).
196 @cindex <library>_BASE_NAME
197 @item <library>_BASE_NAME
198 Function definitions in the @samp{inline} files refer to the library
199 base variable through the @code{<library>_BASE_NAME} symbol (e.g.,
200 @code{AMIGAGUIDE_BASE_NAME} for @samp{amigaguide.library}). At the top
201 of the @samp{inline} file, this symbol is redefined to the appropriate
202 library base variable name (e.g., @code{AmigaGuideBase}),
203 @emph{unless} it has been already defined. This way, you can make the
204 @samp{inlines} use a field of a structure as a library base, for
205 example.
207 @item NO_INLINE_STDARG
208 This symbol prevents the definition of inline macros for varargs
209 functions (@pxref{Old format}).
211 @cindex _USEOLDEXEC_
212 @item _USEOLDEXEC_
213 This symbol is used only in @file{proto/exec.h}. Unlike @samp{SAS/C},
214 @file{proto/exec.h} uses the @code{SysBase} variable as the
215 @samp{Exec} library base by default. This is usually faster than
216 direct dereferencing of @code{0x00000004} (@pxref{Background}), since
217 it does not require reading from @samp{CHIP} memory (things might be
218 even worse if you use @samp{Enforcer} or @samp{CyberGuard}, which
219 protect the low memory region). However, in some low-level cases (like
220 startup code) you might prefer dereferencing @code{0x00000004}. To do
221 this, define @code{_USEOLDEXEC_} before including @file{proto/exec.h}.
223 @cindex __CLIB_TYPES__
224 @item __CLIB_TYPES__
225 By default, the @samp{stub} files just include the file
226 @file{inline/stubs.h>}, which defines some basic types and forward
227 declares a bunch of structures.  This works for most libraries, but it
228 is not enough some third party libraries and some libraries distributed
229 with AmigaOS 3.5 and later. If you define @code{__CLIB_TYPES__}, some
230 definitions from the clib file will be activated.
231 @end table
233 @node Using inlines under p.OS, , Using inlines under AmigaOS, Using inlines
234 @subsection Using inlines under p.OS
235 @cindex Using inlines under p.OS
237 Using @samp{inlines} under p.OS is just as simple as under AmigaOS.
238 The equivalent of the source presented in the previous section
239 (@pxref{Using inlines under AmigaOS}) would look like this:
241 @example
242 #include <pclib/pDos_protos.h>
244 int main(void)
246    pOS_DosDelay(100); /* Wait for 2 seconds */
248 @end example
250 One significant difference is that one should @emph{always} include
251 @file{pclib} files, not @file{pProto} files --- just opposite to
252 AmigaOS.
254 @cindex Preprocessor symbols under p.OS
255 There are two preprocessor symbols which alter the behaviour of the
256 @samp{pInline} files:
258 @table @code
259 @cindex __<library>_BASE_NAME
260 @item __<library>_BASE_NAME
261 Function definitions in the @samp{pInline} files refer to the library
262 base variable through the @code{__<library>_BASE_NAME} symbol (e.g.,
263 @code{__INTUI_BASE_NAME} for @samp{pintui.library}). At the top of the
264 @samp{pInline} file, this symbol is redefined to the appropriate
265 library base variable name (e.g., @code{gb_IntuiBase}), @emph{unless}
266 it has been already defined. This way, you can make the @samp{inlines}
267 use a field of a structure as a library base, for example.
269 @cindex __<library>_LIB_NAME
270 @item __<library>_LIB_NAME
271 This symbol is defined in the same way as the previous one, and it
272 serves similar purpose. @xref{p.OS format}, for more information.
273 @end table
275 @node Using fd2inline, , Using inlines, Usage
276 @section Using fd2inline
277 @cindex Using FD2Inline
278 @cindex Creating inlines
280 You invoke @samp{FD2InLine} by writing:
282 @example
283 @file{fd2inline} [options] @var{fd-file} @var{clib-file} [[-o] @var{output-file}]
284 @end example
286 The command line arguments have the following meaning:
288 @table @var
289 @item fd-file
290 The name of the input @samp{fd} file.
292 @item clib-file
293 The name of the input @samp{clib} file.
295 @item output-file
296 The name of the output @samp{inline} or @samp{proto}file. If it is not
297 specified (or if @samp{-} is specified), standard output will be used
298 instead. The file name can be preceded by a @samp{-o}, for
299 compatibility with most @samp{UN*X} software.
300 @end table
302 The following options can be specified (anywhere on the command line):
304 @table @samp
305 @item --new
306 Produce new format @samp{inlines}.
308 @item --old
309 Produce old format @samp{inlines}.
311 @item --stubs
312 Produce library stubs.
314 @item --proto
315 Produce @samp{proto} file. If this option is specified, providing
316 @samp{clib} file is not necessary. @samp{fd2inline} will only read
317 @samp{fd} file and will generate a @samp{proto} file, to be put in
318 @file{include/proto} directory.
320 @item --pos
321 Produce p.OS @samp{inlines}. This flag is now obsolete. Use the
322 @samp{--target} option instead.
324 @item --target=TARGET
325 Produce @samp{inlines} for the given target. Valid targets are:
326 @samp{m68k-amigaos}, @samp{m68k-pos}, @samp{ppc-powerup}
327 and @samp{ppc-morphos}.
329 @item --version
330 Print version information and exit.
331 @end table
333 @xref{Internals}, for more information.
335 Example:
337 @example
338 fd2inline GG:os-lib/fd/exec_lib.fd GG:os-include/clib/exec_protos.h -o GG:include/inline/exec.h
339 @end example
341 This will build file @file{exec.h} containing new format
342 @samp{inlines} of @samp{exec.library} in directory
343 @file{GG:include/inline}.
345 If you want to add support for @samp{GCC} to an AmigaOS library, there
346 are a few things you should remember about.
348 Sometimes, @samp{FD2InLine} might not know how to handle a function
349 found in a @samp{clib} file, if this function doesn't have a
350 corresponding entry in the @samp{fd} file. This is most often a case
351 of @samp{varargs} functions (@pxref{Background}), if they use
352 nonstandard naming convention. @samp{FD2InLine} will warn you if it
353 finds such a function. There is an array of such exceptions in
354 @samp{FD2InLine} source code. You should add the name of this function
355 there and send a patch to @samp{FD2InLine} maintainer
356 (@pxref{Authors}), for inclussion in the next release of
357 @samp{FD2InLine}.
359 @samp{FD2InLine} assumes that the type of the base variable is
360 @code{struct Library *}. If it is something different in your case,
361 you should extend an array of exceptions in @samp{FD2InLine} source
362 code (see above).
364 @samp{FD2InLine} handles @code{void} functions in a special way. It
365 recognizes them by the return value --- it has to be @code{void} (the
366 case is not significant). If a @samp{clib} file uses a different
367 convention, it has to be modified before running @samp{FD2InLine}.
369 In addition to creating @samp{inlines} you must also create a
370 @samp{proto} file.
372 The easiest way to do it is to call @samp{FD2InLine} with
373 @samp{--proto} option. Most often, the generated file will be ready to
374 use. Unfortunately, some libraries (like, for example,
375 @samp{dos.library}) have broken header files and @samp{GCC} generates
376 warning messages if you try to use them. To avoid these warnings, you
377 have to include various headers in the @samp{proto} file before
378 including the @samp{clib} file.
380 You might also want to create a @samp{pragmas} file, which might be
381 necessary for badly written @samp{SAS/C} sources. @file{pragmas} are
382 generated automatically during the building of @samp{FD2InLine} by an
383 @samp{AWK} script, so you might either have a look at the
384 @file{fd2inline-@value{version}-src.lha} archive, or simply create
385 @file{pragmas} file by hand.
387 Creating a linker library with stubs might also be useful, in case
388 somebody doesn't want to, or can't, use inline headers.
390 @file{fd2inline-@value{version}-src.lha} contains necessary support
391 for this. For example, to generate a library @file{libexec.a} with
392 @samp{exec.library} stubs, you should type:
394 @example
395 make alllib INCBASE=exec LIBBASE=exec
396 @end example
398 This will create three @file{libexec.a} libraries in @file{lib}
399 subdirectory: plain, base relative and 32-bit base relative one. Of
400 course, this particular example doesn't make much sense since
401 @file{libamiga.a} already contains these stubs.
403 @code{INCBASE} and @code{LIBBASE} specify the base names of the
404 (input) @samp{proto} and @samp{fd} files and the (output) library.
405 This will often be the same, but not always. For example, in the case
406 of @samp{MUI}, @code{INCBASE} has to be set to @code{muimaster}, but
407 @code{LIBBASE} should be set to @code{mui}.
409 @node Rebuilding, Internals, Usage, Top
410 @chapter Rebuilding
411 @cindex Rebuilding
413 First, you have to get the @file{fd2inline-@value{version}-src.lha}
414 archive.
416 Unarchive it. You might either build @samp{FD2InLine} in source
417 directory or in a separate, build directory. The latter is
418 recommended. Type:
420 @example
421 lha -mraxe x fd2inline-@value{version}-src.lha
422 mkdir fd2inline-bin
423 cd fd2inline-bin
424 sh ../fd2inline-@value{version}/configure --prefix=/gg
425 make
426 @end example
428 This should build the @samp{FD2InLine} executable, headers, libraries
429 and so on.
431 Please note that the @samp{fd} files should be available in one of the
432 standard locations. For the AmigaOS @samp{fd} files, this is one of:
433 @file{GG:os-lib/fd/amigaos}, @file{GG:os-lib/fd}, @file{FD:}. For p.OS
434 @samp{fd} files, this is one of @file{GG:os-lib/fd/pos},
435 @file{P:/pOS_FD}. The exact location is determined by the
436 @samp{configure} script. If it doesn't find the @samp{fd} files, it
437 will disable the @file{Makefile} rules that build the @samp{inlines}.
439 You can then type:
441 @example
442 make install
443 @end example
445 This will install @samp{fd2inline}, the @samp{inlines} and the
446 documentation in the appropriate subdirectories of @file{GG:}.
448 The @file{fd2inline-@value{version}-src.lha} archive contains four
449 patches in unified diff format, in directory @file{patches}. They fix
450 bugs in AmigaOS 3.1 headers and @samp{fd} files. Without applying
451 @file{amigaguide_lib.fd.diff} to @samp{amigaguide.library} @samp{fd}
452 file, the produced @samp{inlines} will be broken. Applying
453 @file{timer.h.diff} to @file{devices/timer.h} will prevent collision
454 with IXEmul's @file{sys/time.h}. Two other patches rename an argument
455 name from @code{true} to @code{tf}, since @code{true} is a reserved
456 word in @samp{C++}. Use @code{patch} to apply these patches, for
457 example:
459 @example
460 cd GG:os-lib/fd
461 patch amigaguide_lib.fd <amigaguide_lib.fd.diff
462 @end example
464 @samp{GeekGadgets} distribution contains more complete set of patches.
466 @cindex Source code
467 A few words about the source code:
469 I know, it's not state-of-the-art @samp{C} programming example.
470 However, believe me, it was in @emph{much} worse condition when I took
471 it over. In its current state it is at least readable (if you use tab
472 size 3, as I do :-). I think that rewriting it in @samp{C++} would
473 clean it up considerably (it's already written in @samp{OO} fashion,
474 so this should be quite easy). Using @code{flex} and @code{bison} to
475 create the parser would also be a nice thing, I guess. However, I
476 don't think it's worth the effort. But, if somebody wants to do it:
477 feel free, this is @samp{GNU} software, so everybody can modify it.
479 @node Internals, History, Rebuilding, Top
480 @chapter Internals
481 @cindex Internals
483 This chapter describes the implementation details of @samp{inlines}.
485 @menu
486 * Background::    Function calls in AmigaOS shared libraries.
487 * Old format::    Inlines that use @code{__inline}.
488 * New format::    Inlines that use the preprocessor.
489 * p.OS format::   Inlines used for p.OS.
490 * Stubs format::  Not really inlines, but...
491 @end menu
493 @node Background, Old format, Internals, Internals
494 @section Background
495 @cindex Background
496 @cindex Function calls format in AmigaOS shared libraries
498 This section describes the calling conventions used in the AmigaOS
499 shared libraries.
501 @cindex Libraries
502 User-callable functions in the AmigaOS are organized in
503 @dfn{libraries}.
505 @cindex Library base
506 @cindex Jump table
507 From our point of view, the most important part of a library is the
508 @dfn{library base}. It always resides in RAM and contains library
509 variables and a @dfn{jump table}. The location of the library base
510 varies. You can obtain the library base location of the main system
511 library --- @code{exec.library} --- by dereferencing
512 @code{0x00000004}. Locations of other library bases can be obtained
513 using the @code{OpenLibrary} function of @samp{exec.library}.
515 Without providing unnecessary details, every function in a library has
516 a fixed location in the library's jump table. To call a function, one
517 has to jump to this location.
519 @cindex Function arguments
520 Most functions require some arguments. In @samp{C}, these are usually
521 passed on the CPU stack. However, for some obscure reason, AmigaOS
522 system designers decided that arguments to shared libraries should be
523 passed in CPU registers.
525 @cindex FD files
526 All the information required to make library function calls is
527 provided in @dfn{fd} files. Every shared library should have such a
528 file. It provides the name a library base variable should have, the
529 offset in the jump table where each library function resides, and
530 information about which arguments should be passed in which registers.
532 @cindex CLIB files
533 In order to check if arguments passed to a function have the correct
534 type, the @samp{C} compiler requires function prototypes. These are
535 provided in @dfn{clib} files --- every library should have such a
536 file.
538 @cindex Varargs functions
539 Starting with the AmigaOS release 2.0, certain functions have been
540 provided which accept a variable number of arguments (so-called
541 @dfn{varargs functions}). Actually, these are only @samp{C} language
542 stubs. Internally, all optional arguments have to be put into an array
543 of @samp{long ints} and the address of this array must be passed to a
544 fixed args library function.
546 @cindex Linker libraries
547 To implement calls to shared library functions, compiler vendors have
548 to either use some compiler-dependent tricks to make these calls
549 directly (so-called @dfn{in line}), or provide linker libraries with
550 function stubs, usually written in assembler. In the latter case, a
551 function call from the user's code is compiled as usual --- arguments
552 are passed on the stack. Then, in the linking stage, a library stub
553 gets linked in. When this stub is called during program execution, it
554 moves the arguments from the stack to the appropriate registers and
555 jumps to the library jump table. Needless to say, this is slower than
556 making a call in line.
558 @node Old format, New format, Background, Internals
559 @section Old format
560 @cindex Old inlines format
562 @example
563 extern __inline APTR
564 OpenAmigaGuideA(BASE_PAR_DECL struct NewAmigaGuide *nag, struct TagItem *attrs)
566    BASE_EXT_DECL
567    register APTR res __asm("d0");
568    register struct Library *a6 __asm("a6") = BASE_NAME;
569    register struct NewAmigaGuide *a0 __asm("a0") = nag;
570    register struct TagItem *a1 __asm("a1") = attrs;
571    __asm volatile ("jsr a6@@(-0x36:W)"
572    : "=r" (res)
573    : "r" (a6), "r" (a0), "r" (a1)
574    : "d0", "d1", "a0", "a1", "cc", "memory");
575    return res;
577 @end example
579 In this implementation, the AmigaOS shared library function stubs are
580 external functions. They are defined as @code{__inline}, making
581 @samp{GCC} insert them at every place of call. The mysterious
582 @code{BASE_PAR_DECL} and @code{BASE_EXT_DECL} defines are hacks
583 necessary for local library base support (which is quite hard to
584 achieve, so it will not be described here). The biggest disadvantage
585 of these @samp{inlines} is that compilation becomes very slow,
586 requiring huge amounts of memory. Besides, inlining only works with
587 optimization enabled.
589 @example
590 #ifndef NO_INLINE_STDARG
591 #define OpenAmigaGuide(a0, tags...) \
592    (@{ULONG _tags[] = @{ tags @}; OpenAmigaGuideA((a0), (struct TagItem *)_tags);@})
593 #endif /* !NO_INLINE_STDARG */
594 @end example
596 @cindex Varargs problems
597 The source above shows how @samp{varargs} functions are implemented.
598 Handling them cannot be made using @code{__inline} functions, since
599 @code{__inline} functions require a fixed number of arguments.
600 Therefore, the unique features of the @samp{GCC} preprocessor (such as
601 @samp{varargs macros}) have to be used, instead. This has some
602 drawbacks, unfortunately. Since these are actually preprocessor macros
603 and not function calls, you cannot make tricky things involving the
604 preprocessor inside them. For example:
606 @example
607 #include <proto/amigaguide.h>
609 #define OPENAG_BEG OpenAmigaGuide(
610 #define OPENAG_END , TAG_DONE)
612 void f(void)
614    OPENAG_BEG "a_file.guide" OPENAG_END;
615    OpenAmigaGuide(
616 #ifdef ABC
617       "abc.guide",
618 #else
619       "def.guide",
620 #endif
621       TAG_DONE);
623 @end example
625 Neither of the above @code{OpenAmigaGuide()} calls is handled
626 correctly.
628 In the case of the first call, you get an error:
630 @example
631 unterminated macro call
632 @end example
634 By the time the preprocessor attempts to expand the
635 @code{OpenAmigaGuide} macro, @code{OPENAG_END} is not yet expanded, so
636 the preprocessor cannot find the closing bracket. This code might look
637 contrived, but @samp{MUI}, for example, defines such macros to make
638 code look more pretty.
640 In the case of the second call, you'll see:
642 @example
643 warning: preprocessing directive not recognized within macro arg
644 @end example
646 A workaround would be to either surround entire function calls with
647 conditions, or to conditionally define a preprocessor symbol
648 @code{GUIDE} somewhere above and simply put @code{GUIDE} as a function
649 argument:
651 @example
652 #ifdef ABC
653 #define GUIDE "abc.guide"
654 #else
655 #define GUIDE "def.guide"
656 #endif
658 void f(void)
660 #ifdef ABC
661    OpenAmigaGuide("abc.guide", TAG_DONE);
662 #else
663    OpenAmigaGuide("def.guide", TAG_DONE);
664 #endif
665    OpenAmigaGuide(GUIDE, TAG_DONE);
667 @end example
669 Another problem is that when you pass a pointer as an argument, you
670 get a warning:
672 @example
673 warning: initialization makes integer from pointer without a cast
674 @end example
676 This is because all optional arguments are put as initializers to an
677 array of @code{ULONG}. And, if you attempt to initialize an
678 @code{ULONG} with a pointer without a cast, you get a warning. You can
679 avoid it by explicit casting of all pointer arguments to @code{ULONG}.
681 Because of these drawbacks, @samp{varargs inlines} can be disabled by
682 defining @code{NO_INLINE_STDARG} before including a @samp{proto} file.
683 In such a case, you will need a library with function stubs.
685 @node New format, p.OS format, Old format, Internals
686 @section New format
687 @cindex New inlines format
689 @example
690 #define OpenAmigaGuideA(nag, attrs) \
691    LP2(0x36, APTR, OpenAmigaGuideA, struct NewAmigaGuide *, nag, a0, struct TagItem *, attrs, a1, \
692    , AMIGAGUIDE_BASE_NAME)
693 @end example
695 As you can see, this implementation is much more compact. The
696 @code{LP2} macro (and others) are defined in @file{inline/macros.h},
697 which is included at the beginning of every @samp{inline} file.
699 @example
700 #define LP2(offs, rt, name, t1, v1, r1, t2, v2, r2, bt, bn) \
701 (@{                                                     \
702    t1 _##name##_v1 = (v1);                              \
703    t2 _##name##_v2 = (v2);                              \
704    @{                                                   \
705       register rt _##name##_re __asm("d0");             \
706       register struct Library *const _##name##_bn __asm("a6") = (struct Library*)(bn);\
707       register t1 _n1 __asm(#r1) = _##name##_v1;        \
708       register t2 _n2 __asm(#r2) = _##name##_v2;        \
709       __asm volatile ("jsr a6@@(-"#offs":W)"            \
710       : "=r" (_##name##_re)                             \
711       : "r" (_##name##_bn), "r"(_n1), "r"(_n2)          \
712       : "d0", "d1", "a0", "a1", "cc", "memory");        \
713       _##name##_re;                                     \
714    @}                                                   \
716 @end example
718 If you compare this with the old @samp{inlines} (@pxref{Old format})
719 you will notice many similarities. Indeed, both implementations use
720 the same tricks. This means that there should be little, if any,
721 difference in code quality between old and new @samp{inlines}.
723 With the new @samp{inlines}, however, inlining is performed very
724 early, at the preprocessing stage. This makes compilation much faster,
725 less memory hungry, and independent of the optimization options used.
726 This also makes it very easy to use local library bases --- all that
727 is needed is to define a local variable with the same name as library
728 base.
730 Unfortunately, using the preprocessor instead of the compiler for
731 making function calls has its drawbacks, as described earlier
732 (@pxref{Old format}). There is not much you can do about it apart from
733 modifying your code.
735 Depending on the type of a function, @samp{FD2InLine} generates calls
736 to different @samp{LP} macros.
738 Macros are distinguished by one or more of the qualifiers described
739 below:
741 @table @samp
742 @item digit
743 As you may have already guessed, digit indicates the number of
744 arguments a function accepts. Therefore, it is mandatory.
746 @item NR
747 This indicates a ``no return'' (@code{void}) function.
749 @item A4, A5
750 These two are used when one of the arguments has to be in either the
751 @samp{a4} or @samp{a5} register. In certain situations, these
752 registers have special meaning and have to be handled more carefully.
754 @item UB
755 This indicates ``user base'' --- the library base pointer has to be
756 specified explicitly by the user. Currently, this is used for
757 @samp{cia.resource} only. Since there are two @samp{CIA} chips, the
758 programmer has to specify which one [s]he wants to use.
760 @item FP
761 This means that one of the arguments is of type ``pointer to a
762 function''. To overcome strange @samp{C} syntax rules in this case,
763 inside @samp{FP} macros a @code{typedef} to @code{__fpt} is performed.
764 The @samp{inline} file passes @code{__fpt} as the argument type to the
765 @samp{LP} macro. The actual type of the argument, in a form suitable
766 for a @code{typedef}, is passed as an additional, last argument.
767 @end table
769 As you can see, there could be more than a hundred different
770 variations of the @samp{LP} macros. @file{inline/macros.h} contains
771 only 35, which are used in the current OS version and supported 3rd
772 party libraries. More macros will be added in the future, if needed.
774 If you look carefully at the definition of @code{OpenAmigaGuideA} at
775 the beginning of this section, you might notice that the next to last
776 argument to the @samp{LP} macro is not used. New @samp{inlines} were
777 not implemented in one evening, and they went through many
778 modifications. This unused argument (which was once a type of library
779 base pointer) is provided for backwards compatibility. Actually, there
780 are more unnecessary arguments, like function and argument names, but
781 it was decided to leave them in peace.
783 @node p.OS format, Stubs format, New format, Internals
784 @section p.OS format
785 @cindex p.OS inlines format
786 @cindex Function calls format in p.OS shared libraries
788 p.OS uses a different calling convention than AmigaOS. The most
789 fundamental difference is that, despite the information contained in
790 the @samp{fd} files, the arguments are passed on the stack, not in the
791 CPU registers.
793 @example
794 #define pOS_AllocAudioMapA(channel, par2) \
795         __INLINE_FUN_2(__AUDIO_BASE_NAME, __AUDIO_LIB_NAME, 0x12c, __ARID__ struct pOS_AudioMap*, pOS_AllocAudioMapA, \
796         ULONG, channel, __INLINE_REG_D0, const struct pOS_TagItem*, par2, __INLINE_REG_A0)
797 @end example
799 As you can see, the p.OS @samp{inlines} format largely resembles the
800 new @samp{inlines} format (@pxref{New format}). The most important
801 difference is that all the preprocessor symbols are now prefixed with
802 two underscores @code{__}, in order to prevent user namespace
803 polution, as required by @samp{ANSI C}.
805 @example
806 #define __INLINE_FUN_2(__base, __lib, __offs, __type, __name, __type1, __name1, __reg1, __type2, __name2, __reg2) \
807 ((__type (*)(void*, __type1, __type2)) \
808 *(ULONG*)(((char*)__lib)-__offs))(__base, __name1, __name2)
809 @end example
811 The actual implementation is very simple: no @samp{GNU CC}-specific
812 features are used, every @samp{ANSI C}-compatible compiler should be
813 able to use this inlines format.
815 As you might have already noticed, the register names are passed, but
816 not used. As mentioned earlier in this section, p.OS does not pass the
817 arguments in registers. However, since it is not clear if it will
818 continue to do so, it was decided to pass this redundant information.
820 Another difference when compared to AmigaOS is that p.OS supports
821 library jump tables separated from the library bases. As of this
822 writing, this is only the case of @samp{pexec.library}, where the base
823 pointer is identified by @code{gb_ExecBase}, and the jump table by
824 @code{gb_ExecLib}, both of which are obtained form the
825 @code{gb_DosBase}, passed to every application invoked. For this
826 reason, p.OS @samp{inlines} provide two preprocessor symbols for
827 library identification (@pxref{Using inlines under p.OS}).
829 @node Stubs format, , p.OS format, Internals
830 @section Stubs format
831 @cindex Stubs inlines format
833 Stubs format is very similar to old format (@pxref{Old format}). The
834 functions are not defined as @code{extern}, however.
836 The main difference is the format of the @samp{varargs} functions ---
837 they are plain functions, not preprocessor macros.
839 @example
840 APTR OpenAmigaGuide(struct NewAmigaGuide *nag, int tag, ...)
842    return OpenAmigaGuideA(nag, (struct TagItem *)&tag);
844 @end example
846 This format is not suitable for inlining, and it is not provided for
847 this purpose. It is provided for the building of linker libraries with
848 stubs (@pxref{Using fd2inline}).
850 @node History, Authors, Internals, Top
851 @chapter History
852 @cindex History
853 @cindex What has changed
855 Version 1.37,  April 22:nd, 2003,  Martin Blom
856 @itemize @bullet
857 @item
858 Targets are now specified in such a way that canonical system
859 names (from config.sub) can be used, as well as the short names.
860 @item
861 Added some AROS support (old and stubs do not yet work).
862 @item
863 @samp{--gatestubs} now works for all targets.
864 @item
865 Added @samp{--gateproto}.
866 @item
867 Added @samp{--mode}, @samp{--gateprefix} and @samp{--libprefix}.
868 @end itemize
870 Version 1.36,  February 24:th, 2002,  Martin Blom
871 @itemize @bullet
872 @item
873 Added @samp{--gatestubs} for AmigaOS and MorphOS.
874 @end itemize
876 Version 1.35,  February 2nd, 2002,  Martin Blom
877 @itemize @bullet
878 @item
879 Modified @file{share/ix86be-amithlon/macros.h} to use _CallLib68k().
880 @item
881 When creating stubs or old-style inlines, the macros from
882 @file{inline/macros.h} are now used.
883 @end itemize
885 Version 1.34,  January 6th, 2002,  Martin Blom
886 @itemize @bullet
887 @item
888 Modified @file{share/ix86be-amithlon/macros.h} to use a variable length
889 array for the register structure. This reduces the stack usage
890 dramatically, since variable length arrays are deallocated when it goes
891 out of scope, not when the function returns. I also added a7 to the
892 register structure (it's not used when calling OS functions, though).
893 @end itemize
895 Version 1.33,  December 14th, 2001,  Martin Blom
896 @itemize @bullet
897 @item
898 The @samp{--stubs} option was broken (at least) for ix86be-amithlon and
899 ppc-morphos. In addition, it's now possible to define the preprocessor
900 macro @var{__CLIB_TYPES__} and get all includes and typedefs from the
901 clib file included.
902 @item
903 In order to avoid problems where a parameter name is the same as a typedef,
904 all parameters are now prefixed with three underscores.
905 @end itemize
907 Version 1.32,  December 1st, 2001,  Martin Blom
908 @itemize @bullet
909 @item
910 Inlines and @file{macros.h} for @samp{ix86be-amithlon} now include the
911 unused @var{bt} argument. It made it easier to port libnix that way
912 @enddots{}
913 @end itemize
915 Version 1.31,  October 14th, 2001,  Martin Blom
916 @itemize @bullet
917 @item
918 Added added the @samp{ix86be-amithlon} target.
919 @item
920 Added the varproto code from version 2.00.
921 @item
922 Added more compability options: --powerup and --morphos.
923 @end itemize
925 Version 1.30,  October 11th, 2001,  Martin Blom
926 @itemize @bullet
927 @item
928 Backported some of Ralphs and Emmanuels updates from
929 version 2.00 (the PowerUp and MorphOS support; @samp{--pragma} and
930 @samp{--gatestubs} are not included).
931 @item
932 Added @samp{--quiet}.
933 @item
934 Fixed tag calls declared @code{CONST struct TagItem *} instead of
935 just @code{const struct TagItem *}.
936 @end itemize
938 Version 1.21,  March 22nd, 1998,  Kamil Iskra
939 @itemize @bullet
940 @item
941 Fixed a bug in handling of prototypes where the argument name was the
942 same as the type name (Kamil Iskra, reported by Martin Hauner).
943 @end itemize
945 Version 1.2,  October 26th, 1997,  Kamil Iskra
946 @itemize @bullet
947 @item
948 Added @code{LP10} macro to @file{inline/macros.h}, needed by
949 CyberGraphics (Kriton Kyrimis).
950 @item
951 New version of @file{strsup.h} (Gunther Nikl).
952 @item
953 Integrated support for p.OS (Kamil Iskra).
954 @item
955 Replaced "r" constraints with "rf" in @file{inline/macros.h} to
956 support passing arguments in floating point registers. NOTE: full
957 support in @samp{FD2InLine} is not yet available (Kamil Iskra).
958 @item
959 Modified source structure so that rebuilding @samp{FD2InLine} does not
960 require having it already installed (Kamil Iskra).
961 @item
962 Added consistency checks to prevent @samp{FD2InLine} from crashing
963 when certain parsing errors occur (Kamil Iskra).
964 @end itemize
966 Version 1.11,  January 31st, 1997,  Kamil Iskra
967 @itemize @bullet
968 @item
969 Minor fixes in @file{Makefile.in} for @samp{ADE} tree (Fred Fish).
970 @item
971 Fixed handling of prototypes in which the argument name was the same
972 as the type name (Kamil Iskra, reported by Martin Recktenwald).
973 @item
974 Added support for building 32-bit base relative linker libraries
975 (Kamil Iskra).
976 @item
977 Added a paragraph about @code{void} functions (Kamil Iskra, suggested
978 by Martin Recktenwald).
979 @end itemize
981 Version 1.1,  October 24th, 1996,  Kamil Iskra
982 @itemize @bullet
983 @item
984 Removed a lot of language mistakes from the documentation (Kriton
985 Kyrimis).
986 @item
987 Inlines of @samp{dospath.library}, @samp{screennotify.library},
988 @samp{ums.library} and @samp{wbstart.library} integrated (Martin
989 Steigerwald).
990 @item
991 Inlines of @samp{muimaster.library} integrated (Kamil Iskra).
992 @item
993 Floating point registers @samp{fp0} and @samp{fp1} are now marked as
994 clobbered (Kamil Iskra, reported by Kriton Kyrimis).
995 @item
996 Improved handling of @samp{clib} files, particularly recognition of
997 function prototypes and varargs functions (Kamil Iskra).
998 @item
999 Added support for @samp{--proto} and @samp{--version} options. Minor
1000 changes in output file generators, most notably making @samp{proto}
1001 files work with other compilers than @samp{GCC}, too. Finalized
1002 support for building linker libraries (Kamil Iskra, change in
1003 @samp{proto} files suggested by Joop van de Wege).
1004 @end itemize
1006 Version 1.0,  July 14th, 1996,  Kamil Iskra
1007 @itemize @bullet
1008 @item
1009 First officially available version.
1010 @end itemize
1012 @node Authors, Index, History, Top
1013 @chapter Authors
1014 @cindex Authors
1015 @cindex Other parsers
1016 @cindex Reporting bugs
1017 @cindex Address
1018 @cindex Latest version
1020 The first parser for @samp{GCC inlines} was written in @samp{Perl} by
1021 Markus Wild.
1023 It had several limitations, which were apparently hard to fix in
1024 @samp{Perl}. This is why Wolfgang Baron decided to write a new parser
1025 in @samp{C}.
1027 For some reason, however, he never finished it. In early 1995. Rainer
1028 F. Trunz took over its development and ``improved, updated, simply
1029 made it workable'' (quotation from the change log). It still contained
1030 quite a few bugs, though.
1032 In more-or-less the same time, I started a discussion on the
1033 @samp{amiga-gcc-port} mailing list about improving the quality of
1034 @samp{inlines}. The most important idea came from Matthias Fleischer,
1035 who introduced the new format of @samp{inlines} (@pxref{New format}).
1036 Since I started the discussion, I volunteered to make improvements to
1037 the @samp{inlines} parser. Having no idea about programming in
1038 @samp{Perl}, I decided to modify the parser written in @samp{C}. I
1039 fixed all the bugs known to me, added some new features, and wrote
1040 this terribly long documentation :-).
1042 Ralph Schmidt and Emmanuel Lesueur added PowerUp and MorphOS support
1043 (version 2.00).
1045 Martin Blom backported some of their work to version 1.xx and added support
1046 for ix86be-amithlon and AROS.
1048 Not all of the files distributed in the @samp{FD2InLine} archives were
1049 created by me or @samp{FD2InLine}. Most of the files in
1050 @file{include-src/proto} and @file{include-src/inline} (@file{alib.h},
1051 @file{strsub.h} and @file{stubs.h}) were written by Gunther Nikl (with
1052 some modifications by Joerg Hoehle and me).
1054 If you have any comments concerning this work, please write to:
1056 gg-gcc@@ninemoons.com
1058 This is a list to which most of the @samp{GeekGadgets} developers and
1059 activists subscribe, so you are practically guaranteed to get a reply.
1061 However, if, for some reason, you want to contact me personally, you
1062 can do so in one of the following ways:
1064 @itemize @bullet
1065 @item
1066 E-mail (preferred :-):
1068 iskra@@student.uci.agh.edu.pl
1070 Should be valid until October 1999 (at least I hope so :-).
1072 @item
1073 Snail-mail (expect to wait long for a reply :-):
1075 @format
1076 Kamil Iskra
1077 Luzycka 51/258
1078 30-658 Krakow
1079 Poland
1080 @end format
1081 @end itemize
1083 Latest version of this package should always be available on my WWW
1084 page:
1086 @format
1087 http://student.uci.agh.edu.pl/~iskra
1088 @end format
1090 @node Index, , Authors, Top
1091 @chapter Index
1092 @printindex cp
1094 @contents
1095 @bye