예제 #1
0
            public unsafe void LoadElementPtr(int index, VMStack stack)
            {
                if (Type is DynamicArrayTypeInfo arrayType)
                {
                    int elementSize = arrayType.ElementType.SizeOf();

                    var elemptr = new VMPointer(this);
                    elemptr.MemOffset += (index * elementSize);

                    stack.Push(elemptr);
                }
                else
                {
                    throw new System.InvalidOperationException();
                }
            }
예제 #2
0
 public VMSlice(VMPointer pointer, int length)
 {
     Handle = pointer.MemLocation;
     Offset = pointer.MemOffset;
     Length = length;
 }
예제 #3
0
        public unsafe void Load(VMPointer pointer, TypeInfo type)
        {
            var heapRef = _heap.Get(pointer.MemLocation);

            using (var handle = heapRef.Memory.Pin((int)pointer.MemOffset))
            {
                if (type is IntegerTypeInfo intType)
                {
                    switch (intType.Width)
                    {
                    case IntegerWidth.I8:
                        Push(*handle.Ptr);
                        break;

                    case IntegerWidth.I16:
                        Push(*((ushort *)handle.Ptr));
                        break;

                    case IntegerWidth.I32:
                        Push(*((uint *)handle.Ptr));
                        break;

                    case IntegerWidth.I64:
                        Push(*((ulong *)handle.Ptr));
                        break;
                    }
                }
                else if (type is FloatTypeInfo floatType)
                {
                    switch (floatType.Width)
                    {
                    case FloatWidth.F32:
                        Push(*((float *)handle.Ptr));
                        break;

                    case FloatWidth.F64:
                        Push(*((double *)handle.Ptr));
                        break;
                    }
                }
                else if (type is BooleanTypeInfo)
                {
                    Push(*handle.Ptr);
                }
                else if (type is CharTypeInfo)
                {
                    Push(*((char *)handle.Ptr));
                }
                else if (type is DynamicArrayTypeInfo || type is StringTypeInfo)
                {
                    Push(*((VMSlice *)handle.Ptr));
                }
                else if (type is ReferenceTypeInfo || type is PointerTypeInfo)
                {
                    Push(*((VMPointer *)handle.Ptr));
                }
                else if (type is StaticArrayTypeInfo || type is StructTypeInfo)
                {
                    // just direct copy the bytes to the top of the stack
                    int    bytesToCopy = type.SizeOf();
                    ulong *ptr64       = (ulong *)handle.Ptr;

                    while (bytesToCopy >= 8)
                    {
                        Push(*ptr64++);
                        bytesToCopy -= 8;
                    }

                    byte *ptr8 = (byte *)ptr64;
                    while (bytesToCopy > 0)
                    {
                        Push(*ptr8++);
                        bytesToCopy--;
                    }
                }
                else
                {
                    throw new System.NotImplementedException();
                }
            }
        }