DEADSOFTWARE

b17a8cd977cf450733a38fe9510b01db8754bd6f
[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 static void printcontext(oberon_context_t * ctx, char * s)
12 {
13 /*
14 gen_context_t * gen_context = ctx -> gen_context;
15 gcc_jit_context * gcc_context = gen_context -> gcc_context;
16 gcc_jit_block * gcc_block = gen_context -> gcc_block;
18 printf("%s:\n", s);
19 printf(" ctx = %p:\n", ctx);
20 printf(" gctx = %p:\n", gctx);
21 printf(" context = %p:\n", context);
22 printf(" block = %p:\n", block);
23 */
24 }
26 // =======================================================================
27 // ALLOC
28 // =======================================================================
30 static void
31 oberon_generator_open_block(gen_context_t * gen_context, gcc_jit_block * gcc_block)
32 {
33 gen_block_t * block = malloc(sizeof *block);
34 memset(block, 0, sizeof *block);
36 block -> gcc_block = gcc_block;
37 block -> up = gen_context -> block;
39 gen_context -> block = block;
40 }
42 static void
43 oberon_generator_close_block(gen_context_t * gen_context)
44 {
45 gen_context -> block = gen_context -> block -> up;
46 }
48 void
49 oberon_generator_init_context(oberon_context_t * ctx)
50 {
51 gen_context_t * gen_context = malloc(sizeof *gen_context);
52 memset(gen_context, 0, sizeof *gen_context);
54 gcc_jit_context * gcc_context;
55 gcc_context = gcc_jit_context_acquire();
57 ctx -> gen_context = gen_context;
58 gen_context -> gcc_context = gcc_context;
60 printcontext(ctx, "oberon_generator_init_context");
61 }
63 void
64 oberon_generator_destroy_context(oberon_context_t * ctx)
65 {
66 printcontext(ctx, "oberon_generator_destroy_context");
68 gen_context_t * gen_context = ctx -> gen_context;
69 gcc_jit_context * gcc_context = gen_context -> gcc_context;
71 gcc_jit_context_release(gcc_context);
72 }
74 void
75 oberon_generator_init_type(oberon_context_t * ctx, oberon_type_t * type)
76 {
77 gen_type_t * gen_type = malloc(sizeof *gen_type);
78 memset(gen_type, 0, sizeof *gen_type);
79 type -> gen_type = gen_type;
81 gen_context_t * gen_context = ctx -> gen_context;
82 gcc_jit_context * gcc_context = gen_context -> gcc_context;
84 gcc_jit_type * gcc_type = NULL;
85 gcc_jit_struct * gcc_struct = NULL;
86 if(type -> class == OBERON_TYPE_VOID)
87 {
88 gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
89 }
90 else if(type -> class == OBERON_TYPE_INTEGER)
91 {
92 gcc_type = gcc_jit_context_get_int_type(gcc_context, type -> size, 1);
93 }
94 else if(type -> class == OBERON_TYPE_BOOLEAN)
95 {
96 gcc_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
97 }
98 else if(type -> class == OBERON_TYPE_ARRAY)
99 {
100 gen_type_t * gen_base = type -> base -> gen_type;
101 gcc_jit_type * gcc_base = gen_base -> gcc_type;
102 gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size);
104 else if(type -> class == OBERON_TYPE_RECORD)
106 char name[32];
107 snprintf(name, 32, "RECORD%u", gen_context -> record_count);
108 gen_context -> record_count += 1;
110 gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, name);
111 gcc_type = gcc_jit_struct_as_type(gcc_struct);
113 else if(type -> class == OBERON_TYPE_POINTER)
115 gen_type_t * gen_base = type -> base -> gen_type;
116 gcc_jit_type * gcc_base = gen_base -> gcc_type;
117 gcc_type = gcc_jit_type_get_pointer(gcc_base);
119 else if(type -> class == OBERON_TYPE_PROCEDURE)
121 int num_params = type -> num_decl;
122 gcc_jit_type * params[num_params];
123 oberon_object_t * o = type -> decl;
124 for(int i = 0; i < num_params; i++)
126 gen_type_t * gen_type = o -> type -> gen_type;
127 params[i] = gen_type -> gcc_type;
128 o = o -> next;
131 gen_type_t * base = type -> base -> gen_type;
132 gcc_jit_type * result_type = base -> gcc_type;
134 gcc_type = gcc_jit_context_new_function_ptr_type(
135 gcc_context, NULL, result_type, num_params, params, 0
136 );
138 else
140 oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class);
143 assert(gcc_type);
144 gen_type -> gcc_type = gcc_type;
145 gen_type -> gcc_struct = gcc_struct;
148 void
149 oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type)
151 assert(type -> class == OBERON_TYPE_RECORD);
153 gen_type_t * gen_type = type -> gen_type;
154 gcc_jit_struct * gcc_struct = gen_type -> gcc_struct;
156 // TODO type exstension
158 int num_fields = type -> num_decl;
159 gcc_jit_field * fields[num_fields];
160 oberon_object_t * o = type -> decl;
161 for(int i = 0; i < num_fields; i++)
163 assert(o -> class == OBERON_CLASS_FIELD);
164 gen_var_t * var = o -> gen_var;
165 fields[i] = var -> gcc_field;
166 o = o -> next;
169 gcc_jit_struct_set_fields (gcc_struct, NULL, num_fields, fields);
171 //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
174 void
175 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
177 gen_context_t * gen_context = ctx -> gen_context;
178 gen_type_t * gen_type = var -> type -> gen_type;
180 gen_var_t * gen_var = malloc(sizeof *gen_var);
181 memset(gen_var, 0, sizeof *gen_var);
182 var -> gen_var = gen_var;
184 gcc_jit_context * gcc_context = gen_context -> gcc_context;
185 gcc_jit_type * gcc_type = gen_type -> gcc_type;
186 const char * name = var -> name;
188 gcc_jit_lvalue * gcc_lvalue = NULL;
189 gcc_jit_param * gcc_param = NULL;
190 gcc_jit_field * gcc_field = NULL;
191 if(var -> class == OBERON_CLASS_VAR)
193 if(var -> local)
195 gen_proc_t * gen_func = var -> parent -> gen_proc;
196 gcc_jit_function * func = gen_func -> gcc_func;
198 gcc_lvalue = gcc_jit_function_new_local(func, NULL, gcc_type, name);
200 else
202 gcc_lvalue = gcc_jit_context_new_global(
203 gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
204 );
207 else if(var -> class == OBERON_CLASS_PARAM)
209 gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
210 gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
212 else if(var -> class == OBERON_CLASS_VAR_PARAM)
214 gcc_type = gcc_jit_type_get_pointer(gcc_type);
215 gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
216 gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
218 else if(var -> class == OBERON_CLASS_FIELD)
220 gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name);
222 else
224 oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class);
227 gen_var -> gcc_lvalue = gcc_lvalue;
228 gen_var -> gcc_param = gcc_param;
229 gen_var -> gcc_field = gcc_field;
232 void
233 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
235 if(proc -> local)
237 oberon_error(ctx, "generator: local procedures not supported");
240 gen_context_t * gen_context = ctx -> gen_context;
241 gcc_jit_context * gcc_context = gen_context -> gcc_context;
243 gen_proc_t * gen_proc = malloc(sizeof *gen_proc);
244 memset(gen_proc, 0, sizeof *gen_proc);
245 proc -> gen_proc = gen_proc;
247 const char * name = proc -> name;
248 gen_type_t * gen_result_type = proc -> type -> base -> gen_type;
249 gcc_jit_type * result_type = gen_result_type -> gcc_type;
251 /* Строим список параметров */
252 int num_param = proc -> type -> num_decl;
253 oberon_object_t * o = proc -> type -> decl;
254 gcc_jit_param * params[num_param];
255 for(int i = 0; i < num_param; i++)
257 gen_var_t * param_var = o -> gen_var;
258 params[i] = param_var -> gcc_param;
259 o = o -> next;
262 gcc_jit_function * gcc_func;
263 gcc_func = gcc_jit_context_new_function(
264 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, result_type, name, num_param, params, 0
265 );
267 gen_proc -> gcc_func = gcc_func;
270 // =======================================================================
271 // GENERATOR
272 // =======================================================================
274 static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item);
275 static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr);
277 void
278 oberon_generate_begin_module(oberon_context_t * ctx)
280 printcontext(ctx, "oberon_generate_begin_module");
282 gen_context_t * gen_context = ctx -> gen_context;
283 gcc_jit_context * gcc_context = gen_context -> gcc_context;
285 gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
286 gcc_jit_function * func = gcc_jit_context_new_function(
287 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, "BEGIN", 0, NULL, 0
288 );
289 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
291 oberon_generator_open_block(gen_context, gcc_block);
294 void
295 oberon_generate_end_module(oberon_context_t * ctx)
297 printcontext(ctx, "oberon_generate_end_module");
299 gen_context_t * gen_context = ctx -> gen_context;
300 gcc_jit_block * gcc_block = gen_context -> block -> gcc_block;
302 gcc_jit_block_end_with_void_return(gcc_block, NULL);
304 oberon_generator_close_block(gen_context);
307 void
308 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
310 gen_context_t * gen_context = ctx -> gen_context;
311 gen_proc_t * gen_proc = proc -> gen_proc;
313 gcc_jit_function * func = gen_proc -> gcc_func;
314 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
316 oberon_generator_open_block(gen_context, gcc_block);
319 void
320 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
322 gen_context_t * gen_context = ctx -> gen_context;
323 gen_block_t * gen_block = gen_context -> block;
324 gcc_jit_block * block = gen_block -> gcc_block;
326 gcc_jit_rvalue * return_value;
327 return_value = rvalue_from_expr(ctx, desig);
328 gcc_jit_block_add_eval(block, NULL, return_value);
331 void
332 oberon_generate_end_proc(oberon_context_t * ctx)
334 gen_context_t * gen_context = ctx -> gen_context;
335 oberon_generator_close_block(gen_context);
338 void
339 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
341 gen_context_t * gen_context = ctx -> gen_context;
342 gen_block_t * gen_block = gen_context -> block;
343 gcc_jit_block * gcc_block = gen_block -> gcc_block;
345 if(expr == NULL)
347 gcc_jit_block_end_with_void_return(gcc_block, NULL);
349 else
351 gcc_jit_rvalue * r = rvalue_from_expr(ctx, expr);
352 gcc_jit_block_end_with_return(gcc_block, NULL, r);
356 static gcc_jit_lvalue *
357 lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
359 gen_context_t * gen_context = ctx -> gen_context;
360 gcc_jit_context * gcc_context = gen_context -> gcc_context;
362 gcc_jit_lvalue * left;
364 if(item -> mode == MODE_VAR)
366 gen_var_t * gen_var = item -> var -> gen_var;
367 left = gen_var -> gcc_lvalue;
368 if(item -> var -> class == OBERON_CLASS_VAR_PARAM)
370 gcc_jit_rvalue * r = gcc_jit_lvalue_as_rvalue(left);
371 left = gcc_jit_rvalue_dereference(r, NULL);
374 else if(item -> mode == MODE_INDEX)
376 assert(item -> num_args == 1);
377 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
378 gcc_jit_rvalue * index = rvalue_from_expr(ctx, item -> args);
379 left = gcc_jit_context_new_array_access(gcc_context, NULL, parent, index);
381 else if(item -> mode == MODE_FIELD)
383 printf("lvalue_from_item: %s\n", item -> var -> name);
384 gen_var_t * gen_var = item -> var -> gen_var;
385 gcc_jit_field * gcc_field = gen_var -> gcc_field;
387 gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent);
388 left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field);
390 else if(item -> mode == MODE_DEREF)
392 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
393 left = gcc_jit_rvalue_dereference(parent, NULL);
395 else
397 oberon_error(ctx, "invalid lvalue expression");
400 return left;
403 static gcc_jit_lvalue *
404 lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr)
406 gcc_jit_lvalue * left;
407 oberon_item_t * item;
409 if(expr -> is_item)
411 item = (oberon_item_t *) expr;
412 left = lvalue_from_item(ctx, item);
414 else
416 oberon_error(ctx, "invalid lvalue expression");
419 return left;
422 static gcc_jit_rvalue *
423 rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
425 gen_context_t * gen_context = ctx -> gen_context;
426 gcc_jit_context * gcc_context = gen_context -> gcc_context;
428 gcc_jit_rvalue * right;
429 if(item -> mode == MODE_VAR)
431 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
432 right = gcc_jit_lvalue_as_rvalue(left);
434 else if(item -> mode == MODE_INTEGER)
436 gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_INT);
437 right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> integer);
439 else if(item -> mode == MODE_BOOLEAN)
441 gcc_jit_type * bool_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
442 if(item -> boolean)
444 right = gcc_jit_context_one(gcc_context, bool_type);
446 else
448 right = gcc_jit_context_zero(gcc_context, bool_type);
451 else if(item -> mode == MODE_CALL)
453 assert(item -> var -> class == OBERON_CLASS_PROC);
455 oberon_type_t * signature = item -> var -> type;
456 gen_proc_t * gen_proc = item -> var -> gen_proc;
458 int num_args = item -> num_args;
459 gcc_jit_rvalue *args[num_args];
461 oberon_expr_t * expr = item -> args;
462 oberon_object_t * arg_param = signature -> decl;
463 for(int i = 0; i < num_args; i++)
465 if(arg_param -> class == OBERON_CLASS_VAR_PARAM)
467 gcc_jit_lvalue * left = lvalue_from_expr(ctx, expr);
468 args[i] = gcc_jit_lvalue_get_address(left, NULL);
470 else
472 args[i] = rvalue_from_expr(ctx, expr);
474 expr = expr -> next;
475 arg_param = arg_param -> next;
478 gcc_jit_function * func = gen_proc -> gcc_func;
479 right = gcc_jit_context_new_call(
480 gcc_context, NULL, func, num_args, args
481 );
483 else if(item -> mode == MODE_INDEX)
485 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
486 right = gcc_jit_lvalue_as_rvalue(left);
488 else if(item -> mode == MODE_FIELD)
490 gen_var_t * gen_var = item -> var -> gen_var;
491 gcc_jit_field * gcc_field = gen_var -> gcc_field;
493 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
494 right = gcc_jit_rvalue_access_field(parent, NULL, gcc_field);
496 else if(item -> mode == MODE_DEREF)
498 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
499 right = gcc_jit_lvalue_as_rvalue(left);
501 else if(item -> mode == MODE_NIL)
503 gcc_jit_type * type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID_PTR);
504 right = gcc_jit_context_null(gcc_context, type);
506 else
508 oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
511 return right;
514 struct {
515 int type; // 0 - unary, 1 - binary, 2 - comp
516 union {
517 enum gcc_jit_unary_op unary_op;
518 enum gcc_jit_binary_op binary_op;
519 enum gcc_jit_comparison comp_op;
520 };
521 } op_table[] = {
522 { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE },
523 { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
525 { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
526 { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
527 { 1, .binary_op = GCC_JIT_BINARY_OP_MULT },
528 { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
529 { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
530 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND },
531 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR },
533 { 2, .comp_op = GCC_JIT_COMPARISON_EQ },
534 { 2, .comp_op = GCC_JIT_COMPARISON_NE },
535 { 2, .comp_op = GCC_JIT_COMPARISON_LT },
536 { 2, .comp_op = GCC_JIT_COMPARISON_LE },
537 { 2, .comp_op = GCC_JIT_COMPARISON_GT },
538 { 2, .comp_op = GCC_JIT_COMPARISON_GE }
539 };
541 static gcc_jit_rvalue *
542 rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator)
544 gcc_jit_rvalue * right;
546 gen_context_t * gen_context = ctx -> gen_context;
547 gen_type_t * gen_type = operator -> result -> gen_type;
548 gcc_jit_context * gcc_context = gen_context -> gcc_context;
549 gcc_jit_type * result_type = gen_type -> gcc_type;
551 int expr_type = op_table[operator -> op].type;
552 if(expr_type == 0)
554 enum gcc_jit_unary_op op = op_table[operator -> op].unary_op;
555 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
556 right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l);
558 else if(expr_type == 1)
560 enum gcc_jit_unary_op op = op_table[operator -> op].binary_op;
561 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
562 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
563 right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r);
565 else if(expr_type == 2)
567 enum gcc_jit_comparison op = op_table[operator -> op].comp_op;
568 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
569 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
570 right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r);
572 else
574 oberon_error(ctx, "rvalue_from_operator: wat");
577 return right;
580 static gcc_jit_rvalue *
581 rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr)
583 gcc_jit_rvalue * right;
585 if(expr -> is_item)
587 oberon_item_t * item = (oberon_item_t *) expr;
588 right = rvalue_from_item(ctx, item);
590 else
592 oberon_oper_t * operator = (oberon_oper_t *) expr;
593 right = rvalue_from_operator(ctx, operator);
596 return right;
599 void
600 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
602 gcc_jit_lvalue * left;
603 left = lvalue_from_expr(ctx, dst);
605 gcc_jit_rvalue * right;
606 right = rvalue_from_expr(ctx, src);
608 if(src -> is_item)
610 if(src -> item.mode == MODE_NIL)
612 gen_context_t * gen_context = ctx -> gen_context;
613 gcc_jit_context * gcc_context = gen_context -> gcc_context;
614 gen_type_t * gen_type = dst -> result -> gen_type;
615 gcc_jit_type * cast_to_type = gen_type -> gcc_type;
616 right = gcc_jit_context_new_cast(gcc_context, NULL, right, cast_to_type);
620 gen_context_t * gen_context = ctx -> gen_context;
621 gen_block_t * gen_block = gen_context -> block;
622 gcc_jit_block * gcc_block = gen_block -> gcc_block;
623 gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
626 void
627 oberon_generate_code(oberon_context_t * ctx)
629 gen_context_t * gen_context = ctx -> gen_context;
630 gcc_jit_context * gcc_context = gen_context -> gcc_context;
632 gcc_jit_result * gcc_result;
633 gcc_result = gcc_jit_context_compile(gcc_context);
635 gen_context -> gcc_result = gcc_result;
636 ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
639 void
640 oberon_generator_dump(oberon_context_t * ctx, char * path)
642 gen_context_t * gen_context = ctx -> gen_context;
643 gcc_jit_context * gcc_context = gen_context -> gcc_context;
644 gcc_jit_context_dump_to_file(gcc_context, path, 0);