//
// File defining fields in the Yap_heap_codes global structure
//
// these fields used to spread all over the place, because they must be used in 4 ways:
//  - they must be defined somewhere
//  - they have an #ifdef to get a shorter name
//  - they must be initialised somewhere
//  - they must be restorable and collectable (from the atom gc).
//
//
// The defs include 4+ components:
//   Type
//   name in structured
//   global name
//   init code and restore code (optional)
//
//   
//
// MkAT (MkAtomTerm) cvts from a predefined atom to a term
// MkPred constructs a pred_entry
// MkOp gets an opcode
// void does nothing
// =VALUE inits as VALUE
// Init... sets up call to InitFunc
// Restore... sets up call to RestoreFunc
//

/* memory management */
UInt		hole_size		Yap_HoleSize		void void
struct malloc_state *av_		Yap_av			void void
#if USE_DL_MALLOC
struct memory_hole memory_holes[MAX_DLMALLOC_HOLES] Yap_MemoryHoles void void
UInt		nof_memory_holes	Yap_NOfMemoryHoles	void void
#if defined(YAPOR) || defined(THREADS)
lockvar         dlmalloc_lock		DLMallocLock		MkLock
#endif
#endif
#if USE_DL_MALLOC || (USE_SYSTEM_MALLOC && HAVE_MALLINFO)
#ifndef  HeapUsed
#define  HeapUsed  Yap_givemallinfo()		
#endif
Int		heap_used		NotHeapUsed		void void
#else
Int		heap_used		HeapUsed		void void
#endif
Int		heap_max		HeapMax			void void
ADDR		heap_top		HeapTop			void void
ADDR		heap_lim		HeapLim			void void
struct FREEB    *free_blocks		FreeBlocks		void void
#if defined(YAPOR) || defined(THREADS)
lockvar		free_blocks_lock	FreeBlocksLock		MkLock
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 */
#if USE_THREADED_CODE
opentry		*op_rtable		OP_RTABLE		void	OpRTableAdjust
#endif

/* popular opcodes */
OPCODE		execute_cpred_op_code	EXECUTE_CPRED_OP_CODE	MkOp _execute_cpred
OPCODE		expand_op_code		EXPAND_OP_CODE		MkOp _expand_index
OPCODE		fail_op			FAIL_OPCODE		MkOp _op_fail
OPCODE		index_op		INDEX_OPCODE		MkOp _index_pred
OPCODE		lockpred_op		LOCKPRED_OPCODE		MkOp _lock_pred
OPCODE		orlast_op		ORLAST_OPCODE		MkOp _or_last
OPCODE		undef_op		UNDEF_OPCODE		MkOp _undef_p

/* atom tables */
UInt		n_of_atoms		NOfAtoms		void void
UInt		atom_hash_table_size	AtomHashTableSize	void void
UInt		wide_atom_hash_table_size WideAtomHashTableSize void void
UInt		n_of_wide_atoms		NOfWideAtoms		void void
AtomHashEntry	invisiblechain		INVISIBLECHAIN		InitInvisibleAtoms() RestoreInvisibleAtoms()
AtomHashEntry	*wide_hash_chain	WideHashChain		InitWideAtoms()  RestoreWideAtoms()
AtomHashEntry	*hash_chain		HashChain		InitAtoms() RestoreAtoms()

/* use atom defs here */
ATOMS

#ifdef EUROTRA
Term		term_dollar_u		TermDollarU		MkAT AtomDollarU
#endif
Term		term_prolog		TermProlog		MkAT AtomProlog
Term		term_refound_var	TermReFoundVar		MkAT AtomRefoundVar
Term		user_module		USER_MODULE		MkAT AtomUser
Term		idb_module		IDB_MODULE		MkAT AtomIDB
Term		attributes_module	ATTRIBUTES_MODULE	MkAT AtomAttributes
Term		charsio_module		CHARSIO_MODULE		MkAT AtomCharsio
Term		terms_module		TERMS_MODULE		MkAT AtomTerms
Term		system_module		SYSTEM_MODULE		MkAT AtomSystem
Term		operating_system_module	OPERATING_SYSTEM_MODULE	MkAT AtomOperatingSystemSupport
Term		readutil_module		READUTIL_MODULE		MkAT AtomReadutil
Term		hacks_module		HACKS_MODULE		MkAT AtomYapHacks
Term		arg_module		ARG_MODULE		MkAT AtomArg
Term		globals_module		GLOBALS_MODULE		MkAT AtomNb
Term		swi_module		SWI_MODULE		MkAT AtomSwi
Term		dbload_module		DBLOAD_MODULE		MkAT AtomDBLoad
Term		range_module		RANGE_MODULE		MkAT AtomRange

