Exemplo n.º 1
0
        public override void Generate(AstScope currentScope, GenerateContext ctx)
        {
            ctx.ListTo(SourcePosition);

            if (ctx.ListFile != null)
            {
                ctx.ListTo(SourcePosition);
                ctx.WriteListingText($"{ctx.ip:X4}: [{_bytes} bytes]");
                ctx.ListToInclusive(SourcePosition);
            }

            var bytes = new byte?[_bytes];

            if (ValueExpression != null)
            {
                var value = Utils.PackByte(ValueExpression.SourcePosition, ValueExpression.EvaluateNumber(currentScope));

                for (int i = 0; i < bytes.Length; i++)
                {
                    bytes[i] = value;
                }
            }

            ctx.EmitBytes(bytes, false);
        }
Exemplo n.º 2
0
 public override void Generate(AstScope currentScope, GenerateContext ctx)
 {
     ctx.ListToInclusive(SourcePosition);
     ctx.EnterSourceFile(_content.SourcePosition);
     _content.Generate(currentScope, ctx);
     ctx.LeaveSourceFile();
 }
Exemplo n.º 3
0
        public override void DefineSymbols(AstScope currentScope)
        {
            var valueInScope = new ExprNodeDeferredValue(_position, _name);

            _valueToScopeMap.Add(currentScope, valueInScope);
            currentScope.Define(_name, valueInScope);
        }
Exemplo n.º 4
0
        ExprNode FindSymbol(AstScope scope)
        {
            if (Arguments == null)
            {
                // Simple symbol
                var symbol = scope.FindSymbol(_name);
                if (symbol == null)
                {
                    throw new CodeException($"Unrecognized symbol: '{_name}'", SourcePosition);
                }
                var expr = symbol as ExprNode;
                if (expr == null)
                {
                    throw new CodeException($"Invalid expression: '{_name}' is not a value", SourcePosition);
                }
                return(expr);
            }
            else
            {
                // Parameterized symbol
                var symbol = scope.FindSymbol(_name + ExprNodeParameterized.MakeSuffix(Arguments.Length)) as ExprNodeParameterized;
                if (symbol == null)
                {
                    throw new CodeException($"Unrecognized symbol: '{_name}' (with {Arguments.Length} arguments)", SourcePosition);
                }

                // Resolve it
                return(symbol.Resolve(SourcePosition, Arguments));
            }
        }
Exemplo n.º 5
0
 public override IEnumerable <ExprNode> EnumData(AstScope scope)
 {
     foreach (var ch in _value)
     {
         yield return(new ExprNodeNumberLiteral(SourcePosition, ch));
     }
 }
Exemplo n.º 6
0
        public override AddressingMode GetAddressingMode(AstScope scope)
        {
            var lhsMode = LHS.GetAddressingMode(scope);
            var rhsMode = RHS.GetAddressingMode(scope);

            if (lhsMode == AddressingMode.Immediate && rhsMode == AddressingMode.Immediate)
            {
                return(AddressingMode.Immediate);
            }

            if (lhsMode == AddressingMode.Immediate && rhsMode == AddressingMode.Register)
            {
                return(AddressingMode.RegisterPlusImmediate);
            }

            if (lhsMode == AddressingMode.Register && rhsMode == AddressingMode.Immediate)
            {
                return(AddressingMode.RegisterPlusImmediate);
            }

            if (lhsMode == AddressingMode.Immediate && rhsMode == AddressingMode.RegisterPlusImmediate)
            {
                return(AddressingMode.RegisterPlusImmediate);
            }

            if (lhsMode == AddressingMode.RegisterPlusImmediate && rhsMode == AddressingMode.Immediate)
            {
                return(AddressingMode.RegisterPlusImmediate);
            }

            return(AddressingMode.Invalid);
        }
Exemplo n.º 7
0
        public override object Evaluate(AstScope scope)
        {
            // Get the LHS (which must be a type)
            var lhsVal = _lhs.Evaluate(scope);
            var type   = lhsVal as AstType;

            if (type == null)
            {
                var fd = lhsVal as AstFieldDefinition;
                if (fd != null)
                {
                    type = fd.Type;
                }
                else
                {
                    throw new CodeException($"LHS of member operator '.{_name}' is not a type or field ", SourcePosition);
                }
            }

            // Get the field definition
            var fieldDefinition = type.FindField(_name);

            if (fieldDefinition == null)
            {
                throw new CodeException($"The type '{type.Name}' does not contain a field named '{_name}'", SourcePosition);
            }

            // Return the field definition
            return(fieldDefinition);
        }
