Skip to content

Commit

Permalink
Refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Apr 2, 2024
1 parent d027341 commit 72c5ecf
Show file tree
Hide file tree
Showing 22 changed files with 183 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using chibicc.common.Archiving;

using static VerifyNUnit.Verifier;
using static chibiar.ArchiverTestRunner;

namespace chibiar;

[TestFixture]
public sealed class ArchiverTests
public sealed class ArchiverUtilitiesTests
{
private static async Task VerifySymbolTableAsync(ZipArchive zip)
{
var symTableEntry = zip.GetEntry(Archiver.SymbolTableFileName)!;
var symTableEntry = zip.GetEntry(ArchiverUtilities.SymbolTableFileName)!;
using var afs = symTableEntry.Open();

var tr = new StreamReader(afs, Encoding.UTF8, true);
Expand Down Expand Up @@ -57,7 +58,7 @@ public Task ArchiveOne()

Assert.That(
zip.Entries.Select(e => e.Name),
Is.EqualTo(new[] { "parse.o", Archiver.SymbolTableFileName }));
Is.EqualTo(new[] { "parse.o", ArchiverUtilities.SymbolTableFileName }));

await VerifySymbolTableAsync(zip);
});
Expand Down Expand Up @@ -87,7 +88,7 @@ public Task ArchiveTwo()

Assert.That(
zip.Entries.Select(e => e.Name),
Is.EqualTo(new[] { "parse.o", "codegen.o", Archiver.SymbolTableFileName }));
Is.EqualTo(new[] { "parse.o", "codegen.o", ArchiverUtilities.SymbolTableFileName }));

await VerifySymbolTableAsync(zip);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using chibild.Tokenizing;
using chibicc.common.Archiving;
using chibicc.common.Tokenizing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -35,8 +36,6 @@ public enum AddResults

public sealed class Archiver
{
public static readonly string SymbolTableFileName = "__symtable$";

private static Stream OpenStream(string path, bool writable) =>
(path == "-") ?
(writable ? Console.OpenStandardOutput() : Console.OpenStandardInput()) :
Expand All @@ -45,81 +44,6 @@ private static Stream OpenStream(string path, bool writable) =>
writable ? FileMode.Create : FileMode.Open,
writable ? FileAccess.ReadWrite : FileAccess.Read, FileShare.Read);

private readonly struct Symbol
{
public readonly Token Directive;
public readonly Token Scope;
public readonly Token Name;
public readonly string FileName;

public Symbol(Token directive, Token scope, Token name, string fileName)
{
this.Directive = directive;
this.Scope = scope;
this.Name = name;
this.FileName = fileName;
}
}

private sealed class SymbolComparer : IEqualityComparer<Symbol>
{
public bool Equals(Symbol x, Symbol y) =>
x.Directive.Text.Equals(y.Directive.Text) &&
x.Name.Text.Equals(y.Name.Text);

public int GetHashCode(Symbol obj)
{
unchecked
{
return
(obj.Directive.Text.GetHashCode() * 397) ^
obj.Name.Text.GetHashCode();
}
}

public static readonly SymbolComparer Instance = new();
}

private static IEnumerable<Symbol> EnumerateSymbols(TextReader tr, string fileName)
{
var tokenizer = new Tokenizer();

while (true)
{
var line = tr.ReadLine();
if (line == null)
{
break;
}

var tokens = tokenizer.TokenizeLine(line);
if (tokens.Length >= 3)
{
var directive = tokens[0];
var scope = tokens[1];
if (directive.Type == TokenTypes.Directive &&
scope.Type == TokenTypes.Identity &&
scope.Text is "public" or "internal")
{
switch (directive.Text)
{
case "function":
case "global":
case "enumeration":
if (tokens.Length >= 4)
{
yield return new(directive, scope, tokens[3], fileName);
}
break;
case "structure":
yield return new(directive, scope, tokens[2], fileName);
break;
}
}
}
}
}

