Mini moteur de script WebAssembly interprétant un bytecode simple pour opérations arithmétiques et affichage console, idéal pour apprentissage wasm.
;; Mini moteur de script avec bytecode interprété en WebAssembly
;; Instructions supportées :
;; 0 : PUSH_CONST <valeur>
;; 1 : ADD
;; 2 : SUB
;; 3 : MUL
;; 4 : DIV
;; 5 : PRINT
;; 6 : HALT
(module
;; Importer la fonction d'impression depuis l'environnement externe (JavaScript)
(import "env" "print" (func $print (param i32)))
;; Mémoire pour bytecode et pile
(memory $mem 1)
(export "memory" (memory $mem))
;; Pointeur de pile et compteur de programme
(global $sp (mut i32) (i32.const 0)) ;; pointeur de pile
(global $pc (mut i32) (i32.const 0)) ;; compteur de programme
;; Constantes pour instructions
(global $PUSH_CONST i32 (i32.const 0))
(global $ADD i32 (i32.const 1))
(global $SUB i32 (i32.const 2))
(global $MUL i32 (i32.const 3))
(global $DIV i32 (i32.const 4))
(global $PRINT i32 (i32.const 5))
(global $HALT i32 (i32.const 6))
;; Fonction pour lire un entier 32 bits du bytecode (mémoire) à l'adresse donnée
(func $read_i32 (param $addr i32) (result i32)
(i32.load (local.get $addr))
)
;; Fonction pour écrire un entier 32 bits dans la pile
(func $push (param $val i32)
(local $sp_val i32)
(global.get $sp)
(local.set $sp_val)
;; écrire la valeur dans la pile (mémoire) à l'adresse sp_val
(i32.store (local.get $sp_val) (local.get $val))
;; incrémenter sp de 4 (taille i32)
(global.set $sp (i32.add (local.get $sp_val) (i32.const 4)))
)
;; Fonction pour dépiler
(func $pop (result i32)
(local $sp_val i32)
(global.get $sp)
(local.set $sp_val)
;; décrémenter sp de 4
(global.set $sp (i32.sub (local.get $sp_val) (i32.const 4)))
;; lire la valeur de la pile
(i32.load (global.get $sp))
)
;; Fonction principale : exécute le bytecode
(func $run (param $bytecode_offset i32) (param $bytecode_length i32)
(local $instr i32) ;; instruction courante
(local $arg i32) ;; argument de l'instruction
(local $val1 i32)
(local $val2 i32)
(local $running i32)
;; initialiser pc à 0
(global.set $pc (i32.const 0))
;; initialiser sp à 0
(global.set $sp (i32.const 0))
;; flag running = 1
(local.set $running (i32.const 1))
(block $exit
(loop $loop
;; si pc >= bytecode_length sortir
(br_if $exit (i32.ge_u (global.get $pc) (local.get $bytecode_length)))
;; lire instruction
(local.set $instr
(i32.load (i32.add (local.get $bytecode_offset) (global.get $pc)))
)
;; incrémenter pc de 4 (taille i32)
(global.set $pc (i32.add (global.get $pc) (i32.const 4)))
;; switch instruction
(block
(br_table $push_const $add $sub $mul $div $print $halt (local.get $instr))
;; défaut : ignorer et continuer
(br $loop)
)
;; PUSH_CONST
$push_const
(block
;; lire argument (valeur)
(local.set $arg
(i32.load (i32.add (local.get $bytecode_offset) (global.get $pc)))
)
;; incrémenter pc
(global.set $pc (i32.add (global.get $pc) (i32.const 4)))
;; empiler la valeur
(call $push (local.get $arg))
(br $loop)
)
;; ADD
$add
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.add (local.get $val1) (local.get $val2)))
(br $loop)
)
;; SUB
$sub
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.sub (local.get $val1) (local.get $val2)))
(br $loop)
)
;; MUL
$mul
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.mul (local.get $val1) (local.get $val2)))
(br $loop)
)
;; DIV
$div
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
;; division entière, attention à la division par zéro
(if (i32.eqz (local.get $val2))
(then
;; si division par zéro, empiler 0
(call $push (i32.const 0))
)
(else
(call $push (i32.div_s (local.get $val1) (local.get $val2)))
)
)
(br $loop)
)
;; PRINT
$print
(block
(local.set $val1 (call $pop))
(call $print (local.get $val1))
(br $loop)
)
;; HALT
$halt
(block
(local.set $running (i32.const 0))
(br $exit)
)
)
)
)
(export "run" (func $run))
)html
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>Mini moteur de script WebAssembly</title>
</head>
<body>
<h1>Mini moteur de script avec bytecode interprété en WebAssembly</h1>
<pre id="output"></pre>
<script>
// Le bytecode à exécuter : PUSH_CONST 10, PUSH_CONST 20, ADD, PRINT, HALT
// Chaque instruction et argument est un entier 32 bits (4 bytes)
// Instructions :
// 0 : PUSH_CONST <valeur>
// 1 : ADD
// 5 : PRINT
// 6 : HALT
const bytecode = new Int32Array([
0, 10, // PUSH_CONST 10
0, 20, // PUSH_CONST 20
1, // ADD
5, // PRINT
6 // HALT
]);
// Fonction d'impression appelée depuis WebAssembly
function print(val) {
const output = document.getElementById('output');
output.textContent += val + '\n';
}
// Charger et instancier le module WebAssembly
const wasmCode = `
;; Mini moteur de script avec bytecode interprété en WebAssembly
;; Instructions supportées :
;; 0 : PUSH_CONST <valeur>
;; 1 : ADD
;; 2 : SUB
;; 3 : MUL
;; 4 : DIV
;; 5 : PRINT
;; 6 : HALT
(module
(import "env" "print" (func $print (param i32)))
(memory $mem 1)
(export "memory" (memory $mem))
(global $sp (mut i32) (i32.const 0))
(global $pc (mut i32) (i32.const 0))
(global $PUSH_CONST i32 (i32.const 0))
(global $ADD i32 (i32.const 1))
(global $SUB i32 (i32.const 2))
(global $MUL i32 (i32.const 3))
(global $DIV i32 (i32.const 4))
(global $PRINT i32 (i32.const 5))
(global $HALT i32 (i32.const 6))
(func $read_i32 (param $addr i32) (result i32)
(i32.load (local.get $addr))
)
(func $push (param $val i32)
(local $sp_val i32)
(global.get $sp)
(local.set $sp_val)
(i32.store (local.get $sp_val) (local.get $val))
(global.set $sp (i32.add (local.get $sp_val) (i32.const 4)))
)
(func $pop (result i32)
(local $sp_val i32)
(global.get $sp)
(local.set $sp_val)
(global.set $sp (i32.sub (local.get $sp_val) (i32.const 4)))
(i32.load (global.get $sp))
)
(func $run (param $bytecode_offset i32) (param $bytecode_length i32)
(local $instr i32)
(local $arg i32)
(local $val1 i32)
(local $val2 i32)
(local $running i32)
(global.set $pc (i32.const 0))
(global.set $sp (i32.const 0))
(local.set $running (i32.const 1))
(block $exit
(loop $loop
(br_if $exit (i32.ge_u (global.get $pc) (local.get $bytecode_length)))
(local.set $instr
(i32.load (i32.add (local.get $bytecode_offset) (global.get $pc)))
)
(global.set $pc (i32.add (global.get $pc) (i32.const 4)))
(block
(br_table $push_const $add $sub $mul $div $print $halt (local.get $instr))
(br $loop)
)
$push_const
(block
(local.set $arg
(i32.load (i32.add (local.get $bytecode_offset) (global.get $pc)))
)
(global.set $pc (i32.add (global.get $pc) (i32.const 4)))
(call $push (local.get $arg))
(br $loop)
)
$add
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.add (local.get $val1) (local.get $val2)))
(br $loop)
)
$sub
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.sub (local.get $val1) (local.get $val2)))
(br $loop)
)
$mul
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(call $push (i32.mul (local.get $val1) (local.get $val2)))
(br $loop)
)
$div
(block
(local.set $val2 (call $pop))
(local.set $val1 (call $pop))
(if (i32.eqz (local.get $val2))
(then
(call $push (i32.const 0))
)
(else
(call $push (i32.div_s (local.get $val1) (local.get $val2)))
)
)
(br $loop)
)
$print
(block
(local.set $val1 (call $pop))
(call $print (local.get $val1))
(br $loop)
)
$halt
(block
(local.set $running (i32.const 0))
(br $exit)
)
)
)
)
(export "run" (func $run))
)
`;
// Compiler le module WebAssembly
(async () => {
const wasmModule = await WebAssembly.compile(new TextEncoder().encode(wasmCode));
const instance = await WebAssembly.instantiate(wasmModule, {
env: { print }
});
// Copier le bytecode dans la mémoire WebAssembly
const memory = instance.exports.memory;
const mem32 = new Int32Array(memory.buffer);
mem32.set(bytecode, 0);
// Exécuter le bytecode
instance.exports.run(0, bytecode.byteLength);
})();
</script>
</body>
</html>WebAssembly : module écrit en WAT (WebAssembly Text Format) qui implémente l'interpréteur de bytecode.
Bytecode : tableau d'instructions codées en nombres entiers, interprétées par le moteur wasm.
Console JavaScript : pour l'affichage des résultats via la fonction externe importée 'print'.
Configuration de l'environnement : Node.js ou navigateur avec support WebAssembly.
Dépendances logicielles : aucune bibliothèque externe, uniquement WebAssembly standard et JavaScript pour le bootstrap.
Arduino : non applicable, projet exclusivement logiciel WebAssembly.