3 ms·
>"This is StoneKnifeForth, a very simple language inspired by Forth. It is not expected to be useful; instead, its purpose is to show how simple a compiler can
by peter_d_sherman 4y ago
>"This is StoneKnifeForth, a very simple language inspired by Forth. It is not expected to be useful; instead, its purpose is to show how simple a compiler can be.
The compiler is a bit under two pages of code when the comments are removed.
This package includes a “metacircular compiler” which is written in StoneKnifeForth and compiles StoneKnifeForth to an x86 Linux ELF executable."
[...]
"The output executable is 4063 bytes, containing about 1400 instructions."
My thoughts: That would just about fit on a VIC-20...
https://github.com/kragen/stoneknifeforth/blob/master/tinyboot1.tbf1 https://github.com/kragen/stoneknifeforth/blob/master/tinybo...
- kragen 4y agoYou could probably port it to a VIC-20, but even though about 15% of the code is the code to produce ELF headers, it would probably be larger because the i386 has denser code than the 6502. The Type and Addr parallel arrays that form SKF's "symbol table" are 1024 bytes each, so you might want to change that; presumably you'd use 16-bit addresses instead of 32-bit, and you could rule out the use of "extended ASCII" bytes, and so use only 256 bytes for Type and 256 bytes for Addr. It still might be impossible to get it to run in a base VIC-20 with only 5KiB of RAM, because that has to be able to contain two copies of the compiler: the one that's running and the one that's being built, so you'd likely have to trim it down a lot. The biggest unknown would be how much code you need for I/O. If you get it working I'd love to hear about it!
- peter_d_sherman 4y agoHi kragen! I've always loved the various Forth implementations -- especially the more terse ones! That's because in the continuity spectrum of Compilers/OS'es (perhaps 'OS' isn't the best terminology here, perhaps "minimal and most simple code necessary to have a functional development environment which can be used to bootstrap more complex development environments") Forth is about as close to "bare metal" while still being functional and expandable as you can get... (comparable things include Turing-complete self compilers that include a REPL loop of sorts, and MS-DOS's "debug.com" -- which is sort of "half quasi OS" and "half development environment" -- well, early development environment -- again for lack of better terms!) Anyway, your StoneKnifeForth -- deserves high commendations for its simplicity, terseness, readability, understandability -- and overall elegance! Well done! The fact that you've added the ability to boot it on an old 386 PC -- is the proverbial "icing on the cake"! Now, as far as the VIC-20 aspect of things go, I don't plan on porting it to a VIC-20 today or tomorrow; at least, not in the immediate immediate future... But I think it would be highly interesting to learn just how small a fully functional Forth environment could be packed; in other words, what's the absolute minimum computer (I named the VIC-20 as a popular example of a very and I mean, very (8-bit, 5K RAM, etc.) resource constrained system!) that would be needed to run it? That is, maybe there are older computers which are still more resource constrained (2K, 1K, ?), that can still run enough Forth such that the development environment can be extended in some meaningful way -- in whatever little memory remains... If I ever taught an OS/Compiler class in the future and/or wrote an OS/Compiler book -- I would love to teach/refer to StoneKnifeForth -- as part of the curriculum! Anyway, great work!
- kragen 4y agoI'm glad you enjoyed it! Have you tried running it? One could reasonably argue that SKF isn't really "booting" on an old 386 PC; though it'll probably run on one, you need to get Linux running on the PC first, because SKF can't read the input source code from disk or write the output executable to disk on its own, and it needs something to load it into memory and start it running as well. And, unlike real Forths, it's a batch-mode compiler: it can't be used interactively. The sense in which it's a "tiny bootstrap" is that it's a compiler that compiles itself, not the sense of "bootstrapping" that means to load oneself into RAM. (I know you know this, but anyone else reading this later may not.) — ⁂ — In 02007 I wrote https://github.com/kragen/tokthr https://github.com/kragen/tokthr as an investigation into precisely the question of "just how small a fully functional Forth environment could be packed". By using bytecode ("token threading") rather than DTC or ITC, tokthr provides "90% of" a traditional interactive Forth environment in about 1400 bytes, all bytecode except for a 239-byte machine-code core. Unfortunately, because it's incomplete, it's impossible to know whether the remaining 10% of the functionality requires adding another 10% to the code or another 90%—as in the old joke about how the first 90% of the project takes the first 90% of the schedule, and the remaining 10% takes the other 90%. My best guess, though, is that it'd be about 1700 bytes, so in 2KiB you'd have about 350 bytes left for the user program, which is probably about 64–128 lines of code. Interpretation can be slow, but it sure makes for denser code, and you don't need to have two copies of the interpreted code in RAM when you're bootstrapping. There have definitely been 8KiB interactive Forth systems, and I think there have been 4KiB ones, which was also the size Wozniak required for a BASIC interpreter on the Apple. tokthr suggests that 2KiB might be achievable for a stripped-down Forth. Óscar Toledo G.'s bootOS https://github.com/nanochess/bootOS https://github.com/nanochess/bootOS is a particularly interesting non-Forth system in this OS/IDE/debugger vein, because it has enough functionality to write programs (in hexadecimal machine code), load them from disk, edit them, and save them, all in 512 bytes of code. It does require somewhat more than 512 bytes of RAM, but still very little—much less than has ever been present on a machine that it could run on. — ⁂ — My thinking on Forth, which is something I'm an amateur at, has changed a little bit since I wrote tokthr and SKF. I think that if you evaluate Forth as a programming language, it's generally going to look pretty deficient. It's more bug-prone and harder to read than alternatives like C, Lisp, BASIC, or assembly language, although it does support compile-time metaprogramming and powerful abstraction facilities. But if you ask the question, "What's the simplest usable development environment I can build?" the answer starts to look a lot like Forth, especially if you're running on a poorly characterized piece of hardware where you need to prioritize interactive experimentation with I/O ports. It gives you an interactive, scriptable environment for poking around, similar to what Tcl gives you https://yosefk.com/blog/i-cant-believe-im-praising-tcl.html https://yosefk.com/blog/i-cant-believe-im-praising-tcl.html but in 4 kilobytes rather than the megabyte and a half Tcl demands. And the environment includes not just a scriptable command line and the usual programming-language things like arithmetic, subroutines, variables, and arrays, but also compile-time metaprogramming, virtual memory, an editor that allows you to recompile code at screenful granularity (usually), a memory dumper, access to raw hardware devices, an assembler (usually), sometimes multithreading, and a sort of debugger. As in BASIC or Lisp or Python or Tcl, your UI is a REPL, so your programming language is also your command language; but, unlike in BASIC, your command language is extensible, so in many cases it can also work as the user interface to your application. In BASIC or in a C debugger you number the lines of code in your program; in Forth you name them, so you can "single-step" your program by running them one at a time. Then the ? word gives you a quick way to examine your variable values interactively, again as in a debugger. Like, here's me trying to interactively debug a square-root routine into existence in GForth on Linux: variable square variable guess ok : sqstep square @ guess @ / guess + 2/ guess ! ; ok 80346 square ! 1 guess ! ok sqstep ok guess ? 70107476356745 ok sqstep guess ? 70107476316572 ok sqstep guess ? 70107476316572 ok Hmm, I don't know how that's possible, is division doing some kind of double precision thing I've forgotten about? 80346 1 / . 80346 ok Oh, oops, I was adding the memory address of GUESS instead of the value stored in it. : sqstep square @ guess @ / guess @ + 2/ guess ! ; redefined sqstep ok 1 guess ! sqstep guess ? 40173 ok Skipping over 42 more lines of embarrassing floundering around over half an hour omitted (see interactive-forth.md in http://canonical.org/~kragen/pavnotes2.git http://canonical.org/~kragen/pavnotes2.git if you want the details) I eventually ended up with the following working code: : newtloop begin sqstep guess @ dup * square @ - abs guess @ < until ; redefined newtloop ok : newtsqrt dup square ! guess ! newtloop guess @ ; redefined newtsqrt ok 120 newtsqrt . 11 ok By virtue of doing everything interactively and in the most incremental possible way, I was able to get it to work properly even though I kept making dumb type errors, inverted the sense of my tests, couldn't remember the semantics of division, and so on. I was able to blunder my way into working code. And that's a very powerful thing, as you know if you've ever debugged someone else's Excel spreadsheet. And, in cases where the behavior of the underlying system is actually unknown—maybe it's a buggy hardware prototype—that kind of instant experimentation is the best possible facility to have at your fingertips. — ⁂ — However, in the case of the square root function, I probably would have gotten it working faster by reasoning through the algorithm's behavior instead of blundering around. In fact, it was only when I stopped for a while to think about the problem that I came up with the termination approach used in the code above. In this sense Forth's interactivity, like Excel's, can be an "attractive nuisance": a thing that tempts you into getting yourself into trouble. Being able to read the code easily so the bugs in it were obvious probably would have been helpful, too. The other attractive nuisance in Forth is cleverness. Forth is a puzzle box: you can always find a cleverer way to do whatever you've done, for whatever your ideal of cleverness is. You can always find a way to do it in Forth in less RAM, fewer lines of code, fewer VARIABLEs, in a more orthogonal and reusable way, with words that read more naturally, etc. (A particularly pernicious form of the cleverness problem is trying to use the stacks instead of VARIABLEs, which is always possible in the Turing-tarpit way but results in very slow progress.) It's the opposite extreme from what MJD says about Java in https://blog.plover.com/prog/Java.html https://blog.plover.com/prog/Java.html: > I am glad to have had the experience of programming in Java. I liked programming in Java mainly because I found it very relaxing. With a bad language, like say Fortran or csh, you struggle to do anything at all, and the language fights with you every step of the way forward. With a good language there is a different kind of struggle, to take advantage of the language's strengths, to get the maximum amount of functionality, and to achieve the clearest possible expression. > Java is neither a good nor a bad language. It is a mediocre language, and there is no struggle. In Haskell or even in Perl you are always worrying about whether you are doing something in the cleanest and the best way. In Java, you can forget about doing it in the cleanest or the best way, because that is impossible. Whatever you do, however hard you try, the code will come out mediocre, verbose, redundant, and bloated, and the only thing you can do is relax and keep turning the crank until the necessary amount of code has come out of the spout. If it takes ten times as much code as it would to program in Haskell, that is all right, because the IDE will generate half of it for you, and you are still being paid to write the other half. Forth is, in this taxonomy, a good language. In my attempt above it took me half an hour to write a simple square root function, which you could maybe describe as "you struggle to do anything at all, and the language fights with you every step of the way forward," but in fact the language semantics were perfectly adequate, just unfamiliar, and most of my problem was a lack of mental clarity about the algorithm. As long as you use variables roughly as often as you'd use them in other programming languages, instead of bending over backwards to never use them, there's nothing particularly difficult about writing code in Forth rather than C; it's a purely syntactic transformation from if (a >= MOD_ADLER) a -= MOD_ADLER; to a @ MOD_ADLER >= if MOD_ADLER negate a +! then and the like. And also you can do compile-time metaprogramming and you have closures so you don't suffer as much as you do in C. But in Forth you're always worrying about whether you're doing something in the cleanest and the best way. Should I define a -! word? Maybe that if statement should be a word of its own that takes A and/or MOD_ADLER as parameters? Maybe I should do the subtraction and just conditionally store the result into A if it's still positive?