private static Symbol[] ReadSymbols(string objectFilePath, SymbolTableModes symbolTableMode)
{
if (symbolTableMode == SymbolTableModes.ForceUpdate ||
Expand All @@ -130,7 +54,7 @@ private static Symbol[] ReadSymbols(string objectFilePath, SymbolTableModes symb
var tr = new StreamReader(ofs, Encoding.UTF8, true);

var fileName = Path.GetFileNameWithoutExtension(objectFilePath) + ".o";
var symbols = EnumerateSymbols(tr, fileName).
var symbols = ArchiverUtilities.EnumerateSymbols(tr, fileName).
Distinct(SymbolComparer.Instance).
ToArray();

Expand Down Expand Up @@ -205,7 +129,7 @@ public AddResults Add(

if (updated &&
symbolTableMode != SymbolTableModes.ForceIgnore &&
archive?.GetEntry(SymbolTableFileName) is { } symbolTableEntry)
archive?.GetEntry(ArchiverUtilities.SymbolTableFileName) is { } symbolTableEntry)
{
symbolTableEntry.Delete();
}
Expand All @@ -228,7 +152,8 @@ public AddResults Add(

if (archive != null)
{
var symbolTableEntry = archive.CreateEntry(SymbolTableFileName, CompressionLevel.Optimal);
var symbolTableEntry = archive.CreateEntry(
ArchiverUtilities.SymbolTableFileName, CompressionLevel.Optimal);

using var afs = symbolTableEntry.Open();

Expand Down
2 changes: 1 addition & 1 deletion chibiar/chibiar.core/chibiar.core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\chibild\chibild.core\chibild.core.csproj" />
<ProjectReference Include="..\..\toolchain.common\toolchain.common.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions chibicc-cil-toolchain.sln
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "chibiar.core.Tests", "chibi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "chibiar.core", "chibiar\chibiar.core\chibiar.core.csproj", "{EDFB3E84-7FD5-414D-9A17-378C62CA3F35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "toolchain.common", "toolchain.common\toolchain.common.csproj", "{2100986D-9CD5-4A38-8D48-8E6F2781A9FF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -116,6 +118,10 @@ Global
{EDFB3E84-7FD5-414D-9A17-378C62CA3F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EDFB3E84-7FD5-414D-9A17-378C62CA3F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EDFB3E84-7FD5-414D-9A17-378C62CA3F35}.Release|Any CPU.Build.0 = Release|Any CPU
{2100986D-9CD5-4A38-8D48-8E6F2781A9FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2100986D-9CD5-4A38-8D48-8E6F2781A9FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2100986D-9CD5-4A38-8D48-8E6F2781A9FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2100986D-9CD5-4A38-8D48-8E6F2781A9FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using System.Collections.Generic;

Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Linker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibicc.common.Tokenizing;
using chibild.Internal;
using chibild.Parsing;
using chibild.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Mdb;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using System;
using System.IO;

Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

using chibild.Internal;
using chibild.Parsing.Embedding;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_LookupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Internal;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Rocks;
using System;
Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_ParseDirective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Internal;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_ParseEnumeration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using System.Linq;

Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_ParseInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Internal;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_ParseStructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Internal;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using System.Diagnostics;
using System.Linq;
Expand Down
2 changes: 1 addition & 1 deletion chibild/chibild.core/Parsing/Parser_ValueArrayType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/////////////////////////////////////////////////////////////////////////////////////

using chibild.Internal;
using chibild.Tokenizing;
using chibicc.common.Tokenizing;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
Expand Down
4 changes: 4 additions & 0 deletions chibild/chibild.core/chibild.core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
<EmbeddedResource Include="Parsing\Embedding\_start_i.s" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\toolchain.common\toolchain.common.csproj" />
</ItemGroup>

</Project>
60 changes: 60 additions & 0 deletions toolchain.common/Archiving/ArchiverUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/////////////////////////////////////////////////////////////////////////////////////
//
// chibicc-toolchain - The specialized backend toolchain for chibicc-cil
// Copyright (c) Kouji Matsui(@kozy_kekyo, @kekyo @mastodon.cloud)
//
// Licensed under MIT: https://opensource.org/licenses/MIT
//
/////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.IO;
using chibicc.common.Tokenizing;

namespace chibicc.common.Archiving;

public static class ArchiverUtilities
{
public static readonly string SymbolTableFileName = "__symtable$";

public static IEnumerable<Symbol> EnumerateSymbols(TextReader tr, string fileName)
{
var tokenizer = new Tokenizer();

while (true)
{
var line = tr.ReadLine();
if (line == null)
{
break;
}

var tokens = tokenizer.TokenizeLine(line);
if (tokens.Length >= 3)
{
var directive = tokens[0];
var scope = tokens[1];
if (directive.Type == TokenTypes.Directive &&
scope.Type == TokenTypes.Identity &&
scope.Text is "public" or "internal")
{
switch (directive.Text)
{
case "function":
case "global":
case "enumeration":
if (tokens.Length >= 4)
{
yield return new(directive, scope, tokens[3], fileName);
}
break;
case "structure":
yield return new(directive, scope, tokens[2], fileName);
break;
}
}
}
}
}
}
28 changes: 28 additions & 0 deletions toolchain.common/Archiving/Symbol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/////////////////////////////////////////////////////////////////////////////////////
//
// chibicc-toolchain - The specialized backend toolchain for chibicc-cil
// Copyright (c) Kouji Matsui(@kozy_kekyo, @kekyo @mastodon.cloud)
//
// Licensed under MIT: https://opensource.org/licenses/MIT
//
/////////////////////////////////////////////////////////////////////////////////////

using chibicc.common.Tokenizing;

namespace chibicc.common.Archiving;

public readonly struct Symbol
{
public readonly Token Directive;
public readonly Token Scope;
public readonly Token Name;
public readonly string FileName;

public Symbol(Token directive, Token scope, Token name, string fileName)
{
this.Directive = directive;
this.Scope = scope;
this.Name = name;
this.FileName = fileName;
}
}
Loading

0 comments on commit 72c5ecf

Please sign in to comment.