VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker...

25
VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO SMALLTALK SOLUTIONS 2005: ORLANDO for Visualworks Smalltalk Sudhakar Krishnamachari

Transcript of VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker...

Page 1: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

VM Primitive Maker Framework for

SMALLTALK SOLUTIONS 2005: ORLANDOSMALLTALK SOLUTIONS 2005: ORLANDO

for Visualworks Smalltalk

Sudhakar Krishnamachari

Page 2: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

IntroductionIntroduction

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

Page 3: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

ContentsContents� 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

Page 4: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

VisualWorks SmalltalkVisualWorks SmalltalkAn applications development platform: An applications development platform:

Virtual ImageVirtual Image

Virtual EngineVirtual Engine

Objects: Objects ( class) descriptionObjects: Objects ( class) descriptionObject header and dataObject header and dataObject stateObject state

(includes contexts as objects) (includes contexts as objects)

•• CompilerCompiler•• InterpreterInterpreter

Virtual EngineVirtual Engine

Operating SystemOperating System

HardwareHardware

•• InterpreterInterpreter•• Memory handling aka memory reclamationMemory handling aka memory reclamation•• PrimitivesPrimitives

•• NumberedNumbered <primitive: 123><primitive: 123>For user extensions use:For user extensions use:

•• DLLCC DLLCC <c: void func( int …)><c: void func( int …)>new new

•• Named Named <primitive: ‘a’ module: ‘b’><primitive: ‘a’ module: ‘b’>

0303

Page 5: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

HPSVM.InterpreterPluginHPSVM.InterpreterPlugin

Model the primitives in Model the primitives in SmalltalkSmalltalk

HPSVM.InterpreterProxyHPSVM.InterpreterProxy

In Smalltalk: methods to create objects,In Smalltalk: methods to create objects,access and manipulateaccess and manipulateObject valuesObject values

03b03b

Page 6: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

The Process FlowThe Process Flow

..

Calls and obtains the return

Calls to primitive methods that can also be part of any base VW class and not necessarily an external new class as in DLLCC

Basic VW Class/ Methods

with method calls to exported dll “primitive” functions<primitive: ‘primFunc’ module: ‘dllname’>^Plugin01 new prim: self Func: arg1

Testing/ DebuggingTesting/ DebuggingPlugin01 doPrimitive: … receiver:… Plugin01 doPrimitive: … receiver:… Calls and obtains the return

values within the arguments sent

Use the various object/ other value access calls as given in InterpreterProxy instance side

Compiled Dll

encapsulating the exported “primitive” method calls.

HPSVM.InterpreterPlugin subclass

The smalltalk coding of the primitive and helper functionsprim: rcvr Func: arg1

self export: true.….

Plugin01 doPrimitive: … receiver:… Plugin01 doPrimitive: … receiver:… arguments:…arguments:…

0404

Exported automatically as a C Code file that can be compiled easily

C File

Dll_export oopInt primFunc( oopInt arg1){…}

Page 7: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Framework DetailsFramework Details

� HPSVM Parcels: Namespace and Plugin� InterpreterProxy and InterpreterPlugin Classes� The Smalltalk API subset for Plugin class methods a ka Squeak Slang� C CodeGeneration classes� Simulation methods

Smalltalk.HPSVM defineClass: #VMPluginTestSmalltalk.HPSVM defineClass: #VMPluginTestsuperclass: #{HPSVM.InterpreterPlugin} indexedType: #noneprivate: false instanceVariableNames: ''classInstanceVariableNames: '' imports: ''category: 'VMMaker-Plugins'

0505

returnGivenValue: anIntegerRcvr| anInt |self export: true.anInt := interpreterProxy integerValueOf: anInteger.^interpreterProxy integerObjectOf: anInt

Page 8: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Continued..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� 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.

0606

DLUP_EXPORT(oopInt) returnGivenValue ( oopInt rcvr, oopInt anIntegerRcvr){ oopInt anInt;

anInt := integerValueOf (anInteger);return integerObjectOf( anInt);

}

Page 9: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

return: rcvr GivenValue: anIntegerRcvr| anInt |self export: true.anInt := interpreterProxy integerValueOf: anInteger.^interpreterProxy integerObjectOf: anInt

Converted code…Converted code…

DLUP_EXPORT(oopInt) returnGivenValue ( oopInt rcvr, oop anIntegerRcvr){ oopInt anInt;

anInt := integerValueOf (anInteger);return integerObjectOf( anInt);

}

Page 10: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Quick Demo On:…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

Page 11: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Continued..Continued..

� Accessing and manipulating Smalltalk objects in C c ode 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

Page 12: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Oops ptr ( int*)Oops ptr ( int*) Class oopClass oop

Size , hash, gcflags..Size , hash, gcflags..

Indirection pointerIndirection pointer Inst var1Inst var1

Inst var2Inst var2

Inst var3….Inst var3….

Class oopClass oop

Size , hash, …Size , hash, …

