Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Smalltalk in a .NET World


Published on

John Brant:
Smalltalk in a .NET World.
How to write a Smalltalk compiler without writing a VM
(ESUG 2003, Bled)

Published in: Technology
  • Login to see the comments

  • Be the first to like this

Smalltalk in a .NET World

  1. 1. Smalltalk in a .NET World How to write a Smalltalk compiler without writing a VM John Brant [email_address]
  2. 2. #Smalltalk <ul><li>Open source Smalltalk compiler </li></ul><ul><li>Complete Source Available </li></ul><ul><li>Self hosting </li></ul><ul><li>Compiles Smalltalk directly to .NET executables and libraries </li></ul>
  3. 3. Building Smalltalk <ul><li>Virtual Machine </li></ul><ul><ul><li>.NET Virtual Machine </li></ul></ul><ul><li>Compiler </li></ul><ul><ul><li>RB parser </li></ul></ul><ul><li>Class Library </li></ul><ul><ul><li>ANSI Standard </li></ul></ul><ul><li>Development tools (debuggers, browsers, etc.) </li></ul>
  4. 4. What’s .NET Anyway? <ul><li>Common class library (collections, GUI, sockets, database, etc.) </li></ul><ul><li>Common virtual machine and byte code instruction set </li></ul><ul><li>Language isn’t supposed to matter </li></ul>
  5. 5. Why .NET? (business reasons) <ul><li>Backed by Microsoft </li></ul><ul><li>Lots of existing code </li></ul><ul><li>Allow Smalltalk to be used for applications that “require .NET” </li></ul>
  6. 6. Why .NET? (personal reasons) <ul><li>Build a Smalltalk compiler </li></ul><ul><li>Make an experimental platform </li></ul><ul><li>Learn .NET </li></ul><ul><li>Unemployed </li></ul>
  7. 7. Smalltalk vs. .NET ? become:, change classes Non-resumable exceptions Resumable Exceptions Delegates Blocks Primitive integer types Tagged integers Strong, static typing Strong, dynamic typing .NET Smalltalk
  8. 8. Smalltalk vs. .NET II Structs & Enums ? Add classes/methods (can’t remove or recompile) Add/remove classes & methods Static, virtual, & non-virtual methods All methods are virtual .NET Smalltalk
  9. 9. Dynamic Typing <ul><li>Root super type </li></ul><ul><li>Understands every message </li></ul><ul><li>Sends #doesNotUnderstand: </li></ul>printString ^self doesNotUnderstand: (Message selector: #printString arguments: #()) printString | stream | stream := WriteStream with: String new. self printOn: stream. ^stream contents Root Object Proxy
  10. 10. null vs. nil <ul><li>Special object for nil </li></ul><ul><li>Class constructor initializes variables to nil </li></ul><ul><li>Method temporaries initialized to nil only if they could be read before written </li></ul>method: aBoolean | temp one | aBoolean ifTrue: [temp := 5]. one := 1. ^temp + one temp := nil.
  11. 11. SmallIntegers <ul><li>No support for tagged integers </li></ul><ul><li>Real integer objects </li></ul><ul><li>10x slower than tagged integers </li></ul>VW Tagged #Smalltalk 2r00001011 2r00000010
  12. 12. Blocks <ul><li>Create class for each block </li></ul><ul><li>Create object from block class at runtime </li></ul>method ^#(1) collect: [:each | each + 1] method ^#(1) collect: (Method-Block new) MonadicBlock subclass: Method-Block instanceVariableNames: ‘’ … value: each ^each + 1
  13. 13. Block Variables <ul><li>Variable references </li></ul><ul><ul><li>Copied values (method/block arguments) </li></ul></ul><ul><ul><li>Active variables (temporaries) </li></ul></ul>method: arg | block temp | block := [temp + arg]. temp := 5. ^block value arg 5 method arg block temp block arg temp temp value
  14. 14. Block Returns <ul><li>Simulate using .NET exceptions </li></ul><ul><li>100x slower </li></ul><ul><li>Tag object (integer) </li></ul>method1 self method2: [^1] method2: aBlock #(2) do: [:each | aBlock value. ^each] method2 method1 do: method2[ ] method1[ ]
  15. 15. Primitives <ul><li>Some actions aren’t representable (e.g., identityHash, +, etc.) </li></ul><ul><li>Primitive tag </li></ul><ul><ul><li>Only one per method </li></ul></ul><ul><li>Compiler primitive: [] </li></ul><ul><ul><li>Used anywhere in method </li></ul></ul><ul><ul><li>User can add new primitives </li></ul></ul><ul><ul><li>Block code evaluated at compile time </li></ul></ul>
  16. 16. Primitive Example identityHash ^Compiler primitive: [:codeGenerator | codeGenerator call: (System.Object getMethod: 'GetHashCode'); constructNewObject: codeGenerator smalltalk smallIntegerClass initializedConstructor] evaluate: self
  17. 17. Optimized Messages <ul><li>Certain messages aren’t sent (e.g., ifTrue:, whileTrue:, to:do:) </li></ul><ul><li>Can hard code in compiler </li></ul><ul><li>Instead use macros and “Compiler primitive: []” syntax </li></ul>
  18. 18. Macros <ul><li>RB’s rewrite rules </li></ul><ul><ul><li>``@node ifTrue: ``@block  Compiler primitive: [:codeGen :block | …] evaluate: ``@node uses: ``@block </li></ul></ul><ul><li>Allows arbitrary optimizations (e.g., ifNil:  isNil ifTrue:) </li></ul><ul><li>Copy source interval for debugger </li></ul>
  19. 19. Connecting with .NET <ul><li>Over 2500 classes provided </li></ul><ul><li>Seamless integration </li></ul>| algorithm stream | algorithm := HashAlgorithm create: ‘MD5’. stream := FileStream read: ‘’. [algorithm computeHash: stream] ensure: [stream close]
  20. 20. Connecting Smalltalk to .NET <ul><li>Class references (Convert or System.Convert) </li></ul><ul><li>Typed Variables (System.Int32) </li></ul><ul><li>Generic ObjectWrapper </li></ul><ul><li>Messages for field, property, and method access </li></ul><ul><li>Constructors – #new* messages </li></ul>
  21. 21. Method arguments <ul><li>First keyword for method name/rest can be anything </li></ul><ul><li>Arguments converted to .NET objects </li></ul><ul><ul><li>is-a tests for arguments </li></ul></ul><ul><li>Overloaded methods left-to-right, specific-to-generic </li></ul><ul><ul><li>System.Console::Write(int/char/…/object) </li></ul></ul>
  22. 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. 23. Connecting .NET to Smalltalk <ul><li>Events use add_EventName: / remove_EventName: methods </li></ul><ul><li>Delegates (#asDelegate:) </li></ul><ul><ul><li>Button new add_Click: ([:obj :args | …] asDelegate: EventHandler) </li></ul></ul><ul><li>Exported Properties/Methods (specified by annotations) </li></ul><ul><li>Interfaces & subclasses (to do) </li></ul>
  24. 24. #Smalltalk Development (Version 1) <ul><li>Proof of concept </li></ul><ul><li>VisualWorks program </li></ul><ul><li>Compiled generated text file with ilasm (.NET’s assembler) </li></ul><ul><li>Not seamless </li></ul>
  25. 25. #Smalltalk Development (Version 2) <ul><li>Read .SIF files </li></ul><ul><li>VisualWorks DLL connection to .NET </li></ul><ul><li>Override qualified name lookup in VW to use .NET DLL </li></ul><ul><ul><li>System.Int32 Parse: ‘1234’ </li></ul></ul><ul><li>System.Reflection.Emit generate .exe file directly </li></ul>
  26. 26. #Smalltalk Development (Version 3) <ul><li>Code based on Version 2 </li></ul><ul><li>Self hosting </li></ul><ul><li>Complete .exe compiler </li></ul><ul><li>Compile three times (fixed point) </li></ul><ul><ul><li>Old code compiling new code </li></ul></ul><ul><ul><li>New code compiling new code </li></ul></ul><ul><ul><li>Verify new code compiled new code valid </li></ul></ul>
  27. 27. Benchmarks 28.65 70.59 856.50 49.36 36.65 sorting strings 21.14 216.11 48.63 13.25 20.19 forming sets 18.27 179.71 128.79 27.09 98.44 generating strings * 181.13 140.38 43.15 42.22 generating primes 7.20 115.22 87.21 33.55 15.77 generating fractonaccis 2.17 42.67 61.88 30.91 6.80 evaluate blocks 8.07 161.35 37.65 16.12 28.96 perform selectors 14.11 74.77 74.77 37.35 29.91 copy objects 7.16 90.91 30.30 90.82 20.20 create objects 36.28 108.84 54.42 54.37 19.10 access strings 11.45 39.08 2266.67 75.48 64.76 add floats 14.07 576.97 576.97 2.62 36.06 add integers Squeak VW VA #Smalltalk Dolphin
  28. 28. Future work <ul><li>IDE (GUI builders, Browsers, etc.) </li></ul><ul><li>Add Subclassing/Interfaces </li></ul><ul><li>Optimizations </li></ul><ul><ul><li>Reusing intermediate numerical values (e.g., i + j + k) </li></ul></ul><ul><ul><li>Type inferencing – eliminating SmallIntegers (e.g., 1 to: 10 do: [:i | ]) </li></ul></ul><ul><li>Write programs </li></ul>
  29. 29. Contact Information <ul><li>#Smalltalk </li></ul><ul><ul><li> </li></ul></ul><ul><li>VisualWorks .NET DLL </li></ul><ul><ul><li> </li></ul></ul><ul><li>.NET </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul>