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

Implement IL codes for #91, #93, fix #105 #104

Open
wants to merge 46 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
7ac3be8
Merge branch 'kekyo:devel' into devel
cyborgyn Oct 9, 2021
a2c5f30
Merge branch 'devel' of git://github.com/kekyo/IL2C into kekyo-devel
cyborgyn Oct 10, 2021
69cf5f0
Merge branch 'kekyo-devel' into devel
cyborgyn Oct 10, 2021
e29d09e
Merge pull request #3 from kekyo/devel
cyborgyn Oct 12, 2021
e7fef2b
Merge pull request #4 from cyborgyn/devel
cyborgyn Oct 12, 2021
69e814f
Fix double addition of the same DLL reference, in case the transpiled…
cyborgyn Oct 12, 2021
663563c
Fix discovery of implemented method in case of explicit interface imp…
cyborgyn Oct 12, 2021
4234755
Add bitwise NOT arithmetic operator
cyborgyn Oct 12, 2021
19c92d9
Implement shr, shl IL codes
cyborgyn Oct 12, 2021
f6bb406
Add starg and star.s IL codes
cyborgyn Oct 12, 2021
9091c5b
Fix for #105, stop endless loop, fix static constructor handling #97,…
cyborgyn Oct 13, 2021
bab32a0
Implement Shr.un and Neg IL codes
cyborgyn Oct 13, 2021
80e54b5
Implement Bgt.un and Bgt.un.s IL codes
cyborgyn Oct 13, 2021
c5ac124
Extend ConditionalConverters to handle float type parameters
cyborgyn Oct 13, 2021
e01dc9a
Fix typo
cyborgyn Oct 13, 2021
4833ba2
Implement Stind.i1-i8, Stind.r4-r8 IL codes
cyborgyn Oct 13, 2021
90bd0a3
Fix StargConverter
cyborgyn Oct 13, 2021
05ececc
Add Enum to int conversion handling to GetRightExpression()
cyborgyn Oct 13, 2021
f98e142
Fix NullReferenceException occurring in MethodSignatureTypeComparerIm…
cyborgyn Oct 13, 2021
df35a87
Fix InternalWriteVTableTypePreDefinitions() to correctly handle empty…
cyborgyn Oct 13, 2021
1a9cfe8
Enable IL2C_RUNTIME_TYPE_BEGIN writer, to handle compilation of Syste…
cyborgyn Oct 13, 2021
2a73440
Enable handling without error such cases, where BaseType == null
cyborgyn Oct 13, 2021
e8029c1
Implement Switch IL code, for #93 and #91 v0.1
cyborgyn Oct 13, 2021
e1b6cc4
Implement Ldobj and Stobj IL codes
cyborgyn Oct 14, 2021
30c0889
Updated supported-opcodes.md by Unit tests, no regression so far
cyborgyn Oct 14, 2021
08ea06a
Add switch IL code tests
cyborgyn Oct 14, 2021
ff75c55
Fix switch test + generator
cyborgyn Oct 14, 2021
5453d39
Ldobj IL code test + related fixes in converters
cyborgyn Oct 14, 2021
8e3bac8
Fix ldobj unit test
cyborgyn Oct 14, 2021
e0d75e0
More ldobj tests
cyborgyn Oct 15, 2021
1153f48
More Ldobj tests
cyborgyn Oct 15, 2021
b549284
Merge branch 'devel' into feature/implement-ilcodes
cyborgyn Oct 16, 2021
e43b30c
Merge pull request #5 from kekyo/devel
cyborgyn Oct 17, 2021
3c3a734
Merge pull request #6 from cyborgyn/devel
cyborgyn Oct 17, 2021
796f113
Add Stobj IL Code unit tests
cyborgyn Oct 17, 2021
dd83236
Add more TypeInitializer unit tests
cyborgyn Oct 17, 2021
73afbfb
Add Shr and Shr.un IL Code unit tests + fix for it's converter to mak…
cyborgyn Oct 18, 2021
a28587a
Updated supported-opcodes.md & supported-runtime-system-features.md
cyborgyn Oct 18, 2021
92ed390
Merge branch 'devel' into feature/implement-ilcodes
cyborgyn Oct 18, 2021
76eeee7
Add NOT ILCode unit tests
cyborgyn Oct 19, 2021
2b7b595
Remove breaking unit tests
cyborgyn Oct 19, 2021
3cf87a5
Merge branch 'feature/implement-ilcodes' of github.com:cyborgyn/IL2C …
cyborgyn Oct 19, 2021
bc1476f
Refactor ArithmeticalConverters/ NotConverter according to guidelines
cyborgyn Oct 19, 2021
9753e44
Tried to add more Shr unit tests, but discovery fails then for all ot…
cyborgyn Oct 19, 2021
4790411
Remove method filter comment also for AssemblyPreparer
cyborgyn Oct 19, 2021
ca5706c
StargConverter simplification
cyborgyn Oct 19, 2021
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
2 changes: 1 addition & 1 deletion IL2C.Core/AssemblyPreparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public static PreparedInformations Prepare(TranslateContext translateContext)
// All types
type => true,
// The methods except type initializer.
cyborgyn marked this conversation as resolved.
Show resolved Hide resolved
method => !(method.IsConstructor && method.IsStatic));
method => true);
}
}
}
155 changes: 151 additions & 4 deletions IL2C.Core/ILConveters/ArithmeticalConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Mono.Cecil.Cil;

