项目作者: BaseMax

项目描述 :
Just a attempt to implement a own bytecode compiler.
高级语言: C
项目地址: git://github.com/BaseMax/own-forth-bytecode-compiler.git
创建时间: 2021-04-22T02:29:09Z
项目社区:https://github.com/BaseMax/own-forth-bytecode-compiler

开源协议:GNU General Public License v3.0

下载


Own Forth Bytecode Compiler

Just a attempt to implement a own bytecode compiler.

A attempt to write a VM/compiler for One Programming Language. It’s a own forth virtual machine (VM).

Date: 2019-2020

Input

Thee cmd.txt is:

  1. 003a 2074 7420 7322 2048 656c 6c6f 2c20
  2. 776f 726c 6421 0a22 203b 0a74 740a 7479
  3. 7065 0a62 7965 0a0a

$ cat cmd.txt

  1. : tt s" Hello, world!
  2. " ;
  3. tt
  4. type
  5. bye

Benchmark

test.c:

  1. #include <stdio.h>
  2. int main(int argc, char const *argv[]) {
  3. printf("Hello, world! *************!\n");
  4. return 0;
  5. }

C program:

  1. $ gcc test.c -o test
  2. $ time ./test
  3. real 0m0.001s
  4. user 0m0.001s
  5. sys 0m0.000s

Own VM:

  1. $ time ./123
  2. real 0m0.003s
  3. user 0m0.003s
  4. sys 0m0.000s

How to debug machine code?

  1. readelf -sW ./123 > readelf.txt
  2. objdump -d 123 > objdump.txt

Figure

Whether additional functions should be distinguished into a separate category or not.
while leaning towards some redundancy.

This is bytecode (structure) for vm.

own forth virtual machine vm bytecode structure


Second step: rewrite functions with new structure

So newfile is: forth-second.c

rewrite own-forth vm with new structure

Out input file is cat prg.bin.

Try in Windows:

  1. x86_64-w64-mingw32-g++ forth.c -static

Try in GNU/Linux:

  1. gcc forth.c -o forth
  2. ./forth prg.bin

Third step:

third step of own forth vm interpreter

  1. $ gcc forh-third.c -o forth
  2. $ ./forth prg-third.bin
  3. Hello World!

I’m working on mnemonics, step by step completing vm:

  1. nop 0x70000000
  2. push 0x80000000 | 0x3FFFFFF ( max ) 0x7FFFFFFF (-min)
  3. add 0x70000202 A,B -> A+B
  4. xor 0x70000502 A,B -> A^B
  5. and 0x70000302 A,B -> A&B
  6. or 0x70000402 A,B -> A|B
  7. invert 0x70000600 A -> ~A
  8. = 0x70000702 A,B -> (A==B)?1:0
  9. < 0x70000802 A,B -> (A<B)?1:0
  10. >r 0x70000046 A -> ,R
  11. r> 0x70000B89 R, -> A
  12. r@ 0x70000B81 R, -> R, A
  13. swap 0x70000180 A,B -> B,A
  14. dup 0x70000001 A -> A,A
  15. drop 0x70000002 A ->
  16. u< 0x70000F02 A,B -> (Abs(A)<Abs(B))?1:0

warn: prg.bin is LittleEndian

Fourth step

Commands:

  1. nop 0x70000000
  2. push 0x80000000 | 0x3FFFFFF ( max ) 0x7FFFFFFF (-min)
  3. dup 0x70000001 A -> A,A
  4. drop 0x70000002 A ->
  5. over 0x70000181 A,B -> A,B,A
  6. rot 0x70001100 A,B,C -> B,C,A
  7. swap 0x70000180 A,B -> B,A
  8. pick 0x70001301 An,An-1,An-2...,n -> An,An-1,An-2...,An
  9. roll 0x70001402 An,An-1,An-2...,n -> An-1,An-2...,An
  10. . 0x40000200 A -> printf("%d ",A)
  11. + 0x70000202 A,B -> A+B
  12. add 0x70000202 A,B -> A+B
  13. - 0x70001502 A,B -> A-B
  14. sub 0x70001502 A,B -> A-B
  15. * 0x70001602 A,B -> A*B
  16. mul 0x70001602 A,B -> A*B
  17. 2mul 0x70001700 A,B -> lo(A*B),hi(A*B)
  18. / 0x70001802 A,B -> A/B
  19. mod 0x70001902 A,B -> mod(A/B)
  20. /mod 0x70001A00 A,B -> mod(A/B), A/B
  21. xor 0x70000502 A,B -> A^B
  22. and 0x70000302 A,B -> A&B
  23. or 0x70000402 A,B -> A|B
  24. invert 0x70000600 A -> ~A
  25. = 0x70000702 A,B -> (A==B)?1:0
  26. < 0x70000802 A,B -> (A<B)?1:0
  27. >r 0x70000046 A -> ,R
  28. r> 0x70000B89 R, -> A
  29. r@ 0x70000B81 R, -> R, A
  30. u< 0x70000F02 A,B -> (uA<uB)?1:0
  31. 1+ 0x70001200 A -> A+1

So result?

own forth virtual machine using pure c

© Copyright 2019 Max Base, ValK