Beispiel #1
0
 public VMStack(VMHeap heap, int stackSize)
 {
     // stupid hack: to unify pointer logic, we allocate the stack from our VM heap too
     _heap       = heap;
     _stackAlloc = heap.Get(heap.malloc(null, stackSize));
     heap.Pin(_stackAlloc.Handle);
     _mem = _stackAlloc.Memory;
     _ptr = 0;
 }
Beispiel #2
0
        public unsafe void Store(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:
                        *handle.Ptr = PopUInt8();
                        break;

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

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

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

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

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

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