1#!/bin/sh
2#         $NetBSD: asm2gas,v 1.9 2008/04/28 20:23:26 martin Exp $
3
4#
5# Copyright (c) 1998,2008 The NetBSD Foundation, Inc.
6# All rights reserved.
7#
8# This code is derived from software contributed to The NetBSD Foundation
9# by Charles M. Hannum.
10#
11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions
13# are met:
14# 1. Redistributions of source code must retain the above copyright
15#    notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright
17#    notice, this list of conditions and the following disclaimer in the
18#    documentation and/or other materials provided with the distribution.
19#
20# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31#
32
33# This ugly script converts assembler code from Motorola's format to a
34# form that gas (MIT syntax) can digest.
35
36: ${SED:=sed}       # Which sed to use
37P=''                # Prefix for register names, may be '%' or ''
38
39cat "$1" | "${SED}" -e '
40  # format canonicalization
41
42  # leave "#include" alone; change "#" and "*" comment lines to use "|".
43  /^\#include/{p;d;}
44  /^\#/{s//|#/;p;d;}
45  /^\*/{s//|/;p;d;}
46  /[      ]IDNT[    ]/{s/^/|/;p;d;}
47  s/;/|/
48  /[      ]equ[     ]/{
49    s/\([A-Za-z_][A-Za-z0-9_]*\)[       ]*equ[    ]*/\1,/
50    s/[   ][        ]*\(.*\)$/                    |\1/
51    s/              ||/                 |/
52    s/^/  .set      /
53    p;d
54  }
55  s/^\([A-Za-z_][A-Za-z0-9_]*\)[        ][        ]*/\1:    /
56  s/^\([A-Za-z_][A-Za-z0-9_]*\)$/\1:/
57  /^[A-Za-z_][A-Za-z0-9_]*:/{
58    h
59    s/:.*$/:/
60    p
61    g
62    s/^.*:[         ]*/       /
63    /^    $/d
64  }
65  /^[     ][        ]*\([.a-zA-Z][.a-zA-Z0-9]*\)/{
66    h
67    s///
68    s/^[  ][        ]*//
69    s/[   ][        ]*\(.*\)$/                    |\1/
70    s/              ||/                 |/
71    x
72    s/^[  ][        ]*//
73    s/[   ][        ]*.*$/    /
74    y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
75    s/^/  /
76    G
77    s/\n//
78  }
79' | "${SED}" -e '
80  # operator conversion
81
82  s/^     section   7/        .text/
83  s/^     section   8/        .text/
84  s/^     section   15/       .data/
85  /^      include/{s/include[           ]/.include "/;s/\.h[          ]*$/.defs"/;p;d;}
86  s/^     xref/|    xref/
87  s/^     end/|     end/
88  s/^     xdef/     .global/
89
90  s/^     dc\.l/    .long/
91  s/^     dc\.w/    .short/
92  s/^     dc\.b/    .byte/
93
94  /^      [aceg-z]/{
95    /^    add[aiqx]*\.[bwl]   /{s/\.//;p;d;}
96    /^    andi*\.[bwl]        /{s/\.//;p;d;}
97    /^    as[lr]\.[bwl]       /{s/\.//;p;d;}
98    /^    clr\.[bwl]          /{s/\.//;p;d;}
99    /^    cmp[i2]*\.[bwl]     /{s/\.//;p;d;}
100    /^    eori*\.[bwl]        /{s/\.//;p;d;}
101    /^    lea\.l    /{s/\..//;p;d;}
102    /^    ls[lr]\.[bwl]       /{s/\.//;p;d;}
103    /^    move[acmqs]*\.[bwl] /{s/\.//;p;d;}
104    /^    mul[su]\.[wl]       /{s/\.//;p;d;}
105    /^    neg\.[bwl]          /{s/\.//;p;d;}
106    /^    ori*\.[bwl]         /{s/\.//;p;d;}
107    /^    ro[lrx]*\.[bwl]     /{s/\.//;p;d;}
108    /^    sub[aiqx]*\.[bwl]   /{s/\.//;p;d;}
109    /^    swap\.w   /{s/\..//;p;d;}
110    /^    s\([a-tv-z][a-z]*\)\.b        /{s/\..//;p;d;}
111    /^    tst\.[bwl]          /{s/\.//;p;d;}
112    p;d
113  }
114
115  /^      bchg\.[bl]          /{s/\..//;p;d;}
116  /^      bclr\.[bl]          /{s/\..//;p;d;}
117  /^      bset\.[bl]          /{s/\..//;p;d;}
118  /^      btst\.[bl]          /{s/\..//;p;d;}
119  /^      div[sul]*\.[wl]     /{s/\.//;p;d;}
120  /^      fabs\.[sdx]         /{s/\.//;p;d;}
121  /^      fadd\.[sdxbwl]      /{s/\.//;p;d;}
122  /^      fcmp\.[sdxbwl]      /{s/\.//;p;d;}
123  /^      fdiv\.[sdx]         /{s/\.//;p;d;}
124  /^      fmove[mx]*\.[sdxbwl]          /{s/\.//;p;d;}
125  /^      fmul\.[sdx]         /{s/\.//;p;d;}
126  /^      fneg\.[sdx]         /{s/\.//;p;d;}
127  /^      fsqrt\.[sdx]        /{s/\.//;p;d;}
128  /^      fsub\.[sdxbwl]      /{s/\.//;p;d;}
129  /^      ftst\.[sdx]         /{s/\.//;p;d;}
130
131  /^      b[a-eg-z][a-z]*\.b  /{s/\.b/s/;p;d;}
132  /^      b[a-eg-z][a-z]*\.w  /{s/\.w//;p;d;}
133  /^      b[a-eg-z][a-z]*\.l  /{s/\.l/l/;p;d;}
134  /^      db[a-z][a-z]*\.w    /{s/\.w//;p;d;}
135  /^      fb[a-eg-z][a-z]*\.w /{s/\.w//;p;d;}
136  /^      fb[a-eg-z][a-z]*\.l /{s/\.l/l/;p;d;}
137' | "${SED}" -e '
138  # operand conversion
139
140  # register names "FPIAR" -> "%FPI", etc., possibly without the "%"
141  s/\([^_a-zA-Z0-9]\)FPIAR\([^_a-zA-Z0-9]\)/\1'"$P"'FPI\2/g
142  s/\([^_a-zA-Z0-9]\)FPIAR$/\1'"$P"'FPI/g
143  s/\([^_a-zA-Z0-9]\)fpiar\([^_a-zA-Z0-9]\)/\1'"$P"'fpi\2/g
144  s/\([^_a-zA-Z0-9]\)fpiar$/\1'"$P"'fpi/g
145  s/\([^_a-zA-Z0-9]\)FPCR\([^_a-zA-Z0-9]\)/\1'"$P"'FPCR\2/g
146  s/\([^_a-zA-Z0-9]\)FPCR$/\1'"$P"'FPCR/g
147  s/\([^_a-zA-Z0-9]\)fpcr\([^_a-zA-Z0-9]\)/\1'"$P"'fpcr\2/g
148  s/\([^_a-zA-Z0-9]\)fpcr$/\1'"$P"'fpcr/g
149  s/\([^_a-zA-Z0-9]\)FPSR\([^_a-zA-Z0-9]\)/\1'"$P"'FPSR\2/g
150  s/\([^_a-zA-Z0-9]\)FPSR$/\1'"$P"'FPSR/g
151  s/\([^_a-zA-Z0-9]\)fpsr\([^_a-zA-Z0-9]\)/\1'"$P"'fpsr\2/g
152  s/\([^_a-zA-Z0-9]\)fpsr$/\1'"$P"'fpsr/g
153
154  # Hexadecimal numbers
155  s/\$\([0-9a-fA-F]\)/0x\1/g
156  s/#:/#:0x/g
157
158  # Insert "%" before more register names (only if $P = "%").
159  # Some of the rules are repeated because of overlap between trailing
160  # context in one match and leading context in another match; otherwise
161  # only half the register names in "d4{d3:4},d0" would be converted.
162  s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
163  s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
164  s/\([^[:alnum:]_%]\)\(sp\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
165  s/\([^[:alnum:]_%]\)\(pc\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
166
167  s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
168  s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
169  s/\([^[:alnum:]_%]\)\(sp\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
170  s/\([^[:alnum:]_%]\)\(pc\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
171
172  s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)$/\1'"$P"'\2/g
173  s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)$/\1'"$P"'\2/g
174  s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)$/\1'"$P"'\2/g
175  s/\([^[:alnum:]_%]\)\(sp\)$/\1'"$P"'\2/g
176
177  s/\(,\)\([dDaA][0-7]\)/\1'"$P"'\2/g
178  s/\(,\)\([fF][pP][0-7]\)/\1'"$P"'\2/g
179
180  # "-(%sp)" -> "%sp@-", etc. (possibly without the "%")
181  s/-(\('"$P"'[sSpPaA][pPcC0-7]\))/\1@-/g
182  # "(%sp)+" -> "%sp@+", etc. (possibly without the "%")
183  s/(\('"$P"'[sSpPaA][pPcC0-7]\))+/\1@+/g
184  # "foo(%sp,...)" -> "%sp@(foo,...)", etc. (possibly without the "%")
185  s/\([-+A-Za-z0-9_]*\)(\('"$P"'[sSpPaA][pPcC0-7]\)\([),]\)/\2@(\1\3/g
186
187  # ".w" -> ":w"; ".w*nn" -> ":w:nn"; "*nn" -> ":l:nn"; etc.
188  s/\.\([bBwWlL])\)/:\1/g
189  s/\.\([bBwWlL]\)\*\([0-9][0-9]*)\)/:\1:\2/g
190  s/\*\([0-9][0-9]*\))/:l:\1)/g
191  # "{nn:mm}" -> "{#nn:#mm}"
192  s/{\([0-9][0-9]*\):\([0-9][0-9]*\)}/{#\1:#\2}/g
193  # "{%d0:nn}" -> "{%d0:#nn}", etc. (possibly without the "%")
194  s/{\('"$P"'[dD][0-7]\):\([0-9][0-9]*\)}/{\1:#\2}/g
195
196  # Remove empty "()" or "(0)" after "@"
197  s/@(0*)/@/g
198  # Remove leading "," or trailing ":" in parentheses
199  s/(,/(/g;s/:)/)/g
200
201  # make up for a gas bug
202  /^      fmovemx   /{
203          s/        \('"$P"'[fF][pP][0-7]\),/     \1-\1,/
204          s/,\('"$P"'[fF][pP][0-7]\)    /,\1-\1   /
205          s/,\('"$P"'[fF][pP][0-7]\)$/,\1-\1/
206  }
207'
208