1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package net
.multiphasicapps
.classfile
;
12 import cc
.squirreljme
.runtime
.cldc
.debug
.Debugging
;
13 import cc
.squirreljme
.runtime
.cldc
.util
.SortedTreeSet
;
14 import java
.lang
.ref
.Reference
;
15 import java
.lang
.ref
.WeakReference
;
16 import java
.util
.Arrays
;
20 * This represents the targets of jumps that an instruction may jump to.
24 public final class InstructionJumpTargets
27 private final InstructionJumpTarget
[] _normal
;
29 /** Exceptional jumps. */
30 private final InstructionJumpTarget
[] _exception
;
32 /** String representation. */
33 private Reference
<String
> _string
;
39 * Initializes empty jump targets.
43 public InstructionJumpTargets()
45 this._normal
= new InstructionJumpTarget
[0];
46 this._exception
= new InstructionJumpTarget
[0];
50 * Initializes the jump targets.
51 * @param __n Normal jumps.
52 * @param __e Exceptional jumps.
53 * @throws NullPointerException On null arguments.
56 public InstructionJumpTargets(InstructionJumpTarget
[] __n
,
57 InstructionJumpTarget
[] __e
)
58 throws NullPointerException
60 if (__n
== null || __e
== null)
61 throw new NullPointerException("NARG");
63 // Load into sorted sets
64 Set
<InstructionJumpTarget
> nrm
= new SortedTreeSet
<>(),
65 exe
= new SortedTreeSet
<>();
68 for (InstructionJumpTarget i
: __n
)
70 throw new NullPointerException("NARG");
75 for (InstructionJumpTarget i
: __e
)
77 throw new NullPointerException("NARG");
82 this._normal
= nrm
.<InstructionJumpTarget
>toArray(
83 new InstructionJumpTarget
[nrm
.size()]);
84 this._exception
= exe
.<InstructionJumpTarget
>toArray(
85 new InstructionJumpTarget
[exe
.size()]);
93 public final boolean equals(Object __o
)
95 throw Debugging
.todo();
99 * Obtains the given jump target.
101 * @param __i The index.
102 * @return The target jump target.
105 public final InstructionJumpTarget
get(int __i
)
107 InstructionJumpTarget
[] normal
= this._normal
;
108 int numnormal
= normal
.length
;
110 // Treat both arrays as a single part
111 if (__i
>= numnormal
)
112 return this._exception
[__i
- numnormal
];
113 return this._normal
[__i
];
121 public final int hashCode()
123 throw Debugging
.todo();
127 * Checks if any of the jump targets has an address which follows the
130 * @param __pc The address to check.
131 * @return True if any address in the jump targets has an address which
132 * is higher than this address.
135 public final boolean hasLaterAddress(int __pc
)
137 for (int i
= 0, n
= this.size(); i
< n
; i
++)
138 if (this.get(i
).target() > __pc
)
145 * Checks if any of the jump targets has an address which is or follows
148 * @param __pc The address to check.
149 * @return True if any address in the jump targets has an address which
150 * is the same or higher than this address.
153 public final boolean hasSameOrLaterAddress(int __pc
)
155 for (int i
= 0, n
= this.size(); i
< n
; i
++)
156 if (this.get(i
).target() >= __pc
)
163 * Returns if this is empty or not.
165 * @return If this is empty or not.
168 public final boolean isEmpty()
170 return this.size() == 0;
174 * Returns if this is an exception jump.
176 * @param __i The index to get.
177 * @return If this is an exception index.
180 public final boolean isException(int __i
)
182 return __i
>= this._normal
.length
;
186 * Returns the number of jump targets.
188 * @return The number of jump targets.
191 public final int size()
193 return this._normal
.length
+ this._exception
.length
;
201 public final String
toString()
203 Reference
<String
> ref
= this._string
;
206 if (ref
== null || null == (rv
= ref
.get()))
207 this._string
= new WeakReference
<>((rv
=
208 "[N:" + Arrays
.asList(this._normal
) +
209 ", E: " + Arrays
.asList(this._exception
) + "]"));