Beispiel #1
0
        private bool AssertDecodeBranchArguments(
            IEnumerable <LNode> nodes,
            Dictionary <Symbol, ValueTag> valueTags,
            out IReadOnlyList <BranchArgument> args)
        {
            var results = new List <BranchArgument>();

            foreach (var argNode in nodes)
            {
                if (!FeedbackHelpers.AssertIsId(argNode, Log))
                {
                    args = null;
                    return(false);
                }

                var name = argNode.Name;
                if (name == CodeSymbols.Result)
                {
                    results.Add(BranchArgument.TryResult);
                }
                else if (name == EncoderState.tryFlowExceptionSymbol)
                {
                    results.Add(BranchArgument.TryException);
                }
                else
                {
                    results.Add(BranchArgument.FromValue(GetValueTag(argNode.Name, valueTags)));
                }
            }
            args = results;
            return(true);
        }
Beispiel #2
0
        private BasicBlockBuilder DecodeBasicBlock(
            LNode node,
            FlowGraphBuilder graph,
            Dictionary <Symbol, BasicBlockBuilder> blocks,
            Dictionary <Symbol, ValueTag> valueTags)
        {
            // Each basic block is essentially a
            // (name, parameters, instructions, flow) tuple.
            // We just parse all four elements and call it a day.

            // Parse the block's name and create the block.
            var name = FeedbackHelpers.AssertIsId(node.Args[0], Log)
                ? node.Args[0].Name
                : GSymbol.Empty;

            var blockBuilder = GetBasicBlock(name, graph, blocks);

            // Parse the block's parameter list.
            foreach (var paramNode in node.Args[1].Args)
            {
                if (FeedbackHelpers.AssertArgCount(paramNode, 2, Log))
                {
                    blockBuilder.AppendParameter(
                        new BlockParameter(
                            DecodeType(paramNode.Args[0]),
                            FeedbackHelpers.AssertIsId(paramNode.Args[1], Log)
                                ? GetValueTag(paramNode.Args[1].Name, valueTags)
                                : new ValueTag()));
                }
                else
                {
                    blockBuilder.AppendParameter(new BlockParameter(ErrorType.Instance));
                }
            }

            // Parse the block's instructions.
            foreach (var valueNode in node.Args[2].Args)
            {
                // Decode the instruction.
                Instruction insn;
                if (!FeedbackHelpers.AssertIsCall(valueNode, Log) ||
                    !FeedbackHelpers.AssertArgCount(valueNode, 2, Log) ||
                    !FeedbackHelpers.AssertIsId(valueNode.Args[0], Log) ||
                    !AssertDecodeInstruction(valueNode.Args[1], valueTags, out insn))
                {
                    continue;
                }

                // Append the instruction to the basic block.
                blockBuilder.AppendInstruction(
                    insn,
                    GetValueTag(valueNode.Args[0].Name, valueTags));
            }

            // Parse the block's flow.
            blockBuilder.Flow = DecodeBlockFlow(node.Args[3], graph, blocks, valueTags);

            return(blockBuilder);
        }
 private static IntrinsicPrototype DecodeIntrinsic(IReadOnlyList <LNode> data, DecoderState state)
 {
     // TODO: decode exception specifications.
     return(IntrinsicPrototype.Create(
                FeedbackHelpers.AssertIsId(data[0], state.Log)
             ? data[0].Name.Name
             : "error",
                state.DecodeType(data[1]),
                data[2].Args.EagerSelect <LNode, IType>(state.DecodeType)));
 }
        /// <summary>
        /// Decodes a particular piece of data.
        /// </summary>
        /// <param name="data">
        /// Encoded data to decode.
        /// </param>
        /// <param name="state">
        /// The decoder's state.
        /// </param>
        /// <returns>
        /// A decoded object.
        /// </returns>
        public override TObj Decode(LNode data, DecoderState state)
        {
            if (!FeedbackHelpers.AssertIsCall(data, state.Log) &&
                !FeedbackHelpers.AssertIsId(data.Target, state.Log))
            {
                return(default(TObj));
            }

            var identifier = data.Name;
            var args       = data.Args;

            return(Decode(identifier, data, state));
        }
Beispiel #5
0
        private bool AssertDecodeValueTags(
            IEnumerable <LNode> nodes,
            Dictionary <Symbol, ValueTag> valueTags,
            out IReadOnlyList <ValueTag> tags)
        {
            var results = new List <ValueTag>();

            foreach (var argNode in nodes)
            {
                if (!FeedbackHelpers.AssertIsId(argNode, Log))
                {
                    tags = null;
                    return(false);
                }

                results.Add(GetValueTag(argNode.Name, valueTags));
            }
            tags = results;
            return(true);
        }