using IL2C.Translators;
using IL2C.Metadata;

namespace IL2C.ILConverters
{
Expand Down Expand Up @@ -78,7 +79,7 @@ public static ExpressionEmitter Prepare(BinaryOperators binaryOperator, DecodeCo
extractContext.GetSymbolName(si1)) };
}

// Double = (Float) % (Float)
// Double = (Float) op (Float)
if (si0.TargetType.IsFloatStackFriendlyType && si1.TargetType.IsFloatStackFriendlyType &&
(binaryOperator == BinaryOperators.Rem))
{
Expand All @@ -105,7 +106,7 @@ public static ExpressionEmitter Prepare(BinaryOperators binaryOperator, DecodeCo
}

// ByRef = (Int32) + (ByRef)
if (si0.TargetType.IsInt32StackFriendlyType && si1.TargetType.IsByReference &&
if (si0.TargetType.IsInt32StackFriendlyType && (si1.TargetType.IsByReference) &&
(binaryOperator == BinaryOperators.Add))
{
var result = decodeContext.PushStack(si1.TargetType);
Expand All @@ -118,7 +119,7 @@ public static ExpressionEmitter Prepare(BinaryOperators binaryOperator, DecodeCo
}

// ByRef = (IntPtr) + (ByRef)
if (si0.TargetType.IsIntPtrStackFriendlyType && si1.TargetType.IsByReference &&
if (si0.TargetType.IsIntPtrStackFriendlyType && (si1.TargetType.IsByReference) &&
(binaryOperator == BinaryOperators.Add))
{
var result = decodeContext.PushStack(si1.TargetType);
Expand All @@ -131,7 +132,7 @@ public static ExpressionEmitter Prepare(BinaryOperators binaryOperator, DecodeCo
}

// ByRef = (ByRef) +/- (Int32|IntPtr)
if (si0.TargetType.IsByReference &&
if ((si0.TargetType.IsByReference || si0.TargetType.IsArray) &&
(si1.TargetType.IsInt32StackFriendlyType || si1.TargetType.IsIntPtrStackFriendlyType) &&
((binaryOperator == BinaryOperators.Add) || (binaryOperator == BinaryOperators.Sub)))
{
Expand Down Expand Up @@ -234,4 +235,150 @@ public override ExpressionEmitter Prepare(DecodeContext decodeContext)
ArithmeticalConverterUtilities.BinaryOperators.Rem, decodeContext);
}
}

internal sealed class NotConverter : InlineNoneConverter
{
public override OpCode OpCode => OpCodes.Not;

public override ExpressionEmitter Prepare(DecodeContext decodeContext)
{
var si0 = decodeContext.PopStack();
Metadata.ILocalVariableInformation result;

if (si0.TargetType.IsFloatStackFriendlyType || si0.TargetType.IsByReference)
cyborgyn marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidProgramSequenceException(
"Invalid arithmetical NOT operation: Location={0}, Type0={1}",
decodeContext.CurrentCode.RawLocation,
si0.TargetType.FriendlyName);

if (si0.TargetType.IsInt32StackFriendlyType)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyborgyn The IsInt32StackFriendlyType property means "Can place evaluation stack these types, that's compatible int32 type on CLR".

Therefore use IsInt32Type, IsInt64Type, IsIntPtrType and IsUIntPtrType. By the way, can use TargetType directly onto decodeContext.PushStack(...) argument.

We need regression test for not converter, require these topics:

  • Apply not opcode each types int32 int64 and IntPtr.
  • Asserts equality not values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This way, it will be much nicer. I am getting to know more and more of your code, and how parts are interacting with each other. I need to note, when I uncomment IntPtr unit tests, and also insert those into the IL, a UnitTestFramework fails to discover any tests, even though the produced DLL can be verified to be OK with DotPeek.

{
result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.Int32Type);
}
else
{ // Int64 = ~(Int64)
result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.Int64Type);
}

return (extractContext, _) => new[] { string.Format(
"{0} = ~{1}",
extractContext.GetSymbolName(result),
extractContext.GetSymbolName(si0)) };
}
}

internal enum ShiftDirection
{
Left, Right
}

internal abstract class ShiftConverter : InlineNoneConverter
{
public abstract ShiftDirection Direction { get; }

public ExpressionEmitter Prepare(DecodeContext decodeContext, Func<ILocalVariableInformation, string> cast)
{
var si1 = decodeContext.PopStack();
var si0 = decodeContext.PopStack();

if (si0.TargetType.IsFloatStackFriendlyType || si0.TargetType.IsByReference || !si1.TargetType.IsInt32Type)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyborgyn Likely not opcode's integer type limitation.

ECMA-335: III.3.58 shl - shift integer left section is written:

The shl instruction shifts value (int32, int64 or native int) left by the number of bits
specified by shiftAmount. shiftAmount is of type int32 or native int. 

shiftAmount (si1) tricky specification, it's excepted int64.

We need regression test for shl and shr converter, require these topics:

  • Apply shl/shr opcode each types int32 int64 and IntPtr combination with shiftAmount by int32 and IntPtr.
  • Asserts equality constant values.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already added quite an amount of tests especially for shr & shr.un, as they were not that intuitive when they sign extend, and when not. I found this:
// Arithmetic Shift right (with sign)
// Warning!!! MSIL always operates for bit storage on Int32, or Int64
// and as it seems, smaller unsigned values (uint8 & uint16) will never
// be able to set the int32 highest bit => they will operate as logical shift right.
// On the other hand: uint32 and uint64 are still operated as arithmetic shift.

Will try to add the native int shift parameter as test, good point, thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I needed to comment them out again.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I will take a look on my environment, could you tell me explicit commit id reproducible?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it valid on newest commit on branch feature/implement-ilcodes (ca5706c) ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented: #104 (comment)

throw new InvalidProgramSequenceException(
"Invalid arithmetical NOT operation: Location={0}, Type0={1}",
decodeContext.CurrentCode.RawLocation,
si0.TargetType.FriendlyName);

var result = decodeContext.PushStack(si0.TargetType);

if (cast == null)
{
return (extractContext, _) => new[] { string.Format(
"{0} = {1} {2} {3}",
extractContext.GetSymbolName(result),
extractContext.GetSymbolName(si0),
Direction == ShiftDirection.Left ? "<<" : ">>",
extractContext.GetSymbolName(si1)) };
}
else
{
return (extractContext, _) => new[] { string.Format(
"{0} = ({4}){1} {2} {3}",
extractContext.GetSymbolName(result),
extractContext.GetSymbolName(si0),
Direction == ShiftDirection.Left ? "<<" : ">>",
extractContext.GetSymbolName(si1),
cast(si0)) };
}
}
}

internal sealed class ShiftRightConverter : ShiftConverter
{
public override OpCode OpCode => OpCodes.Shr;

public override ShiftDirection Direction => ShiftDirection.Right;

public override ExpressionEmitter Prepare(DecodeContext decodeContext)
{
return Prepare(decodeContext, si => si.TargetType.IsInt32StackFriendlyType ? "int32_t" : "int64_t");
}
}

internal sealed class ShiftLeftConverter : ShiftConverter
{
public override OpCode OpCode => OpCodes.Shl;

public override ShiftDirection Direction => ShiftDirection.Left;

public override ExpressionEmitter Prepare(DecodeContext decodeContext)
{
return Prepare(decodeContext, null);
}
}

internal sealed class ShiftRightUnConverter : ShiftConverter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyborgyn Ouch, I forgot signed integer flag :)

shr opcode will understand signed bit on MSB:

shr 10000010
result: 11000001

It case, we can use C language shift operator with signed integer type (int32_t, int64_t and intptr_t).
But shr.un opcode:

shr.un 10000010
result: 01000001

Strictly ignores MSB bit and insert zero. ECMA-335 III.3.60 shr.un - shift integer right, unsigned section:

shr.un inserts a zero bit on each shift.

On IL2C side, The C language expression is having to write temporary casting unsigned integers explicitly, like:

stack0 = (int32_t)(((uint32_t)stack0) >> shiftAmount);

We need regression test for shr.un (and shr) converter, require these topics:

  • Positive and negative value makes valid shift result.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the temporary casting, if you look closely, and the tests are also walking through all of those tricky cases. One thing I forget, is negative shift (I never tried it, never even thought about it on any platform/language before). :) For the shift left, no magic happens, this is why it is omitted there.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I found it on branch latest commit. (I took and read commit from bottom of branch...)

