Skip to content

Commit

Permalink
i486-toBytes: added userDefinedInvoke handler to SMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed May 25, 2024
1 parent c76ac1a commit 2427db3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
6 changes: 6 additions & 0 deletions core/shared/src/main/scala/sigma/ast/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ package object ast {

def asNumType: SNumericType = tpe.asInstanceOf[SNumericType]

/** Cast this type to numeric type or else throws the given error. */
def asNumTypeOrElse(error: => Exception): SNumericType = tpe match {
case nt: SNumericType => nt
case _ => throw error
}

def asFunc: SFunc = tpe.asInstanceOf[SFunc]

def asProduct: SProduct = tpe.asInstanceOf[SProduct]
Expand Down
19 changes: 16 additions & 3 deletions data/shared/src/main/scala/sigma/ast/SMethod.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ case class SMethod(
explicitTypeArgs: Seq[STypeVar],
irInfo: MethodIRInfo,
docInfo: Option[OperationInfo],
costFunc: Option[MethodCostFunc]) {
costFunc: Option[MethodCostFunc],
userDefinedInvoke: Option[SMethod.InvokeHandler]
) {

/** Operation descriptor of this method. */
lazy val opDesc = MethodDesc(this)
Expand Down Expand Up @@ -112,7 +114,12 @@ case class SMethod(
/** Invoke this method on the given object with the arguments.
* This is used for methods with FixedCost costKind. */
def invokeFixed(obj: Any, args: Array[Any]): Any = {
javaMethod.invoke(obj, args.asInstanceOf[Array[AnyRef]]:_*)
userDefinedInvoke match {
case Some(h) =>
h(obj, args)
case None =>
javaMethod.invoke(obj, args.asInstanceOf[Array[AnyRef]]:_*)
}
}

// TODO optimize: avoid lookup when this SMethod is created via `specializeFor`
Expand Down Expand Up @@ -261,6 +268,12 @@ object SMethod {
*/
type InvokeDescBuilder = SFunc => Seq[SType]

/** Type of user-defined function which is called to handle method invocation.
* Instances of this type can be attached to [[SMethod]] instances.
* @see SNumericTypeMethods.ToBytesMethod
*/
type InvokeHandler = (Any, Array[Any]) => Any

/** Return [[Method]] descriptor for the given `methodName` on the given `cT` type.
* @param methodName the name of the method to lookup
* @param cT the class where to search the methodName
Expand Down Expand Up @@ -295,7 +308,7 @@ object SMethod {
): SMethod = {
SMethod(
objType, name, stype, methodId, costKind, explicitTypeArgs,
MethodIRInfo(None, None, None), None, None)
MethodIRInfo(None, None, None), None, None, None)
}


Expand Down
8 changes: 4 additions & 4 deletions sc/shared/src/main/scala/sigma/compiler/ir/TreeBuilding.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ trait TreeBuilding extends Base { IR: IRContext =>
object IsNumericUnOp {
def unapply(op: UnOp[_,_]): Option[SValue => SValue] = op match {
case NumericNegate(_) => Some({ v: SValue => builder.mkNegation(v.asNumValue) })
case NumericToBigEndianBytes(_) =>
case _: NumericToBigEndianBytes[_] =>
val mkNode = { v: SValue =>
val specMethod = SNumericTypeMethods.ToBytesMethod
.withConcreteTypes(Map(SNumericTypeMethods.tNum -> v.tpe))
builder.mkMethodCall(v.asNumValue, specMethod, IndexedSeq.empty)
val receiverType = v.tpe.asNumTypeOrElse(error(s"Expected numeric type, got: ${v.tpe}"))
val m = SMethod.fromIds(receiverType.typeId, SNumericTypeMethods.ToBytesMethod.methodId)
builder.mkMethodCall(v.asNumValue, m, IndexedSeq.empty)
}
Some(mkNode)
case _ => None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ class SigmaTyper(val builder: SigmaBuilder,
obj.tpe match {
case p: SProduct =>
MethodsContainer.getMethod(p, n) match {
case Some(method @ SMethod(_, _, genFunTpe @ SFunc(_, _, _), _, _, _, _, _, _)) =>
case Some(method: SMethod) =>
val genFunTpe = method.stype
val subst = Map(genFunTpe.tpeParams.head.ident -> rangeTpe)
val concrFunTpe = applySubst(genFunTpe, subst)
val expectedArgs = concrFunTpe.asFunc.tDom.tail
Expand Down

0 comments on commit 2427db3

Please sign in to comment.