Debian Codegraph @

gcc/m2/gm2-libs/M2Dependent.mod

raw
1 (* M2Dependent.mod implements the run time module dependencies.
2
3 Copyright (C) 2022-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE M2Dependent ;
28
29
30 FROM libc IMPORT abort, exit, write, getenv, printf, snprintf, strncpy ;
31 FROM ASCII IMPORT nul, nl ;
32 FROM SYSTEM IMPORT ADR ;
33 FROM Storage IMPORT ALLOCATE ;
34 FROM StrLib IMPORT StrCopy, StrLen, StrEqual ;
35
36
37 TYPE
38 PtrToChar = POINTER TO CHAR ;
39
40 DependencyState = (unregistered, unordered, started, ordered, user) ;
41
42 DependencyList = RECORD
43 proc : PROC ;
44 (* Has this module order been forced by the user? *)
45 forced,
46 (* Is the module a definition module for C? *)
47 forc : BOOLEAN ;
48 appl : BOOLEAN ; (* The application module? *)
49 state : DependencyState ;
50 END ;
51
52 ModuleChain = POINTER TO RECORD
53 name,
54 libname : ADDRESS ;
55 init,
56 fini : ArgCVEnvP ;
57 dependency: DependencyList ;
58 prev,
59 next : ModuleChain ;
60 END ;
61
62 ProcedureList = RECORD
63 head, tail: ProcedureChain
64 END ;
65
66 ProcedureChain = POINTER TO RECORD
67 p : PROC ;
68 prev,
69 next: ProcedureChain ;
70 END ;
71
72 VAR
73 Modules : ARRAY DependencyState OF ModuleChain ;
74 DynamicInitialization,
75 Initialized,
76 WarningTrace,
77 ModuleTrace,
78 HexTrace,
79 DependencyTrace,
80 PreTrace,
81 PostTrace,
82 ForceTrace : BOOLEAN ;
83 InitialProc,
84 TerminateProc : ProcedureList ;
85
86
87 (*
88 InitDependencyList - initialize all fields of DependencyList.
89 *)
90
91 PROCEDURE InitDependencyList (VAR depList: DependencyList;
92 proc: PROC; state: DependencyState) ;
93 BEGIN
94 depList.proc := proc ;
95 depList.forced := FALSE ;
96 depList.forc := FALSE ;
97 depList.appl := FALSE ;
98 depList.state := state
99 END InitDependencyList ;
100
101
102 (*
103 CreateModule - creates a new module entry and returns the
104 ModuleChain.
105 *)
106
107 PROCEDURE CreateModule (name, libname: ADDRESS;
108 init, fini: ArgCVEnvP;
109 dependencies: PROC) : ModuleChain ;
110 VAR
111 mptr: ModuleChain ;
112 BEGIN
113 NEW (mptr) ;
114 mptr^.name := name ;
115 mptr^.libname := libname ;
116 mptr^.init := init ;
117 mptr^.fini := fini ;
118 InitDependencyList (mptr^.dependency, dependencies, unregistered) ;
119 mptr^.prev := NIL ;
120 mptr^.next := NIL ;
121 IF HexTrace
122 THEN
123 printf (" (init: %p fini: %p", init, fini) ;
124 printf (" dep: %p)", dependencies)
125 END ;
126 RETURN mptr
127 END CreateModule ;
128
129
130 (*
131 AppendModule - append chain to end of the list.
132 *)
133
134 PROCEDURE AppendModule (VAR head: ModuleChain; chain: ModuleChain) ;
135 BEGIN
136 IF head = NIL
137 THEN
138 head := chain ;
139 chain^.prev := chain ;
140 chain^.next := chain
141 ELSE
142 chain^.next := head ; (* Add Item to the end of list. *)
143 chain^.prev := head^.prev ;
144 head^.prev^.next := chain ;
145 head^.prev := chain
146 END
147 END AppendModule ;
148
149
150 (*
151 RemoveModule - remove chain from double linked list head.
152 *)
153
154 PROCEDURE RemoveModule (VAR head: ModuleChain; chain: ModuleChain) ;
155 BEGIN
156 IF (chain^.next=head) AND (chain=head)
157 THEN
158 head := NIL
159 ELSE
160 IF head=chain
161 THEN
162 head := head^.next
163 END ;
164 chain^.prev^.next := chain^.next ;
165 chain^.next^.prev := chain^.prev
166 END
167 END RemoveModule ;
168
169
170 (*
171 onChain - returns TRUE if mptr is on the Modules[state] list.
172 *)
173
174 PROCEDURE onChain (state: DependencyState; mptr: ModuleChain) : BOOLEAN ;
175 VAR
176 ptr: ModuleChain ;
177 BEGIN
178 IF Modules[state] # NIL
179 THEN
180 ptr := Modules[state] ;
181 REPEAT
182 IF ptr = mptr
183 THEN
184 RETURN TRUE
185 END ;
186 ptr := ptr^.next
187 UNTIL ptr=Modules[state]
188 END ;
189 RETURN FALSE
190 END onChain ;
191
192
193 (*
194 max -
195 *)
196
197 PROCEDURE max (a, b: CARDINAL) : CARDINAL ;
198 BEGIN
199 IF a > b
200 THEN
201 RETURN a
202 ELSE
203 RETURN b
204 END
205 END max ;
206
207
208 (*
209 min -
210 *)
211
212 PROCEDURE min (a, b: CARDINAL) : CARDINAL ;
213 BEGIN
214 IF a < b
215 THEN
216 RETURN a
217 ELSE
218 RETURN b
219 END
220 END min ;
221
222
223 (*
224 LookupModuleN - lookup module from the state list.
225 The strings lengths are known.
226 *)
227
228 PROCEDURE LookupModuleN (state: DependencyState;
229 name: ADDRESS; namelen: CARDINAL;
230 libname: ADDRESS; libnamelen: CARDINAL) : ModuleChain ;
231 VAR
232 ptr: ModuleChain ;
233 BEGIN
234 IF Modules[state] # NIL
235 THEN
236 ptr := Modules[state] ;
237 REPEAT
238 IF (strncmp (ptr^.name, name,
239 max (namelen, strlen (ptr^.name))) = 0) AND
240 (strncmp (ptr^.libname, libname,
241 max (libnamelen, strlen (ptr^.libname))) = 0)
242 THEN
243 RETURN ptr
244 END ;
245 ptr := ptr^.next
246 UNTIL ptr = Modules[state]
247 END ;
248 RETURN NIL
249 END LookupModuleN ;
250
251
252 (*
253 LookupModule - lookup and return the ModuleChain pointer containing
254 module name from a particular list.
255 *)
256
257 PROCEDURE LookupModule (state: DependencyState; name, libname: ADDRESS) : ModuleChain ;
258 BEGIN
259 RETURN LookupModuleN (state,
260 name, strlen (name),
261 libname, strlen (libname))
262 END LookupModule ;
263
264
265 (*
266 toCString - replace any character sequence \n into a newline.
267 *)
268
269 PROCEDURE toCString (VAR str: ARRAY OF CHAR) ;
270 VAR
271 high, i, j: CARDINAL ;
272 BEGIN
273 i := 0 ;
274 high := HIGH (str) ;
275 WHILE i < high DO
276 IF (i < high) AND (str[i] = "\")
277 THEN
278 IF str[i+1] = "n"
279 THEN
280 str[i] := nl ;
281 j := i+1 ;
282 WHILE j < high DO
283 str[j] := str[j+1] ;
284 INC (j)
285 END
286 END
287 END ;
288 INC (i)
289 END
290 END toCString ;
291
292
293 (*
294 strcmp - return 0 if both strings are equal.
295 We cannot use Builtins.def during bootstrap.
296 *)
297
298 PROCEDURE strcmp (a, b: PtrToChar) : INTEGER ;
299 BEGIN
300 IF (a # NIL) AND (b # NIL)
301 THEN
302 IF a = b
303 THEN
304 RETURN 0
305 ELSE
306 WHILE a^ = b^ DO
307 IF a^ = nul
308 THEN
309 RETURN 0
310 END ;
311 INC (a) ;
312 INC (b)
313 END
314 END
315 END ;
316 RETURN 1
317 END strcmp ;
318
319
320 (*
321 strncmp - return 0 if both strings are equal.
322 We cannot use Builtins.def during bootstrap.
323 *)
324
325 PROCEDURE strncmp (a, b: PtrToChar; n: CARDINAL) : INTEGER ;
326 BEGIN
327 IF n = 0
328 THEN
329 RETURN 0
330 ELSIF (a # NIL) AND (b # NIL)
331 THEN
332 IF a = b
333 THEN
334 RETURN 0
335 ELSE
336 WHILE (a^ = b^) AND (n > 0) DO
337 IF (a^ = nul) OR (n = 1)
338 THEN
339 RETURN 0
340 END ;
341 INC (a) ;
342 INC (b) ;
343 DEC (n)
344 END
345 END
346 END ;
347 RETURN 1
348 END strncmp ;
349
350
351 (*
352 strlen - returns the length of string.
353 *)
354
355 PROCEDURE strlen (string: PtrToChar) : INTEGER ;
356 VAR
357 count: INTEGER ;
358 BEGIN
359 IF string = NIL
360 THEN
361 RETURN 0
362 ELSE
363 count := 0 ;
364 WHILE string^ # nul DO
365 INC (string) ;
366 INC (count)
367 END ;
368 RETURN count
369 END
370 END strlen ;
371
372
373 (*
374 traceprintf - wrap printf with a boolean flag.
375 *)
376
377 PROCEDURE traceprintf (flag: BOOLEAN; str: ARRAY OF CHAR) ;
378 BEGIN
379 IF flag
380 THEN
381 toCString (str) ;
382 printf (str)
383 END
384 END traceprintf ;
385
386
387 (*
388 traceprintf2 - wrap printf with a boolean flag.
389 *)
390
391 PROCEDURE traceprintf2 (flag: BOOLEAN; str: ARRAY OF CHAR; arg: ADDRESS) ;
392 VAR
393 ch: CHAR ;
394 BEGIN
395 IF flag
396 THEN
397 toCString (str) ;
398 IF arg = NIL
399 THEN
400 ch := 0C ;
401 arg := ADR (ch)
402 END ;
403 printf (str, arg)
404 END
405 END traceprintf2 ;
406
407
408 (*
409 traceprintf3 - wrap printf with a boolean flag.
410 *)
411
412 PROCEDURE traceprintf3 (flag: BOOLEAN; str: ARRAY OF CHAR;
413 arg1, arg2: ADDRESS) ;
414 VAR
415 ch: CHAR ;
416 BEGIN
417 IF flag
418 THEN
419 toCString (str) ;
420 IF arg1 = NIL
421 THEN
422 ch := 0C ;
423 arg1 := ADR (ch)
424 END ;
425 IF arg2 = NIL
426 THEN
427 ch := 0C ;
428 arg2 := ADR (ch)
429 END ;
430 printf (str, arg1, arg2)
431 END
432 END traceprintf3 ;
433
434
435 (*
436 moveTo - moves mptr to the new list determined by newstate.
437 It updates the mptr state appropriately.
438 *)
439
440 PROCEDURE moveTo (newstate: DependencyState; mptr: ModuleChain) ;
441 BEGIN
442 IF onChain (mptr^.dependency.state, mptr)
443 THEN
444 RemoveModule (Modules[mptr^.dependency.state], mptr)
445 END ;
446 mptr^.dependency.state := newstate ;
447 AppendModule (Modules[mptr^.dependency.state], mptr)
448 END moveTo ;
449
450
451 (*
452 ResolveDependant -
453 *)
454
455 PROCEDURE ResolveDependant (mptr: ModuleChain; currentmodule, libname: ADDRESS) ;
456 BEGIN
457 IF mptr = NIL
458 THEN
459 traceprintf3 (DependencyTrace,
460 " module %s [%s] has not been registered via a global constructor\n",
461 currentmodule, libname);
462 ELSE
463 IF onChain (started, mptr)
464 THEN
465 traceprintf (DependencyTrace, " processing...\n");
466 ELSE
467 moveTo (started, mptr) ;
468 traceprintf3 (DependencyTrace, " starting: %s [%s]\n",
469 currentmodule, libname);
470 mptr^.dependency.proc ; (* Invoke and process the dependency graph. *)
471 traceprintf3 (DependencyTrace, " finished: %s [%s]\n",
472 currentmodule, libname);
473 moveTo (ordered, mptr)
474 END
475 END
476 END ResolveDependant ;
477
478
479 (*
480 RequestDependant - used to specify that modulename is dependant upon
481 module dependantmodule. It only takes effect
482 if we are using DynamicInitialization.
483 *)
484
485 PROCEDURE RequestDependant (modulename, libname,
486 dependantmodule, dependantlibname: ADDRESS) ;
487 BEGIN
488 CheckInitialized ;
489 PerformRequestDependant (modulename, libname,
490 dependantmodule, dependantlibname)
491 END RequestDependant ;
492
493
494 (*
495 PerformRequestDependant - the current modulename has a dependancy upon
496 dependantmodule. If dependantmodule is NIL then
497 modulename has no further dependants and it can be
498 resolved.
499 *)
500
501 PROCEDURE PerformRequestDependant (modulename, libname,
502 dependantmodule, dependantlibname: ADDRESS) ;
503 VAR
504 mptr: ModuleChain ;
505 BEGIN
506 traceprintf3 (DependencyTrace, " module %s [%s]", modulename, libname) ;
507 IF dependantmodule = NIL
508 THEN
509 traceprintf (DependencyTrace, " has finished its import graph\n") ;
510 mptr := LookupModule (unordered, modulename, libname) ;
511 IF mptr # NIL
512 THEN
513 traceprintf3 (DependencyTrace, " module %s [%s] is now ordered\n",
514 modulename, libname) ;
515 moveTo (ordered, mptr)
516 END
517 ELSE
518 traceprintf3 (DependencyTrace, " imports from %s [%s]\n",
519 dependantmodule, dependantlibname) ;
520 mptr := LookupModule (ordered, dependantmodule, dependantlibname) ;
521 IF mptr = NIL
522 THEN
523 traceprintf3 (DependencyTrace, " module %s [%s] is not ordered\n",
524 dependantmodule, dependantlibname) ;
525 mptr := LookupModule (unordered, dependantmodule, dependantlibname) ;
526 IF mptr = NIL
527 THEN
528 traceprintf3 (DependencyTrace, " module %s [%s] is not unordered\n",
529 dependantmodule, dependantlibname) ;
530 mptr := LookupModule (started, dependantmodule, dependantlibname) ;
531 IF mptr = NIL
532 THEN
533 traceprintf3 (DependencyTrace, " module %s [%s] has not started\n",
534 dependantmodule, dependantlibname) ;
535 traceprintf3 (DependencyTrace, " module %s [%s] attempting to import from",
536 modulename, libname) ;
537 traceprintf3 (DependencyTrace, " %s [%s] which has not registered itself via a constructor\n",
538 dependantmodule, dependantlibname)
539 ELSE
540 traceprintf3 (DependencyTrace, " module %s [%s] has registered itself and has started\n",
541 dependantmodule, dependantlibname)
542 END
543 ELSE
544 traceprintf3 (DependencyTrace, " module %s [%s] resolving\n", dependantmodule, dependantlibname) ;
545 ResolveDependant (mptr, dependantmodule, dependantlibname)
546 END
547 ELSE
548 traceprintf3 (DependencyTrace, " module %s [%s]", modulename, libname) ;
549 traceprintf3 (DependencyTrace, " dependant %s [%s] is ordered\n", dependantmodule, dependantlibname)
550 END
551 END
552 END PerformRequestDependant ;
553
554
555 (*
556 ResolveDependencies - resolve dependencies for currentmodule, libname.
557 *)
558
559 PROCEDURE ResolveDependencies (currentmodule, libname: ADDRESS) ;
560 VAR
561 mptr: ModuleChain ;
562 BEGIN
563 mptr := LookupModule (unordered, currentmodule, libname) ;
564 WHILE mptr # NIL DO
565 traceprintf3 (DependencyTrace, " attempting to resolve the dependants for %s [%s]\n",
566 currentmodule, libname);
567 ResolveDependant (mptr, currentmodule, libname) ;
568 mptr := Modules[unordered]
569 END
570 END ResolveDependencies ;
571
572
573 (*
574 DisplayModuleInfo - displays all module in the state.
575 *)
576
577 PROCEDURE DisplayModuleInfo (state: DependencyState; desc: ARRAY OF CHAR) ;
578 VAR
579 mptr : ModuleChain ;
580 count: CARDINAL ;
581 BEGIN
582 IF Modules[state] # NIL
583 THEN
584 printf ("%s modules\n", ADR (desc)) ;
585 mptr := Modules[state] ;
586 count := 0 ;
587 REPEAT
588 IF mptr^.name = NIL
589 THEN
590 printf (" %d %s []", count, mptr^.name)
591 ELSE
592 printf (" %d %s [%s]", count, mptr^.name, mptr^.libname)
593 END ;
594 INC (count) ;
595 IF mptr^.dependency.appl
596 THEN
597 printf (" application")
598 END ;
599 IF mptr^.dependency.forc
600 THEN
601 printf (" for C")
602 END ;
603 IF mptr^.dependency.forced
604 THEN
605 printf (" forced ordering")
606 END ;
607 printf ("\n") ;
608 mptr := mptr^.next ;
609 UNTIL mptr = Modules[state]
610 END
611 END DisplayModuleInfo ;
612
613
614 (*
615 DumpModuleData -
616 *)
617
618 PROCEDURE DumpModuleData (flag: BOOLEAN) ;
619 BEGIN
620 IF flag
621 THEN
622 DisplayModuleInfo (unregistered, "unregistered") ;
623 DisplayModuleInfo (unordered, "unordered") ;
624 DisplayModuleInfo (started, "started") ;
625 DisplayModuleInfo (ordered, "ordered") ;
626 END
627 END DumpModuleData ;
628
629
630 (*
631 combine - dest := src + dest. Places src at the front of list dest.
632 Pre condition: src, dest are lists.
633 Post condition : dest := src + dest
634 src := NIL.
635 *)
636
637 PROCEDURE combine (src, dest: DependencyState) ;
638 VAR
639 last: ModuleChain ;
640 BEGIN
641 WHILE Modules[src] # NIL DO
642 last := Modules[src]^.prev ;
643 moveTo (ordered, last) ;
644 Modules[dest] := last (* New item is at the head. *)
645 END
646 END combine ;
647
648
649 (*
650 tracemodule -
651 *)
652
653 PROCEDURE tracemodule (flag: BOOLEAN; modname: ADDRESS; modlen: CARDINAL; libname: ADDRESS; liblen: CARDINAL) ;
654 VAR
655 buffer: ARRAY [0..100] OF CHAR ;
656 len : CARDINAL ;
657 BEGIN
658 IF flag
659 THEN
660 len := min (modlen, SIZE (buffer)-1) ;
661 strncpy (ADR(buffer), modname, len) ;
662 buffer[len] := 0C ;
663 printf ("%s ", ADR (buffer)) ;
664 len := min (liblen, SIZE (buffer)-1) ;
665 strncpy (ADR(buffer), libname, len) ;
666 buffer[len] := 0C ;
667 printf (" [%s]", ADR (buffer))
668 END
669 END tracemodule ;
670
671
672 (*
673 ForceModule -
674 *)
675
676 PROCEDURE ForceModule (modname: ADDRESS; modlen: CARDINAL;
677 libname: ADDRESS; liblen: CARDINAL) ;
678 VAR
679 mptr: ModuleChain ;
680 BEGIN
681 traceprintf (ForceTrace, "forcing module: ") ;
682 tracemodule (ForceTrace, modname, modlen, libname, liblen) ;
683 traceprintf (ForceTrace, "\n") ;
684 mptr := LookupModuleN (ordered, modname, modlen, libname, liblen) ;
685 IF mptr # NIL
686 THEN
687 mptr^.dependency.forced := TRUE ;
688 moveTo (user, mptr)
689 END
690 END ForceModule ;
691
692
693 (*
694 ForceDependencies - if the user has specified a forced order then we override
695 the dynamic ordering with the preference.
696 *)
697
698 PROCEDURE ForceDependencies (overrideliborder: ADDRESS) ;
699 VAR
700 len,
701 modlen,
702 liblen : CARDINAL ;
703 modname,
704 libname,
705 pc, start: PtrToChar ;
706 BEGIN
707 IF overrideliborder # NIL
708 THEN
709 traceprintf2 (ForceTrace, "user forcing order: %s\n", overrideliborder) ;
710 pc := overrideliborder ;
711 start := pc ;
712 len := 0 ;
713 modname := NIL ;
714 modlen := 0 ;
715 libname := NIL ;
716 liblen := 0 ;
717 WHILE pc^ # nul DO
718 CASE pc^ OF
719
720 ':': libname := start ;
721 liblen := len ;
722 len := 0 ;
723 INC (pc) ;
724 start := pc |
725 ',': modname := start ;
726 modlen := len ;
727 ForceModule (modname, modlen, libname, liblen) ;
728 libname := NIL ;
729 liblen := 0 ;
730 modlen := 0 ;
731 len := 0 ;
732 INC (pc) ;
733 start := pc
734 ELSE
735 INC (pc) ;
736 INC (len)
737 END
738 END ;
739 IF start # pc
740 THEN
741 ForceModule (start, len, libname, liblen)
742 END ;
743 combine (user, ordered)
744 END
745 END ForceDependencies ;
746
747
748 (*
749 CheckApplication - check to see that the application is the last entry in the list.
750 This might happen if the application only imports FOR C modules.
751 *)
752
753 PROCEDURE CheckApplication ;
754 VAR
755 mptr,
756 appl: ModuleChain ;
757 BEGIN
758 mptr := Modules[ordered] ;
759 IF mptr # NIL
760 THEN
761 appl := NIL ;
762 REPEAT
763 IF mptr^.dependency.appl
764 THEN
765 appl := mptr
766 ELSE
767 mptr := mptr^.next
768 END
769 UNTIL (appl # NIL) OR (mptr=Modules[ordered]) ;
770 IF appl # NIL
771 THEN
772 RemoveModule (Modules[ordered], appl) ;
773 AppendModule (Modules[ordered], appl)
774 END
775 END
776 END CheckApplication ;
777
778
779 (*
780 ConstructModules - resolve dependencies and then call each
781 module constructor in turn.
782 *)
783
784 PROCEDURE ConstructModules (applicationmodule, libname,
785 overrideliborder: ADDRESS;
786 argc: INTEGER; argv, envp: ADDRESS) ;
787 VAR
788 mptr: ModuleChain ;
789 BEGIN
790 CheckInitialized ;
791 DynamicInitialization := TRUE ; (* This procedure is only called if we desire dynamic initialization. *)
792 traceprintf3 (ModuleTrace, "application module: %s [%s]\n",
793 applicationmodule, libname);
794 mptr := LookupModule (unordered, applicationmodule, libname) ;
795 IF mptr # NIL
796 THEN
797 mptr^.dependency.appl := TRUE
798 END ;
799 traceprintf (PreTrace, "Pre resolving dependents\n");
800 DumpModuleData (PreTrace) ;
801 ResolveDependencies (applicationmodule, libname) ;
802 traceprintf (PreTrace, "Post resolving dependents\n");
803 DumpModuleData (PostTrace) ;
804 ForceDependencies (overrideliborder) ;
805 traceprintf (ForceTrace, "After user forcing ordering\n");
806 DumpModuleData (ForceTrace) ;
807 CheckApplication ;
808 traceprintf (ForceTrace, "After runtime forces application to the end\n");
809 DumpModuleData (ForceTrace) ;
810 IF Modules[ordered] = NIL
811 THEN
812 traceprintf3 (ModuleTrace, " module: %s [%s] has not registered itself using a global constructor\n",
813 applicationmodule, libname);
814 traceprintf2 (ModuleTrace, " hint try compile and linking using: gm2 %s.mod\n", applicationmodule);
815 traceprintf2 (ModuleTrace, " or try using: gm2 -fscaffold-static %s.mod\n",
816 applicationmodule);
817 ELSE
818 mptr := Modules[ordered] ;
819 REPEAT
820 IF mptr^.dependency.forc
821 THEN
822 traceprintf3 (ModuleTrace, "initializing module: %s [%s] for C\n", mptr^.name, mptr^.libname)
823 ELSE
824 traceprintf3 (ModuleTrace, "initializing module: %s [%s]\n", mptr^.name, mptr^.libname);
825 END ;
826 IF mptr^.dependency.appl
827 THEN
828 traceprintf3 (ModuleTrace, "application module: %s [%s]\n", mptr^.name, mptr^.libname);
829 traceprintf (ModuleTrace, " calling ExecuteInitialProcedures\n");
830 ExecuteInitialProcedures ;
831 traceprintf (ModuleTrace, " calling application module\n");
832 END ;
833 mptr^.init (argc, argv, envp) ;
834 mptr := mptr^.next
835 UNTIL mptr = Modules[ordered]
836 END
837 END ConstructModules ;
838
839
840 (*
841 DeconstructModules - resolve dependencies and then call each
842 module constructor in turn.
843 *)
844
845 PROCEDURE DeconstructModules (applicationmodule, libname: ADDRESS;
846 argc: INTEGER; argv, envp: ADDRESS) ;
847 VAR
848 mptr: ModuleChain ;
849 BEGIN
850 traceprintf3 (ModuleTrace, "application module finishing: %s [%s]\n",
851 applicationmodule, libname);
852 IF Modules[ordered] = NIL
853 THEN
854 traceprintf (ModuleTrace, " no ordered modules found during finishing\n")
855 ELSE
856 traceprintf (ModuleTrace, "ExecuteTerminationProcedures\n") ;
857 ExecuteTerminationProcedures ;
858 traceprintf (ModuleTrace, "terminating modules in sequence\n") ;
859 mptr := Modules[ordered]^.prev ;
860 REPEAT
861 IF mptr^.dependency.forc
862 THEN
863 traceprintf3 (ModuleTrace, "finalizing module: %s [%s] for C\n",
864 mptr^.name, mptr^.libname)
865 ELSE
866 traceprintf3 (ModuleTrace, "finalizing module: %s [%s]\n",
867 mptr^.name, mptr^.libname)
868 END ;
869 mptr^.fini (argc, argv, envp) ;
870 mptr := mptr^.prev
871 UNTIL mptr = Modules[ordered]^.prev
872 END
873 END DeconstructModules ;
874
875
876 (*
877 warning3 - write format arg1 arg2 to stderr.
878 *)
879
880 PROCEDURE warning3 (format: ARRAY OF CHAR; arg1, arg2: ADDRESS) ;
881 VAR
882 buffer: ARRAY [0..4096] OF CHAR ;
883 len : INTEGER ;
884 BEGIN
885 IF WarningTrace
886 THEN
887 len := snprintf (ADR (buffer), SIZE (buffer), "warning: ") ;
888 write (2, ADR (buffer), len) ;
889 len := snprintf (ADR (buffer), SIZE (buffer), format, arg1, arg2) ;
890 write (2, ADR (buffer), len)
891 END
892 END warning3 ;
893
894
895 (*
896 RegisterModule - adds module name to the list of outstanding
897 modules which need to have their dependencies
898 explored to determine initialization order.
899 *)
900
901 PROCEDURE RegisterModule (modulename, libname: ADDRESS;
902 init, fini: ArgCVEnvP;
903 dependencies: PROC) ;
904 VAR
905 mptr: ModuleChain ;
906 BEGIN
907 CheckInitialized ;
908 mptr := LookupModule (unordered, modulename, libname) ;
909 IF mptr = NIL
910 THEN
911 traceprintf3 (ModuleTrace, "module: %s [%s] registering",
912 modulename, libname);
913 moveTo (unordered,
914 CreateModule (modulename, libname, init, fini, dependencies)) ;
915 traceprintf (ModuleTrace, "\n") ;
916 ELSE
917 warning3 ("module: %s [%s] (ignoring duplicate registration)\n",
918 modulename, libname)
919 END
920 END RegisterModule ;
921
922
923 (*
924 equal - return TRUE if C string cstr is equal to str.
925 *)
926
927 PROCEDURE equal (cstr: ADDRESS; str: ARRAY OF CHAR) : BOOLEAN ;
928 BEGIN
929 RETURN strncmp (cstr, ADR (str), StrLen (str)) = 0
930 END equal ;
931
932
933 (*
934 SetupDebugFlags - By default assigns ModuleTrace, DependencyTrace,
935 DumpPostInit to FALSE. It checks the environment
936 GCC_M2LINK_RTFLAG which can contain
937 "all,module,hex,pre,post,dep,force". all turns them all on.
938 The flag meanings are as follows and flags the are in
939 execution order.
940
941 module generate trace info as the modules are registered.
942 hex dump the modules ctor functions address in hex.
943 pre generate a list of all modules seen prior to having
944 their dependancies resolved.
945 dep display a trace as the modules are resolved.
946 post generate a list of all modules seen after having
947 their dependancies resolved dynamically.
948 force generate a list of all modules seen after having
949 their dependancies resolved and forced.
950 *)
951
952 PROCEDURE SetupDebugFlags ;
953 VAR
954 pc: POINTER TO CHAR ;
955 BEGIN
956 ModuleTrace := FALSE ;
957 DependencyTrace := FALSE ;
958 PostTrace := FALSE ;
959 PreTrace := FALSE ;
960 ForceTrace := FALSE ;
961 HexTrace := FALSE ;
962 WarningTrace := FALSE ;
963 pc := getenv (ADR ("GCC_M2LINK_RTFLAG")) ;
964 WHILE (pc # NIL) AND (pc^ # nul) DO
965 IF equal (pc, "all")
966 THEN
967 ModuleTrace := TRUE ;
968 DependencyTrace := TRUE ;
969 PreTrace := TRUE ;
970 PostTrace := TRUE ;
971 ForceTrace := TRUE ;
972 HexTrace := TRUE ;
973 WarningTrace := TRUE ;
974 INC (pc, 3)
975 ELSIF equal (pc, "module")
976 THEN
977 ModuleTrace := TRUE ;
978 INC (pc, 6)
979 ELSIF equal (pc, "warning")
980 THEN
981 WarningTrace := TRUE ;
982 INC (pc, 7)
983 ELSIF equal (pc, "hex")
984 THEN
985 HexTrace := TRUE ;
986 INC (pc, 3)
987 ELSIF equal (pc, "dep")
988 THEN
989 DependencyTrace := TRUE ;
990 INC (pc, 3)
991 ELSIF equal (pc, "pre")
992 THEN
993 PreTrace := TRUE ;
994 INC (pc, 3)
995 ELSIF equal (pc, "post")
996 THEN
997 PostTrace := TRUE ;
998 INC (pc, 4)
999 ELSIF equal (pc, "force")
1000 THEN
1001 ForceTrace := TRUE ;
1002 INC (pc, 5)
1003 ELSE
1004 INC (pc)
1005 END
1006 END
1007 END SetupDebugFlags ;
1008
1009
1010 (*
1011 Init - initialize the debug flags and set all lists to NIL.
1012 *)
1013
1014 PROCEDURE Init ;
1015 VAR
1016 state: DependencyState ;
1017 BEGIN
1018 InitProcList (InitialProc) ;
1019 InitProcList (TerminateProc) ;
1020 SetupDebugFlags ;
1021 FOR state := MIN (DependencyState) TO MAX (DependencyState) DO
1022 Modules[state] := NIL
1023 END ;
1024 DynamicInitialization := FALSE
1025 END Init ;
1026
1027
1028 (*
1029 CheckInitialized - checks to see if this module has been initialized
1030 and if it has not it calls Init. We need this
1031 approach as this module is called by module ctors
1032 before we reach main.
1033 *)
1034
1035 PROCEDURE CheckInitialized ;
1036 BEGIN
1037 IF NOT Initialized
1038 THEN
1039 Initialized := TRUE ;
1040 Init
1041 END
1042 END CheckInitialized ;
1043
1044
1045 (*
1046 ExecuteReverse - execute the procedure associated with procptr
1047 and then proceed to try and execute all previous
1048 procedures in the chain.
1049 *)
1050
1051 PROCEDURE ExecuteReverse (procptr: ProcedureChain) ;
1052 BEGIN
1053 WHILE procptr # NIL DO
1054 procptr^.p ; (* Invoke the procedure. *)
1055 procptr := procptr^.prev
1056 END
1057 END ExecuteReverse ;
1058
1059
1060 (*
1061 ExecuteTerminationProcedures - calls each installed termination procedure
1062 in reverse order.
1063 *)
1064
1065 PROCEDURE ExecuteTerminationProcedures ;
1066 BEGIN
1067 ExecuteReverse (TerminateProc.tail)
1068 END ExecuteTerminationProcedures ;
1069
1070
1071 (*
1072 ExecuteInitialProcedures - executes the initial procedures installed by
1073 InstallInitialProcedure.
1074 *)
1075
1076 PROCEDURE ExecuteInitialProcedures ;
1077 BEGIN
1078 ExecuteReverse (InitialProc.tail)
1079 END ExecuteInitialProcedures ;
1080
1081
1082 (*
1083 AppendProc - append proc to the end of the procedure list
1084 defined by proclist.
1085 *)
1086
1087 PROCEDURE AppendProc (VAR proclist: ProcedureList; proc: PROC) : BOOLEAN ;
1088 VAR
1089 pdes: ProcedureChain ;
1090 BEGIN
1091 NEW (pdes) ;
1092 WITH pdes^ DO
1093 p := proc ;
1094 prev := proclist.tail ;
1095 next := NIL
1096 END ;
1097 IF proclist.head = NIL
1098 THEN
1099 proclist.head := pdes
1100 END ;
1101 proclist.tail := pdes ;
1102 RETURN TRUE
1103 END AppendProc ;
1104
1105
1106 (*
1107 InstallTerminationProcedure - installs a procedure, p, which will
1108 be called when the procedure
1109 ExecuteTerminationProcedures
1110 is invoked. It returns TRUE if the
1111 procedure is installed.
1112 *)
1113
1114 PROCEDURE InstallTerminationProcedure (p: PROC) : BOOLEAN ;
1115 BEGIN
1116 RETURN AppendProc (TerminateProc, p)
1117 END InstallTerminationProcedure ;
1118
1119
1120 (*
1121 InstallInitialProcedure - installs a procedure to be executed just
1122 before the BEGIN code section of the
1123 main program module.
1124 *)
1125
1126 PROCEDURE InstallInitialProcedure (p: PROC) : BOOLEAN ;
1127 BEGIN
1128 RETURN AppendProc (InitialProc, p)
1129 END InstallInitialProcedure ;
1130
1131
1132 (*
1133 InitProcList - initialize the head and tail pointers to NIL.
1134 *)
1135
1136 PROCEDURE InitProcList (VAR p: ProcedureList) ;
1137 BEGIN
1138 p.head := NIL ;
1139 p.tail := NIL
1140 END InitProcList ;
1141
1142
1143 BEGIN
1144 CheckInitialized
1145 END M2Dependent.