DEADSOFTWARE

24529e0e26add6f595560626773029ce65f903e6
[dsw-obn.git] / generator.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <assert.h>
8 #include "oberon.h"
9 #include "generator.h"
11 #include <libgccjit.h>
13 static void printcontext(oberon_context_t * ctx, char * s)
14 {
15 /*
16 gen_context_t * gen_context = ctx -> gen_context;
17 gcc_jit_context * gcc_context = gen_context -> gcc_context;
18 gcc_jit_block * gcc_block = gen_context -> gcc_block;
20 printf("%s:\n", s);
21 printf(" ctx = %p:\n", ctx);
22 printf(" gctx = %p:\n", gctx);
23 printf(" context = %p:\n", context);
24 printf(" block = %p:\n", block);
25 */
26 }
28 // =======================================================================
29 // ALLOC
30 // =======================================================================
32 void
33 oberon_generator_init_context(oberon_context_t * ctx)
34 {
35 gen_context_t * gen_context = malloc(sizeof *gen_context);
36 memset(gen_context, 0, sizeof *gen_context);
38 gcc_jit_context * gcc_context;
39 gcc_context = gcc_jit_context_acquire();
41 ctx -> gen_context = gen_context;
42 gen_context -> gcc_context = gcc_context;
44 printcontext(ctx, "oberon_generator_init_context");
45 }
47 void
48 oberon_generator_destroy_context(oberon_context_t * ctx)
49 {
50 printcontext(ctx, "oberon_generator_destroy_context");
52 gen_context_t * gen_context = ctx -> gen_context;
53 gcc_jit_context * gcc_context = gen_context -> gcc_context;
55 gcc_jit_context_release(gcc_context);
56 }
58 void
59 oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
60 {
61 gen_type_t * gen_type = malloc(sizeof *gen_type);
62 memset(gen_type, 0, sizeof *gen_type);
63 type -> gen_type = gen_type;
65 gen_context_t * gen_context = ctx -> gen_context;
66 gcc_jit_context * gcc_context = gen_context -> gcc_context;
68 gcc_jit_type * gcc_type = NULL;
69 gcc_jit_struct * gcc_struct = NULL;
70 if(type -> class == OBERON_TYPE_VOID)
71 {
72 gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
73 }
74 else if(type -> class == OBERON_TYPE_INTEGER)
75 {
76 gcc_type = gcc_jit_context_get_int_type(gcc_context, type -> size, 1);
77 }
78 else if(type -> class == OBERON_TYPE_BOOLEAN)
79 {
80 gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
81 }
82 else if(type -> class == OBERON_TYPE_ARRAY)
83 {
84 gen_type_t * gen_base = type -> base -> gen_type;
85 gcc_jit_type * gcc_base = gen_base -> gcc_type;
86 gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size);
87 }
88 else if(type -> class == OBERON_TYPE_RECORD)
89 {
90 gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, "");
91 gcc_type = gcc_jit_struct_as_type(gcc_struct);
92 }
93 else if(type -> class == OBERON_TYPE_POINTER)
94 {
95 gen_type_t * gen_base = type -> base -> gen_type;
96 gcc_jit_type * gcc_base = gen_base -> gcc_type;
97 gcc_type = gcc_jit_type_get_pointer(gcc_base);
98 }
99 else if(type -> class == OBERON_TYPE_PROCEDURE)
101 int num_params = type -> num_decl;
102 gcc_jit_type * params[num_params];
103 oberon_object_t * o = type -> decl;
104 for(int i = 0; i < num_params; i++)
106 gen_type_t * gen_type = o -> type -> gen_type;
107 params[i] = gen_type -> gcc_type;
108 o = o -> next;
111 gen_type_t * base = type -> base -> gen_type;
112 gcc_jit_type * result_type = base -> gcc_type;
114 gcc_type = gcc_jit_context_new_function_ptr_type(
115 gcc_context, NULL, result_type, num_params, params, 0
116 );
118 else
120 oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class);
123 assert(gcc_type);
124 gen_type -> gcc_type = gcc_type;
125 gen_type -> gcc_struct = gcc_struct;
128 void
129 oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type)
131 assert(type -> class == OBERON_TYPE_RECORD);
133 gen_type_t * gen_type = type -> gen_type;
134 gcc_jit_struct * gcc_struct = gen_type -> gcc_struct;
136 // TODO type exstension
138 int num_fields = type -> num_decl;
139 gcc_jit_field * fields[num_fields];
140 oberon_object_t * o = type -> decl;
141 for(int i = 0; i < num_fields; i++)
143 assert(o -> class == OBERON_CLASS_FIELD);
144 gen_var_t * var = o -> gen_var;
145 fields[i] = var -> gcc_field;
146 o = o -> next;
149 gcc_jit_struct_set_fields (gcc_struct, NULL, num_fields, fields);
151 //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
154 void
155 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
157 gen_context_t * gen_context = ctx -> gen_context;
158 gen_type_t * gen_type = var -> type -> gen_type;
160 gen_var_t * gen_var = malloc(sizeof *gen_var);
161 memset(gen_var, 0, sizeof *gen_var);
162 var -> gen_var = gen_var;
164 gcc_jit_context * gcc_context = gen_context -> gcc_context;
165 gcc_jit_type * gcc_type = gen_type -> gcc_type;
166 const char * name = var -> name;
168 // TODO var param
169 gcc_jit_lvalue * gcc_lvalue = NULL;
170 gcc_jit_param * gcc_param = NULL;
171 gcc_jit_field * gcc_field = NULL;
172 if(var -> class == OBERON_CLASS_VAR)
174 gcc_lvalue = gcc_jit_context_new_global(
175 gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
176 );
178 else if(var -> class == OBERON_CLASS_PARAM)
180 gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
181 gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
183 else if(var -> class == OBERON_CLASS_FIELD)
185 gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name);
187 else
189 oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class);
192 gen_var -> gcc_lvalue = gcc_lvalue;
193 gen_var -> gcc_param = gcc_param;
194 gen_var -> gcc_field = gcc_field;
197 void
198 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
200 assert(proc -> class == OBERON_CLASS_PROC);
202 gen_context_t * gen_context = ctx -> gen_context;
203 gcc_jit_context * gcc_context = gen_context -> gcc_context;
205 gen_proc_t * gen_proc = malloc(sizeof *gen_proc);
206 memset(gen_proc, 0, sizeof *gen_proc);
207 proc -> gen_proc = gen_proc;
209 const char * name = proc -> name;
210 gen_type_t * gen_result_type = proc -> type -> base -> gen_type;
211 gcc_jit_type * result_type = gen_result_type -> gcc_type;
213 /* Строим список параметров */
214 int num_param = proc -> type -> num_decl;
215 oberon_object_t * o = proc -> type -> decl;
216 gcc_jit_param * params[num_param];
217 for(int i = 0; i < num_param; i++)
219 gen_var_t * param_var = o -> gen_var;
220 params[i] = param_var -> gcc_param;
221 o = o -> next;
224 gcc_jit_function * gcc_func;
225 gcc_func = gcc_jit_context_new_function(
226 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, result_type, name, num_param, params, 0
227 );
229 gen_proc -> gcc_func = gcc_func;
232 // =======================================================================
233 // GENERATOR
234 // =======================================================================
236 static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item);
237 static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr);
239 void
240 oberon_generate_begin_module(oberon_context_t * ctx)
242 printcontext(ctx, "oberon_generate_begin_module");
244 gen_context_t * gen_context = ctx -> gen_context;
245 gcc_jit_context * gcc_context = gen_context -> gcc_context;
247 gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
248 gcc_jit_function * func = gcc_jit_context_new_function(
249 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, "BEGIN", 0, NULL, 0
250 );
251 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
253 gen_context -> gcc_block = gcc_block;
256 void
257 oberon_generate_end_module(oberon_context_t * ctx)
259 printcontext(ctx, "oberon_generate_end_module");
261 gen_context_t * gen_context = ctx -> gen_context;
262 gcc_jit_block * gcc_block = gen_context -> gcc_block;
264 gcc_jit_block_end_with_void_return(gcc_block, NULL);
266 gen_context -> gcc_block = NULL;
269 void
270 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
272 gen_context_t * gen_context = ctx -> gen_context;
273 gen_proc_t * gen_proc = proc -> gen_proc;
275 gcc_jit_function * func = gen_proc -> gcc_func;
276 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
278 // TODO make stack for block
279 gen_context -> gcc_block = gcc_block;
282 void
283 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
285 gen_context_t * gen_context = ctx -> gen_context;
286 gcc_jit_block * block = gen_context -> gcc_block;
288 gcc_jit_rvalue * return_value;
289 return_value = rvalue_from_expr(ctx, desig);
290 gcc_jit_block_add_eval(block, NULL, return_value);
293 void
294 oberon_generate_end_proc(oberon_context_t * ctx)
296 gen_context_t * gen_context = ctx -> gen_context;
297 gen_context -> gcc_block = NULL;
300 void
301 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
303 gen_context_t * gen_context = ctx -> gen_context;
304 gcc_jit_block * gcc_block = gen_context -> gcc_block;
306 if(expr == NULL)
308 gcc_jit_block_end_with_void_return(gcc_block, NULL);
310 else
312 gcc_jit_rvalue * r = rvalue_from_expr(ctx, expr);
313 gcc_jit_block_end_with_return(gcc_block, NULL, r);
317 static gcc_jit_lvalue *
318 lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
320 gen_context_t * gen_context = ctx -> gen_context;
321 gcc_jit_context * gcc_context = gen_context -> gcc_context;
323 gcc_jit_lvalue * left;
325 if(item -> mode == MODE_VAR)
327 gen_var_t * gen_var = item -> var -> gen_var;
328 left = gen_var -> gcc_lvalue;
330 else if(item -> mode == MODE_INDEX)
332 assert(item -> num_args == 1);
333 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
334 gcc_jit_rvalue * index = rvalue_from_expr(ctx, item -> args);
335 left = gcc_jit_context_new_array_access(gcc_context, NULL, parent, index);
337 else if(item -> mode == MODE_FIELD)
339 printf("lvalue_from_item: %s\n", item -> var -> name);
340 gen_var_t * gen_var = item -> var -> gen_var;
341 gcc_jit_field * gcc_field = gen_var -> gcc_field;
343 gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent);
344 left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field);
346 else if(item -> mode == MODE_DEREF)
348 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
349 left = gcc_jit_rvalue_dereference(parent, NULL);
351 else
353 oberon_error(ctx, "invalid lvalue expression");
356 return left;
359 static gcc_jit_lvalue *
360 lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr)
362 gcc_jit_lvalue * left;
363 oberon_item_t * item;
365 if(expr -> is_item)
367 item = (oberon_item_t *) expr;
368 left = lvalue_from_item(ctx, item);
370 else
372 oberon_error(ctx, "invalid lvalue expression");
375 return left;
378 static gcc_jit_rvalue *
379 rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
381 gen_context_t * gen_context = ctx -> gen_context;
382 gcc_jit_context * gcc_context = gen_context -> gcc_context;
384 gcc_jit_rvalue * right;
385 if(item -> mode == MODE_VAR)
387 assert(item -> var -> class == OBERON_CLASS_VAR
388 || item -> var -> class == OBERON_CLASS_PARAM);
389 gen_var_t * gen_var = item -> var -> gen_var;
390 right = gcc_jit_lvalue_as_rvalue(gen_var -> gcc_lvalue);
392 else if(item -> mode == MODE_INTEGER)
394 gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_INT);
395 right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> integer);
397 else if(item -> mode == MODE_BOOLEAN)
399 gcc_jit_type * bool_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
400 if(item -> boolean)
402 right = gcc_jit_context_one(gcc_context, bool_type);
404 else
406 right = gcc_jit_context_zero(gcc_context, bool_type);
409 else if(item -> mode == MODE_CALL)
411 assert(item -> var -> class == OBERON_CLASS_PROC);
413 gen_proc_t * gen_proc = item -> var -> gen_proc;
415 int num_args = item -> num_args;
416 gcc_jit_rvalue *args[num_args];
418 oberon_expr_t * expr = item -> args;
419 for(int i = 0; i < num_args; i++)
421 args[i] = rvalue_from_expr(ctx, expr);
422 expr = expr -> next;
425 gcc_jit_function * func = gen_proc -> gcc_func;
426 right = gcc_jit_context_new_call(
427 gcc_context, NULL, func, num_args, args
428 );
430 else if(item -> mode == MODE_INDEX)
432 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
433 right = gcc_jit_lvalue_as_rvalue(left);
435 else if(item -> mode == MODE_FIELD)
437 gen_var_t * gen_var = item -> var -> gen_var;
438 gcc_jit_field * gcc_field = gen_var -> gcc_field;
440 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
441 right = gcc_jit_rvalue_access_field(parent, NULL, gcc_field);
443 else if(item -> mode == MODE_DEREF)
445 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
446 right = gcc_jit_lvalue_as_rvalue(left);
448 else if(item -> mode == MODE_NIL)
450 gcc_jit_type * type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR);
451 right = gcc_jit_context_null(gcc_context, type);
453 else
455 oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
458 return right;
461 struct {
462 int type; // 0 - unary, 1 - binary, 2 - comp
463 union {
464 enum gcc_jit_unary_op unary_op;
465 enum gcc_jit_binary_op binary_op;
466 enum gcc_jit_comparison comp_op;
467 };
468 } op_table[] = {
469 { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE },
470 { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
472 { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
473 { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
474 { 1, .binary_op = GCC_JIT_BINARY_OP_MULT },
475 { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
476 { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
477 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND },
478 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR },
480 { 2, .comp_op = GCC_JIT_COMPARISON_EQ },
481 { 2, .comp_op = GCC_JIT_COMPARISON_NE },
482 { 2, .comp_op = GCC_JIT_COMPARISON_LT },
483 { 2, .comp_op = GCC_JIT_COMPARISON_LE },
484 { 2, .comp_op = GCC_JIT_COMPARISON_GT },
485 { 2, .comp_op = GCC_JIT_COMPARISON_GE }
486 };
488 static gcc_jit_rvalue *
489 rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator)
491 gcc_jit_rvalue * right;
493 gen_context_t * gen_context = ctx -> gen_context;
494 gen_type_t * gen_type = operator -> result -> gen_type;
495 gcc_jit_context * gcc_context = gen_context -> gcc_context;
496 gcc_jit_type * result_type = gen_type -> gcc_type;
498 int expr_type = op_table[operator -> op].type;
499 if(expr_type == 0)
501 enum gcc_jit_unary_op op = op_table[operator -> op].unary_op;
502 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
503 right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l);
505 else if(expr_type == 1)
507 enum gcc_jit_unary_op op = op_table[operator -> op].binary_op;
508 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
509 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
510 right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r);
512 else if(expr_type == 2)
514 enum gcc_jit_comparison op = op_table[operator -> op].comp_op;
515 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
516 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
517 right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r);
519 else
521 oberon_error(ctx, "rvalue_from_operator: wat");
524 return right;
527 static gcc_jit_rvalue *
528 rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr)
530 gcc_jit_rvalue * right;
532 if(expr -> is_item)
534 oberon_item_t * item = (oberon_item_t *) expr;
535 right = rvalue_from_item(ctx, item);
537 else
539 oberon_oper_t * operator = (oberon_oper_t *) expr;
540 right = rvalue_from_operator(ctx, operator);
543 return right;
546 void
547 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
549 gcc_jit_lvalue * left;
550 left = lvalue_from_expr(ctx, dst);
552 gcc_jit_rvalue * right;
553 right = rvalue_from_expr(ctx, src);
555 if(src -> is_item)
557 if(src -> item.mode == MODE_NIL)
559 gen_context_t * gen_context = ctx -> gen_context;
560 gcc_jit_context * gcc_context = gen_context -> gcc_context;
561 gen_type_t * gen_type = dst -> result -> gen_type;
562 gcc_jit_type * cast_to_type = gen_type -> gcc_type;
563 right = gcc_jit_context_new_cast(gcc_context, NULL, right, cast_to_type);
567 gen_context_t * gen_context = ctx -> gen_context;
568 gcc_jit_block * gcc_block = gen_context -> gcc_block;
569 gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
572 void
573 oberon_generate_code(oberon_context_t * ctx)
575 gen_context_t * gen_context = ctx -> gen_context;
576 gcc_jit_context * gcc_context = gen_context -> gcc_context;
578 gcc_jit_result * gcc_result;
579 gcc_result = gcc_jit_context_compile(gcc_context);
581 gen_context -> gcc_result = gcc_result;
582 ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
585 void
586 oberon_generator_dump(oberon_context_t * ctx, char * path)
588 gen_context_t * gen_context = ctx -> gen_context;
589 gcc_jit_context * gcc_context = gen_context -> gcc_context;
590 gcc_jit_context_dump_to_file(gcc_context, path, 0);