Skip to content

Commit

Permalink
Merge pull request mono/mono#14309 from lewing/icall-branch
Browse files Browse the repository at this point in the history
[wasm] generate icall_trampoline_dispatch as nested branches

Commit migrated from mono/mono@4cc2c19
  • Loading branch information
lewing authored May 3, 2019
2 parents 7f3d0cc + 51b4a4f commit a2ac2bc
Show file tree
Hide file tree
Showing 2 changed files with 762 additions and 196 deletions.
65 changes: 52 additions & 13 deletions src/mono/mono/mini/m2n-gen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Collections.Generic;

class EmitCtx
{
Expand Down Expand Up @@ -110,7 +112,6 @@ class Driver {
"LII",
"VID",
"VILLI",

"DID",
"DIDD",
"FIF",
Expand All @@ -131,7 +132,7 @@ static string TypeToSigType (char c) {
}
}

static void Main () {
static void Main (string[] args) {
Console.WriteLine ("/*");
Console.WriteLine ("* DON'T EDIT THIS FILE");
Console.WriteLine ("* This file was generated by m2n-gen.cs - use it instead.");
Expand Down Expand Up @@ -177,18 +178,56 @@ static void Main () {

Console.WriteLine ("static void\nicall_trampoline_dispatch (const char *cookie, void *target_func, InterpMethodArguments *margs)");
Console.WriteLine ("{");
for (int i = 0; i < cookies.Length; ++i) {
var c = cookies [i];
Console.Write ("\t");
if (i > 0)
Console.Write ("else ");
Console.WriteLine ($"if (!strcmp (\"{c}\", cookie))");
Console.WriteLine ($"\t\twasm_invoke_{c.ToLower ()} (target_func, margs);");

if (args.Any(a => a == "--flat")) {
for (int i = 0; i < cookies.Length; ++i) {
var c = cookies [i];
Console.Write ("\t");
if (i > 0)
Console.Write ("else ");
Console.WriteLine ($"if (!strcmp (\"{c}\", cookie))");
Console.WriteLine ($"\t\twasm_invoke_{c.ToLower ()} (target_func, margs);");
}
Console.WriteLine ("\telse {");
Console.WriteLine ("\t\tg_error (\"CANNOT HANDLE COOKIE %s\\n\", cookie);");
Console.WriteLine ("\t}");
} else {
Array.Sort (cookies);
WritePartition (cookies, 0);
Console.WriteLine ("\tg_error (\"CANNOT HANDLE COOKIE %s\\n\", cookie);");
}
Console.WriteLine ("\telse {");
Console.WriteLine ("\t\tprintf (\"CANNOT HANDLE COOKIE %s\\n\", cookie);");
Console.WriteLine ("\t\tg_assert (0);");
Console.WriteLine ("\t}");

Console.WriteLine ("}");
}

static void WritePartition (IEnumerable<string> set, int pos = 0, int depth = 0) {
var prefix = "\t";
for (var c = 0; c < pos; c++)
prefix += "\t";

var checks = 0;

var groups = set.OrderBy (s => -s.Length).Where (s => s.Length > pos).GroupBy (s => s.Skip(pos).First().ToString());
foreach (var g in groups) {
Console.WriteLine ($"{prefix}{Elif (checks)} (cookie[{pos}] == '{g.Key}') {{");
WritePartition (g.ToList (), pos + 1, checks + depth);
Console.WriteLine ($"{prefix}}}");
checks++;
}

var hits = set.Where (s => s.Length == pos);
if (hits.Any ()) {
Console.WriteLine ($"{prefix}{Elif (checks++)} (cookie[{pos}] == '\\0') {{");

var h = hits.First ();
Console.WriteLine ($"{prefix}\t// found: {h} depth {pos + checks + depth}");
Console.WriteLine ($"{prefix}\twasm_invoke_{h.ToLower ()} (target_func, margs);");
Console.WriteLine ($"{prefix}\treturn;");
Console.WriteLine ($"{prefix}}}");
}

string Elif (int c) {
return c == 0 ? "if" : "else if";
}
}
}
Loading

0 comments on commit a2ac2bc

Please sign in to comment.