Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:DotNetAsm.ScopeBlockHandler"/> class.
 /// </summary>
 public ScopeBlockHandler()
 {
     Reserved.DefineType("Scoped", ConstStrings.OPEN_SCOPE, ConstStrings.CLOSE_SCOPE);
     _scope          = new Stack <string>();
     _processedLines = new List <SourceLine>();
     _anon           = 0;
 }
        /// <summary>
        /// Constructs an instance of a <see cref="T:DotNetAsm.AssemblyController"/>, which controls the
        /// assembly process.
        /// </summary>
        /// <param name="args">The array of <see cref="T:System.String"/> args passed by the commandline.</param>
        public AssemblyController(string[] args)
        {
            Controller = this;

            Options = new AsmCommandLineOptions();
            Options.ProcessArgs(args);

            Reserved.Comparer = Options.StringComparar;

            Reserved.DefineType("Directives",
                                ".cpu", ".endrelocate", ".equ", ".pseudopc", ".realpc", ".relocate", ".end",
                                ".endrepeat", ".proff", ".pron", ".repeat", ConstStrings.VAR_DIRECTIVE
                                );

            Reserved.DefineType("Functions",
                                "abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "count", "deg",
                                "exp", "floor", "frac", "hypot", "ln", "log10", "pow", "rad", "random",
                                "round", "sgn", "sin", "sinh", "sizeof", "sqrt", "tan", "tanh", "trunc",
                                "str", "format"
                                );

            Reserved.DefineType("UserDefined");


            Log = new ErrorLog();

            _processedLines = new List <SourceLine>();

            Output = new Compilation(!Options.BigEndian);

            _specialLabels = new Regex(@"^\*|\+|-$", RegexOptions.Compiled);

            Encoding = new AsmEncoding(Options.CaseSensitive);

            Evaluator = new Evaluator(Options.CaseSensitive);

            Evaluator.DefineSymbolLookup(SymbolsToValues);

            Symbols          = new SymbolManager(this);
            _localLabelScope = string.Empty;

            _preprocessor = new Preprocessor(this, s => IsSymbolName(s.TrimEnd(':'), true, false));
            _assemblers   = new Stack <ILineAssembler>();
            _assemblers.Push(new PseudoAssembler(this, arg =>
            {
                return(IsReserved(arg) || Symbols.Labels.IsScopedSymbol(arg, _currentLine.Scope));
            }));

            _assemblers.Push(new MiscAssembler(this));

            _blockHandlers = new List <IBlockHandler>
            {
                new ConditionHandler(this),
                new MacroHandler(this, IsInstruction),
                new ForNextHandler(this),
                new RepetitionHandler(this),
                new ScopeBlockHandler(this)
            };
            Disassembler = new Disassembler(this);
        }
Exemple #3
0
 /// <summary>
 /// Creates a new instance of a page block processor.
 /// </summary>
 /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
 /// <param name="index">The index at which the block is defined.</param>
 public PageBlockProcessor(AssemblyServices services,
                           int index)
     : base(services, index, false)
 {
     Reserved.DefineType("Directives", ".page", ".endpage");
     _page = GetPage();
 }
Exemple #4
0
 /// <summary>
 /// Constructs a Preprocessor object.
 /// </summary>
 /// <param name="controller">The assembly controller.</param>
 /// <param name="checkSymbol">A function to check for symbols such as labels or variables.</param>
 public Preprocessor(IAssemblyController controller,
                     Func <string, bool> checkSymbol)
     : base(controller)
 {
     FileRegistry    = new HashSet <string>();
     _symbolNameFunc = checkSymbol;
     Reserved.DefineType("Directives", ".binclude", ".include", ".comment", ".endcomment");
 }
Exemple #5
0
 /// <summary>
 /// Constructs a DotNetAsm.MiscAssembler class.
 /// </summary>
 public MiscAssembler()
 {
     Reserved.DefineType("Directives",
                         "assert", ".eor", ".echo", ".target",
                         ".error", ".errorif",
                         ".warnif", ".warn"
                         );
 }
