Skip to content

Commit

Permalink
vm: convert slurp and staticRead to vmops (#947)
Browse files Browse the repository at this point in the history
## Summary

Convert `slurp` and `staticRead` in the `system` module from magics to
VM callback operations.

## Details

Removal of the magic requires creating a conditional compilation step,
here `nimskullReworkStaticExec` conditional symbol was resued (see:
`condsym`). Two definitions of `slurp` and `staticRead` now exist, one
with the magic and one without. Without this, bootstrapping would no
longer work.

Also marked `slurp` as deprecated, `staticRead` is a far better name.
The reason for the deprecation is that `slurp` is just not as clear as
`staticRead`.

Removed `mSlurp` magic enum, and the `opcSlurp` VM opcode.
  • Loading branch information
saem authored Oct 8, 2023
1 parent 0030a8a commit 9087a62
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 29 deletions.
3 changes: 1 addition & 2 deletions compiler/ast/ast_query.nim
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,7 @@ proc isSinkType*(t: PType): bool {.inline.} =
t.kind == tySink

const magicsThatCanRaise* = {
mNone, mSlurp, mParseExprToAst, mParseStmtToAst, mEcho,
mChckRange }
mNone, mParseExprToAst, mParseStmtToAst, mEcho, mChckRange }

proc canRaiseConservative*(fn: PNode): bool =
if fn.kind == nkSym and fn.sym.magic notin magicsThatCanRaise:
Expand Down
2 changes: 1 addition & 1 deletion compiler/ast/ast_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ type
mDefined, mDeclared, mDeclaredInScope, mCompiles, mArrGet, mArrPut, mAsgn,
mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait,
mIs, mOf, mAddr, mType, mTypeOf,
mPlugin, mEcho, mShallowCopy, mSlurp,
mPlugin, mEcho, mShallowCopy,
mStatic,
mParseExprToAst, mParseStmtToAst, mExpandToAst, mQuoteAst,
mInc, mDec, mOrd,
Expand Down
2 changes: 1 addition & 1 deletion compiler/backend/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1839,7 +1839,7 @@ proc genMagicExpr(p: BProc, e: CgNode, d: var TLoc, op: TMagic) =
of mDefault: genDefault(p, e, d)
of mEcho: genEcho(p, e[1].skipConv)
of mArrToSeq: genArrToSeq(p, e, d)
of mNLen..mNError, mSlurp..mQuoteAst:
of mNLen..mNError, mStatic..mQuoteAst:
localReport(p.config, e.info, reportSym(
rsemConstExpressionExpected, e[0].sym))

Expand Down
2 changes: 1 addition & 1 deletion compiler/backend/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2009,7 +2009,7 @@ proc genMagic(p: PProc, n: CgNode, r: var TCompRes) =
of mDefault: genDefault(p, n, r)
of mWasMoved: genReset(p, n)
of mEcho: genEcho(p, n, r)
of mNLen..mNError, mSlurp:
of mNLen..mNError:
localReport(p.config, n.info, reportSym(
rsemConstExpressionExpected, n[0].sym))

Expand Down
7 changes: 1 addition & 6 deletions compiler/vm/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2026,6 +2026,7 @@ proc rawExecute(c: var TCtx, pc: var int): YieldReason =
heap: addr c.heap,
graph: c.graph,
config: c.config,
currentModule: c.module,
cache: c.cache,
idgen: c.idgen))
of ckDefault:
Expand Down Expand Up @@ -2625,12 +2626,6 @@ proc rawExecute(c: var TCtx, pc: var int): YieldReason =
raiseVmError(VmEvent(
kind: vmEvtNodeNotASymbol, ast: regs[rb].nimNode))

of opcSlurp:
decodeB(akString)
checkHandle(regs[rb])
regs[ra].strVal = opSlurp($regs[rb].strVal, c.debug[pc],
c.module, c.config)

of opcParseExprToAst, opcParseStmtToAst:
decodeBC(rkNimNode)

Expand Down
1 change: 0 additions & 1 deletion compiler/vm/vm_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ type
opcNccValue, opcNccInc, opcNcsAdd, opcNcsIncl, opcNcsLen, opcNcsAt,
opcNctPut, opcNctLen, opcNctGet, opcNctHasNext, opcNctNext, opcNodeId,

