#!/bin/sh
#       $NetBSD: asm2gas,v 1.10 2008/04/28 20:23:26 martin Exp $

#
# Copyright (c) 1998,2008 The NetBSD Foundation, Inc.
# All rights reserved.
#
# This code is derived from software contributed to The NetBSD Foundation
# by Charles M. Hannum.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# This ugly script converts assembler code from Motorola's format to a
# form that gas (MIT syntax) can digest.

: ${SED:=sed}   # Which sed to use
P='%'           # Prefix for register names, may be '%' or ''

cat "$1" | "${SED}" -e '
 # format canonicalization

 # leave "#include" alone; change "#" and "*" comment lines to use "|".
 /^\#include/{p;d;}
 /^\#/{s//|#/;p;d;}
 /^\*/{s//|/;p;d;}
 /[    ]IDNT[  ]/{s/^/|/;p;d;}
 s/;/|/
 /[    ]equ[   ]/{
   s/\([A-Za-z_][A-Za-z0-9_]*\)[       ]*equ[  ]*/\1,/
   s/[         ][      ]*\(.*\)$/              |\1/
   s/          ||/             |/
   s/^/        .set    /
   p;d
 }
 s/^\([A-Za-z_][A-Za-z0-9_]*\)[        ][      ]*/\1:  /
 s/^\([A-Za-z_][A-Za-z0-9_]*\)$/\1:/
 /^[A-Za-z_][A-Za-z0-9_]*:/{
   h
   s/:.*$/:/
   p
   g
   s/^.*:[     ]*/     /
   /^  $/d
 }
 /^[   ][      ]*\([.a-zA-Z][.a-zA-Z0-9]*\)/{
   h
   s///
   s/^[        ][      ]*//
   s/[         ][      ]*\(.*\)$/              |\1/
   s/          ||/             |/
   x
   s/^[        ][      ]*//
   s/[         ][      ]*.*$/  /
   y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
   s/^/        /
   G
   s/\n//
 }
' | "${SED}" -e '
 # operator conversion

 s/^   section 7/      .text/
 s/^   section 8/      .text/
 s/^   section 15/     .data/
 /^    include/{s/include[     ]/.include "/;s/\.h[    ]*$/.defs"/;p;d;}
 s/^   xref/|  xref/
 s/^   end/|   end/
 s/^   xdef/   .global/

 s/^   dc\.l/  .long/
 s/^   dc\.w/  .short/
 s/^   dc\.b/  .byte/

 /^    [aceg-z]/{
   /^  add[aiqx]*\.[bwl]       /{s/\.//;p;d;}
   /^  andi*\.[bwl]    /{s/\.//;p;d;}
   /^  as[lr]\.[bwl]   /{s/\.//;p;d;}
   /^  clr\.[bwl]      /{s/\.//;p;d;}
   /^  cmp[i2]*\.[bwl] /{s/\.//;p;d;}
   /^  eori*\.[bwl]    /{s/\.//;p;d;}
   /^  lea\.l  /{s/\..//;p;d;}
   /^  ls[lr]\.[bwl]   /{s/\.//;p;d;}
   /^  move[acmqs]*\.[bwl]     /{s/\.//;p;d;}
   /^  mul[su]\.[wl]   /{s/\.//;p;d;}
   /^  neg\.[bwl]      /{s/\.//;p;d;}
   /^  ori*\.[bwl]     /{s/\.//;p;d;}
   /^  ro[lrx]*\.[bwl] /{s/\.//;p;d;}
   /^  sub[aiqx]*\.[bwl]       /{s/\.//;p;d;}
   /^  swap\.w /{s/\..//;p;d;}
   /^  s\([a-tv-z][a-z]*\)\.b  /{s/\..//;p;d;}
   /^  tst\.[bwl]      /{s/\.//;p;d;}
   p;d
 }

 /^    bchg\.[bl]      /{s/\..//;p;d;}
 /^    bclr\.[bl]      /{s/\..//;p;d;}
 /^    bset\.[bl]      /{s/\..//;p;d;}
 /^    btst\.[bl]      /{s/\..//;p;d;}
 /^    div[sul]*\.[wl] /{s/\.//;p;d;}
 /^    fabs\.[sdx]     /{s/\.//;p;d;}
 /^    fadd\.[sdxbwl]  /{s/\.//;p;d;}
 /^    fcmp\.[sdxbwl]  /{s/\.//;p;d;}
 /^    fdiv\.[sdx]     /{s/\.//;p;d;}
 /^    fmove[mx]*\.[sdxbwl]    /{s/\.//;p;d;}
 /^    fmul\.[sdx]     /{s/\.//;p;d;}
 /^    fneg\.[sdx]     /{s/\.//;p;d;}
 /^    fsqrt\.[sdx]    /{s/\.//;p;d;}
 /^    fsub\.[sdxbwl]  /{s/\.//;p;d;}
 /^    ftst\.[sdx]     /{s/\.//;p;d;}

 /^    b[a-eg-z][a-z]*\.b      /{s/\.b/s/;p;d;}
 /^    b[a-eg-z][a-z]*\.w      /{s/\.w//;p;d;}
 /^    b[a-eg-z][a-z]*\.l      /{s/\.l/l/;p;d;}
 /^    db[a-z][a-z]*\.w        /{s/\.w//;p;d;}
 /^    fb[a-eg-z][a-z]*\.w     /{s/\.w//;p;d;}
 /^    fb[a-eg-z][a-z]*\.l     /{s/\.l/l/;p;d;}
' | "${SED}" -e '
 # operand conversion

 # register names "FPIAR" -> "%FPI", etc., possibly without the "%"
 s/\([^_a-zA-Z0-9]\)FPIAR\([^_a-zA-Z0-9]\)/\1'"$P"'FPI\2/g
 s/\([^_a-zA-Z0-9]\)FPIAR$/\1'"$P"'FPI/g
 s/\([^_a-zA-Z0-9]\)fpiar\([^_a-zA-Z0-9]\)/\1'"$P"'fpi\2/g
 s/\([^_a-zA-Z0-9]\)fpiar$/\1'"$P"'fpi/g
 s/\([^_a-zA-Z0-9]\)FPCR\([^_a-zA-Z0-9]\)/\1'"$P"'FPCR\2/g
 s/\([^_a-zA-Z0-9]\)FPCR$/\1'"$P"'FPCR/g
 s/\([^_a-zA-Z0-9]\)fpcr\([^_a-zA-Z0-9]\)/\1'"$P"'fpcr\2/g
 s/\([^_a-zA-Z0-9]\)fpcr$/\1'"$P"'fpcr/g
 s/\([^_a-zA-Z0-9]\)FPSR\([^_a-zA-Z0-9]\)/\1'"$P"'FPSR\2/g
 s/\([^_a-zA-Z0-9]\)FPSR$/\1'"$P"'FPSR/g
 s/\([^_a-zA-Z0-9]\)fpsr\([^_a-zA-Z0-9]\)/\1'"$P"'fpsr\2/g
 s/\([^_a-zA-Z0-9]\)fpsr$/\1'"$P"'fpsr/g

 # Hexadecimal numbers
 s/\$\([0-9a-fA-F]\)/0x\1/g
 s/#:/#:0x/g

 # Insert "%" before more register names (only if $P = "%").
 # Some of the rules are repeated because of overlap between trailing
 # context in one match and leading context in another match; otherwise
 # only half the register names in "d4{d3:4},d0" would be converted.
 s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\(sp\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\(pc\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g

 s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\(sp\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g
 s/\([^[:alnum:]_%]\)\(pc\)\([^[:alnum:]_]\)/\1'"$P"'\2\3/g

 s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)$/\1'"$P"'\2/g
 s/\([^[:alnum:]_%]\)\([dDaA][0-7]\)$/\1'"$P"'\2/g
 s/\([^[:alnum:]_%]\)\([fF][pP][0-7]\)$/\1'"$P"'\2/g
 s/\([^[:alnum:]_%]\)\(sp\)$/\1'"$P"'\2/g

 s/\(,\)\([dDaA][0-7]\)/\1'"$P"'\2/g
 s/\(,\)\([fF][pP][0-7]\)/\1'"$P"'\2/g

 # "-(%sp)" -> "%sp@-", etc. (possibly without the "%")
 s/-(\('"$P"'[sSpPaA][pPcC0-7]\))/\1@-/g
 # "(%sp)+" -> "%sp@+", etc. (possibly without the "%")
 s/(\('"$P"'[sSpPaA][pPcC0-7]\))+/\1@+/g
 # "foo(%sp,...)" -> "%sp@(foo,...)", etc. (possibly without the "%")
 s/\([-+A-Za-z0-9_]*\)(\('"$P"'[sSpPaA][pPcC0-7]\)\([),]\)/\2@(\1\3/g

 # ".w" -> ":w"; ".w*nn" -> ":w:nn"; "*nn" -> ":l:nn"; etc.
 s/\.\([bBwWlL])\)/:\1/g
 s/\.\([bBwWlL]\)\*\([0-9][0-9]*)\)/:\1:\2/g
 s/\*\([0-9][0-9]*\))/:l:\1)/g
 # "{nn:mm}" -> "{#nn:#mm}"
 s/{\([0-9][0-9]*\):\([0-9][0-9]*\)}/{#\1:#\2}/g
 # "{%d0:nn}" -> "{%d0:#nn}", etc. (possibly without the "%")
 s/{\('"$P"'[dD][0-7]\):\([0-9][0-9]*\)}/{\1:#\2}/g

 # Remove empty "()" or "(0)" after "@"
 s/@(0*)/@/g
 # Remove leading "," or trailing ":" in parentheses
 s/(,/(/g;s/:)/)/g

 # make up for a gas bug
 /^    fmovemx /{
       s/      \('"$P"'[fF][pP][0-7]\),/       \1-\1,/
       s/,\('"$P"'[fF][pP][0-7]\)      /,\1-\1 /
       s/,\('"$P"'[fF][pP][0-7]\)$/,\1-\1/
 }
' | "${SED}" -e '
 # Floating point literal conversion

 s/:0x41dfffffffc00000/0r2147483647.0/g
 s/:0xc1e0000000000000/0r-2147483648.0/g
 s/:0x41dfffffffe00000/0r2147483647.5/g
 s/:0xc1e0000000100000/0r-2147483648.5/g
 s/:0x46fffe00/0r32767.0/g
 s/:0xc7000000/0r-32768.0/g
 s/:0x46ffff00/0r32767.5/g
 s/:0xc7000080/0r-32768.5/g
 s/:0x42fe0000/0r127.0/g
 s/:0xc3000000/0r-128.0/g
 s/:0x42ff0000/0r127.5/g
 s/:0xc3008000/0r-128.5/g
 s/:0x3F800000/0r1.0/g
 s/:0x00000000/0r0.0/g
 s/:0xBF800000/0r-1.0/g
 s/:0x3F000000/0r0.5/g
 s/:0x3E800000/0r0.25/g
 s/:0x42B8AA3B/0r92.332481384277343750/g
 s/:0xBC317218/0r-0.0108304247260093688964843750/g
 s/:0x3AB60B70/0r0.001388890668749809265136718750/g
 s/:0x3C088895/0r0.0083333449438214302062988281250/g
 s/:0x42B8AA3B/0r92.332481384277343750/g
 s/:0x3950097B/0r0.0001983995753107592463493347167968750/g
 s/:0x3AB60B6A/0r0.001388889970257878303527832031250/g
 s/:0x2F30CAA8/0r1.60791047143504783889511600136756896972656250e-10/g
 s/:0x310F8290/0r2.0883454965314740547910332679748535156250e-09/g
 s/:0x32D73220/0r2.5052088403754169121384620666503906250e-08/g
 s/:0x3493F281/0r2.755732850800995947793126106262207031250e-07/g
 s/:0x40000000/0r2.0/g
 s/:0x42800000/0r6.40e+01/g
 s/:0x3C800000/0r1.56250e-02/g
 s/fadds       #:0x00800000,'"$P"'[fF][pP]0/   .long   0xf23c4422,0x00800000/
 s/fsubs       #:0x00800000,'"$P"'[fF][pP]0/   .long   0xf23c4428,0x00800000/
 s/fsubs       #:0x00800000,'"$P"'[fF][pP]1/   .long   0xf23c44a8,0x00800000/
 s/fmoves      #:0x80000000,'"$P"'[fF][pP]0/   .long   0xf23c4400,0x80000000/
 s/fmoves              #:0x00000000,'"$P"'[fF][pP]0/   .long   0xf23c4400,0x00000000/

'