Skip to content

Commit

Permalink
#66: Added description of file format
Browse files Browse the repository at this point in the history
  • Loading branch information
levBagryansky authored Dec 17, 2023
1 parent 80f9e84 commit 853ab23
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,41 @@ $ build/chai <app.chai>
### Chai bytecode currently is just a Sequence of instructions.
For instance, there is disassembler of simple app.chai
```
Ldia 6
Star r2
Ldia 8
Star r3
Ldra r3
Mul r2
Printc acc
Mov o r0
Ret
fn main 8 0:
Ldia 6
Star r2
Ldia 8
Star r3
Ldra r3
Mul r2
Printc acc
Mov 0 r0
Ret
```
Where Ret returns exit code `r0`.
Where Ret returns exit code `r0`. "main" is name of the starting function, `8` is number of registers in it, `0` is number of arguments passing into the function.
This script compiled to chai-bytecode should print "0", because '0' == 48.
## Chai file format
Our file format is similar to jvm ClassFile Structure. Its content is the following:
- Constants count (`imm`)
- Constant[]
- func_info count (`imm`)
- func_info[]

Where `imm` is 2 bytes.

`Constant` has a 1 byte tag that specifies its type(ConstI64, or ConstFuncNameAndType and so on) and the payload then.

`func_info` structure:
```
func_info {
imm accessFlagsCount
imm ConstFuncNameAndType
imm attsCount // Count of attributes. Now only Code attribute exists
imm attNameIndex // Id of attribute, Now can be arbitrary
imm attLen // Size of the Code atribute in bytes
u1 maxRegisters // Number of registers used in function
u1 nargs // Number of arguments to pass
u4 codeLen // Size of code(instructions only) in bytes
u1[] code // Instructions, byte array.
}
```

0 comments on commit 853ab23

Please sign in to comment.