Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
core.h File Reference
#include <stddef.h>
#include <stdint.h>
#include "llvm-c/Core.h"
#include "llvm-c/Types.h"
#include "serene/rt/impl/seq.h"
Include dependency graph for core.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  srn_src_location_t
 
struct  srn_metadata_t
 
struct  srn_syntax_t
 
struct  srn_value_t
 

Macros

#define SIZE_OF_VALUE_PALYLOAD   8
 
#define TYPE(value_ref)
 
#define METADATA(value_ref)
 
#define IS_A(value_ref, field)
 
#define IS_TRUE(value_ref)
 
#define IS_FALSE(value_ref)
 
#define IS_NIL(value_ref)
 
#define AS_I64(value_ref)
 
#define AS_F64(value_ref)
 
#define AS_CLOSURE(value_ref)
 
#define AS_NS(value_ref)
 
#define AS_STRING(value_ref)
 
#define AS_SYMBOL(value_ref)
 
#define AS_ERROR(value_ref)
 
#define AS_LIST(value_ref)
 
#define AS_KEYWORD(value_ref)
 
#define AS_SEQ(value_ref)
 
#define AS_MAP(value_ref)
 

Typedefs

typedef struct srn_expr_t srn_expr_t
 
typedef struct srn_value_t srn_value_t
 
typedef struct srn_error_t srn_error_t
 
typedef struct srn_namespace_t srn_namespace_t
 
typedef struct srn_string_t srn_string_t
 
typedef struct srn_symbol_t srn_symbol_t
 
typedef struct srn_list_t srn_list_t
 
typedef struct srn_keyword_t srn_keyword_t
 
typedef struct srn_seq_t srn_seq_t
 
typedef struct srn_map_t srn_map_t
 
typedef srn_value_t *(* srn_fnptr_t) (srn_context_t *ctx, srn_value_t *argv, uint32_t argc)
 
typedef struct srn_src_location_t srn_src_location_t
 
typedef struct srn_metadata_t srn_metadata_t
 
typedef enum srn_syntax_tag_e srn_syntax_tag_t
 
typedef struct srn_syntax_t srn_syntax_t
 
typedef enum srn_value_tag_e srn_value_tag_t
 

Enumerations

enum  srn_syntax_tag_e : size_t {
  SNil = 0 , STrue , SFalse , SSymbol ,
  SNumber , SList , SSeq , SQuote ,
  SString , SKeyword , SMap , SError
}
 
enum  srn_value_tag_e : size_t {
  VNil = 0 , VTrue , VFalse , VI64 ,
  VF64 , VList , VSeq , VQuote ,
  VClosure , VNamespace , VString , VSymbol ,
  VKeyword , VMap , VError
}
 

Functions

srn_value_tsrn_value_make (srn_context_t *ctx, srn_value_tag_t tag, srn_metadata_t *metadata, void *payload)
 Creates a new serene value.
 
static LLVMTypeRef srn_llvm_value_t (LLVMContextRef llctx)
 LLVM type for srn_value_t in LLVM IR. { i64, ptr, i64 }.
 

Variables

static srn_metadata_t absurd_metadata = {nullptr, nullptr}
 We use this for testing.
 
static srn_value_t nil_v = {.type = VNil}
 
static srn_value_t true_v = {.type = VTrue}
 
static srn_value_t false_v = {.type = VFalse}
 

Macro Definition Documentation

◆ AS_CLOSURE

#define AS_CLOSURE ( value_ref)
Value:
((value_ref)->as.closure)

Definition at line 169 of file core.h.

◆ AS_ERROR

#define AS_ERROR ( value_ref)
Value:
((value_ref)->as.error)

Definition at line 173 of file core.h.

◆ AS_F64

#define AS_F64 ( value_ref)
Value:
((value_ref)->as.f64)

Definition at line 168 of file core.h.

◆ AS_I64

#define AS_I64 ( value_ref)
Value:
((value_ref)->as.i64)

Definition at line 167 of file core.h.

◆ AS_KEYWORD

#define AS_KEYWORD ( value_ref)
Value:
((value_ref)->as.keyword)

Definition at line 175 of file core.h.

◆ AS_LIST

#define AS_LIST ( value_ref)
Value:
((value_ref)->as.list)

Definition at line 174 of file core.h.

◆ AS_MAP

#define AS_MAP ( value_ref)
Value:
((value_ref)->as.map)

Definition at line 177 of file core.h.

◆ AS_NS

#define AS_NS ( value_ref)
Value:
((value_ref)->as.ns)

Definition at line 170 of file core.h.

◆ AS_SEQ

#define AS_SEQ ( value_ref)
Value:
((value_ref)->as.seq)

Definition at line 176 of file core.h.

◆ AS_STRING

#define AS_STRING ( value_ref)
Value:
((value_ref)->as.string)

Definition at line 171 of file core.h.

◆ AS_SYMBOL

#define AS_SYMBOL ( value_ref)
Value:
((value_ref)->as.symbol)

Definition at line 172 of file core.h.

◆ IS_A

