コード例 #1
0
        public static Symbol Unary(MiddleOperator middleOp, Symbol symbol)
        {
            Type type = symbol.Type;

            switch (middleOp)
            {
            case MiddleOperator.Address:
                if (symbol.Value is StaticValue) // &a[i], &s.i
                {
                    StaticValue   staticValue   = (StaticValue)symbol.Value;
                    StaticAddress staticAddress =
                        new StaticAddress(staticValue.UniqueName, staticValue.Offset);
                    return(new Symbol(new Type(type), staticAddress));
                }
                else if (symbol.IsExternOrStatic()) // &i
                {
                    StaticAddress staticAddress =
                        new StaticAddress(symbol.UniqueName, 0);
                    return(new Symbol(new Type(type), staticAddress));
                }
                break;
            }

            return(null);
        }
コード例 #2
0
        /*private static Expression GenerateIndex(Symbol symbol,
         *                                      BigInteger value) {
         * int offset = ((int) value) * symbol.Type.ArrayType.Size();
         * StaticValue resultValue;
         *
         * if (symbol.Value is StaticAddress) {
         *  StaticAddress staticAddress = (StaticAddress) symbol.Value;
         *  resultValue = new StaticValue(staticAddress.UniqueName,
         *                                staticAddress.Offset + offset);
         * }
         * else {
         *  resultValue = new StaticValue(symbol.UniqueName, offset);
         * }
         *
         * Symbol resultSymbol = new Symbol(symbol.Type, resultValue);
         * return (new Expression(resultSymbol, null, null));
         * }*/

        public static Expression Unary(MiddleOperator middleOp, Expression expression)
        {
            Symbol symbol = expression.Symbol;

            switch (middleOp)
            {
            case MiddleOperator.Address:
                if (symbol.Value is StaticValue) // &a[i], &s.i
                {
                    StaticValue   staticValue   = (StaticValue)symbol.Value;
                    StaticAddress staticAddress =
                        new StaticAddress(staticValue.UniqueName, staticValue.Offset);
                    Symbol resultSymbol =
                        new Symbol(new Type(symbol.Type), staticAddress);
                    return(new Expression(resultSymbol, null, null));
                }
                else if (symbol.IsExternOrStatic()) // &i
                {
                    StaticAddress staticAddress =
                        new StaticAddress(symbol.UniqueName, 0);
                    Symbol resultSymbol =
                        new Symbol(new Type(symbol.Type), staticAddress);
                    return(new Expression(resultSymbol, null, null));
                }
                break;

            case MiddleOperator.Dereference:
                if (symbol.Value is StaticAddress)
                {
                    StaticAddress staticAddress = (StaticAddress)symbol.Value;
                    StaticValue   staticValue   =
                        new StaticValue(staticAddress.UniqueName, staticAddress.Offset);
                    Symbol resultSymbol =
                        new Symbol(new Type(symbol.Type), staticValue);
                    return(new Expression(resultSymbol, null, null));
                }
                break;
            }

            return(null);
        }
コード例 #3
0
        private static Expression GenerateAddition(Symbol symbol,
                                                   BigInteger value)
        {
            int           offset = ((int)value) * symbol.Type.PointerOrArrayType.Size();
            StaticAddress resultValue;

            if (symbol.Value is StaticAddress)
            {
                StaticAddress staticAddress = (StaticAddress)symbol.Value;
                resultValue = new StaticAddress(staticAddress.UniqueName,
                                                staticAddress.Offset + offset);
            }
            else
            {
                resultValue = new StaticAddress(symbol.UniqueName, offset);
            }

            Symbol resultSymbol = new Symbol(symbol.Type, resultValue);

            return(new Expression(resultSymbol, null, null));
        }
