From 853ab23868346b052685f8101ff8660292b57ac2 Mon Sep 17 00:00:00 2001 From: Lev Bagryansky <32939651+levBagryansky@users.noreply.github.com> Date: Mon, 18 Dec 2023 01:35:51 +0300 Subject: [PATCH] #66: Added description of file format --- README.md | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f7424d1..7fd20e8 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,41 @@ $ build/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. +} +```