文件
Paze AI 251ee470bf feat: Paze C Compiler - 完整改造自 tcc 0.9.28rc
- 所有 tcc 文件/内容/宏改名为 pcc (tcc.c->pcc.c, tccelf.c->pccelf.c, libtcc.c->libpcc.c 等)
- 支持架构: i386, x86_64, ARM, ARM64, RISC-V, C67
- 支持格式: PE, ELF, Mach-O, COFF
- 完整预处理、代码生成、链接器、调试信息
- 构建成功: pcc.exe (x86_64 Windows)
- 功能测试通过: 递归/结构体/浮点/switch/循环
- 自编译测试通过: pcc 可以编译自身
2026-08-16 13:30:01 +08:00

104 行
2.4 KiB
C

/*
* Simple Test program for libpcc
*
* libpcc can be useful to use pcc as a "backend" for a code generator.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "libpcc.h"
void handle_error(void *opaque, const char *msg)
{
fprintf(opaque, "%s\n", msg);
}
/* this function is called by the generated code */
int add(int a, int b)
{
return a + b;
}
/* this strinc is referenced by the generated code */
const char hello[] = "Hello World!";
char my_program[] =
"#include <pcclib.h>\n" /* include the "Simple libc header for PCC" */
"extern int add(int a, int b);\n"
"#ifdef _WIN32\n" /* dynamically linked data needs 'dllimport' */
" __attribute__((dllimport))\n"
"#endif\n"
"extern const char hello[];\n"
"int fib(int n)\n"
"{\n"
" if (n <= 2)\n"
" return 1;\n"
" else\n"
" return fib(n-1) + fib(n-2);\n"
"}\n"
"\n"
"int foo(int n)\n"
"{\n"
" printf(\"%s\\n\", hello);\n"
" printf(\"fib(%d) = %d\\n\", n, fib(n));\n"
" printf(\"add(%d, %d) = %d\\n\", n, 2 * n, add(n, 2 * n));\n"
" return 0;\n"
"}\n";
int main(int argc, char **argv)
{
PCCState *s;
int i;
int (*func)(int);
s = pcc_new();
if (!s) {
fprintf(stderr, "Could not create pcc state\n");
exit(1);
}
/* set custom error/warning printer */
pcc_set_error_func(s, stderr, handle_error);
/* if pcclib.h and libpcc1.a are not installed, where can we find them */
for (i = 1; i < argc; ++i) {
char *a = argv[i];
if (a[0] == '-') {
if (a[1] == 'B')
pcc_set_lib_path(s, a+2);
else if (a[1] == 'I')
pcc_add_include_path(s, a+2);
else if (a[1] == 'L')
pcc_add_library_path(s, a+2);
}
}
/* MUST BE CALLED before any compilation */
pcc_set_output_type(s, PCC_OUTPUT_MEMORY);
if (pcc_compile_string(s, my_program) == -1)
return 1;
/* as a test, we add symbols that the compiled program can use.
You may also open a dll with pcc_add_dll() and use symbols from that */
pcc_add_symbol(s, "add", add);
pcc_add_symbol(s, "hello", hello);
/* relocate the code */
if (pcc_relocate(s) < 0)
return 1;
/* get entry symbol */
func = pcc_get_symbol(s, "foo");
if (!func)
return 1;
/* run the code */
func(32);
/* delete the state */
pcc_delete(s);
return 0;
}