Skip to content

Merge several IR nodes into UnaryOp. #5088

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

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 14 additions & 10 deletions compiler/src/main/scala/org/scalajs/nscplugin/GenJSCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2572,9 +2572,10 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
ex match {
case js.New(cls, _, _) if cls != JavaScriptExceptionClassName =>
// Common case where ex is neither null nor a js.JavaScriptException
js.Throw(ex)
js.UnaryOp(js.UnaryOp.Throw, ex)
case _ =>
js.Throw(js.UnwrapFromThrowable(ex))
js.UnaryOp(js.UnaryOp.Throw,
js.UnaryOp(js.UnaryOp.UnwrapFromThrowable, js.UnaryOp(js.UnaryOp.CheckNotNull, ex)))
}

/* !!! Copy-pasted from `CleanUp.scala` upstream and simplified with
Expand Down Expand Up @@ -3108,13 +3109,14 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)

val (exceptValDef, exceptVar) = if (mightCatchJavaScriptException) {
val valDef = js.VarDef(freshLocalIdent("e"), NoOriginalName,
encodeClassType(ThrowableClass), mutable = false, js.WrapAsThrowable(origExceptVar))
encodeClassType(ThrowableClass), mutable = false,
js.UnaryOp(js.UnaryOp.WrapAsThrowable, origExceptVar))
(valDef, valDef.ref)
} else {
(js.Skip(), origExceptVar)
}

val elseHandler: js.Tree = js.Throw(origExceptVar)
val elseHandler: js.Tree = js.UnaryOp(js.UnaryOp.Throw, origExceptVar)

val handler = catches.foldRight(elseHandler) { (caseDef, elsep) =>
implicit val pos = caseDef.pos
Expand Down Expand Up @@ -3323,7 +3325,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
private def genThrowClassCastException()(implicit pos: Position): js.Tree = {
val ctor = ClassCastExceptionClass.info.member(
nme.CONSTRUCTOR).suchThat(_.tpe.params.isEmpty)
js.Throw(genNew(ClassCastExceptionClass, ctor, Nil))
js.UnaryOp(js.UnaryOp.Throw, genNew(ClassCastExceptionClass, ctor, Nil))
}

/** Gen JS code for a super call, of the form Class.super[mix].fun(args).
Expand Down Expand Up @@ -4891,7 +4893,8 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
js.Assign(genSelect(fun.tpe.paramTypes(1)), arguments(1))
} else {
// length of the array
js.ArrayLength(arrayValue)
js.UnaryOp(js.UnaryOp.Array_length,
js.UnaryOp(js.UnaryOp.CheckNotNull, arrayValue))
}
}

Expand Down Expand Up @@ -5326,7 +5329,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)
case IDENTITY_HASH_CODE =>
// runtime.identityHashCode(arg)
val arg = genArgs1
js.IdentityHashCode(arg)
js.UnaryOp(js.UnaryOp.IdentityHashCode, arg)

case DEBUGGER =>
// js.special.debugger()
Expand Down Expand Up @@ -5463,7 +5466,7 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)

case JS_THROW =>
// js.special.throw(arg)
js.Throw(genArgs1)
js.UnaryOp(js.UnaryOp.Throw, genArgs1)

case JS_TRY_CATCH =>
/* js.special.tryCatch(arg1, arg2)
Expand Down Expand Up @@ -5502,11 +5505,12 @@ abstract class GenJSCode[G <: Global with Singleton](val global: G)

case WRAP_AS_THROWABLE =>
// js.special.wrapAsThrowable(arg)
js.WrapAsThrowable(genArgs1)
js.UnaryOp(js.UnaryOp.WrapAsThrowable, genArgs1)

case UNWRAP_FROM_THROWABLE =>
// js.special.unwrapFromThrowable(arg)
js.UnwrapFromThrowable(genArgs1)
js.UnaryOp(js.UnaryOp.UnwrapFromThrowable,
js.UnaryOp(js.UnaryOp.CheckNotNull, genArgs1))

case LINKTIME_PROPERTY =>
// LinkingInfo.linkTimePropertyXXX("...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ trait GenJSExports[G <: Global with Singleton] extends SubComponent {

private def genThrowTypeError(msg: String = "No matching overload")(
implicit pos: Position): js.Tree = {
js.Throw(js.StringLiteral(msg))
js.UnaryOp(js.UnaryOp.Throw, js.StringLiteral(msg))
}

class FormalArgsRegistry(minArgc: Int, needsRestParam: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ class OptimizationTest extends JSASTTest {
}
}
""".hasNot("WrapAsThrowable") {
case js.WrapAsThrowable(_) =>
case js.UnaryOp(js.UnaryOp.WrapAsThrowable, _) =>
}

// Confidence check
Expand All @@ -536,7 +536,7 @@ class OptimizationTest extends JSASTTest {
}
}
""".hasExactly(1, "WrapAsThrowable") {
case js.WrapAsThrowable(_) =>
case js.UnaryOp(js.UnaryOp.WrapAsThrowable, _) =>
}
}

Expand Down
28 changes: 0 additions & 28 deletions ir/shared/src/main/scala/org/scalajs/ir/Hashers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,6 @@ object Hashers {
mixTree(finalizer)
mixType(tree.tpe)

case Throw(expr) =>
mixTag(TagThrow)
mixTree(expr)

case Match(selector, cases, default) =>
mixTag(TagMatch)
mixTree(selector)
Expand Down Expand Up @@ -331,10 +327,6 @@ object Hashers {
mixArrayTypeRef(typeRef)
mixTrees(elems)

case ArrayLength(array) =>
mixTag(TagArrayLength)
mixTree(array)

case ArraySelect(array, index) =>
mixTag(TagArraySelect)
mixTree(array)
Expand Down Expand Up @@ -362,26 +354,6 @@ object Hashers {
mixTree(expr)
mixType(tpe)

case GetClass(expr) =>
mixTag(TagGetClass)
mixTree(expr)

case Clone(expr) =>
mixTag(TagClone)
mixTree(expr)

case IdentityHashCode(expr) =>
mixTag(TagIdentityHashCode)
mixTree(expr)

case WrapAsThrowable(expr) =>
mixTag(TagWrapAsThrowable)
mixTree(expr)

case UnwrapFromThrowable(expr) =>
mixTag(TagUnwrapFromThrowable)
mixTree(expr)

case JSNew(ctor, args) =>
mixTag(TagJSNew)
mixTree(ctor)
Expand Down
105 changes: 40 additions & 65 deletions ir/shared/src/main/scala/org/scalajs/ir/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,6 @@ object Printers {
print(" finally ")
printBlock(finalizer)

case Throw(expr) =>
print("throw ")
print(expr)

case Match(selector, cases, default) =>
print("match (")
print(selector)
Expand Down Expand Up @@ -347,40 +343,47 @@ object Printers {
case UnaryOp(op, lhs) =>
import UnaryOp._

if (op < String_length) {
print('(')
print((op: @switch) match {
case Boolean_! =>
"!"
case IntToChar =>
"(char)"
case IntToByte =>
"(byte)"
case IntToShort =>
"(short)"
case CharToInt | ByteToInt | ShortToInt | LongToInt | DoubleToInt =>
"(int)"
case IntToLong | DoubleToLong =>
"(long)"
case DoubleToFloat | LongToFloat =>
"(float)"
case IntToDouble | LongToDouble | FloatToDouble =>
"(double)"
})
print(lhs)
print(')')
} else {
def p(prefix: String, suffix: String): Unit = {
print(prefix)
print(lhs)
print((op: @switch) match {
case String_length => ".length"
case CheckNotNull => ".notNull"
case Class_name => ".name"
case Class_isPrimitive => ".isPrimitive"
case Class_isInterface => ".isInterface"
case Class_isArray => ".isArray"
case Class_componentType => ".componentType"
case Class_superClass => ".superClass"
})
print(suffix)
}

(op: @switch) match {
case Boolean_! =>
p("(!", ")")
case IntToChar =>
p("((char)", ")")
case IntToByte =>
p("((byte)", ")")
case IntToShort =>
p("((short)", ")")
case CharToInt | ByteToInt | ShortToInt | LongToInt | DoubleToInt =>
p("((int)", ")")
case IntToLong | DoubleToLong =>
p("((long)", ")")
case DoubleToFloat | LongToFloat =>
p("((float)", ")")
case IntToDouble | LongToDouble | FloatToDouble =>
p("((double)", ")")

case String_length => p("", ".length")
case CheckNotNull => p("", ".notNull")
case Class_name => p("", ".name")
case Class_isPrimitive => p("", ".isPrimitive")
case Class_isInterface => p("", ".isInterface")
case Class_isArray => p("", ".isArray")
case Class_componentType => p("", ".componentType")
case Class_superClass => p("", ".superClass")
case Array_length => p("", ".length")
case GetClass => p("", ".getClass()")

case Clone => p("<clone>(", ")")
case IdentityHashCode => p("<identityHashCode>(", ")")
case WrapAsThrowable => p("<wrapAsThrowable>(", ")")
case UnwrapFromThrowable => p("<unwrapFromThrowable>(", ")")

case Throw => p("throw ", "")
}

case BinaryOp(BinaryOp.Int_-, IntLiteral(0), rhs) =>
Expand Down Expand Up @@ -525,10 +528,6 @@ object Printers {
print(typeRef)
printArgs(elems)

case ArrayLength(array) =>
print(array)
print(".length")

case ArraySelect(array, index) =>
print(array)
print('[')
Expand Down Expand Up @@ -564,30 +563,6 @@ object Printers {
print(tpe)
print(']')

case GetClass(expr) =>
print(expr)
print(".getClass()")

case Clone(expr) =>
print("<clone>(")
print(expr)
print(')')

case IdentityHashCode(expr) =>
print("<identityHashCode>(")
print(expr)
print(')')

case WrapAsThrowable(expr) =>
print("<wrapAsThrowable>(")
print(expr)
print(")")

case UnwrapFromThrowable(expr) =>
print("<unwrapFromThrowable>(")
print(expr)
print(")")

// JavaScript expressions

case JSNew(ctor, args) =>
Expand Down
Loading
Loading