SerenityOS / jakt

The Jakt Programming Language
BSD 2-Clause "Simplified" License
2.83k stars 239 forks source link

Error with '-r' for valid compilable source #1319

Open mingodad opened 1 year ago

mingodad commented 1 year ago

After building jakt with clang-15 and try the sample shown bellow I'm getting an error when trying to only run it:

uname -a
Linux laptop 5.4.0-135-generic #152~18.04.2-Ubuntu SMP Tue Nov 29 08:23:49 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
jakt -r fib.jakt 
Error: comptime format accepts at most two arguments, but got 3
───┬─ fib.jakt:6:5
 5 │     let n: i32 = 10 
 6 │     println("fib({}) = {}", n, fib(n: n)) 
   │           ╰─ comptime format accepts at most two arguments, but got 3
 7 │ } 
───┴─
Runtime error: Level 2 not synchronized (errno=45)

fib.jakt

/// Expect:
/// - output: "fib(10) = 89\n"

function main() {
    let n: i32 = 10
    println("fib({}) = {}", n, fib(n: n))
}

function fib(n: i32) -> i32 {
    if n < 2 {
    return 1
    }
    return fib(n: n-2)+fib(n: n-1)
}

Compile/run output:

$ jakt fib.jakt 
Building: 1/4 (jakt__prelude__prelude.cpp)
Building: 2/4 (fib.cpp)
Building: 3/4 (jakt__prelude__iteration.cpp)
Building: 4/4 (jakt__prelude__string.cpp)

$ build/fib
fib(10) = 89
mingodad commented 1 year ago

After rewriting it to only use two arguments with println it does run but it's a bit slow (built with -DCMAKE_BUILD_TYPE=Release).

/usr/bin/time jakt -r fib2.jakt 
fib(32) = 3524578
18.39user 0.00system 0:18.39elapsed 99%CPU (0avgtext+0avgdata 8484maxresident)k
0inputs+0outputs (0major+663minor)pagefaults 0swaps

fib.jackt

function main() {
    let n: i32 = 32
    println("fib(32) = {}", fib(n: n))
}

function fib(n: i32) -> i32 {
    if n < 2 {
    return 1
    }
    return fib(n: n-2)+fib(n: n-1)
}
/usr/bin/time lua-5.4 fibonacci.lua 
3524578
0.18user 0.00system 0:00.19elapsed 96%CPU (0avgtext+0avgdata 2196maxresident)k
624inputs+0outputs (1major+190minor)pagefaults 0swaps

fibonacci.lua

local function fib(n)
    if (n < 2) then 
    return 1
    else
    return fib(n-2) + fib(n-1)
    end
end

print(fib(32))

Native:

$ jakt fib2.jakt 
Building: 1/4 (jakt__prelude__prelude.cpp)
Building: 2/4 (fib2.cpp)
Building: 3/4 (jakt__prelude__iteration.cpp)
Building: 4/4 (jakt__prelude__string.cpp)

$ /usr/bin/time build/fib2
fib(32) = 3524578
0.15user 0.00system 0:00.16elapsed 99%CPU (0avgtext+0avgdata 3100maxresident)k
0inputs+0outputs (0major+121minor)pagefaults 0swaps

Optmized:

$ jakt -O fib2.jakt 
Building: 1/4 (jakt__prelude__prelude.cpp)
Building: 2/4 (fib2.cpp)
Building: 3/4 (jakt__prelude__iteration.cpp)
Building: 4/4 (jakt__prelude__string.cpp)
$ /usr/bin/time build/fib2
fib(32) = 3524578
0.01user 0.00system 0:00.01elapsed 100%CPU (0avgtext+0avgdata 3100maxresident)k
0inputs+0outputs (0major+119minor)pagefaults 0swaps
alimpfard commented 1 year ago

-r/--run uses an interpreter to run your code (which is still WIP), the flag you're looking for is probably --compile-run.

alimpfard commented 1 year ago

Unless the bug report is about the comptime interpreter

In which case, yeah, #soon maybe for the second one, and there's definitely a lot we can do to make it faster; but due to (3) I consider perf work on the interpreter to be premature...unless we come up with an entirely different architecture.

mingodad commented 1 year ago

Thank you for all feedback !

mingodad commented 1 year ago

The https://github.com/SerenityOS/serenity/blob/master/Userland/Libraries/LibJS/Interpreter.cpp seems to have a reasonable performance.

Although testing the sample bellow at https://libjs.dev/repl/ give an error:

function fib(n)
{
    if (n < 2) return 1;
    return fib(n-2) + fib(n-1);
}

let time = new Date();
console.log(fib(32)  , new Date() - time);

Console error (dev tools):

Uncaught RuntimeError: memory access out of bounds
    at libjs.wasm:0xbb37e
    at libjs.wasm:0xbace8
    at libjs.wasm:0xbac71
    at libjs.wasm:0xbafb6
    at libjs.wasm:0x14e69f
    at libjs.wasm:0x19dd15
    at libjs.wasm:0x19d7a1
    at libjs.wasm:0x14b946
    at libjs.wasm:0x510a9
    at libjs.wasm:0x5a0ab
$func1308 @ libjs.wasm:0xbb37e
$func1303 @ libjs.wasm:0xbace8
$func1302 @ libjs.wasm:0xbac71
$func1305 @ libjs.wasm:0xbafb6
$func1895 @ libjs.wasm:0x14e69f
$func2305 @ libjs.wasm:0x19dd15
$func2304 @ libjs.wasm:0x19d7a1
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a14e
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func427 @ libjs.wasm:0x5a0ab
$func400 @ libjs.wasm:0x529c6
$func347 @ libjs.wasm:0x4a575
$func357 @ libjs.wasm:0x4cda2
$func2309 @ libjs.wasm:0x19f3b9
$func2304 @ libjs.wasm:0x19d969
$func1879 @ libjs.wasm:0x14b946
$func389 @ libjs.wasm:0x510a9
$func379 @ libjs.wasm:0x4f91a
$func389 @ libjs.wasm:0x50c4d
$func374 @ libjs.wasm:0x4e9d3
$func347 @ libjs.wasm:0x4a575
$func362 @ libjs.wasm:0x4d543
$func1346 @ libjs.wasm:0xbea9c
$execute @ libjs.wasm:0x11422
(anonymous) @ libjs.js:1475
execute @ repl.js:101
repl.execute @ repl.js:165
(anonymous) @ repl.js:76
ADKaster commented 1 year ago

That just looks like a typical stack overflow. We can't possibly know the stack bounds on wasm without some serious hax. Any LibJS bugs or errors should be reported on the serenity issue tracker though.