IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
IKVM.Runtime.InstructionState Struct Reference

Classes

struct  LocalStoreSites
 

Public Types

enum  ShareFlags : byte { None = 0 , Stack = 1 , Locals = 2 , All = Stack | Locals }
 

Public Member Functions

 InstructionState (RuntimeContext context, RuntimeJavaType[] stack, int stackLen, int stackEnd, RuntimeJavaType[] locals, bool uninitializedThis)
 Initializes a new instance.
 
void SetLocal1 (int index, RuntimeJavaType type)
 
void SetLocal2 (int index, RuntimeJavaType type)
 
void PushHelper (RuntimeJavaType type)
 
void StackCopyOnWrite ()
 Copies the stack for future modification.
 
void LocalsCopyOnWrite ()
 Copies the locals for future modification.
 

Static Public Member Functions

static InstructionState operator+ (InstructionState s1, InstructionState s2)
 Creates a new state which is the merged combination of the two specified states.
 
static RuntimeJavaType FindCommonBaseTypeHelper (RuntimeContext context, RuntimeJavaType type1, RuntimeJavaType type2)
 
static bool HasMissingBaseType (RuntimeJavaType tw)
 

Public Attributes

readonly RuntimeContext _context
 
RuntimeJavaType[] _stack
 
int _stackLen
 
int _stackEnd
 
RuntimeJavaType[] _locals
 
bool _uninitializedThis
 
ShareFlags _flags
 

Detailed Description

Definition at line 31 of file InstructionState.cs.

Member Enumeration Documentation

◆ ShareFlags

Constructor & Destructor Documentation

◆ InstructionState()

IKVM.Runtime.InstructionState.InstructionState ( RuntimeContext context,
RuntimeJavaType[] stack,
int stackLen,
int stackEnd,
RuntimeJavaType[] locals,
bool uninitializedThis )

Initializes a new instance.

Parameters
context
stack
stackLen
stackEnd
locals
uninitializedThis
Exceptions
ArgumentNullException

Definition at line 120 of file InstructionState.cs.

121 {
122 _context = context ?? throw new ArgumentNullException(nameof(context));
123 _flags = ShareFlags.All;
124 _stack = stack;
125 _stackLen = stackLen;
126 _stackEnd = stackEnd;
127 _locals = locals;
128 _uninitializedThis = uninitializedThis;
129 _initialized = true;
130 }
readonly RuntimeContext _context

Member Function Documentation

◆ FindCommonBaseTypeHelper()

static RuntimeJavaType IKVM.Runtime.InstructionState.FindCommonBaseTypeHelper ( RuntimeContext context,
RuntimeJavaType type1,
RuntimeJavaType type2 )
static

Definition at line 383 of file InstructionState.cs.

384 {
385 if (type1 == type2)
386 return type1;
387
388 // NOTE according to a paper by Alessandro Coglio & Allen Goldberg titled
389 // "Type Safety in the JVM: Some Problems in Java 2 SDK 1.2 and Proposed Solutions"
390 // the common base of two interfaces is java.lang.Object, and there is special
391 // treatment for java.lang.Object types that allow it to be assigned to any interface
392 // type, the JVM's typesafety then depends on the invokeinterface instruction to make
393 // sure that the reference actually implements the interface.
394 // NOTE the ECMA CLI spec also specifies this interface merging algorithm, so we can't
395 // really do anything more clever than this.
396 if (type1.IsInterface && type2.IsInterface)
397 return context.JavaBase.TypeOfJavaLangObject;
398
399 // t1 is an interface and is implemented by t2, common base type is t1
400 if (type1.IsInterface)
401 return type2.ImplementsInterface(type1) ? type1 : context.JavaBase.TypeOfJavaLangObject;
402
403 // t2 is an interface and is implemented by t1, common base type is t2
404 if (type2.IsInterface)
405 return type1.ImplementsInterface(type2) ? type2 : context.JavaBase.TypeOfJavaLangObject;
406
407 var st1 = new Stack<RuntimeJavaType>();
408 while (type1 != null)
409 {
410 st1.Push(type1);
411 type1 = type1.BaseTypeWrapper;
412 }
413
414 var st2 = new Stack<RuntimeJavaType>();
415 while (type2 != null)
416 {
417 st2.Push(type2);
418 type2 = type2.BaseTypeWrapper;
419 }
420
421 // pop each item off of the stacks until they do not match
422 var type = context.JavaBase.TypeOfJavaLangObject;
423 for (; ; )
424 {
425 type1 = st1.Count > 0 ? st1.Pop() : null;
426 type2 = st2.Count > 0 ? st2.Pop() : null;
427 if (type1 != type2)
428 return type;
429
430 type = type1;
431 }
432 }

