@@ -12,6 +12,16 @@ import {OperandV2, StackItem} from "rain.interpreter.interface/interface/IInterp
1212
1313import {InputsLengthMismatch} from "../../error/ErrEval.sol " ;
1414
15+ /// @dev Shift to extract a packed 2-byte function pointer from the high bits
16+ /// of a 256-bit mload. `shr(OPCODE_FUNCTION_POINTER_SHIFT, mload(...))` yields
17+ /// the 16-bit pointer value.
18+ uint256 constant OPCODE_FUNCTION_POINTER_SHIFT = 0xf0 ;
19+
20+ /// @dev Shift to extract a packed 2-byte source offset from the bytecode
21+ /// header. Same width as function pointers but semantically distinct — these
22+ /// are relative offsets into the bytecode, not function pointers.
23+ uint256 constant SOURCE_OFFSET_SHIFT = 0xf0 ;
24+
1525library LibEval {
1626 using LibMemoryKV for MemoryKV;
1727
@@ -64,7 +74,7 @@ library LibEval {
6474 // Find start of sources.
6575 let sourcesStart := add (cursor, mul (sourcesLength, 2 ))
6676 // Find relative pointer to source.
67- let sourcesPointer := shr (0xf0 , mload (add (cursor, mul (sourceIndex, 2 ))))
77+ let sourcesPointer := shr (SOURCE_OFFSET_SHIFT , mload (add (cursor, mul (sourceIndex, 2 ))))
6878 // Move cursor to start of source.
6979 cursor := add (sourcesStart, sourcesPointer)
7080 // Calculate the end.
@@ -97,56 +107,71 @@ library LibEval {
97107 // f needs to be looked up from the fn pointers table.
98108 // operand is 3 bytes.
99109 assembly ("memory-safe" ) {
100- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (0 , word), fsCount), 2 ))))
110+ f := shr (OPCODE_FUNCTION_POINTER_SHIFT , mload (add (fPointersStart, mul (mod (byte (0 , word), fsCount), 2 ))))
101111 operand := and (shr (0xe0 , word), 0xFFFFFF )
102112 }
103113 stackTop = f (state, operand, stackTop);
104114
105115 // Bytes [24, 27].
106116 assembly ("memory-safe" ) {
107- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (4 , word), fsCount), 2 ))))
117+ f := shr (OPCODE_FUNCTION_POINTER_SHIFT , mload (add (fPointersStart, mul (mod (byte (4 , word), fsCount), 2 ))))
108118 operand := and (shr (0xc0 , word), 0xFFFFFF )
109119 }
110120 stackTop = f (state, operand, stackTop);
111121
112122 // Bytes [20, 23].
113123 assembly ("memory-safe" ) {
114- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (8 , word), fsCount), 2 ))))
124+ f := shr (OPCODE_FUNCTION_POINTER_SHIFT , mload (add (fPointersStart, mul (mod (byte (8 , word), fsCount), 2 ))))
115125 operand := and (shr (0xa0 , word), 0xFFFFFF )
116126 }
117127 stackTop = f (state, operand, stackTop);
118128
119129 // Bytes [16, 19].
120130 assembly ("memory-safe" ) {
121- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (12 , word), fsCount), 2 ))))
131+ f := shr (
132+ OPCODE_FUNCTION_POINTER_SHIFT,
133+ mload (add (fPointersStart, mul (mod (byte (12 , word), fsCount), 2 )))
134+ )
122135 operand := and (shr (0x80 , word), 0xFFFFFF )
123136 }
124137 stackTop = f (state, operand, stackTop);
125138
126139 // Bytes [12, 15].
127140 assembly ("memory-safe" ) {
128- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (16 , word), fsCount), 2 ))))
141+ f := shr (
142+ OPCODE_FUNCTION_POINTER_SHIFT,
143+ mload (add (fPointersStart, mul (mod (byte (16 , word), fsCount), 2 )))
144+ )
129145 operand := and (shr (0x60 , word), 0xFFFFFF )
130146 }
131147 stackTop = f (state, operand, stackTop);
132148
133149 // Bytes [8, 11].
134150 assembly ("memory-safe" ) {
135- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (20 , word), fsCount), 2 ))))
151+ f := shr (
152+ OPCODE_FUNCTION_POINTER_SHIFT,
153+ mload (add (fPointersStart, mul (mod (byte (20 , word), fsCount), 2 )))
154+ )
136155 operand := and (shr (0x40 , word), 0xFFFFFF )
137156 }
138157 stackTop = f (state, operand, stackTop);
139158
140159 // Bytes [4, 7].
141160 assembly ("memory-safe" ) {
142- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (24 , word), fsCount), 2 ))))
161+ f := shr (
162+ OPCODE_FUNCTION_POINTER_SHIFT,
163+ mload (add (fPointersStart, mul (mod (byte (24 , word), fsCount), 2 )))
164+ )
143165 operand := and (shr (0x20 , word), 0xFFFFFF )
144166 }
145167 stackTop = f (state, operand, stackTop);
146168
147169 // Bytes [0, 3].
148170 assembly ("memory-safe" ) {
149- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (28 , word), fsCount), 2 ))))
171+ f := shr (
172+ OPCODE_FUNCTION_POINTER_SHIFT,
173+ mload (add (fPointersStart, mul (mod (byte (28 , word), fsCount), 2 )))
174+ )
150175 operand := and (word, 0xFFFFFF )
151176 }
152177 stackTop = f (state, operand, stackTop);
@@ -163,7 +188,10 @@ library LibEval {
163188 while (cursor < end) {
164189 assembly ("memory-safe" ) {
165190 word := mload (cursor)
166- f := shr (0xf0 , mload (add (fPointersStart, mul (mod (byte (28 , word), fsCount), 2 ))))
191+ f := shr (
192+ OPCODE_FUNCTION_POINTER_SHIFT,
193+ mload (add (fPointersStart, mul (mod (byte (28 , word), fsCount), 2 )))
194+ )
167195 // 3 bytes mask.
168196 operand := and (word, 0xFFFFFF )
169197 }
0 commit comments