Link-Time Static Analysis for Efficient Separate
Total Page:16
File Type:pdf, Size:1020Kb
LinkTime Static Analysis for Efficient Separate Compilation of ObjectOriented Languages Jean Privat Roland Ducournau [email protected] [email protected] LIRMM CNRS — Universite´ Montpellier II 161 rue Ada 34392 Montpellier cedex 5, France ABSTRACT by many programs. In order to have these advantages, For- tran Compilers used in industry are mainly based on a sepa- has introduced separate compilation: source ¯les are rate compilation framework. However, the knowledge of compiled independently of future uses, then linked to pro- the whole program improves e±ciency of object-oriented duce an executable program. language compilers, therefore more e±cient implementation The problem is that the knowledge of the whole program techniques are based on a global compilation framework. allows more e±cient implementation techniques. Therefore In this paper, we propose a compromise by including three previous works use these techniques in a global compilation global compilation techniques (type analysis, coloring and framework, thus incompatible with modular production of binary tree dispatching) in a separate compilation frame- softwares. Global techniques allow e±cient implementation work. Files are independently compiled into standard binary of the three main object-oriented mechanisms: late binding, ¯les with unresolved symbols. The program is build by link- read and write access to attributes, dynamic type checking. ing object ¯les: ¯les are gathered and analyzed, some link In this paper, we present a genuine separate compila- code is generated then symbols are resolved. tion framework that includes some global optimization tech- niques. The framework described here can be used for any statically typed class-based languages [17] like C++ [21], 1. INTRODUCTION Java [12] and Eiffel [18], but, it is not applicable to According to software engineering, programmers must Self [23] or Smalltalk [11]. write modular software. Object-oriented programming has The remainder of this paper is organized as follows. Sec- become a major trend because it ful¯lls this need: heavy use tion 2 presents the global optimization techniques we con- 1 of inheritance and late binding is likely to make code more sider. Section 3 introduces our separate compilation frame- extensible and reusable. work. Results and benchmarks they where obtained from According to software engineering, programmers also need are presented in section 4. We conclude in section 5. to produce software in a modular way. Typically, we can identify three advantages: (i) a software component (e.g. a library) should be distributable in a compiled form; (ii) a 2. GLOBAL TECHNIQUES small modi¯cation in the source code should not require a re- The knowledge of the whole program source code permits compilation of the whole program; (iii) a single compilation a precise analysis of the behavior of each component and an of a software component should be enough even if it is shared analysis of the class hierarchy structure. Each of those al- 1 lows important optimizations and may be used in any global Instead of applying a function to arguments, a message is compiler. sent to an object, the receiver. The program behavior, i.e. the code which is executed, depends on the value of receiver. 2.1 Type Analysis From an implementation point of view, it follows that the static function call of procedural language must be replaced Late binding is considered as a bottleneck in object- by something more dynamic since the control flow jumps to oriented programs. Statistics show that most method calls an address extracted from the receiver value. are actually monomorphic calls. Type analysis2 can detect these monomorphic calls and reduces the polymorphism of others. A type analysis approximates three sets: the set of the Permission to make digital or hard copies of all or part of this work for classes that have instances (live classes), the set of the con- personal or classroom use is granted without fee provided that copies are crete type of each expression (the concrete type is the set of not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to dynamic types) and the set of called methods for each call republish, to post on servers or to redistribute to lists, requires prior specific site. These three sets are mutually dependent: called meth- permission and/or a fee. 2 PASTE '05 Lisbon, Portugal Type analysis should not be confused with the Ml type Copyright 2005 ACM 1595932399/05/0009 ...$5.00. inference. 20 Class A subtyping table Pointer to methods introduced in A For implantation example, assuming a polymorphic call site x.f and assuming the color of the f method is ¢ , the VFT f A C generated code in assembler language looks like: B C A C mov [x + #tableOffset] ! table mov [table + ¢f ] ! method D A B C D call method A B C D Static The same technique can be used for attribute access and Instance Dynamic c A C type check. See section 3.1.3 for code sample. Finding a coloring that respects the three invariants re- d A B C D quires the knowledge of the whole class hierarchy. Minimiz- Attributes introduced in A ing the size of the table (i.e. minimizing the number of gaps, in gray in Figure 1) is an NP-hard problem similar to the Figure 1: Implementation of Classes and Instances minimum graph coloring problem. Happily, class hierarchies with Coloring seem to be simple cases of this problem and many e±cient heuristics are proposed in [20, 22]. ods depend on the concrete type of the receiver, concrete 2.3 Binary Tree Dispatch types depend on the instantiated classes, and instantiated SmartEiffel [25] introduces an implementation tech- classes depend on the called methods. This interdependence nique for object-oriented languages called binary tree dis- explains the di±culty of the problem [10], and the variety of patch (BTD). It is a systematization of some techniques solutions [13]. There are many kinds of type analysis, even known as polymorphic inline cache and type prediction [14]. simple ones give good result and can detect many monomor- BTD has good results because VFT does not schedule well phic calls [2]. on modern processors since the unpredictable and indirect 2.2 Coloring branches break their pipelines [7]. BTD requires a global type analysis in order to reduce the Multiple inheritance is problematic with the standard vir- number of expected types of each call site. Once the analy- tual function table (VFT) implementation. C++ resolves sis is performed, the knowledge of concrete types permits to it by the use of subobjects and an important overhead [16, implement polymorphism with an e±cient select tree. The 8]: (i) in the worst case, the number of method tables is select tree enumerates types of the concrete type and pro- quadratic (instead of linear) and the size is cubic (instead vides a static resolution for each possible case. This tech- of quadratic); (ii) with subobjects, in the dynamic memory nique does not need a memory implementation for classes allocated for an instance, the number of attributes can be and the dynamic memory area for an instance contains the less than the number of pointers to VFT; (iii) pointers to class identi¯er (an integer) and the attributes. an instance are dependent of the static type of the pointers. For example, let's consider a polymorphic call site x.f Coloring avoids the overhead of multiple inheritance. It where the concrete type of x is fA; B; C; Dg. Assuming the can be applied to attributes, to methods and to classes for class identi¯ers of these classes are, respectively, 19, 12, 27 subtyping check [6, 20, 5, 24, 8]. The implementation of and 15, assuming their f method implementations are, re- classes and instances includes two parts (Figure 1): in static spectively, fA, fB , fC and fD and assuming the identi¯er of memory, an area for each class with the address of each the dynamic class of x is idx, the generated code looks like: method (in the VFT) and the superclass information (in the subtyping table); in dynamic memory, an area for each if idx · 15 then 12 instance with attributes and a pointer to its class. Figure 1 if idx · then fB else fD di®erentiates VFT and subtyping table but in the rest of the end paper, both are merged. else The technique consists in assigning a unique identi¯er to if idx · 19 then fA each class and a color (an index) to each class, method and else fC attribute. Colors are assigned in such a way that: end end Invariant 1. A pointer to an instance does not depend Obviously, the same technique can be used for attribute on the static type of the pointer. Thus, polymorphic as- access and type check. signments and parameter passing are costless (as opposed C++ to pointer adjustments). 3. SEPARATE COMPILATION Invariant 2. The color of an attribute (respectively a Separate compilation frameworks are divided into two method) is invariant by inheritance and rede¯nition. Thus, phases: a local one (compiling) and a global one (linking). the index of the attributes (respectively methods) does not The local phase compiles a single software component depend on the static type of the receiver. (without loss of generality, we consider the compilation units to be classes) independently of the other components. We Invariant 3. Two classes with the same color do not denote binary components the results of this phase3. Binary have a common subclass. The subtyping table of a class 3 Traditionally, the results of separate compilation are called contains the identi¯er of each super-class at the index of this object ¯les.