diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-01-07 19:42:12 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-01-18 10:57:42 +0100 |
commit | 6b2e79af9fe6e7d05d31245fc9049540a96d5d31 (patch) | |
tree | 20aeea16dc72454610be5bf58e83b5070e1c6da8 /include | |
parent | 0e5b273c3d25d1dce3b469f3a6865f430554e730 (diff) |
types: add initial infrastructure for function serialization
- Introduce a new "program" entity which holds the list of functions
created during compilation
- Instead of storing pointers to the in-memory function representation
in the constant list, store the index of the function within the
program's function list
- When loading functions from the constant list, retrieve the function
by index from the program entity
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'include')
-rw-r--r-- | include/ucode/compiler.h | 1 | ||||
-rw-r--r-- | include/ucode/program.h | 31 | ||||
-rw-r--r-- | include/ucode/types.h | 18 | ||||
-rw-r--r-- | include/ucode/vallist.h | 2 |
4 files changed, 48 insertions, 4 deletions
diff --git a/include/ucode/compiler.h b/include/ucode/compiler.h index 04fc0ef..df242dc 100644 --- a/include/ucode/compiler.h +++ b/include/ucode/compiler.h @@ -116,6 +116,7 @@ typedef struct uc_compiler { uc_exprstack_t *exprstack; uc_value_t *function; uc_parser_t *parser; + uc_program_t *program; size_t scope_depth, current_srcpos, last_insn; } uc_compiler_t; diff --git a/include/ucode/program.h b/include/ucode/program.h new file mode 100644 index 0000000..19b3c9f --- /dev/null +++ b/include/ucode/program.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2022 Jo-Philipp Wich <jo@mein.io> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __PROGRAM_H_ +#define __PROGRAM_H_ + +#include "types.h" + + +uc_program_t *uc_program_new(void); + +void uc_program_free(uc_program_t *); + +uc_value_t *uc_program_function_new(uc_program_t *, const char *, size_t, uc_source_t *); +size_t uc_program_function_id(uc_program_t *, uc_value_t *); +uc_value_t *uc_program_function_load(uc_program_t *, size_t); + +#endif /* __PROGRAM_H_ */ diff --git a/include/ucode/types.h b/include/ucode/types.h index cbd03dd..7bd0ea9 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -148,14 +148,16 @@ typedef struct { char source[]; } uc_regexp_t; -typedef struct { +typedef struct uc_function { uc_value_t header; - bool arrow, vararg, strict; + bool arrow, vararg, strict, root; size_t nargs; size_t nupvals; size_t srcpos; uc_chunk_t chunk; uc_source_t *source; + struct uc_program *program; + uc_weakref_t progref; char name[]; } uc_function_t; @@ -199,6 +201,13 @@ typedef struct { uc_declare_vector(uc_resource_types_t, uc_resource_type_t *); +/* Program structure definitions */ + +typedef struct uc_program { + uc_weakref_t functions; +} uc_program_t; + + /* Parser definitions */ typedef struct { @@ -275,6 +284,9 @@ struct uc_vm { void ucv_free(uc_value_t *, bool); void ucv_put(uc_value_t *); +void ucv_unref(uc_weakref_t *); +void ucv_ref(uc_weakref_t *, uc_weakref_t *); + uc_value_t *ucv_get(uc_value_t *uv); uc_type_t ucv_type(uc_value_t *); @@ -338,7 +350,7 @@ size_t ucv_object_length(uc_value_t *); : 0); \ entry##key = entry_next##key) -uc_value_t *ucv_function_new(const char *, size_t, uc_source_t *); +uc_value_t *ucv_function_new(const char *, size_t, uc_source_t *, uc_program_t *); size_t ucv_function_srcpos(uc_value_t *, size_t); uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t); diff --git a/include/ucode/vallist.h b/include/ucode/vallist.h index a1b33a5..f1c1437 100644 --- a/include/ucode/vallist.h +++ b/include/ucode/vallist.h @@ -38,7 +38,7 @@ typedef enum { TAG_DBL = 3, TAG_STR = 4, TAG_LSTR = 5, - TAG_PTR = 6 + TAG_FUNC = 6 } uc_value_type_t; uc_value_t *uc_number_parse(const char *buf, char **end); |