I approved it!

Bit off topic: The CLR (.NET Framework CLR and Core CLR) behavior is funny. Because shr and shr.un opcode behavior isn't defined with any int32, int64 and IntPtr on ECMA-335.

The shr.un instruction shifts value (int32, int 64 or native int) right by the number of bits
specified by shiftAmount.

But real regression test on CLR is passed...

{
public override OpCode OpCode => OpCodes.Shr_Un;

public override ShiftDirection Direction => ShiftDirection.Right;

public override ExpressionEmitter Prepare(DecodeContext decodeContext)
{
return Prepare(decodeContext, si => si.TargetType.IsInt32StackFriendlyType ? "uint32_t" : "uint64_t");
}
}

internal sealed class NegConverter : InlineNoneConverter
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cyborgyn The 'neg' opcode calculus is serious with floating point type...

ECMA-335 III.3.50 neg - negate :

Negation of integral values is standard twos-complement negation. In particular, negating the
most negative number (which does not have a positive counterpart) yields the most negative
number. To detect this overflow use the sub.ovf instruction instead (i.e., subtract from 0).
Negating a floating-point number cannot overflow; negating NaN returns NaN.

If we write the same expression in C language, I feel that the result may not be the same between C and IL (C#). Also, the current IL2C doesn't consider NaN value at all, so it seems that it's time to think about it....

At this point, I think it's okay to include TODO comments and write with comment out regression test case.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote ckfinite(float/double) opcode implementation in C to conform with these tests https://github.com/dotnet/runtime/blob/f9a2449/src/tests/JIT/IL_Conformance/Old/Base/ckfinite.il#L52.

Here is the C version: https://godbolt.org/z/vxMYrvb8d passing all conformance tests. coreclr uses the same approach by checking if all exponent bits are 1 to find out if float/double number is infinite or NaN, and emits ArithmeticException.

Was looking at the place to implement it in IL2C. Note: in this case, we need the raw single/double number in binary storage variable (e.g. uint32_t and uint64_t respectively) as it is represented in IL (IEEE 754 floating-point); without converting it to C float where it may or may not conform to IEEE 754 fp (ISO C99 is relaxed on the format...).

Feel free to implement ckfinite too, as this one is pretty simple. Otherwise, I'll take a stab at it; once this PR is merged. 🙂

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good news! I will include NaN handler and ckfinite implementation when after merged this PR. (Memoized: #112)

{
public override OpCode OpCode => OpCodes.Neg;

public override ExpressionEmitter Prepare(DecodeContext decodeContext)
{
var si0 = decodeContext.PopStack();
Metadata.ILocalVariableInformation result;

if (si0.TargetType.IsByReference)
throw new InvalidProgramSequenceException(
"Invalid arithmetical NEG operation: Location={0}, Type0={1}",
decodeContext.CurrentCode.RawLocation,
si0.TargetType.FriendlyName);

if (si0.TargetType.IsInt32StackFriendlyType)
{
result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.Int32Type);
}
else if (si0.TargetType.IsInt64StackFriendlyType)
{ // Int64 = -(Int64)
result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.Int64Type);
}
else
{ // double = -(double)
result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.DoubleType);
}

return (extractContext, _) => new[] { string.Format(
"{0} = -{1}",
extractContext.GetSymbolName(result),
extractContext.GetSymbolName(si0)) };
}
}
}
22 changes: 22 additions & 0 deletions IL2C.Core/ILConveters/BranchConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,28 @@ public override ExpressionEmitter Prepare(
}
}

