#!/bin/sh

#
# Copyright (c) 1994 Charles Hannum.  All rights reserved.
#
# 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.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#	This product includes software developed by Charles Hannum.
# 4. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
#
#	$Id: asm2gas,v 1.1 1994/07/05 17:57:04 mycroft Exp $
#

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

cat $1 | sed -e '
  /[ 	]IDNT[ 	]/{s/^/|/;p;d;}
  /^\*/{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/[ 	]*[.a-zA-Z][.a-zA-Z0-9]*[ 	]*//
    s/[ 	][ 	]*\(.*\)$/		|\1/
    s/		||/		|/
    x
    s/^[ 	]*\([.a-zA-Z][.a-zA-Z0-9]*\)/\1/
    s/[ 	][ 	]*.*$/	/
    y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
    s/^/	/
    G
    s/\n//
  }
' | sed -e '
  s/^\*/|/
  s/\$/0x/g
  s/#:/#0x/g
  s/	dc\.l/	.long/
  s/	dc\.w/	.short/
  s/	dc\.b/	.byte/
' | sed -e '
  s/\([^_a-zA-Z0-9]\)A\([0-7]\)\([^_a-zA-Z0-9:.]\)/\1a\2\3/g
  s/\([^_a-zA-Z0-9]\)A\([0-7]\)\([^_a-zA-Z0-9:.]\)/\1a\2\3/g
  s/\([^_a-zA-Z0-9]\)A\([0-7]\)$/\1a\2/g
  s/\([^_a-zA-Z0-9]\)D\([0-7]\)\([^_a-zA-Z0-9]\)/\1d\2\3/g
  s/\([^_a-zA-Z0-9]\)D\([0-7]\)\([^_a-zA-Z0-9]\)/\1d\2\3/g
  s/\([^_a-zA-Z0-9]\)D\([0-7]\)$/\1d\2/g
  s/\([^_a-zA-Z0-9]\)FP\([0-7]\)\([^_a-zA-Z0-9]\)/\1fp\2\3/g
  s/\([^_a-zA-Z0-9]\)FP\([0-7]\)\([^_a-zA-Z0-9]\)/\1fp\2\3/g
  s/\([^_a-zA-Z0-9]\)FP\([0-7]\)$/\1fp\2/g
  s/\([^_a-zA-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_]*\)(pc,\([ad].[^)]*\))/pc@(\1,\2)/g

  s/\.\([bBwWlL])\)/:\1/g
  s/\.\([bBwWlL]\)\*\([0-9][0-9]*)\)/:\1:\2/g
  s/\*\([0-9][0-9]*\))/:l:\1)/g
  s/{\([0-9][0-9]*\):\([0-9][0-9]*\)}/{#\1:#\2}/g
  s/{\(d[0-7]\):\([0-9][0-9]*\)}/{\1:#\2}/g

  s/@()/@/g;s/@(0)/@/g
  s/(,/(/g;s/:)/)/g

  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/^	lea\.l	/	lea	/
  s/^	swap\.w	/	swap	/
  s/^	adda\.l	/	addal	/
  s/^	suba\.l	/	subal	/
  s/^	movea\.l	/	moveal	/
  s/^	btst\.[bl]	/	btst	/
  s/^	bclr\.[bl]	/	bclr	/
  s/^	bset\.[bl]	/	bset	/
  s/^	bchg\.[bl]	/	bchg	/
  s/^	clr\.\([lbw]\)	/	clr\1	/
  s/^	tst\.\([lbw]\)	/	tst\1	/
  s/^	neg\.\([lbw]\)	/	neg\1	/
  s/^	or\(i*\)\.\([lbw]\)	/	or\1\2	/
  s/^	eor\(i*\)\.\([lbw]\)	/	eor\1\2	/
  s/^	and\(i*\)\.\([lbw]\)	/	and\1\2	/
  s/^	mul\([su]\)\.\([lw]\)	/	mul\1\2	/
  s/^	div\([sul]*\)\.\([lw]\)	/	div\1\2	/
  s/^	cmp\([i2]*\)\.\([lbw]\)	/	cmp\1\2	/
  s/^	ro\([xlr]*\)\.\([lbw]\)	/	ro\1\2	/
  s/^	\([al]\)s\([lr]\)\.\([lbw]\)	/	\1s\2\3	/
  s/^	add\([xiq]*\)\.\([lbw]\)	/	add\1\2	/
  s/^	sub\([xiq]*\)\.\([lbw]\)	/	sub\1\2	/
  s/^	move\([scmq]*\)\.\([lbw]\)	/	move\1\2	/
  s/^	fabs\.\([sdx]\)	/	fabs\1	/
  s/^	fneg\.\([sdx]\)	/	fneg\1	/
  s/^	ftst\.\([sdx]\)	/	ftst\1	/
  s/^	fmul\.\([sdx]\)	/	fmul\1	/
  s/^	fdiv\.\([sdx]\)	/	fdiv\1	/
  s/^	fsqrt\.\([sdx]\)	/	fsqrt\1	/
  s/^	fadd\.\([sdxbwl]\)	/	fadd\1	/
  s/^	fsub\.\([sdxbwl]\)	/	fsub\1	/
  s/^	fcmp\.\([sdxbwl]\)	/	fcmp\1	/
  s/^	fmove\([mx]*\)\.\([sdxbwl]\)	/	fmove\1\2	/

  # fix an odd instruction
  s/^	bhs\.w	/	bcc	/

  # set and branch instructions; questionable
  s/^	s\([a-z][a-z]*\)\.b	/	s\1	/
  s/^	db\([a-z][a-z]*\)\.w	/	db\1	/
  s/^	b\([a-eg-z][a-z]*\)\.b	/	b\1s	/
  s/^	b\([a-eg-z][a-z]*\)\.w	/	b\1	/
  s/^	b\([a-eg-z][a-z]*\)\.l	/	b\1l	/
  s/^	fb\([a-eg-z][a-z]*\)\.w	/	fb\1	/
  s/^	fb\([a-eg-z][a-z]*\)\.l	/	fb\1l	/

  # make up for a gas bug
  /^	fmovemx	/{
	s/	fp\([0-7]\),/	fp\1-fp\1,/
	s/,fp\([0-7]\)	/,fp\1-fp\1	/
	s/,fp\([0-7]\)$/,fp\1-fp\1/
  }
'
