
SMALLTALK SOLUTIONS 2005: ORLANDO VM Primitive Maker Framework for Visualworks Smalltalk Sudhakar Krishnamachari Introduction Personal: [email protected] Context: Develop higher-performance C implementations of code sections for use as primitives. Dev done entirely as Smalltalk methods. Benefit from both the worlds… Smalltalk coding is the easiest… Compiled C code is better in performance …(for high memory swaps and multiple mathematical operations in single call) As also for: Creating a modular Visualworks VM Current Approach: Pure C code primitive or DLLCC wrapper The New Framework: Ported from Squeak VW development by: Eliot Miranda Mode of Presentation: Semi-interactive to fully interactive …(refer to the slide number if required later…) 0101 Contents Basics Process Flow Framework Details Quick Examples / Demo 01 Framework Details Continued.. Interface methods Steps to create the plugin ( a recap ) Extended Interactive Examples / Demo 02 Technical Design Notes Other Issues Q&A Concluding note… 0202 VisualWorks Smalltalk An applications development platform: Objects: Objects ( class) description Object header and data Object state Virtual Image (includes contexts as objects) Virtual Engine •• Compiler •• Interpreter •• Memory handling aka memory reclamation •• Primitives Operating System •• Numbered <primitive: 123> For user extensions use: •• DLLCC <c: void func( int …)> Hardware new •• Named <primitive: ‘a’ module: ‘b’> 0303 HPSVM.InterpreterPlugin Model the primitives in Smalltalk HPSVM.InterpreterProxy In Smalltalk: methods to create objects, access and manipulate Object values 03b The Process Flow Calls to primitive methods that Basic VW Class/ Methods can also be part of any base VW .. class and not necessarily an with method calls to exported dll “primitive” external new class as in DLLCC functions <primitive: ‘primFunc’ module: ‘dllname’> ^Plugin01 new prim: self Func: arg1 Testing/ Debugging Plugin01 doPrimitive: … receiver:… Calls and obtains the return Use the various object/ arguments:… values within the arguments other value access sent calls as given in InterpreterProxy instance side HPSVM.InterpreterPlugin Exported automatically as a subclass C Code file that can be Compiled Dll compiled easily The smalltalk coding of the primitive C File encapsulating the exported and helper functions “primitive” method calls. prim: rcvr Func: arg1 self export: true. Dll_export oopInt primFunc( …. oopInt arg1) { … } 0404 Framework Details HPSVM Parcels: Namespace and Plugin InterpreterProxy and InterpreterPlugin Classes The Smalltalk API subset for Plugin class methods aka Squeak Slang C CodeGeneration classes Simulation methods Smalltalk.HPSVM defineClass: #VMPluginTest superclass: #{HPSVM.InterpreterPlugin} indexedType: #none private: false instanceVariableNames: '' classInstanceVariableNames: '' imports: '' category: 'VMMaker-Plugins' returnGivenValue: anIntegerRcvr | anInt | self export: true. anInt := interpreterProxy integerValueOf: anInteger. ^interpreterProxy integerObjectOf: anInt 0505 Continued.. Smalltalk Plugin method to C conversion: Unary message Single argument function call Binary or keyword sends Multiple argument function call Instance variable Global Variable Method temp variable Function local variable Declares all variables as default of oopI nt type Coerce by specific declaration to type required Return type defaults to oopInt, coerce if required. DLUP_EXPORT(oopInt) returnGivenValue ( oopInt rcvr, oopInt anIntegerRcvr) { oopInt anInt; anInt := integerValueOf (anInteger); return integerObjectOf( anInt); } 0606 Converted code… return: rcvr GivenValue: anIntegerRcvr | anInt | self export: true. anInt := interpreterProxy integerValueOf: anInteger. ^interpreterProxy integerObjectOf: anInt DLUP_EXPORT(oopInt) returnGivenValue ( oopInt rcvr, oop anIntegerRcvr) { oopInt anInt; anInt := integerValueOf (anInteger); return integerObjectOf( anInt); } Quick Demo On:… Primitives and primitive pragma usage Numbered, DLLCC and Named… A HelloWorld primitive function access The DemoPlugin Example: Smalltalk Plugin class, generated C Code Testing and linking through: * doPrimitive:receiver:arguments: * primitive call… 0707 Continued.. Accessing and manipulating Smalltalk objects in C code or Smalltalk Plugin methods 4 distinct types of Smalltalk objects: -2^29 < SmallIntegers > 2^29 30bits + 2 tag bits identifier… Non-indexable ST objects : Boolean, Fraction, Object, Set objects Indexable to oops : Array objects Indexable to byte or word data : LargePositiveInteger, ByteArray, WordArray etc.. 0808 Oops check … isIntegerValue isNilObject: , isTrueObject.. isBytes: , isPointer:… isIndexable: Is:KindOf: Instance variable access Oops ptr ( int*) Class oop fetchPointer: ofObject: fetchWord: ofObject: 32 bit integer pointer Size , hash, gcflags.. fetchIntegerValue:ofObject: Indirection pointer Inst var1 Inst var2 Class oop Small Integers … Size , hash, … integerValueOf Inst var3…. integerObjectOf SmallInteger oop Indirection pointer Stores the integer value Byte1, 2, 3, 4 with 1’s in the 2 low order bit 5, 6 , 7 , 8 …….. Arrays: Bytes, Integer … firstIndexableField: firstFixedField: 0909 Operators and methods supported by InterpreterProxy / InterpreterPlugin Operators translated to C: object access • Boolean &, or , and:, or: .. ••• fetchArray:ofObject:, fetchFloat:ofObject:, • Arithmetic +, - , * , / … fetchPointer:ofObject:, fetchWord:ofObject: • Bit operators << , bitAnd:… storeInteger:ofObject:withValue: • Comparison operators < <= == … • Special mathematical: raisedTo:, min:.. Object size: ••• slotSizeOf: byteSizeOf: , stSizeOf: loop constructs (remember: no real blocks) • whileTrue: , whileFalse: to:do: … converting ••• booleanValueOf:, checkedIntegerValueOf: , conditionals positive64BitValueOf:, signed32BitValueOf: • ifTrue: , ifFalse: , ifTrue:ifFalse: special objects indexed access ••• falseObject , nilObject , trueObject , zeroObject • firstIndexableField: , firstFixedField: , at: , at:put: , basicAt: , basicAt:put: , arrayValueOf: special classes ••• classArray , classBitmap, classByteArray , integer oop conversion/testing classSemaphore • integerValueOf: , integerObjectOf:, isIntegerObject: instance creation directives ••• clone: , instantiateClass:indexableSize: • inline: , export: , returnTypeC: ,static: • makePointwithxValue:yValue: casts miscellaneous • asFloat ,asInteger cCode: , cCode:inSmalltalk: , cCoerce:to: , preIncrement , preDecrement , primitiveFail , success: type testing isFloat , isIndexable , isIntegerOop , isIntegerValue , isWords , isWordsOrBytes , isPointers , isNil, isMemberOf: , isKindOf: , isNil , notNil 10 Create a simple plugin Visualworks Image with HPSVM parcels loaded Create a subclass of InterpreterPlugin: Plugin01 Add instance side method: Test: primitiveReturnInteger: rcvr Plugin01 self export: true. doPrimitive: # primitiveReturnInteger: ….. self returnTypeC: ‘int’ ^interpreterProxy integerObjectOf: 3 . Export to C File thru VMMaker tool or HPSVM.VMMaker new initialize… ….code execution Create access method in any class TestPlugin>> generateInternalPlugin: generateExternallPlugin: PrimitiveReturnInteger #Plugin01 #Plugin01 <primitive: ‘primitiveReturnInteger’ module: ‘Plugin01’> Compile as vw exe Compile as dll Along with rest of vwntoeimport.lib {VW}/bin/src/include Link and Test the source code TestPlugin new PrimitiveReturnInteger 1111 The VM Maker Demo UI… Create plugin class Generate C Code + makefile View C code and makefile Simulation testing workspace Auto compile C code Create and relink through new pragma methods generated Extended Interactive Session Vector Math Example Run through few methods in the VectorPlugin01 class Debug in Smalltalk through doPrimitive: call.. Emit C Code and compare… Link to vwntoe.lib and compile Run the vector tests.. Profile comparison test… Eg: For Vector Add Internal: 154ms DLLCC: 60ms Interpreter: 373ms PluginDll: 42ms For Vector CrossProduct Normal Internal: 469ms DLLCC: 62ms Interpreter: 399ms PluginDll: 42ms Race through BMPReadWriter, JPEGReader, BitBlt,FFT … and others as possible… VMMaker tool…??? Hold back and discuss here…. 1212 Technical Design Notes: Architecture Overview: 1. Utility routines to manipulate Smalltalk objects passed to external code. 2. Simulation of the primitive method in Smalltalk before exporting to C Code. 3. Automated C code generation and compilation for the specific platform. 4. Automated generation of pragma and relinking tests as required The generated code is emitted as the following segments: 1. Header includes 2. #define declarations 3. Global Variable declarations 4. Function prototypes 5. Function declarations including return type, arguments specified,local variable declarations, C statements. 6. Final exports function pointer array Code Generation: Code generation is primarily converting the given smalltalk methods in the subclass of InterpreterPlugin to C code through mechanism of: 1. PluggableCodeGenerator and CCodeGenerator classes to emit the C code, primarily the header, defines, function prototypes etc to the opened file stream. 2. Building a parse tree representation as in standard Smalltalk method compilation as a CompiledMethod instance with a collection of statements for transformation. 3. The statements/ nodes collection is parsed in the subclasses of TtoCParseNode to generate the equivalent C code for each statement/ node built. Static linking and compile with Object Engine: The code can be statically linked and compiled
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages25 Page
-
File Size-