// hidden predicates
Prop		hidden_predicates	HIDDEN_PREDICATES	=NULL RestoreHiddenPredicates()

//
// Module list
//
struct mod_entry *current_modules	CurrentModules		=NULL	ModEntryPtrAdjust
// make sure we have the modules set at this point.
// don't actually want to define a field
void		void			void			Yap_InitModules()  void

/* Anderson's JIT */
yap_exec_mode	execution_mode		Yap_ExecutionMode	=INTERPRETED void

/*  The Predicate Hash Table: fast access to predicates. */
struct pred_entry **pred_hash		PredHash		InitPredHash() RestorePredHash()
#if defined(YAPOR) || defined(THREADS)
rwlock_t	pred_hash_rw_lock	PredHashRWLock		void
#endif
UInt		preds_in_hash_table	PredsInHashTable	=0 void
UInt		pred_hash_table_size	PredHashTableSize	void


/* Well-Known Predicates */
struct pred_entry *creep_code		CreepCode		MkPred AtomCreep 1 PROLOG_MODULE
struct pred_entry *undef_code		UndefCode		MkPred AtomUndefp 1 PROLOG_MODULE
struct pred_entry *spy_code		SpyCode			MkPred AtomSpy 1 PROLOG_MODULE
struct pred_entry *pred_fail		PredFail		MkPred AtomFail 0 PROLOG_MODULE
struct pred_entry *pred_true		PredTrue		MkPred AtomTrue 0 PROLOG_MODULE
#ifdef COROUTINING
struct pred_entry  *wake_up_code	WakeUpCode		MkPred AtomWakeUpGoal 2 PROLOG_MODULE
#endif
struct pred_entry *pred_goal_expansion	PredGoalExpansion	MkPred FunctorGoalExpansion USER_MODULE
struct pred_entry *pred_meta_call	PredMetaCall		MkPred FunctorMetaCall PROLOG_MODULE
struct pred_entry *pred_dollar_catch	PredDollarCatch         MkPred FunctorCatch PROLOG_MODULE
struct pred_entry *pred_recorded_with_key PredRecordedWithKey	MkPred FunctorRecordedWithKey PROLOG_MODULE
struct pred_entry *pred_log_upd_clause	PredLogUpdClause	MkPred FunctorDoLogUpdClause PROLOG_MODULE
struct pred_entry *pred_log_upd_clause_erase PredLogUpdClauseErase MkPred FunctorDoLogUpdClauseErase PROLOG_MODULE
struct pred_entry *pred_log_upd_clause0	PredLogUpdClause0	MkPred FunctorDoLogUpdClause PROLOG_MODULE
struct pred_entry *pred_static_clause	PredStaticClause	MkPred FunctorDoStaticClause PROLOG_MODULE
struct pred_entry *pred_throw		PredThrow		MkPred FunctorThrow PROLOG_MODULE
struct pred_entry *pred_handle_throw	PredHandleThrow		MkPred FunctorHandleThrow PROLOG_MODULE
struct pred_entry *pred_is		PredIs			MkPred FunctorIs PROLOG_MODULE
struct pred_entry *pred_safe_call_cleanup PredSafeCallCleanup	MkPred FunctorSafeCallCleanup PROLOG_MODULE
struct pred_entry *pred_restore_regs	PredRestoreRegs		MkPred FunctorRestoreRegs PROLOG_MODULE
#ifdef YAPOR
struct pred_entry *pred_getwork		PredGetwork		MkPred AtomGetwork 0 PROLOG_MODULE
struct pred_entry *pred_getwork_seq	PredGetworkSeq		MkPred AtomGetworkSeq 0 PROLOG_MODULE
#endif /* YAPOR */

