Пример #1
0
        /// <summary>
        /// Generates metadata for Declared Types, from TypeExtraInfo
        /// </summary>
        public void GenerateTypesMetadata()
        {
            FlatArrayBuilder fab = new FlatArrayBuilder();
            foreach (KeyValuePair<INamedTypeSymbol, TypeExtraInfo> kvp in TypeExtraInfo)
            {
                LS2IL.TypeExtraInfo.ClassMetadataGenerator cmg = kvp.Value.MetadataGenerator;
                if (!cmg.IsLibrary)
                {
                    fab.Add(cmg.GetFlatValue());
                }
            }

            MetaValues.Add("Declared Types", fab.GetFlatValue());
        }
Пример #2
0
            public void Add(MethodDeclarationSyntax node)
            {
                FlatArrayBuilder fab = new FlatArrayBuilder();

                MethodSymbol s = Chunk.Model.GetDeclaredSymbol(node);
                fab.Add(FlatValue.Int32(s.IsStatic ? (int)ClassMemberType.StaticMethod : (int)ClassMemberType.Method));

                fab.Add(FlatValue.String(s.GetFullyQualifiedName()));

                MethodSymbol ms = Chunk.Model.GetDeclaredSymbol(node);

                Function f;
                if (!Chunk.Functions.TryGetValue(ms, out f))
                {
                    throw new NotImplementedException("Method not found " + ms.ToString());
                }

                fab.Add(FlatValue.Int32(f.NumFunction));

                Members.Add(fab.GetFlatValue());
            }
Пример #3
0
            public new FlatValue GetFlatValue()
            {
                FlatTableBuilder ftb = new FlatTableBuilder();
                ftb.Add("Name", FlatValue.String(Class.GetFullyQualifiedName()));
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();
                    foreach (FlatValue fv in Members)
                    {
                        fab.Add(fv);
                    }
                    ftb.Add("Members", fab.GetFlatValue());
                }

                return ftb.GetFlatValue();
            }
Пример #4
0
            public void Add(FieldDeclarationSyntax node)
            {
                // add field info
                Add(node.Modifiers, node.Declaration);

                /*
                // Summary:
                //     Gets the attribute declaration list.
                public override SyntaxList<AttributeListSyntax> AttributeLists { get; }
                public override VariableDeclarationSyntax Declaration { get; }
                //
                // Summary:
                //     Gets the modifier list.
                public override SyntaxTokenList Modifiers { get; }
                public override SyntaxToken SemicolonToken { get; }
                 */

                // add to metadata for runtime type building
                FlatArrayBuilder fab = new FlatArrayBuilder();

                fab.Add(FlatValue.Int32(node.Modifiers.ToString().Contains("static") ? (int)ClassMemberType.StaticField : (int)ClassMemberType.Field));
                TypeInfo ti = Chunk.Model.GetTypeInfo(node.Declaration.Type);

                fab.Add(FlatValue.String(ti.ConvertedType.GetFullyQualifiedName()));

                {
                    FlatArrayBuilder varList = new FlatArrayBuilder();
                    foreach (VariableDeclaratorSyntax vds in node.Declaration.Variables)
                    {
                        if (vds.ArgumentList != null)
                        {
                            throw new NotImplementedException("array field");
                        }
                        varList.Add(FlatValue.String(vds.Identifier.ToString()));
                    }

                    fab.Add(varList.GetFlatValue());
                }

                Members.Add(fab.GetFlatValue());
            }