Exemplo n.º 8
0
        public void BuildType(AstScope definingScope, ref int offset)
        {
            // Store offset
            _offset = offset;

            // Find the field's type
            var symbol = definingScope.FindSymbol(_typename);

            if (symbol == null)
            {
                throw new CodeException($"Unknown type: '{_typename}'", SourcePosition);
            }
            _type = symbol as AstType;
            if (_type == null)
            {
                throw new CodeException($"Invalid type declaration: '{_typename}' is not a type");
            }

            // Resolve the array size
            _arraySize = 0;
            foreach (var d in _initializer.EnumData(definingScope))
            {
                if (!(d is ExprNodeUninitialized))
                {
                    throw new CodeException($"Invalid struct definition: all fields must be declared with uninitialized data", SourcePosition);
                }
                _arraySize++;
            }


            // Update the size
            offset += _type.SizeOf * _arraySize;
        }
Exemplo n.º 9
0
        public string GetBitPattern(AstScope scope)
        {
            if (_bitPattern == null)
            {
                var value    = (int)_value.EvaluateNumber(scope);
                var bitWidth = (int)_bitWidth.EvaluateNumber(scope);
                var str      = Convert.ToString(value, 2);
                if (str.Length > bitWidth)
                {
                    var cutPart = str.Substring(0, str.Length - bitWidth);
                    if (cutPart.Distinct().Count() != 1 || cutPart[0] != str[str.Length - bitWidth])
                    {
                        throw new CodeException($"DEFBITS value 0b{str} doesn't fit in {bitWidth} bits", SourcePosition);
                    }
                }
                else
                {
                    str = new string('0', bitWidth - str.Length) + str;
                }

                _bitPattern = str;
            }

            return(_bitPattern);
        }
Exemplo n.º 10
0
        public override void Layout(AstScope currentScope, LayoutContext ctx)
        {
            // Capture the current ip address as the place where this EQU was defined
            ((ExprNodeEquWrapper)_value).SetOverrides(ctx.ip, ctx.op);

            // Do default
            base.Layout(currentScope, ctx);
        }
Exemplo n.º 11
0
 public override void DefineSymbols(AstScope currentScope)
 {
     currentScope.Define("DB", this);
     currentScope.Define("DEFB", this);
     currentScope.Define("DM", this);
     currentScope.Define("DEFM", this);
     base.DefineSymbols(currentScope);
 }
Exemplo n.º 12
0
 public override void DefineSymbols(AstScope currentScope)
 {
     if (Parser.IsReservedWord(_name))
     {
         throw new CodeException($"Illegal field name: '{_name}' is a reserved word", SourcePosition);
     }
     base.DefineSymbols(currentScope);
 }
Exemplo n.º 13
0
        public override long EvaluateNumber(AstScope scope)
        {
            if (_value.Length == 1)
            {
                return(_value[0]);
            }

            throw new CodeException($"Only single character strings can be used as numeric literals", SourcePosition);
        }
Exemplo n.º 14
0
        public override long EvaluateNumber(AstScope scope)
        {
            if (_value.HasValue)
            {
                return(_value.Value);
            }

            throw new CodeException($"The value of symbol '{_name}' can't been resolved", SourcePosition);
        }
Exemplo n.º 15
0
        public override void DefineSymbols(AstScope currentScope)
        {
            if (_parameters == null)
            {
                _parameters = new string[0];
            }

            // Define the symbol
            currentScope.Define(_name + ExprNodeParameterized.MakeSuffix(_parameters.Length), this);
        }
Exemplo n.º 16
0
        public override long GetImmediateValue(AstScope scope)
        {
            // Temporarily overridden? (See ExprNodeEquWrapper)
            if (_allowOverride && scope.opOverride.HasValue)
            {
                return(scope.opOverride.Value);
            }

            return(_generateContext.op);
        }
Exemplo n.º 17
0
 public ExprNodeParameterizedInstance(SourcePosition pos, string[] parameterNames, ExprNode[] arguments, ExprNode body)
     : base(pos)
 {
     _scope = new AstScope("parameterized equate");
     for (int i = 0; i < parameterNames.Length; i++)
     {
         _scope.Define(parameterNames[i], arguments[i]);
     }
     _body = body;
 }
