11 #include <libgccjit.h>
13 static void printcontext(oberon_context_t
* ctx
, char * s
)
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;
21 printf(" ctx = %p:\n", ctx);
22 printf(" gctx = %p:\n", gctx);
23 printf(" context = %p:\n", context);
24 printf(" block = %p:\n", block);
28 // =======================================================================
30 // =======================================================================
33 oberon_generator_init_context(oberon_context_t
* ctx
)
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");
48 oberon_generator_destroy_context(oberon_context_t
* ctx
)
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
);
59 oberon_generator_init_type(oberon_context_t
* ctx
, oberon_type_t
* type
)
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
)
72 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
74 else if(type
-> class == OBERON_TYPE_INTEGER
)
76 gcc_type
= gcc_jit_context_get_int_type(gcc_context
, type
-> size
, 1);
78 else if(type
-> class == OBERON_TYPE_BOOLEAN
)
80 gcc_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
82 else if(type
-> class == OBERON_TYPE_ARRAY
)
86 oberon_error(ctx
, "multidimension and open arrays not supported");
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
);
94 else if(type
-> class == OBERON_TYPE_RECORD
)
96 // TODO type exstension
98 int num_fields
= type
-> num_decl
;
99 gcc_jit_field
* fields
[num_fields
];
100 oberon_object_t
* o
= type
-> decl
;
101 for(int i
= 0; i
< num_fields
; i
++)
103 assert(o
-> class == OBERON_CLASS_FIELD
);
104 gen_var_t
* var
= o
-> gen_var
;
105 fields
[i
] = var
-> gcc_field
;
109 gcc_struct
= gcc_jit_context_new_struct_type(gcc_context
, NULL
, "", num_fields
, fields
);
110 gcc_type
= gcc_jit_struct_as_type(gcc_struct
);
112 else if(type
-> class == OBERON_TYPE_POINTER
)
114 gen_type_t
* gen_base
= type
-> base
-> gen_type
;
115 gcc_jit_type
* gcc_base
= gen_base
-> gcc_type
;
116 gcc_type
= gcc_jit_type_get_pointer(gcc_base
);
118 else if(type
-> class == OBERON_TYPE_PROCEDURE
)
120 int num_params
= type
-> num_decl
;
121 gcc_jit_type
* params
[num_params
];
122 oberon_object_t
* o
= type
-> decl
;
123 for(int i
= 0; i
< num_params
; i
++)
125 gen_type_t
* gen_type
= o
-> type
-> gen_type
;
126 params
[i
] = gen_type
-> gcc_type
;
130 gen_type_t
* base
= type
-> base
-> gen_type
;
131 gcc_jit_type
* result_type
= base
-> gcc_type
;
133 gcc_type
= gcc_jit_context_new_function_ptr_type(
134 gcc_context
, NULL
, result_type
, num_params
, params
, 0
139 oberon_error(ctx
, "oberon_generator_init_type: invalid type class %i", type
-> class);
143 gen_type
-> gcc_type
= gcc_type
;
144 gen_type
-> gcc_struct
= gcc_struct
;
148 oberon_generator_init_var(oberon_context_t
* ctx
, oberon_object_t
* var
)
150 gen_context_t
* gen_context
= ctx
-> gen_context
;
151 gen_type_t
* gen_type
= var
-> type
-> gen_type
;
153 gen_var_t
* gen_var
= malloc(sizeof *gen_var
);
154 memset(gen_var
, 0, sizeof *gen_var
);
155 var
-> gen_var
= gen_var
;
157 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
158 gcc_jit_type
* gcc_type
= gen_type
-> gcc_type
;
159 const char * name
= var
-> name
;
162 gcc_jit_lvalue
* gcc_lvalue
= NULL
;
163 gcc_jit_param
* gcc_param
= NULL
;
164 gcc_jit_field
* gcc_field
= NULL
;
165 if(var
-> class == OBERON_CLASS_VAR
)
167 gcc_lvalue
= gcc_jit_context_new_global(
168 gcc_context
, NULL
, GCC_JIT_GLOBAL_INTERNAL
, gcc_type
, name
171 else if(var
-> class == OBERON_CLASS_PARAM
)
173 gcc_param
= gcc_jit_context_new_param(gcc_context
, NULL
, gcc_type
, name
);
174 gcc_lvalue
= gcc_jit_param_as_lvalue(gcc_param
);
176 else if(var
-> class == OBERON_CLASS_FIELD
)
178 gcc_field
= gcc_jit_context_new_field(gcc_context
, NULL
, gcc_type
, name
);
182 oberon_error(ctx
, "oberon_generator_init_var: invalid class %i", var
-> class);
185 gen_var
-> gcc_lvalue
= gcc_lvalue
;
186 gen_var
-> gcc_param
= gcc_param
;
187 gen_var
-> gcc_field
= gcc_field
;
191 oberon_generator_init_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
193 assert(proc
-> class == OBERON_CLASS_PROC
);
195 gen_context_t
* gen_context
= ctx
-> gen_context
;
196 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
198 gen_proc_t
* gen_proc
= malloc(sizeof *gen_proc
);
199 memset(gen_proc
, 0, sizeof *gen_proc
);
200 proc
-> gen_proc
= gen_proc
;
202 const char * name
= proc
-> name
;
203 gen_type_t
* gen_result_type
= proc
-> type
-> base
-> gen_type
;
204 gcc_jit_type
* result_type
= gen_result_type
-> gcc_type
;
206 /* Строим список параметров */
207 int num_param
= proc
-> type
-> num_decl
;
208 oberon_object_t
* o
= proc
-> type
-> decl
;
209 gcc_jit_param
* params
[num_param
];
210 for(int i
= 0; i
< num_param
; i
++)
212 gen_var_t
* param_var
= o
-> gen_var
;
213 params
[i
] = param_var
-> gcc_param
;
217 gcc_jit_function
* gcc_func
;
218 gcc_func
= gcc_jit_context_new_function(
219 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, result_type
, name
, num_param
, params
, 0
222 gen_proc
-> gcc_func
= gcc_func
;
225 // =======================================================================
227 // =======================================================================
229 static gcc_jit_rvalue
* rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
);
230 static gcc_jit_rvalue
* rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
);
233 oberon_generate_begin_module(oberon_context_t
* ctx
)
235 printcontext(ctx
, "oberon_generate_begin_module");
237 gen_context_t
* gen_context
= ctx
-> gen_context
;
238 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
240 gcc_jit_type
* void_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_VOID
);
241 gcc_jit_function
* func
= gcc_jit_context_new_function(
242 gcc_context
, NULL
, GCC_JIT_FUNCTION_EXPORTED
, void_type
, "BEGIN", 0, NULL
, 0
244 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
246 gen_context
-> gcc_block
= gcc_block
;
250 oberon_generate_end_module(oberon_context_t
* ctx
)
252 printcontext(ctx
, "oberon_generate_end_module");
254 gen_context_t
* gen_context
= ctx
-> gen_context
;
255 gcc_jit_block
* gcc_block
= gen_context
-> gcc_block
;
257 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
259 gen_context
-> gcc_block
= NULL
;
263 oberon_generate_begin_proc(oberon_context_t
* ctx
, oberon_object_t
* proc
)
265 gen_context_t
* gen_context
= ctx
-> gen_context
;
266 gen_proc_t
* gen_proc
= proc
-> gen_proc
;
268 gcc_jit_function
* func
= gen_proc
-> gcc_func
;
269 gcc_jit_block
* gcc_block
= gcc_jit_function_new_block(func
, NULL
);
271 // TODO make stack for block
272 gen_context
-> gcc_block
= gcc_block
;
276 oberon_generate_call_proc(oberon_context_t
* ctx
, oberon_expr_t
* desig
)
278 gen_context_t
* gen_context
= ctx
-> gen_context
;
279 gcc_jit_block
* block
= gen_context
-> gcc_block
;
281 gcc_jit_rvalue
* return_value
;
282 return_value
= rvalue_from_expr(ctx
, desig
);
283 gcc_jit_block_add_eval(block
, NULL
, return_value
);
287 oberon_generate_end_proc(oberon_context_t
* ctx
)
289 gen_context_t
* gen_context
= ctx
-> gen_context
;
290 gen_context
-> gcc_block
= NULL
;
294 oberon_generate_return(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
296 gen_context_t
* gen_context
= ctx
-> gen_context
;
297 gcc_jit_block
* gcc_block
= gen_context
-> gcc_block
;
301 gcc_jit_block_end_with_void_return(gcc_block
, NULL
);
305 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, expr
);
306 gcc_jit_block_end_with_return(gcc_block
, NULL
, r
);
310 static gcc_jit_lvalue
*
311 lvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
313 gen_context_t
* gen_context
= ctx
-> gen_context
;
314 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
316 gcc_jit_lvalue
* left
;
318 if(item
-> mode
== MODE_VAR
)
320 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
321 left
= gen_var
-> gcc_lvalue
;
323 else if(item
-> mode
== MODE_INDEX
)
325 assert(item
-> num_args
== 1);
326 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
327 gcc_jit_rvalue
* index
= rvalue_from_expr(ctx
, item
-> args
);
328 left
= gcc_jit_context_new_array_access(gcc_context
, NULL
, parent
, index
);
330 else if(item
-> mode
== MODE_FIELD
)
332 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
333 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
335 gcc_jit_lvalue
* parent
= lvalue_from_item(ctx
, item
-> parent
);
336 left
= gcc_jit_lvalue_access_field(parent
, NULL
, gcc_field
);
340 oberon_error(ctx
, "invalid lvalue expression");
346 static gcc_jit_lvalue
*
347 lvalue_from_expr(oberon_context_t
*ctx
, oberon_expr_t
* expr
)
349 gcc_jit_lvalue
* left
;
350 oberon_item_t
* item
;
354 item
= (oberon_item_t
*) expr
;
355 left
= lvalue_from_item(ctx
, item
);
359 oberon_error(ctx
, "invalid lvalue expression");
365 static gcc_jit_rvalue
*
366 rvalue_from_item(oberon_context_t
* ctx
, oberon_item_t
* item
)
368 gen_context_t
* gen_context
= ctx
-> gen_context
;
369 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
371 gcc_jit_rvalue
* right
;
372 if(item
-> mode
== MODE_VAR
)
374 assert(item
-> var
-> class == OBERON_CLASS_VAR
375 || item
-> var
-> class == OBERON_CLASS_PARAM
);
376 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
377 right
= gcc_jit_lvalue_as_rvalue(gen_var
-> gcc_lvalue
);
379 else if(item
-> mode
== MODE_INTEGER
)
381 gcc_jit_type
* int_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_INT
);
382 right
= gcc_jit_context_new_rvalue_from_int(gcc_context
, int_type
, item
-> integer
);
384 else if(item
-> mode
== MODE_BOOLEAN
)
386 gcc_jit_type
* bool_type
= gcc_jit_context_get_type(gcc_context
, GCC_JIT_TYPE_BOOL
);
389 right
= gcc_jit_context_one(gcc_context
, bool_type
);
393 right
= gcc_jit_context_zero(gcc_context
, bool_type
);
396 else if(item
-> mode
== MODE_CALL
)
398 assert(item
-> var
-> class == OBERON_CLASS_PROC
);
400 gen_proc_t
* gen_proc
= item
-> var
-> gen_proc
;
402 int num_args
= item
-> num_args
;
403 gcc_jit_rvalue
*args
[num_args
];
405 oberon_expr_t
* expr
= item
-> args
;
406 for(int i
= 0; i
< num_args
; i
++)
408 args
[i
] = rvalue_from_expr(ctx
, expr
);
412 gcc_jit_function
* func
= gen_proc
-> gcc_func
;
413 right
= gcc_jit_context_new_call(
414 gcc_context
, NULL
, func
, num_args
, args
417 else if(item
-> mode
== MODE_INDEX
)
419 gcc_jit_lvalue
* left
= lvalue_from_item(ctx
, item
);
420 right
= gcc_jit_lvalue_as_rvalue(left
);
422 else if(item
-> mode
== MODE_FIELD
)
424 gen_var_t
* gen_var
= item
-> var
-> gen_var
;
425 gcc_jit_field
* gcc_field
= gen_var
-> gcc_field
;
427 gcc_jit_rvalue
* parent
= rvalue_from_item(ctx
, item
-> parent
);
428 right
= gcc_jit_rvalue_access_field(parent
, NULL
, gcc_field
);
432 oberon_error(ctx
, "rvalue_from_item: invalid mode %i", item
-> mode
);
439 int type
; // 0 - unary, 1 - binary, 2 - comp
441 enum gcc_jit_unary_op unary_op
;
442 enum gcc_jit_binary_op binary_op
;
443 enum gcc_jit_comparison comp_op
;
446 { 0, .unary_op
= GCC_JIT_UNARY_OP_LOGICAL_NEGATE
},
447 { 0, .unary_op
= GCC_JIT_UNARY_OP_MINUS
},
449 { 1, .binary_op
= GCC_JIT_BINARY_OP_PLUS
},
450 { 1, .binary_op
= GCC_JIT_BINARY_OP_MINUS
},
451 { 1, .binary_op
= GCC_JIT_BINARY_OP_MULT
},
452 { 1, .binary_op
= GCC_JIT_BINARY_OP_DIVIDE
},
453 { 1, .binary_op
= GCC_JIT_BINARY_OP_MODULO
},
454 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_AND
},
455 { 1, .binary_op
= GCC_JIT_BINARY_OP_LOGICAL_OR
},
457 { 2, .comp_op
= GCC_JIT_COMPARISON_EQ
},
458 { 2, .comp_op
= GCC_JIT_COMPARISON_NE
},
459 { 2, .comp_op
= GCC_JIT_COMPARISON_LT
},
460 { 2, .comp_op
= GCC_JIT_COMPARISON_LE
},
461 { 2, .comp_op
= GCC_JIT_COMPARISON_GT
},
462 { 2, .comp_op
= GCC_JIT_COMPARISON_GE
}
465 static gcc_jit_rvalue
*
466 rvalue_from_operator(oberon_context_t
* ctx
, oberon_oper_t
* operator)
468 gcc_jit_rvalue
* right
;
470 gen_context_t
* gen_context
= ctx
-> gen_context
;
471 gen_type_t
* gen_type
= operator -> result
-> gen_type
;
472 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
473 gcc_jit_type
* result_type
= gen_type
-> gcc_type
;
475 int expr_type
= op_table
[operator -> op
].type
;
478 enum gcc_jit_unary_op op
= op_table
[operator -> op
].unary_op
;
479 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
480 right
= gcc_jit_context_new_unary_op(gcc_context
, NULL
, op
, result_type
, l
);
482 else if(expr_type
== 1)
484 enum gcc_jit_unary_op op
= op_table
[operator -> op
].binary_op
;
485 gcc_jit_rvalue
* l
= rvalue_from_expr(ctx
, operator -> left
);
486 gcc_jit_rvalue
* r
= rvalue_from_expr(ctx
, operator -> right
);
487 right
= gcc_jit_context_new_binary_op(gcc_context
, NULL
, op
, result_type
, l
, r
);
489 else if(expr_type
== 2)
491 enum gcc_jit_comparison op
= op_table
[operator -> op
].comp_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_comparison(gcc_context
, NULL
, op
, l
, r
);
498 oberon_error(ctx
, "rvalue_from_operator: wat");
504 static gcc_jit_rvalue
*
505 rvalue_from_expr(oberon_context_t
* ctx
, oberon_expr_t
* expr
)
507 gcc_jit_rvalue
* right
;
511 oberon_item_t
* item
= (oberon_item_t
*) expr
;
512 right
= rvalue_from_item(ctx
, item
);
516 oberon_oper_t
* operator = (oberon_oper_t
*) expr
;
517 right
= rvalue_from_operator(ctx
, operator);
524 oberon_generate_assign(oberon_context_t
* ctx
, oberon_expr_t
* src
, oberon_expr_t
* dst
)
526 gcc_jit_lvalue
* left
;
527 left
= lvalue_from_expr(ctx
, dst
);
529 gcc_jit_rvalue
* right
;
530 right
= rvalue_from_expr(ctx
, src
);
532 gen_context_t
* gen_context
= ctx
-> gen_context
;
533 gcc_jit_block
* gcc_block
= gen_context
-> gcc_block
;
534 gcc_jit_block_add_assignment(gcc_block
, NULL
, left
, right
);
538 oberon_generate_code(oberon_context_t
* ctx
)
540 gen_context_t
* gen_context
= ctx
-> gen_context
;
541 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
543 gcc_jit_result
* gcc_result
;
544 gcc_result
= gcc_jit_context_compile(gcc_context
);
546 gen_context
-> gcc_result
= gcc_result
;
547 ctx
-> mod
-> begin
= gcc_jit_result_get_code(gcc_result
, "BEGIN");
551 oberon_generator_dump(oberon_context_t
* ctx
, char * path
)
553 gen_context_t
* gen_context
= ctx
-> gen_context
;
554 gcc_jit_context
* gcc_context
= gen_context
-> gcc_context
;
555 gcc_jit_context_dump_to_file(gcc_context
, path
, 0);