Commit c1762bfa authored by Stephanie Gawroriski's avatar Stephanie Gawroriski
Browse files

Add ability to get int/long values from JITConfig values; Normalize values parsable as long values.

parent 24d45df3
......@@ -28,6 +28,14 @@ import net.multiphasicapps.squirreljme.jit.JITException;
public class SimulatorConfig
extends JITConfig
{
/** Number of local variables. */
public static final JITConfigKey JIT_ARCH_NUMLOCALS =
new JITConfigKey("jit.arch.maxlocals");
/** Number of global registers. */
public static final JITConfigKey JIT_ARCH_NUMGLOBALS =
new JITConfigKey("jit.arch.numglobals");
/**
* Initializes the MIPS configuration.
*
......@@ -63,6 +71,8 @@ public class SimulatorConfig
{
return new JITConfigKey[]
{
JIT_ARCH_NUMLOCALS,
JIT_ARCH_NUMGLOBALS,
};
}
......@@ -82,6 +92,18 @@ public class SimulatorConfig
// Translate?
switch (__k.toString())
{
// The number of local variables
case "jit.arch.maxlocals":
if (__v == null || !__v.isInteger())
return new JITConfigValue("128");
break;
// The number of global variables
case "jit.arch.maxglobals":
if (__v == null || !__v.isInteger())
return new JITConfigValue("8");
break;
// Unchanged
default:
break;
......
......@@ -304,7 +304,99 @@ public abstract class JITConfig
JITConfigValue rv = get(__k);
if (rv == null)
return false;
return Boolean.valueOf(rv.toString());
return rv.isTrue();
}
/**
* Returns the int value of the specified key.
*
* @param __k The key to get the value of.
* @return The value of the given key.
* @throws NumberFormatException If the value could not be parsed.
* @since 2017/08/19
*/
public final int getInteger(JITConfigKey __k)
throws NumberFormatException
{
// Check
if (__k == null)
throw new NullPointerException("NARG");
// {@squirreljme.error JI25 The specified key is null. (The key)}
JITConfigValue rv = get(__k);
if (rv == null)
throw new NumberFormatException(String.format("JI25 %s", __k));
return rv.toInteger();
}
/**
* Obtains a int value or returns a default value.
*
* @param __k The key to get.
* @param __def The default value to return if the value is not parsed as
* the requested type.
* @return The decoded value or {@code __def}.
* @since 2017/08/19
*/
public final int getInteger(JITConfigKey __k, int __def)
{
// Could fail
try
{
return getInteger(__k);
}
// Not a number
catch (NumberFormatException e)
{
return __def;
}
}
/**
* Returns the long value of the specified key.
*
* @param __k The key to get the value of.
* @return The value of the given key.
* @throws NumberFormatException If the value could not be parsed.
* @since 2017/08/19
*/
public final long getLong(JITConfigKey __k)
throws NumberFormatException
{
// Check
if (__k == null)
throw new NullPointerException("NARG");
// {@squirreljme.error JI26 The specified key is null. (The key)}
JITConfigValue rv = get(__k);
if (rv == null)
throw new NumberFormatException(String.format("JI26 %s", __k));
return rv.toLong();
}
/**
* Obtains a long value or returns a default value.
*
* @param __k The key to get.
* @param __def The default value to return if the value is not parsed as
* the requested type.
* @return The decoded value or {@code __def}.
* @since 2017/08/19
*/
public final long getLong(JITConfigKey __k, long __def)
{
// Could fail
try
{
return getLong(__k);
}
// Not a number
catch (NumberFormatException e)
{
return __def;
}
}
/**
......
......@@ -10,6 +10,9 @@
package net.multiphasicapps.squirreljme.jit;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
/**
* This contains the values associated with keys in the JIT configuration.
*
......@@ -28,6 +31,9 @@ public final class JITConfigValue
/** The value of the option. */
protected final String value;
/** String representation. */
private volatile Reference<String> _string;
/**
* Initializes the value.
*
......@@ -67,7 +73,7 @@ public final class JITConfigValue
if (!(__o instanceof JITConfigValue))
return false;
return this.value.equals(((JITConfigValue)__o).value);
return toString().equals(((JITConfigValue)__o).toString());
}
/**
......@@ -77,7 +83,51 @@ public final class JITConfigValue
@Override
public int hashCode()
{
return this.value.hashCode();
return toString().hashCode();
}
/**
* Returns {@code true} if this is an integer value.
*
* @return If the given value is an integer.
* @since 2017/08/19
*/
public boolean isInteger()
{
// Attempt to decode a value
try
{
Integer.decode(this.value);
return true;
}
// Did not parse a value
catch (NumberFormatException e)
{
return false;
}
}
/**
* Returns {@code true} if this is a long value.
*
* @return If the given value is a long.
* @since 2017/08/19
*/
public boolean isLong()
{
// Attempt to decode a value
try
{
Long.decode(this.value);
return true;
}
// Did not parse a value
catch (NumberFormatException e)
{
return false;
}
}
/**
......@@ -91,6 +141,70 @@ public final class JITConfigValue
return TRUE.equals(matchesTrue(this));
}
/**
* Parses this value as an int value.
*
* @return The parsed value.
* @throws NumberFormatException If the value could not be parsed.
* @since 2017/08/19
*/
public int toInteger()
throws NumberFormatException
{
return Integer.decode(this.value);
}
/**
* Returns the value as an int or returns the default value.
*
* @param __d The default value if this is not parsable as an int.
* @return The parsed value or {@code __d}.
* @since 2017/08/19
*/
public int toInteger(int __d)
{
try
{
return toInteger();
}
catch (NumberFormatException e)
{
return __d;
}
}
/**
* Parses this value as a long value.
*
* @return The parsed value.
* @throws NumberFormatException If the value could not be parsed.
* @since 2017/08/19
*/
public long toLong()
throws NumberFormatException
{
return Long.decode(this.value);
}
/**
* Returns the value as a long or returns the default value.
*
* @param __d The default value if this is not parsable as a long.
* @return The parsed value or {@code __d}.
* @since 2017/08/19
*/
public long toLong(long __d)
{
try
{
return toLong();
}
catch (NumberFormatException e)
{
return __d;
}
}
/**
* {@inheritDoc}
* @since 2017/05/30
......@@ -98,7 +212,29 @@ public final class JITConfigValue
@Override
public String toString()
{
return this.value;
Reference<String> ref = this._string;
String rv;
// Check
if (ref == null || null == (rv = ref.get()))
{
// Decode as a long value
try
{
rv = Long.toString(toLong());
}
// Treat as string value
catch (NumberFormatException e)
{
rv = this.value;
}
// Cache
this._string = new WeakReference<>(rv);
}
return rv;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment