-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcompiling-immediates.c
506 lines (417 loc) · 13.2 KB
/
compiling-immediates.c
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// vim: set tabstop=2 shiftwidth=2 textwidth=79 expandtab:
// gcc -O2 -g -Wall -Wextra -pedantic -fno-strict-aliasing
// assets/code/lisp/compiling-immediates.c
#define _GNU_SOURCE
#include <assert.h> // for assert
#include <stdbool.h> // for bool
#include <stddef.h> // for NULL
#include <stdint.h> // for int32_t, etc
#include <string.h> // for memcpy
#include <sys/mman.h> // for mmap
#undef _GNU_SOURCE
#include "greatest.h"
// Objects
typedef int64_t word;
typedef uint64_t uword;
// These constants are defined in a enum because the right hand side of a
// statement like
// static const int kFoo = ...;
// must be a so-called "Integer Constant Expression". Compilers are required to
// support a certain set of these expressions, but are not required to support
// arbitrary arithmetic with other integer constants. Compilers such as gcc
// before gcc-8 just decided not to play this game, while gcc-8+ and Clang play
// just fine.
// Since this arithmetic with constant values works just fine for enums, make
// all these constants enum values instead.
// See https://twitter.com/tekknolagi/status/1328449329472835586 for more info.
enum {
kBitsPerByte = 8, // bits
kWordSize = sizeof(word), // bytes
kBitsPerWord = kWordSize * kBitsPerByte, // bits
kIntegerTag = 0x0, // 0b00
kIntegerTagMask = 0x3, // 0b11
kIntegerShift = 2,
kIntegerBits = kBitsPerWord - kIntegerShift,
kImmediateTagMask = 0x3f,
kCharTag = 0x0f, // 0b00001111
kCharMask = 0xff, // 0b11111111
kCharShift = 8,
kBoolTag = 0x1f, // 0b0011111
kBoolMask = 0x80, // 0b10000000
kBoolShift = 7,
};
// These are defined as macros because they will not work as static const int
// constants (per above explanation), and enum constants are only required to
// be an int wide (per ISO C).
#define INTEGER_MAX ((1LL << (kIntegerBits - 1)) - 1)
#define INTEGER_MIN (-(1LL << (kIntegerBits - 1)))
word Object_encode_integer(word value) {
assert(value < INTEGER_MAX && "too big");
assert(value > INTEGER_MIN && "too small");
return value << kIntegerShift;
}
word Object_decode_integer(word value) { return value >> kIntegerShift; }
word Object_encode_char(char value) {
return ((word)value << kCharShift) | kCharTag;
}
char Object_decode_char(word value) {
return (value >> kCharShift) & kCharMask;
}
word Object_encode_bool(bool value) {
return ((word)value << kBoolShift) | kBoolTag;
}
bool Object_decode_bool(word value) { return value & kBoolMask; }
word Object_true() { return Object_encode_bool(true); }
word Object_false() { return Object_encode_bool(false); }
word Object_nil() { return 0x2f; }
// End Objects
// Buffer
typedef unsigned char byte;
typedef enum {
kWritable,
kExecutable,
} BufferState;
typedef struct {
byte *address;
BufferState state;
size_t len;
size_t capacity;
} Buffer;
byte *Buffer_alloc_writable(size_t capacity) {
byte *result = mmap(/*addr=*/NULL, capacity, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
/*filedes=*/-1, /*off=*/0);
assert(result != MAP_FAILED);
return result;
}
void Buffer_init(Buffer *result, size_t capacity) {
result->address = Buffer_alloc_writable(capacity);
assert(result->address != MAP_FAILED);
result->state = kWritable;
result->len = 0;
result->capacity = capacity;
}
void Buffer_deinit(Buffer *buf) {
munmap(buf->address, buf->capacity);
buf->address = NULL;
buf->len = 0;
buf->capacity = 0;
}
int Buffer_make_executable(Buffer *buf) {
int result = mprotect(buf->address, buf->len, PROT_EXEC);
buf->state = kExecutable;
return result;
}
byte Buffer_at8(Buffer *buf, size_t pos) { return buf->address[pos]; }
void Buffer_at_put8(Buffer *buf, size_t pos, byte b) { buf->address[pos] = b; }
word max(word left, word right) { return left > right ? left : right; }
void Buffer_ensure_capacity(Buffer *buf, word additional_capacity) {
if (buf->len + additional_capacity <= buf->capacity) {
return;
}
word new_capacity =
max(buf->capacity * 2, buf->capacity + additional_capacity);
byte *address = Buffer_alloc_writable(new_capacity);
memcpy(address, buf->address, buf->len);
int result = munmap(buf->address, buf->capacity);
assert(result == 0 && "munmap failed");
buf->address = address;
buf->capacity = new_capacity;
}
void Buffer_write8(Buffer *buf, byte b) {
Buffer_ensure_capacity(buf, sizeof b);
Buffer_at_put8(buf, buf->len++, b);
}
void Buffer_write32(Buffer *buf, int32_t value) {
for (size_t i = 0; i < 4; i++) {
Buffer_write8(buf, (value >> (i * kBitsPerByte)) & 0xff);
}
}
// End Buffer
// Emit
typedef enum {
kRax = 0,
kRcx,
kRdx,
kRbx,
kRsp,
kRbp,
kRsi,
kRdi,
} Register;
enum {
kRexPrefix = 0x48,
};
void Emit_mov_reg_imm32(Buffer *buf, Register dst, int32_t src) {
Buffer_write8(buf, kRexPrefix);
Buffer_write8(buf, 0xc7);
Buffer_write8(buf, 0xc0 + dst);
Buffer_write32(buf, src);
}
void Emit_ret(Buffer *buf) { Buffer_write8(buf, 0xc3); }
// End Emit
// AST
typedef struct ASTNode ASTNode;
bool AST_is_integer(ASTNode *node) {
return ((word)node & kIntegerTagMask) == kIntegerTag;
}
word AST_get_integer(ASTNode *node) {
return Object_decode_integer((word)node);
}
ASTNode *AST_new_integer(word value) {
return (ASTNode *)Object_encode_integer(value);
}
bool AST_is_char(ASTNode *node) {
return ((word)node & kImmediateTagMask) == kCharTag;
}
char AST_get_char(ASTNode *node) { return Object_decode_char((word)node); }
ASTNode *AST_new_char(char value) {
return (ASTNode *)Object_encode_char(value);
}
bool AST_is_bool(ASTNode *node) {
return ((word)node & kImmediateTagMask) == kBoolTag;
}
bool AST_get_bool(ASTNode *node) { return Object_decode_bool((word)node); }
ASTNode *AST_new_bool(bool value) {
return (ASTNode *)Object_encode_bool(value);
}
bool AST_is_nil(ASTNode *node) { return (word)node == Object_nil(); }
ASTNode *AST_nil() { return (ASTNode *)Object_nil(); }
// End AST
// Compile
int Compile_expr(Buffer *buf, ASTNode *node) {
if (AST_is_integer(node)) {
word value = AST_get_integer(node);
Emit_mov_reg_imm32(buf, kRax, Object_encode_integer(value));
return 0;
}
if (AST_is_char(node)) {
char value = AST_get_char(node);
Emit_mov_reg_imm32(buf, kRax, Object_encode_char(value));
return 0;
}
if (AST_is_bool(node)) {
bool value = AST_get_bool(node);
Emit_mov_reg_imm32(buf, kRax, Object_encode_bool(value));
return 0;
}
if (AST_is_nil(node)) {
Emit_mov_reg_imm32(buf, kRax, Object_nil());
return 0;
}
assert(0 && "unexpected node type");
}
int Compile_function(Buffer *buf, ASTNode *node) {
int result = Compile_expr(buf, node);
if (result != 0)
return result;
Emit_ret(buf);
return 0;
}
// End Compile
typedef int (*JitFunction)();
// Testing
word Testing_execute_expr(Buffer *buf) {
assert(buf != NULL);
assert(buf->address != NULL);
assert(buf->state == kExecutable);
// The pointer-pointer cast is allowed but the underlying
// data-to-function-pointer back-and-forth is only guaranteed to work on
// POSIX systems (because of eg dlsym).
JitFunction function = *(JitFunction *)(&buf->address);
return function();
}
void Testing_print_hex_array(FILE *fp, byte *arr, size_t arr_size) {
for (size_t i = 0; i < arr_size; i++) {
fprintf(fp, "%.2x ", arr[i]);
}
}
#define EXPECT_EQUALS_BYTES(buf, arr) \
ASSERT_MEM_EQ(arr, (buf)->address, sizeof arr)
#define RUN_BUFFER_TEST(test_name) \
do { \
Buffer buf; \
Buffer_init(&buf, 1); \
GREATEST_RUN_TEST1(test_name, &buf); \
Buffer_deinit(&buf); \
} while (0)
// End Testing
// Tests
TEST encode_positive_integer(void) {
ASSERT_EQ(Object_encode_integer(0), 0x0);
ASSERT_EQ(Object_encode_integer(1), 0x4);
ASSERT_EQ(Object_encode_integer(10), 0x28);
PASS();
}
TEST encode_negative_integer(void) {
ASSERT_EQ(Object_encode_integer(0), 0x0);
ASSERT_EQ(Object_encode_integer(-1), (word)0xfffffffffffffffc);
ASSERT_EQ(Object_encode_integer(-10), (word)0xffffffffffffffd8);
PASS();
}
TEST encode_char(void) {
ASSERT_EQ(Object_encode_char('\0'), 0xf);
ASSERT_EQ(Object_encode_char('a'), 0x610f);
PASS();
}
TEST decode_char(void) {
ASSERT_EQ(Object_decode_char(0xf), '\0');
ASSERT_EQ(Object_decode_char(0x610f), 'a');
PASS();
}
TEST encode_bool(void) {
ASSERT_EQ(Object_encode_bool(true), 0x9f);
ASSERT_EQ(Object_encode_bool(false), 0x1f);
ASSERT_EQ(Object_true(), 0x9f);
ASSERT_EQ(Object_false(), 0x1f);
PASS();
}
TEST decode_bool(void) {
ASSERT_EQ(Object_decode_bool(0x9f), true);
ASSERT_EQ(Object_decode_bool(0x1f), false);
PASS();
}
TEST buffer_write8_increases_length(Buffer *buf) {
ASSERT_EQ(buf->len, 0);
Buffer_write8(buf, 0xdb);
ASSERT_EQ(Buffer_at8(buf, 0), 0xdb);
ASSERT_EQ(buf->len, 1);
PASS();
}
TEST buffer_write8_expands_buffer(void) {
Buffer buf;
Buffer_init(&buf, 1);
ASSERT_EQ(buf.capacity, 1);
ASSERT_EQ(buf.len, 0);
Buffer_write8(&buf, 0xdb);
Buffer_write8(&buf, 0xef);
ASSERT(buf.capacity > 1);
ASSERT_EQ(buf.len, 2);
Buffer_deinit(&buf);
PASS();
}
TEST buffer_write32_expands_buffer(void) {
Buffer buf;
Buffer_init(&buf, 1);
ASSERT_EQ(buf.capacity, 1);
ASSERT_EQ(buf.len, 0);
Buffer_write32(&buf, 0xdeadbeef);
ASSERT(buf.capacity > 1);
ASSERT_EQ(buf.len, 4);
Buffer_deinit(&buf);
PASS();
}
TEST buffer_write32_writes_little_endian(Buffer *buf) {
Buffer_write32(buf, 0xdeadbeef);
ASSERT_EQ(Buffer_at8(buf, 0), 0xef);
ASSERT_EQ(Buffer_at8(buf, 1), 0xbe);
ASSERT_EQ(Buffer_at8(buf, 2), 0xad);
ASSERT_EQ(Buffer_at8(buf, 3), 0xde);
PASS();
}
TEST compile_positive_integer(Buffer *buf) {
word value = 123;
ASTNode *node = AST_new_integer(value);
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm(123); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0xec, 0x01, 0x00, 0x00, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_encode_integer(value));
PASS();
}
TEST compile_negative_integer(Buffer *buf) {
word value = -123;
ASTNode *node = AST_new_integer(value);
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm(-123); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0x14, 0xfe, 0xff, 0xff, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_encode_integer(value));
PASS();
}
TEST compile_char(Buffer *buf) {
char value = 'a';
ASTNode *node = AST_new_char(value);
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm('a'); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0x0f, 0x61, 0x00, 0x00, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_encode_char(value));
PASS();
}
TEST compile_true(Buffer *buf) {
ASTNode *node = AST_new_bool(true);
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm(true); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0x9f, 0x0, 0x0, 0x0, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_true());
PASS();
}
TEST compile_false(Buffer *buf) {
ASTNode *node = AST_new_bool(false);
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm(false); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0x1f, 0x00, 0x00, 0x00, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_false());
PASS();
}
TEST compile_nil(Buffer *buf) {
ASTNode *node = AST_nil();
int compile_result = Compile_function(buf, node);
ASSERT_EQ(compile_result, 0);
// mov eax, imm(nil); ret
byte expected[] = {0x48, 0xc7, 0xc0, 0x2f, 0x00, 0x00, 0x00, 0xc3};
EXPECT_EQUALS_BYTES(buf, expected);
Buffer_make_executable(buf);
word result = Testing_execute_expr(buf);
ASSERT_EQ(result, Object_nil());
PASS();
}
SUITE(object_tests) {
RUN_TEST(encode_positive_integer);
RUN_TEST(encode_negative_integer);
RUN_TEST(encode_char);
RUN_TEST(decode_char);
RUN_TEST(encode_bool);
RUN_TEST(decode_bool);
}
SUITE(buffer_tests) {
RUN_BUFFER_TEST(buffer_write8_increases_length);
RUN_TEST(buffer_write8_expands_buffer);
RUN_TEST(buffer_write32_expands_buffer);
RUN_BUFFER_TEST(buffer_write32_writes_little_endian);
}
SUITE(compiler_tests) {
RUN_BUFFER_TEST(compile_positive_integer);
RUN_BUFFER_TEST(compile_negative_integer);
RUN_BUFFER_TEST(compile_char);
RUN_BUFFER_TEST(compile_true);
RUN_BUFFER_TEST(compile_false);
RUN_BUFFER_TEST(compile_nil);
}
// End Tests
GREATEST_MAIN_DEFS();
int main(int argc, char **argv) {
GREATEST_MAIN_BEGIN();
RUN_SUITE(object_tests);
RUN_SUITE(buffer_tests);
RUN_SUITE(compiler_tests);
GREATEST_MAIN_END();
}