:- 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).

file_filter_with_initialization(A,B,C,D,E) :-
    file_filter_with_init(A,B,C,D,E).

main :-
	warning(Warning),
	file_filter_with_initialization('H/HEAPFIELDS','H/generated/hstruct.h',gen_struct,Warning,['hstruct.h','HEAPFIELDS']),
	file_filter_with_initialization('H/HEAPFIELDS','H/generated/dhstruct.h',gen_dstruct,Warning,['dhstruct.h','HEAPFIELDS']),
	file_filter_with_initialization('H/HEAPFIELDS','H/generated/h0struct.h',gen_0struct,Warning,['d0hstruct.h','HEAPFIELDS']),
	file_filter_with_initialization('H/HEAPFIELDS','H/generated/rhstruct.h',gen_hstruct,Warning,['rhstruct.h','HEAPFIELDS']),
	file_filter_with_initialization('H/HEAPFIELDS','H/generated/ihstruct.h',gen_init,Warning,['ihstruct.h','HEAPFIELDS']),
	file_filter_with_initialization('H/GLOBALS','H/generated/h0globals.h',gen_0struct,Warning,['hglobals.h','GLOBALS']),
	file_filter_with_initialization('H/GLOBALS','H/generated/hglobals.h',gen_struct,Warning,['hglobals.h','GLOBALS']),
	file_filter_with_initialization('H/GLOBALS','H/generated/dglobals.h',gen_dstruct,Warning,['dglobals.h','GLOBALS']),
	file_filter_with_initialization('H/GLOBALS','H/generated/iglobals.h',gen_init,Warning,['iglobals.h','GLOBALS']).
%%	file_filter_with_initialization('H/GLOBALS','H/generated/i0globals.h',gen_0init,Warning,['iglobals.h','GLOBALS']),
%	file_filter_with_initialization('H/LOCALS','H/generated/hlocals.h',gen_struct,Warning,['hlocals.h','LOCALS']),
%	file_filter_with_initialization('H/LOCALS','H/generated/dlocals.h',gen_dstruct,Warning,['dlocals.h','LOCALS']),
%	file_filter_with_initialization('H/LOCALS','H/generated/rlocals.h',gen_hstruct,Warning,['rlocals.h','LOCALS']),
%	file_filter_with_initialization('H/LOCALS','H/generated/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 H/~a instead */~n~n').



/* define the field */
gen_struct(Inp,Inp) :-
	Inp = [0'/,0'/|_], !.
gen_struct(Inp,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 = "START_HEAP", !,
	Out = "".
gen_struct(Inp,Out) :-
	Inp = "END_HEAP", !,
	Out = "".
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,Out) :-
	split(Inp,"	 ",["const",Type, Field|_]), !,
	append(["const ", 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]).

/* define the field */
gen_0struct(Inp,Inp) :-
	Inp = [0'/,0'/|_], !.
gen_0struct(Inp,Inp) :-
	Inp = [0'/,0'*|_], !.
