1 %************************************************************************
3 \section[Driver-obj-splitting]{Splitting into many \tr{.o} files (for libraries)}
5 %************************************************************************
8 $TargetPlatform = $TARGETPLATFORM;
10 ($Pgm = $0) =~ s|.*/||;
12 $Tmp_prefix = $ARGV[1];
15 &split_asm_file($ifile);
17 open(OUTPUT, "> $Output") || &tidy_up_and_die(1,"$Pgm: failed to open `$Output' (to write)\n");
18 print OUTPUT "$NoOfSplitFiles\n";
27 local($asm_file) = @_;
29 open(TMPI, "< $asm_file") || &tidy_up_and_die(1,"$Pgm: failed to open `$asm_file' (to read)\n");
31 &collectExports_hppa() if $TargetPlatform =~ /^hppa/;
32 &collectExports_mips() if $TargetPlatform =~ /^mips/;
33 &collectDyldStuff_powerpc() if $TargetPlatform =~ /^powerpc-apple/;
35 $octr = 0; # output file counter
36 $* = 1; # multi-line matches are OK
38 %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
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...
45 # &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
46 # if $prologue_stuff eq $s_stuff;
48 # lie about where this stuff came from
49 $prologue_stuff =~ s|"${Tmp_prefix}\.c"|"$ifile_root\.hc"|g;
51 while ( $_ ne '' ) { # not EOF
54 # grab and de-mangle a section of the .s file...
55 $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
56 $this_piece = &process_asm_block ( $s_stuff );
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";
63 print OUTF $prologue_stuff;
64 print OUTF $this_piece;
67 || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
70 $NoOfSplitFiles = $octr;
72 close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
75 sub collectExports_hppa { # Note: HP-PA only
77 %LocalExport = (); # NB: global table
80 if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
82 local($body) = "\t.IMPORT $label";
88 $label =~ s/\$/\\\$/g;
89 $LocalExport{$label} = $body;
96 sub collectExports_mips { # Note: MIPS only
97 # (not really sure this is necessary [WDP 95/05])
99 $UNDEFINED_FUNS = ''; # NB: global table
102 $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
109 sub collectDyldStuff_powerpc { # Note: Darwin/PowerPC only
110 local($chunk_label,$label,$cur_section,$section,$chunk,$alignment,$cur_alignment);
112 %DyldChunks = (); # NB: global table
113 %DyldChunksDefined = (); # NB: global table
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";
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;
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)/ ) {
144 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
146 } elsif ( /^\s*\.align.*/ ) {
148 printf STDERR "alignment: $cur_alignment\n" if $Dump_asm_splitting_info;
157 sub ReadTMPIUpToAMarker {
158 local($str, $count) = @_; # already read bits
161 for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
164 # if not EOF, then creep forward until next "real" line
165 # (throwing everything away).
166 # that first "real" line will stay in $_.
168 # This loop is intended to pick up the body of the split_marker function
169 # Note that the assembler mangler will already have eliminated this code
170 # if it's been invoked (which it probably has).
172 while ($_ ne '' && (/_?__stg_split_marker/
182 # || /\t\.end/ NOT! Let the split_marker regexp catch it
183 # || /\t\.ent/ NOT! Let the split_marker regexp catch it
184 || /^\s+(save|retl?|restore|nop)/)) {
188 print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
191 $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/; # in case Perl doesn't convert line endings
196 We must (a)~strip the marker off the block, (b)~record any literal C
197 constants that are defined here, and (c)~inject copies of any C constants
198 that are used-but-not-defined here.
201 sub process_asm_block {
204 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/;
205 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
206 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/;
207 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
208 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/;
209 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/;
210 return(&process_asm_block_powerpc($str)) if $TargetPlatform =~ /^powerpc-apple-/;
213 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
216 sub process_asm_block_sparc {
221 $str =~ s/_?__stg_split_marker.*:\n//;
223 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
224 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
227 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
228 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
230 # remove/record any literal constants defined here
231 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
235 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
236 if $LocalConstant{$label};
238 $LocalConstant{$label} = $body;
240 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
243 # inject definitions for any local constants now used herein
244 foreach $k (keys %LocalConstant) {
245 if ( $str =~ /\b$k\b/ ) {
246 $str = $LocalConstant{$k} . $str;
250 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
255 sub process_asm_block_m68k {
260 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
261 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
263 # it seems prudent to stick on one of these:
264 $str = "\.text\n\t.even\n" . $str;
266 # remove/record any literal constants defined here
267 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
271 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
272 if $LocalConstant{$label};
274 $LocalConstant{$label} = $body;
276 $str =~ s/LC\d+:\n\t\.ascii.*\n//;
279 # inject definitions for any local constants now used herein
280 foreach $k (keys %LocalConstant) {
281 if ( $str =~ /\b$k\b/ ) {
282 $str = $LocalConstant{$k} . $str;
286 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
291 sub process_asm_block_alpha {
296 $str =~ s/_?__stg_split_marker.*:\n//;
298 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
301 # remove/record any literal constants defined here
302 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
306 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
307 if $LocalConstant{$label};
309 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
311 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
314 # inject definitions for any local constants now used herein
315 foreach $k (keys %LocalConstant) {
316 if ( $str =~ /\$\b$k\b/ ) {
317 $str = $LocalConstant{$k} . $str;
321 # Slide the dummy direct return code into the vtbl .ent/.end block,
322 # to keep the label fixed if it's the last thing in a module, and
323 # to avoid having any anonymous text that the linker will complain about
324 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
326 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
331 sub process_asm_block_iX86 {
336 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
337 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
339 # it seems prudent to stick on one of these:
340 $str = "\.text\n\t.align 4\n" . $str;
342 # remove/record any literal constants defined here
343 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
344 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
345 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
346 # -- ccshan 2002-09-05]
347 while ( ($str =~ /(\.?(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ )) {
350 local($prefix, $suffix, $*) = ($`, $', 0);
352 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
353 if $LocalConstant{$label};
355 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
359 $LocalConstant{$label} = $body;
360 $str = $prefix . $suffix;
363 # inject definitions for any local constants now used herein
364 foreach $k (keys %LocalConstant) {
365 if ( $str =~ /\b$k\b/ ) {
366 $str = $LocalConstant{$k} . $str;
370 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
377 sub process_asm_block_hppa {
381 $str =~ s/___stg_split_marker.*\n//;
383 # remove/record any imports defined here
384 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
387 $str =~ s/^\s+\.IMPORT.*\n//;
390 # remove/record any literal constants defined here
391 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
396 $label =~ s/\$/\\\$/g;
398 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
399 if $LocalConstant{$label};
401 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
403 $str = $prefix . $suffix;
406 # inject definitions for any local constants now used herein
407 foreach $k (keys %LocalConstant) {
408 if ( $str =~ /\b$k\b/ ) {
409 $str = $LocalConstant{$k} . $str;
413 # inject required imports for local exports in other chunks
414 foreach $k (keys %LocalExport) {
415 if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
416 $str = $LocalExport{$k} . $str;
420 # inject collected imports
422 $str = $Imports . $str;
424 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
431 sub process_asm_block_mips {
436 $str =~ s/_?__stg_split_marker.*:\n//;
438 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
441 # remove/record any literal constants defined here
442 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
446 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
447 if $LocalConstant{$label};
449 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
451 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
454 # inject definitions for any local constants now used herein
455 foreach $k (keys %LocalConstant) {
456 if ( $str =~ /\$\b$k\b/ ) {
457 $str = $LocalConstant{$k} . $str;
461 # Slide the dummy direct return code into the vtbl .ent/.end block,
462 # to keep the label fixed if it's the last thing in a module, and
463 # to avoid having any anonymous text that the linker will complain about
464 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
466 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
468 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
475 sub process_asm_block_powerpc {
477 local($dyld_stuff) = '';
480 $str =~ s/___stg_split_marker.*\n//;
482 $str =~ s/L_.*\$.*:\n(.|\n)*//;
484 # remove/record any literal constants defined here
485 while ( $str =~ s/^(\s+.const_data\n\s+\.align.*\n(LC\d+):\n(\s\.(byte|short|long|fill|space|ascii).*\n)+)// ) {
489 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
490 if $LocalConstant{$label};
492 $LocalConstant{$label} = $body;
495 # inject definitions for any local constants now used herein
496 foreach $k (keys %LocalConstant) {
497 if ( $str =~ /\b$k(\b|\[)/ ) {
498 $str = $LocalConstant{$k} . $str;
502 foreach $k (keys %DyldChunks) {
503 if ( $str =~ /\bL$k\$/ ) {
504 if ( $str =~ /^$k:$/ ) {
505 $dyld_stuff .= $DyldChunksDefined{$k};
507 $dyld_stuff .= $DyldChunks{$k};
512 $str .= "\n" . $dyld_stuff;
514 print STDERR "### STRIPPED BLOCK (powerpc):\n$str" if $Dump_asm_splitting_info;
521 sub tidy_up_and_die {
522 local($return_val, $msg) = @_;
524 exit (($return_val == 0) ? 0 : 1);