/* low-level tracer */
#ifdef LOW_LEVEL_TRACER
int		yap_do_low_level_trace	Yap_do_low_level_trace	=FALSE void
#if defined(YAPOR) || defined(THREADS)
lockvar		low_level_trace_lock	Yap_low_level_trace_lock MkLock
#endif
#endif

/* code management info */
UInt		clause_space		Yap_ClauseSpace		=0 void
UInt		index_space_Tree	Yap_IndexSpace_Tree	=0 void
UInt		index_space_EXT		Yap_IndexSpace_EXT	=0 void
UInt		index_space_SW		Yap_IndexSpace_SW	=0 void
UInt		lu_clause_space		Yap_LUClauseSpace	=0 void
UInt		lu_index_space_Tree	Yap_LUIndexSpace_Tree	=0 void
UInt		lu_index_space_CP	Yap_LUIndexSpace_CP	=0 void
UInt		lu_index_space_EXT	Yap_LUIndexSpace_EXT	=0 void
UInt		lu_index_space_SW	Yap_LUIndexSpace_SW	=0 void

/* static code: may be shared by many predicate or may be used for meta-execution */
yamop		comma_code[5]		COMMA_CODE		void void
yamop		dummycode[1]		DUMMYCODE		MkInstE _op_fail
yamop		failcode[1]		FAILCODE		MkInstE _op_fail
yamop		nocode[1]		NOCODE			MkInstE _Nstop

yamop		env_for_trustfail[2]	ENV_FOR_TRUSTFAIL	InitEnvInst(ENV_FOR_TRUSTFAIL,&TRUSTFAILCODE,_trust_fail,PredFail) RestoreEnvInst(ENV_FOR_TRUSTFAIL,&TRUSTFAILCODE,_trust_fail,PredFail)
yamop		*trustfailcode		TRUSTFAILCODE		void
yamop		env_for_yescode[2]	ENV_FOR_YESCODE		InitEnvInst(ENV_FOR_YESCODE,&YESCODE,_Ystop,PredFail) RestoreEnvInst(ENV_FOR_YESCODE,&YESCODE,_Ystop,PredFail)
yamop		*yescode		YESCODE			void

  yamop		rtrycode[1]		RTRYCODE		InitOtaplInst(RTRYCODE,_retry_and_mark,PredFail) RestoreOtaplInst(RTRYCODE,_retry_and_mark,PredFail)

#ifdef BEAM
yamop		beam_retry_code[1]	BEAM_RETRY_CODE		MkInstE _beam_retry_code
#endif /* BEAM */
#ifdef YAPOR
yamop		getwork_code[1]		GETWORK			InitOtaplInst(GETWORK,_getwork,PredGetwork) RestoreOtaplInst(GETWORK,_getwork,PredGetwork)
yamop		getwork_seq_code[1]	GETWORK_SEQ		InitOtaplInst(GETWORK_SEQ,_getwork_seq,PredGetworkSeq) RestoreOtaplInst(GETWORK_SEQ,_getwork_seq,PredGetworkSeq)
yamop		getwork_first_time[1]	GETWORK_FIRST_TIME	MkInstE _getwork_first_time
#endif /* YAPOR */
#ifdef TABLING
yamop		table_load_answer_code[1] LOAD_ANSWER		InitOtaplInst(LOAD_ANSWER,_table_load_answer,PredFail) RestoreOtaplInst(LOAD_ANSWER,_table_load_answer,PredFail)
yamop		table_try_answer_code[1] TRY_ANSWER		InitOtaplInst(TRY_ANSWER,_table_try_answer,PredFail) RestoreOtaplInst(TRY_ANSWER,_table_try_answer,PredFail)
yamop		table_answer_resolution_code[1]	ANSWER_RESOLUTION InitOtaplInst(ANSWER_RESOLUTION,_table_answer_resolution,PredFail) RestoreOtaplInst(ANSWER_RESOLUTION,_table_answer_resolution,PredFail)
yamop		table_completion_code[1]	COMPLETION	InitOtaplInst(COMPLETION,_table_completion,PredFail) RestoreOtaplInst(COMPLETION,_table_completion,PredFail)
#ifdef THREADS_CONSUMER_SHARING
yamop           table_answer_resolution_completion_code[1]      ANSWER_RESOLUTION_COMPLETION InitOtaplInst(ANSWER_RESOLUTION_COMPLETION,_table_answer_resolution_completion,PredFail) RestoreOtaplInst(ANSWER_RESOLUTION_COMPLETION,_table_answer_resolution_completion,PredFail)
#endif /* THREADS_CONSUMER_SHARING */
#endif /* TABLING */

