Serene Runtime 1.0.0-dev
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_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

srn_metadata_t absurd_metadata
 We use this for testing. Defined once in core.c.
srn_value_t nil_v
srn_value_t true_v
srn_value_t false_v

Macro Definition Documentation

◆ AS_CLOSURE

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

Definition at line 174 of file core.h.

◆ AS_ERROR

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

Definition at line 178 of file core.h.

◆ AS_F64

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

Definition at line 173 of file core.h.

◆ AS_I64

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

Definition at line 172 of file core.h.

◆ AS_KEYWORD

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

Definition at line 180 of file core.h.

◆ AS_LIST

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

Definition at line 179 of file core.h.

◆ AS_MAP

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

Definition at line 182 of file core.h.

◆ AS_NS

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

Definition at line 175 of file core.h.

◆ AS_SEQ

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

Definition at line 181 of file core.h.

◆ AS_STRING

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

Definition at line 176 of file core.h.

◆ AS_SYMBOL

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

Definition at line 177 of file core.h.

◆ IS_A

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

Definition at line 167 of file core.h.

◆ IS_FALSE

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

Definition at line 169 of file core.h.

◆ IS_NIL

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

Definition at line 170 of file core.h.

◆ IS_TRUE

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

Definition at line 168 of file core.h.

◆ METADATA

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

Definition at line 166 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 165 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 47 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()

LLVMTypeRef srn_llvm_value_t ( LLVMContextRef llctx)
inlinestatic

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

Definition at line 191 of file core.h.

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

◆ 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 36 of file core.c.

36 {
37 PANIC_IF_NULL(ctx);
38
39 switch (tag) {
40 case VNil:
41 return &nil_v;
42 case VTrue:
43 return &true_v;
44 case VFalse:
45 return &false_v;
46 default:
47 break;
48 }
49
50 srn_value_t *v = ALLOC(ctx, srn_value_t);
51 v->type = tag;
52 v->metadata = metadata;
53
54 // At this point payload should not be null
55 PANIC_IF_NULL(payload);
56
57 switch (tag) {
58 case VI64:
59 v->as.i64 = *(int64_t *)payload;
60 break;
61 case VF64:
62 v->as.f64 = *(double *)payload;
63 break;
64 case VList:
65 v->as.list = (srn_list_t *)payload;
66 break;
67 case VSeq:
68 v->as.seq = (srn_seq_t *)payload;
69 break;
70 case VQuote:
71 PANIC("Not implemented");
72 case VClosure:
73 PANIC("Not implemented");
74 case VNamespace:
75 v->as.ns = (srn_namespace_t *)payload;
76 break;
77 case VString:
78 v->as.string = (srn_string_t *)payload;
79 break;
80 case VSymbol:
81 v->as.symbol = (srn_symbol_t *)payload;
82 break;
83 case VKeyword:
84 v->as.keyword = (srn_keyword_t *)payload;
85 break;
86 case VMap:
87 v->as.map = (srn_map_t *)payload;
88 break;
89 case VError:
90 v->as.error = (srn_error_t *)payload;
91 break;
92 default:
93 PANIC("Should never happen");
94 }
95 return v;
96}
#define ALLOC(ctx, T)
Definition context.h:84
srn_value_t false_v
Definition core.c:29
srn_value_t true_v
Definition core.c:28
srn_value_t nil_v
Definition core.c:27
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
A keyword, just a name.
Definition keywords.h:29
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:66
#define PANIC(msg)
Definition utils.h:53
Here is the caller graph for this function:

Variable Documentation

◆ absurd_metadata

srn_metadata_t absurd_metadata
extern

We use this for testing. Defined once in core.c.

Definition at line 26 of file core.c.

26{nullptr, nullptr};

◆ false_v

srn_value_t false_v
extern

Definition at line 29 of file core.c.

29{.type = VFalse};

◆ nil_v

srn_value_t nil_v
extern

Definition at line 27 of file core.c.

27{.type = VNil};

◆ true_v

srn_value_t true_v
extern

Definition at line 28 of file core.c.

28{.type = VTrue};