#!/bin/sh
#
# mitToHp - convert MIT format assembler to HP9000/300 format
#
# modification history
# --------------------
# 01a,11nov87,hin  written.
#
#
# SYNOPSIS
# mitToHp [files...]
#
# DESCRIPTION
# This script reads the specified files (or standard input if no files
# are specified) as MIT format assembly language and writes to standard output
# the corresponding HP9000/300 format assembly language.
#*/

cat <<'EOF' >mitTohp_sed.tmp


# put labels on their own line

/^[a-zA-Z0-9_\$\.]*[ 	]*:.*[^ 	].*/s/:/:\
/
# output labels and delete them from current line
/^[a-zA-Z0-9_\$\.]*[ 	]*:/{
P
D
}

# transform operation field:
#   <op>[bwl] -> <op>.[bwl]
#   <op>[bwlsdxp] -> <op>.[bwlsdxp]
#   <op>[bwlsdxp] -> <op>.[bwlsdxp]
#   <op>[bwlsdxp] -> <op>.[bwlsdxp]
#   <op>[bwlsdxp] -> <op>.[bwlsdxp]
#   movec     -> mov.l
#   move      -> mov
#   fmove     -> fmov

s/^[ 	]*\([a-zA-Z][a-zA-Z][a-zA-Z]*\)\([bBwWlL]\)[ 	]/	\1.\2	/
s/^[ 	]*\([f][a-z][a-z][a-z]*\)\([bwlsdxp]\)[ 	]\([^ 	][^ 	]*\)$/	\1.\2	\3/
s/^[ 	]*\([F][A-Z][A-Z][A-Z]*\)\([BWLSDXP]\)[ 	]\([^ 	][^ 	]*\)$/	\1.\2	\3/
s/^[ 	]*\([f][a-z][a-z][a-z]*\)\([bwlsdxp]\)[ 	]\(.*\),\(.*\)$/	\1.\2	\3,\4/
s/^[ 	]*\([F][A-Z][A-Z][A-Z]*\)\([BWLSDXP]\)[ 	]\(.*\),\(.*\)$/	\1.\2	\3,\4/
s/^[ 	]*movec[ 	]/	mov.l	/
s/^[ 	]*move/	mov/
s/^[ 	]*fmove/	fmov/

# transform operand field:
#   a0@(<n>) -> <n>(a0)
#   a0@+     -> (a0)+
#   a0@-     -> -(a0)
#   a0@      -> (a0)
#   a0       -> %a0
#   f0       -> %fp0		fpp instruction
#   fp[cs]r  -> %fp[cs]r	fpp instruction
#   fpiar    -> %fpiar		fpp instruction
#   s[pr]    -> %s[pr]
#   [cp]c    -> %[cp]c
#   fp       -> %fp
#   usp      -> %usp
#   [sd]fc   -> %[sd]fc
#   #xxx     -> &xxx
#   #0[xX]xxx -> &0[xX]xxx
#   cmp* opr1,opr2  ->  cmp* opr2,opr1

s/\([aAdDsS][0-7pP]\)@(\([^ 	,]*\))/\2(\1)/g
s/\([aAdDsS][0-7pP]\)@+/(\1)+/g
s/\([aAdDsS][0-7pP]\)@-/-(\1)/g
s/\([aAdDsS][0-7pP]\)@/(\1)/g
s/\([ 	,(-/]\)\([aAdD][0-7]\)/\1\%\2/g
s/\([ 	,(-/]\)\([fF]\)\([0-7]\)/\1\%\2p\3/g
s/\([ 	,(-/]\)\([fF][pP][cCsS][rR]\)/\1\%\2/g
s/\([ 	,(-/]\)\([fF][pP][iI][aA][rR]\)/\1\%\2/g
s/\([ 	,(-/]\)\([sS][pPrR]\)\([^a-zA-Z_]\)/\1\%\2\3/g
s/\([ 	,(-/]\)\([pPcC][cC]\)\([^a-zA-Z_]\)/\1\%\2\3/g
s/\([ 	,(-/]\)\([fF][pP]\)\([^a-zA-Z_]\)/\1\%\2\3/g
s/\([ 	,(-/]\)\([uU][sS][pP]\)\([^a-zA-Z_]\)/\1\%\2\3/g
s/\([ 	,(-/]\)\([sSdD][fF][cC]\)\([^a-zA-Z_]\)/\1\%\2\3/g
s/\([ 	,]\)\#\([0-9]*\)/\1\&\2/g
s/\([ 	,]\)\#\(0[xX][0-9a-fA-F]*\)/\1\&\2/g
s/\(cmp.*\)[ 	]\(.*\),\(.*\)/\1	\3,\2/

# transform assembler directives

s/^[ 	]*\.globl/	global/
s/^[ 	]*\.byte/	byte/
s/^[ 	]*\.word/	short/
s/^[ 	]*\.int/	long/
s/^[ 	]*\.long/	long/
s/^[ 	]*\.asciz/	asciz	/
s/^[ 	]*\.text/	text/
s/^[ 	]*\.bss/	bss/
s/^[ 	]*\.data/	data/

s/^[ 	]*\.align/	lalign 2/
s/^[ 	]*\.even/	lalign 2/
EOF

sed -f mitTohp_sed.tmp $* 

rm -f mitTohp_sed.tmp 

exit 0
