From 578531395ecbabd8179e31520c2832ac7d6d3765 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Thu, 27 Jul 2023 12:06:17 -0700 Subject: add dynamic typing example --- .../dynamic_typing/runtime/dynamic_typing.c | 72 ++++++++++++++++++++++ .../dynamic_typing/runtime/dynamic_typing.h | 48 +++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 python/examples/dynamic_typing/runtime/dynamic_typing.c create mode 100644 python/examples/dynamic_typing/runtime/dynamic_typing.h (limited to 'python/examples/dynamic_typing/runtime') diff --git a/python/examples/dynamic_typing/runtime/dynamic_typing.c b/python/examples/dynamic_typing/runtime/dynamic_typing.c new file mode 100644 index 0000000..4e47bc9 --- /dev/null +++ b/python/examples/dynamic_typing/runtime/dynamic_typing.c @@ -0,0 +1,72 @@ +#include +#include "dynamic_typing.h" + +struct entry { + void *start; + void *end; + struct type *type; +}; + +static struct entry *ENTRIES; +static unsigned long N_ENTRIES; + +void annotate_type(void *ptr, struct type *type) { + ENTRIES = realloc(ENTRIES, (++N_ENTRIES) * sizeof(ENTRIES[0])); + ENTRIES[N_ENTRIES - 1] = (struct entry){ + .start = ptr, + .end = ptr + 1, + .type = type, + }; +} + +struct type *dynamic_typeof(void *ptr) { + for (unsigned long i = N_ENTRIES; i --> 0;) + if (ENTRIES[i].start <= ptr && ptr < ENTRIES[i].end) + return ENTRIES[i].type; + return 0; +} + +struct type *make_basic(const char *type, unsigned long size) { + struct type *t = calloc(1, sizeof(*t)); + t->kind = TY_BASIC; + t->basic = type; + t->size = size; + return t; +} + +struct type *make_array(struct type *base, unsigned long len) { + struct type *t = calloc(1, sizeof(*t)); + t->kind = TY_ARRAY; + t->base = base; + t->length = len; + t->size = len * t->base->size; + return t; +} + +struct type *make_pointer(struct type *base) { + struct type *t = calloc(1, sizeof(*t)); + t->kind = TY_POINTER; + t->base = base; + t->size = sizeof(void*); + return t; +} + +// function not supported yet +// for struct & union, the members are initialized by the caller +struct type *make_struct(unsigned long size) { + struct type *t = calloc(1, sizeof(*t)); + t->kind = TY_STRUCT; + return t; +} + +struct type *make_union(unsigned long size) { + struct type *t = calloc(1, sizeof(*t)); + t->kind = TY_UNION; + return t; +} +void prepend_member(struct type *aggregate, struct type *member, const char *name, unsigned long offset) { + member->offset = offset; + member->next_member = aggregate->members; + member->field_name = name; + aggregate->members = member; +} diff --git a/python/examples/dynamic_typing/runtime/dynamic_typing.h b/python/examples/dynamic_typing/runtime/dynamic_typing.h new file mode 100644 index 0000000..4f7b1aa --- /dev/null +++ b/python/examples/dynamic_typing/runtime/dynamic_typing.h @@ -0,0 +1,48 @@ +#ifndef DYNAMIC_TYPING_ +#define DYNAMIC_TYPING_ + +enum TYPE_KIND { + TY_BASIC, + TY_ARRAY, + TY_POINTER, + TY_FUNCTION, + TY_STRUCT, + TY_UNION, +}; + +struct type { + enum TYPE_KIND kind; + unsigned long size; + + // basic types: + const char *basic; + + // array & pointer types: + struct type *base; + unsigned long length; + + // struct & union types + struct type *members; + struct type *next_member; + unsigned long offset; + const char *field_name; + + // function types: + struct type *return_type; + struct type *params; + struct type *next_param; +}; + +struct type *dynamic_typeof(void *ptr); + +struct type *make_basic(const char *type, unsigned long size); +struct type *make_array(struct type *base, unsigned long len); +struct type *make_pointer(struct type *base); +// function not supported yet +// for struct & union, the members are initialized by the caller +struct type *make_struct(unsigned long size); +struct type *make_union(unsigned long size); +void prepend_member(struct type *aggregate, struct type *member, const char *name, unsigned long offset); +void annotate_type(void *ptr, struct type *type); + +#endif -- cgit v1.2.3