opcode.tcl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #
  2. # Run this Tcl script to generate the sqlite.html file.
  3. #
  4. set rcsid {$Id: opcode.tcl,v 1.9 2001/11/24 13:50:53 drh Exp $}
  5. puts {<html>
  6. <head>
  7. <title>SQLite Virtual Machine Opcodes</title>
  8. </head>
  9. <body bgcolor=white>
  10. <h1 align=center>
  11. SQLite Virtual Machine Opcodes
  12. </h1>}
  13. puts "<p align=center>
  14. (This page was last modified on [lrange $rcsid 3 4] UTC)
  15. </p>"
  16. set fd [open [lindex $argv 0] r]
  17. set file [read $fd [file size [lindex $argv 0]]]
  18. close $fd
  19. set current_op {}
  20. foreach line [split $file \n] {
  21. set line [string trim $line]
  22. if {[string index $line 1]!="*"} {
  23. set current_op {}
  24. continue
  25. }
  26. if {[regexp {^/\* Opcode: } $line]} {
  27. set current_op [lindex $line 2]
  28. set Opcode($current_op:args) [lrange $line 3 end]
  29. lappend OpcodeList $current_op
  30. continue
  31. }
  32. if {$current_op==""} continue
  33. if {[regexp {^\*/} $line]} {
  34. set current_op {}
  35. continue
  36. }
  37. set line [string trim [string range $line 3 end]]
  38. if {$line==""} {
  39. append Opcode($current_op:text) \n<p>
  40. } else {
  41. append Opcode($current_op:text) \n$line
  42. }
  43. }
  44. unset file
  45. puts {
  46. <h2>Introduction</h2>
  47. <p>In order to execute an SQL statement, the SQLite library first parses
  48. the SQL, analyzes the statement, then generates a short program to execute
  49. the statement. The program is generated for a "virtual machine" implemented
  50. by the SQLite library. This document describes the operation of that
  51. virtual machine.</p>
  52. <p>This document is intended as a reference, not a tutorial.
  53. A separate <a href="vdbe.html">Virtual Machine Tutorial</a> is
  54. available. If you are looking for a narrative description
  55. of how the virtual machine works, you should read the tutorial
  56. and not this document. Once you have a basic idea of what the
  57. virtual machine does, you can refer back to this document for
  58. the details on a particular opcode.
  59. Unfortunately, the virtual machine tutorial was written for
  60. SQLite version 1.0. There are substantial changes in the virtual
  61. machine for version 2.0 and the document has not been updated.
  62. </p>
  63. <p>The source code to the virtual machine is in the <b>vdbe.c</b> source
  64. file. All of the opcode definitions further down in this document are
  65. contained in comments in the source file. In fact, the opcode table
  66. in this document
  67. was generated by scanning the <b>vdbe.c</b> source file
  68. and extracting the necessary information from comments. So the
  69. source code comments are really the canonical source of information
  70. about the virtual macchine. When in doubt, refer to the source code.</p>
  71. <p>Each instruction in the virtual machine consists of an opcode and
  72. up to three operands named P1, P2 and P3. P1 may be an arbitrary
  73. integer. P2 must be a non-negative integer. P2 is always the
  74. jump destination in any operation that might cause a jump.
  75. P3 is a null-terminated
  76. string or NULL. Some operators use all three operands. Some use
  77. one or two. Some operators use none of the operands.<p>
  78. <p>The virtual machine begins execution on instruction number 0.
  79. Execution continues until (1) a Halt instruction is seen, or
  80. (2) the program counter becomes one greater than the address of
  81. last instruction, or (3) there is an execution error.
  82. When the virtual machine halts, all memory
  83. that it allocated is released and all database cursors it may
  84. have had open are closed. If the execution stopped due to an
  85. error, any pending transactions are terminated and changes made
  86. to the database are rollback.</p>
  87. <p>The virtual machine also contains an operand stack of unlimited
  88. depth. Many of the opcodes use operands from the stack. See the
  89. individual opcode descriptions for details.</p>
  90. <p>The virtual machine can have zero or more cursors. Each cursor
  91. is a pointer into a single table or index within the database.
  92. There can be multiple cursors pointing at the same index or table.
  93. All cursors operate independently, even cursors pointing to the same
  94. indices or tables.
  95. The only way for the virtual machine to interact with a database
  96. file is through a cursor.
  97. Instructions in the virtual
  98. machine can create a new cursor (Open), read data from a cursor
  99. (Column), advance the cursor to the next entry in the table
  100. (Next) or index (NextIdx), and many other operations.
  101. All cursors are automatically
  102. closed when the virtual machine terminates.</p>
  103. <p>The virtual machine contains an arbitrary number of fixed memory
  104. locations with addresses beginning at zero and growing upward.
  105. Each memory location can hold an arbitrary string. The memory
  106. cells are typically used to hold the result of a scalar SELECT
  107. that is part of a larger expression.</p>
  108. <p>The virtual machine contains a single sorter.
  109. The sorter is able to accumulate records, sort those records,
  110. then play the records back in sorted order. The sorter is used
  111. to implement the ORDER BY clause of a SELECT statement.</p>
  112. <p>The virtual machine contains a single "List".
  113. The list stores a list of integers. The list is used to hold the
  114. rowids for records of a database table that needs to be modified.
  115. The WHERE clause of an UPDATE or DELETE statement scans through
  116. the table and writes the rowid of every record to be modified
  117. into the list. Then the list is played back and the table is modified
  118. in a separate step.</p>
  119. <p>The virtual machine can contain an arbitrary number of "Sets".
  120. Each set holds an arbitrary number of strings. Sets are used to
  121. implement the IN operator with a constant right-hand side.</p>
  122. <p>The virtual machine can open a single external file for reading.
  123. This external read file is used to implement the COPY command.</p>
  124. <p>Finally, the virtual machine can have a single set of aggregators.
  125. An aggregator is a device used to implement the GROUP BY clause
  126. of a SELECT. An aggregator has one or more slots that can hold
  127. values being extracted by the select. The number of slots is the
  128. same for all aggregators and is defined by the AggReset operation.
  129. At any point in time a single aggregator is current or "has focus".
  130. There are operations to read or write to memory slots of the aggregator
  131. in focus. There are also operations to change the focus aggregator
  132. and to scan through all aggregators.</p>
  133. <h2>Viewing Programs Generated By SQLite</h2>
  134. <p>Every SQL statement that SQLite interprets results in a program
  135. for the virtual machine. But if you precede the SQL statement with
  136. the keyword "EXPLAIN" the virtual machine will not execute the
  137. program. Instead, the instructions of the program will be returned
  138. like a query result. This feature is useful for debugging and
  139. for learning how the virtual machine operates.</p>
  140. <p>You can use the <b>sqlite</b> command-line tool to see the
  141. instructions generated by an SQL statement. The following is
  142. an example:</p>}
  143. proc Code {body} {
  144. puts {<blockquote><pre>}
  145. regsub -all {&} [string trim $body] {\&amp;} body
  146. regsub -all {>} $body {\&gt;} body
  147. regsub -all {<} $body {\&lt;} body
  148. regsub -all {\(\(\(} $body {<font color="#00671f"><u>} body
  149. regsub -all {\)\)\)} $body {</u></font>} body
  150. puts $body
  151. puts {</pre></blockquote>}
  152. }
  153. Code {
  154. $ (((sqlite ex1)))
  155. sqlite> (((.explain)))
  156. sqlite> (((explain delete from tbl1 where two<20;)))
  157. addr opcode p1 p2 p3
  158. ---- ------------ ----- ----- ----------------------------------------
  159. 0 Transaction 0 0
  160. 1 VerifyCookie 219 0
  161. 2 ListOpen 0 0
  162. 3 Open 0 3 tbl1
  163. 4 Rewind 0 0
  164. 5 Next 0 12
  165. 6 Column 0 1
  166. 7 Integer 20 0
  167. 8 Ge 0 5
  168. 9 Recno 0 0
  169. 10 ListWrite 0 0
  170. 11 Goto 0 5
  171. 12 Close 0 0
  172. 13 ListRewind 0 0
  173. 14 OpenWrite 0 3
  174. 15 ListRead 0 19
  175. 16 MoveTo 0 0
  176. 17 Delete 0 0
  177. 18 Goto 0 15
  178. 19 ListClose 0 0
  179. 20 Commit 0 0
  180. }
  181. puts {
  182. <p>All you have to do is add the "EXPLAIN" keyword to the front of the
  183. SQL statement. But if you use the ".explain" command to <b>sqlite</b>
  184. first, it will set up the output mode to make the program more easily
  185. viewable.</p>
  186. <p>If <b>sqlite</b> has been compiled without the "-DNDEBUG=1" option
  187. (that is, with the NDEBUG preprocessor macro not defined) then you
  188. can put the SQLite virtual machine in a mode where it will trace its
  189. execution by writing messages to standard output. The non-standard
  190. SQL "PRAGMA" comments can be used to turn tracing on and off. To
  191. turn tracing on, enter:
  192. </p>
  193. <blockquote><pre>
  194. PRAGMA vdbe_trace=on;
  195. </pre></blockquote>
  196. <p>
  197. You can turn tracing back off by entering a similar statement but
  198. changing the value "on" to "off".</p>
  199. <h2>The Opcodes</h2>
  200. }
  201. puts "<p>There are currently [llength $OpcodeList] opcodes defined by
  202. the virtual machine."
  203. puts {All currently defined opcodes are described in the table below.
  204. This table was generated automatically by scanning the source code
  205. from the file <b>vdbe.c</b>.</p>}
  206. puts {
  207. <p><table cellspacing="1" border="1" cellpadding="10">
  208. <tr><th>Opcode&nbsp;Name</th><th>Description</th></tr>}
  209. foreach op [lsort -dictionary $OpcodeList] {
  210. puts {<tr><td valign="top" align="center">}
  211. puts "$op"
  212. puts "<td>[string trim $Opcode($op:text)]</td></tr>"
  213. }
  214. puts {</table></p>}
  215. puts {
  216. <p><hr /></p>
  217. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  218. Back to the SQLite Home Page</a>
  219. </p>
  220. </body></html>}