YAP 7.1.0
YapCompile.h
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:YapCompile.h *
12* Last rev: *
13* mods: *
14* comments:compiler data structures and routines *
15* *
16*************************************************************************/
17
18/* consult stack management */
19
20#undef COMPILER_OPS
21#undef COMPILER_OPS_END
22#undef f
23
24#ifdef COMPILER_NAMES
25
26#define COMPILER_OPS() char *opDesc[] =
27#define COMPILER_OPS_END()
28#define f(x,y) y
29
30#else
31
32#define COMPILER_OPS() typedef enum compiler_op
33#define COMPILER_OPS_END() compiler_vm_op
34#define f(x,y) x
35
36#endif
37
38/* virtual machine instruction op-codes*/
39COMPILER_OPS() {
40 f(nop_op, "nop"),
41 f(get_var_op, "get_var\t\t %v,%r"),
42 f(put_var_op, "put_var\t\t %v,%r"),
43 f( get_val_op, "get_val\t\t %v,%r"),
44 f(put_val_op, "put_val\t\t %v,%r"),
45 f(get_atom_op, "get_atom\t %a,%r"),
46 f(put_atom_op, "put_atom\t %a,%r"),
47 f(get_num_op, "get_num\t\t %n,%r"),
48 f(put_num_op, "put_num\t\t %n,%r"),
49 f( get_float_op, "get_float\t\t %w,%r"),
50 f(put_float_op, "put_float\t\t %w,%r"),
51 f(get_dbterm_op, "get_dbterm\t %w,%r"),
52 f(put_dbterm_op, "put_dbterm\t %w,%r"),
53 f(get_longint_op, "get_longint\t\t %w,%r"),
54 f( put_longint_op, "put_longint\t\t %w,%r"),
55 f(get_string_op, "get_string\t\t %w,%S"),
56 f(put_string_op, "put_string\t\t %w,%S"),
57 f(get_bigint_op, "get_bigint\t\t %l,%r"),
58 f(put_bigint_op, "put_bigint\t\t %l,%r"),
59 f( get_list_op, "get_list\t %r"),
60 f(put_list_op, "put_list\t %r"),
61 f(get_struct_op, "get_struct\t %f,%r"),
62 f(put_struct_op, "put_struct\t %f,%r"),
63 f(put_unsafe_op, "put_unsafe\t %v,%r"),
64 f( unify_var_op, "unify_var\t %v"),
65 f(write_var_op, "write_var\t %v"),
66 f(unify_val_op, "unify_val\t %v"),
67 f(write_val_op, "write_val\t %v"),
68 f(unify_atom_op, "unify_atom\t %a"),
69 f( write_atom_op, "write_atom\t %a"),
70 f(unify_num_op, "unify_num\t %n"),
71 f(write_num_op, "write_num\t %n"),
72 f(unify_float_op, "unify_float\t %w"),
73 f( write_float_op, "write_float\t %w"),
74 f(unify_dbterm_op, "unify_dbterm\t %w"),
75 f(write_dbterm_op, "write_dbterm\t %w"),
76 f( unify_longint_op, "unify_longint\t %w"),
77 f(write_longint_op, "write_longint\t %w"),
78 f(unify_string_op, "unify_string\t %S"),
79 f( write_string_op, "write_string\t %S"),
80 f(unify_bigint_op, "unify_bigint\t %l"),
81 f(write_bigint_op, "write_bigint\t %l"),
82 f(unify_list_op, "unify_list"),
83 f( write_list_op, "write_list"),
84 f(unify_struct_op, "unify_struct\t %f"),
85 f(write_struct_op, "write_struct\t %f"),
86 f( write_unsafe_op, "write_unsafe\t %v"),
87 f(unify_local_op, "unify_local\t %v"),
88 f(write_local_op, "write local\t %v"),
89 f( unify_last_list_op, "unify_last_list"),
90 f(write_last_list_op, "write_last_list"),
91 f( unify_last_struct_op, "unify_last_struct\t %f"),
92 f(write_last_struct_op, "write_last_struct\t %f"),
93 f(unify_last_var_op, "unify_last_var\t %v"),
94 f( unify_last_val_op, "unify_last_val\t %v"),
95 f(unify_last_local_op, "unify_last_local\t %v"),
96 f(unify_last_atom_op, "unify_last_atom\t %a"),
97 f(unify_last_num_op, "unify_last_num\t %n"),
98 f(unify_last_float_op, "unify_last_float\t %w"),
99 f(unify_last_dbterm_op, "unify_last_dbterm\t %w"),
100 f(unify_last_longint_op, "unify_last_longint\t %w"),
101 f(unify_last_string_op, "unify_last_string\t %S"),
102 f(unify_last_bigint_op, "unify_last_bigint\t %l"),
103 f(ensure_space_op, "ensure_space"),
104 f(native_op, "native_code"),
105 f(f_var_op, "function_to_var\t %v,%B"),
106 f(f_val_op, "function_to_val\t %v,%B"),
107 f(f_0_op, "function_to_0\t %B"),
108 f(align_float_op, "align_float"),
109 f(fail_op, "fail"),
110 f(cut_op, "cut"),
111 f(cutexit_op, "cutexit"),
112 f(allocate_op, "allocate"),
113 f(deallocate_op, "deallocate"),
114 f(tryme_op, "try_me_else\t\t %l\t %x"),
115 f(jump_op, "jump\t\t %l"),
116 f(jumpi_op, "jump_in_indexing\t\t %i"),
117 f(procceed_op, "proceed"),
118 f(call_op, "call\t\t %p,%d,%z"),
119 f(execute_op, "execute\t\t %p"),
120 f(safe_call_op, "sys\t\t %p"),
121 f(label_op, "%l:"),
122 f(name_op, "name\t\t %m,%d"),
123 f(pop_op, "pop\t\t %l"),
124 f(retryme_op, "retry_me_else\t\t %l\t %x"),
125 f(trustme_op, "trust_me_else_fail\t %x"),
126 f(either_op, "either_me\t\t %l,%d,%z"),
127 f(orelse_op, "or_else\t\t %l,%z"),
128 f(orlast_op, "or_last"),
129 f(push_or_op, "push_or"),
130 f(pushpop_or_op, "pushpop_or"),
131 f(pop_or_op, "pop_or"),
132 f(save_b_op, "save_by\t\t %v"),
133 f(commit_b_op, "commit_by\t\t %v"),
134 f(soft_cut_b_op, "soft_cut_by\t\t %v"),
135 f(patch_b_op, "patch_by\t\t %v"),
136 f(try_op, "try\t\t %g\t %x"),
137 f(retry_op, "retry\t\t %g\t %x"),
138 f(trust_op, "trust\t\t %g\t %x"),
139 f(try_in_op, "try_in\t\t %g\t %x"),
140 f(jump_v_op, "jump_if_var\t\t %g"),
141 f(jump_nv_op, "jump_if_nonvar\t\t %g"),
142 f(cache_arg_op, "cache_arg\t %r"),
143 f(cache_sub_arg_op, "cache_sub_arg\t %d"),
144 f(user_switch_op, "user_switch"),
145 f(switch_on_type_op, "switch_on_type\t %h\t %h\t %h\t %h"),
146 f(switch_c_op, "switch_on_constant\t %i,n%c"),
147 f(if_c_op, "if_constant\t %i,n%c"),
148 f(switch_f_op, "switch_on_functor\t %i,n%e"),
149 f(if_f_op, "if_functor\t %i,n%e"),
150 f(if_not_op, "if_not_then\t %i\t %h\t %h\t %h"),
151 f(index_dbref_op, "index_on_dbref"),
152 f(index_blob_op, "index_on_blob"),
153 f(index_string_op, "index_on_string"),
154 f(index_long_op, "index_on_blob"),
155 f( if_nonvar_op, "check_var\t %r"),
156 f(save_pair_op, "save_pair\t %v"),
157 f(save_appl_op, "save_appl\t %v"),
158 f( mark_initialized_pvars_op, "pvar_bitmap\t %l,%b"),
159 f(mark_live_regs_op, "pvar_live_regs\t %l,%b"),
160 f(fetch_args_vv_op, "fetch_reg1_reg2\t %N,%N"),
161 f( fetch_args_cv_op, "fetch_constant_reg\t %l,%N"),
162 f(fetch_args_vc_op, "fetch_reg_constant\t %l,%N"),
163 f(fetch_args_iv_op, "fetch_integer_reg\t %d,%N"),
164 f( fetch_args_vi_op, "fetch_reg_integer\t %d,%N"),
165 f(enter_profiling_op, "enter_profiling\t\t %g"),
166 f(retry_profiled_op, "retry_profiled\t\t %g"),
167 f( count_call_op, "count_call_op\t\t %g"),
168 f(count_retry_op, "count_retry_op\t\t %g"),
169 f(restore_tmps_op, "restore_temps\t\t %l"),
170 f( restore_tmps_and_skip_op, "restore_temps_and_skip\t\t %l"),
171 f(enter_lu_op, "enter_lu"),
172 f( empty_call_op, "empty_call\t\t %l,%d"),
173 f(bccall_op, "binary_cfunc\t\t %v,%r,%2"),
174 f(blob_op, "blob\t %O"),
175 f(string_op, "string\t %O"),
176 f(label_ctl_op, "label_control\t"),
177#ifdef YAPOR
178 f(sync_op, "sync"),
179#endif /* YAPOR */
180#ifdef TABLING
181 f(table_new_answer_op, "table_new_answer"),
182 f(table_try_single_op, "table_try_single\t %g\t %x"),
183#ifdef TABLING_INNER_CUTS
184 f(clause_with_cut_op, "clause_with_cut"),
185#endif /* TABLING_INNER_CUTS */
186 #endif
187#ifdef BEAM
188 f(run_op, "run_op %1,%4"),
189 f(body_op, "body_op %1"),
190 f( endgoal_op, "endgoal_op"),
191 f(try_me_op, "try_me_op %1,%4"),
192 f(retry_me_op, "retry_me_op %1,%4"),
193 f(trust_me_op, "trust_me_op %1,%4"),
194 f(only_1_clause_op, "only_1_clause_op %1,%4"),
195 f( create_first_box_op, "create_first_box_op %1,%4"),
196 f(create_box_op, "create_box_op %1,%4"),
197 f( create_last_box_op, "create_last_box_op %1,%4"),
198 f(remove_box_op, "remove_box_op %1,%4"),
199 f( remove_last_box_op, "remove_last_box_op %1,%4"),
200 f(prepare_tries, "prepare_tries"),
201 f(std_base_op, "std_base_op %1,%4"),
202 f(direct_safe_call_op, "direct_safe_call"),
203 f(commit_op, ),
204 f(skip_while_var_op, "skip_while_var_op"),
205 f(wait_while_var_op, "wait_while_var_op"),
206 f(force_wait_op, "force_wait_op"),
207 f(is_op, "is_op"),
208 f(write_op, "write_op"),
209 f(equal_op, "equal_op"),
210 f(exit_op, "exit"),
211#endif
212#ifdef SFUNC
213 f(get_s_f_op, "get_s_f_op\t %f,%r"),
214 f(put_s_f_op, "put_s_f_op\t %f,%r"),
215 f(unify_s_f_op, "unify_s_f_op\t %f"),
216 f(write_s_f_op, "write_s_f_op\t %f"),
217 f(unify_s_var_op, "unify_s_var\t %v,%r"),
218 f(write_s_var_op, "write_s_var\t %v,%r"),
219 f(unify_s_val_op, "unify_s_val\t %v,%r"),
220 f(write_s_val_op, "write_s_val\t %v,%r"),
221 f(unify_s_a_op, "unify_s_a\t %a,%r"),
222 f(write_s_a_op, "write_s_a\t %a,%r"),
223 f(get_s_end_op, "get_s_end"),
224 f(put_s_end_op, "put_s_end"),
225 f(unify_s_end_op, "unify_s_end"),
226 f(write_s_end_op, "write_s_end"),
227#endif
228 } COMPILER_OPS_END();
229
230#ifndef COMPILER_NAMES
231
232typedef struct PSEUDO {
233 struct PSEUDO *nextInst;
234 enum compiler_op op;
235 CELL rnd1;
236 union {
237 Int oprnd2;
238#if MIN_ARRAY == 0
239 CELL opseqt[MIN_ARRAY];
240#else
241 CELL opseqt[1];
242#endif
243 } ops;
244} PInstr;
245
246#define arnds ops.opseqt
247#define rnd2 ops.oprnd2
248#define rnd3 ops.opseqt[1]
249#define rnd4 ops.opseqt[2]
250#define rnd5 ops.opseqt[3]
251#define rnd6 ops.opseqt[4]
252#define rnd7 ops.opseqt[5]
253#define rnd8 ops.opseqt[6]
254
255typedef struct VENTRY {
256 CELL SelfOfVE;
257 Term AdrsOfVE;
258 Int KindOfVE;
259 CELL NoOfVE;
260 PInstr *FirstOpForV;
261 PInstr *LastOpForV;
262 BITS16 AgeOfVE;
263 BITS16 BranchOfVE;
264 BITS16 LastBranchOfVE;
265 BITS16 FirstOfVE;
266 BITS16 RCountOfVE;
267 BITS16 FlagsOfVE;
268 struct VENTRY *NextOfVE;
269} Ventry;
270
271typedef struct CEXPENTRY {
272 Term TermOfCE;
273 PInstr *CodeOfCE;
274 Term VarOfCE;
275 struct CEXPENTRY *NextCE;
276} CExpEntry;
277
278#define COMPILER_ERR_BOTCH 1
279#define OUT_OF_HEAP_BOTCH 2
280#define OUT_OF_STACK_BOTCH 3
281#define OUT_OF_TEMPS_BOTCH 4
282#define OUT_OF_AUX_BOTCH 5
283#define OUT_OF_TRAIL_BOTCH 6
284
285typedef struct intermediates {
286 char *freep;
287 char *freep0;
288 struct mem_blk *blks;
289 char *blk_cur, *blk_top;
290 struct PSEUDO *cpc;
291 struct PSEUDO *CodeStart;
292 struct PSEUDO *icpc;
293 struct PSEUDO *BlobsStart;
294 struct dbterm_list *dbterml;
295 Int *label_offset;
296 Int *uses;
297 Term *contents;
298 struct pred_entry *CurrentPred;
299 sigjmp_buf CompilerBotch;
300 yamop *code_addr;
301 yamop *expand_block;
302 UInt i_labelno;
303 UInt exception_handler, success_handler, failure_handler;
304 /* for expanding code */
305 yamop **current_try_lab, **current_trust_lab;
306 yamop *try_instructions;
307 struct StructClauseDef *cls;
308 int clause_has_cut;
309 UInt term_depth, last_index_at_depth;
310 UInt last_index_new_depth, last_depth_size;
311 /* for expanding code */
312 union {
313 struct static_index *si;
314 struct logic_upd_index *lui;
315 } current_cl;
317
318typedef enum special_label_id_enum {
319 SPECIAL_LABEL_SUCCESS = 0,
320 SPECIAL_LABEL_FAILURE = 1,
321 SPECIAL_LABEL_EXCEPTION = 2
322} special_label_id;
323
324typedef enum special_label_op_enum {
325 SPECIAL_LABEL_INIT = 0,
326 SPECIAL_LABEL_SET = 1,
327 SPECIAL_LABEL_CLEAR = 2
328} special_label_op;
329
330#define SafeVar 0x01
331#define PermFlag 0x02
332#define GlobalVal 0x04
333#define OnHeadFlag 0x08
334#define NonVoid 0x10
335#define BranchVar 0x20
336#define OnLastGoal 0x40
337
338#define MaskVarClass 0x0f000000L
339#define MaskVarAdrs 0x00ffffffL
340#define Unassigned 0x00000000L
341#define VoidVar 0x01000000L
342#define TempVar 0x02000000L
343#define PermVar 0x03000000L
344
345#define save_b_flag 0x10000
346#define commit_b_flag 0x10001
347#define save_appl_flag 0x10002
348#define save_pair_flag 0x10004
349#define f_flag 0x10008
350#define bt_flag 0x10010
351#define bt2_flag 0x10020 // unused
352#define patch_b_flag 0x10040
353#define soft_cut_b_flag 0x10080
354#define init_v_flag 0x10100
355
356#define Zero 0
357#define One 1
358#define Two 2
359
360extern yamop *Yap_assemble(int, Term, struct pred_entry *, int, struct intermediates *, UInt);
361extern void Yap_emit(compiler_vm_op, Int, CELL, struct intermediates *);
362extern void Yap_emit_3ops(compiler_vm_op, CELL, CELL, CELL, struct intermediates *);
363extern void Yap_emit_4ops(compiler_vm_op, CELL, CELL, CELL, CELL, struct intermediates *);
364extern void Yap_emit_5ops(compiler_vm_op, CELL, CELL, CELL, CELL, CELL,struct intermediates *);
365extern void Yap_emit_6ops(compiler_vm_op, CELL, CELL, CELL, CELL, CELL, CELL,struct intermediates *);
366extern void Yap_emit_7ops(compiler_vm_op, CELL, CELL, CELL, CELL, CELL, CELL, CELL, struct intermediates *);
367extern CELL *Yap_emit_extra_size(compiler_vm_op, CELL, int, struct intermediates *);
368extern char *Yap_AllocCMem(UInt, struct intermediates *);
369extern void Yap_ReleaseCMem(struct intermediates *);
370extern int Yap_is_a_test_pred(Term, Term);
371extern void Yap_bip_name(Int, char *);
372#ifdef DEBUG
373extern void Yap_ShowCode(struct intermediates *);
374#endif /* DEBUG */
375
376#endif
Definition: Yatom.h:544
Definition: amidefs.h:264