Instance variable accessInstance variable accessfetchPointer: ofObject:fetchPointer: ofObject:fetchWord: ofObject:fetchWord: ofObject:fetchIntegerValue:ofObject:fetchIntegerValue:ofObject:

Oops check …Oops check …isIntegerValueisIntegerValueisNilObject: , isTrueObject..isNilObject: , isTrueObject..isBytes: , isPointer:…isBytes: , isPointer:…isIndexable:isIndexable:Is:KindOf:Is:KindOf:

Small Integers …Small Integers …

32 bit integer pointer32 bit integer pointer

Inst var3….Inst var3….

SmallInteger oopSmallInteger oop

Stores the integer value Stores the integer value with 1’s in the 2 low order bitwith 1’s in the 2 low order bit

Byte1, 2, 3, 4Byte1, 2, 3, 4

5, 6 , 7 , 85, 6 , 7 , 8

……..……..

Size , hash, …Size , hash, …

Indirection pointerIndirection pointer

Arrays: Bytes, Integer …Arrays: Bytes, Integer …firstIndexableField:firstIndexableField:firstFixedField:firstFixedField:

Small Integers …Small Integers …integerValueOfintegerValueOfintegerObjectOfintegerObjectOf

0909

Page 13: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Operators and methods supported by InterpreterProxy /

InterpreterPlugin

Operators and methods supported by InterpreterProxy /

InterpreterPlugin

Operators translated to C:Operators translated to C:Operators translated to C:Operators translated to C:• Boolean &, or , and:, or: ..• Arithmetic +, - , * , / …• Bit operators << , bitAnd:… • Comparison operators < <= == … • Special mathematical: raisedTo:, min:..

loop constructs (remember: no real blocks)loop constructs (remember: no real blocks)loop constructs (remember: no real blocks)loop constructs (remember: no real blocks)• whileTrue: , whileFalse: to:do: …

conditionalsconditionalsconditionalsconditionals• ifTrue: , ifFalse: , ifTrue:ifFalse:

object accessobject accessobject accessobject access•••• fetchArray:ofObject:, fetchFloat:ofObject:,

fetchPointer:ofObject:, fetchWord:ofObject: storeInteger:ofObject:withValue:

Object size:Object size:Object size:Object size:•••• slotSizeOf: byteSizeOf: , stSizeOf:

convertingconvertingconvertingconverting•••• booleanValueOf:, checkedIntegerValueOf: ,

positive64BitValueOf:, signed32BitValueOf:

special objectsspecial objectsspecial objectsspecial objectsindexed accessindexed accessindexed accessindexed access• firstIndexableField: , firstFixedField: , at: , at:put: ,

basicAt: , basicAt:put: , arrayValueOf:

integer oop conversion/testinginteger oop conversion/testinginteger oop conversion/testinginteger oop conversion/testing• integerValueOf: , integerObjectOf:, isIntegerObject:

directivesdirectivesdirectivesdirectives• inline: , export: , returnTypeC: ,static:

castscastscastscasts• asFloat ,asInteger

type testingtype testingtype testingtype testingisFloat , isIndexable , isIntegerOop , isIntegerValue , isWords

, isWordsOrBytes , isPointers , isNil, isMemberOf: , isKindOf: , isNil , notNil

special objectsspecial objectsspecial objectsspecial objects•••• falseObject , nilObject , trueObject , zeroObject

special classesspecial classesspecial classesspecial classes•••• classArray , classBitmap, classByteArray ,

classSemaphore

instance creationinstance creationinstance creationinstance creation•••• clone: , instantiateClass:indexableSize: • makePointwithxValue:yValue:

miscellaneousmiscellaneousmiscellaneousmiscellaneouscCode: , cCode:inSmalltalk: , cCoerce:to: , preIncrement ,

preDecrement , primitiveFail , success:

10

Page 14: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Create a simple pluginCreate a simple plugin

Visualworks Image with Visualworks Image with HPSVM parcels loadedHPSVM parcels loaded Create a subclass ofCreate a subclass of

InterpreterPlugin:InterpreterPlugin: Plugin01Plugin01

Add instance side method:Add instance side method:

primitiveReturnInteger: rcvr

self export: true.

self returnTypeC: ‘int’

.

Test:Test:Plugin01 Plugin01 doPrimitive: # doPrimitive: # primitiveReturnInteger: …..

generateInternalPlugin: #Plugin01

generateExternallPlugin: #Plugin01

interpreterProxy integerObjectOf: 3. Export to C File thru VMMaker tool orExport to C File thru VMMaker tool orHPSVM.VMMaker new initialize… ….code execution

Compile as dllCompile as dllvwntoeimport.lib vwntoeimport.lib {VW}/bin/src/include{VW}/bin/src/include

Create access method in any classCreate access method in any classTestPlugin>>TestPlugin>>PrimitiveReturnInteger

<primitive: ‘primitiveReturnInteger’ module: ‘Plugin01’>

Link and TestLink and TestTestPlugin new TestPlugin new PrimitiveReturnInteger 1111

