CorruptVM slang (Was: Re: [squeak-dev] Anyone know the following about Slang?)

Igor Stasenko siguctua at gmail.com
Sat Jul 5 12:43:27 UTC 2008


As follow-up , here an illustration, how easier you can implement
low-level behavior comparing to old ways:

Consider current SmallInteger>>+ implementation:

A method in SmallInteger:
--
+ aNumber
       "Primitive. Add the receiver to the argument and answer with the result
       if it is a SmallInteger. Fail if the argument or the result is not a
       SmallInteger  Essential  No Lookup. See Object documentation
whatIsAPrimitive."

       <primitive: 1>
       ^ super + aNumber
---

And method in VMMaker:
---
primitiveAdd
	self pop2AndPushIntegerIfOK: (self stackIntegerValue: 1) + (self
stackIntegerValue: 0)

which translated to following C code:

sqInt primitiveAdd(void) {
    sqInt integerResult;
    sqInt sp;

	/* begin pop2AndPushIntegerIfOK: */
	integerResult = (stackIntegerValue(1)) + (stackIntegerValue(0));
	if (successFlag) {
		if ((integerResult ^ (integerResult << 1)) >= 0) {
			/* begin pop:thenPush: */
			longAtput(sp = stackPointer - ((2 - 1) * BytesPerWord),
((integerResult << 1) | 1));
			stackPointer = sp;
		} else {
			successFlag = 0;
		}
	}
}

---

Now compare two methods above with single native method which does all itself:
---
SmallInteger>>+ aNumber
<native>

(aNumber bitAnd: 1) equal: 1 ifTrue: [
  | result |
  result := self >> 1 + (aNumber >>1 ).
  (result bitXor: result << 1) greaterOrEqual: 0 ifTrue: [ ^ result <<
1 bitOr: 1 ]
].

^ super perform: #+ with: aNumber   " a perform:[with:..]  is special
message for doing polymorphic send inside a native method"
----

Note , that checking that self is smallinteger is gone, because its
impossible to call this method for other than smallinteger instance.

Think about, how simpler would be to implement methods which use
complex structures, or when primitive needs to instantiate some
object(s) and force to use SpecialObjectsArray. Also, no need in
coercion: you can simply do a type-check and then inline a code which
reads a value(s) from object slots.

Lot of crappy things will be wiped away. Not mentioning, that you can
distribute any native code in your package, and don't need to
accompany it with pre-built plugin .dll etc.

-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list