2 * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 using System.Collections.Generic;
29 * Each value is represented with a TValue structure. Integers use the 'x'
30 * field, and 'ptr' is null; for pointers, the 'ptr' field is used, and the
31 * 'x' is then an offset in the object represented by 'ptr'.
37 internal TPointerBase ptr;
39 internal TValue(int x)
45 internal TValue(uint x)
51 internal TValue(bool b)
57 internal TValue(int x, TPointerBase ptr)
64 * Convert this value to a boolean; integer 0 and null pointer are
65 * 'false', other values are 'true'.
72 return ptr.ToBool(this);
78 * Get this value as an integer. Pointers cannot be converted to
86 throw new Exception("not an integer: " + ToString());
91 * Get this value as an unsigned integer. This is the integer
92 * value, reduced modulo 2^32 in the 0..2^32-1 range.
101 * String format of integers uses decimal representation. For
102 * pointers, this depends on the pointed-to value.
104 public override string ToString()
107 return String.Format("{0}", x);
109 return ptr.ToString(this);
114 * If this value is an XT, then execute it. Otherwise, an exception
117 internal void Execute(T0Comp ctx, CPU cpu)
119 ToXT().Execute(ctx, cpu);
123 * Convert this value to an XT. On failure, an exception is thrown.
125 internal TPointerXT ToXT()
127 TPointerXT xt = ptr as TPointerXT;
130 "value is not an xt: " + ToString());
136 * Compare this value to another.
138 internal bool Equals(TValue v)
146 if (ptr == null || v.ptr == null) {
149 return ptr.Equals(v.ptr);
152 public static implicit operator TValue(bool val)
154 return new TValue(val);
157 public static implicit operator TValue(sbyte val)
159 return new TValue((int)val);
162 public static implicit operator TValue(byte val)
164 return new TValue((int)val);
167 public static implicit operator TValue(short val)
169 return new TValue((int)val);
172 public static implicit operator TValue(ushort val)
174 return new TValue((int)val);
177 public static implicit operator TValue(char val)
179 return new TValue((int)val);
182 public static implicit operator TValue(int val)
184 return new TValue((int)val);
187 public static implicit operator TValue(uint val)
189 return new TValue((int)val);
192 public static implicit operator bool(TValue v)
197 public static implicit operator sbyte(TValue v)
202 public static implicit operator byte(TValue v)
207 public static implicit operator short(TValue v)
212 public static implicit operator ushort(TValue v)
214 return (ushort)v.Int;
217 public static implicit operator char(TValue v)
222 public static implicit operator int(TValue v)
227 public static implicit operator uint(TValue v)