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), \
.marked = GC_CONST
#define be_define_const_bytes(_name, ...) \
const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ }
#define be_define_const_bytes(_name, ...) \
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) { \
.v.c = ( \
& (const binstance_arg3) { \
be_const_header(BE_INSTANCE), \
.super = NULL, \
.sub = NULL, \
._class = (bclass*) &be_class_bytes, \
.members = { \
be_const_comptr(&be_const_bin_##_bytes), \
be_const_int(sizeof(#_bytes) / 2), \
be_const_int(BYTES_SIZE_SOLIDIFIED) \
} \
}), \
.type = BE_INSTANCE \
/* special version to define a default empty bytes */
#define be_define_const_bytes_empty() \
const binstance_arg3 be_const_instance_ = { \
be_const_header(BE_INSTANCE), \
.super = NULL, \
.sub = NULL, \
._class = (bclass*) &be_class_bytes, \
.members = { \
{.v.c = (const void*) & (const uint8_t[]) { 0x00 }, \
.type = BE_COMPTR }, \
be_const_int(0), \
be_const_int(BYTES_SIZE_SOLIDIFIED) \
} \
}
#define be_const_bytes_instance(_bytes) { \
.v.c = &be_const_instance_##_bytes, \
.type = BE_INSTANCE \
}
#define be_define_const_str_weak(_name, _s, _len) \
@ -339,8 +354,8 @@ const bntvmodule_t be_native_module(_module) = { \
#else
#define be_define_const_bytes(_name, ...) \
const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ }
// #define be_define_const_bytes(_name, ...) \
// const uint8_t be_const_bin_##_name[] = { __VA_ARGS__ }
#define be_define_const_str_weak(_name, _s, _len) \
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_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
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);
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");
} else if ((cl != &be_class_map && cl != &be_class_list) || 1) { // TODO
} else {
const char * cl_ptr = "";
if (cl == &be_class_map) { cl_ptr = "map"; }
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"); }
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);
logfmt("be_const_simple_instance(be_nested_simple_instance(&be_class_%s, {\n", cl_ptr);
if (cl == &be_class_map) {
logfmt(" be_const_map( * ");
} else {
logfmt("be_const_simple_instance(be_nested_simple_instance(&be_class_%s, {\n", cl_ptr);
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(" ) } ))");
logfmt(" be_const_list( * ");
}
m_solidify_bvalue(vm, str_literal, &ins->members[0], prefixname, key, fout);
logfmt(" ) } ))");
}
}
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 */
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;
char sub_name[sub_len];
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");
}
if (parentclass_name) {
@ -500,7 +498,7 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, const bclosure *clo,
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");
// closure
@ -564,12 +562,12 @@ static void m_solidify_subclass(bvm *vm, bbool str_literal, const bclass *cla, v
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);
}
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);
if (!modulename) { modulename = ""; }

View File

@ -23,7 +23,7 @@ class bytes_build:
def build_bytes_def(self):
ostr = ""
ostr += "/* binary arrays */\n"
ostr += "be_define_const_bytes(,);\n"
ostr += "be_define_const_bytes_empty();\n"
for k in self.map:
ostr += "be_define_const_bytes("
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):
ostr = ""
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:
ostr += "extern const uint8_t be_const_bin_" + k + "[];\n"
ostr += "extern const binstance_arg3 be_const_instance_" + k + ";\n"
return ostr