Executor compila expressões puras; texto = TEXTO não lia além do fim
Traço novo no core: tb_type_block_set_pure (TB_PURE_NUM/LT/EQ/GT/AND/ OR/NOT/VAR). A library declara que o bloco é puro e qual a semântica; number e operators definem cada conta como uma função de doubles, que é ao mesmo tempo o eval do bloco e o que um executor compila. tb_editor_jit.c: ao montar a árvore do runtime, uma expressão de blocos puros vira um bloco só (Editor.Compiled), com nós nativos (double, variável resolvida para o slot), sem marcar/avaliar/copiar valor em cada nó. O que não é puro (item de lista, Meus Blocos...) entra como nó que avalia a árvore normal. TINYBLOCKS_NO_JIT=1 desliga. Render do Minecraft: 18 ms -> 12 ms, com a mesma imagem (checksum). O "=" e o "<" entre textos passavam do fim da string: o | 32 vinha antes do teste do fim, e "abc" = "ABC" dava falso. tests/vm_bench.c (make bench-vm BENCH=projeto.json) mede a VM sem janela; TINYBLOCKS_VM_MS troca a fatia de tempo da VM por quadro. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
146d58b7af
commit
630a1686e5
13 files changed
+694
-152
No files matched your search
@@ -126,7 +126,7 @@ static void draw_bump(TinyBlocks_RenderableBlock* rb, float x, float y, Col c, C
|
||||
|
||||
/* Converte o texto de um campo no valor esperado pelo slot.
|
||||
* Slots sem tipo (NONE) adivinham: inteiro, real ou texto. */
|
||||
static TinyBlocks_Value value_from_text(const char* raw, TinyBlocks_ValueKind k) {
|
||||
TinyBlocks_Value value_from_text(const char* raw, TinyBlocks_ValueKind k) {
|
||||
char* e;
|
||||
switch (k) {
|
||||
case TB_VALUE_INTEGER: {
|
||||
@@ -1262,7 +1262,7 @@ static void run_reset(Ed* ed) {
|
||||
tb_variables_reset_blocks();
|
||||
}
|
||||
|
||||
static void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
if (ed->run_n >= ed->run_cap) {
|
||||
int nc = ed->run_cap ? ed->run_cap*2 : 256;
|
||||
TinyBlocks_Block** p = realloc(ed->run_blocks, (size_t)nc * sizeof(*p));
|
||||
@@ -1272,16 +1272,20 @@ static void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
ed->run_blocks[ed->run_n++] = b;
|
||||
}
|
||||
|
||||
static TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b) {
|
||||
TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b) {
|
||||
if (!b->type && b->type_name[0]) b->type = tb_type_block_find(ed->rt, b->type_name);
|
||||
return b->type;
|
||||
}
|
||||
|
||||
/* Converte o bloco bi (e seus repórteres) em uma árvore de blocos do runtime */
|
||||
static TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (!t) return NULL;
|
||||
if (t->pure && t->pure != TB_PURE_VAR) { /* expressao pura: compila */
|
||||
TinyBlocks_Block* c = jit_build(ed, T, bi);
|
||||
if (c) return c;
|
||||
}
|
||||
if (ed->run_n >= MAX_RUN_BLOCKS) { run_abort(ed, "limite de blocos em execução atingido"); return NULL; }
|
||||
TinyBlocks_Block* r = tb_block_create(ed->rt, t);
|
||||
if (!r) return NULL;
|
||||
@@ -1315,7 +1319,7 @@ static void rt_cache_clear(Ed* ed) {
|
||||
}
|
||||
|
||||
/* Marca a arvore inteira como "suja" para ser reavaliada */
|
||||
static void rt_mark_dirty(TinyBlocks_Block* r, int depth) {
|
||||
void rt_mark_dirty(TinyBlocks_Block* r, int depth) {
|
||||
if (!r || depth > 64) return;
|
||||
r->dirty = true;
|
||||
for (uint32_t i = 0; i < r->input_count; i++)
|
||||
@@ -1393,6 +1397,15 @@ static bool step(Ed* ed) {
|
||||
* avanca as threads varias vezes no mesmo quadro ate todas pausarem ou
|
||||
* terminarem. O corpo de Meus Blocos roda no mesmo executor, sem pausas. */
|
||||
#define VM_FRAME_MS 12u
|
||||
/* TINYBLOCKS_VM_MS troca a fatia (profiling: uma fatia grande roda tudo num quadro) */
|
||||
static unsigned vm_frame_ms(void) {
|
||||
static unsigned ms = 0;
|
||||
if (!ms) {
|
||||
const char* e = getenv("TINYBLOCKS_VM_MS");
|
||||
ms = e && atoi(e) > 0 ? (unsigned)atoi(e) : VM_FRAME_MS;
|
||||
}
|
||||
return ms;
|
||||
}
|
||||
static unsigned now_ms(void);
|
||||
|
||||
static const TinyBlocks_FlowClass* frame_flow(Ed* ed, VmFrame* f) {
|
||||
@@ -1577,7 +1590,7 @@ static void vm_frame(Ed* ed) {
|
||||
else { tb_vars_scope_set(NULL); tb_vars_scope_free(th->scope); th->scope = NULL; }
|
||||
tb_vars_scope_set(NULL);
|
||||
}
|
||||
} while (alive && !ed->abort && !ed->redraw_req && now_ms() - t0 < VM_FRAME_MS);
|
||||
} while (alive && !ed->abort && !ed->redraw_req && now_ms() - t0 < vm_frame_ms());
|
||||
cap_end(ed);
|
||||
if (!alive || ed->abort) {
|
||||
run_end(ed);
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* tb_editor_jit.c - compilador de expressoes do executor do editor.
|
||||
*
|
||||
* Uma expressao feita de blocos puros (os que a library declara com
|
||||
* tb_type_block_set_pure: contas, comparacoes, logica, "valor de variavel")
|
||||
* vira um bloco so, "Editor.Compiled", com a expressao numa arvore compacta
|
||||
* de nos nativos: numeros em double, variaveis resolvidas uma vez para o
|
||||
* slot (TbVarRef), sem marcar/avaliar/copiar valor em cada no.
|
||||
*
|
||||
* O que nao e puro (item de lista, pergunta, "Meus Blocos"...) entra como no
|
||||
* GEN: a arvore normal do runtime daquele bloco, avaliada como antes. A
|
||||
* semantica de cada no e a declarada pelo traco TB_PURE_* (tb_type_block.h).
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_vars.h>
|
||||
#include <tinyblocks/tb_value_class.h>
|
||||
#include "tb_editor_internal.h"
|
||||
|
||||
enum { J_CONST, J_VAR, J_GEN, J_NUM, J_CMP, J_AND, J_OR, J_NOT };
|
||||
|
||||
#define JIT_MAX_NODES 256
|
||||
#define JIT_MAX_ARGS 4
|
||||
|
||||
typedef struct {
|
||||
int op;
|
||||
int pure; /* J_CMP: TB_PURE_LT/EQ/GT */
|
||||
int arg[JIT_MAX_ARGS];
|
||||
int argc;
|
||||
double (*fn)(const double* x); /* J_NUM */
|
||||
double k; /* J_CONST: como numero */
|
||||
bool k_num; /* J_CONST: o valor e numero? */
|
||||
TinyBlocks_Value cval; /* J_CONST: valor original (owned) */
|
||||
TinyBlocks_Block* gen; /* J_GEN (dono: run_blocks do editor) */
|
||||
TbVarRef ref; /* J_VAR */
|
||||
char name[64]; /* J_VAR */
|
||||
} JNode;
|
||||
|
||||
typedef struct {
|
||||
TinyBlocks* rt;
|
||||
int n, root;
|
||||
JNode node[];
|
||||
} JProg;
|
||||
|
||||
typedef struct { /* compilacao em andamento */
|
||||
Ed* ed;
|
||||
Tab* T;
|
||||
JNode node[JIT_MAX_NODES];
|
||||
int n;
|
||||
bool fail;
|
||||
} JCtx;
|
||||
|
||||
/* -- Compilacao --- */
|
||||
|
||||
static int add_node(JCtx* c) {
|
||||
if (c->n >= JIT_MAX_NODES) { c->fail = true; return -1; }
|
||||
JNode* x = &c->node[c->n];
|
||||
memset(x, 0, sizeof(*x));
|
||||
return c->n++;
|
||||
}
|
||||
|
||||
static int compile_block(JCtx* c, int bi);
|
||||
|
||||
/* Entrada s do bloco bi: o bloco encaixado ou a constante do campo */
|
||||
static int compile_slot(JCtx* c, int bi, int s) {
|
||||
EdBlock* b = &c->T->ws[bi];
|
||||
if (b->args[s] >= 0) return compile_block(c, b->args[s]);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
JNode* x = &c->node[i];
|
||||
x->op = J_CONST;
|
||||
const char* raw = slot_src(b, s);
|
||||
x->cval = raw[0] ? value_from_text(raw, b->type->input_descs[s].expected_kind) : tb_value_none();
|
||||
double d;
|
||||
x->k_num = tb_value_to_number(x->cval, &d);
|
||||
x->k = x->k_num ? d : 0.0;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int compile_gen(JCtx* c, int bi) {
|
||||
TinyBlocks_Block* r = build_rt(c->ed, c->T, bi);
|
||||
if (!r || r->output_count == 0) { c->fail = true; return -1; }
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
c->node[i].op = J_GEN;
|
||||
c->node[i].gen = r;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int compile_block(JCtx* c, int bi) {
|
||||
if (c->fail) return -1;
|
||||
EdBlock* b = &c->T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(c->ed, b);
|
||||
if (!t) { c->fail = true; return -1; }
|
||||
int nin = (int)t->input_desc_count;
|
||||
switch (t->pure) {
|
||||
case TB_PURE_VAR: {
|
||||
if (nin < 1 || b->args[0] >= 0) return compile_gen(c, bi); /* nome calculado */
|
||||
const char* nm = slot_src(b, 0);
|
||||
if (!nm[0] || strlen(nm) >= sizeof(c->node[0].name)) return compile_gen(c, bi);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
c->node[i].op = J_VAR;
|
||||
strcpy(c->node[i].name, nm);
|
||||
c->node[i].ref.fixed = true; /* o nome fica no no: nunca muda */
|
||||
return i;
|
||||
}
|
||||
case TB_PURE_NUM:
|
||||
case TB_PURE_LT: case TB_PURE_EQ: case TB_PURE_GT:
|
||||
case TB_PURE_AND: case TB_PURE_OR: case TB_PURE_NOT: {
|
||||
int argc = t->pure == TB_PURE_NUM ? (int)t->pure_arity : t->pure == TB_PURE_NOT ? 1 : 2;
|
||||
if (argc > JIT_MAX_ARGS || argc > nin || (t->pure == TB_PURE_NUM && !t->pure_num))
|
||||
return compile_gen(c, bi);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
int a[JIT_MAX_ARGS];
|
||||
for (int k = 0; k < argc; k++) a[k] = compile_slot(c, bi, k);
|
||||
if (c->fail) return -1;
|
||||
JNode* x = &c->node[i];
|
||||
x->argc = argc;
|
||||
for (int k = 0; k < argc; k++) x->arg[k] = a[k];
|
||||
x->pure = t->pure;
|
||||
x->fn = t->pure_num;
|
||||
x->op = t->pure == TB_PURE_NUM ? J_NUM : t->pure == TB_PURE_AND ? J_AND
|
||||
: t->pure == TB_PURE_OR ? J_OR : t->pure == TB_PURE_NOT ? J_NOT : J_CMP;
|
||||
return i;
|
||||
}
|
||||
default:
|
||||
return compile_gen(c, bi);
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Execucao --- */
|
||||
|
||||
static double jnum(JProg* P, int i);
|
||||
|
||||
/* Bloco nao compilado: reavalia a arvore dele (como o executor faria) */
|
||||
static TinyBlocks_Value gen_eval(JProg* P, JNode* x) {
|
||||
rt_mark_dirty(x->gen, 0);
|
||||
tb_block_eval(x->gen, P->rt);
|
||||
return x->gen->outputs[0];
|
||||
}
|
||||
|
||||
/* Valor (emprestado) de um no; contas viram numero em *tmp */
|
||||
static TinyBlocks_Value jval(JProg* P, int i, TinyBlocks_Value* tmp) {
|
||||
JNode* x = &P->node[i];
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->cval;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
return v ? *v : tb_value_integer(0);
|
||||
}
|
||||
case J_GEN: return gen_eval(P, x);
|
||||
default: {
|
||||
double d = jnum(P, i);
|
||||
*tmp = (d == floor(d) && fabs(d) < 9e15) ? tb_value_integer((int64_t)d) : tb_value_real(d);
|
||||
return *tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Texto sem diferenciar maiusculas (ASCII), como o "=" do Scratch */
|
||||
static int icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
int ca = tolower((unsigned char)*a), cb = tolower((unsigned char)*b);
|
||||
if (ca != cb || !ca) return (ca > cb) - (ca < cb);
|
||||
}
|
||||
}
|
||||
|
||||
static int jcmp(JProg* P, JNode* x) {
|
||||
TinyBlocks_Value ta, tb2;
|
||||
TinyBlocks_Value a = jval(P, x->arg[0], &ta);
|
||||
double p, q;
|
||||
bool an = tb_value_to_number(a, &p);
|
||||
char sa[256] = "";
|
||||
if (!an) tb_value_to_text(a, sa, sizeof(sa)); /* antes de avaliar b (pode reusar a saida) */
|
||||
TinyBlocks_Value b = jval(P, x->arg[1], &tb2);
|
||||
bool bn = tb_value_to_number(b, &q);
|
||||
if (an && bn) return (p > q) - (p < q);
|
||||
char sb[256];
|
||||
if (an) tb_value_to_text(a, sa, sizeof(sa));
|
||||
tb_value_to_text(b, sb, sizeof(sb));
|
||||
return icmp(sa, sb);
|
||||
}
|
||||
|
||||
static bool jtruth(JProg* P, int i) {
|
||||
JNode* x = &P->node[i];
|
||||
if (x->op == J_CONST || x->op == J_VAR || x->op == J_GEN) {
|
||||
TinyBlocks_Value t;
|
||||
return tb_value_truthy(jval(P, i, &t));
|
||||
}
|
||||
return jnum(P, i) != 0.0;
|
||||
}
|
||||
|
||||
static double jnum(JProg* P, int i) {
|
||||
JNode* x = &P->node[i];
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->k;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
if (!v) return 0.0;
|
||||
if (v->kind == TB_VALUE_INTEGER) return (double)v->as.integer;
|
||||
if (v->kind == TB_VALUE_REAL) return v->as.real;
|
||||
return tb_value_as_number(*v, 0);
|
||||
}
|
||||
case J_GEN: return tb_value_as_number(gen_eval(P, x), 0);
|
||||
case J_NUM: {
|
||||
double a[JIT_MAX_ARGS];
|
||||
for (int k = 0; k < x->argc; k++) a[k] = jnum(P, x->arg[k]);
|
||||
return x->fn(a);
|
||||
}
|
||||
case J_CMP: {
|
||||
int r = jcmp(P, x);
|
||||
return (x->pure == TB_PURE_LT ? r < 0 : x->pure == TB_PURE_EQ ? r == 0 : r > 0) ? 1.0 : 0.0;
|
||||
}
|
||||
case J_AND: { bool a = jtruth(P, x->arg[0]), b = jtruth(P, x->arg[1]); return a && b; }
|
||||
case J_OR: { bool a = jtruth(P, x->arg[0]), b = jtruth(P, x->arg[1]); return a || b; }
|
||||
case J_NOT: return !jtruth(P, x->arg[0]);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/* -- Bloco "Editor.Compiled" --- */
|
||||
|
||||
static void eval_compiled(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
JProg* P = self->userdata;
|
||||
double d = P ? jnum(P, P->root) : 0.0;
|
||||
if (isfinite(d) && d == floor(d) && fabs(d) < 9e15) tb_block_set_output(self, 0, tb_value_integer((int64_t)d));
|
||||
else tb_block_set_output(self, 0, tb_value_real(isfinite(d) ? d : 0.0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void deinit_compiled(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
JProg* P = self->userdata;
|
||||
if (!P) return;
|
||||
for (int i = 0; i < P->n; i++)
|
||||
if (P->node[i].op == J_CONST) tb_value_free(&P->node[i].cval);
|
||||
}
|
||||
|
||||
static TinyBlocks_TypeBlock* compiled_type(TinyBlocks* rt) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_find(rt, "Editor.Compiled");
|
||||
if (t) return t;
|
||||
static const TinyBlocks_SlotDesc out[] = {{"valor", TB_VALUE_NONE}};
|
||||
t = tb_type_block_create(rt, "Editor.Compiled", NULL, 0, out, 1, eval_compiled);
|
||||
if (!t) return NULL;
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
tb_type_block_set_instance_hooks(t, 0, NULL, deinit_compiled);
|
||||
return t;
|
||||
}
|
||||
|
||||
TinyBlocks_Block* jit_build(Ed* ed, Tab* T, int bi) {
|
||||
const char* off = getenv("TINYBLOCKS_NO_JIT");
|
||||
if (off && off[0] && off[0] != '0') return NULL;
|
||||
TinyBlocks_TypeBlock* ct = compiled_type(ed->rt);
|
||||
if (!ct) return NULL;
|
||||
JCtx* c = calloc(1, sizeof(JCtx));
|
||||
if (!c) return NULL;
|
||||
c->ed = ed; c->T = T;
|
||||
int root = compile_block(c, bi);
|
||||
TinyBlocks_Block* r = NULL;
|
||||
if (!c->fail && root >= 0 && c->node[root].op != J_GEN && c->node[root].op != J_CONST) {
|
||||
r = tb_block_create(ed->rt, ct);
|
||||
JProg* P = r ? tb_alloc(ed->rt, sizeof(JProg) + (size_t)c->n * sizeof(JNode)) : NULL;
|
||||
if (P) {
|
||||
P->rt = ed->rt;
|
||||
P->n = c->n;
|
||||
P->root = root;
|
||||
memcpy(P->node, c->node, (size_t)c->n * sizeof(JNode));
|
||||
r->userdata = P;
|
||||
run_track(ed, r);
|
||||
c->n = 0; /* os valores agora sao do P */
|
||||
} else if (r) {
|
||||
tb_block_destroy(r, ed->rt);
|
||||
r = NULL;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < c->n; i++) if (c->node[i].op == J_CONST) tb_value_free(&c->node[i].cval);
|
||||
free(c);
|
||||
return r;
|
||||
}
|
||||
Reference in new issue
Block a user