Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuinox committed Sep 23, 2024
1 parent c1b941e commit 242d9b8
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 6 deletions.
17 changes: 17 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/Field.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Draco.Compiler.Internal.Symbols;

namespace Draco.Compiler.Internal.OptimizingIr.Model;
internal class Field(FieldSymbol symbol, Type declaringType) : IField
{
public FieldSymbol Symbol { get; } = symbol;

public string Name => this.Symbol.Name;

public Type DeclaringType { get; } = declaringType;
IType IField.DeclaringType => this.DeclaringType;

public IReadOnlyList<AttributeInstance> Attributes => this.Symbol.Attributes;

public TypeSymbol Type => this.Symbol.Type;
}
32 changes: 32 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/IField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Draco.Compiler.Internal.Symbols;

namespace Draco.Compiler.Internal.OptimizingIr.Model;

/// <summary>
/// Read-only interface of a field.
/// </summary>
internal interface IField
{
public FieldSymbol Symbol { get; }

/// <summary>
/// The name of this field.
/// </summary>
public string Name { get; }

/// <summary>
/// The type this procedure is defined in.
/// </summary>
public IType DeclaringType { get; }

/// <summary>
/// The attributes on this field.
/// </summary>
public IReadOnlyList<AttributeInstance> Attributes { get; }

/// <summary>
/// The type of this field.
/// </summary>
public TypeSymbol Type { get; }
}
6 changes: 6 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/IModule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using Draco.Compiler.Internal.Symbols;
using Draco.Compiler.Internal.Symbols.Source;

namespace Draco.Compiler.Internal.OptimizingIr.Model;

Expand Down Expand Up @@ -43,6 +44,11 @@ internal interface IModule
/// </summary>
public IReadOnlyDictionary<FunctionSymbol, IProcedure> Procedures { get; }

/// <summary>
/// The compiled types within this module.
/// </summary>
public IReadOnlyDictionary<TypeSymbol, IType> Types { get; }

/// <summary>
/// The procedure performing global initialization.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/IProcedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ internal interface IProcedure
/// </summary>
public string Name { get; }

/// <summary>
/// The type this procedure is defined in.
/// When <see langword="null"/>, this procedure is a free function.
/// </summary>
public IType? DeclaringType { get; }

/// <summary>
/// The module this procedure is defined in.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/IType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Draco.Compiler.Internal.Symbols;

namespace Draco.Compiler.Internal.OptimizingIr.Model;

internal interface IType
{
/// <summary>
/// The name of this type.
/// </summary>
public string Name { get; }

/// <summary>
/// The module this type is defined in.
/// </summary>
public IModule DeclaringModule { get; }

/// <summary>
/// The assembly this type is defined in.
/// </summary>
public IAssembly Assembly { get; }

/// <summary>
/// The generic parameters on this type.
/// </summary>
public IReadOnlyList<TypeParameterSymbol> Generics { get; }

/// <summary>
/// The methods on this type.
/// </summary>
public IReadOnlyDictionary<FunctionSymbol, IProcedure> Methods { get; }

/// <summary>
/// The fields on this type.
/// </summary>
public IReadOnlyDictionary<FieldSymbol, IField> Fields { get; }
}
6 changes: 5 additions & 1 deletion src/Draco.Compiler/Internal/OptimizingIr/Model/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ internal sealed class Module : IModule
public IDictionary<ModuleSymbol, IModule> Submodules => this.submodules;
IReadOnlyDictionary<ModuleSymbol, IModule> IModule.Submodules => this.submodules;

public IDictionary<TypeSymbol, IType> Types => this.types;
IReadOnlyDictionary<TypeSymbol, IType> IModule.Types => this.types;

public IReadOnlySet<GlobalSymbol> Globals => this.globals;

public Procedure GlobalInitializer { get; }
Expand All @@ -35,6 +38,7 @@ internal sealed class Module : IModule
private readonly HashSet<GlobalSymbol> globals = [];
private readonly Dictionary<FunctionSymbol, IProcedure> procedures = [];
private readonly Dictionary<ModuleSymbol, IModule> submodules = [];
private readonly Dictionary<TypeSymbol, IType> types = [];

