Skip to content

Commit

Permalink
Add compiler option to enable additional branch optimization steps (#432
Browse files Browse the repository at this point in the history
)

This allows code like:

define TEST := 5

on init
   if TEST > 7
        declare some_var
   end if

   message(some_var)
end on

to error out because the condition is false
  • Loading branch information
mkruselj authored Jan 26, 2024
1 parent 1c42271 commit e58deda
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
8 changes: 8 additions & 0 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@
"id": "optimize_compiled",
"checkbox": true
},
{
"command": "ksp_global_setting_toggle",
"args": {"setting": "ksp_additional_branch_optimization", "default": false},
"caption": "Additional Branch Optimization Steps",
"mnemonic": "B",
"id": "additional_branch_optimization",
"checkbox": true
},
{"caption": "-"}, //separator
{
"command": "ksp_global_setting_toggle",
Expand Down
33 changes: 21 additions & 12 deletions compiler/ksp_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
\. # a literal dot
''', re.VERBOSE)

compiler_options = '(remove_whitespace|compact_variables|combine_callbacks|extra_syntax_checks|optimize_code|add_compile_date|sanitize_exit_command)'
compiler_options = '(remove_whitespace|compact_variables|combine_callbacks|extra_syntax_checks|optimize_code|additional_branch_optimization|add_compile_date|sanitize_exit_command)'

pragma_compile_with_re = re.compile(r'\{\s*\#pragma\s+compile_with\s+%s\s*\}' % compiler_options)
pragma_compile_without_re = re.compile(r'\{\s*\#pragma\s+compile_without\s+%s\s*\}' % compiler_options)
Expand Down Expand Up @@ -1963,6 +1963,7 @@ def __init__(self,
combine_callbacks = False,
extra_syntax_checks = False,
optimize = False,
additional_branch_optimization = False,
sanitize_exit_command = False,
add_compiled_date_comment = False,
force_compiler_arguments = False):
Expand All @@ -1972,6 +1973,7 @@ def __init__(self,
self.compact = compact
self.compact_variables = compact_variables
self.optimize = optimize
self.additional_branch_optimization = additional_branch_optimization
self.sanitize_exit_command = sanitize_exit_command
self.combine_callbacks = combine_callbacks
self.add_compiled_date_comment = add_compiled_date_comment
Expand Down Expand Up @@ -2281,16 +2283,17 @@ def compile(self, callback = None):
self.combine_callbacks = value
if option == 'extra_syntax_checks':
self.extra_syntax_checks = value
if option == 'optimize_code':
if option == 'extra_branch_optimization':
self.extra_syntax_checks = value
self.optimize = value
self.additional_branch_optimization = value
if option == 'add_compile_date':
self.add_compiled_date_comment = value
if option == 'sanitize_exit_command':
self.sanitize_exit_command = value

do_extra = self.extra_syntax_checks
do_optim = do_extra and self.optimize
do_abo = do_extra and self.additional_branch_optimization
do_sanitize_exit = self.sanitize_exit_command

# description function condition time-weight
Expand All @@ -2311,6 +2314,7 @@ def compile(self, callback = None):

('parsing code', lambda: self.parse_code(), True, 1),
('combining callbacks', lambda: ASTModifierCombineCallbacks(self.module, self.combine_callbacks), True, 1),
('removing unused branches', lambda: comp_extras.ASTModifierRemoveUnusedBranches(self.module), do_abo, 1),
('modifying nodes to native KSP', lambda: ASTModifierNodesToNativeKSP(self.module, self.lines), True, 1),
('adding variable name prefixes', lambda: ASTModifierFixPrefixesIncludingLocalVars(self.module), True, 1),
('inlining functions', lambda: ASTModifierFunctionExpander(self.module), True, 1),
Expand All @@ -2322,6 +2326,7 @@ def compile(self, callback = None):
('initializing extra syntax checks', lambda: self.init_extra_syntax_checks(), do_extra, 1),
('checking expression types', lambda: comp_extras.ASTVisitorDetermineExpressionTypes(self.module, functions), do_extra, 1),
('checking statement types', lambda: comp_extras.ASTVisitorCheckStatementExprTypes(self.module), do_extra, 1),
('removing unused branches', lambda: comp_extras.ASTModifierRemoveUnusedBranches(self.module), do_abo, 1),
('checking declarations', lambda: comp_extras.ASTVisitorCheckDeclarations(self.module), do_extra, 1),
('simplying expressions', lambda: comp_extras.ASTModifierSimplifyExpressions(self.module, True), do_optim, 1),
('removing unused branches', lambda: comp_extras.ASTModifierRemoveUnusedBranches(self.module), do_optim, 1),
Expand Down Expand Up @@ -2434,6 +2439,9 @@ def __repr__(self):
arg_parser.add_argument('-o', '--optimize',
dest = 'optimize', action = 'store_true', default = False,
help = 'optimize the compiled code')
arg_parser.add_argument('-b', '--extra_branch_optimization',
dest = 'additional_branch_optimization', action = 'store_true', default = False,
help = 'adds branch optimization checks earlier in compile process, allowing define constant based branching etc.')
arg_parser.add_argument('-t', '--add_compile_date',
dest = 'add_compile_date', action = 'store_true', default = False,
help = 'adds the date and time comment atop the compiled code')
Expand All @@ -2451,8 +2459,8 @@ def __repr__(self):
if args.source_file.name != '<stdin>':
basepath = os.path.dirname(args.source_file.name)

# make sure that extra syntax checks are enabled if --optimize argument is used
if args.optimize == True and args.extra_syntax_checks == False:
# make sure that extra syntax checks are enabled if --optimize or --extra_branch_optimization arguments are used
if (args.optimize == True or args.additional_branch_optimization) and args.extra_syntax_checks == False:
args.extra_syntax_checks = True

# read the source and compile it
Expand All @@ -2462,14 +2470,15 @@ def __repr__(self):

compiler = KSPCompiler(code,
basepath,
compact = args.compact,
combine_callbacks = args.combine_callbacks,
compact_variables = args.compact_variables,
extra_syntax_checks = args.extra_syntax_checks,
optimize = args.optimize,
sanitize_exit_command = args.sanitize_exit_command,
compact = args.compact,
combine_callbacks = args.combine_callbacks,
compact_variables = args.compact_variables,
extra_syntax_checks = args.extra_syntax_checks,
optimize = args.optimize,
additional_branch_optimization = args.additional_branch_optimization,
sanitize_exit_command = args.sanitize_exit_command,
add_compiled_date_comment = (args.add_compile_date),
force_compiler_arguments = (args.force_compiler_arguments))
force_compiler_arguments = (args.force_compiler_arguments))

compiler.compile(callback = utils.compile_on_progress)

Expand Down
17 changes: 10 additions & 7 deletions ksp_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ def run(self):
optimize = settings.get('ksp_optimize_code', False)
combine_callbacks = settings.get('ksp_combine_callbacks', False)
sanitize_exit_command = settings.get('ksp_sanitize_exit_command', True)
additional_branch_optimization = settings.get('ksp_additional_branch_optimization', True)
add_compiled_date_comment = settings.get('ksp_add_compiled_date', True)
should_play_sound = settings.get('ksp_play_sound', False)

Expand Down Expand Up @@ -221,13 +222,14 @@ def run(self):

self.compiler = ksp_compiler.KSPCompiler(code,
self.base_path,
compact = compact,
compact_variables = compact_variables,
extra_syntax_checks = check,
combine_callbacks = combine_callbacks,
optimize = optimize and check,
sanitize_exit_command = sanitize_exit_command,
add_compiled_date_comment = add_compiled_date_comment)
compact = compact,
compact_variables = compact_variables,
extra_syntax_checks = check,
combine_callbacks = combine_callbacks,
optimize = check and optimize,
additional_branch_optimization = check and additional_branch_optimization,
sanitize_exit_command = sanitize_exit_command,
add_compiled_date_comment = add_compiled_date_comment)

if self.compiler.compile(callback = utils.compile_on_progress):
last_compiler = self.compiler
Expand Down Expand Up @@ -567,6 +569,7 @@ def run(self, setting, default):
'ksp_compact_variables' : 'Compact Variables',
'ksp_extra_checks' : 'Extra Syntax Checks',
'ksp_optimize_code' : 'Optimize Compiled Code',
'ksp_additional_branch_optimization' : 'Additional Branch Optimization Steps',
'ksp_combine_callbacks' : 'Combine Duplicate Callbacks',
'ksp_add_compiled_date' : 'Add Compilation Date/Time Comment',
'ksp_sanitize_exit_command' : 'Sanitize Behavior of \'exit\' Command',
Expand Down

0 comments on commit e58deda

Please sign in to comment.