|
1 | | -// -------------------------------------------------- |
2 | | -// Project: ProX Programming Language (ProXPL) |
3 | | -// Author: ProgrammerKR |
4 | | -// Created: 2025-12-16 |
5 | | -// Copyright © 2025. ProXentix India Pvt. Ltd. All rights reserved. |
6 | | - |
7 | 1 | #ifndef PROXPL_BYTECODE_H |
8 | 2 | #define PROXPL_BYTECODE_H |
9 | | -#include <stdint.h> |
10 | | -#include <stddef.h> |
11 | | - |
12 | | -/* Value representation (simple) */ |
13 | | -typedef enum { |
14 | | - VAL_NULL = 0, |
15 | | - VAL_NUMBER, |
16 | | - VAL_BOOL, |
17 | | - VAL_STRING, |
18 | | - VAL_FUNCTION_PROTO |
19 | | -} ValueType; |
20 | 3 |
|
21 | | -typedef struct { |
22 | | - ValueType type; |
23 | | - union { |
24 | | - double number; |
25 | | - int boolean; |
26 | | - struct { |
27 | | - char *chars; |
28 | | - uint32_t length; |
29 | | - } string; |
30 | | - void *proto; /* placeholder for function proto during runtime */ |
31 | | - } as; |
32 | | -} Value; |
| 4 | +#include "common.h" |
| 5 | +#include "value.h" |
33 | 6 |
|
34 | | -/* Constant table */ |
35 | | -typedef struct { |
36 | | - Value *items; |
37 | | - size_t count; |
38 | | - size_t capacity; |
39 | | -} ConstTable; |
40 | | - |
41 | | -/* Chunk (bytecode + constants) */ |
| 7 | +// ProX-Bytecode Opcodes |
| 8 | +typedef enum { |
| 9 | + OP_CONSTANT, |
| 10 | + OP_NIL, |
| 11 | + OP_TRUE, |
| 12 | + OP_FALSE, |
| 13 | + OP_POP, |
| 14 | + OP_GET_LOCAL, |
| 15 | + OP_SET_LOCAL, |
| 16 | + OP_GET_GLOBAL, |
| 17 | + OP_DEFINE_GLOBAL, |
| 18 | + OP_SET_GLOBAL, |
| 19 | + OP_GET_UPVALUE, |
| 20 | + OP_SET_UPVALUE, |
| 21 | + OP_GET_PROPERTY, |
| 22 | + OP_SET_PROPERTY, |
| 23 | + OP_GET_SUPER, |
| 24 | + OP_EQUAL, |
| 25 | + OP_GREATER, |
| 26 | + OP_LESS, |
| 27 | + OP_ADD, |
| 28 | + OP_SUBTRACT, |
| 29 | + OP_MULTIPLY, |
| 30 | + OP_DIVIDE, |
| 31 | + OP_NOT, |
| 32 | + OP_NEGATE, |
| 33 | + OP_PRINT, |
| 34 | + OP_JUMP, |
| 35 | + OP_JUMP_IF_FALSE, |
| 36 | + OP_LOOP, |
| 37 | + OP_CALL, |
| 38 | + OP_INVOKE, |
| 39 | + OP_SUPER_INVOKE, |
| 40 | + OP_CLOSURE, |
| 41 | + OP_CLOSE_UPVALUE, |
| 42 | + OP_RETURN, |
| 43 | + OP_CLASS, |
| 44 | + OP_INHERIT, |
| 45 | + OP_METHOD, |
| 46 | + |
| 47 | + // Future enhancements |
| 48 | + OP_MATCH, |
| 49 | + OP_AWAIT, |
| 50 | + OP_YIELD, |
| 51 | + |
| 52 | + OP_HALT = 0xFF |
| 53 | +} OpCode; |
| 54 | + |
| 55 | +// Chunk: Sequence of bytecode and metadata |
42 | 56 | typedef struct { |
43 | | - uint8_t *code; |
44 | | - size_t code_len; |
45 | | - size_t code_cap; |
46 | | - ConstTable constants; |
| 57 | + int count; |
| 58 | + int capacity; |
| 59 | + uint8_t *code; |
| 60 | + int *lines; // Line numbers for debugging/errors |
| 61 | + ValueArray constants; // Constant pool |
47 | 62 | } Chunk; |
48 | 63 |
|
49 | | -/* Function prototype (serialized form) */ |
50 | | -typedef struct { |
51 | | - uint32_t arity; |
52 | | - uint16_t max_regs; |
53 | | - Chunk chunk; |
54 | | - uint16_t upvalue_count; |
55 | | -} FunctionProto; |
56 | | - |
57 | | -/* Opcodes */ |
58 | | -typedef enum { |
59 | | - OP_NOP = 0x00, |
60 | | - OP_ADD = 0x01, |
61 | | - OP_SUB = 0x02, |
62 | | - OP_MUL = 0x03, |
63 | | - OP_DIV = 0x04, |
64 | | - OP_MOD = 0x05, |
65 | | - OP_NEG = 0x06, |
66 | | - |
67 | | - OP_EQ = 0x10, |
68 | | - OP_NEQ = 0x11, |
69 | | - OP_LT = 0x12, |
70 | | - OP_LTE = 0x13, |
71 | | - OP_GT = 0x14, |
72 | | - OP_GTE = 0x15, |
73 | | - |
74 | | - OP_NOT = 0x20, |
75 | | - OP_AND = 0x21, |
76 | | - OP_OR = 0x22, |
77 | | - |
78 | | - OP_PUSH_CONST = 0x30, |
79 | | - OP_LOAD_REG = 0x31, |
80 | | - OP_STORE_REG = 0x32, |
81 | | - OP_POP = 0x33, |
82 | | - OP_DUP = 0x34, |
83 | | - |
84 | | - OP_JMP = 0x40, |
85 | | - OP_JMP_IF_TRUE = 0x41, |
86 | | - OP_JMP_IF_FALSE= 0x42, |
87 | | - |
88 | | - OP_CALL = 0x50, |
89 | | - OP_RETURN = 0x51, |
90 | | - OP_TAIL_CALL = 0x52, |
91 | | - |
92 | | - OP_MAKE_FUNCTION = 0x60, |
93 | | - OP_CLOSURE = 0x61, |
94 | | - OP_CLOSE_UPVALUE = 0x62, |
95 | | - OP_LOAD_UPVALUE = 0x63, |
96 | | - OP_STORE_UPVALUE = 0x64, |
97 | | - |
98 | | - OP_NEW_ARRAY = 0x70, |
99 | | - OP_INDEX_GET = 0x71, |
100 | | - OP_INDEX_SET = 0x72, |
101 | | - OP_GET_FIELD = 0x73, |
102 | | - OP_SET_FIELD = 0x74, |
103 | | - |
104 | | - OP_DBG_LINE = 0x90, |
105 | | - OP_DBG_LOC = 0x91, |
106 | | - |
107 | | - OP_HALT = 0xFF |
108 | | -} Opcode; |
109 | | - |
110 | | -/* Addressing modes for CALL / similar */ |
111 | | -typedef enum { |
112 | | - AM_CONST = 0x01, |
113 | | - AM_REG = 0x02, |
114 | | - AM_STACK = 0x03 |
115 | | -} AddrMode; |
116 | | - |
117 | | -/* Chunk API */ |
118 | | -void chunk_init(Chunk *chunk); |
119 | | -void chunk_free(Chunk *chunk); |
120 | | -void chunk_write_byte(Chunk *chunk, uint8_t byte); |
121 | | -void chunk_write_bytes(Chunk *chunk, const uint8_t *bytes, size_t len); |
122 | | - |
123 | | -/* emission helpers */ |
124 | | -void emit_opcode(Chunk *chunk, Opcode op); |
125 | | -void emit_u8(Chunk *chunk, uint8_t x); |
126 | | -void emit_u16_le(Chunk *chunk, uint16_t x); |
127 | | -void emit_u32_le(Chunk *chunk, uint32_t x); |
128 | | -void emit_uleb128(Chunk *chunk, uint64_t value); |
129 | | -void emit_sleb128(Chunk *chunk, int64_t value); |
130 | | - |
131 | | -/* constant table */ |
132 | | -void consttable_init(ConstTable *tbl); |
133 | | -void consttable_free(ConstTable *tbl); |
134 | | -size_t consttable_add(ConstTable *tbl, Value v); |
135 | | -Value consttable_get(const ConstTable *tbl, size_t idx); |
136 | | - |
137 | | -/* serialization */ |
138 | | -int write_chunk_to_file(const char *path, const Chunk *chunk); |
139 | | -int read_chunk_from_file(const char *path, Chunk *out); |
140 | | - |
141 | | -/* utility */ |
142 | | -uint64_t read_uleb128_from(const uint8_t *buf, size_t buf_len, size_t *out_read); |
143 | | -int64_t read_sleb128_from(const uint8_t *buf, size_t buf_len, size_t *out_read); |
| 64 | +// Chunk API |
| 65 | +void initChunk(Chunk *chunk); |
| 66 | +void freeChunk(Chunk *chunk); |
| 67 | +void writeChunk(Chunk *chunk, uint8_t byte, int line); |
| 68 | +int addConstant(Chunk *chunk, Value value); |
144 | 69 |
|
145 | | -#endif /* PROXPL_BYTECODE_H */ |
| 70 | +#endif // PROXPL_BYTECODE_H |
0 commit comments