xref: /onnv-gate/usr/src/lib/efcode/engine/interface.c (revision 0:68f95e015346)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <string.h>
31*0Sstevel@tonic-gate #include <stdlib.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <fcode/private.h>
34*0Sstevel@tonic-gate #include <fcode/log.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * the external start point for this goo
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate void
push_ds(fcode_env_t * env,fstack_t d)41*0Sstevel@tonic-gate push_ds(fcode_env_t *env, fstack_t d)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate 	PUSH(DS, d);
44*0Sstevel@tonic-gate }
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate fstack_t
pop_ds(fcode_env_t * env)47*0Sstevel@tonic-gate pop_ds(fcode_env_t *env)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	return (POP(DS));
50*0Sstevel@tonic-gate }
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate void
push_rs(fcode_env_t * env,fstack_t d)53*0Sstevel@tonic-gate push_rs(fcode_env_t *env, fstack_t d)
54*0Sstevel@tonic-gate {
55*0Sstevel@tonic-gate 	PUSH(RS, d);
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate fstack_t
pop_rs(fcode_env_t * env)59*0Sstevel@tonic-gate pop_rs(fcode_env_t *env)
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	return (POP(RS));
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*
65*0Sstevel@tonic-gate  * Pushes a C string on the stack.
66*0Sstevel@tonic-gate  */
67*0Sstevel@tonic-gate void
push_a_string(fcode_env_t * env,char * str)68*0Sstevel@tonic-gate push_a_string(fcode_env_t *env, char *str)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	if (str) {
71*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)str);
72*0Sstevel@tonic-gate 		PUSH(DS, strlen(str));
73*0Sstevel@tonic-gate 	} else {
74*0Sstevel@tonic-gate 		PUSH(DS, 0);
75*0Sstevel@tonic-gate 		PUSH(DS, 0);
76*0Sstevel@tonic-gate 	}
77*0Sstevel@tonic-gate }
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate  * Pops a (potentially null) string off the stack.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate char *
pop_a_string(fcode_env_t * env,int * lenp)83*0Sstevel@tonic-gate pop_a_string(fcode_env_t *env, int *lenp)
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	int len;
86*0Sstevel@tonic-gate 	char *str;
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	len = POP(DS);
89*0Sstevel@tonic-gate 	str = (char *)POP(DS);
90*0Sstevel@tonic-gate 	if (len == 0)
91*0Sstevel@tonic-gate 		str = NULL;
92*0Sstevel@tonic-gate 	else if (str == NULL)
93*0Sstevel@tonic-gate 		len = 0;
94*0Sstevel@tonic-gate 	if (lenp)
95*0Sstevel@tonic-gate 		*lenp = len;
96*0Sstevel@tonic-gate 	return (str);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate /*
100*0Sstevel@tonic-gate  * Pops & strdup's a string off the stack, handles NULL strings.
101*0Sstevel@tonic-gate  */
102*0Sstevel@tonic-gate char *
pop_a_duped_string(fcode_env_t * env,int * lenp)103*0Sstevel@tonic-gate pop_a_duped_string(fcode_env_t *env, int *lenp)
104*0Sstevel@tonic-gate {
105*0Sstevel@tonic-gate 	char *str;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	str = pop_a_string(env, lenp);
108*0Sstevel@tonic-gate 	if (str)
109*0Sstevel@tonic-gate 		return (STRDUP(str));
110*0Sstevel@tonic-gate 	return (NULL);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  * Push Forth Double type.
115*0Sstevel@tonic-gate  */
116*0Sstevel@tonic-gate void
push_double(fcode_env_t * env,dforth_t d)117*0Sstevel@tonic-gate push_double(fcode_env_t *env, dforth_t d)
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	fstack_t lo, hi;
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	lo = DFORTH_LO(d);
122*0Sstevel@tonic-gate 	hi = DFORTH_HI(d);
123*0Sstevel@tonic-gate 	PUSH(DS, lo);
124*0Sstevel@tonic-gate 	PUSH(DS, hi);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate  * Pop Forth Double type.
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate dforth_t
pop_double(fcode_env_t * env)131*0Sstevel@tonic-gate pop_double(fcode_env_t *env)
132*0Sstevel@tonic-gate {
133*0Sstevel@tonic-gate 	fstack_t lo, hi;
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	hi = POP(DS);
136*0Sstevel@tonic-gate 	lo = POP(DS);
137*0Sstevel@tonic-gate 	return (MAKE_DFORTH(hi, lo));
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * Peek at top of stack Forth Double type.
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate dforth_t
peek_double(fcode_env_t * env)144*0Sstevel@tonic-gate peek_double(fcode_env_t *env)
145*0Sstevel@tonic-gate {
146*0Sstevel@tonic-gate 	dforth_t a;
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	a = pop_double(env);
149*0Sstevel@tonic-gate 	push_double(env, a);
150*0Sstevel@tonic-gate 	return (a);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate void
run_fcode(fcode_env_t * env,uchar_t * buff,int len)154*0Sstevel@tonic-gate run_fcode(fcode_env_t *env, uchar_t *buff, int len)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	int i;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/*
159*0Sstevel@tonic-gate 	 * Really just checking to see if buff is all ascii characters.
160*0Sstevel@tonic-gate 	 * Fcode normally starts with 0xfd, so for fcode, this should be
161*0Sstevel@tonic-gate 	 * a fast check.
162*0Sstevel@tonic-gate 	 */
163*0Sstevel@tonic-gate 	for (i = 0; i < len; i++)
164*0Sstevel@tonic-gate 		if (buff[i] >= 0x80)
165*0Sstevel@tonic-gate 			break;
166*0Sstevel@tonic-gate 	PUSH(DS, (fstack_t)buff);
167*0Sstevel@tonic-gate 	if (i < len) {
168*0Sstevel@tonic-gate 		/* Non-ascii found, probably Fcode */
169*0Sstevel@tonic-gate 		PUSH(DS, (fstack_t)1);
170*0Sstevel@tonic-gate 		byte_load(env);
171*0Sstevel@tonic-gate 	} else {
172*0Sstevel@tonic-gate 		/* All ascii found, probably ascii */
173*0Sstevel@tonic-gate 		PUSH(DS, len);
174*0Sstevel@tonic-gate 		fevaluate(env);
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate void
run_fcode_from_file(fcode_env_t * env,char * fname,int aout_flag)179*0Sstevel@tonic-gate run_fcode_from_file(fcode_env_t *env, char *fname, int aout_flag)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	uchar_t *p;
182*0Sstevel@tonic-gate 	int len;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	push_a_string(env, fname);
185*0Sstevel@tonic-gate 	load_file(env);
186*0Sstevel@tonic-gate 	len = POP(DS);
187*0Sstevel@tonic-gate 	p = (uchar_t *)POP(DS);
188*0Sstevel@tonic-gate 	if (aout_flag) {
189*0Sstevel@tonic-gate 		p += 0x20;
190*0Sstevel@tonic-gate 		len -= 0x20;
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 	run_fcode(env, p, len);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate fcode_env_t *
clone_environment(fcode_env_t * src,void * private)196*0Sstevel@tonic-gate clone_environment(fcode_env_t *src, void *private)
197*0Sstevel@tonic-gate {
198*0Sstevel@tonic-gate 	fcode_env_t *env;
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	if (!src) {
201*0Sstevel@tonic-gate 		src = initial_env;
202*0Sstevel@tonic-gate 		src->private = private;
203*0Sstevel@tonic-gate 		return (src);
204*0Sstevel@tonic-gate 	}
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate #if 0
207*0Sstevel@tonic-gate 	src->private = private;
208*0Sstevel@tonic-gate 	if (src->my_self || src->state) {
209*0Sstevel@tonic-gate 		log_message(MSG_WARN, "Can't clone an active instance or"
210*0Sstevel@tonic-gate 		    " compile state!\n");
211*0Sstevel@tonic-gate 		return (NULL);
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	log_message(MSG_WARN, "Warning: Device-tree state is shared!\n");
215*0Sstevel@tonic-gate #endif
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	env = MALLOC(sizeof (fcode_env_t));
218*0Sstevel@tonic-gate 	memcpy(env, src, sizeof (fcode_env_t));
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate #if 0
221*0Sstevel@tonic-gate 	env->table = MALLOC((MAX_FCODE + 1) * sizeof (fcode_token));
222*0Sstevel@tonic-gate 	memcpy(env->table, src->table, (MAX_FCODE + 1) * sizeof (fcode_token));
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	/*
225*0Sstevel@tonic-gate 	 * Note that cloning the dictionary doesn't make sense unless the
226*0Sstevel@tonic-gate 	 * ptrs + XT's in the dictionary are relative to BASE.
227*0Sstevel@tonic-gate 	 */
228*0Sstevel@tonic-gate 	env->base = MALLOC(dict_size);
229*0Sstevel@tonic-gate 	memcpy(env->base, src->base, dict_size);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	env->here = src->base - (uchar_t *)src + env->base;
232*0Sstevel@tonic-gate #endif
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	env->ds0 = MALLOC(stack_size * sizeof (fstack_t));
235*0Sstevel@tonic-gate 	memcpy(env->ds0, src->ds0, stack_size * sizeof (fstack_t));
236*0Sstevel@tonic-gate 	env->ds = src->ds - src->ds0 + env->ds0;
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	env->rs0 = MALLOC(stack_size * sizeof (fstack_t));
239*0Sstevel@tonic-gate 	memcpy(env->rs0, src->rs0, stack_size * sizeof (fstack_t));
240*0Sstevel@tonic-gate 	env->rs = src->rs - src->rs0 + env->rs0;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	env->order = MALLOC(MAX_ORDER * sizeof (token_t));
243*0Sstevel@tonic-gate 	memcpy(env->order, src->order, MAX_ORDER * sizeof (token_t));
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	env->input = MALLOC(sizeof (input_typ));
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	env->catch_frame = 0;
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate 	IP = 0;
250*0Sstevel@tonic-gate 
251*0Sstevel@tonic-gate 	return (env);
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate void
destroy_environment(fcode_env_t * env)255*0Sstevel@tonic-gate destroy_environment(fcode_env_t *env)
256*0Sstevel@tonic-gate {
257*0Sstevel@tonic-gate 	FREE(env->input);
258*0Sstevel@tonic-gate 	FREE(env->order);
259*0Sstevel@tonic-gate 	FREE(env->ds0);
260*0Sstevel@tonic-gate 	FREE(env->rs0);
261*0Sstevel@tonic-gate #if 0
262*0Sstevel@tonic-gate 	FREE(env->base);
263*0Sstevel@tonic-gate 	FREE(env->table);
264*0Sstevel@tonic-gate #endif
265*0Sstevel@tonic-gate 	FREE(env);
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate 	if (env == initial_env) {
268*0Sstevel@tonic-gate 		/* This call only happens internally */
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 		initial_env = NULL;
271*0Sstevel@tonic-gate 		/* You had better not exercise the engine anymore! */
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate }
274