{"id":18773915,"url":"https://github.com/ejunjsh/mycc","last_synced_at":"2025-12-14T02:30:18.373Z","repository":{"id":109691037,"uuid":"252912862","full_name":"ejunjsh/mycc","owner":"ejunjsh","description":"👻 my c compiler","archived":false,"fork":false,"pushed_at":"2020-06-11T05:45:48.000Z","size":210,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-12-29T08:41:59.223Z","etag":null,"topics":["c","cc","compiler","gcc","self-compiling"],"latest_commit_sha":null,"homepage":"","language":"C","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/ejunjsh.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":"2020-04-04T05:00:12.000Z","updated_at":"2023-02-04T08:17:04.000Z","dependencies_parsed_at":"2023-03-13T14:05:05.220Z","dependency_job_id":null,"html_url":"https://github.com/ejunjsh/mycc","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/ejunjsh%2Fmycc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fmycc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fmycc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ejunjsh%2Fmycc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ejunjsh","download_url":"https://codeload.github.com/ejunjsh/mycc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239680985,"owners_count":19679509,"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":["c","cc","compiler","gcc","self-compiling"],"created_at":"2024-11-07T19:36:18.188Z","updated_at":"2025-12-14T02:30:18.331Z","avatar_url":"https://github.com/ejunjsh.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mycc\n\n[![Build Status](https://travis-ci.org/ejunjsh/mycc.svg?branch=master)](https://travis-ci.org/ejunjsh/mycc)\n\n一个c的编译器, 基本代码来自参考里面的代码，修复部分bug，还有加上中文注释😄\n\n基本实现了c的所有语法，应该说是c的一个子集,而且能够自举（自己编译自己）\n\n这个编译器目前只支持linux（有时间看看能不能跑在macos）\n\n## 编译\n\nlinux平台 gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0\n\n    make install\n\n非linux平台\n\n    docker run -it -v $(pwd):/opt/tmp/ --rm -w /opt/tmp gcc make install\n\n详细make命令，可以看[Makefile](https://github.com/ejunjsh/mycc/blob/master/Makefile)\n\n\n## 使用帮助\n\n````shell\n# ./mycc\nUsage: ./mycc [-vcSTM] [-o outfile] file [file ...]\n       -v give verbose output of the compilation stages\n       -c generate object files but don't link them\n       -S generate assembly files but don't link them\n       -T dump the AST trees for each input file\n       -M dump the symbol table for each input file\n       -o outfile, produce the outfile executable file\n````\n\n## 例子\n\n````c\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n\nstruct a{\n    union {\n        int b\n    } c; // 目前只支持嵌套一个struct/union声明，而且只能是第一个成员😭，或者在外面声明好，再到里面用吧😭\n    long k\n};\n\nstruct a xx;\n\nint kk=1;\n\nvoid  main(){\n    int* kkp=\u0026kk;\n    *kkp=*kkp+1;\n    xx.c.b=1;\n    xx.c.b=xx.c.b+1;\n    struct a* sp=\u0026xx;\n    sp-\u003ek=sp-\u003ek+2;\n    printf(\"hello world mycc! test output %d %d %d \\n\",*kkp,xx.c.b,sp-\u003ek);\n}\n````\n\n编译\n\n    ./mycc -o hello examples/helloworld.c # 编译\n    ./hello # 执行\n    hello world mycc! test output 2 2 2 # 输出    \n\n## 自举测试\n\n````shell\n# make clean quad\nrm -f mycc mycc[0-9] *.o *.s out a.out incdir.h\necho \"#define INCDIR \\\"/tmp/include\\\"\" \u003e incdir.h\ncc -o mycc -g -Wall cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\nmkdir -p /tmp/include\ncp -a include/. /tmp/include\ncp mycc /tmp\nchmod +x /tmp/mycc\n./mycc  -o mycc0 cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\n./mycc0 -o mycc1 cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\n./mycc1 -o mycc2 cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\nsize mycc[012]\n   text    data     bss     dec     hex filename\n  97560    2972      48  100580   188e4 mycc0 \n  97560    2972      48  100580   188e4 mycc1\n  97560    2972      48  100580   188e4 mycc2\n````\n\n可以看到自己编译自己的mycc0，mycc1，mycc2大小一致，证明成功\n\n````shell\n# make clean test \nrm -f mycc mycc[0-9] *.o *.s out a.out incdir.h\necho \"#define INCDIR \\\"/tmp/include\\\"\" \u003e incdir.h\ncc -o mycc -g -Wall cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\nmkdir -p /tmp/include\ncp -a include/. /tmp/include\ncp mycc /tmp\nchmod +x /tmp/mycc\n(cd tests; chmod +x runtests; ./runtests)\ninput001.c: OK\ninput002.c: OK\ninput003.c: OK\n# 省略\ninput149.c: OK\ninput150.c: OK\n````\n这个是gnu c 编译器编译出来的mycc的test case成功\n\n````shell\n# make clean test0\nrm -f mycc mycc[0-9] *.o *.s out a.out incdir.h\necho \"#define INCDIR \\\"/tmp/include\\\"\" \u003e incdir.h\ncc -o mycc -g -Wall cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\nmkdir -p /tmp/include\ncp -a include/. /tmp/include\ncp mycc /tmp\nchmod +x /tmp/mycc\n./mycc  -o mycc0 cg.c decl.c expr.c gen.c main.c misc.c opt.c scan.c stmt.c sym.c tree.c types.c\n(cd tests; chmod +x runtests0; ./runtests0)\ninput001.c: OK\ninput002.c: OK\ninput003.c: OK\n# 省略\ninput149.c: OK\ninput150.c: OK\n````\n这个是mycc编译器编译出来的mycc0的test case成功\n\n## 参考\n\nhttps://github.com/DoctorWkt/acwj\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fejunjsh%2Fmycc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fejunjsh%2Fmycc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fejunjsh%2Fmycc/lists"}