SQLite source code analysis ---------- code generator ①

2021SC@SDUSC

summary

         SQLite works by converting SQL statements to bytecode and then running the bytecode in a virtual machine. This section describes how the bytecode engine works.

Bytecode Engine

         The bytecode program created by the code generator is run by the virtual machine.

         The virtual machine itself is completely contained in a single source file vdbe.c. The vdbe.h header file defines the interface between the virtual machine and the rest of the SQLite library. vdbeInt.h defines the special structure and interface of the virtual machine itself. Various other vdbe*.c files are assistants to virtual machines. The vdbeaux.c file contains the utilities and libraries used by the virtual machine. The rest of the library is used to construct the interface module of the VM program. The vdbeapi.c file contains the external interfaces of the virtual machine, such as sqlite3_bind_int() and sqlite3_step(). Individual values (strings, integers, floating-point numbers, and blob s) are stored in an internal object named "Mem", which is implemented by vdbemem.c.

         SQLite uses callbacks to C language routines to implement SQL functions. Even built-in SQL functions are implemented in this way. Most built-in SQL functions (such as abs (), count (), substr (), etc.) can be found in the func.c source file. Date and time conversion functions can be found in Date.c. The code generator directly implements functions such as coalesce () and typeof () as bytecode.
         The full name of VDBE is virtual database engine; it is an abstraction of real computer resource environment. Its principle is similar to JVM (Java virtual machine) in Java, which realizes the cross platform of the two languages.

vdbeInt.h code analysis

Conditional compilation and preprocessing of some methods and structures

typedef struct VdbeOp Op;

/*
** Boolean values
*/
typedef unsigned char Bool;

/* Opaque type used by code in vdbesort.c
**Opaque The type is used by the code in the vdbesort.c file
*/
typedef struct VdbeSorter VdbeSorter;
/* Opaque type used by the explainer This type is used by the interpreter*/
typedef struct Explain Explain;
#define MEM_Null      0x0001   /* Value is NULL */
#define MEM_Str       0x0002   /* Value is a string */
#define MEM_Int       0x0004   /* Value is an integer */
#Define mem_real 0x0008 / * value is a real number*/
#Define mem_blob 0x0010 / * value is a blob is only one block*/
#Define mem_rowset 0x0020 / * value is a rowset object value sets an object for a row*/
#Define mem_frame 0x0040 / * value is a vdbeframe object value is a Vdbe frame object*/
#define MEM_Invalid   0x0080   /* Value is undefined */
#Define mem_typemask 0x00ff / * mask of type bits*/

Analysis of several important structures

struct VdbeCursor(vdbe cursor)
struct VdbeFrame(vdbe Frame)
struct Mem(Only for vdbe Visible data structure)
struct VdbeFunc((auxiliary data)
struct sqlite3_context
(Context and return value)
struct Explain(vdbe (information for)
struct Vdbe(vdbe Integral structure)
struct VdbeCursor
{
   ...
   int iDb (Retrieve database)
   Bool isTable; (See if the table exists)
   Bool rowidIsValid(Whether the data of a row is valid)
   i64 lastRowid
   int seekResult(Query structure)
   ...
}(Tree structure, database page as child node)
A cursor is a single cursor in a database file BTree Indicator for.
The cursor can find a with a specific value BTree Enter, or traverse BTree All entrances to the. You can start anew BTree Entry or retrieve value or data from the entry normally pointed to by this pointer. The pointer of each virtual machine that has been opened represents a constant of the following structure.
struct VdbeFrame {
 Vdbe *v;
 Op *aOp;
 Mem *aMem (Parent frame information)
 ...
 Vdbe *v;
 VdbeFrame *pParent;
 Op *aOp;
 Mem *aMem;(Subframe information)
}

         The memory of a virtual machine framework object is allocated and managed by a storage unit of the parent framework. When the memory unit is deleted or rewritten, the virtual machine framework object will not be released immediately. Instead, it will be linked to the vdbe. Pdelframe listing. When the virtual machine is restarted in, the directory list of Vdbe.pDelFrame is deleted. Instead of deleting the virtual machine framework object immediately, this is done to avoid calling the sqlite3VdbeMemRelease() method cyclically when the storage unit belonging to the sub framework is released.

struct Vdbe{
  sqlite3 *db;
  Op *aOp;
  Mem *aMem; 	/*Memory address*/
  Mem **apArg	/*parameter*/
  Mem *aColName;
  Mem *pResultSet;
  int pc;		/*Program counter*/
  int rc;		/*Value to return*/
  ...}

         This structure defines the entity of a virtual machine. This structure contains the complete state of the virtual machine. sqlite3_stmt the pointer of this structure is defined by SQLite3_ The prepare () method returns, which is a real pointer to the structural entity. The variable of the Vdbe.inVtab method is set to zero during any virtual table method call generated by the vdbe program. This variable is mainly used for two purposes: to allow the xDestroy method to implement the declaration of deleting the table and to prevent the side effects of memory allocation failure. sqlite3_stmt the pointer of this structure is defined by SQLite3_ The prepare () method returns a real pointer to the virtual machine entity.

Attachment (source code):

vdbe.h
vdbeInt.h
vdbe.c

Keywords: SQLite

Added by ponsho on Mon, 29 Nov 2021 01:17:00 +0200