Skip to content

Commit

Permalink
(add) balde: add --disable-elide-loops and --help
Browse files Browse the repository at this point in the history
  • Loading branch information
xTrayambak committed Dec 18, 2024
1 parent 0356446 commit 1a06f28
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 deletions.
1 change: 0 additions & 1 deletion nim.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# Bali-specific flags
# --define:baliUseStdBase64 # Instead of simdutf's SIMD accelerated base64 encoder/decoder, use the (slower) ones in the Nim standard library.
# --define:baliEmitterDontElideLoops # Don't try eliding loops whenever possible

# Enable SIMD support
--passC: "-march=znver3 -mtune=znver3 -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2 -mpclmul -mavx -mavx2"
39 changes: 37 additions & 2 deletions src/balde.nim
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ proc die(msg: varargs[string]) {.inline, noReturn.} =
proc allocRuntime*(ctx: Input, file: string, ast: AST, repl: bool = false): Runtime =
let test262 = ctx.enabled("test262")
var runtime = newRuntime(
file, ast, InterpreterOpts(test262: test262, dumpBytecode: ctx.enabled("dump-bytecode"), repl: repl)
file, ast, InterpreterOpts(
test262: test262,
dumpBytecode: ctx.enabled("dump-bytecode", "D"),
repl: repl,
codegen: CodegenOpts(
elideLoops: not ctx.enabled("disable-loop-elision")
)
)
)
let expStr = ctx.flag("enable-experiments")

Expand Down Expand Up @@ -107,7 +114,7 @@ proc execFile(ctx: Input, file: string) {.inline.} =
die "failed to open file:", exc.msg
""

if ctx.enabled("dump-tokens"):
if ctx.enabled("dump-tokens", "T"):
let excludeWs = ctx.enabled("no-whitespace")
let tok = newTokenizer(source)
while not tok.eof:
Expand Down Expand Up @@ -280,6 +287,31 @@ You can also just type in JavaScript expressions to evaluate them."""
when promptHistory:
discard noise.historySave(file)

proc showHelp {.noReturn.} =
let name = getAppFilename().splitPath().tail
echo """
Usage: $1 [options] [script]
The Bali Debugger provides a command line interface to the Bali JavaScript engine.
If no files are provided for execution, then Balde starts a read-eval-print-loop (REPL)
session.
Version: $2
Options:
--help, -h Show this message.
--verbose, -v Show additional debug logs, useful for debugging the engine.
--dump-bytecode, -D Dump bytecode for the next evaluation.
--dump-tokens, -T Dump tokens for the provided file.
--dump-ast Dump the abstract syntax tree for the JavaScript file.
--dump-no-eval Dump the abstract syntax tree for the JavaScript file, bypassing the IR generation phase entirely.
--disable-loop-elision Don't attempt to elide loops in the IR generation phase.
--enable-experiments:<a>;<b>; ... <z> Enable certain experimental features that aren't stable yet.
""" % [
name, Version
]
quit(0)

proc main() {.inline.} =
enableLogging()

Expand All @@ -291,6 +323,9 @@ proc main() {.inline.} =
if input.enabled("verbose", "v"):
setLogFilter(lvlAll)

if input.enabled("help", "h"):
showHelp()

if input.command.len < 1:
if not input.enabled("verbose", "v"):
setLogFilter(lvlError)
Expand Down
13 changes: 6 additions & 7 deletions src/bali/runtime/interpreter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,12 @@ proc generateIR*(
runtime.ir.copyAtom(runtime.index(stmt.cpImmutSourceIdent, defaultParams(fn)), dest)
of WhileStmt:
debug "emitter: generate IR for while loop"
when not defined(baliEmitterDontElideLoops):
if stmt.whStmtOnlyMutatesItsState(stmt.whBranch.getValueCaptures()):
debug "emitter: while loop only mutates its own state - eliding it away"
if runtime.optimizeAwayStateMutatorLoop(fn, stmt):
return # we can fully skip creating all of the expensive comparison checks! :D
else:
warn "emitter: failed to elide state mutator loop :("
if runtime.opts.codegen.elideLoops and stmt.whStmtOnlyMutatesItsState(stmt.whBranch.getValueCaptures()):
debug "emitter: while loop only mutates its own state - eliding it away"
if runtime.optimizeAwayStateMutatorLoop(fn, stmt):
return # we can fully skip creating all of the expensive comparison checks! :D
else:
debug "emitter: failed to elide state mutator loop :("

runtime.expand(fn, stmt)

Expand Down
6 changes: 5 additions & 1 deletion src/bali/runtime/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ type
ExperimentOpts* = object
dateRoutines*: bool

CodegenOpts* = object
elideLoops*: bool = true

InterpreterOpts* = object
test262*: bool = false
repl*: bool = false
dumpBytecode*: bool = false


codegen*: CodegenOpts
experiments*: ExperimentOpts

JSType* = object
Expand Down
2 changes: 0 additions & 2 deletions src/bali/stdlib/types/std_string.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ proc generateStdIr*(runtime: Runtime) =
res &= nextString

# 5. Return R.
echo runtime.argumentcount
echo res
ret res
)

Expand Down
3 changes: 2 additions & 1 deletion tests/data/string-concat-001.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var i = 0
while (i < 4)
{
let val = vals[i]
console.log(x.concat(val))
let cat = x.concat(val)
console.log(cat)
i++
}

0 comments on commit 1a06f28

Please sign in to comment.