4b221fe9ffb4a9928c1549477141201b98ccd132
[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      = "\.text\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|netbsd|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      = "\.text\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|freebsd|netbsd_elf)$/ ) {
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|freebsd|netbsd_elf)$/) ? '#' : '/' ;
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 =~ /freebsd|netbsd_elf/ ) {
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      = "\.text\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      = "\.text\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\.text\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[@]?([A-Za-z0-9_]+)_ct$TPOSTLBL[@]?$/o ) {
482             $chk[++$i]   = $_;
483             $chkcat[$i]  = 'data';
484             $chksymb[$i] = '';
485
486         } elsif ( /^$TUS[@]?ghc.*c_ID$TPOSTLBL/o ) {
487             $chk[++$i]  = $_;
488             $chkcat[$i] = 'consist';
489
490         } elsif ( /^($TUS[@]?__gnu_compiled_c|gcc2_compiled\.)$TPOSTLBL/o ) {
491             ; # toss it
492
493         } elsif ( /^$TUS[A-Za-z0-9_]+\.\d+$TPOSTLBL[@]?$/o
494                || /^$TUS[@]?.*_CAT$TPOSTLBL[@]?$/o              # PROF: _entryname_CAT
495                || /^$TUS[@]?.*_done$TPOSTLBL[@]?$/o             # PROF: _module_done
496                || /^$TUS[@]?_module_registered$TPOSTLBL[@]?$/o  # PROF: _module_registered
497                ) {
498             $chk[++$i]   = $_;
499             $chkcat[$i]  = 'data';
500             $chksymb[$i] = '';
501
502         } elsif ( /^([A-Za-z0-9_]+)\s+\.comm/ && $TargetPlatform =~ /^hppa/ ) {
503             $chk[++$i]   = $_;
504             $chkcat[$i]  = 'bss';
505             $chksymb[$i] = '';
506
507         } elsif ( $TargetPlatform =~ /^powerpc-|^rs6000-/ && /^LC\.\.([0-9]+)/ ) {
508             $chk[++$i]   = $_;
509             $chkcat[$i]  = 'toc';
510             $chksymb[$i] = $1;
511
512         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_cc(s)?$TPOSTLBL[@]?$/o ) {
513             # all CC_ symbols go in the data section...
514             $chk[++$i]   = $_;
515             $chkcat[$i]  = 'data';
516             $chksymb[$i] = '';
517
518         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_(alt|dflt)$TPOSTLBL[@]?$/o ) {
519             $chk[++$i]   = $_;
520             $chkcat[$i]  = 'misc';
521             $chksymb[$i] = '';
522             #$symbtmp = $1;
523             #$chksymb[$i] = $symbtmp if ($TargetPlatform =~ /^powerpc-|^rs6000-/) ; #rm andre
524
525         } elsif ( /^$TUS[@]?([A-Za-z0-9_]+)_vtbl$TPOSTLBL[@]?$/o ) {
526             $chk[++$i]   = $_;
527             $chkcat[$i]  = 'vector';
528             $chksymb[$i] = $1;
529
530             $vectorchk{$1} = $i;
531
532         # As a temporary solution for compiling "foreign export" declarations,
533         # we use global variables to pass arguments from C to STG land.
534         # These declarations live in the .hc file and not in the generated C
535         # stub file, so we let them pass through here.
536         } elsif ( /^[\t ]+\.comm[\t ]+__fexp_.*$/ ) {
537             $chk[++$i]   = $_;
538             $chkcat[$i]  = 'data';
539             $chksymb[$i] = '';
540
541         } elsif ( $TargetPlatform =~ /^i386-.*-solaris2/
542              &&   /^(_uname|uname|stat|fstat):/ ) {
543             # for some utterly bizarre reason, this platform
544             # likes to drop little local C routines with these names
545             # into each and every .o file that #includes the
546             # relevant system .h file.  Yuck.  We just don't
547             # tolerate them in .hc files (which we are processing
548             # here).  If you need to call one of these things from
549             # Haskell, make a call to your own C wrapper, then
550             # put that C wrapper (which calls one of these) in a
551             # plain .c file.  WDP 95/12
552             $chk[++$i]   = $_;
553             $chkcat[$i]  = 'toss';
554             $chksymb[$i] = $1;
555
556         } elsif ( /^$TUS[@]?[A-Za-z0-9_]/o
557                 && ( $TargetPlatform !~ /^hppa/ # need to avoid local labels in this case
558                    || ! /^L\$\d+$/ )
559                 && ( $TargetPlatform !~ /^powerpc|^rs6000/ # ditto
560                    || ! /^(L\.\.\d+|LT\.\..*):$/ ) ) {
561             local($thing);
562             chop($thing = $_);
563             print "Funny global thing?: $_"
564                 unless $KNOWN_FUNNY_THING{$thing}
565                     || /^$TUS[@]?stg_.*$TPOSTLBL[@]?$/o    # RTS internals
566                     || /^$TUS[@]__fexp_.*$TPOSTLBL$/o      # foreign export
567                     || /^$TUS[@]?__init.*$TPOSTLBL$/o      # __init<module>
568                     || /^$TUS[@]?.*_btm$TPOSTLBL$/o        # large bitmaps
569                     || /^$TUS[@]?.*_closure_tbl$TPOSTLBL$/o; # closure tables
570             $chk[++$i]   = $_;
571             $chkcat[$i]  = 'misc';
572             if ($TargetPlatform =~ /^powerpc-|^rs6000-/) 
573                { $chksymb[$i] = $thing; }
574             else { $chksymb[$i] = ''; };
575
576         } else { # simple line (duplicated at the top)
577
578             $chk[$i] .= $_;
579         }
580     }
581     $numchks = $#chk + 1;
582
583     # the division into chunks is imperfect;
584     # we throw some things over the fence into the next
585     # chunk.
586     #
587     # also, there are things we would like to know
588     # about the whole module before we start spitting
589     # output.
590
591     local($FIRST_MANGLABLE) = ($TargetPlatform =~ /^(alpha-|hppa|mips-)/) ? 1 : 0;
592
593 #   print STDERR "first chunk to mangle: $FIRST_MANGLABLE\n";
594
595     # Alphas: NB: we start meddling at chunk 1, not chunk 0
596     # The first ".rdata" is quite magical; as of GCC 2.7.x, it
597     # spits a ".quad 0" in after the v first ".rdata"; we
598     # detect this special case (tossing the ".quad 0")!
599     local($magic_rdata_seen) = 0;
600   
601     # HPPAs, MIPSen: also start medding at chunk 1
602
603     if ($TargetPlatform =~ /^powerpc|^rs6000/) {
604        print OUTASM $T_HDR_toc; # yes, we have to put a .toc 
605                                 # in the beginning of every file!
606     %tocequiv = ();          # maps toc symbol number to toc symbol
607     %revtocequiv = ();       # maps toc symbol to toc symbol number
608     for ($i = 1; $i < $numchks; $i++) {
609         $chk[$i] =~ s/\[RW\]//g;
610         $chk[$i] =~ s/\[DS\]//g;
611         $chk[$i] =~ s/^\.csect .*\[DS\]$//g;
612
613         if ( $chkcat[$i] eq 'toc' && $chk[$i] !~ /\.byte/ )
614 #ToDo: instead of all these changes, just fix mangle_powerpc_tailjump and delete/ignore these tocs?
615            { $chk[$i] =~ s/$T_MOVE_DIRVS//g;
616              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(\S+_fast\d+)/\t\.tc \1\[TC\],\.\2/; 
617              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(\S+_entry)\n/\t\.tc \1\[TC\],\.\2\n/;
618              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(ret_\S+)/\t\.tc \1\[TC\],\.\2/;
619              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(alt_\S+)/\t\.tc \1\[TC\],\.\2/;
620              $chk[$i] =~ s/\t\.tc (\S+)\[TC\],(vtbl_\S+)/\t\.tc \1\[TC\],\.\2/;
621
622              $tocnumber = $chksymb[$i];
623              $tocsymb = $chk[$i];
624              $tocsymb =~ s/^LC\.\.\d+:\n//;
625              $tocsymb =~ s/^\t\.tc \S+,(\S+)\n/\1/;
626              $tocequiv{$tocnumber} = $tocsymb;
627
628            } elsif ( $chkcat[$i] eq 'toc' && $chk[$i] =~ /\.byte/ ) {
629              $chkcat[$i] = 'literal';
630            }
631     }
632     };
633
634     for ($i = $FIRST_MANGLABLE; $i < $numchks; $i++) {
635         $c = $chk[$i]; # convenience copy
636
637 #       print STDERR "\nCHK $i (BEFORE) (",$chkcat[$i],"):\n", $c;
638
639         # toss all prologue stuff; HPPA is pretty weird
640         # (see elsewhere)
641         $c = &mash_hppa_prologue($c) if $TargetPlatform =~ /^hppa/;
642
643         # be slightly paranoid to make sure there's
644         # nothing surprising in there
645         if ( $c =~ /--- BEGIN ---/ ) {
646             if (($p, $r) = split(/--- BEGIN ---/, $c)) {
647
648                 if ($TargetPlatform =~ /^i386-/) {
649                     $p =~ s/^\tpushl \%edi\n//;
650                     $p =~ s/^\tpushl \%esi\n//;
651                     $p =~ s/^\tpushl \%ebx\n//;
652                     $p =~ s/^\tsubl \$\d+,\%esp\n//;
653                     $p =~ s/^\tmovl \$\d+,\%eax\n\tcall __alloca\n// if ($TargetPlatform =~ /^.*-cygwin32/);
654                 } elsif ($TargetPlatform =~ /^m68k-/) {
655                     $p =~ s/^\tlink a6,#-?\d.*\n//;
656                     $p =~ s/^\tpea a6@\n\tmovel sp,a6\n//;    
657                                 # The above showed up in the asm code,
658                                 # so I added it here.
659                                 # I hope it's correct.
660                                 # CaS
661                     $p =~ s/^\tmovel d2,sp\@-\n//;
662                     $p =~ s/^\tmovel d5,sp\@-\n//; # SMmark.* only?
663                     $p =~ s/^\tmoveml \#0x[0-9a-f]+,sp\@-\n//; # SMmark.* only?
664                 } elsif ($TargetPlatform =~ /^mips-/) {
665                     # the .frame/.mask/.fmask that we use is the same
666                     # as that produced by GCC for miniInterpret; this
667                     # gives GDB some chance of figuring out what happened
668                     $FRAME = "\t.frame\t\$sp,2168,\$31\n\t.mask\t0x90000000,-4\n\t.fmask\t0x00000000,0\n";
669                     $p =~ s/^\t\.(frame).*\n/__FRAME__/g;
670                     $p =~ s/^\t\.(mask|fmask).*\n//g;
671                     $p =~ s/^\t\.cprestore.*\n/\t\.cprestore 416\n/; # 16 + 100 4-byte args
672                     $p =~ s/^\tsubu\t\$sp,\$sp,\d+\n//;
673                     $p =~ s/^\tsw\t\$31,\d+\(\$sp\)\n//;
674                     $p =~ s/^\tsw\t\$fp,\d+\(\$sp\)\n//;
675                     $p =~ s/^\tsw\t\$28,\d+\(\$sp\)\n//;
676                     $p =~ s/__FRAME__/$FRAME/;
677                 } elsif ($TargetPlatform =~ /^powerpc-|^rs6000/) {
678                     $p =~ s/^\tmflr 0\n//;
679                     $p =~ s/^\tstm \d+,-\d+\(1\)\n//;
680                     $p =~ s/^\tstw? 0,\d+\(1\)\n//g;
681                     $p =~ s/^\tstw? 1,\d+\(1\)\n//g; #mc
682                     $p =~ s/^\tlw?z 0,0\(1\)\n//g;   #mc
683                     $p =~ s/^\tstw?u 1,-\d+\(1\)\n//; 
684                     $p =~ s/^\tstw? \d+,-\d+\(1\)\n//g; 
685                     $p =~ s/^\tstfd \d+,-\d+\(1\)\n//g; 
686                 } else {
687                     print STDERR "$Pgm: unknown prologue mangling? $TargetPlatform\n";
688                 }
689
690                 # HWL HACK: dont die, just print a warning
691                 #print stderr  "HWL: this should die! Prologue junk?: $p\n" if $p =~ /^\t[^\.]/
692                 #    && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
693                 die "Prologue junk?: $p\n" if $p =~ /^\t[^\.]/
694                     && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
695
696                 # glue together what's left
697                 $c = $p . $r;
698                 $c =~ s/\n\t\n/\n/; # junk blank line
699             }
700         }
701
702         if ( $TargetPlatform =~ /^mips-/ ) {
703             # MIPS: first, this basic sequence may occur "--- END ---" or not
704             $c =~ s/^\tlw\t\$31,\d+\(\$sp\)\n\taddu\t\$sp,\$sp,\d+\n\tj\t\$31\n\t\.end/\t\.end/;
705         }
706
707         # toss all epilogue stuff; again, paranoidly
708         if ( $c =~ /--- END ---/ ) {
709             if (($r, $e) = split(/--- END ---/, $c)) {
710                 if ($TargetPlatform =~ /^i386-/) {
711                     $e =~ s/^\tret\n//;
712                     $e =~ s/^\tpopl \%edi\n//;
713                     $e =~ s/^\tpopl \%esi\n//;
714                     $e =~ s/^\tpopl \%ecx\n//;
715                     $e =~ s/^\taddl \$\d+,\%esp\n//;
716                     $e =~ s/^\tsubl \$-\d+,\%esp\n//;
717                 } elsif ($TargetPlatform =~ /^m68k-/) {
718                     $e =~ s/^\tunlk a6\n//;
719                     $e =~ s/^\trts\n//;
720                 } elsif ($TargetPlatform =~ /^mips-/) {
721                     $e =~ s/^\tlw\t\$31,\d+\(\$sp\)\n//;
722                     $e =~ s/^\tlw\t\$fp,\d+\(\$sp\)\n//;
723                     $e =~ s/^\taddu\t\$sp,\$sp,\d+\n//;
724                     $e =~ s/^\tj\t\$31\n//;
725                 } elsif ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
726                     $e =~ s/^\taddi 1,1,\d+\n//;
727                     $e =~ s/^\tcal 1,\d+\(1\)\n//;
728                     $e =~ s/^\tlw?z? \d+,\d+\(1\)\n//; 
729                     $e =~ s/^\tmtlr 0\n//;
730                     $e =~ s/^\tbl?r\n//;
731                 } else {
732                     print STDERR "$Pgm: unknown epilogue mangling? $TargetPlatform\n";
733                 }
734                 # HWL HACK: dont die, just print a warning
735                 #print stderr "HWL: this should die! Epilogue junk?: $e\n" if $e =~ /^\t[^\.]/
736                 #    && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
737                 die "Epilogue junk?: $e\n" if $e =~ /^\t[^\.]/
738                     && $TargetPlatform !~ /^powerpc-/; #ToDo: remove test
739
740                 # glue together what's left
741                 $c = $r . $e;
742                 $c =~ s/\n\t\n/\n/; # junk blank line
743             }
744         }
745
746         # On SPARCs, we don't do --- BEGIN/END ---, we just
747         # toss the register-windowing save/restore/ret* instructions
748         # directly:
749         if ( $TargetPlatform =~ /^sparc-/ ) {
750             $c =~ s/^\t(save.*|restore|ret|retl)\n//g;
751             # throw away PROLOGUE comments
752             $c =~ s/^\t!#PROLOGUE# 0\n\t!#PROLOGUE# 1\n//;
753         }
754
755         # On Alphas, the prologue mangling is done a little later (below)
756
757         # toss all calls to __DISCARD__
758         $c =~ s/^\t(call|jbsr|jal)\s+$TUS[@]?__DISCARD__\n//go;
759
760         # MIPS: that may leave some gratuitous asm macros around
761         # (no harm done; but we get rid of them to be tidier)
762         $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/
763             if $TargetPlatform =~ /^mips-/;
764
765         # toss stack adjustment after DoSparks
766         $c =~ s/^(\tjbsr _DoSparks\n)\taddqw #8,sp/$1/g
767                 if $TargetPlatform =~ /^m68k-/; # this looks old...
768
769         if ( $TargetPlatform =~ /^alpha-/ &&
770            ! $magic_rdata_seen &&
771            $c =~ /^\s*\.rdata\n\t\.quad 0\n\t\.align \d\n/ ) {
772             $c =~ s/^\s*\.rdata\n\t\.quad 0\n\t\.align (\d)\n/\.rdata\n\t\.align $1\n/;
773             $magic_rdata_seen = 1;
774         }
775
776         # pick some end-things and move them to the next chunk
777
778         # pin a funny end-thing on (for easier matching):
779         $c .= 'FUNNY#END#THING';
780
781         while ( $c =~ /$TMOVEDIRVS[@]?FUNNY#END#THING/o ) {  # [@]? is a silly hack to avoid having to use curlies for T_PRE_APP
782                                                            # (this SEGVs perl4 on alphas, you see)
783
784             $to_move = $1;
785
786             # on x86 we try not to copy any directives into a literal
787             # chunk, rather we keep looking for the next real chunk.  This
788             # is because we get things like
789             #
790             #    .globl blah_closure
791             #    .LC32
792             #           .string "..."
793             #    blah_closure:
794             #           ...
795             #
796             if ( $TargetPlatform =~ /^i386/ && $to_move =~ /$TCOPYDIRVS/ ) {
797                 $j = $i + 1;
798                 while ( $j < ($numchks - 1)  && $chk[$j] =~ /$T_CONST_LBL/) {
799                         $j++;
800                 }
801                 if ( $j < ($numchks - 1)) {
802                         $chk[$j] = $to_move . $chk[$j];
803                 }
804             }
805
806             elsif ( $i < ($numchks - 1)
807               && ( $to_move =~ /$TCOPYDIRVS/
808                 || ($TargetPlatform =~ /^hppa/ && $to_move =~ /align/ && $chkcat[$i+1] eq 'literal') )) {
809                 $chk[$i + 1] = $to_move . $chk[$i + 1];
810                 # otherwise they're tossed
811             }
812
813             $c =~ s/$TMOVEDIRVS[@]?FUNNY#END#THING/FUNNY#END#THING/o; # [@]? is a hack (see above)
814         }
815
816         if ( $TargetPlatform =~ /^alpha-/ && $c =~ /^\t\.ent\s+(\S+)/ ) {
817             $ent = $1;
818             # toss all prologue stuff, except for loading gp, and the ..ng address
819             if (($p, $r) = split(/^\t\.prologue/, $c)) {
820                 if (($keep, $junk) = split(/\.\.ng:/, $p)) {
821                     $c = $keep . "..ng:\n";
822                 } else {
823                     print STDERR "malformed code block ($ent)?\n"
824                 }
825             }
826             $c .= "\t.frame \$30,0,\$26,0\n\t.prologue" . $r;
827         }
828   
829         $c =~ s/FUNNY#END#THING//;
830
831 #       print STDERR "\nCHK $i (AFTER) (",$chkcat[$i],"):\n", $c;
832
833         $chk[$i] = $c; # update w/ convenience copy
834     }
835
836     if ( $TargetPlatform =~ /^alpha-/ ) {
837         # print out the header stuff first
838         $chk[0] =~ s/^(\t\.file.*)"(ghc\d+\.c)"/$1"$ifile_root.hc"/;
839         print OUTASM $chk[0];
840
841     } elsif ( $TargetPlatform =~ /^hppa/ ) {
842         print OUTASM $chk[0];
843
844     } elsif ( $TargetPlatform =~ /^mips-/ ) {
845         $chk[0] = "\t\.file\t1 \"$ifile_root.hc\"\n" . $chk[0];
846
847         # get rid of horrible "<dollar>Revision: .*$" strings
848         local(@lines0) = split(/\n/, $chk[0]);
849         local($z) = 0;
850         while ( $z <= $#lines0 ) {
851             if ( $lines0[$z] =~ /^\t\.byte\t0x24,0x52,0x65,0x76,0x69,0x73,0x69,0x6f$/ ) {
852                 undef($lines0[$z]);
853                 $z++;
854                 while ( $z <= $#lines0 ) {
855                     undef($lines0[$z]);
856                     last if $lines0[$z] =~ /[,\t]0x0$/;
857                     $z++;
858                 }
859             }
860             $z++;
861         }
862         $chk[0] = join("\n", @lines0);
863         $chk[0] =~ s/\n\n+/\n/;
864         print OUTASM $chk[0];
865     }
866
867     # print out all the literal strings next
868     for ($i = 0; $i < $numchks; $i++) {
869         if ( $chkcat[$i] eq 'literal' ) {
870             print OUTASM $T_HDR_literal, $chk[$i];
871             print OUTASM "; end literal\n" if $TargetPlatform =~ /^hppa/; # for the splitter
872
873             $chkcat[$i] = 'DONE ALREADY';
874         }
875     }
876
877     # on the HPPA, print out all the bss next
878     if ( $TargetPlatform =~ /^hppa/ ) {
879         for ($i = 1; $i < $numchks; $i++) {
880             if ( $chkcat[$i] eq 'bss' ) {
881                 print OUTASM "\t.SPACE \$PRIVATE\$\n\t.SUBSPA \$BSS\$\n\t.align 4\n";
882                 print OUTASM $chk[$i];
883
884                 $chkcat[$i] = 'DONE ALREADY';
885             }
886         }
887     }
888
889     for ($i = $FIRST_MANGLABLE; $i < $numchks; $i++) {
890 #       print STDERR "$i: cat $chkcat[$i], symb $chksymb[$i]\n";
891
892         next if $chkcat[$i] eq 'DONE ALREADY';
893
894         if ( $chkcat[$i] eq 'misc' ) {
895             if ($chk[$i] ne '') {
896                 print OUTASM $T_HDR_misc;
897                 if ($TargetPlatform =~ /^powerpc-|^rs6000/) { 
898                    $chksymb[$i] =~ s/://;
899 #new                   if ($chksymb[$i] =~ /ret.*upd/ || $KNOWN_FUNNY_THING{$chksymb[$i]}
900 #new                    || $chksymb[$i] =~ /^$.{T_US}_(PRIn|PRStart).*${T_POST_LBL}$/o )
901 #new                      { print OUTASM "\t\.globl $chksymb[$i]\n"; }
902 #                   if ($chksymb[$i] ne '' && $chksymb[$i] !~ /ret_[a-z]/ && $chksymb[$i] !~ /djn_[a-z]/) 
903                    if ($chksymb[$i] ne '')
904                        { print OUTASM "\t\.globl \.$chksymb[$i]\n"; };
905                    if ($chk[$i] =~ /TOC\[tc0\], 0\n/)
906                      { ($p, $r) = split(/TOC\[tc0\], 0\n/, $chk[$i]); $printDS = 1;}
907                    else { $r = $chk[$i]; $printDS = 0; };
908                    $chk[$i] = &mangle_powerpc_tailjump($r);
909                 };
910                 &print_doctored($chk[$i], 0);
911                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/ && $printDS) { 
912 #ok                   if ($chksymb[$i] !~ /\__init_Main/) {
913                      print OUTASM "\.csect ${chksymb[$i]}[DS]\n";       
914                      print OUTASM "${p}TOC[tc0], 0\n";
915 #ok                   }
916                 }
917             }
918
919         } elsif ( $chkcat[$i] eq 'toss' ) {
920             print STDERR "*** NB: TOSSING code for $chksymb[$i] !!! ***\n";
921
922         } elsif ( $chkcat[$i] eq 'data' ) {
923             if ($chk[$i] ne '') {
924                 print OUTASM $T_HDR_data;
925                 print OUTASM $chk[$i];
926             }
927
928         } elsif ( $chkcat[$i] eq 'consist' ) {
929             if ( $chk[$i] =~ /$T_hsc_cc_PAT/o ) {
930                 local($consist) = "$1.$2.$3";
931                 $consist =~ s/,/./g;
932                 $consist =~ s/\//./g;
933                 $consist =~ s/-/_/g;
934                 $consist =~ s/[^A-Za-z0-9_.]/ZZ/g; # ToDo: properly?
935                 #
936                 # Using a cygnus-2.7-96q4 gcc build on hppas, the 
937                 # consistency chunk for ghc_cc_ID often (but not always!)
938                 # gets lumped with a bunch of .IMPORT directives containing info on
939                 # the code or data space nature of external symbols. We can't
940                 # toss these, so once the consistency ID has been turned into
941                 # a representable symbol, we substitute it for the symbol
942                 # that the string was attached to in the first place (ghc_cc_ID.)
943                 # (The original string is also substituted away.)
944                 #
945                 # This change may affect the code output on other platforms in
946                 # adverse ways, hence we restrict this hack hppa targets only.
947                 #
948                 #    -- 2/98 SOF
949                 if ( $TargetPlatform =~ /^hppa/ )  {
950                         $chk[$i] =~ s/^$TUS[@]?ghc.*c_ID$TPOSTLBL/$consist/o;
951                         $chk[$i] =~ s/\t$T_hsc_cc_PAT/$T_HDR_misc/o;
952                         $consist = $chk[$i]; #clumsily
953                 }
954                 print OUTASM $T_HDR_consist, "${consist}${T_POST_LBL}\n";
955
956             } elsif ( $TargetPlatform !~ /^(mips)-/ ) { # we just don't try in those case (ToDo)
957                 # on mips: consistency string is just a v
958                 # horrible bunch of .bytes,
959                 # which I am too lazy to sort out (WDP 95/05)
960
961                 print STDERR "Couldn't grok consistency: ", $chk[$i];
962             }
963
964         } elsif ( $chkcat[$i] eq 'splitmarker' ) {
965             # we can just re-constitute this one...
966             # NB: we emit _three_ underscores no matter what,
967             # so ghc-split doesn't have to care.
968             print OUTASM "___stg_split_marker",$chksymb[$i],"${T_POST_LBL}\n";
969
970         } elsif ( $chkcat[$i] eq 'closure'
971                || $chkcat[$i] eq 'srt'
972                || $chkcat[$i] eq 'infotbl'
973                || $chkcat[$i] eq 'slow'
974                || $chkcat[$i] eq 'fast' ) { # do them in that order
975             $symb = $chksymb[$i];
976
977             # CLOSURE
978             if ( defined($closurechk{$symb}) ) {
979                 print OUTASM $T_HDR_closure;
980                 print OUTASM $chk[$closurechk{$symb}];
981                 $chkcat[$closurechk{$symb}] = 'DONE ALREADY';
982             }
983
984             # SRT
985             if ( defined($srtchk{$symb}) ) {
986                 print OUTASM $T_HDR_srt;
987                 print OUTASM $chk[$srtchk{$symb}];
988                 $chkcat[$srtchk{$symb}] = 'DONE ALREADY';
989             }
990
991             # INFO TABLE
992             if ( defined($infochk{$symb}) ) {
993
994                 print OUTASM $T_HDR_info;
995                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
996                   if ( !defined($slowchk{$symb}) && defined($fastchk{$symb}) ) {
997                      $fastname = $chk[$fastchk{$symb}];
998                      $fastname =~ s/([_A-Za-z]*_fast\d+):.*(.*\n)*/\1/;
999                      $chk[$infochk{$symb}] =~ s/\.long StdErrorCode/\.long $fastname/;
1000                   }
1001                   $chk[$infochk{$symb}] =~ s/\.long ([_A-Za-z]\S+_entry)/\.long \.\1/;
1002                   $chk[$infochk{$symb}] =~ s/\.long ([A-Za-z]\S+_upd)/\.long \.\1/;
1003                   print OUTASM $chk[$infochk{$symb}];
1004                 } else {
1005                   print OUTASM &rev_tbl($symb, $chk[$infochk{$symb}], 1);
1006                 }
1007                 # entry code will be put here!
1008
1009                 $chkcat[$infochk{$symb}] = 'DONE ALREADY';
1010             }
1011
1012             # STD ENTRY POINT
1013             if ( defined($slowchk{$symb}) ) {
1014
1015                 # teach it to drop through to the fast entry point:
1016                 $c = $chk[$slowchk{$symb}];
1017
1018                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
1019                   ($p, $r) = split(/TOC\[tc0\], 0\n/, $c); 
1020                   if ($symb =~ /^[_A-Z]/)
1021                   { 
1022                     print OUTASM "\t\.globl \.${chksymb[$i]}_entry\n"; 
1023                     print OUTASM "\.csect ${symb}_entry[DS]\n";         
1024                     print OUTASM "${p}TOC[tc0], 0\n";
1025                   }; 
1026                   $r =~ s/\.csect \.text\[PR\]\n//; # todo: properly - andre
1027                   $c = &mangle_powerpc_tailjump($r);
1028                 };
1029
1030                 if ( defined($fastchk{$symb}) ) {
1031                     if ( $TargetPlatform =~ /^alpha-/ ) {
1032                         $c =~ s/^\tjmp \$31,\(\$27\),0\n\t\.align 4\n\t\.end/\t.align 4\n\t.end/;
1033                     } elsif ( $TargetPlatform =~ /^hppa/ ) {
1034                         $c =~ s/^\s+ldil.*\n\s+ldo.*\n\s+bv.*\n(.*\n)?\s+\.EXIT/$1\t.EXIT/;
1035                     } elsif ( $TargetPlatform =~ /^i386-/ ) {
1036                         # Reg alloc depending, gcc generated code may jump to the fast entry point via
1037                         # a number of registers.
1038                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%edx\n\tjmp \*\%edx\n//;
1039                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%ecx\n\tjmp \*\%ecx\n//;
1040                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%eax\n\tjmp \*\%eax\n//;
1041                         # The next two only apply if we're not stealing %esi or %edi.
1042                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%esi\n\tjmp \*\%esi\n// if ($StolenX86Regs < 3);
1043                         $c =~ s/^\tmovl \$${T_US}${symb}_fast\d*,\%edi\n\tjmp \*\%edi\n// if ($StolenX86Regs < 4);
1044                     } elsif ( $TargetPlatform =~ /^mips-/ ) {
1045                         $c =~ s/^\tjmp \$31,\(\$27\),0\n\t\.align 4\n\t\.end/\t.align 4\n\t.end/;
1046                     } elsif ( $TargetPlatform =~ /^m68k-/ ) {
1047                         $c =~ s/^\tjmp ${T_US}${symb}_fast\d+.*\n\tnop\n//;
1048                         $c =~ s/^\tjmp ${T_US}${symb}_fast\d+.*\n//;
1049                     } elsif ( $TargetPlatform =~ /^powerpc-|^rs6000-/ ) {
1050                         $c =~ s/^\tb \.${T_US}${symb}_fast\d+\n//;
1051                     } elsif ( $TargetPlatform =~ /^sparc-/ ) {
1052                         $c =~ s/^\tcall\s+${T_US}${symb}_fast\d+,.*\n\t\s*nop\n//;
1053                         $c =~ s/^\tcall\s+${T_US}${symb}_fast\d+,.*\n(\t\s*[a-z].*\n)/$1/;
1054                     } else {
1055                         print STDERR "$Pgm: mystery slow-fast dropthrough: $TargetPlatform\n";
1056                     }
1057                 }
1058
1059                 if ( $TargetPlatform !~ /^(alpha-|hppa|mips-)/ ) {
1060                     # On alphas, hppa: no very good way to look for "dangling"
1061                     # references to fast-entry point.
1062                     # (questionable re hppa and mips...)
1063                     print STDERR "still has jump to fast entry point:\n$c"
1064                         if $c =~ /$TUS[@]?$symb[@]?_fast/; # NB: paranoia
1065                 }
1066
1067                 print OUTASM $T_HDR_entry;
1068
1069                 &print_doctored($c, 1); # NB: the 1!!!
1070
1071                 $chkcat[$slowchk{$symb}] = 'DONE ALREADY';
1072             }
1073             
1074             # FAST ENTRY POINT
1075             if ( defined($fastchk{$symb}) ) {
1076                 $c = $chk[$fastchk{$symb}];
1077                 if ( ! defined($slowchk{$symb})
1078                    # ToDo: the || clause can go once we're no longer
1079                    # concerned about producing exactly the same output as before
1080 #OLD:              || $TargetPlatform =~ /^(m68k|sparc|i386)-/
1081                    ) {
1082                     print OUTASM $T_HDR_fast;
1083                 }
1084                   if ($TargetPlatform =~ /^powerpc-|^rs6000-/) {
1085                     local(@lbls) = split(/:/, $c);
1086                     $fullname = $lbls[0];
1087                     $fullname =~ s/$T_MOVE_DIRVS//g;
1088                     if ( $fullname =~ /^[A-Z]/)
1089                        { print OUTASM "\t\.globl \.${fullname}\n";
1090                     } else {
1091 #                       print OUTASM "\t\.lglobl \.${fullname}\n"; #todo: rm - andre
1092                     };
1093                     $c =~ s/((.*\n)*)\t.long \S+, TOC\[tc0\], 0\n\.csect \.text\[PR\]\n((.*\n)*)/\1\3/;
1094                     $c = &mangle_powerpc_tailjump($c);
1095                   };
1096                 &print_doctored($c, 0);
1097                 $chkcat[$fastchk{$symb}] = 'DONE ALREADY';
1098             }
1099
1100         } elsif ( $chkcat[$i] eq 'vector'
1101                || $chkcat[$i] eq 'direct' ) { # do them in that order
1102             $symb = $chksymb[$i];
1103
1104             # VECTOR TABLE
1105             if ( defined($vectorchk{$symb}) ) {
1106                 print OUTASM $T_HDR_vector;
1107                 if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
1108                   if ( $symb =~ /^[A-Z]/) {
1109                      print OUTASM "\t\.globl \.${symb}_vtbl\n";
1110                      print OUTASM "\t\.globl ${symb}_vtbl\n";
1111                   };
1112                   $chk[$vectorchk{$symb}] =~ s/\.long (\S+)/\.long \.\1/g;
1113                   print OUTASM ".${symb}_vtbl:\n";
1114                   print OUTASM $chk[$vectorchk{$symb}];
1115                 } else {
1116                   print OUTASM &rev_tbl($symb, $chk[$vectorchk{$symb}], 0);
1117                 }
1118                 # direct return code will be put here!
1119                 $chkcat[$vectorchk{$symb}] = 'DONE ALREADY';
1120
1121             } elsif ( $TargetPlatform =~ /^alpha-/ ) {
1122                 # Alphas: the commented nop is for the splitter, to ensure
1123                 # that no module ends with a label as the very last
1124                 # thing.  (The linker will adjust the label to point
1125                 # to the first code word of the next module linked in,
1126                 # even if alignment constraints cause the label to move!)
1127
1128                 print OUTASM "\t# nop\n";
1129             }
1130             
1131         } elsif ( $chkcat[$i] eq 'toc' ) {
1132             # silly optimisation to print tocs, since they come in groups...
1133             print OUTASM $T_HDR_toc;
1134             local($j)   = $i;
1135             while ($chkcat[$j] eq 'toc')
1136               { if (   $chk[$j] !~ /\.tc UpdatePAP\[TC\]/ # not needed: always turned into a jump.
1137                    ) 
1138                 {
1139                   print OUTASM $chk[$j];
1140                 }
1141                 $chkcat[$j] = 'DONE ALREADY';
1142                 $j++;
1143             }
1144             
1145         } else {
1146             &tidy_up_and_die(1,"$Pgm: unknown chkcat (ghc-asm: $TargetPlatform)\n$chkcat[$i]\n$chk[$i]\n");
1147         }
1148     }
1149
1150     print OUTASM $EXTERN_DECLS if $TargetPlatform =~ /^mips-/;
1151
1152     if ($TargetPlatform =~ /^powerpc-|^rs6000-/) { 
1153          print OUTASM ".csect .text[PR]\n_section_.text:\n.csect .data[RW]\n\t.long _section_.text\n"
1154     };
1155
1156     # finished
1157     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1158     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1159 }
1160 \end{code}
1161
1162 \begin{code}
1163 sub mash_hppa_prologue { # OK, epilogue, too
1164     local($_) = @_;
1165
1166     # toss all prologue stuff
1167     s/^\s+\.ENTRY[^\0]*--- BEGIN ---/\t.ENTRY/;
1168
1169     # Lie about our .CALLINFO
1170     s/^\s+\.CALLINFO.*$/\t.CALLINFO NO_CALLS,NO_UNWIND/;
1171
1172     # Get rid of P'
1173
1174     s/LP'/L'/g;
1175     s/RP'/R'/g;
1176
1177     # toss all epilogue stuff
1178     s/^\s+--- END ---[^\0]*\.EXIT/\t.EXIT/;
1179
1180     # Sorry; we moved the _info stuff to the code segment.
1181     s/_info,DATA/_info,CODE/g;
1182
1183     return($_);
1184 }
1185 \end{code}
1186
1187 \begin{code}
1188 sub print_doctored {
1189     local($_, $need_fallthru_patch) = @_;
1190
1191     if ( $TargetPlatform !~ /^i386-/ 
1192       || ! /^\t[a-z]/  # no instructions in here, apparently
1193       || /^${T_US}__init_[A-Za-z0-9_]+${T_POST_LBL}/) {
1194         print OUTASM $_;
1195         return;
1196     }
1197     # OK, must do some x86 **HACKING**
1198
1199     local($entry_patch) = '';
1200     local($exit_patch)  = '';
1201
1202     # gotta watch out for weird instructions that
1203     # invisibly smash various regs:
1204     #   rep*    %ecx used for counting
1205     #   scas*   %edi used for destination index
1206     #   cmps*   %e[sd]i used for indices
1207     #   loop*   %ecx used for counting
1208     #
1209     # SIGH.
1210
1211     # We cater for:
1212     #  * use of STG reg [ nn(%ebx) ] where no machine reg avail
1213     #
1214     #  * GCC used an "STG reg" for its own purposes
1215     #
1216     #  * some secret uses of machine reg, requiring STG reg
1217     #    to be saved/restored
1218
1219     # The most dangerous "GCC uses" of an "STG reg" are when
1220     # the reg holds the target of a jmp -- it's tricky to
1221     # insert the patch-up code before we get to the target!
1222     # So here we change the jmps:
1223
1224     # --------------------------------------------------------
1225     # it can happen that we have jumps of the form...
1226     #   jmp *<something involving %esp>
1227     # or
1228     #   jmp <something involving another naughty register...>
1229     #
1230     # a reasonably-common case is:
1231     #
1232     #   movl $_blah,<bad-reg>
1233     #   jmp  *<bad-reg>
1234     #
1235
1236 # the short form may tickle perl bug:
1237 #    s/^\tmovl \$${T_US}(.*),(\%e[abcd]x)\n\tjmp \*$2/\tjmp $T_US$1/g;
1238     s/^\tmovl \$${T_US}(.*),\%eax\n\tjmp \*\%eax/\tjmp $T_US$1/g;
1239     s/^\tmovl \$${T_US}(.*),\%ebx\n\tjmp \*\%ebx/\tjmp $T_US$1/g;
1240     s/^\tmovl \$${T_US}(.*),\%ecx\n\tjmp \*\%ecx/\tjmp $T_US$1/g;
1241     s/^\tmovl \$${T_US}(.*),\%edx\n\tjmp \*\%edx/\tjmp $T_US$1/g;
1242
1243     if ($StolenX86Regs <= 2 ) { # YURGH! spurious uses of esi?
1244         s/^\tmovl (.*),\%esi\n\tjmp \*%esi\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1245         s/^\tjmp \*(-?\d*)\((.*\%esi.*)\)\n/\tmovl $2,\%eax\n\tjmp \*$1\(\%eax\)\n/g;
1246         s/^\tjmp \*\%esi\n/\tmovl \%esi,\%eax\n\tjmp \*\%eax\n/g;
1247         die "$Pgm: (mangler) still have jump involving \%esi!\n$_"
1248             if /(jmp|call) .*\%esi/;
1249     }
1250     if ($StolenX86Regs <= 3 ) { # spurious uses of edi?
1251         s/^\tmovl (.*),\%edi\n\tjmp \*%edi\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1252         s/^\tjmp \*(-?\d*\(.*\%edi.*\))\n/\tmovl $1,\%eax\n\tjmp \*\%eax\n/g;
1253         s/^\tjmp \*\%edi\n/\tmovl \%edi,\%eax\n\tjmp \*\%eax\n/g;
1254         die "$Pgm: (mangler) still have jump involving \%edi!\n$_"
1255             if /(jmp|call) .*\%edi/;
1256     }
1257
1258     # OK, now we can decide what our patch-up code is going to
1259     # be:
1260
1261     # Offsets into register table - you'd better update these magic
1262     # numbers should you change its contents!
1263     # local($OFFSET_R1)=0;  No offset for R1 in new RTS.
1264     local($OFFSET_Hp)=92;
1265
1266         # Note funky ".=" stuff; we're *adding* to these _patch guys
1267     if ( $StolenX86Regs <= 2
1268          && ( /[^0-9]\(\%ebx\)/ || /\%esi/ || /^\tcmps/ ) ) { # R1 (esi)
1269         $entry_patch .= "\tmovl \%esi,(\%ebx)\n";
1270         $exit_patch  .= "\tmovl (\%ebx),\%esi\n";
1271
1272         # nothing for call_{entry,exit} because %esi is callee-save
1273     }
1274     if ( $StolenX86Regs <= 3
1275          && ( /${OFFSET_Hp}\(\%ebx\)/ || /\%edi/ || /^\t(scas|cmps)/ ) ) { # Hp (edi)
1276         $entry_patch .= "\tmovl \%edi,${OFFSET_Hp}(\%ebx)\n";
1277         $exit_patch  .= "\tmovl ${OFFSET_Hp}(\%ebx),\%edi\n";
1278
1279         # nothing for call_{entry,exit} because %edi is callee-save
1280     }
1281
1282     # --------------------------------------------------------
1283     # next, here we go with non-%esp patching!
1284     #
1285     s/^(\t[a-z])/$entry_patch$1/; # before first instruction
1286
1287 # Before calling GC we must set up the exit condition before the call
1288 # and entry condition when we come back
1289
1290     # fix _all_ non-local jumps:
1291
1292     s/^\tjmp \*${T_X86_PRE_LLBL_PAT}/\tJMP___SL/go;
1293     s/^\tjmp ${T_X86_PRE_LLBL_PAT}/\tJMP___L/go;
1294
1295     s/^(\tjmp .*\n)/$exit_patch$1/g; # here's the fix...
1296
1297     s/^\tJMP___SL/\tjmp \*${T_X86_PRE_LLBL}/go;
1298     s/^\tJMP___L/\tjmp ${T_X86_PRE_LLBL}/go;
1299
1300     if ($StolenX86Regs == 2 ) {
1301         die "ARGH! Jump uses \%esi or \%edi with -monly-2-regs:\n$_" 
1302             if /^\t(jmp|call) .*\%e(si|di)/;
1303     } elsif ($StolenX86Regs == 3 ) {
1304         die "ARGH! Jump uses \%edi with -monly-3-regs:\n$_" 
1305             if /^\t(jmp|call) .*\%edi/;
1306     }
1307
1308     # --------------------------------------------------------
1309     # that's it -- print it
1310     #
1311     #die "Funny jumps?\n$_" if /${T_X86_BADJMP}/o; # paranoia
1312
1313     print OUTASM $_;
1314
1315     if ( $need_fallthru_patch ) { # exit patch for end of slow entry code
1316         print OUTASM $exit_patch;
1317         # ToDo: make it not print if there is a "jmp" at the end
1318     }
1319 }
1320 \end{code}
1321
1322 \begin{code}
1323 sub init_FUNNY_THINGS {
1324     %KNOWN_FUNNY_THING = (
1325         # example
1326         # "${T_US}stg_.*{T_POST_LBL}", 1,  
1327     );
1328 }
1329 \end{code}
1330
1331 The following table reversal is used for both info tables and return
1332 vectors.  In both cases, we remove the first entry from the table,
1333 reverse the table, put the label at the end, and paste some code
1334 (that which is normally referred to by the first entry in the table)
1335 right after the table itself.  (The code pasting is done elsewhere.)
1336
1337 \begin{code}
1338 sub rev_tbl {
1339     local($symb, $tbl, $discard1) = @_;
1340
1341     local($before) = '';
1342     local($label) = '';
1343     local(@imports) = (); # hppa only
1344     local(@words) = ();
1345     local($after) = '';
1346     local(@lines) = split(/\n/, $tbl);
1347     local($i, $j); #local ($i, $extra, $words_to_pad, $j);
1348
1349     # see comment in mangleAsm as to why this silliness is needed.
1350     local($TDOTWORD) = ${T_DOT_WORD};
1351     local($TDOTGLOBAL) = ${T_DOT_GLOBAL};
1352     local($TUS) = ${T_US};
1353     local($TPOSTLBL) = ${T_POST_LBL};
1354
1355     # Deal with the header...
1356     for ($i = 0; $i <= $#lines && $lines[$i] !~ /^\t?$TDOTWORD\s+/o; $i++) {
1357         $label .= $lines[$i] . "\n",
1358             next if $lines[$i] =~ /^[A-Za-z0-9_]+_info$TPOSTLBL[@]?$/o
1359                  || $lines[$i] =~ /$TDOTGLOBAL/o
1360                  || $lines[$i] =~ /^$TUS[@]?\S+_vtbl$TPOSTLBL[@]?$/o;
1361
1362         $before .= $lines[$i] . "\n"; # otherwise...
1363     }
1364
1365     # Grab the table data...
1366     if ( $TargetPlatform !~ /^hppa/ ) {
1367         for ( ; $i <= $#lines && $lines[$i] =~ /^\t?$TDOTWORD\s+/o; $i++) {
1368             push(@words, $lines[$i]);
1369         }
1370     } else { # hppa weirdness
1371         for ( ; $i <= $#lines && $lines[$i] =~ /^\s+($TDOTWORD|\.IMPORT)/; $i++) {
1372             if ($lines[$i] =~ /^\s+\.IMPORT/) {
1373                 push(@imports, $lines[$i]);
1374             } else {
1375                 # We don't use HP's ``function pointers''
1376                 # We just use labels in code space, like normal people
1377                 $lines[$i] =~ s/P%//;
1378                 push(@words, $lines[$i]);
1379             }
1380         }
1381     }
1382
1383     # now throw away the first word (SRT) iff it is empty.
1384     # The .zero business is for Linux/ELF.
1385     # The .skip business is for Sparc/Solaris/ELF.
1386     # The .blockz business is for HPPA.
1387     if ($discard1 && $words[0] =~ /^\t?($TDOTWORD\s+0|\.zero\s+4|\.skip\s+4|\.blockz\s+4)/) {
1388         shift(@words)
1389     }
1390
1391 # Padding removed to reduce code size and improve performance on Pentiums.
1392 # Simon M. 13/4/96
1393     # for 486-cache-friendliness, we want our tables aligned
1394     # on 16-byte boundaries (.align 4).  Let's pad:
1395 #    $extra = ($#words + 1) % 4;
1396 #    $words_to_pad = ($extra == 0) ? 0 : 4 - $extra;
1397 #    for ($j = 0; $j < $words_to_pad; $j++) { push(@words, "\t${T_DOT_WORD} 0"); }
1398
1399     for (; $i <= $#lines; $i++) {
1400         $after .= $lines[$i] . "\n";
1401     }
1402
1403     # Alphas:If we have anonymous text (not part of a procedure), the
1404     # linker may complain about missing exception information.  Bleh.
1405     if ( $TargetPlatform =~ /^alpha-/ && $label =~ /^([A-Za-z0-9_]+):$/) {
1406         $before = "\t.ent $1\n" . $before;
1407         $after .= "\t.end $1\n";
1408     }
1409
1410     $tbl = $before
1411          . (($TargetPlatform !~ /^hppa/) ? '' : join("\n", @imports) . "\n")
1412          . join("\n", @words) . "\n"
1413          . $label . $after;
1414
1415 #   print STDERR "before=$before\n";
1416 #   print STDERR "label=$label\n";
1417 #   print STDERR "words=",(reverse @words),"\n";
1418 #   print STDERR "after=$after\n";
1419
1420     $tbl;
1421 }
1422 \end{code}
1423
1424 The HP is a major nuisance.  The threaded code mangler moved info
1425 tables from data space to code space, but unthreaded code in the RTS
1426 still has references to info tables in data space.  Since the HP
1427 linker is very precise about where symbols live, we need to patch the
1428 references in the unthreaded RTS as well.
1429
1430 \begin{code}
1431 sub mini_mangle_asm_hppa {
1432     local($in_asmf, $out_asmf) = @_;
1433
1434     open(INASM, "< $in_asmf")
1435         || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
1436     open(OUTASM,"> $out_asmf")
1437         || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
1438
1439     while (<INASM>) {
1440         s/_info,DATA/_info,CODE/;   # Move _info references to code space
1441         s/P%_PR/_PR/;
1442         print OUTASM;
1443     }
1444
1445     # finished:
1446     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1447     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1448 }
1449
1450 \end{code}
1451  
1452 \begin{code}
1453 sub mini_mangle_asm_powerpc {
1454     local($in_asmf, $out_asmf) = @_;
1455
1456     open(INASM, "< $in_asmf")
1457         || &tidy_up_and_die(1,"$Pgm: failed to open `$in_asmf' (to read)\n");
1458     open(OUTASM,"> $out_asmf")
1459         || &tidy_up_and_die(1,"$Pgm: failed to open `$out_asmf' (to write)\n");
1460
1461     while (<INASM>) {
1462         s/long _PRStart/long ._PRStart/;
1463         s/long _PRIn_/long ._PRIn_/;
1464         s/long _Dummy_(\S+)_entry/long ._Dummy_\1_entry/;
1465         s/long _PRMarking_MarkNextRoot\[DS\]/long ._PRMarking_MarkNextRoot/;
1466         s/long _PRMarking_MarkNextCAF\[DS\]/long ._PRMarking_MarkNextCAF/;
1467         s/long _PRMarking_MarkNextAStack\[DS\]/long ._PRMarking_MarkNextAStack/;
1468         s/long _PRMarking_MarkNextBStack\[DS\]/long ._PRMarking_MarkNextBStack/;
1469         s/\.tc EnterNodeCode\[TC]\,EnterNodeCode\[DS\]/\.tc EnterNodeCode\[TC]\,.EnterNodeCode/; # CONC
1470         s/\.tc CheckHeapCode\[TC]\,CheckHeapCode\[DS\]/\.tc CheckHeapCode\[TC]\,.CheckHeapCode/; # CONC
1471         print OUTASM;
1472     }
1473
1474     # finished:
1475     close(OUTASM) || &tidy_up_and_die(1,"Failed writing to $out_asmf\n");
1476     close(INASM)  || &tidy_up_and_die(1,"Failed reading from $in_asmf\n");
1477 }
1478
1479 sub mangle_powerpc_tailjump {
1480     local($c) = @_;
1481     local($maybe_more) = 1;
1482     while (($c =~ /\tlw?z? \d+,LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n/) && $maybe_more) 
1483       { $maybe_more = 0;
1484         $lcsymb = $c;
1485         $lcsymb =~ s/(.*\n)*\tlw?z? \d+,LC\.\.(\d+)\(2\)\n\tmtctr \d+\n\tbctr\n(.*\n)*/\2/;
1486 # the checks for r1 and r2 are mostly paranoia...
1487         $r1 = $c;
1488         $r1 =~ s/(.*\n)*\tlw?z? (\d+),LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n(.*\n)*/\2/;
1489         $r2 = $c;
1490         $r2 =~ s/(.*\n)*\tlw?z? \d+,LC\.\.(\d+)\(2\)\n\tmtctr (\d+)\n\tbctr\n(.*\n)*/\3/;
1491         if (r1 == r2)
1492           { $maybe_more = 1;
1493             $c =~ s/((.*\n)*)\tlw?z? \d+,LC\.\.\d+\(2\)\n\tmtctr \d+\n\tbctr\n((.*\n)*)/\1\tb $tocequiv{$lcsymb}\n\3/;
1494           }
1495       };
1496     $c;
1497 }
1498
1499 # make "require"r happy...
1500 1;
1501 \end{code}