195edf3512a47eee27c9ae06a36054f59e20e5fa
[ghc-hetmet.git] / ghc / driver / split / 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 $TargetPlatform = $TARGETPLATFORM;
9
10 ($Pgm = $0) =~ s|.*/||;
11 $ifile      = $ARGV[0];
12 $Tmp_prefix = $ARGV[1];
13 $Output     = $ARGV[2];
14
15 &split_asm_file($ifile);
16
17 open(OUTPUT, "> $Output") ||  &tidy_up_and_die(1,"$Pgm: failed to open `$Output' (to write)\n");
18 print OUTPUT "$NoOfSplitFiles\n";
19 close(OUTPUT);
20
21 exit(0);
22 \end{code}
23
24
25 \begin{code}
26 sub split_asm_file {
27     local($asm_file) = @_;
28
29     open(TMPI, "< $asm_file") || &tidy_up_and_die(1,"$Pgm: failed to open `$asm_file' (to read)\n");
30
31     &collectExports_hppa() if $TargetPlatform =~ /^hppa/;
32     &collectExports_mips() if $TargetPlatform =~ /^mips/;
33     &collectDyldStuff_powerpc() if $TargetPlatform =~ /^powerpc-apple/;
34
35     $octr = 0;  # output file counter
36     $* = 1;     # multi-line matches are OK
37
38     %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
39
40     $s_stuff = &ReadTMPIUpToAMarker( '', $octr );
41     # that first stuff is a prologue for all .s outputs
42     $prologue_stuff = &process_asm_block ( $s_stuff );
43     # $_ already has some of the next stuff in it...
44
45 #   &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
46 #       if $prologue_stuff eq $s_stuff;
47
48     # lie about where this stuff came from
49     $prologue_stuff =~ s|"${Tmp_prefix}\.c"|"$ifile_root\.hc"|g;
50
51     while ( $_ ne '' ) { # not EOF
52         $octr++;
53
54         # grab and de-mangle a section of the .s file...
55         $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
56         $this_piece = &process_asm_block ( $s_stuff );
57
58         # output to a file of its own
59         # open a new output file...
60         $ofname = "${Tmp_prefix}__${octr}.s";
61         open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
62
63         print OUTF $prologue_stuff;
64         print OUTF $this_piece;
65
66         close(OUTF)
67           || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
68     }
69
70     $NoOfSplitFiles = $octr;
71
72     close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
73 }
74
75 sub collectExports_hppa { # Note: HP-PA only
76
77     %LocalExport = (); # NB: global table
78
79     while(<TMPI>) {
80         if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
81             local($label) = $1;
82             local($body)  = "\t.IMPORT $label";
83             if (/,DATA/) { 
84                 $body .= ",DATA\n"; 
85             } else { 
86                 $body .= ",CODE\n"; 
87             }
88             $label =~ s/\$/\\\$/g;
89             $LocalExport{$label} = $body;
90         }
91     }
92
93     seek(TMPI, 0, 0);
94 }
95
96 sub collectExports_mips { # Note: MIPS only
97     # (not really sure this is necessary [WDP 95/05])
98
99     $UNDEFINED_FUNS = ''; # NB: global table
100
101     while(<TMPI>) {
102         $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
103         # just save 'em all
104     }
105
106     seek(TMPI, 0, 0);
107 }
108
109 sub collectDyldStuff_powerpc { # Note: Darwin/PowerPC only
110     local($chunk_label,$label,$cur_section,$section,$chunk,$alignment,$cur_alignment);
111     
112     %DyldChunks = (); # NB: global table
113     %DyldChunksDefined = (); # NB: global table
114         
115     $cur_section = '';
116     $section = '';
117     $label = '';
118     $chunk = '';
119     $alignment = '';
120     $cur_alignment = '';
121     
122     while ( 1 ) {
123         $_ = <TMPI>;
124         if ( $_ eq '' || /^L(_.+)\$.+:/ ) {
125             if ( $label ne '' ) {
126                 $DyldChunksDefined{$label} .= $section . $chunk_label . $alignment . $ chunk;
127                 if( $section =~ s/\.data/\.non_lazy_symbol_pointer/ ) {
128                     $chunk = "\t.indirect_symbol $label\n\t.long 0\n";
129                 }
130                 $DyldChunks{$label} .= $section . $chunk_label . $chunk;
131                 # don't use $alignment, it's only needed for .data, which we change into .non_lazy_symbol_pointer
132                 print STDERR "### dyld chunk: $label\n$section$alignment$chunk\n###\n" if $Dump_asm_splitting_info;
133             }
134             last if ($_ eq '');
135                 
136             $chunk = '';
137             $chunk_label = $_;
138             $label = $1;
139             $section = $cur_section;
140             $alignment = $cur_alignment;
141             print STDERR "label: $label\n" if $Dump_asm_splitting_info;
142         } elsif ( /^\s*\.(symbol_stub|picsymbol_stub|lazy_symbol_pointer|non_lazy_symbol_pointer|data)/ ) {
143             $cur_section = $_;
144             printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
145             $cur_alignment = ''
146         } elsif ( /^\s*\.section\s+__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,\d+/ ) {
147             $cur_section = $_;
148             printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
149             $cur_alignment = ''
150         } elsif ( /^\s*\.align.*/ ) { 
151             $cur_alignment = $_;
152             printf STDERR "alignment: $cur_alignment\n" if $Dump_asm_splitting_info;
153         } else {
154             $chunk .= $_;
155         }
156     }
157     
158     seek(TMPI, 0, 0);
159 }
160
161 sub ReadTMPIUpToAMarker {
162     local($str, $count) = @_; # already read bits
163
164     
165     for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
166         $str .= $_;
167     }
168     # if not EOF, then creep forward until next "real" line
169     # (throwing everything away).
170     # that first "real" line will stay in $_.
171
172     # This loop is intended to pick up the body of the split_marker function
173     # Note that the assembler mangler will already have eliminated this code
174     # if it's been invoked (which it probably has).
175
176     while ($_ ne '' && (/_?__stg_split_marker/
177                      || /^L[^C].*:$/
178                      || /^\.stab/
179                      || /\t\.proc/
180                      || /\t\.stabd/
181                      || /\t\.even/
182                      || /\tunlk a6/
183                      || /^\t!#PROLOGUE/
184                      || /\t\.prologue/
185                      || /\t\.frame/
186                      # || /\t\.end/ NOT!  Let the split_marker regexp catch it
187                      # || /\t\.ent/ NOT!  Let the split_marker regexp catch it
188                      || /^\s+(save|retl?|restore|nop)/)) {
189         $_ = <TMPI>;
190     }
191
192     print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
193
194     # return str
195     $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/; # in case Perl doesn't convert line endings
196     $str;
197 }
198 \end{code}
199
200 We must (a)~strip the marker off the block, (b)~record any literal C
201 constants that are defined here, and (c)~inject copies of any C constants
202 that are used-but-not-defined here.
203
204 \begin{code}
205 sub process_asm_block {
206     local($str) = @_;
207
208     return(&process_asm_block_m68k($str))  if $TargetPlatform =~ /^m68k-/;
209     return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
210     return(&process_asm_block_iX86($str))  if $TargetPlatform =~ /^i[34]86-/;
211     return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
212     return(&process_asm_block_hppa($str))  if $TargetPlatform =~ /^hppa/;
213     return(&process_asm_block_mips($str))   if $TargetPlatform =~ /^mips-/;
214     return(&process_asm_block_powerpc($str))   if $TargetPlatform =~ /^powerpc-apple-/;
215
216     # otherwise...
217     &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
218 }
219
220 sub process_asm_block_sparc {
221     local($str) = @_;
222
223     # strip the marker
224     if ( $OptimiseC ) {
225         $str =~ s/_?__stg_split_marker.*:\n//;
226     } else {
227         $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
228         $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
229     }
230
231     # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
232     $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
233
234     # remove/record any literal constants defined here
235     while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
236         local($label) = $2;
237         local($body)  = $1;
238
239         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
240             if $LocalConstant{$label};
241
242         $LocalConstant{$label} = $body;
243         
244         $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
245     }
246
247     # inject definitions for any local constants now used herein
248     foreach $k (keys %LocalConstant) {
249         if ( $str =~ /\b$k\b/ ) {
250             $str = $LocalConstant{$k} . $str;
251         }
252     }
253
254    print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
255
256    $str;
257 }
258
259 sub process_asm_block_m68k {
260     local($str) = @_;
261
262     # strip the marker
263
264     $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
265     $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
266
267     # it seems prudent to stick on one of these:
268     $str = "\.text\n\t.even\n" . $str;
269
270     # remove/record any literal constants defined here
271     while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
272         local($label) = $2;
273         local($body)  = $1;
274
275         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
276             if $LocalConstant{$label};
277
278         $LocalConstant{$label} = $body;
279         
280         $str =~ s/LC\d+:\n\t\.ascii.*\n//;
281     }
282
283     # inject definitions for any local constants now used herein
284     foreach $k (keys %LocalConstant) {
285         if ( $str =~ /\b$k\b/ ) {
286             $str = $LocalConstant{$k} . $str;
287         }
288     }
289
290    print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
291
292    $str;
293 }
294
295 sub process_asm_block_alpha {
296     local($str) = @_;
297
298     # strip the marker
299     if ( $OptimiseC ) {
300         $str =~ s/_?__stg_split_marker.*:\n//;
301     } else {
302         $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
303     }
304
305     # remove/record any literal constants defined here
306     while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
307         local($label) = $3;
308         local($body)  = $2;
309
310         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
311             if $LocalConstant{$label};
312
313         $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
314         
315         $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
316     }
317
318     # inject definitions for any local constants now used herein
319     foreach $k (keys %LocalConstant) {
320         if ( $str =~ /\$\b$k\b/ ) {
321             $str = $LocalConstant{$k} . $str;
322         }
323     }
324
325     # Slide the dummy direct return code into the vtbl .ent/.end block,
326     # to keep the label fixed if it's the last thing in a module, and
327     # to avoid having any anonymous text that the linker will complain about
328     $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
329
330     print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
331
332     $str;
333 }
334
335 sub process_asm_block_iX86 {
336     local($str) = @_;
337
338     # strip the marker
339
340     $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
341     $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
342
343     # it seems prudent to stick on one of these:
344     $str = "\.text\n\t.align 4\n" . $str;
345
346     # remove/record any literal constants defined here
347     # [perl made uglier to work around the perl 5.7/5.8 bug documented at
348     # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
349     # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
350     # -- ccshan 2002-09-05]
351     while ( ($str =~ /(\.?(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ )) {
352         local($label) = $2;
353         local($body)  = $1;
354         local($prefix, $suffix, $*) = ($`, $', 0);
355
356         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
357             if $LocalConstant{$label};
358
359         while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
360             $body .= $1;
361             $suffix = $';
362         }
363         $LocalConstant{$label} = $body;
364         $str = $prefix . $suffix;
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    print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
375
376    $str;
377 }
378 \end{code}
379
380 \begin{code}
381 sub process_asm_block_hppa {
382     local($str) = @_;
383
384     # strip the marker
385     $str =~ s/___stg_split_marker.*\n//;
386
387     # remove/record any imports defined here
388     while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
389         $Imports .= $1;
390
391         $str =~ s/^\s+\.IMPORT.*\n//;
392     }
393
394     # remove/record any literal constants defined here
395     while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
396         local($label) = $2;
397         local($body)  = $1;
398         local($prefix) = $`;
399         local($suffix) = $';
400         $label =~ s/\$/\\\$/g;
401
402         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
403             if $LocalConstant{$label};
404
405         $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
406         
407         $str = $prefix . $suffix;
408     }
409
410     # inject definitions for any local constants now used herein
411     foreach $k (keys %LocalConstant) {
412         if ( $str =~ /\b$k\b/ ) {
413             $str = $LocalConstant{$k} . $str;
414         }
415     }
416
417     # inject required imports for local exports in other chunks
418     foreach $k (keys %LocalExport) {
419         if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
420             $str = $LocalExport{$k} . $str;
421         }
422     }
423
424     # inject collected imports
425
426     $str = $Imports . $str;
427
428     print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
429
430     $str;
431 }
432 \end{code}
433
434 \begin{code}
435 sub process_asm_block_mips {
436     local($str) = @_;
437
438     # strip the marker
439     if ( $OptimiseC ) {
440         $str =~ s/_?__stg_split_marker.*:\n//;
441     } else {
442         $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
443     }
444
445     # remove/record any literal constants defined here
446     while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
447         local($label) = $3;
448         local($body)  = $2;
449
450         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
451             if $LocalConstant{$label};
452
453         $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
454         
455         $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
456     }
457
458     # inject definitions for any local constants now used herein
459     foreach $k (keys %LocalConstant) {
460         if ( $str =~ /\$\b$k\b/ ) {
461             $str = $LocalConstant{$k} . $str;
462         }
463     }
464
465     # Slide the dummy direct return code into the vtbl .ent/.end block,
466     # to keep the label fixed if it's the last thing in a module, and
467     # to avoid having any anonymous text that the linker will complain about
468     $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
469
470     $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
471
472     print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
473
474     $str;
475 }
476 \end{code}
477
478 \begin{code}
479 sub process_asm_block_powerpc {
480     local($str) = @_;
481     local($dyld_stuff) = '';
482
483     # strip the marker
484     $str =~ s/___stg_split_marker.*\n//;
485
486     $str =~ s/L_.*\$.*:\n(.|\n)*//;
487
488     # remove/record any literal constants defined here
489     while ( $str =~ s/^(\s+.const_data\n\s+\.align.*\n(LC\d+):\n(\s\.(byte|short|long|fill|space|ascii).*\n)+)// ) {
490         local($label) = $2;
491         local($body)  = $1;
492
493         &tidy_up_and_die(1,"Local constant label $label already defined!\n")
494             if $LocalConstant{$label};
495
496         $LocalConstant{$label} = $body;
497     }
498
499     # inject definitions for any local constants now used herein
500     foreach $k (keys %LocalConstant) {
501         if ( $str =~ /\b$k(\b|\[)/ ) {
502             $str = $LocalConstant{$k} . $str;
503         }
504     }
505     
506     foreach $k (keys %DyldChunks) {
507         if ( $str =~ /\bL$k\$/ ) {
508             if ( $str =~ /^$k:$/ ) {
509                 $dyld_stuff .= $DyldChunksDefined{$k};
510             } else {
511                 $dyld_stuff .= $DyldChunks{$k};
512             }
513         }
514     }
515
516     $str .= "\n" . $dyld_stuff;
517
518     print STDERR "### STRIPPED BLOCK (powerpc):\n$str" if $Dump_asm_splitting_info;
519
520     $str;
521 }
522 \end{code}
523
524 \begin{code}
525 sub tidy_up_and_die {
526     local($return_val, $msg) = @_;
527     print STDERR $msg;
528     exit (($return_val == 0) ? 0 : 1);
529 }
530 \end{code}