Skip to content

Move dotty.runtime to scala.runtime #10711

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
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/backend/jvm/BCodeBodyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@ trait BCodeBodyBuilder extends BCodeSkelBuilder {
else {
val arity = app.meth.tpe.widenDealias.firstParamTypes.size - env.size
val returnsUnit = app.meth.tpe.widenDealias.resultType.classSymbol == defn.UnitClass
if (returnsUnit) requiredClass(("dotty.runtime.function.JProcedure" + arity))
else if (arity <= 2) requiredClass(("dotty.runtime.function.JFunction" + arity))
if (returnsUnit) requiredClass(("scala.runtime.function.JProcedure" + arity))
else if (arity <= 2) requiredClass(("scala.runtime.function.JFunction" + arity))
else requiredClass(("scala.Function" + arity))
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ class Definitions {
}
private var myDottyPredefModule: Symbol = _

@tu lazy val DottyArraysModule: Symbol = requiredModule("dotty.runtime.Arrays")
@tu lazy val DottyArraysModule: Symbol = requiredModule("scala.runtime.Arrays")
def newGenericArrayMethod(using Context): TermSymbol = DottyArraysModule.requiredMethod("newGenericArray")
def newArrayMethod(using Context): TermSymbol = DottyArraysModule.requiredMethod("newArray")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FunctionalInterfaces extends MiniPhase {
def phaseName: String = FunctionalInterfaces.name

private val functionName = "JFunction".toTermName
private val functionPackage = "dotty.runtime.function.".toTermName
private val functionPackage = "scala.runtime.function.".toTermName

override def transformClosure(tree: Closure)(using Context): Tree = {
val cls = tree.tpe.classSymbol.asClass
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
val tpe = x.tpe.widen.resultType.widen
val claz = x.symbol.owner.asClass
val thizClass = Literal(Constant(claz.info))
val helperModule = requiredModule("dotty.runtime.LazyVals")
val helperModule = requiredModule("scala.runtime.LazyVals")
val getOffset = Select(ref(helperModule), lazyNme.RLazyVals.getOffset)
var offsetSymbol: TermSymbol = null
var flag: Tree = EmptyTree
Expand All @@ -389,7 +389,7 @@ class LazyVals extends MiniPhase with IdentityDenotTransformer {
// compute or create appropriate offsetSymbol, bitmap and bits used by current ValDef
appendOffsetDefs.get(claz) match {
case Some(info) =>
val flagsPerLong = (64 / dotty.runtime.LazyVals.BITS_PER_LAZY_VAL).toInt
val flagsPerLong = (64 / scala.runtime.LazyVals.BITS_PER_LAZY_VAL).toInt
info.ord += 1
ord = info.ord % flagsPerLong
val id = info.ord / flagsPerLong
Expand Down Expand Up @@ -443,7 +443,7 @@ object LazyVals {
object lazyNme {
import Names.TermName
object RLazyVals {
import dotty.runtime.LazyVals.{Names => N}
import scala.runtime.LazyVals.{Names => N}
val get: TermName = N.get.toTermName
val setFlag: TermName = N.setFlag.toTermName
val wait4Notification: TermName = N.wait4Notification.toTermName
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/reference/changed-features/lazy-vals-init.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The Dotty compiler will generate code equivalent to:

```scala
class Foo {
import dotty.runtime.LazyVals
import scala.runtime.LazyVals
var value_0: Int = _
var bitmap: Long = 0L
val bitmap_offset: Long = LazyVals.getOffset(classOf[LazyCell], "bitmap")
Expand Down
31 changes: 31 additions & 0 deletions library/src/scala/runtime/Arrays.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package scala.runtime

import scala.reflect.ClassTag

import java.lang.{reflect => jlr}

/** All but the first two operations should be short-circuited and implemented specially by
* the backend.
*/
object Arrays {

// note: this class is magical. Do not touch it unless you know what you are doing.`

/** Creates an array of some element type determined by the given `ClassTag`
* argument. The erased type of applications of this method is `Object`.
*/
def newGenericArray[T](length: Int)(implicit tag: ClassTag[T]): Array[T] =
tag.newArray(length)

/** Convert a sequence to a Java array with element type given by `clazz`. */
def seqToArray[T](xs: Seq[T], clazz: Class[_]): Array[T] = {
val arr = java.lang.reflect.Array.newInstance(clazz, xs.length).asInstanceOf[Array[T]]
xs.copyToArray(arr)
arr
}

/** Create an array of a reference type T.
*/
def newArray[Arr](componentType: Class[_], returnType: Class[Arr], dimensions: Array[Int]): Arr =
jlr.Array.newInstance(componentType, dimensions: _*).asInstanceOf[Arr]
}
117 changes: 117 additions & 0 deletions library/src/scala/runtime/LazyVals.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package scala.runtime

/**
* Helper methods used in thread-safe lazy vals.
*/
object LazyVals {
private[this] val unsafe: sun.misc.Unsafe =
classOf[sun.misc.Unsafe].getDeclaredFields.find { field =>
field.getType == classOf[sun.misc.Unsafe] && {
field.setAccessible(true)
true
}
}
.map(_.get(null).asInstanceOf[sun.misc.Unsafe])
.getOrElse {
throw new ExceptionInInitializerError {
new IllegalStateException("Can't find instance of sun.misc.Unsafe")
}
}

private[this] val base: Int = {
val processors = java.lang.Runtime.getRuntime.availableProcessors()
8 * processors * processors
}
private[this] val monitors: Array[Object] =
Array.tabulate(base)(_ => new Object)

private def getMonitor(obj: Object, fieldId: Int = 0) = {
var id = (java.lang.System.identityHashCode(obj) + fieldId) % base

if (id < 0) id += base
monitors(id)
}

private final val LAZY_VAL_MASK = 3L
private final val debug = false

/* ------------- Start of public API ------------- */

final val BITS_PER_LAZY_VAL = 2L

def STATE(cur: Long, ord: Int): Long = {
val r = (cur >> (ord * BITS_PER_LAZY_VAL)) & LAZY_VAL_MASK
if (debug)
println(s"STATE($cur, $ord) = $r")
r
}

def CAS(t: Object, offset: Long, e: Long, v: Int, ord: Int): Boolean = {
if (debug)
println(s"CAS($t, $offset, $e, $v, $ord)")
val mask = ~(LAZY_VAL_MASK << ord * BITS_PER_LAZY_VAL)
val n = (e & mask) | (v.toLong << (ord * BITS_PER_LAZY_VAL))
unsafe.compareAndSwapLong(t, offset, e, n)
}

def setFlag(t: Object, offset: Long, v: Int, ord: Int): Unit = {
if (debug)
println(s"setFlag($t, $offset, $v, $ord)")
var retry = true
while (retry) {
val cur = get(t, offset)
if (STATE(cur, ord) == 1) retry = !CAS(t, offset, cur, v, ord)
else {
// cur == 2, somebody is waiting on monitor
if (CAS(t, offset, cur, v, ord)) {
val monitor = getMonitor(t, ord)
monitor.synchronized {
monitor.notifyAll()
}
retry = false
}
}
}
}

def wait4Notification(t: Object, offset: Long, cur: Long, ord: Int): Unit = {
if (debug)
println(s"wait4Notification($t, $offset, $cur, $ord)")
var retry = true
while (retry) {
val cur = get(t, offset)
val state = STATE(cur, ord)
if (state == 1) CAS(t, offset, cur, 2, ord)
else if (state == 2) {
val monitor = getMonitor(t, ord)
monitor.synchronized {
if (STATE(get(t, offset), ord) == 2) // make sure notification did not happen yet.
monitor.wait()
}
}
else retry = false
}
}

def get(t: Object, off: Long): Long = {
if (debug)
println(s"get($t, $off)")
unsafe.getLongVolatile(t, off)
}

def getOffset(clz: Class[_], name: String): Long = {
val r = unsafe.objectFieldOffset(clz.getDeclaredField(name))
if (debug)
println(s"getOffset($clz, $name) = $r")
r
}

object Names {
final val state = "STATE"
final val cas = "CAS"
final val setFlag = "setFlag"
final val wait4Notification = "wait4Notification"
final val get = "get"
final val getOffset = "getOffset"
}
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcB$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcB$sp extends JFunction0 {
abstract byte apply$mcB$sp();

default Object apply() { return (Byte) apply$mcB$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcC$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcC$sp extends JFunction0 {
abstract char apply$mcC$sp();

default Object apply() { return (Character) apply$mcC$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcD$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcD$sp extends JFunction0 {
abstract double apply$mcD$sp();

default Object apply() { return (Double) apply$mcD$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcF$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcF$sp extends JFunction0 {
abstract float apply$mcF$sp();

default Object apply() { return (Float) apply$mcF$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcI$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcI$sp extends JFunction0 {
abstract int apply$mcI$sp();

default Object apply() { return (Integer) apply$mcI$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcJ$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcJ$sp extends JFunction0 {
abstract long apply$mcJ$sp();

default Object apply() { return (Long) apply$mcJ$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcS$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcS$sp extends JFunction0 {
abstract short apply$mcS$sp();

default Object apply() { return (Short) apply$mcS$sp(); }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcV$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcV$sp extends JFunction0 {
abstract void apply$mcV$sp();

default Object apply() { apply$mcV$sp(); return scala.runtime.BoxedUnit.UNIT; }
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction0$mcZ$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0$mcZ$sp extends JFunction0 {
abstract boolean apply$mcZ$sp();

default Object apply() { return (Boolean) apply$mcZ$sp(); }
}
37 changes: 37 additions & 0 deletions library/src/scala/runtime/function/JFunction0.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction0<R> extends scala.Function0<R>, java.io.Serializable {
default void apply$mcV$sp() {
apply();
}
default byte apply$mcB$sp() {
return scala.runtime.BoxesRunTime.unboxToByte(apply());
}
default short apply$mcS$sp() {
return scala.runtime.BoxesRunTime.unboxToShort(apply());
}
default int apply$mcI$sp() {
return scala.runtime.BoxesRunTime.unboxToInt(apply());
}
default long apply$mcJ$sp() {
return scala.runtime.BoxesRunTime.unboxToLong(apply());
}
default char apply$mcC$sp() {
return scala.runtime.BoxesRunTime.unboxToChar(apply());
}
default float apply$mcF$sp() {
return scala.runtime.BoxesRunTime.unboxToFloat(apply());
}
default double apply$mcD$sp() {
return scala.runtime.BoxesRunTime.unboxToDouble(apply());
}
default boolean apply$mcZ$sp() {
return scala.runtime.BoxesRunTime.unboxToBoolean(apply());
}
}
13 changes: 13 additions & 0 deletions library/src/scala/runtime/function/JFunction1$mcDD$sp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.runtime.function;

@FunctionalInterface
public interface JFunction1$mcDD$sp extends JFunction1<Object, Object> {
abstract double apply$mcDD$sp(double v1);

default Object apply(Object t) { return (Double) apply$mcDD$sp(scala.runtime.BoxesRunTime.unboxToDouble(t)); }
}
Loading