[Firebug-cvs] fireboard/beta/tools/src/xlisten genpgsql.pm,NONE,1.1 fbpacket.c,1.1,1.2
Brought to you by:
doolin
From: David M. D. <do...@us...> - 2005-07-14 16:51:06
|
Update of /cvsroot/firebug/fireboard/beta/tools/src/xlisten In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16894 Modified Files: fbpacket.c Added Files: genpgsql.pm Log Message: Added perl module for mig to generate postgres tables from structs defining tos msg am types. Index: fbpacket.c =================================================================== RCS file: /cvsroot/firebug/fireboard/beta/tools/src/xlisten/fbpacket.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** fbpacket.c 28 Jun 2005 20:03:48 -0000 1.1 --- fbpacket.c 14 Jul 2005 16:50:57 -0000 1.2 *************** *** 81,85 **** init_gga_data(&gga_data); // This is a royal pain in the ass because the packets ! // on the mote side are built using inheritance with // embedded headers, but on the processing side the data // is assumed to be aggregated. What a fuckin bitch for --- 81,85 ---- init_gga_data(&gga_data); // This is a royal pain in the ass because the packets ! // on the mote side are built using unions to for // embedded headers, but on the processing side the data // is assumed to be aggregated. What a fuckin bitch for --- NEW FILE: genpgsql.pm --- # This file is part of the nesC compiler. # Copyright (C) 2002 Intel Corporation # # The attached "nesC" software is provided to you under the terms and # conditions of the GNU General Public License Version 2 as published by the # Free Software Foundation. # # nesC is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with nesC; see the file COPYING. If not, write to # the Free Software Foundation, 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA. ## TODO for autogenerating C code. ## ---------------------------------------------------------- ## Several things need to be done to make this viable for the ## kinds of messages we want to process. The first part is ## to get rid of the java type conversions, and preserve the ## structure as defined the tinyos header file. We can do this ## because nesc native types are identical to c native types. ## ## Parts of this code were written for the NSF_ITR funded ## firebug project. true; sub gen() { my ($classname, @spec) = @_; require migdecode; &decode(@spec); &usage("no classname name specified") if !defined($java_classname); $java_extends = "net.tinyos.message.Message" if !defined($java_extends); # See if name has a package specifier if ($java_classname =~ /(.*)\.([^.]*)$/) { $package = $1; $java_classname = $2; } $I = " "; $smallskip = "\n\n"; $medskip = "\n\n\n\n"; $bigskip = "\n\n\n\n\n\n"; print "/**\n"; print " * This class is automatically generated by mig. DO NOT EDIT THIS FILE.\n"; print " * This code provides postgres table to the '$java_classname'\n"; print " * message type.\n"; print " */\n\n"; &gen_sql_table(); print $medskip; } sub gen_sql_table() { print "CREATE TABLE $java_classname (\n"; # Get the size of the array. $numfields = $#fields; $i = 0; for (@fields) { ($field, $type, $bitlength, $offset, $amax, $abitsize, $aoffset) = @{$_}; ($ctype, $java_access, $arrayspec) = &cbasetype($type, $bitlength, 0); # This is clunky but will work. A better way to do it would be to store # the nested struct internally in the field array somehow. # Maybe a perl guru can figure that out. # What needs to be done is to find one of these, then # store it until all the members written out, # then print it. if ($field =~ /(.*)\.([^.]*)$/) { $struct = $1; $member = $2; #print STDERR $struct;#.", "$member."\n"; } ## Gets an array of format specifiers useful in *print* functions ## in libc. the @format array can be passed push(@format, &formatstring($type, $bitlength, 0)); $field =~ s/\./_/g; printf "$I$field $ctype"; if ($i < $numfields) { print ",\n"; } $i++; } print ");"; } sub gen_struct() { print "struct _$java_classname {\n"; ## todo move this to sub print_msg_struct(). for (@fields) { ($field, $type, $bitlength, $offset, $amax, $abitsize, $aoffset) = @{$_}; ($ctype, $java_access, $arrayspec) = &cbasetype($type, $bitlength, 0); # This is clunky but will work. A better way to do it would be to store # the nested struct internally in the field array somehow. # Maybe a perl guru can figure that out. # What needs to be done is to find one of these, then # store it until all the members written out, # then print it. if ($field =~ /(.*)\.([^.]*)$/) { $struct = $1; $member = $2; #print STDERR $struct;#.", "$member."\n"; } ## Gets an array of format specifiers useful in *print* functions ## in libc. the @format array can be passed push(@format, &formatstring($type, $bitlength, 0)); $field =~ s/\./_/g; printf "$I$field $ctype,\n"; } print "};"; } ## TODO Change all these back to the base types available in c, which are ## the same as in nesc. Probably need to add a "bitfield" handler to ## do it right. sub cbasetype() { my ($basetype, $bitlength, $arraydims) = @_; my $jtype, $acc; # Pick the java type whose range is closest to the corresponding C type if ($basetype eq "U") { $acc = "UIntElement"; if ($bitlength < 8) { $jtype = "smallint"; } elsif ($bitlength < 16) { $jtype = "smallint"; } elsif ($bitlength < 32) { $jtype = "smallint"; } else { $jtype = "integer"; } } elsif ($basetype eq "I") { $acc = "SIntElement"; if ($bitlength <= 8) { $jtype = "smallint"; } elsif ($bitlength <= 16) { $jtype = "smallint"; } elsif ($bitlength <= 32) { $jtype = "integer"; } else { $jtype = "long"; } } elsif ($basetype eq "F" || $basetype eq "D" || $basetype eq "LD") { $acc = "FloatElement"; $jtype = "float"; } if ($arraydims > 0) { # For array types $arrayspec = ""; for ($i = 0; $i < $arraydims; $i++) { $arrayspec = "[]" . $arrayspec; } } return ($jtype, $acc, $arrayspec); } ## TODO Get rid of all the superfluous code, ## or move the functionality into cbasetype. sub formatstring() { my ($basetype, $bitlength, $arraydims) = @_; my $jtype, $acc, $formatstring; # Pick the java type whose range is closest to the corresponding C type if ($basetype eq "U") { $acc = "UIntElement"; if ($bitlength < 8) { $jtype = "byte"; $formatstring = "c";} elsif ($bitlength < 16) { $jtype = "short"; $formatstring = "i";} elsif ($bitlength < 32) { $jtype = "int"; $formatstring = "i";} else { $jtype = "long"; $formatstring = "%i"} } elsif ($basetype eq "I") { $acc = "SIntElement"; if ($bitlength <= 8) { $jtype = "byte"; $formatstring = "c";} elsif ($bitlength <= 16) { $jtype = "short"; $formatstring = "i";} elsif ($bitlength <= 32) { $jtype = "int"; $formatstring = "i";} else { $jtype = "long"; } } elsif ($basetype eq "F" || $basetype eq "D" || $basetype eq "LD") { $acc = "FloatElement"; $jtype = "float"; $formatstring = "f"; } if ($arraydims > 0) { # For array types $arrayspec = ""; for ($i = 0; $i < $arraydims; $i++) { $arrayspec = "[]" . $arrayspec; } } #return ($jtype, $acc, $arrayspec, $formatstring); return ($formatstring); } sub printoffset() { my ($offset, $max, $bitsize, $aoffset, $inbits) = @_; print " int offset = $offset;\n"; for ($i = 1; $i <= @$max; $i++) { # check index bounds. 0-sized arrays don't get an upper-bound check # (they represent variable size arrays. Normally they should only # occur as the first-dimension of the last element of the structure) if ($$max[$i - 1] != 0) { print " if (index$i < 0 || index$i >= $$max[$i - 1]) throw new ArrayIndexOutOfBoundsException();\n"; } else { print " if (index$i < 0) throw new ArrayIndexOutOfBoundsException();\n"; } print " offset += $$aoffset[$i - 1] + index$i * $$bitsize[$i - 1];\n"; } if ($inbits) { print " return offset;\n"; } else { print " return (offset / 8);\n"; } } sub printarrayget() { my ($javafield, $javatype, $arrayspec, $bitlength, $amax, $abitsize) = @_; # Check whether array has known size for ($i = 0; $i < @$amax; $i++) { if ($$amax[$i] == 0) { print " throw new IllegalArgumentException(\"Cannot get field as array - unknown size\");\n"; return; } } print " $javatype$arrayspec tmp = new $javatype"; for ($i = 0; $i < @$amax; $i++) { print "[$$amax[$i]]"; } print ";\n"; $indent = " "; for ($i = 0; $i < @$amax; $i++) { print " $indent for (int index$i = 0; index$i < numElements_$javafield($i); index$i++) {\n"; $indent = $indent . " "; } $indent = $indent . " "; print " $indent tmp"; for ($i = 0; $i < @$amax; $i++) { print "[index$i]"; } print " = getElement_$javafield("; for ($i = 0; $i < @$amax; $i++) { print "index$i"; if ($i != @$amax-1) { print ","; } } print ");\n"; $indent = substr($indent, 0, length($indent)-2); for ($i = 0; $i < @$amax; $i++) { $indent = substr($indent, 0, length($indent)-2); print " $indent }\n"; } print " return tmp;\n"; } sub printarrayset() { my ($javafield, $javatype, $arrayspec, $bitlength, $amax, $abitsize) = @_; $indent = " "; $val = ""; for ($i = 0; $i < @$amax; $i++) { print " $indent for (int index$i = 0; index$i < value$val.length; index$i++) {\n"; $val = $val . "[index$i]"; $indent = $indent . " "; } $indent = $indent . " "; print " $indent setElement_$javafield("; for ($i = 0; $i < @$amax; $i++) { print "index$i"; if ($i != @$amax-1) { print ","; } } print ", value"; for ($i = 0; $i < @$amax; $i++) { print "[index$i]"; } print ");\n"; $indent = substr($indent, 0, length($indent)-2); for ($i = 0; $i < @$amax; $i++) { $indent = substr($indent, 0, length($indent)-2); print " $indent }\n"; } } |