| 1 | package Call::Haskell::FFIGenerator; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use v5.16; | ||
| 5 | use Data::Dumper; | ||
| 6 | use Cwd; | ||
| 7 | use Digest::MD5; | ||
| 8 | |||
| 9 | use version; our $VERSION = version->declare('v0.2.0'); | ||
| 10 | |||
| 11 | use Exporter 'import'; | ||
| 12 | |||
| 13 | @Call::Haskell::FFIGenerator::EXPORT = qw( | ||
| 14 | create_hs_ffi_generator | ||
| 15 | ); | ||
| 16 | |||
| 17 | # This module generates all the glue code needed for calling Haskell from Perl. | ||
| 18 | # What we should do is test if the original module has changed via a checksum | ||
| 19 | our $VV=0; | ||
| 20 | sub create_hs_ffi_generator { | ||
| 21 | ( my $module_name, my $function_names, my $inc, my $CLEAN, my $VV, my $perl_types ) = @_; | ||
| 22 | $Call::Haskell::FFIGenerator::VV=$VV; | ||
| 23 | |||
| 24 | my $wd = cwd(); | ||
| 25 | my $Call_Haskell_path=$INC{"Call/Haskell/FFIGenerator.pm"}; | ||
| 26 | my $hs_FFIGenerator_dir=$Call_Haskell_path; | ||
| 27 | |||
| 28 | $hs_FFIGenerator_dir=~s/Call.Haskell.FFIGenerator.pm$//; | ||
| 29 | if ($hs_FFIGenerator_dir=~/^\.\./) { | ||
| 30 | $hs_FFIGenerator_dir='../'.$hs_FFIGenerator_dir; | ||
| 31 | } | ||
| 32 | if ( not -e '_Call_Haskell' ) { | ||
| 33 | mkdir '_Call_Haskell'; | ||
| 34 | } | ||
| 35 | chdir '_Call_Haskell'; | ||
| 36 | mkdir 'FFIGenerator'; | ||
| 37 | system("cp $hs_FFIGenerator_dir/FFIGenerator/*.hs FFIGenerator"); | ||
| 38 | my $generate = 0; | ||
| 39 | if ( $CLEAN == 1 ) { | ||
| 40 | # say "CLEANING generated sources" if $VV; | ||
| 41 | # unlink $module_name . '_ffi_wrapper_gen.hs'; | ||
| 42 | # unlink $module_name . "FFIWrapper.hs"; | ||
| 43 | # unlink $module_name . "CWrapper.c"; | ||
| 44 | # unlink $module_name . "CWrapper.h"; | ||
| 45 | $generate = 1; | ||
| 46 | } else { | ||
| 47 | # say "Computing MD5 for $wd/$inc/$module_name.hs"; | ||
| 48 | my $filename = "$wd/$inc/$module_name.hs"; | ||
| 49 | open( my $fh, '<', $filename ) or die "Can't open '$filename': $!"; | ||
| 50 | binmode($fh); | ||
| 51 | my $md5 = Digest::MD5->new; | ||
| 52 | while (<$fh>) { | ||
| 53 | $md5->add($_); | ||
| 54 | } | ||
| 55 | close($fh); | ||
| 56 | my $checksum = $md5->hexdigest; | ||
| 57 | if ( -e "$module_name.md5" ) { | ||
| 58 | open( my $md5fh, '<', "$module_name.md5" ); | ||
| 59 | my $ref_checksum = <$md5fh>; | ||
| 60 | close $md5fh; | ||
| 61 | say "MD5:", $checksum eq $ref_checksum if $VV; | ||
| 62 | if ( $checksum ne $ref_checksum ) { | ||
| 63 | unlink "$module_name.md5"; | ||
| 64 | $generate = 1; | ||
| 65 | } else { | ||
| 66 | $generate = 0; | ||
| 67 | } | ||
| 68 | } else { | ||
| 69 | say "no MD5 checksum for $module_name.md5" if $VV; | ||
| 70 | |||
| 71 | $generate = 1; | ||
| 72 | } | ||
| 73 | if ( not -e "$module_name.md5" ) { | ||
| 74 | $generate = 1; | ||
| 75 | |||
| 76 | open( my $md5fh, '>', "$module_name.md5" ); | ||
| 77 | print $md5fh $checksum; | ||
| 78 | close $md5fh; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | if ( | ||
| 82 | not( -e $module_name . '_ffi_wrapper_gen.hs' | ||
| 83 | and -e $module_name . "FFIWrapper.hs" | ||
| 84 | and -e $module_name . "CWrapper.c" | ||
| 85 | and -e $module_name . "CWrapper.h" | ||
| 86 | and -e $module_name . "_Inline_C.c" ) | ||
| 87 | ) | ||
| 88 | { | ||
| 89 | |||
| 90 | $generate = 1; | ||
| 91 | } | ||
| 92 | my $c_code=''; | ||
| 93 | if ( $generate == 1 ) { | ||
| 94 | |||
| 95 | my $hs_module_dir = $inc; | ||
| 96 | |||
| 97 | open my $GEN, '>', "${module_name}_ffi_wrapper_gen.hs"; | ||
| 98 | say $GEN "-- Code generated by $0 on " . scalar localtime; | ||
| 99 | while ( my $line = <DATA> ) { | ||
| 100 | if ( $line =~ /_MODULE_NAME_/ ) { | ||
| 101 | $line =~ s/_MODULE_NAME_/$module_name/; | ||
| 102 | } | ||
| 103 | if ( $line =~ /_FUNC_/ ) { | ||
| 104 | my $nlines = ''; | ||
| 105 | for my $function_name ( @{$function_names} ) { | ||
| 106 | my $nline = $line; | ||
| 107 | $nline =~ s/_FUNC_/$function_name/g; | ||
| 108 | $nlines .= $nline; | ||
| 109 | } | ||
| 110 | $nlines =~ s/\,$//; | ||
| 111 | $line = $nlines; | ||
| 112 | } | ||
| 113 | print $GEN $line; | ||
| 114 | } | ||
| 115 | close DATA; | ||
| 116 | close $GEN; | ||
| 117 | my $Call_Haskell_path=$INC{"Call/Haskell/FFIGenerator.pm"}; | ||
| 118 | my $hs_FFIGenerator_dir=$Call_Haskell_path; | ||
| 119 | |||
| 120 | $hs_FFIGenerator_dir=~s/Call.Haskell.FFIGenerator.pm$//; | ||
| 121 | |||
| 122 | #if ($hs_FFIGenerator_dir=~/^\./) {die "\nThe path to the Call::Haskell module _must_ be absolute, please redefine your PERL5LIB\n\n"; } | ||
| 123 | $hs_FFIGenerator_dir='.'; | ||
| 124 | say "FFIGenerator path:",$hs_FFIGenerator_dir if $VV; | ||
| 125 | my @hs_f_types = | ||
| 126 | `runhaskell -i$wd/$hs_module_dir -i$hs_FFIGenerator_dir ${module_name}_ffi_wrapper_gen.hs`; | ||
| 127 | $c_code = create_C_wrapper( \@hs_f_types, $module_name ); | ||
| 128 | |||
| 129 | open my $INL_C_CODE, '>', $module_name . '_Inline_C.c'; | ||
| 130 | print $INL_C_CODE $c_code; | ||
| 131 | close $INL_C_CODE; | ||
| 132 | my @decomp_hs_types = map { decomposeHSType($_) } @hs_f_types; | ||
| 133 | # say Dumper(@hs_f_types); | ||
| 134 | # die Dumper(@decomp_hs_types); | ||
| 135 | mkdir "$wd/_Call_Haskell/CallHaskellWrappers"; | ||
| 136 | for my $ft (@decomp_hs_types) { | ||
| 137 | (my $fn, my $arg_types, my $ret_type)=@{$ft}; | ||
| 138 | if(isSimpleSig(@{$arg_types}, $ret_type)==0) { | ||
| 139 | if (not -e "$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm") { | ||
| 140 | # create the sub | ||
| 141 | my $code = <<"ENDWC"; | ||
| 142 | package CallHaskellWrappers::$fn; | ||
| 143 | use Exporter qw( import ); | ||
| 144 | \@CallHaskellWrappers::${fn}::EXPORT = qw( $fn ); | ||
| 145 | |||
| 146 | ENDWC | ||
| 147 | my $hs_argtup_type = (scalar @{$arg_types} > 1) ? '('.join(',',@{$arg_types}).')' : $arg_types->[0]; | ||
| 148 | |||
| 149 | $Data::Dumper::Indent=0; | ||
| 150 | $Data::Dumper::Terse=1; | ||
| 151 | |||
| 152 | my $hs_type_str=Dumper($hs_argtup_type); | ||
| 153 | if ($perl_types eq '') { | ||
| 154 | $code .= <<"ENDWH"; | ||
| 155 | use Call::Haskell::ReadShow qw( showH readH ); | ||
| 156 | require Call::Haskell; | ||
| 157 | sub $fn { | ||
| 158 | my \$hs_type=$hs_type_str; | ||
| 159 | my \$in_str = Call::Haskell::ReadShow::showH(\@_,\$hs_type); | ||
| 160 | my \$out_str=Call::Haskell::${fn}_ser(\$in_str); | ||
| 161 | my \$res = Call::Haskell::ReadShow::readH(\$out_str); | ||
| 162 | return \$res; | ||
| 163 | } | ||
| 164 | |||
| 165 | ENDWH | ||
| 166 | |||
| 167 | } else { | ||
| 168 | my $use_perl_types = "$perl_types" eq '1' ? '' : "use $perl_types;"; | ||
| 169 | |||
| 170 | $code .= <<"ENDWTP"; | ||
| 171 | use Call::Haskell::ReadShow qw( showH readH ); | ||
| 172 | use Types; | ||
| 173 | $use_perl_types | ||
| 174 | require Call::Haskell; | ||
| 175 | sub $fn { | ||
| 176 | my \$hs_type=$hs_type_str; | ||
| 177 | # my \$in_str = ''; | ||
| 178 | my \@in_arg_strs=(); | ||
| 179 | for my \$arg (\@_) { | ||
| 180 | if (ref(\$arg) eq 'Types') { | ||
| 181 | push \@in_arg_strs, Types::show(\$arg); | ||
| 182 | } else { | ||
| 183 | push \@in_arg_strs, Call::Haskell::ReadShow::showH(\$arg,\$hs_type); | ||
| 184 | } | ||
| 185 | } | ||
| 186 | my \$in_str = (\@_>1) ? '('.join(', ',\@in_arg_strs).')' : \$in_arg_strs[0]; | ||
| 187 | my \$out_str=Call::Haskell::${fn}_ser(\$in_str); | ||
| 188 | my \$res = eval(\$out_str); | ||
| 189 | return \$res; | ||
| 190 | } | ||
| 191 | |||
| 192 | ENDWTP | ||
| 193 | |||
| 194 | $code .= "1;\n"; | ||
| 195 | } | ||
| 196 | open my $SUB,'>',"$wd/_Call_Haskell/CallHaskellWrappers/$fn.pm"; | ||
| 197 | print $SUB $code; | ||
| 198 | close $SUB; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | say "NO GENERATION NECESSARY!" if $VV; | ||
| 205 | open my $INL_C_CODE, '<', $module_name . '_Inline_C.c'; | ||
| 206 | while ( my $line = <$INL_C_CODE> ) { | ||
| 207 | $c_code .= $line; | ||
| 208 | } | ||
| 209 | close $INL_C_CODE; | ||
| 210 | } | ||
| 211 | |||
| 212 | return ( $c_code, $generate ); | ||
| 213 | } | ||
| 214 | |||
| 215 | my %SimpleTypes = | ||
| 216 | map { ( $_ => 1, "IO $_" => 1 ) } | ||
| 217 | qw( Int Integer Float Double String [Char] ); | ||
| 218 | |||
| 219 | sub HstoP_types { | ||
| 220 | ( my $hs_t, my $is_retval ) = @_; | ||
| 221 | my $p_t = 'NONE'; | ||
| 222 | if ( $hs_t eq 'Int' ) { | ||
| 223 | $p_t = 'long'; | ||
| 224 | } | ||
| 225 | elsif ( $hs_t eq 'Double' ) { | ||
| 226 | $p_t = 'double'; | ||
| 227 | |||
| 228 | } | ||
| 229 | elsif ( $hs_t eq '[Char]' or $hs_t eq 'String' ) { | ||
| 230 | if ($is_retval) { | ||
| 231 | $p_t = 'SV*' | ||
| 232 | ; # SvPV(sv_name, PL_na) char* n_c_str = newSVpvf("STR: <%s %s> OK\n", c_str,hs_str); | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | $p_t = 'char*'; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | else { | ||
| 239 | |||
| 240 | # This type needs to be serialised, so it's a string | ||
| 241 | # This is not correct on a per-arg basis, but we should not come here | ||
| 242 | if ($is_retval) { | ||
| 243 | $p_t = 'SV*' | ||
| 244 | ; # SvPV(sv_name, PL_na) char* n_c_str = newSVpvf("STR: <%s %s> OK\n", c_str,hs_str); | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $p_t = 'char*'; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | return $p_t; | ||
| 251 | } | ||
| 252 | # So with the new type datastructure this will work if the type is a string and it exists. | ||
| 253 | # So that's fine in a very Perl-ish way | ||
| 254 | sub isSimpleSig { | ||
| 255 | ( my @hs_f_types ) = @_; | ||
| 256 | my $test = 1; | ||
| 257 | map { $test = $test && ( exists $SimpleTypes{$_} ? 1 : 0 ) } | ||
| 258 | @hs_f_types ;#( @{$argtypes}, $ret_type ); | ||
| 259 | # say "isSimpleSig <$test>"; | ||
| 260 | return $test; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub decomposeHSTypeOLD { | ||
| 264 | ( my $hs_f_type ) = @_; | ||
| 265 | # say "decomposeHSType:",$hs_f_type; | ||
| 266 | chomp $hs_f_type; | ||
| 267 | ( my $f, my $args ) = split( /\s+::\s+/, $hs_f_type ); | ||
| 268 | my @argtypes = split( /\s+->\s+/, $args ); | ||
| 269 | my $ret_type = pop @argtypes; | ||
| 270 | my $res= [ $f, \@argtypes, $ret_type ]; | ||
| 271 | # say Dumper($res); | ||
| 272 | return $res; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub decomposeHSType { | ||
| 276 | ( my $hs_f_type ) = @_; | ||
| 277 | # say "decomposeHSType:",Dumper($hs_f_type); | ||
| 278 | chomp $hs_f_type; | ||
| 279 | ( my $f, my $args ) = split( /\s+::\s+/, $hs_f_type ); | ||
| 280 | # say "decomposeHSType:",$args; | ||
| 281 | my $argtypes = eval($args); | ||
| 282 | my $ret_type = pop @{$argtypes}; | ||
| 283 | my $res= [ $f, $argtypes, $ret_type ]; | ||
| 284 | # say Dumper($res);die; | ||
| 285 | return $res; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub create_C_wrapper { | ||
| 289 | ( my $hs_f_types, my $module_name ) = @_; | ||
| 290 | my $inc = '#include "' . $module_name . 'CWrapper.h"'; | ||
| 291 | my $date = scalar localtime; | ||
| 292 | my $c_code = "// Code generated by $0 on $date | ||
| 293 | $inc | ||
| 294 | int hs_begin(int n) { | ||
| 295 | hs_${module_name}_init(); | ||
| 296 | return n; | ||
| 297 | } | ||
| 298 | |||
| 299 | int hs_end(int n) { | ||
| 300 | hs_${module_name}_end(); | ||
| 301 | return n; | ||
| 302 | } | ||
| 303 | "; | ||
| 304 | my $ws = ' '; | ||
| 305 | for my $hs_f_type ( @{$hs_f_types} ) { | ||
| 306 | ( my $f, my $argtypes, my $res_t ) = @{&decomposeHSType($hs_f_type)}; | ||
| 307 | |||
| 308 | # say "TYPE: $hs_f_type"; | ||
| 309 | # say "DECTYPE:", Dumper($argtypes); | ||
| 310 | # say $f; | ||
| 311 | my @fdef_lines = do { | ||
| 312 | |||
| 313 | if ( isSimpleSig(@{$argtypes}, $res_t) == 1 ) { | ||
| 314 | say "SIMPLE!" if $VV; | ||
| 315 | my $i = 0; | ||
| 316 | ( | ||
| 317 | '', | ||
| 318 | HstoP_types( $res_t, 1 ) . " $f(" | ||
| 319 | . join( ',', map { HstoP_types( $_, 0 ) . ' x' . ++$i } @{$argtypes} ) | ||
| 320 | . ') {', | ||
| 321 | |||
| 322 | # $ws.'printf("BEFORE FFI CALL\n");', | ||
| 323 | $ws | ||
| 324 | . HstoP_types( $res_t, 0 ) | ||
| 325 | . " res = ${f}_ffi_c(" | ||
| 326 | . join( ', ', map { "x$_" } ( 1 .. $i ) ) . ');', | ||
| 327 | |||
| 328 | # $ws.'printf("AFTER FFI CALL\n");', | ||
| 329 | $ws | ||
| 330 | . HstoP_types( $res_t, 0 ) | ||
| 331 | . ' nres = ' | ||
| 332 | . ( | ||
| 333 | ( $res_t eq '[Char]' or $res_t eq 'String' ) ? 'newSVpv(res,0);' : 'res;' | ||
| 334 | ), | ||
| 335 | |||
| 336 | $ws . 'return nres;', | ||
| 337 | '}', | ||
| 338 | '' | ||
| 339 | ); | ||
| 340 | } | ||
| 341 | else { | ||
| 342 | say "SERIALISE!" if $VV; | ||
| 343 | ( | ||
| 344 | '', | ||
| 345 | "SV* ${f}_ser(char* sstr) {", | ||
| 346 | $ws . "char* res = ${f}_ffi_c(sstr);", | ||
| 347 | $ws . 'SV* nres = newSVpv(res,0);', | ||
| 348 | $ws . 'return nres;', | ||
| 349 | '}', '' | ||
| 350 | ); | ||
| 351 | } | ||
| 352 | }; | ||
| 353 | |||
| 354 | my $fdef = join( "\n", @fdef_lines ); | ||
| 355 | |||
| 356 | # print $fdef; | ||
| 357 | $c_code .= $fdef; | ||
| 358 | } | ||
| 359 | return $c_code; | ||
| 360 | } # END of create_C_wrapper() | ||
| 361 | |||
| 362 | sub AUTOLOAD { | ||
| 363 | our $AUTOLOAD; | ||
| 364 | my $t=$AUTOLOAD; | ||
| 365 | $t=~s/^.+:://; | ||
| 366 | $t eq 'True' && do{$t=1}; | ||
| 367 | $t eq 'False' && do{$t=1}; | ||
| 368 | $t eq 'Nothing' && do{$t=undef}; | ||
| 369 | if (not @_) { | ||
| 370 | return $t; | ||
| 371 | } else { | ||
| 372 | return {TypeName=>$t,TypeArgs=>[@_] }; | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | 1; | ||
| 377 | |||
| 378 | # _FUNC_ gets populated for every function with magic commas | ||
| 379 | |||
| 380 | __DATA__ |