From b82501650089727d3e4f10d6327bd2a62434d003 Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Sun, 1 Sep 2024 13:06:00 +0200 Subject: [PATCH] Slight refactoring of codegen structure (#460) * Moved instructions * More * Fix --- .../Internal/Codegen/CilCodegen.cs | 20 ++----- .../Internal/Codegen/PdbCodegen.cs | 2 +- .../OptimizingIr/FunctionBodyCodegen.cs | 10 +--- .../OptimizingIr/InstructionFactory.cs | 7 +-- .../AddressOfInstruction.cs | 3 +- .../ArithmeticInstruction.cs | 3 +- .../{Model => Instructions}/ArithmeticOp.cs | 2 +- .../ArrayLengthInstruction.cs | 3 +- .../{Model => Instructions}/BoxInstruction.cs | 3 +- .../BranchInstruction.cs | 3 +- .../Instructions/CallInstruction.cs | 52 +++++++++++++++++++ .../{Model => Instructions}/EndScope.cs | 2 +- .../{Model => Instructions}/IInstruction.cs | 3 +- .../IValueInstruction.cs | 4 +- .../InstructionBase.cs | 3 +- .../JumpInstruction.cs | 3 +- .../LoadElementInstruction.cs | 3 +- .../LoadFieldInstruction.cs | 3 +- .../LoadInstruction.cs | 3 +- .../NewArrayInstruction.cs | 3 +- .../NewDelegateInstruction.cs | 3 +- .../NewObjectInstruction.cs | 3 +- .../{Model => Instructions}/NopInstruction.cs | 2 +- .../{Model => Instructions}/RetInstruction.cs | 3 +- .../{Model => Instructions}/SequencePoint.cs | 2 +- .../{Model => Instructions}/StartScope.cs | 2 +- .../StoreElementInstruction.cs | 3 +- .../StoreFieldInstruction.cs | 3 +- .../StoreInstruction.cs | 3 +- .../TreeInstruction.cs | 3 +- .../Internal/OptimizingIr/Model/BasicBlock.cs | 1 + .../OptimizingIr/Model/CallInstruction.cs | 36 ------------- .../OptimizingIr/Model/IBasicBlock.cs | 1 + .../Model/MemberCallInstruction.cs | 42 --------------- .../Internal/OptimizingIr/PassFactory.cs | 1 + .../OptimizingIr/Passes/JumpThreading.cs | 1 + .../Internal/OptimizingIr/Stackifier.cs | 1 + .../Symbols/Metadata/MetadataMethodSymbol.cs | 1 + .../Internal/Symbols/TypeSymbol.cs | 1 + 39 files changed, 116 insertions(+), 131 deletions(-) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/AddressOfInstruction.cs (87%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/ArithmeticInstruction.cs (93%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/ArithmeticOp.cs (91%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/ArrayLengthInstruction.cs (87%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/BoxInstruction.cs (90%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/BranchInstruction.cs (91%) create mode 100644 src/Draco.Compiler/Internal/OptimizingIr/Instructions/CallInstruction.cs rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/EndScope.cs (86%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/IInstruction.cs (93%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/IValueInstruction.cs (69%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/InstructionBase.cs (90%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/JumpInstruction.cs (84%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/LoadElementInstruction.cs (90%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/LoadFieldInstruction.cs (90%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/LoadInstruction.cs (87%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/NewArrayInstruction.cs (91%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/NewDelegateInstruction.cs (92%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/NewObjectInstruction.cs (91%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/NopInstruction.cs (82%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/RetInstruction.cs (85%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/SequencePoint.cs (93%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/StartScope.cs (94%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/StoreElementInstruction.cs (91%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/StoreFieldInstruction.cs (91%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/StoreInstruction.cs (89%) rename src/Draco.Compiler/Internal/OptimizingIr/{Model => Instructions}/TreeInstruction.cs (94%) delete mode 100644 src/Draco.Compiler/Internal/OptimizingIr/Model/CallInstruction.cs delete mode 100644 src/Draco.Compiler/Internal/OptimizingIr/Model/MemberCallInstruction.cs diff --git a/src/Draco.Compiler/Internal/Codegen/CilCodegen.cs b/src/Draco.Compiler/Internal/Codegen/CilCodegen.cs index 43960fe1d..6d1e1c67a 100644 --- a/src/Draco.Compiler/Internal/Codegen/CilCodegen.cs +++ b/src/Draco.Compiler/Internal/Codegen/CilCodegen.cs @@ -7,6 +7,7 @@ using System.Reflection.Metadata.Ecma335; using System.Text; using Draco.Compiler.Internal.OptimizingIr; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; using Constant = Draco.Compiler.Internal.OptimizingIr.Model.Constant; @@ -145,7 +146,7 @@ IEnumerable RemainingOperands() switch (instruction) { - case OptimizingIr.Model.SequencePoint sp: + case OptimizingIr.Instructions.SequencePoint sp: { this.PdbCodegen?.AddSequencePoint(this.InstructionEncoder, sp); break; @@ -357,28 +358,15 @@ IEnumerable RemainingOperands() } case CallInstruction call: { - // Arguments + // Optional receiver and arguments foreach (var arg in RemainingOperands()) this.EncodePush(arg); // Call - this.InstructionEncoder.OpCode(ILOpCode.Call); + this.InstructionEncoder.OpCode(call.Procedure.IsVirtual ? ILOpCode.Callvirt : ILOpCode.Call); this.EncodeToken(call.Procedure); // Store result this.StoreRegister(call.Target); break; } - case MemberCallInstruction mcall: - { - // Receiver - this.EncodePush(NextOperand()); - // Arguments - foreach (var arg in RemainingOperands()) this.EncodePush(arg); - // Call - this.InstructionEncoder.OpCode(mcall.Procedure.IsVirtual ? ILOpCode.Callvirt : ILOpCode.Call); - this.EncodeToken(mcall.Procedure); - // Store result - this.StoreRegister(mcall.Target); - break; - } case NewObjectInstruction newObj: { // Arguments diff --git a/src/Draco.Compiler/Internal/Codegen/PdbCodegen.cs b/src/Draco.Compiler/Internal/Codegen/PdbCodegen.cs index 2b2bf031d..7522656e5 100644 --- a/src/Draco.Compiler/Internal/Codegen/PdbCodegen.cs +++ b/src/Draco.Compiler/Internal/Codegen/PdbCodegen.cs @@ -105,7 +105,7 @@ public void EndScope(int ilOffset) Length: ilOffset - start.StartOffset)); } - public void AddSequencePoint(InstructionEncoder encoder, OptimizingIr.Model.SequencePoint sequencePoint) + public void AddSequencePoint(InstructionEncoder encoder, OptimizingIr.Instructions.SequencePoint sequencePoint) { var sp = MakeSequencePoint(encoder, sequencePoint.Range); this.sequencePoints.Add(sp); diff --git a/src/Draco.Compiler/Internal/OptimizingIr/FunctionBodyCodegen.cs b/src/Draco.Compiler/Internal/OptimizingIr/FunctionBodyCodegen.cs index 29450dae3..f2619d365 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/FunctionBodyCodegen.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/FunctionBodyCodegen.cs @@ -4,6 +4,7 @@ using Draco.Compiler.Api; using Draco.Compiler.Internal.Binding; using Draco.Compiler.Internal.BoundTree; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; using Draco.Compiler.Internal.Symbols.Generic; @@ -290,14 +291,7 @@ public override IOperand VisitCallExpression(BoundCallExpression node) else { var callResult = this.DefineRegister(node.TypeRequired); - if (receiver is null) - { - this.Write(Call(callResult, proc, args)); - } - else - { - this.Write(MemberCall(callResult, proc, receiver, args)); - } + this.Write(Call(callResult, proc, receiver, args)); return callResult; } } diff --git a/src/Draco.Compiler/Internal/OptimizingIr/InstructionFactory.cs b/src/Draco.Compiler/Internal/OptimizingIr/InstructionFactory.cs index 19913ec99..3c0926cbb 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/InstructionFactory.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/InstructionFactory.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using Draco.Compiler.Api.Syntax; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; @@ -28,10 +29,10 @@ public static LoadFieldInstruction LoadField(Register target, IOperand receiver, public static JumpInstruction Jump(BasicBlock target) => new(target); public static BranchInstruction Branch(IOperand condition, BasicBlock then, BasicBlock @else) => new(condition, then, @else); - public static CallInstruction Call(Register target, FunctionSymbol proc, IEnumerable args) => - new(target, proc, args); - public static MemberCallInstruction MemberCall(Register target, FunctionSymbol proc, IOperand receiver, IEnumerable args) => + public static CallInstruction Call(Register target, FunctionSymbol proc, IOperand? receiver, IEnumerable args) => new(target, proc, receiver, args); + public static CallInstruction Call(Register target, FunctionSymbol proc, IEnumerable args) => + Call(target, proc, null, args); public static NewObjectInstruction NewObject(Register target, FunctionSymbol ctor, IEnumerable args) => new(target, ctor, args); public static NewDelegateInstruction NewDelegate(Register target, IOperand? receiver, FunctionSymbol function, FunctionSymbol delegateCtor) => diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/AddressOfInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/AddressOfInstruction.cs similarity index 87% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/AddressOfInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/AddressOfInstruction.cs index 05905af45..0ad794991 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/AddressOfInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/AddressOfInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Loads the address of some local/global/argument. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticInstruction.cs similarity index 93% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticInstruction.cs index 6d1c6ad5a..627aed3ba 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticInstruction.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Represents some kind of binary arithmetic instruction. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticOp.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticOp.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticOp.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticOp.cs index 9422876d3..f4510d5af 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArithmeticOp.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArithmeticOp.cs @@ -1,4 +1,4 @@ -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// The different arithmetic operations supported. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArrayLengthInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArrayLengthInstruction.cs similarity index 87% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/ArrayLengthInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArrayLengthInstruction.cs index 9ee25661b..3c5425ff5 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/ArrayLengthInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/ArrayLengthInstruction.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An array length query. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/BoxInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/BoxInstruction.cs similarity index 90% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/BoxInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/BoxInstruction.cs index cc89e6fc1..eaceb6acd 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/BoxInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/BoxInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Valuetype element boxing. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/BranchInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/BranchInstruction.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/BranchInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/BranchInstruction.cs index 8b925ead6..a9a56bd26 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/BranchInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/BranchInstruction.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A conditional jump. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Instructions/CallInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/CallInstruction.cs new file mode 100644 index 000000000..8a6a6626a --- /dev/null +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/CallInstruction.cs @@ -0,0 +1,52 @@ +using System.Collections.Generic; +using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; +using Draco.Compiler.Internal.Symbols; + +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; + +/// +/// A procedure call. +/// +internal sealed class CallInstruction( + Register target, + FunctionSymbol procedure, + IOperand? receiver, + IEnumerable arguments) : InstructionBase, IValueInstruction +{ + public override string InstructionKeyword => "call"; + + public Register Target { get; set; } = target; + + /// + /// The called procedure. + /// + public FunctionSymbol Procedure { get; set; } = procedure; + + /// + /// The receiver the method is called on. + /// + public IOperand? Receiver { get; set; } = receiver; + + /// + /// The arguments that are passed to the procedure. + /// + public IList Arguments { get; set; } = arguments.ToList(); + + public override IEnumerable StaticOperands => [this.Procedure]; + public override IEnumerable Operands => this.Receiver is null + ? this.Arguments + : this.Arguments.Prepend(this.Receiver); + + public override string ToString() + { + var target = this.Target.ToOperandString(); + var receiver = this.Receiver is null + ? string.Empty + : $"{this.Receiver.ToOperandString()}."; + var args = string.Join(", ", this.Arguments.Select(a => a.ToOperandString())); + return $"{target} := {this.InstructionKeyword} {receiver}[{this.Procedure.FullName}]({args})"; + } + + public override CallInstruction Clone() => new(this.Target, this.Procedure, this.Receiver, this.Arguments); +} diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/EndScope.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/EndScope.cs similarity index 86% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/EndScope.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/EndScope.cs index a407ff655..a9da7fb9b 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/EndScope.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/EndScope.cs @@ -1,4 +1,4 @@ -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A pseudo-instruction for representing the end of a local scope. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/IInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/IInstruction.cs similarity index 93% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/IInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/IInstruction.cs index 95e6db005..8e3ed83fc 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/IInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/IInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Read-only interface of an instruction. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/IValueInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/IValueInstruction.cs similarity index 69% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/IValueInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/IValueInstruction.cs index 6d9979cd6..778fb9f0c 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/IValueInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/IValueInstruction.cs @@ -1,4 +1,6 @@ -namespace Draco.Compiler.Internal.OptimizingIr.Model; +using Draco.Compiler.Internal.OptimizingIr.Model; + +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An instruction that produces a result in a register. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/InstructionBase.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/InstructionBase.cs similarity index 90% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/InstructionBase.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/InstructionBase.cs index c075de148..82632cec1 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/InstructionBase.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/InstructionBase.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A mutable base class for implementations. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/JumpInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/JumpInstruction.cs similarity index 84% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/JumpInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/JumpInstruction.cs index b411505e4..794693e3f 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/JumpInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/JumpInstruction.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An unconditional jump. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadElementInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadElementInstruction.cs similarity index 90% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/LoadElementInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadElementInstruction.cs index 17975339f..db550906e 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadElementInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadElementInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An array element access. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadFieldInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadFieldInstruction.cs similarity index 90% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/LoadFieldInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadFieldInstruction.cs index fc8f27fb0..1607a6335 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadFieldInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadFieldInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A field access. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadInstruction.cs similarity index 87% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/LoadInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadInstruction.cs index 848cd082c..dddd8306d 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/LoadInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/LoadInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Loads a value from a local/global/argument. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewArrayInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewArrayInstruction.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/NewArrayInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewArrayInstruction.cs index 55cadb4f9..a9c603661 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewArrayInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewArrayInstruction.cs @@ -1,8 +1,9 @@ using System.Collections.Generic; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An array instantiation. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewDelegateInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewDelegateInstruction.cs similarity index 92% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/NewDelegateInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewDelegateInstruction.cs index cc8140fee..50f489f35 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewDelegateInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewDelegateInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A delegate instantiation. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewObjectInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewObjectInstruction.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/NewObjectInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewObjectInstruction.cs index 4572ae592..7d94f7e4d 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/NewObjectInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NewObjectInstruction.cs @@ -1,8 +1,9 @@ using System.Collections.Generic; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// An object instantiation. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/NopInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NopInstruction.cs similarity index 82% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/NopInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/NopInstruction.cs index e4fd4a17c..4cadf412b 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/NopInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/NopInstruction.cs @@ -1,4 +1,4 @@ -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Represents no operation. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/RetInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/RetInstruction.cs similarity index 85% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/RetInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/RetInstruction.cs index 1bd9a6493..61d07c8f3 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/RetInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/RetInstruction.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Returns from the current procedure. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/SequencePoint.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/SequencePoint.cs similarity index 93% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/SequencePoint.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/SequencePoint.cs index 401ffbf1f..cffd4da38 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/SequencePoint.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/SequencePoint.cs @@ -1,7 +1,7 @@ using System.Text; using Draco.Compiler.Api.Syntax; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A pseudo-instruction for representing sequence points. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/StartScope.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StartScope.cs similarity index 94% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/StartScope.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/StartScope.cs index 3f7a221b5..ddeb9478d 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/StartScope.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StartScope.cs @@ -4,7 +4,7 @@ using System.Text; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// A pseudo-instruction for representing the start of a local scope. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreElementInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreElementInstruction.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/StoreElementInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreElementInstruction.cs index d4bef4ea4..c495fb178 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreElementInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreElementInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Stores a value in an array element. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreFieldInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreFieldInstruction.cs similarity index 91% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/StoreFieldInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreFieldInstruction.cs index 068b9d87e..14541a9ec 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreFieldInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreFieldInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Stores a value in a field. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreInstruction.cs similarity index 89% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/StoreInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreInstruction.cs index 9144ab2f8..92dfd81b3 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/StoreInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/StoreInstruction.cs @@ -1,7 +1,8 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Stores a value in a local/global/argument. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/TreeInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/TreeInstruction.cs similarity index 94% rename from src/Draco.Compiler/Internal/OptimizingIr/Model/TreeInstruction.cs rename to src/Draco.Compiler/Internal/OptimizingIr/Instructions/TreeInstruction.cs index 8309594d6..c1a753526 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/TreeInstruction.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Instructions/TreeInstruction.cs @@ -2,9 +2,10 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.Symbols; -namespace Draco.Compiler.Internal.OptimizingIr.Model; +namespace Draco.Compiler.Internal.OptimizingIr.Instructions; /// /// Represents a tree-ified instruction, that's only part of the stackification process. diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/BasicBlock.cs b/src/Draco.Compiler/Internal/OptimizingIr/Model/BasicBlock.cs index 9a5d53e5f..036b23508 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/BasicBlock.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Model/BasicBlock.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.Symbols; namespace Draco.Compiler.Internal.OptimizingIr.Model; diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/CallInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Model/CallInstruction.cs deleted file mode 100644 index 58dbcd31b..000000000 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/CallInstruction.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Draco.Compiler.Internal.Symbols; - -namespace Draco.Compiler.Internal.OptimizingIr.Model; - -/// -/// A procedure call. -/// -internal sealed class CallInstruction( - Register target, - FunctionSymbol procedure, - IEnumerable arguments) : InstructionBase, IValueInstruction -{ - public override string InstructionKeyword => "call"; - - public Register Target { get; set; } = target; - - /// - /// The called procedure. - /// - public FunctionSymbol Procedure { get; set; } = procedure; - - /// - /// The arguments that are passed to the procedure. - /// - public IList Arguments { get; set; } = arguments.ToList(); - - public override IEnumerable StaticOperands => [this.Procedure]; - public override IEnumerable Operands => this.Arguments; - - public override string ToString() => - $"{this.Target.ToOperandString()} := {this.InstructionKeyword} [{this.Procedure.FullName}]({string.Join(", ", this.Arguments.Select(a => a.ToOperandString()))})"; - - public override CallInstruction Clone() => new(this.Target, this.Procedure, this.Arguments); -} diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/IBasicBlock.cs b/src/Draco.Compiler/Internal/OptimizingIr/Model/IBasicBlock.cs index 5608f5256..305523511 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/IBasicBlock.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Model/IBasicBlock.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.Symbols; namespace Draco.Compiler.Internal.OptimizingIr.Model; diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Model/MemberCallInstruction.cs b/src/Draco.Compiler/Internal/OptimizingIr/Model/MemberCallInstruction.cs deleted file mode 100644 index 107de4fc7..000000000 --- a/src/Draco.Compiler/Internal/OptimizingIr/Model/MemberCallInstruction.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using Draco.Compiler.Internal.Symbols; - -namespace Draco.Compiler.Internal.OptimizingIr.Model; - -/// -/// A procedure call on a member. -/// -internal sealed class MemberCallInstruction( - Register target, - FunctionSymbol procedure, - IOperand receiver, - IEnumerable arguments) : InstructionBase, IValueInstruction -{ - public override string InstructionKeyword => "membercall"; - - public Register Target { get; set; } = target; - - /// - /// The called procedure. - /// - public FunctionSymbol Procedure { get; set; } = procedure; - - /// - /// The receiver the method is called on. - /// - public IOperand Receiver { get; set; } = receiver; - - /// - /// The arguments that are passed to the procedure. - /// - public IList Arguments { get; set; } = arguments.ToList(); - - public override IEnumerable StaticOperands => [this.Procedure]; - public override IEnumerable Operands => this.Arguments.Prepend(this.Receiver); - - public override string ToString() => - $"{this.Target.ToOperandString()} := {this.InstructionKeyword} {this.Receiver.ToOperandString()}.[{this.Procedure.FullName}]({string.Join(", ", this.Arguments.Select(a => a.ToOperandString()))})"; - - public override MemberCallInstruction Clone() => new(this.Target, this.Procedure, this.Receiver, this.Arguments); -} diff --git a/src/Draco.Compiler/Internal/OptimizingIr/PassFactory.cs b/src/Draco.Compiler/Internal/OptimizingIr/PassFactory.cs index 638ab19fd..3270f9141 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/PassFactory.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/PassFactory.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; using Draco.Compiler.Internal.OptimizingIr.Passes; diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Passes/JumpThreading.cs b/src/Draco.Compiler/Internal/OptimizingIr/Passes/JumpThreading.cs index 4ee8b452c..b609ff0c3 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Passes/JumpThreading.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Passes/JumpThreading.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; namespace Draco.Compiler.Internal.OptimizingIr.Passes; diff --git a/src/Draco.Compiler/Internal/OptimizingIr/Stackifier.cs b/src/Draco.Compiler/Internal/OptimizingIr/Stackifier.cs index be8e6df96..00979962a 100644 --- a/src/Draco.Compiler/Internal/OptimizingIr/Stackifier.cs +++ b/src/Draco.Compiler/Internal/OptimizingIr/Stackifier.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; +using Draco.Compiler.Internal.OptimizingIr.Instructions; using Draco.Compiler.Internal.OptimizingIr.Model; namespace Draco.Compiler.Internal.OptimizingIr; diff --git a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataMethodSymbol.cs b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataMethodSymbol.cs index 9213dfb52..8c74a874b 100644 --- a/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataMethodSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/Metadata/MetadataMethodSymbol.cs @@ -57,6 +57,7 @@ public override bool IsVirtual { get { + if (this.IsStatic) return false; if (this.ContainingSymbol is TypeSymbol { IsValueType: true }) return false; return methodDefinition.Attributes.HasFlag(MethodAttributes.Virtual) || this.Override is not null; diff --git a/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs b/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs index 279865af4..3dd11cff8 100644 --- a/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs +++ b/src/Draco.Compiler/Internal/Symbols/TypeSymbol.cs @@ -135,6 +135,7 @@ public override bool CanBeShadowedBy(Symbol other) where T : Symbol, IOverridableSymbol => this.BaseTypes .SelectMany(x => x.DefinedMembers) .OfType() + .Except([@override]) .FirstOrDefault(x => x.CanBeOverriddenBy(@override)); private ImmutableArray BuildMembers()