Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP-Method Scanner #575

Merged
merged 11 commits into from Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions Source/Mosa.Compiler.Common/HasSetExtension.cs
@@ -0,0 +1,17 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections.Generic;

namespace Mosa.Compiler.Common
{
public static class HasSetExtension
{
public static void AddIfNew<T>(this HashSet<T> hashSet, T item)
{
if (hashSet.Contains(item))
return;

hashSet.Add(item);
}
}
}
21 changes: 0 additions & 21 deletions Source/Mosa.Compiler.Common/ListExtension.cs
Expand Up @@ -6,13 +6,6 @@ namespace Mosa.Compiler.Common
{
public static class ListExtension
{
/// <summary>
/// Adds if new.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <param name="item">The item.</param>
/// <returns></returns>
public static void AddIfNew<T>(this List<T> list, T item)
{
if (list.Contains(item))
Expand All @@ -21,20 +14,6 @@ public static void AddIfNew<T>(this List<T> list, T item)
list.Add(item);
}

public static void AddIfNew<T>(this HashSet<T> list, T item)
{
if (list.Contains(item))
return;

list.Add(item);
}

/// <summary>
/// Determines whether the two lists' elements are equal.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">The list.</param>
/// <param name="other">The other list.</param>
public static bool SequenceEquals<T>(this IList<T> list, IList<T> other)
{
if (list.Count != other.Count)
Expand Down
1 change: 1 addition & 0 deletions Source/Mosa.Compiler.Common/Mosa.Compiler.Common.csproj
Expand Up @@ -46,6 +46,7 @@
<Compile Include="Bits.cs" />
<Compile Include="BitArrayExtension.cs" />
<Compile Include="Exceptions\AssemblyLoadException.cs" />
<Compile Include="HasSetExtension.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SortedSetExtension.cs" />
<Compile Include="Exceptions\NotImplementCompilerException.cs" />
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Expand Up @@ -114,7 +114,7 @@ public abstract class BaseMethodCompilerStage : ITraceFactory
/// <summary>
/// Gets the method data.
/// </summary>
protected CompilerMethodData MethodData { get { return MethodCompiler.MethodData; } }
protected MethodData MethodData { get { return MethodCompiler.MethodData; } }

/// <summary>
/// Gets the linker.
Expand Down
14 changes: 8 additions & 6 deletions Source/Mosa.Compiler.Framework/CIL/IInstructionDecoder.cs
Expand Up @@ -20,17 +20,19 @@ public interface IInstructionDecoder
MosaMethod Method { get; }

/// <summary>
/// Gets the instruction being decoded.
/// Gets the type system.
/// </summary>
MosaInstruction Instruction { get; }
TypeSystem TypeSystem { get; }

/// <summary>
/// Gets the type system.
/// </summary>
/// <value>
/// The type system.
/// </value>
TypeSystem TypeSystem { get; }
MethodScanner MethodScanner { get; }

/// <summary>
/// Gets the instruction being decoded.
/// </summary>
MosaInstruction Instruction { get; }

/// <summary>
/// Gets the block.
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/InvokeInstruction.cs
Expand Up @@ -114,8 +114,6 @@ protected static MosaMethod DecodeInvocationTarget(InstructionNode node, IInstru
{
var method = (MosaMethod)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackMethodInvoked(method);

node.InvokeMethod = method;

// Fix the parameter list
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/LdfldInstruction.cs
Expand Up @@ -37,8 +37,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

node.Result = AllocateVirtualRegisterOrStackSlot(decoder.MethodCompiler, field.FieldType);
node.MosaField = field;
}
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/LdfldaInstruction.cs
Expand Up @@ -36,8 +36,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

node.MosaField = field;
node.Result = AllocateVirtualRegisterOrStackSlot(decoder.MethodCompiler, field.FieldType.ToManagedPointer());
}
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/LdftnInstruction.cs
Expand Up @@ -36,8 +36,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var method = (MosaMethod)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackMethodInvoked(method);

node.Result = decoder.MethodCompiler.CreateVirtualRegister(decoder.TypeSystem.ToFnPtr(method.Signature));
node.InvokeMethod = method;
}
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/LdsfldInstruction.cs
Expand Up @@ -38,8 +38,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

Debug.Assert(field.IsStatic, "Static field access on non-static field.");

node.MosaField = field;
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/LdsfldaInstruction.cs
Expand Up @@ -31,8 +31,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

node.MosaField = field;
node.Result = decoder.MethodCompiler.CreateVirtualRegister(field.FieldType.ToManagedPointer());
}
Expand Down
3 changes: 0 additions & 3 deletions Source/Mosa.Compiler.Framework/CIL/NewobjInstruction.cs
Expand Up @@ -90,9 +90,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)
// Remove the this argument from the invocation, it's not on the stack yet.
node.OperandCount--;

decoder.MethodCompiler.Scheduler.TrackTypeAllocated(ctor.DeclaringType);
decoder.MethodCompiler.Scheduler.TrackTypeAllocated(node.InvokeMethod.DeclaringType);

