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

Classes

struct  FindLocalVarState
 
struct  FindLocalVarStoreSite
 

Static Public Member Functions

static void FindLvtEntry (LocalVar lv, ClassFile.Method method, int instructionIndex)
 
static bool IsLoadLocal (NormalizedByteCode bc)
 
static bool IsStoreLocal (NormalizedByteCode bc)
 
static Dictionary< int, string >[] FindLocalVariables (CodeInfo codeInfo, RuntimeJavaMethod mw, ClassFile classFile, ClassFile.Method method)
 
static Dictionary< int, string >[] FindLocalVariablesImpl (RuntimeContext context, CodeInfo codeInfo, ClassFile classFile, ClassFile.Method method, FindLocalVarState[] state)
 
static void VisitLocalLoads (RuntimeContext context, CodeInfo codeInfo, ClassFile.Method method, List< LocalVar > locals, Dictionary< long, LocalVar > localByStoreSite, Dictionary< int, string > storeSites, int instructionIndex, bool debug)
 
static long MakeKey (int i, int j)
 
static LocalVar MergeLocals (RuntimeContext context, List< LocalVar > locals, Dictionary< long, LocalVar > localByStoreSite, LocalVar l1, LocalVar l2)
 

Public Attributes

readonly LocalVar[] localVars
 
readonly LocalVar[][] invokespecialLocalVars
 
readonly LocalVar[] allLocalVars
 

Detailed Description

Definition at line 34 of file LocalVarInfo.cs.

Member Function Documentation

◆ FindLocalVariables()

static Dictionary< int, string >[] IKVM.Runtime.LocalVarInfo.FindLocalVariables ( CodeInfo codeInfo,
RuntimeJavaMethod mw,
ClassFile classFile,
ClassFile.Method method )
static

Definition at line 342 of file LocalVarInfo.cs.

343 {
344 var state = new FindLocalVarState[method.Instructions.Length];
345 state[0].changed = true;
346 state[0].sites = new FindLocalVarStoreSite[method.MaxLocals];
347
348 var parameters = mw.GetParameters();
349 int argpos = 0;
350 if (!mw.IsStatic)
351 state[0].sites[argpos++].Add(-1);
352
353 for (int i = 0; i < parameters.Length; i++)
354 {
355 state[0].sites[argpos++].Add(-1);
356 if (parameters[i].IsWidePrimitive)
357 argpos++;
358 }
359
360 return FindLocalVariablesImpl(mw.DeclaringType.Context, codeInfo, classFile, method, state);
361 }
static Dictionary< int, string >[] FindLocalVariablesImpl(RuntimeContext context, CodeInfo codeInfo, ClassFile classFile, ClassFile.Method method, FindLocalVarState[] state)

◆ FindLocalVariablesImpl()

static Dictionary< int, string >[] IKVM.Runtime.LocalVarInfo.FindLocalVariablesImpl ( RuntimeContext context,
CodeInfo codeInfo,
ClassFile classFile,
ClassFile.Method method,
FindLocalVarState[] state )
static

Definition at line 363 of file LocalVarInfo.cs.

