[Vm-dev] VMMakerJS

Bert Freudenberg bert at freudenbergs.de
Fri Oct 10 07:27:47 UTC 2014

Is there interest that I add the JavaScript code generator to the VMMaker repository? The code is currently in the SqueakJS repo (*), but I could additionally keep a copy on source.squeak.org.

This is (unsurprisingly) somewhat of a kludge. I had to work around pointer arithmetic (because unlike Smalltalk's VM simulator I cannot override + or at:put: in JavaScript) and emulate type casts (short int*? JS will laugh in your face. It doesn't even have integers. Double-precision float is all you need). Bit shifts are interesting, too, because shifting by 32 does not result in 0 as you might expect. And division gives you a float, and mod is off by 1 with the wrong sign etc.

And then there's cCode:inSmalltalk:. Fun one. I ended up having a translation table with a JS snippet for every C snippet. And a bit of magic sprinkled on top. Don't judge, it serves its purpose, namely generating the plugins that do not depend on libraries or other external code. And that's actually not that few, 17 total:


But it works, and is almost as efficient as my hand-written code. I was able to get rid of 1200 lines of my own BitBlt code that almost worked, and use the actual BitBlt implementation with all its nooks and crannies. And having a LargeIntegersPlugin makes a real difference in performance. And ZipPlugin too. Etc.

The generated code even looks quite nice, modulo excessive parens.

However, the JS code generator remains ugly, and is less than general. I wonder if some of the C-isms should be replaced with more general patterns. The memory access functions are a step in the right direction I think. Although I think they always should use a base (oop+baseHeaderSize would be my preference) and offset. This would make it rather simple to map to JS, and with today's optimizing C compilers and modern CPUs, does it really make a difference to write ptr++ instead of ptr[i++]?

Anyway, I'm not really complaining, Slang was intended as just being C with, so it's no wonder people use it like that. The type declarations help immensely in figuring out what's going on automatically most of the time, and I kludged the rest. Without modifying a single line in the original plugins, so far. Of course, if the plugin code changes I may have to adjust the code generator.

Err, that turned out longer and more rambling than I intended. I really just wanted to know if I should copy it to the main VMMaker repo?

- Bert -

(*) https://github.com/bertfreudenberg/SqueakJS/tree/master/utils/VMMakerJS.package/JSCodeGenerator.class
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4142 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20141010/b872abc1/smime.bin

More information about the Vm-dev mailing list