1. -g 调试信息 (DWARF5) - config.h 启用 CONFIG_DWARF_VERSION 5 - 生成 .debug_info/.debug_line/.debug_str 等现代调试节 - 函数/变量/行号信息完整 (GDB/VS Code 可调试) 2. 代码优化器验证 - 常量折叠: 2+3 → movl \ (编译期计算) - 死代码消除: if(0) 分支移除 - 不可达代码: i>1000 分支消除 - __OPTIMIZE__ 宏正确设置 3. C23 新特性 (-std=c2x/-std=c23) - __STDC_VERSION__ = 202311L - nullptr 关键字 + nullptr_t 类型 - typeof 泛型 (已有 GNU 扩展) 4. libpcc JIT 演示 (examples/jit_demo.c) - 运行时编译 C 代码字符串 - pcc_set_output_type(MEMORY) JIT 模式 - 调用宿主函数/访问宿主变量 - 验证: jit_run(10) 返回 55 5. VS Code 插件打包 - 修复 package.json 结构 (contributes 嵌套) - 生成 pcc-language-support-0.1.0.vsix (5.9 KB) - 语法高亮 + 构建/运行命令
110 行
2.8 KiB
C
110 行
2.8 KiB
C
/*
|
|
* jit_demo.c - libpcc JIT demonstration
|
|
*
|
|
* Uses libpcc.dll to compile C code at runtime, relocate it
|
|
* to executable memory, and call a JIT-compiled function.
|
|
* This turns PCC into an embedded scripting engine.
|
|
*
|
|
* Build (from bin/):
|
|
* pcc ..\examples\jit_demo.c -I .. -L . -lpcc -o jit_demo.exe
|
|
* (libpcc.dll must be in PATH or same dir)
|
|
*
|
|
* License: MIT
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "libpcc.h"
|
|
|
|
/* error/warning printer */
|
|
static void handle_error(void *opaque, const char *msg)
|
|
{
|
|
fprintf(opaque, "%s\n", msg);
|
|
}
|
|
|
|
/* a host function callable from JIT-compiled code */
|
|
static int c_host_add(int a, int b)
|
|
{
|
|
return a + b;
|
|
}
|
|
|
|
/* a host global visible to JIT code */
|
|
static const char host_message[] = "Hello from host!";
|
|
|
|
/* JIT code as a string */
|
|
static const char *jit_code =
|
|
"#include <pcclib.h>\n"
|
|
"extern int c_host_add(int a, int b);\n"
|
|
"#ifdef _WIN32\n"
|
|
" __attribute__((dllimport))\n"
|
|
"#endif\n"
|
|
"extern const char host_message[];\n"
|
|
"int jit_run(int n)\n"
|
|
"{\n"
|
|
" int sum = 0;\n"
|
|
" for (int i = 1; i <= n; i++) sum += i;\n"
|
|
" printf(\"%s\\n\", host_message);\n"
|
|
" printf(\"JIT: c_host_add(%d, %d) = %d\\n\", n, 7, c_host_add(n, 7));\n"
|
|
" printf(\"JIT: sum(1..%d) = %d\\n\", n, sum);\n"
|
|
" return sum;\n"
|
|
"}\n";
|
|
|
|
int main(void)
|
|
{
|
|
PCCState *s;
|
|
int (*jit_run)(int);
|
|
|
|
printf("=== libpcc JIT demo ===\n\n");
|
|
|
|
/* 1. create compiler state */
|
|
s = pcc_new();
|
|
if (!s) {
|
|
fprintf(stderr, "pcc_new failed\n");
|
|
return 1;
|
|
}
|
|
|
|
/* 2. error/warning printer */
|
|
pcc_set_error_func(s, stderr, handle_error);
|
|
|
|
/* 3. JIT mode: compile to memory, not to a file */
|
|
pcc_set_output_type(s, PCC_OUTPUT_MEMORY);
|
|
|
|
/* 4. compile the JIT source string */
|
|
printf("Compiling JIT code...\n");
|
|
if (pcc_compile_string(s, jit_code) == -1) {
|
|
fprintf(stderr, "JIT compile failed\n");
|
|
pcc_delete(s);
|
|
return 1;
|
|
}
|
|
|
|
/* 5. expose host symbols to the JIT code (after compile) */
|
|
pcc_add_symbol(s, "c_host_add", c_host_add);
|
|
pcc_add_symbol(s, "host_message", host_message);
|
|
|
|
/* 6. relocate: produce executable machine code */
|
|
if (pcc_relocate(s) < 0) {
|
|
fprintf(stderr, "JIT relocate failed\n");
|
|
pcc_delete(s);
|
|
return 1;
|
|
}
|
|
printf("JIT code relocated to memory\n\n");
|
|
|
|
/* 7. get the JIT-compiled function and call it */
|
|
jit_run = pcc_get_symbol(s, "jit_run");
|
|
if (!jit_run) {
|
|
fprintf(stderr, "symbol jit_run not found\n");
|
|
pcc_delete(s);
|
|
return 1;
|
|
}
|
|
printf("Calling JIT function jit_run(10)...\n");
|
|
{
|
|
int ret = jit_run(10);
|
|
printf("\nJIT jit_run(10) returned %d\n", ret);
|
|
}
|
|
|
|
pcc_delete(s);
|
|
printf("\n=== JIT demo done ===\n");
|
|
return 0;
|
|
}
|