Berry solidify to file

This commit is contained in:
Stephan Hadinger 2022-07-02 21:46:02 +02:00
parent a04687b314
commit 73dd995c3b
3 changed files with 73 additions and 106 deletions

View File

@ -15,6 +15,7 @@
#include "be_strlib.h"
#include "be_exec.h"
#include "be_mem.h"
#include "be_sys.h"
#include <stdio.h>
#include <string.h>
@ -46,13 +47,17 @@ static const char* opc2str(bopcode op)
return op < array_count(opc_tab) ? opc_tab[op] : "ERROP";
}
void be_print_inst(binstruction ins, int pc)
void be_print_inst(binstruction ins, int pc, void* fout)
{
char __lbuf[INST_BUF_SIZE];
bopcode op = IGET_OP(ins);
logbuf(" %.4X ", pc);
be_writestring(__lbuf);
if (fout) {
be_fwrite(fout, __lbuf, strlen(__lbuf));
} else {
be_writestring(__lbuf);
}
switch (op) {
case OP_ADD: case OP_SUB: case OP_MUL: case OP_DIV:
case OP_MOD: case OP_LT: case OP_LE: case OP_EQ:
@ -132,8 +137,12 @@ void be_print_inst(binstruction ins, int pc)
logbuf("%s", opc2str(op));
break;
}
be_writestring(__lbuf);
be_writenewline();
logbuf("%s\n", __lbuf);
if (fout) {
be_fwrite(fout, __lbuf, strlen(__lbuf));
} else {
be_writestring(__lbuf);
}
}
#endif
@ -159,7 +168,7 @@ void be_dumpclosure(bclosure *cl)
logfmt("; line %d\n", (++lineinfo)->linenumber);
}
#endif
be_print_inst(*code++, pc);
be_print_inst(*code++, pc, NULL);
}
}
#endif

View File

@ -22,7 +22,7 @@ bbool be_debug_varname(bvm *vm, int level, int index);
bbool be_debug_upvname(bvm *vm, int level, int index);
#if BE_USE_DEBUG_MODULE
void be_print_inst(binstruction ins, int pc);
void be_print_inst(binstruction ins, int pc, void* fout);
#endif
#endif

View File

