1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
* Cem Runtime - Stack Machine Implementation
*
* This header defines the runtime stack representation and operations
* for the Cem programming language.
*
* The stack is implemented as a linked list of heap-allocated cells.
* Each cell contains a tagged union representing different value types.
*/
/**
* Value type tags
*
* Each stack value is tagged with its runtime type to support
* dynamic dispatch and type checking.
*/
typedef enum ValueTag;
/**
* Stack cell structure
*
* Represents a single value on the stack.
* Forms a linked list via the 'next' pointer.
*
* MEMORY LAYOUT (64-bit):
* - tag: 4 bytes (ValueTag/int32_t) at offset 0
* - padding: 4 bytes (for union alignment to 8-byte boundary)
* - value union: 16 bytes at offset 8
* - int64_t i: 8 bytes
* - bool b: 1 byte (C99 bool from stdbool.h, typically uint8_t)
* - char* s: 8 bytes
* - void* quotation: 8 bytes
* - variant struct: 16 bytes (4-byte tag + 4-byte padding + 8-byte pointer)
* - next: 8 bytes (pointer) at offset 24
* TOTAL: 32 bytes
*
* IMPORTANT ABI ASSUMPTIONS for LLVM codegen:
* 1. bool is represented as i8 (uint8_t/unsigned char)
* 2. bool value is stored at the first byte of the union (offset 8 from struct
* start)
* 3. The union is 16 bytes due to the variant struct being the largest member
*
* If the C bool type changes (e.g., becomes i32 on some platform), the LLVM
* code generation in src/codegen/mod.rs must be updated accordingly.
*/
typedef struct StackCell StackCell;
// Stack Operations
/**
* dup ( A -- A A )
* Duplicate the top element of the stack
*/
StackCell *;
/**
* drop ( A -- )
* Remove the top element of the stack
*/
StackCell *;
/**
* swap ( A B -- B A )
* Swap the top two elements
*/
StackCell *;
/**
* over ( A B -- A B A )
* Copy the second element to the top
*/
StackCell *;
/**
* rot ( A B C -- B C A )
* Rotate the top three elements
*/
StackCell *;
// Arithmetic Operations
/**
* add ( Int Int -- Int )
* Add two integers
*/
StackCell *;
/**
* subtract ( Int Int -- Int )
* Subtract two integers (second - first)
*/
StackCell *;
/**
* multiply ( Int Int -- Int )
* Multiply two integers
*/
StackCell *;
/**
* divide ( Int Int -- Int )
* Divide two integers (second / first)
*/
StackCell *;
// Comparison Operations
/**
* less_than ( Int Int -- Bool )
* Check if second < first
*/
StackCell *;
/**
* greater_than ( Int Int -- Bool )
* Check if second > first
*/
StackCell *;
/**
* equal ( Int Int -- Bool )
* Check if second == first
*/
StackCell *;
// Push Operations
/**
* push_int ( -- Int )
* Push an integer onto the stack
*/
StackCell *;
/**
* push_bool ( -- Bool )
* Push a boolean onto the stack
*/
StackCell *;
/**
* push_string ( -- String )
* Push a string onto the stack (makes a copy)
*/
StackCell *;
/**
* push_quotation ( -- Quotation )
* Push a quotation (function pointer) onto the stack
*/
StackCell *;
// String Operations
/**
* string_length ( String -- Int )
* Get the length of a string (number of bytes)
*/
StackCell *;
/**
* string_concat ( String String -- String )
* Concatenate two strings (second + first)
*/
StackCell *;
/**
* string_equal ( String String -- Bool )
* Check if two strings are equal
*/
StackCell *;
// Control Flow Operations
/**
* call_quotation ( Quotation -- ... )
* Call a quotation (function pointer)
*/
StackCell *;
/**
* if_then_else ( Bool Quotation Quotation -- ... )
* Conditional execution: if true call first quotation, else call second
*/
StackCell *;
// Utility Functions
/**
* Allocate a new stack cell
*/
StackCell *;
/**
* Free a stack cell and its contents
*/
void ;
/**
* Free entire stack
*/
void ;
/**
* Print stack contents (for debugging)
*/
void ;
/**
* Runtime error handling
*/
void ;
// CEM_RUNTIME_STACK_H