Zeal.Compiler.Parser.ZealCpuDriver.verifyBranchLength C# (CSharp) Method

verifyBranchLength() private method

private verifyBranchLength ( ) : void
return void
        private void verifyBranchLength()
        {
            long physicalAddress = 0;

            Scope parentScope = _globalScope;
            foreach (var scope in parentScope.Children)
            {
                foreach (var instruction in scope.Statements)
                {
                    if (instruction is CpuInstructionStatement)
                    {
                        var cpuInstruction = instruction as CpuInstructionStatement;
                        if (cpuInstruction.AddressingMode == CpuAddressingMode.Relative)
                        {
                            int instructionSize = (int)instruction.ComputeSize();

                            if (cpuInstruction.Arguments[0] is LabelInstructionArgument)
                            {
                                var labelArgument = cpuInstruction.Arguments[0] as LabelInstructionArgument;
                                int labelAddress = (int)scope.AddressFor(labelArgument.Label);

                                int branchLength = labelAddress - ((int)physicalAddress + instructionSize);
                                if (branchLength > sbyte.MaxValue || branchLength < sbyte.MinValue)
                                {
                                    ErrorMessage error = new ErrorMessage();
                                    error.SourceFile = SourceFilePath;
                                    error.Message = String.Format("Branch label '{0}' is too far away. Consider reducing the distance or use a jmp.", labelArgument.Label);
                                    error.Line = instruction.Line;
                                    error.Column = instruction.Column;

                                    Errors.Add(error);
                                }
                            }
                        }
                    }

                    physicalAddress += instruction.ComputeSize();
                }
            }
        }