Пример #5
0
            public void Add(PropertyDeclarationSyntax node)
            {
                Symbol s = Chunk.Model.GetDeclaredSymbol(node);
                Function fGet = null;
                Function fSet = null;
                foreach (AccessorDeclarationSyntax ads in node.AccessorList.Accessors)
                {
                    switch (ads.Keyword.Kind)
                    {
                        case SyntaxKind.GetKeyword:
                            {
                                MethodSymbol ms = Chunk.Model.GetDeclaredSymbol(ads);
                                if (!Chunk.Functions.TryGetValue(ms, out fGet))
                                {
                                    throw new NotImplementedException("Method not found " + ms.ToString());
                                }
                            }
                            break;
                        case SyntaxKind.SetKeyword:
                            {
                                MethodSymbol ms = Chunk.Model.GetDeclaredSymbol(ads);
                                if (!Chunk.Functions.TryGetValue(ms, out fSet))
                                {
                                    throw new NotImplementedException("Method not found " + ms.ToString());
                                }
                            }
                            break;
                        default:
                            throw new NotImplementedException("unhandled property accessor: " + ads.Keyword.Kind.ToString());
                            break;
                    }
                }

                FlatArrayBuilder fab = new FlatArrayBuilder();
                fab.Add(FlatValue.Int32(s.IsStatic ? (int)ClassMemberType.StaticProperty : (int)ClassMemberType.Property));

                //fab.Add(FlatValue.Int32((int)ClassMemberType.Property));
                fab.Add(FlatValue.String(node.Identifier.ToString()));

                if (fGet == null)
                    fab.Add(FlatValue.Null());
                else
                    fab.Add(FlatValue.Int32(fGet.NumFunction));

                if (fSet == null)
                    fab.Add(FlatValue.Null());
                else
                    fab.Add(FlatValue.Int32(fSet.NumFunction));

                Members.Add(fab.GetFlatValue());
            }
Пример #6
0
            public void Add(EnumMemberDeclarationSyntax node)
            {
                /*        // Summary:
                //     Gets the attribute declaration list.
                public SyntaxList<AttributeListSyntax> AttributeLists { get; }
                public EqualsValueClauseSyntax EqualsValue { get; }
                //
                // Summary:
                //     Gets the identifier.
                public SyntaxToken Identifier { get; }
                 */
                // add field info

                TypeExtraInfo tei = Chunk.AddTypeExtraInfo(this.Class,Model);
                tei.AddEnumMember(node.Identifier.ToString(), node.EqualsValue != null ? node.EqualsValue.Value : null);

                /*
                // Summary:
                //     Gets the attribute declaration list.
                public override SyntaxList<AttributeListSyntax> AttributeLists { get; }
                public override VariableDeclarationSyntax Declaration { get; }
                //
                // Summary:
                //     Gets the modifier list.
                public override SyntaxTokenList Modifiers { get; }
                public override SyntaxToken SemicolonToken { get; }
                 */

                // add to metadata for runtime type building
                FlatArrayBuilder fab = new FlatArrayBuilder();

                fab.Add(FlatValue.Int32((int)ClassMemberType.StaticField));
                //TypeInfo ti = Chunk.Model.GetTypeInfo(node..Type);

                fab.Add(FlatValue.String(Class.GetFullyQualifiedName()));

                {
                    FlatArrayBuilder varList = new FlatArrayBuilder();
                    varList.Add(FlatValue.String(node.Identifier.ToString()));
                    fab.Add(varList.GetFlatValue());
                }
                {
                    FlatArrayBuilder valueList = new FlatArrayBuilder();
                    if (node.EqualsValue != null)
                    {
                        if (node.EqualsValue.Value.Kind == SyntaxKind.NumericLiteralExpression)
                        {
                            valueList.Add(FlatValue.Int32(int.Parse(node.EqualsValue.Value.ToString())));
                        }
                        else
                        {
                            throw new NotImplementedException("Enum member without numeric literal expression");
                        }
                    }
                    else
                    {
                        throw new NotImplementedException("Enum member without numeric literal expression");
                    }

                    fab.Add(valueList.GetFlatValue());
                }

                Members.Add(fab.GetFlatValue());
            }
