Berry improve solidification of bytes (#21751)

This commit is contained in:
s-hadinger 2024-07-09 13:19:24 +02:00 committed by GitHub
parent a3048a0b25
commit b56e3c43a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 60 additions and 47 deletions

View File

@ -28,23 +28,38 @@ extern "C" {
.type = (_t), \ .type = (_t), \
.marked = GC_CONST .marked = GC_CONST
#define be_define_const_bytes(_name, ...) \ #define be_define_const_bytes(_name, ...) \
const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ } const binstance_arg3 be_const_instance_##_name = { \
be_const_header(BE_INSTANCE), \
.super = NULL, \
.sub = NULL, \
._class = (bclass*) &be_class_bytes, \
.members = { \
{.v.c = (const void*) & (const uint8_t[]) { __VA_ARGS__ }, \
.type = BE_COMPTR }, \
be_const_int(sizeof(#_name) / 2), \
be_const_int(BYTES_SIZE_SOLIDIFIED) \
} \
}
#define be_const_bytes_instance(_bytes) { \ /* special version to define a default empty bytes */
.v.c = ( \ #define be_define_const_bytes_empty() \
& (const binstance_arg3) { \ const binstance_arg3 be_const_instance_ = { \
be_const_header(BE_INSTANCE), \ be_const_header(BE_INSTANCE), \
.super = NULL, \ .super = NULL, \
.sub = NULL, \ .sub = NULL, \
._class = (bclass*) &be_class_bytes, \ ._class = (bclass*) &be_class_bytes, \
.members = { \ .members = { \
be_const_comptr(&be_const_bin_##_bytes), \ {.v.c = (const void*) & (const uint8_t[]) { 0x00 }, \
be_const_int(sizeof(#_bytes) / 2), \ .type = BE_COMPTR }, \
be_const_int(BYTES_SIZE_SOLIDIFIED) \ be_const_int(0), \
} \ be_const_int(BYTES_SIZE_SOLIDIFIED) \
}), \ } \
.type = BE_INSTANCE \ }
#define be_const_bytes_instance(_bytes) { \
.v.c = &be_const_instance_##_bytes, \
.type = BE_INSTANCE \
} }
#define be_define_const_str_weak(_name, _s, _len) \ #define be_define_const_str_weak(_name, _s, _len) \
@ -339,8 +354,8 @@ const bntvmodule_t be_native_module(_module) = { \
#else #else
#define be_define_const_bytes(_name, ...) \ // #define be_define_const_bytes(_name, ...) \
const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ } // const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ }
#define be_define_const_str_weak(_name, _s, _len) \ #define be_define_const_str_weak(_name, _s, _len) \
const bcstring be_const_str_##_name = { \ const bcstring be_const_str_##_name = { \

View File

@ -132,7 +132,7 @@ static const bclass *m_solidify_get_parentclass(const bproto *pr)
static void m_solidify_bvalue(bvm *vm, bbool str_literal, const bvalue * value, const char *prefixname, const char *key, void* fout); static void m_solidify_bvalue(bvm *vm, bbool str_literal, const bvalue * value, const char *prefixname, const char *key, void* fout);
static void m_solidify_map(bvm *vm, bbool str_literal, const bmap * map, const char *prefixname, void* fout) static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *prefixname, void* fout)
{ {
// compact first // compact first
be_map_compact(vm, map); be_map_compact(vm, map);
@ -288,34 +288,32 @@ static void m_solidify_bvalue(bvm *vm, bbool str_literal, const bvalue * value,
{ {
binstance * ins = (binstance *) var_toobj(value); binstance * ins = (binstance *) var_toobj(value);
bclass * cl = ins->_class; bclass * cl = ins->_class;
if (ins->super || ins->sub) {
if (cl == &be_class_bytes) {
const void * bufptr = var_toobj(&ins->members[0]);
int32_t len = var_toint(&ins->members[1]);
size_t hex_len = len * 2 + 1;
char * hex_out = be_pushbuffer(vm, hex_len);
be_bytes_tohex(hex_out, hex_len, bufptr, len);
logfmt("be_const_bytes_instance(%s)", hex_out);
be_pop(vm, 1);
} else if (ins->super || ins->sub) {
be_raise(vm, "internal_error", "instance must not have a super/sub class"); be_raise(vm, "internal_error", "instance must not have a super/sub class");
} else if ((cl != &be_class_map && cl != &be_class_list) || 1) { // TODO } else {
const char * cl_ptr = ""; const char * cl_ptr = "";
if (cl == &be_class_map) { cl_ptr = "map"; } if (cl == &be_class_map) { cl_ptr = "map"; }
else if (cl == &be_class_list) { cl_ptr = "list"; } else if (cl == &be_class_list) { cl_ptr = "list"; }
else if (cl == &be_class_bytes) { cl_ptr = "bytes"; }
else { be_raise(vm, "internal_error", "unsupported class"); } else { be_raise(vm, "internal_error", "unsupported class"); }
if (cl == &be_class_bytes) { logfmt("be_const_simple_instance(be_nested_simple_instance(&be_class_%s, {\n", cl_ptr);
const void * bufptr = var_toobj(&ins->members[0]); if (cl == &be_class_map) {
int32_t len = var_toint(&ins->members[1]); logfmt(" be_const_map( * ");
size_t hex_len = len * 2 + 1;
char * hex_out = be_pushbuffer(vm, hex_len);
be_bytes_tohex(hex_out, hex_len, bufptr, len);
logfmt("be_const_bytes_instance(%s)", hex_out);
be_pop(vm, 1);
} else { } else {
logfmt("be_const_simple_instance(be_nested_simple_instance(&be_class_%s, {\n", cl_ptr); logfmt(" be_const_list( * ");
if (cl == &be_class_map) {
logfmt(" be_const_map( * ");
} else {
logfmt(" be_const_list( * ");
}
m_solidify_bvalue(vm, str_literal, &ins->members[0], prefixname, key, fout);
logfmt(" ) } ))");
} }
m_solidify_bvalue(vm, str_literal, &ins->members[0], prefixname, key, fout);
logfmt(" ) } ))");
} }
} }
break; break;
@ -354,7 +352,7 @@ static void m_solidify_proto_inner_class(bvm *vm, bbool str_literal, const bprot
} }
} }
static void m_solidify_proto(bvm *vm, bbool str_literal, const bproto *pr, const char * func_name, const char *prefixname, int indent, void* fout) static void m_solidify_proto(bvm *vm, bbool str_literal, const bproto *pr, const char * func_name, int indent, void* fout)
{ {
/* get parent class name if any */ /* get parent class name if any */
const bclass *parentclass = m_solidify_get_parentclass(pr); const bclass *parentclass = m_solidify_get_parentclass(pr);
@ -386,7 +384,7 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, const bproto *pr, const
size_t sub_len = strlen(func_name) + 10; size_t sub_len = strlen(func_name) + 10;
char sub_name[sub_len]; char sub_name[sub_len];
snprintf(sub_name, sizeof(sub_name), "%s_%"PRId32, func_name, i); snprintf(sub_name, sizeof(sub_name), "%s_%"PRId32, func_name, i);
m_solidify_proto(vm, str_literal, pr->ptab[i], sub_name, NULL, indent+2, fout); m_solidify_proto(vm, str_literal, pr->ptab[i], sub_name, indent+2, fout);
logfmt(",\n"); logfmt(",\n");
} }
if (parentclass_name) { if (parentclass_name) {
@ -500,7 +498,7 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, const bclosure *clo,
func_name_id); func_name_id);
} }
m_solidify_proto(vm, str_literal, pr, func_name, prefixname, indent, fout); m_solidify_proto(vm, str_literal, pr, func_name, indent, fout);
logfmt("\n"); logfmt("\n");
// closure // closure
@ -564,12 +562,12 @@ static void m_solidify_subclass(bvm *vm, bbool str_literal, const bclass *cla, v
logfmt(");\n"); logfmt(");\n");
} }
static void m_solidify_class(bvm *vm, bbool str_literal, const bclass *cl, void* fout) static void m_solidify_class(bvm *vm, bbool str_literal, bclass *cl, void* fout)
{ {
m_solidify_subclass(vm, str_literal, cl, fout); m_solidify_subclass(vm, str_literal, cl, fout);
} }
static void m_solidify_module(bvm *vm, bbool str_literal, const bmodule *ml, void* fout) static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, void* fout)
{ {
const char * modulename = be_module_name(ml); const char * modulename = be_module_name(ml);
if (!modulename) { modulename = ""; } if (!modulename) { modulename = ""; }

View File

@ -23,7 +23,7 @@ class bytes_build:
def build_bytes_def(self): def build_bytes_def(self):
ostr = "" ostr = ""
ostr += "/* binary arrays */\n" ostr += "/* binary arrays */\n"
ostr += "be_define_const_bytes(,);\n" ostr += "be_define_const_bytes_empty();\n"
for k in self.map: for k in self.map:
ostr += "be_define_const_bytes(" ostr += "be_define_const_bytes("
ostr += k + ", " + ", ".join( [ "0x" + k[i:i+2] for i in range(0, len(k), 2)] ) ostr += k + ", " + ", ".join( [ "0x" + k[i:i+2] for i in range(0, len(k), 2)] )
@ -34,8 +34,8 @@ class bytes_build:
def build_bytes_ext(self): def build_bytes_ext(self):
ostr = "" ostr = ""
ostr += "/* extern binary arrays */\n" ostr += "/* extern binary arrays */\n"
ostr += "extern const uint8_t be_const_bin_[];\n" ostr += "extern const binstance_arg3 be_const_instance_;\n"
for k in self.map: for k in self.map:
ostr += "extern const uint8_t be_const_bin_" + k + "[];\n" ostr += "extern const binstance_arg3 be_const_instance_" + k + ";\n"
return ostr return ostr