Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
8e20927
Adds tests for ensuring the output from the optimizing compiler. Add …
DBooots Mar 2, 2026
844d8df
Initial implementation of Three-Address Code interim representation. …
DBooots Mar 2, 2026
cd2448f
Clean up IR building code. Ensure IRInstructions remember the source …
DBooots Mar 6, 2026
3f609ff
Align IRInstruction property names and accessibility. Add interfaces …
DBooots Mar 6, 2026
1344914
Add concept of Extended Basic Blocks to help with future analysis and…
DBooots Mar 7, 2026
28410f0
Add abstract IRVariableBase class so that IRTemp can share some commo…
DBooots Mar 7, 2026
171b2bc
Add methods to, outside of executing an opcode, invoke the same math …
DBooots Mar 7, 2026
a53f7b3
Add convenience constructor for compilation exceptions arising from I…
DBooots Mar 7, 2026
131a4a9
Add constant folding pass. This reduces any constant expressions to t…
DBooots Mar 7, 2026
cd3104f
Add constant folding for built-in scalar function calls. This require…
DBooots Mar 7, 2026
2f11d21
Add infrastructure for automatic inclusion of optimization passes usi…
DBooots Mar 7, 2026
159e068
Move the InterimCPU ownership to IROptimizer to share resources acros…
DBooots Mar 7, 2026
2a71ecc
Add suffix replacement pass. This replaces all ship: suffixes that ca…
DBooots Mar 7, 2026
eb11e29
Store variable and constant source line and column so that (theoretic…
DBooots Mar 8, 2026
25b2897
Offload optimization steps from Compiler. Add a new IRCodePart repres…
DBooots Mar 8, 2026
c4da4df
Add nonsequential label capability to BasicBlocks to account for weir…
DBooots Mar 8, 2026
8f0ee70
Implement arg tests.
DBooots Mar 8, 2026
3a5cf78
Fix stack underflow when storing parameters.
DBooots Mar 8, 2026
6651a5f
Rewrite IRCodePart and the flow from Compiler to Optimizer to enable …
DBooots Mar 8, 2026
048d5bb
Give BasicBlocks unique IDs across the entire compilation, rather tha…
DBooots Mar 8, 2026
60c4458
Add concept of dominance to basic blocks. This will be key for determ…
DBooots Mar 8, 2026
cb8e5c6
Introduce IRParameter as a special type of IRValue, for instances whe…
DBooots Mar 8, 2026
294900f
Add Dead Code Elimination pass. This pass eliminates any basic blocks…
DBooots Mar 9, 2026
f727550
Adjust file structure and namespaces to be cleaner. Adjust numbering …
DBooots Mar 9, 2026
4d59836
Outline planned sorting of optimization passes.
DBooots Mar 9, 2026
91f179a
Clean up code for DCE and downgrade it to IHolisticOptimizationPass.
DBooots Mar 9, 2026
b0b85b9
Add methods for storing variable at local, global, or ambiguous scope…
DBooots Mar 10, 2026
80e6cc3
Add X/X=1 simplification to the constant folding pass. This will say …
DBooots Mar 10, 2026
0afa7aa
Add set access to IRInstruction interfaces, as well as indexing for I…
DBooots Mar 11, 2026
cf88539
Make ConstantFolding.AttemptReduction publicly accessible to allow la…
DBooots Mar 11, 2026
a435ddb
Provide a method for overwriting the source location of an instructio…
DBooots Mar 11, 2026
336ac48
Add a static utility class to make further passes that search the ins…
DBooots Mar 11, 2026
a879ba8
Implement equality checking for IR values and instructions. Instructi…
DBooots Mar 12, 2026
c86d308
Fix binary instructions losing appropriate commutative status when th…
DBooots Mar 12, 2026
852ece8
Add an argument marker to the stack before running a unit test progra…
DBooots Mar 12, 2026
73dee51
Force clobber builtins as necessary.
DBooots Mar 12, 2026
60624a6
Add a peephole optimization pass. This pass makes various small optim…
DBooots Mar 12, 2026
b6bb80c
Add property to enumerate which blocks are dominated by a BasicBlock.
DBooots Mar 12, 2026
ef43c4c
Implement scope awareness for BasicBlocks. Class IRScope should not b…
DBooots Mar 12, 2026
9dbea38
Add pass to remove unnecessary scope pushes and pops. This also remov…
DBooots Mar 12, 2026
dfaa8ad
Implement scope awareness into IRVariableBase so that variables are c…
DBooots Mar 12, 2026
bd24143
Finish peephole optimization pass by implementing lex indexing shortc…
DBooots Mar 12, 2026
281d3ae
Clean up and restructure IRCodePart and its child classes.
DBooots Mar 12, 2026
5040e46
Fix opcodebranch where the jump is relative instead of a label. Fix e…
DBooots Mar 13, 2026
567a7ae
Fix IRCodePart behaviour for triggers. It turns out they aren't just …
DBooots Mar 13, 2026
04ca8ce
Harmonize IRVariableBase and subclass equality, hashing, and string o…
DBooots Mar 13, 2026
58f6e52
Make IRAssignment more strict by knowing the exact variable (with sco…
DBooots Mar 13, 2026
898bd3a
Improve debug string readability.
DBooots Mar 13, 2026
5b42033
Fix collection changed while enumerating exception.
DBooots Mar 13, 2026
023de41
Fix infinite loop when dumping the tree where there is a loop contained.
DBooots Mar 13, 2026
c59ce69
Fix equality not working between constants.
DBooots Mar 14, 2026
b59a7b0
Add constant propagation pass. This pass replaces local variables wit…
DBooots Mar 14, 2026
7cfbd79
Patch scopes losing ancestry when an unused scope is collapsed.
DBooots Mar 14, 2026
b094ce3
Add return type indicator to FunctionAttribute and tag all kOS functi…
DBooots Mar 17, 2026
8faf614
Fix Structure not counting as a valid return type.
DBooots Mar 17, 2026
9402dce
Add methods to check if functions are invariant and to get their retu…
DBooots Mar 17, 2026
b11f5d2
Add methods to determine the resulting type of calculator operations.
DBooots Mar 17, 2026
e14da0b
Remove null checks in favour of readonly, statically-constructed fields.
DBooots Mar 17, 2026
50b2dda
Add suffix and index type inferencing through a utility class.
DBooots Mar 19, 2026
9eeb2ca
Finish implementing type inferencing through IRInstructions and IRVal…
DBooots Mar 21, 2026
ee29200
Properly implement static single assignment. This breaks the previous…
DBooots Mar 21, 2026
3cc27a8
Documentation pass.
DBooots Apr 6, 2026
2827131
Reimplement IOperandInstructionBase and make it actually useful. Adds…
DBooots Apr 17, 2026
fa0345b
Make OpcodeStore code shared. Fix StoreLocal not creating a local var…
DBooots Apr 17, 2026
b8e350d
Implement SCCP for constant propagation, replacing the former constan…
DBooots Apr 19, 2026
98e6fd7
Fix multiple issues with how phi values and their invariance state ar…
DBooots Apr 21, 2026
fef06da
Add convenience methods to get triggers or functions by their string …
DBooots Apr 21, 2026
189b714
Add an Attribute to flag if an arithmetic method does not follow stan…
DBooots Apr 21, 2026
e90e35c
Rewrite how operands and variables are implemented so that SSA can wo…
DBooots May 7, 2026
8250589
Make the Evaluate function on a constant only available when boxed as…
DBooots May 8, 2026
eb059f8
Fix function external variables read not reflecting the full range of…
DBooots May 8, 2026
49e6e7e
Add traceability for which assignments a scope receives (or may recei…
DBooots May 9, 2026
495d963
Fix function return type and invariant status not being queried corre…
DBooots May 9, 2026
f8b5ed0
Improve test coverage for optimization passes. The new tests inspect …
DBooots May 17, 2026
c310518
Fix unresolved variable reference equality indicating equality across…
DBooots May 17, 2026
8dd74d0
Add support for OpcodeDup and OpcodeSwap, which were missing.
DBooots May 17, 2026
efc4748
Fix 'exist' not being resolved appropriately when it uses a variable …
DBooots May 17, 2026
99103d9
Implement Equals(IInterimOperand) in the IInterimOperand interface.
DBooots May 17, 2026
268d418
Refactor algebraic simplifications into the constant folding pass.
DBooots May 18, 2026
b00d197
Make it more clear what Commutative means in the sense of subtraction…
DBooots May 18, 2026
9924148
Improve reordering of operations to maximize the amount of constants …
DBooots May 18, 2026
4689cfe
Add a simplification for X/X^N.
DBooots May 18, 2026
3e866e1
Pass the context of whether builtins may be clobbered.
DBooots May 18, 2026
e18ec21
Merge branch 'KSP-KOS:develop' into compiler_optimizations
DBooots May 19, 2026
bf47ba7
Revert inadvertent inclusion of a partially-implemented property.
DBooots May 19, 2026
94e2c60
Fix unit tests failing to compile on release builds..
DBooots May 22, 2026
d168f9c
Fix error messages in unit tests.
DBooots May 22, 2026
f152984
Fix constant variable propagation. Add a feature to remove assignment…
DBooots May 22, 2026
af1a9ef
Fix phi variables not getting updated as blocks become executable.
DBooots May 28, 2026
e558ca7
Replace the DeadCodeElimination pass with a more complete Basic Block…
DBooots May 28, 2026
780b9d6
Refactor some block ordering methods.
DBooots May 28, 2026
ba70fa9
Fix phi variable equality comparisons in cases where reference equali…
DBooots May 28, 2026
766b96f
Fix some memory holds that last longer than necessary by moving to in…
DBooots May 28, 2026
0c45f1f
Fix dominator tree reconstruction keyNotFound exceptions.
DBooots May 28, 2026
86407ef
Implement the SSA conversion as an optimizer pass. This lets other pa…
DBooots May 28, 2026
4722d86
Improve the number of jump statements that can be eliminated.
DBooots May 28, 2026
a19e339
Implement cloning of IInterimOperands. The 'block' parameter is requi…
DBooots May 28, 2026
453dc11
Add new pass to restructure the conditional checks for loops. This sa…
DBooots May 28, 2026
40356d8
Implement invariance testing and resultant value tracking for user fu…
DBooots May 28, 2026
03720c4
Fix IRBinaryInstruction and IRUnaryInstruction hash codes not being e…
DBooots May 29, 2026
27fc9e8
Fix incorrect return values in IRScope.IsEncompassedBy
DBooots May 29, 2026
1404ab3
Add a pass to eliminate common subexpressions by using compiler-gener…
DBooots May 29, 2026
082215a
Add a small optimization for returning 0 in lieu of X - X.
DBooots May 29, 2026
4c02ccc
Implement some IEqualityComparer classes better.
DBooots May 30, 2026
22c5eac
Make variable assignment removal part of the Balanced optimization le…
DBooots May 30, 2026
75255a9
Prevent calls to non-invariant functions from being optimized away. T…
DBooots May 30, 2026
9c1e729
Minor refactoring. Move common expression elimination to after unnece…
DBooots May 30, 2026
6546259
Add documentation to the most recent optimization passes.
DBooots May 30, 2026
2208b41
Fix skipping passes not working. Improve unit test readability and ro…
DBooots May 30, 2026
4853178
Fix propagation of variables through branches and loops being lost an…
DBooots May 30, 2026
4030f73
Remove unnecessary pre-conversion to SSA.
DBooots May 30, 2026
e68c038
Fix opcode labelling being broken after cloning an instruction of cer…
DBooots May 30, 2026
4306ef2
Replace the iterator 'magic string' with one that can be externally r…
DBooots May 30, 2026
382bd56
Fix infinite loop in loop detection (ironic). Make helper methods pub…
DBooots May 30, 2026
3af6d22
Make helper method publicly accessible.
DBooots May 30, 2026
77cf9d6
Add boolean 'Any' and 'All' methods to IOperandInstructionBase.
DBooots May 30, 2026
defdd0a
Implement the concept of a function or instruction being 'inert' - no…
DBooots May 31, 2026
438b769
Fix block ordering for do-while structures. Improves loop and branch …
DBooots May 31, 2026
0daa176
Fix not identifying do-while type loops.
DBooots May 31, 2026
0f286f9
Add a loop-invariant code motion pass. This pass moves operations tha…
DBooots May 31, 2026
994d6e5
Fix loops being not identified or identified twice depending on when …
DBooots Jun 2, 2026
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
1 change: 1 addition & 0 deletions kerboscript_tests/integration/blank.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@lazyGlobal OFF.
7 changes: 7 additions & 0 deletions kerboscript_tests/integration/branching/for.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@lazyGlobal OFF.

print("beginning").
for item in a {
print("body").
}
print("trunk").
10 changes: 10 additions & 0 deletions kerboscript_tests/integration/branching/from.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@lazyGlobal OFF.

print("beginning").
from {local i is 0.}
until i = 10
step {set i to i + 1.}
do {
print("body").
}
print("trunk").
7 changes: 7 additions & 0 deletions kerboscript_tests/integration/branching/if.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@lazyGlobal OFF.

print("beginning").
if (a) {
print("branch").
}
print("trunk").
10 changes: 10 additions & 0 deletions kerboscript_tests/integration/branching/ifElse.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@lazyGlobal OFF.

print("beginning").
if (a) {
print("branch").
}
else {
print("else").
}
print("trunk").
10 changes: 10 additions & 0 deletions kerboscript_tests/integration/branching/ifElseIf.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@lazyGlobal OFF.

print("beginning").
if (a) {
print("branch").
}
else if (b) {
print("elseIf").
}
print("trunk").
13 changes: 13 additions & 0 deletions kerboscript_tests/integration/branching/ifElseIfElse.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@lazyGlobal OFF.

print("beginning").
if (a) {
print("branch").
}
else if (b) {
print("elseIf").
}
else{
print("else").
}
print("trunk").
7 changes: 7 additions & 0 deletions kerboscript_tests/integration/branching/until.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@lazyGlobal OFF.

print("beginning").
until (a) {
print("body").
}
print("trunk").
19 changes: 19 additions & 0 deletions kerboscript_tests/integration/codeMotion.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@lazyGlobal OFF.

randomseed("random", 0).
local a is random("random").
local b is a + 1.
print(b).
from {local i is 0.}
until i = 4
step {set i to i + 1.}
do {
set b to a + 2.
print(i).
}
print(b).
for j in range(0, 4) {
set b to a + 1.
print (j).
}
print(b).
22 changes: 22 additions & 0 deletions kerboscript_tests/integration/commonExpressionElimination.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@lazyGlobal OFF.

randomseed("random", 0).
local a is 1.
local b is random("random").
local d is random("random").

global c is a + b.

print c.
print a + b.
print a + b.
{
print a + b.
global e is b + d.
print b + d.
print b + d.
local f is e.
print f.
}
print "Outside".
print b + d.
64 changes: 64 additions & 0 deletions kerboscript_tests/integration/constantPropagation.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
@lazyGlobal OFF.

global a is 5.
global _false is false.

local b is 1.
set b to 2.
local c is 4.
local d is 7.
local h is false.

print("test"). // The assignments should happen after this line.

print(b+c). // 6
print(h). // False

local function func {
print(b+c). // 6
print(b+d). // 9
print(b+a). // 7
set h to true.
}

func().

{
local i is 10.
print(i). // 10
print(i+c). // 14
}

print(h). // True

set h to false.

print(h). // False

lock e to a + b.

print(e). // 7
set b to 1.
print(e). // 6

lock g to c + d.
print(g). // 11
set d to 5.
print(g). // 9

when _false then {
set c to 10.
}

print(b+c). // 5

set b to 1.
set b to b + d.

print(b). // 6

until b = 1 {
set b to b - 1.
}

print(b). // 1
5 changes: 5 additions & 0 deletions kerboscript_tests/integration/operators.ks
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ print(1 = 1).
print(1 <> 2).
print(true and true).
print(true or false).
print("A" + "b").
print(a * 0).
print(a ^ 0).
print(arcTan2(0,1)).
print(abs(-1)).
21 changes: 21 additions & 0 deletions kerboscript_tests/integration/operators_invalid.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

set a to 0.

print(+ 1).
print(- (-1)).
print(not false).
print(defined a).

print(1 + 2).
print(2 * 3).
print(4 / 2).
print(3 ^ 2).
print(2 > 1).
print(2 >= 1).
print(1 < 2).
print(1 <= 2).
print(1 = 1).
print(1 <> 2).
print(true and true).
print(true or false).
print(1 + false).
51 changes: 51 additions & 0 deletions kerboscript_tests/integration/peepholeOptimizations.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
local a is TRUE.
local c is 3.
local x is 2.
local z is 5.

print("test":typename()).
// v() is unavailable in unit testing.
//local v1 is v(1,2,3).
//local v2 is v(3,4,5).
// Multiplying scalars will work equally well.
local v1 is 2.
local v2 is 3.

print(vectorDotProduct(v1,v2)). // =6
print(vdot(v1,v2)). // =6

if not a {
print("Don't print this.").
}

if not (not a) {
print("Print this.").
}

print(c*x+c*z). // = 21
print(c*x+z*c). // = 21
print(x*c+c*z). // = 21
print(x*c+z*c). // = 21

print(-x+z). // = 3
print(x+-z). // = -3

print(x--z). // = 7

print(x^z/x). // = 16
print(z^4/z). // = 125

print(x^z*x). // = 64
print(z^4*z). // = 3125

print(x^z*x^c). // = 256

print(x^z/x^c). // = 4

print(z^3/z). // = 25

print(z*z*z). // = 125

print(z^3/z*z). // = 125

print(x^2*x^z/x*x^3). // = 512
5 changes: 5 additions & 0 deletions kerboscript_tests/integration/suffixReplacement.ks
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@CLOBBERBUILTINS OFF.
set airspeed to 100. // Clobber the built-in so that this value is pulled after replacement.
print(CONSTANT:g0).
print(CONSTANT():pi).
print(SHIP:airspeed).
92 changes: 92 additions & 0 deletions src/kOS.Safe.Test/Compilation/TypeInferencing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using NUnit.Framework;
using kOS.Safe.Compilation;
using kOS.Safe.Compilation.IR;
using kOS.Safe.Encapsulation;
using kOS.Safe.Test.Execution;
using kOS.Safe.Utilities;

namespace kOS.Safe.Test.Compilation
{

[SetUpFixture]
public class StaticSetup
{
[SetUp]
public void Setup()
{
SafeHouse.Init(new Config(), new VersionInfo(0, 0, 0, 0), "", false, "./");
SafeHouse.Logger = new NoopLogger();

try
{
AssemblyWalkAttribute.Walk();
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine(e.StackTrace);
throw;
}
}
}

[TestFixture]
public class TypeInferencing
{
[Test]
public void StructureSuffixTest()
{
// Tests that the bare minimum works
Type structureType = typeof(Encapsulation.Structure);
Type result = TypeInferencer.GetTypeForSuffix(structureType, "TOSTRING");
Assert.AreEqual(typeof(StringValue), result);

Type integerType = typeof(ScalarIntValue);
result = TypeInferencer.GetTypeForSuffix(integerType, "istype");
Assert.AreEqual(typeof(BooleanValue), result);
}

[Test]
public void ListSuffixTest()
{
// Test that lists and enumerables work
Type listType = typeof(ListValue);
Type result = TypeInferencer.GetTypeForSuffix(listType, "ITERATOR");
Assert.AreEqual(typeof(Enumerator), result);

result = TypeInferencer.GetTypeForSuffix(listType, "COPY");
Assert.AreEqual(typeof(ListValue), result);

result = TypeInferencer.GetTypeForIndex(listType);
Assert.AreEqual(typeof(Encapsulation.Structure), result);

result = TypeInferencer.GetTypeForSuffix(listType, "CLEAR");
Assert.AreEqual(null, result);

// Test an abstract generic type
listType = typeof(EnumerableValue<Encapsulation.Structure, System.Collections.Generic.IEnumerable<Encapsulation.Structure>>);
result = TypeInferencer.GetTypeForSuffix(listType, "ITERATOR");
Assert.AreEqual(typeof(Enumerator), result);
}

[Test]
public void TestInstructionInferencing()
{
InterimConstantValue a = new InterimConstantValue(ScalarIntValue.One, 0, 0);
InterimConstantValue b = new InterimConstantValue(ScalarIntValue.Two, 0, 0);
IRBinaryOp add = new IRBinaryOp(null, new OpcodeMathAdd(), a, b);

Assert.AreEqual(typeof(ScalarValue), add.Type);

IRCall call = new IRCall(null, new OpcodeCall("sin"), true, b);
Assert.IsTrue(typeof(ScalarValue).IsAssignableFrom(call.Type));

IRCall print = new IRCall(null, new OpcodeCall("print"), true, a);
Assert.AreEqual(null, print.Type);

IRCall userCall = new IRCall(null, new OpcodeCall("$test*"), true, a, b);
Assert.AreEqual(typeof(Encapsulation.Structure), userCall.Type);
}
}
}
Loading
Loading