interp_forth.c revision 61371
1/* 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/boot/common/interp_forth.c 61371 2000-06-07 21:57:23Z dcs $ 27 */ 28 29#include <sys/param.h> /* to pick up __FreeBSD_version */ 30#include <string.h> 31#include <stand.h> 32#include "bootstrap.h" 33#include "ficl.h" 34 35extern char bootprog_rev[]; 36 37/* #define BFORTH_DEBUG */ 38 39#ifdef BFORTH_DEBUG 40# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args) 41#else 42# define DEBUG(fmt, args...) 43#endif 44 45/* 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. 49 */ 50#define BF_PARSE 100 51 52/* 53 * BootForth Interface to Ficl Forth interpreter. 54 */ 55 56FICL_VM *bf_vm; 57FICL_WORD *pInterp; 58 59/* 60 * Shim for taking commands from BF and passing them out to 'standard' 61 * argv/argc command functions. 62 */ 63static void 64bf_command(FICL_VM *vm) 65{ 66 char *name, *line, *tail, *cp; 67 int len; 68 struct bootblk_command **cmdp; 69 bootblk_cmd_t *cmd; 70 int nstrings, i; 71 int argc, result; 72 char **argv; 73 74 /* Get the name of the current word */ 75 name = vm->runningWord->name; 76 77 /* Find our command structure */ 78 cmd = NULL; 79 SET_FOREACH(cmdp, Xcommand_set) { 80 if (((*cmdp)->c_name != NULL) && !strcmp(name, (*cmdp)->c_name)) 81 cmd = (*cmdp)->c_fn; 82 } 83 if (cmd == NULL) 84 panic("callout for unknown command '%s'", name); 85 86 /* Check whether we have been compiled or are being interpreted */ 87 if (stackPopINT(vm->pStack)) { 88 /* 89 * Get parameters from stack, in the format: 90 * an un ... a2 u2 a1 u1 n -- 91 * Where n is the number of strings, a/u are pairs of 92 * address/size for strings, and they will be concatenated 93 * in LIFO order. 94 */ 95 nstrings = stackPopINT(vm->pStack); 96 for (i = 0, len = 0; i < nstrings; i++) 97 len += stackFetch(vm->pStack, i * 2).i + 1; 98 line = malloc(strlen(name) + len + 1); 99 strcpy(line, name); 100 101 if (nstrings) 102 for (i = 0; i < nstrings; i++) { 103 len = stackPopINT(vm->pStack); 104 cp = stackPopPtr(vm->pStack); 105 strcat(line, " "); 106 strncat(line, cp, len); 107 } 108 } else { 109 /* Get remainder of invocation */ 110 tail = vmGetInBuf(vm); 111 for (cp = tail, len = 0; cp != vm->tib.end && *cp != 0 && *cp != '\n'; cp++, len++) 112 ; 113 114 line = malloc(strlen(name) + len + 2); 115 strcpy(line, name); 116 if (len > 0) { 117 strcat(line, " "); 118 strncat(line, tail, len); 119 vmUpdateTib(vm, tail + len); 120 } 121 } 122 DEBUG("cmd '%s'", line); 123 124 command_errmsg = command_errbuf; 125 command_errbuf[0] = 0; 126 if (!parse(&argc, &argv, line)) { 127 result = (cmd)(argc, argv); 128 free(argv); 129 } else { 130 result=BF_PARSE; 131 } 132 free(line); 133 /* This is going to be thrown!!! */ 134 stackPushINT(vm->pStack,result); 135} 136 137/* 138 * Replace a word definition (a builtin command) with another 139 * one that: 140 * 141 * - Throw error results instead of returning them on the stack 142 * - Pass a flag indicating whether the word was compiled or is 143 * being interpreted. 144 * 145 * There is one major problem with builtins that cannot be overcome 146 * in anyway, except by outlawing it. We want builtins to behave 147 * differently depending on whether they have been compiled or they 148 * are being interpreted. Notice that this is *not* the interpreter's 149 * current state. For example: 150 * 151 * : example ls ; immediate 152 * : problem example ; \ "ls" gets executed while compiling 153 * example \ "ls" gets executed while interpreting 154 * 155 * Notice that, though the current state is different in the two 156 * invocations of "example", in both cases "ls" has been 157 * *compiled in*, which is what we really want. 158 * 159 * The problem arises when you tick the builtin. For example: 160 * 161 * : example-1 ['] ls postpone literal ; immediate 162 * : example-2 example-1 execute ; immediate 163 * : problem example-2 ; 164 * example-2 165 * 166 * We have no way, when we get EXECUTEd, of knowing what our behavior 167 * should be. Thus, our only alternative is to "outlaw" this. See RFI 168 * 0007, and ANS Forth Standard's appendix D, item 6.7 for a related 169 * problem, concerning compile semantics. 170 * 171 * The problem is compounded by the fact that "' builtin CATCH" is valid 172 * and desirable. The only solution is to create an intermediary word. 173 * For example: 174 * 175 * : my-ls ls ; 176 * : example ['] my-ls catch ; 177 * 178 * So, with the below implementation, here is a summary of the behavior 179 * of builtins: 180 * 181 * ls -l \ "interpret" behavior, ie, 182 * \ takes parameters from TIB 183 * : ex-1 s" -l" 1 ls ; \ "compile" behavior, ie, 184 * \ takes parameters from the stack 185 * : ex-2 ['] ls catch ; immediate \ undefined behavior 186 * : ex-3 ['] ls catch ; \ undefined behavior 187 * ex-2 ex-3 \ "interpret" behavior, 188 * \ catch works 189 * : ex-4 ex-2 ; \ "compile" behavior, 190 * \ catch does not work 191 * : ex-5 ex-3 ; immediate \ same as ex-2 192 * : ex-6 ex-3 ; \ same as ex-3 193 * : ex-7 ['] ex-1 catch ; \ "compile" behavior, 194 * \ catch works 195 * : ex-8 postpone ls ; immediate \ same as ex-2 196 * : ex-9 postpone ls ; \ same as ex-3 197 * 198 * As the definition below is particularly tricky, and it's side effects 199 * must be well understood by those playing with it, I'll be heavy on 200 * the comments. 201 * 202 * (if you edit this definition, pay attention to trailing spaces after 203 * each word -- I warned you! :-) ) 204 */ 205#define BUILTIN_CONSTRUCTOR \ 206": builtin: " \ 207 ">in @ " /* save the tib index pointer */ \ 208 "' " /* get next word's xt */ \ 209 "swap >in ! " /* point again to next word */ \ 210 "create " /* create a new definition of the next word */ \ 211 ", " /* save previous definition's xt */ \ 212 "immediate " /* make the new definition an immediate word */ \ 213 \ 214 "does> " /* Now, the *new* definition will: */ \ 215 "state @ if " /* if in compiling state: */ \ 216 "1 postpone literal " /* pass 1 flag to indicate compile */ \ 217 "@ compile, " /* compile in previous definition */ \ 218 "postpone throw " /* throw stack-returned result */ \ 219 "else " /* if in interpreting state: */ \ 220 "0 swap " /* pass 0 flag to indicate interpret */ \ 221 "@ execute " /* call previous definition */ \ 222 "throw " /* throw stack-returned result */ \ 223 "then ; " 224 225/* 226 * Initialise the Forth interpreter, create all our commands as words. 227 */ 228void 229bf_init(void) 230{ 231 struct bootblk_command **cmdp; 232 char create_buf[41]; /* 31 characters-long builtins */ 233 int fd; 234 235 ficlInitSystem(10000); /* Default dictionary ~4000 cells */ 236 bf_vm = ficlNewVM(); 237 238 /* Put all private definitions in a "builtins" vocabulary */ 239 ficlExec(bf_vm, "vocabulary builtins also builtins definitions"); 240 241 /* Builtin constructor word */ 242 ficlExec(bf_vm, BUILTIN_CONSTRUCTOR); 243 244 /* make all commands appear as Forth words */ 245 SET_FOREACH(cmdp, Xcommand_set) { 246 ficlBuild((*cmdp)->c_name, bf_command, FW_DEFAULT); 247 ficlExec(bf_vm, "also forth definitions"); 248 sprintf(create_buf, "builtin: %s", (*cmdp)->c_name); 249 ficlExec(bf_vm, create_buf); 250 ficlExec(bf_vm, "previous definitions"); 251 } 252 ficlExec(bf_vm, "only forth definitions"); 253 254 /* Export some version numbers so that code can detect the loader/host version */ 255 ficlSetEnv("FreeBSD_version", __FreeBSD_version); 256 ficlSetEnv("loader_version", 257 (bootprog_rev[0] - '0') * 10 + (bootprog_rev[2] - '0')); 258 259 /* try to load and run init file if present */ 260 if ((fd = open("/boot/boot.4th", O_RDONLY)) != -1) { 261 (void)ficlExecFD(bf_vm, fd); 262 close(fd); 263 } 264 265 /* Do this last, so /boot/boot.4th can change it */ 266 pInterp = ficlLookup("interpret"); 267} 268 269/* 270 * Feed a line of user input to the Forth interpreter 271 */ 272int 273bf_run(char *line) 274{ 275 int result; 276 CELL id; 277 278 id = bf_vm->sourceID; 279 bf_vm->sourceID.i = -1; 280 result = ficlExec(bf_vm, line); 281 bf_vm->sourceID = id; 282 283 DEBUG("ficlExec '%s' = %d", line, result); 284 switch (result) { 285 case VM_OUTOFTEXT: 286 case VM_ABORTQ: 287 case VM_QUIT: 288 case VM_ERREXIT: 289 break; 290 case VM_USEREXIT: 291 printf("No where to leave to!\n"); 292 break; 293 case VM_ABORT: 294 printf("Aborted!\n"); 295 break; 296 case BF_PARSE: 297 printf("Parse error!\n"); 298 break; 299 default: 300 /* Hopefully, all other codes filled this buffer */ 301 printf("%s\n", command_errmsg); 302 } 303 304 if (result == VM_USEREXIT) 305 panic("interpreter exit"); 306 setenv("interpret", bf_vm->state ? "" : "ok", 1); 307 308 return result; 309} 310