1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
/*
* jit-compile.c - Function compilation.
*
* Copyright (C) 2004, 2006-2008 Southern Storm Software, Pty Ltd.
* Copyright (C) 2012 Aleksey Demakov
*
* This file is part of the libjit library.
*
* The libjit library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* The libjit library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the libjit library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "jit-internal.h"
#include "jit-rules.h"
#include "jit-reg-alloc.h"
#include "jit-setjmp.h"
#ifdef _JIT_COMPILE_DEBUG
# include <jit/jit-dump.h>
# include <stdio.h>
#endif
/*
* Misc data needed for compilation
*/
typedef struct
{
jit_function_t func;
int memory_locked;
int memory_started;
int restart;
int page_factor;
struct jit_gencode gen;
} _jit_compile_t;
#define _JIT_RESULT_TO_OBJECT(x) ((void *) ((jit_nint) (x) - JIT_RESULT_OK))
#define _JIT_RESULT_FROM_OBJECT(x) ((jit_nint) ((void *) (x)) + JIT_RESULT_OK)
/*
* This exception handler overrides a user-defined handler during compilation.
*/
static void *
internal_exception_handler(int exception_type)
{
return _JIT_RESULT_TO_OBJECT(exception_type);
}
/*
* Optimize a function.
*/
static void
optimize(jit_function_t func)
{
if(func->is_optimized || func->optimization_level == JIT_OPTLEVEL_NONE)
{
/* The function is already optimized or does not need optimization */
return;
}
/* Build control flow graph */
_jit_block_build_cfg(func);
/* Eliminate useless control flow */
_jit_block_clean_cfg(func);
/* Optimization is done */
func->is_optimized = 1;
}
/*@
* @deftypefun int jit_optimize (jit_function_t @var{func})
* Optimize a function by analyzing and transforming its intermediate
* representation. If the function was already compiled or optimized,
* then do nothing.
*
* Returns @code{JIT_RESUlT_OK} on success, otherwise it might return
* @code{JIT_RESULT_OUT_OF_MEMORY}, @code{JIT_RESULT_COMPILE_ERROR} or
* possibly some other more specific @code{JIT_RESULT_} code.
*
* Normally this function should not be used because @code{jit_compile}
* performs all the optimization anyway. However it might be useful for
* debugging to verify the effect of the @code{libjit} code optimization.
* This might be done, for instance, by calling @code{jit_dump_function}
* before and after @code{jit_optimize}.
* @end deftypefun
@*/
int
jit_optimize(jit_function_t func)
{
jit_jmp_buf jbuf;
jit_exception_func handler;
/* Bail out on invalid parameter */
if(!func)
{
return JIT_RESULT_NULL_FUNCTION;
}
/* Bail out if there is nothing to do here */
if(!func->builder)
{
if(func->is_compiled)
{
/* The function is already compiled and we can't optimize it */
return JIT_RESULT_OK;
}
else
{
/* We don't have anything to optimize at all */
return JIT_RESULT_NULL_FUNCTION;
}
}
/* Override user's exception handler */
handler = jit_exception_set_handler(internal_exception_handler);
/* Establish a "setjmp" point here so that we can unwind the
stack to this point when an exception occurs and then prevent
the exception from propagating further up the stack */
_jit_unwind_push_setjmp(&jbuf);
if(setjmp(jbuf.buf))
{
_jit_unwind_pop_setjmp();
jit_exception_set_handler(handler);
return _JIT_RESULT_FROM_OBJECT(jit_exception_get_last_and_clear());
}
/* Perform the optimizations */
optimize(func);
/* Restore the "setjmp" contexts and exit */
_jit_unwind_pop_setjmp();
jit_exception_set_handler(handler);
return JIT_RESULT_OK;
}
/*
* Mark the current position with a bytecode offset value.
*/
void
mark_offset(jit_gencode_t gen, jit_function_t func, unsigned long offset)
{
unsigned long native_offset = gen->ptr - gen->mem_start;
if(!_jit_varint_encode_uint(&gen->offset_encoder, (jit_uint) offset))
{
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
if(!_jit_varint_encode_uint(&gen->offset_encoder, (jit_uint) native_offset))
{
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
}
/*
* Compile a single basic block within a function.
*/
static void
compile_block(jit_gencode_t gen, jit_function_t func, jit_block_t block)
{
jit_insn_iter_t iter;
jit_insn_t insn;
#ifdef _JIT_COMPILE_DEBUG
printf("Block #%d: %d\n\n", func->builder->block_count++, block->label);
#endif
/* Iterate over all blocks in the function */
jit_insn_iter_init(&iter, block);
while((insn = jit_insn_iter_next(&iter)) != 0)
{
#ifdef _JIT_COMPILE_DEBUG
unsigned char *p1, *p2;
p1 = gen->ptr;
printf("Insn #%d: ", func->builder->insn_count++);
jit_dump_insn(stdout, func, insn);
printf("\nStart of binary code: 0x%08x\n", p1);
#endif
switch(insn->opcode)
{
case JIT_OP_NOP:
/* Ignore NOP's */
break;
case JIT_OP_CHECK_NULL:
/* Determine if we can optimize the null check away */
if(!_jit_insn_check_is_redundant(&iter))
{
_jit_gen_insn(gen, func, block, insn);
}
break;
#ifndef JIT_BACKEND_INTERP
case JIT_OP_CALL:
case JIT_OP_CALL_TAIL:
case JIT_OP_CALL_INDIRECT:
case JIT_OP_CALL_INDIRECT_TAIL:
case JIT_OP_CALL_VTABLE_PTR:
case JIT_OP_CALL_VTABLE_PTR_TAIL:
case JIT_OP_CALL_EXTERNAL:
case JIT_OP_CALL_EXTERNAL_TAIL:
/* Spill all caller-saved registers before a call */
_jit_regs_spill_all(gen);
/* Generate code for the instruction with the back end */
_jit_gen_insn(gen, func, block, insn);
/* Free outgoing registers if any */
_jit_regs_clear_all_outgoing(gen);
break;
#endif
#ifndef JIT_BACKEND_INTERP
case JIT_OP_INCOMING_REG:
/* Assign a register to an incoming value */
_jit_regs_set_incoming(gen,
(int)jit_value_get_nint_constant(insn->value2),
insn->value1);
/* Generate code for the instruction with the back end */
_jit_gen_insn(gen, func, block, insn);
break;
#endif
case JIT_OP_INCOMING_FRAME_POSN:
/* Set the frame position for an incoming value */
insn->value1->frame_offset = jit_value_get_nint_constant(insn->value2);
insn->value1->in_register = 0;
insn->value1->has_frame_offset = 1;
if(insn->value1->has_global_register)
{
insn->value1->in_global_register = 1;
_jit_gen_load_global(gen, insn->value1->global_reg, insn->value1);
}
else
{
insn->value1->in_frame = 1;
}
break;
#ifndef JIT_BACKEND_INTERP
case JIT_OP_OUTGOING_REG:
/* Copy a value into an outgoing register */
_jit_regs_set_outgoing(gen,
(int)jit_value_get_nint_constant(insn->value2),
insn->value1);
break;
#endif
case JIT_OP_OUTGOING_FRAME_POSN:
/* Set the frame position for an outgoing value */
insn->value1->frame_offset = jit_value_get_nint_constant(insn->value2);
insn->value1->in_register = 0;
insn->value1->in_global_register = 0;
insn->value1->in_frame = 0;
insn->value1->has_frame_offset = 1;
insn->value1->has_global_register = 0;
break;
#ifndef JIT_BACKEND_INTERP
case JIT_OP_RETURN_REG:
/* Assign a register to a return value */
_jit_regs_set_incoming(gen,
(int)jit_value_get_nint_constant(insn->value2),
insn->value1);
/* Generate code for the instruction with the back end */
_jit_gen_insn(gen, func, block, insn);
break;
#endif
case JIT_OP_MARK_OFFSET:
/* Mark the current code position as corresponding
to a particular bytecode offset */
mark_offset(gen, func, (unsigned long)(long)jit_value_get_nint_constant(insn->value1));
break;
default:
/* Generate code for the instruction with the back end */
_jit_gen_insn(gen, func, block, insn);
break;
}
#ifdef _JIT_COMPILE_DEBUG
p2 = gen->ptr;
printf("Length of binary code: %d\n\n", p2 - p1);
fflush(stdout);
#endif
}
}
/*
* Reset value on codegen restart.
*/
static void
reset_value(jit_value_t value)
{
value->reg = -1;
value->in_register = 0;
value->in_global_register = 0;
value->in_frame = 0;
}
/*
* Clean up the compilation state on codegen restart.
*/
static void
cleanup_on_restart(jit_gencode_t gen, jit_function_t func)
{
jit_block_t block;
jit_insn_iter_t iter;
jit_insn_t insn;
block = 0;
while((block = jit_block_next(func, block)) != 0)
{
/* Clear the block addresses and fixup lists */
block->address = 0;
block->fixup_list = 0;
block->fixup_absolute_list = 0;
/* Reset values referred to by block instructions */
jit_insn_iter_init(&iter, block);
while((insn = jit_insn_iter_next(&iter)) != 0)
{
if(insn->dest && (insn->flags & JIT_INSN_DEST_OTHER_FLAGS) == 0)
{
reset_value(insn->dest);
}
if(insn->value1 && (insn->flags & JIT_INSN_VALUE1_OTHER_FLAGS) == 0)
{
reset_value(insn->value1);
}
if(insn->value2 && (insn->flags & JIT_INSN_VALUE2_OTHER_FLAGS) == 0)
{
reset_value(insn->value2);
}
}
}
/* Reset values referred to by builder */
if(func->builder->setjmp_value)
{
reset_value(func->builder->setjmp_value);
}
if(func->builder->parent_frame)
{
reset_value(func->builder->parent_frame);
}
/* Reset the "touched" registers mask. The first time compilation
might have followed wrong code paths and thus allocated wrong
registers. */
if(func->builder->has_tail_call)
{
/* For functions with tail calls _jit_regs_alloc_global()
does not allocate any global registers. The "permanent"
mask has all global registers set to prevent their use. */
gen->touched = jit_regused_init;
}
else
{
gen->touched = gen->permanent;
}
/* Reset the epilog fixup list */
gen->epilog_fixup = 0;
}
/*
* Acquire the memory context.
*/
static void
memory_acquire(_jit_compile_t *state)
{
/* Store the function's context as codegen context */
state->gen.context = state->func->context;
/* Acquire the memory context lock */
_jit_memory_lock(state->gen.context);
/* Remember that the lock is acquired */
state->memory_locked = 1;
if(!_jit_memory_ensure(state->gen.context))
{
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
}
/*
* Release the memory context.
*/
static void
memory_release(_jit_compile_t *state)
{
/* Release the lock if it was previously acquired */
if(state->memory_locked)
{
_jit_memory_unlock(state->gen.context);
state->memory_locked = 0;
}
}
/*
* Align the method code on a particular boundary if the
* difference between the current position and the aligned
* boundary is less than "diff". The "nop" value is used
* to pad unused bytes.
*/
static void
memory_align(_jit_compile_t *state, int align, int diff, int nop)
{
jit_nuint p, n;
/* Adjust the required alignment */
if(align < 1)
{
align = 1;
}
/* Determine the location of the next alignment boundary */
p = (jit_nuint) state->gen.ptr;
n = (p + (jit_nuint) align - 1) & ~((jit_nuint) align - 1);
if(p == n || (p - n) >= (jit_nuint) diff)
{
return;
}
/* Determine the actual alignment */
align = (int) (n - p);
/* Detect overflow of the free memory region */
_jit_gen_check_space(&state->gen, align);
#ifdef jit_should_pad
/* Use CPU-specific padding, because it may be more efficient */
_jit_pad_buffer(state->gen.ptr, align);
#else
jit_memset(state->gen.ptr, nop, align);
state->gen.ptr += align;
#endif
}
/*
* Prepare to start code generation with just allocated code space.
*/
static void
memory_start(_jit_compile_t *state)
{
/* Remember the memory context state */
state->memory_started = 1;
/* Store the bounds of the available space */
state->gen.mem_start = _jit_memory_get_break(state->gen.context);
state->gen.mem_limit = _jit_memory_get_limit(state->gen.context);
/* Align the function code start as required */
state->gen.ptr = state->gen.mem_start;
memory_align(state, JIT_FUNCTION_ALIGNMENT, JIT_FUNCTION_ALIGNMENT, 0);
/* Prepare the bytecode offset encoder */
_jit_varint_init_encoder(&state->gen.offset_encoder);
}
/*
* Allocate some amount of code space.
*/
static void
memory_alloc(_jit_compile_t *state)
{
int result;
/* Try to allocate within the current memory limit */
result = _jit_memory_start_function(state->gen.context, state->func);
if(result == JIT_MEMORY_RESTART)
{
/* Not enough space. Request to extend the limit and retry */
_jit_memory_extend_limit(state->gen.context, state->page_factor++);
result = _jit_memory_start_function(state->gen.context, state->func);
}
if(result != JIT_MEMORY_OK)
{
/* Failed to allocate any space */
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
/* Start with with allocated space */
memory_start(state);
}
/*
* Finish code generation.
*/
static void
memory_flush(_jit_compile_t *state)
{
int result;
if(state->memory_started)
{
/* Reset the memory state */
state->memory_started = 0;
/* Let the memory context know the address we ended at */
_jit_memory_set_break(state->gen.context, state->gen.code_end);
/* Finally end the function */
result = _jit_memory_end_function(state->gen.context, JIT_MEMORY_OK);
if(result != JIT_MEMORY_OK)
{
if(result == JIT_MEMORY_RESTART)
{
/* Throw an internal exception that causes
a larger code space to be allocated and
the code generation to restart */
jit_exception_builtin(JIT_RESULT_MEMORY_FULL);
}
else
{
/* Throw exception that indicates failure
to allocate enough code space */
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
}
#ifndef JIT_BACKEND_INTERP
/* On success perform a CPU cache flush, to make the code executable */
_jit_flush_exec(state->gen.code_start,
state->gen.code_end - state->gen.code_start);
#endif
/* Terminate the debug information and flush it */
if(!_jit_varint_encode_end(&state->gen.offset_encoder))
{
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
state->func->bytecode_offset = _jit_varint_get_data(&state->gen.offset_encoder);
}
}
/*
* Give back the allocated space in case of failure to generate the code.
*/
static void
memory_abort(_jit_compile_t *state)
{
if(state->memory_started)
{
state->memory_started = 0;
/* Release the code space */
_jit_memory_end_function(state->gen.context, JIT_MEMORY_RESTART);
/* Free encoded bytecode offset data */
_jit_varint_free_data(_jit_varint_get_data(&state->gen.offset_encoder));
}
}
/*
* Allocate more code space.
*/
static void
memory_realloc(_jit_compile_t *state)
{
int result;
/* Release the previously allocated code space */
memory_abort(state);
/* Request to extend memory limit and retry space allocation */
_jit_memory_extend_limit(state->gen.context, state->page_factor++);
result = _jit_memory_start_function(state->gen.context, state->func);
if(result != JIT_MEMORY_OK)
{
/* Failed to allocate enough space */
jit_exception_builtin(JIT_RESULT_OUT_OF_MEMORY);
}
/* Start with with allocated space */
memory_start(state);
}
/*
* Prepare function info needed for code generation.
*/
static void
codegen_prepare(_jit_compile_t *state)
{
/* Intuit "nothrow" and "noreturn" flags for this function */
if(!state->func->builder->may_throw)
{
state->func->no_throw = 1;
}
if(!state->func->builder->ordinary_return)
{
state->func->no_return = 1;
}
/* Compute liveness and "next use" information for this function */
_jit_function_compute_liveness(state->func);
/* Allocate global registers to variables within the function */
#ifndef JIT_BACKEND_INTERP
_jit_regs_alloc_global(&state->gen, state->func);
#endif
}
/*
* Run codegen.
*/
static void
codegen(_jit_compile_t *state)
{
jit_function_t func = state->func;
struct jit_gencode *gen = &state->gen;
jit_block_t block;
/* Remember the start code address (due to alignment it may differ from
the available space start - gen->start) */
gen->code_start = gen->ptr;
#ifdef JIT_PROLOG_SIZE
/* Output space for the function prolog */
_jit_gen_check_space(gen, JIT_PROLOG_SIZE);
gen->ptr += JIT_PROLOG_SIZE;
#endif
/* Generate code for the blocks in the function */
block = 0;
while((block = jit_block_next(func, block)) != 0)
{
/* Notify the back end that the block is starting */
_jit_gen_start_block(gen, block);
#ifndef JIT_BACKEND_INTERP
/* Clear the local register assignments */
_jit_regs_init_for_block(gen);
#endif
/* Generate the block's code */
compile_block(gen, func, block);
#ifndef JIT_BACKEND_INTERP
/* Spill all live register values back to their frame positions */
_jit_regs_spill_all(gen);
#endif
/* Notify the back end that the block is finished */
_jit_gen_end_block(gen, block);
}
/* Output the function epilog. All return paths will jump to here */
_jit_gen_epilog(gen, func);
/* Remember the end code address */
gen->code_end = gen->ptr;
#ifdef JIT_PROLOG_SIZE
/* Back-patch the function prolog and get the real entry point */
gen->code_start = _jit_gen_prolog(gen, func, gen->code_start);
#endif
#if !defined(JIT_BACKEND_INTERP) && (!defined(jit_redirector_size) || !defined(jit_indirector_size))
/* If the function is recompilable, then we need an extra entry
point to properly redirect previous references to the function */
if(func->is_recompilable && !func->indirector)
{
/* TODO: use _jit_create_indirector() instead of
_jit_gen_redirector() as both do the same. */
func->indirector = _jit_gen_redirector(&gen, func);
}
#endif
}
/*
* Compile a function and return its entry point.
*/
static int
compile(_jit_compile_t *state, jit_function_t func)
{
jit_exception_func handler;
jit_jmp_buf jbuf;
int result;
/* Initialize compilation state */
jit_memzero(state, sizeof(_jit_compile_t));
state->func = func;
/* Replace user's exception handler with internal handler */
handler = jit_exception_set_handler(internal_exception_handler);
/* Establish a "setjmp" point here so that we can unwind the
stack to this point when an exception occurs and then prevent
the exception from propagating further up the stack */
_jit_unwind_push_setjmp(&jbuf);
restart:
/* Handle compilation exceptions */
if(setjmp(jbuf.buf))
{
result = _JIT_RESULT_FROM_OBJECT(jit_exception_get_last_and_clear());
if(result == JIT_RESULT_MEMORY_FULL)
{
/* Restart code generation after the memory full condition */
state->restart = 1;
goto restart;
}
/* Release allocated code space and exit */
memory_abort(state);
goto exit;
}
if(state->restart == 0)
{
/* Start compilation */
/* Perform machine-independent optimizations */
optimize(state->func);
/* Prepare data needed for code generation */
codegen_prepare(state);
/* Allocate some space */
memory_acquire(state);
memory_alloc(state);
}
else
{
/* Restart compilation */
/* Clean up the compilation state */
cleanup_on_restart(&state->gen, state->func);
/* Allocate more space */
memory_realloc(state);
}
#ifdef _JIT_COMPILE_DEBUG
if(state->restart == 0)
{
printf("\n*** Start code generation ***\n\n");
}
else
{
printf("\n*** Restart code generation ***\n\n");
}
state->func->builder->block_count = 0;
state->func->builder->insn_count = 0;
#endif
#ifdef jit_extra_gen_init
/* Initialize information that may need to be reset both
on start and restart */
jit_extra_gen_init(&state->gen);
#endif
/* Perform code generation */
codegen(state);
#ifdef jit_extra_gen_cleanup
/* Clean up the extra code generation state */
jit_extra_gen_cleanup(&state->gen);
#endif
/* End the function's output process */
memory_flush(state);
/* Compilation done, no exceptions occurred */
result = JIT_RESULT_OK;
exit:
/* Release the memory context */
memory_release(state);
/* Restore the "setjmp" context */
_jit_unwind_pop_setjmp();
/* Restore user's exception handler */
jit_exception_set_handler(handler);
return result;
}
/*@
* @deftypefun int jit_compile (jit_function_t @var{func})
* Compile a function to its executable form. If the function was
* already compiled, then do nothing. Returns zero on error.
*
* If an error occurs, you can use @code{jit_function_abandon} to
* completely destroy the function. Once the function has been compiled
* successfully, it can no longer be abandoned.
*
* Sometimes you may wish to recompile a function, to apply greater
* levels of optimization the second time around. You must call
* @code{jit_function_set_recompilable} before you compile the function
* the first time. On the second time around, build the function's
* instructions again, and call @code{jit_compile} a second time.
* @end deftypefun
@*/
int
jit_compile(jit_function_t func)
{
_jit_compile_t state;
int result;
/* Bail out on invalid parameter */
if(!func)
{
return JIT_RESULT_NULL_FUNCTION;
}
/* Bail out if there is nothing to do here */
if(!func->builder)
{
if(func->is_compiled)
{
/* The function is already compiled, and we don't need to recompile */
return JIT_RESULT_OK;
}
else
{
/* We don't have anything to compile at all */
return JIT_RESULT_NULL_FUNCTION;
}
}
/* Compile and record the entry point */
result = compile(&state, func);
if(result == JIT_RESULT_OK)
{
func->entry_point = state.gen.code_start;
func->is_compiled = 1;
/* Free the builder structure, which we no longer require */
_jit_function_free_builder(func);
}
return result;
}
/*@
* @deftypefun int jit_compile_entry (jit_function_t @var{func}, void **@var{entry_point})
* Compile a function to its executable form but do not make it
* available for invocation yet. It may be made available later
* with @code{jit_function_setup_entry}.
* @end deftypefun
@*/
int
jit_compile_entry(jit_function_t func, void **entry_point)
{
_jit_compile_t state;
int result;
/* Init entry_point */
if(entry_point)
{
*entry_point = 0;
}
else
{
return JIT_RESULT_NULL_REFERENCE;
}
/* Bail out on invalid parameter */
if(!func)
{
return JIT_RESULT_NULL_FUNCTION;
}
/* Bail out if there is nothing to do here */
if(!func->builder)
{
if(func->is_compiled)
{
/* The function is already compiled, and we don't need to recompile */
*entry_point = func->entry_point;
return JIT_RESULT_OK;
}
else
{
/* We don't have anything to compile at all */
return JIT_RESULT_NULL_FUNCTION;
}
}
/* Compile and return the entry point */
result = compile(&state, func);
if(result == JIT_RESULT_OK)
{
*entry_point = state.gen.code_start;
}
return result;
}
/*@
* @deftypefun int jit_function_setup_entry (jit_function_t @var{func}, void *@var{entry_point})
* Make a function compiled with @code{jit_function_compile_entry}
* available for invocation and free the resources used for
* compilation. If @var{entry_point} is null then it only
* frees the resources.
* @end deftypefun
@*/
void
jit_function_setup_entry(jit_function_t func, void *entry_point)
{
/* Bail out if we have nothing to do */
if(!func)
{
return;
}
/* Record the entry point */
if(entry_point)
{
func->entry_point = entry_point;
func->is_compiled = 1;
}
_jit_function_free_builder(func);
}
/*@
* @deftypefun int jit_function_compile (jit_function_t @var{func})
* Compile a function to its executable form. If the function was
* already compiled, then do nothing. Returns zero on error.
*
* If an error occurs, you can use @code{jit_function_abandon} to
* completely destroy the function. Once the function has been compiled
* successfully, it can no longer be abandoned.
*
* Sometimes you may wish to recompile a function, to apply greater
* levels of optimization the second time around. You must call
* @code{jit_function_set_recompilable} before you compile the function
* the first time. On the second time around, build the function's
* instructions again, and call @code{jit_function_compile}
* a second time.
* @end deftypefun
@*/
int
jit_function_compile(jit_function_t func)
{
return (JIT_RESULT_OK == jit_compile(func));
}
/*@
* @deftypefun int jit_function_compile_entry (jit_function_t @var{func}, void **@var{entry_point})
* Compile a function to its executable form but do not make it
* available for invocation yet. It may be made available later
* with @code{jit_function_setup_entry}.
* @end deftypefun
@*/
int
jit_function_compile_entry(jit_function_t func, void **entry_point)
{
return (JIT_RESULT_OK == jit_compile_entry(func, entry_point));
}
void *
_jit_function_compile_on_demand(jit_function_t func)
{
_jit_compile_t state;
int result;
/* Lock down the context */
jit_context_build_start(func->context);
/* Fast return if we are already compiled */
if(func->is_compiled)
{
jit_context_build_end(func->context);
return func->entry_point;
}
if(!func->on_demand)
{
/* Bail out with an error if the user didn't supply an
on-demand compiler */
result = JIT_RESULT_COMPILE_ERROR;
}
else
{
/* Call the user's on-demand compiler. */
result = (func->on_demand)(func);
if(result == JIT_RESULT_OK && !func->is_compiled)
{
/* Compile the function if the user didn't do so */
result = compile(&state, func);
if(result == JIT_RESULT_OK)
{
func->entry_point = state.gen.code_start;
func->is_compiled = 1;
}
}
_jit_function_free_builder(func);
}
/* Unlock the context and report the result */
jit_context_build_end(func->context);
if(result != JIT_RESULT_OK)
{
jit_exception_builtin(result);
/* Normally this should be unreachable but just in case... */
return 0;
}
return func->entry_point;
}
#define JIT_CACHE_NO_OFFSET (~((unsigned long)0))
unsigned long
_jit_function_get_bytecode(jit_function_t func,
void *func_info, void *pc, int exact)
{
unsigned long offset = JIT_CACHE_NO_OFFSET;
void *start;
unsigned long native_offset;
jit_varint_decoder_t decoder;
jit_uint off, noff;
start = _jit_memory_get_function_start(func->context, func_info);
native_offset = pc - start;
_jit_varint_init_decoder(&decoder, func->bytecode_offset);
for(;;)
{
off = _jit_varint_decode_uint(&decoder);
noff = _jit_varint_decode_uint(&decoder);
if(_jit_varint_decode_end(&decoder))
{
if(exact)
{
offset = JIT_CACHE_NO_OFFSET;
}
break;
}
if(noff >= native_offset)
{
if(noff == native_offset)
{
offset = off;
}
else if (exact)
{
offset = JIT_CACHE_NO_OFFSET;
}
break;
}
offset = off;
}
return offset;
}