Compile as vw Compile as vw exeexeAlong with rest of Along with rest of the source codethe source code

Page 15: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

The VM Maker Demo UI…The VM Maker Demo UI…

� Create plugin class

� Generate C Code + makefile

� View C code and makefile� View C code and makefile

� Simulation testing workspace

� Auto compile C code

� Create and relink through newpragma methods generated

Page 16: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Extended Interactive SessionExtended 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…� Profile comparison test…

� Eg: For Vector AddInternal: 154ms DLLCC: 60ms Interpreter: 373ms PluginDll: 42ms

� For Vector CrossProduct NormalInternal: 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

Page 17: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Technical Design Notes: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 includes2. #define declarations3. Global Variable declarations4. Function prototypes5. Function declarations including return type, arguments specified,local variable declarations,

C statements.6. Final exports function pointer array

Page 18: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Code Generation:

Code generation is primarily converting the given smalltalk methods in the subclass of InterpreterPluginto 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 along with the Object Engine code. This links the namedPrimitives given in the module as an array of function pointers. For statically compiled code the link is maintained by a tuple of values viz:

< "functionName" memoryAddress numofArgs >

a listing of all exported functions tuples can be obtained for a given named module through:

UserPrimitiveMethod someInstance builtinPrimitivesF or: 'BitBltPlugin'

Page 19: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Dynamic Loading

Using the mechanism that is given for DLLCC:

o Library loading - given a platform specific library name, which usually represents a name thatis searched for by using a set of default paths. (ExternalLibrary load )

o Library unloading - given a loaded library identifier answer by the previous step, unload the library from memory. (ExternalLibrary unload )

o Library name mapping - given an external symbol name, answer the symbols address.(ExternalLibrary mapAddressForName: nameID )

o Library error string - answers the last error created by any one of the previous three steps.

The dynamic load primitives are accessed through ExternalLibrary methods, called in from UserPrimitive>>linkMethod

Callout Process:

Every external procedure call is routed through the primitive 394 described below.

Refer HPSNewprim.txt for the text below:

�“ This primitive is a vararg primitive responsible for taking the Smalltalk arguments from the Smalltalk stack frame and building the C stack frame in preparation for an external call.

� Since this primitive is vararg, The same primitive number is used for every external us er primitive invocation .

� Unlike primitive 395 this version passes the receiver along with all argument s, and passes them directly without interpretation."

Page 20: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Callbacks:Use oeSendMessage* OE access messages to call back into Smalltalk. Remember the library this primitive maker generates is always user constructed and not preexisting, thereby C Callbacks can easily be linked through this mechanism.

The framework can be extended to translate between object perform* msgs to the OEoeSendMessage*

Threads:The possibility to branch of a function execution on a native OS thread has been tested with custom implementation of _beginthread(ex) call and an API to link this can be created.

Will be testing with current VM thread call, management of threads and semaphore mechanism in this framework though NBAPIThreadCall etc..

Connecting to pre constructed Shared LibrariesThis is akin to constructing the wrapper to the library functions with each exported primitive function unmarshalling the passed oopInt parameters to the reqd C parameters for the existing library function

Extensions to 64 bit..As of VW731 the conversion to 64 bit C code is also previewed…

Page 21: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Other Issues…Other Issues…

� Connecting to pre constructed Shared Libraries

� Extensions to 64 bit..

Quote from Squeak Doc and needs a reality check her e..

1. “reduce the size of the vm executable

2. allow ( extending or) fixing of bugs in plugins without changing the vm

3. encourage more modular thinking by primitive implementors

4. encourage modular image code to use those plugins “

1313

Page 22: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Q & AQ & A

I had many for Eliot to answer for… let me try answering yours…!!!

1414

Modified pic from www.dilbert.comModified pic from www.dilbert.com

Page 23: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Epilogue…Epilogue…

We didn’t need primitives at all….

� Despite all the rapid progress in CPU/RAM capabilit y the motivation to obtain max performance remains…will continue to remain in future…

References:

{1}src/doc/vm/HPSExternal.text

{2}src/doc/vi/HPSExternal.text

{3}hps32_64.text

{4}hpsglue.text

{5}hpsnewprim.text

{6}THAPI.html

{7}Greenberg.pdf ( Squeak guide for VMPlugin )

{8}DLLCCGuide.pdf ( VW doc )

{9} VMPrimitiveMaker.pdf ( a draft currently )

1515

Page 24: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

Thanks for the patient hearing of the presentation…

Page 25: VM Primitive Maker Framework for Visualworks Smalltalk · 2008-09-19 · VM Primitive Maker Framework for SMALLTALK SOLUTIONS 2005: ORLANDO ... Mode of Presentation: ... The Smalltalk

2004 Cincom Systems, Inc. All Rights Reserved

Developed in the U.S.A.CINCOM, , and The World’s Most Experienced Software Company are trademarks or registered trademarks

of Cincom Systems, Inc

All other trademarks belong to their respective companies.