{"id":19297266,"url":"https://github.com/ShinyaKato/sk2cc","last_synced_at":"2025-04-22T08:31:26.448Z","repository":{"id":102287071,"uuid":"140147537","full_name":"ShinyaKato/sk2cc","owner":"ShinyaKato","description":"Simple C compiler developed by @ShinyaKato","archived":false,"fork":false,"pushed_at":"2019-08-24T16:01:05.000Z","size":560,"stargazers_count":43,"open_issues_count":0,"forks_count":4,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-09T23:02:26.185Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","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/ShinyaKato.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-08T07:40:43.000Z","updated_at":"2024-08-17T08:27:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"48f09754-5edd-4834-be65-ebec555c177c","html_url":"https://github.com/ShinyaKato/sk2cc","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/ShinyaKato%2Fsk2cc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShinyaKato%2Fsk2cc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShinyaKato%2Fsk2cc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShinyaKato%2Fsk2cc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShinyaKato","download_url":"https://codeload.github.com/ShinyaKato/sk2cc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250206186,"owners_count":21392205,"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":[],"created_at":"2024-11-09T23:01:49.886Z","updated_at":"2025-04-22T08:31:26.435Z","avatar_url":"https://github.com/ShinyaKato.png","language":"C","funding_links":[],"categories":["C"],"sub_categories":[],"readme":"# sk2cc\n\nsk2cc is a compiler for the subset of the C language.\nIt aims to support almost all language features of C11 while keeping the implementation as simple as possible.\nFor example, in lexical analyzer and parser, all code is hand-written, parser generators such as lex/flex and yacc/bison are not used.\nSo you can easily understand the implementation.\n\nsk2cc has been achieved self-hosting, which means we can compile the source code of sk2cc with sk2cc itself.\nYou can understand what features are supported by sk2cc by reading its implementation because the all written code can be compiled with sk2cc.\n\nIn sk2cc's code generation, we succeeded in simplifying register allocation by pushing all intermediate calculation results onto the stack.\nFor example, when calculating `1 + 2`, sk2cc pushes `1` and `2` onto the stack, and then these values are popped into the registers.\nThen two values are added, and resulting value is pushed onto the stack again.\nWhile the register allocation is easy, the execution speed of generated assembly is slow.\n\nsk2cc also includes the assembly, so you can generate object file from assembly source code.\nHowever, sk2cc can not link object files and generate executable file, and the existing linker is needed for linking.\nYou can link object files by simply passing them gcc.\n\n\n## Build\n\nYou can build sk2cc with make command:\n\n```bash\nmake\n```\n\nThis repository contains test script of sk2cc.\nWe check the self-hosted executable (self) and the executable generated by self-hosted executable (self2) in addition to the executable generated by gcc (sk2cc).\nYou can run all test suites:\n\n```bash\nmake test\n```\n\n\n## Usage\n\nsk2cc recieves path to a source file and generates assembly to stdout.\nsk2cc also includes assembler, and can convert assmebly source code to object file.\nTo generate executable, use gcc with static link.\nThe example of compiling the sample programs is as follows:\n\n```c\n// hello.c\nint printf(char *format, ...);\n\nint main() {\n  printf(\"Hello World\\n\");\n  return 0;\n}\n```\n\n```bash\n# compile\n./sk2cc hello.c \u003e hello.s\n\n# assemble\n./sk2cc --as hello.s hello.o\n\n# link with gcc\ngcc -static hello.o -o hello\n\n# execution\n./hello\n```\n\n\n## Example\n\nFor example, sk2cc can compile the following program to find fibonacci numbers recursively:\n\n```c\nint printf(char *format, ...);\n\nint fib(int n) {\n  if (n == 0) return 0;\n  if (n == 1) return 1;\n  return fib(n - 1) + fib(n - 2);\n}\n\nint main() {\n  for (int i = 0; i \u003c 20) {\n    printf(\"%d: %d\\n\", i, fib(i));\n  }\n  return 0;\n}\n```\n\nFor another example, sk2cc can convert the following assembly program to print `Hello World`:\n\n```asm\n  .section .rodata\n.S0:\n  .ascii \"Hello World\\n\\0\"\n  .data\n  .global hello\nhello:\n  .quad .S0\n  .text\n  .global main\nmain:\n  pushq %rbp\n  movq %rsp, %rbp\n  movq hello(%rip), %rdi\n  xorl %eax, %eax\n  call printf\n  leave\n  ret\n```\n\nFurthermore, the sk2cc source code itself can also be used as a compilable program.\n\n\n## Author\n\nThis compiler is developed by Shinya Kato (Twitter: 0x19f).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShinyaKato%2Fsk2cc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FShinyaKato%2Fsk2cc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FShinyaKato%2Fsk2cc/lists"}