Skip to content

Commit

Permalink
Slight refactoring of codegen structure (#460)
Browse files Browse the repository at this point in the history
* Moved instructions

* More

* Fix
  • Loading branch information
LPeter1997 authored Sep 1, 2024
1 parent 24b9b33 commit b825016
Show file tree
Hide file tree
Showing 39 changed files with 116 additions and 131 deletions.
20 changes: 4 additions & 16 deletions src/Draco.Compiler/Internal/Codegen/CilCodegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -145,7 +146,7 @@ IEnumerable<IOperand> RemainingOperands()

switch (instruction)
{
case OptimizingIr.Model.SequencePoint sp:
case OptimizingIr.Instructions.SequencePoint sp:
{
this.PdbCodegen?.AddSequencePoint(this.InstructionEncoder, sp);
break;
Expand Down Expand Up @@ -357,28 +358,15 @@ IEnumerable<IOperand> 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
Expand Down
2 changes: 1 addition & 1 deletion src/Draco.Compiler/Internal/Codegen/PdbCodegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 2 additions & 8 deletions src/Draco.Compiler/Internal/OptimizingIr/FunctionBodyCodegen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<IOperand> args) =>
new(target, proc, args);
public static MemberCallInstruction MemberCall(Register target, FunctionSymbol proc, IOperand receiver, IEnumerable<IOperand> args) =>
public static CallInstruction Call(Register target, FunctionSymbol proc, IOperand? receiver, IEnumerable<IOperand> args) =>
new(target, proc, receiver, args);
public static CallInstruction Call(Register target, FunctionSymbol proc, IEnumerable<IOperand> args) =>
Call(target, proc, null, args);
public static NewObjectInstruction NewObject(Register target, FunctionSymbol ctor, IEnumerable<IOperand> args) =>
new(target, ctor, args);
public static NewDelegateInstruction NewDelegate(Register target, IOperand? receiver, FunctionSymbol function, FunctionSymbol delegateCtor) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Loads the address of some local/global/argument.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Represents some kind of binary arithmetic instruction.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Draco.Compiler.Internal.OptimizingIr.Model;
namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// The different arithmetic operations supported.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An array length query.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Valuetype element boxing.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A conditional jump.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A procedure call.
/// </summary>
internal sealed class CallInstruction(
Register target,
FunctionSymbol procedure,
IOperand? receiver,
IEnumerable<IOperand> arguments) : InstructionBase, IValueInstruction
{
public override string InstructionKeyword => "call";

public Register Target { get; set; } = target;

/// <summary>
/// The called procedure.
/// </summary>
public FunctionSymbol Procedure { get; set; } = procedure;

/// <summary>
/// The receiver the method is called on.
/// </summary>
public IOperand? Receiver { get; set; } = receiver;

/// <summary>
/// The arguments that are passed to the procedure.
/// </summary>
public IList<IOperand> Arguments { get; set; } = arguments.ToList();

public override IEnumerable<Symbol> StaticOperands => [this.Procedure];
public override IEnumerable<IOperand> 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);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Draco.Compiler.Internal.OptimizingIr.Model;
namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// A pseudo-instruction for representing the end of a local scope.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Read-only interface of an instruction.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Draco.Compiler.Internal.OptimizingIr.Model;
using Draco.Compiler.Internal.OptimizingIr.Model;

namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// An instruction that produces a result in a register.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A mutable base class for <see cref="IInstruction"/> implementations.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An unconditional jump.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An array element access.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A field access.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Loads a value from a local/global/argument.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An array instantiation.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// A delegate instantiation.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// An object instantiation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Draco.Compiler.Internal.OptimizingIr.Model;
namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// Represents no operation.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Returns from the current procedure.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text;
using Draco.Compiler.Api.Syntax;

namespace Draco.Compiler.Internal.OptimizingIr.Model;
namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// A pseudo-instruction for representing sequence points.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using Draco.Compiler.Internal.Symbols;

namespace Draco.Compiler.Internal.OptimizingIr.Model;
namespace Draco.Compiler.Internal.OptimizingIr.Instructions;

/// <summary>
/// A pseudo-instruction for representing the start of a local scope.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Stores a value in an array element.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Stores a value in a field.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Stores a value in a local/global/argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// Represents a tree-ified instruction, that's only part of the stackification process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit b825016

Please sign in to comment.