DEADSOFTWARE

Добавлены объявления типов-записей и типов-процедур, добавлены и исправлены селекторы...
[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_PROCEDURE)
83 {
84 int num_params = type -> num_decl;
85 gcc_jit_type * params[num_params];
86 oberon_object_t * o = type -> decl;
87 for(int i = 0; i < num_params; i++)
88 {
89 gen_type_t * gen_type = o -> type -> gen_type;
90 params[i] = gen_type -> gcc_type;
91 o = o -> next;
92 }
94 gen_type_t * base = type -> base -> gen_type;
95 gcc_jit_type * result_type = base -> gcc_type;
97 gcc_type = gcc_jit_context_new_function_ptr_type(
98 gcc_context, NULL, result_type, num_params, params, 0
99 );
101 else if(type -> class == OBERON_TYPE_ARRAY)
103 if(type -> dim != 1)
105 oberon_error(ctx, "multidimension and open arrays not supported");
108 gen_type_t * gen_base = type -> base -> gen_type;
109 gcc_jit_type * gcc_base = gen_base -> gcc_type;
111 gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size);
113 else if(type -> class == OBERON_TYPE_RECORD)
115 // TODO type exstension
117 int num_fields = type -> num_decl;
118 gcc_jit_field * fields[num_fields];
119 oberon_object_t * o = type -> decl;
120 for(int i = 0; i < num_fields; i++)
122 assert(o -> class == OBERON_CLASS_FIELD);
123 gen_var_t * var = o -> gen_var;
124 fields[i] = var -> gcc_field;
125 o = o -> next;
128 gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
129 gcc_type = gcc_jit_struct_as_type(gcc_struct);
131 else
133 oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class);
136 gen_type -> gcc_type = gcc_type;
137 gen_type -> gcc_struct = gcc_struct;
140 void
141 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
143 gen_context_t * gen_context = ctx -> gen_context;
144 gen_type_t * gen_type = var -> type -> gen_type;
146 gen_var_t * gen_var = malloc(sizeof *gen_var);
147 memset(gen_var, 0, sizeof *gen_var);
148 var -> gen_var = gen_var;
150 gcc_jit_context * gcc_context = gen_context -> gcc_context;
151 gcc_jit_type * gcc_type = gen_type -> gcc_type;
152 const char * name = var -> name;
154 // TODO var param
155 gcc_jit_lvalue * gcc_lvalue = NULL;
156 gcc_jit_param * gcc_param = NULL;
157 gcc_jit_field * gcc_field = NULL;
158 if(var -> class == OBERON_CLASS_VAR)
160 gcc_lvalue = gcc_jit_context_new_global(
161 gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
162 );
164 else if(var -> class == OBERON_CLASS_PARAM)
166 gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
167 gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
169 else if(var -> class == OBERON_CLASS_FIELD)
171 gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name);
173 else
175 oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class);
178 gen_var -> gcc_lvalue = gcc_lvalue;
179 gen_var -> gcc_param = gcc_param;
180 gen_var -> gcc_field = gcc_field;
183 void
184 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
186 assert(proc -> class == OBERON_CLASS_PROC);
188 gen_context_t * gen_context = ctx -> gen_context;
189 gcc_jit_context * gcc_context = gen_context -> gcc_context;
191 gen_proc_t * gen_proc = malloc(sizeof *gen_proc);
192 memset(gen_proc, 0, sizeof *gen_proc);
193 proc -> gen_proc = gen_proc;
195 const char * name = proc -> name;
196 gen_type_t * gen_result_type = proc -> type -> base -> gen_type;
197 gcc_jit_type * result_type = gen_result_type -> gcc_type;
199 /* Строим список параметров */
200 int num_param = proc -> type -> num_decl;
201 oberon_object_t * o = proc -> type -> decl;
202 gcc_jit_param * params[num_param];
203 for(int i = 0; i < num_param; i++)
205 gen_var_t * param_var = o -> gen_var;
206 params[i] = param_var -> gcc_param;
207 o = o -> next;
210 gcc_jit_function * gcc_func;
211 gcc_func = gcc_jit_context_new_function(
212 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, result_type, name, num_param, params, 0
213 );
215 gen_proc -> gcc_func = gcc_func;
218 // =======================================================================
219 // GENERATOR
220 // =======================================================================
222 static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item);
223 static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr);
225 void
226 oberon_generate_begin_module(oberon_context_t * ctx)
228 printcontext(ctx, "oberon_generate_begin_module");
230 gen_context_t * gen_context = ctx -> gen_context;
231 gcc_jit_context * gcc_context = gen_context -> gcc_context;
233 gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
234 gcc_jit_function * func = gcc_jit_context_new_function(
235 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, "BEGIN", 0, NULL, 0
236 );
237 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
239 gen_context -> gcc_block = gcc_block;
242 void
243 oberon_generate_end_module(oberon_context_t * ctx)
245 printcontext(ctx, "oberon_generate_end_module");
247 gen_context_t * gen_context = ctx -> gen_context;
248 gcc_jit_block * gcc_block = gen_context -> gcc_block;
250 gcc_jit_block_end_with_void_return(gcc_block, NULL);
252 gen_context -> gcc_block = NULL;
255 void
256 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
258 gen_context_t * gen_context = ctx -> gen_context;
259 gen_proc_t * gen_proc = proc -> gen_proc;
261 gcc_jit_function * func = gen_proc -> gcc_func;
262 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
264 // TODO make stack for block
265 gen_context -> gcc_block = gcc_block;
268 void
269 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
271 gen_context_t * gen_context = ctx -> gen_context;
272 gcc_jit_block * block = gen_context -> gcc_block;
274 gcc_jit_rvalue * return_value;
275 return_value = rvalue_from_expr(ctx, desig);
276 gcc_jit_block_add_eval(block, NULL, return_value);
279 void
280 oberon_generate_end_proc(oberon_context_t * ctx)
282 gen_context_t * gen_context = ctx -> gen_context;
283 gen_context -> gcc_block = NULL;
286 void
287 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
289 gen_context_t * gen_context = ctx -> gen_context;
290 gcc_jit_block * gcc_block = gen_context -> gcc_block;
292 if(expr == NULL)
294 gcc_jit_block_end_with_void_return(gcc_block, NULL);
296 else
298 gcc_jit_rvalue * r = rvalue_from_expr(ctx, expr);
299 gcc_jit_block_end_with_return(gcc_block, NULL, r);
303 static gcc_jit_lvalue *
304 lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
306 gen_context_t * gen_context = ctx -> gen_context;
307 gcc_jit_context * gcc_context = gen_context -> gcc_context;
309 gcc_jit_lvalue * left;
311 if(item -> mode == MODE_VAR)
313 gen_var_t * gen_var = item -> var -> gen_var;
314 left = gen_var -> gcc_lvalue;
316 else if(item -> mode == MODE_INDEX)
318 assert(item -> num_args == 1);
319 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
320 gcc_jit_rvalue * index = rvalue_from_expr(ctx, item -> args);
321 left = gcc_jit_context_new_array_access(gcc_context, NULL, parent, index);
323 else if(item -> mode == MODE_FIELD)
325 gen_var_t * gen_var = item -> var -> gen_var;
326 gcc_jit_field * gcc_field = gen_var -> gcc_field;
328 gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent);
329 left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field);
331 else
333 oberon_error(ctx, "invalid lvalue expression");
336 return left;
339 static gcc_jit_lvalue *
340 lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr)
342 gcc_jit_lvalue * left;
343 oberon_item_t * item;
345 if(expr -> is_item)
347 item = (oberon_item_t *) expr;
348 left = lvalue_from_item(ctx, item);
350 else
352 oberon_error(ctx, "invalid lvalue expression");
355 return left;
358 static gcc_jit_rvalue *
359 rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
361 gen_context_t * gen_context = ctx -> gen_context;
362 gcc_jit_context * gcc_context = gen_context -> gcc_context;
364 gcc_jit_rvalue * right;
365 if(item -> mode == MODE_VAR)
367 assert(item -> var -> class == OBERON_CLASS_VAR
368 || item -> var -> class == OBERON_CLASS_PARAM);
369 gen_var_t * gen_var = item -> var -> gen_var;
370 right = gcc_jit_lvalue_as_rvalue(gen_var -> gcc_lvalue);
372 else if(item -> mode == MODE_INTEGER)
374 gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_INT);
375 right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> integer);
377 else if(item -> mode == MODE_BOOLEAN)
379 gcc_jit_type * bool_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
380 if(item -> boolean)
382 right = gcc_jit_context_one(gcc_context, bool_type);
384 else
386 right = gcc_jit_context_zero(gcc_context, bool_type);
389 else if(item -> mode == MODE_CALL)
391 assert(item -> var -> class == OBERON_CLASS_PROC);
393 gen_proc_t * gen_proc = item -> var -> gen_proc;
395 int num_args = item -> num_args;
396 gcc_jit_rvalue *args[num_args];
398 oberon_expr_t * expr = item -> args;
399 for(int i = 0; i < num_args; i++)
401 args[i] = rvalue_from_expr(ctx, expr);
402 expr = expr -> next;
405 gcc_jit_function * func = gen_proc -> gcc_func;
406 right = gcc_jit_context_new_call(
407 gcc_context, NULL, func, num_args, args
408 );
410 else if(item -> mode == MODE_INDEX)
412 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
413 right = gcc_jit_lvalue_as_rvalue(left);
415 else if(item -> mode == MODE_FIELD)
417 gen_var_t * gen_var = item -> var -> gen_var;
418 gcc_jit_field * gcc_field = gen_var -> gcc_field;
420 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
421 right = gcc_jit_rvalue_access_field(parent, NULL, gcc_field);
423 else
425 oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
428 return right;
431 struct {
432 int type; // 0 - unary, 1 - binary, 2 - comp
433 union {
434 enum gcc_jit_unary_op unary_op;
435 enum gcc_jit_binary_op binary_op;
436 enum gcc_jit_comparison comp_op;
437 };
438 } op_table[] = {
439 { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE },
440 { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
442 { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
443 { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
444 { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
445 { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
446 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND },
447 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR },
449 { 2, .comp_op = GCC_JIT_COMPARISON_EQ },
450 { 2, .comp_op = GCC_JIT_COMPARISON_NE },
451 { 2, .comp_op = GCC_JIT_COMPARISON_LT },
452 { 2, .comp_op = GCC_JIT_COMPARISON_LE },
453 { 2, .comp_op = GCC_JIT_COMPARISON_GT },
454 { 2, .comp_op = GCC_JIT_COMPARISON_GE }
455 };
457 static gcc_jit_rvalue *
458 rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator)
460 gcc_jit_rvalue * right;
462 gen_context_t * gen_context = ctx -> gen_context;
463 gen_type_t * gen_type = operator -> result -> gen_type;
464 gcc_jit_context * gcc_context = gen_context -> gcc_context;
465 gcc_jit_type * result_type = gen_type -> gcc_type;
467 int expr_type = op_table[operator -> op].type;
468 if(expr_type == 0)
470 enum gcc_jit_unary_op op = op_table[operator -> op].unary_op;
471 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
472 right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l);
474 else if(expr_type == 1)
476 enum gcc_jit_unary_op op = op_table[operator -> op].binary_op;
477 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
478 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
479 right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r);
481 else if(expr_type == 2)
483 enum gcc_jit_comparison op = op_table[operator -> op].comp_op;
484 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
485 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
486 right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r);
488 else
490 oberon_error(ctx, "rvalue_from_operator: wat");
493 return right;
496 static gcc_jit_rvalue *
497 rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr)
499 gcc_jit_rvalue * right;
501 if(expr -> is_item)
503 oberon_item_t * item = (oberon_item_t *) expr;
504 right = rvalue_from_item(ctx, item);
506 else
508 oberon_oper_t * operator = (oberon_oper_t *) expr;
509 right = rvalue_from_operator(ctx, operator);
512 return right;
515 void
516 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
518 gcc_jit_lvalue * left;
519 left = lvalue_from_expr(ctx, dst);
521 gcc_jit_rvalue * right;
522 right = rvalue_from_expr(ctx, src);
524 gen_context_t * gen_context = ctx -> gen_context;
525 gcc_jit_block * gcc_block = gen_context -> gcc_block;
526 gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
529 void
530 oberon_generate_code(oberon_context_t * ctx)
532 gen_context_t * gen_context = ctx -> gen_context;
533 gcc_jit_context * gcc_context = gen_context -> gcc_context;
535 gcc_jit_result * gcc_result;
536 gcc_result = gcc_jit_context_compile(gcc_context);
538 gen_context -> gcc_result = gcc_result;
539 ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");