This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
HuffClone.huff
91 lines (80 loc) · 3.57 KB
/
HuffClone.huff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// @title HuffClone
/// @notice Clones with Immutable Args Library
/// Original implementation:
/// https://github.com/wighawag/clones-with-immutable-args/blob/master/src/Clone.sol
/// @author wighawag <https://github.com/wighawag>
/// @author zefram <https://github.com/boredGenius>
/// @author hari <https://github.com/hrkrshnn>
/// @author z0r0z <https://github.com/z0r0z>
/// @author clabby <https://github.com/clabby>
/// @notice Reads an immutable arg with type address
/// @param argOffset The offset of the arg in the packed data
/// @return arg The arg value
#define macro GET_ARG_ADDRESS() = takes (1) returns (1) {
// Initial Stack: // [argOffset]
GET_IMMUTABLE_ARGS_OFFSET() // [offset, argOffset]
add // [offset + argOffset]
calldataload // [cd]
0x60 // [0x60, cd]
shr // [cd >> 0x60]
}
/// @notice Reads an immutable arg with type uint256
/// @param argOffset The offset of the arg in the packed data
/// @return arg The arg value
#define macro GET_ARG_UINT_256() = takes (1) returns (1) {
// Initial Stack: // [argOffset]
GET_IMMUTABLE_ARGS_OFFSET() // [offset, argOffset]
add // [offset + argOffset]
calldataload // [cd]
}
/// @notice Reads a uint256 array stored in the immutable args.
/// @param argOffset The offset of the arg in the packed data
/// @param arrLen Number of elements in the array
/// @return arr The beginning location of the array in memory
#define macro GET_ARG_UINT_256_ARR(mem_ptr) = takes (2) returns (1) {
// Initial Stack: [argOffset, arrLen]
GET_IMMUTABLE_ARGS_OFFSET() // [offset, argOffset, arrLen]
add // [offset + argOffset, arrLen]
dup2 <mem_ptr> // [mem_ptr, arrLen, offset + argOffset, arrLen]
mstore // [offset + argOffset, arrLen]
swap1 0x05 shl swap1 // [offset + argOffset, arrLen * 0x20]
<mem_ptr> 0x20 add // [mem_ptr + 0x20, offset + argOffset, arrLen * 0x20]
calldatacopy // []
// Return the memory pointer of the array
<mem_ptr> // [mem_ptr]
}
/// @notice Reads an immutable arg with type uint64
/// @param argOffset The offset of the arg in the packed data
/// @return arg The arg value
#define macro GET_ARG_UINT_64() = takes (1) returns (1) {
// Initial Stack: // [argOffset]
GET_IMMUTABLE_ARGS_OFFSET() // [offset, argOffset]
add // [offset + argOffset]
calldataload // [cd]
0xC0 // [0xC0, cd]
shr // [cd >> 0xC0]
}
/// @notice Reads an immutable arg with type uint8
/// @param argOffset The offset of the arg in the packed data
/// @return The arg value
#define macro GET_ARG_UINT_8() = takes (1) returns (1) {
// Initial Stack: // [argOffset]
GET_IMMUTABLE_ARGS_OFFSET() // [offset, argOffset]
add // [offset + argOffset]
calldataload // [cd]
0xF8 // [0xF8, cd]
shr // [cd >> 0xF8]
}
/// @return The offset of the packed immutable args in calldata
#define macro GET_IMMUTABLE_ARGS_OFFSET() = takes (0) returns (1) {
0x02 // [0x02]
calldatasize // [calldatasize, 0x02]
sub // [calldatasize - 0x02]
calldataload // [cd]
0xF0 // [0xF0, cd]
shr // [cd >> 0xF0]
0x02 // [0x02, cd >> 0xF0]
add // [0x02 + cd >> 0xF0]
calldatasize // [calldatasize, 0x02 + cd >> 0xF0]
sub // [calldatasize - (0x02 + cd >> 0xF0)]
}