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