Fastest HQ9+ Interpreter in the World

For some bizarre reason I felt compelled to spend hours of night writ­ing an HQ9+ inter­preter. Not just any HQ9+ inter­preter, I thought, but the fastest HQ9+ inter­preter in the world. Is that useless, you ask? Of course it is.

Ends with S, too

The real purpose of HJMP1 (project page, GitHub page), which is what I’ve decided to call my new inter­preter2), is of course for me to play with JIT compi­la­tion on differ­ent plat­forms.

Now that recent versions of GCC are freely avail­able on x86 and AMD64 versions of Linux and Windows3, we program­mers can use the same tools and code and target differ­ent plat­forms with the mini­mum of changes (and headaches!). However, those differ­ences do accen­tu­ate them­selves if you begin work­ing closer to the metal, either because of require­ments like perfor­mance or just for fun.

In this case, the distinc­tion caus­ing trou­ble was call­ing conven­tions. Regu­lar 32-bit machines use cdecl, which is straight­for­ward and passes para­me­ters to func­tions on the stack. Newer AMD64 soft­ware use vari­ants of fast­call, which passes some number of argu­ments inside regis­ters to take advan­tage of the doubled regis­ter count in AMD64 over x86. To make things more compli­cated, Microsoft uses their own unique AMD64 call­ing conven­tions for Windows and related tools.

Part of this exer­cise was to abstract away the general nasti­ness of writing—or generating—assembly code through object-oriented design. I accom­plished this through the magic of dynamic4 poly­mor­phism. Differ­ent code emit­ters gener­at­ing code for each plat­form talked directly with AsmJit, the code gener­a­tion library I used, but were hidden behind inter­faces from the token proces­sor. In addi­tion, the token processors—one with JIT and one that executed token directly—were hidden from the parser (the token gener­a­tor) as well through an inter­face, so that I can enable or disable JIT at runtime.

Over­all, this little project was a success. It isn’t exactly ground­break­ing or very useful for day-to-day work, but the expe­ri­ence of making it was a valu­able one.

Download HJMP from the project page

  1. pronounced “HIGH-JUMP” []
  2. in addi­tion to “The Fastest HQ9+ Inter­preter in the World” : []
  3. under TDM GCC and MinGW-w64 []
  4. and static too, really []