Skip to content

Use TERM_PRIMARY_* and TERM_IMMED* macros, following erl_term.h #1701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

pguyot
Copy link
Collaborator

@pguyot pguyot commented Jun 10, 2025

Erlang/OTP's erl_term.h defines several macros to encode terms and
distinguishes TAG_PRIMARY_* for the two lowest bits, _TAG_IMMED1* for
the lowest four bits and _TAG_IMMED2* for the lowest six bits.

AtomVM term.h uses TERM_BOXED_* for the first word of boxed items.
It used magic number 0x2 or TERM_BOXED_VALUE_TAG instead of what
erl_term.h calls TAG_PRIMARY_BOXED, which is confusing as it's not related
to the first word of boxed item but to the (lowest two bits of the) term itself.

This change introduces TERM_PRIMARY_* macros following Erlang/OTP erl_term.h's
TAG_PRIMARY_* macros, and replaces all matching magic numbers with these
macros.

It also introduces TERM_IMMED2_* macros and uses them instead of magic
numbers.

TERM_BOXED_VALUE_TAG is marked as deprecated but kept.

No macro called TERM_IMMED1_* is introduced as AtomVM already uses
non-confusing macros TERM_PID_TAG, TERM_PORT_TAG and TERM_INTEGER_TAG.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

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.

Do you mind leaving a couple of lines of explanation in the commit message about why we are making this change?

@pguyot pguyot force-pushed the w24/use-erl_term-macros branch from f374948 to 9ce5c26 Compare June 15, 2025 19:52
@@ -2080,7 +2091,7 @@ static inline term term_alloc_bin_match_state(term binary_or_state, int slots, H
static inline void term_truncate_boxed(term boxed, size_t new_size, Heap *heap)
{
/* boxed: 10 */
TERM_DEBUG_ASSERT((t & 0x3) == 0x2);
TERM_DEBUG_ASSERT((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
Copy link
Contributor

Choose a reason for hiding this comment

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

t -> boxed

jgonet added a commit to jgonet/AtomVM that referenced this pull request Jun 16, 2025
memory.c had a missing semicolon, get/put_tuple_element in term.h
unpacked header directly instead of using function.
Additionally, first condition is already checked by term_is_tuple().

There's one unfixed assert
TERM_DEBUG_ASSERT((t & 0x3) == 0x2);
but fixing it would create conflicts with atomvm#1701 (changes magic values to macros).

Signed-off-by: Jakub Gonet <jakub.gonet@swmansion.com>
@jgonet jgonet mentioned this pull request Jun 16, 2025
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.

ok, makes sense. My last comment on this topic:
I'm not strongly opinionated about this, but the suffix _TAG makes more clear that is a marker that is applied on top of something else.
such as TERM_PRIMARY_BOXED -> TERM_PRIMARY_BOXED_TAG.

Anyway as soon as it is rebased it can be merged.

Erlang/OTP's erl_term.h defines several macros to encode terms and
distinguishes `TAG_PRIMARY_*` for the two lowest bits, `_TAG_IMMED1*` for
the lowest four bits and `_TAG_IMMED2*` for the lowest six bits.

AtomVM term.h uses `TERM_BOXED_*` for the first word of boxed items.
It used magic number `0x2` or `TERM_BOXED_VALUE_TAG` instead of what
erl_term.h calls `TAG_PRIMARY_BOXED`, which is confusing as it's not related
to the first word of boxed item but to the (lowest two bits of the) term itself.

This change introduces `TERM_PRIMARY_*` macros following Erlang/OTP erl_term.h's
`TAG_PRIMARY_*` macros, and replaces all matching magic numbers with these
macros.

It also introduces `TERM_IMMED2_*` macros and uses them instead of magic
numbers.

`TERM_BOXED_VALUE_TAG` is marked as deprecated but kept.

No macro called `TERM_IMMED1_*` is introduced as AtomVM already uses
non-confusing macros `TERM_PID_TAG`, `TERM_PORT_TAG` and `TERM_INTEGER_TAG`.

Signed-off-by: Paul Guyot <pguyot@kallisys.net>
@pguyot pguyot force-pushed the w24/use-erl_term-macros branch from 9ce5c26 to ea687ad Compare July 7, 2025 06:02
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.

3 participants