Skip to content

Conversation

@mat-hek
Copy link
Contributor

@mat-hek mat-hek commented Nov 13, 2025

In some cases, there's generic code that uses the bitstring modifier because it needs to work for both bitstrings and binaries, for example in erl_eval. Currently such code doesn't work even if no actual bitstrings are involved, and this PR attempts to fix it. In other words, this now works:

defmodule Foo do
  def pattern_match(x, s) do
    case x do
      <<a::bitstring-size(s), b::bitstring>> ->
        {a, b}

      _ ->
        :no_match
    end
  end
end

Foo.pattern_match(<<234, 123, 2>>, 16)
# => {<<234, 123>>, <<2>>}

TODO:

  • add tests
  • adjust jit.erl

@mat-hek mat-hek force-pushed the mf/upstream-support-aligned-bitstrings branch 2 times, most recently from bb34fe8 to 7bb58f2 Compare November 13, 2025 14:37
@mat-hek mat-hek force-pushed the mf/upstream-support-aligned-bitstrings branch from 576fb54 to ffcc68d Compare November 18, 2025 11:21
mat-hek added a commit to software-mansion/popcorn that referenced this pull request Nov 18, 2025
…408)

Closes #393

Once atomvm/AtomVM#1978 is merged and
downstreamed, the eval_bits patch won't be needed (unless there are
other unrelated patches in there OFC)
@mat-hek mat-hek force-pushed the mf/upstream-support-aligned-bitstrings branch from ffcc68d to 89e5cf8 Compare November 19, 2025 09:42
@mat-hek mat-hek changed the title Support creating and matching bitstrings that are in fact binaries (size divisible by 8) Support matching bitstrings that are in fact binaries (size divisible by 8) Nov 19, 2025
@mat-hek mat-hek force-pushed the mf/upstream-support-aligned-bitstrings branch 3 times, most recently from 5aa8f8c to c2b0ede Compare November 26, 2025 08:58
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is still an issue.
build-and-test (arm-linux-gnueabihf-gcc, -mcpu=cortex-a7 -mfloat-abi=hard -O2 -mthumb -mthumb-int is failing and this tells me there is a problem with JIT implementation.
We have to fix this before merging this PR. Maybe @pguyot has some advice here.

@mat-hek mat-hek force-pushed the mf/upstream-support-aligned-bitstrings branch from c2b0ede to 6c9843d Compare December 15, 2025 11:47
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests keep failing:

test_binary_to_term:
CRASH 
======
pid: <0.1.0>

Stacktrace:
[{test_binary_to_term,test_function,0,[{file,"/home/runner/work/AtomVM/AtomVM/tests/erlang_tests/test_binary_to_term.erl"},{line,316}]},{test_binary_to_term,start,0,[{file,"/home/runner/work/AtomVM/AtomVM/tests/erlang_tests/test_binary_to_term.erl"},{line,170}]}]

cp: #CP<module: 0, label: 52, offset: 2272>

x[0]: error
x[1]: {badmatch,<<1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,1,119,19,116,101,115,116,95,98,105,110,97,114,121,95,116,111,95,116,101,114,109,97,5,98,5,26,215,111,88,119,13,110,111,110,111,100,101,64,110,111,104,111,115,116,0,0,0,0,0,0,0,0,0,0,0,0,97,2>>}
x[2]: {2,2,74,1,[{0,8240},{0,27350}],error}

Stack 
-----

<<88,119,13,110,111,110,111,100,101,64,110,111,104,111,115,116,0,0,0,0,0,0,0,0,0,0,0,0>>
85645167
4
85
21
<<119,19,116,101,115,116,95,98,105,110,97,114,121,95,116,111,95,116,101,114,109>>
<<131,108,0,0,0,2,112,0,0,0,85,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,119,19,116,101,115,116,95,98,105,110,97,114,121,95,116,111,95,116,101,114,109,97,4,98,5,26,215,111,88,119,13,110,111,110,111,100,101,64,110,111,104,111,115,116,0,0,0,0,0,0,0,0,0,0,0,0,112,0,0,0,87,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,1,119,19,116,101,115,116,95,98,105,110,97,114,121,95,116,111,95,116,101,114,109,97,5,98,5,26,215,111,88,119,13,110,111,110,111,100,101,64,110,111,104,111,115,116,0,0,0,0,0,0,0,0,0,0,0,0,97,2,106>>
#CP<module: 0, label: 2, offset: 2760>
#CP<module: 0, label: 0, offset: 5424>


Mailbox
-------


Monitors
--------


**End Of Crash Report**
Expected 0 but result is not an integer
test_binary_to_term:FAILED

@mat-hek
Copy link
Contributor Author

mat-hek commented Dec 15, 2025

@bettio I'll try looking into it. There's also https://github.com/atomvm/AtomVM/actions/runs/20231091694/job/58073962162?pr=1978#step:18:596 failing, with disabled JIT 🤔 Instead of {<<123, 234>>, <<245>>}, {<<123, 234>>, <<234>>} is returned. Not sure if it's my changes or some other memory corruption

test_bs:
CRASH 
======
pid: <0.1.0>

Stacktrace:
[{test_bs,test_bs_match_bitstring_modifier,0,[{file,"/__w/AtomVM/AtomVM/tests/erlang_tests/test_bs.erl"},{line,688}]},{test_bs,start,0,[{file,"/__w/AtomVM/AtomVM/tests/erlang_tests/test_bs.erl"},{line,108}]}]

cp: #CP<module: 0, label: 399, offset: 42>

x[0]: error
x[1]: {badmatch,{<<123,234>>,<<234>>}}
x[2]: {2,2,49,1,[{0,902},{0,10758}],error}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants