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 c375613 commit da6d99a
Show file tree
Hide file tree
Showing 24 changed files with 904 additions and 414 deletions.
2 changes: 1 addition & 1 deletion chibiar/chibiar.core.Tests/ArchiverTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//
// chibicc-toolchain - The specialized backend toolchain for chibicc-cil
// Copyright (c) Kouji Matsui(@kozy_kekyo, @kekyo @mastodon.cloud)
Expand Down
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
2 changes: 1 addition & 1 deletion chibild/chibild.core.Tests/LinkerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//
// chibicc-toolchain - The specialized backend toolchain for chibicc-cil
// Copyright (c) Kouji Matsui(@kozy_kekyo, @kekyo @mastodon.cloud)
Expand Down
18 changes: 13 additions & 5 deletions chibild/chibild.core/CilLinker.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//
// chibicc-toolchain - The specialized backend toolchain for chibicc-cil
// Copyright (c) Kouji Matsui(@kozy_kekyo, @kekyo @mastodon.cloud)
Expand Down Expand Up @@ -62,7 +62,9 @@ private bool TryLoadInputReferences(
string[] libraryReferenceBasePaths,
string[] assemblyReferenceBasePaths,
InputReference[] inputReferences,
string? injectToAssemblyPath,
bool isLocationOriginSource,
string? cacheBasePath,
out InputFragment[] fragments)
{
// Load input files in parallelism.
Expand Down Expand Up @@ -107,7 +109,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 @@ -144,6 +146,7 @@ when Path.GetExtension(relativePath) == ".a":
// Asssembly:
case LibraryPathReference(var relativePath):
var isInjectAssembly = (index == 0) && (relativePath == injectToAssemblyPath);
var libraryFilePath = Path.Combine(baseInputPath, relativePath);
if (!File.Exists(libraryFilePath))
{
Expand All @@ -157,7 +160,8 @@ when Path.GetExtension(relativePath) == ".a":
this.logger,
baseInputPath,
relativePath,
this.CreateAssemblyResolver(
cacheBasePath,
() => this.CreateAssemblyResolver(
ReadingMode.Deferred,
assemblyReferenceBasePaths)),
};
Expand Down Expand Up @@ -191,7 +195,8 @@ when Path.GetExtension(relativePath) == ".a":
this.logger,
foundEntry.basePath,
foundEntry.fileName,
this.CreateAssemblyResolver(
cacheBasePath,
() => this.CreateAssemblyResolver(
ReadingMode.Deferred,
assemblyReferenceBasePaths)),
};
Expand Down Expand Up @@ -320,7 +325,9 @@ injectToAssemblyPath is { } injectPath ?
options.LibraryReferenceBasePaths,
assemblyReferenceBasePaths,
totalInputReferences,
injectToAssemblyPath,
produceDebuggingInformation,
options.CacheBasePath,
out var loadedFragments))
{
return false;
Expand Down Expand Up @@ -350,7 +357,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 da6d99a

Please sign in to comment.