Add verbatim builtin.

This commit is contained in:
chriseth
2021-04-26 19:56:44 +02:00
parent 2969bc0f3e
commit e2d8005737
34 changed files with 371 additions and 13 deletions
+1
View File
@@ -0,0 +1 @@
--strict-assembly
+1
View File
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
+1
View File
@@ -0,0 +1 @@
0
+11
View File
@@ -0,0 +1,11 @@
{
let x := 2
let y := sub(x, 2)
let t := verbatim_2i_1o("abc", x, y)
sstore(t, x)
let r := verbatim_0i_1o("def")
verbatim_0i_0o("xyz")
// more than 32 bytes
verbatim_0i_0o(hex"01020304050607090001020304050607090001020304050607090001020102030405060709000102030405060709000102030405060709000102")
r := 9
}
+56
View File
@@ -0,0 +1,56 @@
======= yul_verbatim/input.yul (EVM) =======
Pretty printed source:
object "object" {
code {
let x := 2
let y := sub(x, 2)
let t := verbatim_2i_1o("abc", x, y)
sstore(t, x)
let r := verbatim_0i_1o("def")
verbatim_0i_0o("xyz")
verbatim_0i_0o("\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02\x03\x04\x05\x06\x07\t\x00\x01\x02")
r := 9
}
}
Binary representation:
600260028103808261626382815564656678797a010203040506070900010203040506070900010203040506070900010201020304050607090001020304050607090001020304050607090001026009905050505050
Text representation:
/* "yul_verbatim/input.yul":15:16 */
0x02
/* "yul_verbatim/input.yul":37:38 */
0x02
/* "yul_verbatim/input.yul":34:35 */
dup2
/* "yul_verbatim/input.yul":30:39 */
sub
/* "yul_verbatim/input.yul":78:79 */
dup1
/* "yul_verbatim/input.yul":75:76 */
dup3
verbatimbytecode_616263
/* "yul_verbatim/input.yul":95:96 */
dup3
/* "yul_verbatim/input.yul":92:93 */
dup2
/* "yul_verbatim/input.yul":85:97 */
sstore
verbatimbytecode_646566
/* "yul_verbatim/input.yul":137:158 */
verbatimbytecode_78797a
/* "yul_verbatim/input.yul":189:326 */
verbatimbytecode_01020304050607090001020304050607090001020304050607090001020102030405060709000102030405060709000102030405060709000102
/* "yul_verbatim/input.yul":336:337 */
0x09
/* "yul_verbatim/input.yul":331:337 */
swap1
pop
/* "yul_verbatim/input.yul":0:339 */
pop
pop
pop
pop
@@ -0,0 +1 @@
--strict-assembly --optimize
+1
View File
@@ -0,0 +1 @@
Warning: Yul is still experimental. Please use the output with care.
@@ -0,0 +1 @@
0
@@ -0,0 +1,7 @@
{
// The optimizer assumes verbatim could contain msize,
// so it cannot optimize the mload away.
let x := mload(0x2000)
verbatim_0i_0o("")
sstore(0, 2)
}
@@ -0,0 +1,32 @@
======= yul_verbatim_msize/input.yul (EVM) =======
Pretty printed source:
object "object" {
code {
{
pop(mload(0x2000))
verbatim_0i_0o("aa")
sstore(0, 2)
}
}
}
Binary representation:
612000515061616002600055
Text representation:
/* "yul_verbatim_msize/input.yul":125:131 */
0x2000
/* "yul_verbatim_msize/input.yul":119:132 */
mload
pop
/* "yul_verbatim_msize/input.yul":137:157 */
verbatimbytecode_6161
/* "yul_verbatim_msize/input.yul":172:173 */
0x02
/* "yul_verbatim_msize/input.yul":169:170 */
0x00
/* "yul_verbatim_msize/input.yul":162:174 */
sstore
+5 -1
View File
@@ -33,6 +33,8 @@
#include <boost/test/unit_test.hpp>
#include <range/v3/algorithm/any_of.hpp>
#include <string>
#include <tuple>
#include <memory>
@@ -67,7 +69,9 @@ namespace
{
AssemblyItems input = addDummyLocations(_input);
bool usesMsize = (find(_input.begin(), _input.end(), AssemblyItem{Instruction::MSIZE}) != _input.end());
bool usesMsize = ranges::any_of(_input, [](AssemblyItem const& _i) {
return _i == AssemblyItem{Instruction::MSIZE} || _i.type() == VerbatimBytecode;
});
evmasm::CommonSubexpressionEliminator cse(_state);
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end(), usesMsize) == input.end());
AssemblyItems output = cse.getOptimizedItems();
@@ -0,0 +1,12 @@
contract C {
function f() pure public {
assembly {
let x := verbatim_1o(hex"6001")
}
}
}
// ====
// optimize-yul: true
// ----
// DeclarationError 4619: (84-95): Function "verbatim_1o" not found.
// DeclarationError 3812: (75-106): Variable count mismatch for declaration of "x": 1 variables and 0 values.
@@ -0,0 +1,7 @@
{
verbatim_0i_0o(hex"")
}
// ====
// dialect: evm
// ----
// TypeError 1844: (21-26): The "verbatim_*" builtins cannot be used with empty bytecode.
@@ -0,0 +1,6 @@
{
let x := verbatim_01i_1o(hex"aa", 0)
}
// ----
// DeclarationError 4619: (15-30): Function "verbatim_01i_1o" not found.
// DeclarationError 3812: (6-42): Variable count mismatch for declaration of "x": 1 variables and 0 values.
@@ -0,0 +1,6 @@
{
let verbatim := 2
let verbatim_1i_2o := 3
}
// ----
// ParserError 5568: (32-46): Cannot use builtin function name "verbatim_1i_2o" as identifier name.
@@ -0,0 +1,9 @@
{
let verbatim := 2
let verbatim_abc := 3
function verbatim_fun() {}
}
// ----
// DeclarationError 5017: (10-18): The identifier "verbatim" is reserved and can not be used.
// DeclarationError 5017: (32-44): The identifier "verbatim_abc" is reserved and can not be used.
// DeclarationError 5017: (54-80): The identifier "verbatim_fun" is reserved and can not be used.
@@ -0,0 +1,5 @@
{
let verbatim_01i_02o := 2
}
// ----
// DeclarationError 5017: (10-26): The identifier "verbatim_01i_02o" is reserved and can not be used.