Пример #7
0
            public new FlatValue GetFlatValue()
            {
                FlatTableBuilder ftb = new FlatTableBuilder();
                ftb.Add("Name", FlatValue.String(Class.GetFullyQualifiedName()));
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();
                    foreach (FlatValue fv in Members)
                    {
                        fab.Add(fv);
                    }
                    ftb.Add("Members", fab.GetFlatValue());
                }

                // base type
                if (this.Class.BaseType != null && this.Class.BaseType.SpecialType!= SpecialType.System_Object)
                {
                    ftb.Add("Base", FlatValue.String(this.Class.BaseType.GetFullyQualifiedName()));
                }

                ReadOnlyArray<NamedTypeSymbol> interfaces = this.Class.Interfaces;
                if (interfaces != null && interfaces.Count > 0)
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();
                    foreach (NamedTypeSymbol iface in interfaces)
                    {
                        fab.Add(FlatValue.String(iface.GetFullyQualifiedName()));
                    }
                    ftb.Add("Interfaces", fab.GetFlatValue());
                }

                return ftb.GetFlatValue();
            }
Пример #8
0
        public FlatOperand ResolveArgumentsToArray(ArgumentListSyntax args, FlatOperand return_reference, FlatOperand into_lvalue, List<FlatStatement> instructions)
        {
            if (into_lvalue == null)
            {
                FlatOperand register_fop = AllocateRegister("");
                into_lvalue = register_fop.GetLValue(this, instructions);
            }

            FlatValue literalArray;
            FlatOperand fop_array;

            bool allLiteral = (return_reference==null);
            if (allLiteral)
            {
                foreach (ArgumentSyntax ars in args.Arguments)
                {
                    /*
                            // Summary:
                            //     ExpressionSyntax node representing the argument.
                            public ExpressionSyntax Expression { get; }
                            //
                            // Summary:
                            //     NameColonSyntax node representing the optional name arguments.
                            public NameColonSyntax NameColon { get; }
                            //
                            // Summary:
                            //     SyntaxToken representing the optional ref or out keyword.
                            public SyntaxToken RefOrOutKeyword { get; }
                    /**/
                    if (ars.NameColon != null)
                    {
                        throw new NotImplementedException("name : value");
                    }
                    if (ars.RefOrOutKeyword.Kind != SyntaxKind.None)
                    {
                        throw new NotImplementedException("ref/out keywords");
                    }

                    if (!(ars.Expression is LiteralExpressionSyntax))
                    {
                        allLiteral = false;
                    }
                }

                if (allLiteral)
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();

                    foreach (ArgumentSyntax ars in args.Arguments)
                    {
                        LiteralExpressionSyntax les = (LiteralExpressionSyntax)ars.Expression;
                        // get the type
                        TypeInfo ti = Model.GetTypeInfo(les);
                        FlatOperand fop_element = ResolveExpression(les, ti.ConvertedType);
                        fab.Add(fop_element.ImmediateValue);
                    }

                    literalArray = fab.GetFlatValue();
                    instructions.Add(FlatStatement.DUPLICATE(into_lvalue, FlatOperand.Immediate(literalArray)));
                    fop_array = into_lvalue.AsRValue(literalArray);
                    return fop_array;
                }
            }

            // generate a new array
            literalArray = new FlatValue(FlatValueType.VT_Array, "{ }", null);
            instructions.Add(FlatStatement.DUPLICATE(into_lvalue, FlatOperand.Immediate(literalArray)));

            fop_array = into_lvalue.AsRValue(literalArray);

            if (return_reference != null)
            {
                instructions.Add(FlatStatement.ADD(into_lvalue, fop_array, return_reference));
            }

            foreach(ArgumentSyntax ars in args.Arguments)
            {
                /*
                        // Summary:
                        //     ExpressionSyntax node representing the argument.
                        public ExpressionSyntax Expression { get; }
                        //
                        // Summary:
                        //     NameColonSyntax node representing the optional name arguments.
                        public NameColonSyntax NameColon { get; }
                        //
                        // Summary:
                        //     SyntaxToken representing the optional ref or out keyword.
                        public SyntaxToken RefOrOutKeyword { get; }
                /**/
                FlatOperand fop_element = ResolveExpression(ars.Expression, null, instructions);
                instructions.Add(FlatStatement.ADD(into_lvalue,fop_array,fop_element));
            }

            return fop_array;
        }
