Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite IR Interpreter #241

Closed
wants to merge 65 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
182c9e8
refactor call to a statement & add unreachable and return jumps
ailrst Aug 19, 2024
3711e4d
move transforms out of RunUtils.scala
ailrst Aug 19, 2024
fd56c9a
remove old cfg
ailrst Aug 9, 2024
bd6b2ad
update docs
ailrst Aug 19, 2024
96b9028
pe based expr evaluator
ailrst Aug 19, 2024
d264b97
interpreter test
ailrst Aug 19, 2024
a3bf83b
rewrite interpreter in functional style
ailrst Aug 22, 2024
74c4bc2
cleanup call/return
ailrst Aug 22, 2024
9e23558
cleanup memory ops to enter effects
ailrst Aug 22, 2024
7571aa3
tracing interpreter
ailrst Aug 26, 2024
f36c13e
compile with state monad
ailrst Aug 27, 2024
2c16c83
fix state monad interp
ailrst Aug 28, 2024
dd11175
indirect calls
ailrst Aug 28, 2024
c7b4a56
tracing interpreter
ailrst Aug 28, 2024
f64f904
breakpoints
ailrst Aug 28, 2024
897c565
improve breakpoints
ailrst Aug 29, 2024
cc97052
reorg
ailrst Aug 29, 2024
695b2d2
refactor with statemonad[s, either[v]]
ailrst Aug 29, 2024
00b482a
redo error handling
ailrst Aug 29, 2024
1a139d2
refactor call to a statement & add unreachable and return jumps
ailrst Aug 19, 2024
7e94536
move transforms out of RunUtils.scala
ailrst Aug 19, 2024
aae0064
remove old cfg
ailrst Aug 9, 2024
e2c0cd4
update docs
ailrst Aug 19, 2024
a3adee3
fix
ailrst Aug 30, 2024
4a92c99
fix externals
ailrst Aug 30, 2024
b994c99
disable IDE analyses if mainproc is external
ailrst Aug 30, 2024
b83c9ff
Merge branch 'call-statement' into interpreter
ailrst Aug 30, 2024
d6c5767
work on differential testing
ailrst Aug 30, 2024
2a03a23
hook for dynlinking
ailrst Sep 2, 2024
8b694c0
load full symtab
ailrst Sep 2, 2024
57ca6b3
update relf grammar
ailrst Sep 3, 2024
b276981
init bss
ailrst Sep 3, 2024
888d360
cleanup
ailrst Sep 3, 2024
0abf654
cleanup init trace
ailrst Sep 3, 2024
9192ce3
intrinsic stub and cleanup errors
ailrst Sep 3, 2024
5342358
init relocation table
ailrst Sep 3, 2024
87b0f86
pull stepper outside effects to fix interpreter composition again
ailrst Sep 4, 2024
f5a0590
cleanup
ailrst Sep 4, 2024
a6b58e8
cleanup
ailrst Sep 4, 2024
dd64c14
constprop test with interpreter
ailrst Sep 4, 2024
8be5cb6
interpreter docs
ailrst Sep 4, 2024
d2a2486
fix list
ailrst Sep 4, 2024
d06e268
paragraph
ailrst Sep 4, 2024
4ae3997
trap eval exceptions to monadic errors
ailrst Sep 4, 2024
4fc8ed2
improve interpretOne
ailrst Sep 4, 2024
8f966ce
notes on initialisation
ailrst Sep 4, 2024
f00b44b
simplify invoc funcs
ailrst Sep 4, 2024
ceef233
note missing features
ailrst Sep 5, 2024
cff6c97
run through all system tests
ailrst Sep 5, 2024
822c127
add resource limit
ailrst Sep 5, 2024
9b8f715
doc resource limit
ailrst Sep 5, 2024
648a41d
tweak doc
ailrst Sep 5, 2024
dfa7209
fix
ailrst Sep 5, 2024
38f871c
tweak interpretrlimit
ailrst Sep 5, 2024
6559180
basic malloc implementation
ailrst Sep 9, 2024
c6e938d
implement printf
ailrst Sep 10, 2024
285099b
cleanup intrins
ailrst Sep 10, 2024
aac7f5c
cleanup
ailrst Sep 23, 2024
1c2a5d5
Merge remote-tracking branch 'upstream/main' into interpreter
ailrst Sep 23, 2024
46ff66a
cleanup
ailrst Sep 23, 2024
970c55a
Merge branch 'main' into interpreter
Nov 8, 2024
1a8067c
make compiler options consistent
Nov 8, 2024
32bf439
fix use of deprecated examples
Nov 8, 2024
9cc3c31
fix use of deprecated examples
Nov 8, 2024
fe3678f
give State filename consistent with Package, formatting, clean up imp…
Nov 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tracing interpreter
  • Loading branch information
ailrst committed Aug 26, 2024
commit 7571aa3a82d5454a3513e2fcf980139b68524b67
42 changes: 27 additions & 15 deletions src/main/scala/ir/eval/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ sealed trait Effects[T <: Effects[T]] {

def storeMem(vname: String, update: Map[BasilValue, BasilValue]): T

def getNext : ExecutionContinuation

}

enum Effect:
Expand All @@ -454,30 +456,32 @@ case class TracingInterpreter(

/** effects * */
def setNext(c: ExecutionContinuation) = {
Logger.debug(s" eff : DONEXT $c")
TracingInterpreter(s.setNext(c), Effect.SetNext(c)::trace)
// Logger.debug(s" eff : DONEXT $c")
TracingInterpreter(s.setNext(c), trace)
}

def call(target: String, beginFrom: ExecutionContinuation, returnTo: ExecutionContinuation) = {
Logger.debug(s" eff : CALL $target")
//Logger.debug(s" eff : CALL $target")
TracingInterpreter(s.call(target, beginFrom, returnTo), Effect.Call(target, beginFrom, returnTo)::trace)
}

def doReturn() = {
Logger.debug(s" eff : RETURN")
//Logger.debug(s" eff : RETURN")
TracingInterpreter(s.doReturn(), Effect.Return::trace)
}

def storeVar(v: String, c: Scope, value: BasilValue) = {
Logger.debug(s" eff : SET $v := $value")
//Logger.debug(s" eff : SET $v := $value")
TracingInterpreter(s.storeVar(v, c, value), Effect.StoreVar(v, c, value)::trace)
}

def storeMem(vname: String, update: Map[BasilValue, BasilValue]) = {
Logger.debug(s" eff : STORE $vname <- $update")
//Logger.debug(s" eff : STORE $vname <- $update")
TracingInterpreter(s.storeMem(vname, update), Effect.StoreMem(vname, update)::trace)
}

def getNext = s.getNext

}

case class InterpreterState(
Expand All @@ -497,6 +501,8 @@ case class InterpreterState(

def loadMem(v: String, addrs: List[BasilValue]): List[BasilValue] = memoryState.doLoad(v, addrs)

def getNext = nextCmd

/** effects * */
def setNext(c: ExecutionContinuation): InterpreterState = {
InterpreterState(c, callStack, memoryState)
Expand Down Expand Up @@ -642,12 +648,12 @@ case object InterpFuns {
}

@tailrec
def interpret(s: InterpreterState): InterpreterState = {
Logger.debug(s"interpret ${s.nextCmd}")
s.nextCmd match {
def interpret[T <: Effects[T]](s: T): T = {
Logger.debug(s"interpret ${s.getNext}")
s.getNext match {
case Run(c: Statement) =>
interpret(
protect[InterpreterState](
protect[T](
() => interpretStatement(s, c),
{
case InterpreterError(e) => s.setNext(e)
Expand All @@ -657,7 +663,7 @@ case object InterpFuns {
)
case Run(c: Jump) =>
interpret(
protect[InterpreterState](
protect[T](
() => interpretJump(s, c),
{
case InterpreterError(e) => s.setNext(e)
Expand All @@ -670,13 +676,19 @@ case object InterpFuns {
}
}

def interpretProg(p: Program): InterpreterState = {
var s = initialiseProgram(p, InterpreterState())
def interpretProg[T <: Effects[T]](p: Program, i: T): T = {
var s = initialiseProgram(p, i)
interpret(s)
}

}

def interpret(IRProgram: Program) = {
InterpFuns.interpretProg(IRProgram)
def interpret(IRProgram: Program) : InterpreterState = {
InterpFuns.interpretProg(IRProgram, InterpreterState())
}

def interpretTrace(IRProgram: Program) : TracingInterpreter = {
val s : TracingInterpreter = InterpFuns.interpretProg(IRProgram, TracingInterpreter(InterpreterState(), List()))
s
}

113 changes: 63 additions & 50 deletions src/test/scala/ir/InterpreterTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,61 +286,61 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
testInterpret("no_interference_update_y", expected)
}

test("fibonacci") {

def fibonacciProg(n: Int) = {
def expected(n: Int) : Int = {
n match {
case 0 => 0
case 1 => 1
case n => expected(n - 1) + expected(n - 2)
}
def fibonacciProg(n: Int) = {
def expected(n: Int) : Int = {
n match {
case 0 => 0
case 1 => 1
case n => expected(n - 1) + expected(n - 2)
}
prog(
proc("begin",
block("entry",
Assign(R8, Register("R31", 64)),
Assign(R0, bv64(n)),
directCall("fib"),
goto("done")
),
block("done",
Assert(BinaryExpr(BVEQ, R0, bv64(expected(n)))),
ret
)),
proc("fib",
block("base", goto("base1", "base2", "dofib")),
block("base1",
Assume(BinaryExpr(BVEQ, R0, bv64(0))),
ret),
block("base2",
Assume(BinaryExpr(BVEQ, R0, bv64(1))),
ret),
block("dofib",
Assume(BinaryExpr(BoolAND, BinaryExpr(BVNEQ, R0, bv64(0)), BinaryExpr(BVNEQ, R0, bv64(1)))),
// R8 stack pointer preserved across calls
Assign(R7, BinaryExpr(BVADD, R8, bv64(8))),
MemoryAssign(stack, R7, R8, Endian.LittleEndian, 64), // sp
Assign(R8, R7),
Assign(R8, BinaryExpr(BVADD, R8, bv64(8))), // sp + 8
MemoryAssign(stack, R8, R0, Endian.LittleEndian, 64), // [sp + 8] = arg0
Assign(R0, BinaryExpr(BVSUB, R0, bv64(1))),
directCall("fib"),
Assign(R2, R8), // sp + 8
Assign(R8, BinaryExpr(BVADD, R8, bv64(8))), // sp + 16
MemoryAssign(stack, R8, R0, Endian.LittleEndian, 64), // [sp + 16] = r1
Assign(R0, MemoryLoad(stack, R2, Endian.LittleEndian, 64)), // [sp + 8]
Assign(R0, BinaryExpr(BVSUB, R0, bv64(2))),
directCall("fib"),
Assign(R2, MemoryLoad(stack, R8, Endian.LittleEndian, 64)), // [sp + 16] (r1)
Assign(R0, BinaryExpr(BVADD, R0, R2)),
Assign(R8, MemoryLoad(stack, BinaryExpr(BVSUB, R8, bv64(16)), Endian.LittleEndian, 64)),
ret
)
}
prog(
proc("begin",
block("entry",
Assign(R8, Register("R31", 64)),
Assign(R0, bv64(n)),
directCall("fib"),
goto("done")
),
block("done",
Assert(BinaryExpr(BVEQ, R0, bv64(expected(n)))),
ret
)),
proc("fib",
block("base", goto("base1", "base2", "dofib")),
block("base1",
Assume(BinaryExpr(BVEQ, R0, bv64(0))),
ret),
block("base2",
Assume(BinaryExpr(BVEQ, R0, bv64(1))),
ret),
block("dofib",
Assume(BinaryExpr(BoolAND, BinaryExpr(BVNEQ, R0, bv64(0)), BinaryExpr(BVNEQ, R0, bv64(1)))),
// R8 stack pointer preserved across calls
Assign(R7, BinaryExpr(BVADD, R8, bv64(8))),
MemoryAssign(stack, R7, R8, Endian.LittleEndian, 64), // sp
Assign(R8, R7),
Assign(R8, BinaryExpr(BVADD, R8, bv64(8))), // sp + 8
MemoryAssign(stack, R8, R0, Endian.LittleEndian, 64), // [sp + 8] = arg0
Assign(R0, BinaryExpr(BVSUB, R0, bv64(1))),
directCall("fib"),
Assign(R2, R8), // sp + 8
Assign(R8, BinaryExpr(BVADD, R8, bv64(8))), // sp + 16
MemoryAssign(stack, R8, R0, Endian.LittleEndian, 64), // [sp + 16] = r1
Assign(R0, MemoryLoad(stack, R2, Endian.LittleEndian, 64)), // [sp + 8]
Assign(R0, BinaryExpr(BVSUB, R0, bv64(2))),
directCall("fib"),
Assign(R2, MemoryLoad(stack, R8, Endian.LittleEndian, 64)), // [sp + 16] (r1)
Assign(R0, BinaryExpr(BVADD, R0, R2)),
Assign(R8, MemoryLoad(stack, BinaryExpr(BVSUB, R8, bv64(16)), Endian.LittleEndian, 64)),
ret
)
)
}
)
}

test("fibonacci") {

val fib = fibonacciProg(8)
val r = interpret(fib)
Expand All @@ -352,4 +352,17 @@ class InterpreterTests extends AnyFunSuite with BeforeAndAfter {
// }

}


test("fibonacci Trace") {

val fib = fibonacciProg(8)
val r = interpretTrace(fib)
assert(r.getNext == Stopped())
// Show interpreted result
//
info(r.trace.reverse.mkString("\n"))

}

}