diff --git a/H/dhstruct.h b/H/dhstruct.h index e2d2ea78b..a33b6bb4d 100644 --- a/H/dhstruct.h +++ b/H/dhstruct.h @@ -56,6 +56,8 @@ #define HeapTopLock Yap_heap_regs->heap_top_lock #define HeapTopOwner Yap_heap_regs->heap_top_owner #endif +#define MaxStack Yap_heap_regs->MaxStack_ +#define MaxTrail Yap_heap_regs->MaxTrail_ #if USE_THREADED_CODE diff --git a/H/hstruct.h b/H/hstruct.h index 3fe895370..5dd6f25c2 100644 --- a/H/hstruct.h +++ b/H/hstruct.h @@ -56,6 +56,8 @@ lockvar heap_top_lock; int heap_top_owner; #endif + UInt MaxStack_; + UInt MaxTrail_; #if USE_THREADED_CODE diff --git a/H/ihstruct.h b/H/ihstruct.h index f2073956f..3ffc74630 100644 --- a/H/ihstruct.h +++ b/H/ihstruct.h @@ -56,6 +56,8 @@ INIT_LOCK(HeapTopLock); HeapTopOwner = -1; #endif + MaxStack = 0; + MaxTrail = 0; #if USE_THREADED_CODE diff --git a/H/rhstruct.h b/H/rhstruct.h index 41725804c..485a6ed5e 100644 --- a/H/rhstruct.h +++ b/H/rhstruct.h @@ -58,6 +58,8 @@ #endif + + #if USE_THREADED_CODE OP_RTABLE = OpRTableAdjust(OP_RTABLE); #endif diff --git a/docs/yap.tex b/docs/yap.tex index 0885ca694..c5531f98d 100644 --- a/docs/yap.tex +++ b/docs/yap.tex @@ -824,7 +824,7 @@ $YAPSRC\VC\include, $YAPSRC\include @example ['$YAPSRC\\pl\\init']. -save_program('startup.yss'). +qsave_program('startup.yss'). ^Z @end example @@ -2154,6 +2154,45 @@ Saves an image of the current state of the YAP database in file @var{F}, and guarantee that execution of the restored code will start by trying goal @var{G}. +@item qsave_program(+@var{F}, +@var{ListOfOpts}) +@findex qsave_program/2 +@syindex qsave_program/2 +@cnindex qsave_program/2 + +Saves the current state of the program to the file @var{File}. The +result is a resource archive containing a saved state that expresses +all Prolog data from the running program and all user-defined +resources. Depending on the stand_alone option, the resource is headed +by the emulator, a Unix shell script or nothing. Options is a list of +additional options: + +@table @code +@item stack(+@var{KBytes}) +Limit for the local and global stack. +@item trail(+@var{KBytes}) +Limit for the trail stack. +@item goal(:@var{Callable}) +Initialization goal for the new executable (see -g). +@c @item toplevel(:@var{Callable}) +@c Top-level goal for the new executable (see -t). +@item init_file(+@var{Atom}) +Default initialization file for the new executable. See -f. +@c class(+Class) +@c If runtime, only read resources from the state (default). If kernel, lock all predicates as system predicates. If development, save the predicates in their current state and keep reading resources from their source (if present). See also resource/3. +@c autoload(+Boolean) +@c If true (default), run autoload/0 first. +@c map(+File) +@c Dump a human-readable trace of what has been saved in File. +@c op(+Action) +@c One of save (default) to save the current operator table or standard to use the initial table of the emulator. +@c stand_alone(+Boolean) +@c If true, the emulator is the first part of the state. If the emulator is started it will test whether a boot-file (state) is attached to the emulator itself and load this state. Provided the application has all libraries loaded, the resulting executable is completely independent of the runtime environment or location where it was built. See also section 2.10.2.4. +@c emulator(+File) +@c File to use for the emulator. Default is the running Prolog image. +@c foreign(+Action) +@c If save, include shared objects (DLLs) into the saved state. See current_foreign_library/2. If the program strip is available, this is first used to reduce the size of the shared object. If a state is started, use_foreign_library/1 first tries to locate the foreign resource in the executable. When found it copies the content of the resource to a temporary file and loads it. If possible (Unix), the temporary object is deleted immediately after opening.106 +@end table + @item restore(+@var{F}) @findex restore/1 @syindex restore/1 diff --git a/misc/HEAPFIELDS b/misc/HEAPFIELDS index cf47a0301..ecb62bc9a 100644 --- a/misc/HEAPFIELDS +++ b/misc/HEAPFIELDS @@ -53,7 +53,8 @@ lockvar heap_used_lock HeapUsedLock MkLock lockvar heap_top_lock HeapTopLock MkLock int heap_top_owner HeapTopOwner =-1 void #endif - +UInt MaxStack_ MaxStack =0 void +UInt MaxTrail_ MaxTrail =0 void /* execution info */ /* OPCODE TABLE, needed to recover op tables */ diff --git a/pl/modules.yap b/pl/modules.yap index 4ee94e467..4328c2a83 100644 --- a/pl/modules.yap +++ b/pl/modules.yap @@ -641,6 +641,7 @@ source_module(Mod) :- predicate_property(:,?), predicate_statistics(:,-,-,-), on_exception(+,0,0), + qsave_program(+,:), reconsult(:), retract(:), retract(:,?), diff --git a/pl/qly.yap b/pl/qly.yap index cb3b4cf9e..4e253058d 100755 --- a/pl/qly.yap +++ b/pl/qly.yap @@ -20,20 +20,114 @@ save_program(File) :- qsave_program(File). qsave_program(File) :- - '$save_program_status', + '$save_program_status'([], qsave_program(File)), open(File, write, S, [type(binary)]), '$qsave_program'(S), close(S). +qsave_program(File, Opts) :- + '$save_program_status'(Opts, qsave_program(File,Opts)), + open(File, write, S, [type(binary)]), + '$qsave_program'(S), + % make sure we're not going to bootstrap from this file. + close(S). + save_program(File, Goal) :- recorda('$restore_goal', Goal ,_R), fail. save_program(File, _Goal) :- qsave_program(File). -'$save_program_status' :- - findall(F:V,'$x_yap_flag'(F,V),L), - recordz('$program_state',L,_). +'$save_program_status'(Flags, G) :- + findall(F:V,'$x_yap_flag'(F,V),L), + recordz('$program_state',L,_), + '$cvt_qsave_flags'(Flags, G), + fail. +'$save_program_status'(_Flags, _G). + +'$cvt_qsave_flags'(Flags, G) :- + nonvar(Flags), + strip_module(Flags, M, LFlags), + '$skip_list'(_Len, LFlags, []), + '$cvt_qsave_lflags'(LFlags, G, M). +'$cvt_qsave_flags'(Flags, G,_OFlags) :- + var(Flags), + '$do_error'(instantiation_error,G). +'$cvt_qsave_flags'(Flags, G,_OFlags) :- + '$do_error'(type_error(list,Flags),G). + +'$cvt_qsave_lflags'([], _, _). +'$cvt_qsave_lflags'([Flag|Flags], G, M) :- + '$cvt_qsave_flag'(Flag, G, M), + '$cvt_qsave_lflags'(Flags, G, M). + +'$cvt_qsave_flag'(Flag, G, _) :- + var(Flag), !, + '$do_error'(instantiation_error,G). +'$cvt_qsave_flag'(local(B), G, _) :- !, + ( number(B) -> + ( + B > 0 -> recordz('$restore_flag',local(B),_) ; + B =:= 0 -> true ; + '$do_error'(domain_error(not_less_than_zero,B),G)) + ; + '$do_error'(type_error(integer,B),G) + ). +'$cvt_qsave_flag'(global(B), G, _) :- !, + ( number(B) -> + ( + B > 0 -> recordz('$restore_flag',global(B),_) ; + B =:= 0 -> true ; + '$do_error'(domain_error(not_less_than_zero,B),G)) + ; + '$do_error'(type_error(integer,B),G) + ). +'$cvt_qsave_flag'(stack(B), G, _) :- !, + ( number(B) -> + ( + B > 0 -> recordz('$restore_flag',stack(B),_) ; + B =:= 0 -> true ; + '$do_error'(domain_error(not_less_than_zero,B),G)) + ; + '$do_error'(type_error(integer,B),G) + ). +'$cvt_qsave_flag'(trail(B), G, _) :- !, + ( number(B) -> + ( + B > 0 -> recordz('$restore_flag',trail(B),_) ; + B =:= 0 -> true ; + '$do_error'(domain_error(not_less_than_zero,B),G)) + ; + '$do_error'(type_error(integer,B),G) + ). +'$cvt_qsave_flag'(goal(B), G, M) :- !, + ( callable(B) -> + strip_module(M:B, M1, G1), + recordz('$restore_flag',goal(M1:G1),_) + ; + '$do_error'(type_error(callable,B),G) + ). +'$cvt_qsave_flag'(toplevel(B), G, M) :- !, + ( callable(B) -> + strip_module(M:B, M1, G1), + recordz('$restore_flag',toplevel(M1:G1),_) + ; + '$do_error'(type_error(callable,B),G) + ). +'$cvt_qsave_flag'(init_file(B), G, M) :- !, + ( atom(B) -> + recordz('$restore_flag', init_file(M:B), _) + ; + '$do_error'(type_error(atom,B),G) + ). +%% '$cvt_qsave_flag'(class(_B), G, class(_B)). +%% '$cvt_qsave_flag'(autoload(_B), G, autoload(_B)). +%% '$cvt_qsave_flag'(op(_B), G, op(_B)). +%% '$cvt_qsave_flag'(stand_alone(_B), G, stand_alone(_B)). +%% '$cvt_qsave_flag'(emulator(_B), G, emulator(_B)). +%% '$cvt_qsave_flag'(foreign(_B), G, foreign(_B)). +'$cvt_qsave_flag'(Opt, G, _M) :- + '$do_error'(domain_error(qsave_program,Opt), G). % there is some ordering between flags. '$x_yap_flag'(goal, Goal). @@ -104,6 +198,11 @@ save_program(File, _Goal) :- set_value('$consult_on_boot',[]), '$do_startup_reconsult'(X), fail. +'$init_from_saved_state_and_args' :- + recorded('$restore_flag', init_file(M:B), R), + erase(R), + '$do_startup_reconsult'(M:B), + fail. '$init_from_saved_state_and_args' :- '$startup_goals', fail. @@ -134,6 +233,11 @@ save_program(File, _Goal) :- set_value('$init_goal',[]), '$run_atom_goal'(GA), fail. +'$startup_goals' :- + recorded('$restore_flag', goal(Module:GA), R), + erase(R), + '$system_catch'('$query'(once(GA), []),Module,Error,user:'$Error'(Error)), + fail. '$startup_goals' :- get_value('$myddas_goal',GA), GA \= [], set_value('$myddas_goal',[]),