Skip to content

Commit 545a817

Browse files
committed
clean up a bit.
1 parent 027cfb9 commit 545a817

8 files changed

Lines changed: 93 additions & 77 deletions

File tree

VirtualMachine/EVIL.Ceres/ExecutionEngine/Diagnostics/Chunk.Deserializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ public static Chunk Deserialize(Stream stream, out byte version, out long timest
148148
br.ReadInt32(),
149149
br.ReadInt32(),
150150
br.ReadString(),
151-
br.ReadBoolean(),
152-
br.ReadBoolean(),
151+
(ClosureType)br.ReadInt32(),
153152
br.ReadBoolean()
154153
)
155154
);

VirtualMachine/EVIL.Ceres/ExecutionEngine/Diagnostics/Chunk.Serializer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ private void WriteClosureInfo(BinaryWriter bw)
114114
bw.Write(closure.NestingLevel);
115115
bw.Write(closure.EnclosedId);
116116
bw.Write(closure.EnclosedFunctionName);
117-
bw.Write(closure.IsParameter);
118-
bw.Write(closure.IsClosure);
117+
bw.Write((int)closure.Type);
119118
bw.Write(closure.IsSharedScope);
120119
}
121120
}

VirtualMachine/EVIL.Ceres/ExecutionEngine/Diagnostics/Chunk.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,14 @@ public ClosureContext SetClosureContext(string chunkName)
199199
return value;
200200
}
201201

202-
public (int Id, ClosureInfo Closure) AllocateClosure(int nestingLevel, int enclosedId, string enclosedFunctionName, bool isParameter, bool isClosure, bool isSharedScope)
202+
public (int Id, ClosureInfo Closure) AllocateClosure(int nestingLevel, int enclosedId, string enclosedFunctionName, ClosureType closureType, bool isSharedScope)
203203
{
204204
var id = ClosureCount;
205205
var ret = new ClosureInfo(
206206
nestingLevel,
207207
enclosedId,
208208
enclosedFunctionName,
209-
isParameter,
210-
isClosure,
209+
closureType,
211210
isSharedScope
212211
);
213212

VirtualMachine/EVIL.Ceres/ExecutionEngine/Diagnostics/ClosureInfo.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,21 @@ public record ClosureInfo
88
public int EnclosedId { get; }
99
public string EnclosedFunctionName { get; }
1010

11-
public bool IsParameter { get; }
12-
public bool IsClosure { get; }
13-
public bool IsLocal => !IsParameter && !IsClosure;
11+
public ClosureType Type { get; }
1412

1513
public bool IsSharedScope { get; }
1614

1715
internal ClosureInfo(
1816
int nestingLevel,
1917
int enclosedId,
2018
string enclosedFunctionName,
21-
bool isParameter,
22-
bool isClosure,
19+
ClosureType type,
2320
bool isSharedScope)
2421
{
2522
NestingLevel = nestingLevel;
2623
EnclosedId = enclosedId;
2724
EnclosedFunctionName = enclosedFunctionName;
28-
IsParameter = isParameter;
29-
IsClosure = isClosure;
25+
Type = type;
3026
IsSharedScope = isSharedScope;
3127
}
3228

@@ -35,8 +31,7 @@ public virtual bool Equals(ClosureInfo? other)
3531
return NestingLevel == other?.NestingLevel
3632
&& EnclosedId == other.EnclosedId
3733
&& EnclosedFunctionName == other.EnclosedFunctionName
38-
&& IsParameter == other.IsParameter
39-
&& IsClosure == other.IsClosure
34+
&& Type == other.Type
4035
&& IsSharedScope == other.IsSharedScope;
4136
}
4237

@@ -45,8 +40,7 @@ public override int GetHashCode()
4540
NestingLevel,
4641
EnclosedId,
4742
EnclosedFunctionName,
48-
IsParameter,
49-
IsClosure,
43+
Type,
5044
IsSharedScope
5145
);
5246
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace EVIL.Ceres.ExecutionEngine.Diagnostics;
2+
3+
public enum ClosureType
4+
{
5+
Parameter,
6+
Local,
7+
Closure
8+
}

VirtualMachine/EVIL.Ceres/ExecutionEngine/Diagnostics/Disassembler.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace EVIL.Ceres.ExecutionEngine.Diagnostics;
22

3+
using System;
34
using System.Collections.Generic;
45
using System.IO;
56
using System.Linq;
@@ -137,15 +138,13 @@ public static void Disassemble(Chunk chunk, TextWriter output, DisassemblyOption
137138
if (options.WriteClosureInfo)
138139
{
139140
var closure = chunk.Closures[closureId];
140-
var closureType = "local";
141-
if (closure.IsParameter)
141+
var closureType = closure.Type switch
142142
{
143-
closureType = "parameter";
144-
}
145-
else if (closure.IsClosure)
146-
{
147-
closureType = "closure";
148-
}
143+
ClosureType.Parameter => "parameter",
144+
ClosureType.Local => "local",
145+
ClosureType.Closure => "closure",
146+
_ => "???"
147+
};
149148

150149
var names = new List<string>();
151150
var closureFrom = chunk;

VirtualMachine/EVIL.Ceres/ExecutionEngine/ExecutionUnit.cs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ public void Step()
816816
var localId = frame.FetchInt32();
817817

818818
frame.Locals![localId] = a;
819-
PropagateLocalToSubClosures(frame, localId, a);
819+
PropagateValueToSubClosures(frame, ClosureType.Local, localId, a);
820820
break;
821821
}
822822

