[squeak-dev] Rewinding the pc [was: Objects as Booleans (was Re: Boolean over-optimization #ifTrue:/#ifFalse:)]

Klaus D. Witzel klaus.witzel at cobss.com
Wed Mar 5 12:07:56 UTC 2008


On Wed, 05 Mar 2008 12:44:31 +0100, Igor Stasenko wrote:

> On 05/03/2008, Bert Freudenberg <bert at freudenbergs.de> wrote:
>> On Mar 5, 2008, at 7:52 , Lukas Renggli wrote:
>>
>>  >> So, the only way is to compile method without optimization of
>>  >> #ifTrue #ifFalse:,
>>  >>  to make sure they do real sends?
>>  >
>>  > Just today in my blog-reader:
>>  >
>>  > http://www.cincomsmalltalk.com/userblogs/mls/blogView?
>>  > showComments=true&printTitle=Boolean_Objects&entry=3382109971
>>  >
>>  > This should also work in Squeak.
>>
>>  Hehe, very cool. It works a bit differently in Squeak, where the
>>  image code has to rewind the context before the jump bytecode that
>>  recognized the non-boolean. Attached is a little changeset that adds
>>  #asBoolean to various classes and overrides #mustBeBooleanIn: to use
>>  that, provided objectsAsBooleans has been enabled. File in, then try
>>  this:
>>
>>  Preferences enable: #objectsAsBooleans.
>>  #(0 1 nil #() #(a b c) 'hi' '') collect: [:each |
>>         each  -> (each ifTrue: ['yes'] ifFalse: ['no'])]
>>
>>  #(0->'no' 1->'yes' nil->'no' #()->'no' #(#a #b #c)->'yes' 'hi'->'yes'
>>  ''->'no')
>>
>>  Fun indeed :)
>>
>
> Yeah, rewinding a bytecode counter of parent context is a kind of
> woodo black magic. But hey! It works!

I admit I do it every now and then, for example in a subclass of compiler  
who wants another parser class (critique + better hack appreciated, but no  
code duplication :)

!LLk1Compiler methodsFor: 'adaption-Compiler'!
parserClass
	thisContext sender jump: 1! !

/Klaus

>>
>>  - Bert -
>>
>
>
>





More information about the Squeak-dev mailing list