The Artima Developer Community
Sponsored Link

Agile Buzz Forum
2 2 = 5

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
2 2 = 5 Posted: Jun 13, 2007 6:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: 2 2 = 5
Feed Title: Michael Lucas-Smith
Feed URL: http://www.michaellucassmith.com/site.atom
Feed Description: Smalltalk and my misinterpretations of life
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Michael Lucas-Smith

Advertisement

For some reason the topic of 2 + 2 = 5 came up today while working with Travis. He pointed out a funny thread where he demonstrated with two methods and a compiler deoptimization that he could make 2+2 = 5 in a Smalltalk image without breaking the rest of the running code.

I thought this was pretty funny - I'm sure people had good chuckles about it back when it was posted too. But I was a bit surprised that it took so much code. I wondered what would happen if you just changed the compiler... so if you're curious, add the following to your image:

AssemblerCodeStream>>sendNoCheck: selector numArgs: nargs
	| index |
	selector = #+ ifTrue:
		[code skip: -2.
		(code next: 2) = #[ 75 75 ]
			ifTrue: [code skip: -2. ^self pushConstant: 5]].
	... keep the rest of the method as is ... 

This isn't quite as powerful as Travis's code - since you cannot put 2's in to variables and have them incidently add up to 5, but if you inspect '2+2' you will get 5.

Naturally, we could use this sort of technique for something more useful, like inlining the behaviour of ifNil: and ifNotNil: ... I'd actually rather we had two levels of bytecode - unmodified (exactly represents the code you wrote) and optimized (dynamically inlines, optimizes, unwinds loops, puts in jump statements etc).

May be we'll get there one day.

Read: 2 2 = 5

Topic: Not Paying Attention to the Market Previous Topic   Next Topic Topic: Smalltalk Daily 6/11/07: Using the Dataset Widget

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use