aboutsummaryrefslogtreecommitdiff
path: root/gcc/m2/gm2-compiler/P0SymBuild.mod
blob: 429095b402e5843f364a10a19eb05836d220da10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
(* P0SymBuild.mod pass 0 symbol creation.

Copyright (C) 2011-2023 Free Software Foundation, Inc.
Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.

This file is part of GNU Modula-2.

GNU Modula-2 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GNU Modula-2 is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Modula-2; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  *)

IMPLEMENTATION MODULE P0SymBuild ;

FROM Storage IMPORT ALLOCATE, DEALLOCATE ;
FROM M2Printf IMPORT printf0, printf1, printf2 ;
FROM Lists IMPORT List, InitList, KillList, IncludeItemIntoList, RemoveItemFromList, NoOfItemsInList, GetItemFromList, IsItemInList ;
FROM Indexing IMPORT Index, InitIndex, HighIndice, LowIndice, GetIndice, RemoveIndiceFromIndex, IncludeIndiceIntoIndex ;
FROM M2Batch IMPORT MakeDefinitionSource, MakeProgramSource, MakeImplementationSource ;
FROM SymbolTable IMPORT NulSym, MakeInnerModule, SetCurrentModule, SetFileModule, MakeError, PutDefinitionForC ;
FROM NameKey IMPORT Name, NulName ;
FROM M2Quads IMPORT PushT, PushTF, PopT, PopTF, PopN, OperandT, PopTtok, PushTtok, OperandTok ;
FROM M2Reserved IMPORT ImportTok ;
FROM M2Debug IMPORT Assert ;
FROM M2MetaError IMPORT MetaErrorT1, MetaErrorT2, MetaError1, MetaError2 ;
FROM M2LexBuf IMPORT GetTokenNo, UnknownTokenNo ;
IMPORT M2Error ;


CONST
   Debugging = FALSE ;

TYPE
   Kind = (module, program, defimp, inner, procedure, universe, unknown) ;

   BlockInfoPtr = POINTER TO RECORD
                     name           : Name ;
                     kind           : Kind ;
                     sym            : CARDINAL ;
                     level          : CARDINAL ;
                     token          : CARDINAL ;      (* where the block starts.  *)
                     LocalModules   : List ;          (* locally declared modules at the current level  *)
                     ImportedModules: Index ;         (* current list of imports for the scanned module *)
                     toPC,
                     toReturn,
                     toNext,                          (* next in same level *)
                     toUp,                            (* return to outer level *)
                     toDown         : BlockInfoPtr ;  (* first of the inner level *)
                  END ;

   ModuleDesc = POINTER TO RECORD
                              name: Name ;            (* Name of the module.  *)
                              tok : CARDINAL ;        (* Location where the module ident was first seen.  *)
                           END ;

VAR
   headBP,
   curBP : BlockInfoPtr ;
   Level : CARDINAL ;


(*
   nSpaces -
*)

PROCEDURE nSpaces (n: CARDINAL) ;
BEGIN
   WHILE n > 0 DO
      printf0 (" ") ;
      DEC (n)
   END
END nSpaces ;


(*
   DisplayB -
*)

PROCEDURE DisplayB (b: BlockInfoPtr) ;
BEGIN
   CASE b^.kind OF

   program  :  printf1 ("MODULE %a ;\n", b^.name) |
   defimp   :  printf1 ("DEFIMP %a ;\n", b^.name) |
   inner    :  printf1 ("INNER MODULE %a ;\n", b^.name) |
   procedure:  printf1 ("PROCEDURE %a ;\n", b^.name)

   ELSE
      HALT
   END
END DisplayB ;


(*
   DisplayBlock -
*)

PROCEDURE DisplayBlock (b: BlockInfoPtr; l: CARDINAL) ;
VAR
   a: BlockInfoPtr ;
BEGIN
   nSpaces (l) ;
   DisplayB (b) ;
   a := b^.toDown ;
   INC (l, 3) ;
   WHILE a # NIL DO
      DisplayBlock (a, l) ;
      a := a^.toNext
   END ;
   DEC (l, 3) ;
   nSpaces (l) ;
   printf1 ("END %a\n", b^.name)
