Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

1 Compiler Construction Intermediate Code Generation.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
The Web Warrior Guide to Web Design Technologies
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Lab#1 (14/3/1431h) Introduction To java programming cs425
11. A bit of Smalltalk. © O. Nierstrasz P2 — A bit of Smalltalk 11.2 A bit of Smalltalk Overview  Some history  Smalltalk syntax & object model  The.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Principles of Object-Oriented Software Development The language Smalltalk.
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Stéphane Ducasse 1 The Taste of Smalltalk.
Dynamic Object-Oriented Programming with Smalltalk 1. Introduction Prof. O. Nierstrasz Summer Semester 2006.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
2. Smalltalk Basics. © Oscar Nierstrasz ST — Smalltalk Basics 2.2 Roadmap  Everything is an Object  Syntax on a Postcard  Three Kinds of Messages 
Advanced Object-Oriented Programming Features
Java Programming, 3e Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 13 Object-Oriented Programming I am surprised.
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
03G-1 Everything is an Object Examining builtin classes All these are classes in Little Smalltalk:  Object  Class  Method  Block  Boolean True False.
Object-oriented programming and design 1 Smalltalk in a Nutshell Objects & classes Messages & methods Inheritance & metaclasses.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CSE 1301 J Lecture 2 Intro to Java Programming Richard Gesick.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Java Programming, 2E Introductory Concepts and Techniques Chapter 1 An Introduction to Java and Program Design.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Module 1: Introduction to C# Module 2: Variables and Data Types
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Smalltalk for Unix & W i n d o w s Dennis Smith Cherniak Software Development Corporation.
Centralized vs. Decentralized Interpreter Pattern Visitor Pattern.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
LANGUAGE SYSTEMS Chapter Four Modern Programming Languages 1.
Object Oriented Software Development
C# Programming: From Problem Analysis to Program Design1 10 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
CS 598 Scripting Languages Design and Implementation 8. Self.
PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002.
Object Oriented Software Development 4. C# data types, objects and references.
Creating a Java Application and Applet
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
July 22, 2001Introduction to.NET1 Introduction to.NET Framework Gholamali Semsarzadeh July 2001.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Compiler Construction (CS-636)
Java Primer 1: Types, Classes and Operators
Introduction to C# AKEEL AHMED.
Tonga Institute of Higher Education
Copyright 2000, Georgia Tech
CS 331 Principles of Programming Languages
Intro to PHP.
(Computer fundamental Lab)
Quiz Points 1 Rules Raise your hand if you know the question
CSE 3302 Programming Languages
Trees That Represent Grammars
Smalltalk on a Dime.
Presentation transcript:

Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant

#Smalltalk Open source Smalltalk compiler Complete Source Available Self hosting Compiles Smalltalk directly to.NET executables and libraries

Building Smalltalk Virtual Machine .NET Virtual Machine Compiler  RB parser Class Library  ANSI Standard Development tools (debuggers, browsers, etc.)

What’s.NET Anyway? Common class library (collections, GUI, sockets, database, etc.) Common virtual machine and byte code instruction set Language isn’t supposed to matter

Why.NET? (business reasons) Backed by Microsoft Lots of existing code Allow Smalltalk to be used for applications that “require.NET”

Why.NET? (personal reasons) Build a Smalltalk compiler Make an experimental platform Learn.NET Unemployed

Smalltalk vs..NET Smalltalk.NET Strong, dynamic typingStrong, static typing Tagged integersPrimitive integer types BlocksDelegates Resumable ExceptionsNon-resumable exceptions become:, change classes?

Smalltalk vs..NET II Smalltalk.NET All methods are virtualStatic, virtual, & non-virtual methods Add/remove classes & methods Add classes/methods (can’t remove or recompile) ?Structs & Enums

Dynamic Typing Root super type Understands every message Sends #doesNotUnderstand: Root ObjectProxy printString ^self doesNotUnderstand: (Message selector: #printString arguments: #()) printString | stream | stream := WriteStream with: String new. self printOn: stream. ^stream contents

null vs. nil Special object for nil Class constructor initializes variables to nil Method temporaries initialized to nil only if they could be read before written method: aBoolean | temp one | aBoolean ifTrue: [temp := 5]. one := 1. ^temp + one temp := nil.

