summary refs log tree commit diff stats
path: root/query.go
Commit message (Expand)AuthorAgeFilesLines
* ?page=N query addedBen Morrison2019-06-031-17/+41
* bugfix: errors logged as strings (prev. bytes)Ben Morrison2019-05-311-1/+1
* parseQueryOut() no longer adds newline to last element, test updated to accou...Ben Morrison2019-05-271-2/+2
* merging user keyword/url queriesBen Morrison2019-05-271-1/+5
* simplified indexHandler to reference staticCacheBen Morrison2019-05-261-0/+4
* tweaked error handlingBen Morrison2019-05-221-4/+1
* tuning query handlingBen Morrison2019-05-221-5/+28
* tuning query handlingBen Morrison2019-05-221-5/+9
* streamlined error handling and misc minor fixesBen Morrison2019-05-201-18/+25
* endpoint query function addedBen Morrison2019-05-201-0/+83
4-20 11:36:13 +0200 VM: minor fixes to make lexim work' href='/ahoang/Nim/commit/compiler/vmdef.nim?h=devel&id=7be092bba59ceadeccff1fd884cfde0d4df92a44'>7be092bba ^
ee1192679 ^

b30e237a3 ^




b30e237a3 ^




7be092bba ^
b30e237a3 ^




6e584c42c ^

b30e237a3 ^


b30e237a3 ^

6e584c42c ^



b30e237a3 ^
27b7ecbbf ^
7be092bba ^

b30e237a3 ^





b4e25a637 ^
b30e237a3 ^
7be092bba ^

1a0417fbe ^


b30e237a3 ^


17cace280 ^
d3fc6e1f2 ^

e53fc9128 ^
7be092bba ^
27b7ecbbf ^



7be092bba ^
ef975d277 ^

7be092bba ^



ef975d277 ^


7be092bba ^
288cd05f8 ^

d18c82a9e ^
7be092bba ^
ef975d277 ^



b2f577df2 ^
ef975d277 ^


52f092d02 ^
aefa0da8a ^


695e2e970 ^
7be092bba ^
b30e237a3 ^




ef975d277 ^

ef975d277 ^
27b7ecbbf ^
7be092bba ^
b30e237a3 ^


ee1192679 ^
b30e237a3 ^






b30e237a3 ^
6e584c42c ^
b30e237a3 ^
27b7ecbbf ^
b30e237a3 ^
d15788d00 ^
6e584c42c ^
b30e237a3 ^
f095e5127 ^
5f943cf4b ^
d3fc6e1f2 ^
f063943d5 ^

b30e237a3 ^




3d1805337 ^




4ffe6a828 ^
3d1805337 ^
4ffe6a828 ^
3d1805337 ^






b30e237a3 ^




27b7ecbbf ^

b30e237a3 ^



41587a533 ^

b30e237a3 ^


aa92669d9 ^
b30e237a3 ^

1f85b3b5d ^



b2f577df2 ^
9d488261d ^
1f85b3b5d ^
7be092bba ^
b30e237a3 ^




7be092bba ^
b30e237a3 ^




ef975d277 ^

3d1805337 ^
9035d15ed ^
0c31686fe ^
ee1192679 ^
25610a2f7 ^
1f85b3b5d ^
b2f577df2 ^
773d17cd1 ^
b30e237a3 ^

f9861e1fa ^

7be092bba ^
773d17cd1 ^
27b7ecbbf ^
328f19329 ^
25610a2f7 ^
773d17cd1 ^

b30e237a3 ^
6a9baf3fd ^


903ca7828 ^
6a9baf3fd ^
1f85b3b5d ^


b30e237a3 ^


d3fc6e1f2 ^

b30e237a3 ^
d0438540d ^
b30e237a3 ^
88a5e9d88 ^


60b187513 ^




be4526362 ^
60b187513 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

 
                            






                                                                           
                                                                     
 
                                         




                                                    
                                                             

 




                            




                                         
 




                   

                 


                         

                        



                
                
                           

              





                 
                                                                             
                                                                             

                                                                       


                                                                       


                                                                    
                           

                                               
                           
 



                  
 

                    



                 


                
 

                                                                             
                                                                         
 



                      
                      


                
                                            


                  
               
 




                                                                             

                 
                
              
 


                                 
                                                      






                                   
                                               
                 
                                        
                                              
                                      
                                               
 
                                          
                
                                        
                 

                                    




                           




                                                                            
                                                                        
                                                   
                                                          






                                                                        




                                                                                

                                                                                



                                                                     

                                                                     


                                                         
              

                                                                 



                          
                            
                               
                                               
 




                                                                            
                             




                                                                               

                    
                    
                            
                      
                        
                                                                            
                                                          
                      
                      

                           

                      
 
                                                     
                             
                                                                                 
                                                                               

                                                                         
 


                                      
                                      
 


                                                                     


                                                                  

                                                  
                                 
                                                         
 


                                                      




                                                                                  
 
                                                         
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module contains the type definitions for the new evaluation engine.
## An instruction is 1-3 int32s in memory, it is a register based VM.

