Debian Codegraph @

gcc/m2/gm2-compiler/M2Code.mod

raw
1 (* M2Code.mod coordinate the activity of the front end.
2
3 Copyright (C) 2001-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 M2Code ;
23
24
25 FROM SYSTEM IMPORT WORD ;
26 FROM M2Options IMPORT Statistics, OptimizeUncalledProcedures,
27 OptimizeCommonSubExpressions,
28 StyleChecking, Optimizing, WholeProgram,
29 GetDumpDecl, GetDumpGimple ;
30
31 FROM M2LangDump IMPORT CreateDumpDecl, CloseDumpDecl, MakeGimpleTemplate ;
32 FROM M2Error IMPORT InternalError ;
33 FROM M2Students IMPORT StudentVariableCheck ;
34
35 FROM SymbolTable IMPORT GetMainModule, IsProcedure,
36 IsModuleWithinProcedure,
37 CheckHiddenTypeAreAddress, IsModule, IsDefImp,
38 DebugLineNumbers,
39 ForeachProcedureDo,
40 ForeachInnerModuleDo, GetSymName ;
41
42 FROM M2Printf IMPORT printf2, printf1, printf0 ;
43 FROM NameKey IMPORT Name ;
44 FROM M2Batch IMPORT ForeachSourceModuleDo ;
45
46 FROM M2Quads IMPORT CountQuads, GetFirstQuad,
47 DumpQuadruples, DisplayQuadRange,
48 BackPatchSubrangesAndOptParam,
49 LoopAnalysis, ForLoopAnalysis, GetQuad, QuadOperator ;
50
51 FROM M2SymInit IMPORT ScopeBlockVariableAnalysis ;
52
53 FROM M2Pass IMPORT SetPassToNoPass, SetPassToCodeGeneration ;
54
55 FROM M2BasicBlock IMPORT BasicBlock,
56 InitBasicBlocks, InitBasicBlocksFromRange,
57 KillBasicBlocks, FreeBasicBlocks,
58 ForeachBasicBlockDo ;
59
60 FROM M2Optimize IMPORT FoldBranches, RemoveProcedures ;
61 FROM M2GenGCC IMPORT ConvertQuadsToTree ;
62
63 FROM M2GCCDeclare IMPORT FoldConstants, StartDeclareScope,
64 DeclareProcedure, InitDeclarations,
65 DeclareModuleVariables, MarkExported,
66 DumpFilteredResolver, DumpFilteredDefinitive ;
67
68 FROM M2Scope IMPORT ScopeBlock, InitScopeBlock, KillScopeBlock,
69 ForeachScopeBlockDo2, ForeachScopeBlockDo3 ;
70
71 FROM m2top IMPORT StartGlobalContext, EndGlobalContext, SetFlagUnitAtATime ;
72 FROM M2Error IMPORT FlushErrors, FlushWarnings ;
73 FROM M2Swig IMPORT GenerateSwigFile ;
74 FROM m2flex IMPORT GetTotalLines ;
75 FROM FIO IMPORT FlushBuffer, StdOut ;
76 FROM M2Quiet IMPORT qprintf0 ;
77 FROM M2SSA IMPORT DiscoverSSA ;
78 FROM m2pp IMPORT CreateDumpGimple, CloseDumpGimple ;
79 FROM DynamicStrings IMPORT String, KillString ;
80
81
82 CONST
83 MaxOptimTimes = 10 ; (* upper limit of no of times we run through all optimization *)
84 Debugging = TRUE ;
85 TraceQuadruples = FALSE ;
86
87 VAR
88 Total,
89 Count,
90 OptimTimes,
91 DeltaProc,
92 Proc,
93 DeltaConst,
94 Const,
95 DeltaJump,
96 Jump,
97 DeltaBasicB,
98 BasicB : CARDINAL ;
99
100
101 (*
102 Percent - calculates the percentage from numerator and divisor
103 *)
104
105 PROCEDURE Percent (numerator, divisor: CARDINAL) ;
106 VAR
107 value: CARDINAL ;
108 BEGIN
109 printf0 (' (') ;
110 IF divisor=0
111 THEN
112 printf0 ('overflow error')
113 ELSE
114 value := numerator*100 DIV divisor ;
115 printf1 ('%3d', value)
116 END ;
117 printf0 ('\%)')
118 END Percent ;
119
120
121 (*
122 OptimizationAnalysis - displays some simple front end optimization statistics.
123 *)
124
125 PROCEDURE OptimizationAnalysis ;
126 VAR
127 value: CARDINAL ;
128 BEGIN
129 IF Statistics
130 THEN
131 Count := CountQuads() ;
132
133 printf1 ('M2 initial number of quadruples: %6d', Total) ;
134 Percent (Total, Total) ; printf0 ('\n');
135 printf1 ('M2 constant folding achieved : %6d', Const) ;
136 Percent (Const, Total) ; printf0 ('\n');
137 printf1 ('M2 branch folding achieved : %6d', Jump) ;
138 Percent (Jump, Total) ; printf0 ('\n');
139 value := Const+Jump+Proc ;
140 printf1 ('Front end optimization removed : %6d', value) ;
141 Percent (value, Total) ; printf0 ('\n') ;
142 printf1 ('Front end final : %6d', Count) ;
143 Percent (Count, Total) ; printf0 ('\n') ;
144 Count := GetTotalLines () ;
145 printf1 ('Total source lines compiled : %6d\n', Count) ;
146 FlushBuffer (StdOut)
147 END ;
148 DumpQuadruples ('after all front end optimization\n')
149 END OptimizationAnalysis ;
150
151
152 (*
153 RemoveUnreachableCode -
154 *)
155
156 PROCEDURE RemoveUnreachableCode ;
157 BEGIN
158 IF WholeProgram
159 THEN
160 ForeachSourceModuleDo(RemoveProcedures)
161 ELSE
162 RemoveProcedures(GetMainModule())
163 END
164 END RemoveUnreachableCode ;
165
166
167 (*
168 DoModuleDeclare - declare all constants, types, variables, procedures for the
169 main module or all modules.
170 *)
171
172 PROCEDURE DoModuleDeclare ;
173 BEGIN
174 IF GetDumpDecl ()
175 THEN
176 CreateDumpDecl ("symbol resolver of filtered symbols\n") ;
177 DumpFilteredResolver
178 END ;
179 IF WholeProgram
180 THEN
181 ForeachSourceModuleDo (StartDeclareScope)
182 ELSE
183 StartDeclareScope (GetMainModule ())
184 END ;
185 IF GetDumpDecl ()
186 THEN
187 CloseDumpDecl ;
188 CreateDumpDecl ("definitive declaration of filtered symbols\n") ;
189 DumpFilteredDefinitive ;
190 CloseDumpDecl
191 END
192 END DoModuleDeclare ;
193
194
195 (*
196 PrintModule -
197 *)
198
199 (*
200 PROCEDURE PrintModule (sym: CARDINAL) ;
201 VAR
202 n: Name ;
203 BEGIN
204 n := GetSymName (sym) ;
205 printf1 ('module %a\n', n)
206 END PrintModule ;
207 *)
208
209
210 (*
211 DoCodeBlock - generate code for the main module or all modules.
212 *)
213
214 PROCEDURE DoCodeBlock ;
215 VAR
216 filename: String ;
217 len : CARDINAL ;
218 BEGIN
219 IF GetDumpGimple ()
220 THEN
221 filename := MakeGimpleTemplate (len) ;
222 CreateDumpGimple (filename, len) ;
223 filename := KillString (filename) ;
224 CodeBlock (GetMainModule ()) ;
225 CloseDumpGimple
226 ELSE
227 CodeBlock (GetMainModule ())
228 END
229 END DoCodeBlock ;
230
231
232 (*
233 DetermineSubExpTemporaries -
234 *)
235
236 PROCEDURE DetermineSubExpTemporaries ;
237 BEGIN
238 IF WholeProgram
239 THEN
240 ForeachSourceModuleDo (DiscoverSSA)
241 ELSE
242 DiscoverSSA (GetMainModule ())
243 END
244 END DetermineSubExpTemporaries ;
245
246
247 (*
248 Code - calls procedures to generates trees from the quadruples.
249 All front end quadruple optimization is performed via this call.
250 *)
251
252 PROCEDURE Code ;
253 BEGIN
254 DumpQuadruples ('before any optimization\n') ;
255 CheckHiddenTypeAreAddress ;
256 SetPassToNoPass ;
257 BackPatchSubrangesAndOptParam ;
258 Total := CountQuads () ;
259
260 ForLoopAnalysis ; (* must be done before any optimization as the index variable increment quad might change *)
261
262 DumpQuadruples ('before declaring symbols to gcc\n') ;
263
264 (* now is a suitable time to check for student errors as *)
265 (* we know all the front end symbols must be resolved. *)
266
267 IF StyleChecking
268 THEN
269 StudentVariableCheck
270 END ;
271
272 SetPassToCodeGeneration ;
273 SetFlagUnitAtATime (Optimizing) ;
274 StartGlobalContext ;
275 InitDeclarations ; (* default and fixed sized types are all declared from now on. *)
276
277 RemoveUnreachableCode ;
278 DumpQuadruples ('after dead procedure elimination\n') ;
279 DetermineSubExpTemporaries ;
280 DumpQuadruples ('after identifying simple subexpression temporaries\n') ;
281
282 qprintf0 (' symbols to gcc trees\n') ;
283 DoModuleDeclare ;
284
285 FlushWarnings ;
286 FlushErrors ;
287 qprintf0 (' statements to gcc trees\n') ;
288 DoCodeBlock ;
289
290 MarkExported (GetMainModule ()) ;
291 GenerateSwigFile (GetMainModule ()) ;
292 DebugLineNumbers (GetMainModule ()) ;
293 qprintf0 (' gcc trees given to the gcc backend\n') ;
294 EndGlobalContext ;
295
296 OptimizationAnalysis
297 END Code ;
298
299
300 (*
301 InitialDeclareAndCodeBlock - declares all objects within scope,
302 *)
303
304 PROCEDURE InitialDeclareAndOptimize (scope: CARDINAL; start, end: CARDINAL) ;
305 BEGIN
306 Count := CountQuads () ;
307 FreeBasicBlocks (InitBasicBlocksFromRange (scope, start, end)) ;
308 BasicB := Count - CountQuads () ;
309 Count := CountQuads () ;
310
311 FoldBranches (start, end) ;
312 Jump := Count - CountQuads () ;
313 Count := CountQuads ()
314 END InitialDeclareAndOptimize ;
315
316
317 (*
318 DeclareAndCodeBlock - declares all objects within scope,
319 *)
320
321 PROCEDURE SecondDeclareAndOptimize (scope: CARDINAL;
322 start, end: CARDINAL) ;
323 VAR
324 bb: BasicBlock ;
325 BEGIN
326 REPEAT
327 bb := InitBasicBlocksFromRange (scope, start, end) ;
328 ForeachBasicBlockDo (bb, FoldConstants) ;
329 FreeBasicBlocks (bb) ;
330
331 DeltaConst := Count - CountQuads () ;
332 Count := CountQuads () ;
333
334 FreeBasicBlocks(InitBasicBlocksFromRange (scope, start, end)) ;
335
336 DeltaBasicB := Count - CountQuads () ;
337 Count := CountQuads () ;
338
339 FreeBasicBlocks (InitBasicBlocksFromRange (scope, start, end)) ;
340 FoldBranches(start, end) ;
341 DeltaJump := Count - CountQuads () ;
342 Count := CountQuads () ;
343
344 FreeBasicBlocks(InitBasicBlocksFromRange (scope, start, end)) ;
345 INC (DeltaBasicB, Count - CountQuads ()) ;
346 Count := CountQuads () ;
347
348 (* now total the optimization components *)
349 INC (Proc, DeltaProc) ;
350 INC (Const, DeltaConst) ;
351 INC (Jump, DeltaJump) ;
352 INC (BasicB, DeltaBasicB)
353 UNTIL (OptimTimes>=MaxOptimTimes) OR
354 ((DeltaProc=0) AND (DeltaConst=0) AND (DeltaJump=0) AND (DeltaBasicB=0)) ;
355
356 IF (DeltaProc#0) OR (DeltaConst#0) OR (DeltaJump#0) OR (DeltaBasicB#0)
357 THEN
358 printf0 ('optimization finished although more reduction may be possible (increase MaxOptimTimes)\n')
359 END
360 END SecondDeclareAndOptimize ;
361
362
363 (*
364 InitOptimizeVariables -
365 *)
366
367 PROCEDURE InitOptimizeVariables ;
368 BEGIN
369 Count := CountQuads () ;
370 OptimTimes := 0 ;
371 DeltaProc := 0 ;
372 DeltaConst := 0 ;
373 DeltaJump := 0 ;
374 DeltaBasicB := 0
375 END InitOptimizeVariables ;
376
377
378 (*
379 Init -
380 *)
381
382 PROCEDURE Init ;
383 BEGIN
384 Proc := 0 ;
385 Const := 0 ;
386 Jump := 0 ;
387 BasicB := 0
388 END Init ;
389
390
391 (*
392 OptimizeScopeBlock -
393 *)
394
395 PROCEDURE OptimizeScopeBlock (sb: ScopeBlock) ;
396 VAR
397 OptimTimes,
398 Previous,
399 Current : CARDINAL ;
400 BEGIN
401 InitOptimizeVariables ;
402 OptimTimes := 1 ;
403 Current := CountQuads () ;
404 ForeachScopeBlockDo3 (sb, InitialDeclareAndOptimize) ;
405 ForeachScopeBlockDo3 (sb, ScopeBlockVariableAnalysis) ;
406 REPEAT
407 ForeachScopeBlockDo3 (sb, SecondDeclareAndOptimize) ;
408 Previous := Current ;
409 Current := CountQuads () ;
410 INC (OptimTimes)
411 UNTIL (OptimTimes=MaxOptimTimes) OR (Current=Previous) ;
412 ForeachScopeBlockDo3 (sb, LoopAnalysis)
413 END OptimizeScopeBlock ;
414
415
416 (*
417 CodeProceduresWithinBlock - codes the procedures within the module scope.
418 *)
419
420 PROCEDURE CodeProceduresWithinBlock (scope: CARDINAL) ;
421 BEGIN
422 ForeachProcedureDo (scope, CodeBlock)
423 END CodeProceduresWithinBlock ;
424
425
426 (*
427 CodeProcedures -
428 *)
429
430 PROCEDURE CodeProcedures (scope: CARDINAL) ;
431 BEGIN
432 IF IsDefImp (scope) OR IsModule (scope)
433 THEN
434 ForeachProcedureDo (scope, CodeBlock)
435 END
436 END CodeProcedures ;
437
438
439 (*
440 CodeBlock - generates all code for this block and also declares
441 all types and procedures for this block. It will
442 also optimize quadruples within this scope.
443 *)
444
445 PROCEDURE CodeBlock (scope: WORD) ;
446 VAR
447 sb: ScopeBlock ;
448 n : Name ;
449 BEGIN
450 IF TraceQuadruples
451 THEN
452 n := GetSymName (scope) ;
453 printf1 ('before coding block %a\n', n)
454 END ;
455 sb := InitScopeBlock (scope) ;
456 OptimizeScopeBlock (sb) ;
457 IF IsProcedure (scope)
458 THEN
459 IF TraceQuadruples
460 THEN
461 n := GetSymName(scope) ;
462 printf1('before coding procedure %a\n', n) ;
463 ForeachScopeBlockDo3 (sb, DisplayQuadRange) ;
464 printf0('===============\n')
465 END ;
466 ForeachScopeBlockDo2 (sb, ConvertQuadsToTree)
467 ELSIF IsModuleWithinProcedure(scope)
468 THEN
469 IF TraceQuadruples
470 THEN
471 n := GetSymName(scope) ;
472 printf1('before coding module %a within procedure\n', n) ;
473 ForeachScopeBlockDo3 (sb, DisplayQuadRange) ;
474 printf0('===============\n')
475 END ;
476 ForeachScopeBlockDo2 (sb, ConvertQuadsToTree) ;
477 ForeachProcedureDo(scope, CodeBlock)
478 ELSE
479 IF TraceQuadruples
480 THEN
481 n := GetSymName(scope) ;
482 printf1('before coding module %a\n', n) ;
483 ForeachScopeBlockDo3 (sb, DisplayQuadRange) ;
484 printf0('===============\n')
485 END ;
486 ForeachScopeBlockDo2 (sb, ConvertQuadsToTree) ;
487 IF WholeProgram
488 THEN
489 ForeachSourceModuleDo(CodeProcedures)
490 ELSE
491 ForeachProcedureDo(scope, CodeBlock)
492 END ;
493 ForeachInnerModuleDo(scope, CodeProceduresWithinBlock)
494 END ;
495 KillScopeBlock(sb)
496 END CodeBlock ;
497
498
499 BEGIN
500 Init
501 END M2Code.