xref: /sqlite-3.40.0/src/vdbe.h (revision 8a29dfde)
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code.  In place of
5 ** a legal notice, here is a blessing:
6 **
7 **    May you do good and not evil.
8 **    May you find forgiveness for yourself and forgive others.
9 **    May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** Header file for the Virtual DataBase Engine (VDBE)
13 **
14 ** This header defines the interface to the virtual database engine
15 ** or VDBE.  The VDBE implements an abstract machine that runs a
16 ** simple program to access and modify the underlying database.
17 **
18 ** $Id: vdbe.h,v 1.130 2008/04/11 14:56:53 drh Exp $
19 */
20 #ifndef _SQLITE_VDBE_H_
21 #define _SQLITE_VDBE_H_
22 #include <stdio.h>
23 
24 /*
25 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
26 ** in the source file sqliteVdbe.c are allowed to see the insides
27 ** of this structure.
28 */
29 typedef struct Vdbe Vdbe;
30 
31 /*
32 ** The names of the following types declared in vdbeInt.h are required
33 ** for the VdbeOp definition.
34 */
35 typedef struct VdbeFunc VdbeFunc;
36 typedef struct Mem Mem;
37 typedef struct UnpackedRecord UnpackedRecord;
38 
39 /*
40 ** A single instruction of the virtual machine has an opcode
41 ** and as many as three operands.  The instruction is recorded
42 ** as an instance of the following structure:
43 */
44 struct VdbeOp {
45   u8 opcode;          /* What operation to perform */
46   signed char p4type; /* One of the P4_xxx constants for p4 */
47   u8 opflags;         /* Not currently used */
48   u8 p5;              /* Fifth parameter is an unsigned character */
49   int p1;             /* First operand */
50   int p2;             /* Second parameter (often the jump destination) */
51   int p3;             /* The third parameter */
52   union {             /* forth parameter */
53     int i;                 /* Integer value if p4type==P4_INT32 */
54     void *p;               /* Generic pointer */
55     char *z;               /* Pointer to data for string (char array) types */
56     i64 *pI64;             /* Used when p4type is P4_INT64 */
57     double *pReal;         /* Used when p4type is P4_REAL */
58     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
59     VdbeFunc *pVdbeFunc;   /* Used when p4type is P4_VDBEFUNC */
60     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
61     Mem *pMem;             /* Used when p4type is P4_MEM */
62     sqlite3_vtab *pVtab;   /* Used when p4type is P4_VTAB */
63     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
64   } p4;
65 #ifdef SQLITE_DEBUG
66   char *zComment;     /* Comment to improve readability */
67 #endif
68 #ifdef VDBE_PROFILE
69   int cnt;            /* Number of times this instruction was executed */
70   long long cycles;   /* Total time spend executing this instruction */
71 #endif
72 };
73 typedef struct VdbeOp VdbeOp;
74 
75 /*
76 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
77 ** it takes up less space.
78 */
79 struct VdbeOpList {
80   u8 opcode;          /* What operation to perform */
81   signed char p1;     /* First operand */
82   signed char p2;     /* Second parameter (often the jump destination) */
83   signed char p3;     /* Third parameter */
84 };
85 typedef struct VdbeOpList VdbeOpList;
86 
87 /*
88 ** Allowed values of VdbeOp.p3type
89 */
90 #define P4_NOTUSED    0   /* The P4 parameter is not used */
91 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
92 #define P4_STATIC   (-2)  /* Pointer to a static string */
93 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
94 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
95 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
96 #define P4_VDBEFUNC (-7)  /* P4 is a pointer to a VdbeFunc structure */
97 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
98 #define P4_TRANSIENT (-9) /* P4 is a pointer to a transient string */
99 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
100 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
101 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
102 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
103 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
104 
105 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
106 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
107 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used.  It still
108 ** gets freed when the Vdbe is finalized so it still should be obtained
109 ** from a single sqliteMalloc().  But no copy is made and the calling
110 ** function should *not* try to free the KeyInfo.
111 */
112 #define P4_KEYINFO_HANDOFF (-9)
113 
114 /*
115 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
116 ** number of columns of data returned by the statement.
117 */
118 #define COLNAME_NAME     0
119 #define COLNAME_DECLTYPE 1
120 #define COLNAME_DATABASE 2
121 #define COLNAME_TABLE    3
122 #define COLNAME_COLUMN   4
123 #ifdef SQLITE_ENABLE_COLUMN_METADATA
124 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
125 #else
126 # ifdef SQLITE_OMIT_DECLTYPE
127 #   define COLNAME_N      1      /* Store only the name */
128 # else
129 #   define COLNAME_N      2      /* Store the name and decltype */
130 # endif
131 #endif
132 
133 /*
134 ** The following macro converts a relative address in the p2 field
135 ** of a VdbeOp structure into a negative number so that
136 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
137 ** the macro again restores the address.
138 */
139 #define ADDR(X)  (-1-(X))
140 
141 /*
142 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
143 ** header file that defines a number for each opcode used by the VDBE.
144 */
145 #include "opcodes.h"
146 
147 /*
148 ** Prototypes for the VDBE interface.  See comments on the implementation
149 ** for a description of what each of these routines does.
150 */
151 Vdbe *sqlite3VdbeCreate(sqlite3*);
152 int sqlite3VdbeAddOp0(Vdbe*,int);
153 int sqlite3VdbeAddOp1(Vdbe*,int,int);
154 int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
155 int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
156 int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
157 int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
158 void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
159 void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
160 void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
161 void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
162 void sqlite3VdbeJumpHere(Vdbe*, int addr);
163 void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
164 void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
165 void sqlite3VdbeUsesBtree(Vdbe*, int);
166 VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
167 int sqlite3VdbeMakeLabel(Vdbe*);
168 void sqlite3VdbeDelete(Vdbe*);
169 void sqlite3VdbeMakeReady(Vdbe*,int,int,int,int);
170 int sqlite3VdbeFinalize(Vdbe*);
171 void sqlite3VdbeResolveLabel(Vdbe*, int);
172 int sqlite3VdbeCurrentAddr(Vdbe*);
173 #ifdef SQLITE_DEBUG
174   void sqlite3VdbeTrace(Vdbe*,FILE*);
175 #endif
176 void sqlite3VdbeResetStepResult(Vdbe*);
177 int sqlite3VdbeReset(Vdbe*, int);
178 void sqlite3VdbeSetNumCols(Vdbe*,int);
179 int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, int);
180 void sqlite3VdbeCountChanges(Vdbe*);
181 sqlite3 *sqlite3VdbeDb(Vdbe*);
182 void sqlite3VdbeSetSql(Vdbe*, const char *z, int n);
183 void sqlite3VdbeSwap(Vdbe*,Vdbe*);
184 
185 int sqlite3VdbeReleaseMemory(int);
186 UnpackedRecord *sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,void*,int);
187 void sqlite3VdbeDeleteUnpackedRecord(UnpackedRecord*);
188 int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
189 
190 
191 #ifndef NDEBUG
192   void sqlite3VdbeComment(Vdbe*, const char*, ...);
193 # define VdbeComment(X)  sqlite3VdbeComment X
194 #else
195 # define VdbeComment(X)
196 #endif
197 
198 #endif
199