diff --git a/bin/libpcc.dll b/bin/libpcc.dll index cb594c7..3fe72d9 100644 Binary files a/bin/libpcc.dll and b/bin/libpcc.dll differ diff --git a/bin/pcc.exe b/bin/pcc.exe index 227ee8c..aac99cf 100644 Binary files a/bin/pcc.exe and b/bin/pcc.exe differ diff --git a/examples/jit_demo.c b/examples/jit_demo.c new file mode 100644 index 0000000..eaaec49 --- /dev/null +++ b/examples/jit_demo.c @@ -0,0 +1,109 @@ +/* + * 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 +#include +#include +#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 \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; +} diff --git a/include/stddef.h b/include/stddef.h index 1fab665..33858b0 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -12,6 +12,12 @@ typedef __SIZE_TYPE__ uintptr_t; typedef union { long long __ll; long double __ld; } max_align_t; #endif +#if __STDC_VERSION__ >= 202311L +/* C23: nullptr and nullptr_t */ +typedef void *nullptr_t; +#define nullptr ((void*)0) +#endif + #ifndef NULL #define NULL ((void*)0) #endif diff --git a/libpcc.c b/libpcc.c index 3a3e7da..9109d41 100644 --- a/libpcc.c +++ b/libpcc.c @@ -1997,6 +1997,9 @@ PUB_FUNC int pcc_parse_args(PCCState *s, int *pargc, char ***pargv) || strcmp(optarg, "=c17") == 0 || strcmp(optarg, "=gnu17") == 0 || strcmp(optarg, "=c18") == 0 || strcmp(optarg, "=gnu18") == 0) s->cversion = 201112; + else if (strcmp(optarg, "=c2x") == 0 || strcmp(optarg, "=c23") == 0 + || strcmp(optarg, "=gnu2x") == 0 || strcmp(optarg, "=gnu23") == 0) + s->cversion = 202311; /* C23 */ else if (strcmp(optarg, "=c99") == 0 || strcmp(optarg, "=gnu99") == 0) s->cversion = 199901; else if (strcmp(optarg, "=c89") == 0 || strcmp(optarg, "=c90") == 0 diff --git a/vscode-extension/package.json b/vscode-extension/package.json index 8604c97..bb5069b 100644 --- a/vscode-extension/package.json +++ b/vscode-extension/package.json @@ -1,15 +1,27 @@ { - "name": "PCC C Language Support", + "name": "pcc-language-support", "displayName": "PCC C Language Support", - "description": "C language support with Paze C Compiler (PCC) integration", + "description": "C language support with Paze C Compiler (PCC) integration - syntax highlighting, build and run", "version": "0.1.0", "publisher": "PazeAI", + "license": "MIT", "engines": { "vscode": "^1.60.0" }, "categories": [ "Programming Languages" ], + "keywords": [ + "C", + "pcc", + "compiler", + "Paze" + ], + "main": "./extension.js", + "activationEvents": [ + "onCommand:pcc.build", + "onCommand:pcc.run" + ], "contributes": { "languages": [ { @@ -21,7 +33,8 @@ "extensions": [ ".c", ".h" - ] + ], + "configuration": "./language-configuration.json" } ], "grammars": [ @@ -69,47 +82,44 @@ "description": "Output directory for compiled exe. Empty = same dir as source." } } - } - }, - "main": "./extension.js", - "activationEvents": [ - "onCommand:pcc.build", - "onCommand:pcc.run" - ], - "commands": [ - { - "command": "pcc.build", - "title": "PCC: Build" }, - { - "command": "pcc.run", - "title": "PCC: Build & Run" - } - ], - "keybindings": [ - { - "command": "pcc.build", - "key": "ctrl+shift+b", - "when": "editorLangId == c" - }, - { - "command": "pcc.run", - "key": "ctrl+f5", - "when": "editorLangId == c" - } - ], - "menus": { - "editor/title": [ + "commands": [ { "command": "pcc.build", - "when": "editorLangId == c", - "group": "navigation" + "title": "PCC: Build", + "category": "PCC" }, { "command": "pcc.run", - "when": "editorLangId == c", - "group": "navigation" + "title": "PCC: Build & Run", + "category": "PCC" } - ] + ], + "keybindings": [ + { + "command": "pcc.build", + "key": "ctrl+shift+b", + "when": "editorLangId == c" + }, + { + "command": "pcc.run", + "key": "ctrl+f5", + "when": "editorLangId == c" + } + ], + "menus": { + "editor/title": [ + { + "command": "pcc.build", + "when": "editorLangId == c", + "group": "navigation" + }, + { + "command": "pcc.run", + "when": "editorLangId == c", + "group": "navigation" + } + ] + } } } diff --git a/vscode-extension/pcc-language-support-0.1.0.vsix b/vscode-extension/pcc-language-support-0.1.0.vsix new file mode 100644 index 0000000..f02cf10 Binary files /dev/null and b/vscode-extension/pcc-language-support-0.1.0.vsix differ