Skip to content

Commit

Permalink
Fix: inline only if arguments are fully known
Browse files Browse the repository at this point in the history
  • Loading branch information
stonechoe committed Oct 7, 2024
1 parent d87350a commit 4ddcf1e
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/main/scala/esmeta/peval/PartialEvaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ class PartialEvaluator(
case Unknown =>
pst.define(newLhs, Unknown)
(ISdoCall(newLhs, newBase, method, vs.map(_._2)), pst)
case Known(ast: AstValue) if vs.map(_._1).exists(_.isEmpty) =>
pst.define(newLhs, Unknown)
(ISdoCall(newLhs, newBase, method, vs.map(_._2)), pst)
// TODO: local variable inline: <varname>_<fid>_<ctxtcounter>
// case Known(AstValue(ast)) =>
case Known(value) =>
Expand Down Expand Up @@ -311,7 +314,8 @@ class PartialEvaluator(
val (f, newFexpr) = peval(fexpr, pst)
val vs = args.map(e => peval(e, pst)) // TODO check order
f match
case Known(pclo @ PClo(callee, captured)) =>
case Known(pclo @ PClo(callee, captured))
if vs.map(_._1).forall(_.isDefined) =>
val calleeCtx = PContext(
func = callee.irFunc,
locals = MMap.empty,
Expand Down Expand Up @@ -362,14 +366,18 @@ class PartialEvaluator(
Success(ICall(newLhs, newFexpr, vs.map(_._2)), pst)
}.get

case Known(_: PClo) /* with Unknown argument */ =>
pst.define(newLhs, Unknown)
pst.heap.clear(vs.map(_._1))
(ICall(newLhs, newFexpr, vs.map(_._2)), pst)
case Known(_: PCont) => throwPeval"not yet supported"
case Known(v) => throw NoCallable(v)
case Unknown =>
pst.define(newLhs, Unknown)
pst.heap.clear(vs.map(_._1))
(ICall(newLhs, newFexpr, vs.map(_._2)), pst)

case INop() => (ISeq(Nil), pst)
case INop() => (INop(), pst)
case IAssign(ref, expr) =>
ref match
case x: Var =>
Expand Down Expand Up @@ -428,14 +436,20 @@ class PartialEvaluator(
(newInst.addCmt("not supported yet"), pst)

case IAssert(expr) =>
val (_, newExpr) = peval(expr, pst);
val (pv, newExpr) = peval(expr, pst);
val newInst = IAssert(newExpr);
(newInst.addCmt("assertion will be checked at runtime."), pst)
pv match
case Known(v) =>
v.asBool match
case true => (INop(), pst)
case false => throw AssertionFail(expr)
case Unknown =>
(newInst.addCmt("will be checked at runtime."), pst)

case IPrint(expr) =>
val (_, newExpr) = peval(expr, pst);
val newInst = IAssert(newExpr);
(newInst.addCmt("print will be done at runtime."), pst)
(newInst, pst)

case IWhile(cond, body) =>
val (cv, _) = peval(cond, pst)
Expand Down

0 comments on commit 4ddcf1e

Please sign in to comment.