Skip to content

Tolk: slice.skipBits(0) optimization emits invalid 0 LDU (unassemblable Fift) #2036

@Gusarich

Description

@Gusarich

With default optimization (-O2), Tolk rewrites slice.skipBits(0) into 0 LDU + NIP in the generated Fift. 0 LDU is not a valid instruction encoding and fift fails to interpret the produced .fif, so code that should be a no-op becomes uncompilable at the assembler stage.

Reproduction

Create /tmp/tolk_skipbits0_invalid_ldu.tolk:

tolk 1.0

fun onInternalMessage() { return 0; }

@method_id(100)
fun skip0_single(): int {
    var s: slice = beginCell().endCell().beginParse();
    s.skipBits(0);
    s.assertEnd();
    return 123;
}

Compile (default -O2):

./artifacts/tolk /tmp/tolk_skipbits0_invalid_ldu.tolk > /tmp/tolk_skipbits0_invalid_ldu.fif

Create /tmp/tolk_skipbits0_invalid_ldu_runner.fif:

"/tmp/tolk_skipbits0_invalid_ldu.fif" include <s constant code
100 code 1 runvmx .s cr { drop } depth 1- times

Run (fails while interpreting the generated .fif):

./artifacts/fift -I artifacts/lib /tmp/tolk_skipbits0_invalid_ldu_runner.fif

Observed output

fift errors out with:

LDU:integer does not fit into cell

Expected behavior

skipBits(0) should be a no-op and the program should assemble and run, returning 123 0.

Codegen evidence

In the generated .fif, the optimizer emits:

0 LDU
NIP

Analysis

The optimizer rewrite for MY_skip_bits does not special-case total_skip_bits == 0 and produces 0 LDU, which is invalid. This rewrite should emit a no-op when skipping 0 bits.

Metadata

Metadata

Assignees

No one assigned

    Labels

    TolkRelated to Tolk Language / compiler / toolingllm-fuzzing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions