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;
146 We must (a)~strip the marker off the block, (b)~record any literal C
147 constants that are defined here, and (c)~inject copies of any C constants
148 that are used-but-not-defined here.
151 sub process_asm_block {
154 return(&process_asm_block_m68k($str)) if $TargetPlatform =~ /^m68k-/;
155 return(&process_asm_block_sparc($str)) if $TargetPlatform =~ /^sparc-/;
156 return(&process_asm_block_iX86($str)) if $TargetPlatform =~ /^i[34]86-/;
157 return(&process_asm_block_alpha($str)) if $TargetPlatform =~ /^alpha-/;
158 return(&process_asm_block_hppa($str)) if $TargetPlatform =~ /^hppa/;
159 return(&process_asm_block_mips($str)) if $TargetPlatform =~ /^mips-/;
160 return(&process_asm_block_powerpc($str)) if $TargetPlatform =~ /^powerpc-|^rs6000-/;
163 &tidy_up_and_die(1,"$Pgm: no process_asm_block for $TargetPlatform\n");
166 sub process_asm_block_sparc {
171 $str =~ s/_?__stg_split_marker.*:\n//;
173 $str =~ s/(\.text\n\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
174 $str =~ s/(\t\.align .\n)\t\.global\s+.*_?__stg_split_marker.*\n\t\.proc.*\n/$1/;
177 # make sure the *.hc filename gets saved; not just ghc*.c (temp name)
178 $str =~ s/^\.stabs "(ghc\d+\.c)"/.stabs "$ifile_root.hc"/g; # HACK HACK
180 # remove/record any literal constants defined here
181 while ( $str =~ /(\t\.align .\n\.?(L?LC\d+):\n(\t\.asci[iz].*\n)+)/ ) {
185 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
186 if $LocalConstant{$label};
188 $LocalConstant{$label} = $body;
190 $str =~ s/\t\.align .\n\.?LL?C\d+:\n(\t\.asci[iz].*\n)+//;
193 # inject definitions for any local constants now used herein
194 foreach $k (keys %LocalConstant) {
195 if ( $str =~ /\b$k\b/ ) {
196 $str = $LocalConstant{$k} . $str;
200 print STDERR "### STRIPPED BLOCK (sparc):\n$str" if $Dump_asm_splitting_info;
205 sub process_asm_block_m68k {
210 $str =~ s/(\.text\n\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
211 $str =~ s/(\t\.even\n)\t\.globl\s+.*_?__stg_split_marker.*\n/$1/;
213 # it seems prudent to stick on one of these:
214 $str = "\.text\n\t.even\n" . $str;
216 # remove/record any literal constants defined here
217 while ( $str =~ /((LC\d+):\n\t\.ascii.*\n)/ ) {
221 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
222 if $LocalConstant{$label};
224 $LocalConstant{$label} = $body;
226 $str =~ s/LC\d+:\n\t\.ascii.*\n//;
229 # inject definitions for any local constants now used herein
230 foreach $k (keys %LocalConstant) {
231 if ( $str =~ /\b$k\b/ ) {
232 $str = $LocalConstant{$k} . $str;
236 print STDERR "### STRIPPED BLOCK (m68k):\n$str" if $Dump_asm_splitting_info;
241 sub process_asm_block_alpha {
246 $str =~ s/_?__stg_split_marker.*:\n//;
248 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
251 # remove/record any literal constants defined here
252 while ( $str =~ /(\.rdata\n\t\.align \d\n)?(\$(C\d+):\n\t\..*\n)/ ) {
256 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
257 if $LocalConstant{$label};
259 $LocalConstant{$label} = ".rdata\n\t.align 3\n" . $body . "\t.text\n";
261 $str =~ s/(\.rdata\n\t\.align \d\n)?\$C\d+:\n\t\..*\n//;
264 # inject definitions for any local constants now used herein
265 foreach $k (keys %LocalConstant) {
266 if ( $str =~ /\$\b$k\b/ ) {
267 $str = $LocalConstant{$k} . $str;
271 # Slide the dummy direct return code into the vtbl .ent/.end block,
272 # to keep the label fixed if it's the last thing in a module, and
273 # to avoid having any anonymous text that the linker will complain about
274 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
276 print STDERR "### STRIPPED BLOCK (alpha):\n$str" if $Dump_asm_splitting_info;
281 sub process_asm_block_iX86 {
286 $str =~ s/(\.text\n\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
287 $str =~ s/(\t\.align .(,0x90)?\n)\.globl\s+.*_?__stg_split_marker.*\n/$1/;
289 # it seems prudent to stick on one of these:
290 $str = "\.text\n\t.align 4\n" . $str;
292 # remove/record any literal constants defined here
293 while ( ($str =~ /(\.?(LC\d+):\n(\t\.(ascii|string).*\n|\s*\.byte.*\n)+)/ )) {
297 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
298 if $LocalConstant{$label};
300 $LocalConstant{$label} = $body;
302 $str =~ s/\.?LC\d+:\n(\t\.(ascii|string).*\n|\s*\.byte.*\n)+//;
305 # inject definitions for any local constants now used herein
306 foreach $k (keys %LocalConstant) {
307 if ( $str =~ /\b$k\b/ ) {
308 $str = $LocalConstant{$k} . $str;
312 print STDERR "### STRIPPED BLOCK (iX86):\n$str" if $Dump_asm_splitting_info;
319 sub process_asm_block_hppa {
323 $str =~ s/___stg_split_marker.*\n//;
325 # remove/record any imports defined here
326 while ( $str =~ /^(\s+\.IMPORT\s.*\n)/ ) {
329 $str =~ s/^\s+\.IMPORT.*\n//;
332 # remove/record any literal constants defined here
333 while ( $str =~ /^(\s+\.align.*\n(L\$C\d+)\n(\s.*\n)+); end literal\n/ ) {
338 $label =~ s/\$/\\\$/g;
340 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
341 if $LocalConstant{$label};
343 $LocalConstant{$label} = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n\n" . $body;
345 $str = $prefix . $suffix;
348 # inject definitions for any local constants now used herein
349 foreach $k (keys %LocalConstant) {
350 if ( $str =~ /\b$k\b/ ) {
351 $str = $LocalConstant{$k} . $str;
355 # inject required imports for local exports in other chunks
356 foreach $k (keys %LocalExport) {
357 if ( $str =~ /\b$k\b/ && ! /EXPORT\s+$k\b/ ) {
358 $str = $LocalExport{$k} . $str;
362 # inject collected imports
364 $str = $Imports . $str;
366 print STDERR "### STRIPPED BLOCK (hppa):\n$str" if $Dump_asm_splitting_info;
373 sub process_asm_block_mips {
378 $str =~ s/_?__stg_split_marker.*:\n//;
380 $str =~ s/(\t\.align .\n)\t\.globl\s+.*_?__stg_split_marker.*\n\t\.ent.*\n/$1/;
383 # remove/record any literal constants defined here
384 while ( $str =~ /(\t\.rdata\n\t\.align \d\n)?(\$(LC\d+):\n(\t\.byte\t.*\n)+)/ ) {
388 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
389 if $LocalConstant{$label};
391 $LocalConstant{$label} = "\t.rdata\n\t.align 2\n" . $body . "\t.text\n";
393 $str =~ s/(\t\.rdata\n\t\.align \d\n)?\$LC\d+:\n(\t\.byte\t.*\n)+//;
396 # inject definitions for any local constants now used herein
397 foreach $k (keys %LocalConstant) {
398 if ( $str =~ /\$\b$k\b/ ) {
399 $str = $LocalConstant{$k} . $str;
403 # Slide the dummy direct return code into the vtbl .ent/.end block,
404 # to keep the label fixed if it's the last thing in a module, and
405 # to avoid having any anonymous text that the linker will complain about
406 $str =~ s/(\t\.end [A-Za-z0-9_]+)\n\t# nop/\tnop\n$1/g;
408 $str .= $UNDEFINED_FUNS; # pin on gratuitiously-large amount of info
410 print STDERR "### STRIPPED BLOCK (mips):\n$str" if $Dump_asm_splitting_info;
417 sub process_asm_block_powerpc {
421 $str =~ s/___stg_split_marker.*\n//;
422 $str =~ s/___stg_split_marker.*\n//; # yes, twice.
424 # remove/record any literal constants defined here
425 while ( $str =~ /^(.csect .data[RW]\n\s+\.align.*\n(LC\.\.\d+):\n(\s\.byte .*\n)+)/ ) {
429 &tidy_up_and_die(1,"Local constant label $label already defined!\n")
430 if $LocalConstant{$label};
432 $LocalConstant{$label} = $body;
434 $str =~ s/^.csect .data[RW]\n\s+\.align.*\nLC\.\.\d+:\n(\s\.byte .*\n)+//;
437 # inject definitions for any local constants now used herein
438 foreach $k (keys %LocalConstant) {
439 if ( $str =~ /\b$k(\b|\[)/ ) {
440 $str = $LocalConstant{$k} . $str;
444 print STDERR "### STRIPPED BLOCK (powerpc/rs6000):\n$str" if $Dump_asm_splitting_info;
446 $str = ".toc\n" . $str;
453 sub tidy_up_and_die {
454 local($return_val, $msg) = @_;
456 exit (($return_val == 0) ? 0 : 1);