https://gitlab.com/brunowonder/TinyVM
License: GNU General Public License v3 (GPL-3)
Development screenshot
TinyVM is an experimental Virtual Machine implementation written in C.
//VM control
HALT
PAUSE
//Stack / Register management
PUSH
PUSHINT
PUSHFLOAT
POP
CLR
SET
MOV
SWAP
RSP
//Int64 stack operations
ADD
SUB
MUL
DIV
MOD
POWER
SQRT
CHS
TOFLOAT
//Float64 stack operations
FADD
FSUB
FMUL
FDIV
FPOWER
FSQRT
FCHS
FCMP
TOINT
//Logical stack operations
AND
OR
NEG
RSHIFT
LSHIFT
XOR
CMP
//Program flow control
JMP
JNE
JEQ
JG
JL
JGE
JLE
//Int64 register / register operations
RADD
RSUB
RMUL
RDIV
RMOD
RPOWER
RSQRT
RCHS
RCMP
RINC
RDEC
RTOFLOAT
//Int64 register / value operations
RCMPV
RADDV
RSUBV
RMULV
RDIVV
RMODV
RPOWERV
//Float64 register / register operations
RFADD
RFSUB
RFMUL
RFDIV
RFPOWER
RFSQRT
RFCHS
RFCMP
RTOINT
//Float64 register / value operations
RFCMPV
RFADDV
RFSUBV
RFMULV
RFDIVV
RFPOWERV
//Logical operations [Register vs Register]
RAND
ROR
RNEG
RRSHIFT
RLSHIFT
RXOR
//Logical operations [Register vs Value]
RANDV
RORV
RXORV
HALT
PAUSE
//Stack / Register management
PUSH
PUSHINT
PUSHFLOAT
POP
CLR
SET
MOV
SWAP
RSP
//Int64 stack operations
ADD
SUB
MUL
DIV
MOD
POWER
SQRT
CHS
TOFLOAT
//Float64 stack operations
FADD
FSUB
FMUL
FDIV
FPOWER
FSQRT
FCHS
FCMP
TOINT
//Logical stack operations
AND
OR
NEG
RSHIFT
LSHIFT
XOR
CMP
//Program flow control
JMP
JNE
JEQ
JG
JL
JGE
JLE
//Int64 register / register operations
RADD
RSUB
RMUL
RDIV
RMOD
RPOWER
RSQRT
RCHS
RCMP
RINC
RDEC
RTOFLOAT
//Int64 register / value operations
RCMPV
RADDV
RSUBV
RMULV
RDIVV
RMODV
RPOWERV
//Float64 register / register operations
RFADD
RFSUB
RFMUL
RFDIV
RFPOWER
RFSQRT
RFCHS
RFCMP
RTOINT
//Float64 register / value operations
RFCMPV
RFADDV
RFSUBV
RFMULV
RFDIVV
RFPOWERV
//Logical operations [Register vs Register]
RAND
ROR
RNEG
RRSHIFT
RLSHIFT
RXOR
//Logical operations [Register vs Value]
RANDV
RORV
RXORV
Compiling this lib with NLG 3.00+:
1. Copy-paste the TinyVM project path into the "C/C++ Project" field in NLG and click "Load".
2. Click "Generate B4A Library"
The library source files used for compilation are 'TinyVM.cpp' and 'TinyVM.h'.
Tiny example:
B4X:
Dim VM as TinyVM
Dim VMStack() as Long
VM.Initialize
VM.SetStackLength(1024)
VM.Compile(File.ReadString(File.DirAssets, "primes.asm"))
VM.Execute : VMStack = VM.StackAsLongArray
VM.Kill
For Each entry As Long In VMStack
Log(entry)
Next
';---------------------------------------------------------------------
';PROGRAM: primes.asm
';---------------------------------------------------------------------
'START:
' set 0 r1 ; a = 0
'LOOP_0: ; do {
' rcmpv r1,1000 ; if (a >= 1,000) {
' jeq END ; goto end }
' rcmpv r1,2 ; if (a == 2) {
' jeq RETURN_1 ; goto return true}
' rcmpv r1,1 ; if (a <= 1) {
' jle RETURN_0 ; goto return false }
' rmodv r1,2 r3 ; r3 = (a % 2)
' rcmpv r3,0 ; if (r3 == 0) {
' jeq RETURN_0 ; goto return false }
' rsqrt r1 r4 ; max = sqrt(a)
' set 3 r2 ; n = 3
'LOOP_1: ; do {
' rcmp r2,r4 ; if (n > max) {
' jg RETURN_1 ; goto return true }
' rmod r1,r2 r3 ; res = (a % n)
' rcmpv r3,0 ; if (res == 0) {
' jeq RETURN_0 ; goto return false }
' rinc r2,2 ; n += 2
' jmp LOOP_1 ; } goto loop 1
' ; }
';---------------------------------------------------------------------
'RETURN_0:
' rinc r1,1 ; i++;
' jmp LOOP_0 ; goto loop 0
';---------------------------------------------------------------------
'RETURN_1:
' push r1 ; stack.push(r1)
' rinc r1,1 ; i++;
' jmp LOOP_0 ; goto loop_0
';---------------------------------------------------------------------
'END:
';---------------------------------------------------------------------
Attachments
Last edited: