-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #993 from ajlopez/bytecode_compiler
Bytecode compiler for tests
- Loading branch information
Showing
3 changed files
with
381 additions
and
195 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
ethereumj-core/src/test/java/org/ethereum/vm/BytecodeCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (c) [2018] [ <ether.camp> ] | ||
* This file is part of the ethereumJ library. | ||
* | ||
* The ethereumJ library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The ethereumJ library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.ethereum.vm; | ||
|
||
import org.ethereum.vm.OpCode; | ||
import org.spongycastle.util.encoders.Hex; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BytecodeCompiler { | ||
public byte[] compile(String code) { | ||
return compile(code.split("\\s+")); | ||
} | ||
|
||
private byte[] compile(String[] tokens) { | ||
List<Byte> bytecodes = new ArrayList<>(); | ||
int ntokens = tokens.length; | ||
|
||
for (int i = 0; i < ntokens; i++) { | ||
String token = tokens[i].trim().toUpperCase(); | ||
|
||
if (token.isEmpty()) | ||
continue; | ||
|
||
if (isHexadecimal(token)) | ||
compileHexadecimal(token, bytecodes); | ||
else | ||
bytecodes.add(OpCode.byteVal(token)); | ||
} | ||
|
||
int nbytes = bytecodes.size(); | ||
byte[] bytes = new byte[nbytes]; | ||
|
||
for (int k = 0; k < nbytes; k++) | ||
bytes[k] = bytecodes.get(k).byteValue(); | ||
|
||
return bytes; | ||
} | ||
|
||
private static boolean isHexadecimal(String token) { | ||
return token.startsWith("0X"); | ||
} | ||
|
||
private static void compileHexadecimal(String token, List<Byte> bytecodes) { | ||
byte[] bytes = Hex.decode(token.substring(2)); | ||
|
||
for (int k = 0; k < bytes.length; k++) | ||
bytecodes.add(bytes[k]); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
ethereumj-core/src/test/java/org/ethereum/vm/BytecodeCompilerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright (c) [2018] [ <ether.camp> ] | ||
* This file is part of the ethereumJ library. | ||
* | ||
* The ethereumJ library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* The ethereumJ library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.ethereum.vm; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class BytecodeCompilerTest { | ||
@Test | ||
public void compileSimpleOpcode() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("ADD"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(1, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
} | ||
|
||
@Test | ||
public void compileSimpleOpcodeWithSpaces() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile(" ADD "); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(1, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
} | ||
|
||
@Test | ||
public void compileTwoOpcodes() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("ADD SUB"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(2, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
Assert.assertEquals(3, result[1]); | ||
} | ||
|
||
@Test | ||
public void compileFourOpcodes() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("ADD MUL SUB DIV"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(4, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
Assert.assertEquals(2, result[1]); | ||
Assert.assertEquals(3, result[2]); | ||
Assert.assertEquals(4, result[3]); | ||
} | ||
|
||
@Test | ||
public void compileHexadecimalValueOneByte() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("0x01"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(1, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
} | ||
|
||
@Test | ||
public void compileHexadecimalValueTwoByte() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("0x0102"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(2, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
Assert.assertEquals(2, result[1]); | ||
} | ||
|
||
@Test | ||
public void compileSimpleOpcodeInLowerCase() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("add"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(1, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
} | ||
|
||
@Test | ||
public void compileSimpleOpcodeInMixedCase() { | ||
BytecodeCompiler compiler = new BytecodeCompiler(); | ||
|
||
byte[] result = compiler.compile("Add"); | ||
|
||
Assert.assertNotNull(result); | ||
Assert.assertEquals(1, result.length); | ||
Assert.assertEquals(1, result[0]); | ||
} | ||
} |
Oops, something went wrong.