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 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 gen_var_t * gen_var = item -> var -> gen_var;
340 gcc_jit_field * gcc_field = gen_var -> gcc_field;
342 gcc_jit_lvalue * parent = lvalue_from_item(ctx, item -> parent);
343 left = gcc_jit_lvalue_access_field(parent, NULL, gcc_field);
345 else
347 oberon_error(ctx, "invalid lvalue expression");
350 return left;
353 static gcc_jit_lvalue *
354 lvalue_from_expr(oberon_context_t *ctx, oberon_expr_t * expr)
356 gcc_jit_lvalue * left;
357 oberon_item_t * item;
359 if(expr -> is_item)
361 item = (oberon_item_t *) expr;
362 left = lvalue_from_item(ctx, item);
364 else
366 oberon_error(ctx, "invalid lvalue expression");
369 return left;
372 static gcc_jit_rvalue *
373 rvalue_from_item(oberon_context_t * ctx, oberon_item_t * item)
375 gen_context_t * gen_context = ctx -> gen_context;
376 gcc_jit_context * gcc_context = gen_context -> gcc_context;
378 gcc_jit_rvalue * right;
379 if(item -> mode == MODE_VAR)
381 assert(item -> var -> class == OBERON_CLASS_VAR
382 || item -> var -> class == OBERON_CLASS_PARAM);
383 gen_var_t * gen_var = item -> var -> gen_var;
384 right = gcc_jit_lvalue_as_rvalue(gen_var -> gcc_lvalue);
386 else if(item -> mode == MODE_INTEGER)
388 gcc_jit_type * int_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_INT);
389 right = gcc_jit_context_new_rvalue_from_int(gcc_context, int_type, item -> integer);
391 else if(item -> mode == MODE_BOOLEAN)
393 gcc_jit_type * bool_type = gcc_jit_context_get_type(gcc_context, GCC_JIT_TYPE_BOOL);
394 if(item -> boolean)
396 right = gcc_jit_context_one(gcc_context, bool_type);
398 else
400 right = gcc_jit_context_zero(gcc_context, bool_type);
403 else if(item -> mode == MODE_CALL)
405 assert(item -> var -> class == OBERON_CLASS_PROC);
407 gen_proc_t * gen_proc = item -> var -> gen_proc;
409 int num_args = item -> num_args;
410 gcc_jit_rvalue *args[num_args];
412 oberon_expr_t * expr = item -> args;
413 for(int i = 0; i < num_args; i++)
415 args[i] = rvalue_from_expr(ctx, expr);
416 expr = expr -> next;
419 gcc_jit_function * func = gen_proc -> gcc_func;
420 right = gcc_jit_context_new_call(
421 gcc_context, NULL, func, num_args, args
422 );
424 else if(item -> mode == MODE_INDEX)
426 gcc_jit_lvalue * left = lvalue_from_item(ctx, item);
427 right = gcc_jit_lvalue_as_rvalue(left);
429 else if(item -> mode == MODE_FIELD)
431 gen_var_t * gen_var = item -> var -> gen_var;
432 gcc_jit_field * gcc_field = gen_var -> gcc_field;
434 gcc_jit_rvalue * parent = rvalue_from_item(ctx, item -> parent);
435 right = gcc_jit_rvalue_access_field(parent, NULL, gcc_field);
437 else
439 oberon_error(ctx, "rvalue_from_item: invalid mode %i", item -> mode);
442 return right;
445 struct {
446 int type; // 0 - unary, 1 - binary, 2 - comp
447 union {
448 enum gcc_jit_unary_op unary_op;
449 enum gcc_jit_binary_op binary_op;
450 enum gcc_jit_comparison comp_op;
451 };
452 } op_table[] = {
453 { 0, .unary_op = GCC_JIT_UNARY_OP_LOGICAL_NEGATE },
454 { 0, .unary_op = GCC_JIT_UNARY_OP_MINUS },
456 { 1, .binary_op = GCC_JIT_BINARY_OP_PLUS },
457 { 1, .binary_op = GCC_JIT_BINARY_OP_MINUS },
458 { 1, .binary_op = GCC_JIT_BINARY_OP_MULT },
459 { 1, .binary_op = GCC_JIT_BINARY_OP_DIVIDE },
460 { 1, .binary_op = GCC_JIT_BINARY_OP_MODULO },
461 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_AND },
462 { 1, .binary_op = GCC_JIT_BINARY_OP_LOGICAL_OR },
464 { 2, .comp_op = GCC_JIT_COMPARISON_EQ },
465 { 2, .comp_op = GCC_JIT_COMPARISON_NE },
466 { 2, .comp_op = GCC_JIT_COMPARISON_LT },
467 { 2, .comp_op = GCC_JIT_COMPARISON_LE },
468 { 2, .comp_op = GCC_JIT_COMPARISON_GT },
469 { 2, .comp_op = GCC_JIT_COMPARISON_GE }
470 };
472 static gcc_jit_rvalue *
473 rvalue_from_operator(oberon_context_t * ctx, oberon_oper_t * operator)
475 gcc_jit_rvalue * right;
477 gen_context_t * gen_context = ctx -> gen_context;
478 gen_type_t * gen_type = operator -> result -> gen_type;
479 gcc_jit_context * gcc_context = gen_context -> gcc_context;
480 gcc_jit_type * result_type = gen_type -> gcc_type;
482 int expr_type = op_table[operator -> op].type;
483 if(expr_type == 0)
485 enum gcc_jit_unary_op op = op_table[operator -> op].unary_op;
486 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
487 right = gcc_jit_context_new_unary_op(gcc_context, NULL, op, result_type, l);
489 else if(expr_type == 1)
491 enum gcc_jit_unary_op op = op_table[operator -> op].binary_op;
492 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
493 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
494 right = gcc_jit_context_new_binary_op(gcc_context, NULL, op, result_type, l, r);
496 else if(expr_type == 2)
498 enum gcc_jit_comparison op = op_table[operator -> op].comp_op;
499 gcc_jit_rvalue * l = rvalue_from_expr(ctx, operator -> left);
500 gcc_jit_rvalue * r = rvalue_from_expr(ctx, operator -> right);
501 right = gcc_jit_context_new_comparison(gcc_context, NULL, op, l, r);
503 else
505 oberon_error(ctx, "rvalue_from_operator: wat");
508 return right;
511 static gcc_jit_rvalue *
512 rvalue_from_expr(oberon_context_t * ctx, oberon_expr_t * expr)
514 gcc_jit_rvalue * right;
516 if(expr -> is_item)
518 oberon_item_t * item = (oberon_item_t *) expr;
519 right = rvalue_from_item(ctx, item);
521 else
523 oberon_oper_t * operator = (oberon_oper_t *) expr;
524 right = rvalue_from_operator(ctx, operator);
527 return right;
530 void
531 oberon_generate_assign(oberon_context_t * ctx, oberon_expr_t * src, oberon_expr_t * dst)
533 gcc_jit_lvalue * left;
534 left = lvalue_from_expr(ctx, dst);
536 gcc_jit_rvalue * right;
537 right = rvalue_from_expr(ctx, src);
539 gen_context_t * gen_context = ctx -> gen_context;
540 gcc_jit_block * gcc_block = gen_context -> gcc_block;
541 gcc_jit_block_add_assignment(gcc_block, NULL, left, right);
544 void
545 oberon_generate_code(oberon_context_t * ctx)
547 gen_context_t * gen_context = ctx -> gen_context;
548 gcc_jit_context * gcc_context = gen_context -> gcc_context;
550 gcc_jit_result * gcc_result;
551 gcc_result = gcc_jit_context_compile(gcc_context);
553 gen_context -> gcc_result = gcc_result;
554 ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
557 void
558 oberon_generator_dump(oberon_context_t * ctx, char * path)
560 gen_context_t * gen_context = ctx -> gen_context;
561 gcc_jit_context * gcc_context = gen_context -> gcc_context;
562 gcc_jit_context_dump_to_file(gcc_context, path, 0);