From 0b4cb7683439c6a08538dc46fa9237c427184340 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 20 Mar 2014 20:06:59 +0900 Subject: [PATCH] add xvect --- src/codegen.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/codegen.c b/src/codegen.c index 09b59856..adc75f58 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -15,6 +15,78 @@ # error enable PIC_NONE_IS_FALSE #endif +typedef struct xvect { + char *data; + size_t size, capa, width; +} xvect; + +static inline void xv_init(xvect *, size_t); +static inline void xv_destroy(xvect *); + +static inline void xv_reserve(xvect *, size_t); + +static inline void xv_get(xvect *, size_t, void *); +static inline void xv_set(xvect *, size_t, void *); + +static inline void xv_push(xvect *, void *); +static inline void xv_peek(xvect *, void *); +static inline void xv_pop(xvect *, void *); + +static inline void +xv_init(xvect *x, size_t width) +{ + x->data = NULL; + x->size = 0; + x->capa = 0; + x->width = width; +} + +static inline void +xv_destroy(xvect *x) +{ + free(x->data); +} + +static inline void +xv_reserve(xvect *x, size_t newcapa) +{ + x->data = realloc(x->data, newcapa * x->width); + x->capa = newcapa; +} + +static inline void +xv_get(xvect *x, size_t i, void *dst) +{ + memcpy(dst, x->data + i * x->width, x->width); +} + +static inline void +xv_set(xvect *x, size_t i, void *src) +{ + memcpy(x->data + i * x->width, src, x->width); +} + +static inline void +xv_push(xvect *x, void *src) +{ + if (x->capa <= x->size + 1) { + xv_reserve(x, x->size * 2 + 1); + } + xv_set(x, x->size++, src); +} + +static inline void +xv_peek(xvect *x, void *dst) +{ + xv_get(x, x->size, dst); +} + +static inline void +xv_pop(xvect *x, void *dst) +{ + xv_get(x, --x->size, dst); +} + typedef struct analyze_scope { /* rest args variable is counted by localc */ bool varg;