#! /usr/bin/perl
#
#  TOPPERS/JSP Kernel
#      Toyohashi Open Platform for Embedded Real-Time Systems/
#      Just Standard Profile Kernel
# 
#  Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
#                              Toyohashi Univ. of Technology, JAPAN
#  Copyright (C) 2004-2005 by Embedded and Real-Time Systems Laboratory
#              Graduate School of Information Science, Nagoya Univ., JAPAN
# 
#  L쌠҂́Cȉ (1)`(4) ̏CFree Software Foundation 
#  ɂČ\Ă GNU General Public License  Version 2 ɋL
#  qĂ𖞂ꍇɌC{\tgEFAi{\tgEFA
#  ς̂܂ށDȉjgpEEρEĔzziȉC
#  pƌĂԁj邱Ƃ𖳏ŋD
#  (1) {\tgEFA\[XR[ȟ`ŗpꍇɂ́CL̒
#      \C̗pщL̖ۏ؋K肪Ĉ܂܂̌`Ń\[
#      XR[hɊ܂܂Ă邱ƁD
#  (2) {\tgEFACCu`ȂǁC̃\tgEFAJɎg
#      pł`ōĔzzꍇɂ́CĔzzɔhLgip
#      ҃}jAȂǁjɁCL̒쌠\C̗pщL
#      ̖ۏ؋Kfڂ邱ƁD
#  (3) {\tgEFAC@ɑgݍނȂǁC̃\tgEFAJɎg
#      płȂ`ōĔzzꍇɂ́Ĉꂩ̏𖞂
#      ƁD
#    (a) ĔzzɔhLgip҃}jAȂǁjɁCL̒
#        쌠\C̗pщL̖ۏ؋Kfڂ邱ƁD
#    (b) Ĕzž`ԂCʂɒ߂@ɂāCTOPPERSvWFNg
#        񍐂邱ƁD
#  (4) {\tgEFA̗pɂ蒼ړI܂͊ԐړIɐ邢Ȃ鑹
#      QCL쌠҂TOPPERSvWFNgƐӂ邱ƁD
# 
#  {\tgEFÁCۏ؂Œ񋟂Ă̂łDL쌠҂
#  TOPPERSvWFNǵC{\tgEFAɊւāC̓Kp\
#  ܂߂āCȂۏ؂sȂD܂C{\tgEFA̗pɂ蒼
#  ړI܂͊ԐړIɐȂ鑹QɊւĂC̐ӔC𕉂ȂD
# 
#  @(#) $Id: genoffset,v 1.1 2008/06/17 00:04:55 suikan Exp $
# 

#
#  
#
$infile = $ARGV[0];

#
#  rbgT[`
#
sub search_bit {
	my($val) = @_;
	my($val_bit);

	return(-1) if ($val == 0);

	$val_bit = 0;
	while (($val & 1) == 0) {
		$val >>= 1;
		$val_bit++;
	}
	return($val_bit);
}

