# generate instruction dispatch table for VM.
#   qseawk -f genbct.awk moo-prv.h
#
# TODO: rewrite this script in moo itself.
#

BEGIN { 
	on = 0; 
	for (i = 0; i < 255; i++) code_label[i] = "&&case_DEFAULT";
}

/^enum moo_bcode_t$/ { on = 1; }

/^enum moo_bcode_t {$/ { on = 2; }

/^{$/ { if (on == 1) on = 2; else on = 0; }

/^};$/ { on = 0; }

on == 2 && match($0, /^[[:space:]]*BCODE_([[:alnum:]_]+)[[:space:]]*=[[:space:]]*(0x[[:xdigit:]]+)/, 1, sm) >= 1 {
	count = length(sm) \ 2;
	if (count == 2)
	{
		name = substr ($0, sm[1,"start"], sm[1,"length"]);
		code = substr ($0, sm[2,"start"], sm[2,"length"]);
		code_label[int(code)] = "&&case_BCODE_" name;
	}
}

END {
	printf ("/* generated by genbct.awk */\n\n");

	##printf ("\tstatic void* inst_jump_labels[] =\n");
	##printf ("\t{\n");
	for (i = 0; i < 255; i++)
	{
		printf ("\t\t/* %3d */ %s,\n", i, code_label[i]);
	}
	printf ("\t\t/* %3d */ %s\n", i, code_label[i]);
	##printf ("\t};\n");
}