/*  */
/*    PREG just before we enter $spy. We use that to find out the clause which  */
/*    was calling the debugged goal.  */
/*  */
yamop		*debugger_p_before_spy	P_before_spy		=NULL PtoOpAdjust 

/* support recorded_k  */
yamop		*retry_recordedp_code	RETRY_C_RECORDEDP_CODE	=NULL PtoOpAdjust 
yamop		*retry_recorded_k_code	RETRY_C_RECORDED_K_CODE =NULL PtoOpAdjust 

/* compiler flags */
int		system_profiling	PROFILING		=FALSE void
int		system_call_counting	CALL_COUNTING		=FALSE void
int		system_pred_goal_expansion_all	PRED_GOAL_EXPANSION_ALL	=FALSE void
int		system_pred_goal_expansion_func	PRED_GOAL_EXPANSION_FUNC =FALSE void
int		system_pred_goal_expansion_on	PRED_GOAL_EXPANSION_ON	=FALSE void
int		compiler_optimizer_on	optimizer_on		=TRUE void
int		compiler_compile_mode	compile_mode		=0 void
int		compiler_profiling	profiling		=FALSE void
int		compiler_call_counting	call_counting		=FALSE void
/********* whether we should try to compile array references ******************/
int		compiler_compile_arrays	compile_arrays		=FALSE void

/* DBTerms: pre-compiled ground terms */
#if defined(YAPOR) || defined(THREADS)
lockvar		dbterms_list_lock	DBTermsListLock		MkLock
#endif
struct dbterm_list *dbterms_list	DBTermsList		=NULL RestoreDBTermsList()

/* JITI support */
yamop		*expand_clauses_first	ExpandClausesFirst	=NULL void
yamop		*expand_clauses_last	ExpandClausesLast	=NULL RestoreExpandList()
UInt		expand_clauses		Yap_ExpandClauses	=0 void
#if defined(YAPOR) || defined(THREADS)
lockvar		expand_clauses_list_lock ExpandClausesListLock	MkLock
lockvar		op_list_lock		OpListLock		MkLock
#endif
/* instrumentation */
#ifdef DEBUG
UInt		new_cps			Yap_NewCps		=0L void
UInt		live_cps		Yap_LiveCps		=0L void
UInt		dirty_cps		Yap_DirtyCps		=0L void
UInt		freed_cps		Yap_FreedCps		=0L void
#endif
UInt		expand_clauses_sz	Yap_expand_clauses_sz	=0L void

/* UDI support */
struct udi_info *udi_control_blocks	UdiControlBlocks	=NULL RestoreUdiControlBlocks()

/* data-base statistics */
/* system boots in compile mode */
Int		static_predicates_marked STATIC_PREDICATES_MARKED   =FALSE void


/* Internal Database */
Prop		*IntKeys		INT_KEYS		=NULL RestoreIntKeys()
Prop		*IntLUKeys		INT_LU_KEYS		=NULL RestoreIntLUKeys()
Prop		*IntBBKeys		INT_BB_KEYS		=NULL RestoreIntBBKeys()

/* Internal Database Statistics */
UInt		int_keys_size		INT_KEYS_SIZE		=INT_KEYS_DEFAULT_SIZE void
UInt		int_keys_timestamp	INT_KEYS_TIMESTAMP	=0L void
UInt		int_bb_keys_size	INT_BB_KEYS_SIZE	=INT_KEYS_DEFAULT_SIZE void

/* Internal Data-Base Control */
int		update_mode		UPDATE_MODE	        =UPDATE_MODE_LOGICAL void

