dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / efcode / engine / interface.c
blob31f0cc9e91d93ff9d0fce2959bb36d242045f730
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 2000 by Sun Microsystems, Inc.
24 * All rights reserved.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #include <fcode/private.h>
34 #include <fcode/log.h>
37 * the external start point for this goo
40 void
41 push_ds(fcode_env_t *env, fstack_t d)
43 PUSH(DS, d);
46 fstack_t
47 pop_ds(fcode_env_t *env)
49 return (POP(DS));
52 void
53 push_rs(fcode_env_t *env, fstack_t d)
55 PUSH(RS, d);
58 fstack_t
59 pop_rs(fcode_env_t *env)
61 return (POP(RS));
65 * Pushes a C string on the stack.
67 void
68 push_a_string(fcode_env_t *env, char *str)
70 if (str) {
71 PUSH(DS, (fstack_t)str);
72 PUSH(DS, strlen(str));
73 } else {
74 PUSH(DS, 0);
75 PUSH(DS, 0);
80 * Pops a (potentially null) string off the stack.
82 char *
83 pop_a_string(fcode_env_t *env, int *lenp)
85 int len;
86 char *str;
88 len = POP(DS);
89 str = (char *)POP(DS);
90 if (len == 0)
91 str = NULL;
92 else if (str == NULL)
93 len = 0;
94 if (lenp)
95 *lenp = len;
96 return (str);
100 * Pops & strdup's a string off the stack, handles NULL strings.
102 char *
103 pop_a_duped_string(fcode_env_t *env, int *lenp)
105 char *str;
107 str = pop_a_string(env, lenp);
108 if (str)
109 return (STRDUP(str));
110 return (NULL);
114 * Push Forth Double type.
116 void
117 push_double(fcode_env_t *env, dforth_t d)
119 fstack_t lo, hi;
121 lo = DFORTH_LO(d);
122 hi = DFORTH_HI(d);
123 PUSH(DS, lo);
124 PUSH(DS, hi);
128 * Pop Forth Double type.
130 dforth_t
131 pop_double(fcode_env_t *env)
133 fstack_t lo, hi;
135 hi = POP(DS);
136 lo = POP(DS);
137 return (MAKE_DFORTH(hi, lo));
141 * Peek at top of stack Forth Double type.
143 dforth_t
144 peek_double(fcode_env_t *env)
146 dforth_t a;
148 a = pop_double(env);
149 push_double(env, a);
150 return (a);
153 void
154 run_fcode(fcode_env_t *env, uchar_t *buff, int len)
156 int i;
159 * Really just checking to see if buff is all ascii characters.
160 * Fcode normally starts with 0xfd, so for fcode, this should be
161 * a fast check.
163 for (i = 0; i < len; i++)
164 if (buff[i] >= 0x80)
165 break;
166 PUSH(DS, (fstack_t)buff);
167 if (i < len) {
168 /* Non-ascii found, probably Fcode */
169 PUSH(DS, (fstack_t)1);
170 byte_load(env);
171 } else {
172 /* All ascii found, probably ascii */
173 PUSH(DS, len);
174 fevaluate(env);
178 void
179 run_fcode_from_file(fcode_env_t *env, char *fname, int aout_flag)
181 uchar_t *p;
182 int len;
184 push_a_string(env, fname);
185 load_file(env);
186 len = POP(DS);
187 p = (uchar_t *)POP(DS);
188 if (aout_flag) {
189 p += 0x20;
190 len -= 0x20;
192 run_fcode(env, p, len);
195 fcode_env_t *
196 clone_environment(fcode_env_t *src, void *private)
198 fcode_env_t *env;
200 if (!src) {
201 src = initial_env;
202 src->private = private;
203 return (src);
206 #if 0
207 src->private = private;
208 if (src->my_self || src->state) {
209 log_message(MSG_WARN, "Can't clone an active instance or"
210 " compile state!\n");
211 return (NULL);
214 log_message(MSG_WARN, "Warning: Device-tree state is shared!\n");
215 #endif
217 env = MALLOC(sizeof (fcode_env_t));
218 memcpy(env, src, sizeof (fcode_env_t));
220 #if 0
221 env->table = MALLOC((MAX_FCODE + 1) * sizeof (fcode_token));
222 memcpy(env->table, src->table, (MAX_FCODE + 1) * sizeof (fcode_token));
225 * Note that cloning the dictionary doesn't make sense unless the
226 * ptrs + XT's in the dictionary are relative to BASE.
228 env->base = MALLOC(dict_size);
229 memcpy(env->base, src->base, dict_size);
231 env->here = src->base - (uchar_t *)src + env->base;
232 #endif
234 env->ds0 = MALLOC(stack_size * sizeof (fstack_t));
235 memcpy(env->ds0, src->ds0, stack_size * sizeof (fstack_t));
236 env->ds = src->ds - src->ds0 + env->ds0;
238 env->rs0 = MALLOC(stack_size * sizeof (fstack_t));
239 memcpy(env->rs0, src->rs0, stack_size * sizeof (fstack_t));
240 env->rs = src->rs - src->rs0 + env->rs0;
242 env->order = MALLOC(MAX_ORDER * sizeof (token_t));
243 memcpy(env->order, src->order, MAX_ORDER * sizeof (token_t));
245 env->input = MALLOC(sizeof (input_typ));
247 env->catch_frame = 0;
249 IP = 0;
251 return (env);
254 void
255 destroy_environment(fcode_env_t *env)
257 FREE(env->input);
258 FREE(env->order);
259 FREE(env->ds0);
260 FREE(env->rs0);
261 #if 0
262 FREE(env->base);
263 FREE(env->table);
264 #endif
265 FREE(env);
267 if (env == initial_env) {
268 /* This call only happens internally */
270 initial_env = NULL;
271 /* You had better not exercise the engine anymore! */