Beispiel #6
0
        /// <summary>
        /// Decodes an LNode as a simple name. Logs an error if the decoding
        /// process fails.
        /// </summary>
        /// <param name="node">A node to decode as a simple name.</param>
        /// <param name="name">The name described by <paramref name="node"/>.</param>
        /// <returns>
        /// <c>true</c> if <paramref name="node"/> can be decoded as a simple
        /// name; otherwise, <c>false</c>.
        /// </returns>
        public bool AssertDecodeSimpleName(LNode node, out SimpleName name)
        {
            if (node.IsId)
            {
                name = new SimpleName(node.Name.Name);
                return(true);
            }
            else if (node.IsCall)
            {
                var nameNode = node.Target;
                int arity;
                if (!FeedbackHelpers.AssertIsId(nameNode, Log) ||
                    !FeedbackHelpers.AssertArgCount(node, 1, Log) ||
                    !AssertDecodeInt32(node.Args[0], out arity))
                {
                    name = null;
                    return(false);
                }

                name = new SimpleName(nameNode.Name.Name, arity);
                return(true);
            }
            else
            {
                FeedbackHelpers.LogSyntaxError(
                    Log,
                    node,
                    FeedbackHelpers.QuoteEven(
                        "expected a simple name, which can either be a simple id (e.g., ",
                        "Name",
                        ") or a call to an id that specifies the number of generic parameters (e.g., ",
                        "Name(2)",
                        ")."));
                name = null;
                return(false);
            }
        }
