12 #include "generator.h"
14 // =======================================================================
16 // =======================================================================
19 __OBERON_ALLOC__ (size_t bytes
)
21 void * p
= GC_MALLOC(bytes
);
23 printf("allocated %lu bytes\n", bytes
);
27 // =======================================================================
29 // =======================================================================
32 oberon_generator_open_block(gen_context_t
* gen_context
, gcc_jit_block
* gcc_block
)
34 gen_block_t
* block
= malloc(sizeof *block
);
35 memset(block
, 0, sizeof *block
);
37 block
-> gcc_block
= gcc_block
;
38 block
-> up
= gen_context
-> block
;
40 gen_context
-> block
= block
;
44 oberon_generator_close_block(gen_context_t
* gen_context
)
46 gen_context
-> block
= gen_context
-> block
-> up
;
50 oberon_generator_init_context(oberon_context_t
* ctx
)
52 gen_context_t
* gen_context
= malloc(sizeof *gen_context
);
53 memset(gen_context
, 0, sizeof *gen_context
);
55 gcc_jit_context
* gcc_context
;
56 gcc_context
= gcc_jit_context_acquire();
58 ctx
-> gen_context
= gen_context
;
59 gen_context
-> gcc_context
= gcc_context
;
61 gcc_jit_type
* void_ptr_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID_PTR
);
62 gcc_jit_type
* size_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_SIZE_T
);
63 gcc_jit_type
* alloc_ptr_type
= gcc_jit_context_new_function_ptr_type(
64 gcc_context
, NULL
, void_ptr_type
, 1, &size_type
, 0
66 gen_context
-> gcc_alloc
= gcc_jit_context_new_global(
67 gcc_context
, NULL
, GCC_JIT_GLOBAL_EXPORTED
, alloc_ptr_type
, "__OBERON_ALLOC__"
72 oberon_generator_destroy_context(oberon_context_t
* ctx
)
74 gen_context_t
* gen_context
= ctx
-> gen_context
;
75 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
77 gcc_jit_context_release(gcc_context
);
81 oberon_generator_init_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
83 gen_type_t
* gen_type
= malloc(sizeof *gen_type
);
84 memset(gen_type
, 0, sizeof *gen_type
);
85 type
-> gen_type
= gen_type
;
87 gen_context_t
* gen_context
= ctx
-> gen_context
;
88 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
90 gcc_jit_type
* gcc_type
= NULL
;
91 gcc_jit_struct
* gcc_struct
= NULL
;
92 if(type
-> class == OBERON_TYPE_VOID
)
94 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
96 else if(type
-> class == OBERON_TYPE_INTEGER
)
98 gcc_type
= gcc_jit_context_get_int_type(gcc_context
, type
-> size
, 1);
100 else if(type
-> class == OBERON_TYPE_BOOLEAN
)
102 if(type
-> size
== sizeof(bool))
104 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
108 oberon_error(ctx
, "generator: unsupported boolean size");
111 else if(type
-> class == OBERON_TYPE_REAL
)
113 if(type
-> size
== sizeof(float))
115 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_FLOAT
);
117 else if(type
-> size
== sizeof(double))
119 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_DOUBLE
);
121 else if(type
-> size
== sizeof(long double))
123 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_LONG_DOUBLE
);
127 oberon_error(ctx
, "generator: unsupported real size");
130 else if(type
-> class == OBERON_TYPE_ARRAY
)
132 gen_type_t
* gen_base
= type
-> base
-> gen_type
;
133 gcc_jit_type
* gcc_base
= gen_base
-> gcc_type
;
134 gcc_type
= gcc_jit_context_new_array_type(gcc_context
, NULL
, gcc_base
, type
-> size
);
136 else if(type
-> class == OBERON_TYPE_RECORD
)
139 snprintf(name
, 32, "RECORD%u", gen_context
-> record_count
);
140 gen_context
-> record_count
+= 1;
142 gcc_struct
= gcc_jit_context_new_opaque_struct(gcc_context
, NULL
, name
);
143 gcc_type
= gcc_jit_struct_as_type(gcc_struct
);
145 else if(type
-> class == OBERON_TYPE_POINTER
)
147 gen_type_t
* gen_base
= type
-> base
-> gen_type
;
148 gcc_jit_type
* gcc_base
= gen_base
-> gcc_type
;
149 gcc_type
= gcc_jit_type_get_pointer(gcc_base
);
151 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
153 int num_params
= type
-> num_decl
;
154 gcc_jit_type
* params
[num_params
];
155 oberon_object_t
* o
= type
-> decl
;
156 for(int i
= 0; i
< num_params
; i
++)
158 gen_type_t
* gen_type
= o
-> type
-> gen_type
;
159 params
[i
] = gen_type
-> gcc_type
;
163 gen_type_t
* base
= type
-> base
-> gen_type
;
164 gcc_jit_type
* result_type
= base
-> gcc_type
;
166 gcc_type
= gcc_jit_context_new_function_ptr_type(
167 gcc_context
, NULL
, result_type
, num_params
, params
, 0
172 oberon_error(ctx
, "oberon_generator_init_type: invalid type class %i", type
-> class);
176 gen_type
-> gcc_type
= gcc_type
;
177 gen_type
-> gcc_struct
= gcc_struct
;
181 oberon_generator_init_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
183 assert(type
-> class == OBERON_TYPE_RECORD
);
185 gen_type_t
* gen_type
= type
-> gen_type
;
186 gcc_jit_struct
* gcc_struct
= gen_type
-> gcc_struct
;
188 // TODO type exstension
190 int num_fields
= type
-> num_decl
;
191 gcc_jit_field
* fields
[num_fields
];
192 oberon_object_t
* o
= type
-> decl
;
193 for(int i
= 0; i
< num_fields
; i
++)
195 assert(o
-> class == OBERON_CLASS_FIELD
);
196 gen_var_t
* var
= o
-> gen_var
;
197 fields
[i
] = var
-> gcc_field
;
201 gcc_jit_struct_set_fields (gcc_struct
, NULL
, num_fields
, fields
);
203 //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
207 oberon_generator_get_full_name(char * name
, int max_len
, oberon_object_t
* x
)
215 int add_module_prefix
;
218 case OBERON_CLASS_FIELD
:
219 case OBERON_CLASS_PARAM
:
220 case OBERON_CLASS_VAR_PARAM
:
221 /* В локальных областях префиксы излишни */
222 add_module_prefix
= 0;
225 add_module_prefix
= 1;
230 oberon_generator_get_full_name(parent
, 256, x
-> parent
);
232 if(strlen(parent
) > 0)
234 snprintf(name
, max_len
, "%s_%s", parent
, x
-> name
);
236 else if(add_module_prefix
)
238 snprintf(name
, max_len
, "%s_%s", x
-> module
-> name
, x
-> name
);
242 snprintf(name
, max_len
, "%s", x
-> name
);
247 oberon_generator_init_var(oberon_context_t
* ctx
, oberon_object_t
* var
)
249 gen_context_t
* gen_context
= ctx
-> gen_context
;
250 gen_type_t
* gen_type
= var
-> type
-> gen_type
;
252 gen_var_t
* gen_var
= malloc(sizeof *gen_var
);
253 memset(gen_var
, 0, sizeof *gen_var
);
254 var
-> gen_var
= gen_var
;
256 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
257 gcc_jit_type
* gcc_type
= gen_type
-> gcc_type
;
260 oberon_generator_get_full_name(name
, 256, var
);
262 gcc_jit_lvalue
* gcc_lvalue
= NULL
;
263 gcc_jit_param
* gcc_param
= NULL
;
264 gcc_jit_field
* gcc_field
= NULL
;
265 if(var
-> class == OBERON_CLASS_VAR
)
269 gen_proc_t
* gen_func
= var
-> parent
-> gen_proc
;
270 gcc_jit_function
* func
= gen_func
-> gcc_func
;
272 gcc_lvalue
= gcc_jit_function_new_local(func
, NULL
, gcc_type
, name
);
276 gcc_lvalue
= gcc_jit_context_new_global(
277 gcc_context
, NULL
, GCC_JIT_GLOBAL_EXPORTED
, gcc_type
, name
281 else if(var
-> class == OBERON_CLASS_PARAM
)
283 gcc_param
= gcc_jit_context_new_param(gcc_context
, NULL
, gcc_type
, name
);
284 gcc_lvalue
= gcc_jit_param_as_lvalue(gcc_param
);
286 else if(var
-> class == OBERON_CLASS_VAR_PARAM
)
288 gcc_type
= gcc_jit_type_get_pointer(gcc_type
);
289 gcc_param
= gcc_jit_context_new_param(gcc_context
, NULL
, gcc_type
, name
);
290 gcc_lvalue
= gcc_jit_param_as_lvalue(gcc_param
);
292 else if(var
-> class == OBERON_CLASS_FIELD
)
294 gcc_field
= gcc_jit_context_new_field(gcc_context
, NULL
, gcc_type
, name
);
298 oberon_error(ctx
, "oberon_generator_init_var: invalid class %i", var
-> class);
301 gen_var
-> gcc_lvalue
= gcc_lvalue
;
302 gen_var
-> gcc_param
= gcc_param
;
303 gen_var
-> gcc_field
= gcc_field
;
307 oberon_generator_init_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
311 oberon_error(ctx
, "generator: local procedures not supported");
314 gen_context_t
* gen_context
= ctx
-> gen_context
;
315 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
317 gen_proc_t
* gen_proc
= malloc(sizeof *gen_proc
);
318 memset(gen_proc
, 0, sizeof *gen_proc
);
319 proc
-> gen_proc
= gen_proc
;
322 oberon_generator_get_full_name(name
, 256, proc
);
324 gen_type_t
* gen_result_type
= proc
-> type
-> base
-> gen_type
;
325 gcc_jit_type
* result_type
= gen_result_type
-> gcc_type
;
327 /* Строим список параметров */
328 int num_param
= proc
-> type
-> num_decl
;
329 oberon_object_t
* o
= proc
-> type
-> decl
;
330 gcc_jit_param
* params
[num_param
];
331 for(int i
= 0; i
< num_param
; i
++)
333 gen_var_t
* param_var
= o
-> gen_var
;
334 params
[i
] = param_var
-> gcc_param
;
338 gcc_jit_function
* gcc_func
;
339 gcc_func
= gcc_jit_context_new_function(
340 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, result_type
, name
, num_param
, params
, 0
343 gen_proc
-> gcc_func
= gcc_func
;
346 // =======================================================================
348 // =======================================================================
350 static gcc_jit_rvalue
* rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
);
351 static gcc_jit_rvalue
* rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
354 oberon_generator_get_type_size(oberon_context_t
* ctx
, oberon_type_t
* type
)
357 switch(type
-> class)
359 case OBERON_TYPE_INTEGER
:
361 printf("int size: %i\n", size
);
363 case OBERON_TYPE_BOOLEAN
:
365 printf("bool size: %i\n", size
);
367 case OBERON_TYPE_PROCEDURE
:
368 case OBERON_TYPE_POINTER
:
369 size
= sizeof(void*);
370 printf("ptr size: %i\n", size
);
372 case OBERON_TYPE_ARRAY
:
375 size
*= oberon_generator_get_type_size(ctx
, type
);
376 printf("array size: %i\n", size
);
378 case OBERON_TYPE_RECORD
:
380 int num
= type
-> num_decl
;
381 oberon_object_t
* arg
= type
-> decl
;
382 for(int i
= 0; i
< num
; i
++)
386 size
+= oberon_generator_get_type_size(ctx
, x
);
390 printf("struct size: %i\n", size
);
393 oberon_error(ctx
, "oberon_generator_get_type_size: wat");
401 oberon_generate_begin_module(oberon_context_t
* ctx
)
403 gen_context_t
* gen_context
= ctx
-> gen_context
;
404 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
407 snprintf(name
, 256, "%s_BEGIN", ctx
-> mod
-> name
);
409 gcc_jit_type
* void_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
410 gcc_jit_function
* func
= gcc_jit_context_new_function(
411 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, void_type
, name
, 0, NULL
, 0
413 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
415 oberon_generator_open_block(gen_context
, gcc_block
);
419 oberon_generate_end_module(oberon_context_t
* ctx
)
421 gen_context_t
* gen_context
= ctx
-> gen_context
;
422 gcc_jit_block
* gcc_block
= gen_context
-> block
-> gcc_block
;
424 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
426 oberon_generator_close_block(gen_context
);
430 oberon_generate_begin_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
432 gen_context_t
* gen_context
= ctx
-> gen_context
;
433 gen_proc_t
* gen_proc
= proc
-> gen_proc
;
435 gcc_jit_function
* func
= gen_proc
-> gcc_func
;
436 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
438 oberon_generator_open_block(gen_context
, gcc_block
);
442 oberon_generate_call_proc(oberon_context_t
* ctx
, oberon_expr_t
* desig
)
444 gen_context_t
* gen_context
= ctx
-> gen_context
;
445 gen_block_t
* gen_block
= gen_context
-> block
;
446 gcc_jit_block
* block
= gen_block
-> gcc_block
;
448 gcc_jit_rvalue
* return_value
;
449 return_value
= rvalue_from_expr(ctx
, desig
);
450 gcc_jit_block_add_eval(block
, NULL
, return_value
);
454 oberon_generate_end_proc(oberon_context_t
* ctx
)
456 gen_context_t
* gen_context
= ctx
-> gen_context
;
457 oberon_generator_close_block(gen_context
);
461 oberon_generate_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
463 gen_context_t
* gen_context
= ctx
-> gen_context
;
464 gen_block_t
* gen_block
= gen_context
-> block
;
465 gcc_jit_block
* gcc_block
= gen_block
-> gcc_block
;
469 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
473 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, expr
);
474 gcc_jit_block_end_with_return(gcc_block
, NULL
, r
);
478 static gcc_jit_lvalue
*
479 lvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
481 gen_context_t
* gen_context
= ctx
-> gen_context
;
482 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
484 gcc_jit_lvalue
* left
;
486 if(item
-> mode
== MODE_VAR
)
488 if(item
-> var
-> class == OBERON_CLASS_PROC
)
490 oberon_error(ctx
, "casting static procedure to pointer not supported by generator");
493 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
494 left
= gen_var
-> gcc_lvalue
;
495 if(item
-> var
-> class == OBERON_CLASS_VAR_PARAM
)
497 gcc_jit_rvalue
* r
= gcc_jit_lvalue_as_rvalue(left
);
498 left
= gcc_jit_rvalue_dereference(r
, NULL
);
501 else if(item
-> mode
== MODE_INDEX
)
503 assert(item
-> num_args
== 1);
504 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
505 gcc_jit_rvalue
* index
= rvalue_from_expr(ctx
, item
-> args
);
506 left
= gcc_jit_context_new_array_access(gcc_context
, NULL
, parent
, index
);
508 else if(item
-> mode
== MODE_FIELD
)
510 printf("lvalue_from_item: %s\n", item
-> var
-> name
);
511 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
512 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
514 gcc_jit_lvalue
* parent
= lvalue_from_item(ctx
, item
-> parent
);
515 left
= gcc_jit_lvalue_access_field(parent
, NULL
, gcc_field
);
517 else if(item
-> mode
== MODE_DEREF
)
519 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
520 left
= gcc_jit_rvalue_dereference(parent
, NULL
);
524 oberon_error(ctx
, "lvalue_from_item: invalid mode %i", item
-> mode
);
530 static gcc_jit_lvalue
*
531 lvalue_from_expr(oberon_context_t
*ctx
, oberon_expr_t
* expr
)
533 gcc_jit_lvalue
* left
;
534 oberon_item_t
* item
;
538 item
= (oberon_item_t
*) expr
;
539 left
= lvalue_from_item(ctx
, item
);
543 oberon_error(ctx
, "invalid lvalue expression");
549 static gcc_jit_rvalue
*
550 rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
552 gen_context_t
* gen_context
= ctx
-> gen_context
;
553 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
555 gcc_jit_rvalue
* right
;
556 if(item
-> mode
== MODE_VAR
)
558 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
559 right
= gcc_jit_lvalue_as_rvalue(left
);
561 else if(item
-> mode
== MODE_INTEGER
)
563 gcc_jit_type
* int_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_INT
);
564 right
= gcc_jit_context_new_rvalue_from_int(gcc_context
, int_type
, item
-> integer
);
566 else if(item
-> mode
== MODE_BOOLEAN
)
568 gcc_jit_type
* bool_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
571 right
= gcc_jit_context_one(gcc_context
, bool_type
);
575 right
= gcc_jit_context_zero(gcc_context
, bool_type
);
578 else if(item
-> mode
== MODE_CALL
)
580 oberon_type_t
* signature
= item
-> var
-> type
;
581 gen_proc_t
* gen_proc
= item
-> var
-> gen_proc
;
583 int num_args
= item
-> num_args
;
584 gcc_jit_rvalue
*args
[num_args
];
586 oberon_expr_t
* expr
= item
-> args
;
587 oberon_object_t
* arg_param
= signature
-> decl
;
588 for(int i
= 0; i
< num_args
; i
++)
590 if(arg_param
-> class == OBERON_CLASS_VAR_PARAM
)
592 gcc_jit_lvalue
* left
= lvalue_from_expr(ctx
, expr
);
593 args
[i
] = gcc_jit_lvalue_get_address(left
, NULL
);
597 args
[i
] = rvalue_from_expr(ctx
, expr
);
600 arg_param
= arg_param
-> next
;
603 gcc_jit_rvalue
* fnptr
;
604 gcc_jit_function
* func
;
605 switch(item
-> var
-> class)
607 case OBERON_CLASS_PROC
:
608 func
= gen_proc
-> gcc_func
;
609 right
= gcc_jit_context_new_call(
610 gcc_context
, NULL
, func
, num_args
, args
613 case OBERON_CLASS_VAR
:
614 case OBERON_CLASS_VAR_PARAM
:
615 case OBERON_CLASS_PARAM
:
616 fnptr
= gcc_jit_lvalue_as_rvalue(item
-> var
-> gen_var
-> gcc_lvalue
);
617 right
= gcc_jit_context_new_call_through_ptr(
618 gcc_context
, NULL
, fnptr
, num_args
, args
626 else if(item
-> mode
== MODE_INDEX
)
628 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
629 right
= gcc_jit_lvalue_as_rvalue(left
);
631 else if(item
-> mode
== MODE_FIELD
)
633 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
634 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
636 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
637 right
= gcc_jit_rvalue_access_field(parent
, NULL
, gcc_field
);
639 else if(item
-> mode
== MODE_DEREF
)
641 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
642 right
= gcc_jit_lvalue_as_rvalue(left
);
644 else if(item
-> mode
== MODE_NIL
)
646 gcc_jit_type
* type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID_PTR
);
647 right
= gcc_jit_context_null(gcc_context
, type
);
649 else if(item
-> mode
== MODE_NEW
)
651 assert(item
-> result
-> class == OBERON_TYPE_POINTER
);
653 oberon_type_t
* type
= item
-> result
-> base
;
654 int type_size
= oberon_generator_get_type_size(ctx
, type
);
655 int array_size
= type_size
;
657 int num
= item
-> num_args
;
658 oberon_expr_t
* arg
= item
-> args
;
659 for(int i
= 0; i
< num
; i
++)
661 array_size
*= arg
-> item
.integer
;
665 gcc_jit_type
* size_type
;
666 size_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_SIZE_T
);
668 gcc_jit_rvalue
* fnarg
;
669 fnarg
= gcc_jit_context_new_rvalue_from_int(gcc_context
, size_type
, array_size
);
671 gcc_jit_type
* result_type
= item
-> result
-> gen_type
-> gcc_type
;
672 gcc_jit_rvalue
* gcc_alloc
= gcc_jit_lvalue_as_rvalue(gen_context
-> gcc_alloc
);
673 right
= gcc_jit_context_new_call_through_ptr(gcc_context
, NULL
, gcc_alloc
, 1, &fnarg
);
674 right
= gcc_jit_context_new_cast(gcc_context
, NULL
, right
, result_type
);
676 else if(item
-> mode
== MODE_REAL
)
678 gcc_jit_type
* int_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_FLOAT
);
679 right
= gcc_jit_context_new_rvalue_from_int(gcc_context
, int_type
, item
-> real
);
683 oberon_error(ctx
, "rvalue_from_item: invalid mode %i", item
-> mode
);
690 int type
; // 0 - unary, 1 - binary, 2 - comp
692 enum gcc_jit_unary_op unary_op
;
693 enum gcc_jit_binary_op binary_op
;
694 enum gcc_jit_comparison comp_op
;
697 { 0, .unary_op
= GCC_JIT_UNARY_OP_MINUS
},
698 { 0, .unary_op
= GCC_JIT_UNARY_OP_BITWISE_NEGATE
},
699 { 0, .unary_op
= GCC_JIT_UNARY_OP_LOGICAL_NEGATE
},
700 { 0, .unary_op
= GCC_JIT_UNARY_OP_ABS
},
702 { 1, .binary_op
= GCC_JIT_BINARY_OP_PLUS
},
703 { 1, .binary_op
= GCC_JIT_BINARY_OP_MINUS
},
704 { 1, .binary_op
= GCC_JIT_BINARY_OP_MULT
},
705 { 1, .binary_op
= GCC_JIT_BINARY_OP_DIVIDE
},
706 { 1, .binary_op
= GCC_JIT_BINARY_OP_MODULO
},
707 { 1, .binary_op
= GCC_JIT_BINARY_OP_BITWISE_AND
},
708 { 1, .binary_op
= GCC_JIT_BINARY_OP_BITWISE_XOR
},
709 { 1, .binary_op
= GCC_JIT_BINARY_OP_BITWISE_OR
},
710 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_AND
},
711 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_OR
},
713 { 2, .comp_op
= GCC_JIT_COMPARISON_EQ
},
714 { 2, .comp_op
= GCC_JIT_COMPARISON_NE
},
715 { 2, .comp_op
= GCC_JIT_COMPARISON_LT
},
716 { 2, .comp_op
= GCC_JIT_COMPARISON_LE
},
717 { 2, .comp_op
= GCC_JIT_COMPARISON_GT
},
718 { 2, .comp_op
= GCC_JIT_COMPARISON_GE
}
721 static gcc_jit_rvalue
*
722 rvalue_from_operator(oberon_context_t
* ctx
, oberon_oper_t
* operator)
724 gcc_jit_rvalue
* right
;
726 gen_context_t
* gen_context
= ctx
-> gen_context
;
727 gen_type_t
* gen_type
= operator -> result
-> gen_type
;
728 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
729 gcc_jit_type
* result_type
= gen_type
-> gcc_type
;
731 int expr_type
= op_table
[operator -> op
].type
;
734 enum gcc_jit_unary_op op
= op_table
[operator -> op
].unary_op
;
735 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
736 right
= gcc_jit_context_new_unary_op(gcc_context
, NULL
, op
, result_type
, l
);
738 else if(expr_type
== 1)
740 enum gcc_jit_unary_op op
= op_table
[operator -> op
].binary_op
;
741 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
742 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, operator -> right
);
743 right
= gcc_jit_context_new_binary_op(gcc_context
, NULL
, op
, result_type
, l
, r
);
745 else if(expr_type
== 2)
747 enum gcc_jit_comparison op
= op_table
[operator -> op
].comp_op
;
748 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
749 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, operator -> right
);
750 right
= gcc_jit_context_new_comparison(gcc_context
, NULL
, op
, l
, r
);
754 oberon_error(ctx
, "rvalue_from_operator: wat");
760 static gcc_jit_rvalue
*
761 rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
763 gcc_jit_rvalue
* right
;
767 oberon_item_t
* item
= (oberon_item_t
*) expr
;
768 right
= rvalue_from_item(ctx
, item
);
772 oberon_oper_t
* operator = (oberon_oper_t
*) expr
;
773 right
= rvalue_from_operator(ctx
, operator);
780 oberon_generate_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
782 gcc_jit_lvalue
* left
;
783 left
= lvalue_from_expr(ctx
, dst
);
785 gcc_jit_rvalue
* right
;
786 right
= rvalue_from_expr(ctx
, src
);
790 if(src
-> item
.mode
== MODE_NIL
)
792 gen_context_t
* gen_context
= ctx
-> gen_context
;
793 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
794 gen_type_t
* gen_type
= dst
-> result
-> gen_type
;
795 gcc_jit_type
* cast_to_type
= gen_type
-> gcc_type
;
796 right
= gcc_jit_context_new_cast(gcc_context
, NULL
, right
, cast_to_type
);
800 printf("oberon_generate_assign: class %i := class %i\n", dst
-> result
-> class, src
-> result
-> class);
802 gen_context_t
* gen_context
= ctx
-> gen_context
;
803 gen_block_t
* gen_block
= gen_context
-> block
;
804 gcc_jit_block
* gcc_block
= gen_block
-> gcc_block
;
805 gcc_jit_block_add_assignment(gcc_block
, NULL
, left
, right
);
809 oberon_generate_code(oberon_context_t
* ctx
)
811 gen_context_t
* gen_context
= ctx
-> gen_context
;
812 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
814 gcc_jit_result
* gcc_result
;
815 gcc_result
= gcc_jit_context_compile(gcc_context
);
817 gen_context
-> gcc_result
= gcc_result
;
819 typedef void * (*TOberonAlloc
)(size_t);
820 TOberonAlloc
* fn_alloc_ptr
= gcc_jit_result_get_global(gcc_result
, "__OBERON_ALLOC__");
821 *fn_alloc_ptr
= __OBERON_ALLOC__
;
823 // ctx -> mod -> begin = gcc_jit_result_get_code(gcc_result, "BEGIN");
827 oberon_generator_dump(oberon_context_t
* ctx
, char * path
)
829 gen_context_t
* gen_context
= ctx
-> gen_context
;
830 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
831 gcc_jit_context_dump_to_file(gcc_context
, path
, 0);
835 oberon_generator_get_procedure(oberon_context_t
* ctx
, const char * name
)
837 gen_context_t
* gen_context
= ctx
-> gen_context
;
838 gcc_jit_result
* gcc_result
= gen_context
-> gcc_result
;
840 return gcc_jit_result_get_code(gcc_result
, name
);
844 oberon_generator_get_var(oberon_context_t
* ctx
, const char * name
)
846 gen_context_t
* gen_context
= ctx
-> gen_context
;
847 gcc_jit_result
* gcc_result
= gen_context
-> gcc_result
;
849 return gcc_jit_result_get_global(gcc_result
, name
);