From b3061c38ef984418e266c6f74b366a7e953840a5 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Wed, 25 Aug 2021 14:44:23 +0000 Subject: [PATCH] pywriter: support RANGE helper --- src/openpower/decoder/helpers.py | 10 ++++++++++ src/openpower/decoder/pseudo/parser.py | 14 +++----------- src/openpower/decoder/pseudo/pywriter.py | 1 + 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/openpower/decoder/helpers.py b/src/openpower/decoder/helpers.py index 87b71d99..09f2324a 100644 --- a/src/openpower/decoder/helpers.py +++ b/src/openpower/decoder/helpers.py @@ -22,6 +22,16 @@ Links: """ +def RANGE(start, end): + if start > end: + # auto-subtract-one (sigh) due to python range + end -= 1 + else: + # auto-add-one (sigh) due to python range + end += 1 + return range(start, end) + + def exts(value, bits): sign = 1 << (bits - 1) return (value & (sign - 1)) - (value & sign) diff --git a/src/openpower/decoder/pseudo/parser.py b/src/openpower/decoder/pseudo/parser.py index 72ce61e4..03d0fd5e 100644 --- a/src/openpower/decoder/pseudo/parser.py +++ b/src/openpower/decoder/pseudo/parser.py @@ -495,20 +495,12 @@ class PowerParser: p[0] = ast.Break() def p_for_stmt(self, p): - """for_stmt : FOR atom EQ test TO test COLON suite - | DO atom EQ test TO test COLON suite + """for_stmt : FOR atom EQ comparison TO comparison COLON suite + | DO atom EQ comparison TO comparison COLON suite """ start = p[4] end = p[6] - if start.value > end.value: # start greater than end, must go -ve - # auto-subtract-one (sigh) due to python range - end = ast.BinOp(p[6], ast.Add(), ast.Constant(-1)) - arange = [start, end, ast.Constant(-1)] - else: - # auto-add-one (sigh) due to python range - end = ast.BinOp(p[6], ast.Add(), ast.Constant(1)) - arange = [start, end] - it = ast.Call(ast.Name("range", ast.Load()), arange, []) + it = ast.Call(ast.Name("RANGE", ast.Load()), (start, end), []) p[0] = ast.For(p[2], it, p[8], []) def p_while_stmt(self, p): diff --git a/src/openpower/decoder/pseudo/pywriter.py b/src/openpower/decoder/pseudo/pywriter.py index efca91c9..1387e890 100644 --- a/src/openpower/decoder/pseudo/pywriter.py +++ b/src/openpower/decoder/pseudo/pywriter.py @@ -26,6 +26,7 @@ from openpower.decoder.helpers import (EXTS, EXTS64, EXTZ64, ROTL64, ROTL32, trunc_divs, trunc_rems, MULS, DIVS, MODS, EXTS128, undefined, bitrev, SHL64, + RANGE, ) from openpower.decoder.selectable_int import SelectableInt from openpower.decoder.selectable_int import selectconcat as concat -- 2.30.2