@@ -832,7 +832,7 @@ public void Step()
832832
var argId = frame.FetchInt32();
833833

834834
frame.Arguments[argId] = a;
835-
PropagateArgumentToSubClosures(frame, argId, a);
835+
PropagateValueToSubClosures(frame, ClosureType.Parameter, argId, a);
836836
break;
837837
}
838838

@@ -1394,7 +1394,7 @@ private static bool FindMetaFunction(Table table, string op, [MaybeNullWhen(fals
13941394

13951395
private ClosureInfo ResolveInnermostClosure(ClosureInfo closureInfo, ref ScriptStackFrame? frame)
13961396
{
1397-
while (closureInfo.IsClosure)
1397+
while (closureInfo.Type == ClosureType.Closure)
13981398
{
13991399
closureInfo = frame!.Chunk.Closures[closureInfo.EnclosedId];
14001400
frame = FindClosureSourceFrame(closureInfo.EnclosedFunctionName);
@@ -1409,12 +1409,12 @@ private DynamicValue GetClosureValue(ScriptStackFrame currentFrame, ClosureInfo
14091409

14101410
if (sourceFrame != null)
14111411
{
1412-
if (info.IsParameter)
1412+
if (info.Type == ClosureType.Parameter)
14131413
{
14141414
return sourceFrame.Arguments[info.EnclosedId];
14151415
}
14161416

1417-
if (info.IsClosure)
1417+
if (info.Type == ClosureType.Closure)
14181418
{
14191419
var resolved = ResolveInnermostClosure(info, ref sourceFrame);
14201420
var ctx = resolved.IsSharedScope
@@ -1428,7 +1428,7 @@ private DynamicValue GetClosureValue(ScriptStackFrame currentFrame, ClosureInfo
14281428
}
14291429
else
14301430
{
1431-
if (info.IsClosure)
1431+
if (info.Type == ClosureType.Closure)
14321432
{
14331433
var chunk = currentFrame.Chunk;
14341434
while (chunk.Name != info.EnclosedFunctionName)
@@ -1453,13 +1453,13 @@ private void SetClosureValue(ScriptStackFrame currentFrame, ClosureInfo info, Dy
14531453

14541454
if (targetFrame != null)
14551455
{
1456-
if (info.IsParameter)
1456+
if (info.Type == ClosureType.Parameter)
14571457
{
14581458
targetFrame.Arguments[info.EnclosedId] = value;
14591459
return;
14601460
}
14611461

1462-
if (info.IsClosure)
1462+
if (info.Type == ClosureType.Closure)
14631463
{
14641464
var resolved = ResolveInnermostClosure(info, ref targetFrame);
14651465
var ctx = resolved.IsSharedScope
@@ -1474,7 +1474,7 @@ private void SetClosureValue(ScriptStackFrame currentFrame, ClosureInfo info, Dy
14741474
return;
14751475
}
14761476

1477-
if (info.IsClosure)
1477+
if (info.Type == ClosureType.Closure)
14781478
{
14791479
var chunk = currentFrame.Chunk;
14801480
while (chunk.Name != info.EnclosedFunctionName)
@@ -1512,11 +1512,12 @@ private void InitializeClosures(Chunk chunk, ScriptStackFrame stackFrame)
15121512
if (sourceFrame == null)
15131513
continue;
15141514

1515-
DynamicValue value = closure.IsParameter
1516-
? sourceFrame.Arguments[closure.EnclosedId]
1517-
: closure.IsClosure
1518-
? GetClosureValue(stackFrame, sourceFrame.Chunk.Closures[closure.EnclosedId])
1519-
: sourceFrame.Locals![closure.EnclosedId];
1515+
DynamicValue value = closure.Type switch
1516+
{
1517+
ClosureType.Parameter => sourceFrame.Arguments[closure.EnclosedId],
1518+
ClosureType.Closure => GetClosureValue(stackFrame, sourceFrame.Chunk.Closures[closure.EnclosedId]),
1519+
_ => sourceFrame.Locals![closure.EnclosedId]
1520+
};
15201521

15211522
ctx.Values[closure.EnclosedId] = value;
15221523
}
@@ -1525,37 +1526,8 @@ private void InitializeClosures(Chunk chunk, ScriptStackFrame stackFrame)
15251526
subChunkQueue.Enqueue(current.SubChunks[i].Clone());
15261527
}
15271528
}
1528-
1529-
private void PropagateLocalToSubClosures(ScriptStackFrame frame, int localId, DynamicValue value)
1530-
{
1531-
var subChunkQueue = new Queue<Chunk>(frame.Chunk.SubChunks);
1532-
1533-
while (subChunkQueue.Count > 0)
1534-
{
1535-
var current = subChunkQueue.Dequeue();
1536-
1537-
foreach (var closure in current.Closures)
1538-
{
1539-
if (!closure.IsLocal || closure.EnclosedId != localId)
1540-
continue;
1541-
1542-
var context = closure.IsSharedScope
1543-
? frame.Fiber.SetClosureContext(closure.EnclosedFunctionName)
1544-
: current.SetClosureContext(closure.EnclosedFunctionName);
1545-
1546-
var sourceFrame = FindClosureSourceFrame(closure.EnclosedFunctionName);
1547-
if (sourceFrame != null)
1548-
{
1549-
context.Values[closure.EnclosedId] = value;
1550-
}
1551-
}
15521529

1553-
for (var i = 0; i < current.SubChunks.Count; i++)
1554-
subChunkQueue.Enqueue(current.SubChunks[i].Clone());
1555-
}
1556-
}
1557-
1558-
private void PropagateArgumentToSubClosures(ScriptStackFrame frame, int localId, DynamicValue value)
1530+
private void PropagateValueToSubClosures(ScriptStackFrame frame, ClosureType closureType, int slotId, DynamicValue value)
15591531
{
15601532
var subChunkQueue = new Queue<Chunk>(frame.Chunk.SubChunks);
15611533

@@ -1565,7 +1537,7 @@ private void PropagateArgumentToSubClosures(ScriptStackFrame frame, int localId,
15651537

15661538
foreach (var closure in current.Closures)
15671539
{
1568-
if (!closure.IsParameter || closure.EnclosedId != localId)
1540+
if (closure.Type != closureType || closure.EnclosedId != slotId)
15691541
continue;
15701542

15711543
var context = closure.IsSharedScope

VirtualMachine/EVIL.Ceres/TranslationEngine/Compiler.Utilities.Emit.cs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ private void FinalizeChunk()
1010
{
1111
if (Chunk.CodeGenerator.TryPeekOpCode(out var opCode))
1212
{
13-
if (opCode == OpCode.RET || opCode == OpCode.TAILINVOKE)
13+
if (opCode is OpCode.RET
14+
or OpCode.TAILINVOKE
15+
or OpCode.THROW)
1416
{
1517
return;
1618
}
1719
}
1820

19-
/* Either we have no instructions in chunk, or it's not a RET. */
21+
/* Either we have no instructions in chunk, or it's not a RET nor THROW. */
2022
Chunk.CodeGenerator.Emit(OpCode.LDNIL);
2123
Chunk.CodeGenerator.Emit(OpCode.RET);
2224
}
@@ -65,12 +67,34 @@ private void EmitVarSet(string identifier)
6567
}
6668
else
6769
{
70+
ClosureType closureType;
71+
switch (sym.Type)
72+
{
73+
case Symbol.SymbolType.Parameter:
74+
closureType = ClosureType.Parameter;
75+
break;
76+
case Symbol.SymbolType.Local:
77+
closureType = ClosureType.Local;
78+
break;
79+
case Symbol.SymbolType.Closure:
80+
closureType = ClosureType.Closure;
81+
break;
82+
default:
83+
Log.TerminateWithInternalFailure(
84+
$"Invalid type '{sym.Type}' for symbol '{identifier}'.",
85+
CurrentFileName,
86+
line: Line,
87+
column: Column,
88+
dummyReturn: OpCode.NOOP
89+
);
90+
return;
91+
}
92+
6893
var result = Chunk.AllocateClosure(
6994
level,
7095
sym.Id,
7196
ownerScope.Chunk.Name,
72-
sym.Type == Symbol.SymbolType.Parameter,
73-
sym.Type == Symbol.SymbolType.Closure,
97+
closureType,
7498
ReferenceEquals(ownerScope.Chunk, _rootChunk)
7599
);
76100

@@ -160,12 +184,34 @@ private void EmitVarGet(string identifier)
160184
}
161185
else
162186
{
187+
ClosureType closureType;
188+
switch (sym.Type)
189+
{
190+
case Symbol.SymbolType.Parameter:
191+
closureType = ClosureType.Parameter;
192+
break;
193+
case Symbol.SymbolType.Local:
194+
closureType = ClosureType.Local;
195+
break;
196+
case Symbol.SymbolType.Closure:
197+
closureType = ClosureType.Closure;
198+
break;
199+
default:
200+
Log.TerminateWithInternalFailure(
201+
$"Invalid type '{sym.Type}' for symbol '{identifier}'.",
202+
CurrentFileName,
203+
line: Line,
204+
column: Column,
205+
dummyReturn: OpCode.NOOP
206+
);
207+
return;
208+
}
209+
163210
var result = Chunk.AllocateClosure(
164211
level,
165212
sym.Id,
166213
ownerScope.Chunk.Name,
167-
sym.Type == Symbol.SymbolType.Parameter,
168-
sym.Type == Symbol.SymbolType.Closure,
214+
closureType,
169215
ReferenceEquals(ownerScope.Chunk, _rootChunk)
170216
);
171217

0 commit comments

Comments
 (0)