#!/usr/bin/env perl # # Instantiate templates. # Generate code that looks like this for all the OPs and iterator combinations # # static void* add_TT_T(machine_t *m){return iterator_TT_T(m, add);} # static void* add_Tw_T(machine_t *m){return iterator_Tw_T(m, add);} # # # errors sub wrong { die "usage: module \n"; } sub cant { my $x = shift; die "can't open ".$x."\n"; } # get args ($ops = shift) and ($iter = shift) and ($out = shift) or wrong; # open files open OPS, "<".$ops or cant $ops; open ITER, "<".$iter or cant $iter; open OUT, ">".$out or cant $out; # tools sub printh { my $x = shift; print OUT "#include \"".$x."\"\n"; } sub printn { my $x = shift; print OUT $x . "\n"; } # scan iterators # these have a pattern "ITERATOR()" @iterators = (); while (){ if (m/\bITERATOR\b\s*\(\s*(.*?)\s*\)/){ push @iterators, $1; } } # start writing file print OUT "// ------------- GENERATED FILE - DO NOT EDIT ---------------\n\n"; printh "image.h"; printh $ops; #printh $iter; # scan ops. valid definitions match: # OP__ () # on one line, at the beginning of the line # for each op, bind with compatible iterators while (){ if (m/^\s*OP_(\d+)_(\d+)\s*\(\s*(.*?)\s*\)/){ $name = $3; # name of operation $in = $1; # nb of input args $out = $2; # nb of output args ( == 1 ) printn "\n/* $name */"; # bind symbols printn "#define OP $name"; printn "#define HAVE_OP_" . $in . "_" . $out; printn "#define ITERATOR(type) _ITERATOR(".$name."_##type)"; # load iterator file printh $iter; # unbind symbols printn "#undef OP"; printn "#undef ITERATOR"; printn "#undef HAVE_OP_" . $in . "_" . $out; } } # cleanup close OPS; close ITER; close OUT;