#!/usr/bin/awk -f
# "POM-header-filter.awk"
#	$Id: POM-header-filter.awk.in,v 1.4 2007/06/13 17:45:47 fang Exp $
# filter for text dump of persistent_object_manager's object header

# NOTE: when used as a pipe redirected from stdout combined with stderr,
#	it may crash on particular print statements.  
#	This is probably some problem with an implementation of awk itself.  

# input:
# -v noindex={0,1}
#	default 0.  if 1, masks out the index when rewriting the header entry


{
if (match($0,"^Persistent Object Manager")) {
	print;		# echo title back out
	rewrite_header();
} else if (match($0,"^In module created from")) {
	gsub(": [^ ]*[ ]", ": ##FILE## ",$0);
	print $0;
# } else if (match($0, "^WARNING")) {
	# then filter out this line
} else {
	print;		# crashes???
}
}

function rewrite_header() {
	getline;
	print $0 "\tsize";	# echo table header back out
	while (getline && NF == 6) {
		# expect 6 fields
		rewrite_header_entry($1,$2,$3,$4,$5,$6);
	}
	# until empty line encountered
	print "";	# crashes???
}

function rewrite_header_entry(ind,addr,type,arg,hd,tl) {
	if (noindex) {
		printf("\t##ID##");
	} else {
		printf("\t" ind);
	}
	printf("\t##ADDR##\t" type "\t" arg "\t#HEAD#\t#TAIL#\t");
	if (match(type,"module")) {
		print "##SIZE##";
	} else {
# TODO: +8 alignment may increase to +16 for ILP64 platforms?
@OBJECT_ALIGN_CHECKING_TRUE@		print tl-hd;	# no correction needed
@OBJECT_ALIGN_CHECKING_FALSE@		if (ind) {	# correction term needed
@OBJECT_ALIGN_CHECKING_FALSE@			print tl-hd +8;	# except for 0
@OBJECT_ALIGN_CHECKING_FALSE@		} else { print tl-hd; }
	}
}