Пример #9
0
        public FlatOperand ResolveExpressionsToArray(IEnumerable<ExpressionSyntax> expressions, FlatOperand return_reference, FlatOperand into_lvalue, List<FlatStatement> instructions)
        {
            if (into_lvalue == null)
            {
                FlatOperand register_fop = AllocateRegister("");
                into_lvalue = register_fop.GetLValue(this, instructions);
            }

            FlatValue literalArray;
            FlatOperand fop_array;

            bool allLiteral = (return_reference == null);
            if (allLiteral)
            {
                foreach (ExpressionSyntax es in expressions)
                {
                    if (!(es is LiteralExpressionSyntax))
                    {
                        allLiteral = false;
                    }
                }

                if (allLiteral)
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();

                    foreach (ExpressionSyntax es in expressions)
                    {
                        LiteralExpressionSyntax les = (LiteralExpressionSyntax)es;
                        // get the type
                        TypeInfo ti = Model.GetTypeInfo(les);
                        FlatOperand fop_element = ResolveExpression(les, ti.ConvertedType);
                        fab.Add(fop_element.ImmediateValue);
                    }

                    literalArray = fab.GetFlatValue();
                   // if (literalArray.ToString().Length < 192) // max instruction size is 255 bytes, this has to fit
                    {
                        instructions.Add(FlatStatement.DUPLICATE(into_lvalue, FlatOperand.Immediate(literalArray)));
                        fop_array = into_lvalue.AsRValue(literalArray);
                        return fop_array;
                    }
                }
            }

            // generate a new array
            literalArray = new FlatValue(FlatValueType.VT_Array, "{ }", null);
            instructions.Add(FlatStatement.DUPLICATE(into_lvalue, FlatOperand.Immediate(literalArray)));

            fop_array = into_lvalue.AsRValue(literalArray);

            if (return_reference != null)
            {
                instructions.Add(FlatStatement.ADD(into_lvalue, fop_array, return_reference));
            }

            foreach (ExpressionSyntax es in expressions)
            {
                /*
                        // Summary:
                        //     ExpressionSyntax node representing the argument.
                        public ExpressionSyntax Expression { get; }
                        //
                        // Summary:
                        //     NameColonSyntax node representing the optional name arguments.
                        public NameColonSyntax NameColon { get; }
                        //
                        // Summary:
                        //     SyntaxToken representing the optional ref or out keyword.
                        public SyntaxToken RefOrOutKeyword { get; }
                /**/
                FlatOperand fop_element = ResolveExpression(es, null, instructions);
                instructions.Add(FlatStatement.ADD(into_lvalue, fop_array, fop_element));
            }

            return fop_array;
        }
Пример #10
0
            public FlatArrayBuilder GenerateInputDeclarations(IMethodSymbol ms)
            {
                FlatArrayBuilder fab = new FlatArrayBuilder();
                if (!ms.ReturnsVoid)
                {
                    FlatArrayBuilder decl = new FlatArrayBuilder();
                    // name
                    decl.Add(FlatValue.String(""));
                    // type
                    decl.Add(FlatValue.String(ms.ReturnType.GetFullyQualifiedName()));
                    // csharp declaration
                    decl.Add(FlatValue.String(ms.ReturnType.GetFullyQualifiedName()));

                    fab.Add(decl.GetFlatValue());
                }
                foreach(IParameterSymbol param in ms.Parameters)
                {
                    FlatArrayBuilder decl = new FlatArrayBuilder();
                    // name
                    decl.Add(FlatValue.String(param.Name));
                    // type
                    decl.Add(FlatValue.String(param.Type.GetFullyQualifiedName()));
                    // csharp declaration
                    decl.Add(FlatValue.String(param.Type.GetFullyQualifiedName()+" "+param.Name));

                    fab.Add(decl.GetFlatValue());
                }

                return fab;
            }
