Skip to content
Permalink
Browse files

Merge pull request #638 from tgiphil/047-Inline

- WIP
  • Loading branch information...
tgiphil committed Jun 4, 2019
2 parents 9e1f549 + ad96617 commit 12ca2ec2ed8a5d09d5cf0ee12f43e9a41a9a9430
Showing with 347 additions and 276 deletions.
  1. 0 Demos/Windows/{Run-Mosa.BareMetal.HelloWorld.x86.bat → TODO-Run-Mosa.BareMetal.HelloWorld.x86.bat}
  2. +6 −4 Source/Mosa.Compiler.Framework/BaseCodeTransformationStage.cs
  3. +7 −7 Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
  4. +7 −0 Source/Mosa.Compiler.Framework/CompilerData.cs
  5. +24 −9 Source/Mosa.Compiler.Framework/CompilerStages/MetadataStage.cs
  6. +4 −0 Source/Mosa.Compiler.Framework/CompilerStages/MethodTableStage.cs
  7. +1 −0 Source/Mosa.Compiler.Framework/CompilerStages/SetupStage.cs
  8. +3 −0 Source/Mosa.Compiler.Framework/CompilerStages/TypeInitializerStage.cs
  9. +11 −8 Source/Mosa.Compiler.Framework/Linker/Elf/ElfLinker.cs
  10. +12 −4 Source/Mosa.Compiler.Framework/Linker/LinkerSymbol.cs
  11. +11 −22 Source/Mosa.Compiler.Framework/Linker/MosaLinker.cs
  12. +53 −32 Source/Mosa.Compiler.Framework/MethodCompiler.cs
  13. +0 −2 Source/Mosa.Compiler.Framework/MethodData.cs
  14. +3 −22 Source/Mosa.Compiler.Framework/MethodScheduler.cs
  15. +3 −0 Source/Mosa.Compiler.Framework/Mosa.Compiler.Framework.csproj
  16. +8 −8 Source/Mosa.Compiler.Framework/Stages/CILDecodingStage.cs
  17. +1 −1 Source/Mosa.Compiler.Framework/Stages/CILOperandAssignmentStage.cs
  18. +2 −2 Source/Mosa.Compiler.Framework/Stages/CILStaticAllocationResolutionStage.cs
  19. +2 −3 Source/Mosa.Compiler.Framework/Stages/CILTransformationStage.cs
  20. +13 −3 Source/Mosa.Compiler.Framework/Stages/CallStage.cs
  21. +15 −20 Source/Mosa.Compiler.Framework/Stages/CodeGenerationStage.cs
  22. +0 −2 Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs
  23. +1 −1 Source/Mosa.Compiler.Framework/Stages/ExceptionPrologueStage.cs
  24. +1 −1 Source/Mosa.Compiler.Framework/Stages/ExceptionStage.cs
  25. +0 −2 Source/Mosa.Compiler.Framework/Stages/ExitSSAStage.cs
  26. +1 −2 Source/Mosa.Compiler.Framework/Stages/IRLongDecompositionStage.cs
  27. +67 −51 Source/Mosa.Compiler.Framework/Stages/InlineEvaluationStage.cs
  28. +2 −11 Source/Mosa.Compiler.Framework/Stages/InlineStage.cs
  29. +2 −2 Source/Mosa.Compiler.Framework/Stages/PlugStage.cs
  30. +4 −4 Source/Mosa.Compiler.Framework/Stages/ProtectedRegionLayoutStage.cs
  31. +2 −2 Source/Mosa.Compiler.Framework/Stages/ProtectedRegionStage.cs
  32. +1 −1 Source/Mosa.Compiler.Framework/Stages/StackLayoutStage.cs
  33. +33 −34 Source/Mosa.Compiler.Framework/Stages/UnboxValueTypeStage.cs
  34. +2 −0 Source/Mosa.Platform.Intel/CompilerStages/MultibootV1Stage.cs
  35. +2 −0 Source/Mosa.Platform.Intel/CompilerStages/StartUpStage.cs
  36. +4 −4 Source/Mosa.Platform.Intel/Stages/FloatingPointStage.cs
  37. +1 −1 Source/Mosa.Tool.Explorer/MainForm.cs
  38. +2 −2 Source/Mosa.Tool.Explorer/Stages/DisassemblyStage.cs
  39. +18 −0 Source/Mosa.UnitTests/OptimizationTests.cs
  40. +17 −8 Source/Mosa.Utility.Launcher/Builder.cs
  41. +1 −1 Source/Mosa.Utility.UnitTests/UnitTestEngine.cs
