8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AtomVMは、Bogdan Erlang抽象マシン(別名BEAM)の軽量実装です
マイコンでも動きますが、今回はAtomVMをUbuntu24.04で動かします

インストール

こちらを参考にします

前提 elixirとerlangはインストール済み

  • elixir 1.17.1-otp-27
  • erlang 27.2.1

必要なツールをインストールします

$ sudo apt install cmake
$ sudo apt install gperf
$ sudo apt install rebar3

AtomVMをビルド

$ git clone https://github.com/atomvm/AtomVM.git
$ mkdir build
$ cd build
$ cmake ..
$ make -j 16

動作確認

$ ./src/AtomVM ./examples/erlang/hello_world.avm

結果

Hello World
Console.puts() and Console.print() work with binary or charlist strings.
Return value: ok

AtomVMのコマンド手軽に使える工夫をする

僕は ~/binにフォルダーを作って必要なファイルをコピーしました

$ cp ./src/AtomVM ~/bin
$ cp ./libs/atomvmlib.avm ~/bin

~/.bashrcに追加

.bashrc
alias avm='AtomVM ~/bin/atomvmlib.avm'

もし~/binにパスが通ってなかったらパスを追加してください

確認

$ avm ./examples/erlang/hello_world.avm

結果

Hello World
Console.puts() and Console.print() work with binary or charlist strings.
Return value: ok

簡単にAtomVMを使えるようになりました

ElixirでAtomVMをソースをビルドして実行する

$ git clone https://github.com/atomvm/atomvm_examples.git
$ cd atomvm_examples/elixir/HelloWorld
$ mix deps.get
$ mix atomvm.packbeam

実行

$ avm hello_world.avm

結果

Hello World
Return value: ok

ElixirでAtomVMをソース作ることができました

ソースを改造してみる

下記のソースは1〜10を2倍して合計します

elixir/HelloWorld/lib/HelloWorld.ex
defmodule HelloWorld do
  def start() do
    1..10
    |> Enum.map(fn x -> x * 2 end)
    |> Enum.reduce(0, fn x, acc -> x + acc end)
    |> IO.inspect()
    :ok
  end
end
$ mix atomvm.packbeam
$ avm hello_world.avm

結果

110
Return value: ok

正しく動きました

ハマったところ

elixir/HelloWorld/lib/HelloWorld.ex
ビルドしてを実行するとエラーになりました
そのためavmコマンドを作りました

$ mix atomvm.packbeam
$ AtomVM  HelloWorld.avm 
Unable to open init.beam
Failed load module: init.beam
Unable to open Elixir.Enum.beam
Failed load module: Elixir.Enum.beam
Warning: module Elixir.Enum cannot be resolved.
CRASH 
======
pid: <0.1.0>

Stacktrace:
[{Elixir.HelloWorld,start,0,[]}]

cp: #CP<module: 0, label: 22, offset: 0>

x[0]: error
x[1]: undef
x[2]: {1,0,0,0,[{0,118}],error}

Stack 
-----

#CP<module: 0, label: 22, offset: 0>


Mailbox
-------


Monitors
--------


**End Of Crash Report**
Return value: error

Geminiに教えてもらった
image.png

これがAtomVMのコマンド手軽に使える工夫をするの意味になります

Enum.sumが実装されていない

Enum.reduce(0, fn x, acc -> x + acc end)

で代用できます
AtomVMはコンパクトに実装するため実装されていない関数もあります

ビルドが遅い&コアが暇している

makeの-j指定を変更する

$ make -j 16

これでCPUが本気をだします

8
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?