Exemple #1
0
        public void AllocateLocals()
        {
            foreach (var p in locals.Where(sym => sym.type == SymbolType.Parameter && sym.datatype == "int"))
            {
                localints.Add(p.name, "signal-" + p.fixedAddr);
            }
            //locals.RemoveAll(sym => localints.Keys.Contains(sym.name));
            var newlocals = new SymbolList();

            //TODO: fix this to actually confirm a register is available
            if (locals.Count(sym => sym.frame == PointerIndex.CallStack) < 4)
            {
                int nextReg = 6;
                newlocals.AddRange(locals.Select((symbol) =>
                {
                    if (symbol.frame == PointerIndex.CallStack)
                    {
                        symbol.type      = SymbolType.Register;
                        symbol.fixedAddr = nextReg--;
                        symbol.frame     = 0;
                    }
                    return(symbol);
                }));
            }
            else
            {
                newlocals.AddRange(locals.Select((symbol) =>
                {
                    if (symbol.frame == PointerIndex.CallStack)
                    {
                        symbol.fixedAddr = framesize++;
                    }
                    return(symbol);
                }));
            }


            locals = newlocals;
        }
Exemple #2
0
        public void AllocateSymbols()
        {
            // 0 in both frames is reserved.
            // 0Const has progsym
            // 0Data  is reserved for future use by memory allocator or other system services.
            int constaddr = 1;

            romdata = new List <Table>();

            // leave space for symtable starting at 1
            int symtsize = Symbols.Count(sym => sym.name.All(c => charmap.Keys.Contains(c)));

            constaddr += symtsize;


            int dataaddr = 1;
            var newsyms  = new SymbolList();

            foreach (var symbol in Symbols.OrderByDescending(s => s.type))
            {
                switch (symbol.type)
                {
                case SymbolType.Constant:
                case SymbolType.Function:
                    symbol.assign(constaddr);
                    romdata.AddRange(symbol.data);
                    constaddr += symbol.size ?? 1;
                    break;

                case SymbolType.Data:
                    if (!symbol.fixedAddr.HasValue)
                    {
                        symbol.assign(dataaddr);
                        dataaddr += symbol.size ?? 1;
                    }
                    break;
                }
                newsyms.Add(symbol);
            }
            Symbols = newsyms;

            var progsym = new Table(Name);

            progsym.Add("symtsize", symtsize);
            progsym.Add("romsize", romdata.Count + 1);
            progsym.Add("datasize", dataaddr);
            progsym.Add("mainloc", new AddrSExpr("MAIN"));

            progsym.datatype = "progsym";

            romdata.Insert(0, progsym);

            romdata.InsertRange(
                1,
                Symbols.Where(
                    sym => sym.name.All(c => "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".Contains(c))
                    ).Select(
                    sym =>
            {
                var symtable      = new Table(sym.name);
                symtable.datatype = "symbol";

                //DATA = 1 FUNC = 2 CONST = 3
                switch (sym.type)
                {
                case SymbolType.Data:
                    symtable.Add("symtype", 1);
                    break;

                case SymbolType.Function:
                    symtable.Add("symtype", 2);
                    break;

                case SymbolType.Constant:
                    symtable.Add("symtype", 3);
                    break;

                default:
                    throw new NotImplementedException();
                }

                symtable.Add("addr", sym.fixedAddr ?? 0);
                symtable.Add("size", sym.size ?? 0);

                return(symtable);
            })
                );
        }