Пример #1
0
        public EmitContext(MachineInfo machineInfo, Report report, CompiledFunction fdecl = null)
        {
            if (machineInfo == null)
            {
                throw new ArgumentNullException(nameof(machineInfo));
            }
            if (report == null)
            {
                throw new ArgumentNullException(nameof(report));
            }

            MachineInfo  = machineInfo;
            Report       = report;
            FunctionDecl = fdecl;
        }
Пример #2
0
        void AddStatementDeclarations(Statement statement, Block block)
        {
            if (statement is MultiDeclaratorStatement multi)
            {
                if (multi.InitDeclarators != null)
                {
                    foreach (var idecl in multi.InitDeclarators)
                    {
                        if ((multi.Specifiers.StorageClassSpecifier & StorageClassSpecifier.Typedef) != 0)
                        {
                            if (idecl.Declarator != null)
                            {
                                var name = idecl.Declarator.DeclaredIdentifier;
                                //Typedefs[name] = decl;
                            }
                        }
                        else
                        {
                            var ctype = context.MakeCType(multi.Specifiers, idecl.Declarator, idecl.Initializer, block);
                            var name  = idecl.Declarator.DeclaredIdentifier;

                            if (ctype is CFunctionType ftype && !HasStronglyBoundPointer(idecl.Declarator))
                            {
                                var nameContext = (idecl.Declarator.InnerDeclarator is IdentifierDeclarator ndecl && ndecl.Context.Count > 0) ?
                                                  string.Join("::", ndecl.Context) : "";
                                var f = new CompiledFunction(name, nameContext, ftype);
                                block.Functions.Add(f);
                            }
                            else
                            {
                                if ((ctype is CArrayType atype) &&
                                    (atype.Length == null) &&
                                    (idecl.Initializer != null))
                                {
                                    if (idecl.Initializer is StructuredInitializer structInit)
                                    {
                                        var len = 0;
                                        foreach (var i in structInit.Initializers)
                                        {
                                            if (i.Designation == null)
                                            {
                                                len++;
                                            }
                                            else
                                            {
                                                foreach (var de in i.Designation.Designators)
                                                {
                                                    // TODO: Pay attention to designators
                                                    len++;
                                                }
                                            }
                                        }
                                        atype = new CArrayType(atype.ElementType, len);
                                    }
                                    else
                                    {
                                        //Report.Error();
                                    }
                                }
                                //var init = GetInitExpression(idecl.Initializer);
                                block.AddVariable(name, ctype);
                            }

                            if (idecl.Initializer != null)
                            {
                                var varExpr  = new VariableExpression(name);
                                var initExpr = GetInitializerExpression(idecl.Initializer);
                                block.InitStatements.Add(new ExpressionStatement(new AssignExpression(varExpr, initExpr)));
                            }
                        }
                    }