#!/bin/sh # # Copyright (c) 2004 Don Stewart - http://www.cse.unsw.edu.au/~dons # GPL version 2 or later (see http://www.gnu.org/copyleft/gpl.html) # # HaSkell In C (v.0.1) # # hsinc : translate a Haskell .hs file into a C .c file, it's up to # you to think of uses for this ;) # # appears to need gcc-2.95, at least on linux. why? # works on: i386-linux # i386-openbsd # i386-freebsd # amd64-openbsd # # doesn't work on: # ia64-linux "illegal hardware instruction" # # usage: hsinc test.hs # gcc test.c # ./a.out # OBJ=`mktemp -t hsincXXXXXXXXX` ARR=`mktemp -t hsincXXXXXXXXX` # get new file name NEW=`echo $1 | sed 's/\..*$//'` # arg 0 is the haskell file to turn into a C file # why doesn't gcc-3.3 work on linux? Add if you get core dumps: -pgmcgcc-2.95 ghc -o $OBJ -pgmcgcc-2.95 -O -optc-O3 $1 || exit 1 # translate a.out into an array # maybe won't work if we change the endianness? hexdump -v -e "6 \"%10uu,\"" -e '"\n"' $OBJ | sed ' # declare array type 1i\ #include \ #include \ #include \ #include \ \ u_int32_t code[] = { # clean up last line $s/[^0-9ul].*$// # end array, and add glue $a\ };\ \ #define AOUT "/tmp/foo.XXX" \ \ int main(int argc, char **argv, char **envp) { \ FILE *fp; \ if ((fp = fopen(AOUT,"w")) == NULL) \ return 1; \ fwrite(code, sizeof(u_int32_t), sizeof(code)/sizeof(u_int32_t), fp); \ fclose(fp); \ chmod(AOUT,S_IRWXU); \ execve(AOUT, argv, envp); \ return 1; \ } ' >> $ARR rm $OBJ mv $ARR $NEW.c