gen_0struct(Inp, Out) :-
	Inp = [0'#|_], !, Out = Inp. % '
gen_0struct(Inp,"") :-
	Inp = [0'.|_], !. %'
gen_0struct(Inp,Out) :-
	Inp = "START_GLOBAL_DATA", !,
	Out = "",
    assert(globals(all)).
gen_0struct(Inp,Out) :-
	Inp = "END_GLOBAL_DATA", !,
	Out = "",
    retract(globals(all)).
gen_0struct(Inp,Out) :-
	Inp = "START_HEAP", !,
	Out = "",
    assert(globals(heap)).
gen_0struct(Inp,Out) :-
	Inp = "END_HEAP", !,
	Out = "",
    retract(globals(heap)).
gen_0struct(Inp,Out) :-
	Inp = "ATOMS", !,
	Out = "#include \"tatoms.h\"".
gen_0struct(Inp,Out) :-
	split(Inp,"	 ",["struct",_Type, _Field|_L]), !,
    extract("struct", Inp, NInp),
    gen_0struct( NInp, NOut ),
    extract("EXTERNAL", NOut, IOut),
	append("EXTERNAL    struct ", IOut, Out).
gen_0struct(Inp,Out) :-
	split(Inp,"	 ",["const",_Type, _Field|_L]), !,
    extract("const", Inp, NInp),
    gen_0struct( NInp, NOut ),
    extract("EXTERNAL", NOut, IOut),
	append("EXTERNAL    const ", IOut, Out).
gen_0struct(Inp,Out) :-
	split(Inp,"	 ",["union",_Type, _Field|_L]), !,
    extract("union", Inp, NInp),
    gen_0struct( NInp, NOut ),
    extract("EXTERNAL", NOut, IOut),
	append("EXTERNAL    union ", IOut, Out).
gen_0struct(Inp,"") :-
	split(Inp,"	 ",["void","void"|_]), !.
gen_0struct(Inp,Out) :-
	split(Inp,"	 ",[Type, Field|_]),
	split(Field,"[",[RField,VECField]), !,
    fetch_name(Name, RField),
	append(["EXTERNAL  ",Type,"  ",Name,"[",VECField,";"], Out).
gen_0struct(Inp,Out) :-
	split(Inp,"	 ",[Type, Field|_]), !,
    fetch_name(Name, Field),
	append(["EXTERNAL  ",Type,"  ",Name,";"], Out).
gen_0struct(Inp,_) :-
	split(Inp,"	 ",[_, _, _| _]),
	format(user_error,"OOPS: could not gen_0struct 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,"") :-
	Inp = "START_HEAP", !,
	assert(globals(heap)).
gen_dstruct(Inp,"") :-
	Inp = "END_HEAP", !,
	retract(globals(heap)).
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,"	 ",["const"|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(RField,Global2,RField," ") :-
	globals(heap), !,
	append(["Yap_heap_regs->", 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).
fetch_name(RField, RField) :-
	globals(heap), !.

                                % 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 = "START_HEAP", !,
    Out = "",
    assert(globals(heap)).
gen_hstruct(Inp,Out) :-
    Inp = "END_HEAP", !,
    Out = "",
    retract(globals(heap)).
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,"	 ",["const"|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", _]),
    globals(heap),
    !,
	fetch_name(Global,Field),
	append(["  ",Global," = AtomTermAdjust(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
    globals(heap),
	split(Inp,"	 ",[_, Field, "MkPred"| _]),
    !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = PtoPredAdjust(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
    globals(heap),
	split(Inp,"	 ",[_, Field, "MkLogPred"| _]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = PtoPredAdjust(",Global,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkOp", Name]), !,
	fetch_name(Global,Field),
	append(["  ",Global," = 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,");"], Out).
gen_hstruct(Inp,Out) :-
	split(Inp,"	 ",[_, Field,"MkInstE",OP]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	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,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) :-
    Inp = "START_HEAP", !,
    Out = "",
    assert(globals(heap)).
gen_init(Inp,Out) :-
    Inp = "END_HEAP", !,
    Out = "",
    retract(globals(heap)).
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,"") :-
	split(Inp,"	 ",["const"|_]), !.
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, "MkLogPred", Atom, "0", Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = Yap_MkLogPred(RepPredProp(PredPropByAtom(",Atom,",",Module,")));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkLogPred", Atom, Arity, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = Yap_MkLogPred(RepPredProp(PredPropByFunc(Yap_MkFunctor(",Atom,",",Arity,"),",Module,")));"], Out).
gen_init(Inp,Out) :-
	split(Inp,"	 ",[_, Field, "MkLogPred", Fun, Module]), !,
	cut_c_stuff(Field, RField),
	fetch_name(Global,RField),
	append(["  ",Global," = Yap_MkLogPred(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]).

extract(X, Y, F) :-
    append(X, R, Y),
    !,
    extract(R, F).

extract([0' |H], IF) :- !,
    extract( H, IF).
extract([0'\t |H], IF) :- !,
    extract( H, IF).
extract(H,H).