Skip to content

Commit ac18fb6

Browse files
committed
codegen + vm optimizations
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent a61b301 commit ac18fb6

File tree

6 files changed

+1004
-530
lines changed

6 files changed

+1004
-530
lines changed

src/tim/engine/chunk.nim

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Made by Humans from OpenPeeps
88
# https://github.com/openpeeps/tim | https://openpeeps.dev/packages/tim
99

10-
import std/[tables]
10+
import std/[tables, hashes, dynlib]
1111

1212
import value
1313

@@ -20,20 +20,29 @@ type
2020
opcPushTrue = "pushTrue"
2121
opcPushFalse = "pushFalse"
2222
opcPushNil = "pushNil"
23+
opcPushJNil = "pushJNil" ## push a JSON nil
24+
2325
opcPushI = "pushI" ## push int
2426
opcPushF = "pushF" ## push float
2527
opcPushS = "pushS" ## push string
2628
opcPushG = "pushG" ## push global
2729
opcPopG = "popG" ## pop global
2830
opcPushL = "pushL" ## push local
2931
opcPopL = "popL" ## pop local
32+
33+
opcFFIGetProc = "ffiGetProc" ## get a symbol from a dynamic library
34+
opcPushPointer = "pushPointer" # push a pointer value onto the stack
35+
opcPopPointer = "popPointer" # pop a pointer value from the stack
3036

3137
opcConstrObj = "constrObj" ## construct object
3238

3339
opcGetF = "getF" ## push field
3440
opcSetF = "setF" ## pop field
41+
3542
opcConstrArray = "constrArray" ## construct array
3643
opcGetI = "getI" ## get array item
44+
opcSetI = "setI" ## set array item
45+
3746
opcDiscard = "discard" ## discard values
3847

3948
# json operations
@@ -95,8 +104,10 @@ type
95104
opcViewLoader = "viewLoader" ## load a view
96105
opcHalt = "halt" ## halt the VM
97106

98-
Script* = ref object
107+
Script* {.acyclic.} = ref object
99108
stdpos*: int
109+
libs*: Table[string, LibHandle]
110+
## a table of dynamic libraries loaded by this script
100111
procs*: seq[Proc]
101112
## all procs declared in this script
102113
procsExport*: seq[Proc]
@@ -118,7 +129,7 @@ type
118129
ln, col: int
119130
runLength: int
120131

121-
Chunk* = ref object
132+
Chunk* {.acyclic.} = ref object
122133
## A chunk of bytecode.
123134
file*: string ## the filename of the module this chunk belongs to
124135
code*: seq[uint8] ## the raw bytecode
@@ -131,7 +142,7 @@ type
131142
pkNative ## a native (bytecode) proc
132143
pkForeign ## a foreign (Nim) proc
133144

134-
Proc* = ref object
145+
Proc* {.acyclic.} = ref object
135146
## A runtime procedure.
136147
name*: string
137148
case kind*: ProcKind
@@ -144,7 +155,6 @@ type
144155

145156
proc addLineInfo*(chunk: var Chunk, n: int) =
146157
## Add ``n`` line info entries to the chunk.
147-
148158
if chunk.lineInfo.len > 0:
149159
if chunk.lineInfo[^1].ln == chunk.ln and
150160
chunk.lineInfo[^1].col == chunk.col:
@@ -191,6 +201,11 @@ proc emit*(chunk: var Chunk, val: float64) =
191201
chunk.addLineInfo(ValueSize)
192202
chunk.code.add(cast[array[sizeof(float64), uint8]](val))
193203

204+
proc emit*(chunk: var Chunk, xptr: pointer) =
205+
## Emit a `pointer`.
206+
chunk.addLineInfo(sizeof(pointer))
207+
chunk.code.add(cast[array[sizeof(pointer), uint8]](xptr))
208+
194209
proc emitHole*(chunk: var Chunk, size: int): int =
195210
## Emit a hole, to be filled later by ``fillHole``.
196211
result = chunk.code.len
@@ -261,3 +276,10 @@ proc newScript*(main: Chunk): Script =
261276
## Create a new script, with the given main chunk.
262277
result = Script(mainChunk: main)
263278

279+
proc hash*(x: Chunk): Hash =
280+
## Hashes a Chunk by its address
281+
hash(cast[pointer](x))
282+
283+
proc `==`*(a, b: Chunk): bool =
284+
## Compares two Chunks by address
285+
hash(a) == hash(b)

0 commit comments

Comments
 (0)