// Set a return value according to the type of the object allocated
node.Result = decoder.MethodCompiler.AllocateVirtualRegisterOrStackSlot(ctor.DeclaringType);
node.ResultCount = 1;
Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/StfldInstruction.cs
Expand Up @@ -37,8 +37,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

node.MosaField = field;
}

Expand Down
2 changes: 0 additions & 2 deletions Source/Mosa.Compiler.Framework/CIL/StsfldInstruction.cs
Expand Up @@ -37,8 +37,6 @@ public override void Decode(InstructionNode node, IInstructionDecoder decoder)

var field = (MosaField)decoder.Instruction.Operand;

decoder.MethodCompiler.Scheduler.TrackFieldReferenced(field);

node.MosaField = field;
}

Expand Down
19 changes: 2 additions & 17 deletions Source/Mosa.Compiler.Framework/CompilationScheduler.cs
Expand Up @@ -17,7 +17,7 @@ public sealed class CompilationScheduler
private readonly UniqueQueueThreadSafe<MosaMethod> queue = new UniqueQueueThreadSafe<MosaMethod>();
private readonly HashSet<MosaMethod> methods = new HashSet<MosaMethod>();

private readonly UniqueQueueThreadSafe<CompilerMethodData> inlineQueue = new UniqueQueueThreadSafe<CompilerMethodData>();
private readonly UniqueQueueThreadSafe<MethodData> inlineQueue = new UniqueQueueThreadSafe<MethodData>();

#endregion Data Members

Expand Down Expand Up @@ -103,30 +103,15 @@ public bool IsScheduled(MosaMethod method)
}
}

public void TrackTypeAllocated(MosaType type)
{
}

public void TrackMethodInvoked(MosaMethod method)
{
}

public void TrackFieldReferenced(MosaField field)
{
// TODO
}

public MosaMethod GetMethodToCompile()
{
return queue.Dequeue();
}

public void AddToInlineQueue(CompilerMethodData methodData)
public void AddToInlineQueue(MethodData methodData)
{
Debug.Assert(!methodData.Method.HasOpenGenericParams);

//Debug.Assert(!methodData.Method.IsLinkerGenerated);

inlineQueue.Enqueue(methodData);
}

Expand Down
8 changes: 8 additions & 0 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Expand Up @@ -64,6 +64,11 @@ public sealed class Compiler
/// <value>The compiler options.</value>
public CompilerOptions CompilerOptions { get; }

/// <summary>
/// Gets the method scanner.
/// </summary>
public MethodScanner MethodScanner { get; }

