[project @ 1996-01-18 16:33:17 by partain]
[ghc-hetmet.git] / ghc / driver / ghc-split.lprl
1 %************************************************************************
2 %*                                                                      *
3 \section[Driver-obj-splitting]{Splitting into many \tr{.o} files (for libraries)}
4 %*                                                                      *
5 %************************************************************************
6
7 \begin{code}
8 sub inject_split_markers {
9     local($hc_file) = @_;
10
11     unlink("$Tmp_prefix.unmkd");
12     local($to_do) = "cp $hc_file $Tmp_prefix.unmkd";
13     &run_something($to_do, 'Prepare to number split markers');
14
15     open(TMPI, "< $Tmp_prefix.unmkd") || &tidy_up_and_die(1,"$Pgm: failed to open `$Tmp_prefix.unmkd' (to read)\n");
16     open(TMPO, "> $hc_file") || &tidy_up_and_die(1,"$Pgm: failed to open `$hc_file' (to write)\n");
17
18     local($marker_no) = 1;
19
20     # make sure there is a split marker before any "real" code
21     $_ = <TMPI>;
22     while ( $_ ne '' && ( /^$/ || /^#/ ) ) {
23         print TMPO $_;
24         $_ = <TMPI>;
25     }
26     print TMPO "__STG_SPLIT_MARKER(1)\n";
27     print TMPO $_ if ! /^\s*\/\* SPLIT \*\/\s*$/;
28
29         # Have to be a bit careful detecting /* SPLIT */ comments
30         # since a progam may use a string containing "/* SPLIT */"
31         # We check that there is nothing else on the line
32
33     while (<TMPI>) {
34         if (/^\s*\/\* SPLIT \*\/\s*$/) {
35             $marker_no++;
36             print TMPO "__STG_SPLIT_MARKER($marker_no)\n";
37             next;
38         }
39         print TMPO $_;
40     }
41
42     close(TMPI) || &tidy_up_and_die(1,"Failed reading $Tmp_prefix.unmkd\n");
43     close(TMPO) || &tidy_up_and_die(1,"Failed writing $hc_file\n");
44 }
45 \end{code}
46
47 \begin{code}
48 sub split_asm_file {
49     local($asm_file) = @_;
50
51     open(TMPI, "< $asm_file") || &tidy_up_and_die(1,"$Pgm: failed to open `$asm_file' (to read)\n");
52
53     &collectExports_hppa() if $TargetPlatform =~ /^hppa/;
54     &collectExports_mips() if $TargetPlatform =~ /^mips/;
55
56     $octr = 0;  # output file counter
57     $* = 1;     # multi-line matches are OK
58
59     %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
60
61     $s_stuff = &ReadTMPIUpToAMarker( '', $octr );
62     # that first stuff is a prologue for all .s outputs
63     $prologue_stuff = &process_asm_block ( $s_stuff );
64     # $_ already has some of the next stuff in it...
65
66 #   &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
67 #       if $prologue_stuff eq $s_stuff;
68
69     # lie about where this stuff came from
70     $prologue_stuff =~ s|"/tmp/ghc\d+\.c"|"$ifile_root\.hc"|g;
71
72     while ( $_ ne '' ) { # not EOF
73         $octr++;
74
75         # grab and de-mangle a section of the .s file...
76         $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
77         $this_piece = &process_asm_block ( $s_stuff );
78
79         # output to a file of its own
80         # open a new output file...
81         $ofname = "${Tmp_prefix}__${octr}.s";
82         open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
83
84         print OUTF $prologue_stuff;
85         print OUTF $this_piece;
86
87         close(OUTF)
88           || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
89     }
90
91     $NoOfSplitFiles = $octr;
92
93     close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
94 }
95
96 sub collectExports_hppa { # Note: HP-PA only
97
98     %LocalExport = (); # NB: global table
99
100     while(<TMPI>) {
101         if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
102             local($label) = $1;
103             local($body)  = "\t.IMPORT $label";
104             if (/,DATA/) { 
105                 $body .= ",DATA\n"; 
106             } else { 
107                 $body .= ",CODE\n"; 
108             }
109             $label =~ s/\$/\\\$/g;
110             $LocalExport{$label} = $body;
111         }
112     }
113
114     seek(TMPI, 0, 0);
115 }
116
117 sub collectExports_mips { # Note: MIPS only
118     # (not really sure this is necessary [WDP 95/05])
119
120     $UNDEFINED_FUNS = ''; # NB: global table
121
122     while(<TMPI>) {
123         $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
124         # just save 'em all
125     }
126
127     seek(TMPI, 0, 0);
128 }
129
130 sub ReadTMPIUpToAMarker {
131     local($str, $count) = @_; # already read bits
132
133     
134     for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
135         $str .= $_;
136     }
137     # if not EOF, then creep forward until next "real" line
138     # (throwing everything away).
139     # that first "real" line will stay in $_.
140
141     # This loop is intended to pick up the body of the split_marker function
142     # Note that the assembler mangler will already have eliminated this code
143     # if it's been invoked (which it probably has).
144
145     while ($_ ne '' && (/_?__stg_split_marker/
146                      || /^L[^C].*:$/
147                      || /^\.stab/
148                      || /\t\.proc/
149                      || /\t\.stabd/
150                      || /\t\.even/
151                      || /\tunlk a6/
152                      || /^\t!#PROLOGUE/
153                      || /\t\.prologue/
154                      || /\t\.frame/
155                      # || /\t\.end/ NOT!  Let the split_marker regexp catch it
156                      # || /\t\.ent/ NOT!  Let the split_marker regexp catch it
157                      || /^\s+(save|retl?|restore|nop)/)) {
158         $_ = <TMPI>;
159     }
160
161     print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
162
163     # return str
164     $str;
165 }
166 \end{code}
167
168 We must (a)~strip the marker off the block, (b)~record any literal C
169 constants that are defined here, and (c)~inject copies of any C constants
170 that are used-but-not-defined here.
171
172 \begin{code}
173 sub process_asm_block {
174     local($str) = @_;
175
176     return(&process_asm_block_m68k($str))  if $TargetPlatform =~ /^m68k-/;
177     return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
178     return(&process_asm_block_iX86($str))  if $TargetPlatform =~ /^i[34]86-/;
179     return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
180     return(&process_asm_block_hppa($str))  if $TargetPlatform =~ /^hppa/;
181     return(&process_asm_block_mips($str))   if $TargetPlatform =~ /^mips-/;
182
183     # otherwise...
184     &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
185 }
186
187 sub process_asm_block_sparc {
188     local($str) = @_;
189
190     # strip the marker
191     if ( $OptimiseC ) {
192         $str =~ s/_?__stg_split_marker.*:\n//;
193     } else {
194         $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/\1/;
195         $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/\1/;
196     }
197
198     # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
199     $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
200
201     # remove/record any literal constants defined here
202     while ( $str =~ /(\t\.align .\n(LC\d+):\n(\t\.ascii.*\n)+)/ ) {
203         local($label) = $2;
204         local($body)  = $1;
205
206         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
207             if $LocalConstant{$label};
208
209         $LocalConstant{$label} = $body;
210         
211         $str =~ s/\t\.align .\nLC\d+:\n(\t\.ascii.*\n)+//;
212     }
213
214     # inject definitions for any local constants now used herein
215     foreach $k (keys %LocalConstant) {
216         if ( $str =~ /\b$k\b/ ) {
217             $str = $LocalConstant{$k} . $str;
218         }
219     }
220
221    print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
222
223    $str;
224 }
225
226 sub process_asm_block_m68k {
227     local($str) = @_;
228
229     # strip the marker (ToDo: something special for unregisterized???)
230
231     $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/\1/;
232     $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/\1/;
233
234     # it seems prudent to stick on one of these:
235     $str = "\.text\n\t.even\n" . $str;
236
237     # remove/record any literal constants defined here
238     while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
239         local($label) = $2;
240         local($body)  = $1;
241
242         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
243             if $LocalConstant{$label};
244
245         $LocalConstant{$label} = $body;
246         
247         $str =~ s/LC\d+:\n\t\.ascii.*\n//;
248     }
249
250     # inject definitions for any local constants now used herein
251     foreach $k (keys %LocalConstant) {
252         if ( $str =~ /\b$k\b/ ) {
253             $str = $LocalConstant{$k} . $str;
254         }
255     }
256
257    print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
258
259    $str;
260 }
261
262 sub process_asm_block_alpha {
263     local($str) = @_;
264
265     # strip the marker
266     if ( $OptimiseC ) {
267         $str =~ s/_?__stg_split_marker.*:\n//;
268     } else {
269         $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/\1/;
270     }
271
272     # remove/record any literal constants defined here
273     while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
274         local($label) = $3;
275         local($body)  = $2;
276
277         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
278             if $LocalConstant{$label};
279
280         $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
281         
282         $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
283     }
284
285     # inject definitions for any local constants now used herein
286     foreach $k (keys %LocalConstant) {
287         if ( $str =~ /\$\b$k\b/ ) {
288             $str = $LocalConstant{$k} . $str;
289         }
290     }
291
292     # Slide the dummy direct return code into the vtbl .ent/.end block,
293     # to keep the label fixed if it's the last thing in a module, and
294     # to avoid having any anonymous text that the linker will complain about
295     $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n\1/g;
296
297     print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
298
299     $str;
300 }
301
302 sub process_asm_block_iX86 {
303     local($str) = @_;
304
305     # strip the marker (ToDo: something special for unregisterized???)
306
307     $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/\1/;
308     $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/\1/;
309
310     # it seems prudent to stick on one of these:
311     $str = "\.text\n\t.align 4\n" . $str;
312
313     # remove/record any literal constants defined here
314     while ( ($str =~ /((LC\d+):\n\t\.ascii.*\n)/ )) {
315         local($label) = $2;
316         local($body)  = $1;
317
318         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
319             if $LocalConstant{$label};
320
321         $LocalConstant{$label} = $body;
322         
323         $str =~ s/LC\d+:\n\t\.ascii.*\n//;
324     }
325
326     # inject definitions for any local constants now used herein
327     foreach $k (keys %LocalConstant) {
328         if ( $str =~ /\b$k\b/ ) {
329             $str = $LocalConstant{$k} . $str;
330         }
331     }
332
333    print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
334
335    $str;
336 }
337 \end{code}
338
339 \begin{code}
340 sub process_asm_block_hppa {
341     local($str) = @_;
342
343     # strip the marker
344     $str =~ s/___stg_split_marker.*\n//;
345
346     # remove/record any imports defined here
347     while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
348         $Imports .= $1;
349
350         $str =~ s/^\s+\.IMPORT.*\n//;
351     }
352
353     # remove/record any literal constants defined here
354     while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
355         local($label) = $2;
356         local($body)  = $1;
357         $label =~ s/\$/\\\$/g;
358
359         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
360             if $LocalConstant{$label};
361
362         $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
363         
364         $str =~ s/^\s+\.SPACE \$TEXT\$\n\s+\.SUBSPA \$LIT\$\s+\.align.*\nL\$C\d+\n(\s.*\n)+; end literal\n//;
365     }
366
367     # inject definitions for any local constants now used herein
368     foreach $k (keys %LocalConstant) {
369         if ( $str =~ /\b$k\b/ ) {
370             $str = $LocalConstant{$k} . $str;
371         }
372     }
373
374     # inject required imports for local exports in other chunks
375     foreach $k (keys %LocalExport) {
376         if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
377             $str = $LocalExport{$k} . $str;
378         }
379     }
380
381     # inject collected imports
382
383     $str = $Imports . $str;
384
385     print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
386
387     $str;
388 }
389 \end{code}
390
391 \begin{code}
392 sub process_asm_block_mips {
393     local($str) = @_;
394
395     # strip the marker
396     if ( $OptimiseC ) {
397         $str =~ s/_?__stg_split_marker.*:\n//;
398     } else {
399         $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/\1/;
400     }
401
402     # remove/record any literal constants defined here
403     while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
404         local($label) = $3;
405         local($body)  = $2;
406
407         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
408             if $LocalConstant{$label};
409
410         $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
411         
412         $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
413     }
414
415     # inject definitions for any local constants now used herein
416     foreach $k (keys %LocalConstant) {
417         if ( $str =~ /\$\b$k\b/ ) {
418             $str = $LocalConstant{$k} . $str;
419         }
420     }
421
422     # Slide the dummy direct return code into the vtbl .ent/.end block,
423     # to keep the label fixed if it's the last thing in a module, and
424     # to avoid having any anonymous text that the linker will complain about
425     $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n\1/g;
426
427     $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
428
429     print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
430
431     $str;
432 }
433 \end{code}
434
435 \begin{code}
436 # make "require"r happy...
437 1;
438 \end{code}
439