#
#  ItZbgl̏o
#
sub parse_offset {
	if ($line =~ /OFFSET_DEF ([^ \t]+) = [#\$]?([^ \t]+)/) {
		printf "#define %s\t%d\n",$1,$2;
	}
}

#
#  ߂łfBNeBũe[u
#
%directives = (
	"long", "",
	"dword", "",
	"word", "",
	"hword", "",
	"int", "",
	"short", "",
	"half", "",
	"byte", "",
	"value", "",
	"uaword", "",
	"uashort", "",
	"data32", 4,
	"data16", 2,
	"data8", 1,
	"zero", -1,
	"space", -1,
	"globl", 0,
	"global", 0,
	"align", 0,
	"p2align", 0,
	"even", 0,
	"data", 0,
	"stabs", 0,
	"type", 0,
	"size", 0,
	"section", 0,
	"sdata", 0,
	"code", 0,
);

#
#  rbgʒȕo
#
sub parse_bit {
	my($endian, $size) = @_;
	my($offset, $dir, $val, $val_bit);

	return unless ($line =~ /BIT_([BL])([BHW])_([^ \t]+):/);
	$label = $3;

	$offset = 0;
	while ($line = <INFILE>) {
		chomp $line;
		next if ($line =~ /^[ \t]*[#;].*$/);

		if ($line =~ /[ \t]*\.([a-zA-Z0-9]+)[ \t]*([^ \t]*)[ \t]*/
				&& defined($dir = $directives{$1})) {
			$val = $2;

			# 16i8i̐lւ̕ϊ
			if ($val =~ /^0x(.+)$/) {
				$val = hex($1);
			}
			elsif ($val =~ /^0(.+)$/) {
				$val = oct($1);
			}

			if ($dir eq "") {
				# TCYmȂfBNeBu
				print STDERR "genoffset: unknown directive: ",
						"$line\n";
				$error = 1;
			}
			elsif ($dir == 0) {
				# ǂݔ΂ׂfBNeBu
				next;
			}
			elsif ($dir == -1) {
				# .zero fBNeBȕ
				$offset += $val;
				next;
			}
			elsif ($val == 0) {
				# l 0 ̃tB[h̏
				$offset += $dir;
				next;
			}

			# rbgʒuT
			$val_bit = do search_bit($val);

			# oCgPʂɊZ
			if ($endian eq "B") {
				$offset += $dir - 1;
				$offset -= $val_bit >> 3;
			}
			else {
				$offset += $val_bit >> 3;
			}
			$val_bit &= 0x07;

			# o͒PʂɊZ
			if ($size eq "W") {
				if ($endian eq "B") {
					$val_bit += 24;
					$val_bit -= ($offset & 0x03) << 3;
				}
				else {
					$val_bit += ($offset & 0x03) << 3;
				}
				$offset &= ~0x03;
			}
			elsif ($size eq "H") {
				if ($endian eq "B") {
					$val_bit += 8;
					$val_bit -= ($offset & 0x01) << 3;
				}
				else {
					$val_bit += ($offset & 0x01) << 3;
				}
				$offset &= ~0x01;
			}
			last;
		}
		else {
			# ͂łȂs
			print STDERR "genoffset: cannot parse: $line\n";
			$error = 1;
		}
	}

	# `̏o
	$label =~ s/^_//;
	printf "#define %s\t%d\n",$label,$offset;
	printf "#define %s_bit\t%d\n",$label,$val_bit;
	printf "#define %s_mask\t0x%x\n",$label,(1 << $val_bit);
}

#
#  fBNeBu̓ǂݎ
#
sub ref_bit {
	my($size, $dir, $directive);

	return unless ($line =~ /BIT_REF_([0-9]+):/);
	$size = $1;

	while ($line = <INFILE>) {
		chomp $line;
		next if ($line =~ /^[ \t]*[#;].*$/);

		if ($line =~ /[ \t]*\.([a-zA-Z0-9]+)[ \t]*([^ \t]*)[ \t]*/
				&& defined($dir = $directives{$1})) {
			$directive = $1;
			if ($dir eq "") {
				# o^ׂfBNeBu
				$directives{$directive} = $size;
			}
			last;
		}
		else {
			# ͂łȂs
			print STDERR "genoffset: cannot parse: $line\n";
			$error = 1;
		}
	}
}

#
#  C[`
#
print "/* This file is generated by genoffset. */\n";
print "\n";

$error = 0;
open(INFILE, $infile) || die "Cannot open $infile";
while ($line = <INFILE>) {
	chomp $line;

	if ($line =~ /^[ \t]*OFFSET_DEF/) {
		do parse_offset();
	}
	elsif ($line =~ /^[ \t]*_?BIT_REF/) {
		do ref_bit();
	}
}
seek(INFILE, 0, SEEK_SET);
while ($line = <INFILE>) {
	chomp $line;

	if ($line =~ /^[ \t]*_?BIT_([BL])([BHW])/) {
		do parse_bit($1, $2);
	}
}
close(INFILE);
exit($error);
