2014-10-18 02:41:38 +04:00
|
|
|
module J2ME {
|
|
|
|
import assert = Debug.assert;
|
2015-05-08 01:17:05 +03:00
|
|
|
import Bytecodes = Bytecode.Bytecodes;
|
2014-10-18 02:41:38 +04:00
|
|
|
|
|
|
|
var writer = new IndentingWriter();
|
|
|
|
|
|
|
|
|
2015-08-21 06:10:46 +03:00
|
|
|
export const enum Kind {
|
2014-10-18 02:41:38 +04:00
|
|
|
Boolean,
|
|
|
|
Byte,
|
|
|
|
Short,
|
|
|
|
Char,
|
|
|
|
Int,
|
|
|
|
Float,
|
|
|
|
Long,
|
|
|
|
Double,
|
|
|
|
Reference,
|
|
|
|
Void,
|
2014-11-08 00:40:55 +03:00
|
|
|
Illegal,
|
2015-08-07 03:10:32 +03:00
|
|
|
High,
|
2014-11-08 00:40:55 +03:00
|
|
|
Store
|
2014-10-18 02:41:38 +04:00
|
|
|
}
|
|
|
|
|
2015-08-21 06:10:46 +03:00
|
|
|
export function getKindName(kind: Kind): string {
|
|
|
|
return (<any>J2ME).Kind[kind];
|
|
|
|
}
|
|
|
|
|
2015-01-20 10:31:33 +03:00
|
|
|
export function isTwoSlot(kind: Kind) {
|
|
|
|
return kind === Kind.Long || kind === Kind.Double;
|
|
|
|
}
|
|
|
|
|
2014-11-27 04:27:54 +03:00
|
|
|
export var valueKinds = [
|
|
|
|
Kind.Boolean,
|
|
|
|
Kind.Char,
|
|
|
|
Kind.Float,
|
|
|
|
Kind.Double,
|
|
|
|
Kind.Byte,
|
|
|
|
Kind.Short,
|
|
|
|
Kind.Int,
|
|
|
|
Kind.Long
|
|
|
|
];
|
|
|
|
|
2015-05-08 01:17:05 +03:00
|
|
|
export function returnKind(op: Bytecodes): Kind {
|
|
|
|
switch (op) {
|
|
|
|
case Bytecodes.LRETURN:
|
|
|
|
return Kind.Long;
|
|
|
|
case Bytecodes.DRETURN:
|
|
|
|
return Kind.Double;
|
|
|
|
case Bytecodes.IRETURN:
|
|
|
|
return Kind.Int;
|
|
|
|
case Bytecodes.FRETURN:
|
|
|
|
return Kind.Float;
|
|
|
|
case Bytecodes.ARETURN:
|
|
|
|
return Kind.Reference;
|
|
|
|
case Bytecodes.RETURN:
|
|
|
|
return Kind.Void;
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 08:53:53 +04:00
|
|
|
export function stackKind(kind: Kind): Kind {
|
|
|
|
switch (kind) {
|
|
|
|
case Kind.Boolean: return Kind.Int;
|
|
|
|
case Kind.Byte: return Kind.Int;
|
|
|
|
case Kind.Short: return Kind.Int;
|
|
|
|
case Kind.Char: return Kind.Int;
|
|
|
|
case Kind.Int: return Kind.Int;
|
|
|
|
case Kind.Float: return Kind.Float;
|
|
|
|
case Kind.Long: return Kind.Long;
|
|
|
|
case Kind.Double: return Kind.Double;
|
|
|
|
case Kind.Reference: return Kind.Reference;
|
|
|
|
default: throw Debug.unexpected("Unknown stack kind: " + kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-16 21:08:48 +03:00
|
|
|
export function kindCharacterToKind(kindCharacter: string): Kind {
|
2015-05-19 23:54:22 +03:00
|
|
|
switch (kindCharacter[0]) {
|
2015-05-16 21:08:48 +03:00
|
|
|
case 'Z': return Kind.Boolean;
|
|
|
|
case 'B': return Kind.Byte;
|
|
|
|
case 'S': return Kind.Short;
|
|
|
|
case 'C': return Kind.Char;
|
|
|
|
case 'I': return Kind.Int;
|
|
|
|
case 'F': return Kind.Float;
|
|
|
|
case 'J': return Kind.Long;
|
|
|
|
case 'D': return Kind.Double;
|
|
|
|
case 'V': return Kind.Void;
|
2015-05-20 03:22:18 +03:00
|
|
|
case '[': // Fallthrough
|
2015-05-19 23:54:22 +03:00
|
|
|
case 'L': return Kind.Reference;
|
2015-05-16 21:08:48 +03:00
|
|
|
default: throw Debug.unexpected("Unknown kind character: " + kindCharacter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 08:53:53 +04:00
|
|
|
export function arrayTypeCodeToKind(typeCode): Kind {
|
|
|
|
switch (typeCode) {
|
|
|
|
case 4: return Kind.Boolean;
|
|
|
|
case 5: return Kind.Char;
|
|
|
|
case 6: return Kind.Float;
|
|
|
|
case 7: return Kind.Double;
|
|
|
|
case 8: return Kind.Byte;
|
|
|
|
case 9: return Kind.Short;
|
|
|
|
case 10: return Kind.Int;
|
|
|
|
case 11: return Kind.Long;
|
|
|
|
default: throw Debug.unexpected("Unknown array type code: " + typeCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-09 07:14:44 +03:00
|
|
|
export function kindSize(kind: Kind): number {
|
|
|
|
if (isTwoSlot(kind)) {
|
|
|
|
return 8;
|
|
|
|
}
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
2015-05-17 21:35:37 +03:00
|
|
|
export function getKindCheck(kind: Kind): (l: any, h: number) => boolean {
|
2015-01-27 22:16:36 +03:00
|
|
|
switch (kind) {
|
|
|
|
case Kind.Boolean:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => l === 0 || l === 1;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Byte:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => (l | 0) === l && l >= Constants.BYTE_MIN && l <= Constants.BYTE_MAX;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Short:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => (l | 0) === l && l >= Constants.SHORT_MIN && l <= Constants.SHORT_MAX;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Char:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => (l | 0) === l && l >= Constants.CHAR_MIN && l <= Constants.CHAR_MAX;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Int:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => (l | 0) === l;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Float:
|
2015-06-12 01:47:08 +03:00
|
|
|
return (l, h) => (l | 0) === l;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Long:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Double:
|
2015-06-12 01:47:08 +03:00
|
|
|
return (l, h) => ((l | 0) === l) && ((h | 0) === h);
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Reference:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => l === null || l instanceof Object;
|
2015-01-27 22:16:36 +03:00
|
|
|
case Kind.Void:
|
2015-05-17 21:35:37 +03:00
|
|
|
return (l, h) => typeof l === "undefined";
|
2015-01-27 22:16:36 +03:00
|
|
|
default:
|
|
|
|
throw Debug.unexpected("Unknown kind: " + kind);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 23:05:03 +03:00
|
|
|
export function getSignatureKind(signature: Uint8Array): Kind {
|
2014-12-26 06:08:53 +03:00
|
|
|
switch (signature[0]) {
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.Z:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Boolean;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.B:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Byte;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.S:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Short;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.C:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Char;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.I:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Int;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.F:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Float;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.J:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Long;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.D:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Double;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.OpenBracket:
|
|
|
|
case UTF8Chars.L:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Reference;
|
2015-03-11 23:05:03 +03:00
|
|
|
case UTF8Chars.V:
|
2014-12-26 06:08:53 +03:00
|
|
|
return Kind.Void;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 02:42:07 +03:00
|
|
|
/**
|
|
|
|
* MethodDescriptor:
|
|
|
|
* ( ParameterDescriptor* ) ReturnDescriptor
|
|
|
|
* ParameterDescriptor:
|
|
|
|
* FieldType
|
|
|
|
* ReturnDescriptor:
|
|
|
|
* FieldType
|
|
|
|
* VoidDescriptor
|
|
|
|
* VoidDescriptor:
|
|
|
|
* V
|
|
|
|
* FieldDescriptor:
|
|
|
|
* FieldType
|
|
|
|
* FieldType:
|
|
|
|
* BaseType
|
|
|
|
* ObjectType
|
|
|
|
* ArrayType
|
|
|
|
* BaseType:
|
|
|
|
* B
|
|
|
|
* C
|
|
|
|
* D
|
|
|
|
* F
|
|
|
|
* I
|
|
|
|
* J
|
|
|
|
* S
|
|
|
|
* Z
|
|
|
|
* ObjectType:
|
|
|
|
* L ClassName ;
|
|
|
|
* ArrayType:
|
|
|
|
* [ ComponentType
|
|
|
|
* ComponentType:
|
|
|
|
* FieldType
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Global state for signature parsing, kind of hackish but fast.
|
|
|
|
var globalNextIndex = 0;
|
|
|
|
var descriptorKinds = [];
|
|
|
|
|
|
|
|
/**
|
2015-03-14 01:17:28 +03:00
|
|
|
* Returns an array of kinds that appear in a method signature. The first element is always the
|
|
|
|
* return kind. The returned array is shared, so you if you need a copy of it, you'll need to
|
|
|
|
* clone it.
|
|
|
|
*
|
|
|
|
* The parsing algorithm needs some global state to keep track of the current position in the
|
|
|
|
* descriptor, namely |globalNextIndex| which always points to the next index in the descriptor
|
|
|
|
* after a token has been consumed.
|
2015-03-12 02:42:07 +03:00
|
|
|
*/
|
|
|
|
export function parseMethodDescriptorKinds(value: Uint8Array, startIndex: number): Kind [] {
|
|
|
|
globalNextIndex = 0;
|
|
|
|
if ((startIndex > value.length - 3) || value[startIndex] !== UTF8Chars.OpenParenthesis) {
|
|
|
|
assert(false, "Invalid method signature.");
|
|
|
|
}
|
|
|
|
descriptorKinds.length = 0;
|
|
|
|
descriptorKinds.push(Kind.Void); // placeholder until the return type is parsed
|
|
|
|
var i = startIndex + 1;
|
|
|
|
while (value[i] !== UTF8Chars.CloseParenthesis) {
|
|
|
|
var kind = parseTypeDescriptorKind(value, i);
|
|
|
|
descriptorKinds.push(kind);
|
|
|
|
i = globalNextIndex;
|
|
|
|
if (i >= value.length) {
|
|
|
|
assert(false, "Invalid method signature.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
var kind = parseTypeDescriptorKind(value, i);
|
|
|
|
if (globalNextIndex !== value.length) {
|
|
|
|
assert(false, "Invalid method signature.");
|
|
|
|
}
|
|
|
|
// Plug in the return type
|
|
|
|
descriptorKinds[0] = kind;
|
|
|
|
return descriptorKinds;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseTypeDescriptorKind(value: Uint8Array, startIndex: number): Kind {
|
|
|
|
globalNextIndex = startIndex + 1;
|
|
|
|
switch (value[startIndex]) {
|
|
|
|
case UTF8Chars.Z:
|
|
|
|
return Kind.Boolean;
|
|
|
|
case UTF8Chars.B:
|
|
|
|
return Kind.Byte;
|
|
|
|
case UTF8Chars.C:
|
|
|
|
return Kind.Char;
|
|
|
|
case UTF8Chars.D:
|
|
|
|
return Kind.Double;
|
|
|
|
case UTF8Chars.F:
|
|
|
|
return Kind.Float;
|
|
|
|
case UTF8Chars.I:
|
|
|
|
return Kind.Int;
|
|
|
|
case UTF8Chars.J:
|
|
|
|
return Kind.Long;
|
|
|
|
case UTF8Chars.S:
|
|
|
|
return Kind.Short;
|
|
|
|
case UTF8Chars.V:
|
|
|
|
return Kind.Void;
|
|
|
|
case UTF8Chars.L: {
|
|
|
|
// parse a slashified Java class name
|
|
|
|
var endIndex = parseClassNameKind(value, startIndex + 1, UTF8Chars.Slash);
|
|
|
|
if (endIndex > startIndex + 1 && endIndex < value.length && value[endIndex] === UTF8Chars.Semicolon) {
|
|
|
|
globalNextIndex = endIndex + 1;
|
|
|
|
return Kind.Reference;
|
|
|
|
}
|
2015-03-14 01:17:28 +03:00
|
|
|
Debug.unexpected("Invalid signature.");
|
2015-03-12 02:42:07 +03:00
|
|
|
}
|
|
|
|
case UTF8Chars.OpenBracket: {
|
|
|
|
// compute the number of dimensions
|
|
|
|
var index = startIndex;
|
|
|
|
while (index < value.length && value[index] === UTF8Chars.OpenBracket) {
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
var dimensions = index - startIndex;
|
|
|
|
if (dimensions > 255) {
|
2015-03-14 01:17:28 +03:00
|
|
|
Debug.unexpected("Array with more than 255 dimensions.");
|
2015-03-12 02:42:07 +03:00
|
|
|
}
|
|
|
|
var component = parseTypeDescriptorKind(value, index);
|
|
|
|
return Kind.Reference;
|
|
|
|
}
|
|
|
|
default:
|
2015-03-14 01:17:28 +03:00
|
|
|
Debug.unexpected("Unexpected type descriptor prefix: " + value[startIndex]);
|
2015-03-12 02:42:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseClassNameKind(value: Uint8Array, index: number, separator: number): number {
|
|
|
|
var position = index;
|
|
|
|
var length = value.length;
|
|
|
|
while (position < length) {
|
|
|
|
var nextch = value[position];
|
|
|
|
if (nextch === UTF8Chars.Dot || nextch === UTF8Chars.Slash) {
|
|
|
|
if (separator !== nextch) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
} else if (nextch === UTF8Chars.Semicolon || nextch === UTF8Chars.OpenBracket) {
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function signatureHasTwoSlotArguments(signatureKinds: Kind []): boolean {
|
|
|
|
for (var i = 1; i < signatureKinds.length; i++) {
|
|
|
|
if (isTwoSlot(signatureKinds[i])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function signatureArgumentSlotCount(signatureKinds: Kind []): number {
|
|
|
|
var count = 0;
|
|
|
|
for (var i = 1; i < signatureKinds.length; i++) {
|
|
|
|
count += isTwoSlot(signatureKinds[i]) ? 2 : 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2015-01-14 03:47:42 +03:00
|
|
|
}
|