Fixed a few warnings.
[tangerine.git] / tools / genmodule / writestart.c
blobe757007ec67be47caf9e0775d0e0262373fcbf85
1 /*
2 Copyright © 1995-2009, The AROS Development Team. All rights reserved.
3 $Id$
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
7 */
8 #include "genmodule.h"
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 writeopenlib(FILE *, struct config *);
19 static void writecloselib(FILE *, struct config *);
20 static void writeexpungelib(FILE *, struct config *);
21 static void writeextfunclib(FILE *, struct config *);
22 static void writefunctable(FILE *, struct config *);
23 static void writesets(FILE *, struct config *);
25 void writestart(struct config *cfg)
27 FILE *out;
28 char line[256], *banner;
29 struct classinfo *cl;
31 snprintf(line, 255, "%s/%s_start.c", cfg->gendir, cfg->modulename);
32 out = fopen(line, "w");
34 if (out == NULL)
36 perror(line);
37 exit(20);
40 banner = getBanner(cfg);
41 fprintf(out, banner);
42 freeBanner(banner);
44 writedecl(out, cfg);
45 if (!(cfg->options & OPTION_NORESIDENT))
47 writeresident(out, cfg);
48 writedeclsets(out, cfg);
49 writeinitlib(out, cfg);
50 if (cfg->modtype != RESOURCE)
52 writeopenlib(out, cfg);
53 writecloselib(out, cfg);
54 writeexpungelib(out, cfg);
55 writeextfunclib(out, cfg);
56 if (cfg->modtype == MCC || cfg->modtype == MUI || cfg->modtype == MCP)
57 writemccquery(out, cfg);
58 else if (cfg->modtype == DATATYPE)
59 writeobtainengine(out, cfg);
61 writesets(out, cfg);
63 writefunctable(out, cfg);
65 for (cl = cfg->classlist; cl != NULL; cl = cl->next)
67 switch (cl->classtype)
69 case MCC:
70 case MUI:
71 case MCP:
72 /* Second argument to next call: the class is not the main class if it is not
73 * the first class or the modtype is not a MUI class
75 writemccinit(out, cl != cfg->classlist || cfg->modtype != cl->classtype, cl);
76 break;
77 case GADGET:
78 case DATATYPE:
79 case CLASS:
80 case IMAGE:
81 writeclassinit(out, cl);
82 break;
83 case HIDD:
84 writeoopinit(out, cl);
85 break;
86 default:
87 fprintf(stdout, "Internal error: unsupported classtype in writestart\n");
88 exit(20);
92 fclose(out);
96 static void writedecl(FILE *out, struct config *cfg)
98 struct stringlist *linelistit;
99 int boopsiinc=0, muiinc=0, oopinc=0;
100 struct functionhead *funclistit;
101 struct functionarg *arglistit;
102 struct classinfo *classlistit;
103 char *type, *name;
105 if (cfg->modtype == DEVICE)
107 fprintf(out,
108 "#include <exec/io.h>\n"
109 "#include <exec/errors.h>\n"
112 fprintf(out,
113 "#include <exec/types.h>\n"
114 "#include <exec/libraries.h>\n"
115 "#include <exec/resident.h>\n"
116 "#include <aros/libcall.h>\n"
117 "#include <aros/asmcall.h>\n"
118 "#include <aros/symbolsets.h>\n"
119 "#include <dos/dos.h>\n"
120 "\n"
121 "#include \"%s_libdefs.h\"\n"
122 "\n"
123 "#ifdef SysBase\n"
124 "#undef SysBase\n"
125 "#endif\n"
126 "\n"
127 "#include <proto/exec.h>\n"
128 "#include <proto/alib.h>\n"
129 "#ifndef __AROS__\n"
130 "struct ExecBase *SysBase = NULL;\n"
131 "#endif\n"
132 "\n",
133 cfg->modulename
136 /* Write out declaration section provided in the config file */
137 for (linelistit = cfg->cdeflines; linelistit != NULL; linelistit = linelistit->next)
139 fprintf(out, "%s\n", linelistit->s);
142 /* Is there a variable for storing the segList ? */
143 if (!(cfg->options & OPTION_NOEXPUNGE) && cfg->modtype!=RESOURCE)
145 fprintf(out,
146 "#ifndef GM_SEGLIST_FIELD\n"
147 "static BPTR GM_UNIQUENAME(seglist);\n"
148 "#define GM_SEGLIST_FIELD(lh) (GM_UNIQUENAME(seglist))\n"
149 "#endif\n"
152 if (cfg->options & OPTION_DUPBASE)
153 fprintf(out,
154 "#ifndef GM_ROOTBASE_FIELD\n"
155 "static LIBBASETYPEPTR GM_UNIQUENAME(rootbase);\n"
156 "#define GM_ROOTBASE_FIELD(lh) (GM_UNIQUENAME(rootbase))\n"
157 "#endif\n"
159 if (cfg->options & OPTION_DUPPERID)
160 fprintf(out,
161 "#ifdef __GM_OWNGETID\n"
162 "#include <dos/dosextens.h>\n"
163 "IPTR __GM_Id2(void)\n"
164 "{\n"
165 " struct Process *process = (struct Process *)FindTask(NULL);\n"
166 "\n"
167 " if (process->pr_Task.tc_Node.ln_Type == NT_PROCESS)\n"
168 " return (IPTR)process->pr_ReturnAddr;\n"
169 "\n"
170 " return (IPTR)NULL;\n"
171 "}\n"
172 "#endif\n"
173 "\n"
174 "#ifdef GM_GETID2\n"
175 "struct __GM_ID2Node\n"
176 "{\n"
177 " struct MinNode node;\n"
178 " IPTR id2;\n"
179 " struct Library *lh;\n"
180 " ULONG dupopencount;\n"
181 "};\n"
182 "#endif\n"
183 "\n"
184 "struct __GM_AVLNode {\n"
185 " struct AVLNode node;\n"
186 " IPTR id;\n"
187 "#ifndef GM_GETID2\n"
188 " struct Library *lh;\n"
189 " ULONG dupopencount;\n"
190 "#else\n"
191 " struct MinList id2nodes;\n"
192 "#endif\n"
193 "};\n"
194 "struct __GM_BaseAVL {\n"
195 " LIBBASETYPE base;\n"
196 " struct __GM_AVLNode *avlnode, _avlnode;\n"
197 "#ifdef GM_GETID2\n"
198 " struct __GM_ID2Node *id2node, _id2node;\n"
199 "#endif\n"
200 "};\n"
201 "static AROS_UFH2(LONG, __GM_CompKey,\n"
202 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode, A0),\n"
203 " AROS_UFHA(AVLKey, _key, A1)\n"
204 ")\n"
205 "{\n"
206 " AROS_USERFUNC_INIT\n"
207 "\n"
208 " IPTR id = (IPTR)_key;\n"
209 " if (gm_avlnode->id == id)\n"
210 " return (LONG) 0;\n"
211 " else if (gm_avlnode->id < id)\n"
212 " return (LONG)-1;\n"
213 " else\n"
214 " return (LONG)1;\n"
215 "\n"
216 " AROS_USERFUNC_EXIT\n"
217 "}\n"
218 "static AROS_UFH2(LONG, __GM_CompNode,\n"
219 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode1, A0),\n"
220 " AROS_UFHA(const struct __GM_AVLNode *, gm_avlnode2, A1)\n"
221 ")\n"
222 "{\n"
223 " AROS_USERFUNC_INIT\n"
224 "\n"
225 " return AROS_UFC2(LONG, __GM_CompKey,\n"
226 " AROS_UFPA(const struct __GM_AVLNode *, gm_avlnode1, A0),\n"
227 " AROS_UFPA(AVLKey, (AVLKey)gm_avlnode2->id, A1)\n"
228 " );\n"
229 "\n"
230 " AROS_USERFUNC_EXIT\n"
231 "}\n"
232 "#define LIBBASESIZE sizeof(struct __GM_BaseAVL)\n"
233 "struct AVLNode *__GM_AVLRoot = NULL;\n"
234 "\n"
235 "#ifdef __GM_OWNPARENTBASEID2\n"
236 "LIBBASETYPEPTR GM_UNIQUENAME(__GetParentLibbase)(LIBBASETYPEPTR lh)\n"
237 "{\n"
238 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\n"
239 "\n"
240 " id2node = (struct __GM_ID2Node *)GetSucc(id2node);\n"
241 "\n"
242 " if (id2node != NULL)\n"
243 " return (LIBBASETYPEPTR)id2node->lh;\n"
244 " else\n"
245 " return (LIBBASETYPEPTR)NULL;\n"
246 "}\n"
247 "#endif\n"
248 "\n"
250 else
251 fprintf(out, "#define LIBBASESIZE sizeof(LIBBASETYPE)\n");
253 for (classlistit = cfg->classlist; classlistit != NULL; classlistit = classlistit->next)
255 /* For the main class basename is the same a the module basename */
256 if (strcmp(classlistit->basename, cfg->basename) == 0)
258 if (classlistit->classptr_var == NULL)
260 fprintf(out,
261 "#if !defined(GM_CLASSPTR_FIELD) && !defined(%s_CLASSPTR_FIELD)\n"
262 "static APTR GM_UNIQUENAME(%sClass);\n"
263 "#define GM_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
264 "#define %s_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
265 "#define %s_STORE_CLASSPTR 1\n"
266 "#elif defined(GM_CLASSPTR_FIELD) && !defined(%s_CLASSPTR_FIELD)\n"
267 "#define %s_CLASSPTR_FIELD(lh) (GM_CLASSPTR_FIELD(lh))\n"
268 "#elif !defined(GM_CLASSPTR_FIELD) && defined(%s_CLASSPTR_FIELD)\n"
269 "#define GM_CLASSPTR_FIELD(lh) (%s_CLASSPTR_FIELD(lh))\n"
270 "#endif\n",
271 classlistit->basename,
272 classlistit->basename,
273 classlistit->basename,
274 classlistit->basename, classlistit->basename,
275 classlistit->basename,
276 classlistit->basename,
277 classlistit->basename,
278 classlistit->basename,
279 classlistit->basename
282 else
284 fprintf(out,
285 "#define GM_CLASSPTR_FIELD(lh) (%s)\n"
286 "#define %s_CLASSPTR_FIELD(lh) (%s)\n"
287 "#define %s_STORE_CLASSPTR 1\n",
288 classlistit->classptr_var,
289 classlistit->basename, classlistit->classptr_var,
290 classlistit->basename
294 else
296 if (classlistit->classptr_var == NULL)
298 fprintf(out,
299 "#if !defined(%s_CLASSPTR_FIELD)\n"
300 "static APTR GM_UNIQUENAME(%sClass);\n"
301 "#define %s_CLASSPTR_FIELD(lh) (GM_UNIQUENAME(%sClass))\n"
302 "#define %s_STORE_CLASSPTR 1\n"
303 "#endif\n",
304 classlistit->basename,
305 classlistit->basename,
306 classlistit->basename, classlistit->basename,
307 classlistit->basename
310 else
312 fprintf(out,
313 "#define %s_CLASSPTR_FIELD(lh) (%s)\n"
314 "#define %s_STORE_CLASSPTR 1\n",
315 classlistit->basename, classlistit->classptr_var,
316 classlistit->basename
322 /* Write out the defines for the functions of the function table */
323 writefuncdefs(out, cfg, cfg->funclist);
324 fprintf(out, "\n");
326 /* Write out the includes needed for the classes */
327 if (cfg->classlist != NULL)
328 writeboopsiincludes(out);
330 for (classlistit = cfg->classlist; classlistit != NULL; classlistit = classlistit->next)
332 switch (classlistit->classtype)
334 case MUI:
335 case MCC:
336 case MCP:
337 if (!muiinc)
339 writemuiincludes(out);
340 muiinc = 1;
342 /* Fall through: also write boopsi includes */
343 case GADGET:
344 case DATATYPE:
345 case CLASS:
346 case IMAGE:
347 if (!boopsiinc)
349 writeboopsiincludes(out);
350 boopsiinc = 1;
352 break;
353 case HIDD:
354 if (!oopinc)
356 writeoopincludes(out);
357 oopinc = 1;
359 break;
360 default:
361 fprintf(stderr, "Internal error: unhandled classtype in writedecl\n");
362 exit(20);
368 static void writedeclsets(FILE *out, struct config *cfg)
370 fprintf(out,
371 "THIS_PROGRAM_HANDLES_SYMBOLSETS\n"
372 "DECLARESET(INIT)\n"
373 "DECLARESET(EXIT)\n"
374 "DECLARESET(CTORS)\n"
375 "DECLARESET(DTORS)\n"
376 "DECLARESET(INITLIB)\n"
377 "DECLARESET(EXPUNGELIB)\n"
378 "DECLARESET(OPENLIB)\n"
379 "DECLARESET(CLOSELIB)\n"
380 "DECLARESET(OPENDEV)\n"
381 "DECLARESET(CLOSEDEV)\n"
383 if (cfg->classlist != NULL)
384 fprintf(out,
385 "DECLARESET(CLASSESINIT)\n"
386 "DECLARESET(CLASSESEXPUNGE)\n"
387 "#define ADD2INITCLASSES(symbol, pri) ADD2SET(symbol, classesinit, pri)\n"
388 "#define ADD2EXPUNGECLASSES(symbol, pri) ADD2SET(symbol, classesexpunge, pri)\n"
390 fprintf(out, "\n");
394 static void writeresident(FILE *out, struct config *cfg)
396 fprintf(out,
397 "extern const int GM_UNIQUENAME(End);\n"
398 "extern const APTR GM_UNIQUENAME(FuncTable)[];\n"
400 if (cfg->modtype != RESOURCE)
401 fprintf(out, "static const struct InitTable GM_UNIQUENAME(InitTable);\n");
402 fprintf(out,
403 "\n"
404 "extern const char GM_UNIQUENAME(LibName)[];\n"
405 "extern const char GM_UNIQUENAME(LibID)[];\n"
406 "extern const char GM_UNIQUENAME(Copyright)[];\n"
407 "\n"
410 if (cfg->modtype != RESOURCE)
412 if (!(cfg->options & OPTION_DUPPERID))
414 fprintf(out,
415 "#define __freebase(lh)\\\n"
416 "do {\\\n"
417 " UWORD negsize, possize;\\\n"
418 " UBYTE *negptr = (UBYTE *)lh;\\\n"
419 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
420 " negptr -= negsize;\\\n"
421 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
422 " FreeMem (negptr, negsize+possize);\\\n"
423 "} while(0)\n"
424 "\n"
427 else
429 fprintf(out,
430 "#ifndef GM_GETID2\n"
431 "#define __freebase(lh)\\\n"
432 "do {\\\n"
433 " UWORD negsize, possize;\\\n"
434 " UBYTE *negptr = (UBYTE *)lh;\\\n"
435 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\\\n"
436 " AVL_RemNodeByAddress(&__GM_AVLRoot, (struct AVLNode *)avlnode);\\\n"
437 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
438 " negptr -= negsize;\\\n"
439 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
440 " FreeMem (negptr, negsize+possize);\\\n"
441 "} while(0)\n"
442 "#else\n"
443 "#define __freebase(lh)\\\n"
444 "do {\\\n"
445 " UWORD negsize, possize;\\\n"
446 " UBYTE *negptr = (UBYTE *)lh;\\\n"
447 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\\\n"
448 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\\\n"
449 " BOOL remnode;\\\n"
450 " /* avlnode == NULL for original libbase provided to LibInit */\\\n"
451 " if (avlnode)\\\n"
452 " {\\\n"
453 " Remove((struct Node *)id2node);\\\n"
454 " remnode = GetHead((struct List *)&avlnode->id2nodes) == NULL;\\\n"
455 " if (remnode)\\\n"
456 " AVL_RemNodeByAddress(&__GM_AVLRoot, (struct AVLNode *)avlnode);\\\n"
457 " else if (avlnode == &((struct __GM_BaseAVL *)lh)->_avlnode)\\\n"
458 " /* Do not free as avlnode is still in the AVL Tree\\\n"
459 " This is causing a memory leak because sublibrary is not closed */\\\n"
460 " break;\\\n"
461 " }\\\n"
462 " negsize = ((struct Library *)lh)->lib_NegSize;\\\n"
463 " negptr -= negsize;\\\n"
464 " possize = ((struct Library *)lh)->lib_PosSize;\\\n"
465 " FreeMem (negptr, negsize+possize);\\\n"
466 "} while(0)\n"
467 "#endif\n"
472 fprintf(out,
473 "AROS_UFP3 (LIBBASETYPEPTR, GM_UNIQUENAME(InitLib),\n"
474 " AROS_UFPA(LIBBASETYPEPTR, lh, D0),\n"
475 " AROS_UFPA(BPTR, segList, A0),\n"
476 " AROS_UFPA(struct ExecBase *, sysBase, A6)\n"
477 ");\n"
479 if (cfg->modtype != RESOURCE)
481 fprintf(out,
482 "AROS_LP1(BPTR, GM_UNIQUENAME(ExpungeLib),\n"
483 " AROS_LPA(LIBBASETYPEPTR, extralh, D0),\n"
484 " LIBBASETYPEPTR, lh, 3, %s\n"
485 ");\n"
486 "\n",
487 cfg->basename
490 fprintf(out,
491 "struct Resident const GM_UNIQUENAME(ROMTag) =\n"
492 "{\n"
493 " RTC_MATCHWORD,\n"
494 " (struct Resident *)&GM_UNIQUENAME(ROMTag),\n"
495 " (APTR)&GM_UNIQUENAME(End),\n"
496 " RESIDENTFLAGS,\n"
497 " VERSION_NUMBER,\n"
499 switch (cfg->modtype)
501 case LIBRARY:
502 case MUI:
503 case MCC:
504 case MCP:
505 case GADGET:
506 case DATATYPE:
507 case HIDD:
508 fprintf(out, " NT_LIBRARY,\n");
509 break;
510 case DEVICE:
511 fprintf(out, " NT_DEVICE,\n");
512 break;
513 case RESOURCE:
514 fprintf(out, " NT_RESOURCE,\n");
515 break;
516 default:
517 fprintf(stderr, "Internal error: unsupported modtype for NT_...\n");
518 exit(20);
519 break;
521 fprintf(out,
522 " RESIDENTPRI,\n"
523 " (CONST_STRPTR)&GM_UNIQUENAME(LibName)[0],\n"
524 " (CONST_STRPTR)&GM_UNIQUENAME(LibID)[6],\n"
526 if (cfg->modtype != RESOURCE)
528 fprintf(out,
529 " (APTR)&GM_UNIQUENAME(InitTable)\n"
530 "};\n"
531 "\n"
532 "static struct InitTable\n"
533 "{\n"
534 " IPTR Size;\n"
535 " const APTR *FuncTable;\n"
536 " struct DataTable *DataTable;\n"
537 " APTR InitLibTable;\n"
538 "}\n"
539 "const GM_UNIQUENAME(InitTable) =\n"
540 "{\n"
541 " LIBBASESIZE,\n"
542 " &GM_UNIQUENAME(FuncTable)[0],\n"
543 " NULL,\n"
544 " (APTR)GM_UNIQUENAME(InitLib)\n"
545 "};\n"
548 else /* RESOURCE */
549 fprintf(out, " (APTR)GM_UNIQUENAME(InitLib)\n};\n");
551 fprintf(out,
552 "\n"
553 "const char GM_UNIQUENAME(LibName)[] = MOD_NAME_STRING;\n"
554 "const char GM_UNIQUENAME(LibID)[] = VERSION_STRING;\n"
555 "const char GM_UNIQUENAME(Copyright)[] = COPYRIGHT_STRING;\n"
556 "\n"
560 static void writeinitlib(FILE *out, struct config *cfg)
562 fprintf(out,
563 "AROS_UFH3 (LIBBASETYPEPTR, GM_UNIQUENAME(InitLib),\n"
564 " AROS_UFHA(LIBBASETYPEPTR, lh, D0),\n"
565 " AROS_UFHA(BPTR, segList, A0),\n"
566 " AROS_UFHA(struct ExecBase *, sysBase, A6)\n"
567 ")\n"
568 "{\n"
569 " AROS_USERFUNC_INIT\n"
570 "\n"
571 " int ok;\n"
572 "\n"
575 fprintf(out,
576 "#ifndef __AROS__\n"
577 " SysBase = sysBase;\n"
578 "#endif\n"
579 "#ifdef GM_SYSBASE_FIELD\n"
580 " GM_SYSBASE_FIELD(lh) = sysBase;\n"
581 "#endif\n"
584 if (cfg->modtype == RESOURCE)
586 unsigned int funccount;
587 struct functionhead *funclistit = cfg->funclist;
588 if (funclistit == NULL)
589 funccount = cfg->firstlvo-1;
590 else
592 while (funclistit->next != NULL)
593 funclistit = funclistit->next;
595 funccount = funclistit->lvo;
597 fprintf(out,
598 " int vecsize;\n"
599 " struct Node *n;\n"
600 " char *mem;\n"
601 "\n"
602 " vecsize = %u*LIB_VECTSIZE;\n"
603 " if (vecsize > 0)\n"
604 " vecsize = ((vecsize-1)/sizeof(IPTR) + 1)*sizeof(IPTR);\n"
605 " mem = AllocMem(vecsize+sizeof(LIBBASETYPE), MEMF_PUBLIC|MEMF_CLEAR);\n"
606 " if (mem == NULL)\n"
607 " return NULL;\n"
608 " lh = (LIBBASETYPEPTR)(mem + vecsize);\n"
609 " n = (struct Node *)lh;\n"
610 " n->ln_Type = NT_RESOURCE;\n"
611 " n->ln_Pri = RESIDENTPRI;\n"
612 " n->ln_Name = (char *)GM_UNIQUENAME(LibName);\n"
613 " MakeFunctions(lh, (APTR)GM_UNIQUENAME(FuncTable), NULL);\n",
614 funccount
617 else
619 fprintf(out,
620 " ((struct Library *)lh)->lib_Revision = REVISION_NUMBER;\n"
624 if (!(cfg->options & OPTION_NOEXPUNGE) && cfg->modtype!=RESOURCE)
625 fprintf(out, " GM_SEGLIST_FIELD(lh) = segList;\n");
626 if (cfg->options & OPTION_DUPBASE)
627 fprintf(out, " GM_ROOTBASE_FIELD(lh) = (LIBBASETYPEPTR)lh;\n");
628 fprintf(out, " if (");
629 if (!(cfg->options & OPTION_NOAUTOLIB))
630 fprintf(out, "set_open_libraries() && ");
631 if (cfg->classlist != NULL)
632 fprintf(out, "set_call_libfuncs(SETNAME(CLASSESINIT), 1, 1, lh) && ");
633 fprintf(out,
634 "set_call_funcs(SETNAME(INIT), 1, 1) )\n"
635 " {\n"
636 " set_call_funcs(SETNAME(CTORS), -1, 0);\n"
637 "\n"
638 " ok = set_call_libfuncs(SETNAME(INITLIB), 1, 1, lh);\n"
639 " }\n"
640 " else\n"
641 " ok = 0;\n"
642 "\n"
643 " if (!ok)\n"
644 " {\n"
645 " set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 0, lh);\n"
646 " set_call_funcs(SETNAME(DTORS), 1, 0);\n"
647 " set_call_funcs(SETNAME(EXIT), -1, 0);\n"
649 if (cfg->classlist != NULL)
650 fprintf(out, " set_call_libfuncs(SETNAME(CLASSESEXPUNGE), -1, 0, lh);\n");
651 if (!(cfg->options & OPTION_NOAUTOLIB))
652 fprintf(out, " set_close_libraries();\n");
653 if (cfg->modtype != RESOURCE)
655 fprintf(out,
656 "\n"
657 " __freebase(lh);\n"
660 else
662 fprintf(out,
663 "\n"
664 " FreeMem(mem, vecsize+LIBBASESIZE);\n"
667 fprintf(out,
668 " return NULL;\n"
669 " }\n"
670 " else\n"
671 " {\n"
674 if (cfg->modtype == RESOURCE)
676 fprintf(out,
677 " AddResource(lh);\n"
681 fprintf(out,
682 " return lh;\n"
683 " }\n"
684 "\n"
685 " AROS_USERFUNC_EXIT\n"
686 "}\n"
687 "\n"
692 static void writeopenlib(FILE *out, struct config *cfg)
694 switch (cfg->modtype)
696 case RESOURCE:
697 fprintf(stderr, "Internal error: writeopenlib called for a resource\n");
698 break;
699 case DEVICE:
700 fprintf(out,
701 "AROS_LH3 (void, GM_UNIQUENAME(OpenLib),\n"
702 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
703 " AROS_LHA(ULONG, unitnum, D0),\n"
704 " AROS_LHA(ULONG, flags, D1),\n"
705 " LIBBASETYPEPTR, lh, 1, %s\n"
706 ")\n",
707 cfg->basename
709 fprintf(out,
710 "{\n"
711 " AROS_LIBFUNC_INIT\n"
712 "\n"
713 " if ( set_call_libfuncs(SETNAME(OPENLIB), 1, 1, lh)\n"
714 " && set_call_devfuncs(SETNAME(OPENDEV), 1, 1, lh, ioreq, unitnum, flags)\n"
715 " )\n"
716 " {\n"
717 " ((struct Library *)lh)->lib_OpenCnt++;\n"
718 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
719 "\n"
720 " ioreq->io_Message.mn_Node.ln_Type = NT_REPLYMSG;\n"
721 " }\n"
722 " else\n"
723 " {\n"
724 " if (ioreq->io_Error >= 0)\n"
725 " ioreq->io_Error = IOERR_OPENFAIL;\n"
726 " }\n"
727 "\n"
728 " return;\n"
729 "\n"
730 " AROS_LIBFUNC_EXIT\n"
731 "}\n"
732 "\n"
734 break;
735 default:
736 fprintf(out,
737 "AROS_LH1 (LIBBASETYPEPTR, GM_UNIQUENAME(OpenLib),\n"
738 " AROS_LHA (ULONG, version, D0),\n"
739 " LIBBASETYPEPTR, lh, 1, %s\n"
740 ")\n"
741 "{\n"
742 " AROS_LIBFUNC_INIT\n"
743 "\n",
744 cfg->basename
746 if (!(cfg->options & OPTION_DUPBASE))
748 fprintf(out,
749 " if ( set_call_libfuncs(SETNAME(OPENLIB), 1, 1, lh) )\n"
750 " {\n"
751 " ((struct Library *)lh)->lib_OpenCnt++;\n"
752 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
753 "\n"
754 " return lh;\n"
755 " }\n"
756 "\n"
757 " return NULL;\n"
758 "\n"
759 " AROS_LIBFUNC_EXIT\n"
760 "}\n"
761 "\n"
764 else /* OPTION_DUPBASE */
766 fprintf(out,
767 " struct Library *newlib = NULL;\n"
768 " UWORD possize = ((struct Library *)lh)->lib_PosSize;\n"
770 if (cfg->options & OPTION_DUPPERID)
771 fprintf(out,
772 " struct __GM_AVLNode *avlnode = NULL;\n"
773 " IPTR id = GM_GETID;\n"
774 "#ifdef GM_GETID2\n"
775 " struct __GM_ID2Node *id2node = NULL, *id2node_it;\n"
776 " IPTR id2 = GM_GETID2;\n"
777 "#endif\n"
778 "\n"
779 " avlnode = (struct __GM_AVLNode *)AVL_FindNode(__GM_AVLRoot, (AVLKey)id, (AVLKEYCOMP)__GM_CompKey);\n"
780 "#ifndef GM_GETID2\n"
781 " if (avlnode != NULL)\n"
782 " {\n"
783 " avlnode->dupopencount++;\n"
784 " newlib = avlnode->lh;\n"
785 " }\n"
786 "#else\n"
787 " if (avlnode != NULL)\n"
788 " {\n"
789 " ForeachNode(&avlnode->id2nodes, id2node_it)\n"
790 " {\n"
791 " if (id2node_it->id2 == id2)\n"
792 " id2node = id2node_it;\n"
793 " break;\n"
794 " }\n"
795 " }\n"
796 " if (id2node != NULL)\n"
797 " {\n"
798 " id2node->dupopencount++;\n"
799 " newlib = id2node->lh;\n"
800 " }\n"
801 "#endif\n"
802 "\n"
804 fprintf(out,
805 "\n"
806 " if (newlib == NULL)\n"
807 " {\n"
808 " newlib = MakeLibrary(GM_UNIQUENAME(InitTable).FuncTable,\n"
809 " GM_UNIQUENAME(InitTable).DataTable,\n"
810 " NULL,\n"
811 " GM_UNIQUENAME(InitTable).Size,\n"
812 " (BPTR)NULL\n"
813 " );\n"
814 " if (newlib == NULL)\n"
815 " return NULL;\n"
816 "\n"
817 " CopyMem(lh, newlib, possize);\n"
819 if (cfg->options & OPTION_DUPPERID)
820 fprintf(out,
821 "#ifndef GM_GETID2\n"
822 " avlnode\n"
823 " = ((struct __GM_BaseAVL *)newlib)->avlnode\n"
824 " = &((struct __GM_BaseAVL *)newlib)->_avlnode;\n"
825 " avlnode->id = id;\n"
826 " avlnode->lh = newlib;\n"
827 " avlnode->dupopencount = 1;\n"
828 " AVL_AddNode((struct AVLNode **)&__GM_AVLRoot, (struct AVLNode *)avlnode, (AVLNODECOMP)__GM_CompNode);\n"
829 "#else\n"
830 " if(avlnode == NULL)\n"
831 " {\n"
832 " /* avlnode does not exists yet, use the one in the allocated libbase */\n"
833 " avlnode\n"
834 " = ((struct __GM_BaseAVL *)newlib)->avlnode\n"
835 " = &((struct __GM_BaseAVL *)newlib)->_avlnode;\n"
836 " avlnode->id = id;\n"
837 " NEWLIST(&avlnode->id2nodes);\n"
838 " AVL_AddNode((struct AVLNode **)&__GM_AVLRoot, (struct AVLNode *)avlnode, (AVLNODECOMP)__GM_CompNode);\n"
839 " }\n"
840 " else\n"
841 " /* avlnode already in AVL tree, let avlnode pointer in libbase point to it */\n"
842 " ((struct __GM_BaseAVL *)newlib)->avlnode = avlnode;\n"
843 "\n"
844 " id2node\n"
845 " = ((struct __GM_BaseAVL *)newlib)->id2node\n"
846 " = &((struct __GM_BaseAVL *)newlib)->_id2node;\n"
847 " id2node->id2 = id2;\n"
848 " id2node->lh = newlib;\n"
849 " id2node->dupopencount = 1;\n"
850 " AddHead((struct List *)&avlnode->id2nodes, (struct Node *)id2node);\n"
851 "#endif\n"
853 fprintf(out,
854 "\n"
855 " if (!set_call_libfuncs(SETNAME(OPENLIB), 1, 1, newlib))\n"
856 " {\n"
857 " __freebase(newlib);\n"
858 " return NULL;\n"
859 " }\n"
860 "\n"
861 " ((struct Library *)lh)->lib_OpenCnt++;\n"
862 " ((struct Library *)lh)->lib_Flags &= ~LIBF_DELEXP;\n"
863 " }\n"
864 "\n"
865 " return (LIBBASETYPEPTR)newlib;\n"
866 "\n"
867 " AROS_LIBFUNC_EXIT\n"
868 "}\n"
869 "\n"
876 static void writecloselib(FILE *out, struct config *cfg)
878 if (cfg->modtype != DEVICE)
879 fprintf(out,
880 "AROS_LH0 (BPTR, GM_UNIQUENAME(CloseLib),\n"
881 " LIBBASETYPEPTR, lh, 2, %s\n"
882 ")\n",
883 cfg->basename
885 else
886 fprintf(out,
887 "AROS_LH1(BPTR, GM_UNIQUENAME(CloseLib),\n"
888 " AROS_LHA(struct IORequest *, ioreq, A1),\n"
889 " LIBBASETYPEPTR, lh, 2, %s\n"
890 ")\n",
891 cfg->basename
894 fprintf(out,
895 "{\n"
896 " AROS_LIBFUNC_INIT\n"
897 "\n"
899 if (cfg->modtype == DEVICE)
900 fprintf(out,
901 " if (!set_call_devfuncs(SETNAME(CLOSEDEV), -1, 1, lh, ioreq, 0, 0))\n"
902 " {\n"
903 " return NULL;\n"
904 " }\n"
906 if (!(cfg->options & OPTION_DUPBASE))
908 fprintf(out,
909 " ((struct Library *)lh)->lib_OpenCnt--;\n"
910 " set_call_libfuncs(SETNAME(CLOSELIB), -1, 0, lh);\n"
913 else
915 fprintf(out,
916 " LIBBASETYPEPTR rootbase = GM_ROOTBASE_FIELD(lh);\n"
918 if (cfg->options & OPTION_DUPPERID)
919 fprintf(out,
920 "#ifndef GM_GETID2\n"
921 " struct __GM_AVLNode *avlnode = ((struct __GM_BaseAVL *)lh)->avlnode;\n"
922 " avlnode->dupopencount--;\n"
923 " if (avlnode->dupopencount != 0)\n"
924 " return (BPTR)NULL;\n"
925 "#else\n"
926 " struct __GM_ID2Node *id2node = ((struct __GM_BaseAVL *)lh)->id2node;\n"
927 " id2node->dupopencount--;\n"
928 " if (id2node->dupopencount != 0)\n"
929 " return (BPTR)NULL;\n"
930 "#endif\n"
932 fprintf(out,
933 "\n"
934 " set_call_libfuncs(SETNAME(CLOSELIB), -1, 0, lh);\n"
935 " __freebase(lh);\n"
936 " lh = rootbase;\n"
937 " ((struct Library *)lh)->lib_OpenCnt--;\n"
938 "\n"
941 if (!(cfg->options & OPTION_NOEXPUNGE))
942 fprintf(out,
943 " if\n"
944 " (\n"
945 " (((struct Library *)lh)->lib_OpenCnt == 0)\n"
946 " && (((struct Library *)lh)->lib_Flags & LIBF_DELEXP)\n"
947 " )\n"
948 " {\n"
949 " return AROS_LC1(BPTR, GM_UNIQUENAME(ExpungeLib),\n"
950 " AROS_LCA(LIBBASETYPEPTR, lh, D0),\n"
951 " LIBBASETYPEPTR, lh, 3, %s\n"
952 " );\n"
953 " }\n",
954 cfg->basename
956 fprintf(out,
957 "\n"
958 " return NULL;\n"
959 "\n"
960 " AROS_LIBFUNC_EXIT\n"
961 "}\n"
962 "\n"
967 static void writeexpungelib(FILE *out, struct config *cfg)
969 fprintf(out,
970 "AROS_LH1 (BPTR, GM_UNIQUENAME(ExpungeLib),\n"
971 " AROS_LHA(LIBBASETYPEPTR, extralh, D0),\n"
972 " LIBBASETYPEPTR, lh, 3, %s\n"
973 ")\n",
974 cfg->basename
976 fprintf(out,
977 "{\n"
978 " AROS_LIBFUNC_INIT\n"
979 "\n"
981 if (!(cfg->options & OPTION_NOEXPUNGE))
983 fprintf(out,
984 "\n"
985 " if ( ((struct Library *)lh)->lib_OpenCnt == 0 )\n"
986 " {\n"
987 " BPTR seglist = GM_SEGLIST_FIELD(lh);\n"
988 "\n"
989 " if(!set_call_libfuncs(SETNAME(EXPUNGELIB), -1, 1, lh))\n"
990 " {\n"
991 " ((struct Library *)lh)->lib_Flags |= LIBF_DELEXP;\n"
992 " return NULL;\n"
993 " }\n"
994 "\n"
995 " Remove((struct Node *)lh);\n"
996 "\n"
997 " set_call_funcs(SETNAME(DTORS), 1, 0);\n"
998 " set_call_funcs(SETNAME(EXIT), -1, 0);\n"
1000 if (cfg->classlist != NULL)
1001 fprintf(out, " set_call_libfuncs(SETNAME(CLASSESEXPUNGE), -1, 0, lh);\n");
1002 if (!(cfg->options & OPTION_NOAUTOLIB))
1003 fprintf(out, " set_close_libraries();\n");
1004 fprintf(out,
1005 "\n"
1006 " __freebase(lh);\n"
1007 "\n"
1008 " return seglist;\n"
1009 " }\n"
1010 "\n"
1011 " ((struct Library *)lh)->lib_Flags |= LIBF_DELEXP;\n"
1014 fprintf(out,
1015 "\n"
1016 " return NULL;\n"
1017 "\n"
1018 " AROS_LIBFUNC_EXIT\n"
1019 "}\n"
1020 "\n"
1025 static void writeextfunclib(FILE *out, struct config *cfg)
1027 fprintf(out,
1028 "AROS_LH0 (LIBBASETYPEPTR, GM_UNIQUENAME(ExtFuncLib),\n"
1029 " LIBBASETYPEPTR, lh, 4, %s\n"
1030 ")\n"
1031 "{\n"
1032 " AROS_LIBFUNC_INIT\n"
1033 " return NULL;\n"
1034 " AROS_LIBFUNC_EXIT\n"
1035 "}\n"
1036 "\n",
1037 cfg->basename
1042 static void
1043 writefunctable(FILE *out,
1044 struct config *cfg
1047 struct functionhead *funclistit;
1048 struct functionarg *arglistit;
1049 unsigned int lvo;
1050 int i;
1051 char *name, *type;
1053 /* lvo contains the number of functions already printed in the functable */
1054 lvo = 0;
1056 if (!(cfg->options & OPTION_NORESIDENT))
1058 fprintf(out,
1059 "\n"
1060 "const APTR GM_UNIQUENAME(FuncTable)[]=\n"
1061 "{\n"
1063 if (cfg->modtype != RESOURCE)
1065 fprintf(out,
1066 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(OpenLib),%s),\n"
1067 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(CloseLib),%s),\n"
1068 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(ExpungeLib),%s),\n"
1069 " &AROS_SLIB_ENTRY(GM_UNIQUENAME(ExtFuncLib),%s),\n",
1070 cfg->basename, cfg->basename, cfg->basename, cfg->basename
1072 lvo += 4;
1074 if (cfg->modtype == MCC || cfg->modtype == MUI || cfg->modtype == MCP)
1076 fprintf(out,
1077 " &AROS_SLIB_ENTRY(MCC_Query,%s),\n",
1078 cfg->basename
1080 lvo++;
1082 else if (cfg->modtype == DATATYPE)
1084 fprintf(out,
1085 " &AROS_SLIB_ENTRY(ObtainEngine,%s),\n",
1086 cfg->basename
1088 lvo++;
1090 funclistit = cfg->funclist;
1092 else /* NORESIDENT */
1094 if (cfg->modtype != RESOURCE)
1096 int neednull = 0;
1097 struct functionhead *funclistit2;
1099 funclistit = cfg->funclist;
1100 if (funclistit->lvo != 1)
1102 fprintf(stderr, "Module without a generated resident structure has to provide the Open function (LVO==1)\n");
1103 exit(20);
1105 else
1106 funclistit = funclistit->next;
1108 if (funclistit->lvo != 2)
1110 fprintf(stderr, "Module without a generated resident structure has to provide the Close function (LVO==2)\n");
1111 exit(20);
1113 else
1114 funclistit = funclistit->next;
1116 if (funclistit->lvo == 3)
1117 funclistit = funclistit->next;
1118 else
1119 neednull = 1;
1121 if (funclistit->lvo == 4)
1122 funclistit = funclistit->next;
1123 else
1124 neednull = 1;
1126 if (neednull)
1127 fprintf(out,
1128 "\n"
1129 "AROS_UFH1(static int, %s_null,\n"
1130 " AROS_UFHA(struct Library *, libbase, A6)\n"
1131 ")\n"
1132 "{\n"
1133 " AROS_USERFUNC_INIT\n"
1134 " return 0;\n"
1135 " AROS_USERFUNC_EXIT\n"
1136 "}\n",
1137 cfg->modulename
1140 funclistit = cfg->funclist;
1141 funclistit2 = funclistit->next;
1142 fprintf(out,
1143 "\n"
1144 "const APTR GM_UNIQUENAME(FuncTable)[]=\n"
1145 "{\n"
1146 " &AROS_SLIB_ENTRY(%s,%s),\n"
1147 " &AROS_SLIB_ENTRY(%s,%s),\n",
1148 funclistit->name, cfg->basename,
1149 funclistit2->name, cfg->basename
1151 lvo += 2;
1152 funclistit = funclistit2->next;
1154 if (funclistit->lvo == 3)
1156 fprintf(out, " &AROS_SLIB_ENTRY(%s,%s),\n",
1157 funclistit->name, cfg->basename
1159 funclistit = funclistit->next;
1161 else
1162 fprintf(out, " &%s_null,\n", cfg->modulename);
1163 lvo++;
1165 if (funclistit->lvo == 4)
1167 fprintf(out, " &AROS_SLIB_ENTRY(%s,%s),\n",
1168 funclistit->name, cfg->basename
1170 funclistit = funclistit->next;
1172 else
1173 fprintf(out, " &%s_null,\n", cfg->modulename);
1174 lvo++;
1178 while (funclistit != NULL)
1180 for (i = lvo+1; i<funclistit->lvo; i++)
1181 fprintf(out, " NULL,\n");
1182 lvo = funclistit->lvo;
1184 switch (funclistit->libcall)
1186 case STACK:
1187 fprintf(out, " &%s,\n", funclistit->name);
1188 break;
1190 case REGISTER:
1191 case REGISTERMACRO:
1192 fprintf(out, " &AROS_SLIB_ENTRY(%s,%s),\n", funclistit->name, cfg->basename);
1193 break;
1195 default:
1196 fprintf(stderr, "Internal error: unhandled libcall type in writestart\n");
1197 exit(20);
1198 break;
1201 funclistit = funclistit->next;
1204 fprintf(out, " (void *)-1\n};\n");
1208 static void writesets(FILE *out, struct config *cfg)
1210 fprintf(out,
1211 "DEFINESET(INIT)\n"
1212 "DEFINESET(EXIT)\n"
1213 "DEFINESET(CTORS)\n"
1214 "DEFINESET(DTORS)\n"
1215 "DEFINESET(INITLIB)\n"
1216 "DEFINESET(EXPUNGELIB)\n"
1217 "DEFINESET(OPENLIB)\n"
1218 "DEFINESET(CLOSELIB)\n"
1219 "DEFINESET(OPENDEV)\n"
1220 "DEFINESET(CLOSEDEV)\n"
1222 if (cfg->classlist != NULL)
1223 fprintf(out,
1224 "DEFINESET(CLASSESINIT)\n"
1225 "DEFINESET(CLASSESEXPUNGE)\n"
1227 fprintf(out, "\n");