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