364 {
365 var instructions = method.Instructions;
366 var exceptions = method.ExceptionTable;
367 var maxLocals = method.MaxLocals;
368 var localStoreReaders = new Dictionary<int, string>[instructions.Length];
369
370 var done = false;
371 while (!done)
372 {
373 done = true;
374 for (int i = 0; i < instructions.Length; i++)
375 {
376 if (state[i].changed)
377 {
378 done = false;
379 state[i].changed = false;
380
381 var curr = state[i].Copy();
382
383 for (int j = 0; j < exceptions.Length; j++)
384 if (exceptions[j].startIndex <= i && i < exceptions[j].endIndex)
385 state[exceptions[j].handlerIndex].Merge(curr);
386
387 if (IsLoadLocal(instructions[i].NormalizedOpCode) && (instructions[i].NormalizedOpCode != NormalizedByteCode.__aload || !RuntimeVerifierJavaType.IsFaultBlockException(codeInfo.GetRawStackTypeWrapper(i + 1, 0))))
388 {
389 localStoreReaders[i] ??= new Dictionary<int, string>();
390
391 for (int j = 0; j < curr.sites[instructions[i].NormalizedArg1].Count; j++)
392 localStoreReaders[i][curr.sites[instructions[i].NormalizedArg1][j]] = "";
393 }
394
395 if (IsStoreLocal(instructions[i].NormalizedOpCode) && (instructions[i].NormalizedOpCode != NormalizedByteCode.__astore || !RuntimeVerifierJavaType.IsFaultBlockException(codeInfo.GetRawStackTypeWrapper(i, 0))))
396 {
397 curr.Store(i, instructions[i].NormalizedArg1);
398
399 // if this is a store at the end of an exception block,
400 // we need to propagate the new state to the exception handler
401 for (int j = 0; j < exceptions.Length; j++)
402 if (exceptions[j].endIndex == i + 1)
403 state[exceptions[j].handlerIndex].Merge(curr);
404 }
405
406 if (instructions[i].NormalizedOpCode == NormalizedByteCode.__invokespecial)
407 {
408 var cpi = classFile.GetMethodref(instructions[i].Arg1);
409 if (ReferenceEquals(cpi.Name, StringConstants.INIT))
410 {
411 var type = codeInfo.GetRawStackTypeWrapper(i, cpi.GetArgTypes().Length);
412 // after we've invoked the constructor, the uninitialized references are now initialized
413 if (type == context.VerifierJavaTypeFactory.UninitializedThis || RuntimeVerifierJavaType.IsNew(type))
414 for (int j = 0; j < maxLocals; j++)
415 if (codeInfo.GetLocalTypeWrapper(i, j) == type)
416 curr.Store(i, j);
417 }
418 }
419 else if (instructions[i].NormalizedOpCode == NormalizedByteCode.__goto_finally)
420 {
421 int handler = instructions[i].HandlerIndex;
422
423 // Normally a store at the end of a try block doesn't affect the handler block,
424 // but in the case of a finally handler it does, so we need to make sure that
425 // we merge here in case the try block ended with a store.
426 state[handler].Merge(curr);
427
428 // Now we recursively analyse the handler and afterwards merge the endfault locations back to us
429 FindLocalVarState[] handlerState = new FindLocalVarState[instructions.Length];
430 handlerState[handler].Merge(curr);
431 curr = new FindLocalVarState();
432 FindLocalVariablesImpl(context, codeInfo, classFile, method, handlerState);
433
434 // Merge back to the target of our __goto_finally
435 for (int j = 0; j < handlerState.Length; j++)
436 {
437 if (instructions[j].NormalizedOpCode == NormalizedByteCode.__athrow
438 && codeInfo.HasState(j)
439 && RuntimeVerifierJavaType.IsFaultBlockException(codeInfo.GetRawStackTypeWrapper(j, 0))
440 && ((RuntimeVerifierJavaType)codeInfo.GetRawStackTypeWrapper(j, 0)).Index == handler)
441 {
442 curr.Merge(handlerState[j]);
443 }
444 }
445 }
446
447 switch (ByteCodeMetaData.GetFlowControl(instructions[i].NormalizedOpCode))
448 {
449 case ByteCodeFlowControl.Switch:
450 {
451 for (int j = 0; j < instructions[i].SwitchEntryCount; j++)
452 state[instructions[i].GetSwitchTargetIndex(j)].Merge(curr);
453
454 state[instructions[i].DefaultTarget].Merge(curr);
455 break;
456 }
457 case ByteCodeFlowControl.Branch:
458 state[instructions[i].TargetIndex].Merge(curr);
459 break;
460 case ByteCodeFlowControl.CondBranch:
461 state[instructions[i].TargetIndex].Merge(curr);
462 state[i + 1].Merge(curr);
463 break;
464 case ByteCodeFlowControl.Return:
465 case ByteCodeFlowControl.Throw:
466 break;
467 case ByteCodeFlowControl.Next:
468 state[i + 1].Merge(curr);
469 break;
470 default:
471 throw new InvalidOperationException();
472 }
473 }
474 }
475 }
476
477 return localStoreReaders;
478 }
static bool IsStoreLocal(NormalizedByteCode bc)
static bool IsLoadLocal(NormalizedByteCode bc)