◆ HasMissingBaseType()

static bool IKVM.Runtime.InstructionState.HasMissingBaseType ( RuntimeJavaType tw)
static

Definition at line 772 of file InstructionState.cs.

773 {
774#if IMPORTER
775 for (RuntimeJavaType baseTypeWrapper; (baseTypeWrapper = tw.BaseTypeWrapper) != null; tw = baseTypeWrapper)
776 {
777 if (baseTypeWrapper.IsUnloadable)
778 {
779 tw.Context.StaticCompiler.IssueMissingTypeMessage(tw.TypeAsBaseType.BaseType);
780 return true;
781 }
782 }
783#endif
784 return false;
785 }

◆ LocalsCopyOnWrite()

void IKVM.Runtime.InstructionState.LocalsCopyOnWrite ( )

Copies the locals for future modification.

Definition at line 863 of file InstructionState.cs.

864 {
865 if ((_flags & ShareFlags.Locals) != 0)
866 {
867 _flags &= ~ShareFlags.Locals;
868 _locals = (RuntimeJavaType[])_locals.Clone();
869 }
870 }

◆ operator+()

static InstructionState IKVM.Runtime.InstructionState.operator+ ( InstructionState s1,
InstructionState s2 )
static

Creates a new state which is the merged combination of the two specified states.

Parameters
s1
s2
Returns
Exceptions
VerifyError

Definition at line 195 of file InstructionState.cs.

196 {
197 if (s1._initialized == false)
198 return s2.Copy();
199
200 if (s1._stackLen != s2._stackLen || s1._stackEnd != s2._stackEnd)
201 throw new VerifyError($"Inconsistent stack height: {s1._stackLen + s1._stack.Length - s1._stackEnd} != {s2._stackLen + s2._stack.Length - s2._stackEnd}");
202
203 var s = s1.Copy();
204 s._changed = s1._changed;
205
206 for (int i = 0; i < s._stackLen; i++)
207 {
208 var type1 = s._stack[i];
209 var type2 = s2._stack[i];
210 if (type1 == type2)
211 {
212 // perfect match, nothing to do
213 }
214 else if ((type1 == s1._context.VerifierJavaTypeFactory.ExtendedDouble && type2 == s1._context.PrimitiveJavaTypeFactory.DOUBLE) || (type2 == s1._context.VerifierJavaTypeFactory.ExtendedDouble && type1 == s1._context.PrimitiveJavaTypeFactory.DOUBLE))
215 {
216 if (type1 != s1._context.VerifierJavaTypeFactory.ExtendedDouble)
217 {
218 s.StackCopyOnWrite();
219 s._stack[i] = s1._context.VerifierJavaTypeFactory.ExtendedDouble;
220 s._changed = true;
221 }
222 }
223 else if ((type1 == s1._context.VerifierJavaTypeFactory.ExtendedFloat && type2 == s1._context.PrimitiveJavaTypeFactory.FLOAT) || (type2 == s1._context.VerifierJavaTypeFactory.ExtendedFloat && type1 == s1._context.PrimitiveJavaTypeFactory.FLOAT))
224 {
225 if (type1 != s1._context.VerifierJavaTypeFactory.ExtendedFloat)
226 {
227 s.StackCopyOnWrite();
228 s._stack[i] = s1._context.VerifierJavaTypeFactory.ExtendedFloat;
229 s._changed = true;
230 }
231 }
232 else if (!type1.IsPrimitive)
233 {
234 var baseType = FindCommonBaseType(s1._context, type1, type2);
235 if (baseType == s1._context.VerifierJavaTypeFactory.Invalid)
236 throw new VerifyError(string.Format("cannot merge {0} and {1}", type1.Name, type2.Name));
237
238 if (type1 != baseType)
239 {
240 s.StackCopyOnWrite();
241 s._stack[i] = baseType;
242 s._changed = true;
243 }
244 }
245 else
246 {
247 throw new VerifyError(string.Format("cannot merge {0} and {1}", type1.Name, type2.Name));
248 }
249 }
250
251 for (int i = 0; i < s._locals.Length; i++)
252 {
253 var type = s._locals[i];
254 var type2 = s2._locals[i];
255 var baseType = FindCommonBaseType(s1._context, type, type2);
256 if (type != baseType)
257 {
258 s.LocalsCopyOnWrite();
259 s._locals[i] = baseType;
260 s._changed = true;
261 }
262 }
263
264 if (!s._uninitializedThis && s2._uninitializedThis)
265 {
266 s._uninitializedThis = true;
267 s._changed = true;
268 }
269
270 return s;
271 }

