2001-04-09 20:54:03 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* YAP Prolog %W% %G% *
|
|
|
|
* *
|
|
|
|
* Yap Prolog was developed at NCCUP - Universidade do Porto *
|
|
|
|
* *
|
|
|
|
* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
|
|
|
|
* *
|
|
|
|
**************************************************************************
|
|
|
|
* *
|
|
|
|
* File: compile.h *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
|
|
|
* comments: compiler data structures and routines *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
|
2002-01-01 05:26:25 +00:00
|
|
|
/* consult stack management */
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
/* virtual machine instruction op-codes */
|
|
|
|
typedef enum compiler_op {
|
|
|
|
nop_op,
|
|
|
|
get_var_op,
|
|
|
|
put_var_op,
|
|
|
|
get_val_op,
|
|
|
|
put_val_op,
|
|
|
|
get_atom_op,
|
|
|
|
put_atom_op,
|
|
|
|
get_num_op,
|
|
|
|
put_num_op,
|
|
|
|
get_float_op,
|
|
|
|
put_float_op,
|
2007-11-06 17:02:13 +00:00
|
|
|
get_dbterm_op,
|
|
|
|
put_dbterm_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
get_longint_op,
|
|
|
|
put_longint_op,
|
|
|
|
get_bigint_op,
|
|
|
|
put_bigint_op,
|
|
|
|
get_list_op,
|
|
|
|
put_list_op,
|
|
|
|
get_struct_op,
|
|
|
|
put_struct_op,
|
|
|
|
put_unsafe_op,
|
|
|
|
unify_var_op,
|
|
|
|
write_var_op,
|
|
|
|
unify_val_op,
|
|
|
|
write_val_op,
|
|
|
|
unify_atom_op,
|
|
|
|
write_atom_op,
|
|
|
|
unify_num_op,
|
|
|
|
write_num_op,
|
|
|
|
unify_float_op,
|
|
|
|
write_float_op,
|
2007-11-06 17:02:13 +00:00
|
|
|
unify_dbterm_op,
|
|
|
|
write_dbterm_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
unify_longint_op,
|
|
|
|
write_longint_op,
|
|
|
|
unify_bigint_op,
|
|
|
|
write_bigint_op,
|
|
|
|
unify_list_op,
|
|
|
|
write_list_op,
|
|
|
|
unify_struct_op,
|
|
|
|
write_struct_op,
|
|
|
|
write_unsafe_op,
|
2009-03-05 16:12:21 +00:00
|
|
|
unify_local_op,
|
|
|
|
write_local_op,
|
|
|
|
unify_last_list_op,
|
|
|
|
write_last_list_op,
|
|
|
|
unify_last_struct_op,
|
|
|
|
write_last_struct_op,
|
|
|
|
unify_last_var_op,
|
|
|
|
unify_last_val_op,
|
|
|
|
unify_last_local_op,
|
|
|
|
unify_last_atom_op,
|
|
|
|
unify_last_num_op,
|
|
|
|
unify_last_float_op,
|
|
|
|
unify_last_dbterm_op,
|
|
|
|
unify_last_longint_op,
|
|
|
|
unify_last_bigint_op,
|
2010-08-02 13:04:30 +01:00
|
|
|
ensure_space_op,
|
2009-04-07 23:55:16 +01:00
|
|
|
native_op,
|
2009-03-05 16:12:21 +00:00
|
|
|
f_var_op,
|
|
|
|
f_val_op,
|
|
|
|
f_0_op,
|
|
|
|
align_float_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
fail_op,
|
|
|
|
cut_op,
|
|
|
|
cutexit_op,
|
|
|
|
allocate_op,
|
|
|
|
deallocate_op,
|
|
|
|
tryme_op,
|
|
|
|
jump_op,
|
2003-04-30 18:46:05 +01:00
|
|
|
jumpi_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
procceed_op,
|
|
|
|
call_op,
|
|
|
|
execute_op,
|
|
|
|
safe_call_op,
|
|
|
|
label_op,
|
|
|
|
name_op,
|
|
|
|
pop_op,
|
|
|
|
retryme_op,
|
|
|
|
trustme_op,
|
|
|
|
either_op,
|
|
|
|
orelse_op,
|
|
|
|
orlast_op,
|
|
|
|
push_or_op,
|
|
|
|
pushpop_or_op,
|
|
|
|
pop_or_op,
|
|
|
|
save_b_op,
|
2003-12-27 00:38:53 +00:00
|
|
|
commit_b_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
patch_b_op,
|
|
|
|
try_op,
|
|
|
|
retry_op,
|
|
|
|
trust_op,
|
2003-04-30 18:46:05 +01:00
|
|
|
try_in_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
jump_v_op,
|
2003-10-28 01:16:03 +00:00
|
|
|
jump_nv_op,
|
2003-04-30 18:46:05 +01:00
|
|
|
cache_arg_op,
|
|
|
|
cache_sub_arg_op,
|
2009-02-12 21:35:31 +00:00
|
|
|
user_switch_op,
|
2003-04-30 18:46:05 +01:00
|
|
|
switch_on_type_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
switch_c_op,
|
|
|
|
if_c_op,
|
|
|
|
switch_f_op,
|
|
|
|
if_f_op,
|
|
|
|
if_not_op,
|
2003-08-27 14:37:10 +01:00
|
|
|
index_dbref_op,
|
|
|
|
index_blob_op,
|
2009-02-09 21:56:40 +00:00
|
|
|
index_long_op,
|
2003-10-28 01:16:03 +00:00
|
|
|
if_nonvar_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
save_pair_op,
|
|
|
|
save_appl_op,
|
|
|
|
mark_initialised_pvars_op,
|
|
|
|
mark_live_regs_op,
|
|
|
|
fetch_args_vv_op,
|
|
|
|
fetch_args_cv_op,
|
|
|
|
fetch_args_vc_op,
|
2006-11-06 18:35:05 +00:00
|
|
|
fetch_args_iv_op,
|
|
|
|
fetch_args_vi_op,
|
2009-06-22 18:40:55 +01:00
|
|
|
enter_profiling_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
retry_profiled_op,
|
2009-06-22 18:40:55 +01:00
|
|
|
count_call_op,
|
2002-09-03 15:28:09 +01:00
|
|
|
count_retry_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
restore_tmps_op,
|
|
|
|
restore_tmps_and_skip_op,
|
2003-09-15 02:25:29 +01:00
|
|
|
enter_lu_op,
|
2001-04-09 20:54:03 +01:00
|
|
|
empty_call_op,
|
|
|
|
#ifdef YAPOR
|
|
|
|
sync_op,
|
|
|
|
#endif /* YAPOR */
|
2003-11-05 18:55:03 +00:00
|
|
|
#ifdef TABLING
|
|
|
|
table_new_answer_op,
|
2003-12-18 16:38:40 +00:00
|
|
|
table_try_single_op,
|
2003-11-05 18:55:03 +00:00
|
|
|
#endif /* TABLING */
|
|
|
|
#ifdef TABLING_INNER_CUTS
|
|
|
|
clause_with_cut_op,
|
|
|
|
#endif /* TABLING_INNER_CUTS */
|
2005-09-08 22:59:58 +01:00
|
|
|
#ifdef BEAM
|
|
|
|
run_op,
|
|
|
|
body_op,
|
|
|
|
endgoal_op,
|
|
|
|
try_me_op,
|
|
|
|
retry_me_op,
|
|
|
|
trust_me_op,
|
|
|
|
only_1_clause_op,
|
|
|
|
create_first_box_op,
|
|
|
|
create_box_op,
|
|
|
|
create_last_box_op,
|
|
|
|
remove_box_op,
|
|
|
|
remove_last_box_op,
|
|
|
|
prepare_tries,
|
|
|
|
std_base_op,
|
|
|
|
direct_safe_call_op,
|
|
|
|
commit_op,
|
|
|
|
skip_while_var_op,
|
|
|
|
wait_while_var_op,
|
|
|
|
force_wait_op,
|
|
|
|
write_op,
|
|
|
|
equal_op,
|
|
|
|
exit_op,
|
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
fetch_args_for_bccall,
|
|
|
|
bccall_op,
|
2008-12-09 12:54:27 +00:00
|
|
|
blob_op,
|
|
|
|
label_ctl_op
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef SFUNC
|
|
|
|
,
|
|
|
|
get_s_f_op,
|
|
|
|
put_s_f_op,
|
|
|
|
unify_s_f_op,
|
|
|
|
write_s_f_op,
|
|
|
|
unify_s_var_op,
|
|
|
|
write_s_var_op,
|
|
|
|
unify_s_val_op,
|
|
|
|
write_s_val_op,
|
|
|
|
unify_s_a_op,
|
|
|
|
write_s_a_op,
|
|
|
|
get_s_end_op,
|
|
|
|
put_s_end_op,
|
|
|
|
unify_s_end_op,
|
|
|
|
write_s_end_op,
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} compiler_vm_op;
|
|
|
|
|
|
|
|
typedef struct PSEUDO {
|
|
|
|
struct PSEUDO *nextInst;
|
|
|
|
enum compiler_op op;
|
|
|
|
CELL rnd1;
|
|
|
|
union {
|
|
|
|
Int oprnd2;
|
2013-11-04 01:04:52 +00:00
|
|
|
#if MIN_ARRAY == 0
|
|
|
|
CELL opseqt[MIN_ARRAY];
|
|
|
|
#else
|
2001-04-09 20:54:03 +01:00
|
|
|
CELL opseqt[1];
|
2013-11-04 01:04:52 +00:00
|
|
|
#endif
|
2001-04-09 20:54:03 +01:00
|
|
|
} ops;
|
|
|
|
} PInstr;
|
|
|
|
|
|
|
|
#define arnds ops.opseqt
|
2003-01-29 14:47:17 +00:00
|
|
|
#define rnd2 ops.oprnd2
|
2001-04-09 20:54:03 +01:00
|
|
|
#define rnd3 ops.opseqt[1]
|
2003-01-29 14:47:17 +00:00
|
|
|
#define rnd4 ops.opseqt[2]
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
typedef struct VENTRY {
|
2009-03-05 16:12:21 +00:00
|
|
|
CELL SelfOfVE;
|
|
|
|
Term AdrsOfVE;
|
|
|
|
Int KindOfVE;
|
|
|
|
CELL NoOfVE;
|
|
|
|
PInstr *FirstOpForV;
|
|
|
|
PInstr *LastOpForV;
|
|
|
|
BITS16 AgeOfVE;
|
|
|
|
BITS16 BranchOfVE;
|
|
|
|
BITS16 LastBranchOfVE;
|
|
|
|
BITS16 FirstOfVE;
|
|
|
|
BITS16 RCountOfVE;
|
|
|
|
BITS16 FlagsOfVE;
|
|
|
|
struct VENTRY *NextOfVE;
|
|
|
|
} Ventry;
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
typedef struct CEXPENTRY {
|
|
|
|
Term TermOfCE;
|
|
|
|
PInstr *CodeOfCE;
|
|
|
|
Term VarOfCE;
|
2005-07-06 16:10:18 +01:00
|
|
|
struct CEXPENTRY *NextCE;
|
2001-04-09 20:54:03 +01:00
|
|
|
} CExpEntry;
|
|
|
|
|
2007-11-06 17:02:13 +00:00
|
|
|
#define COMPILER_ERR_BOTCH 1
|
|
|
|
#define OUT_OF_HEAP_BOTCH 2
|
|
|
|
#define OUT_OF_STACK_BOTCH 3
|
|
|
|
#define OUT_OF_TEMPS_BOTCH 4
|
|
|
|
#define OUT_OF_AUX_BOTCH 5
|
|
|
|
#define OUT_OF_TRAIL_BOTCH 6
|
|
|
|
|
2004-01-23 02:23:51 +00:00
|
|
|
|
|
|
|
typedef struct intermediates {
|
|
|
|
char *freep;
|
|
|
|
char *freep0;
|
2010-03-31 15:51:18 +01:00
|
|
|
struct mem_blk *blks;
|
2010-04-15 22:23:54 +01:00
|
|
|
char *blk_cur, *blk_top;
|
2004-01-23 02:23:51 +00:00
|
|
|
struct PSEUDO *cpc;
|
|
|
|
struct PSEUDO *CodeStart;
|
|
|
|
struct PSEUDO *icpc;
|
|
|
|
struct PSEUDO *BlobsStart;
|
2007-11-06 17:02:13 +00:00
|
|
|
struct dbterm_list *dbterml;
|
2010-03-30 09:16:20 +01:00
|
|
|
Int *label_offset;
|
2004-01-23 02:23:51 +00:00
|
|
|
Int *uses;
|
|
|
|
Term *contents;
|
|
|
|
struct pred_entry *CurrentPred;
|
2010-12-16 01:22:10 +00:00
|
|
|
sigjmp_buf CompilerBotch;
|
2004-04-14 20:10:40 +01:00
|
|
|
yamop *code_addr;
|
|
|
|
yamop *expand_block;
|
2006-05-16 19:37:31 +01:00
|
|
|
UInt i_labelno;
|
2008-12-09 12:54:27 +00:00
|
|
|
UInt exception_handler, success_handler, failure_handler;
|
2004-04-16 20:27:31 +01:00
|
|
|
/* for expanding code */
|
2006-12-27 01:32:38 +00:00
|
|
|
yamop **current_try_lab, **current_trust_lab;
|
2006-10-10 15:08:17 +01:00
|
|
|
yamop *try_instructions;
|
2010-04-15 22:23:54 +01:00
|
|
|
struct StructClauseDef *cls;
|
2011-07-05 07:28:28 +01:00
|
|
|
int clause_has_cut;
|
2012-05-28 20:40:12 +01:00
|
|
|
UInt term_depth, last_index_at_depth;
|
|
|
|
UInt last_index_new_depth, last_depth_size;
|
2010-04-15 22:23:54 +01:00
|
|
|
/* for expanding code */
|
2004-04-16 20:27:31 +01:00
|
|
|
union {
|
|
|
|
struct static_index *si;
|
|
|
|
struct logic_upd_index *lui;
|
|
|
|
} current_cl;
|
2004-01-23 02:23:51 +00:00
|
|
|
} CIntermediates;
|
|
|
|
|
2008-12-09 12:54:27 +00:00
|
|
|
typedef enum special_label_id_enum {
|
|
|
|
SPECIAL_LABEL_SUCCESS = 0,
|
|
|
|
SPECIAL_LABEL_FAILURE = 1,
|
|
|
|
SPECIAL_LABEL_EXCEPTION = 2
|
|
|
|
} special_label_id;
|
|
|
|
|
|
|
|
typedef enum special_label_op_enum {
|
|
|
|
SPECIAL_LABEL_INIT = 0,
|
|
|
|
SPECIAL_LABEL_SET = 1,
|
|
|
|
SPECIAL_LABEL_CLEAR =2
|
|
|
|
} special_label_op;
|
|
|
|
|
2001-04-09 20:54:03 +01:00
|
|
|
#define SafeVar 0x01
|
|
|
|
#define PermFlag 0x02
|
|
|
|
#define GlobalVal 0x04
|
|
|
|
#define OnHeadFlag 0x08
|
|
|
|
#define NonVoid 0x10
|
|
|
|
#define BranchVar 0x20
|
|
|
|
#define OnLastGoal 0x40
|
|
|
|
|
|
|
|
#define MaskVarClass 0x0f000000L
|
|
|
|
#define MaskVarAdrs 0x00ffffffL
|
|
|
|
#define Unassigned 0x00000000L
|
|
|
|
#define VoidVar 0x01000000L
|
|
|
|
#define TempVar 0x02000000L
|
|
|
|
#define PermVar 0x03000000L
|
|
|
|
|
2005-07-06 16:10:18 +01:00
|
|
|
#define save_b_flag 0x10000
|
|
|
|
#define commit_b_flag 0x10001
|
|
|
|
#define save_appl_flag 0x10002
|
|
|
|
#define save_pair_flag 0x10004
|
|
|
|
#define f_flag 0x10008
|
|
|
|
#define bt1_flag 0x10010
|
|
|
|
#define bt2_flag 0x10020
|
|
|
|
#define patch_b_flag 0x10040
|
|
|
|
#define init_v_flag 0x10080
|
2001-04-09 20:54:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
#define Zero 0
|
|
|
|
#define One 1
|
|
|
|
#define Two 2
|
|
|
|
|
|
|
|
|
2013-04-25 23:15:04 +01:00
|
|
|
yamop *Yap_assemble(int,Term,struct pred_entry *,int, struct intermediates *, UInt);
|
|
|
|
void Yap_emit(compiler_vm_op,Int,CELL, struct intermediates *);
|
|
|
|
void Yap_emit_3ops(compiler_vm_op,CELL,CELL,CELL, struct intermediates *);
|
|
|
|
void Yap_emit_4ops(compiler_vm_op,CELL,CELL,CELL,CELL, struct intermediates *);
|
|
|
|
CELL *Yap_emit_extra_size(compiler_vm_op,CELL,int, struct intermediates *);
|
|
|
|
char *Yap_AllocCMem(UInt, struct intermediates *);
|
|
|
|
void Yap_ReleaseCMem(struct intermediates *);
|
|
|
|
int Yap_is_a_test_pred(Term, Term);
|
|
|
|
void Yap_bip_name(Int, char *);
|
2001-04-09 20:54:03 +01:00
|
|
|
#ifdef DEBUG
|
2013-04-25 23:15:04 +01:00
|
|
|
void Yap_ShowCode(struct intermediates *);
|
2001-04-09 20:54:03 +01:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2002-09-03 15:28:09 +01:00
|
|
|
|