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