From c2b3488bf28c42c2ad9d2077ac551347c1cdc306 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 3 Apr 2024 14:40:45 +0900 Subject: [PATCH] =?UTF-8?q?trace=5Flimit=EC=9D=84=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EB=B3=80=EC=88=98=EB=A1=9C=20=EC=84=A4=EC=A0=95=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=ED=95=98=EA=B3=A0=20=EA=B8=B0=EB=B3=B8?= =?UTF-8?q?=EA=B0=92=20=EB=81=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aheui/_argparse.py | 3 ++- aheui/aheui.py | 40 +++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/aheui/_argparse.py b/aheui/_argparse.py index f244cbf..43237f1 100644 --- a/aheui/_argparse.py +++ b/aheui/_argparse.py @@ -157,6 +157,7 @@ def parse_args(self, args): """) parser.add_argument('--cmd', '-c', default='', description='Program passed in as string') parser.add_argument('--no-c', '--no-c', narg='0', default='no', description='Do not generate `.aheuic` file automatically.', full_description='\tWhat is .aheuic? https://github.com/aheui/snippets/commit/cbb5a12e7cd2db771538ab28dfbc9ad1ada86f35\n') -parser.add_argument('--warning-limit', '--warning-limit', default='3', description='Set repetitive warning limit. 0 means no warning. -1 means no limit.') +parser.add_argument('--warning-limit', '--warning-limit', default='', description='Set repetitive warning limit. '' fallbacks to environment variable `RPAHEUI_WARNING_LIMIT`. 0 means no warning. -1 means no limit. Default is 3.') +parser.add_argument('--trace-limit', '--trace-limit', default='', description='Set JIT trace limit. '' fallbacks to environment variable `RPAHEUI_TRACE_LIMIT`.') parser.add_argument('--version', '-v', narg='-1', default='no', description='Show program version', message=VERSION) parser.add_argument('--help', '-h', narg='-1', default='no', description='Show this help text') diff --git a/aheui/aheui.py b/aheui/aheui.py index 06402d8..414340f 100644 --- a/aheui/aheui.py +++ b/aheui/aheui.py @@ -329,7 +329,6 @@ def get_label(self, pc): def mainloop(program, debug): - jit.set_param(driver, 'trace_limit', 30000) program = jit.promote(program) jit.assert_green(program) pc = 0 @@ -441,7 +440,34 @@ def mainloop(program, debug): return 0 -def process_opt(argv): +def kwarg_or_environ(kwargs, environ, arg_key, env_key): + if arg_key in kwargs and kwargs[arg_key] != '': + return (1, kwargs[arg_key]) + try: + return (2, environ[env_key]) + except KeyError: + return (0, '') + + +def kwarg_or_environ_int(kwargs, environ, arg_key, env_key, default): + source, arg = kwarg_or_environ(kwargs, environ, arg_key, env_key) + if source == 0: + return default + try: + value = int(arg) + except ValueError: + if source == 1: + msg = b'The value of --%s="%s" is not a valid integer\n' % (arg_key, arg) + elif source == 2: + msg = b'The value %s="%s" is not a valid integer\n' % (env_key, arg) + else: + assert False + os.write(2, msg) + raise + return value + + +def process_opt(argv, environ): def open_r(filename): return os.open(filename, os.O_RDONLY, 0o777) @@ -520,9 +546,10 @@ def open_r(filename): os.write(2, b'aheui: error: --target,-t must be one of "bytecode", "asm", "asm+comment", "run"\n') # noqa: E501 raise SystemExit() - warning_limit = int(kwargs['warning-limit']) + warning_limit = kwarg_or_environ_int(kwargs, environ, 'warning-limit', 'RPAHEUI_WARNING_LIMIT', 3) + trace_limit = kwarg_or_environ_int(kwargs, environ, 'trace-limit', 'RPAHEUI_TRACE_LIMIT', -1) - return cmd, source, contents, opt_level, target, aheuic_output, comment_aheuis, output, warning_limit + return cmd, source, contents, opt_level, target, aheuic_output, comment_aheuis, output, warning_limit, trace_limit def open_w(filename): @@ -563,10 +590,13 @@ def prepare_compiler(contents, opt_level=2, source='code', aheuic_output=None, a def entry_point(argv): try: - cmd, source, contents, str_opt_level, target, aheuic_output, comment_aheuis, output, warning_limit = process_opt(argv) + cmd, source, contents, str_opt_level, target, aheuic_output, comment_aheuis, output, warning_limit, trace_limit = process_opt(argv, os.environ) except SystemExit: return 1 + warnings.limit = warning_limit + if trace_limit >= 0: + jit.set_param(driver, 'trace_limit', trace_limit) add_debug_info = DEBUG or target != 'run' # debug flag for user program compiler = prepare_compiler(contents, int(str_opt_level), source, aheuic_output, add_debug_info)