Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Apr 28, 2024
1 parent dd75fb8 commit 4580b15
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
8 changes: 4 additions & 4 deletions chibild/chibild.core/Generating/CodeGenerator_LookupMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ private void DelayLookingUpType(
var requiredPrioritizedTypes =
TypeGenerator.FilterRequiredPrioritizedCilBasisTypes(type);

// If contains `ArrayTypeNode`,
// If contains `FixedLengthArrayTypeNode`,
// then we need to add the necessary types here to construct the fixed length array type.
// When finally construct the .NET type with `TryConstructCilType()`,
// it is assumed that these .NET types are also prepared in `resolvedTypeReferences`.
if (requiredPrioritizedTypes.Any(rt => rt is ArrayTypeNode))
if (requiredPrioritizedTypes.Any(rt => rt is FixedLengthArrayTypeNode))
{
requiredPrioritizedTypes =
TypeGenerator.FixedLengthArrayTypeConstructRequirementTypes.
Expand Down Expand Up @@ -224,11 +224,11 @@ private void DelayLookingUpType(
// At this point, the `resolvedTypeReferences` should contain
// all the `TypeReferences` needed to construct a fixed length array type.
if (TypeGenerator.TryGetFixedLengthArrayType(
this.logger,
targetModule,
flat,
resolvedTypeReferences,
out var flatr))
out var flatr,
this.OutputError))
{
// Append got .NET type.
resolvedTypeReferences.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@
//
/////////////////////////////////////////////////////////////////////////////////////

using chibicc.toolchain.Logging;
using chibicc.toolchain.Parsing;
using chibicc.toolchain.Tokenizing;
using chibild.Internal;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using chibicc.toolchain.Logging;

namespace chibild.Generating;

Expand Down Expand Up @@ -51,29 +49,40 @@ partial class TypeGenerator
};

private static bool TryCreateFixedLengthArrayType(
ILogger logger,
ModuleDefinition targetModule,
FixedLengthArrayTypeNode type,
bool isPublic,
IReadOnlyDictionary<string, TypeReference> requiredTypes,
out TypeDefinition atr)
out TypeDefinition atr,
Action<Token, string> outputError)
{
var elementType = requiredTypes[type.ElementType.CilTypeName];
var length = type.Length;

var vttr = requiredTypes["System.ValueType"];

var fullName = type.CilTypeName;
var ns = fullName.Substring(0, fullName.LastIndexOf('.'));
var name = fullName.Substring(ns.Length + 1);

var valueArrayType = new TypeDefinition(
"C.type",
type.CilTypeName,
ns,
name,
isPublic ?
TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.SequentialLayout :
TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.SequentialLayout,
vttr)
vttr);

// Flex array type has no size, so `ClassSize` is also set to 0.
// However, CLR does not handle structures of size 0 and always assumes 1 byte.
// If this type is calculated as `sizeof FooBarArray` it will return 1, which is not as intended.
// But since Flex array itself is an array whose size cannot be calculated,
// this problem is ignored.
if (length < 0) // Flex array (length == -1)
{
ClassSize = 0,
PackingSize = 8,
};
valueArrayType.ClassSize = 0;
valueArrayType.PackingSize = 8;
}

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

Expand Down Expand Up @@ -101,6 +110,9 @@ private static bool TryCreateFixedLengthArrayType(
not MethodReference dmacr)
{
atr = null!;
outputError(
type.Token,
$"Could not find a constructor: System.Reflection.DefaultMemberAttribute..ctor");
return false;
}

Expand Down Expand Up @@ -185,6 +197,9 @@ private static bool TryCreateFixedLengthArrayType(
not MethodReference ioorecr)
{
atr = null!;
outputError(
type.Token,
$"Could not find a constructor: System.IndexOutOfRangeException..ctor");
return false;
}

Expand Down Expand Up @@ -456,6 +471,8 @@ private static bool TryCreateFixedLengthArrayType(
break;
}

// TODO: implemente `IList<T>`, `IReadOnlyList<T>`

setItemInstructions.Add(
Instruction.Create(OpCodes.Ret));

Expand Down Expand Up @@ -509,23 +526,23 @@ static bool InnerFilter(
}

public static bool TryGetFixedLengthArrayType(
ILogger logger,
ModuleDefinition targetModule,
FixedLengthArrayTypeNode type,
IReadOnlyDictionary<string, TypeReference> requiredTypes,
out TypeReference flatr)
out TypeReference flatr,
Action<Token, string> outputError)
{
var isPublic = IsInteriorTypesPublic(type, requiredTypes);

if (targetModule.GetType(type.CilTypeName) is not { } flatd)
{
if (!TryCreateFixedLengthArrayType(
logger,
targetModule,
type,
isPublic,
requiredTypes,
out flatd))
out flatd,
outputError))
{
flatr = null!;
return false;
Expand Down

0 comments on commit 4580b15

Please sign in to comment.