Skip to content

Commit

Permalink
more generic funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou committed Oct 20, 2023
1 parent fb60b30 commit abbe6af
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 83 deletions.
4 changes: 3 additions & 1 deletion src/internal/fy-generics.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ static inline fy_generic fy_generic_arena_relocate_collection_ptr(struct fy_relo
return v;
}

#if 0
static fy_generic fy_generic_arena_relocate(struct fy_relocation_info *ri, fy_generic v)
{
void *p;
Expand Down Expand Up @@ -248,6 +249,7 @@ static fy_generic fy_generic_arena_relocate(struct fy_relocation_info *ri, fy_ge

return v;
}
#endif

#if 0
static void fy_generic_print_primitive(FILE *fp, int level, fy_generic v)
Expand Down Expand Up @@ -548,7 +550,7 @@ const void *fy_generic_builder_linearize(struct fy_generic_builder *gb, fy_gener
fy_generic_dump_primitive(stderr, 0,
fy_generic_mapping_lookup( /* the 1st document */
fy_generic_sequence_get_item(v, 0),
fy_generic_string_alloca("root")));
fy_string_alloca("root")));
fprintf(stderr, "\n");

#if 0
Expand Down
89 changes: 63 additions & 26 deletions src/internal/libfyaml-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -4207,7 +4207,7 @@ fy_generic do_x(void)
fy_generic vstr;

// asm volatile("nop; nop" : : : "memory");
vstr = fy_generic_string_alloca("test");
vstr = fy_string_alloca("test");
// asm volatile("nop; nop" : : "r"(vstr) : "memory");

return vstr;
Expand All @@ -4218,7 +4218,7 @@ fy_generic do_x2(void)
fy_generic vf;

asm volatile("nop; nop" : : : "memory");
vf = fy_generic_float_alloca(128.0);
vf = fy_float_alloca(128.0);
asm volatile("nop; nop" : : "r"(vf) : "memory");

return vf;
Expand All @@ -4229,12 +4229,14 @@ fy_generic do_x3(void)
fy_generic vf;

asm volatile("nop; nop" : : : "memory");
vf = fy_generic_float_alloca(128.1);
vf = fy_float_alloca(128.1);
asm volatile("nop; nop" : : "r"(vf) : "memory");

return vf;
}

char *testing_export = "This is export";

int do_generics(int argc, char *argv[], const char *allocator)
{
static const bool btable[] = {
Expand Down Expand Up @@ -4269,6 +4271,7 @@ int do_generics(int argc, char *argv[], const char *allocator)
256.1, -256.1,
INFINITY, -INFINITY,
NAN, -NAN,
100000.00001, /* does not fit in 32 bit float */
};
double fv;
static const size_t sztable[] = {
Expand Down Expand Up @@ -4348,21 +4351,21 @@ int do_generics(int argc, char *argv[], const char *allocator)
printf("null = %016lx\n", fy_null);
for (i = 0; i < ARRAY_SIZE(btable); i++) {
bv = btable[i];
gbl = fy_generic_bool_alloca(bv);
gbl = fy_bool_alloca(bv);
printf("boolean/%s = %016lx %s\n", bv ? "true" : "false", gbl,
fy_generic_get_bool(gbl) ? "true" : "false");
}

for (i = 0; i < ARRAY_SIZE(itable); i++) {
iv = itable[i];
gi = fy_generic_int_alloca(iv);
gi = fy_int_alloca(iv);
printf("int/%lld = %016lx %lld\n", iv, gi,
fy_generic_get_int(gi));
}

for (i = 0; i < ARRAY_SIZE(stable); i++) {
sv = stable[i];
gs = fy_generic_string_alloca(sv);
gs = fy_string_alloca(sv);
printf("string/%s = %016lx", sv, gs);

sv = fy_generic_get_string_size(gs, sinplace, &slen);
Expand All @@ -4372,54 +4375,88 @@ int do_generics(int argc, char *argv[], const char *allocator)

for (i = 0; i < ARRAY_SIZE(ftable); i++) {
fv = ftable[i];
gf = fy_generic_float_alloca(fv);
gf = fy_float_alloca(fv);
printf("float/%f = %016lx %f\n", fv, gf,
fy_generic_get_float(gf));
}

seq = fy_generic_sequence_alloca(3, ((fy_generic[]){
fy_generic_bool_alloca(true),
fy_generic_int_alloca(100),
fy_generic_string_alloca("info")}));
seq = fy_sequence_alloca(3, ((fy_generic[]){
fy_bool_alloca(true),
fy_int_alloca(100),
fy_string_alloca("info")}));
assert(seq != fy_invalid);

printf("seq:\n");
fy_generic_print_primitive(stdout, seq);
printf("\n");

map = fy_generic_mapping_alloca(3, ((fy_generic[]){
fy_generic_string_alloca("foo"), fy_generic_string_alloca("bar"),
fy_generic_string_alloca("frooz-larger"), fy_generic_string_alloca("what"),
fy_generic_string_alloca("seq"), seq}));
map = fy_mapping_alloca(3, ((fy_generic[]){
fy_string_alloca("foo"), fy_string_alloca("bar"),
fy_string_alloca("frooz-larger"), fy_string_alloca("what"),
fy_string_alloca("seq"), seq}));

assert(map != fy_invalid);

printf("map:\n");
fy_generic_print_primitive(stdout, map);
printf("\n");

gv = fy_generic_mapping_lookup(map, fy_generic_string_alloca("foo"));
gv = fy_generic_mapping_lookup(map, fy_string_alloca("foo"));
printf("found: ");
fy_generic_print_primitive(stdout, gv);
printf("\n");

map = fy_generic_mapping_alloca(2, ((fy_generic[]){
fy_generic_string_alloca("foo"), fy_generic_string_alloca("bar"),
fy_generic_sequence_alloca(2, ((fy_generic[]){
fy_generic_int_alloca(10),
fy_generic_int_alloca(100)})),
fy_generic_float_alloca(3.14)}));
map = fy_mapping_alloca(2, ((fy_generic[]){
fy_string_alloca("foo"), fy_string_alloca("bar"),
fy_sequence_alloca(2, ((fy_generic[]){
fy_int_alloca(10),
fy_int_alloca(100)})),
fy_float_alloca(3.14)}));

fy_generic_print_primitive(stdout, map);
printf("\n");

gv = fy_generic_mapping_lookup(map, fy_generic_sequence_alloca(2, ((fy_generic[]){
fy_generic_int_alloca(10),
fy_generic_int_alloca(100)})));
gv = fy_generic_mapping_lookup(map,
fy_sequence_alloca(2, ((fy_generic[]){
fy_int_alloca(10),
fy_int_alloca(100)})));
printf("found: ");
fy_generic_print_primitive(stdout, gv);
printf("\n");

{
const fy_generic t0 = fy_string_const("Hello");
fy_generic t1 = fy_string_const("Hello-there friends");
fy_generic t2 = fy_string("Yet another test");
fy_generic t3 = fy_string(testing_export);

printf("string_size_const t0: ");
fy_generic_print_primitive(stdout, t0);
printf("\n");

printf("string_size_const t1: ");
fy_generic_print_primitive(stdout, t1);
printf("\n");

printf("string t2: ");
fy_generic_print_primitive(stdout, t2);
printf("\n");

printf("string t3: ");
fy_generic_print_primitive(stdout, t3);
printf("\n");
}

{
fy_generic seq = fy_sequence_alloca(3, ((fy_generic[]){
fy_bool(true),
fy_int(100),
fy_string("info-info")}));

printf("seq-x:\n");
fy_generic_print_primitive(stdout, seq);
printf("\n");
}

#define ASTR(_x) \
({ \
Expand All @@ -4441,7 +4478,7 @@ int do_generics(int argc, char *argv[], const char *allocator)
fy_generic vstr;

asm volatile("nop; nop" : : : "memory");
vstr = fy_generic_string_alloca("test");
vstr = fy_string_alloca("test");
asm volatile("nop; nop" : : "r"(vstr) : "memory");

printf("vstr=0x%08lx\n", (unsigned long)vstr);
Expand Down
2 changes: 1 addition & 1 deletion src/util/fy-generic-decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ fy_generic_compose_process_event(struct fy_parser *fyp, struct fy_event *fye, st

if (gd->resolve) {
v = fy_generic_decoder_alias_resolve(gd,
fy_generic_string_size_alloca(anchor, anchor_size));
fy_string_size_alloca(anchor, anchor_size));
if (v == fy_invalid) {
fy_parser_report_error(fyp, fy_event_get_token(fye),
!fy_generic_decoder_alias_is_collecting(gd, va) ?
Expand Down
20 changes: 10 additions & 10 deletions src/util/fy-generic-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,21 @@ int fy_generic_encoder_emit_document(struct fy_generic_encoder *fyge, fy_generic

/* the document state must be a mapping */
if (vds != fy_invalid && fy_generic_get_type(vds) == FYGT_MAPPING) {
vversion = fy_generic_mapping_lookup(vds, fy_generic_string_alloca("version"));
vversion = fy_generic_mapping_lookup(vds, fy_string_alloca("version"));

if (fy_generic_get_type(vversion) == FYGT_MAPPING) {
vers = &vers_local;
memset(vers, 0, sizeof(*vers));
vers->major = fy_generic_get_int(fy_generic_mapping_lookup(vversion, fy_generic_string_alloca("major")));
vers->minor = fy_generic_get_int(fy_generic_mapping_lookup(vversion, fy_generic_string_alloca("minor")));
vers->major = fy_generic_get_int(fy_generic_mapping_lookup(vversion, fy_string_alloca("major")));
vers->minor = fy_generic_get_int(fy_generic_mapping_lookup(vversion, fy_string_alloca("minor")));
}

vtags = fy_generic_mapping_lookup(vds, fy_generic_string_alloca("tags"));
vtags = fy_generic_mapping_lookup(vds, fy_string_alloca("tags"));
if (fy_generic_get_type(vtags) == FYGT_SEQUENCE) {
items = fy_generic_sequence_get_items(vtags, &count);

vhandle = fy_generic_string_alloca("handle");
vprefix = fy_generic_string_alloca("prefix");
vhandle = fy_string_alloca("handle");
vprefix = fy_string_alloca("prefix");

tags = alloca((count + 1) * sizeof(*tags));
for (i = 0; i < count; i++) {
Expand All @@ -258,8 +258,8 @@ int fy_generic_encoder_emit_document(struct fy_generic_encoder *fyge, fy_generic
tags[i] = NULL;
}

version_explicit = fy_generic_mapping_lookup(vds, fy_generic_string_alloca("version-explicit")) == fy_true;
tags_explicit = fy_generic_mapping_lookup(vds, fy_generic_string_alloca("tags-explicit")) == fy_true;
version_explicit = fy_generic_mapping_lookup(vds, fy_string_alloca("version-explicit")) == fy_true;
tags_explicit = fy_generic_mapping_lookup(vds, fy_string_alloca("tags-explicit")) == fy_true;

if (!version_explicit)
vers = NULL;
Expand Down Expand Up @@ -320,8 +320,8 @@ int fy_generic_encoder_emit_all_documents(struct fy_generic_encoder *fyge, fy_ge
if (fy_generic_get_type(vdir) != FYGT_SEQUENCE)
return -1;

vroot_key = fy_generic_string_alloca("root");
vdocs_key = fy_generic_string_alloca("docs");
vroot_key = fy_string_alloca("root");
vdocs_key = fy_string_alloca("docs");

/* no documents? nothing to emit */
items = fy_generic_sequence_get_items(vdir, &count);
Expand Down
10 changes: 3 additions & 7 deletions src/util/fy-generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,9 @@ fy_generic fy_generic_float_create(struct fy_generic_builder *gb, double val)
{
const double *valp;
#ifdef FY_HAS_64BIT_PTR
float f;
uint32_t fi;

if (fy_double_fits_in_float(val)) {
f = (float)val;
memcpy(&fi, &f, sizeof(fi));
return ((fy_generic)fi << FY_FLOAT_INPLACE_SHIFT) | FY_FLOAT_INPLACE_V;
if (!isnormal(val) || (float)val == val) {
float f = (float)val;
return ((fy_generic)*(uint32_t *)&f << FY_FLOAT_INPLACE_SHIFT) | FY_FLOAT_INPLACE_V;
}
#endif
valp = fy_generic_builder_store(gb, &val, sizeof(val), FY_SCALAR_ALIGNOF(double));
Expand Down
Loading

0 comments on commit abbe6af

Please sign in to comment.