Пример #11
0
            public void Add(MethodDeclarationSyntax node)
            {
                if (IsLibrary)
                    return;
                FlatArrayBuilder fab = new FlatArrayBuilder();

                IMethodSymbol s = Model.GetDeclaredSymbol(node);
                fab.Add(FlatValue.Int32(s.IsStatic ? (int)ClassMemberType.StaticMethod : (int)ClassMemberType.Method));

                fab.Add(FlatValue.String(s.GetFullyQualifiedName()));

                IMethodSymbol ms = Model.GetDeclaredSymbol(node);

                Function f;
                if (!Chunk.Functions.TryGetValue(ms, out f))
                {
                    throw new LS2ILMethodException("Method not found " + ms.ToString());
                }

                fab.Add(FlatValue.Int32(f.NumFunction));

                fab.Add(GenerateInputDeclarations(ms).GetFlatValue());

                Members.Add(fab.GetFlatValue());
            }
Пример #12
0
            public void Add(IndexerDeclarationSyntax node)
            {
                if (IsLibrary)
                    return;

                ISymbol s = Model.GetDeclaredSymbol(node);
                Function fGet = null;
                Function fSet = null;

                string sGet = string.Empty;
                string sSet = string.Empty;
                IMethodSymbol msGet = null;
                IMethodSymbol msSet = null;
                foreach (AccessorDeclarationSyntax ads in node.AccessorList.Accessors)
                {
                    switch (ads.Keyword.CSharpKind())
                    {
                        case SyntaxKind.GetKeyword:
                            {
                                msGet = Model.GetDeclaredSymbol(ads);
                                if (!Chunk.Functions.TryGetValue(msGet, out fGet))
                                {
                                    throw new LS2ILMethodException("Method not found " + msGet.ToString());
                                }
                                sGet = /*"get_" +*/ msGet.GetFullyQualifiedName();
                            }
                            break;
                        case SyntaxKind.SetKeyword:
                            {
                                msSet = Model.GetDeclaredSymbol(ads);
                                if (!Chunk.Functions.TryGetValue(msSet, out fSet))
                                {
                                    throw new LS2ILMethodException("Method not found " + msSet.ToString());
                                }
                                sSet = /*"set_" +*/ msSet.GetFullyQualifiedName();
                            }
                            break;
                        default:
                            throw new NotImplementedException("unhandled property accessor: " + ads.Keyword.CSharpKind().ToString());
                            break;
                    }
                }

                if (msGet != null)
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();
                    fab.Add(FlatValue.Int32(s.IsStatic ? (int)ClassMemberType.StaticMethod : (int)ClassMemberType.Method));

                    fab.Add(FlatValue.String(sGet));
                    Function f;
                    if (!Chunk.Functions.TryGetValue(msGet, out f))
                    {
                        throw new LS2ILMethodException("Method not found " + msGet.ToString());
                    }

                    fab.Add(FlatValue.Int32(f.NumFunction));

                    fab.Add(GenerateInputDeclarations(msGet).GetFlatValue());

                    Members.Add(fab.GetFlatValue());
                }
                if (msSet != null)
                {
                    FlatArrayBuilder fab = new FlatArrayBuilder();
                    fab.Add(FlatValue.Int32(s.IsStatic ? (int)ClassMemberType.StaticMethod : (int)ClassMemberType.Method));

                    fab.Add(FlatValue.String(sSet));
                    Function f;
                    if (!Chunk.Functions.TryGetValue(msSet, out f))
                    {
                        throw new LS2ILMethodException("Method not found " + msGet.ToString());
                    }

                    fab.Add(FlatValue.Int32(f.NumFunction));

                    fab.Add(GenerateInputDeclarations(msSet).GetFlatValue());

                    Members.Add(fab.GetFlatValue());
                }
            }