:- 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/buildheap\"~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|_]), !,
	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, Name|_]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global),
	append(["#define ",Name," ",Global,RField], Out).
gen_dstruct(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_dstruct for ~s~n",[Inp]).

fetch_name(Global) :-
	globals(all), !,
	Global = "Yap_global->".
fetch_name(Global) :-
	globals(worker), !,
	Global = "LOCAL->".
fetch_name(Global) :-
	globals(worker_init), !,
	Global = "REMOTE(wid)->".
fetch_name(Global) :-
	Global = "Yap_heap_regs->".

fetch_name(Global,RField,_) :-
	globals(worker_init), !,
	append("REMOTE(wid)->",RField,Global).
fetch_name(Global,_,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, MacroName, "MkAT", _]), !,
	fetch_name(Global,Field,MacroName),
	append(["  ",Global," = AtomTermAdjust(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkPred"| _]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = PtoPredAdjust(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkOp", Name]), !,
	fetch_name(Global,Field,MacroName),
	append(["  ",Global," = Yap_opcode(",Name,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkLock"]), !,
	fetch_name(Global,Field,MacroName),
	append(["  REINIT_LOCK(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkRWLock"]), !,
	fetch_name(Global,Field,MacroName),
	append(["  REINIT_RWLOCK(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkInstE",OP]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global,"->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, MacroName, _, Adjust]),
	append(Adjust,"Adjust",_), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	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, MacroName, "MkAT", AtomName]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = MkAtomTerm(",AtomName,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkOp", Name]), !,
	fetch_name(Global,Field,MacroName),
	append(["  ",Global," = Yap_opcode(",Name,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkLock"]), !,
	fetch_name(Global,Field,MacroName),
	append(["  INIT_LOCK(",Global,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkRWLock"]), !,
	fetch_name(Global,Field,MacroName),
	append(["  INIT_RWLOCK(",Global,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkInstE",OP]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global,"->opc = Yap_opcode(",OP,");"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkPred", Atom, "0", Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = RepPredProp(PredPropByAtom(",Atom,",",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkPred", Atom, Arity, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = RepPredProp(PredPropByFunc(Yap_MkFunctor(",Atom,",",Arity,"),",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, "MkPred", Fun, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = RepPredProp(PredPropByFunc(",Fun,",",Module,"));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, MacroName, F0|_]),
	append("=",F,F0), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField,MacroName),
	append(["  ",Global," = ",F,";"], Out).
gen_init(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_init for ~s~n",[Inp]).