From f6bb40661d8712976a52cdeeb79cb98f954b07c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20Vir=C3=A1gh?= Date: Tue, 12 Oct 2021 21:15:34 +0200 Subject: [PATCH] Add starg and star.s IL codes --- IL2C.Core/ILConveters/StargConverters .cs | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 IL2C.Core/ILConveters/StargConverters .cs diff --git a/IL2C.Core/ILConveters/StargConverters .cs b/IL2C.Core/ILConveters/StargConverters .cs new file mode 100644 index 00000000..6c089e60 --- /dev/null +++ b/IL2C.Core/ILConveters/StargConverters .cs @@ -0,0 +1,67 @@ +///////////////////////////////////////////////////////////////////////////////////////////////// +// +// IL2C - A translator for ECMA-335 CIL/MSIL to C language. +// Copyright (c) 2016-2019 Kouji Matsui (@kozy_kekyo, @kekyo2) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +///////////////////////////////////////////////////////////////////////////////////////////////// + +using System; + +using Mono.Cecil.Cil; + +using IL2C.Translators; +using IL2C.Metadata; + +namespace IL2C.ILConverters +{ + internal static class StargConverterUtilities + { + public static ExpressionEmitter Prepare( + int parameterIndex, DecodeContext decodeContext, bool isReference) + { + var parameter = decodeContext.Method.Parameters[parameterIndex]; + var targetType = isReference ? parameter.TargetType.MakeByReference() : parameter.TargetType; + var symbol = decodeContext.PushStack(targetType); + + return (extractContext, _) => new[] { string.Format( + "{0} = {1}{2}", + parameter.ParameterName, + // NOTE: Don't check "targetType.IsByReference" instead "isReference." + // Because it's maybe double encoded byref type. + isReference ? "&" : string.Empty, + extractContext.GetSymbolName(symbol)) }; + } + } + + internal sealed class StargSConverter : ShortInlineParamConverter + { + public override OpCode OpCode => OpCodes.Starg_S; + + public override ExpressionEmitter Prepare(VariableInformation operand, DecodeContext decodeContext) + { + return StargConverterUtilities.Prepare(operand.Index, decodeContext, false); + } + } + + internal sealed class StargConverter : InlineParamConverter + { + public override OpCode OpCode => OpCodes.Starg; + + public override ExpressionEmitter Prepare(VariableInformation operand, DecodeContext decodeContext) + { + return StargConverterUtilities.Prepare(operand.Index, decodeContext, false); + } + } +}