SizeOfType() public static method

public static SizeOfType ( TypeReference type ) : int
type Mono.Cecil.TypeReference
return int
Exemplo n.º 1
0
        private JSExpression ComputeAddress(
            JSExpression pointer, JSExpression offsetInElements, JSExpression offsetInBytes,
            out TypeReference elementType
            )
        {
            var pointerType = pointer.GetActualType(TypeSystem);

            elementType = pointerType.GetElementType();
            var elementSize = WasmUtil.SizeOfType(elementType);

            if (
                (offsetInElements != null) &&
                // I'm so helpful :-)))))
                (offsetInBytes == null)
                )
            {
                offsetInBytes = new JSBinaryOperatorExpression(
                    JSOperator.Multiply,
                    offsetInElements,
                    JSLiteral.New(elementSize),
                    pointerType
                    );
            }

            if (offsetInBytes != null)
            {
                var result = new JSPointerAddExpression(pointer, offsetInBytes, false);
                // Console.WriteLine("Constructing pae {0} from {1} {2} {3}", result, pointer, offsetInElements, offsetInBytes);
                return(result);
            }
            else
            {
                return(pointer);
            }
        }
Exemplo n.º 2
0
        public int?GetFieldOffset(FieldDefinition fd)
        {
            var key = fd.FullName;

            FieldTableEntry result;

            if (!FieldTable.TryGetValue(key, out result))
            {
                var size = WasmUtil.SizeOfType(fd.FieldType);

                // HACK HACK HACK
                int offset = fd.IsStatic
                    ? (int)ReserveHeapSpace(size)
                    : WasmUtil.OffsetOfField(fd);

                result = new FieldTableEntry(offset, fd);
                FieldTable.Add(key, result);
            }

            return(result.Offset);
        }
Exemplo n.º 3
0
        public void VisitNode(JSSizeOfExpression sizeofExp)
        {
            var type = sizeofExp.Type.Type;

            Formatter.WriteRaw("(i32.const (; sizeof {0} ;) {1})", type, WasmUtil.SizeOfType(type));
        }
Exemplo n.º 4
0
        void EmitCast(JSExpression value, TypeReference toType)
        {
            var fromType = value.GetActualType(TypeSystem);

            var fromIntegral = TypeUtil.IsIntegral(fromType);
            var toIntegral   = TypeUtil.IsIntegral(toType);

            var fromSize = WasmUtil.SizeOfType(fromType);
            var toSize   = WasmUtil.SizeOfType(toType);

            var fromSign = TypeUtil.IsSigned(fromType);
            var toSign   = TypeUtil.IsSigned(toType);

            var signSuffix = fromSign.GetValueOrDefault(toSign.GetValueOrDefault(true))
                ? "s"
                : "u";

            if (fromIntegral && toIntegral)
            {
                if ((toSize == 4) && (fromSize == 8))
                {
                    Formatter.WriteRaw("(i32.wrap/i64 ");
                    Visit(value);
                    Formatter.WriteRaw(")");
                    return;
                }
                else if ((toSize == 8) && (fromSize == 4))
                {
                    Formatter.WriteRaw("(i64.extend_{0}/i32 ", signSuffix);
                    Visit(value);
                    Formatter.WriteRaw(")");
                    return;
                }
                else if ((toSize == fromSize) && (toSign != fromSign))
                {
                    Visit(value);
                    return;
                }
                else if ((toSize == 2) || (toSize == 1))
                {
                    var mask        = (1 << (8 * toSize)) - 1;
                    var synthesized = new JSBinaryOperatorExpression(
                        JSOperator.BitwiseAnd,
                        value, JSLiteral.New(mask),
                        toType
                        );
                    Visit(synthesized);
                    return;
                }
                else if (toType.FullName == "JSIL.Types.NativeInt")
                {
                    Visit(value);
                    return;
                }
            }
            else if (toIntegral)
            {
                Formatter.WriteRaw(
                    "({0}.trunc_{1}/{2} ",
                    WasmUtil.PickTypeKeyword(toType),
                    signSuffix,
                    WasmUtil.PickTypeKeyword(fromType)
                    );
                Visit(value);
                Formatter.WriteRaw(")");
                return;
            }

            Console.Error.WriteLine("unimplemented cast {0} -> {1}", fromType, toType);
            Visit(value);
        }