comparison cpu_dsl.py @ 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 679c31768013
comparison
equal deleted inserted replaced
2451:edd73a009537 2452:8b3daed1c076
1149 '>=U': _geuCImpl, 1149 '>=U': _geuCImpl,
1150 '=': _eqCImpl, 1150 '=': _eqCImpl,
1151 '!=': _neqCImpl 1151 '!=': _neqCImpl
1152 } 1152 }
1153 } 1153 }
1154 _ifCmpEval = {
1155 '>=U': lambda a, b: a >= b,
1156 '=': lambda a, b: a == b,
1157 '!=': lambda a, b: a != b
1158 }
1154 #represents a DSL conditional construct 1159 #represents a DSL conditional construct
1155 class If(ChildBlock): 1160 class If(ChildBlock):
1156 def __init__(self, parent, cond): 1161 def __init__(self, parent, cond):
1157 self.op = 'if' 1162 self.op = 'if'
1158 self.parent = parent 1163 self.parent = parent
1212 self.regValues = parent.regValues 1217 self.regValues = parent.regValues
1213 if self.cond in prog.booleans: 1218 if self.cond in prog.booleans:
1214 self._genConstParam(prog.checkBool(self.cond), prog, fieldVals, output, otype) 1219 self._genConstParam(prog.checkBool(self.cond), prog, fieldVals, output, otype)
1215 else: 1220 else:
1216 if self.cond in _ifCmpImpl[otype]: 1221 if self.cond in _ifCmpImpl[otype]:
1222 if prog.lastOp.op == 'cmp':
1223 params = [prog.resolveParam(p, parent, fieldVals) for p in prog.lastOp.params]
1224 if type(params[0]) is int and type(params[1]) is int:
1225 output.pop()
1226 res = _ifCmpEval[self.cond](params[1], params[0])
1227 self._genConstParam(res, prog, fieldVals, output, otype)
1228 return
1217 oldCond = prog.conditional 1229 oldCond = prog.conditional
1218 prog.conditional = True 1230 prog.conditional = True
1219 #temp = prog.temp.copy() 1231 #temp = prog.temp.copy()
1220 output.append(_ifCmpImpl[otype][self.cond](prog, parent, fieldVals, output)) 1232 output.append(_ifCmpImpl[otype][self.cond](prog, parent, fieldVals, output))
1221 self._genTrueBody(prog, fieldVals, output, otype) 1233 self._genTrueBody(prog, fieldVals, output, otype)