#define IS_A ( value_ref,
field )
Value:
(TYPE(value_ref) == (field))
#define TYPE(value_ref)
Definition core.h:160

Definition at line 162 of file core.h.

◆ IS_FALSE

#define IS_FALSE ( value_ref)
Value:
(TYPE(value_ref) == VFalse)
@ VFalse
Definition core.h:115

Definition at line 164 of file core.h.

◆ IS_NIL

#define IS_NIL ( value_ref)
Value:
(TYPE(value_ref) == VNil)
@ VNil
Definition core.h:113

Definition at line 165 of file core.h.

◆ IS_TRUE

#define IS_TRUE ( value_ref)
Value:
(TYPE(value_ref) == VTrue)
@ VTrue
Definition core.h:114

Definition at line 163 of file core.h.

◆ METADATA

#define METADATA ( value_ref)
Value:
((value_ref)->metadata)

Definition at line 161 of file core.h.

◆ SIZE_OF_VALUE_PALYLOAD

#define SIZE_OF_VALUE_PALYLOAD   8

Definition at line 152 of file core.h.

◆ TYPE

#define TYPE ( value_ref)
Value:
((value_ref)->type)

Definition at line 160 of file core.h.

Typedef Documentation

◆ srn_error_t

typedef struct srn_error_t srn_error_t

Definition at line 32 of file core.h.

◆ srn_expr_t

typedef struct srn_expr_t srn_expr_t

Definition at line 29 of file core.h.

◆ srn_fnptr_t

typedef srn_value_t *(* srn_fnptr_t) (srn_context_t *ctx, srn_value_t *argv, uint32_t argc)

Definition at line 46 of file core.h.

◆ srn_keyword_t

typedef struct srn_keyword_t srn_keyword_t

Definition at line 37 of file core.h.

◆ srn_list_t

typedef struct srn_list_t srn_list_t

Definition at line 36 of file core.h.

◆ srn_map_t

typedef struct srn_map_t srn_map_t

Definition at line 39 of file core.h.

◆ srn_metadata_t

typedef struct srn_metadata_t srn_metadata_t

◆ srn_namespace_t

typedef struct srn_namespace_t srn_namespace_t

Definition at line 33 of file core.h.

◆ srn_seq_t

typedef struct srn_seq_t srn_seq_t

Definition at line 38 of file core.h.

◆ srn_src_location_t

typedef struct srn_src_location_t srn_src_location_t

◆ srn_string_t

typedef struct srn_string_t srn_string_t

Definition at line 34 of file core.h.

◆ srn_symbol_t

typedef struct srn_symbol_t srn_symbol_t

Definition at line 35 of file core.h.

◆ srn_syntax_t

typedef struct srn_syntax_t srn_syntax_t

◆ srn_syntax_tag_t

◆ srn_value_t

typedef struct srn_value_t srn_value_t

Definition at line 30 of file core.h.

◆ srn_value_tag_t

Enumeration Type Documentation

◆ srn_syntax_tag_e

enum srn_syntax_tag_e : size_t
Enumerator
SNil 
STrue 
SFalse 
SSymbol 
SNumber 
SList 
SSeq 
SQuote 
SString 
SKeyword 
SMap 
SError 

SError should be last.

Definition at line 73 of file core.h.

