Reinterpreting The Lua Interpreter

The idea behind Lua is a beautiful one. A simple and concise syntax offers almost all of the niceties of a first-class language. Moreover, a naive implementation of an interpreter with a giant switch case can be implemented in an afternoon. But assembly is your go-to to get decent performance in a JIT-style interpreter. So [Haoran Xu] started to ask himself if he could achieve better performance without hand-rolled assembly, and after a few months of work, he published a work-in-progress called LuaJIT Remake (LJR).

Currently, it supports Lua 5.1, and on a smattering of 34 benchmarks, LJR beats the leading fastest Lua, LuaJIT, by around 28% and the official Lua engine by 3x. [Haoran] offers a great explanation of interpreters that provides excellent background and context for the problem.

But the long and short of it is that switch cases are expensive and hard to optimize for compilers, so using tail calling is a reasonable solution that comes with some significant drawbacks. With tail calls, each case statement becomes a “function” that is jumped to and then jumped out of without mucking with the stack or the registers too much.

However, the calling convention requires any callee-saved registers to be preserved, which means you lose some registers as there is no way to tell the compiler that this function is allowed to break the calling convention. Clang is currently the only compiler that offers a guaranteed tail-call annotation ([[clang::musttail]]). There are other limitations too, for instance requiring the caller and callee to have identical function prototypes to prevent unbounded stack growth.

So [Haoran] went back to the drawing board and wrote two new tools: C++ bytecode semantical description and a special compiler called Deegen. The C++ bytecode looks like this:

void Add(TValue lhs, TValue rhs) {
  if (!lhs.Is<tDouble>() || !rhs.Is<tDouble>()) {
    ThrowError("Can't add!");
  } else {
    double res = lhs.As<tDouble>() + rhs.As<tDouble>();
    Return(TValue::Create<tDouble>(res));
  }
}
DEEGEN_DEFINE_BYTECODE(Add) {
  Operands(
    BytecodeSlotOrConstant("lhs"),
    BytecodeSlotOrConstant("rhs")
  );
  Result(BytecodeValue);
  Implementation(Add);
  Variant(
    Op("lhs").IsBytecodeSlot(),
    Op("rhs").IsBytecodeSlot()
  );
  Variant(
    Op("lhs").IsConstant(),
    Op("rhs").IsBytecodeSlot()
  );
  Variant(
    Op("lhs").IsBytecodeSlot(),
    Op("rhs").IsConstant()
  );
}

Note that this is not the C keyword return. Instead, there is a definition of the bytecode and then an implementation. This bytecode is converted into LLVM IR and then fed into Deegen, which can transform the functions to do tail calls correctly, use the GHC calling conventions, and a few other optimizations like inline caching through a clever C++ lambda mechanism. The blog post is exceptionally well-written and offers a fantastic glimpse into the wild world of interpreters.

The code is on Github. But if you’re interested in a more whimsical interpreter, here’s a Brainf**k interpreter written in Befunge.

The bottom half of a MacBook Air on a purple and pink background has severed wires drawn out of its back to indicate its lack of a screen.

Are Slabtops The Future Of Computing?

The most popular computer ever was the Commodore 64 with its computer-in-a-keyboard form factor. If you have a longing for a keyboard computer with more modern internals, one of the easiest solutions today is to pull the screen off a laptop.

[Umar Shakir] wanted to see what the fuss was about regarding a recent Apple patent and took the top lid off of his M1 Macbook Air and turned it into a “slabtop.” The computer works great wired to a monitor but can also be used wirelessly via AirPlay. The approach doesn’t come without its downsides, of course. Newer MacBooks can’t access recovery mode without the built-in screen, and some older models had their WiFi antennas in the top lid, so making one into a slabtop will leave you desk-bound.

While [Shakir] focuses on MacBooks, this approach should work with any laptop. Apparently, it’s a cottage industry in China already. Back in the day, my own daily driver was a Pentium-powered laptop with its broken LCD (and lid) removed. It worked great with whatever CRT was nearby.

If you’re looking for an off-the-shelf keyboard computer of your own, you might want to check out the Raspberry Pi 400.

Custom Prusa MK3 Fan Duct Gives Camera Perfect View

A growing trend is to mount a borescope “inspection camera” near a 3D printer’s nozzle to provide a unique up-close view of the action. Some argue that this perspective can provide valuable insight if you’re trying to fine tune your machine, but whether or not there’s a practical application for these sort of nozzle cams, certainly everyone can agree it makes for a pretty cool video.

[Caelestis Cosplay] recently decided to outfit his Prusa i3 MK3S+ with such a camera, and was kind enough to share the process in a write-up. The first step was to find a community-developed fan duct, which he then modified to hold the 7 mm camera module. Since the duct blows right on the printer’s nozzle, it provides an ideal vantage point.

The camera module included a few tiny SMD LEDs around the lens, but [Caelestis Cosplay] added holes to the fan duct to fit a pair of 3 mm white LEDs to really light things up. While modifying the printed parts took some effort, he says the hardest part of the whole build was salvaging a 5X lens from a handheld magnifier and filing it down so it would fit neatly over the camera. But judging by the sharp and bright demo video he’s provided, we’d say the extra effort was certainly worth it.

