WebAssembly binary file format(WASM.RFH):
Class: Executable and Object, Status: Complete, Last change: 09.02.2024 14:27:02

data
0 ulong Magic
4 ulong Version

assert (Magic=0x6D736100)and(Version=1);

descr ('WebAssembly binary file format.',NL,
  'Info Source: WebAssembly Specification',NL,
  '  Release 2.0 (Draft 2024-01-17)',NL,
  '  ',URL('https://webassembly.github.io/spec/core/index.html'),NL)
  
include utf-8.rfi

function calcLEB(D, V)=V*0x80 + (D and 0x7F);

type

TLEB128u array of byte?@ and 0x80=0;:let Value=FoldR(0,calcLEB,@);:displ=(/*@,'=',*/INT(@:Value))
TLEB128s TLEB128u //!!!Temp

include Float.rfi

type

f32 TSingle
f64 TDouble 

typeidx TLEB128u
funcidx TLEB128u
tableidx TLEB128u
memidx TLEB128u
globalidx TLEB128u
elemidx TLEB128u
dataidx TLEB128u
localidx TLEB128u
labelidx TLEB128u

TTypeCode enum byte (
//limits
  limMin=0x00,
  limMinMax=0x01,
//blocktype
  empty=0x40,//empty block  
//valtype
 //numtype - Number Types
  tcI32=0x7F, //i32
  tcI64=0x7E, //i64
  tcF32=0x7D, //f32
  tcF64=0x7C, //f64
 //vectype - Vector Types
  tcV128=0x7B, //v128
 //reftype - Reference Types
  tcFuncRef=0x70, //funcref
  tcExternRef=0x6F, //externref
 //functype 
  tcFuncType=0x60 
)

limitskind TTypeCode():assert[@ in (limMin, limMinMax, 3)]
numtype TTypeCode():assert[@ in (tcI32,tcI64,tcF32,tcF64)] //i32 | i64 | f32 | f64
vectype TTypeCode():assert[@=TTypeCode.tcV128] //v128
reftype TTypeCode():assert[@ in (tcFuncRef,tcExternRef)] //funcref | externref

valtype TTypeCode():assert[@ in (tcI32,tcI64,tcF32,tcF64,tcV128,tcFuncRef,tcExternRef)] //numtype | vectype | reftype

limits struc
  limitskind lk
  TLEB128u minV
  case @.lk of
   1,3: TLEB128u //3 was observed in real WASM file
  endc maxV
ends

memtype limits

name struc
  TLEB128u len
  TUtf8Array(@.len:Value) S 
ends

resulttype struc
  TLEB128u len
  array[@.len:Value]of valtype T 
ends

functype struc //rt1->rt2
  TTypeCode C //tcFuncType
  resulttype rt1
  resulttype rt2
ends:assert[@.C=TTypeCode.tcFuncType]

tabletype struc
  reftype et
  limits lim
ends

mut enum byte (
  mConst = 0x00,
  mVar = 0x01 
)

globaltype struc
  valtype t
  mut m
ends

impexpdesckind enum byte of (idFunc = 0, idTable = 1, idMem = 2, idGlobal=3)

importdesc struc
  impexpdesckind kind
  case @.kind of
    idFunc: typeidx
    idTable: tabletype
    idMem: memtype
    idGlobal: globaltype
  endc v
ends

import struc
  name mod
  name nm
  importdesc desc
ends 

exportdesc struc
  impexpdesckind kind
  case @.kind of
    idFunc: typeidx
    idTable: tableidx
    idMem: memidx
    idGlobal: globalidx
  endc v
ends

include WASM_OP.rfi

export struc
  name nm
  exportdesc desc
ends

global struc
 globaltype gt
 expr e
ends

vec_byte struc
  TLEB128u len
  raw[@.len:Value] V 
ends

vec_funcidx struc
  TLEB128u len
  array[@.len:Value]of funcidx T 
ends

vec_expr struc
  TLEB128u len
  array[@.len:Value]of expr T 
ends

elemkind byte0 //0x00 => funcref

element struc
  byte kind /*it is u32 in the specifivation*/
  case @.kind of
   0: struc
     expr e
     vec_funcidx y
    ends
   1,3: struc
     elemkind et
     vec_funcidx y
    ends
   2: struc
     tableidx x
     expr e
     elemkind et
     vec_funcidx y
    ends
   4: struc
     expr e
     vec_expr el
    ends
   5,7: struc
     reftype et
     vec_expr el
    ends
   6: struc
     tableidx x
     expr e
     reftype et
     vec_expr el
    ends
  endc e
ends

local struc
  TLEB128u n
  valtype t
ends

locals struc
  TLEB128u len
  array[@.len:Value]of local T 
ends

func(sz) struc
  locals t
  expr_ex(&@+@:sz) e
  raw[] rest
ends:[@:Size=@:Sz]

codepart struc
  TLEB128u size
  func(@.size:Value) cod
ends 

datapartmode enum byte/*it is u32 in the specification*/ of (active0,passive,active1)
datapart struc
  datapartmode mode
  case @.mode of
   active0: expr
   active1: struc
     memidx x
     expr e
    ends
  endc ofs
  vec_byte b
ends

// SECTIONS:

typesection struc
  TLEB128u len
  array[@.len:Value]of functype T 
ends

importsection struc
  TLEB128u len
  array[@.len:Value]of import T 
ends

tablesection struc
  TLEB128u len
  array[@.len:Value]of tabletype T 
ends

memorysection struc
  TLEB128u len
  array[@.len:Value]of memtype T 
ends

globalsection struc
  TLEB128u len
  array[@.len:Value]of global T 
ends

funcsection struc
  TLEB128u len
  array[@.len:Value]of typeidx T 
ends

exportsection struc
  TLEB128u len
  array[@.len:Value]of export T 
ends

elementsection struc
  TLEB128u len
  array[@.len:Value]of element T 
ends

codesection struc
  TLEB128u len
  array[@.len:Value]of codepart T 
ends

datasection struc
  TLEB128u len
  array[@.len:Value]of datapart T 
ends

TSectionId enum byte of (
  secCustom=0,  //custom section
  secType=1,    //type section
  secImport=2,  //import section
  secFunction=3,//function section
  secTable=4,   //table section
  secMemory=5,  //memory section
  secGlobal=6,  //global section
  secExport=7,  //export section
  secStart=8,   //start section
  secElement=9, //element section
  secCode=10,   //code section
  secData=11,   //data section
  secDataCount=12 //data count section
)

TSectionData(id,Sz) struc
  case TSectionId @:id of
   secType: typesection
   secImport: importsection
   secStart: funcidx
   secTable: tablesection
   secMemory: memorysection
   secGlobal: globalsection
   secFunction: funcsection
   secExport: exportsection
   secElement: elementsection 
   secCode: codesection
   secData: datasection
   secDataCount: TLEB128u
  endc D
  raw[] rest
ends:[@:Size=@:Sz]

TSection struc
  TSectionId id
  TLEB128u Size
  TSectionData(@.id,@.Size:Value) Data
ends:autoname=(valname(@.id))

data
8 array of TSection:[@:Size=FileSize-8] Sec


Other specifications.


FlexT home page, Author`s home page.