|
| 1 | +/* |
| 2 | + * vm_core_opt.c |
| 3 | + * |
| 4 | + * Optimized VM core loop prototype for ProXPL. |
| 5 | + * - computed-goto dispatch (gcc/clang) |
| 6 | + * - inline caching skeleton for call targets/property lookup |
| 7 | + * - fast numeric path for binary arithmetic (type-specialized) |
| 8 | + * |
| 9 | + * This file is a drop-in experimental core; it is intentionally self-contained |
| 10 | + * and documents the optimizations to be integrated into the main VM. |
| 11 | + */ |
| 12 | + |
| 13 | +#include "../../include/bytecode.h" |
| 14 | +#include <stdio.h> |
| 15 | +#include <string.h> |
| 16 | + |
| 17 | +/* Inline cache entry for call targets / property access */ |
| 18 | +typedef struct { |
| 19 | + uint32_t site_pc; /* bytecode pc of the call/property */ |
| 20 | + const char *target_name; /* cached name (for simple engines) */ |
| 21 | + void *native_ptr; /* direct native function pointer if resolved */ |
| 22 | + uint32_t hit_count; |
| 23 | +} ICEntry; |
| 24 | + |
| 25 | +/* Micro-optimization: expect/likely macros for branch prediction */ |
| 26 | +#if defined(__GNUC__) |
| 27 | +#define likely(x) __builtin_expect(!!(x), 1) |
| 28 | +#define unlikely(x) __builtin_expect(!!(x), 0) |
| 29 | +#else |
| 30 | +#define likely(x) (x) |
| 31 | +#define unlikely(x) (x) |
| 32 | +#endif |
| 33 | + |
| 34 | +/* Optimized dispatch; similar to vm_dispatch but with fast-paths and IC hooks */ |
| 35 | +int vm_execute_optimized(const Chunk *chunk) { |
| 36 | + size_t ip = 0; |
| 37 | + Value stack[2048]; |
| 38 | + int sp = 0; |
| 39 | + |
| 40 | + /* small polymorphic inline cache for demonstration */ |
| 41 | + ICEntry icache[64]; |
| 42 | + memset(icache, 0, sizeof(icache)); |
| 43 | + |
| 44 | +#if defined(__GNUC__) || defined(__clang__) |
| 45 | + static void *dispatch_table[256] = { &&do_NOP }; |
| 46 | +#define DISPATCH() goto *dispatch_table[op] |
| 47 | +#else |
| 48 | +#define DISPATCH() goto dispatch_switch |
| 49 | +#endif |
| 50 | + |
| 51 | + for (;;) { |
| 52 | + if (ip >= chunk->code_len) return 0; |
| 53 | + uint8_t op = chunk->code[ip++]; |
| 54 | + |
| 55 | +#if defined(__GNUC__) || defined(__clang__) |
| 56 | + /* fill common handlers once */ |
| 57 | + dispatch_table[OP_NOP] = &&do_NOP; |
| 58 | + dispatch_table[OP_PUSH_CONST] = &&do_PUSH_CONST; |
| 59 | + dispatch_table[OP_CALL] = &&do_CALL; |
| 60 | + dispatch_table[OP_ADD] = &&do_ADD_FAST; |
| 61 | + dispatch_table[OP_HALT] = &&do_HALT; |
| 62 | + DISPATCH(); |
| 63 | + |
| 64 | + do_NOP: { continue; } |
| 65 | + |
| 66 | + do_PUSH_CONST: { |
| 67 | + size_t read = 0; |
| 68 | + uint64_t idx = read_uleb128_from(chunk->code + ip, chunk->code_len - ip, &read); |
| 69 | + ip += read; |
| 70 | + stack[sp++] = consttable_get(&chunk->constants, (size_t)idx); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + /* Fast numeric add - type-specialized path */ |
| 75 | + do_ADD_FAST: { |
| 76 | + if (unlikely(sp < 2)) return -1; |
| 77 | + Value b = stack[--sp]; |
| 78 | + Value a = stack[--sp]; |
| 79 | + if (likely(a.type == VAL_NUMBER && b.type == VAL_NUMBER)) { |
| 80 | + Value r; r.type = VAL_NUMBER; r.as.number = a.as.number + b.as.number; |
| 81 | + stack[sp++] = r; continue; |
| 82 | + } |
| 83 | + /* fallback to generic add handler - simple example */ |
| 84 | + fprintf(stderr, "slow path: non-number add\n"); |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + do_CALL: { |
| 89 | + /* inline cache lookup (very simple) */ |
| 90 | + size_t saved_ip = ip; |
| 91 | + size_t read = 0; |
| 92 | + uint64_t idx = read_uleb128_from(chunk->code + ip, chunk->code_len - ip, &read); |
| 93 | + ip += read; |
| 94 | + if (ip >= chunk->code_len) return -1; |
| 95 | + uint8_t argc = chunk->code[ip++]; |
| 96 | + |
| 97 | + Value callee = consttable_get(&chunk->constants, (size_t)idx); |
| 98 | + /* probe icache for site */ |
| 99 | + uint32_t site = (uint32_t)(saved_ip & 63); |
| 100 | + ICEntry *ent = &icache[site]; |
| 101 | + if (ent->site_pc == (uint32_t)saved_ip && ent->native_ptr) { |
| 102 | + ent->hit_count++; |
| 103 | + /* call native_ptr directly (demo only) */ |
| 104 | + typedef Value (*native_fn)(Value*, int); |
| 105 | + native_fn fn = (native_fn)ent->native_ptr; |
| 106 | + Value ret = fn(&stack[sp-argc], (int)argc); |
| 107 | + sp -= argc; stack[sp++] = ret; continue; |
| 108 | + } |
| 109 | + |
| 110 | + /* resolve and fill cache (demo: only 'print') */ |
| 111 | + if (callee.type == VAL_STRING && strcmp(callee.as.string.chars, "print") == 0) { |
| 112 | + /* cache a sentinel to native print handler */ |
| 113 | + ent->site_pc = (uint32_t)saved_ip; |
| 114 | + ent->target_name = callee.as.string.chars; |
| 115 | + ent->native_ptr = NULL; /* optionally assign a function pointer */ |
| 116 | + /* fallback: implement print directly */ |
| 117 | + for (int i = (int)argc - 1; i >= 0; --i) { |
| 118 | + Value arg = stack[--sp]; |
| 119 | + if (arg.type == VAL_STRING) fputs(arg.as.string.chars, stdout); |
| 120 | + else if (arg.type == VAL_NUMBER) printf("%g", arg.as.number); |
| 121 | + else if (arg.type == VAL_BOOL) fputs(arg.as.boolean?"true":"false", stdout); |
| 122 | + else fputs("<obj>", stdout); |
| 123 | + if (i) putchar(' '); |
| 124 | + } |
| 125 | + putchar('\n'); |
| 126 | + Value r = make_null_const(); stack[sp++] = r; continue; |
| 127 | + } |
| 128 | + |
| 129 | + fprintf(stderr, "unsupported call target\n"); return -1; |
| 130 | + } |
| 131 | + |
| 132 | + do_HALT: { return 0; } |
| 133 | + |
| 134 | +#else |
| 135 | +dispatch_switch: |
| 136 | + switch (op) { |
| 137 | + case OP_NOP: break; |
| 138 | + case OP_PUSH_CONST: { |
| 139 | + size_t read=0; uint64_t idx = read_uleb128_from(chunk->code+ip, chunk->code_len-ip, &read); ip+=read; |
| 140 | + stack[sp++] = consttable_get(&chunk->constants, (size_t)idx); |
| 141 | + } break; |
| 142 | + case OP_ADD: { |
| 143 | + if (sp<2) return -1; Value b = stack[--sp]; Value a = stack[--sp]; |
| 144 | + if (a.type==VAL_NUMBER && b.type==VAL_NUMBER) { Value r; r.type=VAL_NUMBER; r.as.number=a.as.number+b.as.number; stack[sp++]=r; } |
| 145 | + else { fprintf(stderr,"slow path: non-number add\n"); return -1; } |
| 146 | + } break; |
| 147 | + case OP_CALL: { |
| 148 | + size_t read=0; uint64_t idx = read_uleb128_from(chunk->code+ip, chunk->code_len-ip, &read); ip+=read; |
| 149 | + if (ip>=chunk->code_len) return -1; uint8_t argc = chunk->code[ip++]; |
| 150 | + Value callee = consttable_get(&chunk->constants,(size_t)idx); |
| 151 | + if (callee.type==VAL_STRING && strcmp(callee.as.string.chars,"print")==0) { |
| 152 | + for (int i=(int)argc-1;i>=0;--i){ Value arg=stack[--sp]; if (arg.type==VAL_STRING) fputs(arg.as.string.chars, stdout); else if (arg.type==VAL_NUMBER) printf("%g",arg.as.number); else fputs("<obj>",stdout); if (i) putchar(' ');} putchar('\n'); Value r = make_null_const(); stack[sp++]=r; break; |
| 153 | + } |
| 154 | + fprintf(stderr,"unsupported call target\n"); return -1; |
| 155 | + } |
| 156 | + case OP_HALT: return 0; |
| 157 | + default: fprintf(stderr,"unhandled opcode %u\n",op); return -1; |
| 158 | + } |
| 159 | +#endif |
| 160 | + } |
| 161 | + |
| 162 | + return 0; |
| 163 | +} |
0 commit comments