Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
symbols.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "serene/rt/symbols.h"
20
21#include "serene/rt/context.h"
22#include "serene/rt/engine.h"
23#include "serene/rt/errors.h"
26#include "serene/rt/strings.h"
27#include "serene/utils.h"
28
31) {
32 PANIC_IF_NULL(ctx);
33 PANIC_IF_NULL(ns);
34 PANIC_IF_NULL(name);
35
36 srn_symbol_t *sym = ALLOC(ctx, srn_symbol_t);
37 sym->name = srn_string_copy(ctx, name);
38
39 sym->ns = ns;
40
41 return srn_value_make(ctx, VSymbol, metadata, (void *)sym);
42}
43
45 srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *symbol_name
46) {
47 PANIC_IF_NULL(ctx);
48 PANIC_IF_NULL(ns);
49 PANIC_IF_NULL(symbol_name);
50
51 if (srn_string_is_empty(symbol_name)) {
52 return srn_errors_make_error(ctx, metadata, EMPTY_SYMBOL_NAME, nullptr);
53 };
54
55 hmap_key_t k = {.data = &symbol_name->buffer, .len = symbol_name->size};
56
57 // The lock covers the lookup as well as the insert. Two interners racing
58 // on the same new name would otherwise both miss and insert two distinct
59 // symbols for one name, and symbol equality is pointer identity. Holding
60 // the lock for the read also pairs it with the non-atomic replacement of
61 // the sym_table struct below.
63
64 void *result = hmap_lookup(&ns->sym_table, &k, nullptr);
65 if (result != nullptr) {
67 return (srn_value_t *)result;
68 }
69
70 // The table retains the symbol for the namespace's lifetime, so the value
71 // and its name copy are allocated through the namespace's root context,
72 // not the interning caller's transient one.
73 srn_value_t *sym = srn_symbol_make(ns->root_context, metadata, ns, symbol_name);
74
75 ns->sym_table = hmap_insert(&ns->sym_table, &k, (void *)sym);
77 return sym;
78}
79
81 srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *optional_name
82) {
83 // TODO(lxsameer): Fold the optional name into the generated spelling.
84 UNUSED(optional_name);
85 PANIC_IF_NULL(ctx);
86 PANIC_IF_NULL(ns);
87
88 // A generated symbol must be fresh, a symbol nothing else already names.
89 // The spelling is readable text, so a user symbol can occupy it. When the
90 // name is taken, a new id gives a new name to try. The lock covers the
91 // lookup and the insert, the pairing srn_symbol_intern relies on too.
92 for (;;) {
94
95 // Calculate the length
96 int len = snprintf(nullptr, 0, "gensym_%" PRIu64, id);
97 PANIC_IF(len < 0, "This should never happen!");
98
99 // Including the null terminator
100 char *name = srn_allocate(ctx, len + 1, alignof(char));
101 (void)snprintf(name, len + 1, "gensym_%" PRIu64, id);
102
103 srn_value_t *sym_name = srn_string_make(ctx, metadata, name);
104 hmap_key_t k = {.data = &AS_STRING(sym_name)->buffer, .len = AS_STRING(sym_name)->size};
105
107 if (hmap_lookup(&ns->sym_table, &k, nullptr) != nullptr) {
109 continue;
110 }
111
112 // The table retains the symbol for the namespace's lifetime, so the
113 // value and its name copy are allocated through the namespace's root
114 // context, not the generating caller's transient one.
115 srn_value_t *sym = srn_symbol_make(ns->root_context, metadata, ns, AS_STRING(sym_name));
116
117 ns->sym_table = hmap_insert(&ns->sym_table, &k, (void *)sym);
119 return sym;
120 }
121}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
#define ALLOC(ctx, T)
Definition context.h:84
srn_value_t * srn_value_make(srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
Creates a new serene value.
Definition core.c:36
@ VSymbol
Definition core.h:124
#define AS_STRING(value_ref)
Definition core.h:176
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:172
uint64_t srn_object_id_t
Definition engine.h:42
Error handling for the runtime.
@ EMPTY_SYMBOL_NAME
Definition errors.h:82
hmap_t hmap_insert(const hmap_t *hmap, hmap_key_t *k, void *v)
Insert the given key k with the value v in the given hash hmap and return the new map.
Definition hashmap.c:661
void * hmap_lookup(const hmap_t *hmap, const hmap_key_t *k, void *default_value)
Lookup the given k in the given hmap and return the value if it's been found.
Definition hashmap.c:665
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
srn_value_t * srn_string_make(srn_context_t *ctx, srn_metadata_t *metadata, const char *src)
Create a string from a null terminated C string.
Definition strings.c:51
bool srn_string_is_empty(const srn_string_t *s)
Definition strings.c:49
srn_string_t * srn_string_copy(srn_context_t *ctx, const srn_string_t *src)
Allocate a copy of src through ctx and return it.
Definition strings.c:39
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
srn_context_t * root_context
Definition namespaces.h:48
hmap_t sym_table
A mapping symbol names to (srn_value_t with type symbol).
Definition namespaces.h:53
srn_spinlock_t sym_table_lock
Definition namespaces.h:54
size_t size
Size of the buffer.
Definition strings.h:62
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:66
srn_namespace_t * ns
Definition symbols.h:32
srn_string_t * name
Definition symbols.h:31
srn_value_t * srn_symbol_make(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *name)
Create a new symbol. IT DOES NOT INTERNALIZE THE SYMBOL.
Definition symbols.c:29
srn_value_t * srn_symbol_intern(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *symbol_name)
Definition symbols.c:44
srn_value_t * srn_symbol_gen(srn_context_t *ctx, srn_metadata_t *metadata, srn_namespace_t *ns, srn_string_t *optional_name)
Definition symbols.c:80
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
static void srn_spinlock_lock(srn_spinlock_t *lock)
Definition utils.h:285
#define PANIC_IF(cond, msg)
Definition utils.h:59
static void srn_spinlock_unlock(srn_spinlock_t *lock)
Definition utils.h:276
#define UNUSED(x)
Definition utils.h:45