Skip to content

Commit

Permalink
convert EMCC code to generate both libfreecell-solver and libfind-deal
Browse files Browse the repository at this point in the history
optimization of a bare libfind-deal for https://fc-solve.shlomifish.org/js-fc-solve/find-deal/
  • Loading branch information
shlomif committed Jun 17, 2024
1 parent 8956347 commit b1c8286
Show file tree
Hide file tree
Showing 17 changed files with 243 additions and 108 deletions.
71 changes: 43 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,39 +71,54 @@
/fc-solve/site/wml/lib/docbook/5/rtf/fcs_arch_doc.rtf
/fc-solve/site/wml/lib/docbook/5/xml/fcs-book.xml
/fc-solve/site/wml/lib/docbook/5/xml/fcs_arch_doc.xml
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/*.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/bhsolver-dest/can_move.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/bhsolver-dest/fcs_hash.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/bhsolver-dest/generated/lib.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/bhsolver-dest/meta_alloc.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/bhsolver-dest/rank_reach_prune.o
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/board_gen/
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/finish.stamp
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/funcs.lflags.txt
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/inc.cflags.txt
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/libfreecell-solver-asm.js
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/libfreecell-solver-asm.js.mem
/fc-solve/site/wml/lib/fc-solve-for-javascript-asmjs/patsolve/patsolve/*.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/*.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/CMAKE-BUILD-dir/
/fc-solve/site/wml/lib/fc-solve-for-javascript/__DESTDIR/
/fc-solve/site/wml/lib/fc-solve-for-javascript/bhsolver-dest/can_move.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/bhsolver-dest/fcs_hash.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/bhsolver-dest/generated/lib.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/bhsolver-dest/meta_alloc.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/bhsolver-dest/rank_reach_prune.o
/fc-solve/site/wml/lib/fc-solve-for-javascript/board_gen/
/fc-solve/site/wml/lib/fc-solve-for-javascript/finish.stamp
/fc-solve/site/wml/lib/fc-solve-for-javascript/funcs.lflags.txt
/fc-solve/site/wml/lib/fc-solve-for-javascript/inc.cflags.txt
/fc-solve/site/wml/lib/fc-solve-for-javascript/libfreecell-solver.js
/fc-solve/site/wml/lib/fc-solve-for-javascript/libfreecell-solver.wasm
/fc-solve/site/wml/lib/fc-solve-for-javascript/patsolve/patsolve/*.o
/fc-solve/site/wml/lib/fc_listbox.htmlish
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/*.o
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/board_gen/
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/finish.stamp
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/funcs.lflags.txt
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/inc.cflags.txt
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/libfind-deal-asm.js
/fc-solve/site/wml/lib/find-deal-for-javascript-asmjs/libfind-deal-asm.js.mem
/fc-solve/site/wml/lib/find-deal-for-javascript/*.o
/fc-solve/site/wml/lib/find-deal-for-javascript/CMAKE-BUILD-dir/
/fc-solve/site/wml/lib/find-deal-for-javascript/board_gen/
/fc-solve/site/wml/lib/find-deal-for-javascript/finish.stamp
/fc-solve/site/wml/lib/find-deal-for-javascript/funcs.lflags.txt
/fc-solve/site/wml/lib/find-deal-for-javascript/inc.cflags.txt
/fc-solve/site/wml/lib/find-deal-for-javascript/libfind-deal.js
/fc-solve/site/wml/lib/find-deal-for-javascript/libfind-deal.wasm
/fc-solve/site/wml/lib/for-node/js/*.js
/fc-solve/site/wml/lib/for-node/js/libfreecell-solver-asm.js.mem
/fc-solve/site/wml/lib/for-node/js/libfreecell-solver.wasm
/fc-solve/site/wml/lib/for-node/js/tests/*.js
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/*.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/bhsolver-dest/can_move.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/bhsolver-dest/fcs_hash.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/bhsolver-dest/generated/lib.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/bhsolver-dest/meta_alloc.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/bhsolver-dest/rank_reach_prune.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/board_gen/
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/finish.stamp
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/funcs.lflags.txt
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/inc.cflags.txt
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/libfreecell-solver-asm.js
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/libfreecell-solver-asm.js.mem
/fc-solve/site/wml/lib/freecell-solver-for-javascript-asmjs/patsolve/patsolve/*.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/*.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/CMAKE-BUILD-dir/
/fc-solve/site/wml/lib/freecell-solver-for-javascript/__DESTDIR/
/fc-solve/site/wml/lib/freecell-solver-for-javascript/bhsolver-dest/can_move.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/bhsolver-dest/fcs_hash.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/bhsolver-dest/generated/lib.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/bhsolver-dest/meta_alloc.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/bhsolver-dest/rank_reach_prune.o
/fc-solve/site/wml/lib/freecell-solver-for-javascript/board_gen/
/fc-solve/site/wml/lib/freecell-solver-for-javascript/finish.stamp
/fc-solve/site/wml/lib/freecell-solver-for-javascript/funcs.lflags.txt
/fc-solve/site/wml/lib/freecell-solver-for-javascript/inc.cflags.txt
/fc-solve/site/wml/lib/freecell-solver-for-javascript/libfreecell-solver.js
/fc-solve/site/wml/lib/freecell-solver-for-javascript/libfreecell-solver.wasm
/fc-solve/site/wml/lib/freecell-solver-for-javascript/patsolve/patsolve/*.o
/fc-solve/site/wml/lib/front-page-news.htmlish
/fc-solve/site/wml/lib/games.htmlish
/fc-solve/site/wml/lib/h_sections.tt2
Expand Down
2 changes: 1 addition & 1 deletion fc-solve/scripts/Makefile.to-javascript.mak
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ RESULT_JS_LIB__BASENAME = find-deal
LIB_C_FILES = \
board_gen/find_deal.c \
gen_ms_boards__hll_iface.c \
hacks_for_hlls.c \

CMAKE_C_FILES =
BHS_LIB_C_FILES =
Expand Down Expand Up @@ -135,6 +134,7 @@ NEEDED_FUNCTIONS = \
malloc \

endif

RESULT_JS_LIB = lib$(RESULT_JS_LIB__BASENAME)$(LIBSUF).js

C_FILES = $(LIB_C_FILES)
Expand Down
4 changes: 3 additions & 1 deletion fc-solve/site/wml/Tests/style-trailing-space.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ my $finder = Test::TrailingSpace->new(
|
lib/docbook/5
|
lib/fc-solve-for-javascript
lib/freecell-solver-for-javascript
|
lib/find-deal-for-javascript
|
lib/transcrypt_module/src
|
Expand Down
101 changes: 101 additions & 0 deletions fc-solve/site/wml/bin/gen_emcc_libs_makefile_fragments.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#! /usr/bin/env perl

use strict;
use warnings;
use 5.014;
use autodie;

use Carp qw/ confess /;
use Path::Tiny qw/ cwd path tempdir tempfile /;

my $TEMPLATE = <<'EOF';
LIBFREECELL_SOLVER_JS_MEM_BASE = libfreecell-solver.wasm
LIBFREECELL_SOLVER_JS_MEM_BASE__ASMJS = libfreecell-solver-asm.js.mem
LIBFREECELL_SOLVER_JS_DIR := lib/freecell-solver-for-javascript
LIBFREECELL_SOLVER_ASMJS_JS_DIR = $(LIBFREECELL_SOLVER_JS_DIR)-asmjs
LIBFREECELL_SOLVER_WASM_STAMP := $(LIBFREECELL_SOLVER_JS_DIR)/finish.stamp
LIBFREECELL_SOLVER_ASMJS_STAMP := $(LIBFREECELL_SOLVER_ASMJS_JS_DIR)/finish.stamp
LIBFREECELL_SOLVER_JS = $(LIBFREECELL_SOLVER_JS_DIR)/libfreecell-solver.js
LIBFREECELL_SOLVER_ASMJS_JS = $(LIBFREECELL_SOLVER_ASMJS_JS_DIR)/libfreecell-solver-asm.js
DEST_LIBFREECELL_SOLVER_JS = $(DEST_JS_DIR)/libfreecell-solver.min.js
DEST_LIBFREECELL_SOLVER_ASMJS_JS = $(DEST_JS_DIR)/libfreecell-solver-asm.js
DEST_LIBFREECELL_SOLVER_JS_NON_MIN = $(DEST_JS_DIR)/libfreecell-solver.js
DEST_LIBFREECELL_SOLVER_JS_MEM = $(patsubst %,%/$(LIBFREECELL_SOLVER_JS_MEM_BASE),$(DEST_mem_dirs))
DEST_LIBFREECELL_SOLVER_JS_MEM__ASMJS = $(patsubst %,%/$(LIBFREECELL_SOLVER_JS_MEM_BASE__ASMJS),$(DEST_mem_dirs))
LIBFREECELL_SOLVER_JS_DIR__CMAKE_DIR = $(LIBFREECELL_SOLVER_JS_DIR)/CMAKE-BUILD-dir
LIBFREECELL_SOLVER_JS_DIR__CMAKE_CACHE = $(LIBFREECELL_SOLVER_JS_DIR__CMAKE_DIR)/CMakeCache.txt
LIBFREECELL_SOLVER_JS_DIR__DESTDIR = $(PWD)/$(LIBFREECELL_SOLVER_JS_DIR)/__DESTDIR
LIBFREECELL_SOLVER_JS_DIR__DESTDIR_DATA = $(LIBFREECELL_SOLVER_JS_DIR__DESTDIR)/fc-solve/share/freecell-solver/presetrc
LIBFREECELL_SOLVER_JS_DIR__DESTDIR__ASMJS = $(PWD)/$(LIBFREECELL_SOLVER_ASMJS_JS_DIR)/__DESTDIR
LIBFREECELL_SOLVER_FC_SOLVE_CMAKE_DIR := $(PWD)/$(LIBFREECELL_SOLVER_JS_DIR__CMAKE_DIR)
ifeq ($(SKIP_EMCC),1)
LIBFREECELL_SOLVER_JS__NODE__TARGETS =
LIBFREECELL_SOLVER_JS__TARGETS =
else
LIBFREECELL_SOLVER_JS__NODE__TARGETS = lib/for-node/js/libfreecell-solver.min.js lib/for-node/js/libfreecell-solver-asm.js
LIBFREECELL_SOLVER_JS__TARGETS = $(DEST_LIBFREECELL_SOLVER_JS) $(DEST_LIBFREECELL_SOLVER_JS_NON_MIN) $(DEST_LIBFREECELL_SOLVER_JS_MEM) $(DEST_LIBFREECELL_SOLVER_ASMJS_JS) $(DEST_LIBFREECELL_SOLVER_JS_MEM__ASMJS)
endif
real_all: $(LIBFREECELL_SOLVER_JS__TARGETS)
$(DEST_LIBFREECELL_SOLVER_JS): $(LIBFREECELL_SOLVER_WASM_STAMP)
cp -f $(LIBFREECELL_SOLVER_JS) $@
$(DEST_LIBFREECELL_SOLVER_ASMJS_JS): $(LIBFREECELL_SOLVER_ASMJS_STAMP)
cp -f $(LIBFREECELL_SOLVER_ASMJS_JS) $@
$(LIBFREECELL_SOLVER_JS): $(LIBFREECELL_SOLVER_WASM_STAMP)
LIBFREECELL_SOLVER_JS__gmake = gmake -j4 -f $(FC_SOLVE_SOURCE_DIR)/../scripts/Makefile.to-javascript.mak RINUTILS_DIR="$(PWD)/lib/repos/rinutils-include" SRC_DIR=$(FC_SOLVE_SOURCE_DIR) CMAKE_DIR=$(LIBFREECELL_SOLVER_FC_SOLVE_CMAKE_DIR) FIND_DEAL_ONLY=$(LIBFREECELL_SOLVER_build_only_flag)
$(LIBFREECELL_SOLVER_WASM_STAMP): $(LIBFREECELL_SOLVER_JS_DIR__DESTDIR_DATA)
( cd $(LIBFREECELL_SOLVER_JS_DIR) && $(LIBFREECELL_SOLVER_JS__gmake) ASMJS=0 && $(STRIP_TRAIL_SPACE) *.js )
touch $(LIBFREECELL_SOLVER_WASM_STAMP)
$(LIBFREECELL_SOLVER_ASMJS_JS): $(LIBFREECELL_SOLVER_ASMJS_STAMP)
$(LIBFREECELL_SOLVER_ASMJS_JS_DIR)/$(LIBFREECELL_SOLVER_JS_MEM_BASE__ASMJS): $(LIBFREECELL_SOLVER_ASMJS_STAMP)
$(LIBFREECELL_SOLVER_ASMJS_STAMP): $(LIBFREECELL_SOLVER_JS_DIR__DESTDIR_DATA)
( cd $(LIBFREECELL_SOLVER_ASMJS_JS_DIR) && $(LIBFREECELL_SOLVER_JS__gmake) ASMJS=1 DATA_DESTDIR=$(LIBFREECELL_SOLVER_JS_DIR__DESTDIR) && touch $(LIBFREECELL_SOLVER_JS_MEM_BASE__ASMJS) && $(STRIP_TRAIL_SPACE) *.js )
touch $@
$(LIBFREECELL_SOLVER_JS_DIR__CMAKE_CACHE):
mkdir -p $(LIBFREECELL_SOLVER_JS_DIR__CMAKE_DIR)
( cd $(LIBFREECELL_SOLVER_JS_DIR__CMAKE_DIR) && cmake -DCMAKE_INSTALL_PREFIX=/fc-solve $(FC_SOLVE_SOURCE_DIR) && make -j4 generate_c_files)
$(DEST_LIBFREECELL_SOLVER_JS_NON_MIN): $(LIBFREECELL_SOLVER_JS)
$(call COPY)
$(DEST_LIBFREECELL_SOLVER_JS_MEM): %: $(LIBFREECELL_SOLVER_WASM_STAMP)
cp -f $(LIBFREECELL_SOLVER_JS_DIR)/$(LIBFREECELL_SOLVER_JS_MEM_BASE) $@
$(DEST_LIBFREECELL_SOLVER_JS_MEM__ASMJS): %: $(LIBFREECELL_SOLVER_ASMJS_STAMP)
cp -f $(LIBFREECELL_SOLVER_ASMJS_JS_DIR)/$(LIBFREECELL_SOLVER_JS_MEM_BASE__ASMJS) $@
EOF

my $flagname = "LIBFREECELL_SOLVER_build_only_flag";

my $text = "$flagname = 0\n\n" . $TEMPLATE;
my $find_deal_text = "$flagname = 1\n\n" . $TEMPLATE;
my $FROM = "LIBFREECELL_SOLVER_JS_DIR__DESTDIR_DATA";
my $TO = "LIBFREECELL_SOLVER_JS_DIR__CMAKE_CACHE";
$find_deal_text =~ s#^\Q$FROM\E :?=[^\n]+\n##gms
or Carp::confess("no matches!");
$find_deal_text =~ s#: \$\(\Q$FROM\E\)#: \$($TO)#gms
or Carp::confess("no matches!");

$find_deal_text =~ s#LIBFREECELL_SOLVER#LIBFIND_DEAL#gms
or Carp::confess("no matches!");
$find_deal_text =~ s#freecell-solver#find-deal#gms
or Carp::confess("no matches!");
$text .= $find_deal_text;

path("lib/make/generated/emcc_libs.mak")->touchpath()->spew_utf8($text);
1 change: 1 addition & 0 deletions fc-solve/site/wml/gen-helpers
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ _exec( [ $^X, 'bin/gen-web-fc-solve-tests--texts-dictionary.pl' ],
'tests-strings' )
if not -e 'src/ts/web-fcs-tests-strings.ts';
_exec( [ $^X, 'bin/my-cookiecutter.pl' ], 'cookiecutter failed.' );
_exec( [ $^X, 'bin/gen_emcc_libs_makefile_fragments.pl' ], "emcc-libs" );
require HTML::Latemp::DocBook::GenMake;
HTML::Latemp::DocBook::GenMake->new(
{ dest_var => '$(D)', 'disable_docbook4' => 1, post_dest_var => '$(D)' } )
Expand Down
31 changes: 31 additions & 0 deletions fc-solve/site/wml/lib/babel/js/libfind-deal-wrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";
function mydef(x, y) {
if (typeof define !== 'function') {
return require('amdefine')(module)(x, y);
} else {
return define(x, y);
}
}
// Taken from https://stackoverflow.com/questions/47879864/
// thanks!
const supported = (() => {
try {
if (typeof WebAssembly === "object" &&
typeof WebAssembly.instantiate === "function") {
const module = new WebAssembly.Module(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
if (module instanceof WebAssembly.Module) {
return (new WebAssembly.Instance(module) instanceof
WebAssembly.Instance);
}
}
} catch (e) {
}
return false;
})();

mydef([(supported ? "libfind-deal.min" :
// mydef([(false ? "libfind-deal.min" :
"libfind-deal-asm")], function(M) {
return M;
});
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define FREECELL_SOLVER_PREFIX "/fc-solve"

#define FREECELL_SOLVER_PKG_DATA_DIR "/fc-solve/share/freecell-solver"
Empty file.
3 changes: 3 additions & 0 deletions fc-solve/site/wml/lib/freecell-solver-for-javascript/prefix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#define FREECELL_SOLVER_PREFIX "/fc-solve"

#define FREECELL_SOLVER_PKG_DATA_DIR "/fc-solve/share/freecell-solver"
Loading

0 comments on commit b1c8286

Please sign in to comment.