Exemple #6
0
        /// <summary>
        /// Creates a new instance of a switch block processor.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        /// <param name="index">The index at which the block is defined.</param>
        public SwitchBlock(AssemblyServices services,
                           int index)
            : base(services, index)
        {
            Reserved.DefineType("Directives", ".switch", ".case", ".default", ".endswitch");

            Reserved.DefineType("BreakContReturn", ".break", ".continue", ".return");
        }
Exemple #7
0
        /// <summary>
        /// Constructs an instance of a <see cref="T:DotNetAsm.RepetitionHandler"/> object.
        /// </summary>
        public RepetitionHandler()
        {
            Reserved.DefineType("Directives", ".repeat", ".endrepeat");

            _currBlock      =
                _rootBlock  = new RepetitionBlock();
            _levels         = 0;
            _processedLines = new List <SourceLine>();
        }
Exemple #8
0
 /// <summary>
 /// Constructs an instance of the <see cref="T:DotNetAsm.Disassembler"/> class.
 /// </summary>
 public Disassembler()
 {
     PrintingOn = true;
     Reserved.DefineType("Blocks", ConstStrings.OPEN_SCOPE, ConstStrings.CLOSE_SCOPE);
     Reserved.DefineType("Directives",
                         ".cpu", ".elif", ".else", ".endif", ".eor", ".error", ".errorif", ".if", ".ifdef",
                         ".warnif", ".relocate", ".pseudopc", ".realpc", ".endrelocate", ".warn"
                         );
 }
Exemple #9
0
 /// <summary>
 /// Constructs a DotNetAsm.MiscAssembler class.
 /// </summary>
 /// <param name="controller">The DotNetAsm.IAssemblyController to associate</param>
 public MiscAssembler(IAssemblyController controller) :
     base(controller)
 {
     Reserved.DefineType("Directives",
                         "assert", ".eor", ".echo", ".target",
                         ".error", ".errorif",
                         ".warnif", ".warn"
                         );
 }
