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_ARRAY)
83 {
84 if(type -> dim != 1)
85 {
86 oberon_error(ctx, "multidimension and open arrays not supported");
87 }
89 gen_type_t * gen_base = type -> base -> gen_type;
90 gcc_jit_type * gcc_base = gen_base -> gcc_type;
92 gcc_type = gcc_jit_context_new_array_type(gcc_context, NULL, gcc_base, type -> size);
93 }
94 else if(type -> class == OBERON_TYPE_RECORD)
95 {
96 gcc_struct = gcc_jit_context_new_opaque_struct(gcc_context, NULL, "");
97 gcc_type = gcc_jit_struct_as_type(gcc_struct);
98 }
99 else if(type -> class == OBERON_TYPE_POINTER)
101 gen_type_t * gen_base = type -> base -> gen_type;
102 gcc_jit_type * gcc_base = gen_base -> gcc_type;
103 gcc_type = gcc_jit_type_get_pointer(gcc_base);
105 else if(type -> class == OBERON_TYPE_PROCEDURE)
107 int num_params = type -> num_decl;
108 gcc_jit_type * params[num_params];
109 oberon_object_t * o = type -> decl;
110 for(int i = 0; i < num_params; i++)
112 gen_type_t * gen_type = o -> type -> gen_type;
113 params[i] = gen_type -> gcc_type;
114 o = o -> next;
117 gen_type_t * base = type -> base -> gen_type;
118 gcc_jit_type * result_type = base -> gcc_type;
120 gcc_type = gcc_jit_context_new_function_ptr_type(
121 gcc_context, NULL, result_type, num_params, params, 0
122 );
124 else
126 oberon_error(ctx, "oberon_generator_init_type: invalid type class %i", type -> class);
129 assert(gcc_type);
130 gen_type -> gcc_type = gcc_type;
131 gen_type -> gcc_struct = gcc_struct;
134 void
135 oberon_generator_init_record(oberon_context_t * ctx, oberon_type_t * type)
137 assert(type -> class == OBERON_TYPE_RECORD);
139 gen_type_t * gen_type = type -> gen_type;
140 gcc_jit_struct * gcc_struct = gen_type -> gcc_struct;
142 // TODO type exstension
144 int num_fields = type -> num_decl;
145 gcc_jit_field * fields[num_fields];
146 oberon_object_t * o = type -> decl;
147 for(int i = 0; i < num_fields; i++)
149 assert(o -> class == OBERON_CLASS_FIELD);
150 gen_var_t * var = o -> gen_var;
151 fields[i] = var -> gcc_field;
152 o = o -> next;
155 gcc_jit_struct_set_fields (gcc_struct, NULL, num_fields, fields);
157 //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
160 void
161 oberon_generator_init_var(oberon_context_t * ctx, oberon_object_t * var)
163 gen_context_t * gen_context = ctx -> gen_context;
164 gen_type_t * gen_type = var -> type -> gen_type;
166 gen_var_t * gen_var = malloc(sizeof *gen_var);
167 memset(gen_var, 0, sizeof *gen_var);
168 var -> gen_var = gen_var;
170 gcc_jit_context * gcc_context = gen_context -> gcc_context;
171 gcc_jit_type * gcc_type = gen_type -> gcc_type;
172 const char * name = var -> name;
174 // TODO var param
175 gcc_jit_lvalue * gcc_lvalue = NULL;
176 gcc_jit_param * gcc_param = NULL;
177 gcc_jit_field * gcc_field = NULL;
178 if(var -> class == OBERON_CLASS_VAR)
180 gcc_lvalue = gcc_jit_context_new_global(
181 gcc_context, NULL, GCC_JIT_GLOBAL_INTERNAL, gcc_type, name
182 );
184 else if(var -> class == OBERON_CLASS_PARAM)
186 gcc_param = gcc_jit_context_new_param(gcc_context, NULL, gcc_type, name);
187 gcc_lvalue = gcc_jit_param_as_lvalue(gcc_param);
189 else if(var -> class == OBERON_CLASS_FIELD)
191 gcc_field = gcc_jit_context_new_field(gcc_context, NULL, gcc_type, name);
193 else
195 oberon_error(ctx, "oberon_generator_init_var: invalid class %i", var -> class);
198 gen_var -> gcc_lvalue = gcc_lvalue;
199 gen_var -> gcc_param = gcc_param;
200 gen_var -> gcc_field = gcc_field;
203 void
204 oberon_generator_init_proc(oberon_context_t * ctx, oberon_object_t * proc)
206 assert(proc -> class == OBERON_CLASS_PROC);
208 gen_context_t * gen_context = ctx -> gen_context;
209 gcc_jit_context * gcc_context = gen_context -> gcc_context;
211 gen_proc_t * gen_proc = malloc(sizeof *gen_proc);
212 memset(gen_proc, 0, sizeof *gen_proc);
213 proc -> gen_proc = gen_proc;
215 const char * name = proc -> name;
216 gen_type_t * gen_result_type = proc -> type -> base -> gen_type;
217 gcc_jit_type * result_type = gen_result_type -> gcc_type;
219 /* Строим список параметров */
220 int num_param = proc -> type -> num_decl;
221 oberon_object_t * o = proc -> type -> decl;
222 gcc_jit_param * params[num_param];
223 for(int i = 0; i < num_param; i++)
225 gen_var_t * param_var = o -> gen_var;
226 params[i] = param_var -> gcc_param;
227 o = o -> next;
230 gcc_jit_function * gcc_func;
231 gcc_func = gcc_jit_context_new_function(
232 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, result_type, name, num_param, params, 0
233 );
235 gen_proc -> gcc_func = gcc_func;
238 // =======================================================================
239 // GENERATOR
240 // =======================================================================
242 static gcc_jit_rvalue * rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item);
243 static gcc_jit_rvalue * rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr);
245 void
246 oberon_generate_begin_module(oberon_context_t * ctx)
248 printcontext(ctx, "oberon_generate_begin_module");
250 gen_context_t * gen_context = ctx -> gen_context;
251 gcc_jit_context * gcc_context = gen_context -> gcc_context;
253 gcc_jit_type * void_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_VOID);
254 gcc_jit_function * func = gcc_jit_context_new_function(
255 gcc_context, NULL, GCC_JIT_FUNCTION_EXPORTED, void_type, "BEGIN", 0, NULL, 0
256 );
257 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
259 gen_context -> gcc_block = gcc_block;
262 void
263 oberon_generate_end_module(oberon_context_t * ctx)
265 printcontext(ctx, "oberon_generate_end_module");
267 gen_context_t * gen_context = ctx -> gen_context;
268 gcc_jit_block * gcc_block = gen_context -> gcc_block;
270 gcc_jit_block_end_with_void_return(gcc_block, NULL);
272 gen_context -> gcc_block = NULL;
275 void
276 oberon_generate_begin_proc(oberon_context_t * ctx, oberon_object_t * proc)
278 gen_context_t * gen_context = ctx -> gen_context;
279 gen_proc_t * gen_proc = proc -> gen_proc;
281 gcc_jit_function * func = gen_proc -> gcc_func;
282 gcc_jit_block * gcc_block = gcc_jit_function_new_block(func, NULL);
284 // TODO make stack for block
285 gen_context -> gcc_block = gcc_block;
288 void
289 oberon_generate_call_proc(oberon_context_t * ctx, oberon_expr_t * desig)
291 gen_context_t * gen_context = ctx -> gen_context;
292 gcc_jit_block * block = gen_context -> gcc_block;
294 gcc_jit_rvalue * return_value;
295 return_value = rvalue_from_expr(ctx, desig);
296 gcc_jit_block_add_eval(block, NULL, return_value);
299 void
300 oberon_generate_end_proc(oberon_context_t * ctx)
302 gen_context_t * gen_context = ctx -> gen_context;
303 gen_context -> gcc_block = NULL;
306 void
307 oberon_generate_return(oberon_context_t * ctx, oberon_expr_t * expr)
309 gen_context_t * gen_context = ctx -> gen_context;
310 gcc_jit_block * gcc_block = gen_context -> gcc_block;
312 if(expr == NULL)
314 gcc_jit_block_end_with_void_return(gcc_block, NULL);
316 else
318 gcc_jit_rvalue * r = rvalue_from_expr(ctx, expr);
319 gcc_jit_block_end_with_return(gcc_block, NULL, r);
323 static gcc_jit_lvalue *
324 lvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
326 gen_context_t * gen_context = ctx -> gen_context;
327 gcc_jit_context * gcc_context = gen_context -> gcc_context;
329 gcc_jit_lvalue * left;
331 if(item -> mode == MODE_VAR)
333 gen_var_t * gen_var = item -> var -> gen_var;
334 left = gen_var -> gcc_lvalue;
336 else if(item -> mode == MODE_INDEX)
338 assert(item -> num_args == 1);
339 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
340 gcc_jit_rvalue * index = rvalue_from_expr(ctx, item -> args);
341 left = gcc_jit_context_new_array_access(gcc_context, NULL, parent, index);
343 else if(item -> mode == MODE_FIELD)
345 gen_var_t * gen_var = item -> var -> gen_var;
346 gcc_jit_field * gcc_field = gen_var -> gcc_field;
348 gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent);
349 left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field);
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
445 oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
448 return right;
451 struct {
452 int type; // 0 - unary, 1 - binary, 2 - comp
453 union {
454 enum gcc_jit_unary_op unary_op;
455 enum gcc_jit_binary_op binary_op;
456 enum gcc_jit_comparison comp_op;
457 };
458 } op_table[] = {
459 { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE },
460 { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
462 { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
463 { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
464 { 1, .binary_op = GCC_JIT_BINARY_OP_MULT },
465 { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
466 { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
467 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND },
468 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR },
470 { 2, .comp_op = GCC_JIT_COMPARISON_EQ },
471 { 2, .comp_op = GCC_JIT_COMPARISON_NE },
472 { 2, .comp_op = GCC_JIT_COMPARISON_LT },
473 { 2, .comp_op = GCC_JIT_COMPARISON_LE },
474 { 2, .comp_op = GCC_JIT_COMPARISON_GT },
475 { 2, .comp_op = GCC_JIT_COMPARISON_GE }
476 };
478 static gcc_jit_rvalue *
479 rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator)
481 gcc_jit_rvalue * right;
483 gen_context_t * gen_context = ctx -> gen_context;
484 gen_type_t * gen_type = operator -> result -> gen_type;
485 gcc_jit_context * gcc_context = gen_context -> gcc_context;
486 gcc_jit_type * result_type = gen_type -> gcc_type;
488 int expr_type = op_table[operator -> op].type;
489 if(expr_type == 0)
491 enum gcc_jit_unary_op op = op_table[operator -> op].unary_op;
492 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
493 right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l);
495 else if(expr_type == 1)
497 enum gcc_jit_unary_op op = op_table[operator -> op].binary_op;
498 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
499 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
500 right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r);
502 else if(expr_type == 2)
504 enum gcc_jit_comparison op = op_table[operator -> op].comp_op;
505 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
506 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
507 right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r);
509 else
511 oberon_error(ctx, "rvalue_from_operator: wat");
514 return right;
517 static gcc_jit_rvalue *
518 rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr)
520 gcc_jit_rvalue * right;
522 if(expr -> is_item)
524 oberon_item_t * item = (oberon_item_t *) expr;
525 right = rvalue_from_item(ctx, item);
527 else
529 oberon_oper_t * operator = (oberon_oper_t *) expr;
530 right = rvalue_from_operator(ctx, operator);
533 return right;
536 void
537 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
539 gcc_jit_lvalue * left;
540 left = lvalue_from_expr(ctx, dst);
542 gcc_jit_rvalue * right;
543 right = rvalue_from_expr(ctx, src);
545 gen_context_t * gen_context = ctx -> gen_context;
546 gcc_jit_block * gcc_block = gen_context -> gcc_block;
547 gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
550 void
551 oberon_generate_code(oberon_context_t * ctx)
553 gen_context_t * gen_context = ctx -> gen_context;
554 gcc_jit_context * gcc_context = gen_context -> gcc_context;
556 gcc_jit_result * gcc_result;
557 gcc_result = gcc_jit_context_compile(gcc_context);
559 gen_context -> gcc_result = gcc_result;
560 ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
563 void
564 oberon_generator_dump(oberon_context_t * ctx, char * path)
566 gen_context_t * gen_context = ctx -> gen_context;
567 gcc_jit_context * gcc_context = gen_context -> gcc_context;
568 gcc_jit_context_dump_to_file(gcc_context, path, 0);