Exemple #1
0
 public override void Validate_AfterTypeAlignment(int logIndent)
 {
     SynLog.Log("");
     SynLog.LogIndent(logIndent, $"Begin Struct.Validate_AfterTypeAlignment : {this.typeName}");
     base.Validate_AfterTypeAlignment(logIndent + 1);
     SynLog.LogIndent(logIndent, $"End Struct.Validate_AfterTypeAlignment");
 }
Exemple #2
0
        public override void BreakApartParsedTokens()
        {
            SynLog.Log($"Breaking apart type {this.typeName}.");
            SynLog.LogFragments(this.declarationTokens);

            // For struct structname { ... }, remove everything except the ...
            this.declarationTokens.RemoveRange(0, 3);

            int lastIdx = this.declarationTokens.Count - 1;

            if (this.declarationTokens[lastIdx].MatchesSymbol(";"))
            {
                this.declarationTokens.RemoveRange(lastIdx - 1, 2);
            }
            else
            {
                this.declarationTokens.RemoveRange(lastIdx, 1);
            }

            while (this.declarationTokens.Count > 0)
            {
                if (this.declarationTokens[0].MatchesSymbol(";") == true)
                {
                    this.declarationTokens.RemoveAt(0);
                    continue;
                }

                SynRegion rgn = SynRegion.Parse(this, this.declarationTokens);
                if (rgn != null)
                {
                    this.regions.Add(rgn.name, rgn);
                    continue;
                }

                SynFuncDecl fnParse =
                    SynFuncDecl.Parse(
                        this,
                        this.declarationTokens,
                        this.typeName,
                        false,
                        SynFuncDecl.ParseType.StructContext);

                if (fnParse != null)
                {
                    this.AddFunction(fnParse);
                    continue;
                }

                SynVarValue varParse = SynVarValue.ParseBodyVar(this.declarationTokens, SynVarValue.OuterScope.Struct);
                if (varParse != null)
                {
                    this.AddVariable(varParse);
                    continue;
                }
            }
        }
Exemple #3
0
        public void RegisterFunction(SynFuncDecl fn)
        {
            SynLog.Log($"Registering function {fn.functionName}.");

            if (this.functionLookup.ContainsKey(fn) == true)
            {
                throw new SynthExceptionImpossible("");
            }

            // Register the type, and get the type index
            FunctionType fty = TurnFnTypeIntoWASMType(fn);

            FunctionInfo finfo = new FunctionInfo(fn, fty.index, fn.isExtern);

            this.functionInfos.Add(finfo);
            this.functionLookup.Add(fn, finfo);
        }
Exemple #4
0
        public void RealignFunctions()
        {
            SynLog.Log($"Realigning function indices.");

            List <FunctionInfo> origFnInfos = this.functionInfos;

            this.functionInfos = new List <FunctionInfo>();

            uint idx = 0;

            foreach (FunctionInfo fi in origFnInfos)
            {
                if (fi.imported != true)
                {
                    continue;
                }

                fi.functionIndex = idx;
                ++idx;

                this.functionInfos.Add(fi);
            }

            foreach (FunctionInfo fi in origFnInfos)
            {
                if (fi.imported == true)
                {
                    continue;
                }

                fi.functionIndex = idx;
                ++idx;

                this.functionInfos.Add(fi);
            }
        }
