From 7208ab1f9acaee3bfc800610cd4348b263e9aeb0 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Tue, 17 Nov 2026 11:51:02 +0000 Subject: [PATCH 1/3] skc: fold StringConcat of constant strings A StringConcat whose arguments are all constant strings now folds to a single constant string; previously only 0/1-argument concats folded. This also catches constants produced by other folds, e.g. a folded Float.toString concatenated with a literal. The parts are collected into a vector and the fold bails out as soon as one argument is not a constant string, so non-constant concats are left alone. Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) --- skiplang/compiler/src/peephole.sk | 15 +++++++++++++- .../tests/Runtime/StringConcatConst.exp | 4 ++++ .../tests/Runtime/StringConcatConst.sk | 20 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 skiplang/compiler/tests/Runtime/StringConcatConst.exp create mode 100644 skiplang/compiler/tests/Runtime/StringConcatConst.sk diff --git a/skiplang/compiler/src/peephole.sk b/skiplang/compiler/src/peephole.sk index 23b629db1..d6ad12bbd 100644 --- a/skiplang/compiler/src/peephole.sk +++ b/skiplang/compiler/src/peephole.sk @@ -1849,7 +1849,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[]; + 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("")), + ) } } diff --git a/skiplang/compiler/tests/Runtime/StringConcatConst.exp b/skiplang/compiler/tests/Runtime/StringConcatConst.exp new file mode 100644 index 000000000..85b0bc0ed --- /dev/null +++ b/skiplang/compiler/tests/Runtime/StringConcatConst.exp @@ -0,0 +1,4 @@ +foobarbaz +0.1! +x +a123b diff --git a/skiplang/compiler/tests/Runtime/StringConcatConst.sk b/skiplang/compiler/tests/Runtime/StringConcatConst.sk new file mode 100644 index 000000000..8de5153d2 --- /dev/null +++ b/skiplang/compiler/tests/Runtime/StringConcatConst.sk @@ -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; From 2599ba6a26b33068b9257d77e01f4f237f5b9ffd Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Tue, 17 Nov 2026 12:02:39 +0000 Subject: [PATCH 2/3] skc: fold ArrayUnsafeGet on constant arrays ArrayUnsafeGet on a ConstantArray with a constant index now folds to the element constant, so literal arrays with constant indices (e.g. Array[10, 20, 30] read at 0, or tuple elements via tupleIndex) resolve at compile time. The fold only fires when provably in bounds (index >= 0, tupleIndex within the slot, computed slot < values.size()); out-of-bounds or non-constant arrays/indices keep the original instruction (unsafe get is UB out of bounds, so those must not fold). Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) --- skiplang/compiler/src/peephole.sk | 31 ++++++++++++++++++- .../tests/Runtime/ArrayUnsafeGetConst.exp | 7 +++++ .../tests/Runtime/ArrayUnsafeGetConst.sk | 29 +++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.exp create mode 100644 skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.sk diff --git a/skiplang/compiler/src/peephole.sk b/skiplang/compiler/src/peephole.sk index d6ad12bbd..ad2fab418 100644 --- a/skiplang/compiler/src/peephole.sk +++ b/skiplang/compiler/src/peephole.sk @@ -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) @@ -102,7 +103,6 @@ mutable class .Peephole{ | Store _ | Suspend _ | ArrayClone _ - | ArrayUnsafeGet _ | ArrayUnsafeSet _ | Yield _ -> x @@ -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()) diff --git a/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.exp b/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.exp new file mode 100644 index 000000000..8b06b9279 --- /dev/null +++ b/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.exp @@ -0,0 +1,7 @@ +10 +20 +30 +20 +b +20 +30 diff --git a/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.sk b/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.sk new file mode 100644 index 000000000..250869e48 --- /dev/null +++ b/skiplang/compiler/tests/Runtime/ArrayUnsafeGetConst.sk @@ -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; From b21f58f42cb93449a8615d053807b1fb139d73b5 Mon Sep 17 00:00:00 2001 From: Mehdi Bouaziz Date: Tue, 17 Nov 2026 12:02:40 +0000 Subject: [PATCH 3/3] skc: bump to 0.4.3 Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) --- skiplang/compiler/Skargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skiplang/compiler/Skargo.toml b/skiplang/compiler/Skargo.toml index c0b6bd193..de507b70a 100644 --- a/skiplang/compiler/Skargo.toml +++ b/skiplang/compiler/Skargo.toml @@ -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.