{"id":16672165,"url":"https://github.com/vincentdchan/ocaml-binaryen-dsl","last_synced_at":"2025-04-09T19:53:11.539Z","repository":{"id":82248360,"uuid":"399154034","full_name":"vincentdchan/ocaml-binaryen-dsl","owner":"vincentdchan","description":"Write WAT DSL in OCaml.","archived":false,"fork":false,"pushed_at":"2023-07-17T12:49:11.000Z","size":11,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T21:51:53.660Z","etag":null,"topics":["binaryen","bindings","dsl","ocaml","webassembly"],"latest_commit_sha":null,"homepage":"","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vincentdchan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-23T15:24:50.000Z","updated_at":"2024-11-04T23:41:12.000Z","dependencies_parsed_at":"2025-02-15T18:42:15.413Z","dependency_job_id":null,"html_url":"https://github.com/vincentdchan/ocaml-binaryen-dsl","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vincentdchan%2Focaml-binaryen-dsl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vincentdchan%2Focaml-binaryen-dsl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vincentdchan%2Focaml-binaryen-dsl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vincentdchan%2Focaml-binaryen-dsl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vincentdchan","download_url":"https://codeload.github.com/vincentdchan/ocaml-binaryen-dsl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103916,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binaryen","bindings","dsl","ocaml","webassembly"],"created_at":"2024-10-12T12:05:27.511Z","updated_at":"2025-04-09T19:53:11.531Z","avatar_url":"https://github.com/vincentdchan.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# OCaml Binaryen DSL\n\n\nThis library helps you to write Webassembly text format(WAT) in OCaml source code with DSL. It helps you to write a code generator for your language to WASM.\n\nYou can generate it with [Binaryen](https://github.com/WebAssembly/binaryen).\n\nExample:\n\n```OCaml\nopen Binaryen_dsl.Dsl\n\nlet codegen_retain_object m =\n  let module Dsl =\n    Binaryen(struct\n      let ptr_ty = i32\n      let m = m\n    end)\n  in\n  let open Dsl in\n  let _ = def_function \"retain_counter\" ~params:[ ptr_ty ] ~ret_ty:none (fun ctx -\u003e\n  let next_count = def_local ctx i32 in\n  block [\n    (* next_count \u003c- obj-\u003estrong_counter *)\n    local_set next_count (I32.load ~offset:8 (Ptr.local_get 0));\n\n    (* next_count \u003c- next_count + 1 *)\n    local_set next_count I32.((local_get next_count) + (const_i32_of_int 1));\n\n    (* obj-\u003estrong_counter \u003c- next_count *)\n    I32.store ~offset:8 ~ptr:(Ptr.local_get 0) (I32.local_get next_count);\n\n  ]\n  )\n  in\n  let _ = export_function \"retain_counter\" \"retain_counter\" in ()\n\nlet () = \n  let m = module_create() in\n  codegen_retain_object m;\n  print_endline (emit_text m)\n```\n\nGenerating WAT:\n```wat\n(module\n (type $i32_=\u003e_none (func (param i32)))\n (export \"retain_counter\" (func $retain_counter))\n (func $retain_counter (param $0 i32)\n  (local $1 i32)\n  (local.set $1\n   (i32.load offset=8\n    (local.get $0)\n   )\n  )\n  (local.set $1\n   (i32.add\n    (local.get $1)\n    (i32.const 1)\n   )\n  )\n  (i32.store offset=8\n   (local.get $0)\n   (local.get $1)\n  )\n )\n)\n```\n\n# Install\n\n## Before\n\nBefore intalling, make sure you have install [Binaryen](https://github.com/WebAssembly/binaryen).\n\n### Install Binaryen on macOS\n\n```bash\nbrew install binaryen\n```\n\n## Install from source\n\nClone the repo and type:\n\n``` bash\nopam install .\n```\n\n## Install from opam\n\n```bash\nopam install binaryen_dsl\n```\n\n# Usage\n\n## Creating a module\n\n```OCaml\nlet m = module_create()\n```\n\n## Provide the pointer type and module\n\nIf you want to generate wasm32, make your pointer a `i32`,\n`i64` for wasm64.\n\n``` OCaml\nlet module Dsl =\n  Binaryen(struct\n    let ptr_ty = i32\n    let m = m\n  end)\nin\nlet open Dsl in\n```\n\nWhen you open the `Dsl`, all the WAT sematics will be exposed.\n\nIf you want to express `i32.load`, then write `I32.load`.\nMost of them are very similar.\n\n## Emit Code\n\nYou can emit binary code to a file or just emit WAT.\n\n```OCaml\nval emit_binary: module_ -\u003e string -\u003e unit\nval emit_text: module_ -\u003e string\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvincentdchan%2Focaml-binaryen-dsl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvincentdchan%2Focaml-binaryen-dsl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvincentdchan%2Focaml-binaryen-dsl/lists"}