Skip to content

Commit 9ce5c26

Browse files
committed
Use TERM_PRIMARY_* and TERM_IMMED* macros, following erl_term.h
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>
1 parent 1616e62 commit 9ce5c26

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

src/libAtomVM/memory.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static enum MemoryGCResult memory_shrink(Context *ctx, size_t new_size, size_t n
393393
term *boxed_ptr = term_to_term_ptr(head);
394394
term *new_boxed_ptr = memory_rewrite_pointer(boxed_ptr, old_heap_root, old_end, delta);
395395
if (boxed_ptr != new_boxed_ptr) {
396-
new_list_ptr[1] = ((term) new_boxed_ptr) | TERM_BOXED_VALUE_TAG;
396+
new_list_ptr[1] = ((term) new_boxed_ptr) | TERM_PRIMARY_BOXED;
397397
}
398398
// Loop with tail.
399399
mso_ptr = new_list_ptr;
@@ -660,7 +660,7 @@ static void memory_scan_and_copy(HeapFragment *old_fragment, term *mem_start, co
660660

661661
case TERM_BOXED_REFC_BINARY: {
662662
TRACE("- Found refc binary.\n");
663-
term ref = ((term) ptr) | TERM_BOXED_VALUE_TAG;
663+
term ref = ((term) ptr) | TERM_PRIMARY_BOXED;
664664
if (!term_refc_binary_is_const(ref)) {
665665
*mso_list = term_list_init_prepend(ptr + REFC_BINARY_CONS_OFFSET, ref, *mso_list);
666666
refc_binary_increment_refcount((struct RefcBinary *) term_refc_binary_ptr(ref));
@@ -748,7 +748,7 @@ static void memory_scan_and_rewrite(size_t count, term *terms, const term *old_s
748748
term *boxed_ptr = term_to_term_ptr(binary_or_state);
749749
term *new_boxed_ptr = memory_rewrite_pointer(boxed_ptr, old_start, old_end, delta);
750750
if (boxed_ptr != new_boxed_ptr) {
751-
*ptr = ((term) new_boxed_ptr) | TERM_BOXED_VALUE_TAG;
751+
*ptr = ((term) new_boxed_ptr) | TERM_PRIMARY_BOXED;
752752
}
753753
ptr += term_get_size_from_boxed_header(t);
754754
break;
@@ -815,7 +815,7 @@ static void memory_scan_and_rewrite(size_t count, term *terms, const term *old_s
815815
term *boxed_ptr = term_to_term_ptr(t);
816816
term *new_boxed_ptr = memory_rewrite_pointer(boxed_ptr, old_start, old_end, delta);
817817
if (boxed_ptr != new_boxed_ptr) {
818-
ptr[-1] = ((term) new_boxed_ptr) | TERM_BOXED_VALUE_TAG;
818+
ptr[-1] = ((term) new_boxed_ptr) | TERM_PRIMARY_BOXED;
819819
}
820820
}
821821
}
@@ -876,7 +876,7 @@ HOT_FUNC static term memory_shallow_copy_term(HeapFragment *old_fragment, term t
876876
// However it is also required to avoid boxed terms duplication.
877877
// So instead all empty tuples will reference the same boxed term.
878878
if (boxed_size == 1) {
879-
return ((term) &empty_tuple) | TERM_BOXED_VALUE_TAG;
879+
return ((term) &empty_tuple) | TERM_PRIMARY_BOXED;
880880
}
881881

882882
term *dest = *new_heap;
@@ -885,7 +885,7 @@ HOT_FUNC static term memory_shallow_copy_term(HeapFragment *old_fragment, term t
885885
}
886886
*new_heap += boxed_size;
887887

888-
term new_term = ((term) dest) | TERM_BOXED_VALUE_TAG;
888+
term new_term = ((term) dest) | TERM_PRIMARY_BOXED;
889889

890890
if (move) {
891891
memory_replace_with_moved_marker(boxed_value, new_term);

src/libAtomVM/opcodesswitch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,7 +1692,7 @@ term make_fun(Context *ctx, const Module *mod, int fun_index, term argv[])
16921692
boxed_func[i] = *ext_reg;
16931693
}
16941694

1695-
return ((term) boxed_func) | TERM_BOXED_VALUE_TAG;
1695+
return ((term) boxed_func) | TERM_PRIMARY_BOXED;
16961696
}
16971697

16981698
static bool maybe_call_native(Context *ctx, AtomString module_name, AtomString function_name, int arity,
@@ -6626,7 +6626,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
66266626
}
66276627

66286628
#ifdef IMPL_EXECUTE_LOOP
6629-
term fun = ((term) boxed_func) | TERM_BOXED_VALUE_TAG;
6629+
term fun = ((term) boxed_func) | TERM_PRIMARY_BOXED;
66306630
WRITE_REGISTER(dreg, fun);
66316631
#endif
66326632
break;

src/libAtomVM/term.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ term term_alloc_refc_binary(size_t size, bool is_const, Heap *heap, GlobalContex
869869
boxed_value[0] = ((TERM_BOXED_REFC_BINARY_SIZE - 1) << 6) | TERM_BOXED_REFC_BINARY;
870870
boxed_value[1] = (term) size;
871871
boxed_value[2] = (term) is_const ? RefcBinaryIsConst : RefcNoFlags;
872-
term ret = ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
872+
term ret = ((term) boxed_value) | TERM_PRIMARY_BOXED;
873873
if (is_const) {
874874
boxed_value[3] = (term) NULL;
875875
// TODO Consider making const refc binaries 4 words instead of 6
@@ -913,7 +913,7 @@ term term_alloc_sub_binary(term binary_or_state, size_t offset, size_t len, Heap
913913
boxed[2] = (term) offset;
914914
boxed[3] = binary;
915915

916-
return ((term) boxed) | TERM_BOXED_VALUE_TAG;
916+
return ((term) boxed) | TERM_PRIMARY_BOXED;
917917
}
918918

919919
term term_get_map_assoc(term map, term key, GlobalContext *glb)

src/libAtomVM/term.h

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747
extern "C" {
4848
#endif
4949

50-
#define TERM_BOXED_VALUE_TAG 0x2
50+
#define TERM_PRIMARY_MASK 0x3
51+
#define TERM_PRIMARY_CP 0x0
52+
#define TERM_PRIMARY_LIST 0x1
53+
#define TERM_PRIMARY_BOXED 0x2
54+
// #define TERM_PRIMARY_IMMED 0x3
55+
56+
#define TERM_BOXED_VALUE_TAG _Pragma ("TERM_BOXED_VALUE_TAG is deprecated, use TERM_PRIMARY_BOXED instead") TERM_PRIMARY_BOXED
5157

5258
#define TERM_IMMED_TAG_MASK 0xF
5359
#define TERM_PID_TAG 0x3
@@ -61,7 +67,6 @@ extern "C" {
6167
#define TERM_BOXED_REF 0x10
6268
#define TERM_BOXED_FUN 0x14
6369
#define TERM_BOXED_FLOAT 0x18
64-
#define TERM_CATCH_TAG 0x1B
6570
#define TERM_BOXED_REFC_BINARY 0x20
6671
#define TERM_BOXED_HEAP_BINARY 0x24
6772
#define TERM_BOXED_SUB_BINARY 0x28
@@ -71,6 +76,12 @@ extern "C" {
7176
#define TERM_BOXED_EXTERNAL_PORT 0x34
7277
#define TERM_BOXED_EXTERNAL_REF 0x38
7378

79+
#define TERM_IMMED2_TAG_MASK 0x3F
80+
#define TERM_IMMED2_TAG_SIZE 6
81+
#define TERM_IMMED2_ATOM 0xB
82+
#define TERM_IMMED2_CATCH 0x1B
83+
#define TERM_NIL 0x3B
84+
7485
#define TERM_UNUSED 0x2B
7586
#define TERM_RESERVED_MARKER(x) ((x << 6) | TERM_UNUSED)
7687

@@ -137,7 +148,7 @@ extern "C" {
137148

138149
#define TERM_DEBUG_ASSERT(...)
139150

140-
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << 6) | 0xB)
151+
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << TERM_IMMED2_TAG_SIZE) | TERM_IMMED2_ATOM)
141152

142153
// Local ref is at most 30 bytes:
143154
// 2^32-1 = 4294967295 (10 chars)
@@ -293,7 +304,7 @@ static inline const term *term_to_const_term_ptr(term t)
293304
static inline bool term_is_atom(term t)
294305
{
295306
/* atom: | atom index | 00 10 11 */
296-
return ((t & TERM_BOXED_TAG_MASK) == 0xB);
307+
return ((t & TERM_IMMED2_TAG_MASK) == TERM_IMMED2_ATOM);
297308
}
298309

299310
/**
@@ -318,7 +329,7 @@ static inline bool term_is_invalid_term(term t)
318329
static inline bool term_is_nil(term t)
319330
{
320331
/* nil: 11 10 11 */
321-
return ((t & TERM_BOXED_TAG_MASK) == 0x3B);
332+
return ((t & TERM_IMMED2_TAG_MASK) == TERM_NIL);
322333
}
323334

324335
/**
@@ -331,7 +342,7 @@ static inline bool term_is_nil(term t)
331342
static inline bool term_is_nonempty_list(term t)
332343
{
333344
/* list: 01 */
334-
return ((t & 0x3) == 0x1);
345+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_LIST);
335346
}
336347

337348
/**
@@ -357,7 +368,7 @@ static inline bool term_is_list(term t)
357368
static inline bool term_is_boxed(term t)
358369
{
359370
/* boxed: 10 */
360-
return ((t & 0x3) == 0x2);
371+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
361372
}
362373

363374
/**
@@ -383,7 +394,7 @@ static inline size_t term_get_size_from_boxed_header(term header)
383394
static inline size_t term_boxed_size(term t)
384395
{
385396
/* boxed: 10 */
386-
TERM_DEBUG_ASSERT((t & 0x3) == 0x2);
397+
TERM_DEBUG_ASSERT((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
387398

388399
const term *boxed_value = term_to_const_term_ptr(t);
389400
return term_get_size_from_boxed_header(*boxed_value);
@@ -399,7 +410,7 @@ static inline size_t term_boxed_size(term t)
399410
static inline bool term_is_binary(term t)
400411
{
401412
/* boxed: 10 */
402-
if ((t & 0x3) == 0x2) {
413+
if ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED) {
403414
const term *boxed_value = term_to_const_term_ptr(t);
404415
int masked_value = boxed_value[0] & TERM_BOXED_TAG_MASK;
405416
switch (masked_value) {
@@ -521,7 +532,7 @@ static inline bool term_is_any_integer(term t)
521532

522533
static inline bool term_is_catch_label(term t)
523534
{
524-
return (t & TERM_BOXED_TAG_MASK) == TERM_CATCH_TAG;
535+
return (t & TERM_IMMED2_TAG_MASK) == TERM_IMMED2_CATCH;
525536
}
526537

527538
/**
@@ -774,7 +785,7 @@ static inline bool term_is_external_fun(term t)
774785
*/
775786
static inline bool term_is_cp(term t)
776787
{
777-
return ((t & 0x3) == 0);
788+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_CP);
778789
}
779790

780791
/**
@@ -796,7 +807,7 @@ static inline term term_invalid_term()
796807
*/
797808
static inline term term_nil()
798809
{
799-
return 0x3B;
810+
return TERM_NIL;
800811
}
801812

802813
/**
@@ -1022,7 +1033,7 @@ static inline term term_make_boxed_int(avm_int_t value, Heap *heap)
10221033
term *boxed_int = memory_heap_alloc(heap, 1 + BOXED_TERMS_REQUIRED_FOR_INT);
10231034
boxed_int[0] = (BOXED_TERMS_REQUIRED_FOR_INT << 6) | TERM_BOXED_POSITIVE_INTEGER; // OR sign bit
10241035
boxed_int[1] = value;
1025-
return ((term) boxed_int) | TERM_BOXED_VALUE_TAG;
1036+
return ((term) boxed_int) | TERM_PRIMARY_BOXED;
10261037
}
10271038

10281039
static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap)
@@ -1044,7 +1055,7 @@ static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap)
10441055
#else
10451056
#error "unsupported configuration."
10461057
#endif
1047-
return ((term) boxed_int) | TERM_BOXED_VALUE_TAG;
1058+
return ((term) boxed_int) | TERM_PRIMARY_BOXED;
10481059
}
10491060

10501061
static inline term term_make_maybe_boxed_int64(avm_int64_t value, Heap *heap)
@@ -1079,7 +1090,7 @@ static inline size_t term_boxed_integer_size(avm_int64_t value)
10791090

10801091
static inline term term_from_catch_label(unsigned int module_index, unsigned int label)
10811092
{
1082-
return (term) ((module_index << 24) | (label << 6) | TERM_CATCH_TAG);
1093+
return (term) ((module_index << 24) | (label << 6) | TERM_IMMED2_CATCH);
10831094
}
10841095

10851096
/**
@@ -1228,7 +1239,7 @@ static inline term term_create_uninitialized_binary(size_t size, Heap *heap, Glo
12281239
boxed_value[0] = (size_in_terms << 6) | TERM_BOXED_HEAP_BINARY;
12291240
boxed_value[1] = size;
12301241

1231-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1242+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
12321243
} else {
12331244
return term_alloc_refc_binary(size, false, heap, glb);
12341245
}
@@ -1416,7 +1427,7 @@ static inline term term_from_ref_ticks(uint64_t ref_ticks, Heap *heap)
14161427
#error "terms must be either 32 or 64 bit wide"
14171428
#endif
14181429

1419-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1430+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14201431
}
14211432

14221433
static inline uint64_t term_to_ref_ticks(term rt)
@@ -1460,7 +1471,7 @@ static inline term term_make_external_process_id(term node, uint32_t process_id,
14601471
external_thing_words[2] = process_id;
14611472
external_thing_words[3] = serial;
14621473

1463-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1474+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14641475
}
14651476

14661477
/**
@@ -1484,7 +1495,7 @@ static inline term term_make_external_port_number(term node, uint64_t number, ui
14841495
external_thing_words[2] = number >> 32;
14851496
external_thing_words[3] = (uint32_t) number;
14861497

1487-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1498+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14881499
}
14891500

14901501
/**
@@ -1600,7 +1611,7 @@ static inline term term_make_external_reference(term node, uint16_t len, uint32_
16001611
#error "terms must be either 32 or 64 bit wide"
16011612
#endif
16021613

1603-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1614+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
16041615
}
16051616

16061617
/**
@@ -1666,7 +1677,7 @@ static inline term term_alloc_tuple(uint32_t size, Heap *heap)
16661677
term *boxed_value = memory_heap_alloc(heap, 1 + size);
16671678
boxed_value[0] = (size << 6); //tuple
16681679

1669-
return ((term) boxed_value) | 0x2;
1680+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
16701681
}
16711682

16721683
/**
@@ -1740,7 +1751,7 @@ static inline term term_from_string(const uint8_t *data, uint16_t size, Heap *he
17401751
list_cells[i] = (term) &list_cells[i + 2] | 0x1;
17411752
list_cells[i + 1] = term_from_int11(data[i / 2]);
17421753
}
1743-
list_cells[size * 2 - 2] = 0x3B;
1754+
list_cells[size * 2 - 2] = TERM_NIL;
17441755

17451756
return ((term) list_cells) | 0x1;
17461757
}
@@ -1876,7 +1887,7 @@ static inline term term_from_float(avm_float_t f, Heap *heap)
18761887
float_term_t *boxed_float = (float_term_t *) (boxed_value + 1);
18771888
boxed_float->f = f;
18781889

1879-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1890+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
18801891
}
18811892

18821893
static inline avm_float_t term_to_float(term t)
@@ -1985,7 +1996,7 @@ static inline term term_make_function_reference(term m, term f, term a, Heap *he
19851996
boxed_func[2] = f;
19861997
boxed_func[3] = a;
19871998

1988-
return ((term) boxed_func) | TERM_BOXED_VALUE_TAG;
1999+
return ((term) boxed_func) | TERM_PRIMARY_BOXED;
19892000
}
19902001

19912002
static inline bool term_is_match_state(term t)
@@ -2066,7 +2077,7 @@ static inline term term_alloc_bin_match_state(term binary_or_state, int slots, H
20662077
}
20672078
}
20682079

2069-
return ((term) boxed_match_state) | TERM_BOXED_VALUE_TAG;
2080+
return ((term) boxed_match_state) | TERM_PRIMARY_BOXED;
20702081
}
20712082

20722083
/**
@@ -2080,7 +2091,7 @@ static inline term term_alloc_bin_match_state(term binary_or_state, int slots, H
20802091
static inline void term_truncate_boxed(term boxed, size_t new_size, Heap *heap)
20812092
{
20822093
/* boxed: 10 */
2083-
TERM_DEBUG_ASSERT((t & 0x3) == 0x2);
2094+
TERM_DEBUG_ASSERT((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
20842095

20852096
term *boxed_value = term_to_term_ptr(boxed);
20862097
int size_diff = (boxed_value[0] >> 6) - new_size;
@@ -2127,7 +2138,7 @@ static inline term term_alloc_map_maybe_shared(avm_uint_t size, term keys, Heap
21272138
boxed_value[0] = ((1 + size) << 6) | TERM_BOXED_MAP;
21282139
boxed_value[term_get_map_keys_offset()] = keys;
21292140

2130-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
2141+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
21312142
}
21322143

21332144
static inline term term_alloc_map(avm_uint_t size, Heap *heap)
@@ -2229,7 +2240,7 @@ static inline term term_from_resource(void *resource, Heap *heap)
22292240
boxed_value[0] = ((TERM_BOXED_REFC_BINARY_SIZE - 1) << 6) | TERM_BOXED_REFC_BINARY;
22302241
boxed_value[1] = (term) 0; // binary size, this is pre ERTS 9.0 (OTP-20.0) behavior
22312242
boxed_value[2] = (term) RefcNoFlags;
2232-
term ret = ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
2243+
term ret = ((term) boxed_value) | TERM_PRIMARY_BOXED;
22332244
boxed_value[3] = (term) refc;
22342245
// Add the resource to the mso list
22352246
refc->ref_count++;

0 commit comments

Comments
 (0)