Skip to content

Commit

Permalink
#55: Specified isa
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky authored Nov 2, 2023
1 parent 0950230 commit 2099e0a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
49 changes: 49 additions & 0 deletions ISA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
There is ChaiVM's accumulator(acc) based ISA.

| Operation | Format | Description |
|:---------:|:------:|:------------|
| Nop | N | Does nothing |
| Ret | N | Returns a value via acc |
| Mov | RR | i64, moves value from ri to rj |
| Ldia | R | i64, loads constant [i] to acc |
| Ldra | R | i64, loads the R1 to acc |
| Star | R | i64, copies val of acc into register ri |
| Add | R | i64, Adds R1 to acc |
| Addi | I | i64, Adds [imm] to acc |
| Sub | R | i64, sub R1 from acc |
| Subi | I | i64, sub [imm] from acc |
| Mul | R | i64, mul acc by R1 |
| Muli | I | i64, mul acc by [imm] |
| Div | R | i64, divides acc by R1 |
| Divi | I | i64, divides acc by [imm] |
| Ldiaf | R | f64, loads constant [i] to acc |
| Addf | R | f64, Adds R1 to acc |
| Addif | I | f64, Adds [imm] to acc |
| Subf | R | f64, sub R1 from acc |
| Subif | I | i64, sub [imm] from acc |
| Mulf | R | f64, mul acc by R1 |
| Mulif | I | f64, mul acc by [imm] |
| Divf | R | f64, divides acc by R1 |
| Divif | I | f64, divides acc by [imm] |
| IcPrint | N | Intrinsic. Prints char, containing by the first byte of acc |
| IcScani | N | Intrinsic. Scans i64 from stdin |
| IcScanf | N | Intrinsic. Scans f64 from stdin |
| IcSqrt | N | f64, Intrinsic. Calculates sqrt of acc. |
| IcSin | N | f64, Intrinsic. Calculates sin of acc. |
| IcCos | N | f64, Intrinsic. Calculates cos of acc. |
| If_icmpeq | N | i64, if acc == r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_icmpne | RI | i64, if acc != r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_icmpgt | RI | i64, if acc > r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_icmpge | RI | i64, if acc >= r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_icmplt | RI | i64, if acc < r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_icmple | RI | i64, if acc <= r1 then branch to instruction at offset [imm] otherwise just next instr |
| If_acmpeq | RI | ref, if references are equal, branch to instruction at offset [imm] |
| If_acmpne | RI | ref, if references are not equal, branch to instruction at offset [imm] |
| Сmpgf | R | f64 -> i64, compare acc with r1. Acc became 1 i64 if greater than r1, 0 if equal, otherwise -1 |
| Cmplf | R | f64 -> i64, compare acc with r1. Acc became 1 i64 if less than r1, 0 if equal, otherwise -1 |
| Goto | I | Goes to another instruction at branchoffset [imm] |

