Integrating Bigloo into the.NET Platform Séminaire du club INTech Grenoble 15/05/2003

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

The Art of Avoiding Work
Chapt.2 Machine Architecture Impact of languages –Support – faster, more secure Primitive Operations –e.g. nested subroutine calls »Subroutines implemented.
Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
pa 1 Porting BETA to ROTOR ROTOR Projects Presentation Day, June by Peter Andersen.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Java for High Performance Computing Jordi Garcia Almiñana 14 de Octubre de 1998 de la era post-internet.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Building An Interpreter After having done all of the analysis, it’s possible to run the program directly rather than compile it … and it may be worth it.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Efficient Instruction Set Randomization Using Software Dynamic Translation Michael Crane Wei Hu.
Session-02. Objective In this session you will learn : What is Class Loader ? What is Byte Code Verifier? JIT & JAVA API Features of Java Java Environment.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Source Code Basics. Code For a computer to execute instructions, it needs to be in binary Each instruction is given a number Known as “operation code”
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Java Security. Topics Intro to the Java Sandbox Language Level Security Run Time Security Evolution of Security Sandbox Models The Security Manager.
5.3 Machine-Independent Compiler Features
1 The Java Virtual Machine Yearly Programming Project.
Instruction Set Architecture
Adapted from Prof. Necula UCB CS 1641 Overview of COOL ICOM 4029 Lecture 2 ICOM 4029 Fall 2008.
Lecture 10 : Introduction to Java Virtual Machine
CSC 310 – Imperative Programming Languages, Spring, 2009 Virtual Machines and Threaded Intermediate Code (instead of PR Chapter 5 on Target Machine Architecture)
Introduction to the Java Virtual Machine 井民全. JVM (Java Virtual Machine) the environment in which the java programs execute The specification define an.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
1 Introduction to JVM Based on material produced by Bill Venners.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Computer Programming 2 Why do we study Java….. Java is Simple It has none of the following: operator overloading, header files, pre- processor, pointer.
Virtual Machines, Interpretation Techniques, and Just-In-Time Compilers Kostis Sagonas
COMP3190: Principle of Programming Languages
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Lecture 5 Page 1 CS 111 Online Processes CS 111 On-Line MS Program Operating Systems Peter Reiher.
1. An Introduction A Programming Language A Technology Java Development Kit Java API One Language: Three Editions Standard Edition Enterprise Edition.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
Object Oriented Software Development 4. C# data types, objects and references.
Introduction to OOP CPS235: Introduction.
Procedures and Functions Procedures and Functions – subprograms – are named fragments of program they can be called from numerous places  within a main.
We need a new, common Virtual Execution Environment Herman Venter Research in Software Engineering Group Microsoft Research, Redmond.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
RealTimeSystems Lab Jong-Koo, Lim
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
Computer System Structures
Functional Programming
Object Oriented Programming in
Visit for more Learning Resources
Java Yingcai Xiao.
Before You Begin Nahla Abuel-ola /WIT.
Introduction to Web Assembly
A Closer Look at Instruction Set Architectures
Compiler Construction (CS-636)
Java Virtual Machine Complete subject details are available at:
2.1. Compilers and Interpreters
.NET and .NET Core 2. .NET Runtimes Pan Wuming 2017.
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
Computer Organization and Design Assembly & Compilation
(Computer fundamental Lab)
Course Overview PART I: overview material PART II: inside a compiler
ICOM 4029 Fall 2003 Lecture 2 (Adapted from Prof. Necula UCB CS 164)
M S COLLEGE ART’S, COMM., SCI. & BMS
CSc 453 Interpreters & Interpretation
IS 135 Business Programming
Presentation transcript:

Integrating Bigloo into the.NET Platform Séminaire du club INTech Grenoble 15/05/2003

Outline Introduction Scheme, Functional Programming and Bigloo The.NET Framework Motivations for a Bigloo.NET back-end How do we implement such a different language than C#/J#/… in.NET? Separate compilation A different notion of polymorphism Mapping of types and constructs Closures Tail recursion Continuations Inlining Conclusions

