Skip to content

Commit

Permalink
Merge pull request #596 from tgiphil/010-LinkerUpdate
Browse files Browse the repository at this point in the history
Linker Updates
  • Loading branch information
tgiphil committed Feb 10, 2019
2 parents 39286e5 + c331593 commit 09d5b2f
Show file tree
Hide file tree
Showing 41 changed files with 245 additions and 153 deletions.
19 changes: 19 additions & 0 deletions Source/Mosa.Compiler.Common/StopwatchExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;

namespace Mosa.Compiler.Common
{
public static class StopwatchExtension
{
public static long ElapsedNanoSeconds(this Stopwatch watch)
{
return watch.ElapsedTicks * 1000000000 / Stopwatch.Frequency;
}

public static long ElapsedMicroSeconds(this Stopwatch watch)
{
return watch.ElapsedTicks * 1000000 / Stopwatch.Frequency;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class DwarfCompilerExtension : BaseCompilerExtension
{
public override void ExtendCompilerPipeline(Pipeline<BaseCompilerStage> pipeline)
{
pipeline.InsertBefore<LinkerFinalizationStage>(new DwarfCompilerStage());
pipeline.InsertBefore<LinkerEmitStage>(new DwarfCompilerStage());
}

public override void ExtendMethodCompilerPipeline(Pipeline<BaseMethodCompilerStage> pipeline)
Expand Down
15 changes: 5 additions & 10 deletions Source/Mosa.Compiler.Extension.Dwarf/DwarfCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using Mosa.Compiler.Common;
using Mosa.Compiler.Framework;
using Mosa.Compiler.Framework.Linker;
using Mosa.Compiler.Framework.Linker.Elf;
using Mosa.Compiler.Framework.Source;
using System;
Expand All @@ -13,14 +14,6 @@ namespace Mosa.Compiler.Extensions.Dwarf
{
internal class DwarfCompilerStage : BaseCompilerStage
{
protected override void Setup()
{
}

protected override void RunPreCompile()
{
}

protected override void RunPostCompile()
{
if (Linker.CreateExtraSections == null)
Expand Down Expand Up @@ -53,12 +46,14 @@ private void EmitDebugInfo(EndianAwareBinaryWriter wr)

var context = new DwarfWriteContext { Writer = wr, AbbrevList = AbbrevList };

var textSection = Compiler.Linker.Sections[(int)SectionKind.Text];

// Debugging Information Entry
var cu = new DwarfCompilationUnit
{
Producer = "Mosa Compiler",
ProgramCounterLow = (uint)Compiler.CompilerOptions.BaseAddress,
ProgramCounterHigh = 0x00600000,
ProgramCounterLow = (uint)textSection.VirtualAddress,
ProgramCounterHigh = (uint)textSection.VirtualAddress + textSection.Size
};
cu.Emit(context);

Expand Down
4 changes: 1 addition & 3 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,6 @@ protected MosaExceptionHandler FindNextEnclosingFinallyContext(MosaExceptionHand

protected MosaExceptionHandler FindFinallyExceptionContext(InstructionNode node)
{
MosaExceptionHandler innerClause = null;

int label = node.Label;

foreach (var handler in MethodCompiler.Method.ExceptionHandlers)
Expand Down Expand Up @@ -718,7 +716,7 @@ protected void NewCompilerTraceEvent(CompilerEvent compileEvent, string message)
/// <param name="counter">The counter.</param>
public void UpdateCounter(Counter counter)
{
MethodData.Counters.UpdateNoLock(counter.Name, counter.Count);
MethodData.Counters.UpdateSkipLock(counter.Name, counter.Count);
}

/// <summary>
Expand Down
31 changes: 15 additions & 16 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,15 @@ public sealed class Compiler
private static List<BaseCompilerStage> GetDefaultCompilerPipeline(CompilerOptions compilerOptions)
{
return new List<BaseCompilerStage> {
new TypeInitializerSchedulerStage(),
new TypeInitializerStage(),
new StaticFieldStage(),
new MethodLookupTableStage(),
new MethodExceptionLookupTableStage(),
new MethodTableStage(),
new ExceptionTableStage(),
new MetadataStage(),
(compilerOptions.OutputFile != null && compilerOptions.EmitBinary) ? new LinkerFinalizationStage() : null,
(compilerOptions.MapFile != null) ? new MapFileGenerationStage() : null,
(compilerOptions.DebugFile != null) ? new DebugFileGenerationStage() : null
new LinkerLayoutStage(),
(compilerOptions.OutputFile != null && compilerOptions.EmitBinary) ? new LinkerEmitStage() : null,
(compilerOptions.MapFile != null) ? new MapFileStage() : null,
(compilerOptions.DebugFile != null) ? new DebugFileStage() : null
};
}

Expand All @@ -160,9 +161,6 @@ private static List<BaseMethodCompilerStage> GetDefaultMethodPipeline(CompilerOp
new PromoteTemporaryVariables(),
(compilerOptions.EnableSSA) ? new EdgeSplitStage() : null,

//new DominanceOutputStage(),
//new StopStage(),
//new GraphVizStage(),
//new PreciseGCStage(),

(compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
Expand Down Expand Up @@ -267,18 +265,21 @@ public void CompileMethod(MosaMethod method, BasicBlocks basicBlocks, int thread
{
NewCompilerTraceEvent(CompilerEvent.CompilingMethod, method.FullName, threadID);

var methodCompiler = GetMethodCompiler(method, basicBlocks, threadID);
var pipeline = GetOrCreateMethodStagePipeline(threadID);

var methodCompiler = new MethodCompiler(this, method, basicBlocks, threadID)
{
Pipeline = pipeline
};

methodCompiler.Compile();

NewCompilerTraceEvent(CompilerEvent.CompiledMethod, method.FullName, threadID);
CompilerTrace.TraceListener.OnMethodCompiled(method);
}

private MethodCompiler GetMethodCompiler(MosaMethod method, BasicBlocks basicBlocks, int threadID = 0)
private Pipeline<BaseMethodCompilerStage> GetOrCreateMethodStagePipeline(int threadID)
{
var methodCompiler = new MethodCompiler(this, method, basicBlocks, threadID);

var pipeline = methodStagePipelines[threadID];

if (pipeline == null)
Expand All @@ -302,9 +303,7 @@ private MethodCompiler GetMethodCompiler(MosaMethod method, BasicBlocks basicBlo
}
}

methodCompiler.Pipeline = pipeline;

return methodCompiler;
return pipeline;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// An compilation stage, which generates a map file of the built binary file.
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public sealed class DebugFileGenerationStage : BaseCompilerStage
public sealed class DebugFileStage : BaseCompilerStage
{
#region Data Members

Expand Down Expand Up @@ -48,7 +48,7 @@ private void EmitSections()
{
writer.WriteLine("[Sections]");
writer.WriteLine("Address\tOffset\tSize\tKind\tName");
foreach (var linkerSection in Linker.LinkerSections)
foreach (var linkerSection in Linker.Sections)
{
writer.WriteLine(
"{0:x8}\t{1}\t{2}\t{3}\t{4}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// Emits method lookup table
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public class MethodExceptionLookupTableStage : BaseCompilerStage
public class ExceptionTableStage : BaseCompilerStage
{
#region Data Members

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// Finalizes the linking
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public sealed class LinkerFinalizationStage : BaseCompilerStage
public sealed class LinkerEmitStage : BaseCompilerStage
{
protected override void RunPostCompile()
{
if (!CompilerOptions.EmitBinary)
return;

if (string.IsNullOrEmpty(CompilerOptions.OutputFile))
return;

Expand All @@ -22,11 +25,6 @@ protected override void RunPostCompile()
{
Linker.Emit(file);
}

Compiler.GlobalCounters.Update("Linker.Text", (int)Linker.LinkerSections[(int)SectionKind.Text].Size);
Compiler.GlobalCounters.Update("Linker.Data", (int)Linker.LinkerSections[(int)SectionKind.Data].Size);
Compiler.GlobalCounters.Update("Linker.ROData", (int)Linker.LinkerSections[(int)SectionKind.ROData].Size);
Compiler.GlobalCounters.Update("Linker.BSS", (int)Linker.LinkerSections[(int)SectionKind.BSS].Size);
}
}
}
27 changes: 27 additions & 0 deletions Source/Mosa.Compiler.Framework/CompilerStages/LinkerLayoutStage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework.Linker;
using System.IO;

namespace Mosa.Compiler.Framework.CompilerStages
{
/// <summary>
/// Linker Layout Stage
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public sealed class LinkerLayoutStage : BaseCompilerStage
{
protected override void RunPostCompile()
{
if (string.IsNullOrEmpty(CompilerOptions.OutputFile))
return;

Linker.FinalizeLayout();

Compiler.GlobalCounters.Update("Linker.Text", (int)Linker.Sections[(int)SectionKind.Text].Size);
Compiler.GlobalCounters.Update("Linker.Data", (int)Linker.Sections[(int)SectionKind.Data].Size);
Compiler.GlobalCounters.Update("Linker.ROData", (int)Linker.Sections[(int)SectionKind.ROData].Size);
Compiler.GlobalCounters.Update("Linker.BSS", (int)Linker.Sections[(int)SectionKind.BSS].Size);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// An compilation stage, which generates a map file of the built binary file.
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public sealed class MapFileGenerationStage : BaseCompilerStage
public sealed class MapFileStage : BaseCompilerStage
{
#region Data Members

Expand Down Expand Up @@ -59,7 +59,7 @@ protected override void RunPostCompile()
private void EmitSections()
{
writer.WriteLine("Offset Virtual Length Name Class");
foreach (var linkerSection in Linker.LinkerSections)
foreach (var linkerSection in Linker.Sections)
{
writer.WriteLine("{0:x16} {1:x16} {2:x16} {3} {4}", linkerSection.FileOffset, linkerSection.VirtualAddress, linkerSection.Size, linkerSection.Name.PadRight(32), linkerSection.SectionKind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// Emits method lookup table
/// </summary>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public class MethodLookupTableStage : BaseCompilerStage
public class MethodTableStage : BaseCompilerStage
{
#region Data Members

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Mosa.Compiler.Framework.CompilerStages
/// metadata.
/// </remarks>
/// <seealso cref="Mosa.Compiler.Framework.BaseCompilerStage" />
public sealed class TypeInitializerSchedulerStage : BaseCompilerStage
public sealed class TypeInitializerStage : BaseCompilerStage
{
public const string TypeInitializerName = "AssemblyInit";

Expand All @@ -40,9 +40,9 @@ public sealed class TypeInitializerSchedulerStage : BaseCompilerStage
#region Construction

/// <summary>
/// Initializes a new instance of the <see cref="TypeInitializerSchedulerStage"/> class.
/// Initializes a new instance of the <see cref="TypeInitializerStage"/> class.
/// </summary>
public TypeInitializerSchedulerStage()
public TypeInitializerStage()
{
basicBlocks = new BasicBlocks();

Expand Down
9 changes: 7 additions & 2 deletions Source/Mosa.Compiler.Framework/Counters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Mosa.Compiler.Framework
/// </summary>
public sealed class Counters
{
private Dictionary<string, int> counters = new Dictionary<string, int>();
private readonly Dictionary<string, int> counters = new Dictionary<string, int>();
private readonly object _lock = new object();

public void Reset()
Expand All @@ -33,7 +33,7 @@ public void Update(string name, int count)
}
}

public void UpdateNoLock(string name, int count)
public void UpdateSkipLock(string name, int count)
{
if (counters.TryGetValue(name, out int current))
{
Expand All @@ -46,6 +46,11 @@ public void UpdateNoLock(string name, int count)
}
}

public void NewCountSkipLock(string name, int count)
{
counters.Add(name, count);
}

public List<string> Export()
{
var counts = new List<string>();
Expand Down
6 changes: 3 additions & 3 deletions Source/Mosa.Compiler.Framework/Linker/Elf/ElfLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private void WriteProgramHeaders()

elfheader.ProgramHeaderNumber = 0;

foreach (var linkerSection in linker.LinkerSections)
foreach (var linkerSection in linker.Sections)
{
if (linkerSection.Size == 0 && linkerSection.SectionKind != SectionKind.BSS)
continue;
Expand Down Expand Up @@ -243,7 +243,7 @@ private void CreateSections()

var previous = nullSection;

foreach (var linkerSection in linker.LinkerSections)
foreach (var linkerSection in linker.Sections)
{
if (linkerSection.Size == 0 && linkerSection.SectionKind != SectionKind.BSS)
continue;
Expand Down Expand Up @@ -329,7 +329,7 @@ private void WriteSectionHeaders()

private void WriteLinkerSection(Section section, EndianAwareBinaryWriter writer)
{
var linkerSection = linker.LinkerSections[(int)section.SectionKind];
var linkerSection = linker.Sections[(int)section.SectionKind];

writer.Position = section.Offset;
linker.WriteTo(writer.BaseStream, linkerSection);
Expand Down

0 comments on commit 09d5b2f

Please sign in to comment.