public Module(ModuleSymbol symbol, Assembly assembly, Module? Parent)
{
Expand Down Expand Up @@ -64,7 +68,7 @@ public Procedure DefineProcedure(FunctionSymbol functionSymbol)
{
if (!this.procedures.TryGetValue(functionSymbol, out var result))
{
result = new Procedure(this, functionSymbol);
result = new Procedure(this, null, functionSymbol);
this.procedures.Add(functionSymbol, result);
}
return (Procedure)result;
Expand Down
7 changes: 5 additions & 2 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/Procedure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ internal sealed class Procedure : IProcedure
{
public FunctionSymbol Symbol { get; }
public string Name => this.Symbol.NestedName;
public TypeSymbol? Type => this.Symbol.Type;
public TypeSymbol? TypeSymbol => this.Symbol.Type;
public Module DeclaringModule { get; }
IModule IProcedure.DeclaringModule => this.DeclaringModule;
public Type? DeclaringType { get; }
IType? IProcedure.DeclaringType => this.DeclaringType;
public Assembly Assembly => this.DeclaringModule.Assembly;
IAssembly IProcedure.Assembly => this.Assembly;
public BasicBlock Entry { get; }
Expand All @@ -35,9 +37,10 @@ internal sealed class Procedure : IProcedure
private readonly List<LocalSymbol> locals = [];
private readonly List<Register> registers = [];

public Procedure(Module declaringModule, FunctionSymbol symbol)
public Procedure(Module declaringModule, Type? declaringType, FunctionSymbol symbol)
{
this.DeclaringModule = declaringModule;
this.DeclaringType = declaringType;
this.Symbol = symbol;
this.Entry = this.DefineBasicBlock(new SynthetizedLabelSymbol("begin"));
}
Expand Down
42 changes: 42 additions & 0 deletions src/Draco.Compiler/Internal/OptimizingIr/Model/Type.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using Draco.Compiler.Internal.Symbols;

namespace Draco.Compiler.Internal.OptimizingIr.Model;
internal class Type(Module declaringModule, TypeSymbol symbol) : IType
{

public TypeSymbol Symbol { get; } = symbol;
public string Name => this.Symbol.Name;

public Module DeclaringModule { get; } = declaringModule;
IModule IType.DeclaringModule => this.DeclaringModule;
public Assembly Assembly => this.DeclaringModule.Assembly;
IAssembly IType.Assembly => this.Assembly;

public IReadOnlyList<TypeParameterSymbol> Generics => this.Symbol.GenericParameters;
public IReadOnlyDictionary<FunctionSymbol, IProcedure> Methods => this.methods;
public IReadOnlyDictionary<FieldSymbol, IField> Fields => this.fields;

private readonly Dictionary<FunctionSymbol, IProcedure> methods = [];
private readonly Dictionary<FieldSymbol, IField> fields = [];

public Procedure DefineMethod(FunctionSymbol functionSymbol)
{
if (!this.methods.TryGetValue(functionSymbol, out var result))
{
result = new Procedure(this.DeclaringModule, this, functionSymbol);
this.methods.Add(functionSymbol, result);
}
return (Procedure)result;
}

public Field DefineField(FieldSymbol fieldSymbol)
{
if (!this.fields.TryGetValue(fieldSymbol, out var result))
{
result = new Field(fieldSymbol, this);
this.fields.Add(fieldSymbol, result);
}
return (Field)result;
}
}
2 changes: 1 addition & 1 deletion src/Draco.Compiler/Internal/Symbols/AttributeInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private bool TypesMatch(System.Type reflType, TypeSymbol internalType)
var wellKnownTypes = this.Constructor.DeclaringCompilation?.WellKnownTypes;
if (wellKnownTypes is null) throw new System.NotImplementedException();

var translatedReflType = wellKnownTypes.TranslatePrmitive(reflType);
var translatedReflType = wellKnownTypes.TranslatePrimitive(reflType);
if (translatedReflType is null) throw new System.NotImplementedException();

return SymbolEqualityComparer.Default.Equals(translatedReflType, internalType);
Expand Down
1 change: 1 addition & 0 deletions src/Draco.Compiler/Internal/Symbols/SymbolVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public virtual void VisitFunction(FunctionSymbol functionSymbol)

public virtual void VisitType(TypeSymbol typeSymbol)
{
foreach (var genericParam in typeSymbol.GenericParameters) genericParam.Accept(this);
foreach (var member in typeSymbol.Members) member.Accept(this);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Draco.Compiler/Internal/Symbols/WellKnownTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public IEnumerable<Symbol> GetEnumEqualityMembers(TypeSymbol type)
/// </summary>
/// <param name="type">The reflected type to translate.</param>
/// <returns>The translated type symbol, or <see langword="null"/> if it's not a translatable primitive type.</returns>
public TypeSymbol? TranslatePrmitive(Type type)
public TypeSymbol? TranslatePrimitive(System.Type type)
{
if (type == typeof(byte)) return this.SystemByte;
if (type == typeof(ushort)) return this.SystemUInt16;
Expand All @@ -224,7 +224,7 @@ public IEnumerable<Symbol> GetEnumEqualityMembers(TypeSymbol type)
if (type == typeof(string)) return this.SystemString;
if (type == typeof(object)) return this.SystemObject;

if (type == typeof(Type)) return this.SystemType;
if (type == typeof(System.Type)) return this.SystemType;

return null;
}
Expand Down

0 comments on commit 242d9b8

Please sign in to comment.