Skip to content

Commit ea687ad

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 5790613 commit ea687ad

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, atom_index_t module_name, atom_index_t function_name, int arity,
@@ -6605,7 +6605,7 @@ HOT_FUNC int scheduler_entry_point(GlobalContext *glb)
66056605
}
66066606

66076607
#ifdef IMPL_EXECUTE_LOOP
6608-
term fun = ((term) boxed_func) | TERM_BOXED_VALUE_TAG;
6608+
term fun = ((term) boxed_func) | TERM_PRIMARY_BOXED;
66096609
WRITE_REGISTER(dreg, fun);
66106610
#endif
66116611
break;

src/libAtomVM/term.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ term term_alloc_refc_binary(size_t size, bool is_const, Heap *heap, GlobalContex
845845
boxed_value[0] = ((TERM_BOXED_REFC_BINARY_SIZE - 1) << 6) | TERM_BOXED_REFC_BINARY;
846846
boxed_value[1] = (term) size;
847847
boxed_value[2] = (term) is_const ? RefcBinaryIsConst : RefcNoFlags;
848-
term ret = ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
848+
term ret = ((term) boxed_value) | TERM_PRIMARY_BOXED;
849849
if (is_const) {
850850
boxed_value[3] = (term) NULL;
851851
// TODO Consider making const refc binaries 4 words instead of 6
@@ -889,7 +889,7 @@ term term_alloc_sub_binary(term binary_or_state, size_t offset, size_t len, Heap
889889
boxed[2] = (term) offset;
890890
boxed[3] = binary;
891891

892-
return ((term) boxed) | TERM_BOXED_VALUE_TAG;
892+
return ((term) boxed) | TERM_PRIMARY_BOXED;
893893
}
894894

895895
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
@@ -48,7 +48,13 @@
4848
extern "C" {
4949
#endif
5050

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

5359
#define TERM_IMMED_TAG_MASK 0xF
5460
#define TERM_PID_TAG 0x3
@@ -62,7 +68,6 @@ extern "C" {
6268
#define TERM_BOXED_REF 0x10
6369
#define TERM_BOXED_FUN 0x14
6470
#define TERM_BOXED_FLOAT 0x18
65-
#define TERM_CATCH_TAG 0x1B
6671
#define TERM_BOXED_REFC_BINARY 0x20
6772
#define TERM_BOXED_HEAP_BINARY 0x24
6873
#define TERM_BOXED_SUB_BINARY 0x28
@@ -72,6 +77,12 @@ extern "C" {
7277
#define TERM_BOXED_EXTERNAL_PORT 0x34
7378
#define TERM_BOXED_EXTERNAL_REF 0x38
7479

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

@@ -138,7 +149,7 @@ extern "C" {
138149

139150
#define TERM_DEBUG_ASSERT(...)
140151

141-
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << 6) | 0xB)
152+
#define TERM_FROM_ATOM_INDEX(atom_index) ((atom_index << TERM_IMMED2_TAG_SIZE) | TERM_IMMED2_ATOM)
142153

143154
// Local ref is at most 30 bytes:
144155
// 2^32-1 = 4294967295 (10 chars)
@@ -294,7 +305,7 @@ static inline const term *term_to_const_term_ptr(term t)
294305
static inline bool term_is_atom(term t)
295306
{
296307
/* atom: | atom index | 00 10 11 */
297-
return ((t & TERM_BOXED_TAG_MASK) == 0xB);
308+
return ((t & TERM_IMMED2_TAG_MASK) == TERM_IMMED2_ATOM);
298309
}
299310

300311
/**
@@ -319,7 +330,7 @@ static inline bool term_is_invalid_term(term t)
319330
static inline bool term_is_nil(term t)
320331
{
321332
/* nil: 11 10 11 */
322-
return ((t & TERM_BOXED_TAG_MASK) == 0x3B);
333+
return ((t & TERM_IMMED2_TAG_MASK) == TERM_NIL);
323334
}
324335

325336
/**
@@ -332,7 +343,7 @@ static inline bool term_is_nil(term t)
332343
static inline bool term_is_nonempty_list(term t)
333344
{
334345
/* list: 01 */
335-
return ((t & 0x3) == 0x1);
346+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_LIST);
336347
}
337348

338349
/**
@@ -358,7 +369,7 @@ static inline bool term_is_list(term t)
358369
static inline bool term_is_boxed(term t)
359370
{
360371
/* boxed: 10 */
361-
return ((t & 0x3) == 0x2);
372+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
362373
}
363374

