Skip to content

Commit

Permalink
generic rework again
Browse files Browse the repository at this point in the history
Signed-off-by: Pantelis Antoniou <[email protected]>
  • Loading branch information
pantoniou committed Oct 23, 2023
1 parent 3b4968c commit 91ba558
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 311 deletions.
16 changes: 8 additions & 8 deletions src/internal/fy-generics.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,10 @@ static void fy_generic_print_primitive(FILE *fp, int level, fy_generic v)

vanchor = fy_generic_get_anchor(v);
if (fy_generic_get_type(vanchor) == FYGT_STRING)
anchor = fy_generic_get_string_alloca(vanchor);
anchor = fy_generic_get_string(vanchor);
vtag = fy_generic_get_tag(v);
if (fy_generic_get_type(vtag) == FYGT_STRING)
tag = fy_generic_get_string_alloca(vtag);
tag = fy_generic_get_string(vtag);

if (anchor)
fprintf(fp, "&%s ", anchor);
Expand Down Expand Up @@ -298,7 +298,7 @@ static void fy_generic_print_primitive(FILE *fp, int level, fy_generic v)
return;

case FYGT_STRING:
sv = fy_generic_get_string_size_alloca(v, &slen);
sv = fy_generic_get_string_size(v, &slen);
fprintf(fp, "\"%s\"", fy_utf8_format_text_a(sv, slen, fyue_doublequote));
return;

Expand Down Expand Up @@ -363,10 +363,10 @@ static void fy_generic_dump_primitive(FILE *fp, int level, fy_generic vv)

vanchor = fy_generic_get_anchor(vv);
if (fy_generic_get_type(vanchor) == FYGT_STRING)
anchor = fy_generic_get_string_alloca(vanchor);
anchor = fy_generic_get_string(vanchor);
vtag = fy_generic_get_tag(vv);
if (fy_generic_get_type(vtag) == FYGT_STRING)
tag = fy_generic_get_string_alloca(vtag);
tag = fy_generic_get_string(vtag);
v = fy_generic_is_indirect(vv) ? fy_generic_indirect_get_value(vv) : vv;

fprintf(fp, "%*s", level * 2, "");
Expand Down Expand Up @@ -403,7 +403,7 @@ static void fy_generic_dump_primitive(FILE *fp, int level, fy_generic vv)
return;

case FYGT_STRING:
sv = fy_generic_get_string_size_alloca(v, &slen);
sv = fy_generic_get_string_size(v, &slen);
fprintf(fp, "%s", fy_utf8_format_text_a(sv, slen, fyue_doublequote));
return;

Expand All @@ -427,7 +427,7 @@ static void fy_generic_dump_primitive(FILE *fp, int level, fy_generic vv)
break;

case FYGT_ALIAS:
sv = fy_generic_get_alias_size_alloca(v, &slen);
sv = fy_generic_get_alias_size(v, &slen);
fprintf(fp, "%.*s", (int)slen, sv);
break;

Expand Down Expand Up @@ -553,7 +553,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_string_alloca("root")));
fy_string("root")));
fprintf(stderr, "\n");

#if 0
Expand Down
74 changes: 46 additions & 28 deletions src/internal/libfyaml-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -4160,7 +4160,7 @@ void fy_generic_print_primitive(FILE *fp, fy_generic v)
return;

case FYGT_STRING:
sv = fy_generic_get_string_size_alloca(v, &slen);
sv = fy_generic_get_string_size(v, &slen);
fprintf(fp, "'%.*s'", (int)slen, sv);
return;

Expand Down Expand Up @@ -4192,7 +4192,7 @@ void fy_generic_print_primitive(FILE *fp, fy_generic v)
break;

case FYGT_ALIAS:
sv = fy_generic_get_alias_size_alloca(v, &slen);
sv = fy_generic_get_alias_size(v, &slen);
fprintf(fp, "*'%.*s'", (int)slen, sv);
break;

