Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
engine.h
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#pragma once
20
21#include <stddef.h>
22#include <stdint.h>
23
25#include "serene/utils.h"
26
27#define SRN_HASH_TYPE uint32_t
28#define SRN_SEED_TYPE uint32_t
29
30typedef struct srn_jit_t srn_jit_t;
31typedef struct srn_mm_t srn_mm_t;
32typedef struct hmap_t hmap_t;
33typedef struct srn_namespace_t srn_namespace_t;
34typedef struct srn_error_t srn_error_t;
35typedef struct srn_context_t srn_context_t;
36typedef struct srn_value_t srn_value_t;
37typedef struct srn_metadata_t srn_metadata_t;
39
40typedef uint64_t srn_object_id_t;
41/// Start allocating IDs from 100, earlier IDs are reserved
42#define ENGINE_FIRST_OBJECT_ID 100
43
46
47/// Engine is a structure to own the long living and main pieces of the
48/// compiler. There will be only oe instance of the engine at anytime.
49typedef struct srn_engine_t {
50 /// An unsigned counter to allocate object ids atomically. Any object in
51 /// Serene must have a unique id, this id is not guaranteed to be the same on
52 /// each run. We use this id as the routing key on the context level to
53 /// connect different pieces of information related to the same object
54 /// together. ONLY INTERACT WITH THIS VIA `allocate_object_id`.
56
57 /// Memory manager
59
60#ifdef SRN_WITH_SERENE
61 srn_jit_t *jit;
62#endif
63
64 /// The fiber scheduler, set by srn_sched_init. It's here so any fiber can
65 /// reach it through its context as fiber->ctx->engine->scheduler, which is
66 /// how srn_fiber_ready works from a thread that is not a worker.
68
69 /// We use the seed for hashing and the value will be generated at random
70 /// for each new context. There's concern about hashmap collioson DoS attacks
71 /// but as far as I can see other compilers are doing a similar thing with
72 /// a non-cryptographic hashing function. we use xxHash but others are
73 /// using the non-cryptographic version of SipHash with a random seed as
74 /// well.
76
77#ifdef SRN_WITH_SERENE
78 /// A hashmap that maps namespace names to their value. When operating on
79 /// this map, e.g. inserting a namespace which will lead to a new `hmap_t`
80 /// make sure to lock the spinlock.
81 hmap_t namespaces;
82 srn_spinlock_t ns_lock;
83
84 /// Engine wide intern table for keywords. Keys are the keyword name bytes;
85 /// values are the interned srn_value_t* (with tag VKeyword). Lock before
86 /// any lookup-then-insert.
87 hmap_t keywords;
88 srn_spinlock_t keywords_lock;
89#endif
90
92
93// -----------------------------------------------------------------------------
94// Engine's LifeCycle
95// -----------------------------------------------------------------------------
96[[gnu::nonnull(1)]] srn_engine_t *srn_engine_make(srn_mm_t *mm);
97[[gnu::nonnull(1)]] void srn_engine_shutdown(srn_engine_t *engine);
98
99// -----------------------------------------------------------------------------
100// IDs
101// -----------------------------------------------------------------------------
102[[gnu::nonnull(1)]] srn_object_id_t
104
105// -----------------------------------------------------------------------------
106// Hashing
107// -----------------------------------------------------------------------------
108[[gnu::nonnull(1)]]
109srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len);
110
111// -----------------------------------------------------------------------------
112// Keyword interning
113// -----------------------------------------------------------------------------
114#ifdef SRN_WITH_SERENE
115/// Return the canonical interned keyword value for `name`. If a keyword with
116/// the same name has been interned in this engine before, the same
117/// srn_value_t* is returned. Otherwise the value is built, registered in the
118/// engine wide keyword table, and returned. This function is thread safe, via
119/// the `keywords_lock`.
120[[gnu::nonnull(1, 2)]] srn_value_t *
121srn_engine_intern_keyword(srn_context_t *ctx, srn_metadata_t *metadata,
122 const char *name);
123#endif
#define SRN_SEED_TYPE
Definition context.h:42
SRN_HASH_TYPE srn_hash_t
Definition context.h:44
SRN_SEED_TYPE srn_seed_t
Definition context.h:45
#define SRN_HASH_TYPE
Definition context.h:41
srn_engine_t * srn_engine_make(srn_mm_t *mm)
Definition engine.c:92
void srn_engine_shutdown(srn_engine_t *engine)
Definition engine.c:123
srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len)
Definition engine.c:131
uint64_t srn_object_id_t
Definition engine.h:40
struct hmap_t hmap_t
Definition engine.h:32
struct srn_scheduler_t srn_scheduler_t
Definition engine.h:38
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
Definition engine.c:137
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:49
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
Definition engine.h:55
srn_scheduler_t * scheduler
The fiber scheduler, set by srn_sched_init.
Definition engine.h:67
srn_seed_t seed
We use the seed for hashing and the value will be generated at random for each new context.
Definition engine.h:75
srn_mm_t * mm
Memory manager.
Definition engine.h:58
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:112