{"id":13713599,"url":"https://github.com/noti0na1/socc","last_synced_at":"2025-03-24T05:32:07.905Z","repository":{"id":45647873,"uuid":"133878393","full_name":"noti0na1/socc","owner":"noti0na1","description":"Simple C Compiler in OCaml","archived":false,"fork":false,"pushed_at":"2018-06-02T03:05:48.000Z","size":94,"stargazers_count":92,"open_issues_count":0,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-19T02:11:30.601Z","etag":null,"topics":["assembly","c","compiler","lexer","linux","menhir","ocaml","ocamllex","parser","x64"],"latest_commit_sha":null,"homepage":"","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/noti0na1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-17T23:24:35.000Z","updated_at":"2025-03-04T06:02:34.000Z","dependencies_parsed_at":"2022-09-23T12:41:14.235Z","dependency_job_id":null,"html_url":"https://github.com/noti0na1/socc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noti0na1%2Fsocc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noti0na1%2Fsocc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noti0na1%2Fsocc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noti0na1%2Fsocc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noti0na1","download_url":"https://codeload.github.com/noti0na1/socc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245217103,"owners_count":20579288,"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":["assembly","c","compiler","lexer","linux","menhir","ocaml","ocamllex","parser","x64"],"created_at":"2024-08-02T23:01:40.145Z","updated_at":"2025-03-24T05:32:07.630Z","avatar_url":"https://github.com/noti0na1.png","language":"OCaml","funding_links":[],"categories":["OCaml"],"sub_categories":[],"readme":"# SOCC - Simple C Compiler in OCaml\n\nSOCC is a simple C Compiler written in OCaml using ocamllex and menhir.\n\n## Install\n\nDependences: `core, ppx_jane`\n\n```bash\nopam update\nopam install core ppx_jane\n```\n\n```bash\ngit clone git@github.com:noti0na1/socc.git\ncd socc\nmake\n```\n\nThe executable file is `main.native`\n\n## Usage\n\n```bash\n# use gcc to preprocess the source code\ngcc -E test.c -o test.e.c\n./main.native \u003c test.e.c \u003e test.s\ngcc test.s -o test\n./test\n```\n\n## Example\n\nThere are 4 buildin functions for test:\n\n* `void println(int)`\n* `int readi()`\n* `void *malloc(size_t)`\n* `void free(void *)`\n\n```c\nint main() {\n    int *pi = malloc(sizeof(int));\n    int max = readi();\n    for (int i = 0; i \u003c max; i += 2) {\n        println(i);\n        *pi += i;\n    }\n    println(*pi);\n    free(pi);\n    return 0;\n}\n```\n\n```assembly\n.LC0:\n\t.string \"%d\\n\"\n\t.globl println\nprintln:\n\tmovq\t%rdi, %rsi\n\tmovq\t$0, %rax\n\tmovl\t$.LC0, %edi\n\tjmp\tprintf\n.LC1:\n\t.string \"%d\"\n\t.globl readi\nreadi:\n\tsubq\t$24, %rsp\n\tmovl\t$.LC1, %edi\n\tmovq\t$0, %rax\n\tleaq\t12(%rsp), %rsi\n\tmovl\t$0, 12(%rsp)\n\tcall\t__isoc99_scanf\n\tmovl\t12(%rsp), %eax\n\taddq\t$24, %rsp\n\tret\n\t.globl main\nmain:\n\tpushq\t%rbp\n\tmovq\t%rsp, %rbp\n\tmovq\t$4, %rax\n\tmovq\t%rax, %rdi\n\tcall\tmalloc\n\tpushq\t%rax\n\tcall\treadi\n\tpushq\t%rax\n\tmovl\t$0, %eax\n\tpushq\t%rax\nLmainFORA0:\n\tmovq\t-24(%rbp), %rax\n\tpushq\t%rax\n\tmovq\t-16(%rbp), %rax\n\tmovq\t%rax, %rcx\n\tpopq\t%rax\n\tcmpl\t%ecx, %eax\n\tmovl\t$0, %eax\n\tsetl\t%al\n\tcmpl\t$0, %eax\n\tje\tLmainFORC0\n\tmovq\t-24(%rbp), %rax\n\tmovq\t%rax, %rdi\n\tcall\tprintln\n\tmovq\t-8(%rbp), %rax\n\tpushq\t%rax\n\tmovq\t-8(%rbp), %rax\n\tmovq\t(%rax), %rax\n\tpushq\t%rax\n\tmovq\t-24(%rbp), %rax\n\tmovq\t%rax, %rcx\n\tpopq\t%rax\n\taddl\t%ecx, %eax\n\tmovq\t%rax, %rcx\n\tpopq\t%rax\n\tmovl\t%ecx, (%rax)\n\taddq\t$0, %rsp\nLmainFORB0:\n\tmovq\t-24(%rbp), %rax\n\tpushq\t%rax\n\tmovl\t$2, %eax\n\tmovq\t%rax, %rcx\n\tpopq\t%rax\n\taddl\t%ecx, %eax\n\tmovq\t%rax, -24(%rbp)\n\tjmp\tLmainFORA0\nLmainFORC0:\n\taddq\t$8, %rsp\n\tmovq\t-8(%rbp), %rax\n\tmovq\t(%rax), %rax\n\tmovq\t%rax, %rdi\n\tcall\tprintln\n\tmovq\t-8(%rbp), %rax\n\tmovq\t%rax, %rdi\n\tcall\tfree\n\tmovl\t$0, %eax\n\tleave\n\tret\n\n```\n\nIn: `23`\n\nOut:\n\n```\n0\n2\n4\n6\n8\n10\n12\n14\n16\n18\n20\n22\n132\n```\n\n## Structure\n\n- `socc/src` source code\n    - `ast.ml` abstract syntax tree\n    - `parser.mly` parser\n    - `lexer.mll` lexer\n\t- `context.ml`\n\t- `x64.ml` X64 assembly\n\t- `templib.ml` temporery lib (`println`, `readi`)\n    - `generate.ml` code generator\n\t- `util.ml` utils\n    - `main.ml` program entry\n- `socc/test` some test files\n\n## Note\n\n`Core` is used across the project\n\nThe output assembly is for x64 Linux platform\n\n## TODO\n\n### Short Term\n\n- [ ] follow the C11 to edit lexer\n- [ ] add array type and array operate\n- [ ] implement `++` and `--`\n- [ ] support comments\n- [ ] add more type, implement struct, union, enum\n- [ ] complete type system\n- [ ] optimize stack usage\n- [ ] improve parser further more\n\n### Long Term\n\n- [ ] add error, warning print ...\n- [ ] add semantic check pass\n- [ ] add intermediate lang\n\n## References\n\n[Writing a C Compiler](https://norasandler.com/2017/11/29/Write-a-Compiler.html)\n\n[nlsandler/nqcc](https://github.com/nlsandler/nqcc)\n\n[Parsing with OCamllex and Menhir](https://dev.realworldocaml.org/parsing-with-ocamllex-and-menhir.html)\n\n[Lexer and parser generators (ocamllex, ocamlyacc)](https://caml.inria.fr/pub/docs/manual-ocaml/lexyacc.html)\n\n[x64_cheatsheet](https://cs.brown.edu/courses/cs033/docs/guides/x64_cheatsheet.pdf)\n\n## Licence\n\nSOCC is distributed under the terms of [MIT License](LICENSE)\n\nCopyright (c) 2018 noti0na1\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoti0na1%2Fsocc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoti0na1%2Fsocc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoti0na1%2Fsocc/lists"}