Skip to content

Commit

Permalink
Closed #158. Added optional NaNToken patching for ldc.r8/4 -nan(ind) …
Browse files Browse the repository at this point in the history
…& float64/32(-nan(ind))

```
.field = float32(-nan(ind)) -> 0xFFC00000
.field = float64(-nan(ind)) -> 0xFFF8000000000000

ldc.r8 -nan(ind) -> 00 00 00 00 00 00 F8 FF
ldc.r4 -nan(ind) -> 00 00 C0 FF
```
  • Loading branch information
3F committed Jun 7, 2020
1 parent 61defd6 commit 9f26c9b
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 7 deletions.
53 changes: 51 additions & 2 deletions RGiesecke.DllExport/Parsing/Actions/ClassParserAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public override void Execute(ParserStateValues state, string trimmedLine)
}
}

/// <summary>
/// https://github.com/3F/DllExport/issues/128
/// https://github.com/3F/DllExport/issues/158
/// </summary>
/// <param name="state"></param>
/// <param name="raw">raw definition of the .field</param>
/// <returns>true if processed</returns>
Expand Down Expand Up @@ -80,15 +84,39 @@ private bool TreatField(ParserStateValues state, ref string raw)

if(m.Success)
{
raw = new string(' ', 2) + raw.Substring(0, m.Index) + GetFloatDef(m);
raw = GetInst(raw.Substring(0, m.Index) + GetFloatInfDef(m));
return true;
}
}

if((Parser.InputValues.Patches & PatchesType.NaNToken) == PatchesType.NaNToken)
{
// .field public static literal float32 'NaN' = float32(-nan(ind))
// .field public static literal float64 'NaN' = float64(-nan(ind))

Match m = Regex.Match
(
raw,
@"=\s*
float(?:(?'x64'64)|32)
\(
-nan\(ind\)
\)
",
RegexOptions.IgnorePatternWhitespace
);

if(m.Success)
{
raw = GetInst(raw.Substring(0, m.Index) + GetNaNDef(m));
return true;
}
}

return false;
}

private static string GetFloatDef(Match fld)
private static string GetFloatInfDef(Match fld)
{
var sb = new StringBuilder(4);
sb.Append("= float");
Expand All @@ -114,5 +142,26 @@ private static string GetFloatDef(Match fld)

return sb.ToString();
}

private static string GetNaNDef(Match fld)
{
var sb = new StringBuilder(3);
sb.Append("= float");

if(fld.Groups["x64"].Success)
{
sb.Append("64");
sb.Append("(0xFFF8000000000000)");
}
else
{
sb.Append("32");
sb.Append("(0xFFC00000)");
}

return sb.ToString();
}

private static string GetInst(string l) => new string(' ', 2) + l;
}
}
50 changes: 48 additions & 2 deletions RGiesecke.DllExport/Parsing/Actions/MethodParserAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public override void Execute(ParserStateValues state, string trimmedLine)
}
}

// https://github.com/3F/DllExport/issues/128
// https://github.com/3F/DllExport/issues/158
private bool TreatIL(ParserStateValues state, ref string raw)
{
if((Parser.InputValues.Patches & PatchesType.InfToken) == PatchesType.InfToken)
Expand All @@ -59,15 +61,38 @@ private bool TreatIL(ParserStateValues state, ref string raw)

if(m.Success)
{
raw = new string(' ', 4) + raw.Substring(0, m.Index) + GetFloatDef(m);
raw = GetInst(raw.Substring(0, m.Index) + GetFloatInfDef(m));
return true;
}
}

if((Parser.InputValues.Patches & PatchesType.NaNToken) == PatchesType.NaNToken)
{
// ldc.r8 -nan(ind)
// ldc.r4 -nan(ind)

Match m = Regex.Match
(
raw,
@"
ldc.r(?:(?'x64'8)|4)
\s*
-nan\(ind\)
",
RegexOptions.IgnorePatternWhitespace
);

if(m.Success)
{
raw = GetInst(raw.Substring(0, m.Index) + GetNaNDef(m));
return true;
}
}

return false;
}

private static string GetFloatDef(Match fld)
private static string GetFloatInfDef(Match fld)
{
var sb = new StringBuilder(4);
sb.Append("ldc.r");
Expand All @@ -93,5 +118,26 @@ private static string GetFloatDef(Match fld)

return sb.ToString();
}

private static string GetNaNDef(Match fld)
{
var sb = new StringBuilder(3);
sb.Append("ldc.r");

if(fld.Groups["x64"].Success)
{
sb.Append("8 ");
sb.Append("(00 00 00 00 00 00 F8 FF)");
}
else
{
sb.Append("4 ");
sb.Append("(00 00 C0 FF)");
}

return sb.ToString();
}

private static string GetInst(string l) => new string(' ', 4) + l;
}
}
10 changes: 10 additions & 0 deletions RGiesecke.DllExport/PatchesType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@ public enum PatchesType: long
/// https://github.com/3F/DllExport/issues/128
/// </summary>
InfToken = 0x01,

/// <summary>
/// Affects ldc.r8; ldc.r4; .field;
///
/// -nan(ind) to 00 00 C0 FF
/// 00 00 00 00 00 00 F8 FF
///
/// https://github.com/3F/DllExport/issues/158
/// </summary>
NaNToken = 0x02,
}
}
15 changes: 15 additions & 0 deletions Wizard/UI/Controls/ProjectItemControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Wizard/UI/Controls/ProjectItemControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,16 @@ private PatchesType GetPatchesType()
{
PatchesType patches = PatchesType.None;

if(chkInfPatching.Checked) {
patches |= PatchesType.InfToken;
}
if(chkInfPatching.Checked) { patches |= PatchesType.InfToken; }
if(chkNaNPatching.Checked) { patches |= PatchesType.NaNToken; }

return patches;
}

private void SetPatchesType(PatchesType type)
{
chkInfPatching.Checked = (type & PatchesType.InfToken) == PatchesType.InfToken;
chkNaNPatching.Checked = (type & PatchesType.NaNToken) == PatchesType.NaNToken;
}

private void InstalledStatus(bool status)
Expand Down

0 comments on commit 9f26c9b

Please sign in to comment.