L1VM - memory

Here I want to talk about how the L1VM handles the memory, where the variables are stored. If you declare a variable in Brackets, then it is stored in the global memory array. The compiler knows where the start address is and the size of the variable. If the size is greater than 1 then it is an array. The size of the global data array is known at compile time. So the VM can allocate the data array and load the data before the VM runs. The memory is freed after the program ends. The L1VM needs about 9 MB RAM at a minimum to work. There are only global variables in Brackets. There also are constant variables, pointers and boolean variables.
You can use the #var preprocessor to declare a variable ending:

// multiply-func.l1com
//
#include <intr.l1h>
(main func)
    // declare x, y and res variables
    (set int64 1 zero 0)
    (set int64 1 x 23)
    (set int64 1 y 42)
    (set int64 1 res 0)
    // call function multiply
    (x y :multiply !)
    // get result from function
    (res stpop)
    // print result
    print_i (res)
    print_n
    exit (zero)
(funcend)

(multiply func)
    // set variable ending to multiply
    // so the x~ is xmultiply
    #var ~ multiply
    (set int64 1 x~)
    (set int64 1 y~)
    (set int64 1 res~)
    // get x and y from stack
    (y~ x~ stpop)
    // calculate the result
    ((x~ y~ *) res~ =)
    // push the result on the stack
    (res~ stpush)
(funcend)

And here is the assembly output:

.data
Q, 1, zero
@, 0Q, 0
Q, 1, x
@, 8Q, 23
Q, 1, y
@, 16Q, 42
Q, 1, res
@, 24Q, 0
Q, 1, xmultiply
@, 32Q, 
Q, 1, ymultiply
@, 40Q, 
Q, 1, resmultiply
@, 48Q, 
.dend
.code
:main
loada zero, 0, 0
stpushi 0
loada x, 0, 1
stpushi 1
loada y, 0, 2
stpushi 2
jsr :multiply
stpopi 3
load res, 0, 4
pullqw 3, 4, 0
stpopi 0
loada res, 0, 1
intr0 4, 1, 0, 0
intr0 7, 0, 0, 0
loada zero, 0, 2
intr0 255, 2, 0, 0
rts
:multiply
loada zero, 0, 0
stpopi 1
load ymultiply, 0, 2
pullqw 1, 2, 0
stpopi 2
load xmultiply, 0, 3
pullqw 2, 3, 0
muli 2, 1, 3
load resmultiply, 0, 4
pullqw 3, 4, 0
loada resmultiply, 0, 5
stpushi 5
rts
.cend

Here the variables are set in the .data section on top! And you can see that the function variables are called:

xmultiply, ymultiply and resmultiply

If you need a variable array and want to set the size at run time, then you can use my memory modules to do that. See: lib/memlib and the other modules.

Brackets is static typed and variable type safe. Memory accesses are checked at runtime to avoid read/write out of variable range. So you get an error message if an array variable is accessed on a bad index value. The string module also checks if the accesses are in legal range. This avoids buffer overflows. I did debug the L1VM with Valgrind to make sure nothing bad happens.