Expand All @@ -4206,9 +4206,9 @@ fy_generic do_x(void)
{
fy_generic vstr;

// asm volatile("nop; nop" : : : "memory");
vstr = fy_string_alloca("test");
// asm volatile("nop; nop" : : "r"(vstr) : "memory");
asm volatile("nop; nop" : : : "memory");
vstr = fy_string("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_float_alloca(128.0);
vf = fy_float(128.0);
asm volatile("nop; nop" : : "r"(vf) : "memory");

return vf;
Expand All @@ -4235,6 +4235,21 @@ fy_generic do_x3(void)
return vf;
}

fy_generic do_x4(void)
{
fy_generic vstr;
const char *str;

asm volatile("nop; nop" : : : "memory");
vstr = fy_string("test");
asm volatile("nop; nop" : : "r"(vstr) : "memory");
str = fy_generic_get_string(vstr);
asm volatile("nop; nop" : : "r"(str) : "memory");

return vstr;
}


char *testing_export = "This is export";

int do_generics(int argc, char *argv[], const char *allocator)
Expand Down Expand Up @@ -4263,7 +4278,6 @@ int do_generics(int argc, char *argv[], const char *allocator)
"invoice",
};
const char *sv;
char sinplace[FYGT_STRING_INPLACE_BUF];
size_t slen;
static const double ftable[] = {
0.0, 1.0, -1.0, 0.1, -0.1,
Expand Down Expand Up @@ -4347,79 +4361,79 @@ int do_generics(int argc, char *argv[], const char *allocator)
#endif
}

printf("testing alloca methods\n");
printf("testing generic methods\n");
printf("null = %016lx\n", fy_null);
for (i = 0; i < ARRAY_SIZE(btable); i++) {
bv = btable[i];
gbl = fy_bool_alloca(bv);
gbl = fy_bool(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_int_alloca(iv);
gi = fy_int(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_string_alloca(sv);
gs = fy_string(sv);
printf("string/%s = %016lx", sv, gs);

sv = fy_generic_get_string_size(gs, sinplace, &slen);
sv = fy_generic_get_string_size(gs, &slen);
assert(sv);
printf(" %.*s\n", (int)slen, sv);
}

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

seq = fy_sequence_alloca(3, ((fy_generic[]){
fy_bool_alloca(true),
fy_int_alloca(100),
fy_string_alloca("info")}));
fy_bool(true),
fy_int(100),
fy_string("info")}));
assert(seq != fy_invalid);

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

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}));
fy_string("foo"), fy_string("bar"),
fy_string("frooz-larger"), fy_string("what"),
fy_string("seq"), seq}));

assert(map != fy_invalid);

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

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

map = fy_mapping_alloca(2, ((fy_generic[]){
fy_string_alloca("foo"), fy_string_alloca("bar"),
fy_string("foo"), fy_string("bar"),
fy_sequence_alloca(2, ((fy_generic[]){
fy_int_alloca(10),
fy_int_alloca(100)})),
fy_float_alloca(3.14)}));
fy_int(10),
fy_int(100)})),
fy_float(3.14)}));

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

gv = fy_generic_mapping_lookup(map,
fy_sequence_alloca(2, ((fy_generic[]){
fy_int_alloca(10),
fy_int_alloca(100)})));
fy_int(10),
fy_int(100)})));
printf("found: ");
fy_generic_print_primitive(stdout, gv);
printf("\n");
Expand Down Expand Up @@ -4493,7 +4507,7 @@ int do_generics(int argc, char *argv[], const char *allocator)
fy_generic vstr;

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

printf("vstr=0x%08lx\n", (unsigned long)vstr);
Expand Down Expand Up @@ -4528,9 +4542,13 @@ int do_generics(int argc, char *argv[], const char *allocator)
gs = fy_generic_string_create(gb, sv);
printf("string/%s = %016lx", sv, gs);

sv = fy_generic_get_string_size(gs, sinplace, &slen);
sv = fy_generic_get_string_size(gs, &slen);
assert(sv);
printf(" %.*s\n", (int)slen, sv);

sv = fy_generic_get_string(gs);
assert(sv);
printf("\t%s\n", sv);
}

