# 编译运行合约编译器

# 下载Summoner项目

git clone https://github.com/Bytom/summoner

# 确认编译环境

需要确认 bison,byacc,flex等已经安装

如果没有安装,执行以下指令

yum -y install bison 
yum -y install bycc
yum -y install flex

# 安装cmake并配置环境变量

因为yum的版本太低了,必须自己去官网下载最新版安装

下载cmake :https://cmake.org/download/ (opens new window)

解压到用户目录,比如 /usr/local

tar -zxvf cmake-3.22.0-rc1-linux-x86_64.tar.gz

配置环境变量,打开/etc/profile,在文件末尾加上cmake路径

PATH=/usr/local/cmake/bin:$PATH
export PATH

让环境变量生效

source /etc/profile

查看cmake是否可以使用

cmake -version

cmake version 3.22.0-rc1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

打印出版本即可

# 编译Summoner

在Summoner主目录新建一个build目录

mkdir buid
cd build

编译Summoner

# cmake ..

-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/summoner/build

# cmake --build .

[  4%] Building C object builtin/CMakeFiles/BUILTIN.dir/builtin.c.o
[  9%] Linking C static library libBUILTIN.a
[  9%] Built target BUILTIN
[ 13%] Building C object compiler/CMakeFiles/COMPILER.dir/lex.yy.c.o
[ 18%] Building C object compiler/CMakeFiles/COMPILER.dir/y.tab.c.o
[ 22%] Building C object compiler/CMakeFiles/COMPILER.dir/codegen.c.o
[ 27%] Building C object compiler/CMakeFiles/COMPILER.dir/disassemble.c.o
[ 31%] Building C object compiler/CMakeFiles/COMPILER.dir/create.c.o
[ 36%] Building C object compiler/CMakeFiles/COMPILER.dir/error.c.o
[ 40%] Building C object compiler/CMakeFiles/COMPILER.dir/fix_tree.c.o
[ 45%] Building C object compiler/CMakeFiles/COMPILER.dir/opcode.c.o
[ 50%] Building C object compiler/CMakeFiles/COMPILER.dir/string.c.o
[ 54%] Building C object compiler/CMakeFiles/COMPILER.dir/compiler.c.o
[ 59%] Building C object compiler/CMakeFiles/COMPILER.dir/util.c.o
[ 63%] Linking C static library libCOMPILER.a
[ 72%] Built target COMPILER
[ 77%] Building C object debug/CMakeFiles/DEBUG.dir/debug.c.o
[ 81%] Linking C static library libDEBUG.a
[ 81%] Built target DEBUG
[ 86%] Building C object share/CMakeFiles/SHARE.dir/wchar.c.o
[ 90%] Linking C static library libSHARE.a
[ 90%] Built target SHARE
[ 95%] Building C object CMakeFiles/summoner.dir/main/main.c.o
[100%] Linking C executable summoner
[100%] Built target summoner

构建成功

# 编译Summoner合约

随便写一个Summoner合约

func IfLocal(b int64, c int64) {
	var a int64 = 12345678
	if b > c {
		a = b
	} else {
		a = c
	}
	a = a + 6
}

生成a文件,编译该合约

./summoner a

** global variable section *********************************
** function section ****************************************
*** [0] IfLocal ***
void  IfLocal ( b,  c)
   0 DATA_4
   1 12345678
   5 2
   6 PICK
   7 2
   8 PICK
   9 GREATERTHAN
  10 NOT
  11 NOP
  12 JUMPIF
  13 24
  17 DROP
  18 DUP
  19 JUMP
  20 24
  24 DROP
  25 OVER
  26 DUP
  27 6
  28 ADD
*** line number ***
*** assemble opcodes ***
044e61bc0052795279a091616400000018757663000000187578765693
*** end of IfLocal ***
** type section ********************************************
** toplevel ********************
*** line number ***

编译完成,获得机器码和字节码

上次更新: 2021/10/29上午11:13:32