◆ FindLvtEntry()

static void IKVM.Runtime.LocalVarInfo.FindLvtEntry ( LocalVar lv,
ClassFile.Method method,
int instructionIndex )
static

Definition at line 178 of file LocalVarInfo.cs.

179 {
180 var lvt = method.LocalVariableTableAttribute;
181 if (lvt != null)
182 {
183 var pc = method.Instructions[instructionIndex].PC;
184 var nextPC = method.Instructions[instructionIndex + 1].PC;
185 var isStore = IsStoreLocal(method.Instructions[instructionIndex].NormalizedOpCode);
186
187 foreach (var e in lvt)
188 {
189 // TODO validate the contents of the LVT entry
190 if (e.index == lv.local && (e.start_pc <= pc || (e.start_pc == nextPC && isStore)) && e.start_pc + e.length > pc)
191 {
192 lv.name = e.name;
193 lv.start_pc = e.start_pc;
194 lv.end_pc = e.start_pc + e.length;
195 break;
196 }
197 }
198 }
199 }

◆ IsLoadLocal()

static bool IKVM.Runtime.LocalVarInfo.IsLoadLocal ( NormalizedByteCode bc)
static

Definition at line 217 of file LocalVarInfo.cs.

218 {
219 return bc is
220 NormalizedByteCode.__aload or
221 NormalizedByteCode.__iload or
222 NormalizedByteCode.__lload or
223 NormalizedByteCode.__fload or
224 NormalizedByteCode.__dload or
225 NormalizedByteCode.__iinc or
226 NormalizedByteCode.__ret;
227 }

◆ IsStoreLocal()

static bool IKVM.Runtime.LocalVarInfo.IsStoreLocal ( NormalizedByteCode bc)
static

Definition at line 229 of file LocalVarInfo.cs.

230 {
231 return bc is
232 NormalizedByteCode.__astore or
233 NormalizedByteCode.__istore or
234 NormalizedByteCode.__lstore or
235 NormalizedByteCode.__fstore or
236 NormalizedByteCode.__dstore;
237 }

◆ MakeKey()

static long IKVM.Runtime.LocalVarInfo.MakeKey ( int i,
int j )
static

Definition at line 570 of file LocalVarInfo.cs.

571 {
572 return (((long)(uint)i) << 32) + (uint)j;
573 }

◆ MergeLocals()

static LocalVar IKVM.Runtime.LocalVarInfo.MergeLocals ( RuntimeContext context,
List< LocalVar > locals,
Dictionary< long, LocalVar > localByStoreSite,
LocalVar l1,
LocalVar l2 )
static

Definition at line 575 of file LocalVarInfo.cs.

576 {
577 Debug.Assert(l1 != l2);
578 Debug.Assert(l1.local == l2.local);
579
580 for (int i = 0; i < locals.Count; i++)
581 {
582 if (locals[i] == l2)
583 {
584 locals.RemoveAt(i);
585 i--;
586 }
587 }
588
589 var temp = new Dictionary<long, LocalVar>(localByStoreSite);
590 localByStoreSite.Clear();
591 foreach (var kv in temp)
592 localByStoreSite[kv.Key] = kv.Value == l2 ? l1 : kv.Value;
593
594 l1.isArg |= l2.isArg;
595 l1.type = InstructionState.FindCommonBaseType(context, l1.type, l2.type);
596 Debug.Assert(l1.type != context.VerifierJavaTypeFactory.Invalid);
597
598 return l1;
599 }

◆ VisitLocalLoads()

static void IKVM.Runtime.LocalVarInfo.VisitLocalLoads ( RuntimeContext context,
CodeInfo codeInfo,
ClassFile.Method method,
List< LocalVar > locals,
Dictionary< long, LocalVar > localByStoreSite,
Dictionary< int, string > storeSites,
int instructionIndex,
bool debug )
static

Definition at line 480 of file LocalVarInfo.cs.