import ast, passes, msgs, idents, intsets

const
  byteExcess* = 128 # we use excess-K for immediates
  wordExcess* = 32768

  MaxLoopIterations* = 1500_000 # max iterations of all loops


type
  TRegister* = range[0..255]
  TDest* = range[-1 .. 255]
  TInstr* = distinct uint32

  TOpcode* = enum
    opcEof,         # end of code
    opcRet,         # return
    opcYldYoid,     # yield with no value
    opcYldVal,      # yield with a value

    opcAsgnInt,
    opcAsgnStr,
    opcAsgnFloat,
    opcAsgnRef,
    opcAsgnComplex,
    opcRegToNode,
    opcNodeToReg,

    opcLdArr,  # a = b[c]
    opcWrArr,  # a[b] = c
    opcLdObj,  # a = b.c
    opcWrObj,  # a.b = c
    opcAddrReg,
    opcAddrNode,
    opcLdDeref,
    opcWrDeref,
    opcWrStrIdx,
    opcLdStrIdx, # a = b[c]

    opcAddInt,
    opcAddImmInt,
    opcSubInt,
    opcSubImmInt,
    opcLenSeq,
    opcLenStr,

    opcIncl, opcInclRange, opcExcl, opcCard, opcMulInt, opcDivInt, opcModInt,
    opcAddFloat, opcSubFloat, opcMulFloat, opcDivFloat, opcShrInt, opcShlInt,
    opcBitandInt, opcBitorInt, opcBitxorInt, opcAddu, opcSubu, opcMulu,
    opcDivu, opcModu, opcEqInt, opcLeInt, opcLtInt, opcEqFloat,
    opcLeFloat, opcLtFloat, opcLeu, opcLtu,
    opcEqRef, opcEqNimrodNode, opcSameNodeType,
    opcXor, opcNot, opcUnaryMinusInt, opcUnaryMinusFloat, opcBitnotInt,
    opcEqStr, opcLeStr, opcLtStr, opcEqSet, opcLeSet, opcLtSet,
    opcMulSet, opcPlusSet, opcMinusSet, opcSymdiffSet, opcConcatStr,
    opcContainsSet, opcRepr, opcSetLenStr, opcSetLenSeq,
    opcIsNil, opcOf, opcIs,
    opcSubStr, opcParseFloat, opcConv, opcCast,
    opcQuit, opcReset,
    opcNarrowS, opcNarrowU,

    opcAddStrCh,
    opcAddStrStr,
    opcAddSeqElem,
    opcRangeChck,

    opcNAdd,
    opcNAddMultiple,
    opcNKind,
    opcNIntVal,
    opcNFloatVal,
    opcNSymbol,
    opcNIdent,
    opcNGetType,
    opcNStrVal,

    opcNSetIntVal,
    opcNSetFloatVal, opcNSetSymbol, opcNSetIdent, opcNSetType, opcNSetStrVal,
    opcNNewNimNode, opcNCopyNimNode, opcNCopyNimTree, opcNDel, opcGenSym,

    opcSlurp,
    opcGorge,
    opcParseExprToAst,
    opcParseStmtToAst,
    opcQueryErrorFlag,
    opcNError,
    opcNWarning,
    opcNHint,
    opcNGetLine, opcNGetColumn, opcNGetFile,
    opcEqIdent,
    opcStrToIdent,
    opcIdentToStr,
    opcGetImpl,

    opcEcho,
    opcIndCall, # dest = call regStart, n; where regStart = fn, arg1, ...
    opcIndCallAsgn, # dest = call regStart, n; where regStart = fn, arg1, ...

    opcRaise,
    opcNChild,
    opcNSetChild,
    opcCallSite,
    opcNewStr,

    opcTJmp,  # jump Bx if A != 0
    opcFJmp,  # jump Bx if A == 0
    opcJmp,   # jump Bx
    opcJmpBack, # jump Bx; resulting from a while loop
    opcBranch,  # branch for 'case'
    opcTry,
    opcExcept,
    opcFinally,
    opcFinallyEnd,
    opcNew,
    opcNewSeq,
    opcLdNull,    # dest = nullvalue(types[Bx])
    opcLdNullReg,
    opcLdConst,   # dest = constants[Bx]
    opcAsgnConst, # dest = copy(constants[Bx])
    opcLdGlobal,  # dest = globals[Bx]
    opcLdGlobalAddr, # dest = addr(globals[Bx])

    opcLdImmInt,  # dest = immediate value
    opcNBindSym,
    opcSetType,   # dest.typ = types[Bx]
    opcTypeTrait,
    opcMarshalLoad, opcMarshalStore,
    opcToNarrowInt

  TBlock* = object
    label*: PSym
    fixups*: seq[TPosition]

  TEvalMode* = enum           ## reason for evaluation
    emRepl,                   ## evaluate because in REPL mode
    emConst,                  ## evaluate for 'const' according to spec
    emOptimize,               ## evaluate for optimization purposes (same as
                              ## emConst?)
    emStaticExpr,             ## evaluate for enforced compile time eval
                              ## ('static' context)
    emStaticStmt              ## 'static' as an expression

  TSandboxFlag* = enum        ## what the evaluation engine should allow
    allowCast,                ## allow unsafe language feature: 'cast'
    allowFFI,                 ## allow the FFI
    allowInfiniteLoops        ## allow endless loops
  TSandboxFlags* = set[TSandboxFlag]

  TSlotKind* = enum   # We try to re-use slots in a smart way to
                      # minimize allocations; however the VM supports arbitrary
                      # temporary slot usage. This is required for the parameter
                      # passing implementation.
    slotEmpty,        # slot is unused
    slotFixedVar,     # slot is used for a fixed var/result (requires copy then)
    slotFixedLet,     # slot is used for a fixed param/let
    slotTempUnknown,  # slot but type unknown (argument of proc call)
    slotTempInt,      # some temporary int
    slotTempFloat,    # some temporary float
    slotTempStr,      # some temporary string
    slotTempComplex,  # some complex temporary (s.node field is used)
    slotTempPerm      # slot is temporary but permanent (hack)

  PProc* = ref object
    blocks*: seq[TBlock]    # blocks; temp data structure
    sym*: PSym
    slots*: array[TRegister, tuple[inUse: bool, kind: TSlotKind]]
    maxSlots*: int

  VmArgs* = object
    ra*, rb*, rc*: Natural
    slots*: pointer
    currentException*: PNode
    currentLineInfo*: TLineInfo
  VmCallback* = proc (args: VmArgs) {.closure.}

  PCtx* = ref TCtx
  TCtx* = object of passes.TPassContext # code gen context
    code*: seq[TInstr]
    debug*: seq[TLineInfo]  # line info for every instruction; kept separate
                            # to not slow down interpretation
    globals*: PNode         #
    constants*: PNode       # constant data
    types*: seq[PType]      # some instructions reference types (e.g. 'except')
    currentExceptionA*, currentExceptionB*: PNode
    exceptionInstr*: int # index of instruction that raised the exception
    prc*: PProc
    module*: PSym
    callsite*: PNode
    mode*: TEvalMode
    features*: TSandboxFlags
    traceActive*: bool
    loopIterations*: int
    comesFromHeuristic*: TLineInfo # Heuristic for better macro stack traces
    callbacks*: seq[tuple[key: string, value: VmCallback]]
    errorFlag*: string
    cache*: IdentCache

  TPosition* = distinct int

  PEvalContext* = PCtx