internal sealed class Bgt_UnConverter : ShortInlineBrTargetConverter
{
public override OpCode OpCode => OpCodes.Bgt_Un;

public override ExpressionEmitter Prepare(
ICodeInformation operand, DecodeContext decodeContext)
{
return BranchExpressionUtilities.ApplyBinary(operand, ">", decodeContext);
}
}

internal sealed class Bgt_Un_sConverter : ShortInlineBrTargetConverter
{
public override OpCode OpCode => OpCodes.Bgt_Un_S;

public override ExpressionEmitter Prepare(
ICodeInformation operand, DecodeContext decodeContext)
{
return BranchExpressionUtilities.ApplyBinary(operand, ">", decodeContext);
}
}

internal sealed class BleConverter : ShortInlineBrTargetConverter
{
public override OpCode OpCode => OpCodes.Ble;
Expand Down
12 changes: 12 additions & 0 deletions IL2C.Core/ILConveters/ConditionalConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public static ExpressionEmitter Prepare(
extractContext.GetSymbolName(si1)) };
}

if (si0.TargetType.IsFloatStackFriendlyType &&
si1.TargetType.IsFloatStackFriendlyType)
{
var result = decodeContext.PushStack(decodeContext.PrepareContext.MetadataContext.Int32Type);
return (extractContext, _) => new[] { string.Format(
"{0} = (double){1} {2} (double){3}",
extractContext.GetSymbolName(result),
extractContext.GetSymbolName(si0),
oper,
extractContext.GetSymbolName(si1)) };
}

