Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 30b564a153 | |||
|
415e69628d
|
|||
| 1755fc3504 | |||
|
378c7e18cd
|
|||
|
9de59ee3b1
|
|||
|
20f7cb9a4b
|
|||
|
0be2e644e4
|
|||
|
3fb04aef3b
|
|||
| 1230f83951 | |||
|
d3974ad590
|
|||
|
098d689750
|
|||
|
3edf0324c7
|
|||
|
92f0d22805
|
|||
|
811f4f4959
|
|||
| c041518c9b | |||
|
2b26d0d278
|
|||
|
236b50c813
|
|||
|
342b1ab107
|
|||
| 0732f68bcf | |||
|
0ac010ef8f
|
|||
|
c2208fbb15
|
|||
| 295f062797 | |||
|
9c260ef2d5
|
|||
|
0fde11a2bf
|
|||
|
b21d6cc73e
|
|||
| f19801d4e6 |
40
Changelog.md
40
Changelog.md
@@ -1,5 +1,45 @@
|
||||
# Changelog
|
||||
|
||||
[0.3.1]
|
||||
|
||||
- Fixed possible `KeyNotFoundException` in C# code due to invalid
|
||||
dictionary access when an IC housing has an error
|
||||
|
||||
[0.3.0]
|
||||
|
||||
- Implemented a multi-pass optimizer
|
||||
- This should significantly reduce line count in the final output
|
||||
- Fixed source map to line up with newly optimized code
|
||||
|
||||
[0.2.4]
|
||||
|
||||
- Groundwork laid to collect and track source maps
|
||||
- IC Housing will now display the `Slang` source error line (if available)
|
||||
instead of the `IC10` source error line
|
||||
|
||||
[0.2.3]
|
||||
|
||||
- Fixed stack underflow with function invocations
|
||||
- They are still "heavy", but they should work as expected now
|
||||
- Fixed issue where syscall functions were not allowed as infix operators
|
||||
|
||||
[0.2.2]
|
||||
|
||||
- Fixed some formatting issues when converting Markdown to Text Mesh Pro for
|
||||
Stationpedia
|
||||
- Added support for ternary expressions
|
||||
- `let i = someValue ? 4 : 5;`
|
||||
- `i = someValue ? 4 : 5;`
|
||||
- This greedily evaluates both sides, so side effects like calling functions
|
||||
is not recommended i.e.
|
||||
- `i = someValue : doSomething() : doSomethingElse();`
|
||||
- Both sides will be evaluated before calling the `select` instruction
|
||||
|
||||
[0.2.1]
|
||||
|
||||
- Added support for `loadSlot` and `setSlot`
|
||||
- Fixed bug where syscalls like `max(1, 2)` were not allowed in assignment expressions
|
||||
|
||||
[0.2.0]
|
||||
|
||||
- Completely re-wrote the tokenizer to use `logos`
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<ModMetadata xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<Name>Slang</Name>
|
||||
<Author>JoeDiertay</Author>
|
||||
<Version>0.2.0</Version>
|
||||
<Version>0.3.1</Version>
|
||||
<Description>
|
||||
[h1]Slang: High-Level Programming for Stationeers[/h1]
|
||||
|
||||
|
||||
@@ -113,6 +113,34 @@ public static unsafe class SlangExtensions
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
public static unsafe List<SourceMapEntry> ToList(this Vec_FfiSourceMapEntry_t vec)
|
||||
{
|
||||
var toReturn = new List<SourceMapEntry>((int)vec.len);
|
||||
|
||||
var currentPtr = vec.ptr;
|
||||
|
||||
for (int i = 0; i < (int)vec.len; i++)
|
||||
{
|
||||
var item = currentPtr[i];
|
||||
|
||||
toReturn.Add(
|
||||
new SourceMapEntry
|
||||
{
|
||||
Ic10Line = item.line_number,
|
||||
SlangSource = new Range
|
||||
{
|
||||
EndCol = item.span.end_col,
|
||||
EndLine = item.span.end_line,
|
||||
StartCol = item.span.start_col,
|
||||
StartLine = item.span.start_line,
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
|
||||
private static uint GetColorForKind(uint kind)
|
||||
{
|
||||
switch (kind)
|
||||
|
||||
@@ -71,18 +71,6 @@ public unsafe struct Vec_uint8_t {
|
||||
public UIntPtr cap;
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
/// <summary>
|
||||
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
||||
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
||||
/// This should result in the ability to compile many times without triggering frame drops
|
||||
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
||||
/// </summary>
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
Vec_uint8_t compile_from_string (
|
||||
slice_ref_uint16_t input);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 16)]
|
||||
public unsafe struct FfiRange_t {
|
||||
public UInt32 start_col;
|
||||
@@ -94,6 +82,44 @@ public unsafe struct FfiRange_t {
|
||||
public UInt32 end_line;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 20)]
|
||||
public unsafe struct FfiSourceMapEntry_t {
|
||||
public UInt32 line_number;
|
||||
|
||||
public FfiRange_t span;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential, Size = 24)]
|
||||
public unsafe struct Vec_FfiSourceMapEntry_t {
|
||||
public FfiSourceMapEntry_t * ptr;
|
||||
|
||||
public UIntPtr len;
|
||||
|
||||
public UIntPtr cap;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 48)]
|
||||
public unsafe struct FfiCompilationResult_t {
|
||||
public Vec_uint8_t output_code;
|
||||
|
||||
public Vec_FfiSourceMapEntry_t source_map;
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
/// <summary>
|
||||
/// C# handles strings as UTF16. We do NOT want to allocate that memory in C# because
|
||||
/// we want to avoid GC. So we pass it to Rust to handle all the memory allocations.
|
||||
/// This should result in the ability to compile many times without triggering frame drops
|
||||
/// from the GC from a <c>GetBytes()</c> call on a string in C#.
|
||||
/// </summary>
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
FfiCompilationResult_t compile_from_string (
|
||||
slice_ref_uint16_t input);
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Size = 48)]
|
||||
public unsafe struct FfiDiagnostic_t {
|
||||
public Vec_uint8_t message;
|
||||
@@ -146,6 +172,12 @@ public unsafe partial class Ffi {
|
||||
Vec_FfiDocumentedItem_t v);
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
void free_ffi_compilation_result (
|
||||
FfiCompilationResult_t input);
|
||||
}
|
||||
|
||||
public unsafe partial class Ffi {
|
||||
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
|
||||
void free_ffi_diagnostic_vec (
|
||||
|
||||
@@ -15,11 +15,64 @@ public static class GlobalCode
|
||||
// so that save file data is smaller
|
||||
private static Dictionary<Guid, string> codeDict = new();
|
||||
|
||||
// This Dictionary stores the source maps for the given SLANG_REF, where
|
||||
// the key is the IC10 line, and the value is a List of Slang ranges where that
|
||||
// line would have come from
|
||||
private static Dictionary<Guid, Dictionary<uint, List<Range>>> sourceMaps = new();
|
||||
|
||||
public static void ClearCache()
|
||||
{
|
||||
codeDict.Clear();
|
||||
}
|
||||
|
||||
public static void SetSourceMap(Guid reference, List<SourceMapEntry> sourceMapEntries)
|
||||
{
|
||||
var builtDictionary = new Dictionary<uint, List<Range>>();
|
||||
|
||||
foreach (var entry in sourceMapEntries)
|
||||
{
|
||||
if (!builtDictionary.ContainsKey(entry.Ic10Line))
|
||||
{
|
||||
builtDictionary[entry.Ic10Line] = new();
|
||||
}
|
||||
builtDictionary[entry.Ic10Line].Add(entry.SlangSource);
|
||||
}
|
||||
|
||||
sourceMaps[reference] = builtDictionary;
|
||||
}
|
||||
|
||||
public static bool GetSlangErrorLineFromICError(
|
||||
Guid reference,
|
||||
uint icErrorLine,
|
||||
out uint slangSrc,
|
||||
out Range slangSpan
|
||||
)
|
||||
{
|
||||
slangSrc = icErrorLine;
|
||||
slangSpan = new Range { };
|
||||
|
||||
if (!sourceMaps.ContainsKey(reference))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!sourceMaps[reference].ContainsKey(icErrorLine))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var foundRange = sourceMaps[reference][icErrorLine];
|
||||
|
||||
if (foundRange is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
slangSrc = foundRange[0].StartLine;
|
||||
slangSpan = foundRange[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
public static string GetSource(Guid reference)
|
||||
{
|
||||
if (!codeDict.ContainsKey(reference))
|
||||
|
||||
@@ -10,10 +10,23 @@ using StationeersIC10Editor;
|
||||
|
||||
public struct Range
|
||||
{
|
||||
public uint StartCol;
|
||||
public uint EndCol;
|
||||
public uint StartLine;
|
||||
public uint EndLine;
|
||||
public uint StartCol = 0;
|
||||
public uint EndCol = 0;
|
||||
public uint StartLine = 0;
|
||||
public uint EndLine = 0;
|
||||
|
||||
public Range(uint startLine, uint startCol, uint endLine, uint endCol)
|
||||
{
|
||||
StartLine = startLine;
|
||||
StartCol = startCol;
|
||||
EndLine = endLine;
|
||||
EndCol = endCol;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"L{StartLine}C{StartCol} - L{EndLine}C{EndCol}";
|
||||
}
|
||||
}
|
||||
|
||||
public struct Diagnostic
|
||||
@@ -23,6 +36,17 @@ public struct Diagnostic
|
||||
public Range Range;
|
||||
}
|
||||
|
||||
public struct SourceMapEntry
|
||||
{
|
||||
public Range SlangSource;
|
||||
public uint Ic10Line;
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"IC10: {Ic10Line} Slang: `{SlangSource}`";
|
||||
}
|
||||
}
|
||||
|
||||
public static class Marshal
|
||||
{
|
||||
private static IntPtr _libraryHandle = IntPtr.Zero;
|
||||
@@ -78,11 +102,16 @@ public static class Marshal
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe bool CompileFromString(string inputString, out string compiledString)
|
||||
public static unsafe bool CompileFromString(
|
||||
string inputString,
|
||||
out string compiledString,
|
||||
out List<SourceMapEntry> sourceMapEntries
|
||||
)
|
||||
{
|
||||
if (String.IsNullOrEmpty(inputString) || !EnsureLibLoaded())
|
||||
{
|
||||
compiledString = String.Empty;
|
||||
sourceMapEntries = new();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -95,19 +124,16 @@ public static class Marshal
|
||||
};
|
||||
|
||||
var result = Ffi.compile_from_string(input);
|
||||
|
||||
try
|
||||
{
|
||||
if ((ulong)result.len < 1)
|
||||
{
|
||||
compiledString = String.Empty;
|
||||
return false;
|
||||
}
|
||||
compiledString = result.AsString();
|
||||
sourceMapEntries = result.source_map.ToList();
|
||||
compiledString = result.output_code.AsString();
|
||||
return true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
result.Drop();
|
||||
Ffi.free_ffi_compilation_result(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,43 @@
|
||||
namespace Slang;
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Assets.Scripts.Objects;
|
||||
using Assets.Scripts.Objects.Electrical;
|
||||
using Assets.Scripts.Objects.Motherboards;
|
||||
using Assets.Scripts.UI;
|
||||
using HarmonyLib;
|
||||
|
||||
class LineErrorData
|
||||
{
|
||||
public AsciiString SourceRef;
|
||||
public uint IC10ErrorSource;
|
||||
public string SlangErrorReference;
|
||||
public Range SlangErrorSpan;
|
||||
|
||||
public LineErrorData(
|
||||
AsciiString sourceRef,
|
||||
uint ic10ErrorSource,
|
||||
string slangErrorRef,
|
||||
Range slangErrorSpan
|
||||
)
|
||||
{
|
||||
this.SourceRef = sourceRef;
|
||||
this.IC10ErrorSource = ic10ErrorSource;
|
||||
this.SlangErrorReference = slangErrorRef;
|
||||
this.SlangErrorSpan = slangErrorSpan;
|
||||
}
|
||||
}
|
||||
|
||||
[HarmonyPatch]
|
||||
public static class SlangPatches
|
||||
{
|
||||
private static ProgrammableChipMotherboard? _currentlyEditingMotherboard;
|
||||
private static AsciiString? _motherboardCachedCode;
|
||||
private static Guid? _currentlyEditingGuid;
|
||||
|
||||
private static ConditionalWeakTable<ProgrammableChip, LineErrorData> _errorReferenceTable =
|
||||
new();
|
||||
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChipMotherboard),
|
||||
@@ -25,17 +51,20 @@ public static class SlangPatches
|
||||
// guard to ensure we have valid IC10 before continuing
|
||||
if (
|
||||
!SlangPlugin.IsSlangSource(ref result)
|
||||
|| !Marshal.CompileFromString(result, out string compiled)
|
||||
|| !Marshal.CompileFromString(result, out var compiled, out var sourceMap)
|
||||
|| string.IsNullOrEmpty(compiled)
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var thisRef = Guid.NewGuid();
|
||||
var thisRef = _currentlyEditingGuid ?? Guid.NewGuid();
|
||||
|
||||
// Ensure we cache this compiled code for later retreival.
|
||||
GlobalCode.SetSource(thisRef, result);
|
||||
GlobalCode.SetSourceMap(thisRef, sourceMap);
|
||||
|
||||
_currentlyEditingGuid = null;
|
||||
|
||||
// Append REF to the bottom
|
||||
compiled += $"\n{GlobalCode.SLANG_REF}{thisRef}";
|
||||
@@ -77,6 +106,7 @@ public static class SlangPatches
|
||||
return;
|
||||
}
|
||||
|
||||
_currentlyEditingGuid = sourceRef;
|
||||
var slangSource = GlobalCode.GetSource(sourceRef);
|
||||
|
||||
if (string.IsNullOrEmpty(slangSource))
|
||||
@@ -136,6 +166,100 @@ public static class SlangPatches
|
||||
chipData.SourceCode = code;
|
||||
}
|
||||
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChip),
|
||||
nameof(ProgrammableChip.ErrorLineNumberString),
|
||||
MethodType.Getter
|
||||
)]
|
||||
[HarmonyPostfix]
|
||||
public static void pgc_ErrorLineNumberString(ProgrammableChip __instance, ref string __result)
|
||||
{
|
||||
if (
|
||||
String.IsNullOrEmpty(__result)
|
||||
|| !uint.TryParse(__result.Trim(), out var ic10ErrorLineNumber)
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var sourceAscii = __instance.GetSourceCode();
|
||||
|
||||
if (_errorReferenceTable.TryGetValue(__instance, out var cache))
|
||||
{
|
||||
if (cache.SourceRef.Equals(sourceAscii) && cache.IC10ErrorSource == ic10ErrorLineNumber)
|
||||
{
|
||||
__result = cache.SlangErrorReference;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var source = System.Text.Encoding.UTF8.GetString(
|
||||
System.Text.Encoding.ASCII.GetBytes(__instance.GetSourceCode())
|
||||
);
|
||||
|
||||
var slangIndex = source.LastIndexOf(GlobalCode.SLANG_REF);
|
||||
|
||||
if (
|
||||
slangIndex < 0
|
||||
|| !Guid.TryParse(
|
||||
source
|
||||
.Substring(
|
||||
source.LastIndexOf(GlobalCode.SLANG_REF) + GlobalCode.SLANG_REF.Length
|
||||
)
|
||||
.Trim(),
|
||||
out var slangGuid
|
||||
)
|
||||
|| !GlobalCode.GetSlangErrorLineFromICError(
|
||||
slangGuid,
|
||||
ic10ErrorLineNumber,
|
||||
out var slangErrorLineNumber,
|
||||
out var slangSpan
|
||||
)
|
||||
)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
L.Warning($"IC error at: {__result} -- Slang source error line: {slangErrorLineNumber}");
|
||||
__result = slangErrorLineNumber.ToString();
|
||||
_errorReferenceTable.Remove(__instance);
|
||||
_errorReferenceTable.Add(
|
||||
__instance,
|
||||
new LineErrorData(
|
||||
sourceAscii,
|
||||
ic10ErrorLineNumber,
|
||||
slangErrorLineNumber.ToString(),
|
||||
slangSpan
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChip),
|
||||
nameof(ProgrammableChip.SetSourceCode),
|
||||
new Type[] { typeof(string) }
|
||||
)]
|
||||
[HarmonyPostfix]
|
||||
public static void pgc_SetSourceCode_string(ProgrammableChip __instance, string sourceCode)
|
||||
{
|
||||
_errorReferenceTable.Remove(__instance);
|
||||
}
|
||||
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChip),
|
||||
nameof(ProgrammableChip.SetSourceCode),
|
||||
new Type[] { typeof(string), typeof(ICircuitHolder) }
|
||||
)]
|
||||
[HarmonyPostfix]
|
||||
public static void pgc_SetSourceCode_string_parent(
|
||||
ProgrammableChip __instance,
|
||||
string sourceCode,
|
||||
ICircuitHolder parent
|
||||
)
|
||||
{
|
||||
_errorReferenceTable.Remove(__instance);
|
||||
}
|
||||
|
||||
[HarmonyPatch(
|
||||
typeof(ProgrammableChipMotherboard),
|
||||
nameof(ProgrammableChipMotherboard.SerializeSave)
|
||||
@@ -223,6 +347,7 @@ public static class SlangPatches
|
||||
|
||||
_currentlyEditingMotherboard = null;
|
||||
_motherboardCachedCode = null;
|
||||
_currentlyEditingGuid = null;
|
||||
}
|
||||
|
||||
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Slang
|
||||
{
|
||||
public const string PluginGuid = "com.biddydev.slang";
|
||||
public const string PluginName = "Slang";
|
||||
public const string PluginVersion = "0.1.1";
|
||||
public const string PluginVersion = "0.3.1";
|
||||
|
||||
public static Mod MOD = new Mod(PluginName, PluginVersion);
|
||||
|
||||
|
||||
@@ -26,12 +26,18 @@ public static class TextMeshProFormatter
|
||||
RegexOptions.Singleline
|
||||
);
|
||||
|
||||
// 3. Handle Headers (## Header)
|
||||
// Convert ## Header to large bold text
|
||||
text = Regex.Replace(
|
||||
text,
|
||||
@"^##(\s+)?(.+)$",
|
||||
"<size=120%><b>$1</b></size>",
|
||||
@"^\s*##\s+(.+)$",
|
||||
"<size=110%><color=#ffffff><b>$1</b></color></size>",
|
||||
RegexOptions.Multiline
|
||||
);
|
||||
|
||||
// 3. Handle # Headers SECOND (General)
|
||||
text = Regex.Replace(
|
||||
text,
|
||||
@"^\s*#\s+(.+)$",
|
||||
"<size=120%><color=#ffffff><b>$1</b></color></size>",
|
||||
RegexOptions.Multiline
|
||||
);
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<AssemblyName>StationeersSlang</AssemblyName>
|
||||
<Description>Slang Compiler Bridge</Description>
|
||||
<Version>0.2.0</Version>
|
||||
<Version>0.3.1</Version>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
24
rust_compiler/Cargo.lock
generated
24
rust_compiler/Cargo.lock
generated
@@ -268,6 +268,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"helpers",
|
||||
"il",
|
||||
"indoc",
|
||||
"lsp-types",
|
||||
"parser",
|
||||
@@ -397,6 +398,15 @@ name = "helpers"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"lsp-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "il"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"helpers",
|
||||
"rust_decimal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -563,6 +573,16 @@ version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "optimizer"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"helpers",
|
||||
"il",
|
||||
"rust_decimal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parser"
|
||||
version = "0.1.0"
|
||||
@@ -571,6 +591,7 @@ dependencies = [
|
||||
"helpers",
|
||||
"lsp-types",
|
||||
"pretty_assertions",
|
||||
"safer-ffi",
|
||||
"thiserror",
|
||||
"tokenizer",
|
||||
]
|
||||
@@ -909,13 +930,14 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e"
|
||||
|
||||
[[package]]
|
||||
name = "slang"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"compiler",
|
||||
"helpers",
|
||||
"lsp-types",
|
||||
"optimizer",
|
||||
"parser",
|
||||
"rust_decimal",
|
||||
"safer-ffi",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "slang"
|
||||
version = "0.2.0"
|
||||
version = "0.3.1"
|
||||
edition = "2021"
|
||||
|
||||
[workspace]
|
||||
@@ -9,9 +9,10 @@ members = ["libs/*"]
|
||||
[workspace.dependencies]
|
||||
thiserror = "2"
|
||||
rust_decimal = "1"
|
||||
safer-ffi = { version = "0.1" } # Safely share structs in memory between C# and Rust
|
||||
lsp-types = { version = "0.97" } # Allows for LSP style reporting to the frontend
|
||||
crc32fast = "1.5" # This is for `HASH(..)` calls to be optimized away
|
||||
safer-ffi = { version = "0.1" } # Safely share structs in memory between C# and Rust
|
||||
lsp-types = { version = "0.97" } # Allows for LSP style reporting to the frontend
|
||||
crc32fast = "1.5" # This is for `HASH(..)` calls to be optimized away
|
||||
anyhow = { version = "^1.0", features = ["backtrace"] }
|
||||
|
||||
[features]
|
||||
headers = ["safer-ffi/headers"]
|
||||
@@ -42,7 +43,8 @@ tokenizer = { path = "libs/tokenizer" }
|
||||
parser = { path = "libs/parser" }
|
||||
compiler = { path = "libs/compiler" }
|
||||
helpers = { path = "libs/helpers" }
|
||||
optimizer = { path = "libs/optimizer" }
|
||||
safer-ffi = { workspace = true }
|
||||
anyhow = { version = "^1.0", features = ["backtrace"] }
|
||||
anyhow = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -8,6 +8,7 @@ thiserror = { workspace = true }
|
||||
parser = { path = "../parser" }
|
||||
tokenizer = { path = "../tokenizer" }
|
||||
helpers = { path = "../helpers" }
|
||||
il = { path = "../il" }
|
||||
lsp-types = { workspace = true }
|
||||
rust_decimal = { workspace = true }
|
||||
|
||||
|
||||
@@ -3,4 +3,4 @@ mod test;
|
||||
mod v1;
|
||||
mod variable_manager;
|
||||
|
||||
pub use v1::{Compiler, CompilerConfig, Error};
|
||||
pub use v1::{CompilationResult, Compiler, CompilerConfig, Error};
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
use crate::compile;
|
||||
use anyhow::Result;
|
||||
use indoc::indoc;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[test]
|
||||
fn simple_binary_expression() -> anyhow::Result<()> {
|
||||
fn simple_binary_expression() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
@@ -17,7 +18,7 @@ fn simple_binary_expression() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 3 #i
|
||||
move r8 3
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -26,7 +27,7 @@ fn simple_binary_expression() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn nested_binary_expressions() -> anyhow::Result<()> {
|
||||
fn nested_binary_expressions() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
@@ -44,13 +45,15 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
calculateArgs:
|
||||
pop r8 #arg3
|
||||
pop r9 #arg2
|
||||
pop r10 #arg1
|
||||
pop r8
|
||||
pop r9
|
||||
pop r10
|
||||
push ra
|
||||
add r1 r10 r9
|
||||
mul r2 r1 r8
|
||||
move r15 r2
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
@@ -60,9 +63,9 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
||||
push 20
|
||||
push 30
|
||||
jal calculateArgs
|
||||
move r1 r15 #__binary_temp_3
|
||||
move r1 r15
|
||||
add r2 r1 100
|
||||
move r8 r2 #returned
|
||||
move r8 r2
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -71,7 +74,7 @@ fn nested_binary_expressions() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stress_test_constant_folding() -> anyhow::Result<()> {
|
||||
fn stress_test_constant_folding() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
@@ -85,7 +88,7 @@ fn stress_test_constant_folding() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 -123 #negationHell
|
||||
move r8 -123
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -94,7 +97,7 @@ fn stress_test_constant_folding() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
||||
fn test_constant_folding_with_variables_mixed_in() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
@@ -113,7 +116,59 @@ fn test_constant_folding_with_variables_mixed_in() -> anyhow::Result<()> {
|
||||
mul r2 373.2 r1
|
||||
sub r3 1 r2
|
||||
add r4 r3 518.15
|
||||
move r8 r4 #i
|
||||
move r8 r4
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ternary_expression() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
let i = 1 > 2 ? 15 : 20;
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
sgt r1 1 2
|
||||
select r2 r1 15 20
|
||||
move r8 r2
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ternary_expression_assignment() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
let i = 0;
|
||||
i = 1 > 2 ? 15 : 20;
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0
|
||||
sgt r1 1 2
|
||||
select r2 r1 15 20
|
||||
move r8 r2
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
@@ -20,10 +20,10 @@ fn test_if_statement() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 10 #a
|
||||
move r8 10
|
||||
sgt r1 r8 5
|
||||
beq r1 0 L1
|
||||
move r8 20 #a
|
||||
beqz r1 L1
|
||||
move r8 20
|
||||
L1:
|
||||
"
|
||||
}
|
||||
@@ -52,13 +52,13 @@ fn test_if_else_statement() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
sgt r1 10 5
|
||||
beq r1 0 L2
|
||||
move r8 1 #a
|
||||
beqz r1 L2
|
||||
move r8 1
|
||||
j L1
|
||||
L2:
|
||||
move r8 2 #a
|
||||
move r8 2
|
||||
L1:
|
||||
"
|
||||
}
|
||||
@@ -89,18 +89,18 @@ fn test_if_else_if_statement() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
seq r1 r8 1
|
||||
beq r1 0 L2
|
||||
move r8 10 #a
|
||||
beqz r1 L2
|
||||
move r8 10
|
||||
j L1
|
||||
L2:
|
||||
seq r2 r8 2
|
||||
beq r2 0 L4
|
||||
move r8 20 #a
|
||||
beqz r2 L4
|
||||
move r8 20
|
||||
j L3
|
||||
L4:
|
||||
move r8 30 #a
|
||||
move r8 30
|
||||
L3:
|
||||
L1:
|
||||
"
|
||||
@@ -136,18 +136,18 @@ fn test_spilled_variable_update_in_branch() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 1 #a
|
||||
move r9 2 #b
|
||||
move r10 3 #c
|
||||
move r11 4 #d
|
||||
move r12 5 #e
|
||||
move r13 6 #f
|
||||
move r14 7 #g
|
||||
push 8 #h
|
||||
move r8 1
|
||||
move r9 2
|
||||
move r10 3
|
||||
move r11 4
|
||||
move r12 5
|
||||
move r13 6
|
||||
move r14 7
|
||||
push 8
|
||||
seq r1 r8 1
|
||||
beq r1 0 L1
|
||||
beqz r1 L1
|
||||
sub r0 sp 1
|
||||
put db r0 99 #h
|
||||
put db r0 99
|
||||
L1:
|
||||
sub sp sp 1
|
||||
"
|
||||
|
||||
@@ -17,13 +17,14 @@ fn no_arguments() -> anyhow::Result<()> {
|
||||
j main
|
||||
doSomething:
|
||||
push ra
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
jal doSomething
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
};
|
||||
|
||||
@@ -34,14 +35,17 @@ fn no_arguments() -> anyhow::Result<()> {
|
||||
|
||||
#[test]
|
||||
fn let_var_args() -> anyhow::Result<()> {
|
||||
// !IMPORTANT this needs to be stabilized as it currently incorrectly calculates sp offset at
|
||||
// both ends of the cleanup lifecycle
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
fn doSomething(arg1) {};
|
||||
let arg1 = 123;
|
||||
let i = doSomething(arg1);
|
||||
fn mul2(arg1) {
|
||||
return arg1 * 2;
|
||||
};
|
||||
loop {
|
||||
let arg1 = 123;
|
||||
let i = mul2(arg1);
|
||||
i = i ** 2;
|
||||
}
|
||||
"
|
||||
};
|
||||
|
||||
@@ -50,23 +54,31 @@ fn let_var_args() -> anyhow::Result<()> {
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg1
|
||||
mul2:
|
||||
pop r8
|
||||
push ra
|
||||
mul r1 r8 2
|
||||
move r15 r1
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
move r8 123 #arg1
|
||||
L2:
|
||||
move r8 123
|
||||
push r8
|
||||
push r8
|
||||
jal doSomething
|
||||
jal mul2
|
||||
sub r0 sp 1
|
||||
get r8 db r0
|
||||
sub sp sp 1
|
||||
move r9 r15 #i
|
||||
sub sp sp 1
|
||||
move r9 r15
|
||||
pow r1 r9 2
|
||||
move r9 r1
|
||||
j L2
|
||||
L3:
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -97,7 +109,9 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
"
|
||||
fn doSomething(arg1, arg2) {};
|
||||
fn doSomething(arg1, arg2) {
|
||||
return 5;
|
||||
};
|
||||
let thisVariableShouldStayInPlace = 123;
|
||||
let returnedValue = doSomething(12, 34);
|
||||
"
|
||||
@@ -109,15 +123,18 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg2
|
||||
pop r9 #arg1
|
||||
pop r8
|
||||
pop r9
|
||||
push ra
|
||||
move r15 5
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
move r8 123 #thisVariableShouldStayInPlace
|
||||
move r8 123
|
||||
push r8
|
||||
push 12
|
||||
push 34
|
||||
@@ -125,8 +142,7 @@ fn inline_literal_args() -> anyhow::Result<()> {
|
||||
sub r0 sp 1
|
||||
get r8 db r0
|
||||
sub sp sp 1
|
||||
move r9 r15 #returnedValue
|
||||
sub sp sp 1
|
||||
move r9 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -151,15 +167,16 @@ fn mixed_args() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg2
|
||||
pop r9 #arg1
|
||||
pop r8
|
||||
pop r9
|
||||
push ra
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
move r8 123 #arg1
|
||||
move r8 123
|
||||
push r8
|
||||
push r8
|
||||
push 456
|
||||
@@ -167,8 +184,7 @@ fn mixed_args() -> anyhow::Result<()> {
|
||||
sub r0 sp 1
|
||||
get r8 db r0
|
||||
sub sp sp 1
|
||||
move r9 r15 #returnValue
|
||||
sub sp sp 1
|
||||
move r9 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -195,9 +211,11 @@ fn with_return_statement() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg1
|
||||
pop r8
|
||||
push ra
|
||||
move r15 456 #returnValue
|
||||
move r15 456
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
@@ -205,7 +223,7 @@ fn with_return_statement() -> anyhow::Result<()> {
|
||||
main:
|
||||
push 123
|
||||
jal doSomething
|
||||
move r8 r15 #returned
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -232,14 +250,15 @@ fn with_negative_return_literal() -> anyhow::Result<()> {
|
||||
j main
|
||||
doSomething:
|
||||
push ra
|
||||
move r15 -1 #returnValue
|
||||
move r15 -1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
jal doSomething
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
@@ -15,7 +15,7 @@ fn variable_declaration_numeric_literal() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 293.15 #i
|
||||
move r8 293.15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -46,16 +46,16 @@ fn variable_declaration_numeric_literal_stack_spillover() -> anyhow::Result<()>
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r9 1 #b
|
||||
move r10 2 #c
|
||||
move r11 3 #d
|
||||
move r12 4 #e
|
||||
move r13 5 #f
|
||||
move r14 6 #g
|
||||
push 7 #h
|
||||
push 8 #i
|
||||
push 9 #j
|
||||
move r8 0
|
||||
move r9 1
|
||||
move r10 2
|
||||
move r11 3
|
||||
move r12 4
|
||||
move r13 5
|
||||
move r14 6
|
||||
push 7
|
||||
push 8
|
||||
push 9
|
||||
sub sp sp 3
|
||||
"
|
||||
}
|
||||
@@ -79,7 +79,7 @@ fn variable_declaration_negative() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 -1 #i
|
||||
move r8 -1
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -103,8 +103,8 @@ fn test_boolean_declaration() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 1 #t
|
||||
move r9 0 #f
|
||||
move r8 1
|
||||
move r9 0
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -120,7 +120,7 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
||||
fn getTrue() {
|
||||
return true;
|
||||
};
|
||||
|
||||
|
||||
let val = getTrue();
|
||||
"
|
||||
};
|
||||
@@ -132,14 +132,16 @@ fn test_boolean_return() -> anyhow::Result<()> {
|
||||
j main
|
||||
getTrue:
|
||||
push ra
|
||||
move r15 1 #returnValue
|
||||
move r15 1
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
jal getTrue
|
||||
move r8 r15 #val
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
@@ -13,14 +13,15 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
||||
indoc! {"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg9
|
||||
pop r9 #arg8
|
||||
pop r10 #arg7
|
||||
pop r11 #arg6
|
||||
pop r12 #arg5
|
||||
pop r13 #arg4
|
||||
pop r14 #arg3
|
||||
pop r8
|
||||
pop r9
|
||||
pop r10
|
||||
pop r11
|
||||
pop r12
|
||||
pop r13
|
||||
pop r14
|
||||
push ra
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 3
|
||||
@@ -31,6 +32,48 @@ fn test_function_declaration_with_spillover_params() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_early_return() -> anyhow::Result<()> {
|
||||
let compiled = compile!(debug r#"
|
||||
// This is a test function declaration with no body
|
||||
fn doSomething() {
|
||||
if (1 == 1) {
|
||||
return;
|
||||
}
|
||||
let i = 1 + 2;
|
||||
return;
|
||||
};
|
||||
doSomething();
|
||||
"#);
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
doSomething:
|
||||
push ra
|
||||
seq r1 1 1
|
||||
beqz r1 L2
|
||||
j L1
|
||||
L2:
|
||||
move r8 3
|
||||
j L1
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
j ra
|
||||
main:
|
||||
jal doSomething
|
||||
move r1 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
||||
let compiled = compile!(debug r#"
|
||||
@@ -44,9 +87,10 @@ fn test_function_declaration_with_register_params() -> anyhow::Result<()> {
|
||||
indoc! {"
|
||||
j main
|
||||
doSomething:
|
||||
pop r8 #arg2
|
||||
pop r9 #arg1
|
||||
pop r8
|
||||
pop r9
|
||||
push ra
|
||||
L1:
|
||||
sub r0 sp 1
|
||||
get ra db r0
|
||||
sub sp sp 1
|
||||
|
||||
@@ -23,17 +23,17 @@ fn test_comparison_expressions() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
sgt r1 10 5
|
||||
move r8 r1 #isGreater
|
||||
move r8 r1
|
||||
slt r2 5 10
|
||||
move r9 r2 #isLess
|
||||
move r9 r2
|
||||
seq r3 5 5
|
||||
move r10 r3 #isEqual
|
||||
move r10 r3
|
||||
sne r4 5 10
|
||||
move r11 r4 #isNotEqual
|
||||
move r11 r4
|
||||
sge r5 10 10
|
||||
move r12 r5 #isGreaterOrEqual
|
||||
move r12 r5
|
||||
sle r6 5 5
|
||||
move r13 r6 #isLessOrEqual
|
||||
move r13 r6
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -59,11 +59,11 @@ fn test_logical_and_or_not() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
and r1 1 1
|
||||
move r8 r1 #logic1
|
||||
move r8 r1
|
||||
or r2 1 0
|
||||
move r9 r2 #logic2
|
||||
move r9 r2
|
||||
seq r3 1 0
|
||||
move r10 r3 #logic3
|
||||
move r10 r3
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -89,7 +89,7 @@ fn test_complex_logic() -> anyhow::Result<()> {
|
||||
sgt r1 10 5
|
||||
slt r2 5 10
|
||||
and r3 r1 r2
|
||||
move r8 r3 #logic
|
||||
move r8 r3
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -113,7 +113,7 @@ fn test_math_with_logic() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
sgt r1 3 1
|
||||
move r8 r1 #logic
|
||||
move r8 r1
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -137,7 +137,7 @@ fn test_boolean_in_logic() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
and r1 1 0
|
||||
move r8 r1 #res
|
||||
move r8 r1
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -163,11 +163,11 @@ fn test_invert_a_boolean() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 1 #i
|
||||
move r8 1
|
||||
seq r1 r8 0
|
||||
move r9 r1 #y
|
||||
move r9 r1
|
||||
seq r2 r9 0
|
||||
move r10 r2 #result
|
||||
move r10 r2
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
@@ -21,10 +21,10 @@ fn test_infinite_loop() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
L1:
|
||||
add r1 r8 1
|
||||
move r8 r1 #a
|
||||
move r8 r1
|
||||
j L1
|
||||
L2:
|
||||
"
|
||||
@@ -56,12 +56,12 @@ fn test_loop_break() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
L1:
|
||||
add r1 r8 1
|
||||
move r8 r1 #a
|
||||
move r8 r1
|
||||
sgt r2 r8 10
|
||||
beq r2 0 L3
|
||||
beqz r2 L3
|
||||
j L2
|
||||
L3:
|
||||
j L1
|
||||
@@ -92,12 +92,12 @@ fn test_while_loop() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
L1:
|
||||
slt r1 r8 10
|
||||
beq r1 0 L2
|
||||
beqz r1 L2
|
||||
add r2 r8 1
|
||||
move r8 r2 #a
|
||||
move r8 r2
|
||||
j L1
|
||||
L2:
|
||||
"
|
||||
@@ -130,12 +130,12 @@ fn test_loop_continue() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0 #a
|
||||
move r8 0
|
||||
L1:
|
||||
add r1 r8 1
|
||||
move r8 r1 #a
|
||||
move r8 r1
|
||||
slt r2 r8 5
|
||||
beq r2 0 L3
|
||||
beqz r2 L3
|
||||
j L1
|
||||
L3:
|
||||
j L2
|
||||
|
||||
@@ -19,7 +19,7 @@ fn test_acos() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
acos r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -43,7 +43,7 @@ fn test_asin() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
asin r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -67,7 +67,7 @@ fn test_atan() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
atan r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -91,7 +91,7 @@ fn test_atan2() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
atan2 r15 123 456
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -115,7 +115,7 @@ fn test_abs() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
abs r15 -123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -139,7 +139,7 @@ fn test_ceil() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
ceil r15 123.90
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -163,7 +163,7 @@ fn test_cos() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
cos r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -187,7 +187,7 @@ fn test_floor() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
floor r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -211,7 +211,7 @@ fn test_log() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
log r15 123
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -235,7 +235,33 @@ fn test_max() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
max r15 123 456
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_max_from_game() -> Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
let item = 0;
|
||||
item = max(1 + 2, 2);
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 0
|
||||
max r15 3 2
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -259,7 +285,7 @@ fn test_min() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
min r15 123 456
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -283,7 +309,7 @@ fn test_rand() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
rand r15
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -307,7 +333,7 @@ fn test_sin() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
sin r15 3
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -331,7 +357,7 @@ fn test_sqrt() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
sqrt r15 3
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -355,7 +381,7 @@ fn test_tan() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
tan r15 3
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
@@ -379,7 +405,7 @@ fn test_trunc() -> Result<()> {
|
||||
j main
|
||||
main:
|
||||
trunc r15 3.234
|
||||
move r8 r15 #i
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
@@ -12,31 +12,29 @@ macro_rules! compile {
|
||||
let mut writer = std::io::BufWriter::new(Vec::new());
|
||||
let compiler = ::Compiler::new(
|
||||
parser::Parser::new(tokenizer::Tokenizer::from(String::from($source))),
|
||||
&mut writer,
|
||||
None,
|
||||
);
|
||||
compiler.compile();
|
||||
let res = compiler.compile();
|
||||
res.instructions.write(&mut writer)?;
|
||||
output!(writer)
|
||||
}};
|
||||
|
||||
(result $source:expr) => {{
|
||||
let mut writer = std::io::BufWriter::new(Vec::new());
|
||||
let compiler = crate::Compiler::new(
|
||||
parser::Parser::new(tokenizer::Tokenizer::from($source)),
|
||||
&mut writer,
|
||||
Some(crate::CompilerConfig { debug: true }),
|
||||
);
|
||||
compiler.compile()
|
||||
compiler.compile().errors
|
||||
}};
|
||||
|
||||
(debug $source:expr) => {{
|
||||
let mut writer = std::io::BufWriter::new(Vec::new());
|
||||
let compiler = crate::Compiler::new(
|
||||
parser::Parser::new(tokenizer::Tokenizer::from($source)),
|
||||
&mut writer,
|
||||
Some(crate::CompilerConfig { debug: true }),
|
||||
);
|
||||
compiler.compile();
|
||||
let res = compiler.compile();
|
||||
res.instructions.write(&mut writer)?;
|
||||
output!(writer)
|
||||
}};
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ fn test_sleep() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
sleep 3
|
||||
move r8 15 #sleepAmount
|
||||
move r8 15
|
||||
sleep r8
|
||||
mul r1 r8 2
|
||||
sleep r1
|
||||
@@ -73,7 +73,7 @@ fn test_set_on_device() -> anyhow::Result<()> {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
move r8 293.15 #internalTemp
|
||||
move r8 293.15
|
||||
sgt r1 r8 298.15
|
||||
s d0 On r1
|
||||
"
|
||||
@@ -150,7 +150,58 @@ fn test_load_from_device() -> anyhow::Result<()> {
|
||||
j main
|
||||
main:
|
||||
l r15 d0 On
|
||||
move r8 r15 #setting
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_from_slot() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
device airCon = "d0";
|
||||
|
||||
let setting = ls(airCon, 0, "Occupied");
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
ls r15 d0 0 Occupied
|
||||
move r8 r15
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_set_slot() -> anyhow::Result<()> {
|
||||
let compiled = compile! {
|
||||
debug
|
||||
r#"
|
||||
device airCon = "d0";
|
||||
|
||||
ss(airCon, 0, "Occupied", true);
|
||||
"#
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
compiled,
|
||||
indoc! {
|
||||
"
|
||||
j main
|
||||
main:
|
||||
ss d0 0 Occupied 1
|
||||
"
|
||||
}
|
||||
);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,8 +3,9 @@
|
||||
// r1 - r7 : Temporary Variables
|
||||
// r8 - r14 : Persistant Variables
|
||||
|
||||
use helpers::Span;
|
||||
use lsp_types::{Diagnostic, DiagnosticSeverity};
|
||||
use parser::tree_node::{Literal, Span};
|
||||
use parser::tree_node::Literal;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::{HashMap, VecDeque},
|
||||
@@ -52,7 +53,7 @@ pub enum LocationRequest {
|
||||
Stack,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum VariableLocation<'a> {
|
||||
/// Represents a temporary register (r1 - r7)
|
||||
Temporary(u8),
|
||||
@@ -66,7 +67,6 @@ pub enum VariableLocation<'a> {
|
||||
Device(Cow<'a, str>),
|
||||
}
|
||||
|
||||
// FIX: Added 'b lifetime for the parent reference
|
||||
pub struct VariableScope<'a, 'b> {
|
||||
temporary_vars: VecDeque<u8>,
|
||||
persistant_vars: VecDeque<u8>,
|
||||
@@ -75,7 +75,6 @@ pub struct VariableScope<'a, 'b> {
|
||||
parent: Option<&'b VariableScope<'a, 'b>>,
|
||||
}
|
||||
|
||||
// FIX: Updated Default impl to include 'b
|
||||
impl<'a, 'b> Default for VariableScope<'a, 'b> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
@@ -88,7 +87,6 @@ impl<'a, 'b> Default for VariableScope<'a, 'b> {
|
||||
}
|
||||
}
|
||||
|
||||
// FIX: Updated impl block to include 'b
|
||||
impl<'a, 'b> VariableScope<'a, 'b> {
|
||||
#[allow(dead_code)]
|
||||
pub const TEMP_REGISTER_COUNT: u8 = 7;
|
||||
@@ -97,22 +95,23 @@ impl<'a, 'b> VariableScope<'a, 'b> {
|
||||
pub const RETURN_REGISTER: u8 = 15;
|
||||
pub const TEMP_STACK_REGISTER: u8 = 0;
|
||||
|
||||
pub fn registers(&self) -> impl Iterator<Item = &u8> {
|
||||
self.var_lookup_table
|
||||
.values()
|
||||
.filter(|val| {
|
||||
matches!(
|
||||
val,
|
||||
VariableLocation::Temporary(_) | VariableLocation::Persistant(_)
|
||||
)
|
||||
})
|
||||
.map(|loc| match loc {
|
||||
VariableLocation::Persistant(reg) | VariableLocation::Temporary(reg) => reg,
|
||||
_ => unreachable!(),
|
||||
})
|
||||
pub fn registers(&self) -> Vec<u8> {
|
||||
let mut used = Vec::new();
|
||||
|
||||
for r in TEMP {
|
||||
if !self.temporary_vars.contains(&r) {
|
||||
used.push(r);
|
||||
}
|
||||
}
|
||||
|
||||
for r in PERSIST {
|
||||
if !self.persistant_vars.contains(&r) {
|
||||
used.push(r);
|
||||
}
|
||||
}
|
||||
used
|
||||
}
|
||||
|
||||
// FIX: parent is now &'b VariableScope<'a, 'b>
|
||||
pub fn scoped(parent: &'b VariableScope<'a, 'b>) -> Self {
|
||||
Self {
|
||||
parent: Option::Some(parent),
|
||||
|
||||
@@ -5,3 +5,4 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
crc32fast = { workspace = true }
|
||||
lsp-types = { workspace = true }
|
||||
|
||||
@@ -2,6 +2,44 @@ mod helper_funcs;
|
||||
mod macros;
|
||||
mod syscall;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Span {
|
||||
pub start_line: usize,
|
||||
pub end_line: usize,
|
||||
pub start_col: usize,
|
||||
pub end_col: usize,
|
||||
}
|
||||
|
||||
impl From<Span> for lsp_types::Range {
|
||||
fn from(value: Span) -> Self {
|
||||
Self {
|
||||
start: lsp_types::Position {
|
||||
line: value.start_line as u32,
|
||||
character: value.start_col as u32,
|
||||
},
|
||||
end: lsp_types::Position {
|
||||
line: value.end_line as u32,
|
||||
character: value.end_col as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Span> for lsp_types::Range {
|
||||
fn from(value: &Span) -> Self {
|
||||
Self {
|
||||
start: lsp_types::Position {
|
||||
line: value.start_line as u32,
|
||||
character: value.start_col as u32,
|
||||
},
|
||||
end: lsp_types::Position {
|
||||
line: value.end_line as u32,
|
||||
character: value.end_col as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This trait will allow the LSP to emit documentation for various tokens and expressions.
|
||||
/// You can easily create documentation for large enums with the `documented!` macro.
|
||||
pub trait Documentation {
|
||||
|
||||
@@ -9,9 +9,11 @@ macro_rules! with_syscalls {
|
||||
"load",
|
||||
"loadBatched",
|
||||
"loadBatchedNamed",
|
||||
"loadSlot",
|
||||
"set",
|
||||
"setBatched",
|
||||
"setBatchedNamed",
|
||||
"setSlot",
|
||||
"acos",
|
||||
"asin",
|
||||
"atan",
|
||||
@@ -32,9 +34,11 @@ macro_rules! with_syscalls {
|
||||
"l",
|
||||
"lb",
|
||||
"lbn",
|
||||
"ls",
|
||||
"s",
|
||||
"sb",
|
||||
"sbn"
|
||||
"sbn",
|
||||
"ss"
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
8
rust_compiler/libs/il/Cargo.toml
Normal file
8
rust_compiler/libs/il/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "il"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
helpers = { path = "../helpers" }
|
||||
rust_decimal = { workspace = true }
|
||||
350
rust_compiler/libs/il/src/lib.rs
Normal file
350
rust_compiler/libs/il/src/lib.rs
Normal file
@@ -0,0 +1,350 @@
|
||||
use helpers::Span;
|
||||
use rust_decimal::Decimal;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::io::{BufWriter, Write};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Instructions<'a>(Vec<InstructionNode<'a>>);
|
||||
|
||||
impl<'a> Deref for Instructions<'a> {
|
||||
type Target = Vec<InstructionNode<'a>>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for Instructions<'a> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Instructions<'a> {
|
||||
pub fn new(instructions: Vec<InstructionNode<'a>>) -> Self {
|
||||
Self(instructions)
|
||||
}
|
||||
pub fn into_inner(self) -> Vec<InstructionNode<'a>> {
|
||||
self.0
|
||||
}
|
||||
pub fn write<W: Write>(self, writer: &mut BufWriter<W>) -> Result<(), std::io::Error> {
|
||||
for node in self.0 {
|
||||
writer.write_all(node.to_string().as_bytes())?;
|
||||
writer.write_all(b"\n")?;
|
||||
}
|
||||
|
||||
writer.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn source_map(&self) -> HashMap<usize, Span> {
|
||||
let mut map = HashMap::new();
|
||||
|
||||
for (line_num, node) in self.0.iter().enumerate() {
|
||||
if let Some(span) = node.span {
|
||||
map.insert(line_num, span);
|
||||
}
|
||||
}
|
||||
|
||||
map
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for Instructions<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
for node in &self.0 {
|
||||
writeln!(f, "{node}")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct InstructionNode<'a> {
|
||||
pub instruction: Instruction<'a>,
|
||||
pub span: Option<Span>,
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for InstructionNode<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.instruction)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> InstructionNode<'a> {
|
||||
pub fn new(instr: Instruction<'a>, span: Option<Span>) -> Self {
|
||||
Self {
|
||||
span,
|
||||
instruction: instr,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the different types of operands available in IC10.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Operand<'a> {
|
||||
/// A hardware register (r0-r15)
|
||||
Register(u8),
|
||||
/// A device alias or direct connection (d0-d5, db)
|
||||
Device(Cow<'a, str>),
|
||||
/// A numeric literal (integer or float)
|
||||
Number(Decimal),
|
||||
/// A label used for jumping
|
||||
Label(Cow<'a, str>),
|
||||
/// A logic type string (e.g., "Temperature", "Open")
|
||||
LogicType(Cow<'a, str>),
|
||||
/// Special register: Stack Pointer
|
||||
StackPointer,
|
||||
/// Special register: Return Address
|
||||
ReturnAddress,
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Operand<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Operand::Register(r) => write!(f, "r{}", r),
|
||||
Operand::Device(d) => write!(f, "{}", d),
|
||||
Operand::Number(n) => write!(f, "{}", n),
|
||||
Operand::Label(l) => write!(f, "{}", l),
|
||||
Operand::LogicType(t) => write!(f, "{}", t),
|
||||
Operand::StackPointer => write!(f, "sp"),
|
||||
Operand::ReturnAddress => write!(f, "ra"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a single IC10 MIPS instruction.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum Instruction<'a> {
|
||||
/// `move dst val` - Copy value to register
|
||||
Move(Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `add dst a b` - Addition
|
||||
Add(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sub dst a b` - Subtraction
|
||||
Sub(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `mul dst a b` - Multiplication
|
||||
Mul(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `div dst a b` - Division
|
||||
Div(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `mod dst a b` - Modulo
|
||||
Mod(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `pow dst a b` - Power
|
||||
Pow(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `acos dst a`
|
||||
Acos(Operand<'a>, Operand<'a>),
|
||||
/// `asin dst a`
|
||||
Asin(Operand<'a>, Operand<'a>),
|
||||
/// `atan dst a`
|
||||
Atan(Operand<'a>, Operand<'a>),
|
||||
/// `atan2 dst a b`
|
||||
Atan2(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `abs dst a`
|
||||
Abs(Operand<'a>, Operand<'a>),
|
||||
/// `ceil dst a`
|
||||
Ceil(Operand<'a>, Operand<'a>),
|
||||
/// `cos dst a`
|
||||
Cos(Operand<'a>, Operand<'a>),
|
||||
/// `floor dst a`
|
||||
Floor(Operand<'a>, Operand<'a>),
|
||||
/// `log dst a`
|
||||
Log(Operand<'a>, Operand<'a>),
|
||||
/// `max dst a b`
|
||||
Max(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `min dst a b`
|
||||
Min(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `rand dst`
|
||||
Rand(Operand<'a>),
|
||||
/// `sin dst a`
|
||||
Sin(Operand<'a>, Operand<'a>),
|
||||
/// `sqrt dst a`
|
||||
Sqrt(Operand<'a>, Operand<'a>),
|
||||
/// `tan dst a`
|
||||
Tan(Operand<'a>, Operand<'a>),
|
||||
/// `trunc dst a`
|
||||
Trunc(Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `l register device type` - Load from device
|
||||
Load(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `s device type value` - Set on device
|
||||
Store(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `ls register device slot type` - Load Slot
|
||||
LoadSlot(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `ss device slot type value` - Set Slot
|
||||
StoreSlot(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `lb register deviceHash type batchMode` - Load Batch
|
||||
LoadBatch(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sb deviceHash type value` - Set Batch
|
||||
StoreBatch(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `lbn register deviceHash nameHash type batchMode` - Load Batch Named
|
||||
LoadBatchNamed(
|
||||
Operand<'a>,
|
||||
Operand<'a>,
|
||||
Operand<'a>,
|
||||
Operand<'a>,
|
||||
Operand<'a>,
|
||||
),
|
||||
/// `sbn deviceHash nameHash type value` - Set Batch Named
|
||||
StoreBatchNamed(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `j label` - Unconditional Jump
|
||||
Jump(Operand<'a>),
|
||||
/// `jal label` - Jump and Link (Function Call)
|
||||
JumpAndLink(Operand<'a>),
|
||||
/// `jr offset` - Jump Relative
|
||||
JumpRelative(Operand<'a>),
|
||||
|
||||
/// `beq a b label` - Branch if Equal
|
||||
BranchEq(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `bne a b label` - Branch if Not Equal
|
||||
BranchNe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `bgt a b label` - Branch if Greater Than
|
||||
BranchGt(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `blt a b label` - Branch if Less Than
|
||||
BranchLt(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `bge a b label` - Branch if Greater or Equal
|
||||
BranchGe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `ble a b label` - Branch if Less or Equal
|
||||
BranchLe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `beqz a label` - Branch if Equal Zero
|
||||
BranchEqZero(Operand<'a>, Operand<'a>),
|
||||
/// `bnez a label` - Branch if Not Equal Zero
|
||||
BranchNeZero(Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `seq dst a b` - Set if Equal
|
||||
SetEq(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sne dst a b` - Set if Not Equal
|
||||
SetNe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sgt dst a b` - Set if Greater Than
|
||||
SetGt(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `slt dst a b` - Set if Less Than
|
||||
SetLt(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sge dst a b` - Set if Greater or Equal
|
||||
SetGe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `sle dst a b` - Set if Less or Equal
|
||||
SetLe(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `and dst a b` - Logical AND
|
||||
And(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `or dst a b` - Logical OR
|
||||
Or(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// `xor dst a b` - Logical XOR
|
||||
Xor(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `push val` - Push to Stack
|
||||
Push(Operand<'a>),
|
||||
/// `pop dst` - Pop from Stack
|
||||
Pop(Operand<'a>),
|
||||
/// `peek dst` - Peek from Stack (Usually sp - 1)
|
||||
Peek(Operand<'a>),
|
||||
/// `get dst dev num`
|
||||
Get(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
/// put dev addr val
|
||||
Put(Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `select dst cond a b` - Ternary Select
|
||||
Select(Operand<'a>, Operand<'a>, Operand<'a>, Operand<'a>),
|
||||
|
||||
/// `yield` - Pause execution
|
||||
Yield,
|
||||
/// `sleep val` - Sleep for seconds
|
||||
Sleep(Operand<'a>),
|
||||
|
||||
/// `alias name target` - Define Alias (Usually handled by compiler, but good for IR)
|
||||
Alias(Cow<'a, str>, Operand<'a>),
|
||||
/// `define name val` - Define Constant (Usually handled by compiler)
|
||||
Define(Cow<'a, str>, f64),
|
||||
|
||||
/// A label definition `Label:`
|
||||
LabelDef(Cow<'a, str>),
|
||||
|
||||
/// A comment `# text`
|
||||
Comment(Cow<'a, str>),
|
||||
}
|
||||
|
||||
impl<'a> fmt::Display for Instruction<'a> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Instruction::Move(dst, val) => write!(f, "move {} {}", dst, val),
|
||||
Instruction::Add(dst, a, b) => write!(f, "add {} {} {}", dst, a, b),
|
||||
Instruction::Sub(dst, a, b) => write!(f, "sub {} {} {}", dst, a, b),
|
||||
Instruction::Mul(dst, a, b) => write!(f, "mul {} {} {}", dst, a, b),
|
||||
Instruction::Div(dst, a, b) => write!(f, "div {} {} {}", dst, a, b),
|
||||
Instruction::Mod(dst, a, b) => write!(f, "mod {} {} {}", dst, a, b),
|
||||
Instruction::Pow(dst, a, b) => write!(f, "pow {} {} {}", dst, a, b),
|
||||
Instruction::Acos(dst, a) => write!(f, "acos {} {}", dst, a),
|
||||
Instruction::Asin(dst, a) => write!(f, "asin {} {}", dst, a),
|
||||
Instruction::Atan(dst, a) => write!(f, "atan {} {}", dst, a),
|
||||
Instruction::Atan2(dst, a, b) => write!(f, "atan2 {} {} {}", dst, a, b),
|
||||
Instruction::Abs(dst, a) => write!(f, "abs {} {}", dst, a),
|
||||
Instruction::Ceil(dst, a) => write!(f, "ceil {} {}", dst, a),
|
||||
Instruction::Cos(dst, a) => write!(f, "cos {} {}", dst, a),
|
||||
Instruction::Floor(dst, a) => write!(f, "floor {} {}", dst, a),
|
||||
Instruction::Log(dst, a) => write!(f, "log {} {}", dst, a),
|
||||
Instruction::Max(dst, a, b) => write!(f, "max {} {} {}", dst, a, b),
|
||||
Instruction::Min(dst, a, b) => write!(f, "min {} {} {}", dst, a, b),
|
||||
Instruction::Rand(dst) => write!(f, "rand {}", dst),
|
||||
Instruction::Sin(dst, a) => write!(f, "sin {} {}", dst, a),
|
||||
Instruction::Sqrt(dst, a) => write!(f, "sqrt {} {}", dst, a),
|
||||
Instruction::Tan(dst, a) => write!(f, "tan {} {}", dst, a),
|
||||
Instruction::Trunc(dst, a) => write!(f, "trunc {} {}", dst, a),
|
||||
|
||||
Instruction::Load(reg, dev, typ) => write!(f, "l {} {} {}", reg, dev, typ),
|
||||
Instruction::Store(dev, typ, val) => write!(f, "s {} {} {}", dev, typ, val),
|
||||
Instruction::LoadSlot(reg, dev, slot, typ) => {
|
||||
write!(f, "ls {} {} {} {}", reg, dev, slot, typ)
|
||||
}
|
||||
Instruction::StoreSlot(dev, slot, typ, val) => {
|
||||
write!(f, "ss {} {} {} {}", dev, slot, typ, val)
|
||||
}
|
||||
Instruction::LoadBatch(reg, hash, typ, mode) => {
|
||||
write!(f, "lb {} {} {} {}", reg, hash, typ, mode)
|
||||
}
|
||||
Instruction::StoreBatch(hash, typ, val) => write!(f, "sb {} {} {}", hash, typ, val),
|
||||
Instruction::LoadBatchNamed(reg, d_hash, n_hash, typ, mode) => {
|
||||
write!(f, "lbn {} {} {} {} {}", reg, d_hash, n_hash, typ, mode)
|
||||
}
|
||||
Instruction::StoreBatchNamed(d_hash, n_hash, typ, val) => {
|
||||
write!(f, "sbn {} {} {} {}", d_hash, n_hash, typ, val)
|
||||
}
|
||||
Instruction::Jump(lbl) => write!(f, "j {}", lbl),
|
||||
Instruction::JumpAndLink(lbl) => write!(f, "jal {}", lbl),
|
||||
Instruction::JumpRelative(off) => write!(f, "jr {}", off),
|
||||
Instruction::BranchEq(a, b, lbl) => write!(f, "beq {} {} {}", a, b, lbl),
|
||||
Instruction::BranchNe(a, b, lbl) => write!(f, "bne {} {} {}", a, b, lbl),
|
||||
Instruction::BranchGt(a, b, lbl) => write!(f, "bgt {} {} {}", a, b, lbl),
|
||||
Instruction::BranchLt(a, b, lbl) => write!(f, "blt {} {} {}", a, b, lbl),
|
||||
Instruction::BranchGe(a, b, lbl) => write!(f, "bge {} {} {}", a, b, lbl),
|
||||
Instruction::BranchLe(a, b, lbl) => write!(f, "ble {} {} {}", a, b, lbl),
|
||||
Instruction::BranchEqZero(a, lbl) => write!(f, "beqz {} {}", a, lbl),
|
||||
Instruction::BranchNeZero(a, lbl) => write!(f, "bnez {} {}", a, lbl),
|
||||
Instruction::SetEq(dst, a, b) => write!(f, "seq {} {} {}", dst, a, b),
|
||||
Instruction::SetNe(dst, a, b) => write!(f, "sne {} {} {}", dst, a, b),
|
||||
Instruction::SetGt(dst, a, b) => write!(f, "sgt {} {} {}", dst, a, b),
|
||||
Instruction::SetLt(dst, a, b) => write!(f, "slt {} {} {}", dst, a, b),
|
||||
Instruction::SetGe(dst, a, b) => write!(f, "sge {} {} {}", dst, a, b),
|
||||
Instruction::SetLe(dst, a, b) => write!(f, "sle {} {} {}", dst, a, b),
|
||||
Instruction::And(dst, a, b) => write!(f, "and {} {} {}", dst, a, b),
|
||||
Instruction::Or(dst, a, b) => write!(f, "or {} {} {}", dst, a, b),
|
||||
Instruction::Xor(dst, a, b) => write!(f, "xor {} {} {}", dst, a, b),
|
||||
Instruction::Push(val) => write!(f, "push {}", val),
|
||||
Instruction::Pop(dst) => write!(f, "pop {}", dst),
|
||||
Instruction::Peek(dst) => write!(f, "peek {}", dst),
|
||||
Instruction::Get(dst, dev, val) => write!(f, "get {} {} {}", dst, dev, val),
|
||||
Instruction::Put(dev, addr, val) => write!(f, "put {} {} {}", dev, addr, val),
|
||||
Instruction::Select(dst, cond, a, b) => {
|
||||
write!(f, "select {} {} {} {}", dst, cond, a, b)
|
||||
}
|
||||
Instruction::Yield => write!(f, "yield"),
|
||||
Instruction::Sleep(val) => write!(f, "sleep {}", val),
|
||||
Instruction::Alias(name, target) => write!(f, "alias {} {}", name, target),
|
||||
Instruction::Define(name, val) => write!(f, "define {} {}", name, val),
|
||||
Instruction::LabelDef(lbl) => write!(f, "{}:", lbl),
|
||||
Instruction::Comment(c) => write!(f, "# {}", c),
|
||||
}
|
||||
}
|
||||
}
|
||||
10
rust_compiler/libs/optimizer/Cargo.toml
Normal file
10
rust_compiler/libs/optimizer/Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "optimizer"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
il = { path = "../il" }
|
||||
helpers = { path = "../helpers" }
|
||||
rust_decimal = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
44
rust_compiler/libs/optimizer/src/leaf_function.rs
Normal file
44
rust_compiler/libs/optimizer/src/leaf_function.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use il::{Instruction, InstructionNode};
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Scans the instruction set to identify "leaf functions".
|
||||
/// A leaf function is defined as a function (delimited by LabelDefs) that does not
|
||||
/// contain any `jal` (JumpAndLink) instructions.
|
||||
///
|
||||
/// Returns a Set containing the names of all identified leaf functions.
|
||||
pub fn find_leaf_functions(instructions: &[InstructionNode]) -> HashSet<String> {
|
||||
let mut leaf_functions = HashSet::new();
|
||||
let mut current_label: Option<String> = None;
|
||||
let mut is_current_leaf = true;
|
||||
|
||||
for node in instructions {
|
||||
match &node.instruction {
|
||||
Instruction::LabelDef(label) => {
|
||||
// If we were tracking a function, and it remained a leaf until now, save it.
|
||||
if let Some(name) = current_label.take()
|
||||
&& is_current_leaf
|
||||
{
|
||||
leaf_functions.insert(name);
|
||||
}
|
||||
|
||||
// Start tracking the new function
|
||||
current_label = Some(label.to_string());
|
||||
is_current_leaf = true;
|
||||
}
|
||||
Instruction::JumpAndLink(_) => {
|
||||
// If we see a JAL, this function is NOT a leaf.
|
||||
is_current_leaf = false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the final function in the file
|
||||
if let Some(name) = current_label
|
||||
&& is_current_leaf
|
||||
{
|
||||
leaf_functions.insert(name);
|
||||
}
|
||||
|
||||
leaf_functions
|
||||
}
|
||||
868
rust_compiler/libs/optimizer/src/lib.rs
Normal file
868
rust_compiler/libs/optimizer/src/lib.rs
Normal file
@@ -0,0 +1,868 @@
|
||||
use il::{Instruction, InstructionNode, Instructions, Operand};
|
||||
use rust_decimal::Decimal;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
mod leaf_function;
|
||||
use leaf_function::find_leaf_functions;
|
||||
|
||||
/// Entry point for the optimizer.
|
||||
pub fn optimize<'a>(instructions: Instructions<'a>) -> Instructions<'a> {
|
||||
let mut instructions = instructions.into_inner();
|
||||
let mut changed = true;
|
||||
let mut pass_count = 0;
|
||||
const MAX_PASSES: usize = 10;
|
||||
|
||||
// Iterative passes for code simplification
|
||||
while changed && pass_count < MAX_PASSES {
|
||||
changed = false;
|
||||
pass_count += 1;
|
||||
|
||||
// Pass 1: Constant Propagation
|
||||
let (new_inst, c1) = constant_propagation(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c1;
|
||||
|
||||
// Pass 2: Register Forwarding (Intermediate Move Elimination)
|
||||
let (new_inst, c2) = register_forwarding(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c2;
|
||||
|
||||
// Pass 3: Function Call Optimization (Remove unused push/pop around calls)
|
||||
let (new_inst, c3) = optimize_function_calls(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c3;
|
||||
|
||||
// Pass 4: Leaf Function Optimization (Remove RA save/restore for leaf functions)
|
||||
// This is separate from pass 3 as it deals with the function *definition*, not the call site.
|
||||
let (new_inst, c4) = optimize_leaf_functions(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c4;
|
||||
|
||||
// Pass 5: Redundant Move Elimination
|
||||
let (new_inst, c5) = remove_redundant_moves(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c5;
|
||||
|
||||
// Pass 6: Dead Code Elimination
|
||||
let (new_inst, c6) = remove_unreachable_code(instructions);
|
||||
instructions = new_inst;
|
||||
changed |= c6;
|
||||
}
|
||||
|
||||
// Final Pass: Resolve Labels to Line Numbers
|
||||
Instructions::new(resolve_labels(instructions))
|
||||
}
|
||||
|
||||
/// Helper: Check if a function body contains unsafe stack manipulation.
|
||||
/// Returns true if the function modifies SP in a way that makes static RA offset analysis unsafe.
|
||||
fn function_has_complex_stack_ops(
|
||||
instructions: &[InstructionNode],
|
||||
start_idx: usize,
|
||||
end_idx: usize,
|
||||
) -> bool {
|
||||
for instruction in instructions.iter().take(end_idx).skip(start_idx) {
|
||||
match instruction.instruction {
|
||||
Instruction::Push(_) | Instruction::Pop(_) => return true,
|
||||
// Check for explicit SP modification
|
||||
Instruction::Add(Operand::StackPointer, _, _)
|
||||
| Instruction::Sub(Operand::StackPointer, _, _)
|
||||
| Instruction::Mul(Operand::StackPointer, _, _)
|
||||
| Instruction::Div(Operand::StackPointer, _, _)
|
||||
| Instruction::Move(Operand::StackPointer, _) => return true,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Pass: Leaf Function Optimization
|
||||
/// If a function makes no calls (is a leaf), it doesn't need to save/restore `ra`.
|
||||
fn optimize_leaf_functions<'a>(
|
||||
input: Vec<InstructionNode<'a>>,
|
||||
) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let leaves = find_leaf_functions(&input);
|
||||
if leaves.is_empty() {
|
||||
return (input, false);
|
||||
}
|
||||
|
||||
let mut changed = false;
|
||||
let mut to_remove = HashSet::new();
|
||||
|
||||
// We map function names to the INDEX of the instruction that restores RA.
|
||||
// We use this to validate the function body later.
|
||||
let mut func_restore_indices = HashMap::new();
|
||||
let mut func_ra_offsets = HashMap::new();
|
||||
|
||||
let mut current_function: Option<String> = None;
|
||||
let mut function_start_indices = HashMap::new();
|
||||
|
||||
// First scan: Identify instructions to remove and capture RA offsets
|
||||
for (i, node) in input.iter().enumerate() {
|
||||
match &node.instruction {
|
||||
Instruction::LabelDef(label) => {
|
||||
current_function = Some(label.to_string());
|
||||
function_start_indices.insert(label.to_string(), i);
|
||||
}
|
||||
Instruction::Push(Operand::ReturnAddress) => {
|
||||
if let Some(func) = ¤t_function
|
||||
&& leaves.contains(func)
|
||||
{
|
||||
to_remove.insert(i);
|
||||
}
|
||||
}
|
||||
Instruction::Get(Operand::ReturnAddress, _, Operand::Register(_)) => {
|
||||
// This is the restore instruction: `get ra db r0`
|
||||
if let Some(func) = ¤t_function
|
||||
&& leaves.contains(func)
|
||||
{
|
||||
to_remove.insert(i);
|
||||
func_restore_indices.insert(func.clone(), i);
|
||||
|
||||
// Look back for the address calc: `sub r0 sp OFFSET`
|
||||
if i > 0
|
||||
&& let Instruction::Sub(_, Operand::StackPointer, Operand::Number(n)) =
|
||||
&input[i - 1].instruction
|
||||
{
|
||||
func_ra_offsets.insert(func.clone(), *n);
|
||||
to_remove.insert(i - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Safety Check: Verify that functions marked for optimization don't have complex stack ops.
|
||||
// If they do, unmark them.
|
||||
let mut safe_functions = HashSet::new();
|
||||
|
||||
for (func, start_idx) in &function_start_indices {
|
||||
if let Some(restore_idx) = func_restore_indices.get(func) {
|
||||
// Check instructions between start and restore using the helper function.
|
||||
// We need to skip the `push ra` we just marked for removal, otherwise the helper
|
||||
// will flag it as a complex op (Push).
|
||||
// `start_idx` is the LabelDef. `start_idx + 1` is typically `push ra`.
|
||||
|
||||
let check_start = if to_remove.contains(&(start_idx + 1)) {
|
||||
start_idx + 2
|
||||
} else {
|
||||
start_idx + 1
|
||||
};
|
||||
|
||||
// `restore_idx` points to the `get ra` instruction. The helper scans up to `end_idx` exclusive,
|
||||
// so we don't need to worry about the restore instruction itself.
|
||||
if !function_has_complex_stack_ops(&input, check_start, *restore_idx) {
|
||||
safe_functions.insert(func.clone());
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !changed {
|
||||
return (input, false);
|
||||
}
|
||||
|
||||
// Second scan: Rebuild with adjustments, but only for SAFE functions
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
let mut processing_function: Option<String> = None;
|
||||
|
||||
for (i, mut node) in input.into_iter().enumerate() {
|
||||
if to_remove.contains(&i)
|
||||
&& let Some(func) = &processing_function
|
||||
&& safe_functions.contains(func)
|
||||
{
|
||||
continue; // SKIP (Remove)
|
||||
}
|
||||
|
||||
if let Instruction::LabelDef(l) = &node.instruction {
|
||||
processing_function = Some(l.to_string());
|
||||
}
|
||||
|
||||
// Apply Stack Adjustments
|
||||
if let Some(func) = &processing_function
|
||||
&& safe_functions.contains(func)
|
||||
&& let Some(ra_offset) = func_ra_offsets.get(func)
|
||||
{
|
||||
// 1. Stack Cleanup Adjustment
|
||||
if let Instruction::Sub(
|
||||
Operand::StackPointer,
|
||||
Operand::StackPointer,
|
||||
Operand::Number(n),
|
||||
) = &mut node.instruction
|
||||
{
|
||||
// Decrease cleanup amount by 1 (for the removed RA)
|
||||
let new_n = *n - Decimal::from(1);
|
||||
if new_n.is_zero() {
|
||||
continue;
|
||||
}
|
||||
*n = new_n;
|
||||
}
|
||||
|
||||
// 2. Stack Variable Offset Adjustment
|
||||
// Since we verified the function is "Simple" (no nested stack mods),
|
||||
// we can safely assume offsets > ra_offset need shifting.
|
||||
if let Instruction::Sub(_, Operand::StackPointer, Operand::Number(n)) =
|
||||
&mut node.instruction
|
||||
&& *n > *ra_offset
|
||||
{
|
||||
*n -= Decimal::from(1);
|
||||
}
|
||||
}
|
||||
|
||||
output.push(node);
|
||||
}
|
||||
|
||||
(output, true)
|
||||
}
|
||||
|
||||
/// Analyzes which registers are written to by each function label.
|
||||
fn analyze_clobbers(instructions: &[InstructionNode]) -> HashMap<String, HashSet<u8>> {
|
||||
let mut clobbers = HashMap::new();
|
||||
let mut current_label = None;
|
||||
|
||||
for node in instructions {
|
||||
if let Instruction::LabelDef(label) = &node.instruction {
|
||||
current_label = Some(label.to_string());
|
||||
clobbers.insert(label.to_string(), HashSet::new());
|
||||
}
|
||||
|
||||
if let Some(label) = ¤t_label
|
||||
&& let Some(reg) = get_destination_reg(&node.instruction)
|
||||
&& let Some(set) = clobbers.get_mut(label)
|
||||
{
|
||||
set.insert(reg);
|
||||
}
|
||||
}
|
||||
clobbers
|
||||
}
|
||||
|
||||
/// Pass: Function Call Optimization
|
||||
/// Removes Push/Restore pairs surrounding a JAL if the target function does not clobber that register.
|
||||
fn optimize_function_calls<'a>(
|
||||
input: Vec<InstructionNode<'a>>,
|
||||
) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let clobbers = analyze_clobbers(&input);
|
||||
let mut changed = false;
|
||||
let mut to_remove = HashSet::new();
|
||||
let mut stack_adjustments = HashMap::new();
|
||||
|
||||
let mut i = 0;
|
||||
while i < input.len() {
|
||||
if let Instruction::JumpAndLink(Operand::Label(target)) = &input[i].instruction {
|
||||
let target_key = target.to_string();
|
||||
|
||||
if let Some(func_clobbers) = clobbers.get(&target_key) {
|
||||
// 1. Identify Pushes immediately preceding the JAL
|
||||
let mut pushes = Vec::new(); // (index, register)
|
||||
let mut scan_back = i.saturating_sub(1);
|
||||
while scan_back > 0 {
|
||||
if to_remove.contains(&scan_back) {
|
||||
scan_back -= 1;
|
||||
continue;
|
||||
}
|
||||
if let Instruction::Push(Operand::Register(r)) = &input[scan_back].instruction {
|
||||
pushes.push((scan_back, *r));
|
||||
scan_back -= 1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Identify Restores immediately following the JAL
|
||||
let mut restores = Vec::new(); // (index_of_get, register, index_of_sub)
|
||||
let mut scan_fwd = i + 1;
|
||||
while scan_fwd < input.len() {
|
||||
// Skip 'sub r0 sp X'
|
||||
if let Instruction::Sub(Operand::Register(0), Operand::StackPointer, _) =
|
||||
&input[scan_fwd].instruction
|
||||
{
|
||||
// Check next instruction for the Get
|
||||
if scan_fwd + 1 < input.len()
|
||||
&& let Instruction::Get(Operand::Register(r), _, Operand::Register(0)) =
|
||||
&input[scan_fwd + 1].instruction
|
||||
{
|
||||
restores.push((scan_fwd + 1, *r, scan_fwd));
|
||||
scan_fwd += 2;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 3. Stack Cleanup
|
||||
let cleanup_idx = scan_fwd;
|
||||
let has_cleanup = if cleanup_idx < input.len() {
|
||||
matches!(
|
||||
input[cleanup_idx].instruction,
|
||||
Instruction::Sub(
|
||||
Operand::StackPointer,
|
||||
Operand::StackPointer,
|
||||
Operand::Number(_)
|
||||
)
|
||||
)
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
// SAFEGUARD: Check Counts!
|
||||
// If we pushed r8 twice but only restored it once, we have an argument.
|
||||
// We must ensure the number of pushes for each register MATCHES the number of restores.
|
||||
let mut push_counts = HashMap::new();
|
||||
for (_, r) in &pushes {
|
||||
*push_counts.entry(*r).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let mut restore_counts = HashMap::new();
|
||||
for (_, r, _) in &restores {
|
||||
*restore_counts.entry(*r).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
let counts_match = push_counts
|
||||
.iter()
|
||||
.all(|(reg, count)| restore_counts.get(reg).unwrap_or(&0) == count);
|
||||
// Also check reverse to ensure we didn't restore something we didn't push (unlikely but possible)
|
||||
let counts_match_reverse = restore_counts
|
||||
.iter()
|
||||
.all(|(reg, count)| push_counts.get(reg).unwrap_or(&0) == count);
|
||||
|
||||
// Clobber Check
|
||||
let all_pushes_safe = pushes.iter().all(|(_, r)| !func_clobbers.contains(r));
|
||||
|
||||
if all_pushes_safe && has_cleanup && counts_match && counts_match_reverse {
|
||||
// We can remove ALL found pushes/restores safely
|
||||
for (p_idx, _) in pushes {
|
||||
to_remove.insert(p_idx);
|
||||
}
|
||||
for (g_idx, _, s_idx) in restores {
|
||||
to_remove.insert(g_idx);
|
||||
to_remove.insert(s_idx);
|
||||
}
|
||||
|
||||
// Reduce stack cleanup amount
|
||||
let num_removed = push_counts.values().sum::<i32>() as i64;
|
||||
stack_adjustments.insert(cleanup_idx, num_removed);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if changed {
|
||||
let mut clean = Vec::with_capacity(input.len());
|
||||
for (idx, mut node) in input.into_iter().enumerate() {
|
||||
if to_remove.contains(&idx) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Apply stack adjustment
|
||||
if let Some(reduction) = stack_adjustments.get(&idx)
|
||||
&& let Instruction::Sub(dst, a, Operand::Number(n)) = &node.instruction
|
||||
{
|
||||
let new_n = n - Decimal::from(*reduction);
|
||||
if new_n.is_zero() {
|
||||
continue; // Remove the sub entirely if 0
|
||||
}
|
||||
node.instruction = Instruction::Sub(dst.clone(), a.clone(), Operand::Number(new_n));
|
||||
}
|
||||
|
||||
clean.push(node);
|
||||
}
|
||||
return (clean, changed);
|
||||
}
|
||||
|
||||
(input, false)
|
||||
}
|
||||
|
||||
/// Pass: Register Forwarding
|
||||
/// Eliminates intermediate moves by writing directly to the final destination.
|
||||
/// Example: `l r1 d0 T` + `move r9 r1` -> `l r9 d0 T`
|
||||
fn register_forwarding<'a>(
|
||||
mut input: Vec<InstructionNode<'a>>,
|
||||
) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let mut changed = false;
|
||||
let mut i = 0;
|
||||
|
||||
// We use a while loop to manually control index so we can peek ahead
|
||||
while i < input.len().saturating_sub(1) {
|
||||
let next_idx = i + 1;
|
||||
|
||||
// Check if current instruction defines a register
|
||||
// and the NEXT instruction is a move from that register.
|
||||
let forward_candidate = if let Some(def_reg) = get_destination_reg(&input[i].instruction) {
|
||||
if let Instruction::Move(Operand::Register(dest_reg), Operand::Register(src_reg)) =
|
||||
&input[next_idx].instruction
|
||||
{
|
||||
if *src_reg == def_reg {
|
||||
// Candidate found: Instruction `i` defines `src_reg`, Instruction `i+1` moves `src_reg` to `dest_reg`.
|
||||
// We can optimize if `src_reg` (the temp) is NOT used after this move.
|
||||
Some((def_reg, *dest_reg))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some((temp_reg, final_reg)) = forward_candidate {
|
||||
// Check liveness: Is temp_reg used after i+1?
|
||||
// We scan from i+2 onwards.
|
||||
let mut temp_is_dead = true;
|
||||
for node in input.iter().skip(i + 2) {
|
||||
if reg_is_read(&node.instruction, temp_reg) {
|
||||
temp_is_dead = false;
|
||||
break;
|
||||
}
|
||||
// If the temp is redefined, then the old value is dead, so we are safe.
|
||||
if let Some(redef) = get_destination_reg(&node.instruction)
|
||||
&& redef == temp_reg
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// If we hit a label/jump, we assume liveness might leak (conservative safety)
|
||||
if matches!(
|
||||
node.instruction,
|
||||
Instruction::LabelDef(_) | Instruction::Jump(_) | Instruction::JumpAndLink(_)
|
||||
) {
|
||||
temp_is_dead = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if temp_is_dead {
|
||||
// Perform the swap
|
||||
// 1. Rewrite input[i] to write to final_reg
|
||||
if let Some(new_instr) = set_destination_reg(&input[i].instruction, final_reg) {
|
||||
input[i].instruction = new_instr;
|
||||
// 2. Remove input[i+1] (The Move)
|
||||
input.remove(next_idx);
|
||||
changed = true;
|
||||
// Don't increment i, re-evaluate current index (which is now a new neighbor)
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
(input, changed)
|
||||
}
|
||||
|
||||
/// Pass: Resolve Labels
|
||||
/// Converts all Jump/Branch labels to absolute line numbers and removes LabelDefs.
|
||||
fn resolve_labels<'a>(input: Vec<InstructionNode<'a>>) -> Vec<InstructionNode<'a>> {
|
||||
let mut label_map: HashMap<String, usize> = HashMap::new();
|
||||
let mut line_number = 0;
|
||||
|
||||
// 1. Build Label Map (filtering out LabelDefs from the count)
|
||||
for node in &input {
|
||||
if let Instruction::LabelDef(name) = &node.instruction {
|
||||
label_map.insert(name.to_string(), line_number);
|
||||
} else {
|
||||
line_number += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
|
||||
// 2. Rewrite Jumps and Filter Labels
|
||||
for mut node in input {
|
||||
// Helper to get line number as Decimal operand
|
||||
let get_line = |lbl: &Operand| -> Option<Operand<'a>> {
|
||||
if let Operand::Label(name) = lbl {
|
||||
label_map
|
||||
.get(name.as_ref())
|
||||
.map(|&l| Operand::Number(Decimal::from(l)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
match &mut node.instruction {
|
||||
Instruction::LabelDef(_) => continue, // Strip labels
|
||||
|
||||
// Jumps
|
||||
Instruction::Jump(op) => {
|
||||
if let Some(num) = get_line(op) {
|
||||
*op = num;
|
||||
}
|
||||
}
|
||||
Instruction::JumpAndLink(op) => {
|
||||
if let Some(num) = get_line(op) {
|
||||
*op = num;
|
||||
}
|
||||
}
|
||||
Instruction::BranchEq(_, _, op)
|
||||
| Instruction::BranchNe(_, _, op)
|
||||
| Instruction::BranchGt(_, _, op)
|
||||
| Instruction::BranchLt(_, _, op)
|
||||
| Instruction::BranchGe(_, _, op)
|
||||
| Instruction::BranchLe(_, _, op) => {
|
||||
if let Some(num) = get_line(op) {
|
||||
*op = num;
|
||||
}
|
||||
}
|
||||
Instruction::BranchEqZero(_, op) | Instruction::BranchNeZero(_, op) => {
|
||||
if let Some(num) = get_line(op) {
|
||||
*op = num;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
output.push(node);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
// --- Helpers for Register Analysis ---
|
||||
|
||||
fn get_destination_reg(instr: &Instruction) -> Option<u8> {
|
||||
match instr {
|
||||
Instruction::Move(Operand::Register(r), _)
|
||||
| Instruction::Add(Operand::Register(r), _, _)
|
||||
| Instruction::Sub(Operand::Register(r), _, _)
|
||||
| Instruction::Mul(Operand::Register(r), _, _)
|
||||
| Instruction::Div(Operand::Register(r), _, _)
|
||||
| Instruction::Mod(Operand::Register(r), _, _)
|
||||
| Instruction::Pow(Operand::Register(r), _, _)
|
||||
| Instruction::Load(Operand::Register(r), _, _)
|
||||
| Instruction::LoadSlot(Operand::Register(r), _, _, _)
|
||||
| Instruction::LoadBatch(Operand::Register(r), _, _, _)
|
||||
| Instruction::LoadBatchNamed(Operand::Register(r), _, _, _, _)
|
||||
| Instruction::SetEq(Operand::Register(r), _, _)
|
||||
| Instruction::SetNe(Operand::Register(r), _, _)
|
||||
| Instruction::SetGt(Operand::Register(r), _, _)
|
||||
| Instruction::SetLt(Operand::Register(r), _, _)
|
||||
| Instruction::SetGe(Operand::Register(r), _, _)
|
||||
| Instruction::SetLe(Operand::Register(r), _, _)
|
||||
| Instruction::And(Operand::Register(r), _, _)
|
||||
| Instruction::Or(Operand::Register(r), _, _)
|
||||
| Instruction::Xor(Operand::Register(r), _, _)
|
||||
| Instruction::Peek(Operand::Register(r))
|
||||
| Instruction::Get(Operand::Register(r), _, _)
|
||||
| Instruction::Select(Operand::Register(r), _, _, _)
|
||||
| Instruction::Rand(Operand::Register(r))
|
||||
| Instruction::Acos(Operand::Register(r), _)
|
||||
| Instruction::Asin(Operand::Register(r), _)
|
||||
| Instruction::Atan(Operand::Register(r), _)
|
||||
| Instruction::Atan2(Operand::Register(r), _, _)
|
||||
| Instruction::Abs(Operand::Register(r), _)
|
||||
| Instruction::Ceil(Operand::Register(r), _)
|
||||
| Instruction::Cos(Operand::Register(r), _)
|
||||
| Instruction::Floor(Operand::Register(r), _)
|
||||
| Instruction::Log(Operand::Register(r), _)
|
||||
| Instruction::Max(Operand::Register(r), _, _)
|
||||
| Instruction::Min(Operand::Register(r), _, _)
|
||||
| Instruction::Sin(Operand::Register(r), _)
|
||||
| Instruction::Sqrt(Operand::Register(r), _)
|
||||
| Instruction::Tan(Operand::Register(r), _)
|
||||
| Instruction::Trunc(Operand::Register(r), _)
|
||||
| Instruction::Pop(Operand::Register(r)) => Some(*r),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn set_destination_reg<'a>(instr: &Instruction<'a>, new_reg: u8) -> Option<Instruction<'a>> {
|
||||
// Helper to easily recreate instruction with new dest
|
||||
let r = Operand::Register(new_reg);
|
||||
match instr {
|
||||
Instruction::Move(_, b) => Some(Instruction::Move(r, b.clone())),
|
||||
Instruction::Add(_, a, b) => Some(Instruction::Add(r, a.clone(), b.clone())),
|
||||
Instruction::Sub(_, a, b) => Some(Instruction::Sub(r, a.clone(), b.clone())),
|
||||
Instruction::Mul(_, a, b) => Some(Instruction::Mul(r, a.clone(), b.clone())),
|
||||
Instruction::Div(_, a, b) => Some(Instruction::Div(r, a.clone(), b.clone())),
|
||||
Instruction::Mod(_, a, b) => Some(Instruction::Mod(r, a.clone(), b.clone())),
|
||||
Instruction::Pow(_, a, b) => Some(Instruction::Pow(r, a.clone(), b.clone())),
|
||||
Instruction::Load(_, a, b) => Some(Instruction::Load(r, a.clone(), b.clone())),
|
||||
Instruction::LoadSlot(_, a, b, c) => {
|
||||
Some(Instruction::LoadSlot(r, a.clone(), b.clone(), c.clone()))
|
||||
}
|
||||
Instruction::LoadBatch(_, a, b, c) => {
|
||||
Some(Instruction::LoadBatch(r, a.clone(), b.clone(), c.clone()))
|
||||
}
|
||||
Instruction::LoadBatchNamed(_, a, b, c, d) => Some(Instruction::LoadBatchNamed(
|
||||
r,
|
||||
a.clone(),
|
||||
b.clone(),
|
||||
c.clone(),
|
||||
d.clone(),
|
||||
)),
|
||||
Instruction::SetEq(_, a, b) => Some(Instruction::SetEq(r, a.clone(), b.clone())),
|
||||
Instruction::SetNe(_, a, b) => Some(Instruction::SetNe(r, a.clone(), b.clone())),
|
||||
Instruction::SetGt(_, a, b) => Some(Instruction::SetGt(r, a.clone(), b.clone())),
|
||||
Instruction::SetLt(_, a, b) => Some(Instruction::SetLt(r, a.clone(), b.clone())),
|
||||
Instruction::SetGe(_, a, b) => Some(Instruction::SetGe(r, a.clone(), b.clone())),
|
||||
Instruction::SetLe(_, a, b) => Some(Instruction::SetLe(r, a.clone(), b.clone())),
|
||||
Instruction::And(_, a, b) => Some(Instruction::And(r, a.clone(), b.clone())),
|
||||
Instruction::Or(_, a, b) => Some(Instruction::Or(r, a.clone(), b.clone())),
|
||||
Instruction::Xor(_, a, b) => Some(Instruction::Xor(r, a.clone(), b.clone())),
|
||||
Instruction::Peek(_) => Some(Instruction::Peek(r)),
|
||||
Instruction::Get(_, a, b) => Some(Instruction::Get(r, a.clone(), b.clone())),
|
||||
Instruction::Select(_, a, b, c) => {
|
||||
Some(Instruction::Select(r, a.clone(), b.clone(), c.clone()))
|
||||
}
|
||||
Instruction::Rand(_) => Some(Instruction::Rand(r)),
|
||||
Instruction::Pop(_) => Some(Instruction::Pop(r)),
|
||||
|
||||
// Math funcs
|
||||
Instruction::Acos(_, a) => Some(Instruction::Acos(r, a.clone())),
|
||||
Instruction::Asin(_, a) => Some(Instruction::Asin(r, a.clone())),
|
||||
Instruction::Atan(_, a) => Some(Instruction::Atan(r, a.clone())),
|
||||
Instruction::Atan2(_, a, b) => Some(Instruction::Atan2(r, a.clone(), b.clone())),
|
||||
Instruction::Abs(_, a) => Some(Instruction::Abs(r, a.clone())),
|
||||
Instruction::Ceil(_, a) => Some(Instruction::Ceil(r, a.clone())),
|
||||
Instruction::Cos(_, a) => Some(Instruction::Cos(r, a.clone())),
|
||||
Instruction::Floor(_, a) => Some(Instruction::Floor(r, a.clone())),
|
||||
Instruction::Log(_, a) => Some(Instruction::Log(r, a.clone())),
|
||||
Instruction::Max(_, a, b) => Some(Instruction::Max(r, a.clone(), b.clone())),
|
||||
Instruction::Min(_, a, b) => Some(Instruction::Min(r, a.clone(), b.clone())),
|
||||
Instruction::Sin(_, a) => Some(Instruction::Sin(r, a.clone())),
|
||||
Instruction::Sqrt(_, a) => Some(Instruction::Sqrt(r, a.clone())),
|
||||
Instruction::Tan(_, a) => Some(Instruction::Tan(r, a.clone())),
|
||||
Instruction::Trunc(_, a) => Some(Instruction::Trunc(r, a.clone())),
|
||||
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn reg_is_read(instr: &Instruction, reg: u8) -> bool {
|
||||
let check = |op: &Operand| matches!(op, Operand::Register(r) if *r == reg);
|
||||
|
||||
match instr {
|
||||
Instruction::Move(_, a) => check(a),
|
||||
Instruction::Add(_, a, b)
|
||||
| Instruction::Sub(_, a, b)
|
||||
| Instruction::Mul(_, a, b)
|
||||
| Instruction::Div(_, a, b)
|
||||
| Instruction::Mod(_, a, b)
|
||||
| Instruction::Pow(_, a, b) => check(a) || check(b),
|
||||
|
||||
Instruction::Load(_, a, _) => check(a), // Load reads device? Device can be reg? Yes.
|
||||
Instruction::Store(a, _, b) => check(a) || check(b),
|
||||
|
||||
Instruction::BranchEq(a, b, _)
|
||||
| Instruction::BranchNe(a, b, _)
|
||||
| Instruction::BranchGt(a, b, _)
|
||||
| Instruction::BranchLt(a, b, _)
|
||||
| Instruction::BranchGe(a, b, _)
|
||||
| Instruction::BranchLe(a, b, _) => check(a) || check(b),
|
||||
|
||||
Instruction::BranchEqZero(a, _) | Instruction::BranchNeZero(a, _) => check(a),
|
||||
|
||||
Instruction::SetEq(_, a, b)
|
||||
| Instruction::SetNe(_, a, b)
|
||||
| Instruction::SetGt(_, a, b)
|
||||
| Instruction::SetLt(_, a, b)
|
||||
| Instruction::SetGe(_, a, b)
|
||||
| Instruction::SetLe(_, a, b)
|
||||
| Instruction::And(_, a, b)
|
||||
| Instruction::Or(_, a, b)
|
||||
| Instruction::Xor(_, a, b) => check(a) || check(b),
|
||||
|
||||
Instruction::Push(a) => check(a),
|
||||
Instruction::Get(_, a, b) => check(a) || check(b),
|
||||
Instruction::Put(a, b, c) => check(a) || check(b) || check(c),
|
||||
|
||||
Instruction::Select(_, a, b, c) => check(a) || check(b) || check(c),
|
||||
Instruction::Sleep(a) => check(a),
|
||||
|
||||
// Math single arg
|
||||
Instruction::Acos(_, a)
|
||||
| Instruction::Asin(_, a)
|
||||
| Instruction::Atan(_, a)
|
||||
| Instruction::Abs(_, a)
|
||||
| Instruction::Ceil(_, a)
|
||||
| Instruction::Cos(_, a)
|
||||
| Instruction::Floor(_, a)
|
||||
| Instruction::Log(_, a)
|
||||
| Instruction::Sin(_, a)
|
||||
| Instruction::Sqrt(_, a)
|
||||
| Instruction::Tan(_, a)
|
||||
| Instruction::Trunc(_, a) => check(a),
|
||||
|
||||
// Math double arg
|
||||
Instruction::Atan2(_, a, b) | Instruction::Max(_, a, b) | Instruction::Min(_, a, b) => {
|
||||
check(a) || check(b)
|
||||
}
|
||||
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// --- Constant Propagation & Dead Code ---
|
||||
fn constant_propagation<'a>(input: Vec<InstructionNode<'a>>) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
let mut changed = false;
|
||||
let mut registers: [Option<Decimal>; 16] = [None; 16];
|
||||
|
||||
for mut node in input {
|
||||
match &node.instruction {
|
||||
Instruction::LabelDef(_) | Instruction::JumpAndLink(_) => registers = [None; 16],
|
||||
_ => {}
|
||||
}
|
||||
|
||||
let simplified = match &node.instruction {
|
||||
Instruction::Move(dst, src) => resolve_value(src, ®isters)
|
||||
.map(|val| Instruction::Move(dst.clone(), Operand::Number(val))),
|
||||
Instruction::Add(dst, a, b) => try_fold_math(dst, a, b, ®isters, |x, y| x + y),
|
||||
Instruction::Sub(dst, a, b) => try_fold_math(dst, a, b, ®isters, |x, y| x - y),
|
||||
Instruction::Mul(dst, a, b) => try_fold_math(dst, a, b, ®isters, |x, y| x * y),
|
||||
Instruction::Div(dst, a, b) => {
|
||||
try_fold_math(
|
||||
dst,
|
||||
a,
|
||||
b,
|
||||
®isters,
|
||||
|x, y| if y.is_zero() { x } else { x / y },
|
||||
)
|
||||
}
|
||||
Instruction::Mod(dst, a, b) => {
|
||||
try_fold_math(
|
||||
dst,
|
||||
a,
|
||||
b,
|
||||
®isters,
|
||||
|x, y| if y.is_zero() { x } else { x % y },
|
||||
)
|
||||
}
|
||||
Instruction::BranchEq(a, b, l) => {
|
||||
try_resolve_branch(a, b, l, ®isters, |x, y| x == y)
|
||||
}
|
||||
Instruction::BranchNe(a, b, l) => {
|
||||
try_resolve_branch(a, b, l, ®isters, |x, y| x != y)
|
||||
}
|
||||
Instruction::BranchGt(a, b, l) => try_resolve_branch(a, b, l, ®isters, |x, y| x > y),
|
||||
Instruction::BranchLt(a, b, l) => try_resolve_branch(a, b, l, ®isters, |x, y| x < y),
|
||||
Instruction::BranchGe(a, b, l) => {
|
||||
try_resolve_branch(a, b, l, ®isters, |x, y| x >= y)
|
||||
}
|
||||
Instruction::BranchLe(a, b, l) => {
|
||||
try_resolve_branch(a, b, l, ®isters, |x, y| x <= y)
|
||||
}
|
||||
Instruction::BranchEqZero(a, l) => {
|
||||
try_resolve_branch(a, &Operand::Number(0.into()), l, ®isters, |x, y| x == y)
|
||||
}
|
||||
Instruction::BranchNeZero(a, l) => {
|
||||
try_resolve_branch(a, &Operand::Number(0.into()), l, ®isters, |x, y| x != y)
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(new) = simplified {
|
||||
node.instruction = new;
|
||||
changed = true;
|
||||
}
|
||||
|
||||
// Update tracking
|
||||
match &node.instruction {
|
||||
Instruction::Move(Operand::Register(r), src) => {
|
||||
registers[*r as usize] = resolve_value(src, ®isters)
|
||||
}
|
||||
// Invalidate if destination is register
|
||||
_ => {
|
||||
if let Some(r) = get_destination_reg(&node.instruction) {
|
||||
registers[r as usize] = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Filter out NOPs (Empty LabelDefs from branch resolution)
|
||||
if let Instruction::LabelDef(l) = &node.instruction
|
||||
&& l.is_empty()
|
||||
{
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
output.push(node);
|
||||
}
|
||||
(output, changed)
|
||||
}
|
||||
|
||||
fn resolve_value(op: &Operand, regs: &[Option<Decimal>; 16]) -> Option<Decimal> {
|
||||
match op {
|
||||
Operand::Number(n) => Some(*n),
|
||||
Operand::Register(r) => regs[*r as usize],
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn try_fold_math<'a, F>(
|
||||
dst: &Operand<'a>,
|
||||
a: &Operand<'a>,
|
||||
b: &Operand<'a>,
|
||||
regs: &[Option<Decimal>; 16],
|
||||
op: F,
|
||||
) -> Option<Instruction<'a>>
|
||||
where
|
||||
F: Fn(Decimal, Decimal) -> Decimal,
|
||||
{
|
||||
let val_a = resolve_value(a, regs)?;
|
||||
let val_b = resolve_value(b, regs)?;
|
||||
Some(Instruction::Move(
|
||||
dst.clone(),
|
||||
Operand::Number(op(val_a, val_b)),
|
||||
))
|
||||
}
|
||||
|
||||
fn try_resolve_branch<'a, F>(
|
||||
a: &Operand<'a>,
|
||||
b: &Operand<'a>,
|
||||
label: &Operand<'a>,
|
||||
regs: &[Option<Decimal>; 16],
|
||||
check: F,
|
||||
) -> Option<Instruction<'a>>
|
||||
where
|
||||
F: Fn(Decimal, Decimal) -> bool,
|
||||
{
|
||||
let val_a = resolve_value(a, regs)?;
|
||||
let val_b = resolve_value(b, regs)?;
|
||||
if check(val_a, val_b) {
|
||||
Some(Instruction::Jump(label.clone()))
|
||||
} else {
|
||||
Some(Instruction::LabelDef("".into())) // NOP
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_redundant_moves<'a>(input: Vec<InstructionNode<'a>>) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
let mut changed = false;
|
||||
for node in input {
|
||||
if let Instruction::Move(dst, src) = &node.instruction
|
||||
&& dst == src
|
||||
{
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
output.push(node);
|
||||
}
|
||||
(output, changed)
|
||||
}
|
||||
|
||||
fn remove_unreachable_code<'a>(
|
||||
input: Vec<InstructionNode<'a>>,
|
||||
) -> (Vec<InstructionNode<'a>>, bool) {
|
||||
let mut output = Vec::with_capacity(input.len());
|
||||
let mut changed = false;
|
||||
let mut dead = false;
|
||||
for node in input {
|
||||
if let Instruction::LabelDef(_) = node.instruction {
|
||||
dead = false;
|
||||
}
|
||||
if dead {
|
||||
changed = true;
|
||||
continue;
|
||||
}
|
||||
if let Instruction::Jump(_) = node.instruction {
|
||||
dead = true
|
||||
}
|
||||
output.push(node);
|
||||
}
|
||||
(output, changed)
|
||||
}
|
||||
@@ -7,6 +7,7 @@ edition = "2024"
|
||||
tokenizer = { path = "../tokenizer" }
|
||||
helpers = { path = "../helpers" }
|
||||
lsp-types = { workspace = true }
|
||||
safer-ffi = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ mod test;
|
||||
pub mod tree_node;
|
||||
|
||||
use crate::sys_call::{Math, System};
|
||||
use helpers::Span;
|
||||
use std::{borrow::Cow, io::SeekFrom};
|
||||
use sys_call::SysCall;
|
||||
use thiserror::Error;
|
||||
@@ -293,12 +294,12 @@ impl<'a> Parser<'a> {
|
||||
// Handle Infix operators (Binary, Logical, Assignment)
|
||||
if self_matches_peek!(
|
||||
self,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||
) {
|
||||
return Ok(Some(self.infix(lhs)?));
|
||||
} else if self_matches_current!(
|
||||
self,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question)
|
||||
) {
|
||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||
return Ok(Some(self.infix(lhs)?));
|
||||
@@ -645,6 +646,15 @@ impl<'a> Parser<'a> {
|
||||
.node
|
||||
.ok_or(Error::UnexpectedEOF)?,
|
||||
|
||||
TokenType::Identifier(ref id) if SysCall::is_syscall(id) => {
|
||||
let spanned_call = self.spanned(|p| p.syscall())?;
|
||||
|
||||
Spanned {
|
||||
span: spanned_call.span,
|
||||
node: Expression::Syscall(spanned_call),
|
||||
}
|
||||
}
|
||||
|
||||
TokenType::Identifier(_)
|
||||
if self_matches_peek!(self, TokenType::Symbol(Symbol::LParen)) =>
|
||||
{
|
||||
@@ -757,9 +767,11 @@ impl<'a> Parser<'a> {
|
||||
Expression::Binary(_)
|
||||
| Expression::Logical(_)
|
||||
| Expression::Invocation(_)
|
||||
| Expression::Syscall(_)
|
||||
| Expression::Priority(_)
|
||||
| Expression::Literal(_)
|
||||
| Expression::Variable(_)
|
||||
| Expression::Ternary(_)
|
||||
| Expression::Negation(_)
|
||||
| Expression::MemberAccess(_)
|
||||
| Expression::MethodCall(_) => {}
|
||||
@@ -779,7 +791,7 @@ impl<'a> Parser<'a> {
|
||||
// Include Assign in the operator loop
|
||||
while token_matches!(
|
||||
temp_token,
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign)
|
||||
TokenType::Symbol(s) if s.is_operator() || s.is_comparison() || s.is_logical() || matches!(s, Symbol::Assign | Symbol::Question | Symbol::Colon)
|
||||
) {
|
||||
let operator = match temp_token.token_type {
|
||||
TokenType::Symbol(s) => s,
|
||||
@@ -1010,7 +1022,52 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
operators.retain(|symbol| !matches!(symbol, Symbol::LogicalOr));
|
||||
|
||||
// --- PRECEDENCE LEVEL 8: Assignment (=) ---
|
||||
// -- PRECEDENCE LEVEL 8: Ternary (x ? 1 : 2)
|
||||
for i in (0..operators.len()).rev() {
|
||||
if matches!(operators[i], Symbol::Question) {
|
||||
// Ensure next operator is a colon
|
||||
if i + 1 >= operators.len() || !matches!(operators[i + 1], Symbol::Colon) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
self.current_span(),
|
||||
"Ternary operator '?' missing matching ':'".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
let false_branch = expressions.remove(i + 2);
|
||||
let true_branch = expressions.remove(i + 1);
|
||||
let condition = expressions.remove(i);
|
||||
|
||||
let span = Span {
|
||||
start_line: condition.span.start_line,
|
||||
end_line: false_branch.span.end_line,
|
||||
start_col: condition.span.start_col,
|
||||
end_col: false_branch.span.end_col,
|
||||
};
|
||||
|
||||
let ternary_node = Spanned {
|
||||
span,
|
||||
node: TernaryExpression {
|
||||
condition: Box::new(condition),
|
||||
true_value: Box::new(true_branch),
|
||||
false_value: Box::new(false_branch),
|
||||
},
|
||||
};
|
||||
|
||||
expressions.insert(
|
||||
i,
|
||||
Spanned {
|
||||
node: Expression::Ternary(ternary_node),
|
||||
span,
|
||||
},
|
||||
);
|
||||
|
||||
// Remove the `?` and the `:` from the operators list
|
||||
operators.remove(i);
|
||||
operators.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
// --- PRECEDENCE LEVEL 9: Assignment (=) ---
|
||||
// Assignment is Right Associative: a = b = c => a = (b = c)
|
||||
// We iterate Right to Left
|
||||
for (i, operator) in operators.iter().enumerate().rev() {
|
||||
@@ -1050,7 +1107,9 @@ impl<'a> Parser<'a> {
|
||||
|
||||
if token_matches!(
|
||||
temp_token,
|
||||
TokenType::Symbol(Symbol::Semicolon) | TokenType::Symbol(Symbol::RParen)
|
||||
TokenType::Symbol(Symbol::Semicolon)
|
||||
| TokenType::Symbol(Symbol::RParen)
|
||||
| TokenType::Symbol(Symbol::Comma)
|
||||
) {
|
||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||
}
|
||||
@@ -1168,18 +1227,34 @@ impl<'a> Parser<'a> {
|
||||
// Need to capture return span
|
||||
let ret_start_span = Self::token_to_span(¤t_token);
|
||||
self.assign_next()?;
|
||||
let expression = self.expression()?.ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
let expr = if token_matches!(
|
||||
self.current_token.as_ref().ok_or(Error::UnexpectedEOF)?,
|
||||
TokenType::Symbol(Symbol::Semicolon)
|
||||
) {
|
||||
// rewind 1 token so we can check for the semicolon at the bottom of this function.
|
||||
self.tokenizer.seek(SeekFrom::Current(-1))?;
|
||||
None
|
||||
} else {
|
||||
Some(self.expression()?.ok_or(Error::UnexpectedEOF)?)
|
||||
};
|
||||
|
||||
let ret_span = Span {
|
||||
start_line: ret_start_span.start_line,
|
||||
start_col: ret_start_span.start_col,
|
||||
end_line: expression.span.end_line,
|
||||
end_col: expression.span.end_col,
|
||||
end_line: expr
|
||||
.as_ref()
|
||||
.map(|e| e.span.end_line)
|
||||
.unwrap_or(ret_start_span.end_line),
|
||||
end_col: expr
|
||||
.as_ref()
|
||||
.map(|e| e.span.end_col)
|
||||
.unwrap_or(ret_start_span.end_col),
|
||||
};
|
||||
|
||||
let return_expr = Spanned {
|
||||
span: ret_span,
|
||||
node: Expression::Return(boxed!(expression)),
|
||||
node: Expression::Return(expr.map(Box::new)),
|
||||
};
|
||||
expressions.push(return_expr);
|
||||
|
||||
@@ -1518,18 +1593,23 @@ impl<'a> Parser<'a> {
|
||||
}
|
||||
|
||||
fn syscall(&mut self) -> Result<SysCall<'a>, Error<'a>> {
|
||||
fn check_length<'a>(
|
||||
span: Span,
|
||||
arguments: &[Spanned<Expression<'a>>],
|
||||
length: usize,
|
||||
) -> Result<(), Error<'a>> {
|
||||
if arguments.len() != length {
|
||||
let invocation = self.invocation()?;
|
||||
|
||||
let check_length = |len: usize| -> Result<(), Error> {
|
||||
if invocation.arguments.len() != len {
|
||||
return Err(Error::InvalidSyntax(
|
||||
span,
|
||||
format!("Expected {} arguments", length),
|
||||
self.current_span(),
|
||||
format!("Expected {} arguments", len),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
|
||||
macro_rules! args {
|
||||
($count:expr) => {{
|
||||
check_length($count)?;
|
||||
invocation.arguments.into_iter()
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! literal_or_variable {
|
||||
@@ -1581,23 +1661,19 @@ impl<'a> Parser<'a> {
|
||||
};
|
||||
}
|
||||
|
||||
let invocation = self.invocation()?;
|
||||
|
||||
match invocation.name.node.as_ref() {
|
||||
// System SysCalls
|
||||
"yield" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 0)?;
|
||||
check_length(0)?;
|
||||
Ok(SysCall::System(sys_call::System::Yield))
|
||||
}
|
||||
"sleep" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
let mut arg = invocation.arguments.into_iter();
|
||||
let expr = arg.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let mut args = args!(1);
|
||||
let expr = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
Ok(SysCall::System(System::Sleep(boxed!(expr))))
|
||||
}
|
||||
"hash" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(1);
|
||||
let lit_str = literal_or_variable!(args.next());
|
||||
|
||||
let Spanned {
|
||||
@@ -1617,8 +1693,7 @@ impl<'a> Parser<'a> {
|
||||
})))
|
||||
}
|
||||
"load" | "l" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(2);
|
||||
|
||||
let tmp = args.next();
|
||||
let device = literal_or_variable!(tmp);
|
||||
@@ -1662,8 +1737,7 @@ impl<'a> Parser<'a> {
|
||||
)))
|
||||
}
|
||||
"loadBatched" | "lb" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 3)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(3);
|
||||
let tmp = args.next();
|
||||
let device_hash = literal_or_variable!(tmp);
|
||||
|
||||
@@ -1680,8 +1754,7 @@ impl<'a> Parser<'a> {
|
||||
)))
|
||||
}
|
||||
"loadBatchedNamed" | "lbn" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 4)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(4);
|
||||
let tmp = args.next();
|
||||
let dev_hash = literal_or_variable!(tmp);
|
||||
|
||||
@@ -1699,8 +1772,7 @@ impl<'a> Parser<'a> {
|
||||
)))
|
||||
}
|
||||
"set" | "s" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 3)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(3);
|
||||
let tmp = args.next();
|
||||
let device = literal_or_variable!(tmp);
|
||||
|
||||
@@ -1720,8 +1792,7 @@ impl<'a> Parser<'a> {
|
||||
)))
|
||||
}
|
||||
"setBatched" | "sb" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 3)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(3);
|
||||
let tmp = args.next();
|
||||
let device_hash = literal_or_variable!(tmp);
|
||||
|
||||
@@ -1739,8 +1810,7 @@ impl<'a> Parser<'a> {
|
||||
)))
|
||||
}
|
||||
"setBatchedNamed" | "sbn" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 4)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let mut args = args!(4);
|
||||
let tmp = args.next();
|
||||
let device_hash = literal_or_variable!(tmp);
|
||||
|
||||
@@ -1760,30 +1830,110 @@ impl<'a> Parser<'a> {
|
||||
expr,
|
||||
)))
|
||||
}
|
||||
"loadSlot" | "ls" => {
|
||||
let mut args = args!(3);
|
||||
let next = args.next();
|
||||
let dev_name = literal_or_variable!(next);
|
||||
let next = args.next();
|
||||
let slot_index = get_arg!(Literal, literal_or_variable!(next));
|
||||
if !matches!(
|
||||
slot_index,
|
||||
Spanned {
|
||||
node: Literal::Number(_),
|
||||
..
|
||||
},
|
||||
) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
slot_index.span,
|
||||
"Expected a number".to_string(),
|
||||
));
|
||||
}
|
||||
let next = args.next();
|
||||
let slot_logic = get_arg!(Literal, literal_or_variable!(next));
|
||||
if !matches!(
|
||||
slot_logic,
|
||||
Spanned {
|
||||
node: Literal::String(_),
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
slot_logic.span,
|
||||
"Expected a String".into(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(SysCall::System(System::LoadSlot(
|
||||
dev_name, slot_index, slot_logic,
|
||||
)))
|
||||
}
|
||||
"setSlot" | "ss" => {
|
||||
let mut args = args!(4);
|
||||
let next = args.next();
|
||||
let dev_name = literal_or_variable!(next);
|
||||
let next = args.next();
|
||||
let slot_index = get_arg!(Literal, literal_or_variable!(next));
|
||||
if !matches!(
|
||||
slot_index,
|
||||
Spanned {
|
||||
node: Literal::Number(_),
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
slot_index.span,
|
||||
"Expected a number".into(),
|
||||
));
|
||||
}
|
||||
let next = args.next();
|
||||
let slot_logic = get_arg!(Literal, literal_or_variable!(next));
|
||||
if !matches!(
|
||||
slot_logic,
|
||||
Spanned {
|
||||
node: Literal::String(_),
|
||||
..
|
||||
}
|
||||
) {
|
||||
return Err(Error::InvalidSyntax(
|
||||
slot_logic.span,
|
||||
"Expected a string".into(),
|
||||
));
|
||||
}
|
||||
let next = args.next();
|
||||
let expr = next.ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::System(System::SetSlot(
|
||||
dev_name,
|
||||
slot_index,
|
||||
slot_logic,
|
||||
Box::new(expr),
|
||||
)))
|
||||
}
|
||||
|
||||
// Math SysCalls
|
||||
"acos" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let tmp = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Acos(boxed!(tmp))))
|
||||
}
|
||||
"asin" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let tmp = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Asin(boxed!(tmp))))
|
||||
}
|
||||
"atan" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let expr = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Atan(boxed!(expr))))
|
||||
}
|
||||
"atan2" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 2)?;
|
||||
check_length(2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
@@ -1791,42 +1941,42 @@ impl<'a> Parser<'a> {
|
||||
Ok(SysCall::Math(Math::Atan2(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"abs" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let expr = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Abs(boxed!(expr))))
|
||||
}
|
||||
"ceil" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Ceil(boxed!(arg))))
|
||||
}
|
||||
"cos" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Cos(boxed!(arg))))
|
||||
}
|
||||
"floor" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Floor(boxed!(arg))))
|
||||
}
|
||||
"log" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Log(boxed!(arg))))
|
||||
}
|
||||
"max" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 2)?;
|
||||
check_length(2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
@@ -1834,7 +1984,7 @@ impl<'a> Parser<'a> {
|
||||
Ok(SysCall::Math(Math::Max(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"min" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 2)?;
|
||||
check_length(2)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg1 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
let arg2 = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
@@ -1842,32 +1992,32 @@ impl<'a> Parser<'a> {
|
||||
Ok(SysCall::Math(Math::Min(boxed!(arg1), boxed!(arg2))))
|
||||
}
|
||||
"rand" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 0)?;
|
||||
check_length(0)?;
|
||||
Ok(SysCall::Math(Math::Rand))
|
||||
}
|
||||
"sin" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Sin(boxed!(arg))))
|
||||
}
|
||||
"sqrt" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Sqrt(boxed!(arg))))
|
||||
}
|
||||
"tan" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
Ok(SysCall::Math(Math::Tan(boxed!(arg))))
|
||||
}
|
||||
"trunc" => {
|
||||
check_length(self.current_span(), &invocation.arguments, 1)?;
|
||||
check_length(1)?;
|
||||
let mut args = invocation.arguments.into_iter();
|
||||
let arg = args.next().ok_or(Error::UnexpectedEOF)?;
|
||||
|
||||
|
||||
@@ -214,6 +214,30 @@ documented! {
|
||||
Spanned<Literal<'a>>,
|
||||
Box<Spanned<Expression<'a>>>,
|
||||
),
|
||||
/// Loads slot LogicSlotType from device into a variable
|
||||
///
|
||||
/// ## IC10
|
||||
/// `ls r0 d0 2 Occupied`
|
||||
/// ## Slang
|
||||
/// `let isOccupied = loadSlot(deviceHash, 2, "Occupied");`
|
||||
/// `let isOccupied = ls(deviceHash, 2, "Occupied");`
|
||||
LoadSlot(
|
||||
Spanned<LiteralOrVariable<'a>>,
|
||||
Spanned<Literal<'a>>,
|
||||
Spanned<Literal<'a>>
|
||||
),
|
||||
/// Stores a value of LogicType on a device by the index value
|
||||
/// ## IC10
|
||||
/// `ss d0 0 "Open" 1`
|
||||
/// ## Slang
|
||||
/// `setSlot(deviceHash, 0, "Open", true);`
|
||||
/// `ss(deviceHash, 0, "Open", true);`
|
||||
SetSlot(
|
||||
Spanned<LiteralOrVariable<'a>>,
|
||||
Spanned<Literal<'a>>,
|
||||
Spanned<Literal<'a>>,
|
||||
Box<Spanned<Expression<'a>>>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,6 +259,8 @@ impl<'a> std::fmt::Display for System<'a> {
|
||||
System::SetOnDeviceBatchedNamed(a, b, c, d) => {
|
||||
write!(f, "setOnDeviceBatchedNamed({}, {}, {}, {})", a, b, c, d)
|
||||
}
|
||||
System::LoadSlot(a, b, c) => write!(f, "loadSlot({}, {}, {})", a, b, c),
|
||||
System::SetSlot(a, b, c, d) => write!(f, "setSlot({}, {}, {}, {})", a, b, c, d),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,3 +160,37 @@ fn test_negative_literal_const() -> Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ternary_expression() -> Result<()> {
|
||||
let expr = parser!(r#"let i = x ? 1 : 2;"#).parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = (x ? 1 : 2))", expr.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complex_binary_with_ternary() -> Result<()> {
|
||||
let expr = parser!("let i = (x ? 1 : 3) * 2;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = ((x ? 1 : 3) * 2))", expr.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_operator_prescedence_with_ternary() -> Result<()> {
|
||||
let expr = parser!("let x = x ? 1 : 3 * 2;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let x = (x ? 1 : (3 * 2)))", expr.to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_nested_ternary_right_associativity() -> Result<()> {
|
||||
let expr = parser!("let i = a ? b : c ? d : e;").parse()?.unwrap();
|
||||
|
||||
assert_eq!("(let i = (a ? b : (c ? d : e)))", expr.to_string());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::sys_call::SysCall;
|
||||
use crate::sys_call;
|
||||
use helpers::Span;
|
||||
use safer_ffi::prelude::*;
|
||||
use std::{borrow::Cow, ops::Deref};
|
||||
use tokenizer::token::Number;
|
||||
|
||||
@@ -277,50 +279,29 @@ pub struct WhileExpression<'a> {
|
||||
pub body: BlockExpression<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub struct TernaryExpression<'a> {
|
||||
pub condition: Box<Spanned<Expression<'a>>>,
|
||||
pub true_value: Box<Spanned<Expression<'a>>>,
|
||||
pub false_value: Box<Spanned<Expression<'a>>>,
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for TernaryExpression<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"({} ? {} : {})",
|
||||
self.condition, self.true_value, self.false_value
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> std::fmt::Display for WhileExpression<'a> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "(while {} {})", self.condition, self.body)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Span {
|
||||
pub start_line: usize,
|
||||
pub end_line: usize,
|
||||
pub start_col: usize,
|
||||
pub end_col: usize,
|
||||
}
|
||||
|
||||
impl From<Span> for lsp_types::Range {
|
||||
fn from(value: Span) -> Self {
|
||||
Self {
|
||||
start: lsp_types::Position {
|
||||
line: value.start_line as u32,
|
||||
character: value.start_col as u32,
|
||||
},
|
||||
end: lsp_types::Position {
|
||||
line: value.end_line as u32,
|
||||
character: value.end_col as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&Span> for lsp_types::Range {
|
||||
fn from(value: &Span) -> Self {
|
||||
Self {
|
||||
start: lsp_types::Position {
|
||||
line: value.start_line as u32,
|
||||
character: value.start_col as u32,
|
||||
},
|
||||
end: lsp_types::Position {
|
||||
line: value.end_line as u32,
|
||||
character: value.end_col as u32,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Spanned<T> {
|
||||
pub span: Span,
|
||||
@@ -364,8 +345,9 @@ pub enum Expression<'a> {
|
||||
MethodCall(Spanned<MethodCallExpression<'a>>),
|
||||
Negation(Box<Spanned<Expression<'a>>>),
|
||||
Priority(Box<Spanned<Expression<'a>>>),
|
||||
Return(Box<Spanned<Expression<'a>>>),
|
||||
Return(Option<Box<Spanned<Expression<'a>>>>),
|
||||
Syscall(Spanned<SysCall<'a>>),
|
||||
Ternary(Spanned<TernaryExpression<'a>>),
|
||||
Variable(Spanned<Cow<'a, str>>),
|
||||
While(Spanned<WhileExpression<'a>>),
|
||||
}
|
||||
@@ -391,8 +373,17 @@ impl<'a> std::fmt::Display for Expression<'a> {
|
||||
Expression::MethodCall(e) => write!(f, "{}", e),
|
||||
Expression::Negation(e) => write!(f, "(-{})", e),
|
||||
Expression::Priority(e) => write!(f, "({})", e),
|
||||
Expression::Return(e) => write!(f, "(return {})", e),
|
||||
Expression::Return(e) => write!(
|
||||
f,
|
||||
"(return {})",
|
||||
if let Some(e) = e {
|
||||
e.to_string()
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
),
|
||||
Expression::Syscall(e) => write!(f, "{}", e),
|
||||
Expression::Ternary(e) => write!(f, "{}", e),
|
||||
Expression::Variable(id) => write!(f, "{}", id),
|
||||
Expression::While(e) => write!(f, "{}", e),
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ impl From<LexError> for Diagnostic {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
_ => todo!(),
|
||||
_ => Diagnostic::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,6 +225,7 @@ pub enum TokenType<'a> {
|
||||
#[token(".", symbol!(Dot))]
|
||||
#[token("^", symbol!(Caret))]
|
||||
#[token("%", symbol!(Percent))]
|
||||
#[token("?", symbol!(Question))]
|
||||
#[token("==", symbol!(Equal))]
|
||||
#[token("!=", symbol!(NotEqual))]
|
||||
#[token("&&", symbol!(LogicalAnd))]
|
||||
@@ -402,6 +403,12 @@ pub enum Number {
|
||||
Decimal(Decimal),
|
||||
}
|
||||
|
||||
impl From<bool> for Number {
|
||||
fn from(value: bool) -> Self {
|
||||
Self::Integer(if value { 1 } else { 0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Number> for Decimal {
|
||||
fn from(value: Number) -> Self {
|
||||
match value {
|
||||
@@ -535,6 +542,8 @@ pub enum Symbol {
|
||||
Caret,
|
||||
/// Represents the `%` symbol
|
||||
Percent,
|
||||
/// Represents the `?` symbol
|
||||
Question,
|
||||
|
||||
// Double Character Symbols
|
||||
/// Represents the `==` symbol
|
||||
@@ -601,6 +610,7 @@ impl std::fmt::Display for Symbol {
|
||||
Self::Asterisk => write!(f, "*"),
|
||||
Self::Slash => write!(f, "/"),
|
||||
Self::LessThan => write!(f, "<"),
|
||||
Self::Question => write!(f, "?"),
|
||||
Self::LessThanOrEqual => write!(f, "<="),
|
||||
Self::GreaterThan => write!(f, ">"),
|
||||
Self::GreaterThanOrEqual => write!(f, ">="),
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use compiler::Compiler;
|
||||
use helpers::Documentation;
|
||||
use compiler::{CompilationResult, Compiler};
|
||||
use helpers::{Documentation, Span};
|
||||
use parser::{sys_call::SysCall, Parser};
|
||||
use safer_ffi::prelude::*;
|
||||
use std::io::BufWriter;
|
||||
@@ -8,6 +8,20 @@ use tokenizer::{
|
||||
Tokenizer,
|
||||
};
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiSourceMapEntry {
|
||||
pub line_number: u32,
|
||||
pub span: FfiRange,
|
||||
}
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiCompilationResult {
|
||||
pub output_code: safer_ffi::String,
|
||||
pub source_map: safer_ffi::Vec<FfiSourceMapEntry>,
|
||||
}
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
pub struct FfiToken {
|
||||
@@ -34,6 +48,17 @@ pub struct FfiDocumentedItem {
|
||||
docs: safer_ffi::String,
|
||||
}
|
||||
|
||||
impl From<Span> for FfiRange {
|
||||
fn from(value: Span) -> Self {
|
||||
Self {
|
||||
start_line: value.start_line as u32,
|
||||
end_line: value.end_line as u32,
|
||||
start_col: value.start_col as u32,
|
||||
end_col: value.end_col as u32,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<lsp_types::Range> for FfiRange {
|
||||
fn from(value: lsp_types::Range) -> Self {
|
||||
Self {
|
||||
@@ -69,6 +94,11 @@ impl From<lsp_types::Diagnostic> for FfiDiagnostic {
|
||||
}
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
pub fn free_ffi_compilation_result(input: FfiCompilationResult) {
|
||||
drop(input)
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
pub fn free_ffi_token_vec(v: safer_ffi::Vec<FfiToken>) {
|
||||
drop(v)
|
||||
@@ -94,28 +124,56 @@ pub fn free_docs_vec(v: safer_ffi::Vec<FfiDocumentedItem>) {
|
||||
/// This should result in the ability to compile many times without triggering frame drops
|
||||
/// from the GC from a `GetBytes()` call on a string in C#.
|
||||
#[ffi_export]
|
||||
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::String {
|
||||
pub fn compile_from_string(input: safer_ffi::slice::Ref<'_, u16>) -> FfiCompilationResult {
|
||||
let res = std::panic::catch_unwind(|| {
|
||||
let input = String::from_utf16_lossy(input.as_slice());
|
||||
let mut writer = BufWriter::new(Vec::new());
|
||||
|
||||
let tokenizer = Tokenizer::from(input.as_str());
|
||||
let parser = Parser::new(tokenizer);
|
||||
let compiler = Compiler::new(parser, &mut writer, None);
|
||||
let compiler = Compiler::new(parser, None);
|
||||
|
||||
if !compiler.compile().is_empty() {
|
||||
return safer_ffi::String::EMPTY;
|
||||
let res = compiler.compile();
|
||||
|
||||
if !res.errors.is_empty() {
|
||||
return (safer_ffi::String::EMPTY, res.instructions.source_map());
|
||||
}
|
||||
|
||||
let mut writer = BufWriter::new(Vec::new());
|
||||
|
||||
// writing into a Vec<u8>. This should not fail.
|
||||
let optimized = optimizer::optimize(res.instructions);
|
||||
let map = optimized.source_map();
|
||||
_ = optimized.write(&mut writer);
|
||||
|
||||
let Ok(compiled_vec) = writer.into_inner() else {
|
||||
return safer_ffi::String::EMPTY;
|
||||
return (safer_ffi::String::EMPTY, map);
|
||||
};
|
||||
|
||||
// Safety: I know the compiler only outputs valid utf8
|
||||
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) })
|
||||
(
|
||||
safer_ffi::String::from(unsafe { String::from_utf8_unchecked(compiled_vec) }),
|
||||
map,
|
||||
)
|
||||
});
|
||||
|
||||
res.unwrap_or("".into())
|
||||
if let Ok((res_str, source_map)) = res {
|
||||
FfiCompilationResult {
|
||||
source_map: source_map
|
||||
.into_iter()
|
||||
.map(|(line_num, span)| FfiSourceMapEntry {
|
||||
span: span.into(),
|
||||
line_number: line_num as u32,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.into(),
|
||||
output_code: res_str,
|
||||
}
|
||||
} else {
|
||||
FfiCompilationResult {
|
||||
output_code: "".into(),
|
||||
source_map: vec![].into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
@@ -180,11 +238,12 @@ pub fn diagnose_source(input: safer_ffi::slice::Ref<'_, u16>) -> safer_ffi::Vec<
|
||||
let res = std::panic::catch_unwind(|| {
|
||||
let input = String::from_utf16_lossy(input.as_slice());
|
||||
|
||||
let mut writer = BufWriter::new(Vec::new());
|
||||
let tokenizer = Tokenizer::from(input.as_str());
|
||||
let compiler = Compiler::new(Parser::new(tokenizer), &mut writer, None);
|
||||
let compiler = Compiler::new(Parser::new(tokenizer), None);
|
||||
|
||||
let diagnosis = compiler.compile();
|
||||
let CompilationResult {
|
||||
errors: diagnosis, ..
|
||||
} = compiler.compile();
|
||||
|
||||
let mut result_vec: Vec<FfiDiagnostic> = Vec::with_capacity(diagnosis.len());
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![allow(clippy::result_large_err)]
|
||||
|
||||
use clap::Parser;
|
||||
use compiler::Compiler;
|
||||
use compiler::{CompilationResult, Compiler};
|
||||
use parser::Parser as ASTParser;
|
||||
use std::{
|
||||
fs::File,
|
||||
@@ -88,9 +88,13 @@ fn run_logic<'a>() -> Result<(), Error<'a>> {
|
||||
None => BufWriter::new(Box::new(std::io::stdout())),
|
||||
};
|
||||
|
||||
let compiler = Compiler::new(parser, &mut writer, None);
|
||||
let compiler = Compiler::new(parser, None);
|
||||
|
||||
let errors = compiler.compile();
|
||||
let CompilationResult {
|
||||
errors,
|
||||
instructions,
|
||||
..
|
||||
} = compiler.compile();
|
||||
|
||||
if !errors.is_empty() {
|
||||
let mut std_error = stderr();
|
||||
@@ -103,6 +107,8 @@ fn run_logic<'a>() -> Result<(), Error<'a>> {
|
||||
}
|
||||
}
|
||||
|
||||
optimizer::optimize(instructions).write(&mut writer)?;
|
||||
|
||||
writer.flush()?;
|
||||
|
||||
Ok(())
|
||||
|
||||
43
spilling.slang
Normal file
43
spilling.slang
Normal file
@@ -0,0 +1,43 @@
|
||||
device self = "db";
|
||||
device gasSensor = "d0";
|
||||
device atmosAnal = "d1";
|
||||
device atmosValve = "d2";
|
||||
device atmosTank = "d3";
|
||||
device atmosInlet = "d4";
|
||||
|
||||
atmosInlet.Lock = true;
|
||||
atmosInlet.Mode = 1;
|
||||
atmosValve.On = false;
|
||||
atmosValve.Lock = true;
|
||||
|
||||
let isPumping = false;
|
||||
let tempPressure = 0;
|
||||
|
||||
loop {
|
||||
yield();
|
||||
let temp = gasSensor.Temperature;
|
||||
let pres = atmosAnal.Pressure;
|
||||
let liqV = atmosAnal.VolumeOfLiquid;
|
||||
let tempVol = atmosAnal.Volume;
|
||||
|
||||
let stress = 5_000 * liqV / tempVol;
|
||||
|
||||
tempPressure = isPumping ? 1_000 : 10_000;
|
||||
|
||||
let shouldTurnOnInlet = (
|
||||
temp > 0c &&
|
||||
pres < tempPressure &&
|
||||
stress < 50
|
||||
);
|
||||
|
||||
isPumping = (
|
||||
!shouldTurnOnInlet &&
|
||||
atmosTank.Pressure < 35_000 &&
|
||||
atmosAnal.RatioPollutant == 0 &&
|
||||
atmosAnal.RatioLiquidPollutant == 0 &&
|
||||
atmosAnal.Pressure > 1_000
|
||||
);
|
||||
|
||||
atmosValve.On = isPumping;
|
||||
atmosInlet.On = shouldTurnOnInlet;
|
||||
}
|
||||
72
test.slang
Normal file
72
test.slang
Normal file
@@ -0,0 +1,72 @@
|
||||
/// Laree script V1
|
||||
|
||||
device self = "db";
|
||||
device larre = "d0";
|
||||
device exportChute = "d1";
|
||||
|
||||
const TOTAL_SLOTS = 19;
|
||||
const EXPORT_CHUTE = 1;
|
||||
const START_STATION = 2;
|
||||
|
||||
let currentIndex = 0;
|
||||
|
||||
/// Waits for the larre to be idle before continuing
|
||||
fn waitForIdle() {
|
||||
yield();
|
||||
while (!larre.Idle) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
/// Instructs the Larre to go to the chute and deposit
|
||||
/// what is currently in its arm
|
||||
fn deposit() {
|
||||
larre.Setting = EXPORT_CHUTE;
|
||||
waitForIdle();
|
||||
larre.Activate = true;
|
||||
waitForIdle();
|
||||
exportChute.Open = false;
|
||||
}
|
||||
|
||||
/// This function is responsible for checking the plant under
|
||||
/// the larre at this index, and harvesting if applicable
|
||||
fn checkAndHarvest(currentIndex) {
|
||||
if (currentIndex <= EXPORT_CHUTE || ls(larre, 255, "Seeding") < 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// harvest from this device
|
||||
while (ls(larre, 255, "Mature")) {
|
||||
yield();
|
||||
larre.Activate = true;
|
||||
}
|
||||
let hasRemainingPlant = ls(larre, 255, "Occupied");
|
||||
|
||||
// move to the export chute
|
||||
larre.Setting = EXPORT_CHUTE;
|
||||
waitForIdle();
|
||||
deposit();
|
||||
if (hasRemainingPlant) {
|
||||
deposit();
|
||||
}
|
||||
|
||||
larre.Setting = currentIndex;
|
||||
waitForIdle();
|
||||
|
||||
if (ls(larre, 0, "Occupied")) {
|
||||
larre.Activate = true;
|
||||
}
|
||||
waitForIdle();
|
||||
}
|
||||
|
||||
loop {
|
||||
yield();
|
||||
if (!larre.Idle) {
|
||||
continue;
|
||||
}
|
||||
let newIndex = currentIndex + 1 > TOTAL_SLOTS ? START_STATION : currentIndex + 1;
|
||||
|
||||
checkAndHarvest(currentIndex);
|
||||
larre.Setting = newIndex;
|
||||
currentIndex = newIndex;
|
||||
}
|
||||
Reference in New Issue
Block a user