Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnaldo committed Sep 20, 2024
1 parent 92af7dc commit 37229b8
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 53 deletions.
9 changes: 7 additions & 2 deletions src/main/scala/esmeta/peval/PartialEvaluator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ class PartialEvaluator(
}
(ISeq(newInsts), newPst)
case ISdoCall(lhs, base, method, args) =>
pst.define(lhs, Unknown)
(inst, pst)
val (pv, newBase) = peval(base, pst)
pv match
// TODO: local variable inline: <varname>_<fid>_<ctxtcounter>
// case Known(AstValue(ast)) =>
case _ =>
pst.define(lhs, Unknown)
(inst, pst)
case call @ ICall(lhs, fexpr, args) =>
pst.define(lhs, Unknown)
(inst, pst)
Expand Down
5 changes: 0 additions & 5 deletions src/main/scala/esmeta/peval/pstate/PCallContext.scala

This file was deleted.

8 changes: 8 additions & 0 deletions src/main/scala/esmeta/peval/pstate/PContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package esmeta.peval.pstate

import esmeta.peval.*
import esmeta.state.*
import esmeta.ir.{Func, Local}
import scala.collection.mutable.{Map => MMap}

case class PContext(func: Func, locals: MMap[Local, Predict[Value]])
10 changes: 1 addition & 9 deletions src/main/scala/esmeta/peval/pstate/PHeap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import esmeta.state.*
/** IR PHeap for partial Evaluation. similar to state/PHeap.scala */
case class PHeap(
val map: MMap[Addr, PObj] = MMap(),
var size: Int = 0,
var size: Int = 0, // TODO: move to PartialEvaluator
) extends StateElem {

/** getter */
Expand Down Expand Up @@ -84,11 +84,3 @@ case class PHeap(
size,
)
}

object PHeap {
def fromHeap(heap: esmeta.state.Heap): PHeap = ???
// PHeap(
// Map.from(heap.map).map((addr, obj) => (addr, PObj.from(obj))),
// heap.size,
// )
}
46 changes: 11 additions & 35 deletions src/main/scala/esmeta/peval/pstate/PState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@ import scala.util.{Try, Success}
import esmeta.state.*
import esmeta.peval.*

type PContext = Map[Local, Predict[Value]]

/** IR PStates */
case class PState(
// var func: IRFunc,
// var callStack: List[PCallContext] = Nil,
val globals: Map[Global, Predict[Value]] = Map(),
var locals: PContext = Map(),
val heap: PHeap = PHeap(),
val globals: Map[Global, Predict[Value]],
var callStack: List[PContext],
val context: PContext,
val heap: PHeap,
) extends StateElem {

def setContext(func: IRFunc): PState = PState(
// func,
// callStack,
globals,
locals,
heap,
)
inline def func = context.func
inline def locals = context.locals

/** getter */
def apply(rt: Predict[RefTarget])(using CFG): Predict[Value] = rt match
Expand Down Expand Up @@ -65,12 +57,12 @@ case class PState(
case _ => throw WrongStringRef(str, field)

/** address getter */
def apply(addr: Addr): PObj = ??? // heap(addr)
def apply(addr: Addr): PObj = heap(addr)

/** define variables */
def define(x: Var, value: Predict[Value]): Unit = x match
case x: Global =>
case x: Local => this.locals = locals + (x -> value)
case x: Global => /* do nothing */
case x: Local => locals += x -> value

/** setter */
def update(rt: RefTarget, value: Predict[Value]): Unit = rt match
Expand All @@ -79,8 +71,8 @@ case class PState(

/** variable setter */
def update(x: Var, value: Predict[Value]): Unit = x match
case x: Global =>
case x: Local => this.locals = locals + (x -> value)
case x: Global => /* do nothing */
case x: Local => locals += x -> value

/** field setter */
def update(base: Value, field: Value, value: Predict[Value]): PState = ???
Expand Down Expand Up @@ -138,19 +130,3 @@ case class PState(
// val addr -> pheap = heap.allocList(vs)
// (addr, this.replaced(heap = pheap))
}
object PState {

def fromState(st: State): PState = PState(
// st.func.irFunc,
// st.callStack.map((cc) =>
// PCallContext(
// cc.context.func.irFunc,
// Map.from(cc.context.locals).map((l, v) => (l, Known(v))),
// ),
// ),
Map.from(st.globals).map((l, v) => (l, Known(v))),
Map.from(st.context.locals).map((l, v) => (l, Known(v))),
PHeap.fromHeap(st.heap),
)

}
9 changes: 7 additions & 2 deletions src/main/scala/esmeta/phase/PEval.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ case object PEval extends Phase[CFG, Unit] {

for (fd <- fds) {

val st = PState(globals = globals)
val func = pevalTarget.irFunc
val st = PState(
globals = globals,
callStack = Nil,
context = PContext(func = func, locals = MMap()),
heap = PHeap(),
)
// .fromState(Initialize.fromFile(cfg, filename))
// .setContext(pevalTarget.irFunc)
// val (addr_empty_map, st) = st.allocMap(Nil)
val func = pevalTarget.irFunc
val addr_func_obj_record = st.allocRecord(
"ECMAScriptFunctionObject",
List(
Expand Down

0 comments on commit 37229b8

Please sign in to comment.