Skip to content

Commit

Permalink
more code cleanup and refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianoc committed Mar 27, 2024
1 parent 33a2f5c commit acc55ab
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cecilifier.Core/AST/AssignmentVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ bool HandleIndexer(SyntaxNode node, LinkedListNode<string> lastInstructionLoadin
}
private void EmitIndirectStore(ITypeSymbol typeBeingStored)
{
var indirectStoreOpCode = typeBeingStored.Stind();
var indirectStoreOpCode = typeBeingStored.StindOpCodeFor();
Context.EmitCilInstruction(ilVar, indirectStoreOpCode, indirectStoreOpCode == OpCodes.Stobj ? Context.TypeResolver.Resolve(typeBeingStored.ElementTypeSymbolOf()) : null);
}

Expand Down
2 changes: 1 addition & 1 deletion Cecilifier.Core/AST/ExpressionVisitor.Stackalloc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void EmitSlowInitialization(InitializerExpressionSyntax node, TypeInfo t
}

exp.Accept(this);
Context.EmitCilInstruction(ilVar, arrayType.Stind());
Context.EmitCilInstruction(ilVar, arrayType.StindOpCodeFor());
offset += elementTypeSize;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Cecilifier.Core/AST/StatementVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ private void ProcessVariableInitialization(VariableDeclaratorSyntax localVar, IT
var valueBeingAssignedIsByRef = Context.SemanticModel.GetSymbolInfo(localVar.Initializer.Value).Symbol.IsByRef();
if (!variableType.IsByRef() && valueBeingAssignedIsByRef)
{
OpCode opCode = variableType.LoadIndirectOpCodeFor();
OpCode opCode = variableType.LdindOpCodeFor();
Context.EmitCilInstruction(_ilVar, opCode);
}

Expand Down
2 changes: 1 addition & 1 deletion Cecilifier.Core/AST/SyntaxWalkerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ protected void HandlePotentialRefLoad(string ilVar, SyntaxNode expression, IType

if (needsLoadIndirect)
{
Context.EmitCilInstruction(ilVar, type.LoadIndirectOpCodeFor());
Context.EmitCilInstruction(ilVar, type.LdindOpCodeFor());
}
}

Expand Down
22 changes: 0 additions & 22 deletions Cecilifier.Core/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,28 +179,6 @@ public static OpCode LoadOpCodeFor(this ITypeSymbol type)
_ => throw new ArgumentException($"Literal type {type} not supported.", nameof(type))
};
}

public static OpCode LoadIndirectOpCodeFor(this ITypeSymbol type)
{
return type.SpecialType switch
{
SpecialType.System_Single => OpCodes.Ldind_R4,
SpecialType.System_Double => OpCodes.Ldind_R8,
SpecialType.System_SByte => OpCodes.Ldind_I1,
SpecialType.System_Byte => OpCodes.Ldind_U1,
SpecialType.System_Int16 => OpCodes.Ldind_I2,
SpecialType.System_UInt16 => OpCodes.Ldind_U2,
SpecialType.System_Int32 => OpCodes.Ldind_I4,
SpecialType.System_UInt32 => OpCodes.Ldind_U4,
SpecialType.System_Int64 => OpCodes.Ldind_I8,
SpecialType.System_UInt64 => OpCodes.Ldind_I8,
SpecialType.System_Char => OpCodes.Ldind_U2,
SpecialType.System_Boolean => OpCodes.Ldind_U1,
SpecialType.System_Object => OpCodes.Ldind_Ref,

_ => type.IsValueType ? OpCodes.Ldobj : OpCodes.Ldind_Ref
};
}

public static string ValueForDefaultLiteral(this ITypeSymbol literalType) => literalType switch
{
Expand Down
30 changes: 26 additions & 4 deletions Cecilifier.Core/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ public static uint SizeofArrayLikeItemElement(this ITypeSymbol type)
};
}

public static OpCode Stind(this ITypeSymbol type)
public static OpCode StindOpCodeFor(this ITypeSymbol type)
{
switch (type)
{
case INamedTypeSymbol { IsGenericType: true } ns:
return Stind(ns.TypeArguments[0]);
return StindOpCodeFor(ns.TypeArguments[0]);
case IPointerTypeSymbol ptr:
return Stind(ptr.PointedAtType);
return StindOpCodeFor(ptr.PointedAtType);
case IArrayTypeSymbol array:
return Stind(array.ElementType);
return StindOpCodeFor(array.ElementType);
}

return type.SpecialType switch
Expand All @@ -113,6 +113,28 @@ public static OpCode Stind(this ITypeSymbol type)
};
}

public static OpCode LdindOpCodeFor(this ITypeSymbol type)
{
return type.SpecialType switch
{
SpecialType.System_Single => OpCodes.Ldind_R4,
SpecialType.System_Double => OpCodes.Ldind_R8,
SpecialType.System_SByte => OpCodes.Ldind_I1,
SpecialType.System_Byte => OpCodes.Ldind_U1,
SpecialType.System_Int16 => OpCodes.Ldind_I2,
SpecialType.System_UInt16 => OpCodes.Ldind_U2,
SpecialType.System_Int32 => OpCodes.Ldind_I4,
SpecialType.System_UInt32 => OpCodes.Ldind_U4,
SpecialType.System_Int64 => OpCodes.Ldind_I8,
SpecialType.System_UInt64 => OpCodes.Ldind_I8,
SpecialType.System_Char => OpCodes.Ldind_U2,
SpecialType.System_Boolean => OpCodes.Ldind_U1,
SpecialType.System_Object => OpCodes.Ldind_Ref,

_ => type.IsValueType ? OpCodes.Ldobj : OpCodes.Ldind_Ref
};
}

public static OpCode StelemOpCode(this ITypeSymbol type) =>
type.SpecialType switch
{
Expand Down

0 comments on commit acc55ab

Please sign in to comment.