11 static void printcontext(oberon_context_t
* ctx
, char * s
)
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;
19 printf(" ctx = %p:\n", ctx);
20 printf(" gctx = %p:\n", gctx);
21 printf(" context = %p:\n", context);
22 printf(" block = %p:\n", block);
26 // =======================================================================
28 // =======================================================================
31 oberon_generator_open_block(gen_context_t
* gen_context
, gcc_jit_block
* gcc_block
)
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
;
43 oberon_generator_close_block(gen_context_t
* gen_context
)
45 gen_context
-> block
= gen_context
-> block
-> up
;
49 oberon_generator_init_context(oberon_context_t
* ctx
)
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");
64 oberon_generator_destroy_context(oberon_context_t
* ctx
)
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
);
75 oberon_generator_init_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
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
)
88 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
90 else if(type
-> class == OBERON_TYPE_INTEGER
)
92 gcc_type
= gcc_jit_context_get_int_type(gcc_context
, type
-> size
, 1);
94 else if(type
-> class == OBERON_TYPE_BOOLEAN
)
96 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
98 else if(type
-> class == OBERON_TYPE_ARRAY
)
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 gcc_struct
= gcc_jit_context_new_opaque_struct(gcc_context
, NULL
, "");
107 gcc_type
= gcc_jit_struct_as_type(gcc_struct
);
109 else if(type
-> class == OBERON_TYPE_POINTER
)
111 gen_type_t
* gen_base
= type
-> base
-> gen_type
;
112 gcc_jit_type
* gcc_base
= gen_base
-> gcc_type
;
113 gcc_type
= gcc_jit_type_get_pointer(gcc_base
);
115 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
117 int num_params
= type
-> num_decl
;
118 gcc_jit_type
* params
[num_params
];
119 oberon_object_t
* o
= type
-> decl
;
120 for(int i
= 0; i
< num_params
; i
++)
122 gen_type_t
* gen_type
= o
-> type
-> gen_type
;
123 params
[i
] = gen_type
-> gcc_type
;
127 gen_type_t
* base
= type
-> base
-> gen_type
;
128 gcc_jit_type
* result_type
= base
-> gcc_type
;
130 gcc_type
= gcc_jit_context_new_function_ptr_type(
131 gcc_context
, NULL
, result_type
, num_params
, params
, 0
136 oberon_error(ctx
, "oberon_generator_init_type: invalid type class %i", type
-> class);
140 gen_type
-> gcc_type
= gcc_type
;
141 gen_type
-> gcc_struct
= gcc_struct
;
145 oberon_generator_init_record(oberon_context_t
* ctx
, oberon_type_t
* type
)
147 assert(type
-> class == OBERON_TYPE_RECORD
);
149 gen_type_t
* gen_type
= type
-> gen_type
;
150 gcc_jit_struct
* gcc_struct
= gen_type
-> gcc_struct
;
152 // TODO type exstension
154 int num_fields
= type
-> num_decl
;
155 gcc_jit_field
* fields
[num_fields
];
156 oberon_object_t
* o
= type
-> decl
;
157 for(int i
= 0; i
< num_fields
; i
++)
159 assert(o
-> class == OBERON_CLASS_FIELD
);
160 gen_var_t
* var
= o
-> gen_var
;
161 fields
[i
] = var
-> gcc_field
;
165 gcc_jit_struct_set_fields (gcc_struct
, NULL
, num_fields
, fields
);
167 //gcc_struct = gcc_jit_context_new_struct_type(gcc_context, NULL, "", num_fields, fields);
171 oberon_generator_init_var(oberon_context_t
* ctx
, oberon_object_t
* var
)
173 gen_context_t
* gen_context
= ctx
-> gen_context
;
174 gen_type_t
* gen_type
= var
-> type
-> gen_type
;
176 gen_var_t
* gen_var
= malloc(sizeof *gen_var
);
177 memset(gen_var
, 0, sizeof *gen_var
);
178 var
-> gen_var
= gen_var
;
180 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
181 gcc_jit_type
* gcc_type
= gen_type
-> gcc_type
;
182 const char * name
= var
-> name
;
184 gcc_jit_lvalue
* gcc_lvalue
= NULL
;
185 gcc_jit_param
* gcc_param
= NULL
;
186 gcc_jit_field
* gcc_field
= NULL
;
187 if(var
-> class == OBERON_CLASS_VAR
)
191 gen_proc_t
* gen_func
= var
-> parent
-> gen_proc
;
192 gcc_jit_function
* func
= gen_func
-> gcc_func
;
194 gcc_lvalue
= gcc_jit_function_new_local(func
, NULL
, gcc_type
, name
);
198 gcc_lvalue
= gcc_jit_context_new_global(
199 gcc_context
, NULL
, GCC_JIT_GLOBAL_INTERNAL
, gcc_type
, name
203 else if(var
-> class == OBERON_CLASS_PARAM
)
205 gcc_param
= gcc_jit_context_new_param(gcc_context
, NULL
, gcc_type
, name
);
206 gcc_lvalue
= gcc_jit_param_as_lvalue(gcc_param
);
208 else if(var
-> class == OBERON_CLASS_VAR_PARAM
)
210 gcc_type
= gcc_jit_type_get_pointer(gcc_type
);
211 gcc_param
= gcc_jit_context_new_param(gcc_context
, NULL
, gcc_type
, name
);
212 gcc_lvalue
= gcc_jit_param_as_lvalue(gcc_param
);
214 else if(var
-> class == OBERON_CLASS_FIELD
)
216 gcc_field
= gcc_jit_context_new_field(gcc_context
, NULL
, gcc_type
, name
);
220 oberon_error(ctx
, "oberon_generator_init_var: invalid class %i", var
-> class);
223 gen_var
-> gcc_lvalue
= gcc_lvalue
;
224 gen_var
-> gcc_param
= gcc_param
;
225 gen_var
-> gcc_field
= gcc_field
;
229 oberon_generator_init_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
233 oberon_error(ctx
, "generator: local procedures not supported");
236 gen_context_t
* gen_context
= ctx
-> gen_context
;
237 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
239 gen_proc_t
* gen_proc
= malloc(sizeof *gen_proc
);
240 memset(gen_proc
, 0, sizeof *gen_proc
);
241 proc
-> gen_proc
= gen_proc
;
243 const char * name
= proc
-> name
;
244 gen_type_t
* gen_result_type
= proc
-> type
-> base
-> gen_type
;
245 gcc_jit_type
* result_type
= gen_result_type
-> gcc_type
;
247 /* Строим список параметров */
248 int num_param
= proc
-> type
-> num_decl
;
249 oberon_object_t
* o
= proc
-> type
-> decl
;
250 gcc_jit_param
* params
[num_param
];
251 for(int i
= 0; i
< num_param
; i
++)
253 gen_var_t
* param_var
= o
-> gen_var
;
254 params
[i
] = param_var
-> gcc_param
;
258 gcc_jit_function
* gcc_func
;
259 gcc_func
= gcc_jit_context_new_function(
260 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, result_type
, name
, num_param
, params
, 0
263 gen_proc
-> gcc_func
= gcc_func
;
266 // =======================================================================
268 // =======================================================================
270 static gcc_jit_rvalue
* rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
);
271 static gcc_jit_rvalue
* rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
274 oberon_generate_begin_module(oberon_context_t
* ctx
)
276 printcontext(ctx
, "oberon_generate_begin_module");
278 gen_context_t
* gen_context
= ctx
-> gen_context
;
279 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
281 gcc_jit_type
* void_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
282 gcc_jit_function
* func
= gcc_jit_context_new_function(
283 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, void_type
, "BEGIN", 0, NULL
, 0
285 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
287 oberon_generator_open_block(gen_context
, gcc_block
);
291 oberon_generate_end_module(oberon_context_t
* ctx
)
293 printcontext(ctx
, "oberon_generate_end_module");
295 gen_context_t
* gen_context
= ctx
-> gen_context
;
296 gcc_jit_block
* gcc_block
= gen_context
-> block
-> gcc_block
;
298 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
300 oberon_generator_close_block(gen_context
);
304 oberon_generate_begin_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
306 gen_context_t
* gen_context
= ctx
-> gen_context
;
307 gen_proc_t
* gen_proc
= proc
-> gen_proc
;
309 gcc_jit_function
* func
= gen_proc
-> gcc_func
;
310 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
312 oberon_generator_open_block(gen_context
, gcc_block
);
316 oberon_generate_call_proc(oberon_context_t
* ctx
, oberon_expr_t
* desig
)
318 gen_context_t
* gen_context
= ctx
-> gen_context
;
319 gen_block_t
* gen_block
= gen_context
-> block
;
320 gcc_jit_block
* block
= gen_block
-> gcc_block
;
322 gcc_jit_rvalue
* return_value
;
323 return_value
= rvalue_from_expr(ctx
, desig
);
324 gcc_jit_block_add_eval(block
, NULL
, return_value
);
328 oberon_generate_end_proc(oberon_context_t
* ctx
)
330 gen_context_t
* gen_context
= ctx
-> gen_context
;
331 oberon_generator_close_block(gen_context
);
335 oberon_generate_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
337 gen_context_t
* gen_context
= ctx
-> gen_context
;
338 gen_block_t
* gen_block
= gen_context
-> block
;
339 gcc_jit_block
* gcc_block
= gen_block
-> gcc_block
;
343 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
347 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, expr
);
348 gcc_jit_block_end_with_return(gcc_block
, NULL
, r
);
352 static gcc_jit_lvalue
*
353 lvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
355 gen_context_t
* gen_context
= ctx
-> gen_context
;
356 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
358 gcc_jit_lvalue
* left
;
360 if(item
-> mode
== MODE_VAR
)
362 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
363 left
= gen_var
-> gcc_lvalue
;
364 if(item
-> var
-> class == OBERON_CLASS_VAR_PARAM
)
366 gcc_jit_rvalue
* r
= gcc_jit_lvalue_as_rvalue(left
);
367 left
= gcc_jit_rvalue_dereference(r
, NULL
);
370 else if(item
-> mode
== MODE_INDEX
)
372 assert(item
-> num_args
== 1);
373 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
374 gcc_jit_rvalue
* index
= rvalue_from_expr(ctx
, item
-> args
);
375 left
= gcc_jit_context_new_array_access(gcc_context
, NULL
, parent
, index
);
377 else if(item
-> mode
== MODE_FIELD
)
379 printf("lvalue_from_item: %s\n", item
-> var
-> name
);
380 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
381 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
383 gcc_jit_lvalue
* parent
= lvalue_from_item(ctx
, item
-> parent
);
384 left
= gcc_jit_lvalue_access_field(parent
, NULL
, gcc_field
);
386 else if(item
-> mode
== MODE_DEREF
)
388 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
389 left
= gcc_jit_rvalue_dereference(parent
, NULL
);
393 oberon_error(ctx
, "invalid lvalue expression");
399 static gcc_jit_lvalue
*
400 lvalue_from_expr(oberon_context_t
*ctx
, oberon_expr_t
* expr
)
402 gcc_jit_lvalue
* left
;
403 oberon_item_t
* item
;
407 item
= (oberon_item_t
*) expr
;
408 left
= lvalue_from_item(ctx
, item
);
412 oberon_error(ctx
, "invalid lvalue expression");
418 static gcc_jit_rvalue
*
419 rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
421 gen_context_t
* gen_context
= ctx
-> gen_context
;
422 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
424 gcc_jit_rvalue
* right
;
425 if(item
-> mode
== MODE_VAR
)
427 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
428 right
= gcc_jit_lvalue_as_rvalue(left
);
430 else if(item
-> mode
== MODE_INTEGER
)
432 gcc_jit_type
* int_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_INT
);
433 right
= gcc_jit_context_new_rvalue_from_int(gcc_context
, int_type
, item
-> integer
);
435 else if(item
-> mode
== MODE_BOOLEAN
)
437 gcc_jit_type
* bool_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
440 right
= gcc_jit_context_one(gcc_context
, bool_type
);
444 right
= gcc_jit_context_zero(gcc_context
, bool_type
);
447 else if(item
-> mode
== MODE_CALL
)
449 assert(item
-> var
-> class == OBERON_CLASS_PROC
);
451 oberon_type_t
* signature
= item
-> var
-> type
;
452 gen_proc_t
* gen_proc
= item
-> var
-> gen_proc
;
454 int num_args
= item
-> num_args
;
455 gcc_jit_rvalue
*args
[num_args
];
457 oberon_expr_t
* expr
= item
-> args
;
458 oberon_object_t
* arg_param
= signature
-> decl
;
459 for(int i
= 0; i
< num_args
; i
++)
461 if(arg_param
-> class == OBERON_CLASS_VAR_PARAM
)
463 gcc_jit_lvalue
* left
= lvalue_from_expr(ctx
, expr
);
464 args
[i
] = gcc_jit_lvalue_get_address(left
, NULL
);
468 args
[i
] = rvalue_from_expr(ctx
, expr
);
471 arg_param
= arg_param
-> next
;
474 gcc_jit_function
* func
= gen_proc
-> gcc_func
;
475 right
= gcc_jit_context_new_call(
476 gcc_context
, NULL
, func
, num_args
, args
479 else if(item
-> mode
== MODE_INDEX
)
481 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
482 right
= gcc_jit_lvalue_as_rvalue(left
);
484 else if(item
-> mode
== MODE_FIELD
)
486 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
487 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
489 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
490 right
= gcc_jit_rvalue_access_field(parent
, NULL
, gcc_field
);
492 else if(item
-> mode
== MODE_DEREF
)
494 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
495 right
= gcc_jit_lvalue_as_rvalue(left
);
497 else if(item
-> mode
== MODE_NIL
)
499 gcc_jit_type
* type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID_PTR
);
500 right
= gcc_jit_context_null(gcc_context
, type
);
504 oberon_error(ctx
, "rvalue_from_item: invalid mode %i", item
-> mode
);
511 int type
; // 0 - unary, 1 - binary, 2 - comp
513 enum gcc_jit_unary_op unary_op
;
514 enum gcc_jit_binary_op binary_op
;
515 enum gcc_jit_comparison comp_op
;
518 { 0, .unary_op
= GCC_JIT_UNARY_OP_LOGICAL_NEGATE
},
519 { 0, .unary_op
= GCC_JIT_UNARY_OP_MINUS
},
521 { 1, .binary_op
= GCC_JIT_BINARY_OP_PLUS
},
522 { 1, .binary_op
= GCC_JIT_BINARY_OP_MINUS
},
523 { 1, .binary_op
= GCC_JIT_BINARY_OP_MULT
},
524 { 1, .binary_op
= GCC_JIT_BINARY_OP_DIVIDE
},
525 { 1, .binary_op
= GCC_JIT_BINARY_OP_MODULO
},
526 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_AND
},
527 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_OR
},
529 { 2, .comp_op
= GCC_JIT_COMPARISON_EQ
},
530 { 2, .comp_op
= GCC_JIT_COMPARISON_NE
},
531 { 2, .comp_op
= GCC_JIT_COMPARISON_LT
},
532 { 2, .comp_op
= GCC_JIT_COMPARISON_LE
},
533 { 2, .comp_op
= GCC_JIT_COMPARISON_GT
},
534 { 2, .comp_op
= GCC_JIT_COMPARISON_GE
}
537 static gcc_jit_rvalue
*
538 rvalue_from_operator(oberon_context_t
* ctx
, oberon_oper_t
* operator)
540 gcc_jit_rvalue
* right
;
542 gen_context_t
* gen_context
= ctx
-> gen_context
;
543 gen_type_t
* gen_type
= operator -> result
-> gen_type
;
544 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
545 gcc_jit_type
* result_type
= gen_type
-> gcc_type
;
547 int expr_type
= op_table
[operator -> op
].type
;
550 enum gcc_jit_unary_op op
= op_table
[operator -> op
].unary_op
;
551 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
552 right
= gcc_jit_context_new_unary_op(gcc_context
, NULL
, op
, result_type
, l
);
554 else if(expr_type
== 1)
556 enum gcc_jit_unary_op op
= op_table
[operator -> op
].binary_op
;
557 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
558 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, operator -> right
);
559 right
= gcc_jit_context_new_binary_op(gcc_context
, NULL
, op
, result_type
, l
, r
);
561 else if(expr_type
== 2)
563 enum gcc_jit_comparison op
= op_table
[operator -> op
].comp_op
;
564 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
565 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, operator -> right
);
566 right
= gcc_jit_context_new_comparison(gcc_context
, NULL
, op
, l
, r
);
570 oberon_error(ctx
, "rvalue_from_operator: wat");
576 static gcc_jit_rvalue
*
577 rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
579 gcc_jit_rvalue
* right
;
583 oberon_item_t
* item
= (oberon_item_t
*) expr
;
584 right
= rvalue_from_item(ctx
, item
);
588 oberon_oper_t
* operator = (oberon_oper_t
*) expr
;
589 right
= rvalue_from_operator(ctx
, operator);
596 oberon_generate_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
598 gcc_jit_lvalue
* left
;
599 left
= lvalue_from_expr(ctx
, dst
);
601 gcc_jit_rvalue
* right
;
602 right
= rvalue_from_expr(ctx
, src
);
606 if(src
-> item
.mode
== MODE_NIL
)
608 gen_context_t
* gen_context
= ctx
-> gen_context
;
609 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
610 gen_type_t
* gen_type
= dst
-> result
-> gen_type
;
611 gcc_jit_type
* cast_to_type
= gen_type
-> gcc_type
;
612 right
= gcc_jit_context_new_cast(gcc_context
, NULL
, right
, cast_to_type
);
616 gen_context_t
* gen_context
= ctx
-> gen_context
;
617 gen_block_t
* gen_block
= gen_context
-> block
;
618 gcc_jit_block
* gcc_block
= gen_block
-> gcc_block
;
619 gcc_jit_block_add_assignment(gcc_block
, NULL
, left
, right
);
623 oberon_generate_code(oberon_context_t
* ctx
)
625 gen_context_t
* gen_context
= ctx
-> gen_context
;
626 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
628 gcc_jit_result
* gcc_result
;
629 gcc_result
= gcc_jit_context_compile(gcc_context
);
631 gen_context
-> gcc_result
= gcc_result
;
632 ctx
-> mod
-> begin
= gcc_jit_result_get_code(gcc_result
, "BEGIN");
636 oberon_generator_dump(oberon_context_t
* ctx
, char * path
)
638 gen_context_t
* gen_context
= ctx
-> gen_context
;
639 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
640 gcc_jit_context_dump_to_file(gcc_context
, path
, 0);