#
# awk program by F.Frances
#  : converts output of gcc02 (proprietary Franklin assembler)
#    to Archie Cobbs' a2asm assembler
#
BEGIN {
# first field is a label, second is opcode or pseudo-instruction
	FS="[ \t]+"
# pretty print ...
	OFS="\t"
# skip the beginning of the file
	for (i=0;i<31;i++)
		getline
# end add this instead
	print "#include \"gcc_app.h\""
	print "#include \"gcc_lib02.h\""
	print "#include \"gcc_macros02.h\""
	}
#
# skip over whole comment lines
#
substr($1,1,1)==";" { print ; next }
substr($1,1,2)=="/*" { print ; next }
#
# argh, take care of strings, unless you want to destroy the spacing inside !
# and change the hexadecimal syntax in strings... and zero too
# (also, a2asm reads hexadecimal integers instead of hex chars, and then
# complains if a third hex figure follows, so we insert two double quotes...)
# oh my god...
#
/".*"/ { sub(/text/,".ascii")
	gsub(/\/\$/,"\\x")
	gsub(/\\x../,"&\"\"")
	gsub(/\/0/,"\\0")
	print
	next }
#
# handle local labels :
#
# .l1, .l2 ,etc. are used for small branches
#   (we replace the initial dot by an 'l' since L1, L2, etc. are also used for
#   longer branches)
#
# .sp is used for local space and duplicated if several functions are there,
#   we have to substitute it with a unique name
#   (it seems local regions are delimited with 'loc')
#
substr($1,1,2)==".l" { $1= "ll" substr($1,3) }
$2=="bmi" || $2=="bpl" || $2=="bne" || $2=="beq" || $2=="bvs" || $2=="bvc" || $2=="bcc" || $2=="bcs" {
	if (substr($3,1,1)==".")
		$3="ll" substr($3,3)
	}
$2=="loc" { counter++ ; next }
/\.sp/ { sub(/\.sp/, "locsp" counter) }
#
# ouch, substitute parenthesis with brackets in expressions (these can be
# anywhere, and we shouldn't substitute indirect access !)
# (ok, only substituting in immediate operands for now)
# and change the indirect mode syntax from (...),0 to (...)
# 
/#.*(.*)/ { sub(/\(/,"[") ; sub(/\)/,"]") }
/(.*),0/ { sub(/\),0/,")") }
#
# change the mnemonic for DCA to DEC
$2=="dca" { $2="dec" }
# substitute some pseudo-instructions
$2=="incl" { $1="#include" ; $2="" }
$2=="ifdef" { $1="#ifdef" ; $2="" }
$2=="ifndef" { $1="#ifndef" ; $2="" }
$2=="endi" { $1="#endif" ; $2="" }
$2=="def" { $2=".global" }
$2=="ds" { $2=".ds" }
# segment definitions
# we don't have such 'shared' segments, so we don't share...
$2=="cseg" { $2=".code" }
$2=="dseg" { $2=".data" }
$2=="share" { $2=".bss" ; $3="" }
#
# Ugl.. 'ref' statements may be emitted for local things
# hopefully, missing references are "extern" by default
# so we skip the 'ref' (could be a problem for zref though)
$2=="ref" { next }
#
# substitute some operators
$3=="#low" { $3="#<" }
$3=="#high" { $3="#>" }
# this one has to be computed : crossing fingers for a positive constant...
$3=="#bank" { $3="#" ; $4=int($4/65536) }
#
# substitute some macros
#
substr($2,1,5)=="CSEG_" { $2=".code" }
substr($2,1,5)=="TSEG_" { $2=".code" }
substr($2,1,5)=="TEXT_" { $2=".code" }
$2=="d8" { $2=".byte" }
$2=="d16" { $2=".word" }
$2=="d24" { $2=".byte" ; tmp=int($3/256) ; $3=$3%256 "," tmp%256 "," int(tmp/256) }
$2=="fbeg" || $2=="fend" { next }
$2=="_begin_pushregs" || $2=="_end_pushregs" { next }
$2=="_begin_popregs" || $2=="_end_popregs" { next }
$2=="_pop16_r8_end_popregs_exit" { $2="jmp" ; $3="lib_exit_pop16_r8" }
$2=="push_8" { print $1,"lda",$3 
		print "","pha" 
		next }
$2=="push_16" { print $1,"lda",$3"+1"
		print "","pha"
		print "","lda",$3
		print "","pha"
		next }
$2=="push_24" {
	print $1,"lda",$3"+2"
	print "","pha"
	print "","lda",$3"+1"
	print "","pha"
	print "","lda",$3
	print "","pha"
	next
	}
$2=="pop_8" {
	print $1,"pla"
	print "","sta",$3
	next
	}
$2=="pop_16" {
	print $1,"pla"
	print "","sta",$3
	print "","pla"
	print "","sta",$3"+1"
	next
	}
$2=="pop_24" {
	print $1,"pla"
	print "","sta",$3
	print "","pla"
	print "","sta",$3"+1"
	print "","pla"
	print "","sta",$3"+2"
	next
	}
#
# simplistic translations, only the assembler could resolve such things
#
$2=="jmi" { $2="bpl" OFS "*+5" ORS OFS "jmp" }
$2=="jpl" { $2="bmi" OFS "*+5" ORS OFS "jmp" }
$2=="jne" { $2="beq" OFS "*+5" ORS OFS "jmp" }
$2=="jeq" { $2="bne" OFS "*+5" ORS OFS "jmp" }
$2=="jcc" { $2="bcs" OFS "*+5" ORS OFS "jmp" }
$2=="jcs" { $2="bcc" OFS "*+5" ORS OFS "jmp" }
$2=="jma" { $2="jmp" }
#
# finally, we print the modified input
#
{ print }
