
:- use_module(library(lineutils),
	[file_filter_with_init/5,
	 split/3,
	 glue/3]).

:- use_module(library(lists),
	[append/2,
	 append/3]).

:- initialization(main).

:- dynamic globals/1.

:- yap_flag(write_strings,on).

:- style_check(all).

main :-
	warning(Warning),
	%file_filter_with_init('misc/HEAPFIELDS','H/hstruct.h',gen_struct,Warning,['hstruct.h','HEAPFIELDS']),
	%file_filter_with_init('misc/HEAPFIELDS','H/dhstruct.h',gen_dstruct,Warning,['dhstruct.h','HEAPFIELDS']),
	%file_filter_with_init('misc/HEAPFIELDS','H/rhstruct.h',gen_hstruct,Warning,['rhstruct.h','HEAPFIELDS']),
	%file_filter_with_init('misc/HEAPFIELDS','H/ihstruct.h',gen_init,Warning,['ihstruct.h','HEAPFIELDS']),
	file_filter_with_init('misc/GLOBALS','H/hglobals.h',gen_struct,Warning,['hglobals.h','GLOBALS']),
	file_filter_with_init('misc/GLOBALS','H/dglobals.h',gen_dstruct,Warning,['dglobals.h','GLOBALS']),
	file_filter_with_init('misc/GLOBALS','H/rglobals.h',gen_hstruct,Warning,['rglobals.h','GLOBALS']),
	file_filter_with_init('misc/GLOBALS','H/iglobals.h',gen_init,Warning,['iglobals.h','GLOBALS']),
	file_filter_with_init('misc/LOCALS','H/hlocals.h',gen_struct,Warning,['hlocals.h','LOCALS']),
	file_filter_with_init('misc/LOCALS','H/dlocals.h',gen_dstruct,Warning,['dlocals.h','LOCALS']),
	file_filter_with_init('misc/LOCALS','H/rlocals.h',gen_hstruct,Warning,['rlocals.h','LOCALS']),
	file_filter_with_init('misc/LOCALS','H/ilocals.h',gen_init,Warning,['ilocals.h','LOCALS']).
	
warning('~n  /* This file, ~a, was generated automatically by \"yap -L misc/buildlocalglobal\"~n     please do not update, update misc/~a instead */~n~n').



/* define the field */
gen_struct(Inp,"") :-
	Inp = [0'/,0'/|_], !.
gen_struct(Inp,"") :-
	Inp = [0'/,0'*|_], !.