if ((si0.TargetType.IsIntPtrStackFriendlyType || !si0.TargetType.IsValueType || si0.TargetType.IsByReference || si0.TargetType.IsUntypedReferenceType) &&
(si1.TargetType.IsIntPtrStackFriendlyType || !si1.TargetType.IsValueType || si1.TargetType.IsByReference || si0.TargetType.IsUntypedReferenceType))
{
Expand Down
36 changes: 30 additions & 6 deletions IL2C.Core/ILConveters/LdindConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static ExpressionEmitter Prepare(DecodeContext decodeContext)
{
var siFrom = decodeContext.PopStack();

// Requre only managed refs
if (!siFrom.TargetType.IsByReference)
// Require only managed refs
if (!(siFrom.TargetType.IsByReference || siFrom.TargetType.IsArray))
{
throw new InvalidProgramSequenceException(
"Invalid managed reference: Location={0}, StackType={1}",
Expand All @@ -43,10 +43,22 @@ public static ExpressionEmitter Prepare(DecodeContext decodeContext)
var targetType = siFrom.TargetType.ElementType;
var symbol = decodeContext.PushStack(targetType);

return (extractContext, _) => new[] { string.Format(
"{0} = *{1}",
extractContext.GetSymbolName(symbol),
extractContext.GetSymbolName(siFrom)) };
if (siFrom.TargetType.IsArray)
{
return (extractContext, _) => new[] { string.Format(
"{0} = *(({2}*){1})",
extractContext.GetSymbolName(symbol),
extractContext.GetSymbolName(siFrom),
targetType.CLanguageTypeName
) };
}
else
{
return (extractContext, _) => new[] { string.Format(
"{0} = *{1}",
extractContext.GetSymbolName(symbol),
extractContext.GetSymbolName(siFrom)) };
}
}
}

Expand Down Expand Up @@ -159,4 +171,16 @@ public override ExpressionEmitter Prepare(DecodeContext decodeContext)
return LdindConverterUtilities.Prepare(decodeContext);
}
}

// It is just a generalized Ldind. C compiler handles struct copy
internal sealed class LdobjConverter : InlineTypeConverter
{
public override OpCode OpCode => OpCodes.Ldobj;

public override ExpressionEmitter Prepare(Metadata.ITypeInformation operand, DecodeContext decodeContext)
{
// Note: note really need the operand in this case, since IL2C knows the type already
return LdindConverterUtilities.Prepare(decodeContext);
}
}
}
2 changes: 1 addition & 1 deletion IL2C.Core/ILConveters/LdtokenConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal sealed class LdtokenConverters : InlineMemberConverter
public override ExpressionEmitter Prepare(
IMemberInformation member, DecodeContext decodeContext)
{
// ECMA-335 III.4.17 ldtoken - load the runt ime representat ion of a metadata
// ECMA-335 III.4.17 ldtoken - load the runtime representation of a metadata

var field = member as IFieldInformation;
if (field != null)
Expand Down
69 changes: 69 additions & 0 deletions IL2C.Core/ILConveters/StargConverters .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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)
cyborgyn marked this conversation as resolved.
Show resolved Hide resolved
{
var si = decodeContext.PopStack();
var parameter = decodeContext.Method.Parameters[parameterIndex];
var targetType = isReference ? parameter.TargetType.MakeByReference() : parameter.TargetType;

// TODO: check target and source type

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(si)) };
}
}

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);
}
}
}
Loading