FormatMemberName() public static method

public static FormatMemberName ( IMemberDefinition member ) : string
member IMemberDefinition
return string
Beispiel #1
0
        public void EmitMethodDefinition(DecompilerContext context, MethodReference methodRef, MethodDefinition method, IAstEmitter astEmitter, bool stubbed, JSRawOutputIdentifier dollar, MethodInfo methodInfo = null)
        {
            // Skip Main() and emit it at the footer
            if (Assembly.EntryPoint == method)
            {
                // HACK: Store this so we can use it to emit the entry point's body later
                EntryPointAstEmitter = astEmitter;
                return;
            }

            var name = WasmUtil.FormatMemberName(method);

            var sExport = "Wasm.Module.ExportAttribute";

            methodInfo = methodInfo ?? Translator.TypeInfoProvider.GetMethod(methodRef);

            if (methodInfo != null && methodInfo.Metadata.HasAttribute(sExport))
            {
                var exportName = method.Name;
                var ap         = methodInfo.Metadata.GetAttributeParameters(sExport);
                if ((ap.Count == 1) && (ap[0].Value is string))
                {
                    exportName = (string)ap[0].Value;
                }

                Switch(PrecedingType.Export);

                Formatter.WriteRaw("(export \"{0}\" ${1})", exportName, name);
                Formatter.ConditionalNewLine();
            }
        }
Beispiel #2
0
        public void VisitNode(JSInvocationExpression ie)
        {
            var jsm = ie.JSMethod;

            if (jsm == null)
            {
                Console.WriteLine("Can't translate non-JSMethod {0}", ie);
                Formatter.WriteSExpr("untranslatable.call");
                return;
            }
            else if (!jsm.Method.IsStatic)
            {
                Console.WriteLine("Can't translate instance call {0}", ie);
                Formatter.WriteSExpr("untranslatable.call");
                return;
            }

            var methodDef  = jsm.Reference.Resolve();
            var memberName = WasmUtil.FormatMemberName(methodDef);

            Formatter.WriteSExpr("call", (_) => {
                _.WriteRaw("${0} ", memberName);
                EmitArgumentList(_, ie.Arguments, false);
            });
        }
Beispiel #3
0
        private void EmitFunctionTable()
        {
            if (MethodTable.Count == 0)
            {
                return;
            }

            Formatter.NewLine();

            Formatter.WriteRaw(";; function table");
            Formatter.NewLine();

            Formatter.WriteRaw("(table ");
            Formatter.Indent();
            Formatter.NewLine();

            foreach (var md in MethodTable)
            {
                var name = WasmUtil.FormatMemberName(md);

                Formatter.ConditionalNewLine();
                Formatter.WriteRaw("${0}", name);
            }

            Formatter.Unindent();
            Formatter.ConditionalNewLine();
            Formatter.WriteRaw(")");
            Formatter.NewLine();
        }
Beispiel #4
0
        private void Assign(JSExpression target, JSExpression value)
        {
            var leftVar   = target as JSVariable;
            var leftField = target as JSFieldAccess;
            var leftProp  = target as JSPropertyAccess;

            if (leftVar != null)
            {
                Formatter.WriteSExpr(
                    "set_local",
                    (_) => {
                    _.WriteRaw("${0} ", WasmUtil.EscapeIdentifier(leftVar.Name));
                    Visit(value);
                },
                    lineBreakAfter: true
                    );
            }
            else if (leftField != null)
            {
                Formatter.WriteRaw(
                    "(call $__set_{0} ",
                    EscapedName(leftField)
                    );

                if (!leftField.Field.Field.IsStatic)
                {
                    Visit(leftField.Target);
                    Formatter.WriteRaw(" ");
                }

                Visit(value);
                Formatter.WriteRaw(")");
                Formatter.NewLine();
            }
            else if (leftProp != null)
            {
                var method     = leftProp.OriginalMethod;
                var memberName = WasmUtil.FormatMemberName(method.Reference.Resolve());

                Formatter.WriteSExpr(
                    "call",
                    (_) => {
                    _.WriteRaw("${0} ", memberName);
                    Visit(value);
                },
                    lineBreakAfter: true
                    );
            }
            else
            {
                Console.WriteLine("Invalid assignment target {0} {1}", target.GetType().Name, target);
                return;
            }
        }
