embedded-react 0.16.0 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot/compiler/assemble.mjs +143 -27
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +63 -8
- package/aot/compiler/c/nodes.mjs +15 -3
- package/aot/compiler/c/statements.mjs +34 -5
- package/aot/compiler/collect.mjs +50 -8
- package/aot/compiler/control-flow.mjs +19 -6
- package/aot/compiler/elements/dial.mjs +7 -4
- package/aot/compiler/expressions.mjs +104 -4
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +28 -4
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/lower.mjs +39 -10
- package/aot/compiler/nodes.mjs +6 -1
- package/aot/compiler/static-eval.mjs +280 -54
- package/aot/compiler/style-text.mjs +166 -7
- package/package.json +1 -1
|
@@ -25,8 +25,11 @@ import { printAnimationCallback, printDepEffectCheck, printEffectFunction, print
|
|
|
25
25
|
import { printKeyboard } from './c/keyboard.mjs';
|
|
26
26
|
import { printElementTree, printNode, printNodeUpdate } from './c/nodes.mjs';
|
|
27
27
|
import { printPanDecls, printPanQuery } from './c/pan-responder.mjs';
|
|
28
|
+
import { CHANGED_FLAGS, printChangedFlag } from './c/statements.mjs';
|
|
28
29
|
import { printVectorBlocks, printVectorRebuild } from './c/vectors.mjs';
|
|
29
|
-
import { isStateDriven, nodesOf, stateDrivenVectorOf } from './ir/nodes.mjs';
|
|
30
|
+
import { isStateDriven, nodesOf, stateDrivenVectorOf, updatedVectorOf, } from './ir/nodes.mjs';
|
|
31
|
+
import { nodeReads, vectorReads } from './ir/reads.mjs';
|
|
32
|
+
import { hasBoot, printBootDecls, printBootFunction } from './c/boot.mjs';
|
|
30
33
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
31
34
|
- Constants
|
|
32
35
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -36,6 +39,8 @@ const [PKG_MAJOR, PKG_MINOR] = PKG_VERSION.split('.');
|
|
|
36
39
|
// libm symbols, and the helpers that call them; any of them in the generated code means it includes <math.h>.
|
|
37
40
|
const LIBM_USE = new RegExp('\\b(sinf|cosf|tanf|sqrtf|fabsf|roundf|floorf|ceilf|fminf|fmaxf|atan2f|powf|fmodf|isfinite|' +
|
|
38
41
|
'app_roundf|app_ftoa|app_vector_dirty|M_PI)\\b');
|
|
42
|
+
// The double-precision libm symbols a value worked out at boot can call.
|
|
43
|
+
const BOOT_LIBM_USE = /\b(sin|cos|tan|sqrt|fabs|floor|ceil|fmin|fmax|atan2|pow|fmod|app_round)\(/;
|
|
39
44
|
/*----------------------------------------------------------------------------------------------------------------------
|
|
40
45
|
- Implementation
|
|
41
46
|
---------------------------------------------------------------------------------------------------------------------*/
|
|
@@ -60,6 +65,40 @@ export const demoMarker = (demo) => `ER_AOT_DEMO_${demo.replace(/[^A-Za-z0-9]/gu
|
|
|
60
65
|
* @returns The generated app.gen.c and app.gen.h, and what they contain.
|
|
61
66
|
*/
|
|
62
67
|
export const compileSourceImpl = (src, demo, opts, wide, stores) => writeApp(lowerApp(src, opts, wide, stores), demo, opts);
|
|
68
|
+
/**
|
|
69
|
+
* app_update()'s entries, each set again only when a state it reads changed. Consecutive entries that read the
|
|
70
|
+
* same state share one test; an entry that reads something untracked, or nothing, runs every time.
|
|
71
|
+
*
|
|
72
|
+
* @param entries Each node's or drawing's reads, and its C lines.
|
|
73
|
+
* @param stateRecords The app's state, in declaration order.
|
|
74
|
+
*
|
|
75
|
+
* @returns The C lines.
|
|
76
|
+
*/
|
|
77
|
+
function printGuardedUpdates(entries, stateRecords) {
|
|
78
|
+
const testOf = (reads) => reads?.size
|
|
79
|
+
? stateRecords
|
|
80
|
+
.filter(stateRecord => reads.has(stateRecord))
|
|
81
|
+
.map(stateRecord => `changed.${stateRecord.cField}`)
|
|
82
|
+
.join(' || ')
|
|
83
|
+
: null;
|
|
84
|
+
const lines = [];
|
|
85
|
+
let index = 0;
|
|
86
|
+
while (index < entries.length) {
|
|
87
|
+
const test = testOf(entries[index].reads);
|
|
88
|
+
if (test === null) {
|
|
89
|
+
lines.push(...entries[index].lines);
|
|
90
|
+
index++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
lines.push(` if (${test})`, ' {');
|
|
94
|
+
while (index < entries.length && testOf(entries[index].reads) === test) {
|
|
95
|
+
lines.push(...entries[index].lines.map(line => ' ' + line));
|
|
96
|
+
index++;
|
|
97
|
+
}
|
|
98
|
+
lines.push(' }');
|
|
99
|
+
}
|
|
100
|
+
return lines;
|
|
101
|
+
}
|
|
63
102
|
/**
|
|
64
103
|
* Writes a lowered app as C: file-scope state, declarations and functions, then er_app_build(), in
|
|
65
104
|
* app.gen.c, and the app's public entry points in app.gen.h.
|
|
@@ -105,11 +144,22 @@ export function writeApp(app, demo, opts) {
|
|
|
105
144
|
`{${scalarRecords.map(scalarRecord => ` .${scalarRecord.cField} = ${cConstant(scalarRecord.initial, scalarRecord.cType)}`).join(',')} ` +
|
|
106
145
|
`};\n`
|
|
107
146
|
: '';
|
|
108
|
-
|
|
147
|
+
// One flag per state a node or drawing reads, set by each write and cleared by app_update().
|
|
148
|
+
const watchedRecords = stateRecords.filter(stateRecord => stateRecord.isWatched);
|
|
149
|
+
const changedBlock = watchedRecords.length
|
|
150
|
+
? `typedef struct\n{\n${watchedRecords.map(stateRecord => ` uint8_t ${stateRecord.cField};`).join('\n')}\n} ` +
|
|
151
|
+
`ErAppChanged;\n\nstatic ErAppChanged ${CHANGED_FLAGS};\n`
|
|
152
|
+
: '';
|
|
153
|
+
const bootDecls = printBootDecls(app.boot);
|
|
154
|
+
const stateBlock = [scalarBlock, listBlocks, changedBlock, bootDecls]
|
|
155
|
+
.filter(Boolean)
|
|
156
|
+
.join('\n');
|
|
157
|
+
const bootFn = printBootFunction(app.boot, opts.screen);
|
|
109
158
|
// The nodes er_app_build() creates, each before its children, and what app_update() re-applies to them.
|
|
110
159
|
const nodes = nodesOf(app.root);
|
|
111
160
|
const buildLines = printElementTree(app.root).join('\n');
|
|
112
|
-
const
|
|
161
|
+
const updatedNodes = nodes.filter(isStateDriven);
|
|
162
|
+
const updates = updatedNodes.map(node => printNode(node).update);
|
|
113
163
|
// A file-scope handle for each node app_update() re-applies, and one for each drawing it rebuilds.
|
|
114
164
|
const handleDecls = nodes
|
|
115
165
|
.flatMap(node => [
|
|
@@ -151,16 +201,29 @@ export function writeApp(app, demo, opts) {
|
|
|
151
201
|
if (updates.length) {
|
|
152
202
|
lines.push(' ERProps p;');
|
|
153
203
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
// State-driven Svgs: recompute the op-tape from state and re-upload.
|
|
158
|
-
for (const node of nodes) {
|
|
159
|
-
const vector = stateDrivenVectorOf(node);
|
|
160
|
-
if (vector) {
|
|
161
|
-
lines.push(...printVectorRebuild(node.id, vector));
|
|
162
|
-
}
|
|
204
|
+
// A snapshot of the flags, cleared first, so state an effect below writes is applied on the next update.
|
|
205
|
+
if (watchedRecords.length) {
|
|
206
|
+
lines.push(` const ErAppChanged changed = ${CHANGED_FLAGS};`, ` memset(&${CHANGED_FLAGS}, 0, sizeof(${CHANGED_FLAGS}));`);
|
|
163
207
|
}
|
|
208
|
+
// Node props, then state-driven Svgs' op-tapes, each set again only when a state it reads changed.
|
|
209
|
+
const entries = [
|
|
210
|
+
...updatedNodes.map((node, index) => ({
|
|
211
|
+
reads: nodeReads(node),
|
|
212
|
+
lines: printNodeUpdate(updates[index]),
|
|
213
|
+
})),
|
|
214
|
+
...nodes.flatMap(node => {
|
|
215
|
+
const vector = updatedVectorOf(node);
|
|
216
|
+
return vector
|
|
217
|
+
? [
|
|
218
|
+
{
|
|
219
|
+
reads: vectorReads(vector),
|
|
220
|
+
lines: printVectorRebuild(node.id, vector),
|
|
221
|
+
},
|
|
222
|
+
]
|
|
223
|
+
: [];
|
|
224
|
+
}),
|
|
225
|
+
];
|
|
226
|
+
lines.push(...printGuardedUpdates(entries, stateRecords));
|
|
164
227
|
// Dep-driven useEffect: run each effect whose dependency value changed since the last app_update.
|
|
165
228
|
for (const effect of out.depEffects) {
|
|
166
229
|
lines.push(printDepEffectCheck(effect));
|
|
@@ -332,7 +395,9 @@ static int16_t app_round_dim(double v)
|
|
|
332
395
|
: '';
|
|
333
396
|
// Include <math.h> when the generated code, or a helper it calls, uses a libm symbol.
|
|
334
397
|
const usesMath = out.needsMath ||
|
|
398
|
+
BOOT_LIBM_USE.test(bootFn) ||
|
|
335
399
|
LIBM_USE.test([
|
|
400
|
+
bootFn,
|
|
336
401
|
stateBlock,
|
|
337
402
|
refDecls,
|
|
338
403
|
vectorBuilderBlock,
|
|
@@ -348,7 +413,11 @@ static int16_t app_round_dim(double v)
|
|
|
348
413
|
const hostRecords = scalarRecords.filter(scalarRecord => scalarRecord.host);
|
|
349
414
|
const hostSettersBlock = hostRecords
|
|
350
415
|
.map(hostRecord => `void er_app_set_${hostRecord.name}(${cScalarType(hostRecord.cType)} v)\n{\n ` +
|
|
351
|
-
`${hostRecord.cMember} = v;\n
|
|
416
|
+
`${hostRecord.cMember} = v;\n` +
|
|
417
|
+
printChangedFlag(hostRecord, ' ')
|
|
418
|
+
.map(line => line + '\n')
|
|
419
|
+
.join('') +
|
|
420
|
+
`${hasUpdate ? ' app_update();\n' : ''}}`)
|
|
352
421
|
.join('\n\n');
|
|
353
422
|
const hostSetterProtos = hostRecords
|
|
354
423
|
.map(hostRecord => `/** @brief Host-fed input '${hostRecord.name}' (useHostValue): set its value and ` +
|
|
@@ -448,6 +517,16 @@ static int16_t app_round_dim(double v)
|
|
|
448
517
|
' if (v <= -2147483648.0f)\n {\n return INT_MIN;\n }\n' +
|
|
449
518
|
' return (int)v;\n}',
|
|
450
519
|
],
|
|
520
|
+
[
|
|
521
|
+
'app_d2i',
|
|
522
|
+
'/* A double kept as an int, as app_f2i keeps a float: for a value worked out at boot, which a float ' +
|
|
523
|
+
'would round. */\n' +
|
|
524
|
+
'static int app_d2i(double v)\n{\n' +
|
|
525
|
+
' if (v != v)\n {\n return 0;\n }\n' +
|
|
526
|
+
' if (v >= 2147483647.0)\n {\n return INT_MAX;\n }\n' +
|
|
527
|
+
' if (v <= -2147483648.0)\n {\n return INT_MIN;\n }\n' +
|
|
528
|
+
' return (int)v;\n}',
|
|
529
|
+
],
|
|
451
530
|
[
|
|
452
531
|
'app_roundf',
|
|
453
532
|
"/* Math.round as JS rounds: to the nearest whole number, halves up (toward +Infinity). C's roundf takes\n" +
|
|
@@ -466,6 +545,12 @@ static int16_t app_round_dim(double v)
|
|
|
466
545
|
' if (v == 0.0f)\n {\n return "0";\n }\n' +
|
|
467
546
|
' snprintf(buf, 16, "%g", (double)v);\n return buf;\n}',
|
|
468
547
|
],
|
|
548
|
+
[
|
|
549
|
+
'app_round',
|
|
550
|
+
'/* Math.round as JS rounds, in double precision for a value worked out at boot: halves up. */\n' +
|
|
551
|
+
'static double app_round(double v)\n{\n' +
|
|
552
|
+
' const double f = floor(v);\n return v - f >= 0.5 ? f + 1.0 : f;\n}',
|
|
553
|
+
],
|
|
469
554
|
[
|
|
470
555
|
'app_opacity',
|
|
471
556
|
"/* A state-driven opacity as the engine's 0-255 byte: clamped to 0..1 and rounded, as Flow A's bridge and\n" +
|
|
@@ -610,6 +695,7 @@ static int16_t app_round_dim(double v)
|
|
|
610
695
|
];
|
|
611
696
|
// Generated code with literals and comments blanked, so only a real call emits a helper (unused statics warn).
|
|
612
697
|
const helperCallers = stripCLiterals([
|
|
698
|
+
bootFn,
|
|
613
699
|
stateBlock,
|
|
614
700
|
refDecls,
|
|
615
701
|
vectorBuilderBlock,
|
|
@@ -694,8 +780,30 @@ static int16_t app_round_dim(double v)
|
|
|
694
780
|
? keyboard.setup +
|
|
695
781
|
' /* app-supplied on-screen keyboard layout/appearance */\n'
|
|
696
782
|
: '';
|
|
783
|
+
const initialChanged = watchedRecords.length
|
|
784
|
+
? ` memset(&${CHANGED_FLAGS}, 1, sizeof(${CHANGED_FLAGS})); /* the first update sets every node */\n`
|
|
785
|
+
: '';
|
|
697
786
|
const initialUpdate = hasUpdate
|
|
698
|
-
?
|
|
787
|
+
? `\n${initialChanged} app_update(); /* apply initial state-dependent props */\n`
|
|
788
|
+
: '';
|
|
789
|
+
// er_app_fits() asks app_boot() without disturbing the values a built app reads.
|
|
790
|
+
const fitsBody = !hasBoot(app.boot)
|
|
791
|
+
? [' (void)screen_w;', ' (void)screen_h;', ' return true;']
|
|
792
|
+
: bootDecls
|
|
793
|
+
? [
|
|
794
|
+
' const ErAppBoot built = s_boot;',
|
|
795
|
+
' const bool fits = app_boot(screen_w, screen_h);',
|
|
796
|
+
' s_boot = built;',
|
|
797
|
+
' return fits;',
|
|
798
|
+
]
|
|
799
|
+
: [' return app_boot(screen_w, screen_h);'];
|
|
800
|
+
const fitsFn = 'bool er_app_fits(int screen_w, int screen_h)\n{\n' +
|
|
801
|
+
fitsBody.join('\n') +
|
|
802
|
+
'\n}';
|
|
803
|
+
const bootCall = hasBoot(app.boot)
|
|
804
|
+
? '\n /* The values that depend on the panel size; a panel the app was not compiled for builds ' +
|
|
805
|
+
'nothing. */\n' +
|
|
806
|
+
' if (!app_boot(screen_w, screen_h))\n {\n return false;\n }\n'
|
|
699
807
|
: '';
|
|
700
808
|
// app.gen.c: includes and version pin, then file-scope state, declarations and functions, then er_app_build().
|
|
701
809
|
const body = `/*
|
|
@@ -732,12 +840,14 @@ void er_app_set_wall_clock(int64_t epoch_ms)
|
|
|
732
840
|
{
|
|
733
841
|
s_wall_offset_ms = epoch_ms - (int64_t)er_now_ms64();${hasUpdate ? '\n s_wall_clock_changed = 1;' : ''}
|
|
734
842
|
}
|
|
735
|
-
${hostSettersBlock ? '\n' + hostSettersBlock + '\n' : ''}
|
|
736
|
-
|
|
843
|
+
${hostSettersBlock ? '\n' + hostSettersBlock + '\n' : ''}${bootFn ? '\n' + bootFn + '\n' : ''}
|
|
844
|
+
${fitsFn}
|
|
845
|
+
|
|
846
|
+
bool er_app_build(int screen_w, int screen_h)
|
|
737
847
|
{
|
|
738
848
|
ERProps p;
|
|
739
849
|
ERNode* ${nodeDecls.join(';\n ERNode* ')};
|
|
740
|
-
|
|
850
|
+
${bootCall}
|
|
741
851
|
/* A screen-sized root the app tree fills (mirrors AppRegistry mounting into a screen-sized host). */
|
|
742
852
|
ERNode* root = er_node_create(ER_NODE_VIEW);
|
|
743
853
|
er_props_default(&p);
|
|
@@ -748,23 +858,22 @@ ${animCreate ? '\n' + animCreate + '\n' : ''}
|
|
|
748
858
|
${buildLines}
|
|
749
859
|
er_tree_append_child(root, ${app.root.id});
|
|
750
860
|
er_tree_set_root(root);
|
|
751
|
-
${keyboardSetup}${initialUpdate}${mountEffectsBlock}
|
|
861
|
+
${keyboardSetup}${initialUpdate}${mountEffectsBlock} return true;
|
|
862
|
+
}
|
|
752
863
|
`;
|
|
753
864
|
// app.gen.h: what the file was generated for, and the app's public entry points.
|
|
754
865
|
const header = `/* Generated by the embedded-react Flow B AOT compiler. DO NOT EDIT. */
|
|
755
866
|
#ifndef ER_APP_GEN_H
|
|
756
867
|
#define ER_APP_GEN_H
|
|
757
868
|
|
|
869
|
+
#include <stdbool.h>
|
|
758
870
|
#include <stdint.h>
|
|
759
871
|
|
|
760
872
|
/*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
*
|
|
765
|
-
* Every board example consumes the same dist/app.gen.c, so generating for one board and then building
|
|
766
|
-
* another produces firmware that compiles, links, boots, and lays out wrong. Boards \`_Static_assert\` these
|
|
767
|
-
* against their own panel size to turn that into a compile error; see each board example's main.c.
|
|
873
|
+
* The panel this file was generated at. Values that depend only on the panel size are worked out at boot by
|
|
874
|
+
* er_app_build(), but a branch on \`screen\` that changes the tree was compiled for this panel only, and so
|
|
875
|
+
* was any value the compiler had to know. er_app_build() checks those at boot and builds nothing on a panel
|
|
876
|
+
* that differs in any of them, returning false; er_app_fits() asks the same without building.
|
|
768
877
|
*
|
|
769
878
|
* WHICH demo is recorded the same way, as a marker macro named ER_AOT_DEMO_<demo> with every character
|
|
770
879
|
* outside [A-Za-z0-9] encoded as _<hex>_ (so \`watch-face\` defines ER_AOT_DEMO_watch_2d_face). The encoding
|
|
@@ -777,8 +886,15 @@ ${keyboardSetup}${initialUpdate}${mountEffectsBlock}}
|
|
|
777
886
|
#define ER_AOT_DEMO ${cstr(demo)}
|
|
778
887
|
#define ${demoMarker(demo)} 1
|
|
779
888
|
|
|
780
|
-
/** @brief Builds the AOT-compiled app's scene graph + state machine (call once after backend init).
|
|
781
|
-
|
|
889
|
+
/** @brief Builds the AOT-compiled app's scene graph + state machine (call once after backend init).
|
|
890
|
+
* @return false, having built nothing, when the panel does not take the layout this file was compiled
|
|
891
|
+
* for (see er_app_fits()). */
|
|
892
|
+
bool er_app_build(int screen_w, int screen_h);
|
|
893
|
+
|
|
894
|
+
/** @brief Whether a panel of this size takes the layout this file was compiled for. Values that depend only
|
|
895
|
+
* on the panel size are worked out at boot, so any panel that takes the same layout fits; what the
|
|
896
|
+
* compiler had to fold at its own panel (a branch that changes the tree, a font size) must match. */
|
|
897
|
+
bool er_app_fits(int screen_w, int screen_h);
|
|
782
898
|
|
|
783
899
|
/** @brief Advances app timers (setInterval/setTimeout). Call once per frame with the elapsed ms; a no-op
|
|
784
900
|
* for apps that use no timers, so it is always safe to call. */
|