コード例 #4
0
        public static List <MiddleCode> GenerateStatic(Type toType,
                                                       object fromInitializer)
        {
            List <MiddleCode> codeList = new List <MiddleCode>();

            if (fromInitializer is Expression)
            {
                Expression fromExpression = (Expression)fromInitializer;
                Symbol     fromSymbol     = fromExpression.Symbol;
                Assert.Error(fromSymbol.IsExternOrStatic(), fromSymbol,
                             Message.Non__static_initializer);
                Type fromType = fromSymbol.Type;

                if (toType.IsArray() && toType.ArrayType.IsChar() &&
                    fromType.IsString())
                {
                    string text = (string)fromSymbol.Value;

                    if (toType.ArraySize == 0)
                    {
                        toType.ArraySize = text.Length + 1;
                    }
                    else
                    {
                        Assert.Error(text.Length < toType.ArraySize, toType,
                                     Message.Too_many_initializers_in_array);
                    }

                    codeList.Add(new MiddleCode(MiddleOperator.Initializer,
                                                fromSymbol.Type.Sort, text));
                }
                else if (toType.IsPointer() && fromType.IsArrayFunctionOrString())
                {
                    Assert.ErrorXXX((fromType.IsString() && toType.PointerType.IsChar()) ||
                                    (fromType.IsArray() &&
                                     fromType.ArrayType.Equals(toType.PointerType)) ||
                                    (fromType.IsFunction() &&
                                     fromType.Equals(toType.PointerType)));
                    StaticAddress staticAddress =
                        new StaticAddress(fromSymbol.UniqueName, 0);
                    codeList.Add(new MiddleCode(MiddleOperator.Initializer,
                                                toType.Sort, staticAddress));
                }
                else
                {
                    Expression toExpression =
                        TypeCast.ImplicitCast(fromExpression, toType);
                    Symbol toSymbol = toExpression.Symbol;
                    Assert.Error(toSymbol.Value != null, toSymbol,
                                 Message.Non__constant_expression);
                    codeList.Add(new MiddleCode(MiddleOperator.Initializer,
                                                toSymbol.Type.Sort, toSymbol.Value));
                }
            }
            else
            {
                List <object> fromList = (List <object>)fromInitializer;

                switch (toType.Sort)
                {
                case Sort.Array: {
                    fromList = ModifyInitializer.ModifyArray(toType, fromList);

                    if (toType.ArraySize == 0)
                    {
                        toType.ArraySize = fromList.Count;
                    }
                    else
                    {
                        Assert.Error(fromList.Count <= toType.ArraySize,
                                     toType, Message.Too_many_initializers_in_array);
                    }

                    foreach (object value in fromList)
                    {
                        codeList.AddRange(GenerateStatic(toType.ArrayType, value));
                    }

                    int restSize = toType.Size() -
                                   (fromList.Count * toType.ArrayType.Size());
                    if (restSize > 0)
                    {
                        codeList.Add(new MiddleCode(MiddleOperator.InitializerZero,
                                                    restSize));
                    }
                }
                break;

                case Sort.Struct: {
                    List <Symbol> memberList = toType.MemberList;
                    Assert.Error(fromList.Count <= memberList.Count, toType,
                                 Message.Too_many_initializers_in_struct);

                    int initSize = 0;
                    for (int index = 0; index < fromList.Count; ++index)
                    {
                        Symbol memberSymbol = memberList[index];
                        codeList.AddRange(GenerateStatic(memberSymbol.Type,
                                                         fromList[index]));
                        initSize += memberSymbol.Type.Size();
                    }

                    int restSize = toType.Size() - initSize;
                    if (restSize > 0)
                    {
                        codeList.Add(new MiddleCode(MiddleOperator.InitializerZero,
                                                    restSize));
                    }
                }
                break;

                case Sort.Union: {
                    List <Symbol> memberList = toType.MemberList;
                    Assert.Error(fromList.Count == 1, toType,
                                 Message.Only_one_Initlizer_allowed_in_unions);

                    Symbol memberSymbol = memberList[0];
                    codeList.AddRange(GenerateStatic(memberSymbol.Type,
                                                     fromList[0]));

                    int restSize = toType.Size() - memberSymbol.Type.Size();
                    if (restSize > 0)
                    {
                        codeList.Add(new MiddleCode(MiddleOperator.InitializerZero,
                                                    restSize));
                    }
                }
                break;

                default:
                    Assert.Error(toType, Message.
                                 Only_array_struct_or_union_can_be_initialized_by_a_list);
                    break;
                }
            }

            return(codeList);
        }
コード例 #5
0
        public List <byte> ByteList()
        {
            object operand0 = m_operandArray[0],
                   operand1 = m_operandArray[1],
                   operand2 = m_operandArray[2];

            if ((Operator == AssemblyOperator.empty) ||
                (Operator == AssemblyOperator.label) ||
                (Operator == AssemblyOperator.comment))
            {
                return(new List <byte>());
            }
            else if (Operator == AssemblyOperator.define_address)
            {
                int         offset   = (int)operand1;
                List <byte> byteList = new List <byte>(new byte[TypeSize.PointerSize]);
                AssemblyCode.LoadByteList(byteList, 0, TypeSize.PointerSize,
                                          (BigInteger)offset);
                return(byteList);
            }
            else if (Operator == AssemblyOperator.define_zero_sequence)
            {
                int size = (int)operand0;
                return(new List <byte>(new byte[size]));
            }
            else if (Operator == AssemblyOperator.define_value)
            {
                Sort   sort  = (Sort)operand0;
                object value = operand1;

                if (sort == Sort.Pointer)
                {
                    List <byte> byteList = new List <byte>(new byte[TypeSize.PointerSize]);

                    if (value is string)
                    {
                        AssemblyCode.LoadByteList(byteList, 0, TypeSize.PointerSize,
                                                  BigInteger.Zero);
                    }
                    else if (value is StaticAddress)
                    {
                        StaticAddress staticAddress = (StaticAddress)value;
                        int           offset        = staticAddress.Offset;
                        AssemblyCode.LoadByteList(byteList, 0, TypeSize.PointerSize,
                                                  (BigInteger)offset);
                    }
                    else
                    {
                        AssemblyCode.LoadByteList(byteList, 0, TypeSize.PointerSize,
                                                  (BigInteger)value);
                    }

                    return(byteList);
                }
                else if (sort == Sort.Float)
                {
                    float floatValue = (float)((decimal)operand0);
                    return(new List <byte>(BitConverter.GetBytes(floatValue)));
                }
                else if ((sort == Sort.Double) || (sort == Sort.Long_Double))
                {
                    double doubleValue = (double)((decimal)value);
                    return(new List <byte>(BitConverter.GetBytes(doubleValue)));
                }
                else if (sort == Sort.String)
                {
                    string      text     = (string)value;
                    List <byte> byteList = new List <byte>();

                    foreach (char c in text)
                    {
                        byteList.Add((byte)c);
                    }

                    byteList.Add((byte)0);
                    return(byteList);
                }
                else
                {
                    int         size     = TypeSize.Size(sort);
                    List <byte> byteList = new List <byte>(new byte[size]);
                    AssemblyCode.LoadByteList(byteList, 0, size, (BigInteger)value);
                    return(byteList);
                }
            }
            else if (IsJumpRegister() || IsCallRegister())
            {
                Register register = (Register)operand0;
                return(LookupByteArray(AssemblyOperator.jmp, register));
            }
            else if (IsCallNotRegister())
            {
                List <byte> byteList =
                    LookupByteArray(AssemblyOperator.jmp, TypeSize.PointerSize);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, 0);
                return(byteList);
            }
            else if (Operator == AssemblyOperator.return_address)
            {
                Register    register = (Register)operand0;
                int         offset   = (int)operand1;
                int         size     = SizeOfValue(offset);
                int         address  = (int)((BigInteger)operand2);
                List <byte> byteList =
                    LookupByteArray(AssemblyOperator.mov_word, register,
                                    size, TypeSize.PointerSize);
                LoadByteList(byteList, byteList.Count - (size + TypeSize.PointerSize),
                             size, offset);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, address);
                return(byteList);
            }
            else if (IsRelationNotRegister() || IsJumpNotRegister())
            {
                int         address  = (int)operand0;
                int         size     = ((address >= -128) && (address <= 127)) ? 1 : 2;
                List <byte> byteList = LookupByteArray(Operator, size);
                LoadByteList(byteList, byteList.Count - size, size, address);
                return(byteList);
            }



            // mov ax, bx
            else if ((operand0 is Register) && (operand1 is Register) && (operand2 == null))
            {
                Register toRegister   = (Register)operand0,
                         fromRegister = (Register)operand1;
                return(LookupByteArray(Operator, toRegister, fromRegister));
            }
            // mov ax, global
            else if ((operand0 is Register) && (operand1 is string) && (operand2 == null))
            {
                Register    register = (Register)operand0;
                int         size     = SizeOfRegister(register);
                List <byte> byteList = LookupByteArray(Operator, register, size);
                LoadByteList(byteList, byteList.Count - size, size, 0);
                return(byteList);
            }
            // mov ax, 123
            else if ((operand0 is Register) && (operand1 is BigInteger) && (operand2 == null))
            {
                Register   register = (Register)operand0;
                BigInteger value    = (BigInteger)operand1;
                int        size     = ((Operator == AssemblyOperator.mov) ||
                                       (Operator == AssemblyOperator.and))
                   ? SizeOfRegister(register) : SizeOfValue(value);
                List <byte> byteList = LookupByteArray(Operator, register, size);
                LoadByteList(byteList, byteList.Count - size, size, value);
                return(byteList);
            }



            /*//	cmp global, bx
             * else if (((operand0 is string) || (operand0 == null)) &&
             *       (operand1 is Register) && (operand2 == null)) {
             * Assert.ErrorXXX(Operator == AssemblyOperator.cmp);
             * Register fromRegister = (Register) operand1;
             * List<byte> byteList = LookupByteArray(Operator, null, fromRegister);
             * return byteList;
             * }
             *
             * //	cmp global, 123
             * else if (((operand0 is string) || (operand0 == null)) &&
             *       (operand1 is BigInteger) && (operand2 == null)) {
             * Assert.ErrorXXX(Operator == AssemblyOperator.cmp);
             * BigInteger value = (BigInteger) operand1;
             * int size = SizeOfValue(value);
             * List<byte> byteList = LookupByteArray(Operator, null, size);
             * LoadByteList(byteList, byteList.Count - size, size, value);
             * return byteList;
             * }
             *
             * //	cmp global, global
             * else if (((operand0 is string) || (operand0 == null)) &&
             *       ((operand1 is string) || (operand1 == null)) &&
             *       (operand2 == null)) {
             * Assert.ErrorXXX(Operator == AssemblyOperator.cmp);
             * return LookupByteArray(Operator, TypeSize.PointerSize, TypeSize.PointerSize);
             * }*/



            // mov [bp + 2], ax
            else if ((operand0 is Register) && (operand1 is int) &&
                     (operand2 is Register))
            {
                Register baseRegister = (Register)operand0,
                         fromRegister = (Register)operand2;
                int         offset    = (int)operand1;
                int         size      = SizeOfValue(offset);
                List <byte> byteList  =
                    LookupByteArray(Operator, baseRegister, size, fromRegister);
                LoadByteList(byteList, byteList.Count - size, size, offset);
                return(byteList);
            }
            // mov [bp + 2], global
            else if ((operand0 is Register) && (operand1 is int) &&
                     (operand2 is string))
            {
                Register    baseRegister = (Register)operand0;
                int         offset       = (int)operand1;
                int         offsetSize   = SizeOfValue(offset);
                List <byte> byteList     =
                    LookupByteArray(Operator, baseRegister, offsetSize,
                                    TypeSize.PointerSize);
                LoadByteList(byteList, byteList.Count -
                             (offsetSize + TypeSize.PointerSize), offsetSize, offset);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, 0);
                return(byteList);
            }
            // mov [bp + 2], 123
            else if ((operand0 is Register) && (operand1 is int) &&
                     (operand2 is BigInteger))
            {
                Register   baseRegister = (Register)operand0;
                int        offset       = (int)operand1;
                BigInteger value        = (BigInteger)operand2;
                int        offsetSize   = SizeOfValue(offset),
                           valueSize    = SizeOfValue(value, Operator);
                List <byte> byteList    =
                    LookupByteArray(Operator, baseRegister, offsetSize, valueSize);
                LoadByteList(byteList, byteList.Count - (offsetSize + valueSize),
                             offsetSize, offset);
                LoadByteList(byteList, byteList.Count - valueSize,
                             valueSize, value);
                return(byteList);
            }



            // mov [global + 4], ax
            else if (((operand0 is string) || (operand0 == null)) &&
                     (operand1 is int) && (operand2 is Register))
            {
                int         offset       = (int)operand1;
                Register    fromRegister = (Register)operand2;
                List <byte> byteList     =
                    LookupByteArray(Operator, null, TypeSize.PointerSize, fromRegister);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, offset);
                return(byteList);
            }
            // mov [global + 4], 123
            else if (((operand0 is string) || (operand0 == null)) &&
                     (operand1 is int) && (operand2 is BigInteger))
            {
                int         offset    = (int)operand1;
                BigInteger  value     = (BigInteger)operand2;
                int         valueSize = SizeOfValue(value, Operator);
                List <byte> byteList  =
                    LookupByteArray(Operator, null, TypeSize.PointerSize, valueSize);
                LoadByteList(byteList, byteList.Count - (TypeSize.PointerSize +
                                                         valueSize), TypeSize.PointerSize, offset);
                LoadByteList(byteList, byteList.Count - valueSize,
                             valueSize, value);
                return(byteList);
            }



            // mov ax, [bp + 2]
            else if ((operand0 is Register) && (operand1 is Register) &&
                     (operand2 is int))
            {
                Register toRegister   = (Register)operand0,
                         baseRegister = (Register)operand1;
                int         offset    = (int)operand2;
                int         size      = SizeOfValue(offset);
                List <byte> byteList  =
                    LookupByteArray(Operator, toRegister, baseRegister, size);
                LoadByteList(byteList, byteList.Count - size, size, offset);
                return(byteList);
            }

            /*// cmp global, [bp + 2]
             * else if (((operand0 is string) || (operand0 == null)) &&
             *       (operand1 is Register) && (operand2 is int)) {
             * Assert.ErrorXXX(Operator == AssemblyOperator.cmp);
             * Register baseRegister = (Register) operand1;
             * int offset = (int) operand2;
             * int size = SizeOfValue(offset);
             * List<byte> byteList =
             *  LookupByteArray(Operator, null, baseRegister, size);
             * LoadByteList(byteList, byteList.Count - size, size, offset);
             * return byteList;
             * }*/

            // mov ax, [global + 4]
            else if ((operand0 is Register) && ((operand1 is string) ||
                                                (operand1 == null)) && (operand2 is int))
            {
                Register    toRegister = (Register)operand0;
                int         offset     = (int)operand2;
                List <byte> byteList   =
                    LookupByteArray(Operator, toRegister, null, TypeSize.PointerSize);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, offset);
                return(byteList);
            }


            /*// cmp global, [global + 4]
             * else if (((operand0 is string) || (operand0 == null)) &&
             *       ((operand1 is string) || (operand1 == null)) &&
             *       (operand2 is int)) {
             * int offset = (int) operand2;
             * List<byte> byteList =
             *  LookupByteArray(Operator, null, null, TypeSize.PointerSize);
             * LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
             *             TypeSize.PointerSize, offset);
             * return byteList;
             * }*/



            // inc ax
            else if ((operand0 is Register) && (operand1 == null) && (operand2 == null))
            {
                Register register = (Register)operand0;
                return(LookupByteArray(Operator, register));
            }
            // int 33
            else if ((operand0 is BigInteger) && (operand1 == null) && (operand2 == null))
            {
                BigInteger  value    = (BigInteger)operand0;
                int         size     = SizeOfValue(value);
                List <byte> byteList = LookupByteArray(Operator, size);
                LoadByteList(byteList, byteList.Count - size, size, value);
                return(byteList);
            }



            // inc [bp + 2]
            else if ((operand0 is Register) && (operand1 is int) && (operand2 == null))
            {
                Register    baseRegister = (Register)operand0;
                int         offset       = (int)operand1;
                int         size         = SizeOfValue(offset);
                List <byte> byteList     = LookupByteArray(Operator, baseRegister, size);
                LoadByteList(byteList, byteList.Count - size, size, offset);
                return(byteList);
            }
            // inc [global + 4]
            else if (((operand0 is string) || (operand0 == null)) &&
                     (operand1 is int) && (operand2 == null))
            {
                int         offset   = (int)operand1;
                List <byte> byteList =
                    LookupByteArray(Operator, null, TypeSize.PointerSize);
                LoadByteList(byteList, byteList.Count - TypeSize.PointerSize,
                             TypeSize.PointerSize, offset);
                return(byteList);
            }



            // lahf
            else if ((operand0 == null) && (operand1 == null) &&
                     (operand2 == null))
            {
                return(LookupByteArray(Operator));
            }

            Assert.ErrorXXX(false);
            return(null);
        }
