Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant"— Presentation transcript:

1 Smalltalk in a.NET World How to write a Smalltalk compiler without writing a VM John Brant brant@refactory.com

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

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

4 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

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

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

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

8 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

9 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

10 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.

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

12 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

13 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

14 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[ ]

15 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

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

17 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

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

19 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]

20 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

21 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)

22 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]]]

23 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)

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

25 #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

26 #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

27 Benchmarks Dolphin#SmalltalkVAVWSqueak add integers36.062.62576.97 14.07 add floats64.7675.482266.6739.0811.45 access strings19.1054.3754.42108.8436.28 create objects20.2090.8230.3090.917.16 copy objects29.9137.3574.77 14.11 perform selectors28.9616.1237.65161.358.07 evaluate blocks6.8030.9161.8842.672.17 generating fractonaccis15.7733.5587.21115.227.20 generating primes42.2243.15140.38181.13* generating strings98.4427.09128.79179.7118.27 forming sets20.1913.2548.63216.1121.14 sorting strings36.6549.36856.5070.5928.65

28 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

29 Contact Information #Smalltalk  http://www.refactory.com/Software/SharpSmalltalk/ VisualWorks.NET DLL  http://www.rileywhite.com/software/assemblyconnect.html.NET  http://www.microsoft.com/net/  http://www.go-mono.com/


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

Similar presentations


Ads by Google