diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/ucode/compiler.h | 4 | ||||
-rw-r--r-- | include/ucode/program.h | 33 | ||||
-rw-r--r-- | include/ucode/types.h | 31 | ||||
-rw-r--r-- | include/ucode/vallist.h | 3 | ||||
-rw-r--r-- | include/ucode/vm.h | 3 |
5 files changed, 48 insertions, 26 deletions
diff --git a/include/ucode/compiler.h b/include/ucode/compiler.h index df242dc..c112027 100644 --- a/include/ucode/compiler.h +++ b/include/ucode/compiler.h @@ -114,7 +114,7 @@ typedef struct uc_compiler { uc_upvals_t upvals; uc_patchlist_t *patchlist; uc_exprstack_t *exprstack; - uc_value_t *function; + uc_function_t *function; uc_parser_t *parser; uc_program_t *program; size_t scope_depth, current_srcpos, last_insn; @@ -126,7 +126,7 @@ typedef struct { uc_precedence_t precedence; } uc_parse_rule_t; -uc_function_t *uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp); +uc_program_t *uc_compile(uc_parse_config_t *config, uc_source_t *source, char **errp); #define uc_compiler_exprstack_push(compiler, token, flags) \ uc_exprstack_t expr = { compiler->exprstack, flags, token }; \ diff --git a/include/ucode/program.h b/include/ucode/program.h index 39131eb..2b2817b 100644 --- a/include/ucode/program.h +++ b/include/ucode/program.h @@ -22,11 +22,36 @@ uc_program_t *uc_program_new(uc_source_t *); -void uc_program_free(uc_program_t *); +static inline uc_program_t * +uc_program_get(uc_program_t *prog) { + return (uc_program_t *)ucv_get(prog ? &prog->header : NULL); +} + +static inline void +uc_program_put(uc_program_t *prog) { + ucv_put(prog ? &prog->header : NULL); +} + +#define uc_program_function_foreach(prog, fn) \ + uc_function_t *fn; \ + for (fn = (uc_function_t *)prog->functions.prev; \ + fn != (uc_function_t *)&prog->functions; \ + fn = (uc_function_t *)fn->progref.prev) + +#define uc_program_function_foreach_safe(prog, fn) \ + uc_function_t *fn, *fn##_tmp; \ + for (fn = (uc_function_t *)prog->functions.prev, \ + fn##_tmp = (uc_function_t *)fn->progref.prev; \ + fn != (uc_function_t *)&prog->functions; \ + fn = fn##_tmp, \ + fn##_tmp = (uc_function_t *)fn##_tmp->progref.prev) + +uc_function_t *uc_program_function_new(uc_program_t *, const char *, size_t); +size_t uc_program_function_id(uc_program_t *, uc_function_t *); +uc_function_t *uc_program_function_load(uc_program_t *, size_t); +size_t uc_program_function_srcpos(uc_function_t *, size_t); +void uc_program_function_free(uc_function_t *); -uc_value_t *uc_program_function_new(uc_program_t *, const char *, size_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); uc_value_t *uc_program_get_constant(uc_program_t *, size_t); ssize_t uc_program_add_constant(uc_program_t *, uc_value_t *); diff --git a/include/ucode/types.h b/include/ucode/types.h index cc55150..ff87ca7 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -36,11 +36,11 @@ typedef enum uc_type { UC_ARRAY, UC_OBJECT, UC_REGEXP, - UC_FUNCTION, UC_CFUNCTION, UC_CLOSURE, UC_UPVALUE, - UC_RESOURCE + UC_RESOURCE, + UC_PROGRAM } uc_type_t; typedef struct uc_value { @@ -106,6 +106,17 @@ typedef struct uc_weakref { struct uc_weakref *next; } uc_weakref_t; +typedef struct uc_function { + uc_weakref_t progref; + bool arrow, vararg, strict; + size_t nargs; + size_t nupvals; + size_t srcpos; + uc_chunk_t chunk; + struct uc_program *program; + char name[]; +} uc_function_t; + typedef struct { uc_value_t header; double dbl; @@ -147,18 +158,6 @@ typedef struct { char source[]; } uc_regexp_t; -typedef struct uc_function { - uc_value_t header; - bool arrow, vararg, strict, root; - size_t nargs; - size_t nupvals; - size_t srcpos; - uc_chunk_t chunk; - struct uc_program *program; - uc_weakref_t progref; - char name[]; -} uc_function_t; - typedef struct uc_upval_tref { uc_value_t header; size_t slot; @@ -202,6 +201,7 @@ uc_declare_vector(uc_resource_types_t, uc_resource_type_t *); /* Program structure definitions */ typedef struct uc_program { + uc_value_t header; uc_value_list_t constants; uc_weakref_t functions; uc_source_t *source; @@ -350,9 +350,6 @@ 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_program_t *); -size_t ucv_function_srcpos(uc_value_t *, size_t); - uc_value_t *ucv_cfunction_new(const char *, uc_cfn_ptr_t); uc_value_t *ucv_closure_new(uc_vm_t *, uc_function_t *, bool); diff --git a/include/ucode/vallist.h b/include/ucode/vallist.h index f1c1437..f3f1b06 100644 --- a/include/ucode/vallist.h +++ b/include/ucode/vallist.h @@ -37,8 +37,7 @@ typedef enum { TAG_LNUM = 2, TAG_DBL = 3, TAG_STR = 4, - TAG_LSTR = 5, - TAG_FUNC = 6 + TAG_LSTR = 5 } uc_value_type_t; uc_value_t *uc_number_parse(const char *buf, char **end); diff --git a/include/ucode/vm.h b/include/ucode/vm.h index caebb7a..4ba4627 100644 --- a/include/ucode/vm.h +++ b/include/ucode/vm.h @@ -24,6 +24,7 @@ #include "util.h" #include "lexer.h" #include "types.h" +#include "program.h" #define __insns \ __insn(NOOP) \ @@ -139,7 +140,7 @@ uc_exception_type_t uc_vm_call(uc_vm_t *vm, bool mcall, size_t nargs); void __attribute__((format(printf, 3, 0))) uc_vm_raise_exception(uc_vm_t *vm, uc_exception_type_t type, const char *fmt, ...); -uc_vm_status_t uc_vm_execute(uc_vm_t *vm, uc_function_t *fn, uc_value_t **retval); +uc_vm_status_t uc_vm_execute(uc_vm_t *vm, uc_program_t *fn, uc_value_t **retval); uc_value_t *uc_vm_invoke(uc_vm_t *vm, const char *fname, size_t nargs, ...); #endif /* __VM_H_ */ |