Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 5.28 KB | None | 0 0
  1. /// A variant type used to hold a single value in the CTFE machine stack. CTFE is basically restricted to value types
  2. /// (with the exception of dynamic arrays). Aggregate value types - structs, static arrays, and tuples - are simply stored
  3. /// as consecutive stack slots.
  4. struct CtfeValue
  5. {
  6.     Type mType;
  7.  
  8.     union
  9.     {
  10.         bool mBool;
  11.         ulong mInt;
  12.         real mFloat;
  13.         dchar mChar;
  14.         void[] mArray;
  15.         SmartPtr mPtr;
  16.         FuncSym mFunc;
  17.     }
  18.  
  19.     static CtfeValue opCall(T)(T val)
  20.     {
  21.         CtfeValue ret;
  22.         ret = val;
  23.         return ret;
  24.     }
  25.  
  26.     static CtfeValue errVal()
  27.     {
  28.         CtfeValue ret;
  29.         ret.mType = ErrorType.get();
  30.         return ret;
  31.     }
  32.  
  33.     static CtfeValue stackPtr(size_t offset)
  34.     {
  35.         CtfeValue ret;
  36.         ret.mType = PointerType.get(BasicType.get(Tvoid));
  37.         ret.mPtr.isStack = true;
  38.         ret.mPtr.mStackOffset = offset;
  39.         return ret;
  40.     }
  41.  
  42.     static CtfeValue heapPtr(CtfeValue* ptr)
  43.     {
  44.         CtfeValue ret;
  45.         ret.mType = PointerType.get(BasicType.get(Tvoid));
  46.         ret.mPtr.mPtr = ptr;
  47.         return ret;
  48.     }
  49.    
  50.     void opAssign(bool b)
  51.     {
  52.         mType = BasicType.get(Tbool);
  53.         mBool = b;
  54.     }
  55.  
  56.     void opAssign(byte b)
  57.     {
  58.         mType = BasicType.get(Tint8);
  59.         mInt = b;
  60.     }
  61.  
  62.     void opAssign(ubyte b)
  63.     {
  64.         mType = BasicType.get(Tuint8);
  65.         mInt = b;
  66.     }
  67.  
  68.     void opAssign(short b)
  69.     {
  70.         mType = BasicType.get(Tint16);
  71.         mInt = b;
  72.     }
  73.  
  74.     void opAssign(ushort b)
  75.     {
  76.         mType = BasicType.get(Tuint16);
  77.         mInt = b;
  78.     }
  79.  
  80.     void opAssign(int b)
  81.     {
  82.         mType = BasicType.get(Tint32);
  83.         mInt = b;
  84.     }
  85.  
  86.     void opAssign(uint b)
  87.     {
  88.         mType = BasicType.get(Tuint32);
  89.         mInt = b;
  90.     }
  91.  
  92.     void opAssign(long b)
  93.     {
  94.         mType = BasicType.get(Tint64);
  95.         mInt = b;
  96.     }
  97.  
  98.     void opAssign(ulong b)
  99.     {
  100.         mType = BasicType.get(Tuint64);
  101.         mInt = b;
  102.     }
  103.  
  104.     void opAssign(float f)
  105.     {
  106.         mType = BasicType.get(Tfloat32);
  107.         mFloat = f;
  108.     }
  109.  
  110.     void opAssign(double f)
  111.     {
  112.         mType = BasicType.get(Tfloat64);
  113.         mFloat = f;
  114.     }
  115.  
  116.     void opAssign(real f)
  117.     {
  118.         mType = BasicType.get(Treal);
  119.         mFloat = f;
  120.     }
  121.  
  122.     void opAssign(char c)
  123.     {
  124.         mType = BasicType.get(Tchar);
  125.         mChar = c;
  126.     }
  127.  
  128.     void opAssign(wchar c)
  129.     {
  130.         mType = BasicType.get(Twchar);
  131.         mChar = c;
  132.     }
  133.  
  134.     void opAssign(dchar c)
  135.     {
  136.         mType = BasicType.get(Tdchar);
  137.         mChar = c;
  138.     }
  139.  
  140.     void opAssign(char[] s)
  141.     {
  142.         mType = arrayOf(BasicType.get(Tchar));
  143.         mArray = s;
  144.     }
  145.  
  146.     void opAssign(wchar[] s)
  147.     {
  148.         mType = arrayOf(BasicType.get(Twchar));
  149.         mArray = s;
  150.     }
  151.  
  152.     void opAssign(dchar[] s)
  153.     {
  154.         mType = arrayOf(BasicType.get(Tdchar));
  155.         mArray = s;
  156.     }
  157.  
  158.     void opAssign(FuncSym s)
  159.     {
  160.         mType = s.mType;
  161.         mFunc = s;
  162.     }
  163.  
  164.     bool toBool()
  165.     {
  166.         assert(mType.mTypeTag == Tbool);
  167.         return mBool;
  168.     }
  169.  
  170.     byte toI8()
  171.     {
  172.         assert(mType.mTypeTag == Tint8);
  173.         return cast(byte)((cast(long)mInt) & 0xFF);
  174.     }
  175.  
  176.     ubyte toU8()
  177.     {
  178.         assert(mType.mTypeTag == Tuint8);
  179.         return cast(ubyte)(mInt & 0xFF);
  180.     }
  181.  
  182.     short toI16()
  183.     {
  184.         assert(mType.mTypeTag == Tint16);
  185.         return cast(short)((cast(long)mInt) & 0xFFFF);
  186.     }
  187.  
  188.     ushort toU16()
  189.     {
  190.         assert(mType.mTypeTag == Tuint16);
  191.         return cast(ushort)(mInt & 0xFFFF);
  192.     }
  193.  
  194.     int toI32()
  195.     {
  196.         assert(mType.mTypeTag == Tint32, typeTagToString(mType.mTypeTag));
  197.         return cast(int)((cast(long)mInt) & 0xFFFF_FFFF);
  198.     }
  199.  
  200.     uint toU32()
  201.     {
  202.         assert(mType.mTypeTag == Tuint32);
  203.         return cast(uint)(mInt & 0xFFFF_FFFF);
  204.     }
  205.  
  206.     long toI64()
  207.     {
  208.         assert(mType.mTypeTag == Tint64);
  209.         return cast(long)mInt;
  210.     }
  211.  
  212.     ulong toU64()
  213.     {
  214.         assert(mType.mTypeTag == Tuint64);
  215.         return mInt;
  216.     }
  217.  
  218.     float toF32()
  219.     {
  220.         assert(mType.mTypeTag == Tfloat32);
  221.         return cast(float)mFloat;
  222.     }
  223.  
  224.     double toF64()
  225.     {
  226.         assert(mType.mTypeTag == Tfloat64);
  227.         return cast(double)mFloat;
  228.     }
  229.  
  230.     real toF80()
  231.     {
  232.         assert(mType.mTypeTag == Treal);
  233.         return mFloat;
  234.     }
  235.  
  236.     char toChar()
  237.     {
  238.         assert(mType.mTypeTag == Tchar);
  239.         return mChar & 0xFF;
  240.     }
  241.  
  242.     wchar toWChar()
  243.     {
  244.         assert(mType.mTypeTag == Twchar);
  245.         return mChar & 0xFFFF;
  246.     }
  247.  
  248.     dchar toDChar()
  249.     {
  250.         assert(mType.mTypeTag == Tdchar);
  251.         return mChar;
  252.     }
  253.  
  254.     FuncSym toFunc()
  255.     {
  256.         assert(mType.mTypeTag == Tfunction);
  257.         return mFunc;
  258.     }
  259.    
  260.     CtfeValue* toPtr(CtfeValue* stackBottom)
  261.     {
  262.         assert(mType.mTypeTag == Tpointer);
  263.         return mPtr.isStack ? stackBottom + mPtr.mStackOffset : cast(CtfeValue*)mPtr.mPtr;
  264.     }
  265.    
  266.     char[] toStr()
  267.     {
  268.         assert(mType.mTypeTag == Tdarray && mType.next.mTypeTag == Tchar);
  269.         return cast(char[])mArray;
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement