Limiting the number of method arguments in a Smalltalk-80 implementation

The maximum number of arguments to a method is limited to 2^5-1 (i.e. 31), since there are only 5 bits to encode the number of arguments in a compiled method, as shown in Figure 27.4 of the blue book . But the double extended send bytecode has 8 bits to encode the number of arguments (see the definition of doubleExtendedSendBytecode here ), which means that I can send up to 2^8-1 (i.e. 127) arguments for the message (using perform: or operator will not be compiled). Is this a contradiction? I think the bytecode uses too many bits to encode the number of arguments.

+4
source share
2 answers

Yes, this is a contradiction, but it is still not very important.

In addition, the number of arguments in methods is also limited by the maximum number of temporary variables in the method, which in most Smalltalks is 2^8-1 .

There is another part:

In Squeak, the number of arguments is actually limited to 15 ( 2^4-1 ), and also has only a space (4 bits) in the method header. A Squeak CompiledMethod comment says:

 (index 18) 6 bits: number of temporary variables (#numTemps) (index 24) 4 bits: number of arguments to the method (#numArgs) 

with #numTemps , including the number of arguments.

In short, yes, doubleExtendedSendBytecode can encode more arguments than the actual expression in CompiledMethod .

This is one of the reasons why it was replaced in Squeak with doubleExtendedDoAnything bytecode that can do more than just send, but limits the number of arguments 2^5-1 (which is even more than CompiledMethod can encode, but it is unlikely that CompiledMethod will change the encoding of more than 15 arguments in the foreseeable future).

+4
source

The actual number of arguments used is mostly small. The number of arguments of CompiledMethods in the Moose 4.6 image I have:

 |bag| bag := IdentityBag new. CompiledMethod allInstances do:[ :element | bag add: element numArgs ]. bag sortedCounts 52006 -> 0 25202 -> 1 6309 -> 2 2133 -> 3 840 -> 4 391 -> 5 191 -> 6 104 -> 7 61 -> 8 12 -> 9 11 -> 10 5 -> 11 4 -> 12 3 -> 13 2 -> 15 1 -> 14 
+1
source

Source: https://habr.com/ru/post/1494431/


All Articles