Skip to content

Commit

Permalink
Merge pull request #638 from tgiphil/047-Inline
Browse files Browse the repository at this point in the history
- WIP
  • Loading branch information
tgiphil authored Jun 4, 2019
2 parents 9e1f549 + ad96617 commit 12ca2ec
Show file tree
Hide file tree
Showing 41 changed files with 347 additions and 276 deletions.
10 changes: 6 additions & 4 deletions Source/Mosa.Compiler.Framework/BaseCodeTransformationStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
}
}

Expand Down
14 changes: 7 additions & 7 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
{
Expand All @@ -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;
Expand All @@ -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))
{
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions Source/Mosa.Compiler.Framework/CompilerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ public MethodData GetMethodData(MosaMethod method)
}
}

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

return methodData.Inlined;
}

#endregion Methods
}
}
33 changes: 24 additions & 9 deletions Source/Mosa.Compiler.Framework/CompilerStages/MetadataStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
19 changes: 11 additions & 8 deletions Source/Mosa.Compiler.Framework/Linker/Elf/ElfLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -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()
Expand Down
16 changes: 12 additions & 4 deletions Source/Mosa.Compiler.Framework/Linker/LinkerSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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)
Expand Down
33 changes: 11 additions & 22 deletions Source/Mosa.Compiler.Framework/Linker/MosaLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -217,6 +197,9 @@ private void ApplyPatches()
{
foreach (var symbol in Symbols)
{
if (symbol.IsReplaced)
continue;

foreach (var linkRequest in symbol.LinkRequests)
{
ApplyPatch(linkRequest);
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Loading

0 comments on commit 12ca2ec

Please sign in to comment.