@@ -35,11 +35,9 @@ protected override void Run()
{
for (var node = BasicBlocks[index].AfterFirst; !node.IsBlockEndInstruction; node = node.Next)
{
if (node.IsEmpty)
if (node.IsEmptyOrNop)
continue;

//instructionCount++;

if (node.Instruction.ID == 0)
continue; // no mapping

@@ -49,13 +47,17 @@ protected override void Run()
{
context.Node = node;
visitationContext(context);
continue;
}

if (node.IsEmpty)
if (node.IsEmptyOrNop)
continue;

visitationNodes[node.Instruction.ID]?.Invoke(node);
}

if (MethodCompiler.IsStopped)
return;
}
}

@@ -178,7 +178,7 @@ public abstract class BaseMethodCompilerStage : ITraceFactory
/// <value>
/// <c>true</c> if this instance has protected regions; otherwise, <c>false</c>.
/// </value>
protected bool HasProtectedRegions { get { return MethodCompiler.Method.ExceptionHandlers.Count != 0; } }
protected bool HasProtectedRegions { get { return Method.ExceptionHandlers.Count != 0; } }

/// <summary>
/// Gets a value indicating whether this instance has code.
@@ -569,7 +569,7 @@ protected static void UpdatePhiInstructionTargets(List<BasicBlock> targets, Basi

protected MosaExceptionHandler FindImmediateExceptionContext(int label)
{
foreach (var handler in MethodCompiler.Method.ExceptionHandlers)
foreach (var handler in Method.ExceptionHandlers)
{
if (handler.IsLabelWithinTry(label) || handler.IsLabelWithinHandler(label))
{
@@ -582,11 +582,11 @@ protected MosaExceptionHandler FindImmediateExceptionContext(int label)

protected MosaExceptionHandler FindNextEnclosingFinallyContext(MosaExceptionHandler exceptionContext)
{
int index = MethodCompiler.Method.ExceptionHandlers.IndexOf(exceptionContext);
int index = Method.ExceptionHandlers.IndexOf(exceptionContext);

for (int i = index + 1; i < MethodCompiler.Method.ExceptionHandlers.Count; i++)
for (int i = index + 1; i < Method.ExceptionHandlers.Count; i++)
{
var entry = MethodCompiler.Method.ExceptionHandlers[i];
var entry = Method.ExceptionHandlers[i];

if (!entry.IsLabelWithinTry(exceptionContext.TryStart))
return null;
@@ -604,7 +604,7 @@ protected MosaExceptionHandler FindFinallyExceptionContext(InstructionNode node)
{
int label = node.Label;

foreach (var handler in MethodCompiler.Method.ExceptionHandlers)
foreach (var handler in Method.ExceptionHandlers)
{
if (handler.IsLabelWithinHandler(label))
{
@@ -620,7 +620,7 @@ protected bool IsSourceAndTargetWithinSameTryOrException(InstructionNode node)
int leaveLabel = node.Label;
int targetLabel = node.BranchTargets[0].First.Label;

foreach (var handler in MethodCompiler.Method.ExceptionHandlers)
foreach (var handler in Method.ExceptionHandlers)
{
bool one = handler.IsLabelWithinTry(leaveLabel);
bool two = handler.IsLabelWithinTry(targetLabel);
@@ -59,6 +59,13 @@ public MethodData GetMethodData(MosaMethod method)
}
}

public bool IsMethodInlined(MosaMethod method)
{
var methodData = GetMethodData(method);

return methodData.Inlined;
}

#endregion Methods
}
}
@@ -185,8 +185,12 @@ private LinkerSymbol CreateTypeDefinition(MosaType type, LinkerSymbol assemblyTa
if (!method.IsConstructor || method.Signature.Parameters.Count != 0 || method.HasOpenGenericParams)
continue;

//if (!Compiler.MethodScanner.IsMethodInvoked(method))
// break;
// TODO: Inline
//if (Compiler.CompilerData.IsMethodInlined(method))
// continue;

if (!Compiler.MethodScanner.IsMethodInvoked(method))
break;

Linker.Link(LinkType.AbsoluteAddress, NativePatchType, typeTableSymbol, writer.Position, Metadata.MethodDefinition + method.FullName, 0);

@@ -243,12 +247,16 @@ private LinkerSymbol CreateTypeDefinition(MosaType type, LinkerSymbol assemblyTa
// 15. Pointer to Methods
foreach (var method in methodList)
{
if ((!(!method.HasImplementation && method.IsAbstract)) && !method.HasOpenGenericParams && !method.DeclaringType.HasOpenGenericParams)
Debug.Assert((!(!method.HasImplementation && method.IsAbstract)) == (method.HasImplementation || !method.IsAbstract));

if ((!(!method.HasImplementation && method.IsAbstract))
&& !method.HasOpenGenericParams
&& !method.DeclaringType.HasOpenGenericParams

//&& !Compiler.CompilerData.IsMethodInlined(method) // TODO: Inline
&& Compiler.MethodScanner.IsMethodInvoked(method))
{
if (Compiler.MethodScanner.IsMethodInvoked(method))
{
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, typeTableSymbol, writer.Position, method.FullName, 0);
}
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, typeTableSymbol, writer.Position, GetMethodNameConsiderPlug(method), 0);
}
writer.WriteZeroBytes(TypeLayout.NativePointerSize);
}
@@ -565,11 +573,11 @@ private LinkerSymbol CreateMethodDefinition(MosaMethod method)
writer.Write(value, TypeLayout.NativePointerSize);

// 5. Pointer to Method
if (method.HasImplementation && !method.HasOpenGenericParams && !method.DeclaringType.HasOpenGenericParams)
if (method.HasImplementation && !method.HasOpenGenericParams && !method.DeclaringType.HasOpenGenericParams /*&& !methodData.Inlined*/) // TODO: Inline
{
if (Compiler.MethodScanner.IsMethodInvoked(method))
{
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, methodTableSymbol, writer.Position, method.FullName, 0);
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, methodTableSymbol, writer.Position, GetMethodNameConsiderPlug(method), 0);
}
}
writer.WriteZeroBytes(TypeLayout.NativePointerSize);
@@ -895,5 +903,12 @@ private void WriteArgument(EndianAwareBinaryWriter writer, LinkerSymbol symbol,
}

#endregion Custom Attributes

private string GetMethodNameConsiderPlug(MosaMethod method)
{
var plugMethod = Compiler.PlugSystem.GetReplacement(method);

return (plugMethod == null) ? method.FullName : plugMethod.FullName;
}
}
}
@@ -56,6 +56,10 @@ protected override void Finalization()
if (!Compiler.MethodScanner.IsMethodInvoked(method))
continue;

// Don't emit entry for plug methods
if (Compiler.PlugSystem.GetReplacement(method) != null)
continue;

// 1. Pointer to Method
Linker.Link(LinkType.AbsoluteAddress, NativePatchType, methodLookupTable, writer.Position, method.FullName, 0);
writer.WriteZeroBytes(TypeLayout.NativePointerSize);
@@ -37,6 +37,7 @@ protected override void Setup()
{
setupMethod = Compiler.CreateLinkerMethod(SetupStagerName);

Compiler.CompilerData.GetMethodData(setupMethod).DoNotInline = true;
MethodScanner.MethodInvoked(setupMethod, setupMethod);

Linker.EntryPoint = Linker.GetSymbol(setupMethod.FullName);
@@ -79,6 +79,9 @@ protected override void Setup()

Compiler.PlugSystem.CreatePlug(initializeAssemblyMethod, typeInitializerMethod);

Compiler.CompilerData.GetMethodData(initializeAssemblyMethod).DoNotInline = true;
Compiler.CompilerData.GetMethodData(typeInitializerMethod).DoNotInline = true;

MethodScanner.MethodInvoked(initializeAssemblyMethod, initializeAssemblyMethod);
MethodScanner.MethodInvoked(typeInitializerMethod, typeInitializerMethod);

@@ -444,14 +444,17 @@ private void WriteSymbolSection(Section section, EndianAwareBinaryWriter writer)

foreach (var symbol in linker.Symbols)
{
if (symbol.SectionKind == SectionKind.Unknown && symbol.LinkRequests.Count == 0)
continue;

Debug.Assert(symbol.SectionKind != SectionKind.Unknown, "symbol.SectionKind != SectionKind.Unknown");

if (!(symbol.IsExport || linker.EmitAllSymbols))
if (!(symbol.IsExternalSymbol || linker.EmitAllSymbols))
continue;

var symbolEntry = new SymbolEntry()
{
Name = AddToStringTable(symbol.ExportName ?? symbol.Name),
Name = AddToStringTable(symbol.ExternalSymbolName ?? symbol.Name),
Value = symbol.VirtualAddress,
Size = symbol.Size,
SymbolBinding = SymbolBinding.Global,
@@ -481,15 +484,15 @@ private void CreateRelocationSections()
if (symbol.SectionKind != kind)
continue;

if (symbol.IsExport)
if (symbol.IsExternalSymbol)
continue;

foreach (var patch in symbol.LinkRequests)
{
if (patch.LinkType == LinkType.Size)
continue;

if (!patch.ReferenceSymbol.IsExport)
if (!patch.ReferenceSymbol.IsExternalSymbol)
continue;

if (patch.ReferenceOffset == 0)
@@ -571,7 +574,7 @@ private void EmitRelocation(Section section, EndianAwareBinaryWriter writer)

foreach (var symbol in linker.Symbols)
{
if (symbol.IsExport)
if (symbol.IsExternalSymbol)
continue;

foreach (var patch in symbol.LinkRequests)
@@ -585,7 +588,7 @@ private void EmitRelocation(Section section, EndianAwareBinaryWriter writer)
if (patch.LinkType == LinkType.Size)
continue;

if (!patch.ReferenceSymbol.IsExport) // FUTURE: include relocations for static symbols, if option selected
if (!patch.ReferenceSymbol.IsExternalSymbol) // FUTURE: include relocations for static symbols, if option selected
continue;

var relocationEntry = new RelocationEntry()
@@ -612,7 +615,7 @@ private void EmitRelocationAddend(Section section, EndianAwareBinaryWriter write
//if (symbol.SectionKind != section.SectionKind)
// continue;

if (symbol.IsExport)
if (symbol.IsExternalSymbol)
continue;

foreach (var patch in symbol.LinkRequests)
@@ -626,7 +629,7 @@ private void EmitRelocationAddend(Section section, EndianAwareBinaryWriter write
if (patch.LinkType == LinkType.Size)
continue;

if (!patch.ReferenceSymbol.IsExport) // FUTURE: include relocations for static symbols, if option selected
if (!patch.ReferenceSymbol.IsExternalSymbol) // FUTURE: include relocations for static symbols, if option selected
continue;

var relocationAddendEntry = new RelocationAddendEntry()
@@ -13,8 +13,6 @@ public sealed class LinkerSymbol
{
public string Name { get; }

public string ExportName { get; internal set; }

public SectionKind SectionKind { get; internal set; }

public Stream Stream { get; internal set; }
@@ -27,12 +25,16 @@ public sealed class LinkerSymbol

public bool IsResolved { get { return VirtualAddress != 0; } }

public bool IsExport { get; set; }
public string ExternalSymbolName { get; internal set; }

public bool IsExternalSymbol { get; set; }

public uint SectionOffset { get; internal set; }

public ulong VirtualAddress { get; internal set; }

public bool IsReplaced { get; internal set; }

public List<LinkRequest> LinkRequests { get; }

private readonly object _lock = new object();
@@ -43,7 +45,8 @@ internal LinkerSymbol(string name, uint alignment = 0, SectionKind kind = Sectio
Alignment = alignment;
SectionKind = kind;
LinkRequests = new List<LinkRequest>();
IsExport = false;
IsExternalSymbol = false;
IsReplaced = false;
}

public void SetData(MemoryStream stream)
@@ -56,6 +59,11 @@ public void SetData(byte[] data)
SetData(new MemoryStream(data));
}

public void SetReplacementStatus(bool replaced)
{
IsReplaced = replaced;
}

public void AddPatch(LinkRequest linkRequest)
{
lock (_lock)
@@ -145,7 +145,7 @@ public LinkerSymbol DefineSymbol(string name, SectionKind kind, int alignment, i

Symbols.Add(symbol);
symbolLookup.Add(name, symbol);
symbol.IsExport = false;
symbol.IsExternalSymbol = false;
}

symbol.Alignment = aligned;
@@ -162,26 +162,6 @@ public LinkerSymbol DefineSymbol(string name, SectionKind kind, int alignment, i
}
}

public LinkerSymbol DefineExternalSymbol(string name, string externalName, SectionKind kind)
{
lock (_lock)
{
if (!symbolLookup.TryGetValue(name, out LinkerSymbol symbol))
{
symbol = new LinkerSymbol(name, 0, kind);

Symbols.Add(symbol);
symbolLookup.Add(name, symbol);
}

symbol.SectionKind = kind;
symbol.IsExport = true;
symbol.ExportName = externalName;

return symbol;
}
}

public void SetFirst(LinkerSymbol symbol)
{
Symbols.Remove(symbol);
@@ -217,6 +197,9 @@ private void ApplyPatches()
{
foreach (var symbol in Symbols)
{
if (symbol.IsReplaced)
continue;

foreach (var linkRequest in symbol.LinkRequests)
{
ApplyPatch(linkRequest);
@@ -270,13 +253,16 @@ private void ResolveSectionLayout(LinkerSection section, uint fileOffset, ulong

foreach (var symbol in Symbols)
{
if (symbol.IsReplaced)
continue;

if (symbol.SectionKind != section.SectionKind)
continue;

if (symbol.IsResolved)
continue;

if (symbol.IsExport)
if (symbol.IsExternalSymbol)
continue;

symbol.SectionOffset = section.Size;
@@ -292,6 +278,9 @@ internal void WriteTo(Stream stream, LinkerSection section)
{
foreach (var symbol in Symbols)
{
if (symbol.IsReplaced)
continue;

if (symbol.SectionKind != section.SectionKind)
continue;

0 comments on commit 12ca2ec

Please sign in to comment.
You can’t perform that action at this time.