/* nasty IDB stuff */
struct DB_STRUCT *db_erased_marker	DBErasedMarker		InitDBErasedMarker() RestoreDBErasedMarker() 
struct logic_upd_clause *logdb_erased_marker LogDBErasedMarker	InitLogDBErasedMarker() RestoreLogDBErasedMarker() 

/* Dead clauses and IDB entries */
struct static_clause *dead_static_clauses DeadStaticClauses	=NULL RestoreDeadStaticClauses()
struct static_mega_clause *dead_mega_clauses DeadMegaClauses	=NULL RestoreDeadMegaClauses()
struct static_index *dead_static_indices DeadStaticIndices	=NULL RestoreDeadStaticIndices()
struct logic_upd_clause *db_erased_list	DBErasedList		=NULL RestoreDBErasedList()
struct logic_upd_index *db_erased_ilist DBErasedIList		=NULL RestoreDBErasedIList()
#if defined(YAPOR) || defined(THREADS)
lockvar  dead_static_clauses_lock	DeadStaticClausesLock	MkLock
lockvar  dead_mega_clauses_lock		DeadMegaClausesLock	MkLock
lockvar  dead_static_indices_lock	DeadStaticIndicesLock	MkLock
#endif

#ifdef COROUTINING
/* number of attribute modules */
int		num_of_atts		NUM_OF_ATTS		=1	void
/* initialised by memory allocator */
UInt		atts_size		Yap_AttsSize		void void
#endif

/* YAP control flags */
Int		yap_flags_field[NUMBER_OF_YAP_FLAGS] yap_flags InitFlags() void

/* Operators */
struct operator_entry	*op_list	OpList			=NULL OpListAdjust

/* Input/Output */

/* stream array */
struct stream_desc *yap_streams		Stream			=NULL RestoreStreams()

/* stream aliases */
UInt		n_of_file_aliases	NOfFileAliases		=0 void
UInt		sz_of_file_aliases	SzOfFileAliases		=0 void
struct AliasDescS *file_aliases		FileAliases		=NULL RestoreAliases()

/* prompting */
Atom		atprompt		AtPrompt		=AtomNil AtomAdjust
char		_prompt[MAX_PROMPT]	YapPrompt		void void

/* ISO char conversion: I will make no comments */
char		*char_conversion_table	CharConversionTable	=NULL CodeCharPAdjust
char		*char_conversion_table2	CharConversionTable2	=NULL CodeCharPAdjust

/* what to do when the parser gives an error: should be thread bound?  */
int		parser_error_style	ParserErrorStyle	=EXCEPTION_ON_PARSER_ERROR void

/* library location.  */
char		*yap_lib_dir		Yap_LibDir		=NULL CodeCharPAdjust

/* time */
void		*last_wtime		LastWtimePtr		=NULL CodeVoidPAdjust

/* foreign code loaded */
struct ForeignLoadItem 	*foreign_code_loaded	ForeignCodeLoaded	=NULL RestoreForeignCode()
ADDR		foreign_code_base	ForeignCodeBase		=NULL void
ADDR		foreign_code_top	ForeignCodeTop		=NULL void
ADDR		foreign_code_max	ForeignCodeMax		=NULL void

/* recorded terms */
struct record_list *yap_records		Yap_Records		=NULL		RestoreYapRecords()

/* SWI atoms and functors */
Atom		swi_atoms[N_SWI_ATOMS]	SWI_Atoms		InitSWIAtoms() RestoreSWIAtoms()
Functor		swi_functors[N_SWI_FUNCTORS] SWI_Functors	void void
struct swi_reverse_hash	swi_reverse_hash[N_SWI_HASH] SWI_ReverseHash	void void

/* SWI blobs */
struct PL_blob_t	*swi_blob_types	SWI_BlobTypes		=NULL		RestoreSWIBlobTypes()
struct AtomEntryStruct	*swi_blobs	SWI_Blobs		=NULL		RestoreSWIBlobs()
UInt   			nofblobs	NOfBlobs		=0
UInt   			nofblobsmax	NOfBlobsMax		=256
#if defined(YAPOR) || defined(THREADS)
lockvar         swi_blobs_lock		SWI_Blobs_Lock		MkLock
#endif
