Fastest HQ9+ Interpreter in the World

For some bizarre rea­son I felt com­pelled 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 use­less, you ask? Of course it is.

Ends with S, too

The real pur­pose of HJMP1 (project page, GitHub page), which is what I’ve decid­ed to call my new inter­preter2), is of course for me to play with JIT com­pi­la­tion on dif­fer­ent plat­forms.

Now that recent ver­sions of GCC are freely avail­able on x86 and AMD64 ver­sions of Lin­ux and Win­dows3, we pro­gram­mers can use the same tools and code and tar­get dif­fer­ent plat­forms with the min­i­mum of changes (and headaches!). How­ev­er, those dif­fer­ences do accen­tu­ate them­selves if you begin work­ing closer to the met­al, either because of require­ments like per­for­mance or just for fun.

In this case, the dis­tinc­tion caus­ing trou­ble was call­ing con­ven­tions. Reg­u­lar 32-bit machi­nes use cde­cl, which is straight­for­ward and pass­es para­me­ters to func­tions on the stack. New­er AMD64 soft­ware use vari­ants of fast­call, which pass­es some num­ber of argu­ments inside reg­is­ters to take advan­tage of the dou­bled reg­is­ter count in AMD64 over x86. To make things more com­pli­cat­ed, Microsoft uses their own unique AMD64 call­ing con­ven­tions for Win­dows and relat­ed tools.

Part of this exer­cise was to abstract away the gen­er­al nas­ti­ness of writing—or generating—assembly code through object-ori­ent­ed design. I accom­plished this through the mag­ic of dynam­ic4 poly­mor­phism. Dif­fer­ent code emit­ters gen­er­at­ing code for each plat­form talked direct­ly with AsmJit, the code gen­er­a­tion library I used, but were hid­den behind inter­faces from the token proces­sor. In addi­tion, the token processors—one with JIT and one that exe­cut­ed token directly—were hid­den from the parser (the token gen­er­a­tor) as well through an inter­face, so that I can enable or dis­able JIT at run­time.

Over­all, this lit­tle project was a suc­cess. It isn’t exact­ly ground­break­ing or very use­ful for day-to-day work, but the expe­ri­ence of mak­ing it was a valu­able one.

Download HJMP from the project page

  1. pro­nounced “HIGH-JUMP” []
  2. in addi­tion to “The Fastest HQ9+ Inter­preter in the World” : []
  3. under TDM GCC and MinGW-w64 []
  4. and sta­t­ic too, real­ly []