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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
|
(* M2Comp.mod continually calls the compiler for every source file.
Copyright (C) 2001-2024 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 M2Comp ;
FROM M2Pass IMPORT SetPassToPass0, SetPassToPass1, SetPassToPass2, SetPassToPassC,
SetPassToPass3, SetPassToNoPass, SetPassToPassHidden ;
FROM M2Reserved IMPORT toktype ;
FROM M2Search IMPORT FindSourceDefFile, FindSourceModFile ;
FROM M2Code IMPORT Code ;
FROM M2LexBuf IMPORT OpenSource, CloseSource, ResetForNewPass, currenttoken, GetToken,
ReInitialize, currentstring, GetTokenNo, BuiltinTokenNo,
UnknownTokenNo ;
FROM M2FileName IMPORT CalculateFileName ;
FROM M2Preprocess IMPORT PreprocessModule, MakeSaveTempsFileNameExt, OnExitDelete ;
FROM libc IMPORT exit ;
FROM M2Error IMPORT ErrorStringAt, ErrorStringAt2, ErrorStringsAt2,
WriteFormat0, FlushErrors, FlushWarnings, ResetErrorScope ;
FROM M2MetaError IMPORT MetaErrorString0, MetaErrorString1, MetaError0, MetaError1,
MetaString0 ;
FROM FormatStrings IMPORT Sprintf1 ;
FROM P0SymBuild IMPORT P0Init, P1Init ;
FROM M2Debug IMPORT Assert ;
IMPORT m2flex ;
IMPORT P0SyntaxCheck ;
IMPORT P1Build ;
IMPORT P2Build ;
IMPORT PCBuild ;
IMPORT P3Build ;
IMPORT PHBuild ;
IMPORT PCSymBuild ;
IMPORT DynamicStrings ;
FROM M2Batch IMPORT GetSource, GetModuleNo, GetDefinitionModuleFile, GetModuleFile,
AssociateModule, AssociateDefinition, MakeImplementationSource,
MakeProgramSource ;
FROM SymbolTable IMPORT GetSymName, IsDefImp, NulSym,
IsHiddenTypeDeclared, GetFirstUsed, GetMainModule, SetMainModule,
ResolveConstructorTypes, SanityCheckConstants, IsDefinitionForC,
IsBuiltinInModule, PutModLink, IsDefLink, IsModLink, PutLibName,
GetModuleDefImportStatementList, GetModuleModImportStatementList,
GetImportModule, IsImportStatement, IsImport,
GetImportStatementList ;
FROM M2Search IMPORT FindSourceDefFile ;
FROM FIO IMPORT File, StdErr, StdOut, Close, EOF, IsNoError, WriteLine,
WriteChar, FlushOutErr ;
FROM SFIO IMPORT WriteS, OpenToRead, OpenToWrite, ReadS, WriteS ;
FROM NameKey IMPORT Name, GetKey, KeyToCharStar, makekey ;
FROM M2Printf IMPORT fprintf0, fprintf1 ;
FROM M2Quiet IMPORT qprintf0, qprintf1, qprintf2 ;
FROM M2Options IMPORT Verbose, GetM2Prefix, GetM, GetMM, GetDepTarget, GetMF, GetMP,
GetObj, PPonly, Statistics, Quiet, WholeProgram, GetMD, GetMMD,
ExtendedOpaque, GenModuleList ;
FROM PathName IMPORT DumpPathName ;
FROM Lists IMPORT List, NoOfItemsInList, GetItemFromList ;
FROM Indexing IMPORT Index, InitIndex, KillIndex, GetIndice, PutIndice, HighIndice ;
FROM DynamicStrings IMPORT String, InitString, KillString, InitStringCharStar,
Dup, Mark, EqualArray, string, Length, ConCat, ConCatChar,
InitStringChar, RIndex, Slice, Equal, RemoveWhitePrefix ;
CONST
Debugging = FALSE ;
VAR
ModuleType : (None, Definition, Implementation, Program) ;
DepContent : Index ;
DepOutput : String ;
(*
CompilingDefinitionModule - returns true if the current module being
compiled is a definition module.
*)
PROCEDURE CompilingDefinitionModule() : BOOLEAN ;
BEGIN
RETURN( ModuleType=Definition )
END CompilingDefinitionModule ;
(*
CompilingImplementationModule - returns true if the current module being
compiled is an implementation module.
*)
PROCEDURE CompilingImplementationModule() : BOOLEAN ;
BEGIN
RETURN( ModuleType=Implementation )
END CompilingImplementationModule ;
(*
CompilingProgramModule - returns true if the current module being
compiled is a program module.
*)
PROCEDURE CompilingProgramModule() : BOOLEAN ;
BEGIN
RETURN( ModuleType=Program )
END CompilingProgramModule ;
(*
NeedToParseImplementation -
*)
PROCEDURE NeedToParseImplementation (sym: CARDINAL) : BOOLEAN ;
BEGIN
RETURN (IsDefImp(sym) AND IsHiddenTypeDeclared(sym) AND ExtendedOpaque) OR
(IsDefImp(sym) AND IsBuiltinInModule(sym)) OR
(WholeProgram AND (NOT IsDefinitionForC(sym)))
END NeedToParseImplementation ;
(*
GenerateDefDependency - generate a single dependency for the definition module
providing that it can be found and is not blocked by -MM.
*)
PROCEDURE GenerateDefDependency (module: CARDINAL) ;
VAR
stem,
fullpath,
named : String ;
BEGIN
stem := InitStringCharStar (KeyToCharStar (GetSymName (module))) ;
named := NIL ;
IF FindSourceDefFile (stem, fullpath, named)
THEN
IF EqualArray (named, '') OR (NOT GetMM ())
THEN
MergeDep (DepContent, fullpath)
ELSE
fullpath := KillString (fullpath)
END
END ;
stem := KillString (stem) ;
named := KillString (named)
END GenerateDefDependency ;
(*
GenerateDependenciesFromImport - lookup the module associated with the import
and call GenerateDefDependency.
*)
PROCEDURE GenerateDependenciesFromImport (import: CARDINAL) ;
VAR
module : CARDINAL ;
BEGIN
Assert (IsImport (import)) ;
module := GetImportModule (import) ;
GenerateDefDependency (module)
END GenerateDependenciesFromImport ;
(*
GenerateDependenciesFromList - iterative over the import lists and for
each module issue a dependency.
*)
PROCEDURE GenerateDependenciesFromList (dep: List) ;
VAR
importList: List ;
import : CARDINAL ;
i, n, j, m: CARDINAL ;
BEGIN
n := NoOfItemsInList (dep) ;
i := 1 ;
WHILE i <= n DO
import := GetItemFromList (dep, i) ;
IF IsImportStatement (import)
THEN
importList := GetImportStatementList (import) ;
j := 1 ;
m := NoOfItemsInList (importList) ;
WHILE j <= m DO
import := GetItemFromList (importList, j) ;
GenerateDependenciesFromImport (import) ;
INC (j)
END
ELSE
GenerateDependenciesFromImport (import)
END ;
INC (i)
END
END GenerateDependenciesFromList ;
(*
GenerateDependencies - generate a list of dependencies for the main module where
the source code is found in sourcefile.
*)
PROCEDURE GenerateDependencies (sourcefile: String) ;
BEGIN
IF IsDefImp (GetMainModule ())
THEN
GenerateDependenciesFromList (GetModuleDefImportStatementList (GetMainModule ())) ;
GenerateDefDependency (GetMainModule ())
END ;
GenerateDependenciesFromList (GetModuleModImportStatementList (GetMainModule ())) ;
WriteDepContents (DepOutput, DepContent)
END GenerateDependencies ;
(*
Compile - compile file, s, using a 5 pass technique.
*)
PROCEDURE Compile (s: String) ;
BEGIN
DoPass0 (s) ;
FlushWarnings ; FlushErrors ;
ResetForNewPass ; ResetErrorScope ;
qprintf0('Pass 1: scopes, enumerated types, imports and exports\n') ;
DoPass1 ;
FlushWarnings ; FlushErrors ;
IF GetM () OR GetMM ()
THEN
GenerateDependencies (s)
END ;
IF NOT PPonly
THEN
qprintf0('Pass 2: constants and types\n') ;
ResetForNewPass ; ResetErrorScope ;
DoPass2 ;
FlushWarnings ; FlushErrors ;
qprintf0('Pass C: aggregate constants\n') ;
ResetForNewPass ; ResetErrorScope ;
DoPassC ;
FlushWarnings ; FlushErrors ;
qprintf0('Pass 3: quadruple generation\n') ;
ResetForNewPass ; ResetErrorScope ;
DoPass3 ;
FlushWarnings ; FlushErrors ;
qprintf0('Pass 4: gcc tree generation\n') ;
Code ;
FlushWarnings ; FlushErrors
END
END Compile ;
(*
compile - compile the filename.
*)
PROCEDURE compile (filename: ADDRESS) ;
VAR
f: String ;
BEGIN
f := InitStringCharStar (filename) ;
Compile (f) ;
f := KillString (f)
END compile ;
(*
ExamineHeader - examines up until the ';', '[' or eof and determines if the source file
is a program, implementation/definition module.
*)
PROCEDURE ExamineHeader (VAR name: ADDRESS; VAR isdefimp, module: BOOLEAN) ;
BEGIN
(* Stop if we see one of eof ';' '['. *)
WHILE (currenttoken#eoftok) AND
(currenttoken#semicolontok) AND (currenttoken#lsbratok) DO
IF name = NIL
THEN
IF (currenttoken=implementationtok) OR (currenttoken=definitiontok)
THEN
isdefimp := TRUE ;
GetToken
END ;
IF currenttoken=moduletok
THEN
module := TRUE ;
GetToken ;
IF currenttoken=identtok
THEN
name := currentstring
END
END ;
END ;
GetToken
END ;
END ExamineHeader ;
(*
ExamineCompilationUnit - opens the source file to obtain the module name and kind of module.
*)
PROCEDURE ExamineCompilationUnit () : CARDINAL ;
VAR
Message : String ;
name : ADDRESS ;
module,
isdefimp: BOOLEAN ;
BEGIN
name := NIL ;
isdefimp := FALSE ; (* default to program module *)
module := FALSE ; (* Seen module keyword? *)
ExamineHeader (name, isdefimp, module) ;
IF name = NIL
THEN
IF module
THEN
Message := MetaString0 (InitString ('no {%kMODULE} keyword seen'))
ELSE
Message := MetaString0 (InitString ('no module ident seen'))
END ;
m2flex.M2Error (string (Message)) ;
exit (1)
ELSE
(* The token used is will be overwritten when P0 is underway.
At this point we are determining the module kind and the tokens
read will be discarded (see ReInitialize below). *)
IF isdefimp
THEN
RETURN MakeImplementationSource (BuiltinTokenNo, makekey (name))
ELSE
RETURN MakeProgramSource (BuiltinTokenNo, makekey (name))
END
END
END ExamineCompilationUnit ;
(*
PeepInto - peeps into source, s, and initializes a definition/implementation or
program module accordingly.
*)
PROCEDURE PeepInto (s: String) ;
VAR
mainModule: CARDINAL ;
BEGIN
IF OpenSource (s)
THEN
mainModule := ExamineCompilationUnit () ;
IF mainModule # NulSym
THEN
SetMainModule (mainModule)
END ;
CloseSource ;
ReInitialize
ELSE
fprintf1 (StdErr, 'failed to open %s\n', s) ;
exit (1)
END
END PeepInto ;
(*
qprintLibName - print the libname.
*)
PROCEDURE qprintLibName (LibName: String) ;
BEGIN
IF (LibName # NIL) AND (NOT EqualArray (LibName, ''))
THEN
qprintf1 (' [%s]', LibName)
END
END qprintLibName ;
(*
CreateFileStem - create a stem using the template LibName_ModuleName.
*)
PROCEDURE CreateFileStem (SymName, LibName: String) : String ;
BEGIN
IF Length (LibName) > 0
THEN
RETURN ConCat (Dup (LibName), ConCat (InitStringChar ('_'), SymName))
ELSE
RETURN SymName
END
END CreateFileStem ;
(*
Return basename of path. CutExt determines whether the .extension
should be removed.
*)
PROCEDURE BaseName (Path: String; CutExt: BOOLEAN) : String ;
VAR
ext,
basename: INTEGER ;
BEGIN
basename := RIndex (Path, '/', 0) ;
IF basename = -1
THEN
basename := 0
ELSE
basename := basename + 1
END ;
IF CutExt
THEN
ext := RIndex (Path, '.', 0) ;
IF ext=-1
THEN
ext := 0
END
ELSE
ext := 0
END ;
RETURN Slice (Path, basename, ext)
END BaseName ;
(*
IsLibrary - return TRUE if line contains a library module.
*)
PROCEDURE IsLibrary (line: String) : BOOLEAN ;
VAR
moduleName,
libname, filename: String ;
result : BOOLEAN ;
BEGIN
result := FALSE ;
moduleName := BaseName (line, TRUE) ;
filename := NIL ;
libname := NIL ;
IF FindSourceDefFile (moduleName, filename, libname)
THEN
moduleName := KillString (moduleName) ;
IF Length (libname) > 0
THEN
moduleName := BaseName (line, FALSE) ;
line := BaseName (line, FALSE) ;
result := Equal (line, moduleName) ;
line := KillString (line) ;
END
END ;
libname := KillString (libname) ;
filename := KillString (filename) ;
moduleName := KillString (moduleName) ;
RETURN result
END IsLibrary ;
(*
IsUnique - return TRUE if line is unique in array content.
*)
PROCEDURE IsUnique (content: Index; line: String) : BOOLEAN ;
VAR
high, i: CARDINAL ;
BEGIN
high := HighIndice (content) ;
i := 1 ;
WHILE i <= high DO
IF Equal (line, GetIndice (content, i))
THEN
RETURN FALSE
END ;
INC (i)
END ;
RETURN TRUE
END IsUnique ;
(*
Append - append line to array content.
*)
PROCEDURE Append (content: Index; line: String) ;
VAR
high: CARDINAL ;
BEGIN
high := HighIndice (content) ;
PutIndice (content, high+1, line)
END Append ;
(*
MergeDep - if line is unique in array content then append.
Check to see (and ignore) if line is a library module and -MM
is present.
*)
PROCEDURE MergeDep (content: Index; line: String) ;
BEGIN
line := RemoveWhitePrefix (line) ;
IF (NOT EqualArray (line, "\")) AND (Length (line) > 0)
THEN
(* Ignore if -MM and is a library module. *)
IF NOT (GetMM () AND IsLibrary (line))
THEN
IF IsUnique (content, line)
THEN
Append (content, line)
END
END
END
END MergeDep ;
(*
splitLine - split a line into words separated by spaces
and call MergeDep on each word.
*)
PROCEDURE splitLine (content: Index; line: String) ;
VAR
word : String ;
space: INTEGER ;
BEGIN
REPEAT
line := RemoveWhitePrefix (line) ;
space := DynamicStrings.Index (line, ' ', 0) ;
IF space > 0
THEN
word := Slice (line, 0, space) ;
word := RemoveWhitePrefix (word) ;
IF Length (word) > 0
THEN
MergeDep (content, word)
END ;
line := Slice (line, space, 0) ;
ELSIF space < 0
THEN
MergeDep (content, line)
END
UNTIL space <= 0
END splitLine ;
(*
MergeDeps - foreach dependency in ChildDep do
add dependency to ChildDep if not already present.
ignore all ChildDep if -MM and libname # "".
*)
PROCEDURE MergeDeps (content: Index; ChildDep, LibName: String) ;
VAR
line: String ;
in : File ;
BEGIN
IF (content # NIL) AND (NOT (GetMM () AND (Length (LibName) > 0)))
THEN
in := OpenToRead (ChildDep) ;
IF IsNoError (in)
THEN
line := ReadS (in) ; (* Skip over first line containing the module object. *)
WHILE NOT EOF (in) DO
line := ReadS (in) ;
splitLine (content, line)
END
END ;
Close (in)
END
END MergeDeps ;
(*
GetRuleTarget - return the rule target which is derived from the -MT arg
or -o arg or filename.mod.
*)
PROCEDURE GetRuleTarget (filename: String) : String ;
BEGIN
IF GetDepTarget () # NIL
THEN
RETURN InitStringCharStar (GetDepTarget ())
ELSIF GetMF () # NIL
THEN
RETURN InitStringCharStar (GetMF ())
ELSE
RETURN ConCat (BaseName (filename, TRUE), InitString ('.o'))
END
END GetRuleTarget ;
(*
ReadDepContents - reads the contents of file dep into a dynamic array
and return the array. The file will be split into words
and each word stored as an entry in the array.
*)
PROCEDURE ReadDepContents (filename, dep: String) : Index ;
VAR
content: Index ;
line : String ;
in : File ;
BEGIN
content := NIL ;
IF GetM () OR GetMM ()
THEN
in := OpenToRead (dep) ;
(* The file might not be created (if -MD or -MMD is used as these options
operate without preprocessing) in which case we create an dynamic
array with the source filename and target. *)
content := InitIndex (1) ;
IF GetMD () OR GetMMD () OR (NOT IsNoError (in))
THEN
(* No preprocessing done therefore create first two lines using
target and source. *)
PutIndice (content, 1, ConCatChar (GetRuleTarget (filename), ':')) ;
PutIndice (content, 2, Dup (filename))
ELSE
(* Preprocessing (using cc1) has created one for us, so we read it. *)
WHILE NOT EOF (in) DO
line := ReadS (in) ;
splitLine (content, line)
END
END ;
Close (in)
END ;
RETURN content
END ReadDepContents ;
(*
WriteDep - write the dependencies and target to file out.
*)
PROCEDURE WriteDep (dep: String; contents: Index; out: File) ;
VAR
i, h: CARDINAL ;
line: String ;
BEGIN
i := 1 ;
h := HighIndice (contents) ;
WHILE i <= h DO
line := GetIndice (contents, i) ;
line := RemoveWhitePrefix (line) ;
IF Length (line) > 0
THEN
IF i = 1
THEN
(* First line is always the target. *)
IF GetDepTarget () # NIL
THEN
line := ConCatChar (InitStringCharStar (GetDepTarget ()), ':')
END
ELSIF i > 1
THEN
WriteChar (out, ' ')
END ;
line := WriteS (out, line) ;
IF i < h
THEN
WriteChar (out, ' ') ;
WriteChar (out, '\')
END ;
WriteLine (out)
END ;
INC (i)
END
END WriteDep ;
(*
WritePhonyDep - write the dependencies and target to file out.
*)
PROCEDURE WritePhonyDep (dep: String; contents: Index; out: File) ;
VAR
i, h: CARDINAL ;
line: String ;
BEGIN
(* The first line is always the target and the second line is always
the top level source file. *)
i := 3 ;
h := HighIndice (contents) ;
WHILE i <= h DO
line := GetIndice (contents, i) ;
line := RemoveWhitePrefix (line) ;
IF Length (line) > 0
THEN
line := WriteS (out, line) ;
WriteChar (out, ':') ;
WriteLine (out)
END ;
INC (i)
END
END WritePhonyDep ;
(*
WriteDepContents - write the dynamic array to filename dep (or StdOut) if
the GetMF file is NIL.
*)
PROCEDURE WriteDepContents (dep: String; contents: Index) ;
VAR
out: File ;
BEGIN
IF (contents # NIL) AND (GetM () OR GetMM ())
THEN
IF GetMF () = NIL
THEN
out := StdOut ;
dep := OnExitDelete (dep)
ELSE
out := OpenToWrite (dep)
END ;
IF IsNoError (out)
THEN
WriteDep (dep, contents, out) ;
IF GetMP ()
THEN
WritePhonyDep (dep, contents, out)
END
END ;
IF GetMF () = NIL
THEN
FlushOutErr
ELSE
Close (out) ;
END ;
contents := KillIndex (contents)
END
END WriteDepContents ;
(*
CreateDepFilename - return a dependency filename associated with filename or use GetMF.
*)
PROCEDURE CreateDepFilename (filename: String) : String ;
VAR
depfile: String ;
BEGIN
IF GetMF () = NIL
THEN
depfile := MakeSaveTempsFileNameExt (filename, InitString ('.d')) ;
RETURN OnExitDelete (depfile)
ELSE
RETURN InitStringCharStar (GetMF ())
END
END CreateDepFilename ;
(*
Pass0CheckDef -
*)
PROCEDURE Pass0CheckDef (sym: CARDINAL) : BOOLEAN ;
VAR
ChildDep,
SymName,
FileName,
LibName : String ;
BEGIN
LibName := NIL ;
FileName := NIL ;
SymName := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ;
IF IsDefImp (sym)
THEN
IF FindSourceDefFile (SymName, FileName, LibName)
THEN
ModuleType := Definition ;
ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.def.d')) ;
IF OpenSource (AssociateDefinition (PreprocessModule (FileName, FALSE, TRUE,
ChildDep), sym))
THEN
IF NOT P0SyntaxCheck.CompilationUnit ()
THEN
WriteFormat0 ('compilation failed') ;
CloseSource ;
SymName := KillString (SymName) ;
FileName := KillString (FileName) ;
LibName := KillString (LibName) ;
RETURN FALSE
END ;
qprintf2 (' Module %-20s : %s', SymName, FileName) ;
qprintLibName (LibName) ;
PutLibName (sym, makekey (string (LibName))) ;
IF IsDefinitionForC (sym)
THEN
qprintf0 (' (for C)')
END ;
IF IsDefLink (sym)
THEN
qprintf0 (' (linking)')
END ;
qprintf0 ('\n') ;
CloseSource ;
MergeDeps (DepContent, ChildDep, LibName)
ELSE
(* Unrecoverable error. *)
MetaErrorString1 (Sprintf1 (InitString ('file {%%1EUAF%s} containing module {%%1a} cannot be found'),
FileName), sym)
END
ELSE
(* Unrecoverable error. *)
MetaError1 ('the file containing the definition module {%1EMAa} cannot be found', sym)
END ;
ModuleType := Implementation
ELSE
ModuleType := Program
END ;
SymName := KillString (SymName) ;
FileName := KillString (FileName) ;
LibName := KillString (LibName) ;
RETURN TRUE
END Pass0CheckDef ;
(*
Pass0CheckMod -
*)
PROCEDURE Pass0CheckMod (sym: CARDINAL; PPSource: String) : BOOLEAN ;
VAR
Main : CARDINAL ;
ChildDep,
SymName,
FileName,
LibName : String ;
BEGIN
SymName := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ;
FileName := NIL ;
LibName := NIL ;
Main := GetMainModule () ;
IF (Main = sym) OR NeedToParseImplementation (sym)
THEN
(* Only need to read implementation module if hidden types are
declared or it is the main module. *)
IF Main = sym
THEN
FileName := Dup (PPSource) ;
LibName := InitStringCharStar (GetM2Prefix ()) ;
PutLibName (sym, makekey (string (LibName)))
ELSE
IF FindSourceModFile (SymName, FileName, LibName)
THEN
ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.mod.d')) ;
FileName := PreprocessModule (FileName, FALSE, TRUE, ChildDep) ;
PutLibName (sym, makekey (string (LibName))) ;
MergeDeps (DepContent, ChildDep, LibName)
ELSE
qprintf1 (' Module %-20s : implementation source file not found\n', SymName)
END
END ;
IF FileName # NIL
THEN
IF OpenSource (AssociateModule (Dup (FileName), sym))
THEN
IF NOT P0SyntaxCheck.CompilationUnit ()
THEN
WriteFormat0 ('compilation failed') ;
CloseSource ;
SymName := KillString (SymName) ;
FileName := KillString (FileName) ;
LibName := KillString (LibName) ;
RETURN FALSE
END ;
qprintf2 (' Module %-20s : %s', SymName, FileName) ;
qprintLibName (LibName) ;
IF IsModLink (sym)
THEN
qprintf0 (' (linking)')
END ;
qprintf0 ('\n') ;
CloseSource
ELSE
(* It is quite legitimate to implement a module in C (and pretend it was a M2
implementation) providing that it is not the main program module and the
definition module does not declare a hidden type when -fextended-opaque
is used. *)
IF (NOT WholeProgram) OR (sym = Main) OR IsHiddenTypeDeclared (sym)
THEN
(* Unrecoverable error. *)
MetaErrorString1 (Sprintf1 (InitString ('file {%%1EUAF%s} containing module {%%1a} cannot be found'),
FileName), sym) ;
END
END
END
ELSIF GenModuleList
THEN
IF IsDefImp (sym) AND (NOT IsDefinitionForC (sym))
THEN
(* The implementation module is only useful if -fgen-module-list= is
used (to gather all dependencies). Note that we do not insist
upon finding the implementation module. *)
LibName := NIL ;
IF FindSourceModFile (SymName, FileName, LibName)
THEN
PutLibName (sym, makekey (string (LibName))) ;
qprintf2 (' Module %-20s : %s' , SymName, FileName) ;
qprintLibName (LibName) ;
qprintf0 (' (linking)\n') ;
ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.mod.d')) ;
IF OpenSource (AssociateModule (PreprocessModule (FileName, FALSE, TRUE, ChildDep), sym))
THEN
PutModLink (sym, TRUE) ; (* This source is only used to determine link time info. *)
IF NOT P0SyntaxCheck.CompilationUnit ()
THEN
WriteFormat0 ('compilation failed') ;
CloseSource ;
SymName := KillString (SymName) ;
FileName := KillString (FileName) ;
LibName := KillString (LibName) ;
RETURN FALSE
END ;
CloseSource ;
MergeDeps (DepContent, ChildDep, LibName)
END
END
END
END ;
SymName := KillString (SymName) ;
FileName := KillString (FileName) ;
LibName := KillString (LibName) ;
RETURN TRUE
END Pass0CheckMod ;
(*
DoPass0 -
*)
PROCEDURE DoPass0 (filename: String) ;
VAR
sym : CARDINAL ;
i : CARDINAL ;
PPSource : String ;
BEGIN
P0Init ;
SetPassToPass0 ;
(* Maybe preprocess the main file. *)
DepOutput := CreateDepFilename (filename) ;
PPSource := PreprocessModule (filename, TRUE, FALSE, DepOutput) ;
DepContent := ReadDepContents (filename, DepOutput) ;
PeepInto (PPSource) ;
i := 1 ;
sym := GetModuleNo (i) ;
qprintf1 ('Compiling: %s\n', PPSource) ;
IF Debugging
THEN
DumpPathName ('DoPass0')
END ;
IF Verbose
THEN
fprintf1 (StdOut, 'Compiling: %s\n', PPSource)
END ;
qprintf0 ('Pass 0: lexical analysis, parsing, modules and associated filenames\n') ;
WHILE sym # NulSym DO
IF NOT Pass0CheckDef (sym)
THEN
RETURN
END ;
IF NOT Pass0CheckMod (sym, PPSource)
THEN
RETURN
END ;
INC (i) ;
sym := GetModuleNo (i)
END ;
SetPassToNoPass
END DoPass0 ;
(*
DoPass1 - parses the sources of all modules necessary to compile
the required module, Main.
*)
PROCEDURE DoPass1 ;
VAR
name : Name ;
Sym : CARDINAL ;
i : CARDINAL ;
FileName: String ;
BEGIN
P1Init ;
SetPassToPass1 ;
i := 1 ;
Sym := GetModuleNo(i) ;
WHILE Sym#NulSym DO
FileName := GetDefinitionModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
ModuleType := Definition ;
IF NOT P1Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END ;
ModuleType := Implementation
ELSE
ModuleType := Program
END ;
FileName := GetModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
IF NOT P1Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END
END ;
INC(i) ;
Sym := GetModuleNo(i)
END ;
SetPassToNoPass
END DoPass1 ;
(*
DoPass2 - parses the sources of all modules necessary to compile
the required module, Main.
*)
PROCEDURE DoPass2 ;
VAR
name : Name ;
Sym : CARDINAL ;
i : CARDINAL ;
FileName: String ;
BEGIN
SetPassToPass2 ;
i := 1 ;
Sym := GetModuleNo(i) ;
WHILE Sym#NulSym DO
FileName := GetDefinitionModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
ModuleType := Definition ;
IF NOT P2Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END ;
ModuleType := Implementation
ELSE
ModuleType := Program
END ;
FileName := GetModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
IF NOT P2Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END
END ;
INC(i) ;
Sym := GetModuleNo(i)
END ;
SetPassToNoPass
END DoPass2 ;
(*
DoPassC - parses the sources of all modules necessary to compile
the required module, Main.
*)
PROCEDURE DoPassC ;
VAR
name : Name ;
Sym : CARDINAL ;
i : CARDINAL ;
FileName: String ;
BEGIN
SetPassToPassC ;
i := 1 ;
Sym := GetModuleNo(i) ;
WHILE Sym#NulSym DO
FileName := GetDefinitionModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
ModuleType := Definition ;
IF NOT PCBuild.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END ;
ModuleType := Implementation
ELSE
ModuleType := Program
END ;
FileName := GetModuleFile(Sym) ;
IF FileName#NIL
THEN
IF Debugging
THEN
name := GetSymName(Sym) ;
qprintf1(' Module %a\n', name)
END ;
IF OpenSource(FileName)
THEN
IF NOT PCBuild.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END
END ;
INC(i) ;
Sym := GetModuleNo(i)
END ;
PCSymBuild.ResolveConstTypes ;
ResolveConstructorTypes ;
SanityCheckConstants ;
SetPassToNoPass
END DoPassC ;
(*
DoPass3 - parses the sources of all modules necessary to compile
the required module, Main.
*)
PROCEDURE DoPass3 ;
VAR
Main,
Sym : CARDINAL ;
i : CARDINAL ;
FileName: String ;
BEGIN
SetPassToPass3 ;
Main := GetMainModule() ;
i := 1 ;
Sym := GetModuleNo(i) ;
WHILE Sym#NulSym DO
FileName := GetDefinitionModuleFile(Sym) ;
IF FileName#NIL
THEN
IF OpenSource(FileName)
THEN
ModuleType := Definition ;
IF NOT P3Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END ;
ModuleType := Implementation
ELSE
ModuleType := Program
END ;
FileName := GetModuleFile(Sym) ;
IF FileName#NIL
THEN
IF OpenSource(FileName)
THEN
IF (Main=Sym) OR WholeProgram
THEN
IF NOT P3Build.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END
ELSE
(*
not the main module .mod therefore must be implementing
a hidden type - we dont want to generate any
StatementSequence quadrupes but we do want to build TYPEs
and ConstExpressions.
*)
SetPassToNoPass ;
SetPassToPassHidden ;
IF NOT PHBuild.CompilationUnit()
THEN
MetaError0('compilation failed') ;
CloseSource ;
RETURN
END ;
SetPassToNoPass ;
SetPassToPass3
END ;
CloseSource
ELSE
fprintf1(StdErr, 'failed to open %s\n', FileName) ;
exit(1)
END
END ;
INC(i) ;
Sym := GetModuleNo(i)
END ;
SetPassToNoPass
END DoPass3 ;
BEGIN
ModuleType := None ;
DepContent := NIL ;
DepOutput := NIL
END M2Comp.
|