2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
5 Print the library magic and init code in the file modname_start.c.
6 This code is partly based on code in CLib37x.lha from Andreas R. Kleinert
9 #include "oopsupport.h"
10 #include "muisupport.h"
11 #include "dtsupport.h"
12 #include "boopsisupport.h"
14 static void writedecl(FILE *, struct config
*);
15 static void writedeclsets(FILE *, struct config
*);
16 static void writeresident(FILE *, struct config
*);
17 static void writeinitlib(FILE *, struct config
*);
18 static void writehandler(FILE *, struct config
*);
19 static void writeopenlib(FILE *, struct config
*);
20 static void writecloselib(FILE *, struct config
*);
21 static void writeexpungelib(FILE *, struct config
*);
22 static void writeextfunclib(FILE *, struct config
*);
23 static void writefunctable(FILE *, struct config
*);
24 static void writesets(FILE *, struct config
*);
26 void writestart(struct config
*cfg
)
29 char line
[256], *banner
;
32 snprintf(line
, 255, "%s/%s_start.c", cfg
->gendir
, cfg
->modulename
);
33 out
= fopen(line
, "w");
41 banner
= getBanner(cfg
);
42 fprintf(out
, "%s", banner
);
46 if (!(cfg
->options
& OPTION_NORESIDENT
))
48 writeresident(out
, cfg
);
49 writedeclsets(out
, cfg
);
50 writeinitlib(out
, cfg
);
51 if (cfg
->modtype
!= RESOURCE
&& cfg
->modtype
!= HANDLER
)
53 writeopenlib(out
, cfg
);
54 writecloselib(out
, cfg
);
55 writeexpungelib(out
, cfg
);
56 writeextfunclib(out
, cfg
);
57 if (cfg
->modtype
== MCC
|| cfg
->modtype
== MUI
|| cfg
->modtype
== MCP
)
58 writemccquery(out
, cfg
);
59 else if (cfg
->modtype
== DATATYPE
)
60 writeobtainengine(out
, cfg
);
65 if (cfg
->modtype
!= HANDLER
)
66 writefunctable(out
, cfg
);
68 for (cl
= cfg
->classlist
; cl
!= NULL
; cl
= cl
->next
)
70 switch (cl
->classtype
)
75 /* Second argument to next call: the class is not the main class if it is not
76 * the first class or the modtype is not a MUI class
78 writemccinit(out
, cl
!= cfg
->classlist
|| cfg
->modtype
!= cl
->classtype
, cl
);
84 writeclassinit(out
, cl
);
87 writeoopinit(out
, cl
);
90 fprintf(stdout
, "Internal error: unsupported classtype in writestart\n");
99 static void writedecl(FILE *out
, struct config
*cfg
)
101 struct stringlist
*linelistit
;
102 int boopsiinc
=0, muiinc
=0, oopinc
=0;
103 struct functionhead
*funclistit
;
104 struct functionarg
*arglistit
;
105 struct classinfo
*classlistit
;
108 if (cfg
->modtype
== DEVICE
)
111 "#include <exec/io.h>\n"
112 "#include <exec/errors.h>\n"
116 "#include <exec/types.h>\n"
117 "#include <exec/libraries.h>\n"
118 "#include <exec/resident.h>\n"
119 "#include <aros/libcall.h>\n"
120 "#include <aros/asmcall.h>\n"
121 "#include <aros/symbolsets.h>\n"
122 "#include <dos/dos.h>\n"
124 "#include \"%s_libdefs.h\"\n"
130 "#include <proto/exec.h>\n"
131 "#include <proto/alib.h>\n"
133 "struct ExecBase *SysBase = NULL;\n"
138 if (cfg
->options
& OPTION_BASEREL
)
139 fprintf(out
, "#include <aros/relbase.h>\n");
141 /* Write out declaration section provided in the config file */
142 for (linelistit
= cfg
->cdeflines
; linelistit
!= NULL
; linelistit
= linelistit
->next
)
144 fprintf(out
, "%s\n", linelistit
->s
);
147 /* Is there a variable for storing the segList ? */
148 if (!(cfg
->options
& OPTION_NOEXPUNGE
) && cfg
->modtype
!=RESOURCE
&& cfg
->modtype
!= HANDLER
)
151 "#ifndef GM_SEGLIST_FIELD\n"
152 "static BPTR __attribute__((unused)) GM_UNIQUENAME(seglist);\n"
153 "#define GM_SEGLIST_FIELD(lh) (GM_UNIQUENAME(seglist))\n"
157 if (cfg
->options
& OPTION_DUPBASE
)
159 "#ifndef GM_ROOTBASE_FIELD\n"
160 "static LIBBASETYPEPTR GM_UNIQUENAME(rootbase);\n"
161 "#define GM_ROOTBASE_FIELD(lh) (GM_UNIQUENAME(rootbase))\n"
164 if (cfg
->options
& OPTION_DUPPERID
)
166 "#ifdef __GM_OWNGETID\n"
167 "#include <dos/dosextens.h>\n"
168 "IPTR __GM_Id2(void)\n"
170 " struct Process *process = (struct Process *)FindTask(NULL);\n"
172 " if (process->pr_Task.tc_Node.ln_Type == NT_PROCESS)\n"
173 " return (IPTR)process->pr_ReturnAddr;\n"
175 " return (IPTR)NULL;\n"
180 "struct __GM_ID2Node\n"
182 " struct MinNode node;\n"
184 " struct Library *lh;\n"
185 " ULONG dupopencount;\n"
189 "struct __GM_AVLNode {\n"
190 " struct AVLNode node;\n"
192 "#ifndef GM_GETID2\n"
193 " struct Library *lh;\n"
194 " ULONG dupopencount;\n"
196 " struct MinList id2nodes;\n"
199 "struct __GM_BaseAVL {\n"
200 " LIBBASETYPE base;\n"
201 " struct __GM_AVLNode *avlnode, _avlnode;\n"
203 " struct __GM_ID2Node *id2node, _id2node;\n"
206 "static AROS_UFH2(LONG, __GM_CompKey,\n"
207 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode, A0),\n"
208 " AROS_UFHA(AVLKey, _key, A1)\n"
211 " AROS_USERFUNC_INIT\n"
213 " IPTR id = (IPTR)_key;\n"
214 " if (gm_avlnode->id == id)\n"
215 " return (LONG) 0;\n"
216 " else if (gm_avlnode->id < id)\n"
217 " return (LONG)-1;\n"
221 " AROS_USERFUNC_EXIT\n"
223 "static AROS_UFH2(LONG, __GM_CompNode,\n"
224 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode1, A0),\n"
225 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode2, A1)\n"
228 " AROS_USERFUNC_INIT\n"
230 " return AROS_UFC2(LONG, __GM_CompKey,\n"
231 " AROS_UFPA(const struct __GM_AVLNode *, gm_avlnode1, A0),\n"
232 " AROS_UFPA(AVLKey, (AVLKey)gm_avlnode2->id, A1)\n"
235 " AROS_USERFUNC_EXIT\n"
237 "#define LIBBASESIZE sizeof(struct __GM_BaseAVL)\n"
238 "struct AVLNode *__GM_AVLRoot = NULL;\n"
240 "#ifdef __GM_OWNPARENTBASEID2\n"
241 "LIBBASETYPEPTR GM_UNIQUENAME(__GetParentLibbase)(LIBBASETYPEPTR lh)\n"
243 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\n"
245 " id2node = (struct __GM_ID2Node *)GetSucc(id2node);\n"
247 " if (id2node != NULL)\n"
248 " return (LIBBASETYPEPTR)id2node->lh;\n"
250 " return (LIBBASETYPEPTR)NULL;\n"
256 fprintf(out
, "#define LIBBASESIZE sizeof(LIBBASETYPE)\n");
258 for (classlistit
= cfg
->classlist
; classlistit
!= NULL
; classlistit
= classlistit
->next
)
260 /* For the main class basename is the same as the module basename */
261 if (strcmp(classlistit
->basename
, cfg
->basename
) == 0)
263 if (classlistit
->classptr_var
== NULL
)
266 "#if !defined(GM_CLASSPTR_FIELD) && !defined(%s_CLASSPTR_FIELD)\n"
267 "static APTR GM_UNIQUENAME(%sClass);\n"
268 "#define GM_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
269 "#define %s_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
270 "#define %s_STORE_CLASSPTR 1\n"
271 "#elif defined(GM_CLASSPTR_FIELD) && !defined(%s_CLASSPTR_FIELD)\n"
272 "#define %s_CLASSPTR_FIELD(lh) (GM_CLASSPTR_FIELD(lh))\n"
273 "#elif !defined(GM_CLASSPTR_FIELD) && defined(%s_CLASSPTR_FIELD)\n"
274 "#define GM_CLASSPTR_FIELD(lh) (%s_CLASSPTR_FIELD(lh))\n"
276 classlistit
->basename
,
277 classlistit
->basename
,
278 classlistit
->basename
,
279 classlistit
->basename
, classlistit
->basename
,
280 classlistit
->basename
,
281 classlistit
->basename
,
282 classlistit
->basename
,
283 classlistit
->basename
,
284 classlistit
->basename
290 "#define GM_CLASSPTR_FIELD(lh) (%s)\n"
291 "#define %s_CLASSPTR_FIELD(lh) (%s)\n"
292 "#define %s_STORE_CLASSPTR 1\n",
293 classlistit
->classptr_var
,
294 classlistit
->basename
, classlistit
->classptr_var
,
295 classlistit
->basename
301 if (classlistit
->classptr_var
== NULL
)
304 "#if !defined(%s_CLASSPTR_FIELD)\n"
305 "static APTR GM_UNIQUENAME(%sClass);\n"
306 "#define %s_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
307 "#define %s_STORE_CLASSPTR 1\n"
309 classlistit
->basename
,
310 classlistit
->basename
,
311 classlistit
->basename
, classlistit
->basename
,
312 classlistit
->basename
318 "#define %s_CLASSPTR_FIELD(lh) (%s)\n"
319 "#define %s_STORE_CLASSPTR 1\n",
320 classlistit
->basename
, classlistit
->classptr_var
,
321 classlistit
->basename
327 /* Write out the defines for the functions of the function table */
328 writefuncdefs(out
, cfg
, cfg
->funclist
);
331 /* Write out the includes needed for the classes */
332 if (cfg
->classlist
!= NULL
)
333 writeboopsiincludes(out
);
335 for (classlistit
= cfg
->classlist
; classlistit
!= NULL
; classlistit
= classlistit
->next
)
337 switch (classlistit
->classtype
)
344 writemuiincludes(out
);
347 /* Fall through: also write boopsi includes */
354 writeboopsiincludes(out
);
361 writeoopincludes(out
);
366 fprintf(stderr
, "Internal error: unhandled classtype in writedecl\n");
373 static void writedeclsets(FILE *out
, struct config
*cfg
)
376 "THIS_PROGRAM_HANDLES_SYMBOLSETS\n"
379 "DECLARESET(CTORS)\n"
380 "DECLARESET(DTORS)\n"
381 "DECLARESET(INITLIB)\n"
382 "DECLARESET(EXPUNGELIB)\n"
384 if (!(cfg
->options
& OPTION_NOOPENCLOSE
))
386 "DECLARESET(OPENLIB)\n"
387 "DECLARESET(CLOSELIB)\n"
389 if (cfg
->modtype
== DEVICE
)
391 "DECLARESET(OPENDEV)\n"
392 "DECLARESET(CLOSEDEV)\n"
394 if (cfg
->classlist
!= NULL
)
396 "DECLARESET(CLASSESINIT)\n"
397 "DECLARESET(CLASSESEXPUNGE)\n"
398 "#define ADD2INITCLASSES(symbol, pri) ADD2SET(symbol, classesinit, pri)\n"
399 "#define ADD2EXPUNGECLASSES(symbol, pri) ADD2SET(symbol, classesexpunge, pri)\n"
405 static void writeresident(FILE *out
, struct config
*cfg
)
407 char *rt_skip
= cfg
->addromtag
;
410 fprintf(out
, "extern const struct Resident %s;\n", rt_skip
);
413 rt_skip
= "GM_UNIQUENAME(End)";
414 fprintf(out
, "extern const int %s;\n", rt_skip
);
417 "extern const APTR GM_UNIQUENAME(FuncTable)[];\n"
419 if (cfg
->options
& OPTION_RESAUTOINIT
)
420 fprintf(out
, "static const struct InitTable GM_UNIQUENAME(InitTable);\n");
423 "extern const char GM_UNIQUENAME(LibName)[];\n"
424 "extern const char GM_UNIQUENAME(LibID)[];\n"
425 "extern const char GM_UNIQUENAME(Copyright)[];\n"
429 if (cfg
->options
& OPTION_RESAUTOINIT
)
431 if (!(cfg
->options
& OPTION_DUPPERID
))
434 "#define __freebase(lh)\\\n"
436 " UWORD negsize, possize;\\\n"
437 " UBYTE *negptr = (UBYTE *)lh;\\\n"
438 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
439 " negptr -= negsize;\\\n"
440 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
441 " FreeMem (negptr, negsize+possize);\\\n"
449 "#ifndef GM_GETID2\n"
450 "#define __freebase(lh)\\\n"
452 " UWORD negsize, possize;\\\n"
453 " UBYTE *negptr = (UBYTE *)lh;\\\n"
454 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\\\n"
455 " AVL_RemNodeByAddress(&__GM_AVLRoot, (struct AVLNode *)avlnode);\\\n"
456 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
457 " negptr -= negsize;\\\n"
458 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
459 " FreeMem (negptr, negsize+possize);\\\n"
462 "#define __freebase(lh)\\\n"
464 " UWORD negsize, possize;\\\n"
465 " UBYTE *negptr = (UBYTE *)lh;\\\n"
466 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\\\n"
467 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\\\n"
469 " /* avlnode == NULL for original libbase provided to LibInit */\\\n"
472 " Remove((struct Node *)id2node);\\\n"
473 " remnode = GetHead((struct List *)&avlnode->id2nodes) == NULL;\\\n"
475 " AVL_RemNodeByAddress(&__GM_AVLRoot, (struct AVLNode *)avlnode);\\\n"
476 " else if (avlnode == &((struct __GM_BaseAVL *)lh)->_avlnode)\\\n"
477 " /* Do not free as avlnode is still in the AVL Tree\\\n"
478 " This is causing a memory leak because sublibrary is not closed */\\\n"
481 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
482 " negptr -= negsize;\\\n"
483 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
484 " FreeMem (negptr, negsize+possize);\\\n"
492 "AROS_UFP3 (LIBBASETYPEPTR, GM_UNIQUENAME(InitLib),\n"
493 " AROS_UFPA(LIBBASETYPEPTR, lh, D0),\n"
494 " AROS_UFPA(BPTR, segList, A0),\n"
495 " AROS_UFPA(struct ExecBase *, sysBase, A6)\n"
498 if (cfg
->modtype
!= RESOURCE
&& cfg
->modtype
!= HANDLER
)
501 "AROS_LP1(BPTR, GM_UNIQUENAME(ExpungeLib),\n"
502 " AROS_LPA(LIBBASETYPEPTR, extralh, D0),\n"
503 " LIBBASETYPEPTR, lh, 3, %s\n"
510 "struct Resident const GM_UNIQUENAME(ROMTag) =\n"
513 " (struct Resident *)&GM_UNIQUENAME(ROMTag),\n"
516 " VERSION_NUMBER,\n",
519 switch (cfg
->modtype
)
529 fprintf(out
, " NT_LIBRARY,\n");
532 fprintf(out
, " NT_DEVICE,\n");
536 fprintf(out
, " NT_RESOURCE,\n");
539 fprintf(stderr
, "Internal error: unsupported modtype for NT_...\n");
545 " (CONST_STRPTR)&GM_UNIQUENAME(LibName)[0],\n"
546 " (CONST_STRPTR)&GM_UNIQUENAME(LibID)[6],\n"
548 if (cfg
->options
& OPTION_RESAUTOINIT
)
551 " (APTR)&GM_UNIQUENAME(InitTable)\n"
554 "static struct InitTable\n"
557 " const APTR *FuncTable;\n"
558 " struct DataTable *DataTable;\n"
559 " APTR InitLibTable;\n"
561 "const GM_UNIQUENAME(InitTable) =\n"
564 " &GM_UNIQUENAME(FuncTable)[0],\n"
566 " (APTR)GM_UNIQUENAME(InitLib)\n"
571 fprintf(out
, " (APTR)GM_UNIQUENAME(InitLib)\n};\n");
575 "const char GM_UNIQUENAME(LibName)[] = MOD_NAME_STRING;\n"
576 "const char GM_UNIQUENAME(LibID)[] = VERSION_STRING;\n"
577 "const char GM_UNIQUENAME(Copyright)[] = COPYRIGHT_STRING;\n"
582 static void writehandler(FILE *out
, struct config
*cfg
)
585 struct handlerinfo
*hl
;
589 "#include <resources/filesysres.h>\n"
590 "#include <aros/system.h>\n"
591 "#include <proto/arossupport.h>\n"
592 "#include <proto/dos.h>\n"
596 for (hl
= cfg
->handlerlist
; hl
!= NULL
; hl
= hl
->next
) {
598 "extern void %s(void);\n",
604 "void GM_UNIQUENAME(InitHandler)(void)\n"
606 " struct FileSysResource *fsr;\n"
611 " BYTE autodetect;\n"
613 " ULONG stacksize;\n"
614 " void (*handler)(void);\n"
615 " } const __handler[] = { \n");
616 for (hl
= cfg
->handlerlist
; hl
!= NULL
; hl
= hl
->next
)
620 case HANDLER_RESIDENT
:
622 " { .id = 0, .name = AROS_CONST_BSTR(\"%s\"), .handler = %s }, \n",
623 hl
->name
, hl
->handler
);
625 case HANDLER_DOSTYPE
:
627 " { .id = 0x%08x, .name = AROS_CONST_BSTR(MOD_NAME_STRING), .handler = %s, .autodetect = %d, .priority = %d, .stacksize = %d*sizeof(IPTR) }, \n",
628 hl
->id
, hl
->handler
, hl
->autodetect
, hl
->priority
, hl
->stacksize
);
634 " BPTR seg[sizeof(__handler)/sizeof(__handler[0])] = { };\n"
636 " fsr = (struct FileSysResource *)OpenResource(\"FileSystem.resource\");\n"
637 " if (fsr == NULL)\n"
640 " for (i = 0; i < sizeof(__handler)/sizeof(__handler[0]); i++) {\n"
641 " struct FileSysEntry *fse;\n"
644 " /* Check to see if we can allocate the memory for the fse */\n"
645 " fse = AllocMem(sizeof(*fse), MEMF_CLEAR);\n"
649 " /* Did we already make a segment for this handler? */\n"
650 " for (j = 0; j < i; j++)\n"
651 " if (__handler[i].handler == __handler[j].handler)\n"
653 " if (seg[j] == (BPTR)0)\n"
654 " seg[j] = CreateSegList(__handler[j].handler);\n"
655 " if (seg[j] == BNULL) {\n"
656 " FreeMem(fse, sizeof(*fse));\n"
660 " /* DOS ID based handlers\n"
661 " * NOTE: fse_DosType == 0 is a special flag use in\n"
662 " * dos.library's init to add the handler to the\n"
663 " * resident segment list\n"
665 " fse->fse_Node.ln_Name = VERSION_STRING;\n"
666 " fse->fse_Node.ln_Pri = __handler[i].autodetect;\n"
667 " fse->fse_DosType = __handler[i].id;\n"
668 " fse->fse_Version = (MAJOR_VERSION << 16) | MINOR_VERSION;\n"
669 " fse->fse_PatchFlags = FSEF_SEGLIST | FSEF_GLOBALVEC | FSEF_HANDLER | FSEF_PRIORITY;\n"
670 " if (__handler[i].stacksize) {\n"
671 " fse->fse_PatchFlags |= FSEF_STACKSIZE;\n"
672 " fse->fse_StackSize = __handler[i].stacksize;\n"
674 " fse->fse_Handler = __handler[i].name;\n"
675 " fse->fse_Priority = __handler[i].priority;\n"
676 " fse->fse_SegList = seg[j];\n"
677 " fse->fse_GlobalVec = (BPTR)(SIPTR)-1;\n"
679 " /* Add to the list. I know forbid and permit are\n"
680 " * a little unnecessary for the pre-multitasking state\n"
681 " * we should be in at this point, but you never know\n"
682 " * who's going to blindly copy this code as an example.\n"
685 " Enqueue(&fsr->fsr_FileSysEntries, (struct Node *)fse);\n"
691 static void writeinitlib(FILE *out
, struct config
*cfg
)
693 if (cfg
->handlerlist
)
694 writehandler(out
, cfg
);
697 "AROS_UFH3 (LIBBASETYPEPTR, GM_UNIQUENAME(InitLib),\n"
698 " AROS_UFHA(LIBBASETYPEPTR, lh, D0),\n"
699 " AROS_UFHA(BPTR, segList, A0),\n"
700 " AROS_UFHA(struct ExecBase *, sysBase, A6)\n"
703 " AROS_USERFUNC_INIT\n"
709 if (cfg
->options
& OPTION_RESAUTOINIT
) {
712 " SysBase = sysBase;\n"
714 "#ifdef GM_SYSBASE_FIELD\n"
715 " GM_SYSBASE_FIELD(lh) = (APTR)sysBase;\n"
720 if (cfg
->modtype
!= HANDLER
)
722 if (!(cfg
->options
& OPTION_RESAUTOINIT
))
729 " vecsize = FUNCTIONS_COUNT * LIB_VECTSIZE;\n"
730 " if (vecsize > 0)\n"
731 " vecsize = ((vecsize-1)/sizeof(IPTR) + 1)*sizeof(IPTR);\n"
732 " mem = AllocMem(vecsize+sizeof(LIBBASETYPE), MEMF_PUBLIC|MEMF_CLEAR);\n"
733 " if (mem == NULL)\n"
735 " lh = (LIBBASETYPEPTR)(mem + vecsize);\n"
736 " n = (struct Node *)lh;\n"
737 " n->ln_Type = NT_RESOURCE;\n"
738 " n->ln_Pri = RESIDENTPRI;\n"
739 " n->ln_Name = (char *)GM_UNIQUENAME(LibName);\n"
740 " MakeFunctions(lh, (APTR)GM_UNIQUENAME(FuncTable), NULL);\n"
742 if ((cfg
->modtype
!= RESOURCE
) && (cfg
->options
& OPTION_SELFINIT
))
745 " ((struct Library*)lh)->lib_NegSize = vecsize;\n"
746 " ((struct Library*)lh)->lib_PosSize = sizeof(LIBBASETYPE);\n"
754 " ((struct Library *)lh)->lib_Revision = REVISION_NUMBER;\n"
759 if (cfg
->options
& OPTION_BASEREL
)
761 " void *oldbase = AROS_SET_LIBBASE(lh);\n");
763 if (!(cfg
->options
& OPTION_NOEXPUNGE
) && cfg
->modtype
!=RESOURCE
&& cfg
->modtype
!= HANDLER
)
764 fprintf(out
, " GM_SEGLIST_FIELD(lh) = segList;\n");
765 if (cfg
->options
& OPTION_DUPBASE
)
766 fprintf(out
, " GM_ROOTBASE_FIELD(lh) = (LIBBASETYPEPTR)lh;\n");
767 fprintf(out
, " if (");
768 if (!(cfg
->options
& OPTION_NOAUTOLIB
))
769 fprintf(out
, "set_open_libraries() && ");
770 if (cfg
->classlist
!= NULL
)
771 fprintf(out
, "set_call_libfuncs(SETNAME(CLASSESINIT), 1, 1, lh) && ");
773 "set_call_funcs(SETNAME(INIT), 1, 1) )\n"
775 " set_call_funcs(SETNAME(CTORS), -1, 0);\n"
779 if (cfg
->modtype
== HANDLER
)
784 " ok = set_call_libfuncs(SETNAME(INITLIB), 1, 1, lh);\n");
794 if (cfg
->modtype
!= HANDLER
)
796 " set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 0, lh);\n");
799 " set_call_funcs(SETNAME(DTORS), 1, 0);\n"
800 " set_call_funcs(SETNAME(EXIT), -1, 0);\n"
802 if (cfg
->classlist
!= NULL
)
803 fprintf(out
, " set_call_libfuncs(SETNAME(CLASSESEXPUNGE), -1, 0, lh);\n");
804 if (!(cfg
->options
& OPTION_NOAUTOLIB
))
805 fprintf(out
, " set_close_libraries();\n");
806 if (cfg
->options
& OPTION_BASEREL
)
807 fprintf(out
, " (void)AROS_SET_LIBBASE(oldbase);\n");
809 if (cfg
->modtype
!= HANDLER
)
811 if (cfg
->options
& OPTION_RESAUTOINIT
)
822 " FreeMem(mem, vecsize+LIBBASESIZE);\n"
833 if (!(cfg
->options
& OPTION_RESAUTOINIT
) && !(cfg
->options
& OPTION_SELFINIT
))
835 switch (cfg
->modtype
)
838 fprintf(out
, " AddResource(lh);\n");
842 fprintf(out
, " AddDevice(lh);\n");
845 /* Bare handlers don't require adding at all */
849 /* Everything else is library */
850 fprintf(out
, " AddLibrary(lh);\n");
855 if (cfg
->handlerlist
)
856 fprintf(out
, " GM_UNIQUENAME(InitHandler)();\n");
858 if (cfg
->options
& OPTION_BASEREL
)
860 " (void)AROS_SET_LIBBASE(oldbase);\n");
865 " AROS_USERFUNC_EXIT\n"
871 static void writeopenlib(FILE *out
, struct config
*cfg
)
873 switch (cfg
->modtype
)
876 fprintf(stderr
, "Internal error: writeopenlib called for a resource\n");
879 fprintf(stderr
, "Internal error: writeopenlib called for a handler\n");
882 if (cfg
->options
& OPTION_NOOPENCLOSE
)
884 "AROS_LD3 (void, GM_UNIQUENAME(OpenLib),\n"
885 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
886 " AROS_LHA(ULONG, unitnum, D0),\n"
887 " AROS_LHA(ULONG, flags, D1),\n"
888 " LIBBASETYPEPTR, lh, 1, %s\n"
895 "AROS_LH3 (void, GM_UNIQUENAME(OpenLib),\n"
896 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
897 " AROS_LHA(IPTR, unitnum, D0),\n"
898 " AROS_LHA(ULONG, flags, D1),\n"
899 " LIBBASETYPEPTR, lh, 1, %s\n"
905 " AROS_LIBFUNC_INIT\n"
907 " if ( set_call_libfuncs(SETNAME(OPENLIB), 1, 1, lh)\n"
908 " && set_call_devfuncs(SETNAME(OPENDEV), 1, 1, lh, ioreq, unitnum, flags)\n"
911 " ((struct Library *)lh)->lib_OpenCnt++;\n"
912 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
914 " ioreq->io_Message.mn_Node.ln_Type = NT_REPLYMSG;\n"
918 " if (ioreq->io_Error >= 0)\n"
919 " ioreq->io_Error = IOERR_OPENFAIL;\n"
924 " AROS_LIBFUNC_EXIT\n"
931 if (cfg
->options
& OPTION_NOOPENCLOSE
)
934 "AROS_LD1 (LIBBASETYPEPTR, GM_UNIQUENAME(OpenLib),\n"
935 " AROS_LHA (ULONG, version, D0),\n"
936 " LIBBASETYPEPTR, lh, 1, %s\n"
943 "AROS_LH1 (LIBBASETYPEPTR, GM_UNIQUENAME(OpenLib),\n"
944 " AROS_LHA (ULONG, version, D0),\n"
945 " LIBBASETYPEPTR, lh, 1, %s\n"
948 " AROS_LIBFUNC_INIT\n"
952 if (!(cfg
->options
& OPTION_DUPBASE
))
955 " if ( set_call_libfuncs(SETNAME(OPENLIB), 1, 1, lh) )\n"
957 " ((struct Library *)lh)->lib_OpenCnt++;\n"
958 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
965 " AROS_LIBFUNC_EXIT\n"
970 else /* OPTION_DUPBASE */
973 " struct Library *newlib = NULL;\n"
974 " UWORD possize = ((struct Library *)lh)->lib_PosSize;\n"
976 if (cfg
->options
& OPTION_DUPPERID
)
978 " struct __GM_AVLNode *avlnode = NULL;\n"
979 " IPTR id = GM_GETID;\n"
981 " struct __GM_ID2Node *id2node = NULL, *id2node_it;\n"
982 " IPTR id2 = GM_GETID2;\n"
985 " avlnode = (struct __GM_AVLNode *)AVL_FindNode(__GM_AVLRoot, (AVLKey)id, (AVLKEYCOMP)__GM_CompKey);\n"
986 "#ifndef GM_GETID2\n"
987 " if (avlnode != NULL)\n"
989 " avlnode->dupopencount++;\n"
990 " newlib = avlnode->lh;\n"
993 " if (avlnode != NULL)\n"
995 " ForeachNode(&avlnode->id2nodes, id2node_it)\n"
997 " if (id2node_it->id2 == id2)\n"
998 " id2node = id2node_it;\n"
1002 " if (id2node != NULL)\n"
1004 " id2node->dupopencount++;\n"
1005 " newlib = id2node->lh;\n"
1012 " if (newlib == NULL)\n"
1014 " newlib = MakeLibrary(GM_UNIQUENAME(InitTable).FuncTable,\n"
1015 " GM_UNIQUENAME(InitTable).DataTable,\n"
1017 " GM_UNIQUENAME(InitTable).Size,\n"
1020 " if (newlib == NULL)\n"
1023 " CopyMem(lh, newlib, possize);\n"
1025 if (cfg
->options
& OPTION_DUPPERID
)
1027 "#ifndef GM_GETID2\n"
1029 " = ((struct __GM_BaseAVL *)newlib)->avlnode\n"
1030 " = &((struct __GM_BaseAVL *)newlib)->_avlnode;\n"
1031 " avlnode->id = id;\n"
1032 " avlnode->lh = newlib;\n"
1033 " avlnode->dupopencount = 1;\n"
1034 " AVL_AddNode((struct AVLNode **)&__GM_AVLRoot, (struct AVLNode *)avlnode, (AVLNODECOMP)__GM_CompNode);\n"
1036 " if(avlnode == NULL)\n"
1038 " /* avlnode does not exists yet, use the one in the allocated libbase */\n"
1040 " = ((struct __GM_BaseAVL *)newlib)->avlnode\n"
1041 " = &((struct __GM_BaseAVL *)newlib)->_avlnode;\n"
1042 " avlnode->id = id;\n"
1043 " NEWLIST(&avlnode->id2nodes);\n"
1044 " AVL_AddNode((struct AVLNode **)&__GM_AVLRoot, (struct AVLNode *)avlnode, (AVLNODECOMP)__GM_CompNode);\n"
1047 " /* avlnode already in AVL tree, let avlnode pointer in libbase point to it */\n"
1048 " ((struct __GM_BaseAVL *)newlib)->avlnode = avlnode;\n"
1051 " = ((struct __GM_BaseAVL *)newlib)->id2node\n"
1052 " = &((struct __GM_BaseAVL *)newlib)->_id2node;\n"
1053 " id2node->id2 = id2;\n"
1054 " id2node->lh = newlib;\n"
1055 " id2node->dupopencount = 1;\n"
1056 " AddHead((struct List *)&avlnode->id2nodes, (struct Node *)id2node);\n"
1060 if (cfg
->options
& OPTION_BASEREL
)
1063 " void *oldbase = AROS_SET_LIBBASE(newlib);");
1066 " if (!(set_open_rellibraries(newlib)\n"
1067 " && set_call_libfuncs(SETNAME(OPENLIB), 1, 1, newlib)\n"
1071 if (cfg
->options
& OPTION_BASEREL
)
1073 " (void)AROS_SET_LIBBASE(oldbase);\n");
1075 " __freebase(newlib);\n"
1078 if (cfg
->options
& OPTION_BASEREL
)
1080 " (void)AROS_SET_LIBBASE(oldbase);\n");
1083 " ((struct Library *)lh)->lib_OpenCnt++;\n"
1084 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
1087 " return (LIBBASETYPEPTR)newlib;\n"
1089 " AROS_LIBFUNC_EXIT\n"
1098 static void writecloselib(FILE *out
, struct config
*cfg
)
1100 if (cfg
->options
& OPTION_NOOPENCLOSE
)
1102 if (cfg
->modtype
!= DEVICE
)
1104 "AROS_LD0 (BPTR, GM_UNIQUENAME(CloseLib),\n"
1105 " LIBBASETYPEPTR, lh, 2, %s\n"
1111 "AROS_LD1(BPTR, GM_UNIQUENAME(CloseLib),\n"
1112 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
1113 " LIBBASETYPEPTR, lh, 2, %s\n"
1119 if (cfg
->modtype
!= DEVICE
)
1121 "AROS_LH0 (BPTR, GM_UNIQUENAME(CloseLib),\n"
1122 " LIBBASETYPEPTR, lh, 2, %s\n"
1128 "AROS_LH1(BPTR, GM_UNIQUENAME(CloseLib),\n"
1129 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
1130 " LIBBASETYPEPTR, lh, 2, %s\n"
1137 " AROS_LIBFUNC_INIT\n"
1140 if (cfg
->options
& OPTION_BASEREL
)
1142 " void *oldbase = AROS_SET_LIBBASE(lh);\n"
1145 if (cfg
->modtype
== DEVICE
)
1148 " if (!set_call_devfuncs(SETNAME(CLOSEDEV), -1, 1, lh, ioreq, 0, 0))\n"
1150 if (cfg
->options
& OPTION_BASEREL
)
1152 " (void)AROS_SET_LIBBASE(oldbase);\n");
1158 if (!(cfg
->options
& OPTION_DUPBASE
))
1161 " ((struct Library *)lh)->lib_OpenCnt--;\n"
1162 " set_call_libfuncs(SETNAME(CLOSELIB), -1, 0, lh);\n"
1168 " LIBBASETYPEPTR rootbase = GM_ROOTBASE_FIELD(lh);\n"
1170 if (cfg
->options
& OPTION_DUPPERID
)
1172 "#ifndef GM_GETID2\n"
1173 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\n"
1174 " avlnode->dupopencount--;\n"
1175 " if (avlnode->dupopencount != 0)\n"
1178 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\n"
1179 " id2node->dupopencount--;\n"
1180 " if (id2node->dupopencount != 0)\n"
1186 " set_call_libfuncs(SETNAME(CLOSELIB), -1, 0, lh);\n"
1187 " set_close_rellibraries(lh);\n"
1188 " __freebase(lh);\n"
1190 " ((struct Library *)lh)->lib_OpenCnt--;\n"
1194 if (!(cfg
->options
& OPTION_NOEXPUNGE
))
1199 " (((struct Library *)lh)->lib_OpenCnt == 0)\n"
1200 " && (((struct Library *)lh)->lib_Flags & LIBF_DELEXP)\n"
1203 if (cfg
->options
& OPTION_BASEREL
)
1205 " (void)AROS_SET_LIBBASE(oldbase);\n");
1207 " return AROS_LC1(BPTR, GM_UNIQUENAME(ExpungeLib),\n"
1208 " AROS_LCA(LIBBASETYPEPTR, lh, D0),\n"
1209 " LIBBASETYPEPTR, lh, 3, %s\n"
1215 if (cfg
->options
& OPTION_BASEREL
)
1218 " (void)AROS_SET_LIBBASE(oldbase);");
1223 " AROS_LIBFUNC_EXIT\n"
1230 static void writeexpungelib(FILE *out
, struct config
*cfg
)
1233 "AROS_LH1 (BPTR, GM_UNIQUENAME(ExpungeLib),\n"
1234 " AROS_LHA(LIBBASETYPEPTR, extralh, D0),\n"
1235 " LIBBASETYPEPTR, lh, 3, %s\n"
1241 " AROS_LIBFUNC_INIT\n"
1244 if (!(cfg
->options
& OPTION_NOEXPUNGE
))
1248 " if ( ((struct Library *)lh)->lib_OpenCnt == 0 )\n"
1250 " BPTR seglist = GM_SEGLIST_FIELD(lh);\n"
1252 if (cfg
->options
& OPTION_BASEREL
)
1254 " void *oldbase = AROS_SET_LIBBASE(lh);\n"
1257 " if(!set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 1, lh))\n"
1259 " ((struct Library *)lh)->lib_Flags |= LIBF_DELEXP;\n"
1263 " Remove((struct Node *)lh);\n"
1265 " set_call_funcs(SETNAME(DTORS), 1, 0);\n"
1266 " set_call_funcs(SETNAME(EXIT), -1, 0);\n"
1268 if (cfg
->classlist
!= NULL
)
1269 fprintf(out
, " set_call_libfuncs(SETNAME(CLASSESEXPUNGE), -1, 0, lh);\n");
1270 if (!(cfg
->options
& OPTION_NOAUTOLIB
))
1271 fprintf(out
, " set_close_libraries();\n");
1272 if (cfg
->options
& OPTION_BASEREL
)
1275 " (void)AROS_SET_LIBBASE(oldbase);");
1278 " __freebase(lh);\n"
1280 " return seglist;\n"
1283 " ((struct Library *)lh)->lib_Flags |= LIBF_DELEXP;\n"
1290 " AROS_LIBFUNC_EXIT\n"
1297 static void writeextfunclib(FILE *out
, struct config
*cfg
)
1300 "AROS_LH0 (LIBBASETYPEPTR, GM_UNIQUENAME(ExtFuncLib),\n"
1301 " LIBBASETYPEPTR, lh, 4, %s\n"
1304 " AROS_LIBFUNC_INIT\n"
1306 " AROS_LIBFUNC_EXIT\n"
1315 writefunctable(FILE *out
,
1319 struct functionhead
*funclistit
= cfg
->funclist
;
1320 struct functionarg
*arglistit
;
1324 int lastversion
= 0;
1326 /* lvo contains the number of functions already printed in the functable */
1329 if (!(cfg
->options
& OPTION_NORESIDENT
))
1333 "const APTR GM_UNIQUENAME(FuncTable)[]=\n"
1336 if (cfg
->modtype
!= RESOURCE
&& cfg
->modtype
!= HANDLER
)
1339 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(OpenLib),%s,1),\n"
1340 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(CloseLib),%s,2),\n"
1341 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(ExpungeLib),%s,3),\n"
1342 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(ExtFuncLib),%s,4),\n",
1343 cfg
->basename
, cfg
->basename
, cfg
->basename
, cfg
->basename
1347 if (cfg
->modtype
== MCC
|| cfg
->modtype
== MUI
|| cfg
->modtype
== MCP
)
1351 " &AROS_SLIB_ENTRY(MCC_Query,%s,%d),\n",
1355 else if (cfg
->modtype
== DATATYPE
)
1359 " &AROS_SLIB_ENTRY(ObtainEngine,%s,%d),\n",
1364 else /* NORESIDENT */
1366 if (cfg
->modtype
!= RESOURCE
&& cfg
->modtype
!= HANDLER
)
1369 struct functionhead
*funclistit2
;
1371 if (funclistit
->lvo
!= 1)
1373 fprintf(stderr
, "Module without a generated resident structure has to provide the Open function (LVO==1)\n");
1377 funclistit
= funclistit
->next
;
1379 if (funclistit
->lvo
!= 2)
1381 fprintf(stderr
, "Module without a generated resident structure has to provide the Close function (LVO==2)\n");
1385 funclistit
= funclistit
->next
;
1387 if (funclistit
->lvo
== 3)
1388 funclistit
= funclistit
->next
;
1392 if (funclistit
->lvo
== 4)
1393 funclistit
= funclistit
->next
;
1400 "AROS_UFH1S(int, %s_null,\n"
1401 " AROS_UFHA(struct Library *, libbase, A6)\n"
1404 " AROS_USERFUNC_INIT\n"
1406 " AROS_USERFUNC_EXIT\n"
1411 funclistit
= cfg
->funclist
;
1412 funclistit2
= funclistit
->next
;
1415 "const APTR GM_UNIQUENAME(FuncTable)[]=\n"
1417 " &AROS_SLIB_ENTRY(%s,%s,%d),\n"
1418 " &AROS_SLIB_ENTRY(%s,%s,%d),\n",
1419 funclistit
->internalname
, cfg
->basename
, lvo
+1,
1420 funclistit2
->internalname
, cfg
->basename
, lvo
+2
1423 funclistit
= funclistit2
->next
;
1425 if (funclistit
->lvo
== 3)
1427 fprintf(out
, " &AROS_SLIB_ENTRY(%s,%s,%d),\n",
1428 funclistit
->internalname
, cfg
->basename
, lvo
+1
1430 funclistit
= funclistit
->next
;
1433 fprintf(out
, " &%s_null,\n", cfg
->modulename
);
1436 if (funclistit
->lvo
== 4)
1438 fprintf(out
, " &AROS_SLIB_ENTRY(%s,%s,%d),\n",
1439 funclistit
->internalname
, cfg
->basename
, lvo
+1
1441 funclistit
= funclistit
->next
;
1444 fprintf(out
, " &%s_null,\n", cfg
->modulename
);
1451 "const APTR GM_UNIQUENAME(FuncTable)[]=\n"
1456 while (funclistit
!= NULL
)
1458 for (i
= lvo
+1; i
<funclistit
->lvo
; i
++)
1459 fprintf(out
, " NULL,\n");
1460 lvo
= funclistit
->lvo
;
1462 switch (funclistit
->libcall
)
1465 fprintf(out
, " &%s,\n", funclistit
->internalname
);
1470 if (funclistit
->version
!= lastversion
) {
1471 lastversion
= funclistit
->version
;
1472 fprintf(out
, " /* Version %d */\n", lastversion
);
1474 fprintf(out
, " &AROS_SLIB_ENTRY(%s,%s,%d),\n", funclistit
->internalname
, cfg
->basename
, lvo
);
1478 fprintf(stderr
, "Internal error: unhandled libcall type in writestart\n");
1483 funclistit
= funclistit
->next
;
1486 fprintf(out
, " (void *)-1\n};\n");
1490 static void writesets(FILE *out
, struct config
*cfg
)
1495 "DEFINESET(CTORS)\n"
1496 "DEFINESET(DTORS)\n"
1498 if (cfg
->modtype
!= HANDLER
)
1500 "DEFINESET(INITLIB)\n"
1501 "DEFINESET(EXPUNGELIB)\n"
1503 if (!(cfg
->options
& OPTION_NOOPENCLOSE
))
1505 "DEFINESET(OPENLIB)\n"
1506 "DEFINESET(CLOSELIB)\n"
1508 if (cfg
->modtype
== DEVICE
)
1510 "DEFINESET(OPENDEV)\n"
1511 "DEFINESET(CLOSEDEV)\n"
1513 if (cfg
->classlist
!= NULL
)
1515 "DEFINESET(CLASSESINIT)\n"
1516 "DEFINESET(CLASSESEXPUNGE)\n"