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/;
34 $octr = 0; # output file counter
35 $* = 1; # multi-line matches are OK
37 %LocalConstant = (); # we have to subvert C compiler's commoning-up of constants...
39 $s_stuff = &ReadTMPIUpToAMarker( '', $octr );
40 # that first stuff is a prologue for all .s outputs
41 $prologue_stuff = &process_asm_block ( $s_stuff );
42 # $_ already has some of the next stuff in it...
44 # &tidy_up_and_die(1,"$Pgm: no split markers in .s file!\n")
45 # if $prologue_stuff eq $s_stuff;
47 # lie about where this stuff came from
48 $prologue_stuff =~ s|"${Tmp_prefix}\.c"|"$ifile_root\.hc"|g;
50 while ( $_ ne '' ) { # not EOF
53 # grab and de-mangle a section of the .s file...
54 $s_stuff = &ReadTMPIUpToAMarker ( $_, $octr );
55 $this_piece = &process_asm_block ( $s_stuff );
57 # output to a file of its own
58 # open a new output file...
59 $ofname = "${Tmp_prefix}__${octr}.s";
60 open(OUTF, "> $ofname") || die "$Pgm: can't open output file: $ofname\n";
62 print OUTF $prologue_stuff;
63 print OUTF $this_piece;
66 || &tidy_up_and_die(1,"$Pgm:Failed writing ${Tmp_prefix}__${octr}.s\n");
69 $NoOfSplitFiles = $octr;
71 close(TMPI) || &tidy_up_and_die(1,"Failed reading $asm_file\n");
74 sub collectExports_hppa { # Note: HP-PA only
76 %LocalExport = (); # NB: global table
79 if (/^\s+\.EXPORT\s+([^,]+),.*\n/) {
81 local($body) = "\t.IMPORT $label";
87 $label =~ s/\$/\\\$/g;
88 $LocalExport{$label} = $body;
95 sub collectExports_mips { # Note: MIPS only
96 # (not really sure this is necessary [WDP 95/05])
98 $UNDEFINED_FUNS = ''; # NB: global table
101 $UNDEFINED_FUNS .= $_ if /^\t\.globl\s+\S+ \.\S+\n/;
108 sub ReadTMPIUpToAMarker {
109 local($str, $count) = @_; # already read bits
112 for ( $_ = <TMPI>; $_ ne '' && ! /_?__stg_split_marker/; $_ = <TMPI> ) {
115 # if not EOF, then creep forward until next "real" line
116 # (throwing everything away).
117 # that first "real" line will stay in $_.
119 # This loop is intended to pick up the body of the split_marker function
120 # Note that the assembler mangler will already have eliminated this code
121 # if it's been invoked (which it probably has).
123 while ($_ ne '' && (/_?__stg_split_marker/
133 # || /\t\.end/ NOT! Let the split_marker regexp catch it
134 # || /\t\.ent/ NOT! Let the split_marker regexp catch it
135 || /^\s+(save|retl?|restore|nop)/)) {
139 print STDERR "### BLOCK:$count:\n$str" if $Dump_asm_splitting_info;
142 $str =~ tr/\r//d if $TargetPlatform =~ /-mingw32$/;
147 We must (a)~strip the marker off the block, (b)~record any literal C
148 constants that are defined here, and (c)~inject copies of any C constants
149 that are used-but-not-defined here.
152 sub process_asm_block {
155 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/;
156 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
157 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/;
158 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
159 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/;
160 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/;
161 return(&process_asm_block_powerpc($str)) if $TargetPlatform =~ /^powerpc-|^rs6000-/;
164 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
167 sub process_asm_block_sparc {
172 $str =~ s/_?__stg_split_marker.*:\n//;
174 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
175 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
178 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
179 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
181 # remove/record any literal constants defined here
182 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
186 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
187 if $LocalConstant{$label};
189 $LocalConstant{$label} = $body;
191 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
194 # inject definitions for any local constants now used herein
195 foreach $k (keys %LocalConstant) {
196 if ( $str =~ /\b$k\b/ ) {
197 $str = $LocalConstant{$k} . $str;
201 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
206 sub process_asm_block_m68k {
211 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
212 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
214 # it seems prudent to stick on one of these:
215 $str = "\.text\n\t.even\n" . $str;
217 # remove/record any literal constants defined here
218 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
222 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
223 if $LocalConstant{$label};
225 $LocalConstant{$label} = $body;
227 $str =~ s/LC\d+:\n\t\.ascii.*\n//;
230 # inject definitions for any local constants now used herein
231 foreach $k (keys %LocalConstant) {
232 if ( $str =~ /\b$k\b/ ) {
233 $str = $LocalConstant{$k} . $str;
237 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
242 sub process_asm_block_alpha {
247 $str =~ s/_?__stg_split_marker.*:\n//;
249 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
252 # remove/record any literal constants defined here
253 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
257 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
258 if $LocalConstant{$label};
260 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
262 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
265 # inject definitions for any local constants now used herein
266 foreach $k (keys %LocalConstant) {
267 if ( $str =~ /\$\b$k\b/ ) {
268 $str = $LocalConstant{$k} . $str;
272 # Slide the dummy direct return code into the vtbl .ent/.end block,
273 # to keep the label fixed if it's the last thing in a module, and
274 # to avoid having any anonymous text that the linker will complain about
275 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
277 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
282 sub process_asm_block_iX86 {
287 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
288 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
290 # it seems prudent to stick on one of these:
291 $str = "\.text\n\t.align 4\n" . $str;
293 # remove/record any literal constants defined here
294 while ( ($str =~ /(\.?(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n)+)/ )) {
298 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
299 if $LocalConstant{$label};
301 $LocalConstant{$label} = $body;
303 $str =~ s/\.?LC\d+:\n(\t\.(ascii|string).*\n|\s*\.byte.*\n)+//;
306 # inject definitions for any local constants now used herein
307 foreach $k (keys %LocalConstant) {
308 if ( $str =~ /\b$k\b/ ) {
309 $str = $LocalConstant{$k} . $str;
313 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
320 sub process_asm_block_hppa {
324 $str =~ s/___stg_split_marker.*\n//;
326 # remove/record any imports defined here
327 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
330 $str =~ s/^\s+\.IMPORT.*\n//;
333 # remove/record any literal constants defined here
334 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
339 $label =~ s/\$/\\\$/g;
341 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
342 if $LocalConstant{$label};
344 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
346 $str = $prefix . $suffix;
349 # inject definitions for any local constants now used herein
350 foreach $k (keys %LocalConstant) {
351 if ( $str =~ /\b$k\b/ ) {
352 $str = $LocalConstant{$k} . $str;
356 # inject required imports for local exports in other chunks
357 foreach $k (keys %LocalExport) {
358 if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
359 $str = $LocalExport{$k} . $str;
363 # inject collected imports
365 $str = $Imports . $str;
367 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
374 sub process_asm_block_mips {
379 $str =~ s/_?__stg_split_marker.*:\n//;
381 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
384 # remove/record any literal constants defined here
385 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
389 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
390 if $LocalConstant{$label};
392 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
394 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
397 # inject definitions for any local constants now used herein
398 foreach $k (keys %LocalConstant) {
399 if ( $str =~ /\$\b$k\b/ ) {
400 $str = $LocalConstant{$k} . $str;
404 # Slide the dummy direct return code into the vtbl .ent/.end block,
405 # to keep the label fixed if it's the last thing in a module, and
406 # to avoid having any anonymous text that the linker will complain about
407 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
409 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
411 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
418 sub process_asm_block_powerpc {
422 $str =~ s/___stg_split_marker.*\n//;
423 $str =~ s/___stg_split_marker.*\n//; # yes, twice.
425 # remove/record any literal constants defined here
426 while ( $str =~ /^(.csect .data[RW]\n\s+\.align.*\n(LC\.\.\d+):\n(\s\.byte .*\n)+)/ ) {
430 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
431 if $LocalConstant{$label};
433 $LocalConstant{$label} = $body;
435 $str =~ s/^.csect .data[RW]\n\s+\.align.*\nLC\.\.\d+:\n(\s\.byte .*\n)+//;
438 # inject definitions for any local constants now used herein
439 foreach $k (keys %LocalConstant) {
440 if ( $str =~ /\b$k(\b|\[)/ ) {
441 $str = $LocalConstant{$k} . $str;
445 print STDERR "### STRIPPED BLOCK (powerpc/rs6000):\n$str" if $Dump_asm_splitting_info;
447 $str = ".toc\n" . $str;
454 sub tidy_up_and_die {
455 local($return_val, $msg) = @_;
457 exit (($return_val == 0) ? 0 : 1);