00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #ifndef PLANG_INST_PRIV_H
00021 #define PLANG_INST_PRIV_H
00022 
00023 #include "term-priv.h"
00024 #include "rbtree-priv.h"
00025 #include <stdio.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00033 typedef enum {
00034     P_OP_PUT_X_VARIABLE,
00035     P_OP_PUT_X_VARIABLE2,
00036     P_OP_PUT_X_VARIABLE2_LARGE,
00037     P_OP_PUT_Y_VARIABLE2,
00038     P_OP_PUT_Y_VARIABLE2_LARGE,
00039     P_OP_PUT_X_VALUE,
00040     P_OP_PUT_X_VALUE_LARGE,
00041     P_OP_PUT_Y_VALUE,
00042     P_OP_PUT_Y_VALUE_LARGE,
00043     P_OP_PUT_FUNCTOR,
00044     P_OP_PUT_FUNCTOR_LARGE,
00045     P_OP_PUT_LIST,
00046     P_OP_PUT_CONSTANT,
00047     P_OP_PUT_MEMBER_VARIABLE,
00048     P_OP_PUT_MEMBER_VARIABLE_LARGE,
00049     P_OP_PUT_MEMBER_VARIABLE_AUTO,
00050     P_OP_PUT_MEMBER_VARIABLE_AUTO_LARGE,
00051 
00052     P_OP_SET_X_VARIABLE,
00053     P_OP_SET_Y_VARIABLE,
00054     P_OP_SET_X_VALUE,
00055     P_OP_SET_Y_VALUE,
00056     P_OP_SET_FUNCTOR,
00057     P_OP_SET_FUNCTOR_LARGE,
00058     P_OP_SET_LIST,
00059     P_OP_SET_LIST_TAIL,
00060     P_OP_SET_NIL_TAIL,
00061     P_OP_SET_CONSTANT,
00062     P_OP_SET_VOID,
00063 
00064     P_OP_GET_Y_VARIABLE,
00065     P_OP_GET_Y_VARIABLE_LARGE,
00066     P_OP_GET_X_VALUE,
00067     P_OP_GET_X_VALUE_LARGE,
00068     P_OP_GET_Y_VALUE,
00069     P_OP_GET_Y_VALUE_LARGE,
00070     P_OP_GET_FUNCTOR,
00071     P_OP_GET_FUNCTOR_LARGE,
00072     P_OP_GET_LIST,
00073     P_OP_GET_LIST_LARGE,
00074     P_OP_GET_ATOM,
00075     P_OP_GET_CONSTANT,
00076 
00077     P_OP_GET_IN_X_VALUE,
00078     P_OP_GET_IN_X_VALUE_LARGE,
00079     P_OP_GET_IN_Y_VALUE,
00080     P_OP_GET_IN_Y_VALUE_LARGE,
00081     P_OP_GET_IN_FUNCTOR,
00082     P_OP_GET_IN_FUNCTOR_LARGE,
00083     P_OP_GET_IN_LIST,
00084     P_OP_GET_IN_LIST_LARGE,
00085     P_OP_GET_IN_ATOM,
00086     P_OP_GET_IN_CONSTANT,
00087 
00088     P_OP_UNIFY_X_VARIABLE,
00089     P_OP_UNIFY_Y_VARIABLE,
00090     P_OP_UNIFY_X_VALUE,
00091     P_OP_UNIFY_Y_VALUE,
00092     P_OP_UNIFY_FUNCTOR,
00093     P_OP_UNIFY_FUNCTOR_LARGE,
00094     P_OP_UNIFY_LIST,
00095     P_OP_UNIFY_LIST_TAIL,
00096     P_OP_UNIFY_NIL_TAIL,
00097     P_OP_UNIFY_ATOM,
00098     P_OP_UNIFY_CONSTANT,
00099     P_OP_UNIFY_VOID,
00100 
00101     P_OP_UNIFY_IN_X_VALUE,
00102     P_OP_UNIFY_IN_Y_VALUE,
00103     P_OP_UNIFY_IN_FUNCTOR,
00104     P_OP_UNIFY_IN_FUNCTOR_LARGE,
00105     P_OP_UNIFY_IN_LIST,
00106     P_OP_UNIFY_IN_LIST_TAIL,
00107     P_OP_UNIFY_IN_NIL_TAIL,
00108     P_OP_UNIFY_IN_ATOM,
00109     P_OP_UNIFY_IN_CONSTANT,
00110     P_OP_UNIFY_IN_VOID,
00111 
00112     P_OP_RESET_ARGUMENT,
00113     P_OP_RESET_ARGUMENT_LARGE,
00114     P_OP_RESET_TAIL,
00115 
00116     P_OP_JUMP,
00117 
00118     P_OP_PROCEED,
00119     P_OP_FAIL,
00120     P_OP_RETURN,
00121     P_OP_RETURN_TRUE,
00122     P_OP_THROW,
00123 
00124     P_OP_CALL,
00125     P_OP_EXECUTE,
00126     P_OP_TRY_ME_ELSE,
00127 
00128     P_OP_RETRY_ME_ELSE,
00129     P_OP_TRUST_ME,
00130 
00131     P_OP_NECK_CUT,
00132     P_OP_GET_LEVEL,
00133     P_OP_CUT,
00134 
00135     P_OP_END
00136 
00137 } p_opcode;
00138 
00139 
00140 struct p_inst_header
00141 {
00142 #if defined(P_TERM_64BIT)
00143     unsigned int opcode;
00144     unsigned int arg1;
00145 #else
00146     unsigned int opcode : 8;
00147     unsigned int arg1   : 24;
00148 #endif
00149 };
00150 
00151 
00152 struct p_inst_one_reg
00153 {
00154 #if defined(P_TERM_64BIT)
00155     unsigned int opcode;
00156     unsigned int reg1;
00157 #else
00158     unsigned int opcode : 8;
00159     unsigned int reg1   : 24;
00160 #endif
00161 };
00162 
00163 
00164 struct p_inst_two_reg
00165 {
00166 #if defined(P_TERM_64BIT)
00167     unsigned int opcode;
00168     unsigned int reg1   : 16;
00169     unsigned int reg2   : 16;
00170 #define P_MAX_SMALL_REG 65535
00171 #else
00172     unsigned int opcode : 8;
00173     unsigned int reg1   : 12;
00174     unsigned int reg2   : 12;
00175 #define P_MAX_SMALL_REG 4095
00176 #endif
00177 };
00178 
00179 
00180 
00181 struct p_inst_large_two_reg
00182 {
00183 #if defined(P_TERM_64BIT)
00184     unsigned int opcode;
00185     unsigned int reg1;
00186     unsigned int reg2;
00187     unsigned int pad;
00188 #else
00189     unsigned int opcode : 8;
00190     unsigned int reg1   : 24;
00191     unsigned int reg2;
00192 #endif
00193 };
00194 
00195 
00196 struct p_inst_functor
00197 {
00198 #if defined(P_TERM_64BIT)
00199     unsigned int opcode;
00200     unsigned int reg1   : 16;
00201     unsigned int arity  : 16;
00202 #else
00203     unsigned int opcode : 8;
00204     unsigned int reg1   : 12;
00205     unsigned int arity  : 12;
00206 #endif
00207     p_term *name;
00208 };
00209 
00210 
00211 struct p_inst_large_functor
00212 {
00213 #if defined(P_TERM_64BIT)
00214     unsigned int opcode;
00215     unsigned int reg1;
00216     unsigned int arity;
00217     unsigned int pad;
00218 #else
00219     unsigned int opcode : 8;
00220     unsigned int reg1   : 24;
00221     unsigned int arity;
00222 #endif
00223     p_term *name;
00224 };
00225 
00226 
00227 struct p_inst_set_value
00228 {
00229 #if defined(P_TERM_64BIT)
00230     unsigned int opcode;
00231     unsigned int reg1;
00232     unsigned int index;
00233     unsigned int reg2;
00234 #else
00235     unsigned int opcode : 8;
00236     unsigned int reg1   : 24;
00237     unsigned int index;
00238     unsigned int reg2;
00239 #endif
00240 };
00241 
00242 
00243 struct p_inst_constant
00244 {
00245 #if defined(P_TERM_64BIT)
00246     unsigned int opcode;
00247     unsigned int reg1;
00248 #else
00249     unsigned int opcode : 8;
00250     unsigned int reg1   : 24;
00251 #endif
00252     p_term *value;
00253 };
00254 
00255 
00256 struct p_inst_label
00257 {
00258 #if defined(P_TERM_64BIT)
00259     unsigned int opcode;
00260     unsigned int reg1;
00261 #else
00262     unsigned int opcode : 8;
00263     unsigned int reg1   : 24;
00264 #endif
00265     p_inst *label;
00266 };
00267 
00268 union p_inst
00269 {
00270     struct p_inst_header        header;
00271     struct p_inst_one_reg       one_reg;
00272     struct p_inst_two_reg       two_reg;
00273     struct p_inst_large_two_reg large_two_reg;
00274     struct p_inst_functor       functor;
00275     struct p_inst_large_functor large_functor;
00276     struct p_inst_set_value     set_value;
00277     struct p_inst_constant      constant;
00278     struct p_inst_label         label;
00279 };
00280 
00281 #define P_CODE_BLOCK_WORDS      64
00282 #define P_CODE_BLOCK_SIZE       (P_CODE_BLOCK_WORDS * sizeof(void *))
00283 
00284 typedef struct p_code_block p_code_block;
00285 struct p_code_block
00286 {
00287     void *inst[P_CODE_BLOCK_WORDS];
00288 };
00289 
00290 typedef struct p_code p_code;
00291 struct p_code
00292 {
00293     p_code_block *first_block;
00294     p_code_block *current_block;
00295     size_t posn;
00296     size_t first_block_size;
00297 
00298     unsigned int *used_regs;
00299     unsigned int *temp_regs;
00300     int num_regs;
00301     int max_regs;
00302     int blocked_regs;
00303 
00304     int num_yregs;
00305 
00306     int force_large_regs;
00307 };
00308 
00309 void _p_code_allocate_args(p_code *code, int arity);
00310 int _p_code_generate_builder
00311     (p_context *context, p_term *term, p_code *code, int preferred_reg);
00312 void _p_code_generate_return(p_code *code, int reg);
00313 void _p_code_generate_matcher
00314     (p_context *context, p_term *term, p_code *code, int reg,
00315      int input_only);
00316 void _p_code_generate_dynamic_clause
00317     (p_context *context, p_term *head, p_term *body, p_code *code);
00318 
00319 p_code *_p_code_new(void);
00320 void _p_code_finish(p_code *code, p_code_clause *clause);
00321 
00322 void _p_code_set_xreg(p_context *context, int reg, p_term *value);
00323 p_goal_result _p_code_run
00324     (p_context *context, const p_code_clause *clause, p_term **error);
00325 
00326 void _p_code_disassemble
00327     (FILE *output, p_context *context, const p_code_clause *clause);
00328 int _p_code_argument_key
00329     (p_rbkey *key, const p_code_clause *clause, unsigned int arg);
00330 
00333 #ifdef __cplusplus
00334 };
00335 #endif
00336 
00337 #endif