1 /*************************************************************************
2 *									 *
3 *	 YAP Prolog  %W% %G%						 *
4 *									 *
5 *	Yap Prolog was developed at NCCUP - Universidade do Porto	 *
6 *									 *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997	 *
8 *									 *
9 **************************************************************************
10 *									 *
11 * File:		compile.h						 *
12 * Last rev:								 *
13 * mods:									 *
14 * comments:	compiler data structures and routines			 *
15 *									 *
16 *************************************************************************/
17 
18 /* consult stack management */
19 
20 /* virtual machine instruction op-codes					*/
21 typedef enum compiler_op {
22   nop_op,
23   get_var_op,
24   put_var_op,
25   get_val_op,
26   put_val_op,
27   get_atom_op,
28   put_atom_op,
29   get_num_op,
30   put_num_op,
31   get_float_op,
32   put_float_op,
33   get_dbterm_op,
34   put_dbterm_op,
35   get_longint_op,
36   put_longint_op,
37   get_bigint_op,
38   put_bigint_op,
39   get_list_op,
40   put_list_op,
41   get_struct_op,
42   put_struct_op,
43   put_unsafe_op,
44   unify_var_op,
45   write_var_op,
46   unify_val_op,
47   write_val_op,
48   unify_atom_op,
49   write_atom_op,
50   unify_num_op,
51   write_num_op,
52   unify_float_op,
53   write_float_op,
54   unify_dbterm_op,
55   write_dbterm_op,
56   unify_longint_op,
57   write_longint_op,
58   unify_bigint_op,
59   write_bigint_op,
60   unify_list_op,
61   write_list_op,
62   unify_struct_op,
63   write_struct_op,
64   write_unsafe_op,
65   unify_local_op,
66   write_local_op,
67   unify_last_list_op,
68   write_last_list_op,
69   unify_last_struct_op,
70   write_last_struct_op,
71   unify_last_var_op,
72   unify_last_val_op,
73   unify_last_local_op,
74   unify_last_atom_op,
75   unify_last_num_op,
76   unify_last_float_op,
77   unify_last_dbterm_op,
78   unify_last_longint_op,
79   unify_last_bigint_op,
80   ensure_space_op,
81   native_op,
82   f_var_op,
83   f_val_op,
84   f_0_op,
85   align_float_op,
86   fail_op,
87   cut_op,
88   cutexit_op,
89   allocate_op,
90   deallocate_op,
91   tryme_op,
92   jump_op,
93   jumpi_op,
94   procceed_op,
95   call_op,
96   execute_op,
97   safe_call_op,
98   label_op,
99   name_op,
100   pop_op,
101   retryme_op,
102   trustme_op,
103   either_op,
104   orelse_op,
105   orlast_op,
106   push_or_op,
107   pushpop_or_op,
108   pop_or_op,
109   save_b_op,
110   commit_b_op,
111   patch_b_op,
112   try_op,
113   retry_op,
114   trust_op,
115   try_in_op,
116   jump_v_op,
117   jump_nv_op,
118   cache_arg_op,
119   cache_sub_arg_op,
120   user_switch_op,
121   switch_on_type_op,
122   switch_c_op,
123   if_c_op,
124   switch_f_op,
125   if_f_op,
126   if_not_op,
127   index_dbref_op,
128   index_blob_op,
129   index_long_op,
130   if_nonvar_op,
131   save_pair_op,
132   save_appl_op,
133   mark_initialised_pvars_op,
134   mark_live_regs_op,
135   fetch_args_vv_op,
136   fetch_args_cv_op,
137   fetch_args_vc_op,
138   fetch_args_iv_op,
139   fetch_args_vi_op,
140   enter_profiling_op,
141   retry_profiled_op,
142   count_call_op,
143   count_retry_op,
144   restore_tmps_op,
145   restore_tmps_and_skip_op,
146   enter_lu_op,
147   empty_call_op,
148 #ifdef YAPOR
149   sync_op,
150 #endif /* YAPOR */
151 #ifdef TABLING
152   table_new_answer_op,
153   table_try_single_op,
154 #endif /* TABLING */
155 #ifdef TABLING_INNER_CUTS
156   clause_with_cut_op,
157 #endif /* TABLING_INNER_CUTS */
158 #ifdef BEAM
159   run_op,
160   body_op,
161   endgoal_op,
162   try_me_op,
163   retry_me_op,
164   trust_me_op,
165   only_1_clause_op,
166   create_first_box_op,
167   create_box_op,
168   create_last_box_op,
169   remove_box_op,
170   remove_last_box_op,
171   prepare_tries,
172   std_base_op,
173   direct_safe_call_op,
174   commit_op,
175   skip_while_var_op,
176   wait_while_var_op,
177   force_wait_op,
178   write_op,
179   equal_op,
180   exit_op,
181 #endif
182   fetch_args_for_bccall,
183   bccall_op,
184   blob_op,
185   label_ctl_op
186 #ifdef SFUNC
187   ,
188   get_s_f_op,
189   put_s_f_op,
190   unify_s_f_op,
191   write_s_f_op,
192   unify_s_var_op,
193   write_s_var_op,
194   unify_s_val_op,
195   write_s_val_op,
196   unify_s_a_op,
197   write_s_a_op,
198   get_s_end_op,
199   put_s_end_op,
200   unify_s_end_op,
201   write_s_end_op,
202 #endif
203 
204 } compiler_vm_op;
205 
206 typedef struct PSEUDO {
207 	struct PSEUDO *nextInst;
208 	enum compiler_op op;
209         CELL rnd1;
210 	union {
211 	    Int   oprnd2;
212 	    CELL opseqt[1];
213 	} ops;
214     } PInstr;
215 
216 #define arnds ops.opseqt
217 #define rnd2  ops.oprnd2
218 #define rnd3  ops.opseqt[1]
219 #define rnd4  ops.opseqt[2]
220 
221 typedef struct VENTRY {
222   CELL SelfOfVE;
223   Term AdrsOfVE;
224   Int  KindOfVE;
225   CELL NoOfVE;
226   PInstr *FirstOpForV;
227   PInstr *LastOpForV;
228   BITS16 AgeOfVE;
229   BITS16 BranchOfVE;
230   BITS16 LastBranchOfVE;
231   BITS16 FirstOfVE;
232   BITS16 RCountOfVE;
233   BITS16 FlagsOfVE;
234   struct VENTRY *NextOfVE;
235 } Ventry;
236 
237 typedef struct CEXPENTRY {
238          Term   TermOfCE;
239 	 PInstr *CodeOfCE;
240 	 Term   VarOfCE;
241 	 struct CEXPENTRY *NextCE;
242        } CExpEntry;
243 
244 #define COMPILER_ERR_BOTCH 1
245 #define OUT_OF_HEAP_BOTCH  2
246 #define OUT_OF_STACK_BOTCH 3
247 #define OUT_OF_TEMPS_BOTCH 4
248 #define OUT_OF_AUX_BOTCH   5
249 #define OUT_OF_TRAIL_BOTCH 6
250 
251 
252 typedef struct intermediates {
253   char *freep;
254   char *freep0;
255   struct mem_blk *blks;
256   char *blk_cur, *blk_top;
257   struct PSEUDO *cpc;
258   struct PSEUDO *CodeStart;
259   struct PSEUDO *icpc;
260   struct PSEUDO *BlobsStart;
261   struct dbterm_list *dbterml;
262   Int  *label_offset;
263   Int  *uses;
264   Term *contents;
265   struct pred_entry *CurrentPred;
266   sigjmp_buf CompilerBotch;
267   yamop *code_addr;
268   yamop *expand_block;
269   UInt  i_labelno;
270   UInt  exception_handler, success_handler, failure_handler;
271   /* for expanding code */
272   yamop **current_try_lab, **current_trust_lab;
273   yamop *try_instructions;
274   struct StructClauseDef *cls;
275   /* for expanding code */
276   union {
277     struct static_index *si;
278     struct logic_upd_index *lui;
279   } current_cl;
280 } CIntermediates;
281 
282 typedef enum special_label_id_enum {
283   SPECIAL_LABEL_SUCCESS = 0,
284   SPECIAL_LABEL_FAILURE = 1,
285   SPECIAL_LABEL_EXCEPTION = 2
286 } special_label_id;
287 
288 typedef enum special_label_op_enum {
289   SPECIAL_LABEL_INIT = 0,
290   SPECIAL_LABEL_SET = 1,
291   SPECIAL_LABEL_CLEAR =2
292 } special_label_op;
293 
294 #define	 SafeVar	0x01
295 #define  PermFlag	0x02
296 #define  GlobalVal	0x04
297 #define  OnHeadFlag	0x08
298 #define  NonVoid	0x10
299 #define  BranchVar      0x20
300 #define  OnLastGoal     0x40
301 
302 #define  MaskVarClass   0x0f000000L
303 #define  MaskVarAdrs    0x00ffffffL
304 #define	 Unassigned	0x00000000L
305 #define	 VoidVar	0x01000000L
306 #define	 TempVar	0x02000000L
307 #define	 PermVar	0x03000000L
308 
309 #define save_b_flag	   0x10000
310 #define commit_b_flag	   0x10001
311 #define save_appl_flag	   0x10002
312 #define save_pair_flag	   0x10004
313 #define f_flag		   0x10008
314 #define bt1_flag	   0x10010
315 #define bt2_flag	   0x10020
316 #define patch_b_flag	   0x10040
317 #define init_v_flag	   0x10080
318 
319 
320 #define Zero	 0
321 #define One	 1
322 #define Two	 2
323 
324 
325 yamop  *STD_PROTO(Yap_assemble,(int,Term,struct pred_entry *,int, struct intermediates *, UInt));
326 void	STD_PROTO(Yap_emit,(compiler_vm_op,Int,CELL, struct intermediates *));
327 void	STD_PROTO(Yap_emit_3ops,(compiler_vm_op,CELL,CELL,CELL, struct intermediates *));
328 void	STD_PROTO(Yap_emit_4ops,(compiler_vm_op,CELL,CELL,CELL,CELL, struct intermediates *));
329 CELL   *STD_PROTO(Yap_emit_extra_size,(compiler_vm_op,CELL,int, struct intermediates *));
330 char   *STD_PROTO(Yap_AllocCMem,(UInt, struct intermediates *));
331 void    STD_PROTO(Yap_ReleaseCMem, (struct intermediates *));
332 int	STD_PROTO(Yap_is_a_test_pred,(Term, Term));
333 void    STD_PROTO(Yap_bip_name,(Int, char *));
334 #ifdef DEBUG
335 void	STD_PROTO(Yap_ShowCode,(struct intermediates *));
336 #endif /* DEBUG */
337 
338 
339