From e3b151c7565abd4893e7a9151a47b2abef8dbc9e Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sun, 19 Nov 2023 17:16:03 +0100 Subject: [PATCH] Fix Berry parser error in specific cases (#20059) * Fix Berry parser error in specific cases * fix typo * Fix last bug (hopefully) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_code.c | 12 ++--- lib/libesp32/berry/tests/compiler.be | 21 +++++++++ .../src/solidify/solidified_animate_module.h | 12 ++--- .../src/solidify/solidified_partition_core.h | 47 +++++++++---------- .../src/solidify/solidified_rule_matcher.h | 12 ++--- 6 files changed, 63 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ee382262..a05a2dad9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ All notable changes to this project will be documented in this file. - Matter flow sensor (#19961) - Berry ``gpio.dac_voltage()`` regression from v13.1.0.1 (#19997) - ESP32-C3 ledlink functionality regression from v13.1.0.2 +- Fix Berry parser error in specific cases ### Removed diff --git a/lib/libesp32/berry/src/be_code.c b/lib/libesp32/berry/src/be_code.c index c6aa6e815..7a1b2bd00 100644 --- a/lib/libesp32/berry/src/be_code.c +++ b/lib/libesp32/berry/src/be_code.c @@ -478,11 +478,7 @@ static int exp2reg(bfuncinfo *finfo, bexpdesc *e, int dst) reg = e->v.idx; } else { /* otherwise, we allocate a new register or use the target provided */ - if (dst < 0) { - reg = be_code_allocregs(finfo, 1); - } else { - reg = dst; - } + reg = (dst < 0) ? be_code_allocregs(finfo, 1) : dst; } be_code_conjump(finfo, &e->t, jpt); pcf = code_bool(finfo, reg, 0, 1); @@ -690,16 +686,20 @@ static void setsfxvar(bfuncinfo *finfo, bopcode op, bexpdesc *e1, int src) /* Assign expr e2 to e1 */ /* e1 must be in a register and have a valid idx */ -/* if `keep_reg` is true, do not release registre */ +/* if `keep_reg` is true, do not release register */ /* return 1 if assignment was possible, 0 if type is not compatible */ int be_code_setvar(bfuncinfo *finfo, bexpdesc *e1, bexpdesc *e2, bbool keep_reg) { + /* free_e2 indicates special case where ETINDEX or ETMEMBER need to be freed if top of registers */ + bbool free_e2 = (e2->type == ETINDEX || e2->type == ETMEMBER) && (e2->v.ss.idx != e1->v.idx) && (e2->v.ss.idx == finfo->freereg - 1); int src = exp2reg(finfo, e2, e1->type == ETLOCAL ? e1->v.idx : -1); /* Convert e2 to kreg */ /* If e1 is a local variable, use the register */ if (!keep_reg && (e1->type != ETLOCAL || e1->v.idx != src)) { free_expreg(finfo, e2); /* free source (checks only ETREG) */ /* TODO e2 is at top */ + } else if (!keep_reg && free_e2) { + be_code_freeregs(finfo, 1); } switch (e1->type) { case ETLOCAL: /* It can't be ETREG. */ diff --git a/lib/libesp32/berry/tests/compiler.be b/lib/libesp32/berry/tests/compiler.be index 3741095a2..a2b25640e 100644 --- a/lib/libesp32/berry/tests/compiler.be +++ b/lib/libesp32/berry/tests/compiler.be @@ -57,3 +57,24 @@ assert(s == "foo") def f(a,b) return b end l = [1,2,3,4] assert(f(l[-1],l[-2]) == 3) + +# Compilation problem: +# def test() +# var line = '1234567890' +# line = line[3..7] +# # print(line) +# for n : 1..2 end +# end +# test() + +# BRY: Exception> 'attribute_error' - the 'range' object has no method '()' +# stack traceback: +# :5: in function `test` +# :7: in function `main` +def test() + var line = '1234567890' + line = line[3..7] +# print(line) + for n : 1..2 end +end +test() diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_animate_module.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_animate_module.h index a153a3817..7beb1387f 100644 --- a/lib/libesp32/berry_tasmota/src/solidify/solidified_animate_module.h +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_animate_module.h @@ -603,12 +603,12 @@ be_local_closure(Animate_engine_animate, /* name */ 0x880C0105, // 001B GETMBR R3 R0 K5 0x88100104, // 001C GETMBR R4 R0 K4 0x940C0604, // 001D GETIDX R3 R3 R4 - 0x6014000F, // 001E GETGBL R5 G15 - 0x5C180600, // 001F MOVE R6 R3 - 0xB81E1200, // 0020 GETNGBL R7 K9 - 0x881C0F0A, // 0021 GETMBR R7 R7 K10 - 0x7C140400, // 0022 CALL R5 2 - 0x78160020, // 0023 JMPF R5 #0045 + 0x6010000F, // 001E GETGBL R4 G15 + 0x5C140600, // 001F MOVE R5 R3 + 0xB81A1200, // 0020 GETNGBL R6 K9 + 0x88180D0A, // 0021 GETMBR R6 R6 K10 + 0x7C100400, // 0022 CALL R4 2 + 0x78120020, // 0023 JMPF R4 #0045 0x8810010B, // 0024 GETMBR R4 R0 K11 0x8814070C, // 0025 GETMBR R5 R3 K12 0x14140405, // 0026 LT R5 R2 R5 diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_partition_core.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_partition_core.h index 919b870ca..7fb4faa76 100644 --- a/lib/libesp32/berry_tasmota/src/solidify/solidified_partition_core.h +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_partition_core.h @@ -711,26 +711,25 @@ be_local_closure(Partition_get_unallocated_k, /* name */ }), &be_const_str_get_unallocated_k, &be_const_str_solidified, - ( &(const binstruction[19]) { /* code */ + ( &(const binstruction[18]) { /* code */ 0x88040100, // 0000 GETMBR R1 R0 K0 0x5409FFFE, // 0001 LDINT R2 -1 0x94040202, // 0002 GETIDX R1 R1 R2 - 0x8C0C0301, // 0003 GETMET R3 R1 K1 - 0x7C0C0200, // 0004 CALL R3 1 - 0x780E000B, // 0005 JMPF R3 #0012 - 0x8C0C0102, // 0006 GETMET R3 R0 K2 - 0x7C0C0200, // 0007 CALL R3 1 - 0x5C080600, // 0008 MOVE R2 R3 - 0x880C0303, // 0009 GETMBR R3 R1 K3 - 0x88100304, // 000A GETMBR R4 R1 K4 - 0x000C0604, // 000B ADD R3 R3 R4 - 0x541203FF, // 000C LDINT R4 1024 - 0x0C0C0604, // 000D DIV R3 R3 R4 - 0x14100602, // 000E LT R4 R3 R2 - 0x78120001, // 000F JMPF R4 #0012 - 0x04100403, // 0010 SUB R4 R2 R3 - 0x80040800, // 0011 RET 1 R4 - 0x80060A00, // 0012 RET 1 K5 + 0x8C080301, // 0003 GETMET R2 R1 K1 + 0x7C080200, // 0004 CALL R2 1 + 0x780A000A, // 0005 JMPF R2 #0011 + 0x8C080102, // 0006 GETMET R2 R0 K2 + 0x7C080200, // 0007 CALL R2 1 + 0x880C0303, // 0008 GETMBR R3 R1 K3 + 0x88100304, // 0009 GETMBR R4 R1 K4 + 0x000C0604, // 000A ADD R3 R3 R4 + 0x541203FF, // 000B LDINT R4 1024 + 0x0C0C0604, // 000C DIV R3 R3 R4 + 0x14100602, // 000D LT R4 R3 R2 + 0x78120001, // 000E JMPF R4 #0011 + 0x04100403, // 000F SUB R4 R2 R3 + 0x80040800, // 0010 RET 1 R4 + 0x80060A00, // 0011 RET 1 K5 }) ) ); @@ -888,13 +887,13 @@ be_local_closure(Partition_resize_fs_to_max, /* name */ 0x88080107, // 0012 GETMBR R2 R0 K7 0x540DFFFE, // 0013 LDINT R3 -1 0x94080403, // 0014 GETIDX R2 R2 R3 - 0x541603FF, // 0015 LDINT R5 1024 - 0x08140205, // 0016 MUL R5 R1 R5 - 0x88100508, // 0017 GETMBR R4 R2 K8 - 0x00100805, // 0018 ADD R4 R4 R5 - 0x900A1004, // 0019 SETMBR R2 K8 R4 - 0x8C100109, // 001A GETMET R4 R0 K9 - 0x7C100200, // 001B CALL R4 1 + 0x541203FF, // 0015 LDINT R4 1024 + 0x08100204, // 0016 MUL R4 R1 R4 + 0x880C0508, // 0017 GETMBR R3 R2 K8 + 0x000C0604, // 0018 ADD R3 R3 R4 + 0x900A1003, // 0019 SETMBR R2 K8 R3 + 0x8C0C0109, // 001A GETMET R3 R0 K9 + 0x7C0C0200, // 001B CALL R3 1 0xB80E0400, // 001C GETNGBL R3 K2 0x880C070A, // 001D GETMBR R3 R3 K10 0x900E1705, // 001E SETMBR R3 K11 K5 diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_rule_matcher.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_rule_matcher.h index ced3ca45e..81f51d586 100644 --- a/lib/libesp32/berry_tasmota/src/solidify/solidified_rule_matcher.h +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_rule_matcher.h @@ -1372,8 +1372,8 @@ be_local_closure(Rule_Matcher_parse, /* name */ 0x0434170A, // 0049 SUB R13 R11 K10 0x4034120D, // 004A CONNECT R13 R9 R13 0x94300A0D, // 004B GETIDX R12 R5 R13 - 0x0038170A, // 004C ADD R14 R11 K10 - 0x5C241C00, // 004D MOVE R9 R14 + 0x0034170A, // 004C ADD R13 R11 K10 + 0x5C241A00, // 004D MOVE R9 R13 0x70020002, // 004E JMP #0052 0x40341310, // 004F CONNECT R13 R9 K16 0x94300A0D, // 0050 GETIDX R12 R5 R13 @@ -1400,10 +1400,10 @@ be_local_closure(Rule_Matcher_parse, /* name */ 0x04441B0A, // 0065 SUB R17 R13 K10 0x40461211, // 0066 CONNECT R17 K9 R17 0x94301811, // 0067 GETIDX R12 R12 R17 - 0x60480009, // 0068 GETGBL R18 G9 - 0x5C4C2000, // 0069 MOVE R19 R16 - 0x7C480200, // 006A CALL R18 1 - 0x5C382400, // 006B MOVE R14 R18 + 0x60440009, // 0068 GETGBL R17 G9 + 0x5C482000, // 0069 MOVE R18 R16 + 0x7C440200, // 006A CALL R17 1 + 0x5C382200, // 006B MOVE R14 R17 0x1C3C1915, // 006C EQ R15 R12 K21 0x783E0004, // 006D JMPF R15 #0073 0x8C3C0703, // 006E GETMET R15 R3 K3