Skip to content
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
12 changes: 6 additions & 6 deletions effekt/shared/src/main/scala/effekt/core/vm/Builtin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,31 +248,31 @@ lazy val chars: Builtins = Map(
)

lazy val arrays: Builtins = Map(
builtin("array::allocate(Int)") {
builtin("array::unsafeAllocate(Int)") {
case As.Int(x) :: Nil => Value.Array(scala.Array.ofDim(x.toInt))
},
builtin("array::size[T](Array[T])") {
case As.Array(arr) :: Nil => Value.Int(arr.length.toLong)
},
builtin("array::unsafeGet[T](Array[T], Int)") {
builtin("array::get[T](Array[T], Int)") {
case As.Array(arr) :: As.Int(index) :: Nil => arr(index.toInt)
},
builtin("array::unsafeSet[T](Array[T], Int, T)") {
builtin("array::set[T](Array[T], Int, T)") {
case As.Array(arr) :: As.Int(index) :: value :: Nil => arr.update(index.toInt, value); Value.Unit()
},
)

lazy val bytearrays: Builtins = Map(
builtin("bytearray::allocate(Int)") {
builtin("bytearray::unsafeAllocate(Int)") {
case As.Int(x) :: Nil => Value.ByteArray(scala.Array.ofDim(x.toInt))
},
builtin("bytearray::size(ByteArray)") {
case As.ByteArray(arr) :: Nil => Value.Int(arr.length.toLong)
},
builtin("bytearray::unsafeGet(ByteArray, Int)") {
builtin("bytearray::get(ByteArray, Int)") {
case As.ByteArray(arr) :: As.Int(index) :: Nil => Value.Byte(arr(index.toInt))
},
builtin("bytearray::unsafeSet(ByteArray, Int, Byte)") {
builtin("bytearray::set(ByteArray, Int, Byte)") {
case As.ByteArray(arr) :: As.Int(index) :: As.Byte(value) :: Nil => arr.update(index.toInt, value); Value.Unit()
},
builtin("bytearray::compare(ByteArray, ByteArray)") {
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/are_we_fast_yet/bounce.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def run(n: Int) = {

each(0, n) { i =>
each(0, ballCount) { i =>
val ball = balls.unsafeGet(i);
val ball = balls.get(i);
val didBounce = ball.bounce();
if (didBounce)
bounces = bounces + 1;
Expand Down
30 changes: 15 additions & 15 deletions examples/benchmarks/are_we_fast_yet/nbody.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ def run(n: Int) = {
val sun = makeBody(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0)

def createBodies(): Array[Body] = {
val bodies = allocate[Body](5)
val bodies = unsafeAllocate[Body](5)

bodies.unsafeSet(0, sun);
bodies.unsafeSet(1, jupiter);
bodies.unsafeSet(2, saturn);
bodies.unsafeSet(3, uranus);
bodies.unsafeSet(4, neptune);
bodies.set(0, sun);
bodies.set(1, jupiter);
bodies.set(2, saturn);
bodies.set(3, uranus);
bodies.set(4, neptune);

var px = 0.0;
var py = 0.0;
Expand All @@ -97,16 +97,16 @@ def run(n: Int) = {
0.0 - (pz / SOLAR_MASS),
body.mass)

bodies.unsafeSet(0, sun.offsetMomentum(px, py, pz))
bodies.set(0, sun.offsetMomentum(px, py, pz))

bodies
}

def advance(bodies: Array[Body], dt: Double) = {
each(0, bodies.size) { i =>
each(i + 1, bodies.size) { j =>
val iBody = bodies.unsafeGet(i);
val jBody = bodies.unsafeGet(j);
val iBody = bodies.get(i);
val jBody = bodies.get(j);

val dx = iBody.x - jBody.x;
val dy = iBody.y - jBody.y;
Expand All @@ -116,15 +116,15 @@ def run(n: Int) = {
val distance = sqrt(dSquared);
val mag = dt / (dSquared * distance);

bodies.unsafeSet(i, Body(
bodies.set(i, Body(
iBody.x, iBody.y, iBody.z,
iBody.vx - dx * jBody.mass * mag,
iBody.vy - dy * jBody.mass * mag,
iBody.vz - dz * jBody.mass * mag,
iBody.mass
));

bodies.unsafeSet(j, Body(
bodies.set(j, Body(
jBody.x, jBody.y, jBody.z,
jBody.vx + dx * iBody.mass * mag,
jBody.vy + dy * iBody.mass * mag,
Expand All @@ -135,8 +135,8 @@ def run(n: Int) = {
}

each(0, bodies.size) { i =>
val body = bodies.unsafeGet(i);
bodies.unsafeSet(i, Body(
val body = bodies.get(i);
bodies.set(i, Body(
body.x + dt * body.vx,
body.y + dt * body.vy,
body.z + dt * body.vz,
Expand All @@ -151,11 +151,11 @@ def run(n: Int) = {
def energy(bodies: Array[Body]): Double = {
var e = 0.0;
each(0, bodies.size()){ i =>
val iBody = bodies.unsafeGet(i);
val iBody = bodies.get(i);
e = e + 0.5 * iBody.mass * (iBody.vx * iBody.vx + iBody.vy * iBody.vy + iBody.vz * iBody.vz);

each(i + 1, bodies.size){ j =>
val jBody = bodies.unsafeGet(j);
val jBody = bodies.get(j);
val dx = iBody.x - jBody.x;
val dy = iBody.y - jBody.y;
val dz = iBody.z - jBody.z;
Expand Down
6 changes: 3 additions & 3 deletions examples/benchmarks/are_we_fast_yet/permute.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import ref
import array

def swap(arr: Array[Int], i: Int, j: Int) = {
val tmp = arr.unsafeGet(i);
arr.unsafeSet(i, arr.unsafeGet(j));
arr.unsafeSet(j, tmp)
val tmp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, tmp)
}

def run(n: Int) = {
Expand Down
10 changes: 5 additions & 5 deletions examples/benchmarks/are_we_fast_yet/queens.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ def run(n: Int) = {
val queenRows: Array[Int] = array(n, -1);

def getRowColumn(r: Int, c: Int): Bool =
freeRows.unsafeGet(r) && freeMaxs.unsafeGet(c + r) && freeMins.unsafeGet(c - r + (n - 1))
freeRows.get(r) && freeMaxs.get(c + r) && freeMins.get(c - r + (n - 1))

def setRowColumn(r: Int, c: Int, v: Bool) = {
freeRows.unsafeSet(r, v);
freeMaxs.unsafeSet(c + r, v);
freeMins.unsafeSet(c - r + (n - 1), v)
freeRows.set(r, v);
freeMaxs.set(c + r, v);
freeMins.set(c - r + (n - 1), v)
}

def placeQueen(c: Int): Bool =
try {
each(0, n) { r =>
if (getRowColumn(r, c)) {
queenRows.unsafeSet(r, c);
queenRows.set(r, c);
setRowColumn(r, c, false);

if (c == (n - 1)) {
Expand Down
4 changes: 2 additions & 2 deletions examples/benchmarks/are_we_fast_yet/sieve.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def run(size: Int) = {
var primeCount: Int = 0;
val flags: Array[Bool] = array(size, true);
each(2, size) { i =>
if (flags.unsafeGet(i - 1)) {
if (flags.get(i - 1)) {
primeCount = primeCount + 1;
var k = i + i;
while (k <= size) {
flags.unsafeSet(k - 1, false);
flags.set(k - 1, false);
k = k + i;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/are_we_fast_yet/storage.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(n: Int) = {
def buildTreeDepth(depth: Int) { rand: Random }: Tree = {
count.set(count.get + 1);
if (depth == 1) {
Leaf(allocate(mod(rand.next, 10) + 1))
Leaf(array::allocateZeros(mod(rand.next, 10) + 1))
} else {
Node(
buildTreeDepth(depth - 1){ rand },
Expand Down
8 changes: 4 additions & 4 deletions examples/benchmarks/are_we_fast_yet/towers.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ def run(n: Int) = {
var towers: Towers = array(3, Nil());

def pushDisk(newTopDiskOnPile: Disk, pileIdx: Int): Unit = {
val pile: Pile = towers.unsafeGet(pileIdx);
val pile: Pile = towers.get(pileIdx);
pile match {
case Cons(topDiskOnStack, _) =>
if (newTopDiskOnPile >= topDiskOnStack) {
panic("Cannot put a big disk onto a smaller one");
}
case Nil() => ()
}
towers.unsafeSet(pileIdx, Cons(newTopDiskOnPile, pile));
towers.set(pileIdx, Cons(newTopDiskOnPile, pile));
}

def popDisk(pileIdx: Int): Disk = {
val pile = towers.unsafeGet(pileIdx);
val pile = towers.get(pileIdx);

pile match {
case Nil() =>
panic("Attempting to remove a disk from an empty pile");
case Cons(disk, next) =>
towers.unsafeSet(pileIdx, next)
towers.set(pileIdx, next)
disk
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarks/input_output/large_file.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def run(n: Int) = {
panic("failed open for writing")
}
val outputBuffer = bytearray(size, 33.toByte)
outputBuffer.unsafeSet(999, 10.toByte)
outputBuffer.set(999, 10.toByte)
repeat(n) {
val m = internal::write(file, outputBuffer, 0, size, -1)
if (m < outputBuffer.size) {
Expand Down
2 changes: 1 addition & 1 deletion examples/pos/array/list_conversion.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def main() = {
listToArrayAndBack(list)

println("empty array")
val emptyArray = array::allocate[Int](0)
val emptyArray = array[Int](0, 0)
arrayToListAndBack(emptyArray)

println("nonempty array")
Expand Down
2 changes: 1 addition & 1 deletion examples/pos/array/sum.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module examples/pos/array/sum
import array

def main() = {
val emptyArray: Array[Int] = array::allocate(0)
val emptyArray: Array[Int] = array(0, 0)
println(emptyArray.sum())

val array: Array[Int] = build(3) { i => i + 1 } // Array(1, 2, 3)
Expand Down
10 changes: 5 additions & 5 deletions examples/pos/arrays.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module examples/pos/arrays
import array

def main() = {
val arr = array::allocate[String](5);
val arr = array[String](5, "");

arr.unsafeSet(0, "hello");
arr.unsafeSet(2, "world");
println(arr.unsafeGet(0));
println(arr.unsafeGet(2));
arr.set(0, "hello");
arr.set(2, "world");
println(arr.get(0));
println(arr.get(2));

val arrWithVals: Array[String] = array(4, "Hello World");
println(arrWithVals)
Expand Down
14 changes: 7 additions & 7 deletions examples/pos/raytracer.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def render(scene: Scene, image: Buffer) = {
def storeAt(color: Color, x: Int, y: Int): Unit = {
val opaque = 255;
val index = x * 4 + y * width * 4;
unsafeSet(image, index + 0, color.red);
unsafeSet(image, index + 1, color.green);
unsafeSet(image, index + 2, color.blue);
unsafeSet(image, index + 3, opaque)
set(image, index + 0, color.red);
set(image, index + 1, color.green);
set(image, index + 2, color.blue);
set(image, index + 3, opaque)
}

// now this is a real stress-test of backtrackable state
Expand Down Expand Up @@ -255,19 +255,19 @@ def bench() = {
val scene = exampleScene();

measure(5, 10) {
val buffer: Buffer = array::allocate(scene.width * scene.height * 4);
val buffer: Buffer = array::unsafeAllocate(scene.width * scene.height * 4);
render(scene, buffer)
}
}

def main() = {
val scene = exampleScene();
val buffer: Buffer = array::allocate(scene.width * scene.height * 4);
val buffer: Buffer = array::unsafeAllocate(scene.width * scene.height * 4);
render(scene, buffer);

var i = 0;
while (i < buffer.size) {
println(buffer.unsafeGet(i).show ++ ", " ++ buffer.unsafeGet(i + 1).show ++ ", " ++ buffer.unsafeGet(i + 2).show);
println(buffer.get(i).show ++ ", " ++ buffer.get(i + 1).show ++ ", " ++ buffer.get(i + 2).show);
i = i + 4
}
}
2 changes: 1 addition & 1 deletion examples/stdlib/array/foldLeft.effekt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def main() = {
println(array::build(10) { x => x }.foldLeft(0) { (acc, x) => acc + x })
println(array::build(10000) { x => x }.foldLeft(0) { (acc, x) => acc + x })
println(array::allocate[Int](0).foldLeft(0) { (acc, x) => acc + x })
println(array[Int](0, 0).foldLeft(0) { (acc, x) => acc + x })
println(["hello", "world", ",", "effekt!"].fromList.foldLeft("") { (acc, x) => acc ++ x })
}
2 changes: 1 addition & 1 deletion examples/stdlib/array/foldRight.effekt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
def main() = {
println(array::build(10) { x => x }.foldRight(0) { (acc, x) => acc + x })
println(array::build(10000) { x => x }.foldRight(0) { (acc, x) => acc + x })
println(array::allocate[Int](0).foldRight(0) { (acc, x) => acc + x })
println(array[Int](0, 0).foldRight(0) { (acc, x) => acc + x })
println(["hello", "world", ",", "effekt!"].fromList.foldRight("") { (acc, x) => acc ++ x })
}
2 changes: 1 addition & 1 deletion examples/stdlib/array/map.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def main() = {
arr.map { x => x * 2 }
println(arr)

val arr1 = array::allocate(0)
val arr1 = array(0, 0)
arr1.map { x => x * 2 }
println(arr1)
}
4 changes: 2 additions & 2 deletions examples/stdlib/bytearray/bytearray_compare.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ def main() = {
val empty: ByteArray = "".fromString
val zeros: ByteArray = bytearray::allocate(8)
each(0, zeros.size) { i =>
zeros.unsafeSet(i, 0.toByte)
zeros.set(i, 0.toByte)
}
val abcs: ByteArray = bytearray::allocate(8)
each(0, abcs.size) { i =>
abcs.unsafeSet(i, (97 + i).toByte)
abcs.set(i, (97 + i).toByte)
}

println(empty.compareByteArray(empty))
Expand Down
18 changes: 9 additions & 9 deletions examples/stdlib/bytearray/bytearray_hello.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import bytearray
def main() = {

val b = bytearray::allocate(8);
b.unsafeSet(0, 104.toByte)
b.unsafeSet(1, 101.toByte)
b.unsafeSet(2, 108.toByte)
b.unsafeSet(3, 108.toByte)
b.unsafeSet(4, 111.toByte)
b.unsafeSet(5, 33.toByte)
b.unsafeSet(6, 33.toByte)
b.unsafeSet(7, 33.toByte)
b.set(0, 104.toByte)
b.set(1, 101.toByte)
b.set(2, 108.toByte)
b.set(3, 108.toByte)
b.set(4, 111.toByte)
b.set(5, 33.toByte)
b.set(6, 33.toByte)
b.set(7, 33.toByte)

b.unsafeSet(1, 102.toByte)
b.set(1, 102.toByte)

println(b.toString) // hfllo!!!
}
Loading
Loading