Beispiel #5
0
        public void VisitNode(JSPropertyAccess pa)
        {
            if (pa.IsWrite)
            {
                throw new Exception("Unhandled property write: " + pa);
            }

            var method     = pa.OriginalMethod;
            var memberName = WasmUtil.FormatMemberName(method.Reference.Resolve());

            Formatter.WriteSExpr("call", (_) => {
                _.WriteRaw("${0}", memberName);
            });
        }
Beispiel #6
0
        private void Assign(JSExpression target, JSExpression value)
        {
            var leftVar   = target as JSVariable;
            var leftField = target as JSFieldAccess;
            var leftProp  = target as JSPropertyAccess;

            if (leftVar != null)
            {
                Formatter.WriteSExpr(
                    "setlocal",
                    (_) => {
                    _.WriteRaw("${0} ", WasmUtil.EscapeIdentifier(leftVar.Name));
                    Visit(value);
                },
                    lineBreakAfter: true
                    );
            }
            else if (leftField != null)
            {
                Formatter.WriteSExpr(
                    // what?????
                    "store_global",
                    (_) => {
                    _.WriteRaw("${0} ", EscapedName(leftField));
                    Visit(value);
                },
                    lineBreakAfter: true
                    );
            }
            else if (leftProp != null)
            {
                var method     = leftProp.OriginalMethod;
                var memberName = WasmUtil.FormatMemberName(method.Reference.Resolve());

                Formatter.WriteSExpr(
                    "call",
                    (_) => {
                    _.WriteRaw("${0} ", memberName);
                    Visit(value);
                },
                    lineBreakAfter: true
                    );
            }
            else
            {
                Console.WriteLine("Invalid assignment target {0}", target);
                return;
            }
        }
Beispiel #7
0
        private void VisitFakeMethod(JSInvocationExpression ie, JSFakeMethod fakeMethod)
        {
            if (TypeUtil.IsDelegateType(fakeMethod.ReturnType) && fakeMethod.Name == "New")
            {
                var type = (JSType)ie.Arguments[0];
                // FIXME: What the heck is in arg1????
                var method = UnwrapDeferred <JSMethod>(ie.Arguments[2]);

                var methodDef  = method.Reference.Resolve();
                var memberName = WasmUtil.FormatMemberName(methodDef);

                Formatter.WriteRaw("(i32.const {0} (; {1} ;))", AssemblyEmitter.GetFunctionIndex(methodDef), methodDef.FullName);
                return;
            }

            Console.WriteLine("Can't translate fake method {0}", ie);
            Formatter.WriteSExpr("untranslatable.call");
            return;
        }
Beispiel #8
0
        public void VisitNode(JSInvocationExpression ie)
        {
            var jsm = ie.JSMethod;

            if (jsm == null)
            {
                var d = ie.Method as JSDotExpression;
                if (d != null)
                {
                    var m = d.Member as JSFakeMethod;
                    if (m != null)
                    {
                        VisitFakeMethod(ie, m);
                        return;
                    }
                }

                Console.WriteLine("Can't translate non-JSMethod {0}", ie);
                Formatter.WriteSExpr("untranslatable.call");
                return;
            }
            else if (!jsm.Method.IsStatic)
            {
                Console.WriteLine("Can't translate instance call {0}", ie);
                Formatter.WriteSExpr("untranslatable.call");
                return;
            }

            var methodDef  = jsm.Reference.Resolve();
            var memberName = WasmUtil.FormatMemberName(methodDef);

            var manyArgs = ie.Arguments.Count > 2;

            Formatter.WriteSExpr("call", (_) => {
                _.WriteRaw("${0} ", memberName);
                EmitArgumentList(_, ie.Arguments, manyArgs);
            }, lineBreakInside: manyArgs);
        }
