Debian Codegraph @

gcc/m2/gm2-compiler/M2Scaffold.mod

raw
1 (* M2Scaffold.mod declare and create scaffold entities.
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 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2Scaffold ;
23
24 FROM SymbolTable IMPORT NulSym, ProcedureKind, MakeProcedure, PutFunction,
25 PutPublic, PutCtor, PutParam, IsProcedure,
26 MakeConstant, PutExtern, MakeArray, PutArray,
27 MakeSubrange, PutSubrange,
28 MakeSubscript, PutSubscript, PutArraySubscript,
29 MakeVar, PutVar, MakeProcedureCtorExtern,
30 PutMonoName,
31 GetMainModule, GetModuleCtors, MakeDefImp,
32 PutModuleCtorExtern, IsDefinitionForC,
33 ForeachModuleDo, IsDefImp, IsModule,
34 IsModuleBuiltin, IsImport, IsImportStatement,
35 GetSymName, StartScope, EndScope,
36 GetModuleDefImportStatementList,
37 GetModuleModImportStatementList,
38 GetImportModule, GetImportStatementList,
39 PutLibName,
40 PutProcedureDeclaredTok, PutProcedureParametersDefined,
41 PutProcedureDefined ;
42
43 FROM NameKey IMPORT NulName, Name, MakeKey, makekey, KeyToCharStar ;
44 FROM M2Base IMPORT Integer, Cardinal ;
45 FROM M2System IMPORT Address ;
46 FROM M2LexBuf IMPORT GetTokenNo, BuiltinTokenNo ;
47 FROM Assertion IMPORT Assert ;
48 FROM Lists IMPORT List, InitList, IncludeItemIntoList, NoOfItemsInList, GetItemFromList, KillList, IsItemInList ;
49 FROM M2MetaError IMPORT MetaErrorT0, MetaErrorStringT0 ;
50 FROM M2Search IMPORT FindSourceDefFile ;
51
52 FROM SFIO IMPORT OpenToWrite, WriteS, ReadS, OpenToRead, Exists ;
53 FROM FIO IMPORT File, EOF, IsNoError, Close ;
54 FROM FormatStrings IMPORT Sprintf1 ;
55
56 FROM M2Options IMPORT GetUselist, ScaffoldStatic, ScaffoldDynamic, GenModuleList,
57 GetGenModuleFilename, GetUselistFilename, GetUselist, cflag,
58 SharedFlag, WholeProgram ;
59
60 FROM M2Base IMPORT Proc ;
61
62 FROM M2Quads IMPORT PushTFtok, PushTtok, PushT, BuildDesignatorArray, BuildAssignment,
63 BuildProcedureCall ;
64
65 FROM M2Batch IMPORT IsModuleKnown, Get ;
66 FROM M2Printf IMPORT printf0, printf1, printf2, printf3, printf4 ;
67 FROM FormatStrings IMPORT HandleEscape ;
68
69 FROM DynamicStrings IMPORT String, InitString, KillString, ConCat, RemoveWhitePrefix,
70 EqualArray, Mark, Assign, Fin, InitStringChar, Length, Slice, Equal,
71 RemoveComment, string, InitStringCharStar ;
72
73 FROM M2Graph IMPORT Graph, InitGraph, KillGraph, AddDependent, SortGraph ;
74
75
76 CONST
77 Comment = '#' ; (* Comment leader *)
78 Debugging = FALSE ;
79
80 VAR
81 uselistModules,
82 ctorModules,
83 ctorGlobals : List ;
84 ctorArrayType : CARDINAL ;
85 initialized : BOOLEAN ;
86
87
88 (* The dynamic scaffold takes the form:
89
90 static void _M2_init (int argc, char *argv[], char *envp[])
91 {
92 M2RTS_ConstructModules (module_name, libname, overrideliborder, argc, argv, envp);
93 }
94
95
96 static void _M2_fini (int argc, char *argv[], char *envp[])
97 {
98 M2RTS_Terminate ();
99 M2RTS_DeconstructModules (module_name, libname, argc, argv, envp);
100 }
101
102
103 int
104 main (int argc, char *argv[], char *envp[])
105 {
106 init (argc, argv, envp);
107 fini (argc, argv, envp);
108 return (0);
109 } *)
110
111
112 (*
113 DeclareCtorArrayType - declare an ARRAY [0..high] OF PROC which will
114 be used to reference every module ctor.
115 *)
116
117 PROCEDURE DeclareCtorArrayType (tokenno: CARDINAL; high: CARDINAL) : CARDINAL ;
118 VAR
119 subscript,
120 subrange : CARDINAL ;
121 BEGIN
122 (* ctorArrayType = ARRAY [0..n] OF PROC ; *)
123 ctorArrayType := MakeArray (tokenno, MakeKey ('ctorGlobalType')) ;
124 PutArray (ctorArrayType, Proc) ;
125 subrange := MakeSubrange (tokenno, NulName) ;
126 PutSubrange (subrange,
127 MakeConstant (tokenno, 0),
128 MakeConstant (tokenno, high),
129 Cardinal) ;
130 subscript := MakeSubscript () ;
131 PutSubscript (subscript, subrange) ;
132 PutArraySubscript (ctorArrayType, subscript) ;
133 RETURN ctorArrayType
134 END DeclareCtorArrayType ;
135
136
137 (*
138 DeclareCtorGlobal - declare the ctorArray variable.
139 *)
140
141 PROCEDURE DeclareCtorGlobal (tokenno: CARDINAL) ;
142 VAR
143 n: CARDINAL ;
144 BEGIN
145 n := NoOfItemsInList (ctorGlobals) ;
146 ctorArrayType := DeclareCtorArrayType (tokenno, n) ;
147 ctorArray := MakeVar (tokenno, MakeKey ('_M2_ctorArray')) ;
148 PutVar (ctorArray, ctorArrayType)
149 END DeclareCtorGlobal ;
150
151
152 (*
153 ForeachModuleCallInit - is only called when -fscaffold-static is enabled.
154 precondition: the module list will be ordered.
155 postcondition: foreach module in the application universe
156 call _M2_module_init (argc, argv, envp);
157 *)
158
159 PROCEDURE ForeachModuleCallInit (tok: CARDINAL; argc, argv, envp: CARDINAL) ;
160 VAR
161 module : CARDINAL ;
162 i, n : CARDINAL ;
163 ctor, init,
164 fini, dep : CARDINAL ;
165 BEGIN
166 i := 1 ;
167 n := NoOfItemsInList (uselistModules) ;
168 WHILE i <= n DO
169 module := GetItemFromList (uselistModules, i) ;
170 IF module # NulSym
171 THEN
172 GetModuleCtors (module, ctor, init, fini, dep) ;
173 IF init # NulSym
174 THEN
175 PushTtok (init, tok) ;
176 PushTtok (argc, tok) ;
177 PushTtok (argv, tok) ;
178 PushTtok (envp, tok) ;
179 PushT (3) ;
180 BuildProcedureCall (tok)
181 END
182 END ;
183 INC (i)
184 END
185 END ForeachModuleCallInit ;
186
187
188 (*
189 ForeachModuleCallFinish - precondition: the module list will be ordered.
190 postcondition: foreach module in the application universe
191 call _M2_module_fini (argc, argv, envp);
192 *)
193
194 PROCEDURE ForeachModuleCallFinish (tok: CARDINAL; argc, argv, envp: CARDINAL) ;
195 VAR
196 module : CARDINAL ;
197 i : CARDINAL ;
198 ctor, init,
199 fini, dep : CARDINAL ;
200 BEGIN
201 i := NoOfItemsInList (uselistModules) ;
202 WHILE i >= 1 DO
203 module := GetItemFromList (uselistModules, i) ;
204 IF module # NulSym
205 THEN
206 GetModuleCtors (module, ctor, init, fini, dep) ;
207 IF fini # NulSym
208 THEN
209 PushTtok (fini, tok) ;
210 PushTtok (argc, tok) ;
211 PushTtok (argv, tok) ;
212 PushTtok (envp, tok) ;
213 PushT (3) ;
214 BuildProcedureCall (tok)
215 END
216 END ;
217 DEC (i)
218 END
219 END ForeachModuleCallFinish ;
220
221
222 (*
223 PopulateCtorArray - assign each element of the ctorArray to the external module ctor.
224 This is only used to force the linker to pull in the ctors from
225 a library.
226 *)
227
228 PROCEDURE PopulateCtorArray (tok: CARDINAL) ;
229 VAR
230 i, n: CARDINAL ;
231 BEGIN
232 n := NoOfItemsInList (ctorModules) ;
233 i := 1 ;
234 WHILE i <= n DO
235 PushTFtok (ctorArray, ctorArrayType, tok) ;
236 PushTtok (MakeConstant (tok, i), tok) ;
237 BuildDesignatorArray ;
238 PushTtok (GetItemFromList (ctorModules, i), tok) ;
239 BuildAssignment (tok) ;
240 INC (i)
241 END
242 END PopulateCtorArray ;
243
244
245 (*
246 LookupModuleSym - returns a defimp module. It looks up an existing
247 module and if this does not exist creates a new one.
248 *)
249
250 PROCEDURE LookupModuleSym (tok: CARDINAL; name: Name) : CARDINAL ;
251 VAR
252 sym : CARDINAL ;
253 FileName,
254 LibName : String ;
255 BEGIN
256 sym := Get (name) ;
257 IF sym = NulSym
258 THEN
259 LibName := NIL ;
260 FileName := NIL ;
261 IF FindSourceDefFile (InitStringCharStar (KeyToCharStar (name)),
262 FileName, LibName)
263 THEN
264 sym := MakeDefImp (tok, name) ;
265 PutLibName (sym, makekey (string (LibName))) ;
266 IF sym # GetMainModule ()
267 THEN
268 PutModuleCtorExtern (tok, sym, NOT WholeProgram)
269 END
270 ELSE
271 MetaErrorStringT0 (tok,
272 Sprintf1 (InitString ('the definition module file for {%%1a} cannot be found'),
273 name))
274 END
275 END ;
276 RETURN sym
277 END LookupModuleSym ;
278
279
280 (*
281 addDependentStatement -
282 *)
283
284 PROCEDURE addDependentStatement (graph: Graph; moduleSym: CARDINAL; list: List) ;
285 VAR
286 n1, n2: Name ;
287 import,
288 depmod,
289 i, n : CARDINAL ;
290 BEGIN
291 n := NoOfItemsInList (list) ;
292 i := 1 ;
293 WHILE i <= n DO
294 import := GetItemFromList (list, i) ;
295 Assert (IsImport (import)) ;
296 depmod := GetImportModule (import) ;
297 AddDependent (graph, moduleSym, depmod) ;
298 IF Debugging
299 THEN
300 n1 := GetSymName (moduleSym) ;
301 n2 := GetSymName (depmod) ;
302 printf2 ("AddDependent (%a, %a)\n",
303 n1, n2)
304 END ;
305 INC (i)
306 END
307 END addDependentStatement ;
308
309
310 (*
311 addDependentImport - adds dependent imports of moduleSym into the graph.
312 *)
313
314 PROCEDURE addDependentImport (graph: Graph; moduleSym: CARDINAL; importList: List) ;
315 VAR
316 stmt,
317 i, n: CARDINAL ;
318 BEGIN
319 n := NoOfItemsInList (importList) ;
320 i := 1 ;
321 WHILE i <= n DO
322 stmt := GetItemFromList (importList, i) ;
323 Assert (IsImportStatement (stmt)) ;
324 addDependentStatement (graph, moduleSym, GetImportStatementList (stmt)) ;
325 INC (i)
326 END
327 END addDependentImport ;
328
329
330 (*
331 TopologicallySortList - topologically sort the list based on import graph.
332 A new list is returned.
333 *)
334
335 PROCEDURE TopologicallySortList (list: List; topModule: CARDINAL) : List ;
336 VAR
337 graph : Graph ;
338 i, n : CARDINAL ;
339 moduleSym: CARDINAL ;
340 BEGIN
341 graph := InitGraph () ;
342 n := NoOfItemsInList (list) ;
343 i := 1 ;
344 WHILE i <= n DO
345 moduleSym := GetItemFromList (uselistModules, i) ;
346 addDependentImport (graph, moduleSym, GetModuleDefImportStatementList (moduleSym)) ;
347 addDependentImport (graph, moduleSym, GetModuleModImportStatementList (moduleSym)) ;
348 INC (i) ;
349 END ;
350 (* Ensure that topModule is also in the graph. *)
351 IF NOT IsItemInList (list, topModule)
352 THEN
353 addDependentImport (graph, topModule, GetModuleDefImportStatementList (topModule)) ;
354 addDependentImport (graph, topModule, GetModuleModImportStatementList (topModule))
355 END ;
356 RETURN SortGraph (graph, topModule)
357 END TopologicallySortList ;
358
359
360 (*
361 AddEntry - adds an entry to the ctorGlobals and uselistModules.
362 *)
363
364 PROCEDURE AddEntry (tok: CARDINAL; name: Name) ;
365 BEGIN
366 IF ctorGlobals # NIL
367 THEN
368 IncludeItemIntoList (ctorGlobals, name)
369 END ;
370 IncludeItemIntoList (uselistModules, LookupModuleSym (tok, name))
371 END AddEntry ;
372
373
374 (*
375 ReadModules - populate ctorGlobals with the modules specified by -fuse-list=filename.
376 *)
377
378 PROCEDURE ReadModules (tok: CARDINAL; filename: String) ;
379 VAR
380 f: File ;
381 s: String ;
382 BEGIN
383 InitList (ctorGlobals) ;
384 InitList (uselistModules) ;
385 f := OpenToRead (filename) ;
386 WHILE NOT EOF (f) DO
387 s := ReadS (f) ;
388 s := RemoveComment (RemoveWhitePrefix (s), Comment) ;
389 IF (NOT Equal (Mark (InitStringChar (Comment)),
390 Mark (Slice (s, 0, Length (Mark (InitStringChar (Comment)))-1)))) AND
391 (NOT EqualArray (s, ''))
392 THEN
393 AddEntry (tok, makekey (string (s)))
394 END ;
395 s := KillString (s)
396 END ;
397 Close (f)
398 END ReadModules ;
399
400
401 VAR
402 ctorTok: CARDINAL ;
403
404
405 (*
406 AddModuleToCtor - adds moduleSym to the uselistModules and
407 sets all modules ctors as extern.
408 *)
409
410 PROCEDURE AddModuleToCtor (moduleSym: CARDINAL) ;
411 BEGIN
412 IF IsModule (moduleSym) OR (NOT IsDefinitionForC (moduleSym))
413 THEN
414 IF (moduleSym # GetMainModule ()) AND (NOT IsModuleBuiltin (moduleSym))
415 THEN
416 PutModuleCtorExtern (ctorTok, moduleSym, NOT WholeProgram) ;
417 IncludeItemIntoList (uselistModules, moduleSym)
418 END
419 END
420 END AddModuleToCtor ;
421
422
423 (*
424 WriteList - writes the list to GetGenModuleFilename
425 providing the filename is not NIL and not '-'.
426 *)
427
428 PROCEDURE WriteList (tok: CARDINAL; list: List) ;
429 VAR
430 fo : File ;
431 name : Name ;
432 moduleSym: CARDINAL ;
433 i, n : CARDINAL ;
434 s : String ;
435 BEGIN
436 IF (GetGenModuleFilename () # NIL) AND (NOT EqualArray (GetGenModuleFilename (), '-'))
437 THEN
438 fo := OpenToWrite (GetGenModuleFilename ()) ;
439 IF IsNoError (fo)
440 THEN
441 i := 1 ;
442 n := NoOfItemsInList (list) ;
443 WHILE i <= n DO
444 moduleSym := GetItemFromList (list, i) ;
445 name := GetSymName (moduleSym) ;
446 s := InitStringCharStar (KeyToCharStar (name)) ;
447 s := ConCat (s, Mark (InitString ('\n'))) ;
448 s := HandleEscape (s) ;
449 s := WriteS (fo, s) ;
450 s := KillString (s) ;
451 INC (i)
452 END ;
453 Close (fo)
454 ELSE
455 s := InitString ("unable to create file containing ctor module list: ") ;
456 s := ConCat (s, GetGenModuleFilename ()) ;
457 MetaErrorStringT0 (tok, s)
458 END
459 END
460 END WriteList ;
461
462
463 (*
464 CreateCtorListFromImports - if GenModuleList then populate
465 the ctor list from all modules which are
466 not FOR 'C'.
467 *)
468
469 PROCEDURE CreateCtorListFromImports (tok: CARDINAL) : BOOLEAN ;
470 VAR
471 newlist: List ;
472 i, n : CARDINAL ;
473 BEGIN
474 IF GenModuleList
475 THEN
476 InitList (uselistModules) ;
477 ctorTok := tok ;
478 ForeachModuleDo (AddModuleToCtor) ;
479 newlist := TopologicallySortList (uselistModules, GetMainModule ()) ;
480 KillList (uselistModules) ;
481 uselistModules := newlist ;
482 (* Now create the ctorGlobals using uselistModules and retain the same order. *)
483 InitList (ctorGlobals) ;
484 i := 1 ;
485 n := NoOfItemsInList (uselistModules) ;
486 WHILE i <= n DO
487 IncludeItemIntoList (ctorGlobals, GetSymName (GetItemFromList (uselistModules, i))) ;
488 INC (i)
489 END ;
490 WriteList (tok, uselistModules) ;
491 RETURN TRUE
492 END ;
493 RETURN FALSE
494 END CreateCtorListFromImports ;
495
496
497 (*
498 CreateCtorList - uses GetUselistFilename and then reads the list of modules.
499 *)
500
501 PROCEDURE CreateCtorList (tok: CARDINAL) : BOOLEAN ;
502 VAR
503 filename: String ;
504 BEGIN
505 IF GetUselist ()
506 THEN
507 filename := GetUselistFilename () ;
508 IF filename # NIL
509 THEN
510 IF Exists (filename)
511 THEN
512 ReadModules (tok, filename) ;
513 RETURN TRUE
514 ELSE
515 IF NOT EqualArray (filename, '-')
516 THEN
517 MetaErrorT0 (tok,
518 '{%E}the filename specified by the -fuse-list= option does not exist') ;
519 END
520 END
521 END ;
522 RETURN FALSE
523 ELSE
524 RETURN CreateCtorListFromImports (tok)
525 END
526 END CreateCtorList ;
527
528
529 (*
530 DeclareModuleExtern - declare the extern _M2_modulename_ctor, _M2_modulename_init,
531 _M2_modulename_fini, _M2_modulename_dep for each external module.
532 *)
533
534 PROCEDURE DeclareModuleExtern (tokenno: CARDINAL) ;
535 VAR
536 n1 : Name ;
537 init,
538 fini,
539 dep,
540 ctor,
541 module: CARDINAL ;
542 n, i : CARDINAL ;
543 BEGIN
544 InitList (ctorModules) ;
545 i := 1 ;
546 n := NoOfItemsInList (uselistModules) ;
547 WHILE i <= n DO
548 module := GetItemFromList (uselistModules, i) ;
549 IF module # GetMainModule ()
550 THEN
551 PutModuleCtorExtern (tokenno, module, NOT WholeProgram)
552 END ;
553 GetModuleCtors (module, ctor, init, fini, dep) ;
554 IncludeItemIntoList (ctorModules, ctor) ;
555 IF Debugging
556 THEN
557 n1 := GetSymName (module) ;
558 printf1 ("%a_ctor added to ctorModules\n", n1)
559 END ;
560 INC (i)
561 END
562 END DeclareModuleExtern ;
563
564
565 (*
566 DeclareScaffoldFunctions - declare main, _M2_init,_M2_finish
567 and _M2_link to the modula-2
568 front end.
569 *)
570
571 PROCEDURE DeclareScaffoldFunctions (tokenno: CARDINAL) ;
572 BEGIN
573 IF CreateCtorList (tokenno)
574 THEN
575 DeclareCtorGlobal (tokenno) ;
576 DeclareModuleExtern (tokenno) ;
577 linkFunction := MakeProcedure (tokenno, MakeKey ("_M2_link")) ;
578 PutMonoName (linkFunction, TRUE) ;
579 PutProcedureDefined (linkFunction, ProperProcedure) ;
580 ELSIF ScaffoldDynamic AND (NOT cflag)
581 THEN
582 MetaErrorT0 (tokenno,
583 '{%O}dynamic linking enabled but no module ctor list has been created, hint use -fuse-list=filename or -fgen-module-list=-')
584 END ;
585
586 initFunction := MakeProcedure (tokenno, MakeKey ("_M2_init")) ;
587 PutMonoName (initFunction, TRUE) ;
588 PutProcedureDefined (initFunction, ProperProcedure) ;
589 finiFunction := MakeProcedure (tokenno, MakeKey ("_M2_fini")) ;
590 PutMonoName (finiFunction, TRUE) ;
591 PutProcedureDefined (initFunction, ProperProcedure) ;
592 IF SharedFlag
593 THEN
594 PutCtor (initFunction, TRUE) ;
595 PutCtor (finiFunction, TRUE)
596 ELSE
597 DeclareArgEnvParams (tokenno, initFunction) ;
598 DeclareArgEnvParams (tokenno, finiFunction) ;
599
600 mainFunction := MakeProcedure (tokenno, MakeKey ("main")) ;
601 PutMonoName (mainFunction, TRUE) ;
602 StartScope (mainFunction) ;
603 PutFunction (BuiltinTokenNo, mainFunction, ProperProcedure, Integer) ;
604 DeclareArgEnvParams (tokenno, mainFunction) ;
605 PutPublic (mainFunction, TRUE) ;
606 PutProcedureDefined (mainFunction, ProperProcedure) ;
607 EndScope
608 END
609 END DeclareScaffoldFunctions ;
610
611
612 (*
613 DeclareArgEnvParams - declares (int argc, void *argv, void *envp)
614 *)
615
616 PROCEDURE DeclareArgEnvParams (tokno: CARDINAL; proc: CARDINAL) ;
617 BEGIN
618 Assert (IsProcedure (proc)) ;
619 StartScope (proc) ;
620 Assert (PutParam (tokno, proc, ProperProcedure, 1, MakeKey ("argc"), Integer, FALSE, tokno)) ;
621 Assert (PutParam (tokno, proc, ProperProcedure, 2, MakeKey ("argv"), Address, FALSE, tokno)) ;
622 Assert (PutParam (tokno, proc, ProperProcedure, 3, MakeKey ("envp"), Address, FALSE, tokno)) ;
623 PutProcedureParametersDefined (proc, ProperProcedure) ;
624 PutProcedureDeclaredTok (proc, ProperProcedure, tokno) ;
625 EndScope
626 END DeclareArgEnvParams ;
627
628
629 (*
630 DeclareScaffold - declare scaffold related entities.
631 *)
632
633 PROCEDURE DeclareScaffold (tokno: CARDINAL) ;
634 BEGIN
635 IF NOT initialized
636 THEN
637 initialized := TRUE ;
638 DeclareScaffoldFunctions (tokno)
639 END
640 END DeclareScaffold ;
641
642
643 BEGIN
644 initialized := FALSE ;
645 finiFunction := NulSym ;
646 initFunction := NulSym ;
647 mainFunction := NulSym ;
648 linkFunction := NulSym ;
649 ctorArray := NulSym ;
650 ctorGlobals := NIL ;
651 ctorModules := NIL ;
652 uselistModules := NIL
653 END M2Scaffold.