Spaces:
Running
Running
scratch0-5 / utils /VMMakerJS.package /JSMethod.class /instance /covertToZeroBasedArrayReferences.st
| primitive compilation | |
| covertToZeroBasedArrayReferences | |
| "Replace the index expressions in at: and at:put: messages with (<expr> - 1), since C uses zero-based array indexing." | |
| "Note: Up through release 1.31, generated primitives used the convention that array variables pointed to the first element. That meant that Smalltalk one-based index expressions had to have one subtracted to yield a zero-based index. Later, we decided to adjust the base address by -1 once in the primitive prolog rather on every array access. This resulted in a five percent performance increase for the bitmap compress/decompress primitives. This method is retained as documentation and in case we choose to revert the the previous scheme." | |
| | oldIndexExpr newIndexExpr | | |
| parseTree nodesDo: [ :n | | |
| (n isSend and: [(n selector = #at:) or: [ n selector = #at:put: ]]) ifTrue: [ | |
| oldIndexExpr := n args first. | |
| oldIndexExpr isConstant ifTrue: [ | |
| "index expression is a constant: decrement the constant now" | |
| newIndexExpr := TConstantNode new setValue: (n args first value - 1). | |
| ] ifFalse: [ | |
| "index expression is complex: build an expression to decrement result at runtime" | |
| newIndexExpr := TSendNode new | |
| setSelector: #- | |
| receiver: oldIndexExpr | |
| arguments: (Array with: (TConstantNode new setValue: 1)). | |
| ]. | |
| n args at: 1 put: newIndexExpr. | |
| ]. | |
| ]. | |