5

I'm interested in seeing how low-level a programmer can go in pure Common Lisp (or, failing that, in implementation-specific extensions). Google hasn't been able to find me much information about this, so I'd like to hear what the experts have to say. This post mentioned a feature of SBCL to define what the author called "virtual operators", but searching for "common lisp virtual operators" didn't yield much. The author also mentioned how hard it was to find documentation about it. Do similar systems exist for other implementations, is there any basis for it in the standard (though considering that such a feature would mostly be used for writing ISA-specific code, portability shouldn't really be high on the priorities for users of it), and where can documentation for features such as this be found?

It would be great to find a way to extend the "programmable programming language" concept to low-level code as well (especially for areas where efficiency is highly important and other libraries written in C or assembly may not be available).

Reepca
  • 323
  • 2
  • 7
  • 4
    You can take a look here: http://www.pvk.ca/Blog/2014/08/16/how-to-define-new-intrinsics-in-sbcl/ And here's an example usage: https://github.com/sionescu/swap-bytes/blob/master/sbcl-vops.lisp – Vsevolod Dyomkin Nov 16 '15 at 04:39
  • @VsevolodDyomkin Did you find anything similar for other common lisp implementations? – Reepca Nov 16 '15 at 05:41
  • 2
    I wasn't actively looking. Also, not all CL implementations generate native code (some compile to C or Java, for instance) – Vsevolod Dyomkin Nov 16 '15 at 11:59

1 Answers1

3

This is very implementation specific and not portable at all.

Yes, SBCL uses VOPs, but other implementations have completely different compilation targets.

If you intend to go low-level in a somewhat portable way, you have two avenues:

  • Optimize using declarations, inlining, manual unrolling etc., checking your progress with disassemble

  • Implement a static blob externally, then interface it with an FFI

Svante
  • 50,694
  • 11
  • 78
  • 122
  • What is `VOP` stands for? – muyinliu Apr 08 '18 at 14:19
  • Virtual OPerator. See the original question. – Svante Apr 08 '18 at 21:18
  • I am familiar with your first method (declarations and check disassemble). Do you know of any tools to enforce that you don't break optimizations, without the fragile reliance on remembering to check the assembly? Perhaps a compile option for that function, or a macro for writing lower level prog blocks? – Justin Meiners Oct 08 '22 at 04:37
  • Not really. The SBCL compiler gives some notes when it can't optimize something, as soon as you declared some optimization. Other than that, I could imagine performance tests (comparing against a known computation so that changing or unknown machine speeds get compensated). – Svante Oct 10 '22 at 16:30