proc newCtx*(module: PSym; cache: IdentCache): PCtx =
  PCtx(code: @[], debug: @[],
    globals: newNode(nkStmtListExpr), constants: newNode(nkStmtList), types: @[],
    prc: PProc(blocks: @[]), module: module, loopIterations: MaxLoopIterations,
    comesFromHeuristic: unknownLineInfo(), callbacks: @[], errorFlag: "",
    cache: cache)

proc refresh*(c: PCtx, module: PSym) =
  c.module = module
  c.prc = PProc(blocks: @[])
  c.loopIterations = MaxLoopIterations

proc registerCallback*(c: PCtx; name: string; callback: VmCallback) =
  c.callbacks.add((name, callback))

const
  firstABxInstr* = opcTJmp
  largeInstrs* = { # instructions which use 2 int32s instead of 1:
    opcSubStr, opcConv, opcCast, opcNewSeq, opcOf,
    opcMarshalLoad, opcMarshalStore}
  slotSomeTemp* = slotTempUnknown
  relativeJumps* = {opcTJmp, opcFJmp, opcJmp, opcJmpBack}

# flag is used to signal opcSeqLen if node is NimNode.
const nimNodeFlag* = 16

template opcode*(x: TInstr): TOpcode = TOpcode(x.uint32 and 0xff'u32)
template regA*(x: TInstr): TRegister = TRegister(x.uint32 shr 8'u32 and 0xff'u32)
template regB*(x: TInstr): TRegister = TRegister(x.uint32 shr 16'u32 and 0xff'u32)
template regC*(x: TInstr): TRegister = TRegister(x.uint32 shr 24'u32)
template regBx*(x: TInstr): int = (x.uint32 shr 16'u32).int

template jmpDiff*(x: TInstr): int = regBx(x) - wordExcess