SQLite Forum

programming sqlite bytecode directly ?

programming sqlite bytecode directly ?

(1) By anonymous on 2021-04-15 11:58:27 [link] [source]

Is there a recommended guide/tutorial on programming https://sqlite.org/opcode.html directly ?

Possible application: suppose we want to write a datalog-like front end to sqlite:

  1. we use peg/packrat for parsing

  2. we emit some sqlite bytecode aka ("then a miracle happens")

  3. we execute said bytecode

How do we approach step 2 ?

(2) By Richard Hipp (drh) on 2021-04-15 12:07:05 in reply to 1 [link] [source]

You cannot program bytecode directly.

(1) The opcodes change from one version to the next, so any bytecode you write for the current version of SQLite might not work for the next version to come along.

(2) The opcodes are only designed to be used as coded by the code generator. They do not handle corner cases gracefully. You could cause segfaults and other undefined behavior if you could insert arbitrary bytecode.

(3) By anonymous on 2021-04-15 12:21:25 in reply to 2 [source]

Thank you for the informative response.

Is the higher order bit basically:

  1. compile to sqlite's dialect of SQL
  2. call sqlite functions directly (probably not recommended)
  3. use some engine besides sqlite ?

(4) By Simon Slavin (slavin) on 2021-04-16 12:33:25 in reply to 3 [link] [source]

I'd recommend the first of these. The most valuable result of this is that you can see the results as a string, which will make it ridiculously fast to improve and debug your code. I've done this to translate a GUI interface into a SQL SELECT, and it was easy and fun. It also has the advantage that if you ever move to another SQL engine you can see exactly what's going on and won't have to change much code.

There's no way to perform SQL functions by calling SQLite functions directly. For instance, there's no SQLite function which performs an UPDATE which doesn't involve you constructing the whole SQL command and executing it. (Unless you get hacky and call undocumented functions inside the SQLite library.)

You could use some other SQL engine, but SQLite's parser is ridiculously fast and efficient, because it uses lemon which is also written by the SQLite team. You're not really losing much time over trying to construct your own bytecoded program.