Questions about primitives - how to avoid one ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Questions about primitives - how to avoid one ?

cedreek
For whatever reasons, I want sometimes to experiment some smalltalk
code by avoiding a primitive call, like in Float>>=, which start by
<primitive:47>. Only commenting doesn't work. So, is it possible ? and
if the primitive fail, is the smalltalk code evaluated ?

Out of curiosity, where can we find the "primitive 47" code ? I
imagine in the source of the VM...  but if someone can give me some
insights :)

Thanks again

Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Questions about primitives - how to avoid one ?

David T. Lewis
On Sun, Feb 17, 2008 at 11:30:18PM +0100, cdrick wrote:
> For whatever reasons, I want sometimes to experiment some smalltalk
> code by avoiding a primitive call, like in Float>>=, which start by
> <primitive:47>. Only commenting doesn't work. So, is it possible ? and
> if the primitive fail, is the smalltalk code evaluated ?

If you comment out the primitive like this is should definitely work:

> aNumber
        "Primitive. Compare the receiver with the argument and return true
        if the receiver is greater than the argument. Otherwise return false.
        Fail if the argument is not a Float. Essential. See Object documentation
        whatIsAPrimitive."

        "<primitive: 44>"
        ^ aNumber adaptToFloat: self andSend: #>

And yes, if the primitive fails, the alternative Smalltalk (after the
"<primitive: 44>" is evaluated.

> Out of curiosity, where can we find the "primitive 47" code ? I
> imagine in the source of the VM...  but if someone can give me some
> insights :)

Load the VMMaker package to see the source code. Look at
Interpreter class>>initializePrimitiveTable to find the method that
implements primitive 47, which is #primitiveFloatEqual. Find implementers
of #primitiveFloatEqual, and you will have the Smalltalk source code.
This will be translated from Smalltalk to C when you generate the VM,
and the resulting C is what implements the primitive.

Dave

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Questions about primitives - how to avoid one ?

cedreek
> If you comment out the primitive like this is should definitely work:
>

If I comment out the primitive, in Float>>= ... and put a self halt
after, the halt point has no effect.
>
> Load the VMMaker package to see the source code. Look at
> Interpreter class>>initializePrimitiveTable to find the method that
> implements primitive 47, which is #primitiveFloatEqual. Find implementers
> of #primitiveFloatEqual, and you will have the Smalltalk source code.
> This will be translated from Smalltalk to C when you generate the VM,
> and the resulting C is what implements the primitive.
>
Ok, I 'll have a look :)

Thanks

Cédrick

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Questions about primitives - how to avoid one ?

Klaus D. Witzel
Hi C'edrick,

on Mon, 18 Feb 2008 01:10:41 +0100, you wrote:

> David T. Lewis wrote:
>> If you comment out the primitive like this is should definitely work:
>>
>
> If I comment out the primitive, in Float>>= ... and put a self halt
> after, the halt point has no effect.

This is correct, both your observation and the corresponding behavior of  
Squeak's VM. There are some message selectors whose method is only looked  
up if receiver/args don't match, for performance reason. To this belongs  
#= (bytecode #182) which is first tried for SmallInteger receiver/args,  
then if that fails tried for Float receiver/args, and if that also fails  
then routine #bytecodePrimEqual in Squeak's VM performs a normal send  
(which would then find the primitive number and/or the Smalltalk code if  
the primitive where absent or failed).

Looks complicated but works *fast* (only less failure is more speed :)

For what you want to do I'd suggest you duplicate method Float>>#= as  
Float>>#~=~ (just add the two ~ in Float's #= selector and alt-s) and then  
you can do

  1.234 ~=~ 1.234

and the changes you do to your #~=~ method then do what you want (it's  
Smalltalk land :)

HTH.

/Klaus

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners