Skip to content

Commit

Permalink
Assume boxed regions
Browse files Browse the repository at this point in the history
  • Loading branch information
serkm committed Oct 22, 2024
1 parent 97538fd commit fc4c49d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,31 +198,17 @@ object Transformer {
case machine.Var(ref @ machine.Variable(name, machine.Type.Reference(tpe)), init, retType, rest) =>
val environment = List(init)
val returnAddressName = freshName("returnAddress")
retType match {
case Some(retType) =>
val returnType = transform(retType)
val returnValue = freshName("returnValue")
val parameters = List(Parameter(returnType, returnValue))
defineLabel(returnAddressName, parameters) {
emit(Comment(s"var $name / return address"))
popEnvironmentFrom(getStack(), environment)
eraseValue(init)
val nextReturn = LocalReference(returnAddressType, freshName("returnAddress"))
popReturnAddressFrom(getStack(), nextReturn.name)
emit(callLabel(nextReturn, List(LocalReference(returnType, returnValue))))
RetVoid()
}

case None =>
defineFunction(returnAddressName, List(Parameter(stackType, "stack"))) {
emit(Comment(s"region var $name / return address"))
popEnvironmentFrom(getStack(), environment)
eraseValue(init)
val nextReturn = LocalReference(returnAddressType, freshName("returnAddress"))
popReturnAddressFrom(getStack(), nextReturn.name)
emit(Call("", Ccc(), VoidType(), nextReturn, List(getStack())))
RetVoid()
}
val returnType = transform(retType)
val returnValue = freshName("returnValue")
val parameters = List(Parameter(returnType, returnValue))
defineLabel(returnAddressName, parameters) {
emit(Comment(s"var $name / return address"))
popEnvironmentFrom(getStack(), environment)
eraseValue(init)
val nextReturn = LocalReference(returnAddressType, freshName("returnAddress"))
popReturnAddressFrom(getStack(), nextReturn.name)
emit(callLabel(nextReturn, List(LocalReference(returnType, returnValue))))
RetVoid()
}

val sharerName = freshName("sharer");
Expand Down Expand Up @@ -349,13 +335,9 @@ object Transformer {
emit(callLabel(returnAddress, values.map(transform)))
RetVoid()

case machine.Reset(prompt, frame, isRegion, rest) =>
case machine.Reset(prompt, frame, rest) =>
emit(Comment(s"Reset ${prompt.name}"))

if (isRegion) {
pushReturnAddressOnto(getStack(), "nop", shareFrames.name, eraseFrames.name)
}

val newStack = LocalReference(stackType, freshName("stack"))
emit(Call(newStack.name, Ccc(), stackType, reset, List(getStack())));
setStack(newStack)
Expand All @@ -375,11 +357,6 @@ object Transformer {
val nextStack = LocalReference(stackType, freshName("stack"));
emit(Call(nextStack.name, Ccc(), nextStack.tpe, underflowStack, List(getStack())));
setStack(nextStack);
if (isRegion) {
val clearRegion = freshName("clearRegion")
popReturnAddressFrom(getStack(), clearRegion)
emit(Call("", Ccc(), VoidType(), LocalReference(PointerType(), clearRegion), List(getStack())))
}

transform(frame.body);
}
Expand Down
2 changes: 1 addition & 1 deletion effekt/shared/src/main/scala/effekt/machine/Analysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def freeVariables(statement: Statement): Set[Variable] =
freeVariables(frame) ++ freeVariables(rest)
case Return(values) =>
Set.from(values)
case Reset(prompt, frame, isRegion, rest) =>
case Reset(prompt, frame, rest) =>
freeVariables(frame) ++ (freeVariables(rest) -- Set(prompt))
case Resume(value, rest) =>
Set(value) ++ freeVariables(rest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ object PrettyPrinter extends ParenPrettyPrinter {
case Return(arguments) =>
"return" <+> hsep(arguments map toDoc, ",")

case Reset(prompt, frame, isRegion, rest) =>
case Reset(prompt, frame, rest) =>
"let" <+> prompt <+> "=" <+> "reset" <+> toDoc(frame) <> ";" <> line <> toDoc(rest)

case Resume(stack, rest) =>
Expand Down
10 changes: 5 additions & 5 deletions effekt/shared/src/main/scala/effekt/machine/Transformer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ object Transformer {
val returnClause = Clause(List(variable), Return(List(variable)))
val prompt = Variable(freshName("prompt"), Type.Prompt())

Reset(prompt, returnClause, false,
Reset(prompt, returnClause,
(bparams zip handlers).foldRight(transform(body)){
case ((id, handler), body) =>
New(transform(id), transform(handler, Some(prompt)), body)
Expand All @@ -222,7 +222,7 @@ object Transformer {
val returnClause = Clause(List(variable), Return(List(variable)))
val prompt = transform(region)

Reset(prompt, returnClause, true, transform(body))
Reset(prompt, returnClause, transform(body))

case core.Alloc(id, init, region, body) =>
transform(init).run { value =>
Expand All @@ -241,11 +241,11 @@ object Transformer {
val globalPrompt = Variable(freshName("global"), Type.Prompt())
LiteralInt(globalPrompt, 2L,
Shift(temporary, globalPrompt,
Var(reference, value, None,
Var(reference, value, Type.Positive(),
Resume(temporary, transform(body)))))
case _ =>
Shift(temporary, prompt,
Var(reference, value, None,
Var(reference, value, Type.Positive(),
Resume(temporary, transform(body))))
}
}
Expand All @@ -256,7 +256,7 @@ object Transformer {
val prompt = Variable(freshName("prompt"), Type.Prompt())

transform(init).run { value =>
Var(reference, value, Some(transform(body.tpe)),
Var(reference, value, transform(body.tpe),
transform(body))
}

Expand Down
4 changes: 2 additions & 2 deletions effekt/shared/src/main/scala/effekt/machine/Tree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ enum Statement {
/**
* e.g. var x = 42; s
*/
case Var(name: Variable, init: Variable, returnType: Option[Type], rest: Statement)
case Var(name: Variable, init: Variable, returnType: Type, rest: Statement)

/**
* e.g. let y = loadVar(x); s
Expand All @@ -171,7 +171,7 @@ enum Statement {
/**
* e.g. let prompt = reset { (x, ...) => s }; s
*/
case Reset(name: Variable, frame: Clause, isRegion: Boolean, rest: Statement)
case Reset(name: Variable, frame: Clause, rest: Statement)

/**
* e.g. resume k; s
Expand Down

0 comments on commit fc4c49d

Please sign in to comment.