To generate this file use the following python script:
```shell
$ python3 tools/isa-spec.py
```
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ChaiVM - Virtual machine for chai bytecode.
You can read more about chai isa [here](ISA.md)
## How to build:
```shell
$ make build
Expand Down
40 changes: 40 additions & 0 deletions tools/isa-spec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sys
import jinja2
import yaml
import os


def main() -> int:
path_to_file = os.path.dirname(os.path.realpath(__file__)) + "/resources/instructions.yml"
with open(path_to_file) as file:
full = yaml.safe_load(file)
instrs = full["instructions"]
tpl = """There is ChaiVM's accumulator(acc) based ISA.
| Operation | Format | Description |
|:---------:|:------:|:------------|
{% for n, item in enumerate(items, 1) %}
| {{ item.mnemonic }} | {{ item.format }} | {{ item.description }} |
{% endfor %}
To generate this file use the following python script:
```shell
$ python3 tools/isa-spec.py
```
"""
content = {
'items': instrs,
'enumerate': enumerate,
}
fp = open(os.path.dirname(os.path.realpath(__file__)) + "/../ISA.md", 'w')
fp.write(
jinja2
.Template(tpl, trim_blocks=True)
.render(content)
)
fp.close()
return 0


if __name__ == '__main__':
sys.exit(main())
40 changes: 40 additions & 0 deletions tools/resources/instructions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,160 +15,200 @@ instructions:
fixedvalue: 1
mnemonic: Nop
format: N
description: "Does nothing"
-
fixedvalue: 2
mnemonic: Ret
format: N
description: "Returns a value via acc"
-
fixedvalue: 3
mnemonic: Mov
format: RR
description: "i64, moves value from ri to rj"
-
fixedvalue: 4
mnemonic: Ldia
format: R
description: "i64, loads constant [i] to acc"
-
fixedvalue: 5
mnemonic: Ldra
format: R
description: "i64, loads the R1 to acc"
-
fixedvalue: 6
mnemonic: Star
format: R
description: "i64, copies val of acc into register ri"
-
fixedvalue: 7
mnemonic: Add
format: R
description: "i64, Adds R1 to acc"
-
fixedvalue: 8
mnemonic: Addi
format: I
description: "i64, Adds [imm] to acc"
-
fixedvalue: 9
mnemonic: Sub
format: R
description: "i64, sub R1 from acc"
-
fixedvalue: 10
mnemonic: Subi
format: I
description: "i64, sub [imm] from acc"
-
fixedvalue: 11
mnemonic: Mul
format: R
description: "i64, mul acc by R1"
-
fixedvalue: 12
mnemonic: Muli
format: I
description: "i64, mul acc by [imm]"
-
fixedvalue: 13
mnemonic: Div
format: R
description: "i64, divides acc by R1"
-
fixedvalue: 14
mnemonic: Divi
format: I
description: "i64, divides acc by [imm]"
-
fixedvalue: 15
mnemonic: Ldiaf
format: R
description: "f64, loads constant [i] to acc"
-
fixedvalue: 16
mnemonic: Addf
format: R
description: "f64, Adds R1 to acc"
-
fixedvalue: 17
mnemonic: Addif
format: I
description: "f64, Adds [imm] to acc"
-
fixedvalue: 18
mnemonic: Subf
format: R
description: "f64, sub R1 from acc"
-
fixedvalue: 19
mnemonic: Subif
format: I
description: "i64, sub [imm] from acc"
-
fixedvalue: 20
mnemonic: Mulf
format: R
description: "f64, mul acc by R1"
-
fixedvalue: 21
mnemonic: Mulif
format: I
description: "f64, mul acc by [imm]"
-
fixedvalue: 22
mnemonic: Divf
format: R
description: "f64, divides acc by R1"
-
fixedvalue: 23
mnemonic: Divif
format: I
description: "f64, divides acc by [imm]"
-
fixedvalue: 24
mnemonic: IcPrint
format: N
description: "Intrinsic. Prints char, containing by the first byte of acc"
-
fixedvalue: 25
mnemonic: IcScani
format: N
description: "Intrinsic. Scans i64 from stdin"
-
fixedvalue: 26
mnemonic: IcScanf
format: N
description: "Intrinsic. Scans f64 from stdin"
-
fixedvalue: 27
mnemonic: IcSqrt
format: N
description: "f64, Intrinsic. Calculates sqrt of acc."
-
fixedvalue: 28
mnemonic: IcSin
format: N
description: "f64, Intrinsic. Calculates sin of acc."
-
fixedvalue: 29
mnemonic: IcCos
format: N
description: "f64, Intrinsic. Calculates cos of acc."
-
fixedvalue: 30
mnemonic: If_icmpeq
format: N
description: "i64, if acc == r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 31
mnemonic: If_icmpne
format: RI
description: "i64, if acc != r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 32
mnemonic: If_icmpgt
format: RI
description: "i64, if acc > r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 33
mnemonic: If_icmpge
format: RI
description: "i64, if acc >= r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 34
mnemonic: If_icmplt
format: RI
description: "i64, if acc < r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 35
mnemonic: If_icmple
format: RI

description: "i64, if acc <= r1 then branch to instruction at offset [imm] otherwise just next instr"
-
fixedvalue: 36
mnemonic: If_acmpeq
format: RI
description: "ref, if references are equal, branch to instruction at offset [imm]"
-
fixedvalue: 37
mnemonic: If_acmpne
format: RI
description: "ref, if references are not equal, branch to instruction at offset [imm]"
-
fixedvalue: 38
mnemonic: Сmpgf
format: R
description: "f64 -> i64, compare acc with r1. Acc became 1 i64 if greater than r1, 0 if equal, otherwise -1"
-
fixedvalue: 39
mnemonic: Cmplf
format: R
description: "f64 -> i64, compare acc with r1. Acc became 1 i64 if less than r1, 0 if equal, otherwise -1"
-
fixedvalue: 40
mnemonic: Goto
format: I
description: "Goes to another instruction at branchoffset [imm]"

0 comments on commit 2099e0a

Please sign in to comment.