2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
29 #include <sys/param.h> /* to pick up __FreeBSD_version */
32 #include "bootstrap.h"
35 extern char bootprog_rev
[];
37 /* #define BFORTH_DEBUG */
40 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
42 # define DEBUG(fmt, args...)
46 * Eventually, all builtin commands throw codes must be defined
47 * elsewhere, possibly bootstrap.h. For now, just this code, used
48 * just in this file, it is getting defined.
53 * FreeBSD loader default dictionary cells
56 #define BF_DICTSIZE 30000
60 * BootForth Interface to Ficl Forth interpreter.
63 ficlSystemInformation
*fsi
;
69 * Shim for taking commands from BF and passing them out to 'standard'
70 * argv/argc command functions.
73 bf_command(ficlVm
*vm
)
75 char *name
, *line
, *tail
, *cp
;
77 struct bootblk_command
**cmdp
;
83 /* Get the name of the current word */
84 name
= vm
->runningWord
->name
;
86 /* Find our command structure */
88 SET_FOREACH(cmdp
, Xcommand_set
) {
89 if (((*cmdp
)->c_name
!= NULL
) && !strcmp(name
, (*cmdp
)->c_name
))
93 panic("callout for unknown command '%s'", name
);
95 /* Check whether we have been compiled or are being interpreted */
96 if (ficlStackPopInteger(ficlVmGetDataStack(vm
))) {
98 * Get parameters from stack, in the format:
99 * an un ... a2 u2 a1 u1 n --
100 * Where n is the number of strings, a/u are pairs of
101 * address/size for strings, and they will be concatenated
104 nstrings
= ficlStackPopInteger(ficlVmGetDataStack(vm
));
105 for (i
= 0, len
= 0; i
< nstrings
; i
++)
106 len
+= ficlStackFetch(ficlVmGetDataStack(vm
), i
* 2).i
+ 1;
107 line
= malloc(strlen(name
) + len
+ 1);
111 for (i
= 0; i
< nstrings
; i
++) {
112 len
= ficlStackPopInteger(ficlVmGetDataStack(vm
));
113 cp
= ficlStackPopPointer(ficlVmGetDataStack(vm
));
115 strncat(line
, cp
, len
);
118 /* Get remainder of invocation */
119 tail
= ficlVmGetInBuf(vm
);
121 for (cp
= tail
, len
= 0; cp
!= vm
->tib
.end
&& *cp
!= 0 && *cp
!= '\n'; cp
++, len
++)
124 line
= malloc(strlen(name
) + len
+ 2);
128 strncat(line
, tail
, len
);
129 ficlVmUpdateTib(vm
, tail
+ len
);
132 DEBUG("cmd '%s'", line
);
134 command_errmsg
= command_errbuf
;
135 command_errbuf
[0] = 0;
136 if (!parse(&argc
, &argv
, line
)) {
137 result
= (cmd
)(argc
, argv
);
145 printf("%s\n", command_errmsg
);
148 panic("%s\n", command_errmsg
);
153 * If there was error during nested ficlExec(), we may no longer have
154 * valid environment to return. Throw all exceptions from here.
156 if (result
!= CMD_OK
)
157 ficlVmThrow(vm
, result
);
159 /* This is going to be thrown!!! */
160 ficlStackPushInteger(ficlVmGetDataStack(vm
),result
);
164 * Replace a word definition (a builtin command) with another
167 * - Throw error results instead of returning them on the stack
168 * - Pass a flag indicating whether the word was compiled or is
171 * There is one major problem with builtins that cannot be overcome
172 * in anyway, except by outlawing it. We want builtins to behave
173 * differently depending on whether they have been compiled or they
174 * are being interpreted. Notice that this is *not* the interpreter's
175 * current state. For example:
177 * : example ls ; immediate
178 * : problem example ; \ "ls" gets executed while compiling
179 * example \ "ls" gets executed while interpreting
181 * Notice that, though the current state is different in the two
182 * invocations of "example", in both cases "ls" has been
183 * *compiled in*, which is what we really want.
185 * The problem arises when you tick the builtin. For example:
187 * : example-1 ['] ls postpone literal ; immediate
188 * : example-2 example-1 execute ; immediate
189 * : problem example-2 ;
192 * We have no way, when we get EXECUTEd, of knowing what our behavior
193 * should be. Thus, our only alternative is to "outlaw" this. See RFI
194 * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related
195 * problem, concerning compile semantics.
197 * The problem is compounded by the fact that "' builtin CATCH" is valid
198 * and desirable. The only solution is to create an intermediary word.
202 * : example ['] my-ls catch ;
204 * So, with the below implementation, here is a summary of the behavior
207 * ls -l \ "interpret" behavior, ie,
208 * \ takes parameters from TIB
209 * : ex-1 s" -l" 1 ls ; \ "compile" behavior, ie,
210 * \ takes parameters from the stack
211 * : ex-2 ['] ls catch ; immediate \ undefined behavior
212 * : ex-3 ['] ls catch ; \ undefined behavior
213 * ex-2 ex-3 \ "interpret" behavior,
215 * : ex-4 ex-2 ; \ "compile" behavior,
216 * \ catch does not work
217 * : ex-5 ex-3 ; immediate \ same as ex-2
218 * : ex-6 ex-3 ; \ same as ex-3
219 * : ex-7 ['] ex-1 catch ; \ "compile" behavior,
221 * : ex-8 postpone ls ; immediate \ same as ex-2
222 * : ex-9 postpone ls ; \ same as ex-3
224 * As the definition below is particularly tricky, and it's side effects
225 * must be well understood by those playing with it, I'll be heavy on
228 * (if you edit this definition, pay attention to trailing spaces after
229 * each word -- I warned you! :-) )
231 #define BUILTIN_CONSTRUCTOR \
233 ">in @ " /* save the tib index pointer */ \
234 "' " /* get next word's xt */ \
235 "swap >in ! " /* point again to next word */ \
236 "create " /* create a new definition of the next word */ \
237 ", " /* save previous definition's xt */ \
238 "immediate " /* make the new definition an immediate word */ \
240 "does> " /* Now, the *new* definition will: */ \
241 "state @ if " /* if in compiling state: */ \
242 "1 postpone literal " /* pass 1 flag to indicate compile */ \
243 "@ compile, " /* compile in previous definition */ \
244 "postpone throw " /* throw stack-returned result */ \
245 "else " /* if in interpreting state: */ \
246 "0 swap " /* pass 0 flag to indicate interpret */ \
247 "@ execute " /* call previous definition */ \
248 "throw " /* throw stack-returned result */ \
252 * Initialise the Forth interpreter, create all our commands as words.
257 struct bootblk_command
**cmdp
;
258 char create_buf
[41]; /* 31 characters-long builtins */
260 ficlDictionary
*dict
;
263 fsi
= malloc(sizeof(ficlSystemInformation
));
264 ficlSystemInformationInitialize(fsi
);
265 fsi
->dictionarySize
= BF_DICTSIZE
;
267 bf_sys
= ficlSystemCreate(fsi
);
268 bf_vm
= ficlSystemCreateVm(bf_sys
);
270 /* Put all private definitions in a "builtins" vocabulary */
271 rv
= ficlVmEvaluate(bf_vm
, "vocabulary builtins also builtins definitions");
272 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
273 panic("error interpreting forth: %d\n", rv
);
276 /* Builtin constructor word */
277 rv
= ficlVmEvaluate(bf_vm
, BUILTIN_CONSTRUCTOR
);
278 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
279 panic("error interpreting forth: %d\n", rv
);
282 /* make all commands appear as Forth words */
283 dict
= ficlSystemGetDictionary(bf_sys
);
284 SET_FOREACH(cmdp
, Xcommand_set
) {
285 ficlDictionaryAppendPrimitive(dict
, (char *)(*cmdp
)->c_name
,
286 bf_command
, FICL_WORD_DEFAULT
);
287 rv
= ficlVmEvaluate(bf_vm
, "forth definitions builtins");
288 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
289 panic("error interpreting forth: %d\n", rv
);
291 sprintf(create_buf
, "builtin: %s", (*cmdp
)->c_name
);
292 rv
= ficlVmEvaluate(bf_vm
, create_buf
);
293 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
294 panic("error interpreting forth: %d\n", rv
);
296 rv
= ficlVmEvaluate(bf_vm
, "builtins definitions");
297 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
298 panic("error interpreting forth: %d\n", rv
);
301 rv
= ficlVmEvaluate(bf_vm
, "only forth definitions");
302 if (rv
!= FICL_VM_STATUS_OUT_OF_TEXT
) {
303 panic("error interpreting forth: %d\n", rv
);
307 * Export some version numbers so that code can detect the loader/host
310 env
= ficlSystemGetEnvironment(bf_sys
);
311 ficlDictionarySetConstant(env
, "loader_version",
312 (bootprog_rev
[0] - '0') * 10 + (bootprog_rev
[2] - '0'));
314 pInterp
= ficlSystemLookup(bf_sys
, "interpret");
316 /* try to load and run init file if present */
318 rc
= "/boot/forth/boot.4th";
320 fd
= open(rc
, O_RDONLY
);
322 (void)ficlExecFD(bf_vm
, fd
);
327 /* Do this again, so that interpret can be redefined. */
328 pInterp
= ficlSystemLookup(bf_sys
, "interpret");
332 * Feed a line of user input to the Forth interpreter
340 FICL_STRING_SET_FROM_CSTRING(s
, line
);
341 result
= ficlVmExecuteString(bf_vm
, s
);
343 DEBUG("ficlExec '%s' = %d", line
, result
);
345 case FICL_VM_STATUS_OUT_OF_TEXT
:
346 case FICL_VM_STATUS_ABORTQ
:
347 case FICL_VM_STATUS_QUIT
:
348 case FICL_VM_STATUS_ERROR_EXIT
:
350 case FICL_VM_STATUS_USER_EXIT
:
351 printf("No where to leave to!\n");
353 case FICL_VM_STATUS_ABORT
:
354 printf("Aborted!\n");
357 printf("Parse error!\n");
360 /* Hopefully, all other codes filled this buffer */
361 printf("%s\n", command_errmsg
);
364 /* bye is same as reboot and will behave depending on platform */
365 if (result
== FICL_VM_STATUS_USER_EXIT
)
367 setenv("interpret", bf_vm
->state
? "" : "ok", 1);