Scheme A functional language (strict evaluation, side-effects), derived from L ISP First class data types: Numerals, strings, symbols, pairs, vectors (arrays), lambdas High-order language functions are first-class objects Dynamic type-checking Very powerful macro system + eval at run-time Prefixed notation n-ary functions made coherent (and cond1 cond2 cond3 cond4 …) A standard: R5RS (Revised 5 Report on Scheme)

Functional Programming int fac( int n ) { int result= 1; while (n > 0) { result*= n; --n; } return result; } (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))))) Imperative programming: Functional programming:

Bigloo An extension of Scheme R5RS Separate compilation (modules) Object extensions à la C LOS /Meroon + extensible classes Optional type annotations for compile-time strong typing Fair-Threads … Functional programming often thought to have bad performances Bigloo generates C code whose performance is close to human-written C code Bigloo JVM programs are about 2 times slower than Bigloo C programs An optimizing Scheme compiler to C code, JVM bytecode and now.NET bytecode Support for continuations on the C back-end, only No arbitrary-precision integer arithmetic

The.NET Framework Similar to the Java platform: A stack-based register-less Virtual Machine (the Common Language Runtime) Memory is garbage collected Bytecode is Just-In-Time compiled to native code (can be precompiled in.NET) Microsoft (Commercial + Rotor): de facto reference implementations DotGNU (Portable.NET): interpreter only, missing parts in runtime, many tools 3~4 implementations of.NET so far: Hopes of higher performances: Bytecode is fully linked within assemblies Ximian (Mono): JIT compiler, runtime almost complete, few compilation tools At least, applications start faster: building.NET shell tools is realistic A portable bytecode (Common Intermediate Language), verified then executed Bytecode is systematically JITed: more efficient than looking for hot-spots? At least, more deterministic run times

Motivations for a Bigloo.NET back-end Not a simple mapping of Java constructs to JVM bytecode As for Java:.NET is more language-agnostic Bigloo generates C code whose efficiency is close to human-written C code Can we achieve the same in the.NET Framework? Better interoperability with other languages Ability to use the most suitable language for different parts of an application Can we get a.NET back-end at low cost once we have a JVM back-end? "Write Once, Run Everywhere" promise Big sets of features provided by the runtime libraries Lots of services provided by the VM: RTTI, GC, subtyping polymorphism, ….NET has better performances?

··· Bigloo and its back-ends OS Bigloo C Runtime OS JVM Bigloo JVM Runtime Bigloo JVM App OS CLR Bigloo.NET RT Bigloo CIL App C# App Eiffel# App Bigloo Application Bigloo Compiler Bigloo Runtime Bigloo Runtime Bigloo C App

Compiler and runtime code sizes (lines) Compiler Common code % C back-end3 0005% JVM back-end5 5009%.NET back-end4 5007% Runtime Common code % C runtime % Boehm GC % JVM runtime7 0008%.NET runtime7 5008%

Outline Introduction Scheme, Functional Programming and Bigloo The.NET Framework Motivations for a Bigloo.NET back-end How do we implement such a different language than C#/J#/… in.NET? Separate compilation A different notion of polymorphism Mapping of types and constructs Closures Tail recursion Continuations Inlining Conclusions

Separate compilation A Bigloo application is made of several modules (one per file), a module may define several classes, the graph of module references can be cyclic 2 problems: MS ilasm wants types to be fully-qualified by the module defining the type MS ilasm cant compile to object files no C# and IL mixing Solution: Code is compiled using PNet ilasm, which performs separate compilation Problem: Binaries generated by PNet are not recognized by MS VMs and not signed (yet) Solution: Binaries can be disassembled, reassembled and signed by MS tools Finding the module that defines a type should be the job of the linker The Bigloo.NET runtime is a mix of C# sources and Bigloo generated IL Each module is compiled separately into a.il file