Exemple #5
0
        // TODO: Perhaps this function should be moved into WASMBuild?
        public SynNestingBuilder BuildFunction(SynFuncDecl fnd)
        {
            SynNestingBuilder builder = new SynNestingBuilder(null);

            if (fnd.ast != null)
            {
                throw new SynthExceptionImpossible($"Attempting to build function {fnd.functionName} AST multiple times.");
            }

            SynLog.Log($"Entered SynthFuncDecl.Build() for {fnd.functionName}.");

            // NOTE: For now functions are non-typed (in the SynthSyn language) and
            // are non addressable.
            fnd.ast =
                new AST(
                    fnd.declPhrase[0],
                    builder,
                    ASTOp.FunctionDecl,
                    fnd,
                    null,
                    false,
                    AST.DataManifest.NoData);

            SynLog.Log($"Building function AST for {fnd.functionName}.");
            SynLog.Log("");

            List <SynNestingBuilder> builders = new List <SynNestingBuilder>();

            builders.Add(builder);

            //List<TokenTree> treeLines = new List<TokenTree>();
            for (int i = 0; i < fnd.executingLines.Count; ++i)
            {
                List <Token> execLine = fnd.executingLines[i];
                SynLog.LogFragments(execLine);

                TokenTree rootLineNode = TokenTree.EatTokensIntoTree(execLine, fnd, true);

                SynLog.LogTree(rootLineNode);

                // If it's a member function (not a static function) then full in the struct
                // we belong to as a he invoking scope. Or else set it to null. Its syntax scope
                // it still all the way where the source code is, but doesn't have a "this" member
                // function.
                SynScope invokingScope = null;
                if (fnd.isStatic == false)
                {
                    invokingScope = fnd.GetStructScope();
                }

                AST exprAST = builder.ProcessFunctionExpression(builders, this, invokingScope, fnd, rootLineNode);
                if (exprAST == null)
                {
                    // We shouldn't have received null, we should have thrown before this
                    throw new SynthExceptionImpossible(""); //TODO:
                }
                fnd.ast.branches.Add(exprAST);
            }
            fnd.ast.branches.Add(new AST(new Token(), builder, ASTOp.EndScope, builder, null, false, AST.DataManifest.NoData));

            SynLog.Log("");
            SynLog.Log($"Encountered {builders.Count} nested scopes.");
            for (int i = 0; i < builders.Count; ++i)
            {
                SynLog.LogIndent(0, $"Scope {i}");
                SynLog.LogIndent(1, $"Line number : {builders[i].lineNumber}");
                SynLog.LogIndent(1, $"Stack Elements : Ele {builders[i].locStkEle.Count} - Vars {builders[i].locStkVars.Count}");
                SynLog.LogIndent(1, $"Memory Stack : Vars {builders[i].memStkVars.Count}");
                SynLog.LogIndent(1, $"Total Memory Stack : Bytes {builders[i].totalMemoryStackBytes}");
            }

            SynLog.Log("");
            SynLog.Log(fnd.ast.DumpDiagnostic());

            SynLog.Log($"Finished building AST : {fnd.functionName}.");
            SynLog.Log($"Converting AST to binary WASM : {fnd.functionName}.");

            // Before the AST is turned into actual WASM binary, we need to finalize the
            // byte alignment and the indices of local stack variables. This is done with
            // CompileAlignment, who's lower scopes should appear earlier in the builders
            // list before higher children scopes.
            for (int i = 0; i < builders.Count; ++i)
            {
                if (i == 0)
                {
                    builders[i].CompileAlignment(fnd);
                }
                else
                {
                    builders[i].CompileAlignment();
                }
            }

            // Gather all the local variables and declare them. This isn't as efficient as things
            // could be because after variables go out of scope, their positions on the stack
            // can be reused if they match types we encounter in the future, but that can be
            // handled later.

            // The program binary
            WASMByteBuilder fnBuild = new WASMByteBuilder();

            // Gather all the local variables
            List <WASM.Bin.TypeID> localVarTys = new List <WASM.Bin.TypeID>();

            foreach (SynNestingBuilder b in builders)
            {
                foreach (ValueRef stkVal in b.locStkEle)
                {
                    if (stkVal.varType.intrinsic == false)
                    {
                        throw new SynthExceptionCompile(""); // TODO: Error msg
                    }
                    switch (stkVal.varType.typeName)
                    {
                    case "bool":
                    case "int8":
                    case "uint8":
                    case "int16":
                    case "uint16":
                    case "int":
                    case "int32":
                        localVarTys.Add(WASM.Bin.TypeID.Int32);
                        break;

                    case "int64":
                    case "uint64":
                        localVarTys.Add(WASM.Bin.TypeID.Int64);
                        break;

                    case "float":
                        localVarTys.Add(WASM.Bin.TypeID.Float32);
                        break;

                    case "double":
                        localVarTys.Add(WASM.Bin.TypeID.Float64);
                        break;
                    }
                }
            }

            List <KeyValuePair <WASM.Bin.TypeID, int> > consolidatedLocalTys = new List <KeyValuePair <WASM.Bin.TypeID, int> >();

            for (int i = 0; i < localVarTys.Count; ++i)
            {
                WASM.Bin.TypeID tyid = localVarTys[i];
                int             ct   = 1;
                ++i;

                for (; i < localVarTys.Count; ++i)
                {
                    if (localVarTys[i] != tyid)
                    {
                        break;
                    }

                    ++ct;
                }
                consolidatedLocalTys.Add(new KeyValuePair <WASM.Bin.TypeID, int>(tyid, ct));
            }

            fnBuild.AddLEB128((uint)consolidatedLocalTys.Count); // Local decl
            for (int i = 0; i < consolidatedLocalTys.Count; ++i)
            {
                fnBuild.AddLEB128((uint)consolidatedLocalTys[i].Value);

                // These are really bytes, but they'll end up being added as bytes since they're
                // small constants
                fnBuild.AddLEB128((uint)consolidatedLocalTys[i].Key);
            }

            builder.BuildBSFunction(fnd, fnd.ast, this, builder, fnBuild);
            fnBuild.Add_End();
            fnd.fnBin = fnBuild.Bin();

            SynLog.Log($"Exiting SynthFuncDecl.Build({fnd.functionName}).");
            return(builder);
        }
