#! /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
# 
#  嵭Ԥϡʲ (1)(4) ξ狼Free Software Foundation 
#  ˤäƸɽƤ GNU General Public License  Version 2 ˵
#  ҤƤ˸¤ꡤܥեȥܥեȥ
#  ѤΤޤࡥʲƱˤѡʣѡۡʰʲ
#  ѤȸƤ֡ˤ뤳Ȥ̵ǵ롥
#  (1) ܥեȥ򥽡ɤηѤˤϡ嵭
#      ɽѾ浪Ӳ̵ݾڵ꤬Τޤޤηǥ
#      ˴ޤޤƤ뤳ȡ
#  (2) ܥեȥ򡤥饤֥ʤɡ¾Υեȥȯ˻
#      ѤǤǺۤˤϡۤȼɥȡ
#      ԥޥ˥奢ʤɡˤˡ嵭ɽѾ浪Ӳ
#      ̵ݾڵǺܤ뤳ȡ
#  (3) ܥեȥ򡤵Ȥ߹ʤɡ¾Υեȥȯ˻
#      ѤǤʤǺۤˤϡΤ줫ξ
#      ȡ
#    (a) ۤȼɥȡѼԥޥ˥奢ʤɡˤˡ嵭
#        ɽѾ浪Ӳ̵ݾڵǺܤ뤳ȡ
#    (b) ۤη֤̤ˡˤäơTOPPERSץȤ
#        𤹤뤳ȡ
#  (4) ܥեȥѤˤľŪޤϴŪ뤤ʤ»
#      ⡤嵭ԤTOPPERSץȤդ뤳ȡ
# 
#  ܥեȥϡ̵ݾڤ󶡤ƤΤǤ롥嵭Ԥ
#  TOPPERSץȤϡܥեȥ˴ؤơŬѲǽ
#  ޤơʤݾڤԤʤޤܥեȥѤˤľ
#  ŪޤϴŪʤ»˴ؤƤ⡤Ǥʤ
# 
#  @(#) $Id: genoffset,v 1.18 2005/11/12 15:00:36 hiro Exp $
# 

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

#
#  ӥåȥ
#
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);
}

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

#
#  Ǥǥ쥯ƥ֤Υơ֥
#
%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,
);

#
#  ӥåȰ֤ν
#
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;

			# 16ʿ8ʿοͤؤѴ
			if ($val =~ /^0x(.+)$/) {
				$val = hex($1);
			}
			elsif ($val =~ /^0(.+)$/) {
				$val = oct($1);
			}

			if ($dir eq "") {
				# Τʤǥ쥯ƥ
				print STDERR "genoffset: unknown directive: ",
						"$line\n";
				$error = 1;
			}
			elsif ($dir == 0) {
				# ɤФ٤ǥ쥯ƥ
				next;
			}
			elsif ($dir == -1) {
				# .zero ǥ쥯ƥ֤ν
				$offset += $val;
				next;
			}
			elsif ($val == 0) {
				# ͤ 0 Υեɤν
				$offset += $dir;
				next;
			}

			# ӥåȰ֤õ
			$val_bit = do search_bit($val);

			# Хñ̤˴
			if ($endian eq "B") {
				$offset += $dir - 1;
				$offset -= $val_bit >> 3;
			}
			else {
				$offset += $val_bit >> 3;
			}
			$val_bit &= 0x07;

			# ñ̤˴
			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 {
			# ϤǤʤ
			print STDERR "genoffset: cannot parse: $line\n";
			$error = 1;
		}
	}

	# ν
	$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);
}

#
#  ǥ쥯ƥ֤ɤ߼
#
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 "") {
				# Ͽ٤ǥ쥯ƥ
				$directives{$directive} = $size;
			}
			last;
		}
		else {
			# ϤǤʤ
			print STDERR "genoffset: cannot parse: $line\n";
			$error = 1;
		}
	}
}

#
#  ᥤ롼
#
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);