73 : size_t {
74 SNil = 0,
75 STrue,
76 SFalse,
77 SSymbol,
78 SNumber,
79 SList,
80 SSeq,
81 SQuote,
82 SString,
84 SMap,
85 /// SError should be last
86 SError,
enum srn_syntax_tag_e srn_syntax_tag_t
@ SQuote
Definition core.h:81
@ SKeyword
Definition core.h:83
@ SFalse
Definition core.h:76
@ SSymbol
Definition core.h:77
@ SMap
Definition core.h:84
@ SNumber
Definition core.h:78
@ SList
Definition core.h:79
@ SNil
Definition core.h:74
@ SError
SError should be last.
Definition core.h:86
@ SString
Definition core.h:82
@ SSeq
Definition core.h:80
@ STrue
Definition core.h:75

◆ srn_value_tag_e

enum srn_value_tag_e : size_t
Enumerator
VNil 
VTrue 
VFalse 
VI64 
VF64 
VList 
VSeq 
VQuote 
VClosure 
VNamespace 
VString 
VSymbol 
VKeyword 
VMap 
VError 

VError should be last.

Definition at line 112 of file core.h.

112 : size_t {
113 VNil = 0,
114 VTrue,
115 VFalse,
116 VI64,
117 VF64,
118 VList,
119 VSeq,
120 VQuote,
121 VClosure,
123 VString,
124 VSymbol,
125 VKeyword,
126 VMap,
127 /// VError should be last
128 VError,
@ VQuote
Definition core.h:120
@ VSeq
Definition core.h:119
@ VNamespace
Definition core.h:122
@ VClosure
Definition core.h:121
@ VList
Definition core.h:118
@ VF64
Definition core.h:117
@ VSymbol
Definition core.h:124
@ VI64
Definition core.h:116
@ VMap
Definition core.h:126
@ VError
VError should be last.
Definition core.h:128
@ VKeyword
Definition core.h:125
@ VString
Definition core.h:123
enum srn_value_tag_e srn_value_tag_t

Function Documentation

◆ srn_llvm_value_t()

static LLVMTypeRef srn_llvm_value_t ( LLVMContextRef llctx)
inlinestatic

LLVM type for srn_value_t in LLVM IR. { i64, ptr, i64 }.

Definition at line 186 of file core.h.

186 {
187 LLVMTypeRef i64t = LLVMInt64TypeInContext(llctx);
188
189 // opaque ptr
190 LLVMTypeRef i8t = LLVMInt8TypeInContext(llctx);
191 LLVMTypeRef ptrt = LLVMPointerType(i8t, 0);
192
193 LLVMTypeRef fields[3];
194 fields[0] = i64t; // type tag (srn_value_tag_t as i64)
195 fields[1] = ptrt; // loc pointer
196 fields[2] = i64t; // payload (the union)
197
198 LLVMTypeRef st = LLVMStructCreateNamed(llctx, "srn_value_t");
199 LLVMStructSetBody(st, fields, 3, 0);
200 return st;
201}

◆ srn_value_make()

srn_value_t * srn_value_make ( srn_context_t * ctx,
srn_value_tag_t tag,
srn_metadata_t * metadata,
void * payload )
nodiscard

Creates a new serene value.

We always heap allocate the values. So, ANY value creation should go through this function

Definition at line 24 of file core.c.

25 {
26 PANIC_IF_NULL(ctx);
27
28 switch (tag) {
29 case VNil:
30 return &nil_v;
31 case VTrue:
32 return &true_v;
33 case VFalse:
34 return &false_v;
35 default:
36 break;
37 }
38
39 srn_value_t *v = ALLOC(ctx, srn_value_t);
40 v->type = tag;
41 v->metadata = metadata;
42
43 // At this point payload should not be null
44 PANIC_IF_NULL(payload);
45
46 switch (tag) {
47 case VI64:
48 v->as.i64 = *(int64_t *)payload;
49 break;
50 case VF64:
51 v->as.f64 = *(double *)payload;
52 break;
53 case VList:
54 v->as.list = (srn_list_t *)payload;
55 break;
56 case VSeq:
57 v->as.seq = (srn_seq_t *)payload;
58 break;
59 case VQuote:
60 PANIC("Not implemented");
61 case VClosure:
62 PANIC("Not implemented");
63 case VNamespace:
64 v->as.ns = (srn_namespace_t *)payload;
65 break;
66 case VString:
67 v->as.string = (srn_string_t *)payload;
68 break;
69 case VSymbol:
70 v->as.symbol = (srn_symbol_t *)payload;
71 break;
72 case VKeyword:
73 v->as.keyword = (srn_keyword_t *)payload;
74 break;
75 case VMap:
76 v->as.map = (srn_map_t *)payload;
77 break;
78 case VError:
79 v->as.error = (srn_error_t *)payload;
80 break;
81 default:
82 PANIC("Should never happen");
83 }
84 return v;
85}
#define ALLOC(ctx, T)
Definition context.h:82
static srn_value_t false_v
Definition core.h:158
static srn_value_t true_v
Definition core.h:157
static srn_value_t nil_v
Definition core.h:156
A keyword: just a name.
Definition keywords.h:29
Since all the values are immutable and persistent.
Definition lists.h:36
A persistent, immutable map.
Definition maps.h:29
A persistent, immutable, indexed sequence.
Definition seqs.h:27
srn_metadata_t * metadata
Definition core.h:133
srn_namespace_t * ns
Definition core.h:141
srn_list_t * list
Definition core.h:145
int64_t i64
We represent all the immidiate numbers with this field and cast as we to an appropriate type.
Definition core.h:138
double f64
Definition core.h:139
srn_seq_t * seq
Definition core.h:147
union srn_value_t::@033047061046230251001111174367071167226300135003 as
IMPORTANT NOTE: The size of this union should never be larger than a word.
srn_error_t * error
Definition core.h:144
srn_map_t * map
Definition core.h:148
srn_symbol_t * symbol
Definition core.h:143
srn_value_tag_t type
Definition core.h:132
srn_keyword_t * keyword
Definition core.h:146
srn_string_t * string
Definition core.h:142
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
#define PANIC(msg)
Definition utils.h:51
Here is the caller graph for this function:

Variable Documentation

◆ absurd_metadata

srn_metadata_t absurd_metadata = {nullptr, nullptr}
static

We use this for testing.

Definition at line 66 of file core.h.

66{nullptr, nullptr};

◆ false_v

srn_value_t false_v = {.type = VFalse}
static

Definition at line 158 of file core.h.

158{.type = VFalse};

◆ nil_v

srn_value_t nil_v = {.type = VNil}
static

Definition at line 156 of file core.h.

156{.type = VNil};

◆ true_v

srn_value_t true_v = {.type = VTrue}
static

Definition at line 157 of file core.h.

157{.type = VTrue};