From 60d9b95ff46bee5d3d52902241613bf1d73eee70 Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sat, 18 Sep 2021 16:00:56 +0200 Subject: [PATCH] Berry fix compiler bug in complex compound assignments --- lib/libesp32/Berry/src/be_parser.c | 6 ++++-- lib/libesp32/Berry/tests/compound.be | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 lib/libesp32/Berry/tests/compound.be diff --git a/lib/libesp32/Berry/src/be_parser.c b/lib/libesp32/Berry/src/be_parser.c index bb455df68..cc4ae7969 100644 --- a/lib/libesp32/Berry/src/be_parser.c +++ b/lib/libesp32/Berry/src/be_parser.c @@ -900,10 +900,12 @@ static void suffix_expr(bparser *parser, bexpdesc *e) static void suffix_alloc_reg(bparser *parser, bexpdesc *l) { bfuncinfo *finfo = parser->finfo; - bbool suffix = l->type == ETINDEX || l->type == ETMEMBER; + bbool is_suffix = l->type == ETINDEX || l->type == ETMEMBER; /* is suffix */ + bbool is_suffix_reg = l->v.ss.tt == ETREG || l->v.ss.tt == ETLOCAL || l->v.ss.tt == ETGLOBAL || l->v.ss.tt == ETNGLOBAL; /* if suffix, does it need a register */ + bbool is_global = l->type == ETGLOBAL || l->type == ETNGLOBAL; /* in the suffix expression, if the object is a temporary * variable (l->v.ss.tt == ETREG), it needs to be cached. */ - if (suffix && l->v.ss.tt == ETREG) { + if (is_global || (is_suffix && is_suffix_reg)) { be_code_allocregs(finfo, 1); } } diff --git a/lib/libesp32/Berry/tests/compound.be b/lib/libesp32/Berry/tests/compound.be new file mode 100644 index 000000000..bda74c9e2 --- /dev/null +++ b/lib/libesp32/Berry/tests/compound.be @@ -0,0 +1,19 @@ +# test bug in compound statements + +a = 0 +assert(a == 0) +a += 1 +assert(a == 1) +a += 10/2 +assert(a == 6) + +class A var a def init() self.a = 1 end def f(x) self.a+=x/2 end def g(x) self.a = self.a + x/2 end end + +a = A() +assert(a.a == 1) +a.f(10) +assert(a.a == 6) +b=A() +assert(b.a == 1) +b.g(10) +assert(b.a == 6) \ No newline at end of file