481 {
482 Debug.Assert(IsLoadLocal(method.Instructions[instructionIndex].NormalizedOpCode));
483
484 LocalVar local = null;
485 var type = context.VerifierJavaTypeFactory.Null;
486 var localIndex = method.Instructions[instructionIndex].NormalizedArg1;
487 var isArg = false;
488 foreach (int store in storeSites.Keys)
489 {
490 if (store == -1)
491 {
492 // it's a method argument, it has no initial store, but the type is simply the parameter type
493 type = InstructionState.FindCommonBaseType(context, type, codeInfo.GetLocalTypeWrapper(0, localIndex));
494 isArg = true;
495 }
496 else
497 {
498 if (method.Instructions[store].NormalizedOpCode == NormalizedByteCode.__invokespecial)
499 {
500 type = InstructionState.FindCommonBaseType(context, type, codeInfo.GetLocalTypeWrapper(store + 1, localIndex));
501 }
502 else if (method.Instructions[store].NormalizedOpCode == NormalizedByteCode.__static_error)
503 {
504 // it's an __invokespecial that turned into a __static_error
505 // (since a __static_error doesn't continue, we don't need to set type)
506 }
507 else
508 {
509 Debug.Assert(IsStoreLocal(method.Instructions[store].NormalizedOpCode));
510 type = InstructionState.FindCommonBaseType(context, type, codeInfo.GetStackTypeWrapper(store, 0));
511 }
512 }
513 // we can't have an invalid type, because that would have failed verification earlier
514 Debug.Assert(type != context.VerifierJavaTypeFactory.Invalid);
515
516 if (localByStoreSite.TryGetValue(MakeKey(store, localIndex), out var l))
517 {
518 if (local == null)
519 {
520 local = l;
521 }
522 else if (local != l)
523 {
524 // If we've already defined a LocalVar and we find another one, then we merge them
525 // together.
526 // This happens for the following code fragment:
527 //
528 // int i = -1;
529 // try { i = 0; for(; ; ) System.out.println(i); } catch(Exception x) {}
530 // try { i = 0; for(; ; ) System.out.println(i); } catch(Exception x) {}
531 // System.out.println(i);
532 //
533 local = MergeLocals(context, locals, localByStoreSite, local, l);
534 }
535 }
536 }
537
538 if (local == null)
539 {
540 local = new LocalVar();
541 local.local = localIndex;
542 local.type = RuntimeVerifierJavaType.IsThis(type) ? ((RuntimeVerifierJavaType)type).UnderlyingType : type;
543 local.isArg = isArg;
544
545 if (debug)
546 FindLvtEntry(local, method, instructionIndex);
547
548 locals.Add(local);
549 }
550 else
551 {
552 local.isArg |= isArg;
553 local.type = InstructionState.FindCommonBaseType(context, local.type, type);
554 Debug.Assert(local.type != context.VerifierJavaTypeFactory.Invalid);
555 }
556
557 foreach (int store in storeSites.Keys)
558 {
559 if (!localByStoreSite.TryGetValue(MakeKey(store, localIndex), out var v))
560 {
561 localByStoreSite[MakeKey(store, localIndex)] = local;
562 }
563 else if (v != local)
564 {
565 local = MergeLocals(context, locals, localByStoreSite, local, v);
566 }
567 }
568 }
static void FindLvtEntry(LocalVar lv, ClassFile.Method method, int instructionIndex)
static long MakeKey(int i, int j)
static LocalVar MergeLocals(RuntimeContext context, List< LocalVar > locals, Dictionary< long, LocalVar > localByStoreSite, LocalVar l1, LocalVar l2)

Member Data Documentation

◆ allLocalVars

readonly LocalVar [] IKVM.Runtime.LocalVarInfo.allLocalVars

Definition at line 39 of file LocalVarInfo.cs.

◆ invokespecialLocalVars

readonly LocalVar [][] IKVM.Runtime.LocalVarInfo.invokespecialLocalVars

Definition at line 38 of file LocalVarInfo.cs.

◆ localVars

readonly LocalVar [] IKVM.Runtime.LocalVarInfo.localVars

Definition at line 37 of file LocalVarInfo.cs.


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