USOO8863098B2
(12) United States Patent (10) Patent No.: US 8,863,098 B2 Makino et al. (45) Date of Patent: *Oct. 14, 2014
(54) PROCESSING REFEO GENERCS IN 6,091,897 A * 7/2000 Yates et al...... 717/138 OBJECT-BASED PROGRAMMING 6,282,702 B1* 8/2001 Ungar ...... T17,148 6,772,413 B2 * 8/2004 Kuznetsov . T17.136 ENVIRONMENTS 7,039,904 B2* 5/2006 Sokolov ...... 717, 147 7,100,153 B1* 8/2006 Ringseth et al...... T17,140 (75) Inventors: Yuki Makino, Tokyo (JP); Mikio 7,275,079 B2 9/2007 Brodsky et al. Takeuchi, Kanagawa (JP) 7,734,895 B1* 6/2010 Agarwal et al...... T12/13 7,774,746 B2 * 8/2010 Mansfield et al...... 717/106 (73) Assignee: International Business Machines (Continued) Corporation, Armonk, NY (US) FOREIGN PATENT DOCUMENTS (*) Notice: Subject to any disclaimer, the term of this patent is extended or adjusted under 35 JP 2005284729 10/2005 U.S.C. 154(b) by 119 days. JP 2010.123060 6, 2010 This patent is Subject to a terminal dis OTHER PUBLICATIONS claimer. Millstein et al. “Expressive and Modular Predicate Dispatch for (21) Appl. No.: 13/569,599 Java’, ACM Transactions on Programming Languages and Systems, vol. 31, No. 2, Article 7, pp. 1-54, 2009.* (22) Filed: Aug. 8, 2012 (Continued) (65) Prior Publication Data Primary Examiner — Anil Khatri US 2013/0332912 A1 Dec. 12, 2013 (74) Attorney, Agent, or Firm — Cantor Colburn LLP: Related U.S. Application Data Vazken Alexanian (63) Continuation of application No. 13/494.698, filed on (57) ABSTRACT Jun. 12, 2012. A computer-implemented method of processing a reified (51) Int. C. generic in an interface method written in a first programming G06F 9/45 (2006.01) language includes a processor accessing a definition and an (52) U.S. C. invocation of the interface method from a memory device; the USPC ...... 717/137; 717/136; 717/140 processor generating a definition of a general dispatch (58) Field of Classification Search method in a second programming language; the processor USPC ...... 717/136-143, 148 generating definitions of special dispatch methods in the sec IPC ...... G06F 8/22,8/24, 8/31, 8/315, 8/316 ond programming language, each of the special dispatch See application file for complete search history. methods corresponding to each primitive return type of the interface method the processor generating an invocation of (56) References Cited either the general dispatch method or one of the special dis patch methods in the second programming language based on U.S. PATENT DOCUMENTS a return type of the interface method. 5,390,314 A * 2, 1995 Swanson ...... 717/138 5,751,982 A * 5/1998 Morley ...... T12/209 10 Claims, 3 Drawing Sheets
definition of interface method
generate definition of general dispatch method 210 list all implemented methods that have primitive return types and group them by return type N220 SPECIALDSPATCH for each return type
generate definition of special dispatch method 230 SPECIAL DISPATCH US 8,863,098 B2 Page 2
(56) References Cited Muschevici et al. “Multiple Dispatch in Practice”. ACM, pp. 563 582, 2008.* U.S. PATENT DOCUMENTS Gil et al. “Efficient Dynamic Dispatching with Type Slicing”. ACM Transactions on Programming Languages and Systems, vol. 30.No. 7,774,751 B2 8/2010 Zhuk ...... 717/120 7,934,203 B2 4/2011 Lovett et al. 1, Article 5, pp. 1-53, 2007.* 7,934.204 B2 4/2011 Brown et al...... 717/137 Preuber et al., “Enabling Constant-Time Interface Method Dispatch 7,962,888 B2* 6/2011 Rojer ...... 717/1OO in Embedded Java Processors”, ACM, 2007, pp. 196-205. 8,127,280 B2 2/2012 Thomas et al. 717.136 Takeuchi et al., “Fast Method Dispatch and Effective Use of Primi 8,136,102 B2 * 3/2012 Papakipos etal 717,140 tives for Reified Generics in Managed X10”, ACM, 2012, pp. 1-7. 8,166,468 B2 * 4/2012 Foley et al. ... 717, 159 Takeuchi et al., “Compiling X10 to Java’, ACM, 2011, pp. 1-10. 8,392,897 B2 3, 2013 Franz et al. . 717,146 8,522,223 B2 * 8/2013 Wang et al. ... 717, 149 Takeuchietal., “Java Interoperability in Managed X10'. ACM, 2013, 8,584, 106 B2 * 1 1/2013 Papakipos et al...... T17,140 pp. 39-46. OTHER PUBLICATIONS * cited by examiner U.S. Patent Oct. 14, 2014 Sheet 1 of 3 US 8,863,098 B2
C U -- O ...A ? t O CD O V
s U.S. Patent Oct. 14, 2014 Sheet 2 of 3 US 8,863,098 B2
OTZ OZZ 08Z U.S. Patent Oct. 14, 2014 Sheet 3 of 3 US 8,863,098 B2
US 8,863,098 B2 1. 2 PROCESSING REIFIED GENERCS IN According to a further embodiment, a non-transitory com OBJECT-BASED PROGRAMMING puter program product includes a storage medium including ENVIRONMENTS computer-readable program code which, when executed by a processor, causes the processor to implement a method of CROSS REFERENCE TO RELATED processing a reified generic in an interface method written in APPLICATION a first programming language. The method includes access ing a definition and an invocation of the interface method This application is a continuation of U.S. application Ser. from a memory device; generating a definition of a general No. 13/494,698, filed Jun. 12, 2012, the disclosure of which is dispatch method in a second programming language; gener incorporated by reference herein in its entirety. 10 ating definitions of special dispatch methods in the second programming language, each of the special dispatch methods BACKGROUND corresponding to each primitive return type of the interface method; generating an invocation of either the general dis The present invention relates to object-based programming patch method or one of the special dispatch methods in the environments, and more specifically, to techniques to address 15 second programming language based on a return type of the dispatch speed when dealing with reified generics. interface method. A number of new programming languages have been Additional features and advantages are realized through developed and trademarked by commercial companies. Some the techniques of the present invention. Other embodiments of these trademarked languages include Fortress, Chapel, and and aspects of the invention are described in detail herein and X10. Others that are mentioned in this application are also are considered a part of the claimed invention. For a better trademarked. When these languages are designed to use the understanding of the invention with the advantages and the resources of existing languages, productivity is improved and features, refer to the description and to the drawings. past investment is more effectively utilized. One of these existing languages is Java, for example, and developers of BRIEF DESCRIPTION OF THE SEVERAL many of the new programming languages strive to ensure 25 VIEWS OF THE DRAWINGS compatibility with Java. One of the important things that allow compatibility of new The subject matter which is regarded as the invention is programming languages with existing languages, such as particularly pointed out and distinctly claimed in the claims at Java, is the use of generics of existing languages. Two types of the conclusion of the specification. The forgoing and other generics are erased generics, in which the value of the type 30 features, and advantages of the invention are apparent from parameter is not available at runtime, and reified generics, in the following detailed description taken in conjunction with which the value of the type parameter is available at runtime. the accompanying drawings in which: Two implementation techniques for reified generics are the FIG. 1 is a block diagram of a system that implements type specialization technique, which generates a non-generic special dispatch methods according to an embodiment; type for each different instantiation of a generic type, and the 35 FIG. 2 depicts the processes involved in generating defini type lifting technique, which represents the value of the type tions of general and special dispatch methods according to parameter as a type descriptor. embodiments; and When using the type lifting technique, a method (dispatch FIG. 3 depicts the processes involved in generating invo method) is invoked with particular type descriptor objects, cations of dispatch methods according to embodiments. and the dispatch method then calls the actual method that 40 corresponds to the type descriptor objects in a technique DETAILED DESCRIPTION called self dispatching. A dispatch method corresponds to multiple original methods with different return types. Thus, Although embodiments of the invention are detailed below the return type of a dispatch method is a reference type. with reference to the Managed X10 programming language, Consequently, primitives are converted to objects (boxed) in 45 alternate embodiments contemplate other programming lan the callee and are restored to primitives (unboxed) in the guages and, thus, the exemplary detailed embodiments caller. This boxing/unboxing creates an overhead cost with should not be regarded as limiting principles and aspects of respect to execution time and memory usage. As a result, the invention to the Managed X10 environment. efficient self dispatching would be appreciated in program X10 is a programming language that Supports reified ming languages. 50 generics. Managed X10 is an implementation of X10 that compiles X10 programs to Java for running the programs on SUMMARY a cluster of Java virtual machines (VMs). Thus, Managed X10 is a Java virtual machine (JVM) language with a goal of According to another embodiment, a computer-imple ensuring interoperability with existing Java programs. For mented method of processing a reified generic in an interface 55 example, it is convenient for Java programmers to call X10 method written in a first programming language includes a code if an X10 generic type is seen as a Java generic type. To processor accessing a definition and an invocation of the ensure the compatibility between compiler-generated code interface method from a memory device; the processor gen and existing Java code, Managed X10 leverages the same type erating a definition of a general dispatch method in a second erasure technique as in Java. However, because erased gener programming language; the processor generating definitions 60 ics do not Support Some features that are Supported by reified of special dispatch methods in the second programming lan generics, X10 generics cannot be compiled directly to Java guage, each of the special dispatch methods corresponding to generics. Managed X10 translates each X10 source code file each primitive return type of the interface method; the pro into equivalent Source code in Generic Java. cessor generating an invocation of either the general dispatch However, this presents several issues. First, X10 supports method or one of the special dispatch methods in the second 65 unsigned numeric types while Java does not. Also, X10 Sup programming language based on a return type of the interface ports reified generic types while Java only provides erased method generics. In addition, X10 has the top type “Any” that is a US 8,863,098 B2 3 4 Supertype of value types (including primitive numeric types) the dispatch method needs to be the upper bound of all return and of reference types (objects), but in Java, primitive types types of the actual methods, which is usually Any. Because and object types do not have a common Supertype. The use of Any is compiled to java.lang. Object, the primitives need to be type lifting in Managed X10 fills the gap between reified boxed. However, because a special dispatch method is gen generics and erased generics. Constructors in the generated 5 erated for each primitive or void return type, the caller calls code take a type descriptor object for each type parameter and the appropriate special dispatch function when the return type store it in an instance field. Runtime type information of a is known as primitive or void at compile time. When the return generic type is represented as a combination of structural type is not known as primitive or void at compile time, the information stored in a static field, Such as the Super class or caller calls the general dispatch method. implemented interfaces, and the type descriptor object. 10 In one embodiment, each special dispatch method has a To implement overloading of a generic method, runtime special suffix. For example, a “S” followed by a symbol type information of method parameters is needed. With type corresponding with the type, as shown at Table 1 below: specialization, a generic type is compiled to a separate non generic type specialized for a particular value of type param TABLE 1 eter and, therefore, is naturally overloaded. However, with 15 type lifting, a type descriptor object must be passed for each X10 Java Java (when boxed) Symbol method parameter of generic type. When the method (called X10.lang. Byte byte x10.core. Byte B a dispatch method) is invoked with particular type descriptor X10.lang.UByte byte x10.core.UByte b objects, it then calls the actual method which corresponds to X10.lang...Short short x10.core.Short S the type descriptor objects in a technique called self dispatch X10.lang.UShort short x10.core...UShort S X10.lang.Int int X10.core.Int I ing. However, self dispatching is slower than normal Java X10.lang.UInt int X10.core.UInt i dispatching because it requires a comparison of type descrip X10.lang.Long long X10.core..Long J tor objects followed by an extra method invocation. X10.lang.ULong long X10.core.ULong j X10.lang. Float float x10.core.Float F Method mangling is one way to address the issue of self X10.lang. Double double x10.core...Double D dispatching being slower than Java dispatching, but method 25 X10.lang. Char char x10.core.Char C mangling applies only to virtual and Static methods (name X10.lang. Boolean boolean x10.core..Boolean Z. mangling) and constructors (parametermangling). Java's dis void void na V patch mechanism can be directly leveraged and dispatch speed can be made equivalent to non-generic speed by encod Thus, as one example, a special dispatch method for an inte ing runtime type information to method signature. In name 30 ger return type would have the suffix SI. As another example, mangling, type parameters or signedness of method param a special dispatch method for a Boolean return type would eters is encoded into the method name. Because X10 has have the suffix SZ according to Table 1. unsigned types but Java does not, signedness is encoded to Exemplary X10 code is shown at Table 2 below. The code use the same Java primitive for both signed and unsigned correlates to both general and special dispatch methods. The types. The result of the encoding is a string that can be used as 35 interface methods at lines 2 and 10 have return types of Any. a Java identifier. Because the name of a constructor is fixed in Thus, these methods do not correlate with special dispatch Java, name mangling cannot be applied for constructors. methods but, rather, with general dispatch methods that Instead, overloading of constructors is implemented by gen require boxing. On the other hand, interface methods at lines erating synthetic classes with the same name as the encoded 6-9 have return types that are either numbers, characters, strings and adding a dummy parameter of the synthetic class. 40 Boolean, or void. Thus, for these interface methods, special This is called parameter mangling. Although method man dispatch methods are generated that each have a return type of gling addresses self dispatching cost for virtual and static a primitive type and, therefore, do not require boxing. methods and constructors, it is not applicable to interface methods. For interface methods, another technique to address TABLE 2 boxing cost is proposed. This technique is the generation of 45 special dispatch methods and is detailed below. X10 Code In X10, numbers, characters, and Boolean types are first 1 interface IIT {} class types that can be passed to type parameters in X10 2 interface JIT) { deff(IIT):Any; } (numbers, characters, and Boolean are subtypes of 3 abstract class S implements JInt) { X 10.lang. Any). As a result, a method which returns Any can 50 4 abstract deff(IDInt):Any; be implemented or overridden with a method which returns, 5 } 6 interface KIT) { deff(IIT):Int; } for example, X 10.lang. Int. This is called covariant overriding 7 interface LIT deff(IIT):UInt: } or covariant return type. This is not the case in Java (Java 8 interface MIT deff(IIT):Int: } primitives are not subtypes of java.lang. Object). Thus, boxed 9 interface NIT deff(IIT):void; } object types can be used to represent primitive values so that 55 10 interface OT deff(IIT):Any; } 11 class C extends Simplements KInt).LFloat,MAny.NUInt, the type java.lang. Object can be used to represent the top OLong{ type. However, as noted above, boxing causes inefficiencies 12 def f(IInt) = 1; in both memory footprint and execution time. To address 13 def f(IDFloat) = 2u. redundant boxing of primitives and useless return of dummy 14 deff(ILAny)= 3; 15 deff(IUInt) {} null values, embodiments of the invention include a special 60 16 deff(ILong):Any = null: dispatch method and control function that determines when 17 } the special dispatch method should be called in lieu of the 18 val c = new C(); general dispatch method. 19 vals:S = c. 20 valj:JInt) = c; A special dispatch method is generated for each primitive 21 valk:KInt= c; or void return type. As previously noted, a single dispatch 65 22 vall:LFloat) = c; method corresponds to multiple actual methods. Thus, to 23 val n:NUInt) = c; handle multiple independent return types, the return type of US 8,863,098 B2 5 6 TABLE 2-continued TABLE 4-continued
X10 Code 65 return f OS1x10SlangSAnyS2SO((I) a1); 66 throw new X10.lang.Error(); 24 val vs = s.?ff null); 67 } 25 valvi = j.f(null); 68 intfSi(I a1,Type t1){return f OS1x1OSlangSFloat S2SO((I)a1); //L 26 valvk = k.f(null); 69 void fSV(Ia1, Type t1) {f OS1x10SlangSUIntS2((I) a1); } // N 27 val vl = 1.f(null); 70 Intf OS1x1OSlangSIntS2(I a1) { // bridge method 28 n.f null); 71 return Int.Sbox(f OS1x1OSlangSIntS2SO(a1)); 29 valvck = c.f(null as IInt); 72} 30 valvci = c.f(null as IDFloat); 73 intf OS1x1OSlangSIntS2SO(I a1) { return 1; } // actual methods 31 c.f(null as IUInt); 10 74 intf OS1x1OSlangSFloat S2SO(I a1) { return 2: } 75 intf OS1x1OSlangSAnyS2SO(I a1) { return 3: } 76 void f OS1x1OSlangSUIntS2(I a1) { } The translated Java code resulting from the exemplary X10 77 Object f OS1x1OSlangSLongS2(I a1) { return null; } 78 } code above is shown at Table 3 below. As noted above, the 79 C c = new C(); interface methods at lines 2 and 10, which have return types of 80 S s = (S) c; Any, result in general dispatch methods at lines 35 and 45. 81 J-Int> = (J
TABLE 3 FIG. 1 is a block diagram of a system 100 that processes reified generics according to one embodiment. In the embodi Translated Java Code ment of FIG. 1, the system 100 includes a host computer 102 32 import X 10.core.*: 30 that executes computer instructions. The host computer 102 33 import x10.rtt.*: may operate in any type of environment in which a Software 34 interface I