for (i = 0; i < ARRAY_SIZE(sztable); i++) {
Expand Down
3 changes: 1 addition & 2 deletions src/util/fy-generic-decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,8 +526,7 @@ fy_generic_compose_process_event(struct fy_parser *fyp, struct fy_event *fye, st
fyp_error_check(fyp, anchor, err_out, "fy_token_get_text() failed");

if (gd->resolve) {
v = fy_generic_decoder_alias_resolve(gd,
fy_string_size_alloca(anchor, anchor_size));
v = fy_generic_decoder_alias_resolve(gd, fy_string_size(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
38 changes: 20 additions & 18 deletions src/util/fy-generic-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int fy_encode_generic_string(struct fy_generic_encoder *fyge, const char *anchor
const char *str;
size_t len;

str = fy_generic_get_string_size_alloca(v, &len);
str = fy_generic_get_string_size(v, &len);
return fy_emit_scalar_write(fyge->emit, FYSS_ANY, anchor, tag, str, len);
}

Expand Down Expand Up @@ -162,7 +162,7 @@ int fy_encode_generic_mapping(struct fy_generic_encoder *fyge, const char *ancho

int fy_encode_generic_alias(struct fy_generic_encoder *fyge, fy_generic v)
{
return fy_emit_eventf(fyge->emit, FYET_ALIAS, fy_generic_get_alias_alloca(v));
return fy_emit_eventf(fyge->emit, FYET_ALIAS, fy_generic_get_alias(v));
}

int fy_encode_generic(struct fy_generic_encoder *fyge, fy_generic v)
Expand All @@ -173,9 +173,9 @@ int fy_encode_generic(struct fy_generic_encoder *fyge, fy_generic v)
if (fy_generic_is_indirect(v)) {
fy_generic_indirect_get(v, &gi);
if (fy_generic_get_type(gi.anchor) == FYGT_STRING)
anchor = fy_generic_get_string_alloca(gi.anchor);
anchor = fy_generic_get_string(gi.anchor);
if (fy_generic_get_type(gi.tag) == FYGT_STRING)
tag = fy_generic_get_string_alloca(gi.tag);
tag = fy_generic_get_string(gi.tag);
}

switch (fy_generic_get_type(v)) {
Expand Down Expand Up @@ -214,7 +214,7 @@ int fy_encode_generic(struct fy_generic_encoder *fyge, fy_generic v)

int fy_generic_encoder_emit_document(struct fy_generic_encoder *fyge, fy_generic vroot, fy_generic vds)
{
fy_generic vtags, vversion, vhandle, vprefix;
fy_generic vtags, vversion, vhandle, vprefix, vmajor, vminor;
const fy_generic *items;
struct fy_version *vers = NULL, vers_local;
struct fy_tag **tags = NULL, *tag;
Expand All @@ -231,35 +231,37 @@ 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_string_alloca("version"));
vversion = fy_generic_mapping_lookup(vds, fy_string("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_string_alloca("major")));
vers->minor = fy_generic_get_int(fy_generic_mapping_lookup(vversion, fy_string_alloca("minor")));
vmajor = fy_generic_mapping_lookup(vversion, fy_string("major"));
vminor = fy_generic_mapping_lookup(vversion, fy_string("minor"));
vers->major = fy_generic_get_int(vmajor);
vers->minor = fy_generic_get_int(vminor);
}

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

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

tags = alloca((count + 1) * sizeof(*tags));
for (i = 0; i < count; i++) {
tag = alloca(sizeof(*tag));
tag->handle = fy_generic_get_string_alloca(fy_generic_mapping_lookup(items[i], vhandle));
tag->prefix = fy_generic_get_string_alloca(fy_generic_mapping_lookup(items[i], vprefix));
vhandle = fy_generic_mapping_lookup(items[i], fy_string("handle"));
vprefix = fy_generic_mapping_lookup(items[i], fy_string("prefix"));

tag->handle = fy_generic_get_string(vhandle);
tag->prefix = fy_generic_get_string(vprefix);

tags[i] = tag;
}
tags[i] = NULL;
}

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;
version_explicit = fy_generic_mapping_lookup(vds, fy_string("version-explicit")) == fy_true;
tags_explicit = fy_generic_mapping_lookup(vds, fy_string("tags-explicit")) == fy_true;

if (!version_explicit)
vers = NULL;
Expand Down Expand Up @@ -320,8 +322,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_string_alloca("root");
vdocs_key = fy_string_alloca("docs");
vroot_key = fy_string("root");
vdocs_key = fy_string("docs");

/* no documents? nothing to emit */
items = fy_generic_sequence_get_items(vdir, &count);
Expand Down
Loading

0 comments on commit 91ba558

Please sign in to comment.