Exemple #6
0
 public SynthExceptionSyntax()
     : base()
 {
     SynLog.Log("SYNTAXERROR!");
 }
Exemple #7
0
 public SynthExceptionSyntax(Token t, string why)
     : base($"Syntax Error line {t.line}: " + why)
 {
     SynLog.Log($"Syntax Error line {t.line}: " + why);
 }
Exemple #8
0
 public SynthExceptionSyntax(int line, string why)
     : base(why)
 {
     SynLog.Log($"Syntax Error line {line}: " + why);
 }
Exemple #9
0
 public SynthExceptionCompile(string why)
     : base(why)
 {
     SynLog.Log("COMPILE!: " + why);
 }
Exemple #10
0
 public SynthExceptionCompile()
     : base()
 {
     SynLog.Log("COMPILE ERROR!");
 }
Exemple #11
0
        public void ParseContext(List <Token> tokens)
        {
            SynLog.LogHeader("Starting Parse Content");
            SynLog.Log("\tTokens:");
            SynLog.Log(tokens);

            //      Get param globals first
            //
            //////////////////////////////////////////////////
            while (tokens.Count > 0)
            {
                SynVarValue parsedParam = SynVarValue.ParseExposedParam(tokens);
                if (parsedParam == null)
                {
                    break;
                }

                this.AddVariable(parsedParam);
            }

            //      Parse Sections
            //
            //////////////////////////////////////////////////
            int idx = 0;

            while (tokens.Count > 0)
            {
                // Get rid of stray parenthesis
                if (tokens[idx].Matches(TokenType.tySymbol, ";") == true)
                {
                    tokens.RemoveAt(0);
                    continue;
                }

                // Struct parsing
                if (tokens[0].Matches(TokenType.tyWord, "struct") == true)
                {
                    SynStruct sst = SynStruct.Parse(this, tokens);
                    if (sst != null)
                    {
                        this.AddType(sst);
                        continue;
                    }
                }

                // Function parsing
                if (tokens[0].Matches(TokenType.tyWord, "entry") == true)
                {
                    SynFuncDecl sfd = SynFuncDecl.Parse(this, tokens, "", true, SynFuncDecl.ParseType.Entry);
                    sfd.callType = SynFuncDecl.CallType.Entry;

                    if (sfd != null)
                    {
                        this.AddFunction(sfd);
                        continue;
                    }
                    else
                    {
                        throw new System.Exception("entry keyword not part of valid function.");
                    }
                }

                SynFuncDecl synthFn =
                    SynFuncDecl.Parse(
                        this, tokens,
                        "",
                        true,
                        SynFuncDecl.ParseType.RootContext);

                if (synthFn != null)
                {
                    this.AddFunction(synthFn);
                    continue;
                }

                SynVarValue synthVar = SynVarValue.ParseBodyVar(tokens, SynVarValue.OuterScope.Global);
                if (synthVar != null)
                {
                    this.AddVariable(synthVar);
                    continue;
                }

                throw new System.Exception("Unknown token while parsing root context.");
            }

            //      Verify Structs
            //
            //////////////////////////////////////////////////
            SynLog.LogHeader("Verifying types");

            while (true)
            {
                TypeConsolidate tc = this.ResolveStaticTypeAlignments();
                if (tc == TypeConsolidate.UndeterminedNoChange)
                {
                    throw new System.Exception("Could not resolve all types");
                }

                if (tc == TypeConsolidate.AllDetermined)
                {
                    break;
                }
            }

            SynLog.Log("Finished verifying struct successfully.");

            //      Gathering globals
            //
            //////////////////////////////////////////////////
            SynLog.LogHeader("Gathering globals");

            List <SynVarValue> globals = new List <SynVarValue>();

            foreach (SynScope s in this.EnumerateScopes())
            {
                s.RegisterGlobals(globals);
            }

            this.totalGlobalBytes = 0;
            foreach (SynVarValue svv in globals)
            {
                svv.alignmentOffset = this.totalGlobalBytes;

                int byteSz = svv.type.GetByteSize();
                if (byteSz <= 0)
                {
                    throw new SynthExceptionImpossible("Data type for global variable is zero in size.");
                }

                SynLog.Log($"Added {svv.varName} to globals at offset {svv.alignmentOffset}");

                this.totalGlobalBytes += byteSz;
            }

            SynLog.Log($"Total global variable space is {this.totalGlobalBytes}.");

            //      Verify Functions
            //
            //////////////////////////////////////////////////
            SynLog.LogHeader("Verifying After function and variable collection pass.");
            this.Validate_AfterTypeAlignment(0);

            SynLog.Log("Finished verifying functions successfully.");
        }