xref: /sqlite-3.40.0/src/vdbe.h (revision ef5ecb41)
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.85 2004/06/05 10:22:18 danielk1977 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 ** A single instruction of the virtual machine has an opcode
33 ** and as many as three operands.  The instruction is recorded
34 ** as an instance of the following structure:
35 */
36 struct VdbeOp {
37   u8 opcode;          /* What operation to perform */
38   int p1;             /* First operand */
39   int p2;             /* Second parameter (often the jump destination) */
40   char *p3;           /* Third parameter */
41   int p3type;         /* P3_STATIC, P3_DYNAMIC or P3_POINTER */
42 #ifndef NDEBUG
43   char *zComment;     /* Comments explaining what this opcode does */
44 #endif
45 #ifdef VDBE_PROFILE
46   int cnt;            /* Number of times this instruction was executed */
47   long long cycles;   /* Total time spend executing this instruction */
48 #endif
49 };
50 typedef struct VdbeOp VdbeOp;
51 
52 /*
53 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
54 ** it takes up less space.
55 */
56 struct VdbeOpList {
57   u8 opcode;          /* What operation to perform */
58   signed char p1;     /* First operand */
59   short int p2;       /* Second parameter (often the jump destination) */
60   char *p3;           /* Third parameter */
61 };
62 typedef struct VdbeOpList VdbeOpList;
63 
64 /*
65 ** Allowed values of VdbeOp.p3type
66 */
67 #define P3_NOTUSED    0   /* The P3 parameter is not used */
68 #define P3_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
69 #define P3_STATIC   (-2)  /* Pointer to a static string */
70 #define P3_POINTER  (-3)  /* P3 is a pointer to some structure or object */
71 #define P3_COLLSEQ  (-4)  /* P3 is a pointer to a CollSeq structure */
72 #define P3_FUNCDEF  (-5)  /* P3 is a pointer to a FuncDef structure */
73 #define P3_KEYINFO  (-6)  /* P3 is a pointer to a KeyInfo structure */
74 #define P3_VDBEFUNC (-7)  /* P3 is a pointer to a VdbeFunc structure */
75 
76 /* When adding a P3 argument using P3_KEYINFO, a copy of the KeyInfo structure
77 ** is made.  That copy is freed when the Vdbe is finalized.  But if the
78 ** argument is P3_KEYINFO_HANDOFF, the passed in pointer is used.  It still
79 ** gets freed when the Vdbe is finalized so it still should be obtained
80 ** from a single sqliteMalloc().  But no copy is made and the calling
81 ** function should *not* try to free the KeyInfo.
82 */
83 #define P3_KEYINFO_HANDOFF (-7)
84 
85 /*
86 ** The following macro converts a relative address in the p2 field
87 ** of a VdbeOp structure into a negative number so that
88 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
89 ** the macro again restores the address.
90 */
91 #define ADDR(X)  (-1-(X))
92 
93 /*
94 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
95 ** header file that defines a number for each opcode used by the VDBE.
96 */
97 #include "opcodes.h"
98 
99 /*
100 ** Prototypes for the VDBE interface.  See comments on the implementation
101 ** for a description of what each of these routines does.
102 */
103 Vdbe *sqlite3VdbeCreate(sqlite*);
104 void sqlite3VdbeCreateCallback(Vdbe*, int*);
105 int sqlite3VdbeAddOp(Vdbe*,int,int,int);
106 int sqlite3VdbeOp3(Vdbe*,int,int,int,const char *zP3,int);
107 int sqlite3VdbeCode(Vdbe*,...);
108 int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
109 void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
110 void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
111 void sqlite3VdbeChangeP3(Vdbe*, int addr, const char *zP1, int N);
112 void sqlite3VdbeDequoteP3(Vdbe*, int addr);
113 int sqlite3VdbeFindOp(Vdbe*, int, int, int);
114 VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
115 int sqlite3VdbeMakeLabel(Vdbe*);
116 void sqlite3VdbeDelete(Vdbe*);
117 void sqlite3VdbeMakeReady(Vdbe*,int,int);
118 int sqlite3VdbeFinalize(Vdbe*,char**);
119 void sqlite3VdbeResolveLabel(Vdbe*, int);
120 int sqlite3VdbeCurrentAddr(Vdbe*);
121 void sqlite3VdbeTrace(Vdbe*,FILE*);
122 void sqlite3VdbeCompressSpace(Vdbe*,int);
123 int sqlite3VdbeReset(Vdbe*,char **);
124 int sqliteVdbeSetVariables(Vdbe*,int,const char**);
125 void sqlite3VdbeSetNumCols(Vdbe*,int);
126 int sqlite3VdbeSetColName(Vdbe*, int, const char *, int);
127 
128 #ifndef NDEBUG
129   void sqlite3VdbeComment(Vdbe*, const char*, ...);
130 # define VdbeComment(X)  sqlite3VdbeComment X
131 #else
132 # define VdbeComment(X)
133 #endif
134 
135 #endif
136