Exemple #10
0
        /// <summary>
        /// Constructs a new instance of the assignment assembler class.
        /// </summary>
        public AssignmentAssembler()
        {
            Reserved.DefineType("Assignments", ".equ", ".global");

            Reserved.DefineType("Pseudo",
                                ".relocate", ".pseudopc", ".endrelocate", ".realpc");

            Reserved.DefineType("Directives", ".let", ".org");
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="T:DotNetAsm.MacroHandler"/> class.
 /// </summary>
 /// <param name="controller">A <see cref="T:DotNetAsm.IAssemblyController"/>.</param>
 /// <param name="instructionFcn">The lookup function to validate whether the name is an instruction or directive.</param>
 public MacroHandler(IAssemblyController controller, Func <string, bool> instructionFcn)
     : base(controller)
 {
     Reserved.DefineType("Directives", ".macro", ".endmacro", ".segment", ".endsegment");
     _macros           = new Dictionary <string, Macro>(controller.Options.StringComparar);
     _expandedSource   = new List <SourceLine>();
     _macroDefinitions = new Stack <List <SourceLine> >();
     _instructionFcn   = instructionFcn;
     _definitions      = new Stack <SourceLine>();
 }
Exemple #12
0
 /// <summary>
 /// Constructs a DotNetAsm.MiscAssembler class.
 /// </summary>
 public MiscAssembler()
 {
     Reserved.DefineType("Directives",
                         ".assert", ".bank",
                         ".eor", ".echo", ".format",
                         ".initmem", ".target",
                         ".error", ".errorif",
                         ".pron", ".proff",
                         ".warnif", ".warn"
                         );
 }
Exemple #13
0
 /// <summary>
 /// Constructs an instance of the <see cref="T:DotNetAsm.Disassembler"/> class.
 /// </summary>
 /// <param name="controller">The assembly controller.</param>
 public Disassembler(IAssemblyController controller)
     : base(controller)
 {
     PrintingOn = true;
     Reserved.DefineType("Blocks", ConstStrings.OPEN_SCOPE, ConstStrings.CLOSE_SCOPE);
     Reserved.DefineType("Directives",
                         ".cpu", ".elif", ".else", ".endif", ".eor", ".error", ".errorif", ".if", ".ifdef",
                         ".warnif", ".relocate", ".pseudopc", ".realpc", ".endrelocate", ".warn",
                         ".m16", ".m8", ".x16", ".x8", ".mx16", ".mx8"
                         );
 }
Exemple #14
0
        /// <summary>
        /// Constructs a new instance of the preprocessor class.
        /// </summary>
        public Preprocessor()
        {
            Reserved.DefineType("Directives",
                                ".comment", ".endcomment", ".macro",
                                ".endmacro", ".include", ".binclude");

            Reserved.DefineType("MacroNames");

            _includedFiles = new HashSet <string>();
            _macros        = new Dictionary <string, Macro>();
        }
        /// <summary>
        /// Constructs a <see cref="T:DotNetAsm.StringAssemblerBase"/> class.
        /// </summary>
        protected StringAssemblerBase()
        {
            Reserved.DefineType("Directives",
                                ".cstring", ".lsstring", ".nstring", ".pstring", ".string"
                                );

            Reserved.DefineType("Encoding", ".encoding", ".map", ".unmap");

            _regEncName = new Regex("^" + Patterns.SymbolBasic + "$",
                                    Assembler.Options.RegexOption | RegexOptions.Compiled);
        }
Exemple #16
0
 /// <summary>
 /// Constructs an instance of the <see cref="T:DotNetAsm.ForNextHandler"/>.
 /// </summary>
 public ForNextHandler()
 {
     Reserved.DefineType("Directives",
                         ".for", ".next", ".break",
                         "@@ for @@", "@@ next @@", "@@ break @@"
                         );
     _currBlock      =
         _rootBlock  = new ForNextBlock();
     _breakBlock     = null;
     _levels         = 0;
     _processedLines = new List <SourceLine>();
 }
Exemple #17
0
        /// <summary>
        /// Constructs an instance of a <see cref="T:DotNetAsm.PseudoAssembler"/> line assembler.
        /// </summary>
        /// <param name="isInstructionFunc">A function callback to determine if the given token is
        /// an instruction.</param>
        /// <param name="reservedSymbolFunc">A function callback to determine if the given token
        /// is a symbol name.</param>
        public PseudoAssembler(Func <string, bool> isInstructionFunc, Func <string, bool> reservedSymbolFunc)
        {
            _includedBinaries = new HashSet <BinaryFile>();

            Reserved.DefineType("PseudoOps",
                                ".addr", ".align", ".binary", ".byte", ".sbyte",
                                ".dint", ".dword", ".fill", ".lint", ".long",
                                ".sint", ".typedef", ".word"
                                );
            _typeDefs       = new Dictionary <string, string>(Assembler.Options.StringComparar);
            _instruction    = isInstructionFunc;
            _reservedSymbol = reservedSymbolFunc;
        }
Exemple #18
0
        /// <summary>
        /// Constructs an instance of a <see cref="T:DotNetAsm.PseudoAssembler"/> line assembler.
        /// </summary>
        /// <param name="controller">The assembly controller</param>
        /// <param name="reservedSymbolFunc">A function callback to determine if the given token
        /// is a symbol name.</param>
        public PseudoAssembler(IAssemblyController controller, Func <string, bool> reservedSymbolFunc) :
            base(controller)
        {
            _includedBinaries = new HashSet <BinaryFile>();

            Reserved.DefineType("PseudoOps",
                                ".addr", ".align", ".binary", ".byte", ".sbyte",
                                ".dint", ".dword", ".fill", ".lint", ".long",
                                ".sint", ".typedef", ".word"
                                );
            _typeDefs       = new Dictionary <string, string>();
            _reservedSymbol = reservedSymbolFunc;
        }
Exemple #19
0
 /// <summary>
 /// Initialize a new instance of a <see cref="T:DotNetAsm.ConditionHandler"/> class.
 /// </summary>
 public ConditionHandler()
 {
     Reserved.DefineType("Conditions",
                         ".if", ".ifdef", ".ifndef",
                         ".elif", ".elifdef", ".elifndef",
                         ".else", ".endif"
                         );
     _condLevel      = 0;
     _condStack      = new Stack <string>();
     _resultStack    = new Stack <bool>();
     _processedLines = new List <SourceLine>();
     _doNotAsm       = false;
 }
Exemple #20
0
        /// <summary>
        /// Constructs an instance of a <see cref="PseudoAssembler"/> line assembler.
        /// </summary>
        public PseudoAssembler()
        {
            _includedBinaries = new Dictionary <string, BinaryFile>();

            Reserved.DefineType("Types",
                                ".addr", ".align", ".binary", ".byte", ".sbyte",
                                ".dint", ".dword", ".fill", ".lint", ".long",
                                ".rta", ".sint", ".word",
                                ".cstring", ".lstring", ".nstring", ".pstring",
                                ".string"
                                );

            Evaluator.AddFunctionEvaluator(this);
        }
Exemple #21
0
 /// <summary>
 /// Constructs a DotNetAsm.MiscAssembler class.
 /// </summary>
 /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
 public MiscAssembler(AssemblyServices services)
     : base(services)
 {
     Reserved.DefineType("Directives",
                         ".assert", ".bank", ".end",
                         ".eor", ".echo", ".forcepass",
                         ".format", ".invoke",
                         ".initmem", ".target",
                         ".error", ".errorif",
                         ".pron", ".proff",
                         ".warnif", ".warn",
                         ".dsection", ".section"
                         );
     ExcludedInstructionsForLabelDefines.Add(".section");
     Services.PassChanged += (s, a) => Services.PrintOff = false;
 }
Exemple #22
0
        /// <summary>
        /// Constructs an instance of a <see cref="PseudoAssembler"/> line assembler.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        public PseudoAssembler(AssemblyServices services)
            : base(services)
        {
            Reserved.DefineType("Types",
                                ".addr", ".align", ".binary", ".bstring", ".byte", ".sbyte",
                                ".char", ".dint", ".dword", ".fill", ".hstring", ".lint",
                                ".long", ".rta", ".short", ".sint", ".word",
                                ".cstring", ".lstring", ".nstring", ".pstring",
                                ".string", ".cbmflt", ".cbmfltp"
                                );

            Reserved.DefineType("Functions",
                                "cbmflt", "cbmfltp", "char", "format", "peek", "poke", "section"
                                );
            services.Evaluator.AddFunctionEvaluator(this);
            _includedBinaries = new Dictionary <StringView, BinaryFile>(services.StringViewComparer);
        }
        /// <summary>
        /// Creates a new instance of a conditional block processor.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        /// <param name="index">The index at which the block is defined.</param>
        public ConditionalBlock(AssemblyServices services, int index)
            : base(services, index, false)
        {
            _opens = new HashSet <string>(services.StringComparer)
            {
                ".if", ".ifdef", ".ifndef"
            };
            Reserved.DefineType("Keywords",
                                ".if", ".ifdef", ".ifndef", ".else", ".elseif",
                                ".elseif", ".elseifdef", ".elseifndef", ".endif");

            Reserved.DefineType("Defs",
                                ".ifdef", ".ifndef");

            _ifDefEvaluations  = new Dictionary <(string, int), bool>();
            _ifTrue            =
                _elseEvaluated = false;
        }
        /// <summary>
        /// Constructs a <see cref="T:DotNetAsm.StringAssemblerBase"/> class.
        /// </summary>
        /// <param name="controller">The <see cref="T:DotNetAsm.IAssemblyController"/> to associate</param>
        public StringAssemblerBase(IAssemblyController controller) :
            base(controller)
        {
            Reserved.DefineType("Directives",
                                ".cstring", ".lsstring", ".nstring", ".pstring", ".string"
                                );

            Reserved.DefineType("Encoding", ".encoding", ".map", ".unmap");

            _regStrFunc = new Regex(@"str(\(.+\))",
                                    Controller.Options.RegexOption | RegexOptions.Compiled);

            _regFmtFunc = new Regex(@"format(\(.+\))",
                                    Controller.Options.RegexOption | RegexOptions.Compiled);

            _regEncName = new Regex("^" + Patterns.SymbolBasic + "$",
                                    Controller.Options.RegexOption | RegexOptions.Compiled);
        }
Exemple #25
0
        /// <summary>
        /// Constructs a new instance of a block assembler.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        public BlockAssembler(AssemblyServices services)
            : base(services)
        {
            _blocks       = new Stack <BlockProcessorBase>();
            _functionDefs = new Dictionary <StringView, Function>(services.StringViewComparer);

            _currentBlock = null;

            _openClosures = new Dictionary <StringView, StringView>(services.StringViewComparer)
            {
                { ".block", ".endblock" },
                { ".for", ".next" },
                { ".foreach", ".next" },
                { ".function", ".endfunction" },
                { ".if", ".endif" },
                { ".ifdef", ".endif" },
                { ".ifndef", ".endif" },
                { ".namespace", ".endnamespace" },
                { ".page", ".endpage" },
                { ".repeat", ".endrepeat" },
                { ".switch", ".endswitch" },
                { ".while", ".endwhile" }
            };

            Reserved.DefineType("Functional",
                                ".function", ".endfunction");

            Reserved.DefineType("NonOpens",
                                ".break", ".case", ".continue", ".default", ".endblock", ".endif",
                                ".endfunction", ".endpage", ".endnamespace", ".endrepeat", ".endswitch",
                                ".endwhile", ".else", ".elseif", ".elseifdef", ".elseifdef", ".elseifndef", ".next");

            Reserved.DefineType("BreakContinue", ".break", ".continue");

            Reserved.DefineType("Goto", ".goto");

            ExcludedInstructionsForLabelDefines.Add(".function");
            ExcludedInstructionsForLabelDefines.Add(".block");

            Services.Evaluator.AddFunctionEvaluator(this);

            Services.IsReserved.Add(s => _functionDefs.ContainsKey(s));
        }
        /// <summary>
        /// Constructs a new instance of the assignment assembler class.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        public AssignmentAssembler(AssemblyServices services)
            : base(services)
        {
            Reserved.DefineType("Assignments", ".equ", ".global", "=");

            Reserved.DefineType("Pseudo",
                                ".relocate", ".pseudopc", ".endrelocate", ".realpc");

            Reserved.DefineType("Directives", ".let", ".org");

            Reserved.DefineType("Functions", "len");

            ExcludedInstructionsForLabelDefines.Add(".org");
            ExcludedInstructionsForLabelDefines.Add(".equ");
            ExcludedInstructionsForLabelDefines.Add("=");
            ExcludedInstructionsForLabelDefines.Add(".global");

            Services.Evaluator.AddFunctionEvaluator(this);
        }
Exemple #27
0
        /// <summary>
        /// Constructs an instance of a <see cref="T:DotNetAsm.AssemblyController"/>, which controls the
        /// assembly process.
        /// </summary>
        /// <param name="args">The array of <see cref="T:System.String"/> args passed by the commandline.</param>
        public AssemblyController(string[] args) :
            base(args)
        {
            Reserved.DefineType("Directives",
                                ".cpu", ".endrelocate", ".equ", ".pseudopc", ".realpc", ".relocate", ".end",
                                ".endrepeat", ".proff", ".pron", ".repeat", ConstStrings.VAR_DIRECTIVE
                                );

            Reserved.DefineType("Functions",
                                "abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "count", "deg",
                                "exp", "floor", "frac", "hypot", "ln", "log10", "pow", "rad", "random",
                                "round", "sgn", "sin", "sinh", "sizeof", "sqrt", "tan", "tanh", "trunc",
                                "format"
                                );

            Reserved.DefineType("UserDefined");


            _processedLines = new List <SourceLine>();
            _sourceHandler  = new SourceHandler();

            Assembler.Evaluator.DefineParser(SymbolsToValues);

            _localLabelScope = string.Empty;

            _assemblers = new Stack <ILineAssembler>();
            _assemblers.Push(new PseudoAssembler(IsInstruction,
                                                 arg => IsReserved(arg) || Assembler.Symbols.Labels.IsScopedSymbol(arg, _currentLine.Scope)));

            _assemblers.Push(new MiscAssembler());

            _blockHandlers = new List <IBlockHandler>
            {
                _sourceHandler,
                new ConditionHandler(),
                new MacroHandler(IsInstruction),
                new ForNextHandler(),
                new RepetitionHandler(),
                new ScopeBlockHandler()
            };
            Disassembler = new Disassembler();
        }
Exemple #28
0
        /// <summary>
        /// Constructs a new instance of a CPU assembler.
        /// </summary>
        /// <param name="services">The shared <see cref="AssemblyServices"/> object.</param>
        protected CpuAssembler(AssemblyServices services)
            : base(services)
        {
            Reserved.DefineType("CPU", ".cpu");
            if (!string.IsNullOrEmpty(Services.CPU))
            {
                CPU = Services.CPU;
            }
            else
            {
                _cpu = string.Empty;
            }
            _initCpu = _cpu;
            OnSetCpu();

            Evaluations = new double[]
            {
                double.NaN, double.NaN, double.NaN
            };
        }
Exemple #29
0
        public Z80Asm()
        {
            Reserved.DefineType("Mnemonics",
                                "adc", "add", "ccf", "cpd", "cpdr", "cpi", "cpir", "cpl",
                                "daa", "dec", "di", "ei", "ex", "exx", "halt", "in",
                                "inc", "ind", "indr", "ini", "inir", "ld", "ldd", "lddr",
                                "ldi", "ldir", "neg", "nop", "otdr", "otir", "out", "outd",
                                "outi", "pop", "push", "reti", "retn", "rl", "rla", "rlc",
                                "rlca", "rld", "rr", "rra", "rrc", "rrca", "rrd", "rst",
                                "sbc", "scf", "sla", "sll", "slr", "sra", "srl", "xor"
                                );

            Reserved.DefineType("Bits",
                                "bit", "res", "set"
                                );

            Reserved.DefineType("Shifts",
                                "rl", "rla", "rlc", "rld", "rr", "rra", "rrc",
                                "rrd", "sla", "sll", "slr", "sra", "srl"
                                );

            Reserved.DefineType("ImpliedA",
                                "and", "cp", "or", "sub", "xor"
                                );

            Reserved.DefineType("Interrupt",
                                "im"
                                );

            Reserved.DefineType("Branches",
                                "call", "jp", "jr", "ret"
                                );

            Reserved.DefineType("Relatives",
                                "djnz", "jr"
                                );

            Assembler.SymbolManager.AddValidSymbolNameCriterion(s => !_namedModes.ContainsKey(s));
        }
Exemple #30
0
        /// <summary>
        /// Constructs a new instance of the multiline assembler class.
        /// </summary>
        public MultiLineAssembler()
        {
            _blocks = new Stack <BlockProcessorBase>();

            _functionDefs = new Dictionary <string, Function>();

            _currentBlock = null;

            Reserved.DefineType("Scope", ".block", ".endblock");

            Reserved.DefineType("Conditional",
                                ".if", ".ifdef", ".ifndef", ".else", ".elseif",
                                ".elseifdef", ".elseifndef", ".endif");

            Reserved.DefineType("SwitchCase",
                                ".switch", ".case", ".default", ".endswitch");

            Reserved.DefineType("Functional",
                                ".function", ".endfunction", ".invoke", ".return");

            Reserved.DefineType("ForNext", ".for", ".next");

            Reserved.DefineType("While", ".while", ".endwhile");

            Reserved.DefineType("Repeat", ".repeat", ".endrepeat");

            Reserved.DefineType("BreakContinue", ".break", ".continue");

            Reserved.DefineType("Page", ".page", ".endpage");

            Reserved.DefineType("GotoEnd", ".goto", ".end");

            Evaluator.AddFunctionEvaluator(this);

            Assembler.PassChanged += CheckCurrentLineAtPass;
        }