Debian Codegraph @

gcc/m2/gm2-compiler/gm2lorder.mod

raw
1 (* gm2lorder.mod ensure that underlying runtime modules are initialized.
2
3 Copyright (C) 2008-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 MODULE gm2lorder ;
23
24 (*
25 Author : Gaius Mulley
26 Title : gm2lorder
27 Date : Thu Sep 4 21:18:33 BST 2008
28 Description: Ensures that underlying runtime modules are initialized
29 before all other modules.
30 *)
31
32
33 FROM libc IMPORT exit ;
34 FROM ASCII IMPORT eof ;
35 FROM SArgs IMPORT GetArg ;
36 FROM StrLib IMPORT StrLen ;
37 FROM DynamicStrings IMPORT String, InitString, KillString, Length, Equal, EqualArray, Slice, Mark ;
38 FROM Indexing IMPORT Index, PutIndice, GetIndice, RemoveIndiceFromIndex, InitIndex, KillIndex, HighIndice ;
39 FROM FIO IMPORT File, StdIn, StdOut, StdErr, WriteChar, WriteLine,
40 Close, EOF, IsNoError, WriteString, FlushBuffer ;
41 FROM SFIO IMPORT OpenToRead, OpenToWrite, ReadS, WriteS ;
42
43 IMPORT DynamicStrings ;
44
45
46 CONST
47 Comment = '#' ; (* Comment identifier. *)
48 DefaultRuntimeModules = 'Storage,SYSTEM,M2RTS,RTExceptions,IOLink' ;
49
50 VAR
51 fi, fo : File ;
52 runTime : Index ;
53 moduleList: Index ;
54
55
56 (*
57 InitRuntimeModules - initializes the list of critical runtime modules
58 which must be initialized first and in a particular
59 order.
60 *)
61
62 PROCEDURE InitRuntimeModules (s: String) ;
63 VAR
64 a : CARDINAL ;
65 i, j: INTEGER ;
66 BEGIN
67 IF runTime # NIL
68 THEN
69 runTime := KillIndex (runTime)
70 END ;
71 runTime := InitIndex (0) ;
72 i := 0 ;
73 a := 0 ;
74 REPEAT
75 j := DynamicStrings.Index (s, ',', i) ;
76 IF j = -1
77 THEN
78 PutIndice (runTime, a, Slice (s, i, 0))
79 ELSE
80 PutIndice (runTime, a, Slice (s, i, j)) ;
81 i := j+1
82 END ;
83 INC(a)
84 UNTIL j=-1 ;
85 s := KillString (s)
86 END InitRuntimeModules ;
87
88
89 (*
90 Reorder - reorders the list of modules to ensure critical runtime
91 modules are initialized first. It writes out the new
92 ordered list.
93 *)
94
95 PROCEDURE Reorder ;
96 VAR
97 rh, mh,
98 ri, mi: CARDINAL ;
99 rs, ms: String ;
100 BEGIN
101 rh := HighIndice (runTime) ;
102 mh := HighIndice (moduleList) ;
103 ri := 0 ;
104 WHILE ri <= rh DO
105 mi := 0 ;
106 rs := GetIndice (runTime, ri) ;
107 WHILE mi <= mh DO
108 ms := GetIndice (moduleList, mi) ;
109 IF Equal (rs, ms)
110 THEN
111 rs := WriteS (fo, rs) ; WriteLine (fo) ;
112 RemoveIndiceFromIndex (moduleList, ms) ;
113 mh := HighIndice (moduleList)
114 ELSE
115 INC (mi)
116 END
117 END ;
118 INC (ri)
119 END ;
120 mi := 0 ;
121 WHILE mi <= mh DO
122 ms := GetIndice (moduleList, mi) ;
123 ms := WriteS (fo, ms) ; WriteLine (fo) ;
124 INC (mi)
125 END ;
126 Close (fo)
127 END Reorder ;
128
129
130 (*
131 ReadList - populates the moduleList with a list of module names.
132 *)
133
134 PROCEDURE ReadList ;
135 VAR
136 s: String ;
137 i: CARDINAL ;
138 BEGIN
139 moduleList := InitIndex (0) ;
140 i := 0 ;
141 s := ReadS (fi) ;
142 WHILE NOT EOF (fi) DO
143 IF NOT EqualArray (s, '')
144 THEN
145 PutIndice (moduleList, i, s) ;
146 INC (i)
147 END ;
148 s := ReadS (fi)
149 END ;
150 IF NOT EqualArray (s, '')
151 THEN
152 PutIndice (moduleList, i, s)
153 END
154 END ReadList ;
155
156
157 (*
158 OpenOutputFile - attempts to open an output file.
159 *)
160
161 PROCEDURE OpenOutputFile (s: String) ;
162 BEGIN
163 IF EqualArray(s, '-')
164 THEN
165 fo := StdOut
166 ELSE
167 fo := OpenToWrite(s) ;
168 IF NOT IsNoError(fo)
169 THEN
170 WriteString(StdErr, 'cannot write to: ') ; s := WriteS(StdErr, s) ; WriteLine(StdErr) ;
171 exit(1)
172 END
173 END
174 END OpenOutputFile ;
175
176
177 (*
178 Usage - prints out a usage and exits with 0.
179 *)
180
181 PROCEDURE Usage ;
182 BEGIN
183 WriteString(StdOut, 'gm2lorder [-h] [-o outputfile] [-fruntime-modules=] inputfile') ; WriteLine(StdOut) ;
184 WriteString(StdOut, ' inputfile is a file containing a list of modules, each module on a separate line') ; WriteLine(StdOut) ;
185 WriteString(StdOut, ' the list of runtime modules can be specified as follows -fruntime-modules=module1,module2,module3') ; WriteLine(StdOut) ;
186 WriteString(StdOut, ' the default for this flag is -fruntime-modules=') ;
187 WriteString(StdOut, DefaultRuntimeModules) ; WriteLine(StdOut) ;
188 WriteString(StdOut, ' Note that the list of runtime modules does not mean they will appear in the executable') ; WriteLine(StdOut) ;
189 WriteString(StdOut, ' a runtime module is only included into the final executable if it is required,') ; WriteLine(StdOut) ;
190 WriteString(StdOut, ' however gm2lorder will ensure the order of these modules.') ; WriteLine(StdOut) ;
191 FlushBuffer(StdOut) ;
192 exit(0)
193 END Usage ;
194
195
196 (*
197 ScanArgs - scans arguments.
198 *)
199
200 PROCEDURE ScanArgs ;
201 VAR
202 i : CARDINAL ;
203 s : String ;
204 FoundFile: BOOLEAN ;
205 BEGIN
206 FoundFile := FALSE ;
207 fi := StdIn ;
208 fo := StdOut ;
209 i := 1 ;
210 WHILE GetArg(s, i) DO
211 IF EqualArray(s, '-o')
212 THEN
213 s := KillString(s) ;
214 INC(i) ;
215 IF GetArg(s, i)
216 THEN
217 OpenOutputFile(s)
218 ELSE
219 WriteString(StdErr, 'missing filename option after -o') ; WriteLine(StdErr) ;
220 exit(1)
221 END
222 ELSIF EqualArray(s, '-h')
223 THEN
224 Usage
225 ELSIF EqualArray(Mark(Slice(s, 0, StrLen('-fruntime-modules='))), '-fruntime-modules=')
226 THEN
227 InitRuntimeModules(Slice(s, StrLen('-fruntime-modules='), 0))
228 ELSE
229 IF FoundFile
230 THEN
231 WriteString(StdErr, 'already opened one file for reading') ; WriteLine(StdErr)
232 ELSE
233 FoundFile := TRUE ;
234 fi := OpenToRead(s) ;
235 IF NOT IsNoError(fi)
236 THEN
237 WriteString(StdErr, 'failed to open: ') ; s := WriteS(StdErr, s) ; WriteLine(StdErr) ;
238 exit(1)
239 END
240 END
241 END ;
242 INC(i)
243 END ;
244 IF NOT FoundFile
245 THEN
246 WriteString(StdErr, 'a module file list must be specified on the command line') ; WriteLine(StdErr) ;
247 exit(1)
248 END
249 END ScanArgs ;
250
251
252 (*
253 Init -
254 *)
255
256 PROCEDURE Init ;
257 BEGIN
258 runTime := NIL ;
259 moduleList := NIL ;
260 InitRuntimeModules(InitString(DefaultRuntimeModules)) ;
261 ScanArgs ;
262 ReadList ;
263 Reorder
264 END Init ;
265
266
267 BEGIN
268 Init
269 END gm2lorder.