加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.c 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
#include "chibi.h"
// Returns the contents of a given file.
static char *read_file(char *path) {
// Open and read the file.
FILE *fp = fopen(path, "r");
if (!fp)
error("cannot open %s: %s", path, strerror(errno));
int filemax = 10 * 1024 * 1024;
char *buf = malloc(filemax);
int size = fread(buf, 1, filemax - 2, fp);
if (!feof(fp))
error("%s: file too large");
// Make sure that the string ends with "\n\0".
if (size == 0 || buf[size - 1] != '\n')
buf[size++] = '\n';
buf[size] = '\0';
return buf;
}
int main(int argc, char **argv) {
if (argc != 2)
error("%s: invalid number of arguments", argv[0]);
// Tokenize and parse.
filename = argv[1];
user_input = read_file(argv[1]);
token = tokenize();
Program *prog = program();
// Assign offsets to local variables.
for (Function *fn = prog->fns; fn; fn = fn->next) {
int offset = fn->has_varargs ? 56 : 0;
for (VarList *vl = fn->locals; vl; vl = vl->next) {
Var *var = vl->var;
offset = align_to(offset, var->ty->align);
offset += var->ty->size;
var->offset = offset;
}
fn->stack_size = align_to(offset, 8);
}
// Traverse the AST to emit assembly.
codegen(prog);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化