vdbe.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifndef _SQLITE_VDBE_H_
  19. #define _SQLITE_VDBE_H_
  20. #include <stdio.h>
  21. /*
  22. ** A single VDBE is an opaque structure named "Vdbe". Only routines
  23. ** in the source file sqliteVdbe.c are allowed to see the insides
  24. ** of this structure.
  25. */
  26. typedef struct Vdbe Vdbe;
  27. /*
  28. ** The names of the following types declared in vdbeInt.h are required
  29. ** for the VdbeOp definition.
  30. */
  31. typedef struct Mem Mem;
  32. typedef struct SubProgram SubProgram;
  33. /*
  34. ** A single instruction of the virtual machine has an opcode
  35. ** and as many as three operands. The instruction is recorded
  36. ** as an instance of the following structure:
  37. */
  38. struct VdbeOp {
  39. u8 opcode; /* What operation to perform */
  40. signed char p4type; /* One of the P4_xxx constants for p4 */
  41. u8 opflags; /* Mask of the OPFLG_* flags in opcodes.h */
  42. u8 p5; /* Fifth parameter is an unsigned character */
  43. int p1; /* First operand */
  44. int p2; /* Second parameter (often the jump destination) */
  45. int p3; /* The third parameter */
  46. union { /* fourth parameter */
  47. int i; /* Integer value if p4type==P4_INT32 */
  48. void *p; /* Generic pointer */
  49. char *z; /* Pointer to data for string (char array) types */
  50. i64 *pI64; /* Used when p4type is P4_INT64 */
  51. double *pReal; /* Used when p4type is P4_REAL */
  52. FuncDef *pFunc; /* Used when p4type is P4_FUNCDEF */
  53. CollSeq *pColl; /* Used when p4type is P4_COLLSEQ */
  54. Mem *pMem; /* Used when p4type is P4_MEM */
  55. VTable *pVtab; /* Used when p4type is P4_VTAB */
  56. KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
  57. int *ai; /* Used when p4type is P4_INTARRAY */
  58. SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
  59. int (*xAdvance)(BtCursor *, int *);
  60. } p4;
  61. #ifdef SQLITE_DEBUG
  62. char *zComment; /* Comment to improve readability */
  63. #endif
  64. #ifdef VDBE_PROFILE
  65. int cnt; /* Number of times this instruction was executed */
  66. u64 cycles; /* Total time spent executing this instruction */
  67. #endif
  68. };
  69. typedef struct VdbeOp VdbeOp;
  70. /*
  71. ** A sub-routine used to implement a trigger program.
  72. */
  73. struct SubProgram {
  74. VdbeOp *aOp; /* Array of opcodes for sub-program */
  75. int nOp; /* Elements in aOp[] */
  76. int nMem; /* Number of memory cells required */
  77. int nCsr; /* Number of cursors required */
  78. int nOnce; /* Number of OP_Once instructions */
  79. void *token; /* id that may be used to recursive triggers */
  80. SubProgram *pNext; /* Next sub-program already visited */
  81. };
  82. /*
  83. ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
  84. ** it takes up less space.
  85. */
  86. struct VdbeOpList {
  87. u8 opcode; /* What operation to perform */
  88. signed char p1; /* First operand */
  89. signed char p2; /* Second parameter (often the jump destination) */
  90. signed char p3; /* Third parameter */
  91. };
  92. typedef struct VdbeOpList VdbeOpList;
  93. /*
  94. ** Allowed values of VdbeOp.p4type
  95. */
  96. #define P4_NOTUSED 0 /* The P4 parameter is not used */
  97. #define P4_DYNAMIC (-1) /* Pointer to a string obtained from sqliteMalloc() */
  98. #define P4_STATIC (-2) /* Pointer to a static string */
  99. #define P4_COLLSEQ (-4) /* P4 is a pointer to a CollSeq structure */
  100. #define P4_FUNCDEF (-5) /* P4 is a pointer to a FuncDef structure */
  101. #define P4_KEYINFO (-6) /* P4 is a pointer to a KeyInfo structure */
  102. #define P4_MEM (-8) /* P4 is a pointer to a Mem* structure */
  103. #define P4_TRANSIENT 0 /* P4 is a pointer to a transient string */
  104. #define P4_VTAB (-10) /* P4 is a pointer to an sqlite3_vtab structure */
  105. #define P4_MPRINTF (-11) /* P4 is a string obtained from sqlite3_mprintf() */
  106. #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
  107. #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
  108. #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
  109. #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
  110. #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
  111. #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
  112. /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
  113. ** is made. That copy is freed when the Vdbe is finalized. But if the
  114. ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
  115. ** gets freed when the Vdbe is finalized so it still should be obtained
  116. ** from a single sqliteMalloc(). But no copy is made and the calling
  117. ** function should *not* try to free the KeyInfo.
  118. */
  119. #define P4_KEYINFO_HANDOFF (-16)
  120. #define P4_KEYINFO_STATIC (-17)
  121. /*
  122. ** The Vdbe.aColName array contains 5n Mem structures, where n is the
  123. ** number of columns of data returned by the statement.
  124. */
  125. #define COLNAME_NAME 0
  126. #define COLNAME_DECLTYPE 1
  127. #define COLNAME_DATABASE 2
  128. #define COLNAME_TABLE 3
  129. #define COLNAME_COLUMN 4
  130. #ifdef SQLITE_ENABLE_COLUMN_METADATA
  131. # define COLNAME_N 5 /* Number of COLNAME_xxx symbols */
  132. #else
  133. # ifdef SQLITE_OMIT_DECLTYPE
  134. # define COLNAME_N 1 /* Store only the name */
  135. # else
  136. # define COLNAME_N 2 /* Store the name and decltype */
  137. # endif
  138. #endif
  139. /*
  140. ** The following macro converts a relative address in the p2 field
  141. ** of a VdbeOp structure into a negative number so that
  142. ** sqlite3VdbeAddOpList() knows that the address is relative. Calling
  143. ** the macro again restores the address.
  144. */
  145. #define ADDR(X) (-1-(X))
  146. /*
  147. ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
  148. ** header file that defines a number for each opcode used by the VDBE.
  149. */
  150. #include "opcodes.h"
  151. /*
  152. ** Prototypes for the VDBE interface. See comments on the implementation
  153. ** for a description of what each of these routines does.
  154. */
  155. Vdbe *sqlite3VdbeCreate(sqlite3*);
  156. int sqlite3VdbeAddOp0(Vdbe*,int);
  157. int sqlite3VdbeAddOp1(Vdbe*,int,int);
  158. int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
  159. int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
  160. int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
  161. int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
  162. int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
  163. void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
  164. void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
  165. void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
  166. void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
  167. void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
  168. void sqlite3VdbeJumpHere(Vdbe*, int addr);
  169. void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
  170. void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
  171. void sqlite3VdbeUsesBtree(Vdbe*, int);
  172. VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
  173. int sqlite3VdbeMakeLabel(Vdbe*);
  174. void sqlite3VdbeRunOnlyOnce(Vdbe*);
  175. void sqlite3VdbeDelete(Vdbe*);
  176. void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
  177. void sqlite3VdbeMakeReady(Vdbe*,Parse*);
  178. int sqlite3VdbeFinalize(Vdbe*);
  179. void sqlite3VdbeResolveLabel(Vdbe*, int);
  180. int sqlite3VdbeCurrentAddr(Vdbe*);
  181. #ifdef SQLITE_DEBUG
  182. int sqlite3VdbeAssertMayAbort(Vdbe *, int);
  183. void sqlite3VdbeTrace(Vdbe*,FILE*);
  184. #endif
  185. void sqlite3VdbeResetStepResult(Vdbe*);
  186. void sqlite3VdbeRewind(Vdbe*);
  187. int sqlite3VdbeReset(Vdbe*);
  188. void sqlite3VdbeSetNumCols(Vdbe*,int);
  189. int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
  190. void sqlite3VdbeCountChanges(Vdbe*);
  191. sqlite3 *sqlite3VdbeDb(Vdbe*);
  192. void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
  193. void sqlite3VdbeSwap(Vdbe*,Vdbe*);
  194. VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
  195. sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
  196. void sqlite3VdbeSetVarmask(Vdbe*, int);
  197. #ifndef SQLITE_OMIT_TRACE
  198. char *sqlite3VdbeExpandSql(Vdbe*, const char*);
  199. #endif
  200. void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
  201. int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
  202. UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
  203. #ifndef SQLITE_OMIT_TRIGGER
  204. void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
  205. #endif
  206. #ifndef NDEBUG
  207. void sqlite3VdbeComment(Vdbe*, const char*, ...);
  208. # define VdbeComment(X) sqlite3VdbeComment X
  209. void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
  210. # define VdbeNoopComment(X) sqlite3VdbeNoopComment X
  211. #else
  212. # define VdbeComment(X)
  213. # define VdbeNoopComment(X)
  214. #endif
  215. #endif