Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#718) Minor follow-up changes, correctness + code cleanup #759

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
IL_000e: ldc.i4.s 11
IL_0010: stfld System.Int32 <typedef>TestStruct::x
IL_0015: ldloc V_0
IL_0019: stsfld <typedef>TestStruct <Module>::global_tmp_0
IL_001e: ldsflda <typedef>TestStruct <Module>::global_tmp_0
IL_0019: stsfld <typedef>TestStruct <Module>::__global_tmp_0
IL_001e: ldsflda <typedef>TestStruct <Module>::__global_tmp_0
IL_0023: stsfld <typedef>TestStruct* <Module>::x
IL_0028: ret
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Type: <Module>
Fields:
Node* <Module>::root
Node <Module>::global_tmp_0
Node <Module>::__global_tmp_0
Methods:
System.Void <Module>::.cctor()
Locals:
Expand All @@ -13,8 +13,8 @@
IL_000e: ldc.i4.0
IL_000f: stfld Node* Node::parent
IL_0014: ldloc V_0
IL_0018: stsfld Node <Module>::global_tmp_0
IL_001d: ldsflda Node <Module>::global_tmp_0
IL_0018: stsfld Node <Module>::__global_tmp_0
IL_001d: ldsflda Node <Module>::__global_tmp_0
IL_0022: stsfld Node* <Module>::root
IL_0027: ret

Expand Down
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Contexts/FunctionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@ public void MergeScope(BlockScope scope)
/// <inheritdoc />
public string GetTmpVariable()
{
return "local_" + tempLocalIndex++;
return "__local_" + tempLocalIndex++;
}
}
2 changes: 1 addition & 1 deletion Cesium.CodeGen/Contexts/GlobalConstructorScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,6 @@ public void RemovePragma<T>(Predicate<T> predicate) where T : IPragma
/// <inheritdoc />
public string GetTmpVariable()
{
return "global_tmp_" + tempLocalIndex++;
return "__global_tmp_" + tempLocalIndex++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
using Cesium.CodeGen.Contexts;
using Cesium.CodeGen.Extensions;
using Cesium.CodeGen.Ir.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cesium.CodeGen.Ir.Expressions;
internal sealed class CompoundObjectFieldInitializer : IExpression
Expand All @@ -15,9 +10,8 @@ internal sealed class CompoundObjectFieldInitializer : IExpression
internal Designation Designation;

internal CompoundObjectFieldInitializer(AssignmentInitializer initializer, IDeclarationScope scope)
: this(initializer.Expression.ToIntermediate(scope), initializer.Designation!)
{
Inner = initializer.Expression.ToIntermediate(scope);
Designation = initializer.Designation!;
}

internal CompoundObjectFieldInitializer(IExpression inner, Designation designation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Immutable;
using Cesium.Ast;
using Cesium.CodeGen.Contexts;
using Cesium.CodeGen.Extensions;
Expand All @@ -6,31 +7,30 @@
using Cesium.Core;
using Mono.Cecil;
using Mono.Cecil.Cil;
using System.Collections.Immutable;

namespace Cesium.CodeGen.Ir.Expressions;
internal sealed class CompoundObjectInitializationExpression : IExpression
{
private IType? _type;
private readonly IType? _type;
private FieldDefinition? _typeDef;
private Action? _prefixAction;
private Action? _postfixAction;
private readonly ImmutableArray<IExpression?> _initializers;

public CompoundObjectInitializationExpression(IType type, ImmutableArray<IExpression?> initializers)
public CompoundObjectInitializationExpression(IType? type, ImmutableArray<IExpression?> initializers)
{
_type = type;
_initializers = initializers;
}

public CompoundObjectInitializationExpression(ImmutableArray<IExpression?> initializers)
: this(null, initializers)
{
_initializers = initializers;
}

public CompoundObjectInitializationExpression(Ast.CompoundLiteralExpression expression, IDeclarationScope scope)
public CompoundObjectInitializationExpression(CompoundLiteralExpression expression, IDeclarationScope scope)
{
var (type, cliDescription) = LocalDeclarationInfo.ProcessSpecifiers(expression.TypeName.SpecifierQualifierList, scope);
var (type, _) = LocalDeclarationInfo.ProcessSpecifiers(expression.TypeName.SpecifierQualifierList, scope);
_type = type;
_initializers = expression.Initializers.Select(initializer => IScopedDeclarationInfo.ConvertInitializer(_type, initializer, scope)).ToImmutableArray();
}
Expand Down Expand Up @@ -62,7 +62,7 @@ public void EmitTo(IEmitScope scope)
if (expr == null)
throw new CompilationException($"Retrieved null initializer!");


if (_prefixAction is not null)
{
_prefixAction();
Expand Down Expand Up @@ -97,7 +97,7 @@ public void EmitTo(IEmitScope scope)
instructions.Add(Instruction.Create(OpCodes.Ldflda, _typeDef));
}
return;
}
}

for (int i = 0; i < initializers.Length; i++)
{
Expand All @@ -118,8 +118,9 @@ public void EmitTo(IEmitScope scope)
EmitPathToField(scope, typeDef, f);
}
else if (init is CompoundObjectInitializationExpression objInit)
{
{
// UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE UNSAFE
var index = i;
objInit.Hint(
fieldsDefs[i],
() =>
Expand All @@ -128,7 +129,7 @@ public void EmitTo(IEmitScope scope)
},
() =>
{
instructions.Add(Instruction.Create(OpCodes.Stfld, fieldsDefs[i]));
instructions.Add(Instruction.Create(OpCodes.Stfld, fieldsDefs[index]));
});
objInit.EmitTo(scope);
}
Expand All @@ -155,7 +156,7 @@ private static void EmitPathToField(IEmitScope scope, TypeDefinition type, Compo
var p = path[i];
if (p is IdentifierDesignator id)
{
field = type.Fields.FirstOrDefault(_ => _.Name == id.FieldName)!;
field = type.Fields.FirstOrDefault(d => d.Name == id.FieldName)!;
if (field == null) // maybe anon?
{
List<FieldDefinition> list = new(1);
Expand Down Expand Up @@ -270,7 +271,7 @@ static Instruction GetWriteInstruction(MetadataType type)
case MetadataType.ByReference:
return Instruction.Create(OpCodes.Stind_I);
default:
throw new CompilationException($"This array type isnt supported: {type}");
throw new CompilationException($"This array type isn't supported: {type}");
}
}

Expand All @@ -279,7 +280,7 @@ static Instruction GetWriteInstruction(MetadataType type)
public IExpression Lower(IDeclarationScope scope)
{
var resolvedType = _type?.TypeKind == TypeKind.Unresolved ? scope.ResolveType(_type) : _type;
var initializers = _initializers.Select(_ => _?.Lower(scope)).ToImmutableArray();
var initializers = _initializers.Select(e => e?.Lower(scope)).ToImmutableArray();
return resolvedType == null ? new CompoundObjectInitializationExpression(initializers) : new CompoundObjectInitializationExpression(resolvedType, initializers);
}
}
Loading