opcSlurp,
opcParseExprToAst,
opcParseStmtToAst,
opcNGetLineInfo, opcNSetLineInfo,
Expand Down
1 change: 1 addition & 0 deletions compiler/vm/vmdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ type
# compiler interfacing:
graph*: ModuleGraph
config*: ConfigRef
currentModule*: PSym ## module currently being compiled
cache*: IdentCache
idgen*: IdGenerator

Expand Down
1 change: 0 additions & 1 deletion compiler/vm/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,6 @@ proc genMagic(c: var TCtx; n: CgNode; dest: var TDest; m: TMagic) =
c.gABx(n, opcNSetType, tmp, c.genTypeInfo(n[1].typ))
c.gABC(n, opcTypeTrait, dest, tmp)
c.freeTemp(tmp)
of mSlurp: genUnaryABC(c, n, dest, opcSlurp)
of mNLen: genUnaryABI(c, n, dest, opcLenSeq, nimNodeFlag)
of mGetImpl: genUnaryABC(c, n, dest, opcGetImpl)
of mGetImplTransf: genUnaryABC(c, n, dest, opcGetImplTransf)
Expand Down
8 changes: 8 additions & 0 deletions compiler/vm/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@ iterator compileTimeOps*(): Override =

override "stdlib.os.getCurrentCompilerExe", proc (a: VmArgs) {.nimcall.} =
setResult(a, getAppFilename())

# xxx: not really a compile-time query, but runs at compiletime and unlike
# osOps it directly impacts compilation
for op in ["stdlib.system.slurp", "stdlib.system.staticRead"]:
override op, proc (a: VmArgs) {.nimcall.} =
let output = opSlurp(getString(a, 0), a.currentLineInfo, a.currentModule,
a.config)
writeResult(output)

iterator gorgeOps*(): Override =
## Special operations for executing external programs at compile time.
Expand Down
34 changes: 19 additions & 15 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2570,23 +2570,21 @@ proc `[]=`*[Idx, T](a: var array[Idx, T]; i: BackwardsIndex; x: T) {.inline.} =
proc `[]=`*(s: var string; i: BackwardsIndex; x: char) {.inline.} =
s[s.len - int(i)] = x

proc slurp*(filename: string): string {.magic: "Slurp".}
## This is an alias for `staticRead <#staticRead,string>`_.
## to be deprecated, use `staticRead`
when defined(nimskullReworkStaticExec):
proc slurp*(filename: string): string {.compileTime,
deprecated: "use staticRead".} = discard
## This is an alias for `staticRead <#staticRead,string>`_.

proc staticRead*(filename: string): string {.magic: "Slurp".}
## Compile-time `readFile <io.html#readFile,string>`_ proc for easy
## `resource`:idx: embedding:
##
## The maximum file size limit that `staticRead` and `slurp` can read is
## near or equal to the *free* memory of the device you are using to compile.
##
## .. code-block:: Nim
## const myResource = staticRead"mydatafile.bin"
##
## `slurp <#slurp,string>`_ is an alias for `staticRead`.
proc staticRead*(filename: string): string {.compileTime.} = discard
## Compile-time `readFile <io.html#readFile,string>`_ proc for easy
## `resource`:idx: embedding:
##
## The maximum file size limit that `staticRead` and `slurp` can read is
## near or equal to the *free* memory of the device you are using to compile.
##
## .. code-block:: Nim
## const myResource = staticRead"mydatafile.bin"

when defined(nimskullReworkStaticExec):
proc gorge*(command: string, input = "", cache = ""): string {.
compileTime, deprecated: "use staticExec".} = discard
## This is an alias for `staticExec <#staticExec,string,string,string>`_.
Expand Down Expand Up @@ -2620,6 +2618,12 @@ when defined(nimskullReworkStaticExec):
##
## Deprecate/Replace with variant that returns the exit code and output
else:
proc slurp*(filename: string): string {.magic: "Slurp".} = discard
## kept for bootstrapping

proc staticRead*(filename: string): string {.magic: "Slurp".} = discard
## kept for bootstrapping

proc gorge*(command: string, input = "", cache = ""): string {.
magic: "StaticExec".} = discard
## kept for bootstrapping
Expand Down
2 changes: 1 addition & 1 deletion tests/vm/tslurp.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
import std/os

template getScriptDir(): string =
parentDir(instantiationInfo(-1, true).filename)
Expand Down

0 comments on commit 9087a62

Please sign in to comment.