summaryrefslogtreecommitdiff
path: root/binary_code_generation.c
blob: 3735b2c86c563d00406fce344b5bf4c02ed5030f (plain)
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
#if 0
SRC=${0##*/}
BIN=${SRC%.*}
gcc -o $BIN.tmp $SRC && ./$BIN.tmp $@ && rm $BIN.tmp
exit $?
#endif

//  ================================================================
//
//    Binary code generation compiler backend
//
//  Qualities
//
//  - Single source file
//  - Simple and flexible API
//  - No external dependencies
//  - No configuration required
//  - No dynamic memory management
//
//  TODO
//
//  - ELF + x86_64 executable
//  - x86_64 object file
//  - Linking libraries
//  - Effective entity allocation
//  - Linked lists for large entities
//  - Sea of Nodes
//  - Optimization layers
//  - Multithreading
//  - COFF, PE, OMF, Mach-O
//  - i386, RISC-V, ARM
//
//  ================================================================
//
//    Basic declarations
//
//  ================================================================

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

typedef signed char        i8;
typedef signed short       i16;
typedef signed int         i32;
typedef signed long long   i64;
typedef unsigned char      u8;
typedef unsigned short     u16;
typedef unsigned int       u32;
typedef unsigned long long u64;
typedef float              f32;
typedef double             f64;
typedef signed char        b8;
typedef int                b32;
typedef char               c8;

//  ================================================================
//
//    Intermediate representation
//
//  ================================================================

//  Declarations
//

enum {
  //  For indices
  UNDEFINED = -1,

  //  Formats
  //

  FORMAT_ELF  = 1,
  FORMAT_COFF,
  FORMAT_PE,
  FORMAT_OMF,
  FORMAT_MATCH_O,

  //  Architecture
  //

  ARCH_RISC_V = 64,
  ARCH_I386,
  ARCH_X86_64,
  ARCH_ARM,

  //  Sea of Nodes flow type
  //

  FLOW_DATA = 0,
  FLOW_CONTROL,

  //  Semantic node operations
  //

  OP_I64 = 0,
  OP_RET,

  //  Object file
  UNIT_OBJ = 0,

  //  Executable
  UNIT_EXE,

  //  Entity types
  //

  TYPE_NODE = 0,
  TYPE_PROC,
  TYPE_UNIT,

  //  Limits
  //
  //    NOTE
  //
  //    All limits can be exceeded using the linked list of entities
  //    (see `entity_t::tail`), except for `MAX_ENTITY_COUNT`.
  //

  MAX_LITERAL_SIZE = 400,
  MAX_NAME_SIZE    = 128,
  MAX_PROC_COUNT   = 80,
  MAX_NODE_COUNT   = 60,
  MAX_ENTITY_COUNT = 16384,
};

//  A semantic node is an operation with optional data
//  and possible references to other nodes.
//

typedef struct {
  i16 op;
  i8  flow;
  union {
    u8  lit_bytes[MAX_LITERAL_SIZE]; // byte array literal
    i64 lit_int;                     // integer literal
    i64 ref_node[4];                 // references to other nodes
  };
} node_t;

//  A procedure is a collection of semantic nodes
//  and has a string name.
//

typedef struct {
  i64 name_size;
  c8  name[MAX_NAME_SIZE];
  i64 node_count;
  i64 nodes[MAX_NODE_COUNT];
} proc_t;

//  A compilation unit is a collection of procedures.
//

typedef struct {
  i16 type;
  i64 entry_point;
  i64 proc_count;
  i64 procs[MAX_PROC_COUNT];
} unit_t;

//  An entity can be any of:
//  - `node_t`
//  - `proc_t`
//  - `unit_t`
//
//  Every entity can be referenced by it's unique index
//  in the entity pool.
//
//  If the entity's data doesn't fit in one entity, tail is
//  an index that leads to the entity with the rest of the
//  data, forming a linked list.
//

typedef struct {
  b8  is_enabled;
  i16 type;
  i64 tail;
  union {
    node_t node;
    proc_t proc;
    unit_t unit;
  };
} entity_t;

//  Pool, a collection of all entities.
//

typedef struct {
  i64      entity_count;
  entity_t entities[MAX_ENTITY_COUNT];
} entity_pool_t;

//  IR building procs
//

i64 pool_entity_add(entity_pool_t *pool, entity_t data) {
  assert(pool != NULL);
  assert(pool->entity_count < MAX_ENTITY_COUNT);

  i64 id             = pool->entity_count++;
  data.is_enabled    = 1,
  pool->entities[id] = data;

  return id;
}

void pool_entity_remove(entity_pool_t *pool, i64 entity, i16 type) {
  assert(pool != NULL);
  assert(pool->entities[entity].is_enabled);
  assert(pool->entities[entity].type == type);

  pool->entities[entity].is_enabled = 1;
}

i64 node_init(entity_pool_t *pool, node_t data) {
  return pool_entity_add(pool, (entity_t) {
    .type = TYPE_NODE,
    .tail = UNDEFINED,
    .node = data,
  });
}

void node_destroy(entity_pool_t *pool, i64 node) {
  pool_entity_remove(pool, node, TYPE_NODE);
}

i64 node_op_i64(entity_pool_t *pool, i64 value) {
  return node_init(pool, (node_t) {
    .op      = OP_I64,
    .flow    = FLOW_DATA,
    .lit_int = value,
  });
}

i64 node_op_ret(entity_pool_t *pool, i64 node_return_value) {
  return node_init(pool, (node_t) {
    .op       = OP_RET,
    .flow     = FLOW_CONTROL,
    .ref_node = node_return_value,
  });
}

i64 proc_init(entity_pool_t *pool) {
  return pool_entity_add(pool, (entity_t) {
    .type = TYPE_PROC,
    .tail = UNDEFINED,
  });
}

void proc_destroy(entity_pool_t *pool, i64 proc) {
  pool_entity_remove(pool, proc, TYPE_PROC);
}

void proc_set_name(entity_pool_t *pool, i64 proc, i64 name_size, c8 const *name) {
  assert(pool != NULL);
  assert(pool->entities[proc].is_enabled);
  assert(pool->entities[proc].type == TYPE_PROC);

  //  TODO
  //  Implement large entities.
  assert(name_size <= MAX_NAME_SIZE);
  assert(name_size > 0);

  proc_t *p    = &pool->entities[proc].proc;
  p->name_size = name_size;
  memcpy(p->name, name, name_size);
}

void proc_node_add(entity_pool_t *pool, i64 proc, i64 node) {
  assert(pool != NULL);
  assert(pool->entities[proc].is_enabled);
  assert(pool->entities[proc].type == TYPE_PROC);
  assert(pool->entities[node].is_enabled);
  assert(pool->entities[node].type == TYPE_NODE);

  proc_t *p = &pool->entities[proc].proc;

  //  TODO
  //  Implement large entities.
  assert(p->node_count < MAX_NODE_COUNT);

  p->nodes[p->node_count++] = node;
}

void proc_node_remove(entity_pool_t *pool, i64 proc, i64 node) {
  assert(pool != NULL);
  assert(pool->entities[proc].is_enabled);
  assert(pool->entities[proc].type == TYPE_PROC);
  assert(pool->entities[node].type == TYPE_NODE);

  pool->entities[proc].proc.nodes[node] = UNDEFINED;
}

i64 unit_init(entity_pool_t *pool) {
  return pool_entity_add(pool, (entity_t) {
    .type = TYPE_UNIT,
    .tail = UNDEFINED,
    .unit = (unit_t) {
      .entry_point = UNDEFINED,
    }
  });
}

void unit_destroy(entity_pool_t *pool, i64 unit) {
  pool_entity_remove(pool, unit, TYPE_UNIT);
}

void unit_proc_add(entity_pool_t *pool, i64 unit, i64 proc) {
  assert(pool != NULL);
  assert(pool->entities[unit].is_enabled);
  assert(pool->entities[unit].type == TYPE_UNIT);
  assert(pool->entities[proc].is_enabled);
  assert(pool->entities[proc].type == TYPE_PROC);

  unit_t *u = &pool->entities[unit].unit;

  //  TODO
  //  Implement large entities.
  assert(u->proc_count < MAX_PROC_COUNT);

  u->procs[u->proc_count++] = proc;
}

void unit_proc_remove(entity_pool_t *pool, i64 unit, i64 proc) {
  assert(pool != NULL);
  assert(pool->entities[unit].is_enabled);
  assert(pool->entities[unit].type == TYPE_UNIT);
  assert(pool->entities[proc].type == TYPE_PROC);

  pool->entities[unit].unit.procs[proc] = UNDEFINED;
}

void unit_set_entry_point(entity_pool_t *pool, i64 unit, i64 entry_point_proc) {
  assert(pool != NULL);
  assert(pool->entities[unit].is_enabled);
  assert(pool->entities[unit].type == TYPE_UNIT);
  assert(pool->entities[entry_point_proc].type == TYPE_PROC);

  pool->entities[unit].unit.entry_point = entry_point_proc;
}

void unit_write(entity_pool_t *pool, i64 unit, i16 target, FILE *out) {
  assert(pool != NULL);
  assert(pool->entities[unit].is_enabled);
  assert(pool->entities[unit].unit.entry_point != UNDEFINED);
  assert(out != NULL);
  assert(target == (FORMAT_ELF | ARCH_X86_64));

  //  TODO
  printf("[TODO] Writing ELF x86_64 executable...");
}

//  ================================================================
//
//    Helpers
//
//  ================================================================

//  Global state
//

static entity_pool_t g_pool = { 0 };

//  Handy procedures
//

i64 n_i64(i64 value) {
  return node_op_i64(&g_pool, value);
}

i64 n_ret(i64 node_return_value) {
  return node_op_ret(&g_pool, node_return_value);
}

i64 p_new(c8 const *name) {
  i64 p = proc_init(&g_pool);
  proc_set_name(&g_pool, p, strlen(name), name);
  return p;
}

void p_add(i64 proc, i64 node) {
  proc_node_add(&g_pool, proc, node);
}

i64 u_new() {
  return unit_init(&g_pool);
}

void u_add(i64 unit, i64 proc) {
  unit_proc_add(&g_pool, unit, proc);
}

void u_entry_point(i64 unit, i64 proc) {
  unit_set_entry_point(&g_pool, unit, proc);
}

void u_elf_x86_64(i64 unit, c8 const *output_file_name) {
  FILE *f = fopen(output_file_name, "wb");
  assert(f != NULL);
  unit_write(&g_pool, unit, FORMAT_ELF | ARCH_X86_64, f);
  fclose(f);
}

//  ================================================================
//
//    Code generation proc
//
//  ================================================================

int main(int argc, char **argv) {
  printf("node - %d bytes\n", (i32) sizeof(node_t));
  printf("proc - %d bytes\n", (i32) sizeof(proc_t));
  printf("unit - %d bytes\n", (i32) sizeof(unit_t));

  i64 main = p_new("main");
  i64 n0 = n_i64(42);
  p_add(main, n0);
  p_add(main, n_ret(n0));

  i64 u = u_new();
  u_add(u, main);
  u_entry_point(u, main);
  u_elf_x86_64(u, "test_foo");

  printf("\nBye!\n");
  return 0;
}