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
2 changes: 1 addition & 1 deletion skiplang/compiler/Skargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "skc"
version = "0.4.2"
version = "0.4.3"
# Default: all test files compiled into one binary, @exptest functions
# auto-discovered. To use subprocess mode (compile each test individually),
# either set SKC_SUBPROCESS=1 or uncomment the line below.
Expand Down
46 changes: 44 additions & 2 deletions skiplang/compiler/src/peephole.sk
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ mutable class .Peephole{
| v @ ArrayAlloc _ -> peepholeArrayAlloc(v, this)
| v @ ArrayNew _ -> peepholeArrayNew(v, this)
| v @ ArraySize _ -> peepholeArraySize(v, this)
| v @ ArrayUnsafeGet _ -> peepholeArrayUnsafeGet(v, this)
| v @ With _ -> peepholeWith(v, this)
| v @ ZeroExtend _ -> peepholeZeroExtend(v, this)

Expand All @@ -102,7 +103,6 @@ mutable class .Peephole{
| Store _
| Suspend _
| ArrayClone _
| ArrayUnsafeGet _
| ArrayUnsafeSet _
| Yield _ ->
x
Expand Down Expand Up @@ -1818,6 +1818,35 @@ private fun peepholeArraySize(peep: ArraySize, s: mutable Peephole): Instr {
}
}

private fun peepholeArrayUnsafeGet(
peep: ArrayUnsafeGet,
s: mutable Peephole,
): Instr {
s.getInstr(peep.index).constantInteger() match {
| Some(i) ->
s.getInstr(peep.obj) match {
| ConstantArray{typ, values} ->
if (i >= 0) {
sc = ClassInputs.exampleSClass(typ, s.env, s.classReader);
valsPerSlot = sc.getArraySlotInfo(peep.pos).types.size();
piece = if (peep.tupleIndex < 0) 0 else peep.tupleIndex;
slot = i * valsPerSlot + piece;
// Only fold when provably in bounds (unsafe get is UB otherwise, so
// out-of-bounds must keep the original instruction).
if (piece < valsPerSlot && slot < values.size()) {
s.getConstant(values[slot] with {id => s.iid()})
} else {
peep
}
} else {
peep
}
| _ -> peep
}
| _ -> peep
}
}

private fun peepholeFloatBits(peep: FloatBits, s: mutable Peephole): Instr {
s.getInstr(peep.value) match {
| ConstantFloat{value} -> s.constantInt(value.toBits())
Expand Down Expand Up @@ -1849,7 +1878,20 @@ private fun peepholeStringConcat(
args.size() match {
| 0 -> s.constantString("")
| 1 -> s.getInstr(args[0])
| _ -> peep
| _ ->
// Fold a concat of constant strings (e.g. a folded Float.toString
// concatenated with a literal). Collect the parts and bail out as soon
// as one argument is not a constant string.
parts = mutable Vector<UTF8String>[];
for (a in args) {
s.getInstr(a) match {
| ConstantString{value} -> parts.push(value)
| _ -> return peep
}
};
s.constantUTF8String(
parts.reduce((acc, p) -> acc + p, UTF8String::make("")),
)
}
}

Expand Down
7 changes: 7 additions & 0 deletions skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
10
20
30
20
b
20
30
29 changes: 29 additions & 0 deletions skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Runtime_ArrayUnsafeGetConst;
// ArrayUnsafeGet on a constant array with a constant in-bounds index folds
// at compile time. Non-constant arrays or indices are not folded but must
// still compute the right value at runtime.

@exptest
fun main(): void {
print_raw(
// Scalar array: first, middle, and last in-bounds indices.
Unsafe.array_get(Array[10, 20, 30], 0).toString() +
"\n" +
Unsafe.array_get(Array[10, 20, 30], 1).toString() +
"\n" +
Unsafe.array_get(Array[10, 20, 30], 2).toString() +
"\n" +
// Tuple elements exercise tupleIndex 0 and 1.
Unsafe.array_get(Array[(10, "a"), (20, "b")], 1).i0.toString() +
"\n" +
Unsafe.array_get(Array[(10, "a"), (20, "b")], 1).i1 +
"\n" +
// Non-constant index: not folded, computed at runtime.
Unsafe.array_get(Array[10, 20, 30], "ab".length() - 1).toString() +
"\n" +
// Non-constant array (clone is not folded): not folded, runtime value.
Unsafe.array_get(Array[10, 20, 30].clone(), 2).toString() +
"\n",
)
}
module end;
4 changes: 4 additions & 0 deletions skiplang/compiler/tests/Runtime/StringConcatConst.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
foobarbaz
0.1!
x
a123b
20 changes: 20 additions & 0 deletions skiplang/compiler/tests/Runtime/StringConcatConst.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Runtime_StringConcatConst;
// StringConcat folds when all arguments are constant strings (including
// constants produced by other folds, e.g. Float.toString). A concat with a
// non-constant argument is not folded but must still compute the right
// string at runtime.

@exptest
fun main(): void {
runtimeStr = 123.toString();
print_raw(
"foo" +
"bar" +
"baz" +
"\n" +
(0.1.toString() + "!\n") +
("" + "" + "x\n") +
("a" + runtimeStr + "b\n"),
)
}
module end;