/// <summary>
/// Gets the counters.
/// </summary>
Expand Down Expand Up @@ -126,6 +131,7 @@ private static List<BaseCompilerStage> GetDefaultCompilerPipeline(CompilerOption
{
return new List<BaseCompilerStage> {
new TypeInitializerSchedulerStage(),
new StaticFieldStage(),
new MethodLookupTableStage(),
new MethodExceptionLookupTableStage(),
new MetadataStage(),
Expand Down Expand Up @@ -207,6 +213,8 @@ public Compiler(MosaCompiler mosaCompiler)
CompilationScheduler = mosaCompiler.CompilationScheduler;
Linker = mosaCompiler.Linker;

MethodScanner = new MethodScanner(this);

CompilerExtensions.AddRange(mosaCompiler.CompilerExtensions);

methodStagePipelines = new Pipeline<BaseMethodCompilerStage>[mosaCompiler.MaxThreads];
Expand Down
18 changes: 9 additions & 9 deletions Source/Mosa.Compiler.Framework/CompilerData.cs
Expand Up @@ -12,13 +12,13 @@ public sealed class CompilerData
{
#region Data Members

private readonly Dictionary<MosaType, CompilerTypeData> types = new Dictionary<MosaType, CompilerTypeData>();
private readonly Dictionary<MosaType, TypeData> types = new Dictionary<MosaType, TypeData>();

private readonly Dictionary<MosaMethod, CompilerMethodData> methods = new Dictionary<MosaMethod, CompilerMethodData>();
private readonly Dictionary<MosaMethod, MethodData> methods = new Dictionary<MosaMethod, MethodData>();

#endregion Data Members

public IEnumerable<CompilerMethodData> MethodData
public IEnumerable<MethodData> MethodData
{
get
{
Expand All @@ -31,27 +31,27 @@ public IEnumerable<CompilerMethodData> MethodData

#region Methods

public CompilerTypeData GetCompilerTypeData(MosaType type)
public TypeData GetTypeData(MosaType type)
{
lock (types)
{
if (!types.TryGetValue(type, out CompilerTypeData compilerType))
if (!types.TryGetValue(type, out TypeData compilerType))
{
compilerType = new CompilerTypeData(type);
compilerType = new TypeData(type);
types.Add(type, compilerType);
}

return compilerType;
}
}

public CompilerMethodData GetCompilerMethodData(MosaMethod method)
public MethodData GetMethodData(MosaMethod method)
{
lock (methods)
{
if (!methods.TryGetValue(method, out CompilerMethodData compilerMethod))
if (!methods.TryGetValue(method, out MethodData compilerMethod))
{
compilerMethod = new CompilerMethodData(method);
compilerMethod = new MethodData(method);
methods.Add(method, compilerMethod);
}

Expand Down
6 changes: 6 additions & 0 deletions Source/Mosa.Compiler.Framework/CompilerOptions.cs
Expand Up @@ -141,6 +141,11 @@ public class CompilerOptions
/// </summary>
public List<string> SourceFiles { get; set; } = new List<string>();

/// <summary>
/// Gets or sets a value indicating whether [enable method scanner].
/// </summary>
public bool EnableMethodScanner { get; set; }

/// <summary>
/// Adds additional sections to the Elf-File.
/// </summary>
Expand Down Expand Up @@ -311,6 +316,7 @@ public CompilerOptions()
EnableValueNumbering = true;
EnableLoopInvariantCodeMotion = true;
EnablePlatformOptimizations = true;
EnableMethodScanner = false;
}
}
}
Expand Up @@ -132,7 +132,7 @@ private void EmitMethods()
foreach (var method in type.Methods)
{
var symbol = Linker.GetSymbol(method.FullName);
var methodData = Compiler.CompilerData.GetCompilerMethodData(method);
var methodData = Compiler.CompilerData.GetMethodData(method);

writer.WriteLine(
"{0}\t{1:x8}\t{2}\t{3:x8}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}",
Expand Down
27 changes: 5 additions & 22 deletions Source/Mosa.Compiler.Framework/CompilerStages/MetadataStage.cs
Expand Up @@ -29,6 +29,9 @@ protected override void Setup()

protected override void RunPostCompile()
{
//if (CompilerOptions.EnableMethodScanner) // FIXME: Temp - REMOVE ME!!!
// return; // FIXME: Temp - REMOVE ME!!!

CreateDefinitionTables();
}

Expand Down Expand Up @@ -186,7 +189,7 @@ private LinkerSymbol CreateTypeDefinition(MosaType type, LinkerSymbol assemblyTa
// 9. Constructor that accepts no parameters, if any, for this type
foreach (var method in type.Methods)
{
if (!method.Name.Equals(".ctor") || method.Signature.Parameters.Count != 0 || method.HasOpenGenericParams)
if (!method.IsConstructor || method.Signature.Parameters.Count != 0 || method.HasOpenGenericParams)
continue;

Linker.Link(LinkType.AbsoluteAddress, NativePatchType, typeTableSymbol, (int)writer1.Position, Metadata.MethodDefinition + method.FullName, 0);
Expand Down Expand Up @@ -449,26 +452,6 @@ private LinkerSymbol CreateFieldDefinitions(MosaType type)
writer2.Write(TypeLayout.GetFieldOffset(field), TypeLayout.NativePointerSize);
}

// Create another symbol with field data if any
if (field.IsStatic)
{
// Assign a memory slot to the static & initialize it, if there's initial data set
// Determine the size of the type & alignment requirements
//Architecture.GetTypeRequirements(TypeLayout, field.FieldType, out int size, out int alignment);

int size = TypeLayout.GetFieldSize(field);

// The linker section to move this field into
var section = field.Data != null ? SectionKind.ROData : SectionKind.BSS;

var symbol = Compiler.Linker.DefineSymbol(field.FullName, section, Architecture.NativeAlignment, size);

if (field.Data != null)
{
symbol.Stream.Write(field.Data, 0, size);
}
}

// Add pointer to field list
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, fieldsTableSymbol, (int)writer1.Position, fieldDefSymbol, 0);
writer1.WriteZeroBytes(TypeLayout.NativePointerSize);
Expand Down Expand Up @@ -583,7 +566,7 @@ private LinkerSymbol CreateMethodDefinition(MosaMethod method)
writer1.Write((uint)method.MethodAttributes, TypeLayout.NativePointerSize);

// 4. Local Stack Size (16 Bits) && Parameter Stack Size (16 Bits)
var methodData = Compiler.CompilerData.GetCompilerMethodData(method);
var methodData = Compiler.CompilerData.GetMethodData(method);
int value = methodData.LocalMethodStackSize | (methodData.ParameterStackSize << 16);
writer1.Write(value, TypeLayout.NativePointerSize);

Expand Down
Expand Up @@ -28,6 +28,9 @@ protected override void Setup()

protected override void RunPostCompile()
{
//if (CompilerOptions.EnableMethodScanner) // FIXME: Temp - REMOVE ME!!!
// return; // FIXME: Temp - REMOVE ME!!!

// Emit assembly list
var methodLookupTable = Linker.DefineSymbol(Metadata.MethodLookupTable, SectionKind.ROData, TypeLayout.NativePointerAlignment, 0);
var writer = new EndianAwareBinaryWriter(methodLookupTable.Stream, Architecture.Endianness);
Expand Down