changeset 2452:8b3daed1c076

Allow more if statements to be constant folded in CPU DSL
author Michael Pavone <pavone@retrodev.com>
date Mon, 19 Feb 2024 18:14:12 -0800
parents edd73a009537
children 7d7525769ce2
files cpu_dsl.py
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/cpu_dsl.py	Mon Feb 19 17:55:45 2024 -0800
+++ b/cpu_dsl.py	Mon Feb 19 18:14:12 2024 -0800
@@ -1151,6 +1151,11 @@
 		'!=': _neqCImpl
 	}
 }
+_ifCmpEval = {
+	'>=U': lambda a, b: a >= b,
+	'=': lambda a, b: a == b,
+	'!=': lambda a, b: a != b
+}
 #represents a DSL conditional construct
 class If(ChildBlock):
 	def __init__(self, parent, cond):
@@ -1214,6 +1219,13 @@
 			self._genConstParam(prog.checkBool(self.cond), prog, fieldVals, output, otype)
 		else:
 			if self.cond in _ifCmpImpl[otype]:
+				if prog.lastOp.op == 'cmp':
+					params = [prog.resolveParam(p, parent, fieldVals) for p in prog.lastOp.params]
+					if type(params[0]) is int and type(params[1]) is int:
+						output.pop()
+						res = _ifCmpEval[self.cond](params[1], params[0])
+						self._genConstParam(res, prog, fieldVals, output, otype)
+						return
 				oldCond = prog.conditional
 				prog.conditional = True
 				#temp = prog.temp.copy()