382 int arity() {
383 return arity;
384 }
385
386 /** Return the method type corresponding to my basic type signature. */
387 MethodType methodType() {
388 return signatureType(basicTypeSignature());
389 }
390 /** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
391 final String basicTypeSignature() {
392 StringBuilder buf = new StringBuilder(arity() + 3);
393 for (int i = 0, a = arity(); i < a; i++)
394 buf.append(basicTypeChar(parameterType(i)));
395 return buf.append('_').append(basicTypeChar(returnType())).toString();
396 }
397 static int signatureArity(String sig) {
398 assert(isValidSignature(sig));
399 return sig.indexOf('_');
400 }
401 static byte signatureReturn(String sig) {
402 return basicType(sig.charAt(signatureArity(sig)+1));
403 }
404 static boolean isValidSignature(String sig) {
405 int arity = sig.indexOf('_');
406 if (arity < 0) return false; // must be of the form *_*
407 int siglen = sig.length();
408 if (siglen != arity + 2) return false; // *_X
409 for (int i = 0; i < siglen; i++) {
410 if (i == arity) continue; // skip '_'
411 char c = sig.charAt(i);
412 if (c == 'V')
413 return (i == siglen - 1 && arity == siglen - 2);
414 if (BASIC_TYPE_CHARS.indexOf(c) < 0) return false; // must be [LIJFD]
415 }
416 return true; // [LIJFD]*_[LIJFDV]
417 }
418 static MethodType signatureType(String sig) {
419 Class<?>[] ptypes = new Class<?>[signatureArity(sig)];
420 for (int i = 0; i < ptypes.length; i++)
421 ptypes[i] = basicTypeClass(basicType(sig.charAt(i)));
422 Class<?> rtype = basicTypeClass(signatureReturn(sig));
500 }
501 LambdaForm prep = getPreparedForm(basicTypeSignature());
502 this.vmentry = prep.vmentry;
503 // TO DO: Maybe add invokeGeneric, invokeWithArguments
504 }
505
506 /** Generate optimizable bytecode for this form. */
507 MemberName compileToBytecode() {
508 MethodType invokerType = methodType();
509 assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
510 if (vmentry != null && isCompiled) {
511 return vmentry; // already compiled somehow
512 }
513 try {
514 vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
515 if (TRACE_INTERPRETER)
516 traceInterpreter("compileToBytecode", this);
517 isCompiled = true;
518 return vmentry;
519 } catch (Error | Exception ex) {
520 throw newInternalError("compileToBytecode", ex);
521 }
522 }
523
524 private static final ConcurrentHashMap<String,LambdaForm> PREPARED_FORMS;
525 static {
526 int capacity = 512; // expect many distinct signatures over time
527 float loadFactor = 0.75f; // normal default
528 int writers = 1;
529 PREPARED_FORMS = new ConcurrentHashMap<>(capacity, loadFactor, writers);
530 }
531
532 private static Map<String,LambdaForm> computeInitialPreparedForms() {
533 // Find all predefined invokers and associate them with canonical empty lambda forms.
534 HashMap<String,LambdaForm> forms = new HashMap<>();
535 for (MemberName m : MemberName.getFactory().getMethods(LambdaForm.class, false, null, null, null)) {
536 if (!m.isStatic() || !m.isPackage()) continue;
537 MethodType mt = m.getMethodType();
538 if (mt.parameterCount() > 0 &&
539 mt.parameterType(0) == MethodHandle.class &&
540 m.getName().startsWith("interpret_")) {
|
382 int arity() {
383 return arity;
384 }
385
386 /** Return the method type corresponding to my basic type signature. */
387 MethodType methodType() {
388 return signatureType(basicTypeSignature());
389 }
390 /** Return ABC_Z, where the ABC are parameter type characters, and Z is the return type character. */
391 final String basicTypeSignature() {
392 StringBuilder buf = new StringBuilder(arity() + 3);
393 for (int i = 0, a = arity(); i < a; i++)
394 buf.append(basicTypeChar(parameterType(i)));
395 return buf.append('_').append(basicTypeChar(returnType())).toString();
396 }
397 static int signatureArity(String sig) {
398 assert(isValidSignature(sig));
399 return sig.indexOf('_');
400 }
401 static byte signatureReturn(String sig) {
402 return basicType(sig.charAt(signatureArity(sig) + 1));
403 }
404 static boolean isValidSignature(String sig) {
405 int arity = sig.indexOf('_');
406 if (arity < 0) return false; // must be of the form *_*
407 int siglen = sig.length();
408 if (siglen != arity + 2) return false; // *_X
409 for (int i = 0; i < siglen; i++) {
410 if (i == arity) continue; // skip '_'
411 char c = sig.charAt(i);
412 if (c == 'V')
413 return (i == siglen - 1 && arity == siglen - 2);
414 if (BASIC_TYPE_CHARS.indexOf(c) < 0) return false; // must be [LIJFD]
415 }
416 return true; // [LIJFD]*_[LIJFDV]
417 }
418 static MethodType signatureType(String sig) {
419 Class<?>[] ptypes = new Class<?>[signatureArity(sig)];
420 for (int i = 0; i < ptypes.length; i++)
421 ptypes[i] = basicTypeClass(basicType(sig.charAt(i)));
422 Class<?> rtype = basicTypeClass(signatureReturn(sig));
500 }
501 LambdaForm prep = getPreparedForm(basicTypeSignature());
502 this.vmentry = prep.vmentry;
503 // TO DO: Maybe add invokeGeneric, invokeWithArguments
504 }
505
506 /** Generate optimizable bytecode for this form. */
507 MemberName compileToBytecode() {
508 MethodType invokerType = methodType();
509 assert(vmentry == null || vmentry.getMethodType().basicType().equals(invokerType));
510 if (vmentry != null && isCompiled) {
511 return vmentry; // already compiled somehow
512 }
513 try {
514 vmentry = InvokerBytecodeGenerator.generateCustomizedCode(this, invokerType);
515 if (TRACE_INTERPRETER)
516 traceInterpreter("compileToBytecode", this);
517 isCompiled = true;
518 return vmentry;
519 } catch (Error | Exception ex) {
520 throw newInternalError("compileToBytecode: " + this, ex);
521 }
522 }
523
524 private static final ConcurrentHashMap<String,LambdaForm> PREPARED_FORMS;
525 static {
526 int capacity = 512; // expect many distinct signatures over time
527 float loadFactor = 0.75f; // normal default
528 int writers = 1;
529 PREPARED_FORMS = new ConcurrentHashMap<>(capacity, loadFactor, writers);
530 }
531
532 private static Map<String,LambdaForm> computeInitialPreparedForms() {
533 // Find all predefined invokers and associate them with canonical empty lambda forms.
534 HashMap<String,LambdaForm> forms = new HashMap<>();
535 for (MemberName m : MemberName.getFactory().getMethods(LambdaForm.class, false, null, null, null)) {
536 if (!m.isStatic() || !m.isPackage()) continue;
537 MethodType mt = m.getMethodType();
538 if (mt.parameterCount() > 0 &&
539 mt.parameterType(0) == MethodHandle.class &&
540 m.getName().startsWith("interpret_")) {
|