SmallIntegers No support for tagged integers Real integer objects 10x slower than tagged integers 2r r VW Tagged#Smalltalk

Blocks Create class for each block Create object from block class at runtime method ^#(1) collect: [:each | each + 1] method ^#(1) collect: (Method-Block new) MonadicBlock subclass: Method-Block instanceVariableNames: ‘’ … value: each ^each + 1

Block Variables Variable references  Copied values (method/block arguments)  Active variables (temporaries) method: arg | block temp | block := [temp + arg]. temp := 5. ^block value method arg block temp block arg temp value arg 5

Block Returns Simulate using.NET exceptions 100x slower Tag object (integer) method1 self method2: [^1] method2: aBlock #(2) do: [:each | aBlock value. ^each] method2 method1 do: method2[ ] method1[ ]

Primitives Some actions aren’t representable (e.g., identityHash, +, etc.) Primitive tag  Only one per method Compiler primitive: []  Used anywhere in method  User can add new primitives  Block code evaluated at compile time

Primitive Example identityHash ^Compiler primitive: [:codeGenerator | codeGenerator call: (System.Object getMethod: 'GetHashCode'); constructNewObject: codeGenerator smalltalk smallIntegerClass initializedConstructor] evaluate: self

Optimized Messages Certain messages aren’t sent (e.g., ifTrue:, whileTrue:, to:do:) Can hard code in compiler Instead use macros and “Compiler primitive: []” syntax

Macros RB’s rewrite rules  ifTrue:  Compiler primitive: [:codeGen :block | …] evaluate: uses: Allows arbitrary optimizations (e.g., ifNil:  isNil ifTrue:) Copy source interval for debugger

Connecting with.NET Over 2500 classes provided Seamless integration | algorithm stream | algorithm := HashAlgorithm create: ‘MD5’. stream := FileStream read: ‘rb.im’. [algorithm computeHash: stream] ensure: [stream close]

Connecting Smalltalk to.NET Class references (Convert or System.Convert) Typed Variables (System.Int32) Generic ObjectWrapper Messages for field, property, and method access Constructors – #new* messages

Method arguments First keyword for method name/rest can be anything Arguments converted to.NET objects  is-a tests for arguments Overloaded methods left-to-right, specific-to-generic  System.Console::Write(int/char/…/object)

Console Example System.Console write: anObject (anObject isKindOf: SmallInteger) ifTrue: [System.Console write: anObject integer] ifFalse: [(anObject isKindOf: Character) ifTrue: [System.Console write: anObject character] ifFalse: [(anObject isKindOf: String) ifTrue: [System.Console write: anObject string] ifFalse: [System.Console write: anObject]]]

Connecting.NET to Smalltalk Events use add_EventName: / remove_EventName: methods Delegates (#asDelegate:) Button new add_Click: ([:obj :args | …] asDelegate: EventHandler) Exported Properties/Methods (specified by annotations) Interfaces & subclasses (to do)

#Smalltalk Development (Version 1) Proof of concept VisualWorks program Compiled generated text file with ilasm (.NET’s assembler) Not seamless

#Smalltalk Development (Version 2) Read.SIF files VisualWorks DLL connection to.NET Override qualified name lookup in VW to use.NET DLL  System.Int32 Parse: ‘1234’ System.Reflection.Emit generate.exe file directly

#Smalltalk Development (Version 3) Code based on Version 2 Self hosting Complete.exe compiler Compile three times (fixed point)  Old code compiling new code  New code compiling new code  Verify new code compiled new code valid

Benchmarks Dolphin#SmalltalkVAVWSqueak add integers add floats access strings create objects copy objects perform selectors evaluate blocks generating fractonaccis generating primes * generating strings forming sets sorting strings

Future work IDE (GUI builders, Browsers, etc.) Add Subclassing/Interfaces Optimizations  Reusing intermediate numerical values (e.g., i + j + k)  Type inferencing – eliminating SmallIntegers (e.g., 1 to: 10 do: [:i | ]) Write programs

Contact Information #Smalltalk  VisualWorks.NET DLL   