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 # about to use $Tmp_prefix in a regex - make sure backslashes don't get
49 # interpreted in a strange way. ToDo: what about other regex
50 # characters, eg. '.'?
51 $Tmp_prefix =~ s/\\/\\\\/g;
53 # lie about where this stuff came from
54 $prologue_stuff =~ s|"${Tmp_prefix}\.c"|"$ifile_root\.hc"|g;
56 while ( $_ ne '' ) { # not EOF
59 # grab and de-mangle a section of the .s file...
60 $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
61 $this_piece = &process_asm_block ( $s_stuff );
63 # output to a file of its own
64 # open a new output file...
65 $ofname = "${Tmp_prefix}__${octr}.s";
66 open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
68 print OUTF $prologue_stuff;
69 print OUTF $this_piece;
72 || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
75 $NoOfSplitFiles = $octr;
77 close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
80 sub collectExports_hppa { # Note: HP-PA only
82 %LocalExport = (); # NB: global table
85 if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
87 local($body) = "\t.IMPORT $label";
93 $label =~ s/\$/\\\$/g;
94 $LocalExport{$label} = $body;
101 sub collectExports_mips { # Note: MIPS only
102 # (not really sure this is necessary [WDP 95/05])
104 $UNDEFINED_FUNS = ''; # NB: global table
107 $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
114 sub collectDyldStuff_powerpc { # Note: Darwin/PowerPC only
115 local($chunk_label,$label,$cur_section,$section,$chunk,$alignment,$cur_alignment);
117 %DyldChunks = (); # NB: global table
118 %DyldChunksDefined = (); # NB: global table
129 if ( $_ eq '' || /^L(_.+)\$.+:/ ) {
130 if ( $label ne '' ) {
131 $DyldChunksDefined{$label} .= $section . $chunk_label . $alignment . $ chunk;
132 if( $section =~ s/\.data/\.non_lazy_symbol_pointer/ ) {
133 $chunk = "\t.indirect_symbol $label\n\t.long 0\n";
135 $DyldChunks{$label} .= $section . $chunk_label . $chunk;
136 # don't use $alignment, it's only needed for .data, which we change into .non_lazy_symbol_pointer
137 print STDERR "### dyld chunk: $label\n$section$alignment$chunk\n###\n" if $Dump_asm_splitting_info;
144 $section = $cur_section;
145 $alignment = $cur_alignment;
146 print STDERR "label: $label\n" if $Dump_asm_splitting_info;
147 } elsif ( /^\s*\.(symbol_stub|picsymbol_stub|lazy_symbol_pointer|non_lazy_symbol_pointer|data)/ ) {
149 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
151 } elsif ( /^\s*\.section\s+__TEXT,__symbol_stub1,symbol_stubs,pure_instructions,\d+/ ) {
153 printf STDERR "section: $cur_section\n" if $Dump_asm_splitting_info;
155 } elsif ( /^\s*\.align.*/ ) {
157 printf STDERR "alignment: $cur_alignment\n" if $Dump_asm_splitting_info;
166 sub ReadTMPIUpToAMarker {
167 local($str, $count) = @_; # already read bits
170 for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
173 # if not EOF, then creep forward until next "real" line
174 # (throwing everything away).
175 # that first "real" line will stay in $_.
177 # This loop is intended to pick up the body of the split_marker function
178 # Note that the assembler mangler will already have eliminated this code
179 # if it's been invoked (which it probably has).
181 while ($_ ne '' && (/_?__stg_split_marker/
191 # || /\t\.end/ NOT! Let the split_marker regexp catch it
192 # || /\t\.ent/ NOT! Let the split_marker regexp catch it
193 || /^\s+(save|retl?|restore|nop)/)) {
197 print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
200 $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/; # in case Perl doesn't convert line endings
205 We must (a)~strip the marker off the block, (b)~record any literal C
206 constants that are defined here, and (c)~inject copies of any C constants
207 that are used-but-not-defined here.
210 sub process_asm_block {
213 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/;
214 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
215 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/;
216 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
217 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/;
218 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/;
219 return(&process_asm_block_powerpc($str)) if $TargetPlatform =~ /^powerpc-apple-/;
222 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
225 sub process_asm_block_sparc {
230 $str =~ s/_?__stg_split_marker.*:\n//;
232 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
233 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
236 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
237 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
239 # remove/record any literal constants defined here
240 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
244 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
245 if $LocalConstant{$label};
247 $LocalConstant{$label} = $body;
249 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
252 # inject definitions for any local constants now used herein
253 foreach $k (keys %LocalConstant) {
254 if ( $str =~ /\b$k\b/ ) {
255 $str = $LocalConstant{$k} . $str;
259 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
264 sub process_asm_block_m68k {
269 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
270 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
272 # it seems prudent to stick on one of these:
273 $str = "\.text\n\t.even\n" . $str;
275 # remove/record any literal constants defined here
276 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
280 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
281 if $LocalConstant{$label};
283 $LocalConstant{$label} = $body;
285 $str =~ s/LC\d+:\n\t\.ascii.*\n//;
288 # inject definitions for any local constants now used herein
289 foreach $k (keys %LocalConstant) {
290 if ( $str =~ /\b$k\b/ ) {
291 $str = $LocalConstant{$k} . $str;
295 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
300 sub process_asm_block_alpha {
305 $str =~ s/_?__stg_split_marker.*:\n//;
307 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
310 # remove/record any literal constants defined here
311 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
315 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
316 if $LocalConstant{$label};
318 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
320 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
323 # inject definitions for any local constants now used herein
324 foreach $k (keys %LocalConstant) {
325 if ( $str =~ /\$\b$k\b/ ) {
326 $str = $LocalConstant{$k} . $str;
330 # Slide the dummy direct return code into the vtbl .ent/.end block,
331 # to keep the label fixed if it's the last thing in a module, and
332 # to avoid having any anonymous text that the linker will complain about
333 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
335 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
340 sub process_asm_block_iX86 {
345 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
346 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
348 # it seems prudent to stick on one of these:
349 $str = "\.text\n\t.align 4\n" . $str;
351 # remove/record any literal constants defined here
352 # [perl made uglier to work around the perl 5.7/5.8 bug documented at
353 # http://bugs6.perl.org/rt2/Ticket/Display.html?id=1760 and illustrated
354 # by the seg fault of perl -e '("x\n" x 5000) =~ /(.*\n)+/'
355 # -- ccshan 2002-09-05]
356 while ( ($str =~ /(\.?(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ )) {
359 local($prefix, $suffix, $*) = ($`, $', 0);
361 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
362 if $LocalConstant{$label};
364 while ( $suffix =~ /^((\t\.(ascii|string).*\n|\s*\.byte.*\n){1,100})/ ) {
368 $LocalConstant{$label} = $body;
369 $str = $prefix . $suffix;
372 # inject definitions for any local constants now used herein
373 foreach $k (keys %LocalConstant) {
374 if ( $str =~ /\b$k\b/ ) {
375 $str = $LocalConstant{$k} . $str;
379 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
386 sub process_asm_block_hppa {
390 $str =~ s/___stg_split_marker.*\n//;
392 # remove/record any imports defined here
393 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
396 $str =~ s/^\s+\.IMPORT.*\n//;
399 # remove/record any literal constants defined here
400 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
405 $label =~ s/\$/\\\$/g;
407 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
408 if $LocalConstant{$label};
410 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
412 $str = $prefix . $suffix;
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;
422 # inject required imports for local exports in other chunks
423 foreach $k (keys %LocalExport) {
424 if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
425 $str = $LocalExport{$k} . $str;
429 # inject collected imports
431 $str = $Imports . $str;
433 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
440 sub process_asm_block_mips {
445 $str =~ s/_?__stg_split_marker.*:\n//;
447 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
450 # remove/record any literal constants defined here
451 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
455 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
456 if $LocalConstant{$label};
458 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
460 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
463 # inject definitions for any local constants now used herein
464 foreach $k (keys %LocalConstant) {
465 if ( $str =~ /\$\b$k\b/ ) {
466 $str = $LocalConstant{$k} . $str;
470 # Slide the dummy direct return code into the vtbl .ent/.end block,
471 # to keep the label fixed if it's the last thing in a module, and
472 # to avoid having any anonymous text that the linker will complain about
473 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
475 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
477 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
484 sub process_asm_block_powerpc {
486 local($dyld_stuff) = '';
489 $str =~ s/___stg_split_marker.*\n//;
491 $str =~ s/L_.*\$.*:\n(.|\n)*//;
493 # remove/record any literal constants defined here
494 while ( $str =~ s/^(\s+.const_data\n\s+\.align.*\n(LC\d+):\n(\s\.(byte|short|long|fill|space|ascii).*\n)+)// ) {
498 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
499 if $LocalConstant{$label};
501 $LocalConstant{$label} = $body;
504 # inject definitions for any local constants now used herein
505 foreach $k (keys %LocalConstant) {
506 if ( $str =~ /\b$k(\b|\[)/ ) {
507 $str = $LocalConstant{$k} . $str;
511 foreach $k (keys %DyldChunks) {
512 if ( $str =~ /\bL$k\$/ ) {
513 if ( $str =~ /^$k:$/ ) {
514 $dyld_stuff .= $DyldChunksDefined{$k};
516 $dyld_stuff .= $DyldChunks{$k};
521 $str .= "\n" . $dyld_stuff;
523 print STDERR "### STRIPPED BLOCK (powerpc):\n$str" if $Dump_asm_splitting_info;
530 sub tidy_up_and_die {
531 local($return_val, $msg) = @_;
533 exit (($return_val == 0) ? 0 : 1);