Beispiel #9
0
        public void EmitFunctionBody(IAstEmitter astEmitter, MethodDefinition method, JSFunctionExpression function)
        {
            // Skip Main() and emit it at the footer
            if (Assembly.EntryPoint == method)
            {
                // HACK: Store this so we can use it to emit the entry point's body later
                EntryPointAstEmitter = astEmitter;
                return;
            }

            var name = WasmUtil.FormatMemberName(method);

            Switch(PrecedingType.Function, true);

            Formatter.WriteRaw("(func ${0}", name);
            Formatter.Indent();
            Formatter.NewLine();

            int v = 0;

            foreach (var kvp in function.AllVariables)
            {
                var variable = kvp.Value;

                var type = WasmUtil.PickTypeKeyword(variable.IdentifierType);
                if (type != null)
                {
                    Formatter.WriteRaw(
                        "({0} ${1} {2}) ",
                        variable.IsParameter
                            ? "param"
                            : "local",
                        WasmUtil.EscapeIdentifier(kvp.Key), type
                        );

                    if (v++ >= 3)
                    {
                        v = 0;
                        Formatter.NewLine();
                    }
                }
            }

            if (function.LabelGroupCount > 0)
            {
                Formatter.NewLine();
            }
            for (var i = 0; i < function.LabelGroupCount; i++)
            {
                Formatter.WriteRaw("(local $currentLabel_{0} i32) ", i);
            }

            var returnType = WasmUtil.PickTypeKeyword(method.ReturnType);

            if (returnType != "void")
            {
                Formatter.NewLine();
                Formatter.WriteRaw("(result {0})", returnType);
            }

            Formatter.ConditionalNewLine();
            Formatter.NewLine();

            astEmitter.Emit(function.Body);

            Formatter.ConditionalNewLine();
            Formatter.Unindent();
            Formatter.WriteRaw(")");

            Formatter.NewLine();
        }
Beispiel #10
0
        private void EmitFieldIntrinsics(int heapSize)
        {
            // FIXME: Gross
            var tis = (ITypeInfoSource)Translator.TypeInfoProvider;

            Formatter.WriteRaw(";; Compiler-generated field accessors");
            Formatter.NewLine();

            foreach (var kvp in FieldTable.OrderBy(kvp => kvp.Value.Offset))
            {
                var fd         = kvp.Value.Field;
                var fi         = (FieldInfo)tis.Get(fd);
                var name       = WasmUtil.FormatMemberName(fi.Member);
                var typeSystem = fd.FieldType.Module.TypeSystem;

                // HACK
                var baseAddressParam = new JSVariable("address", typeSystem.Int32, null);
                // HACK
                var valueParam = new JSVariable("value", fd.FieldType, null);

                JSExpression address;
                if (fd.IsStatic)
                {
                    address = JSLiteral.New(kvp.Value.Offset + heapSize);
                }
                else
                {
                    address = new JSBinaryOperatorExpression(
                        JSOperator.Add,
                        baseAddressParam,
                        JSLiteral.New(kvp.Value.Offset),
                        typeSystem.Int32
                        );
                }

                Formatter.ConditionalNewLine();
                Formatter.WriteRaw(
                    "(func $__get_{0} (result {1}){2}(return ",
                    name,
                    WasmUtil.PickTypeKeyword(fd.FieldType),
                    fd.IsStatic
                        ? " "
                        : " (param $address i32) "
                    );

                var gm = new GetMemory(
                    fd.FieldType, /* FIXME: Align addresses */ false,
                    address
                    );

                // HACK
                EntryPointAstEmitter.Emit(gm);

                Formatter.WriteRaw(") )");

                if (fd.IsInitOnly)
                {
                    continue;
                }

                Formatter.NewLine();
                Formatter.WriteRaw(
                    "(func $__set_{0}{2}(param $value {1}) ",
                    name,
                    WasmUtil.PickTypeKeyword(fd.FieldType),
                    fd.IsStatic
                        ? " "
                        : " (param $address i32) "
                    );
                Formatter.Indent();
                Formatter.NewLine();
                var sm = new SetMemory(
                    fd.FieldType, /* FIXME: Align addresses */ false,
                    address, valueParam
                    );

                // HACK
                EntryPointAstEmitter.Emit(sm);

                Formatter.Unindent();
                Formatter.ConditionalNewLine();
                Formatter.WriteRaw(")");
            }

            Formatter.NewLine();
            Formatter.NewLine();
        }
Beispiel #11
0
 private string EscapedName(FieldInfo fi)
 {
     return(WasmUtil.FormatMemberName(fi.Member));
 }