1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright (c) 2000 by Sun Microsystems, Inc. 24*0Sstevel@tonic-gate * All rights reserved. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate #ifndef _FCODE_ENGINE_H 28*0Sstevel@tonic-gate #define _FCODE_ENGINE_H 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #include <sys/types.h> 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #ifdef __cplusplus 35*0Sstevel@tonic-gate extern "C" { 36*0Sstevel@tonic-gate #endif 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #define MAX_ORDER 32 39*0Sstevel@tonic-gate #define CONVERT_HANDLES 40*0Sstevel@tonic-gate 41*0Sstevel@tonic-gate #ifdef BIGSTACK 42*0Sstevel@tonic-gate typedef long long fstack_t; 43*0Sstevel@tonic-gate typedef unsigned long long ufstack_t; 44*0Sstevel@tonic-gate #else 45*0Sstevel@tonic-gate typedef long fstack_t; 46*0Sstevel@tonic-gate typedef unsigned long ufstack_t; 47*0Sstevel@tonic-gate #endif 48*0Sstevel@tonic-gate typedef long *acf_t; /* pointer to execution token */ 49*0Sstevel@tonic-gate typedef long token_t; /* sizeof a forth token */ 50*0Sstevel@tonic-gate 51*0Sstevel@tonic-gate /* x@, x! type */ 52*0Sstevel@tonic-gate typedef uint64_t u_xforth_t; 53*0Sstevel@tonic-gate typedef int64_t s_xforth_t; 54*0Sstevel@tonic-gate typedef uint64_t xforth_t; 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate /* l@, l! type */ 57*0Sstevel@tonic-gate typedef uint32_t u_lforth_t; 58*0Sstevel@tonic-gate typedef int32_t s_lforth_t; 59*0Sstevel@tonic-gate typedef uint32_t lforth_t; 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate /* w@, w! type */ 62*0Sstevel@tonic-gate typedef uint16_t u_wforth_t; 63*0Sstevel@tonic-gate typedef int16_t s_wforth_t; 64*0Sstevel@tonic-gate typedef uint16_t wforth_t; 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /* Double type */ 67*0Sstevel@tonic-gate typedef uint64_t u_dforth_t; 68*0Sstevel@tonic-gate typedef int64_t s_dforth_t; 69*0Sstevel@tonic-gate typedef uint64_t dforth_t; 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate /* Variable/Value/Constant type */ 72*0Sstevel@tonic-gate typedef token_t variable_t; 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate typedef struct PROPERTY { 75*0Sstevel@tonic-gate char *name; 76*0Sstevel@tonic-gate uchar_t *data; 77*0Sstevel@tonic-gate int size; 78*0Sstevel@tonic-gate struct PROPERTY *next; 79*0Sstevel@tonic-gate } prop_t; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate typedef struct RESOURCE { 82*0Sstevel@tonic-gate struct RESOURCE *next; 83*0Sstevel@tonic-gate void *data; 84*0Sstevel@tonic-gate } fc_resource_t; 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gate #define INIT_DATA 0 87*0Sstevel@tonic-gate #define UINIT_DATA 1 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate typedef struct FCODE_ENV fcode_env_t; 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate typedef struct DEVICE_VECTOR { 92*0Sstevel@tonic-gate /* 93*0Sstevel@tonic-gate * If there is private data associated with a node this vector 94*0Sstevel@tonic-gate * table contains the routines that will be called to augment the 95*0Sstevel@tonic-gate * device. 96*0Sstevel@tonic-gate * These two routines allow the interpreter to use a different 97*0Sstevel@tonic-gate * 98*0Sstevel@tonic-gate * Interface Note: 99*0Sstevel@tonic-gate * Any routine installed here is assumed to have the standard forth 100*0Sstevel@tonic-gate * call state. It must be a void function call taking a forth execution 101*0Sstevel@tonic-gate * environment, returning any data on the stack. In general the 102*0Sstevel@tonic-gate * vector call should have the same semantics as the original routine 103*0Sstevel@tonic-gate * it is replacing. (see get_prop as an example). 104*0Sstevel@tonic-gate * 105*0Sstevel@tonic-gate * The caller has the responsibility of converting the resulting data 106*0Sstevel@tonic-gate * back to a form it requires. 107*0Sstevel@tonic-gate * 108*0Sstevel@tonic-gate */ 109*0Sstevel@tonic-gate void (*get_package_prop)(fcode_env_t *); 110*0Sstevel@tonic-gate void (*get_inherited_prop)(fcode_env_t *); 111*0Sstevel@tonic-gate } device_vector_t; 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate typedef struct DEVICE device_t; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate #define MAX_MY_ADDR 4 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gate struct DEVICE { 118*0Sstevel@tonic-gate device_t *parent; 119*0Sstevel@tonic-gate device_t *child; 120*0Sstevel@tonic-gate device_t *peer; 121*0Sstevel@tonic-gate prop_t *properties; 122*0Sstevel@tonic-gate token_t *vocabulary; 123*0Sstevel@tonic-gate fstack_t parent_adr_cells; 124*0Sstevel@tonic-gate fstack_t my_space; 125*0Sstevel@tonic-gate fstack_t my_addr[MAX_MY_ADDR]; 126*0Sstevel@tonic-gate fstack_t frame_buffer_adr; 127*0Sstevel@tonic-gate int data_size[2]; 128*0Sstevel@tonic-gate token_t *init_data; /* initialised instance data */ 129*0Sstevel@tonic-gate void *private; /* app private data */ 130*0Sstevel@tonic-gate device_vector_t vectors; 131*0Sstevel@tonic-gate }; 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate typedef struct INSTANCE { 134*0Sstevel@tonic-gate struct INSTANCE *parent; 135*0Sstevel@tonic-gate device_t *device; 136*0Sstevel@tonic-gate /* 137*0Sstevel@tonic-gate * These are copies of the same structures from the device definition 138*0Sstevel@tonic-gate * however changes here will be thrown away when the instance is 139*0Sstevel@tonic-gate * destroyed. 140*0Sstevel@tonic-gate */ 141*0Sstevel@tonic-gate char *my_args; 142*0Sstevel@tonic-gate int my_args_len; 143*0Sstevel@tonic-gate fstack_t my_space; 144*0Sstevel@tonic-gate fstack_t my_addr[MAX_MY_ADDR]; 145*0Sstevel@tonic-gate fstack_t frame_buffer_adr; 146*0Sstevel@tonic-gate token_t *data[2]; 147*0Sstevel@tonic-gate } instance_t; 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate typedef struct FCODE_TOKEN { 150*0Sstevel@tonic-gate ulong_t flags; 151*0Sstevel@tonic-gate char *name; 152*0Sstevel@tonic-gate acf_t apf; /* pointer to acf in dictionary */ 153*0Sstevel@tonic-gate #ifdef DEBUG 154*0Sstevel@tonic-gate int usage; 155*0Sstevel@tonic-gate #endif 156*0Sstevel@tonic-gate } fcode_token; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate typedef struct { 159*0Sstevel@tonic-gate char *buffer; 160*0Sstevel@tonic-gate char *scanptr; 161*0Sstevel@tonic-gate int maxlen; 162*0Sstevel@tonic-gate int separator; 163*0Sstevel@tonic-gate } input_typ; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate typedef struct ERROR_FRAME { 166*0Sstevel@tonic-gate struct ERROR_FRAME *next; 167*0Sstevel@tonic-gate fstack_t *ds; 168*0Sstevel@tonic-gate fstack_t *rs; 169*0Sstevel@tonic-gate instance_t *myself; 170*0Sstevel@tonic-gate token_t *ip; 171*0Sstevel@tonic-gate fstack_t code; 172*0Sstevel@tonic-gate } error_frame; 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate struct FCODE_ENV { 175*0Sstevel@tonic-gate fcode_token *table; /* token table */ 176*0Sstevel@tonic-gate uchar_t *base; /* dictionary base */ 177*0Sstevel@tonic-gate uchar_t *here; /* current dp */ 178*0Sstevel@tonic-gate char *name; /* last name */ 179*0Sstevel@tonic-gate long level; /* level */ 180*0Sstevel@tonic-gate token_t *ip; /* instruction pointer */ 181*0Sstevel@tonic-gate token_t *wa; /* word address */ 182*0Sstevel@tonic-gate fstack_t *ds0; /* base of dats stack */ 183*0Sstevel@tonic-gate fstack_t *rs0; /* base of return stack */ 184*0Sstevel@tonic-gate fstack_t *ds; /* data stack base */ 185*0Sstevel@tonic-gate fstack_t *rs; /* return stack base */ 186*0Sstevel@tonic-gate variable_t num_base; /* current base */ 187*0Sstevel@tonic-gate token_t *current; /* current voc */ 188*0Sstevel@tonic-gate long order_depth; 189*0Sstevel@tonic-gate token_t **order; /* Voc. search order */ 190*0Sstevel@tonic-gate token_t *lastlink; /* last forth def */ 191*0Sstevel@tonic-gate token_t *forth_voc_link; /* Storage location for 'forth' voc */ 192*0Sstevel@tonic-gate int last_token; /* last defined token */ 193*0Sstevel@tonic-gate device_t *root_node; /* root node */ 194*0Sstevel@tonic-gate device_t *attachment_pt; 195*0Sstevel@tonic-gate device_t *current_device; /* */ 196*0Sstevel@tonic-gate instance_t *my_self; /* pointer to my data */ 197*0Sstevel@tonic-gate int offset_incr; /* size of FCODE token offsets */ 198*0Sstevel@tonic-gate error_frame *catch_frame; 199*0Sstevel@tonic-gate uchar_t *fcode_buffer; /* pointer to fcode buffer */ 200*0Sstevel@tonic-gate uchar_t *fcode_ptr; /* pointer into fcode buffer */ 201*0Sstevel@tonic-gate uchar_t *last_fcode_ptr; /* pointer to last fcode fetched */ 202*0Sstevel@tonic-gate fstack_t last_fcode; /* last fcode# executed */ 203*0Sstevel@tonic-gate fstack_t last_error; /* last throw code executed */ 204*0Sstevel@tonic-gate int fcode_incr; /* space between bytecodes */ 205*0Sstevel@tonic-gate int interpretting; 206*0Sstevel@tonic-gate variable_t state; /* compile or run? */ 207*0Sstevel@tonic-gate int fcode_debug; 208*0Sstevel@tonic-gate int diagnostic_mode; 209*0Sstevel@tonic-gate fstack_t instance_mode; 210*0Sstevel@tonic-gate int interactive; /* DEBUG, interact variable */ 211*0Sstevel@tonic-gate int num_actions; 212*0Sstevel@tonic-gate int action_count; 213*0Sstevel@tonic-gate token_t *action_ptr; 214*0Sstevel@tonic-gate int strict_fcode; 215*0Sstevel@tonic-gate fstack_t control; /* control VM behaviour */ 216*0Sstevel@tonic-gate input_typ *input; /* input buffer pointer */ 217*0Sstevel@tonic-gate variable_t span; 218*0Sstevel@tonic-gate char *picturebufpos; /* pictured string buffer position */ 219*0Sstevel@tonic-gate char *picturebuf; /* pictured string buffer */ 220*0Sstevel@tonic-gate int picturebuflen; /* pictured string buffer length */ 221*0Sstevel@tonic-gate variable_t output_column; /* output column# (#out) */ 222*0Sstevel@tonic-gate variable_t output_line; /* output line# (#line) */ 223*0Sstevel@tonic-gate #ifdef CONVERT_HANDLES 224*0Sstevel@tonic-gate device_t *(*convert_phandle)(fcode_env_t *, fstack_t); 225*0Sstevel@tonic-gate fstack_t (*revert_phandle)(fcode_env_t *, device_t *); 226*0Sstevel@tonic-gate void (*allocate_phandle)(fcode_env_t *); 227*0Sstevel@tonic-gate #endif 228*0Sstevel@tonic-gate fc_resource_t *propbufs; 229*0Sstevel@tonic-gate void *private; /* private data ptr for app use. */ 230*0Sstevel@tonic-gate }; 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate #define MAX_FCODE 0xfff /* max no. of Fcode entries in table */ 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate typedef unsigned char flag_t; 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gate #define DS (env->ds) 238*0Sstevel@tonic-gate #define RS (env->rs) 239*0Sstevel@tonic-gate #define TOS *DS 240*0Sstevel@tonic-gate #define IP (env->ip) 241*0Sstevel@tonic-gate #define WA (env->wa) 242*0Sstevel@tonic-gate #define DEPTH (DS-env->ds0) 243*0Sstevel@tonic-gate #define CURRENT (env->current) 244*0Sstevel@tonic-gate #define ORDER (env->order) 245*0Sstevel@tonic-gate #define BASE (env->base) 246*0Sstevel@tonic-gate #define HERE (env->here) 247*0Sstevel@tonic-gate #define CONTEXT env->order[env->order_depth] 248*0Sstevel@tonic-gate #define MYSELF (env->my_self) 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate #ifdef FCODE_INTERNAL 251*0Sstevel@tonic-gate #include <fcode/proto.h> 252*0Sstevel@tonic-gate #endif 253*0Sstevel@tonic-gate #include <fcode/public.h> 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate #define SIGN_SHIFT ((8*(sizeof (fstack_t)))-1) 256*0Sstevel@tonic-gate #define SIGN_BIT (((ufstack_t)1)<<SIGN_SHIFT) 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate /* 259*0Sstevel@tonic-gate * Note that sizeof (token_t) MUST equal sizeof (token_t *). If it doesn't, 260*0Sstevel@tonic-gate * many things will break. 261*0Sstevel@tonic-gate */ 262*0Sstevel@tonic-gate #define _ALIGN(x, y) (((long)(x)) & ~(sizeof (y)-1)) 263*0Sstevel@tonic-gate #define TOKEN_ROUNDUP(x) _ALIGN((x + ((sizeof (token_t)-1))), token_t) 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate #define min(x, y) ((x) < (y) ? (x) : (y)) 266*0Sstevel@tonic-gate #define max(x, y) ((x) > (y) ? (x) : (y)) 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate /* values for flag_t */ 269*0Sstevel@tonic-gate #define ANSI_WORD 0x01 270*0Sstevel@tonic-gate #define P1275_WORD 0x02 271*0Sstevel@tonic-gate #define FLAG_NONAME 0x04 272*0Sstevel@tonic-gate #define IMMEDIATE 0x08 273*0Sstevel@tonic-gate #define FLAG_VALUE 0x10 274*0Sstevel@tonic-gate #define FLAG_DEBUG 0x20 275*0Sstevel@tonic-gate #define DEFINER (FLAG_NONAME|IMMEDIATE) 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate #define FORTH(fl, nm, fnc) define_word(env, fl, nm, fnc); 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gate #define LINK_TO_ACF(x) (((token_t *)(x))+1) 280*0Sstevel@tonic-gate #define LINK_TO_FLAGS(x) (((flag_t *)(x))-1) 281*0Sstevel@tonic-gate #define ACF_TO_LINK(x) (((token_t *)(x))-1) 282*0Sstevel@tonic-gate #define ACF_TO_BODY(x) (((acf_t)(x))+1) 283*0Sstevel@tonic-gate #define BODY_TO_LINK(x) (((acf_t)(x))-1) 284*0Sstevel@tonic-gate #define BODY_TO_FLAGS(x) (((flag_t *)(BODY_TO_LINK(x))) - 1) 285*0Sstevel@tonic-gate #define EXPOSE_ACF *((acf_t)env->current) = \ 286*0Sstevel@tonic-gate (token_t)(env->lastlink) 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate #define COMPILE_TOKEN(x) PUSH(DS, (fstack_t)(x)); compile_comma(env); 289*0Sstevel@tonic-gate #define CHECK_DEPTH(env, x, w) if ((x) > (env->ds - env->ds0)) \ 290*0Sstevel@tonic-gate forth_abort(env, "%s: stack underflow\n", w); 291*0Sstevel@tonic-gate #define CHECK_RETURN_DEPTH(env, x, w) if ((x) > (env->rs - env->rs0)) \ 292*0Sstevel@tonic-gate forth_abort(env, "%s: return stack underflow\n", w); 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate #define FCRP_NOERROR 0x80000000 /* fc_run_priv: no err msg. */ 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate #ifdef CONVERT_HANDLES 297*0Sstevel@tonic-gate #define CONVERT_PHANDLE(e, x, y) x = env->convert_phandle(e, y) 298*0Sstevel@tonic-gate #define REVERT_PHANDLE(e, x, y) x = env->revert_phandle(e, y) 299*0Sstevel@tonic-gate #define ALLOCATE_PHANDLE(e) env->allocate_phandle(e) 300*0Sstevel@tonic-gate #else 301*0Sstevel@tonic-gate #define CONVERT_PHANDLE(e, x, y) x = (device_t *)(y) 302*0Sstevel@tonic-gate #define REVERT_PHANDLE(e, x, y) x = (fstack_t)(y) 303*0Sstevel@tonic-gate #define ALLOCATE_PHANDLE(e) 304*0Sstevel@tonic-gate #endif 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate extern fcode_env_t *env; 307*0Sstevel@tonic-gate extern int dict_size; 308*0Sstevel@tonic-gate extern int in_forth_abort; 309*0Sstevel@tonic-gate extern int stack_size; 310*0Sstevel@tonic-gate extern token_t value_defines[][3]; 311*0Sstevel@tonic-gate extern void (*bbranch_ptrs[3])(fcode_env_t *); 312*0Sstevel@tonic-gate extern void (*blit_ptr)(fcode_env_t *); 313*0Sstevel@tonic-gate extern void (*create_ptr)(fcode_env_t *); 314*0Sstevel@tonic-gate extern void (*do_bdo_ptr)(fcode_env_t *); 315*0Sstevel@tonic-gate extern void (*do_bqdo_ptr)(fcode_env_t *); 316*0Sstevel@tonic-gate extern void (*do_leave_ptr)(fcode_env_t *); 317*0Sstevel@tonic-gate extern void (*do_loop_ptr)(fcode_env_t *); 318*0Sstevel@tonic-gate extern void (*do_ploop_ptr)(fcode_env_t *); 319*0Sstevel@tonic-gate extern void (*does_ptr)(fcode_env_t *); 320*0Sstevel@tonic-gate extern void (*quote_ptr)(fcode_env_t *); 321*0Sstevel@tonic-gate extern void (*quote_ptr)(fcode_env_t *); 322*0Sstevel@tonic-gate extern void (*semi_ptr)(fcode_env_t *); 323*0Sstevel@tonic-gate extern void (*tlit_ptr)(fcode_env_t *); 324*0Sstevel@tonic-gate extern void (*to_ptr)(fcode_env_t *); 325*0Sstevel@tonic-gate extern void (*to_ptr)(fcode_env_t *); 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate #ifdef __cplusplus 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate #endif 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate #endif /* _FCODE_ENGINE_H */ 332