◆ PushHelper()

void IKVM.Runtime.InstructionState.PushHelper ( RuntimeJavaType type)

Definition at line 813 of file InstructionState.cs.

814 {
815 if (type.IsWidePrimitive || type == _context.VerifierJavaTypeFactory.ExtendedDouble)
816 _stackEnd--;
817
818 if (_stackLen >= _stackEnd)
819 throw new VerifyError("Stack overflow");
820
822 _stack[_stackLen++] = type;
823 }
RuntimeVerifierJavaTypeFactory VerifierJavaTypeFactory
Gets the RuntimeVerifierJavaTypeFactory associated with this instance of the runtime.
void StackCopyOnWrite()
Copies the stack for future modification.

◆ SetLocal1()

void IKVM.Runtime.InstructionState.SetLocal1 ( int index,
RuntimeJavaType type )

Definition at line 434 of file InstructionState.cs.

435 {
436 try
437 {
439 if (index > 0 && _locals[index - 1] != _context.VerifierJavaTypeFactory.Invalid && _locals[index - 1].IsWidePrimitive)
440 _locals[index - 1] = _context.VerifierJavaTypeFactory.Invalid;
441
442 _locals[index] = type;
443 }
444 catch (IndexOutOfRangeException)
445 {
446 throw new VerifyError("Illegal local variable number");
447 }
448 }
void LocalsCopyOnWrite()
Copies the locals for future modification.

◆ SetLocal2()

void IKVM.Runtime.InstructionState.SetLocal2 ( int index,
RuntimeJavaType type )

Definition at line 450 of file InstructionState.cs.

451 {
452 try
453 {
455 if (index > 0 && _locals[index - 1] != _context.VerifierJavaTypeFactory.Invalid && _locals[index - 1].IsWidePrimitive)
456 _locals[index - 1] = _context.VerifierJavaTypeFactory.Invalid;
457
458 _locals[index] = type;
459 _locals[index + 1] = _context.VerifierJavaTypeFactory.Invalid;
460 }
461 catch (IndexOutOfRangeException)
462 {
463 throw new VerifyError("Illegal local variable number");
464 }
465 }

◆ StackCopyOnWrite()

void IKVM.Runtime.InstructionState.StackCopyOnWrite ( )

Copies the stack for future modification.

Definition at line 851 of file InstructionState.cs.

852 {
853 if ((_flags & ShareFlags.Stack) != 0)
854 {
855 _flags &= ~ShareFlags.Stack;
856 _stack = (RuntimeJavaType[])_stack.Clone();
857 }
858 }

Member Data Documentation

◆ _context

readonly RuntimeContext IKVM.Runtime.InstructionState._context

Definition at line 100 of file InstructionState.cs.

◆ _flags

ShareFlags IKVM.Runtime.InstructionState._flags

Definition at line 107 of file InstructionState.cs.

◆ _locals

RuntimeJavaType [] IKVM.Runtime.InstructionState._locals

Definition at line 104 of file InstructionState.cs.

◆ _stack

RuntimeJavaType [] IKVM.Runtime.InstructionState._stack

Definition at line 101 of file InstructionState.cs.

◆ _stackEnd

int IKVM.Runtime.InstructionState._stackEnd

Definition at line 103 of file InstructionState.cs.

◆ _stackLen

int IKVM.Runtime.InstructionState._stackLen

Definition at line 102 of file InstructionState.cs.

◆ _uninitializedThis

bool IKVM.Runtime.InstructionState._uninitializedThis

Definition at line 105 of file InstructionState.cs.


The documentation for this struct was generated from the following file: