1 %************************************************************************
3 \section[Driver-asm-fiddling]{Fiddling with assembler files (HP-PA)}
5 %************************************************************************
10 Utterly stomp out C functions' prologues and epilogues; i.e., the
11 stuff to do with the C stack.
13 Any other required tidying up.
19 The HP linker is very picky about symbols being in the appropriate
20 space (code vs. data). When we mangle the threaded code to put the
21 info tables just prior to the code, they wind up in code space
22 rather than data space. This means that references to *_info from
23 un-mangled parts of the RTS (e.g. unthreaded GC code) get
24 unresolved symbols. Solution: mini-mangler for .c files on HP. I
25 think this should really be triggered in the driver by a new -rts
26 option, so that user code doesn't get mangled inappropriately.
28 With reversed tables, jumps are to the _info label rather than to
29 the _entry label. The _info label is just an address in code
30 space, rather than an entry point with the descriptive blob we
31 talked about yesterday. As a result, you can't use the call-style
32 JMP_ macro. However, some JMP_ macros take _info labels as targets
33 and some take code entry points within the RTS. The latter won't
34 work with the goto-style JMP_ macro. Sigh. Solution: Use the goto
35 style JMP_ macro, and mangle some more assembly, changing all
36 "RP'literal" and "LP'literal" references to "R'literal" and
37 "L'literal," so that you get the real address of the code, rather
38 than the descriptive blob. Also change all ".word P%literal"
39 entries in info tables and vector tables to just ".word literal,"
40 for the same reason. Advantage: No more ridiculous call sequences.
45 local($in_asmf, $out_asmf) = @_;
47 # multi-line regexp matching:
52 open(INASM, "< $in_asmf")
53 || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
54 open(OUTASM,"> $out_asmf")
55 || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
57 # read whole file, divide into "chunks":
58 # record some info about what we've found...
60 @chk = (); # contents of the chunk
61 $numchks = 0; # number of them
62 @chkcat = (); # what category of thing in each chunk
63 @chksymb = (); # what symbol(base) is defined in this chunk
64 %slowchk = (); # ditto, its regular "slow" entry code
65 %fastchk = (); # ditto, fast entry code
66 %closurechk = (); # ditto, the (static) closure
67 %infochk = (); # ditto, normal info tbl
68 %vectorchk = (); # ditto, return vector table
69 %directchk = (); # ditto, direct return code
75 #??? next if /^\.stab.*___stg_split_marker/;
76 #??? next if /^\.stab.*ghc.*c_ID/;
80 if ( /^\s+/ ) { # most common case first -- a simple line!
81 # duplicated from the bottom
84 } elsif ( /^L\$C(\d+)$/ ) {
86 $chkcat[$i] = 'literal';
89 } elsif ( /^(ret_|djn_)/ ) {
94 } elsif ( /^vtbl_([A-Za-z0-9_]+)$/ ) {
96 $chkcat[$i] = 'vector';
101 } elsif ( /^([A-Za-z0-9_]+)DirectReturn$/ ) {
103 $chkcat[$i] = 'direct';
108 } elsif ( /^[A-Za-z0-9_]+_upd$/ ) {
110 $chkcat[$i] = 'misc';
113 } elsif ( /^__stg_split_marker(\d+)$/ ) {
115 $chkcat[$i] = 'splitmarker';
118 } elsif ( /^([A-Za-z0-9_]+)_info$/ ) {
121 $chkcat[$i] = 'infotbl';
122 $chksymb[$i] = $symb;
124 $infochk{$symb} = $i;
126 } elsif ( /^([A-Za-z0-9_]+)_entry$/ ) {
128 $chkcat[$i] = 'slow';
133 } elsif ( /^([A-Za-z0-9_]+)_fast\d+$/ ) {
135 $chkcat[$i] = 'fast';
140 } elsif ( /^([A-Za-z0-9_]+)_closure$/ ) {
142 $chkcat[$i] = 'closure';
145 $closurechk{$1} = $i;
147 } elsif ( /^ghc.*c_ID/ ) {
149 $chkcat[$i] = 'consist';
151 } elsif ( /^(__gnu_compiled_c|gcc2_compiled\.)/ ) {
154 } elsif ( /^ErrorIO_call_count/ # HACK!!!!
155 || /^[A-Za-z0-9_]+\.\d+$/
156 || /^.*_CAT/ # PROF: _entryname_CAT
157 || /^CC_.*_struct/ # PROF: _CC_ccident_struct
158 || /^.*_done/ # PROF: _module_done
159 || /^_module_registered/ # PROF: _module_registered
162 $chkcat[$i] = 'data';
165 } elsif ( /^([A-Za-z0-9_]+)\s+\.comm/ ) {
170 } elsif ( /^[A-Za-z0-9_]/ && ! /^L\$\d+$/) {
173 print STDERR "Funny global thing?: $_"
174 unless $KNOWN_FUNNY_THING{$thing}
175 || /^_(PRIn|PRStart)/ # pointer reversal GC routines
176 || /^CC_.*/ # PROF: _CC_ccident
177 || /^_reg.*/; # PROF: _reg<module>
179 $chkcat[$i] = 'misc';
182 } else { # simple line (duplicated at the top)
186 $numchks = $#chk + 1;
188 # print STDERR "\nCLOSURES:\n";
189 # foreach $s (sort (keys %closurechk)) {
190 # print STDERR "$s:\t\t",$closurechk{$s},"\n";
192 # print STDERR "\nNORMAL INFOS:\n";
193 # foreach $s (sort (keys %infochk)) {
194 # print STDERR "$s:\t\t",$infochk{$s},"\n";
196 # print STDERR "SLOWS:\n";
197 # foreach $s (sort (keys %slowchk)) {
198 # print STDERR "$s:\t\t",$slowchk{$s},"\n";
200 # print STDERR "\nFASTS:\n";
201 # foreach $s (sort (keys %fastchk)) {
202 # print STDERR "$s:\t\t",$fastchk{$s},"\n";
205 # the division into chunks is imperfect;
206 # we throw some things over the fence into the next
209 # also, there are things we would like to know
210 # about the whole module before we start spitting
213 # NB: we start meddling at chunk 1, not chunk 0
215 for ($i = 1; $i < $numchks; $i++) {
216 $c = $chk[$i]; # convenience copy
218 # print STDERR "\nCHK $i (BEFORE):\n", $c;
220 # toss all prologue stuff
221 $c =~ s/^\s+\.ENTRY[^\0]*--- BEGIN ---/\t.ENTRY/;
223 # Lie about our .CALLINFO
224 $c =~ s/^\s+\.CALLINFO.*$/\t.CALLINFO NO_CALLS,NO_UNWIND/;
231 # print STDERR "\nCHK $i (STEP 1):\n", $c;
233 # toss all epilogue stuff
234 $c =~ s/^\s+--- END ---[^\0]*\.EXIT/\t.EXIT/;
236 # print STDERR "\nCHK $i (STEP 2):\n", $c;
238 # Sorry; we moved the _info stuff to the code segment.
239 $c =~ s/_info,DATA/_info,CODE/g;
241 # pin a funny end-thing on (for easier matching):
242 $c .= 'FUNNY#END#THING';
244 # pick some end-things and move them to the next chunk
246 # print STDERR "\nCHK $i (STEP 3):\n", $c;
247 while ($c =~ /^(\s+\.(IMPORT|EXPORT|PARAM).*\n)FUNNY#END#THING/
248 || $c =~ /^(\s+\.align\s+\d+\n)FUNNY#END#THING/
249 || $c =~ /^(\s+\.(SPACE|SUBSPA)\s+\S+\n)FUNNY#END#THING/
250 || $c =~ /^(\s*\n)FUNNY#END#THING/ ) {
253 if ( $i < ($numchks - 1) && ($to_move =~ /^\s+\.(IMPORT|EXPORT)/
254 || ($to_move =~ /align/ && $chkcat[$i+1] eq 'literal')) ) {
255 $chk[$i + 1] = $to_move . $chk[$i + 1];
256 # otherwise they're tossed
258 $c =~ s/^.*\nFUNNY#END#THING/FUNNY#END#THING/;
260 # print STDERR "\nCHK $i (STEP 4):\n", $c;
262 $c =~ s/FUNNY#END#THING//;
263 $chk[$i] = $c; # update w/ convenience copy
266 # print out the header stuff first
268 print OUTASM $chk[0];
270 # print out all the literals second
272 for ($i = 1; $i < $numchks; $i++) {
273 if ( $chkcat[$i] eq 'literal' ) {
274 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n";
275 print OUTASM $chk[$i];
276 print OUTASM "; end literal\n"; # for the splitter
278 $chkcat[$i] = 'DONE ALREADY';
282 # print out all the bss third
284 for ($i = 1; $i < $numchks; $i++) {
285 if ( $chkcat[$i] eq 'bss' ) {
286 print OUTASM "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$BSS\$\n\t.align 4\n";
287 print OUTASM $chk[$i];
289 $chkcat[$i] = 'DONE ALREADY';
293 for ($i = 1; $i < $numchks; $i++) {
294 # print STDERR "$i: cat $chkcat[$i], symb $chksymb[$i]\n";
296 next if $chkcat[$i] eq 'DONE ALREADY';
298 if ( $chkcat[$i] eq 'misc' ) {
299 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
300 print OUTASM $chk[$i];
302 } elsif ( $chkcat[$i] eq 'data' ) {
303 print OUTASM "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$DATA\$\n\t\.align 4\n";
304 print OUTASM $chk[$i];
306 } elsif ( $chkcat[$i] eq 'consist' ) {
307 if ( $chk[$i] =~ /\.STRING.*\)(hsc|cc) (.*)\\x09(.*)\\x00/ ) {
308 local($consist) = "$1.$2.$3";
310 $consist =~ s/\//./g;
312 $consist =~ s/[^A-Za-z0-9_.]/ZZ/g; # ToDo: properly?
313 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n$consist\n";
315 print STDERR "Couldn't grok consistency: ", $chk[$i];
318 } elsif ( $chkcat[$i] eq 'splitmarker' ) {
319 # we can just re-constitute this one...
320 # ignore the final split marker, to save an empty object module
321 # Use _three_ underscores so that ghc-split doesn't get overly complicated
322 print OUTASM "___stg_split_marker$chksymb[$i]\n";
324 } elsif ( $chkcat[$i] eq 'closure'
325 || $chkcat[$i] eq 'infotbl'
326 || $chkcat[$i] eq 'slow'
327 || $chkcat[$i] eq 'fast' ) { # do them in that order
328 $symb = $chksymb[$i];
331 if ( defined($closurechk{$symb}) ) {
332 print OUTASM "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$DATA\$\n\t\.align 4\n";
333 print OUTASM $chk[$closurechk{$symb}];
334 $chkcat[$closurechk{$symb}] = 'DONE ALREADY';
338 if ( defined($infochk{$symb}) ) {
339 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
340 print OUTASM &rev_tbl($symb, $chk[$infochk{$symb}], 1);
341 # entry code will be put here!
342 # if ( $chk[$infochk{$symb}] =~ /\.word\s+([A-Za-z0-9_]+_entry)$/
343 # && $1 ne "_${symb}_entry" ) {
344 # print STDERR "!!! entry point???\n",$chk[$infochk{$symb}];
347 $chkcat[$infochk{$symb}] = 'DONE ALREADY';
351 if ( defined($slowchk{$symb}) ) {
353 # teach it to drop through to the fast entry point:
354 $c = $chk[$slowchk{$symb}];
355 if ( defined($fastchk{$symb}) ) {
356 $c =~ s/^\s+ldil.*\n\s+ldo.*\n\s+bv.*\n(.*\n)?\s+\.EXIT/$1\t.EXIT/;
359 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
361 $chkcat[$slowchk{$symb}] = 'DONE ALREADY';
365 if ( defined($fastchk{$symb}) ) {
366 $c = $chk[$fastchk{$symb}];
367 if ( ! defined($slowchk{$symb}) ) {
368 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
371 $chkcat[$fastchk{$symb}] = 'DONE ALREADY';
374 } elsif ( $chkcat[$i] eq 'vector'
375 || $chkcat[$i] eq 'direct' ) { # do them in that order
376 $symb = $chksymb[$i];
379 if ( defined($vectorchk{$symb}) ) {
380 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
381 print OUTASM &rev_tbl($symb, $chk[$vectorchk{$symb}], 0);
382 # direct return code will be put here!
383 $chkcat[$vectorchk{$symb}] = 'DONE ALREADY';
387 if ( defined($directchk{$symb}) ) {
388 print OUTASM "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
389 print OUTASM $chk[$directchk{$symb}];
390 $chkcat[$directchk{$symb}] = 'DONE ALREADY';
393 &tidy_up_and_die(1,"$Pgm: unknown chkcat (ghc-asm hppa)\n$chkcat[$i]\n$chk[$i]\n");
398 close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
399 close(INASM) || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
403 The HP is a major nuisance. The threaded code mangler moved info tables
404 from data space to code space, but unthreaded code in the RTS still has
405 references to info tables in data space. Since the HP linker is very precise
406 about where symbols live, we need to patch the references in the unthreaded
411 sub mini_mangle_asm {
412 local($in_asmf, $out_asmf) = @_;
414 open(INASM, "< $in_asmf")
415 || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
416 open(OUTASM,"> $out_asmf")
417 || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
420 s/_info,DATA/_info,CODE/; # Move _info references to code space
426 close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
427 close(INASM) || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
433 sub init_FUNNY_THINGS {
434 %KNOWN_FUNNY_THING = (
436 'CommonUnderflow', 1,
439 'ErrorIO_call_count', 1,
440 'ErrorIO_innards', 1,
451 'StackUnderflowEnterNode', 1,
463 'WorldStateToken', 1,
464 '_Enter_Internal', 1,
465 '_PRMarking_MarkNextAStack', 1,
466 '_PRMarking_MarkNextBStack', 1,
467 '_PRMarking_MarkNextCAF', 1,
468 '_PRMarking_MarkNextGA', 1,
469 '_PRMarking_MarkNextRoot', 1,
470 '_PRMarking_MarkNextSpark', 1,
471 '_Scavenge_Forward_Ref', 1,
472 '__std_entry_error__', 1,
473 '_startMarkWorld', 1,
475 'startCcRegisteringWorld', 1,
476 'startEnterFloat', 1,
485 The following table reversal is used for both info tables and return
486 vectors. In both cases, we remove the first entry from the table,
487 reverse the table, put the label at the end, and paste some code
488 (that which is normally referred to by the first entry in the table)
489 right after the table itself. (The code pasting is done elsewhere.)
493 local($symb, $tbl, $discard1) = @_;
497 local(@imports) = ();
500 local(@lines) = split(/\n/, $tbl);
503 for ($i = 0; $i <= $#lines && $lines[$i] !~ /^\s+\.word\s+/; $i++) {
504 $label .= $lines[$i] . "\n",
505 next if $lines[$i] =~ /^[A-Za-z0-9_]+$/
506 || $lines[$i] =~ /^\s+\.EXPORT/;
508 $before .= $lines[$i] . "\n"; # otherwise...
511 for ( ; $i <= $#lines && $lines[$i] =~ /^\s+\.(word|IMPORT)/; $i++) {
512 if ($lines[$i] =~ /^\s+\.IMPORT/) {
513 push(@imports, $lines[$i]);
515 # We don't use HP's ``function pointers''
516 # We just use labels in code space, like normal people
517 $lines[$i] =~ s/P%//;
518 push(@words, $lines[$i]);
521 # now throw away the first word (entry code):
526 for (; $i <= $#lines; $i++) {
527 $after .= $lines[$i] . "\n";
530 $tbl = $before . join("\n", @imports) . "\n" .
531 join("\n", (reverse @words)) . "\n" . $label . $after;
533 # print STDERR "before=$before\n";
534 # print STDERR "label=$label\n";
535 # print STDERR "words=",(reverse @words),"\n";
536 # print STDERR "after=$after\n";
542 %************************************************************************
544 \subsection[Driver-asm-info]{Collect interesting (static) info from an assembler file}
546 %************************************************************************
548 How many times is each asm instruction used?
551 %AsmInsn = (); # init
553 sub dump_asm_insn_counts {
556 open(INASM, "< $asmf") || &tidy_up_and_die(1,"$Pgm: failed to open `$asmf' (to read)\n");
558 if ( /^\t([a-z][a-z0-9]+)\b/ ) {
562 close(INASM) || &tidy_up_and_die(1,"Failed reading from $asmf\n");
564 # OK, now print what we collected (to stderr)
565 foreach $i (sort (keys %AsmInsn)) {
566 print STDERR "INSN:: $i\t",$AsmInsn{$i},"\n";
570 sub dump_asm_globals_info {
573 # make "require"r happy...