Exemplo n.º 18
0
        public override AddressingMode GetAddressingMode(AstScope scope)
        {
            var mode = Pointer.GetAddressingMode(scope);

            if ((mode & (AddressingMode.Deref | AddressingMode.SubOp)) == 0)
            {
                return(mode | AddressingMode.Deref);
            }

            return(AddressingMode.Invalid);
        }
Exemplo n.º 19
0
 public override void Layout(AstScope currentScope, LayoutContext ctx)
 {
     if (_isTrue)
     {
         TrueBlock.Layout(currentScope, ctx);
     }
     else if (FalseBlock != null)
     {
         FalseBlock.Layout(currentScope, ctx);
     }
 }
Exemplo n.º 20
0
 public override void Generate(AstScope currentScope, GenerateContext ctx)
 {
     if (_isTrue)
     {
         TrueBlock.Generate(currentScope, ctx);
     }
     else if (FalseBlock != null)
     {
         FalseBlock.Generate(currentScope, ctx);
     }
 }
Exemplo n.º 21
0
 public override IEnumerable <ExprNode> EnumData(AstScope scope)
 {
     if (Condition.EvaluateNumber(scope) != 0)
     {
         return(TrueValue.EnumData(scope));
     }
     else
     {
         return(FalseValue.EnumData(scope));
     }
 }
Exemplo n.º 22
0
 public override AddressingMode GetAddressingMode(AstScope scope)
 {
     if (TrueValue.GetAddressingMode(scope) == AddressingMode.Immediate && FalseValue.GetAddressingMode(scope) == AddressingMode.Immediate)
     {
         return(AddressingMode.Immediate);
     }
     else
     {
         return(AddressingMode.Invalid);
     }
 }
Exemplo n.º 23
0
 public override long EvaluateNumber(AstScope scope)
 {
     if (Condition.EvaluateNumber(scope) != 0)
     {
         return(TrueValue.EvaluateNumber(scope));
     }
     else
     {
         return(FalseValue.EvaluateNumber(scope));
     }
 }
Exemplo n.º 24
0
 public override void DefineSymbols(AstScope currentScope)
 {
     if (this is AstMacroDefinition)
     {
         base.DefineSymbols(currentScope);
     }
     else
     {
         base.DefineSymbols(this);
     }
 }
Exemplo n.º 25
0
 public override AddressingMode GetAddressingMode(AstScope scope)
 {
     if (RHS.GetAddressingMode(scope) == AddressingMode.Immediate && LHS.GetAddressingMode(scope) == AddressingMode.Immediate)
     {
         return(AddressingMode.Immediate);
     }
     else
     {
         return(AddressingMode.Invalid);
     }
 }
Exemplo n.º 26
0
 public override void Layout(AstScope currentScope, LayoutContext ctx)
 {
     if (this is AstMacroDefinition)
     {
         base.Layout(currentScope, ctx);
     }
     else
     {
         base.Layout(this, ctx);
     }
 }
Exemplo n.º 27
0
 public override void Generate(AstScope currentScope, GenerateContext ctx)
 {
     if (this is AstMacroDefinition)
     {
         base.Generate(currentScope, ctx);
     }
     else
     {
         base.Generate(this, ctx);
     }
 }
Exemplo n.º 28
0
 public override AddressingMode GetAddressingMode(AstScope scope)
 {
     if (_value.Length == 1)
     {
         return(AddressingMode.Immediate);
     }
     else
     {
         return(AddressingMode.Invalid);
     }
 }
Exemplo n.º 29
0
        public override void Generate(AstScope currentScope, GenerateContext ctx)
        {
            if (ctx.ListFile != null)
            {
                ctx.ListTo(SourcePosition);
                ctx.WriteListingText($"{ctx.ip:X4}: [{_data.Length} bytes]");
                ctx.ListToInclusive(SourcePosition);
            }

            ctx.EmitBytes(_data, false);
        }
Exemplo n.º 30
0
 public override long EvaluateNumber(AstScope scope)
 {
     if (scope.IsSymbolDefined(_symbolName, true))
     {
         return(1);
     }
     else
     {
         return(0);
     }
 }