gen_struct(Inp, Out) :-
	Inp = [0'#|_], !, Out = Inp. % '
gen_struct(Inp,"") :-
	Inp = [0'.|_], !. %'
gen_struct(Inp,Out) :-
	Inp = "START_WORKER_LOCAL", !,
	Out = "typedef struct worker_local {".
gen_struct(Inp,Out) :-
	Inp = "END_WORKER_LOCAL", !,
	Out = "} w_local;".
gen_struct(Inp,Out) :-
	Inp = "START_GLOBAL_DATA", !,
	Out = "typedef struct global_data {".
gen_struct(Inp,Out) :-
	Inp = "END_GLOBAL_DATA", !,
	Out = "} w_shared;".
gen_struct(Inp,Out) :-
	Inp = "ATOMS", !,
	Out = "#include \"tatoms.h\"".
gen_struct(Inp,Out) :-
	split(Inp,"	 ",["struct",Type, Field|_]), !,
	append(["  struct ",Type,"  ",Field,"_",";"], Out).
gen_struct(Inp,Out) :-
	split(Inp,"	 ",["union",Type, Field|_]), !,
	append(["  union ",Type,"  ",Field,"_",";"], Out).
gen_struct(Inp,"") :-
	split(Inp,"	 ",["void","void"|_]), !.
gen_struct(Inp,Out) :-
	split(Inp,"	 ",[Type, Field|_]), 
	split(Field,"[",[RField,VECField]), !,
	append(["  ",Type,"  ",RField,"_","[",VECField,";"], Out).
gen_struct(Inp,Out) :-
	split(Inp,"	 ",[Type, Field|_]), !,
	append(["  ",Type,"  ",Field,"_",";"], Out).
gen_struct(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_struct for ~s~n",[Inp]).
	
gen_dstruct(Inp,"") :-
	Inp = [0'/,0'/|_], !.
gen_dstruct(Inp,"") :-
	Inp = [0'/,0'*|_], !.
gen_dstruct(Inp, Out) :-
	Inp = [0'#|_], !, Out = Inp. % '
gen_dstruct(Inp,"") :-
	Inp = "START_WORKER_LOCAL", !,
	assert(globals(worker)).
gen_dstruct(Inp,"") :-
	Inp = "END_WORKER_LOCAL", !,
	retract(globals(worker)).
gen_dstruct(Inp,"") :-
	Inp = "START_GLOBAL_DATA", !,
	assert(globals(all)).
gen_dstruct(Inp,"") :-
	Inp = "END_GLOBAL_DATA", !,
	retract(globals(all)).
gen_dstruct(Inp,Out) :-
	Inp = "ATOMS", !,
	Out = "".
gen_dstruct(Inp,"") :-
	split(Inp,"	 ",["void","void"|_]), !.
gen_dstruct(Inp,Out) :-
	split(Inp,"	 ",["struct"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_dstruct(Inp3, Out).
gen_dstruct(Inp,Out) :-
	split(Inp,"	 ",["union"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_dstruct(Inp3, Out).
gen_dstruct(Inp,Out) :-
	split(Inp,"	 ",[".", Field,_|_]), !,
	cut_c_stuff(Field, RField),
	split(RField,".",[_,MField]),
	fetch_name(Global,Global2,RField,MField),
	append(["#define ",Global," ",Global2], Out).
gen_dstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field,_|_]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,Global2,RField," "),
	append(["#define ",Global," ",Global2], Out).
gen_dstruct(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_dstruct for ~s~n",[Inp]).

fetch_name(Global,Global2,RField," ") :-
	globals(all), !,
	append(["GLOBAL_", RField],Global),
	append(["Yap_global->", RField,"_"],Global2).
fetch_name(Global,Global2,RField," ") :-
	globals(worker),
	append(["LOCAL_", RField],Global),
	append(["LOCAL->", RField,"_"],Global2).
fetch_name(Global,Global2,RField," ") :-
	globals(worker), !,
	append(["REMOTE_", RField,"(wid)"],Global),
	append(["REMOTE(wid)->", RField,"_"],Global2).
fetch_name(Global,Global2,RField,MField) :-
	globals(worker),
	append(["REMOTE_", MField,"(wid)"],Global),
	append(["REMOTE(wid)->", RField],Global2).
fetch_name(Global,Global2,RField,MField) :-
	globals(worker),!,
	append(["LOCAL_", MField],Global),
	append(["LOCAL->", RField],Global2).

fetch_name(Global, RField) :-
	globals(worker_init), !,
	append(["REMOTE_", RField,"(wid)"],Global).
fetch_name(Global, RField) :-
	globals(all), !,
	append(["GLOBAL_", RField],Global).

% handle *field[4]
cut_c_stuff([0'*|Name], RName) :- !, % 'cut *
	cut_c_stuff(Name, RName).
cut_c_stuff(Name, RName) :-
	cut_mat(Name, RName).

cut_mat([], []).
cut_mat([0'[|_], []) :- !. %' 
cut_mat(H.Name, H.RName) :- 
	cut_mat(Name, RName).

gen_hstruct(Inp,"") :-
	Inp = [0'/,0'/|_], !.
gen_hstruct(Inp,"") :-
	Inp = [0'.|_], !. %'
gen_hstruct(Inp,"") :-
	Inp = [0'/,0'*|_], !.
gen_hstruct(Inp,Out) :-
	Inp = "ATOMS", !,
	Out = "#include \"ratoms.h\"".
gen_hstruct(Inp,Out) :-
	Inp = "START_WORKER_LOCAL", !,
	Out = "static void RestoreWorker(int wid USES_REGS) {",
	assert(globals(worker_init)).
gen_hstruct(Inp,Out) :-
	Inp = "END_WORKER_LOCAL", !,
	Out = "}",
	retract(globals(worker_init)).
gen_hstruct(Inp,Out) :-
	Inp = "START_GLOBAL_DATA", !,
	Out = "static void RestoreGlobal(void) {",
	assert(globals(all)).
gen_hstruct(Inp,Out) :-
	Inp = "END_GLOBAL_DATA", !,
	Out = "}",
	retract(globals(all)).
gen_hstruct(Inp, Out) :-
	Inp = [0'#|_], !, Out = Inp. % '
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",["struct"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_hstruct(Inp3,Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",["union"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_hstruct(Inp3,Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkAT", _]), !,
	fetch_name(Global,Field),
	append(["  ",Global,Field," = AtomTermAdjust(Yap_heap_regs->",Field,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkPred"| _]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global,RField," = PtoPredAdjust(Yap_heap_regs->",RField,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkOp", Name]), !,
	fetch_name(Global,Field),
	append(["  ",Global,Field," = Yap_opcode(",Name,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkLock"]), !,
	fetch_name(Global,Field),
	append(["  REINIT_LOCK(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field,"MkRWLock"]), !,
	fetch_name(Global,Field),
	append(["  REINIT_RWLOCK(",Global,Field,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field,"MkInstE",OP]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global,RField,"->opc = Yap_opcode(",OP,");"], Out).
gen_hstruct(Inp,"") :-
	split(Inp,"	 ",[_, _, _]), !.
gen_hstruct(Inp,"") :-
	split(Inp,"	 ",[_, _, _, "void"]), !.
gen_hstruct(Inp,Restore) :-
	split(Inp,"	 ",[_, _, _, Restore0]),
	append("Restore",_,Restore0), !,
	append(["  ",Restore0,";"],Restore). %'
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, _, Adjust]),
	append(Adjust,"Adjust",_), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = ",Adjust,"(",Global,");"], Out).
gen_hstruct(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_hstruct for ~s~n",[Inp]).

gen_init(Inp,"") :-
	Inp = [0'/,0'*|_], !.
gen_init(Inp,"") :-
	Inp = [0'/,0'/|_], !.
gen_init(Inp, Out) :-
	Inp = [0'#|_], !, Out = Inp. % '
gen_init(Inp,Out) :-
	Inp = "ATOMS", !,
	Out = "#include \"iatoms.h\"".
gen_init(Inp,Out) :-
	Inp = "START_WORKER_LOCAL", !,
	Out = "static void InitWorker(int wid) {",
	assert(globals(worker_init)).
gen_init(Inp,Out) :-
	Inp = "END_WORKER_LOCAL", !,
	Out = "}",
	retract(globals(worker_init)).
gen_init(Inp,Out) :-
	Inp = "START_GLOBAL_DATA", !,
	Out = "static void InitGlobal(void) {",
	assert(globals(all)).
gen_init(Inp,Out) :-
	Inp = "END_GLOBAL_DATA", !,
	Out = "}",
	retract(globals(all)).
gen_init(Inp,Out) :-
	split(Inp,"	 ",["struct"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_init(Inp3, Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",["union"|Inp2]), !,
	glue(Inp2, " ", Inp3),
	gen_init(Inp3, Out).
gen_init(Inp,"") :-
	split(Inp,"	 ",[_, _, "void"|_]), !.
gen_init(Inp,Init) :-
	split(Inp,"	 ",[_, _, Init0| _]),
	append("Init",_,Init0), !,
	append(["  ",Init0,";"],Init).
gen_init(Inp,Init) :-
	split(Inp,"	 ",[_, _, Init0| _]),
	append("Yap_Init",_,Init0), !,
	append(["  ",Init0,";"],Init).
gen_init(Inp,Init) :-
	split(Inp,"	 ",[_, _, Init0| _]),
	append("Yap_init",_,Init0), !,
	append(["  ",Init0,";"],Init).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkAT", AtomName]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = MkAtomTerm(",AtomName,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkOp", Name]), !,
	fetch_name(Global,Field),
	append(["  ",Global," = Yap_opcode(",Name,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkLock"]), !,
	fetch_name(Global,Field),
	append(["  INIT_LOCK(",Global,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkRWLock"]), !,
	fetch_name(Global,Field),
	append(["  INIT_RWLOCK(",Global,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkInstE",OP]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global,"->opc = Yap_opcode(",OP,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkPred", Atom, "0", Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = RepPredProp(PredPropByAtom(",Atom,",",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkPred", Atom, Arity, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = RepPredProp(PredPropByFunc(Yap_MkFunctor(",Atom,",",Arity,"),",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkPred", Fun, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = RepPredProp(PredPropByFunc(",Fun,",",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[".", Field,F0|_]), !,
	cut_c_stuff(Field, RField),
	split(RField,".",[_,MField]),
	append("=",F,F0), !,
	fetch_name(Global,MField),
	append(["  ",Global," = ",F,";"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, F0|_]),
	append("=",F,F0), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = ",F,";"], Out).
gen_init(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_init for ~s~n",[Inp]).

