
Compiling Scheme programs to .NET Common Intermediate Language Yannis Bres, Bernard Paul Serpette, Manuel Serrano Inria Sophia-Antipolis 2004 route des Lucioles - BP 93, F-06902 Sophia Antipolis, Cedex, France fYannis.Bres,Bernard.Serpette,[email protected] ABSTRACT CLR with language agnosticism in mind. Indeed, This paper presents the compilation of the Scheme the CLR supports more language constructs than the programming language to .NET platform. .NET pro- JVM: the CLR supports enumerated types, structures vides a virtual machine, the Common Language Run- and value types, contiguous multidimensional arrays, time (CLR), that executes bytecode: the Common In- etc. The CLR supports tail calls, i.e. calls that do termediate Language (CIL). Since CIL was designed not consume stack space. The CLR supports closures with language agnosticism in mind, it provides a rich through delegates. Finally, pointers to functions can set of language constructs and functionalities. There- be used although this leads to unverifiable bytecode. fore, the CLR is the first execution environment that The .NET framework has 4 publicly available imple- offers type safety, managed memory, tail recursion mentations: support and several flavors of pointers to functions. As such, the CLR presents an interesting ground for • From Microsoft, one commercial version and one functional language implementations. whose sources are published under a shared source License (Rotor [16]). Rotor was released for re- We discuss how to map Scheme constructs to CIL. We search and educational purposes. As such, Rotor present performance analyses on a large set of real-life JIT and GC are simplified and stripped-down ver- and standard Scheme benchmarks. In particular, we sions of the commercial CLR, which lead to poorer compare the performances of these programs when performances. compiled to C, JVM and .NET. We show that .NET • Ximian/Novell's Mono Open Source project offers still lags behind C and JVM. a quite complete runtime and good performances but has only a few compilation tools. • From DotGNU, the Portable.Net GPL project pro- 1. INTRODUCTION vides a quite complete runtime and many compi- Introduced by Microsoft in 2001, the .NET Frame- lation tools. Unfortunately, it does not provide work has many similarities with the Sun Microsystems a full-fledged JIT [20]. Hence, its speed cannot Java Platform [9]. The execution engine, the Com- compete with other implementations so we will mon Language Runtime (CLR), is a stack-based Vir- not show performance figures for this platform. tual Machine (VM) which executes a portable byte- code: the Common Intermediate Language (CIL) [8]. 1.1 Bigloo 5 The CLR enforces type safety through its bytecode Bigloo is an optimizing compiler for the Scheme (R rs verifier (BCV), it supports polymorphism, the mem- [7]) programming language. It targets C code, JVM ory is garbage collected and the bytecode is Just-In- bytecode and now .NET CIL. In the rest of this pre- Time [1,17] compiled to native code. sentation, we will use BiglooC, BiglooJvm, and Bigloo- .NET to refer to the specific Bigloo backends. Bench- Beyond these similarities, Microsoft has designed the marks show that BiglooC generates C code whose per- Permission to make digital or hard copies of all or part of formance is close to human-written C code. When this work for personal or classroom use is granted without fee targeting the JVM, programs run, in general, less provided that copies are not made or distributed for profit or than 2 times slower than C code on the best JDK commercial advantage and that copies bear this notice and the implementations [12]. full citation on the first page. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior Bigloo offers several extensions to Scheme [7] such as: specific permission and/or a fee. modules for separate compilation, object extensions .NET Technologies'2004 workshop proceedings, a` la Clos [3] + extensible classes [14], optional type ISBN 80-903100-4-4 annotations for compile-time type verification and op- c UNION Agency - Science Press, Plzen, Czech Republic timization. Bigloo is itself written in Bigloo and the compiler is sures are instances of bigloo.procedure, as we will bootstrapped on all of its three backends. The run- see in Section 2.3.3. time is made of 90% of Bigloo code and 10% of C, Java, or C# for each backend. 2.2 Separate Compilation A Bigloo program is made of several modules. Each 1.2 Motivations module is compiled into a CIL class that aggregates As for the JVM, the .NET Framework is appealing for the module definitions as static variables and func- language implementors. The runtime offers a large set tions. Modules can define several classes. Such classes of libraries, the execution engine provides a lot of ser- are compiled as regular CIL classes (see x2.3.4). Since vices and the produced binaries are expected to run on we do not have a built-in CIL assembler yet, we print a wide range of platforms. Moreover, we wanted to out each module class as a file and use the Portable.Net explore what the \more language-agnostic" promise assembler to produce an object file. Once all modules can really bring to functional language implementa- have been separately compiled, they are linked using tions as well as the possibilities for language interop- the Portable.NET linker. erability. 2.3 Compilation of functions 1.3 Outline of this paper Functions can be separated in several categories: Section 2 presents the main techniques used to com- pile Bigloo programs to CIL. Section 3 enumerates • Local tail-recursive functions that are not used as the new functionalities of the .NET Framework that first-class values are compiled as loops. could be used to improve the performances of pro- • Non tail-recursive functions that are not used as duced code. Section 4 compares the run times of sev- first-class values are compiled as static methods. eral benchmark and real life Bigloo programs on the • Functions used as first-class values are compiled three C, JVM and .NET backends. as real closures. A function is used as a first-class value when it is used in a non-functional position, 2. COMPILATION OUTLINE i.e., used as an argument of another function call or used as a return value of another function. This section presents the general compilation scheme • Generic functions are compiled as static methods of Bigloo programs to .NET CIL. Since CLR and JVM and use an ad hoc framework for resolving late are built upon similar concepts, the techniques de- binding. ployed for these two platforms are close. The compi- lation to JVM being thoroughly presented in a pre- vious paper [12], only a shallow presentation is given 2.3.1 Compiling tail-recursive functions here. In order to avoid the overhead of function calls, local functions that are not used as values and always called 2.1 Data Representation tail-recursively are compiled into CIL loops. Here is an example of two mutually recursive functions: Scheme polymorphism implies that, in the general (define (odd x) case, all data types (numerals, characters, strings, (define (even? n) pairs, etc.) have a uniform representation. This may (if (= n 0) #t (odd? (- n 1)))) lead to boxing values such as numerals and characters, (define (odd? n) i.e., allocating heap cells pointing to numbers and (if (= n 0) #f (even? (- n 1)))) (odd? x)) characters. Since boxing reduces performances (be- cause of additional indirections) and increase memory These functions are compiled as: usage, we aim at avoiding boxing as much as possi- .method static bool odd(int32) cil managed f ble. Thanks to the Storage Use Analysis [15] or user- .locals(int32) provided type annotations, numerals or characters are ldarg.0 // load arg odd?: stloc.0 // store in local var #0 usually passed as values and not boxed, i.e. not allo- ldloc.0 // load local var #0 cated in the heap any more. Note that in the C back- ldc.i4.0 // load constant 0 end, boxing of integers is always avoided using usual brne.s loop1 // if not equal go to loop1 tagging techniques [6]. In order to save memory and ldc.i4.0 // load constant 0 (false) avoid frequent allocations, integers in the range [-100 br.s end // go to end loop1: ldloc.0 // load local var #0 ... 2048] and all 256 characters (objects that embed a ldc.i4.1 // load constant 1 single byte) are preallocated. Integers are represented sub // subtract using the int32 type. Reals are represented using even?: stloc.0 // store in local var #0 float64. Strings are represented by arrays of bytes, ldloc.0 // load local var #0 as Scheme strings are mutable sequences of 1 byte ldc.i4.0 // load constant 0 brne.s loop2 // if not equal go to loop2 characters while the .NET built-in System.Strings ldc.i4.1 // load constant 1 (true) are non-mutable sequences of wide characters. Clo- br.s end // go to end loop2: ldloc.0 // load local var #0 closures, all the closures of a single module share the ldc.i4.1 // load constant 1 sub // subtract same entry-point function. This function uses the in- br.s odd? // go to odd? dex of the closure to call the body of the closure, using end: ret // return a switch. Closure bodies are implemented as static g methods of the class associated to the module and they receive as first argument the bigloo.procedure 2.3.2 Compiling regular functions instance. As a more general case, functions that cannot be com- piled to loops are compiled as CIL static methods.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-