From 1533629f4e11bee6e474a4adb5e2c9e32e9f762c Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 12 Oct 2022 14:25:59 +0100 Subject: [PATCH] Implement fixed-integer vla Signed-off-by: Mathieu Desnoyers --- include/side/trace.h | 39 ++++++++--- src/Makefile | 2 +- src/test.c | 24 +++++-- src/tracer.c | 151 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 202 insertions(+), 14 deletions(-) diff --git a/include/side/trace.h b/include/side/trace.h index 5fb703e..0a0764d 100644 --- a/include/side/trace.h +++ b/include/side/trace.h @@ -44,8 +44,16 @@ enum side_type { SIDE_TYPE_ARRAY_S32, SIDE_TYPE_ARRAY_S64, + SIDE_TYPE_VLA_U8, + SIDE_TYPE_VLA_U16, + SIDE_TYPE_VLA_U32, + SIDE_TYPE_VLA_U64, + SIDE_TYPE_VLA_S8, + SIDE_TYPE_VLA_S16, + SIDE_TYPE_VLA_S32, + SIDE_TYPE_VLA_S64, + //TODO: - //specialized vla for fixed-size integers (optimization) //variants (discriminated unions) }; @@ -127,6 +135,10 @@ struct side_arg_vec { void *side_vla_visitor_ctx; void *side_array_fixint; + struct { + void *p; + uint32_t length; + } side_vla_fixint; } u; }; @@ -223,14 +235,23 @@ struct side_arg_vec_description { #define side_arg_vla(_side_type) { .type = SIDE_TYPE_VLA, .u = { .side_vla = (_side_type) } } #define side_arg_vla_visitor(_ctx) { .type = SIDE_TYPE_VLA_VISITOR, .u = { .side_vla_visitor_ctx = (_ctx) } } -#define side_arg_array_u8(ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_u16(ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_u32(ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_u64(ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_s8(ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_s16(ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_s32(ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (ptr) } } -#define side_arg_array_s64(ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (ptr) } } +#define side_arg_array_u8(_ptr) { .type = SIDE_TYPE_ARRAY_U8, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_u16(_ptr) { .type = SIDE_TYPE_ARRAY_U16, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_u32(_ptr) { .type = SIDE_TYPE_ARRAY_U32, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_u64(_ptr) { .type = SIDE_TYPE_ARRAY_U64, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_s8(_ptr) { .type = SIDE_TYPE_ARRAY_S8, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_s16(_ptr) { .type = SIDE_TYPE_ARRAY_S16, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_s32(_ptr) { .type = SIDE_TYPE_ARRAY_S32, .u = { .side_array_fixint = (_ptr) } } +#define side_arg_array_s64(_ptr) { .type = SIDE_TYPE_ARRAY_S64, .u = { .side_array_fixint = (_ptr) } } + +#define side_arg_vla_u8(_ptr, _length) { .type = SIDE_TYPE_VLA_U8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } +#define side_arg_vla_u16(_ptr, _length) { .type = SIDE_TYPE_VLA_U16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_u32(_ptr, _length) { .type = SIDE_TYPE_VLA_U32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_u64(_ptr, _length) { .type = SIDE_TYPE_VLA_U64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_s8(_ptr, _length) { .type = SIDE_TYPE_VLA_S8, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_s16(_ptr, _length) { .type = SIDE_TYPE_VLA_S16, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_s32(_ptr, _length) { .type = SIDE_TYPE_VLA_S32, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } +#define side_arg_vla_s64(_ptr, _length) { .type = SIDE_TYPE_VLA_S64, .u = { .side_vla_fixint = { .p = (_ptr), .length = (_length) } } } #define side_arg_define_vec(_identifier, _sav) \ const struct side_arg_vec _identifier##_vec[] = { _sav }; \ diff --git a/src/Makefile b/src/Makefile index 24c93d2..5056c13 100644 --- a/src/Makefile +++ b/src/Makefile @@ -12,4 +12,4 @@ test: tracer.o test.o .PHONY: clean clean: - rm -f test + rm -f test *.o diff --git a/src/test.c b/src/test.c index b92fd17..f4f95a7 100644 --- a/src/test.c +++ b/src/test.c @@ -155,10 +155,25 @@ static void test_array_fixint(void) { my_provider_event_array_fixint.enabled = 1; - side_event_cond(&my_provider_event_array_fixint) { - side_event_call(&my_provider_event_array_fixint, - side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42))); - } + side_event(&my_provider_event_array_fixint, + side_arg_list(side_arg_array_s64(array_fixint), side_arg_s64(42))); +} + +static int64_t vla_fixint[] = { -444, 555, 123, 2897432587 }; + +static side_define_event(my_provider_event_vla_fixint, "myprovider", "myvlafixint", SIDE_LOGLEVEL_DEBUG, + side_field_list( + side_field_vla("vlafixint", side_elem(SIDE_TYPE_S64)), + side_field(SIDE_TYPE_S64, "v"), + ) +); + +static +void test_vla_fixint(void) +{ + my_provider_event_vla_fixint.enabled = 1; + side_event(&my_provider_event_vla_fixint, + side_arg_list(side_arg_vla_s64(vla_fixint, SIDE_ARRAY_SIZE(vla_fixint)), side_arg_s64(42))); } int main() @@ -169,5 +184,6 @@ int main() test_vla(); test_vla_visitor(); test_array_fixint(); + test_vla_fixint(); return 0; } diff --git a/src/tracer.c b/src/tracer.c index eea7df6..2637f4a 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -20,6 +20,8 @@ static void tracer_print_vla_visitor(const struct side_type_description *type_desc, void *ctx); static void tracer_print_array_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item); +static +void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item); static void tracer_print_type(const struct side_type_description *type_desc, const struct side_arg_vec *item) @@ -38,6 +40,20 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru abort(); } break; + case SIDE_TYPE_VLA_U8: + case SIDE_TYPE_VLA_U16: + case SIDE_TYPE_VLA_U32: + case SIDE_TYPE_VLA_U64: + case SIDE_TYPE_VLA_S8: + case SIDE_TYPE_VLA_S16: + case SIDE_TYPE_VLA_S32: + case SIDE_TYPE_VLA_S64: + if (type_desc->type != SIDE_TYPE_VLA) { + printf("ERROR: type mismatch between description and arguments\n"); + abort(); + } + break; + default: if (type_desc->type != SIDE_TYPE_DYNAMIC && type_desc->type != item->type) { printf("ERROR: type mismatch between description and arguments\n"); @@ -95,6 +111,16 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru case SIDE_TYPE_ARRAY_S64: tracer_print_array_fixint(type_desc, item); break; + case SIDE_TYPE_VLA_U8: + case SIDE_TYPE_VLA_U16: + case SIDE_TYPE_VLA_U32: + case SIDE_TYPE_VLA_U64: + case SIDE_TYPE_VLA_S8: + case SIDE_TYPE_VLA_S16: + case SIDE_TYPE_VLA_S32: + case SIDE_TYPE_VLA_S64: + tracer_print_vla_fixint(type_desc, item); + break; default: printf(""); abort(); @@ -323,6 +349,131 @@ type_error: abort(); } +void tracer_print_vla_fixint(const struct side_type_description *type_desc, const struct side_arg_vec *item) +{ + const struct side_type_description *elem_type = type_desc->u.side_vla.elem_type; + uint32_t side_sav_len = item->u.side_vla_fixint.length; + void *p = item->u.side_vla_fixint.p; + enum side_type side_type; + int i; + + if (elem_type->type != SIDE_TYPE_DYNAMIC) { + switch (item->type) { + case SIDE_TYPE_VLA_U8: + if (elem_type->type != SIDE_TYPE_U8) + goto type_error; + break; + case SIDE_TYPE_VLA_U16: + if (elem_type->type != SIDE_TYPE_U16) + goto type_error; + break; + case SIDE_TYPE_VLA_U32: + if (elem_type->type != SIDE_TYPE_U32) + goto type_error; + break; + case SIDE_TYPE_VLA_U64: + if (elem_type->type != SIDE_TYPE_U64) + goto type_error; + break; + case SIDE_TYPE_VLA_S8: + if (elem_type->type != SIDE_TYPE_S8) + goto type_error; + break; + case SIDE_TYPE_VLA_S16: + if (elem_type->type != SIDE_TYPE_S16) + goto type_error; + break; + case SIDE_TYPE_VLA_S32: + if (elem_type->type != SIDE_TYPE_S32) + goto type_error; + break; + case SIDE_TYPE_VLA_S64: + if (elem_type->type != SIDE_TYPE_S64) + goto type_error; + break; + default: + goto type_error; + } + side_type = elem_type->type; + } else { + switch (item->type) { + case SIDE_TYPE_VLA_U8: + side_type = SIDE_TYPE_U8; + break; + case SIDE_TYPE_VLA_U16: + side_type = SIDE_TYPE_U16; + break; + case SIDE_TYPE_VLA_U32: + side_type = SIDE_TYPE_U32; + break; + case SIDE_TYPE_VLA_U64: + side_type = SIDE_TYPE_U64; + break; + case SIDE_TYPE_VLA_S8: + side_type = SIDE_TYPE_S8; + break; + case SIDE_TYPE_VLA_S16: + side_type = SIDE_TYPE_S16; + break; + case SIDE_TYPE_VLA_S32: + side_type = SIDE_TYPE_S32; + break; + case SIDE_TYPE_VLA_S64: + side_type = SIDE_TYPE_S64; + break; + default: + goto type_error; + } + } + + printf("[ "); + for (i = 0; i < side_sav_len; i++) { + struct side_arg_vec sav_elem = { + .type = side_type, + }; + + switch (side_type) { + case SIDE_TYPE_U8: + sav_elem.u.side_u8 = ((const uint8_t *) p)[i]; + break; + case SIDE_TYPE_S8: + sav_elem.u.side_s8 = ((const int8_t *) p)[i]; + break; + case SIDE_TYPE_U16: + sav_elem.u.side_u16 = ((const uint16_t *) p)[i]; + break; + case SIDE_TYPE_S16: + sav_elem.u.side_s16 = ((const int16_t *) p)[i]; + break; + case SIDE_TYPE_U32: + sav_elem.u.side_u32 = ((const uint32_t *) p)[i]; + break; + case SIDE_TYPE_S32: + sav_elem.u.side_s32 = ((const int32_t *) p)[i]; + break; + case SIDE_TYPE_U64: + sav_elem.u.side_u64 = ((const uint64_t *) p)[i]; + break; + case SIDE_TYPE_S64: + sav_elem.u.side_s64 = ((const int64_t *) p)[i]; + break; + + default: + printf("ERROR: Unexpected type\n"); + abort(); + } + + printf("%s", i ? ", " : ""); + tracer_print_type(elem_type, &sav_elem); + } + printf(" ]"); + return; + +type_error: + printf("ERROR: type mismatch\n"); + abort(); +} + void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc) { const struct side_arg_vec *sav = sav_desc->sav; -- 2.34.1