#!/bin/sh
#
# mitToHk - convert MIT format assembler to Heurikon UniPlus+ Version 5.2 format
#
# modification history
# --------------------
# 01a,17dec87,ecs  ripped off from mitToHp version 01a.
#
#
# 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.
#*/

# System V Release 2 sed can't eat comments "#" when reading from a file

cat <<'EOF' | sed -e "/^#/d" >mitToHk_sed.tmp


# delete prepended underscores from labels
s/\([^a-zA-Z0-9_\$\.]\)_\([a-zA-Z0-9_\$\.]*\)/\1\2/g
s/^_//

# 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
#   moveq     -> mov
#   move      -> mov
#   fmove     -> fmov
#   bccs      -> bcc
#   bcss      -> bcs
#   beqs      -> beq
#   bges      -> bge
#   bgts      -> bgt
#   bhis      -> bhi
#   bhss      -> bhs
#   bles      -> ble
#   blos      -> blo
#   blss      -> bls
#   blts      -> blt
#   bmis      -> bmi
#   bnes      -> bne
#   bpls      -> bpl
#   bras      -> bra
#   bsrs      -> bsr
#   bvcs      -> bvc
#   bvss      -> bvs

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/^[ 	]*moveq/	mov/
s/^[ 	]*move/	mov/
s/^[ 	]*fmove/	fmov/
s/^[ 	]*bccs/	bcc/
s/^[ 	]*bcss/	bcs/
s/^[ 	]*beqs/	beq/
s/^[ 	]*bges/	bge/
s/^[ 	]*bgts/	bgt/
s/^[ 	]*bhis/	bhi/
s/^[ 	]*bhss/	bhs/
s/^[ 	]*bles/	ble/
s/^[ 	]*blos/	blo/
s/^[ 	]*blss/	bls/
s/^[ 	]*blts/	blt/
s/^[ 	]*bmis/	bmi/
s/^[ 	]*bnes/	bne/
s/^[ 	]*bpls/	bpl/
s/^[ 	]*bras/	bra/
s/^[ 	]*bsrs/	bsr/
s/^[ 	]*bvcs/	bvc/
s/^[ 	]*bvss/	bvs/

# 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]\)\([^a-zA-Z0-9_]\)/\1\%\2p\3\4/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/#/
s/^[ 	]*\.text/	text/
s/^[ 	]*\.bss/	bss/
s/^[ 	]*\.data/	data/

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

sed -f mitToHk_sed.tmp $* 

rm -f mitToHk_sed.tmp 

exit 0
