Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the language JIT compiled (part 1) #106

Merged
merged 71 commits into from
Aug 31, 2021
Merged

Make the language JIT compiled (part 1) #106

merged 71 commits into from
Aug 31, 2021

Conversation

mertyildiran
Copy link
Member

@mertyildiran mertyildiran commented Aug 31, 2021

Related to #105

This PR rewrites the compiler, introduces a JIT abstraction layer (IR) and rewrites the code under vm directory to compile the IR into machine code by using MyJIT as the JIT backend.

This PR;

  • Rewrites the compiler.
  • Introduces a JIT abstraction layer.
  • Improves the performance of the language dramatically.
  • Adds a benchmarking test that compares the performance on recursive fibonacci against PHP, Python and Ruby.
  • Removes compiler_ prefixed GitHub workflows.
  • Increases the number of debug levels:
    • 0: No debugging
    • 1: Print AST and quit
    • 2: Print AST, JIT Abstraction Layer and quit
    • 3: Print AST, JIT Abstraction Layer, JIT_DEBUG_OPS and JIT_DEBUG_CODE
    • 4: Print AST, JIT Abstraction Layer, JIT_DEBUG_OPS, JIT_DEBUG_CODE and JIT_DEBUG_COMBINED
    • 5: Print AST, JIT Abstraction Layer, JIT_DEBUG_OPS, JIT_DEBUG_CODE and the values in the registers, in program runtime
    • 6: Print AST, JIT Abstraction Layer, JIT_DEBUG_COMBINED and the values in the registers, in program runtime
  • Passes only a portion of the tests: number, del, decision, spell, echo, boolean, exit_var, exit_int, pretty, string, dict, print, underscore, samedirlib, syntax_error, nonewline, immutability, quit, operator, list, exit and any.
  • Breaks the interactive shell completely.
  • Breaks the binary build (formerly known as "compiler").
  • Adds myjit as a Git submodule.
  • Does changes in the Makefile to separate compilation and linking.
  • Breaks the Chaos C extension API.

Note: Do a git reset --hard 21d679a7ab2d83896968c387dfdd4c3d75407fca to the state before this PR. The commit is titled Include printing the heap into debug level 3 and with the date Sat Jun 26 00:21:20 2021 +0300.

Example of JIT Abstraction Layer, JIT_DEBUG_OPS and JIT_DEBUG_CODE

dev.kaos:

print 42

Once it's run with chaos -d 3 dev.kaos it prints:

Abstract Syntax Tree (AST):
{
    "_type": "Program",
    "files": [
        {
            "_type": "File",
            "imports": [],
            "stmt_list": [
                {
                    "_type": "PrintStmt",
                    "mod": null,
                    "x": {
                        "_type": "BasicLit",
                        "value_type": "int",
                        "value": "42"
                    }
                }
            ]
        }
    ]
}

JIT Abstraction Layer:
Counter                                  Instructions                             Line                                     File                                                                             Line Number                             
1                                        MAIN_PROLOG                              (null)                                   (null)                                                                           0                                       
2                                        MOVI R(0) 1                              print 42                                 dev.kaos                                                                         1                                       
3                                        MOVI R(1) 42                             print 42                                 dev.kaos                                                                         1                                       
4                                        DYN_PRNT                                 print 42                                 dev.kaos                                                                         1                                       
5                                        RETI 0                                   print 42                                 dev.kaos                                                                         1                                       
6                                        HLT                                      print 42                                 dev.kaos                                                                         1                                       


JIT Runtime:
uninitialized register(s):  fr1 at function `execute' (vm/cpu.c:128)
    prolog   0x564c6b33dce8
unreachable operation at function `run_cpu' (vm/cpu.c:76)
    reti     0x0

>>>>>>>>>> JIT_DEBUG_OPS <<<<<<<<<                                   
    prolog   0x564c6b33dce8        
    movi     r0, 0x1               
    movi     r1, 0x2a              
    movi     r3, 0x564c6b2e36a7    
                                   
                                   
                                   
    prepare                        
    putargr  r0                    
    putargr  r1                    
    fputargr (qword) fr1           
    putargi  0x1                   
    putargi  0x0                   
    callr    r3                    
    reti     0x0                   
>>>>>>>>>> JIT_DEBUG_OPS <<<<<<<<<

myjitzU4aS9:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
   0:	55                   	push   rbp
   1:	48 8b ec             	mov    rbp,rsp
   4:	48 83 ec 20          	sub    rsp,0x20
   8:	48 bf 01 00 00 00 00 	movabs rdi,0x1
   f:	00 00 00 
  12:	48 be 2a 00 00 00 00 	movabs rsi,0x2a
  19:	00 00 00 
  1c:	48 ba a7 36 2e 6b 4c 	movabs rdx,0x564c6b2e36a7
  23:	56 00 00 
  26:	48 89 7d f8          	mov    QWORD PTR [rbp-0x8],rdi
  2a:	48 89 75 f0          	mov    QWORD PTR [rbp-0x10],rsi
  2e:	48 89 55 e0          	mov    QWORD PTR [rbp-0x20],rdx
  32:	48 b9 00 00 00 00 00 	movabs rcx,0x0
  39:	00 00 00 
  3c:	48 ba 01 00 00 00 00 	movabs rdx,0x1
  43:	00 00 00 
  46:	66 0f 12 45 d0       	movlpd xmm0,QWORD PTR [rbp-0x30]
  4b:	48 8b 75 f0          	mov    rsi,QWORD PTR [rbp-0x10]
  4f:	48 8b 7d f8          	mov    rdi,QWORD PTR [rbp-0x8]
  53:	b8 01 00 00 00       	mov    eax,0x1
  58:	ff 55 e0             	call   QWORD PTR [rbp-0x20]
  5b:	48 b8 00 00 00 00 00 	movabs rax,0x0
  62:	00 00 00 
  65:	48 8b e5             	mov    rsp,rbp
  68:	5d                   	pop    rbp
  69:	c3                   	ret    
42

…and a function to debug the registers inside JIT
…HR`, and `RSHI` opcodes to JIT abstraction layer
…nstructions from the Dynamic Instructions
…` opcodes

Also rename `PRNT` opcode to `DYN_PRINT`.
…DYN_LER` and `DYN_LNOT` opcodes and pass the operator tests
@mertyildiran mertyildiran added feature request New feature or request performance Performance issues or improvements labels Aug 31, 2021
@mertyildiran mertyildiran self-assigned this Aug 31, 2021
@mertyildiran mertyildiran merged commit 5c38e46 into master Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request performance Performance issues or improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant