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