END DisplayBlock ;


(*
   pc - an interactive debugging aid callable from gdb.
*)

(*
PROCEDURE pc ;
BEGIN
   DisplayB (curBP)
END pc ;
*)


(*
   Display -
*)

PROCEDURE Display ;
VAR
   b: BlockInfoPtr ;
BEGIN
   printf0 ("Universe of Modula-2 modules\n") ;
   IF headBP # NIL
   THEN
      b := headBP^.toDown ;
      WHILE b # NIL DO
         DisplayBlock (b, 0) ;
         b := b^.toNext
      END
   END
END Display ;


(*
   addDown - adds, b, to the down link of, a.
*)

PROCEDURE addDown (a, b: BlockInfoPtr) ;
BEGIN
   IF a^.toDown = NIL
   THEN
      a^.toDown := b
   ELSE
      a := a^.toDown ;
      WHILE a^.toNext # NIL DO
         a := a^.toNext
      END ;
      a^.toNext := b
   END
END addDown ;


(*
   GraftBlock - add a new block, b, into the tree in the correct order.
*)

PROCEDURE GraftBlock (b: BlockInfoPtr) ;
BEGIN
   Assert (curBP # NIL) ;
   Assert (ABS (Level-curBP^.level) <= 1) ;
   CASE Level-curBP^.level OF

   -1:  (* returning up to the outer scope *)
        curBP := curBP^.toUp ;
        Assert (curBP^.toNext = NIL) ;
        curBP^.toNext := b |
    0:  (* add toNext *)
        Assert (curBP^.toNext = NIL) ;
        curBP^.toNext := b ;
        b^.toUp := curBP^.toUp |
   +1:  (* insert down a level *)
        b^.toUp := curBP ;  (* save return value *)
        addDown (curBP, b)

   ELSE
      HALT
   END ;
   curBP := b
END GraftBlock ;


(*
   BeginBlock - denotes the start of the next block.  We remember all imports and
                local modules and procedures created in this block.
*)

PROCEDURE BeginBlock (n: Name; k: Kind; s: CARDINAL; tok: CARDINAL) ;
VAR
   b: BlockInfoPtr ;
BEGIN
   NEW (b) ;
   WITH b^ DO
      name := n ;
      kind := k ;
      sym := s ;
      InitList (LocalModules) ;
      ImportedModules := InitIndex (1) ;
      toPC := NIL ;
      toReturn := NIL ;
      toNext := NIL ;
      toDown := NIL ;
      toUp := NIL ;
      level := Level ;
      token := tok
   END ;
   GraftBlock(b)
END BeginBlock ;


(*
   InitUniverse -
*)

PROCEDURE InitUniverse ;
BEGIN
   NEW (curBP) ;
   WITH curBP^ DO
      name := NulName ;
      kind := universe ;
      sym := NulSym ;
      InitList (LocalModules) ;
      ImportedModules := InitIndex (1) ;
      toNext := NIL ;
      toDown := NIL ;
      toUp := curBP ;
      level := Level
   END ;
   headBP := curBP
END InitUniverse ;


(*
   FlushImports -
*)

PROCEDURE FlushImports (b: BlockInfoPtr) ;
VAR
   i, n: CARDINAL ;
   desc: ModuleDesc ;
BEGIN
   WITH b^ DO
      i := LowIndice (ImportedModules) ;
      n := HighIndice (ImportedModules) ;
      WHILE i <= n DO
         desc := GetIndice (ImportedModules, i) ;
         sym := MakeDefinitionSource (desc^.tok, desc^.name) ;
         Assert (sym # NulSym) ;
         INC (i)
      END
   END
END FlushImports ;


(*
   EndBlock - shutdown the module and create definition symbols for all imported
              modules.
*)

PROCEDURE EndBlock ;
BEGIN
   FlushImports (curBP) ;
   curBP := curBP^.toUp ;
   DEC (Level) ;
   IF Level = 0
   THEN
      FlushImports (curBP)
   END
END EndBlock ;


(*
   RegisterLocalModule - register, n, as a local module.
*)

PROCEDURE RegisterLocalModule (modname: Name) ;
VAR
   i, n: CARDINAL ;
   desc: ModuleDesc ;
BEGIN
   (* printf1('seen local module %a\n', n) ; *)
   WITH curBP^ DO
      IncludeItemIntoList (LocalModules, modname) ;
      i := LowIndice (ImportedModules) ;
      n := HighIndice (ImportedModules) ;
      WHILE i <= n DO
         desc := GetIndice (ImportedModules, i) ;
         IF desc^.name = modname
         THEN
            RemoveIndiceFromIndex (ImportedModules, desc) ;
            DISPOSE (desc) ;
            DEC (n)
            (* Continue checking in case a user imported the same module again.  *)
         ELSE
            INC (i)
         END
      END
   END
END RegisterLocalModule ;


(*
   RegisterImport - register, n, as a module imported from either a local scope or definition module.
*)

PROCEDURE RegisterImport (tok: CARDINAL; modname: Name) ;
VAR
   bp  : BlockInfoPtr ;
   desc: ModuleDesc ;
BEGIN
   (* printf1('register import from module %a\n', n) ; *)
   Assert (curBP # NIL) ;
   Assert (curBP^.toUp # NIL) ;
   bp := curBP^.toUp ;   (* skip over current module *)
   WITH bp^ DO
      IF NOT IsItemInList (LocalModules, modname)
      THEN
         NEW (desc) ;
         desc^.name := modname ;
         desc^.tok := tok ;
         IncludeIndiceIntoIndex (ImportedModules, desc)
      END
   END
END RegisterImport ;


(*
   RegisterImports -
*)

PROCEDURE RegisterImports ;
VAR
   index,
   i, n : CARDINAL ;
BEGIN
   PopT (n) ;       (* n   = # of the Ident List *)
   IF OperandT (n+1) = ImportTok
   THEN
      (* Ident list contains Module Names *)
      i := 1 ;
      WHILE i<=n DO
         index := n+1-i ;
         RegisterImport (OperandTok (index), OperandT (index)) ;
         INC (i)
      END
   ELSE
      (* Ident List contains list of objects *)
      RegisterImport (OperandTok (n+1), OperandT (n+1))
   END ;
   PopN (n+1)   (* clear stack *)
END RegisterImports ;


(*
   RegisterInnerImports -
*)

PROCEDURE RegisterInnerImports ;
VAR
   n: CARDINAL ;
BEGIN
   PopT (n) ;       (* n   = # of the Ident List *)
   IF OperandT (n+1) = ImportTok
   THEN
      (* Ident list contains list of objects, which will be seen outside the scope of this module.  *)
   ELSE
      (* Ident List contains list of objects, but we are importing directly from a module OperandT(n+1) *)
      RegisterImport (OperandTok (n+1), OperandT (n+1))
   END ;
   PopN (n+1)   (* clear stack *)
END RegisterInnerImports ;


(*
   RegisterProgramModule - register the top of stack as a program module.
*)

PROCEDURE RegisterProgramModule ;
VAR
   n  : Name ;
   sym: CARDINAL ;
   tok: CARDINAL ;
BEGIN
   Assert (Level = 0) ;
   INC (Level) ;
   PopTtok (n, tok) ;
   PushTtok (n, tok) ;
   sym := MakeProgramSource (tok, n) ;
   SetCurrentModule (sym) ;
   SetFileModule (sym) ;
   BeginBlock (n, program, sym, tok) ;
   M2Error.EnterProgramScope (n)
END RegisterProgramModule ;


(*
   RegisterImplementationModule - register the top of stack as an implementation module.
*)

PROCEDURE RegisterImplementationModule ;
VAR
   n  : Name ;
   sym: CARDINAL ;
   tok: CARDINAL ;
BEGIN
   Assert (Level = 0) ;
   INC (Level) ;
   PopTtok (n, tok) ;
   PushTtok (n, tok) ;
   sym := MakeImplementationSource (tok, n) ;
   SetCurrentModule (sym) ;
   SetFileModule (sym) ;
   BeginBlock (n, defimp, sym, tok) ;
   M2Error.EnterImplementationScope (n)
END RegisterImplementationModule ;


(*
   RegisterDefinitionModule - register the top of stack as a definition module.
*)

PROCEDURE RegisterDefinitionModule (forC: BOOLEAN) ;
VAR
   n  : Name ;
   sym: CARDINAL ;
   tok: CARDINAL ;
BEGIN
   Assert (Level=0) ;
   INC (Level) ;
   PopTtok (n, tok) ;
   PushTtok (n, tok) ;
   sym := MakeDefinitionSource (tok, n) ;
   SetCurrentModule (sym) ;
   SetFileModule (sym) ;
   IF forC
   THEN
      PutDefinitionForC (sym)
   END ;
   BeginBlock (n, defimp, sym, tok) ;
   M2Error.EnterDefinitionScope (n)
END RegisterDefinitionModule ;


(*
   RegisterInnerModule - register the top of stack as an inner module, this module name
                         will be removed from the list of outstanding imports in the
                         current module block.
*)

PROCEDURE RegisterInnerModule ;
VAR
   n  : Name ;
   tok: CARDINAL ;
BEGIN
   INC (Level) ;
   PopTtok (n, tok) ;
   PushTtok (n, tok) ;
   RegisterLocalModule (n) ;
   BeginBlock (n, inner, NulSym, tok) ;
   M2Error.EnterModuleScope (n)
END RegisterInnerModule ;


(*
   RegisterProcedure - register the top of stack as a procedure.
*)

PROCEDURE RegisterProcedure ;
VAR
   n  : Name ;
   tok: CARDINAL ;
BEGIN
   INC (Level) ;
   PopTtok (n, tok) ;
   PushTtok (n, tok) ;
   BeginBlock (n, procedure, NulSym, tok) ;
   M2Error.EnterProcedureScope (n)
END RegisterProcedure ;


(*
   EndBuildProcedure - ends building a Procedure.
*)

PROCEDURE EndProcedure ;
VAR
   NameEnd, NameStart: Name ;
   end, start        : CARDINAL ;
BEGIN
   PopTtok (NameEnd, end) ;
   PopTtok (NameStart, start) ;
   Assert (start # UnknownTokenNo) ;
   Assert (end # UnknownTokenNo) ;
   IF NameEnd # NameStart
   THEN
      IF NameEnd = NulName
      THEN
         MetaErrorT1 (start,
                      'procedure name at beginning {%1Ea} does not match the name at end',
                      MakeError (start, NameStart)) ;
         MetaError1 ('procedure name at end does not match the name at beginning {%1Ea}',
                     MakeError (start, NameStart))
      ELSE
         MetaErrorT2 (start,
                      'procedure name at beginning {%1Ea} does not match the name at end {%2a}',
                      MakeError (start, curBP^.name), MakeError (end, NameEnd)) ;
         MetaErrorT2 (end,
                      'procedure name at end {%1Ea} does not match the name at beginning {%2Ea}',
                      MakeError (end, NameEnd), MakeError (start, curBP^.name))
      END
   END ;
   EndBlock ;
   M2Error.LeaveErrorScope
END EndProcedure ;


(*
   EndModule -
*)

PROCEDURE EndModule ;
VAR
   NameEnd, NameStart: Name ;
   end, start        : CARDINAL ;
BEGIN
   PopTtok (NameEnd, end) ;
   PopTtok (NameStart, start) ;
   Assert (start # UnknownTokenNo) ;
   Assert (end # UnknownTokenNo) ;
   IF NameEnd # NameStart
   THEN
      IF NameEnd = NulName
      THEN
         MetaErrorT1 (start,
                      'module name at beginning {%1Ea} does not match the name at end',
                      MakeError (start, NameStart)) ;
         MetaError1 ('module name at end does not match the name at beginning {%1Ea}',
                     MakeError (start, NameStart))
      ELSE
         MetaErrorT2 (start,
                      'module name at beginning {%1Ea} does not match the name at end {%2a}',
                      MakeError (start, curBP^.name), MakeError (end, NameEnd)) ;
         MetaErrorT2 (end,
                      'module name at end {%1Ea} does not match the name at beginning {%2Ea}',
                      MakeError (end, NameEnd), MakeError (start, curBP^.name))
      END
   END ;
   EndBlock ;
   M2Error.LeaveErrorScope
END EndModule ;


(*
   DeclareModules - declare all inner modules seen at the current block level.
*)

PROCEDURE DeclareModules ;
VAR
   b: BlockInfoPtr ;
   s: CARDINAL ;
BEGIN
   b := curBP^.toDown ;
   WHILE b # NIL DO
      IF b^.kind = inner
      THEN
         IF Debugging
         THEN
            printf1 ("***  declaring inner module %a\n", b^.name)
         END ;
         s := MakeInnerModule (curBP^.token, b^.name) ;
         Assert (s # NulSym)
      END ;
      b := b^.toNext
   END
END DeclareModules ;


(****
(*
   MoveNext -
*)

PROCEDURE MoveNext ;
VAR
   b: BlockInfoPtr ;
BEGIN
   IF curBP^.toNext#NIL
   THEN
      b := curBP^.toUp ;
      (* moving to next *)
      curBP := curBP^.toNext ;
      (* remember our return *)
      curBP^.toUp := b
   END
END MoveNext ;


(*
   MoveDown -
*)

PROCEDURE MoveDown ;
VAR
   b: BlockInfoPtr ;
BEGIN
   (* move down a level *)
   (* remember where we came from *)
   b := curBP ;
   curBP := curBP^.toDown ;
   curBP^.toUp := b
END MoveDown ;


(*
   MoveUp -
*)

PROCEDURE MoveUp ;
BEGIN
   (* move up to the outer scope *)
   curBP := curBP^.toUp ;
END MoveUp ;
***** *)


(*
   Move -
*)

PROCEDURE Move ;
VAR
   b: BlockInfoPtr ;
BEGIN
   IF Level = curBP^.level
   THEN
      b := curBP^.toReturn ;
      (* moving to next *)
      curBP := curBP^.toNext ;
      (* remember our return *)
      curBP^.toReturn := b
   ELSE
      WHILE Level # curBP^.level DO
         IF Level < curBP^.level
         THEN
            (* move up to the outer scope *)
            b := curBP ;
            curBP := curBP^.toReturn ;
            curBP^.toPC := b^.toNext   (* remember where we reached *)
         ELSE
            (* move down a level *)
            (* remember where we came from *)
            b := curBP ;
            IF curBP^.toPC = NIL
            THEN
               Assert (curBP^.toDown#NIL) ;
               curBP^.toPC := curBP^.toDown
            END ;
            Assert (curBP^.toPC#NIL) ;
            curBP := curBP^.toPC ;
            curBP^.toReturn := b
         END
      END
   END
END Move ;


(*
   EnterBlock -
*)

PROCEDURE EnterBlock (n: Name) ;
BEGIN
   Assert (curBP#NIL) ;
   INC (Level) ;
   Move ;
   IF Debugging
   THEN
      nSpaces (Level*3) ;
      IF n = curBP^.name
      THEN
         printf1 ('block %a\n', n)
      ELSE
         printf2 ('seen block %a but tree has recorded %a\n', n, curBP^.name)
      END
   END ;
   Assert ((n = curBP^.name) OR (curBP^.name = NulName)) ;
   DeclareModules
END EnterBlock ;


(*
   LeaveBlock -
*)

PROCEDURE LeaveBlock ;
BEGIN
   IF Debugging
   THEN
      printf1 ('leaving block %a ', curBP^.name)
   END ;
   DEC (Level) ;
   Move
END LeaveBlock ;


(*
   P0Init -
*)

PROCEDURE P0Init ;
BEGIN
   headBP := NIL ;
   curBP := NIL ;
   Level := 0 ;
   InitUniverse
END P0Init ;


(*
   P1Init -
*)

PROCEDURE P1Init ;
BEGIN
   IF Debugging
   THEN
      Display
   END ;
   (* curBP := headBP^.toDown ; *)
   curBP := headBP ;
   Assert(curBP#NIL) ;
   curBP^.toPC := curBP^.toDown ;
   curBP^.toReturn := curBP ;
   Level := 0
END P1Init ;


END P0SymBuild.