A different notion of polymorphism In C++/Java/C#/Eiffel/…/CLR object systems: Classes encapsulate both data and behaviour (methods), different visibility levels In C LOS object system: Classes only encapsulate data, no visibility concept Behaviour is implemented through generic functions Generic function dispatch based on dynamic type of arguments Bigloo performs single dispatch, i.e. dispatch based on first argument only First idea: use.NET object system All generics must be defined on the root class No more separate compilation Our approach: All Bigloo types are indexed, all instances have a type index Dispatch based on the type index, using compressed vptr tables Very large and sparse vptr tables Method dispatch based on dynamic or static type of objects

Mapping of types and constructs pair integer string symbol int32, possibly boxed byte[] (for mutability) bigloo.symbol bigloo.pair real float64, possibly boxed method module function class (singleton) class static method or inlined loop class method closure bigloo.procedure Boxing is reduced as much as possible thanks to optional type annotation and type inference system (SUA) Unboxed numerals are passed on the stack Boxed numerals are allocated in the heap, and a reference is passed on the stack

Closures used as first-class values (define (inc x) (lambda (y) (+ x y))) First idea: each closure is a singleton instance of a class Too much of a load on the type system (>4000 real closures in the compiler!) Even worse with JVM, where each class is a file! Most of non-escaping closures (not used as values) are simplified by the compiler: Inlined as loops when called as tail-recursive functions Turned into regular functions otherwise (map (inc 5) '( )) ( ) Comparable with Java inner anonymous classes with a single method

Closures Current implementation from the Java back-end, which lacks of function pointers: Each closure is an instance of a bigloo.procedure : Object[] environment; int arity, index; public Object apply( Object argument_list ) { switch( index ) { case 0: call closure body 0 case 1: call closure body 1 … Problems: Cost of virtual function apply Loss of potential type information for environment variables Cost of body dispatching depending on closure index Delegates ( ) could simplify the dispatch.NET possible improvement? Delegate invocation seems to be 2x slower than indexed call…

Tail Recursion In functional programming, iterations are most of the time written as recursions: (define (fac n) (if (zero? n) 1 (* n (fac (- n 1))))) (fac 3) (define (fac n) (define (iter n acc) (if (zero? n) acc (iter (- n 1) (* acc n)))) (iter n 1)) (fac 3) R5RS requirement: tail-calls should not add activation blocks (* 3 ) (fac 2) (* 2 ) (fac 1) (* 1 ) (fac 0) (iter 3 1)(iter 2 3)(iter 1 6)(iter 0 6)6 Tail-recursive n!: Recursive n!: Continuation of fac call No continuation for iter call

Tail Recursion Self-recursive or co-recursive function calls implemented as goto s (define (even? n) (or (zero? n) (odd? (- n 1)))) (define (odd? n) (and (not (zero? n)) (even? (- n 1)))) What about functions called as tail recursion ? In Java: stack consumption In.NET: call instructions can be flagged as tail. Security issue: security grant/denial frames are dropped Probably not a real concern for Bigloo users Significant performance penalty! Tail recursion not feasible in C and Java

Continuations call/cc (call with current continuation) captures the continuation of a computation (+ 3 (call/cc (lambda (k) (* 3 4)))) 15 (define kont '?) (+ 3 (call/cc (lambda (k) (set! kont k) (* (/ 5 (k 8)) 4)))) 1 (kont 10) 13 Ability to restore the stack state save by a call/cc at any time R5RS requirement: continuations are first-class values The stack can only be introspected in.NET! Simplification: continuations can only be called within the call/cc dynamic extent Continuations become simple computation escapes Perfectly handled by exceptions holding the call/cc return value Continuations are generalization of escapes: Abort a computation (exception) + resume a computation (stack restoration)

Inlining Scheme/Bigloo programs use a lot of small functions like cons, car, cdr, … Such functions must be inlined ! As for the JVM JIT compiler, inlining doesnt seem to be performed as expected Bigloo already performs inlining of Bigloo functions What about Bigloo.NET runtime functions? Inlining must be done "by hand", by injecting IL code instead of function calls

Conclusions A new back-end for Bigloo, to embrace the "Common Language Runtime" initiative Enlargement of Functional Programming targets A work still in progress Tail calls not yet investigated Recette succeeds at 100% on MS platforms The Bigloo compiler has succeeded the bootstrap proof of concept Could we handle continuations within the CLR? No benchmark yet available