#!/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
# 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
# "-(%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