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