summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-01-18 11:42:47 +0100
committerGitHub <noreply@github.com>2022-01-18 11:42:47 +0100
commit4015c2c1769989c20ea7234296eea02a16d1703d (patch)
tree4c57d279a8b54df10e6b194b586d3f6423d8e7cd /include
parent0e5b273c3d25d1dce3b469f3a6865f430554e730 (diff)
parent3578afee0ab77650cf6adf3987a6f653f9311e9b (diff)
Merge pull request #32 from jow-/precompile
Introduce ucode precompilation support
Diffstat (limited to 'include')
-rw-r--r--include/ucode/chunk.h2
-rw-r--r--include/ucode/compiler.h1
-rw-r--r--include/ucode/program.h39
-rw-r--r--include/ucode/source.h12
-rw-r--r--include/ucode/types.h22
-rw-r--r--include/ucode/util.h8
-rw-r--r--include/ucode/vallist.h2
7 files changed, 76 insertions, 10 deletions
diff --git a/include/ucode/chunk.h b/include/ucode/chunk.h
index 0005e3c..6804eeb 100644
--- a/include/ucode/chunk.h
+++ b/include/ucode/chunk.h
@@ -28,8 +28,6 @@ void uc_chunk_init(uc_chunk_t *chunk);
void uc_chunk_free(uc_chunk_t *chunk);
size_t uc_chunk_add(uc_chunk_t *chunk, uint8_t byte, size_t line);
-ssize_t uc_chunk_add_constant(uc_chunk_t *chunk, uc_value_t *value);
-uc_value_t *uc_chunk_get_constant(uc_chunk_t *chunk, size_t idx);
void uc_chunk_pop(uc_chunk_t *chunk);
size_t uc_chunk_debug_get_srcpos(uc_chunk_t *chunk, size_t off);
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..9a5c553
--- /dev/null
+++ b/include/ucode/program.h
@@ -0,0 +1,39 @@
+/*
+ * 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(uc_source_t *);
+
+void uc_program_free(uc_program_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 *);
+
+void uc_program_to_file(uc_program_t *, FILE *, bool);
+uc_program_t *uc_program_from_file(FILE *file, char **);
+
+uc_function_t *uc_program_entry(uc_program_t *);
+
+#endif /* __PROGRAM_H_ */
diff --git a/include/ucode/source.h b/include/ucode/source.h
index 3de7c93..ac0b487 100644
--- a/include/ucode/source.h
+++ b/include/ucode/source.h
@@ -25,6 +25,13 @@
#include "types.h"
+#define UC_PRECOMPILED_BYTECODE_MAGIC 0x1b756362 /* <esc> 'u' 'c' 'b' */
+
+typedef enum {
+ UC_SOURCE_TYPE_PLAIN = 0,
+ UC_SOURCE_TYPE_PRECOMPILED = 1,
+} uc_source_type_t;
+
uc_source_t *uc_source_new_file(const char *path);
uc_source_t *uc_source_new_buffer(const char *name, char *buf, size_t len);
@@ -33,4 +40,9 @@ size_t uc_source_get_line(uc_source_t *source, size_t *offset);
uc_source_t *uc_source_get(uc_source_t *source);
void uc_source_put(uc_source_t *source);
+uc_source_type_t uc_source_type_test(uc_source_t *source);
+
+void uc_source_line_next(uc_source_t *source);
+void uc_source_line_update(uc_source_t *source, size_t off);
+
#endif /* __SOURCE_H_ */
diff --git a/include/ucode/types.h b/include/ucode/types.h
index cbd03dd..66db5ea 100644
--- a/include/ucode/types.h
+++ b/include/ucode/types.h
@@ -90,7 +90,6 @@ uc_declare_vector(uc_offsetinfo_t, uint8_t);
typedef struct {
size_t count;
uint8_t *entries;
- uc_value_list_t constants;
uc_ehranges_t ehranges;
struct {
uc_variables_t variables;
@@ -148,14 +147,15 @@ 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 +199,15 @@ typedef struct {
uc_declare_vector(uc_resource_types_t, uc_resource_type_t *);
+/* Program structure definitions */
+
+typedef struct uc_program {
+ uc_value_list_t constants;
+ uc_weakref_t functions;
+ uc_source_t *source;
+} 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_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/util.h b/include/ucode/util.h
index 858a3fd..1ad13bd 100644
--- a/include/ucode/util.h
+++ b/include/ucode/util.h
@@ -71,8 +71,8 @@
/* "failsafe" utility functions */
-static inline void *xalloc(size_t size) {
- void *ptr = calloc(1, size);
+static inline void *xcalloc(size_t size, size_t nmemb) {
+ void *ptr = calloc(size, nmemb);
if (!ptr) {
fprintf(stderr, "Out of memory\n");
@@ -82,6 +82,10 @@ static inline void *xalloc(size_t size) {
return ptr;
}
+static inline void *xalloc(size_t size) {
+ return xcalloc(1, size);
+}
+
static inline void *xrealloc(void *ptr, size_t size) {
ptr = realloc(ptr, size);
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);