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

ErgoScript compiler without optimizations #1022

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
23 changes: 23 additions & 0 deletions data/shared/src/main/scala/sigma/ast/values.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sigma.ast
import debox.cfor
import sigma.Extensions.ArrayOps
import sigma._
import sigma.ast.FuncValue.{AddToEnvironmentDesc, AddToEnvironmentDesc_CostKind}
import sigma.ast.SCollection.SByteArray
import sigma.ast.TypeCodes.ConstantCode
import sigma.ast.syntax._
Expand Down Expand Up @@ -1387,6 +1388,28 @@ case class Lambda(

/** This is not used as operation, but rather to form a program structure */
override def opType: SFunc = SFunc(Vector(), tpe)

protected final override def eval(env: DataEnv)(implicit E: ErgoTreeEvaluator): Any = {
addCost(FuncValue.costKind)
if (args.length == 1) {
val arg0 = args(0)
(vArg: Any) => {
Value.checkType(arg0._2, vArg)
var env1: DataEnv = null
E.addFixedCost(AddToEnvironmentDesc_CostKind, AddToEnvironmentDesc) {
env1 = env + (arg0._1 -> vArg)
}
body.map { b =>
val res = b.evalTo[Any](env1)
Value.checkType(b, res)
res
}.getOrElse(error(s"Lambda body is missing: $this"))
}
} else {
error(s"Function must have 1 argument, but was: $this")
}
}

}

object Lambda extends ValueCompanion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ abstract class ErgoTreeEvaluator {

object ErgoTreeEvaluator {
/** Immutable data environment used to assign data values to graph nodes. */
type DataEnv = Map[Int, Any]
type DataEnv = Map[Any, Any]
}
5 changes: 4 additions & 1 deletion data/shared/src/main/scala/sigma/eval/EvalSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ case class EvalSettings(
*
* @see ErgoTreeEvaluator
*/
scriptCostLimitInEvaluator: Int = 1000000
scriptCostLimitInEvaluator: Int = 1000000,

/** If true, then test vectors are checked against the expected values. */
isCheckTestVectors: Boolean = true
)

object EvalSettings {
Expand Down
20 changes: 14 additions & 6 deletions sc/shared/src/main/scala/sigma/compiler/SigmaCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SCollectionMethods.{ExistsMethod, ForallMethod, MapMethod}
import sigma.compiler.ir.{GraphIRReflection, IRContext}
import sigma.compiler.phases.{SigmaBinder, SigmaTyper}
import sigmastate.InterpreterReflection
import sigmastate.lang.SigmaParser
import sigmastate.lang.{DirectCompiler, SigmaParser}

/**
* @param networkPrefix network prefix to decode an ergo address from string (PK op)
Expand All @@ -26,11 +26,13 @@ import sigmastate.lang.SigmaParser
* call can be lowered to MapCollection node.
* The lowering if preferable, because it is more compact (1 byte
* for MapCollection instead of 3 bytes for MethodCall).
* @param enableOptimizations if true, then optimizations are applied to the compiled ErgoTree
*/
case class CompilerSettings(
networkPrefix: NetworkPrefix,
builder: SigmaBuilder,
lowerMethodCalls: Boolean
lowerMethodCalls: Boolean,
enableOptimizations: Boolean = true
)

/** Result of ErgoScript source code compilation.
Expand All @@ -42,7 +44,7 @@ case class CompilerSettings(
case class CompilerResult[Ctx <: IRContext](
env: ScriptEnv,
code: String,
compiledGraph: Ctx#Ref[Ctx#Context => Any],
compiledGraph: Option[Ctx#Ref[Ctx#Context => Any]],
/** Tree obtained from graph created by GraphBuilding */
buildTree: SValue
)
Expand Down Expand Up @@ -98,9 +100,15 @@ class SigmaCompiler private(settings: CompilerSettings) {
.zipWithIndex
.map { case ((name, t), index) => name -> ConstantPlaceholder(index, t) }
.toMap
val compiledGraph = IR.buildGraph(env ++ placeholdersEnv, typedExpr)
val compiledTree = IR.buildTree(compiledGraph)
CompilerResult(env, "<no source code>", compiledGraph, compiledTree)
if (settings.enableOptimizations) {
val compiledGraph = IR.buildGraph(env ++ placeholdersEnv, typedExpr)
val compiledTree = IR.buildTree(compiledGraph)
CompilerResult(env, "<no source code>", Some(compiledGraph), compiledTree)
} else {
val dc = new DirectCompiler(settings)
val compiledTree = dc.compileTyped(typedExpr)
CompilerResult(env, "<no source code>", None, compiledTree)
}
}

/** Compiles the given parsed contract source. */
Expand Down
Loading
Loading