After covering how the camera rig was put together, [Caelestis Cosplay] then goes over how it was integrated into OctoPrint, including how the external LEDs are switched on and off. He’s running OctoPrint on a Raspberry Pi, though as we’ve covered recently, a small form factor desktop computer could just as easily run the show.

Continue reading “Custom Prusa MK3 Fan Duct Gives Camera Perfect View”

Squish That Stack With Rampart

[P B Richards] and [Aaron Flin] were bemoaning the resource hunger of modern JavaScript environments and planned to produce a system that was much stingier with memory and CPU, that would fit better on lower-end platforms. Think Nginx, NodeJS, and your flavour of database and how much resource that all needs to run properly. Now try wedge that lot onto a Raspberry Pi Zero. Well, they did, creating Rampart: a JavaScript-based complete stack development environment.

The usual web applications have lots of tricks to optimise for speed, but according to the developers, Rampart is still pretty fast. Its reason for existence is purely about resource usage, and looking at a screen grab, the Rampart HTTP server weighs in at less than 10 MB of RAM. It appears to support a decent slew of technologies, such as HTTPS, WebSockets, SQL search, REDIS, as well as various utility and OS functions, so shouldn’t be so lightweight as to make developing non-trivial applications too much work. One interesting point they make is that in making Rampart so frugal when deployed onto modern server farms it could be rather efficient. Anyway, it may be worth a look if you have a reasonable application to wedge onto a small platform.

We’ve seen many JavaScript runtimes over the years, like this recent effort, but there’s always room for one more.

Electroplated 3D Printed Sword: Shiny!

We all want to 3D print metals, but the equipment to do that is still beyond most home workshops. However, [HEN3DRIK] takes resin 3D-printed items and electroplates them. Might not be as good as printing in metal, but it sure looks metallic. As you can see in the video below, the sword looks like it was crafted from highly-polished steel.

The sword comes out in four pieces. He repeats several times that sanding is the key because you must have flat surfaces. Using sandpaper and steel wool, he worked the parts to a fine finish. The parts assemble along an M8 threaded rod to form a whole. The next step was to electroplate with copper.

Continue reading “Electroplated 3D Printed Sword: Shiny!”

Racing Cars On A PCB

Carl Friedrich Gauss was, to put it mildly, a polymath responsible for a large percentage of the things we take for granted in the modern world. As a physicist and mathematician he pioneered several fields of study including within the field of magnetism. But since he died decades before the first car was built, it’s unlikely he could have imagined this creation, a magnetic slot-car race track called the Gauss Speedway by [Jeff McBride], which bears the name of the famous scientist.

The Gauss Speedway takes its inspiration from a recent development in robotics, where many small robots can travel around a large area with the help of circuit traces integrated into their operating area. With the right current applied to these traces, magnetic fields are generated which propel the robots. [Jeff] wanted to build something similar, integrated into a printed circuit board directly, and came up with the slot car idea. The small cars have tiny magnets in them which interact with the traces in the PCB, allowing the cars to move with high precision around the track. He did abandon the traditional slot car controller in favor of a push-button style one directly on the PCB too, which means everything is completely integrated.

While this was more of a demonstration or proof-of-concept, some of the features of this style of robot can be seen in this video, which shows them moving extremely rapidly with high precision, on uneven surfaces, or even up walls. Magnetic robots like these are seeing quite a renaissance, and we’ve even seen some that use magnetism to shape-shift.

Continue reading “Racing Cars On A PCB”

IRL minesweeper render showing game on top of a campaign map

Meat-Space Minesweeper Game Hits The Mark

Hackers of a certain age will remember that before the Internet was available to distract us from our work, we had to find our own fun. Luckily, Windows was there to come to our aid, in the shape of “Minesweeper” – a classic of the age that involved figuring out/occasionally just guessing where a selection of mines had been hidden on a grid of squares via numerical clues to their proximity. For those missing such simple times, [Martin] has brought the game into physical space with his 3D-printed travel-game version.

GIF showing how to play IRL minesweeper game

A number of pre-determined game fields can be inserted (by a friend… or enemy, we presume!) and covered by tiles, which the mine-clearing player can then remove with their plastic shovel to reveal the clues. The aim of the game is to avoid uncovering a bomb, and to place flags where the bombs are hiding.

Aficionados of the game may remember that a little guessing was often inevitable, which sometimes ended in disaster. On the computer version, this merely entailed clicking the Smiley Face button for a new game, but in this case would require a new sheet to be inserted. Blank sheet templates are included for producing your own fiendish bomb-sites, and all the pieces pack away neatly into a handy clam-shell design that would be ideal for long car journeys when the data package on the kids’ tablets has run out.

We wonder what other classic games may lend themselves to a travel remake and look forward to the first 3D-printed travel set of Doom with anticipation!

If you’re above solving your own Minesweeper games, then you can learn how to write a solver in Java here. Continue reading “Meat-Space Minesweeper Game Hits The Mark”