Beispiel #7
0
        private bool AssertDecodeBranch(
            LNode node,
            FlowGraphBuilder graph,
            Dictionary <Symbol, BasicBlockBuilder> blocks,
            Dictionary <Symbol, ValueTag> valueTags,
            out Branch result)
        {
            IReadOnlyList <BranchArgument> args;

            if (FeedbackHelpers.AssertIsCall(node, Log) &&
                FeedbackHelpers.AssertIsId(node.Target, Log) &&
                AssertDecodeBranchArguments(node.Args, valueTags, out args))
            {
                result = new Branch(
                    GetBasicBlock(node.Target.Name, graph, blocks).Tag,
                    args);
                return(true);
            }
            else
            {
                result = default(Branch);
                return(false);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Decodes a control-flow graph as a method body.
        /// </summary>
        /// <param name="node">An encoded control-flow graph.</param>
        /// <returns>
        /// A new method body that includes the decoded control-flow graph.
        /// </returns>
        public FlowGraph DecodeFlowGraph(LNode node)
        {
            // A CFG consists of a list of basic blocks and a specially
            // marked entry point block.

            var graph = new FlowGraphBuilder();

            var blocks           = new Dictionary <Symbol, BasicBlockBuilder>();
            var valueTags        = new Dictionary <Symbol, ValueTag>();
            var parsedEntryPoint = false;

            // Do a quick pass through all blocks for determinism: we want
            // to define the blocks in the same order as the original IR.
            foreach (var blockNode in node.Args)
            {
                if (!FeedbackHelpers.AssertArgCount(blockNode, 4, Log))
                {
                    // Log the error and return an empty flow graph.
                    return(new FlowGraph());
                }

                var name = FeedbackHelpers.AssertIsId(blockNode.Args[0], Log)
                    ? blockNode.Args[0].Name
                    : GSymbol.Empty;

                // Define the basic block for determinism.
                GetBasicBlock(name, graph, blocks);
            }

            foreach (var blockNode in node.Args)
            {
                // Parse the basic block.
                var blockBuilder = DecodeBasicBlock(blockNode, graph, blocks, valueTags);

                // Entry points get special treatment.
                if (blockNode.Calls(EncoderState.entryPointBlockSymbol))
                {
                    if (parsedEntryPoint)
                    {
                        Log.LogSyntaxError(
                            blockNode,
                            "there can be only one entry point block in a control-flow graph.");
                    }

                    parsedEntryPoint = true;

                    // Update the graph's entry point.
                    var oldEntryPointTag = graph.EntryPointTag;
                    graph.EntryPointTag = blockBuilder.Tag;
                    graph.RemoveBasicBlock(oldEntryPointTag);
                }
            }

            if (!parsedEntryPoint)
            {
                Log.LogSyntaxError(
                    node,
                    "all control-flow graphs must define exactly one " +
                    "entry point, but this one doesn't.");
            }

            return(graph.ToImmutable());
        }
Beispiel #9
0
        /// <inheritdoc/>
        public override ITypeMember Decode(LNode data, DecoderState state)
        {
            if (data.Calls(accessorSymbol))
            {
                if (!FeedbackHelpers.AssertArgCount(data, 2, state.Log) ||
                    !FeedbackHelpers.AssertIsId(data.Args[1], state.Log))
                {
                    return(null);
                }

                var property = state.DecodeProperty(data.Args[0]);
                if (property == null)
                {
                    return(null);
                }
                else
                {
                    var kindName = data.Args[1].Name.Name;
                    var accessor = property.Accessors.FirstOrDefault(
                        acc => accessorKindEncodings[acc.Kind] == kindName);

                    if (accessor == null)
                    {
                        FeedbackHelpers.LogSyntaxError(
                            state.Log,
                            data.Args[1],
                            Quotation.QuoteEvenInBold(
                                "property ",
                                FeedbackHelpers.Print(data.Args[0]),
                                " does not define a ",
                                kindName,
                                " accessor."));
                    }
                    return(accessor);
                }
            }
            else if (data.Calls(CodeSymbols.Dot))
            {
                // Simple dot indicates a field.
                IType      parentType;
                SimpleName name;
                if (!AssertDecodeTypeAndName(data, state, out parentType, out name))
                {
                    return(null);
                }

                var candidates = state.TypeMemberIndex
                                 .GetAll(parentType, name)
                                 .OfType <IField>()
                                 .ToArray();

                return(CheckSingleCandidate(
                           candidates,
                           data.Args[0],
                           data.Args[1],
                           "field",
                           state));
            }
            else if (data.CallsMin(CodeSymbols.IndexBracks, 1))
            {
                IType      parentType;
                SimpleName name;
                if (!AssertDecodeTypeAndName(data.Args[0], state, out parentType, out name))
                {
                    return(null);
                }

                var indexTypes = data.Args
                                 .Slice(1)
                                 .EagerSelect(state.DecodeType);

                var candidates = state.TypeMemberIndex
                                 .GetAll(parentType, name)
                                 .OfType <IProperty>()
                                 .Where(prop =>
                                        prop.IndexerParameters
                                        .Select(p => p.Type)
                                        .SequenceEqual(indexTypes))
                                 .ToArray();

                return(CheckSingleCandidate(
                           candidates,
                           data.Args[0].Args[0],
                           data,
                           "property",
                           state));
            }
            else if (data.Calls(CodeSymbols.Lambda))
            {
                IType      parentType;
                SimpleName name;
                if (!FeedbackHelpers.AssertArgCount(data, 2, state.Log) ||
                    !FeedbackHelpers.AssertIsCall(data.Args[0], state.Log) ||
                    !AssertDecodeTypeAndName(data.Args[0].Target, state, out parentType, out name))
                {
                    return(null);
                }

                // TODO: implement generic parameter decoding, use generic
                // parameters in resolution process.

                var paramTypes = data.Args[0].Args
                                 .EagerSelect(state.DecodeType);

                var retType = state.DecodeType(data.Args[1]);

                var candidates = state.TypeMemberIndex
                                 .GetAll(parentType, name)
                                 .OfType <IMethod>()
                                 .Where(method =>
                                        method.Parameters
                                        .Select(p => p.Type)
                                        .SequenceEqual(paramTypes) &&
                                        object.Equals(
                                            method.ReturnParameter.Type,
                                            retType))
                                 .ToArray();

                return(CheckSingleCandidate(
                           candidates,
                           data.Args[0].Target.Args[0],
                           data,
                           "method",
                           state));
            }
            else if (data.Calls(CodeSymbols.Of))
            {
                if (!FeedbackHelpers.AssertMinArgCount(data, 1, state.Log))
                {
                    return(null);
                }

                var func = state.DecodeMethod(data.Args[0]);
                var args = data.Args.Slice(1).EagerSelect(state.DecodeType);

                if (func.GenericParameters.Count == args.Count)
                {
                    return(func.MakeGenericMethod(args));
                }
                else
                {
                    state.Log.LogSyntaxError(
                        data,
                        Quotation.QuoteEvenInBold(
                            "generic arity mismatch; expected ",
                            func.GenericParameters.Count.ToString(),
                            " parameters but got ",
                            args.Count.ToString(),
                            "."));
                    return(null);
                }
            }
            else
            {
                state.Log.LogSyntaxError(
                    data,
                    Quotation.QuoteEvenInBold(
                        "cannot interpret ",
                        FeedbackHelpers.Print(data),
                        " as a type member; expected a call to one of ",
                        accessorSymbol.Name, ", ",
                        CodeSymbols.Dot.Name, ", ",
                        CodeSymbols.IndexBracks.Name, ", ",
                        CodeSymbols.Of.Name, " or ",
                        CodeSymbols.Lambda.Name));
                return(null);
            }
        }