[project @ 1999-08-02 10:35:53 by simonmar]
[ghc-hetmet.git] / ghc / driver / ghc-asm.lprl
1 %************************************************************************
2 %*                                                                      *
3 \section[Driver-asm-fiddling]{Fiddling with assembler files}
4 %*                                                                      *
5 %************************************************************************
6
7 Tasks:
8 \begin{itemize}
9 \item
10 Utterly stomp out C functions' prologues and epilogues; i.e., the
11 stuff to do with the C stack.
12 \item
13 Any other required tidying up.
14 \end{itemize}
15
16 HPPA specific notes:
17 \begin{itemize}
18 \item
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.
27 \item
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.
41 \end{itemize}
42
43 %************************************************************************
44 %*                                                                      *
45 \subsection{Constants for various architectures}
46 %*                                                                      *
47 %************************************************************************
48
49 \begin{code}
50 sub init_TARGET_STUFF {
51
52     #--------------------------------------------------------#
53     if ( $TargetPlatform =~ /^alpha-.*-.*/ ) {
54
55     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
56     $T_US           = ''; # _ if symbols have an underscore on the front
57     $T_PRE_APP      = 'DONT THINK THIS APPLIES'; # regexp that says what comes before APP/NO_APP
58     $T_CONST_LBL    = '^\$C(\d+):$'; # regexp for what such a lbl looks like
59     $T_POST_LBL     = ':';
60
61     $T_MOVE_DIRVS   = '^(\s*(\.align\s+\d+|\.(globl|ent)\s+\S+|\#.*|\.(file|loc)\s+\S+\s+\S+|\.text|\.r?data)\n)';
62     $T_COPY_DIRVS   = '^\s*(\#|\.(file|globl|ent|loc))';
63
64     $T_hsc_cc_PAT   = '\.ascii.*\)(hsc|cc) (.*)\\\\11"\n\t\.ascii\s+"(.*)\\\\0"';
65     $T_DOT_WORD     = '\.quad';
66     $T_DOT_GLOBAL   = '^\t\.globl';
67     $T_HDR_literal  = "\.rdata\n\t\.align 3\n";
68     $T_HDR_misc     = "\.text\n\t\.align 3\n";
69     $T_HDR_data     = "\.data\n\t\.align 3\n";
70     $T_HDR_consist  = "\.text\n";
71     $T_HDR_closure  = "\.data\n\t\.align 3\n";
72     $T_HDR_srt      = "\.data\n\t\.align 3\n";
73     $T_HDR_info     = "\.text\n\t\.align 3\n";
74     $T_HDR_entry    = "\.text\n\t\.align 3\n";
75     $T_HDR_fast     = "\.text\n\t\.align 3\n";
76     $T_HDR_vector   = "\.text\n\t\.align 3\n";
77     $T_HDR_direct   = "\.text\n\t\.align 3\n";
78
79     #--------------------------------------------------------#
80     } elsif ( $TargetPlatform =~ /^hppa/ ) {
81
82     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
83     $T_US           = ''; # _ if symbols have an underscore on the front
84     $T_PRE_APP      = 'DONT THINK THIS APPLIES'; # regexp that says what comes before APP/NO_APP
85     $T_CONST_LBL    = '^L\$C(\d+)$'; # regexp for what such a lbl looks like
86     $T_POST_LBL     = '';
87
88     $T_MOVE_DIRVS   = '^((\s+\.(IMPORT|EXPORT|PARAM).*|\s+\.align\s+\d+|\s+\.(SPACE|SUBSPA)\s+\S+|\s*)\n)';
89     $T_COPY_DIRVS   = '^\s+\.(IMPORT|EXPORT)';
90
91     $T_hsc_cc_PAT   = '\.STRING.*\)(hsc|cc) (.*)\\\\x09(.*)\\\\x00"';
92     $T_DOT_WORD     = '\.(blockz|word|half|byte)';
93     $T_DOT_GLOBAL   = '^\s+\.EXPORT';
94     $T_HDR_literal  = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n";
95     $T_HDR_misc     = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
96     $T_HDR_data     = "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$DATA\$\n\t\.align 4\n";
97     $T_HDR_consist  = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$LIT\$\n";
98     $T_HDR_closure  = "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$DATA\$\n\t\.align 4\n";
99     $T_HDR_srt      = "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$DATA\$\n\t\.align 4\n";
100     $T_HDR_info     = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
101     $T_HDR_entry    = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
102     $T_HDR_fast     = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
103     $T_HDR_vector   = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
104     $T_HDR_direct   = "\t.SPACE \$TEXT\$\n\t.SUBSPA \$CODE\$\n\t\.align 4\n";
105
106     #--------------------------------------------------------#
107     } elsif ( $TargetPlatform =~ /^i386-.*-(linuxaout|freebsd2|nextstep3|cygwin32|mingw32)$/ ) {
108                                 # NeXT added but not tested. CaS
109
110     $T_STABBY       = 1; # 1 iff .stab things (usually if a.out format)
111     $T_US           = '_'; # _ if symbols have an underscore on the front
112     $T_PRE_APP      = '^#'; # regexp that says what comes before APP/NO_APP
113     $T_CONST_LBL    = '^LC(\d+):$';
114     $T_POST_LBL     = ':';
115     $T_X86_PRE_LLBL_PAT = 'L';
116     $T_X86_PRE_LLBL         = 'L';
117     $T_X86_BADJMP   = '^\tjmp [^L\*]';
118
119     $T_MOVE_DIRVS   = '^(\s*(\.(p2)?align\s+\d+(,0x90)?|\.globl\s+\S+|\.text|\.data|\.stab[^n].*|\.type\s+.*|\.size\s+.*)\n)';
120     $T_COPY_DIRVS   = '\.(globl|stab)';
121     $T_hsc_cc_PAT   = '\.ascii.*\)(hsc|cc) (.*)\\\\11"\n\t\.ascii\s+"(.*)\\\\0"';
122     $T_DOT_WORD     = '\.(long|word|value|byte|space)';
123     $T_DOT_GLOBAL   = '\.globl';
124     $T_HDR_literal  = "\.text\n\t\.align 2\n";
125     $T_HDR_misc     = "\.text\n\t\.align 2,0x90\n";
126     $T_HDR_data     = "\.data\n\t\.align 2\n";
127     $T_HDR_consist  = "\.text\n";
128     $T_HDR_closure  = "\.data\n\t\.align 2\n";
129     $T_HDR_closure  = "\.data\n\t\.align 2\n\t.long 0\n" if ( $TargetPlatform =~ /.*-mingw32$/ );
130     $T_HDR_srt      = "\.data\n\t\.align 2\n";
131     $T_HDR_info     = "\.text\n\t\.align 2\n"; # NB: requires padding
132     $T_HDR_entry    = "\.text\n"; # no .align so we're right next to _info (arguably wrong...?)
133     $T_HDR_fast     = "\.text\n\t\.align 2,0x90\n";
134     $T_HDR_vector   = "\.text\n\t\.align 2\n"; # NB: requires padding
135     $T_HDR_direct   = "\.text\n\t\.align 2,0x90\n";
136
137     #--------------------------------------------------------#
138     } elsif ( $TargetPlatform =~ /^i386-.*-(solaris2|linux|freebsd3)$/ ) {
139
140     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
141     $T_US           = ''; # _ if symbols have an underscore on the front
142     $T_PRE_APP      = # regexp that says what comes before APP/NO_APP
143                       ($TargetPlatform =~ /-(linux|freebsd3)$/) ? '#' : '/' ;
144     $T_CONST_LBL    = '^\.LC(\d+):$'; # regexp for what such a lbl looks like
145     $T_POST_LBL     = ':';
146     $T_X86_PRE_LLBL_PAT = '\.L';
147     $T_X86_PRE_LLBL         = '.L';
148     $T_X86_BADJMP   = '^\tjmp [^\.\*]';
149
150     $T_MOVE_DIRVS   = '^(\s*(\.(p2)?align\s+\d+(,0x90)?|\.globl\s+\S+|\.text|\.data|\.section\s+.*|\.type\s+.*|\.Lfe.*\n\t\.size\s+.*|\.size\s+.*|\.ident.*)\n)';
151     $T_COPY_DIRVS   = '\.(globl)';
152
153     if ( $TargetPlatform =~ /freebsd3/ ) {
154         $T_hsc_cc_PAT   = '\.ascii.*\)(hsc|cc) (.*)\\\\11"\n\t\.ascii\s+"(.*)\\\\0"';
155     } else {
156         $T_hsc_cc_PAT   = '\.string.*\)(hsc|cc) (.*)\\\\t(.*)"';
157     }
158
159     $T_DOT_WORD     = '\.(long|value|byte|zero)';
160     $T_DOT_GLOBAL   = '\.globl';
161     $T_HDR_literal  = "\.section\t\.rodata\n"; # or just use .text??? (WDP 95/11)
162     $T_HDR_misc     = "\.text\n\t\.align 4\n";
163     $T_HDR_data     = "\.data\n\t\.align 4\n"; # ToDo: change align??
164     $T_HDR_consist  = "\.text\n";
165     $T_HDR_closure  = "\.data\n\t\.align 4\n"; # ToDo: change align?
166     $T_HDR_srt      = "\.data\n\t\.align 4\n"; # ToDo: change align?
167     $T_HDR_info     = "\.text\n\t\.align 4\n"; # NB: requires padding
168     $T_HDR_entry    = "\.text\n"; # no .align so we're right next to _info (arguably wrong...?)
169     $T_HDR_fast     = "\.text\n\t\.align 4\n";
170     $T_HDR_vector   = "\.text\n\t\.align 4\n"; # NB: requires padding
171     $T_HDR_direct   = "\.text\n\t\.align 4\n";
172
173     #--------------------------------------------------------#
174     } elsif ( $TargetPlatform =~ /^m68k-.*-sunos4/ ) {
175
176     $T_STABBY       = 1; # 1 iff .stab things (usually if a.out format)
177     $T_US           = '_'; # _ if symbols have an underscore on the front
178     $T_PRE_APP      = '^# MAY NOT APPLY'; # regexp that says what comes before APP/NO_APP
179     $T_CONST_LBL    = '^LC(\d+):$';
180     $T_POST_LBL     = ':';
181
182     $T_MOVE_DIRVS   = '^(\s*(\.align\s+\d+|\.proc\s+\d+|\.const|\.cstring|\.globl\s+\S+|\.text|\.data|\.even|\.stab[^n].*)\n)';
183     $T_COPY_DIRVS   = '\.(globl|proc|stab)';
184     $T_hsc_cc_PAT   = '\.ascii.*\)(hsc|cc) (.*)\\\\11"\n\t\.ascii\s+"(.*)\\\\0"';
185
186     $T_DOT_WORD     = '\.long';
187     $T_DOT_GLOBAL   = '\.globl';
188     $T_HDR_literal  = "\.text\n\t\.even\n";
189     $T_HDR_misc     = "\.text\n\t\.even\n";
190     $T_HDR_data     = "\.data\n\t\.even\n";
191     $T_HDR_consist  = "\.text\n";
192     $T_HDR_closure  = "\.data\n\t\.even\n";
193     $T_HDR_srt      = "\.data\n\t\.even\n";
194     $T_HDR_info     = "\.text\n\t\.even\n";
195     $T_HDR_entry    = "\.text\n\t\.even\n";
196     $T_HDR_fast     = "\.text\n\t\.even\n";
197     $T_HDR_vector   = "\.text\n\t\.even\n";
198     $T_HDR_direct   = "\.text\n\t\.even\n";
199
200     #--------------------------------------------------------#
201     } elsif ( $TargetPlatform =~ /^mips-.*/ ) {
202
203     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
204     $T_US           = ''; # _ if symbols have an underscore on the front
205     $T_PRE_APP      = '^\s*#'; # regexp that says what comes before APP/NO_APP
206     $T_CONST_LBL    = '^\$LC(\d+):$'; # regexp for what such a lbl looks like
207     $T_POST_LBL     = ':';
208
209     $T_MOVE_DIRVS   = '^(\s*(\.align\s+\d+|\.(globl|ent)\s+\S+|\.text|\.r?data)\n)';
210     $T_COPY_DIRVS   = '\.(globl|ent)';
211
212     $T_hsc_cc_PAT   = 'I WAS TOO LAZY TO DO THIS BIT (WDP 95/05)';
213     $T_DOT_WORD     = '\.word';
214     $T_DOT_GLOBAL   = '^\t\.globl';
215     $T_HDR_literal  = "\t\.rdata\n\t\.align 2\n";
216     $T_HDR_misc     = "\t\.text\n\t\.align 2\n";
217     $T_HDR_data     = "\t\.data\n\t\.align 2\n";
218     $T_HDR_consist  = 'TOO LAZY TO DO THIS TOO';
219     $T_HDR_closure  = "\t\.data\n\t\.align 2\n";
220     $T_HDR_srt      = "\t\.data\n\t\.align 2\n";
221     $T_HDR_info     = "\t\.text\n\t\.align 2\n";
222     $T_HDR_entry    = "\t\.text\n\t\.align 2\n";
223     $T_HDR_fast     = "\t\.text\n\t\.align 2\n";
224     $T_HDR_vector   = "\t\.text\n\t\.align 2\n";
225     $T_HDR_direct   = "\t\.text\n\t\.align 2\n";
226
227     #--------------------------------------------------------#
228     } elsif ( $TargetPlatform =~ /^powerpc-.*|^rs6000-.*/ ) {
229
230     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
231     $T_US           = ''; # _ if symbols have an underscore on the front
232     $T_PRE_APP      = 'NOT APPLICABLE'; # regexp that says what comes before APP/NO_APP
233     $T_CONST_LBL    = 'NOT APPLICABLE'; # regexp for what such a lbl looks like
234     $T_POST_LBL     = ':';
235
236     $T_MOVE_DIRVS   = '^(\s*(\.toc|\.align \d+|\.csect \S+|\t\.?l?globl \S+)\n)';
237     $T_COPY_DIRVS   = '\.(l?globl)';
238
239     $T_hsc_cc_PAT   = '\.byte.*\)(hsc|cc) (.*)"\n\t\.byte \d+\n\t\.byte "(.*)"\n\t\.byte \d+';
240     $T_DOT_WORD     = '\.long';
241     $T_DOT_GLOBAL   = '\.globl';
242     $T_HDR_toc      = "\.toc\n";
243     $T_HDR_literal  = "\.csect .data[RW]\n\t\.align 2\n";               #not RO!?
244     $T_HDR_misc     = "# misc\n\.csect \.text[PR]\n\t\.align 2\n";
245     $T_HDR_data     = "# data\n\.csect \.data[RW]\n\t\.align 2\n";
246     $T_HDR_consist  = "# consist\n\.csect \.data[RW]\n\t\.align 2\n";
247     $T_HDR_closure  = "# closure\n\.csect \.data[RW]\n\t\.align 2\n";
248     $T_HDR_srt      = "# closure\n\.csect \.data[RW]\n\t\.align 2\n";
249     $T_HDR_info     = "# info\n\.csect \.data[RW]\n\t\.align 2\n"; #not RO!?
250     $T_HDR_entry    = "# entry\n\.csect \.text[PR]\n\t\.align 2\n";
251     $T_HDR_fast     = "# fast\n\.csect \.text[PR]\n\t\.align 2\n";
252     $T_HDR_vector   = "# vector\n\.csect \.data[RW]\n\t\.align 2\n"; #not RO!?
253     $T_HDR_direct   = "# direct\n";
254
255     #--------------------------------------------------------#
256     } elsif ( $TargetPlatform =~ /^sparc-.*-solaris2/ ) {
257
258     $T_STABBY       = 0; # 1 iff .stab things (usually if a.out format)
259     $T_US           = ''; # _ if symbols have an underscore on the front
260     $T_PRE_APP      = 'DOES NOT SEEM TO APPLY'; # regexp that says what comes before APP/NO_APP
261     $T_CONST_LBL    = '^\.LLC(\d+):$'; # regexp for what such a lbl looks like
262     $T_POST_LBL     = ':';
263
264     $T_MOVE_DIRVS   = '^((\s+\.align\s+\d+|\s+\.proc\s+\d+|\s+\.global\s+\S+|\.text|\.data|\.stab.*|\.section.*|\s+\.type.*|\s+\.size.*)\n)';
265     $T_COPY_DIRVS   = '\.(global|proc|stab)';
266
267     $T_hsc_cc_PAT   = '\.asciz.*\)(hsc|cc) (.*)\\\\t(.*)"';
268     $T_DOT_WORD     = '\.(word|byte|half|skip|uahalf|uaword)';
269     $T_DOT_GLOBAL   = '^\t\.global';
270     $T_HDR_literal  = "\.text\n\t\.align 8\n";
271     $T_HDR_misc     = "\.text\n\t\.align 4\n";
272     $T_HDR_data     = "\.data\n\t\.align 8\n";
273     $T_HDR_consist  = "\.text\n";
274     $T_HDR_closure  = "\.data\n\t\.align 4\n";
275     $T_HDR_srt      = "\.data\n\t\.align 4\n";
276     $T_HDR_info     = "\.text\n\t\.align 4\n";
277     $T_HDR_entry    = "\.text\n\t\.align 4\n";
278     $T_HDR_fast     = "\.text\n\t\.align 4\n";
279     $T_HDR_vector   = "\.text\n\t\.align 4\n";
280     $T_HDR_direct   = "\.text\n\t\.align 4\n";
281
282     #--------------------------------------------------------#
283     } elsif ( $TargetPlatform =~ /^sparc-.*-sunos4/ ) {
284
285     $T_STABBY       = 1; # 1 iff .stab things (usually if a.out format)
286     $T_US           = '_'; # _ if symbols have an underscore on the front
287     $T_PRE_APP      = '^# DOES NOT SEEM TO APPLY'; # regexp that says what comes before APP/NO_APP
288     $T_CONST_LBL    = '^LC(\d+):$';
289     $T_POST_LBL     = ':';
290
291     $T_MOVE_DIRVS   = '^((\s+\.align\s+\d+|\s+\.proc\s+\d+|\s+\.global\s+\S+|\.text|\.data|\.stab.*)\n)';
292     $T_COPY_DIRVS   = '\.(global|proc|stab)';
293     $T_hsc_cc_PAT   = '\.ascii.*\)(hsc|cc) (.*)\\\\11"\n\t\.ascii\s+"(.*)\\\\0"';
294
295     $T_DOT_WORD     = '\.word';
296     $T_DOT_GLOBAL   = '^\t\.global';
297     $T_HDR_literal  = "\.text\n\t\.align 8\n";
298     $T_HDR_misc     = "\.text\n\t\.align 4\n";
299     $T_HDR_data     = "\.data\n\t\.align 8\n";
300     $T_HDR_consist  = "\.text\n";
301     $T_HDR_closure  = "\.data\n\t\.align 4\n";
302     $T_HDR_srt      = "\.data\n\t\.align 4\n";
303     $T_HDR_info     = "\.text\n\t\.align 4\n";
304     $T_HDR_entry    = "\.text\n\t\.align 4\n";
305     $T_HDR_fast     = "\.text\n\t\.align 4\n";
306     $T_HDR_vector   = "\.text\n\t\.align 4\n";
307     $T_HDR_direct   = "\.text\n\t\.align 4\n";
308
309     #--------------------------------------------------------#
310     } else {
311         print STDERR "$Pgm: don't know how to mangle assembly language for: $TargetPlatform\n";
312         exit 1;
313     }
314
315 if ( 0 ) {
316 print STDERR "T_STABBY: $T_STABBY\n";
317 print STDERR "T_US: $T_US\n";
318 print STDERR "T_PRE_APP: $T_PRE_APP\n";
319 print STDERR "T_CONST_LBL: $T_CONST_LBL\n";
320 print STDERR "T_POST_LBL: $T_POST_LBL\n";
321 if ( $TargetPlatform =~ /^i386-/ ) {
322     print STDERR "T_X86_PRE_LLBL_PAT: $T_X86_PRE_LLBL_PAT\n";
323     print STDERR "T_X86_PRE_LLBL: $T_X86_PRE_LLBL\n";
324     print STDERR "T_X86_BADJMP: $T_X86_BADJMP\n";
325 }
326 print STDERR "T_MOVE_DIRVS: $T_MOVE_DIRVS\n";
327 print STDERR "T_COPY_DIRVS: $T_COPY_DIRVS\n";
328 print STDERR "T_hsc_cc_PAT: $T_hsc_cc_PAT\n";
329 print STDERR "T_DOT_WORD: $T_DOT_WORD\n";
330 print STDERR "T_HDR_literal: $T_HDR_literal\n";
331 print STDERR "T_HDR_misc: $T_HDR_misc\n";
332 print STDERR "T_HDR_data: $T_HDR_data\n";
333 print STDERR "T_HDR_consist: $T_HDR_consist\n";
334 print STDERR "T_HDR_closure: $T_HDR_closure\n";
335 print STDERR "T_HDR_info: $T_HDR_info\n";
336 print STDERR "T_HDR_entry: $T_HDR_entry\n";
337 print STDERR "T_HDR_fast: $T_HDR_fast\n";
338 print STDERR "T_HDR_vector: $T_HDR_vector\n";
339 print STDERR "T_HDR_direct: $T_HDR_direct\n";
340 }
341
342 }
343 \end{code}
344
345 %************************************************************************
346 %*                                                                      *
347 \subsection{Mangle away}
348 %*                                                                      *
349 %************************************************************************
350
351 \begin{code}
352 sub mangle_asm {
353     local($in_asmf, $out_asmf) = @_;
354
355     # multi-line regexp matching:
356     local($*) = 1;
357     local($i, $c);
358
359
360     &init_TARGET_STUFF();
361     &init_FUNNY_THINGS();
362
363     # perl4 on alphas SEGVs when give ${foo} substitutions in patterns.
364     # To avoid them we declare some locals that allows to avoid using curlies.
365     local($TUS)      = ${T_US};
366     local($TPOSTLBL) = ${T_POST_LBL};
367     local($TMOVEDIRVS) = ${T_MOVE_DIRVS};
368     local($TPREAPP)    = ${T_PRE_APP};
369     local($TCOPYDIRVS) = ${T_COPY_DIRVS};
370     local($TDOTWORD)   = ${T_DOT_WORD};
371
372     open(INASM, "< $in_asmf")
373         || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
374     open(OUTASM,"> $out_asmf")
375         || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
376
377     # read whole file, divide into "chunks":
378     #   record some info about what we've found...
379
380     @chk = ();          # contents of the chunk
381     $numchks = 0;       # number of them
382     @chkcat = ();       # what category of thing in each chunk
383     @chksymb = ();      # what symbol(base) is defined in this chunk
384     %slowchk = ();      # ditto, its regular "slow" entry code
385     %fastchk = ();      # ditto, fast entry code
386     %closurechk = ();   # ditto, the (static) closure
387     %srtchk = ();       # ditto, its SRT (for top-level things)
388     %infochk = ();      # given a symbol base, say what chunk its info tbl is in
389     %vectorchk = ();    # ditto, return vector table
390     $EXTERN_DECLS = ''; # .globl <foo> .text (MIPS only)
391
392     $i = 0; $chkcat[0] = 'misc'; $chk[0] = '';
393
394     while (<INASM>) {
395         next if $T_STABBY && /^\.stab.*$TUS[@]?__stg_split_marker/o;
396         next if $T_STABBY && /^\.stab.*ghc.*c_ID/;
397         next if /^\t\.def.*endef$/;
398         next if /$TPREAPP(NO_)?APP/o; 
399         next if /^;/ && $TargetPlatform =~ /^hppa/;
400
401         next if /(^$|^\t\.file\t|^ # )/ && $TargetPlatform =~ /^(mips|powerpc|rs6000)-/;
402
403         last if /^_section_\.text:$/ && $TargetPlatform =~ /^powerpc-|^rs6000-/;
404
405         if ( $TargetPlatform =~ /^mips-/ 
406           && /^\t\.(globl \S+ \.text|comm\t)/ ) {
407             $EXTERN_DECLS .= $_ unless /(__DISCARD__|\b(PK_|ASSIGN_)(FLT|DBL)\b)/;
408   
409         # As a temporary solution for compiling "foreign export" declarations,
410         # we use global variables to pass arguments from C to STG land.
411         # These declarations live in the .hc file and not in the generated C
412         # stub file, so we let them pass through here.
413         } elsif ( /^\t\.comm\t__fexp_.*$/ ) {
414             $chk[++$i]   = $_;
415             $chkcat[$i]  = 'data';
416             $chksymb[$i] = '';
417
418         } elsif ( /^\s+/ ) { # most common case first -- a simple line!
419             # duplicated from the bottom
420
421             $chk[$i] .= $_;
422
423         } elsif ( /\.\.ng:$/ && $TargetPlatform =~ /^alpha-/ ) {
424             # Alphas: Local labels not to be confused with new chunks
425             $chk[$i] .= $_;
426   
427         # NB: all the rest start with a non-space
428
429         } elsif ( $TargetPlatform =~ /^mips-/
430                && /^\d+:/ ) { # a funny-looking very-local label
431             $chk[$i] .= $_;
432
433         } elsif ( /$T_CONST_LBL/o ) {
434             $chk[++$i]   = $_;
435             $chkcat[$i]  = 'literal';
436             $chksymb[$i] = $1;
437
438         } elsif ( /^$TUS[@]?__stg_split_marker(\d+)$TPOSTLBL[@]?$/o ) {
439             $chk[++$i]   = $_;
440             $chkcat[$i]  = 'splitmarker';
441             $chksymb[$i] = $1;
442
443         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_info$TPOSTLBL[@]?$/o ) {
444             $symb = $1;
445             $chk[++$i]   = $_;
446             $chkcat[$i]  = 'infotbl';
447             $chksymb[$i] = $symb;
448
449             die "Info table already? $symb; $i\n" if defined($infochk{$symb});
450
451             $infochk{$symb} = $i;
452
453         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_(entry|ret)$TPOSTLBL[@]?$/o ) {
454             $chk[++$i]   = $_;
455             $chkcat[$i]  = 'slow';
456             $chksymb[$i] = $1;
457
458             $slowchk{$1} = $i;
459
460         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_fast\d*$TPOSTLBL[@]?$/o ) {
461             $chk[++$i]   = $_;
462             $chkcat[$i]  = 'fast';
463             $chksymb[$i] = $1;
464
465             $fastchk{$1} = $i;
466
467         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_closure$TPOSTLBL[@]?$/o ) {
468             $chk[++$i]   = $_;
469             $chkcat[$i]  = 'closure';
470             $chksymb[$i] = $1;
471
472             $closurechk{$1} = $i;
473
474         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_srt$TPOSTLBL[@]?$/o ) {
475             $chk[++$i]   = $_;
476             $chkcat[$i]  = 'srt';
477             $chksymb[$i] = $1;
478
479             $srtchk{$1} = $i;
480
481         } elsif ( /^$TUS[@]?ghc.*c_ID$TPOSTLBL/o ) {
482             $chk[++$i]  = $_;
483             $chkcat[$i] = 'consist';
484
485         } elsif ( /^($TUS[@]?__gnu_compiled_c|gcc2_compiled\.)$TPOSTLBL/o ) {
486             ; # toss it
487
488         } elsif ( /^$TUS[A-Za-z0-9_]+\.\d+$TPOSTLBL[@]?$/o
489                || /^$TUS[@]?.*_CAT$TPOSTLBL[@]?$/o              # PROF: _entryname_CAT
490                || /^$TUS[@]?CC_.*_struct$TPOSTLBL[@]?$/o        # PROF: _CC_ccident_struct
491                || /^$TUS[@]?.*_done$TPOSTLBL[@]?$/o             # PROF: _module_done
492                || /^$TUS[@]?_module_registered$TPOSTLBL[@]?$/o  # PROF: _module_registered
493                ) {
494             $chk[++$i]   = $_;
495             $chkcat[$i]  = 'data';
496             $chksymb[$i] = '';
497
498         } elsif ( /^([A-Za-z0-9_]+)\s+\.comm/ && $TargetPlatform =~ /^hppa/ ) {
499             $chk[++$i]   = $_;
500             $chkcat[$i]  = 'bss';
501             $chksymb[$i] = '';
502
503         } elsif ( $TargetPlatform =~ /^powerpc-|^rs6000-/ && /^LC\.\.([0-9]+)/ ) {
504             $chk[++$i]   = $_;
505             $chkcat[$i]  = 'toc';
506             $chksymb[$i] = $1;
507
508         } elsif ( /^$TUS[@]?CC(S)?_.*$/ ) {
509             # all CC_ symbols go in the data section...
510             $chk[++$i]   = $_;
511             $chkcat[$i]  = 'data';
512             $chksymb[$i] = '';
513
514         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_(alt|dflt)$TPOSTLBL[@]?$/o ) {
515             $chk[++$i]   = $_;
516             $chkcat[$i]  = 'misc';
517             $chksymb[$i] = '';
518             #$symbtmp = $1;
519             #$chksymb[$i] = $symbtmp if ($TargetPlatform =~ /^powerpc-|^rs6000-/) ; #rm andre
520
521         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_vtbl$TPOSTLBL[@]?$/o ) {
522             $chk[++$i]   = $_;
523             $chkcat[$i]  = 'vector';
524             $chksymb[$i] = $1;
525
526             $vectorchk{$1} = $i;
527
528         # As a temporary solution for compiling "foreign export" declarations,
529         # we use global variables to pass arguments from C to STG land.
530         # These declarations live in the .hc file and not in the generated C
531         # stub file, so we let them pass through here.
532         } elsif ( /^[\t ]+\.comm[\t ]+__fexp_.*$/ ) {
533             $chk[++$i]   = $_;
534             $chkcat[$i]  = 'data';
535             $chksymb[$i] = '';
536
537         } elsif ( $TargetPlatform =~ /^i386-.*-solaris2/
538              &&   /^(_uname|uname|stat|fstat):/ ) {
539             # for some utterly bizarre reason, this platform
540             # likes to drop little local C routines with these names
541             # into each and every .o file that #includes the
542             # relevant system .h file.  Yuck.  We just don't
543             # tolerate them in .hc files (which we are processing
544             # here).  If you need to call one of these things from
545             # Haskell, make a call to your own C wrapper, then
546             # put that C wrapper (which calls one of these) in a
547             # plain .c file.  WDP 95/12
548             $chk[++$i]   = $_;
549             $chkcat[$i]  = 'toss';
550             $chksymb[$i] = $1;
551
552         } elsif ( /^$TUS[@]?[A-Za-z0-9_]/o
553                 && ( $TargetPlatform !~ /^hppa/ # need to avoid local labels in this case
554                    || ! /^L\$\d+$/ )
555                 && ( $TargetPlatform !~ /^powerpc|^rs6000/ # ditto
556                    || ! /^(L\.\.\d+|LT\.\..*):$/ ) ) {
557             local($thing);
558             chop($thing = $_);
559             print "Funny global thing?: $_"
560                 unless $KNOWN_FUNNY_THING{$thing}
561                     || /^$TUS[@]?stg_.*$TPOSTLBL[@]?$/o    # RTS internals
562                     || /^$TUS[@]__fexp_.*$TPOSTLBL$/o      # foreign export
563                     || /^$TUS[@]?_reg.*$TPOSTLBL$/o        # PROF: __reg<module>
564                     || /^$TUS[@]?.*_btm$TPOSTLBL$/o        # large bitmaps
565                     || /^$TUS[@]?.*_closure_tbl$TPOSTLBL$/o; # closure tables
566             $chk[++$i]   = $_;
567             $chkcat[$i]  = 'misc';
568             if ($TargetPlatform =~ /^powerpc-|^rs6000-/) 
569                { $chksymb[$i] = $thing; }
570             else { $chksymb[$i] = ''; };
571
572         } else { # simple line (duplicated at the top)
573
574             $chk[$i] .= $_;
575         }
576     }
577     $numchks = $#chk + 1;
578
579     # the division into chunks is imperfect;
580     # we throw some things over the fence into the next
581     # chunk.
582     #
583     # also, there are things we would like to know
584     # about the whole module before we start spitting
585     # output.
586
587     local($FIRST_MANGLABLE) = ($TargetPlatform =~ /^(alpha-|hppa|mips-)/) ? 1 : 0;
588
589 #   print STDERR "first chunk to mangle: $FIRST_MANGLABLE\n";
590
591     # Alphas: NB: we start meddling at chunk 1, not chunk 0
592     # The first ".rdata" is quite magical; as of GCC 2.7.x, it
593     # spits a ".quad 0" in after the v first ".rdata"; we
594     # detect this special case (tossing the ".quad 0")!
595     local($magic_rdata_seen) = 0;
596   
597     # HPPAs, MIPSen: also start medding at chunk 1
598
599     if ($TargetPlatform =~ /^powerpc|^rs6000/) {
600        print OUTASM $T_HDR_toc; # yes, we have to put a .toc 
601                                 # in the beginning of every file!
602     %tocequiv = ();          # maps toc symbol number to toc symbol
603     %revtocequiv = ();       # maps toc symbol to toc symbol number
604     for ($i = 1; $i < $numchks; $i++) {
605         $chk[$i] =~ s/\[RW\]//g;
606         $chk[$i] =~ s/\[DS\]//g;
607         $chk[$i] =~ s/^\.csect .*\[DS\]$//g;
608
609         if ( $chkcat[$i] eq 'toc' && $chk[$i] !~ /\.byte/ )
610 #ToDo: instead of all these changes, just fix mangle_powerpc_tailjump and delete/ignore these tocs?
611            { $chk[$i] =~ s/$T_MOVE_DIRVS//g;
612              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(\S+_fast\d+)/\t\.tc \1\[TC\],\.\2/; 
613              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(\S+_entry)\n/\t\.tc \1\[TC\],\.\2\n/;
614              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(ret_\S+)/\t\.tc \1\[TC\],\.\2/;
615              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(alt_\S+)/\t\.tc \1\[TC\],\.\2/;
616              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(vtbl_\S+)/\t\.tc \1\[TC\],\.\2/;
617
618              $tocnumber = $chksymb[$i];
619              $tocsymb = $chk[$i];
620              $tocsymb =~ s/^LC\.\.\d+:\n//;
621              $tocsymb =~ s/^\t\.tc \S+,(\S+)\n/\1/;
622              $tocequiv{$tocnumber} = $tocsymb;
623
624            } elsif ( $chkcat[$i] eq 'toc' && $chk[$i] =~ /\.byte/ ) {
625              $chkcat[$i] = 'literal';
626            }
627     }
628     };
629
630     for ($i = $FIRST_MANGLABLE; $i < $numchks; $i++) {
631         $c = $chk[$i]; # convenience copy
632
633 #       print STDERR "\nCHK $i (BEFORE) (",$chkcat[$i],"):\n", $c;
634
635         # toss all prologue stuff; HPPA is pretty weird
636         # (see elsewhere)
637         $c = &mash_hppa_prologue($c) if $TargetPlatform =~ /^hppa/;
638
639         # be slightly paranoid to make sure there's
640         # nothing surprising in there
641         if ( $c =~ /--- BEGIN ---/ ) {
642             if (($p, $r) = split(/--- BEGIN ---/, $c)) {
643
644                 if ($TargetPlatform =~ /^i386-/) {
645                     $p =~ s/^\tpushl \%edi\n//;
646                     $p =~ s/^\tpushl \%esi\n//;
647                     $p =~ s/^\tpushl \%ebx\n//;
648                     $p =~ s/^\tsubl \$\d+,\%esp\n//;
649                     $p =~ s/^\tmovl \$\d+,\%eax\n\tcall __alloca\n// if ($TargetPlatform =~ /^.*-cygwin32/);
650                 } elsif ($TargetPlatform =~ /^m68k-/) {
651                     $p =~ s/^\tlink a6,#-?\d.*\n//;
652                     $p =~ s/^\tpea a6@\n\tmovel sp,a6\n//;    
653                                 # The above showed up in the asm code,
654                                 # so I added it here.
655                                 # I hope it's correct.
656                                 # CaS
657                     $p =~ s/^\tmovel d2,sp\@-\n//;
658                     $p =~ s/^\tmovel d5,sp\@-\n//; # SMmark.* only?
659                     $p =~ s/^\tmoveml \#0x[0-9a-f]+,sp\@-\n//; # SMmark.* only?
660                 } elsif ($TargetPlatform =~ /^mips-/) {
661                     # the .frame/.mask/.fmask that we use is the same
662                     # as that produced by GCC for miniInterpret; this
663                     # gives GDB some chance of figuring out what happened
664                     $FRAME = "\t.frame\t\$sp,2168,\$31\n\t.mask\t0x90000000,-4\n\t.fmask\t0x00000000,0\n";
665                     $p =~ s/^\t\.(frame).*\n/__FRAME__/g;
666                     $p =~ s/^\t\.(mask|fmask).*\n//g;
667                     $p =~ s/^\t\.cprestore.*\n/\t\.cprestore 416\n/; # 16 + 100 4-byte args
668                     $p =~ s/^\tsubu\t\$sp,\$sp,\d+\n//;
669                     $p =~ s/^\tsw\t\$31,\d+\(\$sp\)\n//;
670                     $p =~ s/^\tsw\t\$fp,\d+\(\$sp\)\n//;
671                     $p =~ s/^\tsw\t\$28,\d+\(\$sp\)\n//;
672                     $p =~ s/__FRAME__/$FRAME/;
673                 } elsif ($TargetPlatform =~ /^powerpc-|^rs6000/) {
674                     $p =~ s/^\tmflr 0\n//;
675                     $p =~ s/^\tstm \d+,-\d+\(1\)\n//;
676                     $p =~ s/^\tstw? 0,\d+\(1\)\n//g;
677                     $p =~ s/^\tstw? 1,\d+\(1\)\n//g; #mc
678                     $p =~ s/^\tlw?z 0,0\(1\)\n//g;   #mc
679                     $p =~ s/^\tstw?u 1,-\d+\(1\)\n//; 
680                     $p =~ s/^\tstw? \d+,-\d+\(1\)\n//g; 
681                     $p =~ s/^\tstfd \d+,-\d+\(1\)\n//g; 
682                 } else {
683                     print STDERR "$Pgm: unknown prologue mangling? $TargetPlatform\n";
684                 }
685
686                 die "Prologue junk?: $p\n" if $p =~ /^\t[^\.]/
687                     && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
688
689                 # glue together what's left
690                 $c = $p . $r;
691                 $c =~ s/\n\t\n/\n/; # junk blank line
692             }
693         }
694
695         if ( $TargetPlatform =~ /^mips-/ ) {
696             # MIPS: first, this basic sequence may occur "--- END ---" or not
697             $c =~ s/^\tlw\t\$31,\d+\(\$sp\)\n\taddu\t\$sp,\$sp,\d+\n\tj\t\$31\n\t\.end/\t\.end/;
698         }
699
700         # toss all epilogue stuff; again, paranoidly
701         if ( $c =~ /--- END ---/ ) {
702             if (($r, $e) = split(/--- END ---/, $c)) {
703                 if ($TargetPlatform =~ /^i386-/) {
704                     $e =~ s/^\tret\n//;
705                     $e =~ s/^\tpopl \%edi\n//;
706                     $e =~ s/^\tpopl \%esi\n//;
707                     $e =~ s/^\tpopl \%ecx\n//;
708                     $e =~ s/^\taddl \$\d+,\%esp\n//;
709                 } elsif ($TargetPlatform =~ /^m68k-/) {
710                     $e =~ s/^\tunlk a6\n//;
711                     $e =~ s/^\trts\n//;
712                 } elsif ($TargetPlatform =~ /^mips-/) {
713                     $e =~ s/^\tlw\t\$31,\d+\(\$sp\)\n//;
714                     $e =~ s/^\tlw\t\$fp,\d+\(\$sp\)\n//;
715                     $e =~ s/^\taddu\t\$sp,\$sp,\d+\n//;
716                     $e =~ s/^\tj\t\$31\n//;
717                 } elsif ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
718                     $e =~ s/^\taddi 1,1,\d+\n//;
719                     $e =~ s/^\tcal 1,\d+\(1\)\n//;
720                     $e =~ s/^\tlw?z? \d+,\d+\(1\)\n//; 
721                     $e =~ s/^\tmtlr 0\n//;
722                     $e =~ s/^\tbl?r\n//;
723                 } else {
724                     print STDERR "$Pgm: unknown epilogue mangling? $TargetPlatform\n";
725                 }
726                 die "Epilogue junk?: $e\n" if $e =~ /^\t[^\.]/
727                     && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
728
729                 # glue together what's left
730                 $c = $r . $e;
731                 $c =~ s/\n\t\n/\n/; # junk blank line
732             }
733         }
734
735         # On SPARCs, we don't do --- BEGIN/END ---, we just
736         # toss the register-windowing save/restore/ret* instructions
737         # directly:
738         if ( $TargetPlatform =~ /^sparc-/ ) {
739             $c =~ s/^\t(save .*|restore|ret|retl)\n//g;
740             # throw away PROLOGUE comments
741             $c =~ s/^\t!#PROLOGUE# 0\n\t!#PROLOGUE# 1\n//;
742         }
743
744         # On Alphas, the prologue mangling is done a little later (below)
745
746         # toss all calls to __DISCARD__
747         $c =~ s/^\t(call|jbsr|jal)\s+$TUS[@]?__DISCARD__\n//go;
748
749         # MIPS: that may leave some gratuitous asm macros around
750         # (no harm done; but we get rid of them to be tidier)
751         $c =~ s/^\t\.set\tnoreorder\n\t\.set\tnomacro\n\taddu\t(\S+)\n\t\.set\tmacro\n\t\.set\treorder\n/\taddu\t$1\n/
752             if $TargetPlatform =~ /^mips-/;
753
754         # toss stack adjustment after DoSparks
755         $c =~ s/^(\tjbsr _DoSparks\n)\taddqw #8,sp/$1/g
756                 if $TargetPlatform =~ /^m68k-/; # this looks old...
757
758         if ( $TargetPlatform =~ /^alpha-/ &&
759            ! $magic_rdata_seen &&
760            $c =~ /^\s*\.rdata\n\t\.quad 0\n\t\.align \d\n/ ) {
761             $c =~ s/^\s*\.rdata\n\t\.quad 0\n\t\.align (\d)\n/\.rdata\n\t\.align $1\n/;
762             $magic_rdata_seen = 1;
763         }
764
765         # pick some end-things and move them to the next chunk
766
767         # pin a funny end-thing on (for easier matching):
768         $c .= 'FUNNY#END#THING';
769
770         while ( $c =~ /$TMOVEDIRVS[@]?FUNNY#END#THING/o ) {  # [@]? is a silly hack to avoid having to use curlies for T_PRE_APP
771                                                            # (this SEGVs perl4 on alphas, you see)
772
773             $to_move = $1;
774             if ( $i < ($numchks - 1)
775               && ( $to_move =~ /$TCOPYDIRVS/
776                 || ($TargetPlatform =~ /^hppa/ && $to_move =~ /align/ && $chkcat[$i+1] eq 'literal') )) {
777                 $chk[$i + 1] = $to_move . $chk[$i + 1];
778                 # otherwise they're tossed
779             }
780
781             $c =~ s/$TMOVEDIRVS[@]?FUNNY#END#THING/FUNNY#END#THING/o; # [@]? is a hack (see above)
782         }
783
784         if ( $TargetPlatform =~ /^alpha-/ && $c =~ /^\t\.ent\s+(\S+)/ ) {
785             $ent = $1;
786             # toss all prologue stuff, except for loading gp, and the ..ng address
787             if (($p, $r) = split(/^\t\.prologue/, $c)) {
788                 if (($keep, $junk) = split(/\.\.ng:/, $p)) {
789                     $c = $keep . "..ng:\n";
790                 } else {
791                     print STDERR "malformed code block ($ent)?\n"
792                 }
793             }
794             $c .= "\t.frame \$30,0,\$26,0\n\t.prologue" . $r;
795         }
796   
797         $c =~ s/FUNNY#END#THING//;
798
799 #       print STDERR "\nCHK $i (AFTER) (",$chkcat[$i],"):\n", $c;
800
801         $chk[$i] = $c; # update w/ convenience copy
802     }
803
804     if ( $TargetPlatform =~ /^alpha-/ ) {
805         # print out the header stuff first
806         $chk[0] =~ s/^(\t\.file.*)"(ghc\d+\.c)"/$1"$ifile_root.hc"/;
807         print OUTASM $chk[0];
808
809     } elsif ( $TargetPlatform =~ /^hppa/ ) {
810         print OUTASM $chk[0];
811
812     } elsif ( $TargetPlatform =~ /^mips-/ ) {
813         $chk[0] = "\t\.file\t1 \"$ifile_root.hc\"\n" . $chk[0];
814
815         # get rid of horrible "<dollar>Revision: .*$" strings
816         local(@lines0) = split(/\n/, $chk[0]);
817         local($z) = 0;
818         while ( $z <= $#lines0 ) {
819             if ( $lines0[$z] =~ /^\t\.byte\t0x24,0x52,0x65,0x76,0x69,0x73,0x69,0x6f$/ ) {
820                 undef($lines0[$z]);
821                 $z++;
822                 while ( $z <= $#lines0 ) {
823                     undef($lines0[$z]);
824                     last if $lines0[$z] =~ /[,\t]0x0$/;
825                     $z++;
826                 }
827             }
828             $z++;
829         }
830         $chk[0] = join("\n", @lines0);
831         $chk[0] =~ s/\n\n+/\n/;
832         print OUTASM $chk[0];
833     }
834
835     # print out all the literal strings next
836     for ($i = 0; $i < $numchks; $i++) {
837         if ( $chkcat[$i] eq 'literal' ) {
838             print OUTASM $T_HDR_literal, $chk[$i];
839             print OUTASM "; end literal\n" if $TargetPlatform =~ /^hppa/; # for the splitter
840
841             $chkcat[$i] = 'DONE ALREADY';
842         }
843     }
844
845     # on the HPPA, print out all the bss next
846     if ( $TargetPlatform =~ /^hppa/ ) {
847         for ($i = 1; $i < $numchks; $i++) {
848             if ( $chkcat[$i] eq 'bss' ) {
849                 print OUTASM "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$BSS\$\n\t.align 4\n";
850                 print OUTASM $chk[$i];
851
852                 $chkcat[$i] = 'DONE ALREADY';
853             }
854         }
855     }
856
857     for ($i = $FIRST_MANGLABLE; $i < $numchks; $i++) {
858 #       print STDERR "$i: cat $chkcat[$i], symb $chksymb[$i]\n";
859
860         next if $chkcat[$i] eq 'DONE ALREADY';
861
862         if ( $chkcat[$i] eq 'misc' ) {
863             if ($chk[$i] ne '') {
864                 print OUTASM $T_HDR_misc;
865                 if ($TargetPlatform =~ /^powerpc-|^rs6000/) { 
866                    $chksymb[$i] =~ s/://;
867 #new                   if ($chksymb[$i] =~ /ret.*upd/ || $KNOWN_FUNNY_THING{$chksymb[$i]}
868 #new                    || $chksymb[$i] =~ /^$.{T_US}_(PRIn|PRStart).*${T_POST_LBL}$/o )
869 #new                      { print OUTASM "\t\.globl $chksymb[$i]\n"; }
870 #                   if ($chksymb[$i] ne '' && $chksymb[$i] !~ /ret_[a-z]/ && $chksymb[$i] !~ /djn_[a-z]/) 
871                    if ($chksymb[$i] ne '')
872                        { print OUTASM "\t\.globl \.$chksymb[$i]\n"; };
873                    if ($chk[$i] =~ /TOC\[tc0\], 0\n/)
874                      { ($p, $r) = split(/TOC\[tc0\], 0\n/, $chk[$i]); $printDS = 1;}
875                    else { $r = $chk[$i]; $printDS = 0; };
876                    $chk[$i] = &mangle_powerpc_tailjump($r);
877                 };
878                 &print_doctored($chk[$i], 0);
879                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/ && $printDS) { 
880 #ok                   if ($chksymb[$i] !~ /\_regMain/) {
881                      print OUTASM "\.csect ${chksymb[$i]}[DS]\n";       
882                      print OUTASM "${p}TOC[tc0], 0\n";
883 #ok                   }
884                 }
885             }
886
887         } elsif ( $chkcat[$i] eq 'toss' ) {
888             print STDERR "*** NB: TOSSING code for $chksymb[$i] !!! ***\n";
889
890         } elsif ( $chkcat[$i] eq 'data' ) {
891             if ($chk[$i] ne '') {
892                 print OUTASM $T_HDR_data;
893                 print OUTASM $chk[$i];
894             }
895
896         } elsif ( $chkcat[$i] eq 'consist' ) {
897             if ( $chk[$i] =~ /$T_hsc_cc_PAT/o ) {
898                 local($consist) = "$1.$2.$3";
899                 $consist =~ s/,/./g;
900                 $consist =~ s/\//./g;
901                 $consist =~ s/-/_/g;
902                 $consist =~ s/[^A-Za-z0-9_.]/ZZ/g; # ToDo: properly?
903                 #
904                 # Using a cygnus-2.7-96q4 gcc build on hppas, the 
905                 # consistency chunk for ghc_cc_ID often (but not always!)
906                 # gets lumped with a bunch of .IMPORT directives containing info on
907                 # the code or data space nature of external symbols. We can't
908                 # toss these, so once the consistency ID has been turned into
909                 # a representable symbol, we substitute it for the symbol
910                 # that the string was attached to in the first place (ghc_cc_ID.)
911                 # (The original string is also substituted away.)
912                 #
913                 # This change may affect the code output on other platforms in
914                 # adverse ways, hence we restrict this hack hppa targets only.
915                 #
916                 #    -- 2/98 SOF
917                 if ( $TargetPlatform =~ /^hppa/ )  {
918                         $chk[$i] =~ s/^$TUS[@]?ghc.*c_ID$TPOSTLBL/$consist/o;
919                         $chk[$i] =~ s/\t$T_hsc_cc_PAT/$T_HDR_misc/o;
920                         $consist = $chk[$i]; #clumsily
921                 }
922                 print OUTASM $T_HDR_consist, "${consist}${T_POST_LBL}\n";
923
924             } elsif ( $TargetPlatform !~ /^(mips)-/ ) { # we just don't try in those case (ToDo)
925                 # on mips: consistency string is just a v
926                 # horrible bunch of .bytes,
927                 # which I am too lazy to sort out (WDP 95/05)
928
929                 print STDERR "Couldn't grok consistency: ", $chk[$i];
930             }
931
932         } elsif ( $chkcat[$i] eq 'splitmarker' ) {
933             # we can just re-constitute this one...
934             # NB: we emit _three_ underscores no matter what,
935             # so ghc-split doesn't have to care.
936             print OUTASM "___stg_split_marker",$chksymb[$i],"${T_POST_LBL}\n";
937
938         } elsif ( $chkcat[$i] eq 'closure'
939                || $chkcat[$i] eq 'srt'
940                || $chkcat[$i] eq 'infotbl'
941                || $chkcat[$i] eq 'slow'
942                || $chkcat[$i] eq 'fast' ) { # do them in that order
943             $symb = $chksymb[$i];
944
945             # CLOSURE
946             if ( defined($closurechk{$symb}) ) {
947                 print OUTASM $T_HDR_closure;
948                 print OUTASM $chk[$closurechk{$symb}];
949                 $chkcat[$closurechk{$symb}] = 'DONE ALREADY';
950             }
951
952             # SRT
953             if ( defined($srtchk{$symb}) ) {
954                 print OUTASM $T_HDR_srt;
955                 print OUTASM $chk[$srtchk{$symb}];
956                 $chkcat[$srtchk{$symb}] = 'DONE ALREADY';
957             }
958
959             # INFO TABLE
960             if ( defined($infochk{$symb}) ) {
961
962                 print OUTASM $T_HDR_info;
963                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
964                   if ( !defined($slowchk{$symb}) && defined($fastchk{$symb}) ) {
965                      $fastname = $chk[$fastchk{$symb}];
966                      $fastname =~ s/([_A-Za-z]*_fast\d+):.*(.*\n)*/\1/;
967                      $chk[$infochk{$symb}] =~ s/\.long StdErrorCode/\.long $fastname/;
968                   }
969                   $chk[$infochk{$symb}] =~ s/\.long ([_A-Za-z]\S+_entry)/\.long \.\1/;
970                   $chk[$infochk{$symb}] =~ s/\.long ([A-Za-z]\S+_upd)/\.long \.\1/;
971                   print OUTASM $chk[$infochk{$symb}];
972                 } else {
973                   print OUTASM &rev_tbl($symb, $chk[$infochk{$symb}], 1);
974                 }
975                 # entry code will be put here!
976
977                 $chkcat[$infochk{$symb}] = 'DONE ALREADY';
978             }
979
980             # STD ENTRY POINT
981             if ( defined($slowchk{$symb}) ) {
982
983                 # teach it to drop through to the fast entry point:
984                 $c = $chk[$slowchk{$symb}];
985
986                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
987                   ($p, $r) = split(/TOC\[tc0\], 0\n/, $c); 
988                   if ($symb =~ /^[_A-Z]/)
989                   { 
990                     print OUTASM "\t\.globl \.${chksymb[$i]}_entry\n"; 
991                     print OUTASM "\.csect ${symb}_entry[DS]\n";         
992                     print OUTASM "${p}TOC[tc0], 0\n";
993                   }; 
994                   $r =~ s/\.csect \.text\[PR\]\n//; # todo: properly - andre
995                   $c = &mangle_powerpc_tailjump($r);
996                 };
997
998                 if ( defined($fastchk{$symb}) ) {
999                     if ( $TargetPlatform =~ /^alpha-/ ) {
1000                         $c =~ s/^\tjmp \$31,\(\$27\),0\n\t\.align 4\n\t\.end/\t.align 4\n\t.end/;
1001                     } elsif ( $TargetPlatform =~ /^hppa/ ) {
1002                         $c =~ s/^\s+ldil.*\n\s+ldo.*\n\s+bv.*\n(.*\n)?\s+\.EXIT/$1\t.EXIT/;
1003                     } elsif ( $TargetPlatform =~ /^i386-/ ) {
1004                         # Reg alloc depending, gcc generated code may jump to the fast entry point via
1005                         # a number of registers.
1006                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%edx\n\tjmp \*\%edx\n//;
1007                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%ecx\n\tjmp \*\%ecx\n//;
1008                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%eax\n\tjmp \*\%eax\n//;
1009                         # The next two only apply if we're not stealing %esi or %edi.
1010                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%esi\n\tjmp \*\%esi\n// if ($StolenX86Regs < 3);
1011                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%edi\n\tjmp \*\%edi\n// if ($StolenX86Regs < 4);
1012                     } elsif ( $TargetPlatform =~ /^mips-/ ) {
1013                         $c =~ s/^\tjmp \$31,\(\$27\),0\n\t\.align 4\n\t\.end/\t.align 4\n\t.end/;
1014                     } elsif ( $TargetPlatform =~ /^m68k-/ ) {
1015                         $c =~ s/^\tjmp ${T_US}${symb}_fast\d+.*\n\tnop\n//;
1016                         $c =~ s/^\tjmp ${T_US}${symb}_fast\d+.*\n//;
1017                     } elsif ( $TargetPlatform =~ /^powerpc-|^rs6000-/ ) {
1018                         $c =~ s/^\tb \.${T_US}${symb}_fast\d+\n//;
1019                     } elsif ( $TargetPlatform =~ /^sparc-/ ) {
1020                         $c =~ s/^\tcall ${T_US}${symb}_fast\d+,.*\n\tnop\n//;
1021                         $c =~ s/^\tcall ${T_US}${symb}_fast\d+,.*\n(\t[a-z].*\n)/$1/;
1022                     } else {
1023                         print STDERR "$Pgm: mystery slow-fast dropthrough: $TargetPlatform\n";
1024                     }
1025                 }
1026
1027                 if ( $TargetPlatform !~ /^(alpha-|hppa|mips-)/ ) {
1028                     # On alphas, hppa: no very good way to look for "dangling"
1029                     # references to fast-entry point.
1030                     # (questionable re hppa and mips...)
1031                     print STDERR "still has jump to fast entry point:\n$c"
1032                         if $c =~ /$TUS[@]?$symb[@]?_fast/; # NB: paranoia
1033                 }
1034
1035                 print OUTASM $T_HDR_entry;
1036
1037                 &print_doctored($c, 1); # NB: the 1!!!
1038
1039                 $chkcat[$slowchk{$symb}] = 'DONE ALREADY';
1040             }
1041             
1042             # FAST ENTRY POINT
1043             if ( defined($fastchk{$symb}) ) {
1044                 $c = $chk[$fastchk{$symb}];
1045                 if ( ! defined($slowchk{$symb})
1046                    # ToDo: the || clause can go once we're no longer
1047                    # concerned about producing exactly the same output as before
1048 #OLD:              || $TargetPlatform =~ /^(m68k|sparc|i386)-/
1049                    ) {
1050                     print OUTASM $T_HDR_fast;
1051                 }
1052                   if ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
1053                     local(@lbls) = split(/:/, $c);
1054                     $fullname = $lbls[0];
1055                     $fullname =~ s/$T_MOVE_DIRVS//g;
1056                     if ( $fullname =~ /^[A-Z]/)
1057                        { print OUTASM "\t\.globl \.${fullname}\n";
1058                     } else {
1059 #                       print OUTASM "\t\.lglobl \.${fullname}\n"; #todo: rm - andre
1060                     };
1061                     $c =~ s/((.*\n)*)\t.long \S+, TOC\[tc0\], 0\n\.csect \.text\[PR\]\n((.*\n)*)/\1\3/;
1062                     $c = &mangle_powerpc_tailjump($c);
1063                   };
1064                 &print_doctored($c, 0);
1065                 $chkcat[$fastchk{$symb}] = 'DONE ALREADY';
1066             }
1067
1068         } elsif ( $chkcat[$i] eq 'vector'
1069                || $chkcat[$i] eq 'direct' ) { # do them in that order
1070             $symb = $chksymb[$i];
1071
1072             # VECTOR TABLE
1073             if ( defined($vectorchk{$symb}) ) {
1074                 print OUTASM $T_HDR_vector;
1075                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
1076                   if ( $symb =~ /^[A-Z]/) {
1077                      print OUTASM "\t\.globl \.${symb}_vtbl\n";
1078                      print OUTASM "\t\.globl ${symb}_vtbl\n";
1079                   };
1080                   $chk[$vectorchk{$symb}] =~ s/\.long (\S+)/\.long \.\1/g;
1081                   print OUTASM ".${symb}_vtbl:\n";
1082                   print OUTASM $chk[$vectorchk{$symb}];
1083                 } else {
1084                   print OUTASM &rev_tbl($symb, $chk[$vectorchk{$symb}], 0);
1085                 }
1086                 # direct return code will be put here!
1087                 $chkcat[$vectorchk{$symb}] = 'DONE ALREADY';
1088
1089             } elsif ( $TargetPlatform =~ /^alpha-/ ) {
1090                 # Alphas: the commented nop is for the splitter, to ensure
1091                 # that no module ends with a label as the very last
1092                 # thing.  (The linker will adjust the label to point
1093                 # to the first code word of the next module linked in,
1094                 # even if alignment constraints cause the label to move!)
1095
1096                 print OUTASM "\t# nop\n";
1097             }
1098             
1099         } elsif ( $chkcat[$i] eq 'toc' ) {
1100             # silly optimisation to print tocs, since they come in groups...
1101             print OUTASM $T_HDR_toc;
1102             local($j)   = $i;
1103             while ($chkcat[$j] eq 'toc')
1104               { if (   $chk[$j] !~ /\.tc UpdatePAP\[TC\]/ # not needed: always turned into a jump.
1105                    ) 
1106                 {
1107                   print OUTASM $chk[$j];
1108                 }
1109                 $chkcat[$j] = 'DONE ALREADY';
1110                 $j++;
1111             }
1112             
1113         } else {
1114             &tidy_up_and_die(1,"$Pgm: unknown chkcat (ghc-asm: $TargetPlatform)\n$chkcat[$i]\n$chk[$i]\n");
1115         }
1116     }
1117
1118     print OUTASM $EXTERN_DECLS if $TargetPlatform =~ /^mips-/;
1119
1120     if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
1121          print OUTASM ".csect .text[PR]\n_section_.text:\n.csect .data[RW]\n\t.long _section_.text\n"
1122     };
1123
1124     # finished
1125     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1126     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1127 }
1128 \end{code}
1129
1130 \begin{code}
1131 sub mash_hppa_prologue { # OK, epilogue, too
1132     local($_) = @_;
1133
1134     # toss all prologue stuff
1135     s/^\s+\.ENTRY[^\0]*--- BEGIN ---/\t.ENTRY/;
1136
1137     # Lie about our .CALLINFO
1138     s/^\s+\.CALLINFO.*$/\t.CALLINFO NO_CALLS,NO_UNWIND/;
1139
1140     # Get rid of P'
1141
1142     s/LP'/L'/g;
1143     s/RP'/R'/g;
1144
1145     # toss all epilogue stuff
1146     s/^\s+--- END ---[^\0]*\.EXIT/\t.EXIT/;
1147
1148     # Sorry; we moved the _info stuff to the code segment.
1149     s/_info,DATA/_info,CODE/g;
1150
1151     return($_);
1152 }
1153 \end{code}
1154
1155 \begin{code}
1156 sub print_doctored {
1157     local($_, $need_fallthru_patch) = @_;
1158
1159     if ( $TargetPlatform !~ /^i386-/ 
1160       || ! /^\t[a-z]/  # no instructions in here, apparently
1161       || /^${T_US}_reg[A-Za-z0-9_]+${T_POST_LBL}/) {
1162         print OUTASM $_;
1163         return;
1164     }
1165     # OK, must do some x86 **HACKING**
1166
1167     local($entry_patch) = '';
1168     local($exit_patch)  = '';
1169
1170     # gotta watch out for weird instructions that
1171     # invisibly smash various regs:
1172     #   rep*    %ecx used for counting
1173     #   scas*   %edi used for destination index
1174     #   cmps*   %e[sd]i used for indices
1175     #   loop*   %ecx used for counting
1176     #
1177     # SIGH.
1178
1179     # We cater for:
1180     #  * use of STG reg [ nn(%ebx) ] where no machine reg avail
1181     #
1182     #  * GCC used an "STG reg" for its own purposes
1183     #
1184     #  * some secret uses of machine reg, requiring STG reg
1185     #    to be saved/restored
1186
1187     # The most dangerous "GCC uses" of an "STG reg" are when
1188     # the reg holds the target of a jmp -- it's tricky to
1189     # insert the patch-up code before we get to the target!
1190     # So here we change the jmps:
1191
1192     # --------------------------------------------------------
1193     # it can happen that we have jumps of the form...
1194     #   jmp *<something involving %esp>
1195     # or
1196     #   jmp <something involving another naughty register...>
1197     #
1198     # a reasonably-common case is:
1199     #
1200     #   movl $_blah,<bad-reg>
1201     #   jmp  *<bad-reg>
1202     #
1203
1204 # the short form may tickle perl bug:
1205 #    s/^\tmovl \$${T_US}(.*),(\%e[abcd]x)\n\tjmp \*$2/\tjmp $T_US$1/g;
1206     s/^\tmovl \$${T_US}(.*),\%eax\n\tjmp \*\%eax/\tjmp $T_US$1/g;
1207     s/^\tmovl \$${T_US}(.*),\%ebx\n\tjmp \*\%ebx/\tjmp $T_US$1/g;
1208     s/^\tmovl \$${T_US}(.*),\%ecx\n\tjmp \*\%ecx/\tjmp $T_US$1/g;
1209     s/^\tmovl \$${T_US}(.*),\%edx\n\tjmp \*\%edx/\tjmp $T_US$1/g;
1210
1211     if ($StolenX86Regs <= 2 ) { # YURGH! spurious uses of esi?
1212         s/^\tmovl (.*),\%esi\n\tjmp \*%esi\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1213         s/^\tjmp \*(-?\d*)\((.*\%esi.*)\)\n/\tmovl $2,\%eax\n\tjmp \*$1\(\%eax\)\n/g;
1214         s/^\tjmp \*\%esi\n/\tmovl \%esi,\%eax\n\tjmp \*\%eax\n/g;
1215         die "$Pgm: (mangler) still have jump involving \%esi!\n$_"
1216             if /(jmp|call) .*\%esi/;
1217     }
1218     if ($StolenX86Regs <= 3 ) { # spurious uses of edi?
1219         s/^\tmovl (.*),\%edi\n\tjmp \*%edi\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1220         s/^\tjmp \*(-?\d*\(.*\%edi.*\))\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1221         s/^\tjmp \*\%edi\n/\tmovl \%edi,\%eax\n\tjmp \*\%eax\n/g;
1222         die "$Pgm: (mangler) still have jump involving \%edi!\n$_"
1223             if /(jmp|call) .*\%edi/;
1224     }
1225
1226     # OK, now we can decide what our patch-up code is going to
1227     # be:
1228
1229     # Offsets into register table - you'd better update these magic
1230     # numbers should you change its contents!
1231     # local($OFFSET_R1)=0;  No offset for R1 in new RTS.
1232     local($OFFSET_Hp)=92;
1233
1234         # Note funky ".=" stuff; we're *adding* to these _patch guys
1235     if ( $StolenX86Regs <= 2
1236          && ( /[^0-9]\(\%ebx\)/ || /\%esi/ || /^\tcmps/ ) ) { # R1 (esi)
1237         $entry_patch .= "\tmovl \%esi,(\%ebx)\n";
1238         $exit_patch  .= "\tmovl (\%ebx),\%esi\n";
1239
1240         # nothing for call_{entry,exit} because %esi is callee-save
1241     }
1242     if ( $StolenX86Regs <= 3
1243          && ( /${OFFSET_Hp}\(\%ebx\)/ || /\%edi/ || /^\t(scas|cmps)/ ) ) { # Hp (edi)
1244         $entry_patch .= "\tmovl \%edi,${OFFSET_Hp}(\%ebx)\n";
1245         $exit_patch  .= "\tmovl ${OFFSET_Hp}(\%ebx),\%edi\n";
1246
1247         # nothing for call_{entry,exit} because %edi is callee-save
1248     }
1249
1250     # --------------------------------------------------------
1251     # next, here we go with non-%esp patching!
1252     #
1253     s/^(\t[a-z])/$entry_patch$1/; # before first instruction
1254
1255 # Before calling GC we must set up the exit condition before the call
1256 # and entry condition when we come back
1257
1258     # fix _all_ non-local jumps:
1259
1260     s/^\tjmp \*${T_X86_PRE_LLBL_PAT}/\tJMP___SL/go;
1261     s/^\tjmp ${T_X86_PRE_LLBL_PAT}/\tJMP___L/go;
1262
1263     s/^(\tjmp .*\n)/$exit_patch$1/g; # here's the fix...
1264
1265     s/^\tJMP___SL/\tjmp \*${T_X86_PRE_LLBL}/go;
1266     s/^\tJMP___L/\tjmp ${T_X86_PRE_LLBL}/go;
1267
1268     if ($StolenX86Regs == 2 ) {
1269         die "ARGH! Jump uses \%esi or \%edi with -monly-2-regs:\n$_" 
1270             if /^\t(jmp|call) .*\%e(si|di)/;
1271     } elsif ($StolenX86Regs == 3 ) {
1272         die "ARGH! Jump uses \%edi with -monly-3-regs:\n$_" 
1273             if /^\t(jmp|call) .*\%edi/;
1274     }
1275
1276     # --------------------------------------------------------
1277     # that's it -- print it
1278     #
1279     #die "Funny jumps?\n$_" if /${T_X86_BADJMP}/o; # paranoia
1280
1281     print OUTASM $_;
1282
1283     if ( $need_fallthru_patch ) { # exit patch for end of slow entry code
1284         print OUTASM $exit_patch;
1285         # ToDo: make it not print if there is a "jmp" at the end
1286     }
1287 }
1288 \end{code}
1289
1290 \begin{code}
1291 sub init_FUNNY_THINGS {
1292     %KNOWN_FUNNY_THING = (
1293         # example
1294         # "${T_US}stg_.*{T_POST_LBL}", 1,  
1295     );
1296 }
1297 \end{code}
1298
1299 The following table reversal is used for both info tables and return
1300 vectors.  In both cases, we remove the first entry from the table,
1301 reverse the table, put the label at the end, and paste some code
1302 (that which is normally referred to by the first entry in the table)
1303 right after the table itself.  (The code pasting is done elsewhere.)
1304
1305 \begin{code}
1306 sub rev_tbl {
1307     local($symb, $tbl, $discard1) = @_;
1308
1309     local($before) = '';
1310     local($label) = '';
1311     local(@imports) = (); # hppa only
1312     local(@words) = ();
1313     local($after) = '';
1314     local(@lines) = split(/\n/, $tbl);
1315     local($i, $j); #local ($i, $extra, $words_to_pad, $j);
1316
1317     # see comment in mangleAsm as to why this silliness is needed.
1318     local($TDOTWORD) = ${T_DOT_WORD};
1319     local($TDOTGLOBAL) = ${T_DOT_GLOBAL};
1320     local($TUS) = ${T_US};
1321     local($TPOSTLBL) = ${T_POST_LBL};
1322
1323     # Deal with the header...
1324     for ($i = 0; $i <= $#lines && $lines[$i] !~ /^\t?$TDOTWORD\s+/o; $i++) {
1325         $label .= $lines[$i] . "\n",
1326             next if $lines[$i] =~ /^[A-Za-z0-9_]+_info$TPOSTLBL[@]?$/o
1327                  || $lines[$i] =~ /$TDOTGLOBAL/o
1328                  || $lines[$i] =~ /^$TUS[@]?\S+_vtbl$TPOSTLBL[@]?$/o;
1329
1330         $before .= $lines[$i] . "\n"; # otherwise...
1331     }
1332
1333     # Grab the table data...
1334     if ( $TargetPlatform !~ /^hppa/ ) {
1335         for ( ; $i <= $#lines && $lines[$i] =~ /^\t?$TDOTWORD\s+/o; $i++) {
1336             push(@words, $lines[$i]);
1337         }
1338     } else { # hppa weirdness
1339         for ( ; $i <= $#lines && $lines[$i] =~ /^\s+($TDOTWORD|\.IMPORT)/; $i++) {
1340             if ($lines[$i] =~ /^\s+\.IMPORT/) {
1341                 push(@imports, $lines[$i]);
1342             } else {
1343                 # We don't use HP's ``function pointers''
1344                 # We just use labels in code space, like normal people
1345                 $lines[$i] =~ s/P%//;
1346                 push(@words, $lines[$i]);
1347             }
1348         }
1349     }
1350
1351     # now throw away the first word (SRT) iff it is empty.
1352     # The .zero business is for Linux/ELF.
1353     # The .skip business is for Sparc/Solaris/ELF.
1354     # The .blockz business is for HPPA.
1355     if ($discard1 && $words[0] =~ /^\t?($TDOTWORD\s+0|\.zero\s+4|\.skip\s+4|\.blockz\s+4)/) {
1356         shift(@words)
1357     }
1358
1359 # Padding removed to reduce code size and improve performance on Pentiums.
1360 # Simon M. 13/4/96
1361     # for 486-cache-friendliness, we want our tables aligned
1362     # on 16-byte boundaries (.align 4).  Let's pad:
1363 #    $extra = ($#words + 1) % 4;
1364 #    $words_to_pad = ($extra == 0) ? 0 : 4 - $extra;
1365 #    for ($j = 0; $j < $words_to_pad; $j++) { push(@words, "\t${T_DOT_WORD} 0"); }
1366
1367     for (; $i <= $#lines; $i++) {
1368         $after .= $lines[$i] . "\n";
1369     }
1370
1371     # Alphas:If we have anonymous text (not part of a procedure), the
1372     # linker may complain about missing exception information.  Bleh.
1373     if ( $TargetPlatform =~ /^alpha-/ && $label =~ /^([A-Za-z0-9_]+):$/) {
1374         $before = "\t.ent $1\n" . $before;
1375         $after .= "\t.end $1\n";
1376     }
1377
1378     $tbl = $before
1379          . (($TargetPlatform !~ /^hppa/) ? '' : join("\n", @imports) . "\n")
1380          . join("\n", @words) . "\n"
1381          . $label . $after;
1382
1383 #   print STDERR "before=$before\n";
1384 #   print STDERR "label=$label\n";
1385 #   print STDERR "words=",(reverse @words),"\n";
1386 #   print STDERR "after=$after\n";
1387
1388     $tbl;
1389 }
1390 \end{code}
1391
1392 The HP is a major nuisance.  The threaded code mangler moved info
1393 tables from data space to code space, but unthreaded code in the RTS
1394 still has references to info tables in data space.  Since the HP
1395 linker is very precise about where symbols live, we need to patch the
1396 references in the unthreaded RTS as well.
1397
1398 \begin{code}
1399 sub mini_mangle_asm_hppa {
1400     local($in_asmf, $out_asmf) = @_;
1401
1402     open(INASM, "< $in_asmf")
1403         || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
1404     open(OUTASM,"> $out_asmf")
1405         || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
1406
1407     while (<INASM>) {
1408         s/_info,DATA/_info,CODE/;   # Move _info references to code space
1409         s/P%_PR/_PR/;
1410         print OUTASM;
1411     }
1412
1413     # finished:
1414     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1415     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1416 }
1417
1418 \end{code}
1419  
1420 \begin{code}
1421 sub mini_mangle_asm_powerpc {
1422     local($in_asmf, $out_asmf) = @_;
1423
1424     open(INASM, "< $in_asmf")
1425         || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
1426     open(OUTASM,"> $out_asmf")
1427         || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
1428
1429     while (<INASM>) {
1430         s/long _PRStart/long ._PRStart/;
1431         s/long _PRIn_/long ._PRIn_/;
1432         s/long _Dummy_(\S+)_entry/long ._Dummy_\1_entry/;
1433         s/long _PRMarking_MarkNextRoot\[DS\]/long ._PRMarking_MarkNextRoot/;
1434         s/long _PRMarking_MarkNextCAF\[DS\]/long ._PRMarking_MarkNextCAF/;
1435         s/long _PRMarking_MarkNextAStack\[DS\]/long ._PRMarking_MarkNextAStack/;
1436         s/long _PRMarking_MarkNextBStack\[DS\]/long ._PRMarking_MarkNextBStack/;
1437         s/\.tc EnterNodeCode\[TC]\,EnterNodeCode\[DS\]/\.tc EnterNodeCode\[TC]\,.EnterNodeCode/; # CONC
1438         s/\.tc CheckHeapCode\[TC]\,CheckHeapCode\[DS\]/\.tc CheckHeapCode\[TC]\,.CheckHeapCode/; # CONC
1439         print OUTASM;
1440     }
1441
1442     # finished:
1443     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1444     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1445 }
1446
1447 sub mangle_powerpc_tailjump {
1448     local($c) = @_;
1449     local($maybe_more) = 1;
1450     while (($c =~ /\tlw?z? \d+,LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n/) && $maybe_more) 
1451       { $maybe_more = 0;
1452         $lcsymb = $c;
1453         $lcsymb =~ s/(.*\n)*\tlw?z? \d+,LC\.\.(\d+)\(2\)\n\tmtctr \d+\n\tbctr\n(.*\n)*/\2/;
1454 # the checks for r1 and r2 are mostly paranoia...
1455         $r1 = $c;
1456         $r1 =~ s/(.*\n)*\tlw?z? (\d+),LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n(.*\n)*/\2/;
1457         $r2 = $c;
1458         $r2 =~ s/(.*\n)*\tlw?z? \d+,LC\.\.(\d+)\(2\)\n\tmtctr (\d+)\n\tbctr\n(.*\n)*/\3/;
1459         if (r1 == r2)
1460           { $maybe_more = 1;
1461             $c =~ s/((.*\n)*)\tlw?z? \d+,LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n((.*\n)*)/\1\tb $tocequiv{$lcsymb}\n\3/;
1462           }
1463       };
1464     $c;
1465 }
1466
1467 # make "require"r happy...
1468 1;
1469 \end{code}