364375
/**
@@ -384,7 +395,7 @@ static inline size_t term_get_size_from_boxed_header(term header)
384395
static inline size_t term_boxed_size(term t)
385396
{
386397
/* boxed: 10 */
387-
TERM_DEBUG_ASSERT((t & 0x3) == 0x2);
398+
TERM_DEBUG_ASSERT((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED);
388399

389400
const term *boxed_value = term_to_const_term_ptr(t);
390401
return term_get_size_from_boxed_header(*boxed_value);
@@ -400,7 +411,7 @@ static inline size_t term_boxed_size(term t)
400411
static inline bool term_is_binary(term t)
401412
{
402413
/* boxed: 10 */
403-
if ((t & 0x3) == 0x2) {
414+
if ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_BOXED) {
404415
const term *boxed_value = term_to_const_term_ptr(t);
405416
int masked_value = boxed_value[0] & TERM_BOXED_TAG_MASK;
406417
switch (masked_value) {
@@ -522,7 +533,7 @@ static inline bool term_is_any_integer(term t)
522533

523534
static inline bool term_is_catch_label(term t)
524535
{
525-
return (t & TERM_BOXED_TAG_MASK) == TERM_CATCH_TAG;
536+
return (t & TERM_IMMED2_TAG_MASK) == TERM_IMMED2_CATCH;
526537
}
527538

528539
/**
@@ -775,7 +786,7 @@ static inline bool term_is_external_fun(term t)
775786
*/
776787
static inline bool term_is_cp(term t)
777788
{
778-
return ((t & 0x3) == 0);
789+
return ((t & TERM_PRIMARY_MASK) == TERM_PRIMARY_CP);
779790
}
780791

781792
/**
@@ -797,7 +808,7 @@ static inline term term_invalid_term()
797808
*/
798809
static inline term term_nil()
799810
{
800-
return 0x3B;
811+
return TERM_NIL;
801812
}
802813

803814
/**
@@ -1023,7 +1034,7 @@ static inline term term_make_boxed_int(avm_int_t value, Heap *heap)
10231034
term *boxed_int = memory_heap_alloc(heap, 1 + BOXED_TERMS_REQUIRED_FOR_INT);
10241035
boxed_int[0] = (BOXED_TERMS_REQUIRED_FOR_INT << 6) | TERM_BOXED_POSITIVE_INTEGER; // OR sign bit
10251036
boxed_int[1] = value;
1026-
return ((term) boxed_int) | TERM_BOXED_VALUE_TAG;
1037+
return ((term) boxed_int) | TERM_PRIMARY_BOXED;
10271038
}
10281039

10291040
static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap)
@@ -1045,7 +1056,7 @@ static inline term term_make_boxed_int64(avm_int64_t large_int64, Heap *heap)
10451056
#else
10461057
#error "unsupported configuration."
10471058
#endif
1048-
return ((term) boxed_int) | TERM_BOXED_VALUE_TAG;
1059+
return ((term) boxed_int) | TERM_PRIMARY_BOXED;
10491060
}
10501061

10511062
static inline term term_make_maybe_boxed_int64(avm_int64_t value, Heap *heap)
@@ -1080,7 +1091,7 @@ static inline size_t term_boxed_integer_size(avm_int64_t value)
10801091

10811092
static inline term term_from_catch_label(unsigned int module_index, unsigned int label)
10821093
{
1083-
return (term) ((module_index << 24) | (label << 6) | TERM_CATCH_TAG);
1094+
return (term) ((module_index << 24) | (label << 6) | TERM_IMMED2_CATCH);
10841095
}
10851096

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

1232-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1243+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
12331244
} else {
12341245
return term_alloc_refc_binary(size, false, heap, glb);
12351246
}
@@ -1417,7 +1428,7 @@ static inline term term_from_ref_ticks(uint64_t ref_ticks, Heap *heap)
14171428
#error "terms must be either 32 or 64 bit wide"
14181429
#endif
14191430

1420-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1431+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14211432
}
14221433

14231434
static inline uint64_t term_to_ref_ticks(term rt)
@@ -1461,7 +1472,7 @@ static inline term term_make_external_process_id(term node, uint32_t process_id,
14611472
external_thing_words[2] = process_id;
14621473
external_thing_words[3] = serial;
14631474

1464-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1475+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14651476
}
14661477

14671478
/**
@@ -1485,7 +1496,7 @@ static inline term term_make_external_port_number(term node, uint64_t number, ui
14851496
external_thing_words[2] = number >> 32;
14861497
external_thing_words[3] = (uint32_t) number;
14871498

1488-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1499+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
14891500
}
14901501

14911502
/**
@@ -1601,7 +1612,7 @@ static inline term term_make_external_reference(term node, uint16_t len, uint32_
16011612
#error "terms must be either 32 or 64 bit wide"
16021613
#endif
16031614

1604-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1615+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
16051616
}
16061617

16071618
/**
@@ -1667,7 +1678,7 @@ static inline term term_alloc_tuple(uint32_t size, Heap *heap)
16671678
term *boxed_value = memory_heap_alloc(heap, 1 + size);
16681679
boxed_value[0] = (size << 6); //tuple
16691680

1670-
return ((term) boxed_value) | 0x2;
1681+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
16711682
}
16721683

16731684
/**
@@ -1741,7 +1752,7 @@ static inline term term_from_string(const uint8_t *data, uint16_t size, Heap *he
17411752
list_cells[i] = (term) &list_cells[i + 2] | 0x1;
17421753
list_cells[i + 1] = term_from_int11(data[i / 2]);
17431754
}
1744-
list_cells[size * 2 - 2] = 0x3B;
1755+
list_cells[size * 2 - 2] = TERM_NIL;
17451756

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

1880-
return ((term) boxed_value) | TERM_BOXED_VALUE_TAG;
1891+
return ((term) boxed_value) | TERM_PRIMARY_BOXED;
18811892
}
18821893

18831894
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)