@ -15,6 +15,7 @@
#include "be_map.h"
#include "be_vm.h"
#include "be_decoder.h"
#include "be_sys.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
@ -25,17 +26,22 @@ extern const bclass be_class_map;
#if BE_USE_SOLIDIFY_MODULE
#include <inttypes.h>
#define be_builtin_count(vm) \
be_vector_count(&(vm)->gbldesc.builtin.vlist)
#ifndef INST_BUF_SIZE
#define INST_BUF_SIZE 288
#endif
#define logbuf(...) snprintf(__lbuf, sizeof(__lbuf), __VA_ARGS__)
#define logfmt(...) \
do { \
char __lbuf[INST_BUF_SIZE]; \
logbuf(__VA_ARGS__); \
be_writestring(__lbuf); \
#define logfmt(...) \
do { \
char __lbuf[INST_BUF_SIZE]; \
snprintf(__lbuf, sizeof(__lbuf), __VA_ARGS__); \
if (fout) { \
be_fwrite(fout, __lbuf, strlen(__lbuf)); \
} else { \
be_writestring(__lbuf); \
} \
} while (0)
/********************************************************************\
@ -94,63 +100,9 @@ static void toidentifier(char *to, const char *p)
*to = 0; // final NULL
}
static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const char *classname, const char *key, void* fout);
/********************************************************************\
* Encode string as literals with simple rules
*
* Encode specifically \\, \n, \"
* All other characters outside of 0x20-0x7F is escaped with \x..
\********************************************************************/
static unsigned toliteral_length(const char *s)
{
unsigned len = 1;
const char * p = s;
while (*p) {
if (*p == '\n' || *p == '\\' || *p == '"') {
len += 2;
} else if (*p >= 0x20 && (*p & 0x80) == 0) {
len++;
} else {
len += 4; /* encode as \x.. */
}
p++;
}
return len;
}
static void toliteral(char *to, const char *p)
{
while (*p) {
if (*p == '\n') {
to[0] = '\\';
to[1] = 'n';
to += 2;
} else if (*p == '\\') {
to[0] = '\\';
to[1] = '\\';
to += 2;
} else if (*p == '"') {
to[0] = '\\';
to[1] = '"';
to += 2;
} else if (*p >= 0x20 && (*p & 0x80) == 0) {
*to = *p;
to++;
} else {
to[0] = '\\';
to[1] = 'x';
to[2] = hexdigit((*p & 0xF0) >> 4);
to[3] = hexdigit(*p & 0x0F);
to += 4;
}
p++;
}
*to = 0; // final NULL
}
static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const char *classname, const char *key);
static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *class_name)
static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *class_name, void* fout)
{
// compact first
be_map_compact(vm, map);
@ -178,10 +130,10 @@ static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *c
} else {
logfmt(" { be_const_key_weak(%s, %i), ", id_buf, key_next);
}
m_solidify_bvalue(vm, str_literal, &node->value, class_name, str(node->key.v.s));
m_solidify_bvalue(vm, str_literal, &node->value, class_name, str(node->key.v.s), fout);
} else if (node->key.type == BE_INT) {
logfmt(" { be_const_key_int(%i, %i), ", node->key.v.i, key_next);
m_solidify_bvalue(vm, str_literal, &node->value, class_name, NULL);
logfmt(" { be_const_key_int(%lli, %i), ", node->key.v.i, key_next);
m_solidify_bvalue(vm, str_literal, &node->value, class_name, NULL, fout);
} else {
char error[64];
snprintf(error, sizeof(error), "Unsupported type in key: %i", node->key.type);
@ -194,21 +146,21 @@ static void m_solidify_map(bvm *vm, bbool str_literal, bmap * map, const char *c
}
static void m_solidify_list(bvm *vm, bbool str_literal, blist * list, const char *class_name)
static void m_solidify_list(bvm *vm, bbool str_literal, blist * list, const char *class_name, void* fout)
{
logfmt(" be_nested_list(%i,\n", list->count);
logfmt(" ( (struct bvalue*) &(const bvalue[]) {\n");
for (int i = 0; i < list->count; i++) {
logfmt(" ");
m_solidify_bvalue(vm, str_literal, &list->data[i], class_name, "");
m_solidify_bvalue(vm, str_literal, &list->data[i], class_name, "", fout);
logfmt(",\n");
}
logfmt(" }))"); // TODO need terminal comma?
}
// pass key name in case of class, or NULL if none
static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const char *classname, const char *key)
static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const char *classname, const char *key, void* fout)
{
int type = var_primetype(value);
switch (type) {
@ -297,16 +249,16 @@ static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const
} else {
logfmt(" be_const_list( * ");
}
m_solidify_bvalue(vm, str_literal, &ins->members[0], classname, key);
m_solidify_bvalue(vm, str_literal, &ins->members[0], classname, key, fout);
logfmt(" ) } ))");
}
}
break;
case BE_MAP:
m_solidify_map(vm, str_literal, (bmap *) var_toobj(value), classname);
m_solidify_map(vm, str_literal, (bmap *) var_toobj(value), classname, fout);
break;
case BE_LIST:
m_solidify_list(vm, str_literal, (blist *) var_toobj(value), classname);
m_solidify_list(vm, str_literal, (blist *) var_toobj(value), classname, fout);
break;
default:
{
@ -317,24 +269,24 @@ static void m_solidify_bvalue(bvm *vm, bbool str_literal, bvalue * value, const
}
}
static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, int builtins);
static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, void* fout);
/* solidify any inner class */
static void m_solidify_proto_inner_class(bvm *vm, bbool str_literal, bproto *pr, int builtins)
static void m_solidify_proto_inner_class(bvm *vm, bbool str_literal, bproto *pr, void* fout)
{
// parse any class in constants to output it first
if (pr->nconst > 0) {
for (int k = 0; k < pr->nconst; k++) {
if (var_type(&pr->ktab[k]) == BE_CLASS) {
// output the class
m_solidify_subclass(vm, str_literal, (bclass*) var_toobj(&pr->ktab[k]), builtins);
m_solidify_subclass(vm, str_literal, (bclass*) var_toobj(&pr->ktab[k]), fout);
}
}
}
}
static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char * func_name, int builtins, int indent)
static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char * func_name, int indent, void* fout)
{
// const char * func_name = str(pr->name);
// const char * func_source = str(pr->source);
@ -364,7 +316,7 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char
size_t sub_len = strlen(func_name) + 10;
char sub_name[sub_len];
snprintf(sub_name, sizeof(sub_name), "%s_%d", func_name, i);
m_solidify_proto(vm, str_literal, pr->ptab[i], sub_name, builtins, indent+2);
m_solidify_proto(vm, str_literal, pr->ptab[i], sub_name, indent+2, fout);
logfmt(",\n");
}
logfmt("%*s}),\n", indent, "");
@ -377,7 +329,7 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char
logfmt("%*s( &(const bvalue[%2d]) { /* constants */\n", indent, "", pr->nconst);
for (int k = 0; k < pr->nconst; k++) {
logfmt("%*s/* K%-3d */ ", indent, "", k);
m_solidify_bvalue(vm, str_literal, &pr->ktab[k], NULL, NULL);
m_solidify_bvalue(vm, str_literal, &pr->ktab[k], NULL, NULL, fout);
logfmt(",\n");
}
logfmt("%*s}),\n", indent, "");
@ -402,12 +354,12 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char
for (int pc = 0; pc < pr->codesize; pc++) {
uint32_t ins = pr->code[pc];
logfmt("%*s 0x%08X, //", indent, "", ins);
be_print_inst(ins, pc);
be_print_inst(ins, pc, fout);
bopcode op = IGET_OP(ins);
if (op == OP_GETGBL || op == OP_SETGBL) {
// check if the global is in built-ins
int glb = IGET_Bx(ins);
if (glb > builtins) {
if (glb > be_builtin_count(vm)) {
// not supported
logfmt("\n===== unsupported global G%d\n", glb);
be_raise(vm, "internal_error", "Unsupported access to non-builtin global");
@ -420,7 +372,7 @@ static void m_solidify_proto(bvm *vm, bbool str_literal, bproto *pr, const char
}
static void m_solidify_closure(bvm *vm, bbool str_literal, bclosure *cl, const char * classname, int builtins)
static void m_solidify_closure(bvm *vm, bbool str_literal, bclosure *cl, const char * classname, void* fout)
{
bproto *pr = cl->proto;
const char * func_name = str(pr->name);
@ -432,7 +384,7 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, bclosure *cl, const c
int indent = 2;
m_solidify_proto_inner_class(vm, str_literal, pr, builtins);
m_solidify_proto_inner_class(vm, str_literal, pr, fout);
logfmt("\n");
logfmt("/********************************************************************\n");
@ -448,7 +400,7 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, bclosure *cl, const c
func_name_id);
}
m_solidify_proto(vm, str_literal, pr, func_name, builtins, indent);
m_solidify_proto(vm, str_literal, pr, func_name, indent, fout);
logfmt("\n");
// closure
@ -456,7 +408,7 @@ static void m_solidify_closure(bvm *vm, bbool str_literal, bclosure *cl, const c
logfmt("/*******************************************************************/\n\n");
}
static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, int builtins)
static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, void* fout)
{
const char * class_name = str(cl->name);
@ -467,7 +419,7 @@ static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, int buil
while ((node = be_map_next(cl->members, &iter)) != NULL) {
if (var_isstr(&node->key) && var_isclosure(&node->value)) {
bclosure *f = var_toobj(&node->value);
m_solidify_closure(vm, str_literal, f, class_name, builtins);
m_solidify_closure(vm, str_literal, f, class_name, fout);
}
}
}
@ -491,7 +443,7 @@ static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, int buil
}
if (cl->members) {
m_solidify_map(vm, str_literal, cl->members, class_name);
m_solidify_map(vm, str_literal, cl->members, class_name, fout);
logfmt(",\n");
} else {
logfmt(" NULL,\n");
@ -509,10 +461,10 @@ static void m_solidify_subclass(bvm *vm, bbool str_literal, bclass *cl, int buil
}
static void m_solidify_class(bvm *vm, bbool str_literal, bclass *cl, int builtins)
static void m_solidify_class(bvm *vm, bbool str_literal, bclass *cl, void* fout)
{
const char * class_name = str(cl->name);
m_solidify_subclass(vm, str_literal, cl, builtins);
m_solidify_subclass(vm, str_literal, cl, fout);
logfmt("/*******************************************************************/\n\n");
logfmt("void be_load_%s_class(bvm *vm) {\n", class_name);
@ -522,7 +474,7 @@ static void m_solidify_class(bvm *vm, bbool str_literal, bclass *cl, int builtin
logfmt("}\n");
}
static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, int builtins)
static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, void* fout)
{
const char * module_name = be_module_name(ml);
if (!module_name) { module_name = ""; }
@ -534,11 +486,11 @@ static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, int built
while ((node = be_map_next(ml->table, &iter)) != NULL) {
if (var_isstr(&node->key) && var_isclosure(&node->value)) {
bclosure *f = var_toobj(&node->value);
m_solidify_closure(vm, str_literal, f, module_name, builtins);
m_solidify_closure(vm, str_literal, f, module_name, fout);
}
if (var_isstr(&node->key) && var_isclass(&node->value)) {
bclass *cl = var_toobj(&node->value);
m_solidify_subclass(vm, str_literal, cl, builtins);
m_solidify_subclass(vm, str_literal, cl, fout);
}
}
}
@ -553,7 +505,7 @@ static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, int built
logfmt(" \"%s\",\n", module_name);
if (ml->table) {
m_solidify_map(vm, str_literal, ml->table, module_name);
m_solidify_map(vm, str_literal, ml->table, module_name, fout);
logfmt("\n");
} else {
logfmt(" NULL,\n");
@ -564,23 +516,29 @@ static void m_solidify_module(bvm *vm, bbool str_literal, bmodule *ml, int built
}
#define be_builtin_count(vm) \
be_vector_count(&(vm)->gbldesc.builtin.vlist)
static int m_dump(bvm *vm)
{
if (be_top(vm) >= 1) {
int top = be_top(vm);
if (top >= 1) {
bvalue *v = be_indexof(vm, 1);
bbool str_literal = bfalse;
if (be_top(vm) >= 2) {
if (top >= 2) {
str_literal = be_tobool(vm, 2);
}
void* fout = NULL; /* output file */
if (top >= 3 && be_isinstance(vm, 3)) {
be_getmember(vm, 3, ".p");
if (be_iscomptr(vm, -1)) {
fout = be_tocomptr(vm, -1);
}
be_pop(vm, 1);
}
if (var_isclosure(v)) {
m_solidify_closure(vm, str_literal, var_toobj(v), NULL, be_builtin_count(vm));
m_solidify_closure(vm, str_literal, var_toobj(v), NULL, fout);
} else if (var_isclass(v)) {
m_solidify_class(vm, str_literal, var_toobj(v), be_builtin_count(vm));
m_solidify_class(vm, str_literal, var_toobj(v), fout);
} else if (var_ismodule(v)) {
m_solidify_module(vm, str_literal, var_toobj(v), be_builtin_count(vm));
m_solidify_module(vm, str_literal, var_toobj(v), fout);
} else {
be_raise(vm, "value_error", "unsupported type");
}