Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Jun 4, 2024
1 parent e778b2c commit e05ee29
Show file tree
Hide file tree
Showing 22 changed files with 899 additions and 418 deletions.
15 changes: 8 additions & 7 deletions chibiar/chibiar.core/Archiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibiar.Cli;
using chibicc.toolchain.Archiving;
using chibicc.toolchain.Generating;
using chibicc.toolchain.Internal;
using chibicc.toolchain.IO;
using chibicc.toolchain.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using chibiar.Cli;
using chibicc.toolchain.Archiving;
using chibicc.toolchain.Internal;
using chibicc.toolchain.IO;
using chibicc.toolchain.Logging;

namespace chibiar;

Expand Down Expand Up @@ -216,11 +217,11 @@ private static void AddSymbolTable(

using var outputStream = symbolTableEntry.Open();

ArchiverUtilities.WriteSymbolTable(outputStream, symbolLists);
SymbolUtilities.WriteSymbolTable(outputStream, symbolLists);
}
else
{
ArchiverUtilities.WriteSymbolTable(new MemoryStream(), symbolLists);
SymbolUtilities.WriteSymbolTable(new MemoryStream(), symbolLists);
}
}

Expand Down
2 changes: 1 addition & 1 deletion chibias/chibias.core/Assembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public bool Assemble(
{
// Convert source code to object file.
using var outputStream = isDryrun ?
null : ObjectStreamUtilities.OpenObjectStream(outputTemporaryFilePath, true);
null : CompressionStreamUtilities.OpenStream(outputTemporaryFilePath, true);
if (outputStream != null)
{
Expand Down
6 changes: 5 additions & 1 deletion chibild/chibild.core.Tests/LinkerTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public static string RunCore(
ApplyOptimization = false,
CreationOptions = creationOptions,
PrependExecutionSearchPaths = prependExecutionSearchPaths ?? Array.Empty<string>(),
//CacheBasePath = null,
},
injectToAssemblyPath,
basePath,
Expand Down Expand Up @@ -175,7 +176,10 @@ public static string RunCore(
{
try
{
logtw.Flush();
if (logfs.CanWrite)
{
logtw.Flush();
}
}
catch
{
Expand Down
23 changes: 11 additions & 12 deletions chibild/chibild.core/CilLinker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private bool TryLoadInputReferences(
string[] assemblyReferenceBasePaths,
InputReference[] inputReferences,
bool isLocationOriginSource,
string? cacheBasePath,
out InputFragment[] fragments)
{
// Load input files in parallelism.
Expand Down Expand Up @@ -107,7 +108,7 @@ private bool TryLoadInputReferences(
$"Unable to find the object file: {relativePath}");
break;
}
using (var fs = ObjectStreamUtilities.OpenObjectStream(
using (var fs = CompressionStreamUtilities.OpenStream(
Path.Combine(baseInputPath, relativePath), false))
{
var tr = new StreamReader(fs, Encoding.UTF8, true);
Expand Down Expand Up @@ -157,11 +158,9 @@ when Path.GetExtension(relativePath) == ".a":
this.logger,
baseInputPath,
relativePath,
// Create this assembly specific resolver,
// because shared resolver can not resolve on multi-threaded context.
// At the cost of having to load it again later in the primary assembly resolver.
this.CreateAssemblyResolver(
ReadingMode.Immediate,
cacheBasePath,
() => this.CreateAssemblyResolver(
ReadingMode.Deferred,
assemblyReferenceBasePaths)),
};
break;
Expand Down Expand Up @@ -194,11 +193,9 @@ when Path.GetExtension(relativePath) == ".a":
this.logger,
foundEntry.basePath,
foundEntry.fileName,
// Create this assembly specific resolver,
// because shared resolver can not resolve on multi-threaded context.
// At the cost of having to load it again later in the primary assembly resolver.
this.CreateAssemblyResolver(
ReadingMode.Immediate,
cacheBasePath,
() => this.CreateAssemblyResolver(
ReadingMode.Deferred,
assemblyReferenceBasePaths)),
};
}
Expand Down Expand Up @@ -327,6 +324,7 @@ injectToAssemblyPath is { } injectPath ?
assemblyReferenceBasePaths,
totalInputReferences,
produceDebuggingInformation,
options.CacheBasePath,
out var loadedFragments))
{
return false;
Expand Down Expand Up @@ -356,7 +354,8 @@ injectToAssemblyPath is { } injectPath ?
// Will be injected exist (loaded) assembly.
if (injectToAssemblyPath != null)
{
primaryAssembly = ((AssemblyInputFragment)loadedFragments[0]).Assembly;
primaryAssembly = ((AssemblyInputFragment)loadedFragments[0]).
GetAssembly();
}
// Will be created a new assembly.
else
Expand Down
68 changes: 15 additions & 53 deletions chibild/chibild.core/Generating/ArchivedObjectInputFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using System;
using chibicc.toolchain.Generating;
using chibicc.toolchain.Archiving;
using chibicc.toolchain.Parsing;
using chibicc.toolchain.Logging;
Expand Down Expand Up @@ -57,19 +57,16 @@ private enum RequiredStates
private ArchivedObjectInputFragment(
string baseInputPath,
string relativePath,
string archivedObjectName,
Dictionary<string, Symbol> typeSymbols,
Dictionary<string, Symbol> variableSymbols,
Dictionary<string, Symbol> functionSymbols) :
AggregatedSymbols extraction) :
base(baseInputPath, relativePath)
{
this.archivedObjectName = archivedObjectName;
this.archivedObjectName = extraction.ObjectName;
this.ObjectName = Path.GetFileNameWithoutExtension(this.archivedObjectName);
this.ObjectPath = $"{this.archivedObjectName}@{base.ObjectPath}";

this.typeSymbols = typeSymbols;
this.variableSymbols = variableSymbols;
this.functionSymbols = functionSymbols;
this.typeSymbols = extraction.TypeSymbols;
this.variableSymbols = extraction.VariableSymbols;
this.functionSymbols = extraction.FunctionSymbols;
}

public override string ObjectName { get; }
Expand Down Expand Up @@ -162,14 +159,7 @@ public override bool ContainsFunctionAndSchedule(

//////////////////////////////////////////////////////////////

public enum LoadObjectResults
{
Ignored,
Loaded,
CaughtError,
}

public LoadObjectResults LoadObjectIfRequired(
public override LoadObjectResults LoadObjectIfRequired(
ILogger logger,
bool isLocationOriginSource)
{
Expand Down Expand Up @@ -227,7 +217,9 @@ public LoadObjectResults LoadObjectIfRequired(
LoadObjectResults.CaughtError :
LoadObjectResults.Loaded;
}
return LoadObjectResults.Ignored;

return this.requiredState == (int)RequiredStates.Loaded ?
LoadObjectResults.Loaded : LoadObjectResults.Ignored;
}

public static ArchivedObjectInputFragment[] Load(
Expand All @@ -239,42 +231,12 @@ public static ArchivedObjectInputFragment[] Load(

var symbolLists = ArchiverUtilities.EnumerateSymbolTable(
Path.Combine(baseInputPath, relativePath));

return symbolLists.Select(symbolList =>
{
var symbols = symbolList.Symbols.
GroupBy(symbol =>
{
switch (symbol.Directive)
{
case "enumeration": return "type";
case "structure": return "type";
case "global": return "variable";
case "constant": return "variable";
case "function": return "function";
default:
logger.Warning($"Ignored invalid symbol table entry: {symbol.Directive}");
return "unknown";
}
}).
ToDictionary(
g => g.Key,
g => g.
// Takes largest member count.
OrderByDescending(symbol => symbol.MemberCount ?? 0).
DistinctBy(symbol => symbol.Name).
ToDictionary(symbol => symbol.Name));
var empty = new Dictionary<string, Symbol>();
return new ArchivedObjectInputFragment(

return SymbolAggregator.AggregateSymbolsFromSymbolTable(logger, symbolLists).
Select(extraction => new ArchivedObjectInputFragment(
baseInputPath,
relativePath,
symbolList.ObjectName,
symbols.TryGetValue("type", out var types) ? types : empty,
symbols.TryGetValue("variable", out var variableNames) ? variableNames : empty,
symbols.TryGetValue("function", out var functionNames) ? functionNames : empty);
}).
ToArray();
extraction)).
ToArray();
}
}
Loading

0 comments on commit e05ee29

Please sign in to comment.