News for hackers. Stuff that matters.
Re:StoneKnifeForth (With a Metacircular Compiler) by carapace on Thursday February 18, 2021 @10:26AM (#26180869)
For this sort of thing I bounce back and forth between targeting C vs ASM. C is so universal and there are so many tools you can use to harden it. On the other hand, you're still using a big ol' abstraction layer between C and the machine. On the gripping hand, CPUs are doing so many things under the hood now that ASM can be considered an abstraction layer obscuring the actual machine, so you kind of need a modern compiler to have a hope of taking advantage of all that without doing a ton of research.

Over on the "αcτµαlly pδrταblε εxεcµταblε" page ( https://justine.lol/ape.html ) there's a subsection called "x86-64 Linux ABI Makes a Pretty Good Lingua Franca" it seems to me to be a reasonable suggestion (although I've never like x86 myself.) You can bundle an x86 emulator with your code to run on other architectures.

I like the Project Oberon 32-bit RISC developed by Prof. Wirth as a target. It's really simple for educational purposes. (Meaning you can more easily teach more people Oberon RISC than, say, RISC-V. ALthough see the "Selfie" project: https://selfie.cs.uni-salzburg.at/ "An educational software system of a tiny self-compiling C compiler, a tiny self-executing RISC-V emulator, and a tiny self-hosting RISC-V hypervisor.")

There are many emulators for Oberon RISC already, including one in Javascript, so you can run the old Oberon OS in your browser! Launch an emulator with a disk image from here: https://schierlm.github.io/OberonEmulator/

Re:StoneKnifeForth (With a Metacircular Compiler) by astrobe_ on Thursday February 18, 2021 @01:17PM (#26183415)
I have done something along the lines of TFA a long time ago, a system that could boot from a floppy and recompile itself from source (never did anything useful with it), but today I daily use a Forth dialect written in C that is designed to play nice with C and the OS.

C is a sure bet. Interpreters that rely too much on JIT are simply not available on processors where the JIT is not available (duh), which was often the case for ARM before it became really popular. Same thing with relying on an emulator (also, you are running a VM in another kind of VM; it's usually bad for performance).

As far as speed is concerned, if your Forth implementation makes it really easy to extend, you can always get something fast enough by re-coding the critical parts in C. That's why I stopped worrying about which implementation technique is the fastest and chose the one that made it the most trivial to extend the interpreter.

Re:StoneKnifeForth (With a Metacircular Compiler) by carapace on Friday February 19, 2021 @01:42PM (#26196390)
> That's why I stopped worrying about which implementation technique is the fastest and chose the one that made it the most trivial to extend the interpreter.

I like that, it appeals to my sense of pragmatism.

I wrote a Joy interpreter in Nim the other day, and when it came time to switch from machine ints to BigInts it was a breeze. There was a library already written, I updated the type and the compiler told me what else to change, the whole thing was over in fifteen minutes. And if I ever find that integer math is a performance bottleneck there is also a wrapper for GMP ready to go.

- - - -

BTW, I'm a big fan of Oberon, and I think Astrobe is awesome and admire you for making and supporting it. Cheers!

Re:StoneKnifeForth (With a Metacircular Compiler) by guerrilla on Thursday February 18, 2021 @01:53PM (#26183935)
Is that dialect written in C available online? I'm collecting implementations at the moment. If you know any other C implementations besides the popular ones I'd be grateful for pointers to those too.
Re:StoneKnifeForth (With a Metacircular Compiler) by astrobe_ on Saturday February 20, 2021 @02:46AM (#26202395)
I've been wanting to put it online for a long time to show how I do it for cases like this, but I need to strip it first of semi-confidential stuff because I hack it a lot to do work-related things. And it also need some documentation to be useful as an "educational" Forth. Maybe one day...

For Forth implementations, although I suspect it might be yourself (you gotta admit this sort of fetish is not very common ;-), Charles Childers [1] is collecting them.

[1] https://github.com/crcx?tab=repositories

Re:StoneKnifeForth (With a Metacircular Compiler) by kragen on Thursday February 18, 2021 @11:22PM (#26189688)
pfe ("portable Forth environment") was the first one I used; it was included in Slackware96 and included a Tetris game. You're probably aware GForth, BigForth, and F-83 are mostly written in Forth. I recently read through Retro-Forth, which compiles to an interpreter for a bytecode called Nga, written in C. There was another one I ran into recently but can't find now... aha, SOD32: https://lennartb.home.xs4all.nl/forth.html
Re:StoneKnifeForth (With a Metacircular Compiler) by guerrilla on Friday February 19, 2021 @02:30AM (#26190594)
Thanks, SOD32 looks interesting. It also led me to find this: https://github.com/kt97679/relf
Re:StoneKnifeForth (With a Metacircular Compiler) by kragen on Thursday February 18, 2021 @11:28PM (#26189744)
Cool! Do you still have a copy of it?

Nowadays it's sometimes adequately fast to implement a JIT by spawning gcc or tcc and then running dlopen() on the result ;) It also makes the FFI easier :)

Re:StoneKnifeForth (With a Metacircular Compiler) by astrobe_ on Saturday February 20, 2021 @02:26AM (#26202322)
Technically I should still have a floppy and its backup, but the old laptop I used to make it is dead. It's kind of difficult to find floppy drives these days, and the whole thing is so old that the floppies might be no more readable.

The dlopen trick is something I'll remember if I need that sort of stuff, but I've grown wary of FFIs. With libraries, there's often #define constants or macros that make FFIs a bit awkward. I prefer to use static linking if the lib is not too big, or dynamic loading but still with light bindings if not. If you know a bit the internals of Lua, I use a very similar method to extend Forth's "vocabulary", and the loading (and manual linkink with dlsym) is done only when the extension is invoked and initializes itself.