#! /usr/bin/perl

# Make a pass to find all global values used and 1st label defined

# 0123456789012345678901234567890123456789012345678901234567890123456789
# L0214 BRN      L1164                    [ 12348 995394  12354
# L1164 LDX   4  G107                     [ 12354 -8386767 NEXTCELL

my %global;

while (@ARGV) {
	my ($g, $s) = (shift, shift);
	$global{$g} = $s;
}

open GLOBALS, 'zz-globals' or die "Can't find zz-globals\n";

my %NAME;

while (<GLOBALS>) {
	my ($g, $n, $a) = split;
	$NAME{$g} = $n;
}

close GLOBALS;


while (<STDIN>) {
	if (!$start && /^(L\d+) /) {
		$start = $1;
	}
	
	if (substr ($_, 15, 1) eq 'G') {
		push @globals, substr ($_, 16,3) + 0;
	}
}

@globals = sort { $a <=> $b } @globals, keys %global;

my $max = $globals [-1];

print "#PROGRAM       /MISSING\n";

print "#LOWER         COMMON/%BGLOBALVEC/\n";

my $here = 0;

for (@globals) {
	next if $_ == $here;
	++$here;
	if ($_ > $here) {
		print " " x 15, "G", $here, "(", $_ - $here, ")\n";
	}
	if ($global {$_}) {
		printf "G%-14s/%-24s[ %s\n", $_, $global{$_}, $NAME{$_};
	}
	else {
		printf "%-15sG%-24s[ %s\n", " ", $_, $NAME{$_};
	}
	$here = $_;
}