コード例 #6
0
        public static Expression Binary(MiddleOperator middleOp,
                                        Expression leftExpression,
                                        Expression rightExpression)
        {
            Type leftType     = leftExpression.Symbol.Type,
                 rightType    = rightExpression.Symbol.Type;
            object leftValue  = leftExpression.Symbol.Value,
                   rightValue = rightExpression.Symbol.Value;

            switch (middleOp)
            {
            case MiddleOperator.BinaryAdd: // &i + 2
                if ((leftValue is StaticAddress) && (rightValue is BigInteger))
                {
                    StaticAddress staticAddress = (StaticAddress)leftValue;
                    int           resultOffset  =
                        ((int)rightValue) * leftType.PointerType.Size();
                    object resultValue =
                        new StaticAddress(staticAddress.UniqueName,
                                          staticAddress.Offset + resultOffset);
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }

                // 2 + &i
                if ((leftValue is BigInteger) && (rightValue is StaticAddress))
                {
                    StaticAddress staticAddress = (StaticAddress)rightValue;
                    int           resultOffset  =
                        ((int)leftValue) * rightType.PointerType.Size();
                    object resultValue =
                        new StaticAddress(staticAddress.UniqueName,
                                          staticAddress.Offset + resultOffset);
                    Symbol resultSymbol = new Symbol(rightType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }

                if (leftExpression.Symbol.IsExternOrStatic() && leftType.IsArray() &&
                    (rightValue is BigInteger)) // a + 2
                {
                    int    resultOffset = ((int)rightValue) * leftType.ArrayType.Size();
                    object resultValue  =
                        new StaticAddress(leftExpression.Symbol.UniqueName,
                                          resultOffset);
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }

                if (leftExpression.Symbol.IsExternOrStatic() &&
                    (leftValue is BigInteger) && rightType.IsArray()) // 2 + a
                {
                    int    resultOffset = ((int)leftValue) * rightType.ArrayType.Size();
                    object resultValue  =
                        new StaticAddress(rightExpression.Symbol.UniqueName,
                                          resultOffset);
                    Symbol resultSymbol = new Symbol(rightType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }
                break;

            case MiddleOperator.BinarySubtract: // &i - 2
                if ((leftValue is StaticAddress) && (rightValue is BigInteger))
                {
                    StaticAddress staticAddress = (StaticAddress)leftValue;
                    int           resultOffset  = ((int)leftValue) * leftType.PointerType.Size();
                    object        resultValue   =
                        new StaticAddress(staticAddress.UniqueName,
                                          staticAddress.Offset - resultOffset);
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }

                if (leftExpression.Symbol.IsExternOrStatic() && leftType.IsArray() &&
                    (rightValue is BigInteger)) // a - 2
                {
                    int    resultOffset = ((int)rightValue) * leftType.ArrayType.Size();
                    object resultValue  =
                        new StaticAddress(leftExpression.Symbol.UniqueName,
                                          -resultOffset);
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }
                break;

            case MiddleOperator.Index:
                if (leftExpression.Symbol.IsExternOrStatic() && leftType.IsArray() &&
                    (rightValue is BigInteger)) // a[2]
                {
                    int         size        = leftType.ArrayType.Size();
                    int         offset      = ((int)((BigInteger)rightValue)) * size;
                    StaticValue resultValue =
                        new StaticValue(leftExpression.Symbol.UniqueName, offset);
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    resultSymbol.Addressable = !leftExpression.Symbol.IsRegister() &&
                                               !leftType.ArrayType.IsBitfield();
                    resultSymbol.Assignable =
                        !leftType.ArrayType.IsConstantRecursive() &&
                        !leftType.ArrayType.IsArrayFunctionOrString();
                    return(new Expression(resultSymbol, null, null));
                }
                else if ((leftValue is BigInteger) && rightType.IsArray() &&
                         rightExpression.Symbol.IsExternOrStatic()) // [2]a
                {
                    int         size        = rightType.ArrayType.Size();
                    int         offset      = ((int)((BigInteger)leftValue)) * size;
                    StaticValue resultValue =
                        new StaticValue(rightExpression.Symbol.UniqueName, offset);
                    Symbol resultSymbol = new Symbol(rightType, resultValue);
                    resultSymbol.Addressable = !leftExpression.Symbol.IsRegister() &&
                                               !leftType.ArrayType.IsBitfield();
                    resultSymbol.Assignable =
                        !leftType.ArrayType.IsConstantRecursive() &&
                        !leftType.ArrayType.IsArrayFunctionOrString();
                    return(new Expression(resultSymbol, null, null));
                }
                break;

            case MiddleOperator.Dot:
                if (leftExpression.Symbol.IsExternOrStatic())
                {
                    object resultValue =
                        new StaticValue(leftExpression.Symbol.UniqueName,
                                        rightExpression.Symbol.Offset); // s.i
                    Symbol resultSymbol = new Symbol(leftType, resultValue);
                    return(new Expression(resultSymbol, null, null));
                }
                break;
            }

            return(null);
        }
コード例 #7
0
 private Expression Generate(StaticAddress staticAddress, BigInteger offset)
 {
     return(null);
 }
コード例 #8
0
        public static void GenerateByteList(Type type, object value, List <sbyte> sbyteList,
                                            IDictionary <int, string> accessMap)
        {
            if (type.IsString())
            {
                foreach (char c in ((string)value))
                {
                    sbyteList.Add((sbyte)c);
                }
                sbyteList.Add((sbyte)0);
            }
            else if (value is StaticAddress)
            {
                StaticAddress staticAddress = (StaticAddress)value;
                accessMap[sbyteList.Count] = staticAddress.UniqueName;
                sbyteList.Add((sbyte)((short)staticAddress.Offset));
                sbyteList.Add((sbyte)(((short)staticAddress.Offset) >> 8));
            }
            else if (value is StaticValue)
            {
                StaticValue staticValue = (StaticValue)value;

                foreach (KeyValuePair <int, string> pair in staticValue.AccessMap)
                {
                    accessMap[pair.Key + sbyteList.Count] = pair.Value;
                }

                sbyteList.AddRange(staticValue.ByteList);
            }
            else
            {
                switch (type.Sort)
                {
                case Sort.Signed_Short_Int:
                case Sort.Unsigned_Short_Int: {
                    sbyteList.Add((sbyte)((long)value));
                }
                break;

                case Sort.Signed_Char:
                case Sort.Unsigned_Char: {
                    sbyteList.Add((sbyte)((long)value));
                }
                break;

                case Sort.Signed_Int:
                case Sort.Unsigned_Int:
                case Sort.Pointer: {
                    long longValue = (long)value;
                    sbyteList.Add((sbyte)longValue);
                    sbyteList.Add((sbyte)(longValue >> 8));
                }
                break;

                case Sort.Signed_Long_Int:
                case Sort.Unsigned_Long_Int: {
                    long longValue = (long)value;
                    sbyteList.Add((sbyte)longValue);
                    sbyteList.Add((sbyte)(longValue >> 8));
                    sbyteList.Add((sbyte)(longValue >> 16));
                    sbyteList.Add((sbyte)(longValue >> 24));
                }
                break;

                case Sort.Float: {
                    float  floatValue = (float)((decimal)value);
                    byte[] byteArray  = BitConverter.GetBytes(floatValue);

                    foreach (byte b in byteArray)
                    {
                        sbyteList.Add((sbyte)b);
                    }
                }
                break;

                case Sort.Double:
                case Sort.Long_Double: {
                    double doubleValue = (double)((decimal)value);
                    byte[] byteArray   = BitConverter.GetBytes(doubleValue);

                    foreach (byte b in byteArray)
                    {
                        sbyteList.Add((sbyte)b);
                    }
                }
                break;
                }
            }
        }
コード例 #9
0
        public static void GenerateStatic(Type toType, object fromInit,
                                          List <sbyte> byteList, IDictionary <int, string> accessMap)
        {
            if (fromInit is Expression)
            {
                Symbol fromSymbol = ((Expression)fromInit).Symbol();
                Assert.Error(fromSymbol.IsStaticOrExtern(), fromSymbol,
                             Message.Non__static_initializer);
                Type fromType = fromSymbol.Type;

                if (toType.IsArray() && fromType.IsString())
                {
                    Assert.Error(toType.ArrayType.IsChar());
                    fromInit = TextToCharList((string)fromSymbol.Value);
                }

                if (fromType.IsArrayFunctionOrString() && toType.IsPointer())
                {
                    Assert.Error((fromType.IsString() && toType.PointerType.IsChar()) ||
                                 (fromType.IsArray() && fromType.ArrayType.Equals(toType.PointerType)) ||
                                 (fromType.IsFunction() && fromType.Equals(toType.PointerType)));
                    accessMap[byteList.Count] = fromSymbol.UniqueName;
                    byteList.Add((sbyte)0);
                    byteList.Add((sbyte)0);
                }
                else if (fromSymbol.Value is StaticAddress)
                {
                    Assert.Error(fromType.IsPointer() && toType.IsPointer());
                    StaticAddress staticAddress = (StaticAddress)fromSymbol.Value;
                    accessMap[byteList.Count] = staticAddress.UniqueName;
                    byteList.Add((sbyte)staticAddress.Offset);
                    byteList.Add((sbyte)(staticAddress.Offset >> 8));
                }
                else if (fromSymbol.Value is StaticValue)
                {
                    Assert.Error(fromType.Equals(toType));
                    StaticValue staticValue = (StaticValue)fromSymbol.Value;

                    foreach (KeyValuePair <int, string> entry in staticValue.AccessMap)
                    {
                        accessMap[byteList.Count + entry.Key] = entry.Value;
                    }

                    byteList.AddRange(staticValue.ByteList);
                }
                else
                {
                    Symbol toSymbol = TypeCast.ImplicitCast(null, fromSymbol, toType);

                    foreach (KeyValuePair <int, string> entry in toSymbol.StaticSymbol.AccessMap)
                    {
                        accessMap[byteList.Count + entry.Key] = entry.Value;
                    }

                    byteList.AddRange(toSymbol.StaticSymbol.ByteList);
                }
            }
            else
            {
                List <object> fromList = (List <object>)fromInit;

                switch (toType.Sort)
                {
                case Sort.Array: {
                    if (toType.ArraySize == 0)
                    {
                        toType.ArraySize = fromList.Count;
                    }

                    int toByteListSize = byteList.Count + toType.Size();
                    Assert.Error(fromList.Count <= toType.ArraySize, toType, Message.Too_many_initializers);
                    //Assert.Warning(fromList.Count == toType.ArraySize, toType, Message.Too_few_initializers);
                    Type subType = toType.ArrayType;

                    foreach (object value in fromList)
                    {
                        GenerateStatic(subType, value, byteList, accessMap);
                    }

                    GenerateZeroByteList(toByteListSize - byteList.Count, byteList);
                }
                break;

                case Sort.Struct: {
                    IDictionary <string, Symbol> memberMap = toType.MemberMap;
                    Assert.Error(fromList.Count <= memberMap.Count,
                                 toType, Message.Too_many_initializers);

                    /*Assert.Warning(fromList.Count == memberMap.Count,
                     *             toType, Message.Too_few_initializers);*/

                    int toByteListSize = byteList.Count + toType.Size();
                    KeyValuePair <string, Symbol>[] memberArray = new KeyValuePair <string, Symbol> [memberMap.Count];
                    memberMap.CopyTo(memberArray, 0);
                    for (int index = 0; index < fromList.Count; ++index)
                    {
                        Symbol memberSymbol = memberArray[index].Value;
                        object init         = ModifyInitializer.DoInit(memberSymbol.Type, fromList[index]);
                        GenerateStatic(memberSymbol.Type, init, byteList, accessMap);
                    }

                    GenerateZeroByteList(toByteListSize - byteList.Count, byteList);
                }
                break;

                case Sort.Union: {
                    Assert.Error(fromList.Count == 1, toType, Message.A_union_can_be_initalized_by_one_value_only);
                    int toByteListSize = byteList.Count + toType.Size();
                    IDictionary <string, Symbol> memberMap = toType.MemberMap;
                    Symbol firstSymbol = memberMap.Values.GetEnumerator().Current;
                    object init        = ModifyInitializer.DoInit(firstSymbol.Type, fromList[0]);
                    GenerateStatic(firstSymbol.Type, init, byteList, accessMap);
                    GenerateZeroByteList(toByteListSize - byteList.Count, byteList);
                }
                break;
                }
            }
        }