forked from emacs-mirror/emacs
/
comp.c
5279 lines (4643 loc) · 156 KB
/
comp.c
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
/* Compile elisp into native code.
Copyright (C) 2019-2021 Free Software Foundation, Inc.
Author: Andrea Corallo <akrl@sdf.org>
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include "lisp.h"
#ifdef HAVE_NATIVE_COMP
#include <setjmp.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <libgccjit.h>
#include <epaths.h>
#include "puresize.h"
#include "window.h"
#include "dynlib.h"
#include "buffer.h"
#include "blockinput.h"
#include "md5.h"
#include "sysstdio.h"
#include "zlib.h"
/********************************/
/* Dynamic loading of libgccjit */
/********************************/
#ifdef WINDOWSNT
# include "w32common.h"
#undef gcc_jit_block_add_assignment
#undef gcc_jit_block_add_comment
#undef gcc_jit_block_add_eval
#undef gcc_jit_block_end_with_conditional
#undef gcc_jit_block_end_with_jump
#undef gcc_jit_block_end_with_return
#undef gcc_jit_block_end_with_void_return
#undef gcc_jit_context_acquire
#undef gcc_jit_context_add_driver_option
#undef gcc_jit_context_compile_to_file
#undef gcc_jit_context_dump_reproducer_to_file
#undef gcc_jit_context_dump_to_file
#undef gcc_jit_context_get_builtin_function
#undef gcc_jit_context_get_first_error
#undef gcc_jit_context_get_int_type
#undef gcc_jit_context_get_type
#undef gcc_jit_context_new_array_access
#undef gcc_jit_context_new_array_type
#undef gcc_jit_context_new_binary_op
#undef gcc_jit_context_new_call
#undef gcc_jit_context_new_call_through_ptr
#undef gcc_jit_context_new_comparison
#undef gcc_jit_context_new_field
#undef gcc_jit_context_new_function
#undef gcc_jit_context_new_function_ptr_type
#undef gcc_jit_context_new_global
#undef gcc_jit_context_new_opaque_struct
#undef gcc_jit_context_new_param
#undef gcc_jit_context_new_rvalue_from_int
#undef gcc_jit_context_new_rvalue_from_long
#undef gcc_jit_context_new_rvalue_from_ptr
#undef gcc_jit_context_new_string_literal
#undef gcc_jit_context_new_struct_type
#undef gcc_jit_context_new_unary_op
#undef gcc_jit_context_new_union_type
#undef gcc_jit_context_release
#undef gcc_jit_context_set_bool_option
#undef gcc_jit_context_set_int_option
#undef gcc_jit_context_set_logfile
#undef gcc_jit_function_get_param
#undef gcc_jit_function_new_block
#undef gcc_jit_function_new_local
#undef gcc_jit_global_set_initializer
#undef gcc_jit_lvalue_access_field
#undef gcc_jit_lvalue_as_rvalue
#undef gcc_jit_lvalue_get_address
#undef gcc_jit_param_as_lvalue
#undef gcc_jit_param_as_rvalue
#undef gcc_jit_rvalue_access_field
#undef gcc_jit_rvalue_dereference
#undef gcc_jit_rvalue_dereference_field
#undef gcc_jit_rvalue_get_type
#undef gcc_jit_struct_as_type
#undef gcc_jit_struct_set_fields
#undef gcc_jit_type_get_const
#undef gcc_jit_type_get_pointer
#undef gcc_jit_version_major
#undef gcc_jit_version_minor
#undef gcc_jit_version_patchlevel
/* In alphabetical order */
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_rvalue_from_int,
(gcc_jit_context *ctxt, gcc_jit_type *numeric_type, int value));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_lvalue_as_rvalue,
(gcc_jit_lvalue *lvalue));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_rvalue_access_field,
(gcc_jit_rvalue *struct_or_union, gcc_jit_location *loc,
gcc_jit_field *field));
DEF_DLL_FN (void, gcc_jit_block_add_comment,
(gcc_jit_block *block, gcc_jit_location *loc, const char *text));
DEF_DLL_FN (void, gcc_jit_context_release, (gcc_jit_context *ctxt));
DEF_DLL_FN (const char *, gcc_jit_context_get_first_error,
(gcc_jit_context *ctxt));
DEF_DLL_FN (gcc_jit_block *, gcc_jit_function_new_block,
(gcc_jit_function *func, const char *name));
DEF_DLL_FN (gcc_jit_context *, gcc_jit_context_acquire, (void));
DEF_DLL_FN (void, gcc_jit_context_add_driver_option,
(gcc_jit_context *ctxt, const char *optname));
DEF_DLL_FN (gcc_jit_field *, gcc_jit_context_new_field,
(gcc_jit_context *ctxt, gcc_jit_location *loc, gcc_jit_type *type,
const char *name));
DEF_DLL_FN (gcc_jit_function *, gcc_jit_context_get_builtin_function,
(gcc_jit_context *ctxt, const char *name));
DEF_DLL_FN (gcc_jit_function *, gcc_jit_context_new_function,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
enum gcc_jit_function_kind kind, gcc_jit_type *return_type,
const char *name, int num_params, gcc_jit_param **params,
int is_variadic));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_context_new_array_access,
(gcc_jit_context *ctxt, gcc_jit_location *loc, gcc_jit_rvalue *ptr,
gcc_jit_rvalue *index));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_context_new_global,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
enum gcc_jit_global_kind kind, gcc_jit_type *type,
const char *name));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_function_new_local,
(gcc_jit_function *func, gcc_jit_location *loc, gcc_jit_type *type,
const char *name));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_global_set_initializer,
(gcc_jit_lvalue *global, const void *blob, size_t num_bytes));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_lvalue_access_field,
(gcc_jit_lvalue *struct_or_union, gcc_jit_location *loc,
gcc_jit_field *field));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_param_as_lvalue, (gcc_jit_param *param));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_rvalue_dereference,
(gcc_jit_rvalue *rvalue, gcc_jit_location *loc));
DEF_DLL_FN (gcc_jit_lvalue *, gcc_jit_rvalue_dereference_field,
(gcc_jit_rvalue *ptr, gcc_jit_location *loc, gcc_jit_field *field));
DEF_DLL_FN (gcc_jit_param *, gcc_jit_context_new_param,
(gcc_jit_context *ctxt, gcc_jit_location *loc, gcc_jit_type *type,
const char *name));
DEF_DLL_FN (gcc_jit_param *, gcc_jit_function_get_param,
(gcc_jit_function *func, int index));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_binary_op,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
enum gcc_jit_binary_op op, gcc_jit_type *result_type,
gcc_jit_rvalue *a, gcc_jit_rvalue *b));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_call,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
gcc_jit_function *func, int numargs , gcc_jit_rvalue **args));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_call_through_ptr,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
gcc_jit_rvalue *fn_ptr, int numargs, gcc_jit_rvalue **args));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_comparison,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
enum gcc_jit_comparison op, gcc_jit_rvalue *a, gcc_jit_rvalue *b));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_rvalue_from_long,
(gcc_jit_context *ctxt, gcc_jit_type *numeric_type, long value));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_rvalue_from_ptr,
(gcc_jit_context *ctxt, gcc_jit_type *pointer_type, void *value));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_string_literal,
(gcc_jit_context *ctxt, const char *value));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_context_new_unary_op,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
enum gcc_jit_unary_op op, gcc_jit_type *result_type,
gcc_jit_rvalue *rvalue));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_lvalue_get_address,
(gcc_jit_lvalue *lvalue, gcc_jit_location *loc));
DEF_DLL_FN (gcc_jit_rvalue *, gcc_jit_param_as_rvalue, (gcc_jit_param *param));
DEF_DLL_FN (gcc_jit_struct *, gcc_jit_context_new_opaque_struct,
(gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name));
DEF_DLL_FN (gcc_jit_struct *, gcc_jit_context_new_struct_type,
(gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name,
int num_fields, gcc_jit_field **fields));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_context_get_int_type,
(gcc_jit_context *ctxt, int num_bytes, int is_signed));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_context_get_type,
(gcc_jit_context *ctxt, enum gcc_jit_types type_));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_context_new_array_type,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
gcc_jit_type *element_type, int num_elements));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_context_new_function_ptr_type,
(gcc_jit_context *ctxt, gcc_jit_location *loc,
gcc_jit_type *return_type, int num_params,
gcc_jit_type **param_types, int is_variadic));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_context_new_union_type,
(gcc_jit_context *ctxt, gcc_jit_location *loc, const char *name,
int num_fields, gcc_jit_field **fields));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_rvalue_get_type, (gcc_jit_rvalue *rvalue));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_struct_as_type,
(gcc_jit_struct *struct_type));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_type_get_const, (gcc_jit_type *type));
DEF_DLL_FN (gcc_jit_type *, gcc_jit_type_get_pointer, (gcc_jit_type *type));
DEF_DLL_FN (void, gcc_jit_block_add_assignment,
(gcc_jit_block *block, gcc_jit_location *loc, gcc_jit_lvalue *lvalue,
gcc_jit_rvalue *rvalue));
DEF_DLL_FN (void, gcc_jit_block_add_eval,
(gcc_jit_block *block, gcc_jit_location *loc,
gcc_jit_rvalue *rvalue));
DEF_DLL_FN (void, gcc_jit_block_end_with_conditional,
(gcc_jit_block *block, gcc_jit_location *loc,
gcc_jit_rvalue *boolval, gcc_jit_block *on_true,
gcc_jit_block *on_false));
DEF_DLL_FN (void, gcc_jit_block_end_with_jump,
(gcc_jit_block *block, gcc_jit_location *loc,
gcc_jit_block *target));
DEF_DLL_FN (void, gcc_jit_block_end_with_return,
(gcc_jit_block *block, gcc_jit_location *loc,
gcc_jit_rvalue *rvalue));
DEF_DLL_FN (void, gcc_jit_block_end_with_void_return,
(gcc_jit_block *block, gcc_jit_location *loc));
DEF_DLL_FN (void, gcc_jit_context_compile_to_file,
(gcc_jit_context *ctxt, enum gcc_jit_output_kind output_kind,
const char *output_path));
DEF_DLL_FN (void, gcc_jit_context_dump_reproducer_to_file,
(gcc_jit_context *ctxt, const char *path));
DEF_DLL_FN (void, gcc_jit_context_dump_to_file,
(gcc_jit_context *ctxt, const char *path, int update_locations));
DEF_DLL_FN (void, gcc_jit_context_set_bool_option,
(gcc_jit_context *ctxt, enum gcc_jit_bool_option opt, int value));
DEF_DLL_FN (void, gcc_jit_context_set_int_option,
(gcc_jit_context *ctxt, enum gcc_jit_int_option opt, int value));
DEF_DLL_FN (void, gcc_jit_context_set_logfile,
(gcc_jit_context *ctxt, FILE *logfile, int flags, int verbosity));
DEF_DLL_FN (void, gcc_jit_struct_set_fields,
(gcc_jit_struct *struct_type, gcc_jit_location *loc, int num_fields,
gcc_jit_field **fields));
DEF_DLL_FN (int, gcc_jit_version_major, (void));
DEF_DLL_FN (int, gcc_jit_version_minor, (void));
DEF_DLL_FN (int, gcc_jit_version_patchlevel, (void));
static bool
init_gccjit_functions (void)
{
HMODULE library = w32_delayed_load (Qgccjit);
if (!library)
return false;
/* In alphabetical order */
LOAD_DLL_FN (library, gcc_jit_block_add_assignment);
LOAD_DLL_FN (library, gcc_jit_block_add_comment);
LOAD_DLL_FN (library, gcc_jit_block_add_eval);
LOAD_DLL_FN (library, gcc_jit_block_end_with_conditional);
LOAD_DLL_FN (library, gcc_jit_block_end_with_jump);
LOAD_DLL_FN (library, gcc_jit_block_end_with_return);
LOAD_DLL_FN (library, gcc_jit_block_end_with_void_return);
LOAD_DLL_FN (library, gcc_jit_context_acquire);
LOAD_DLL_FN (library, gcc_jit_context_compile_to_file);
LOAD_DLL_FN (library, gcc_jit_context_dump_reproducer_to_file);
LOAD_DLL_FN (library, gcc_jit_context_dump_to_file);
LOAD_DLL_FN (library, gcc_jit_context_get_builtin_function);
LOAD_DLL_FN (library, gcc_jit_context_get_first_error);
LOAD_DLL_FN (library, gcc_jit_context_get_int_type);
LOAD_DLL_FN (library, gcc_jit_context_get_type);
LOAD_DLL_FN (library, gcc_jit_context_new_array_access);
LOAD_DLL_FN (library, gcc_jit_context_new_array_type);
LOAD_DLL_FN (library, gcc_jit_context_new_binary_op);
LOAD_DLL_FN (library, gcc_jit_context_new_call);
LOAD_DLL_FN (library, gcc_jit_context_new_call_through_ptr);
LOAD_DLL_FN (library, gcc_jit_context_new_comparison);
LOAD_DLL_FN (library, gcc_jit_context_new_field);
LOAD_DLL_FN (library, gcc_jit_context_new_function);
LOAD_DLL_FN (library, gcc_jit_context_new_function_ptr_type);
LOAD_DLL_FN (library, gcc_jit_context_new_global);
LOAD_DLL_FN (library, gcc_jit_context_new_opaque_struct);
LOAD_DLL_FN (library, gcc_jit_context_new_param);
LOAD_DLL_FN (library, gcc_jit_context_new_rvalue_from_int);
LOAD_DLL_FN (library, gcc_jit_context_new_rvalue_from_long);
LOAD_DLL_FN (library, gcc_jit_context_new_rvalue_from_ptr);
LOAD_DLL_FN (library, gcc_jit_context_new_string_literal);
LOAD_DLL_FN (library, gcc_jit_context_new_struct_type);
LOAD_DLL_FN (library, gcc_jit_context_new_unary_op);
LOAD_DLL_FN (library, gcc_jit_context_new_union_type);
LOAD_DLL_FN (library, gcc_jit_context_release);
LOAD_DLL_FN (library, gcc_jit_context_set_bool_option);
LOAD_DLL_FN (library, gcc_jit_context_set_int_option);
LOAD_DLL_FN (library, gcc_jit_context_set_logfile);
LOAD_DLL_FN (library, gcc_jit_function_get_param);
LOAD_DLL_FN (library, gcc_jit_function_new_block);
LOAD_DLL_FN (library, gcc_jit_function_new_local);
LOAD_DLL_FN (library, gcc_jit_lvalue_access_field);
LOAD_DLL_FN (library, gcc_jit_lvalue_as_rvalue);
LOAD_DLL_FN (library, gcc_jit_lvalue_get_address);
LOAD_DLL_FN (library, gcc_jit_param_as_lvalue);
LOAD_DLL_FN (library, gcc_jit_param_as_rvalue);
LOAD_DLL_FN (library, gcc_jit_rvalue_access_field);
LOAD_DLL_FN (library, gcc_jit_rvalue_dereference);
LOAD_DLL_FN (library, gcc_jit_rvalue_dereference_field);
LOAD_DLL_FN (library, gcc_jit_rvalue_get_type);
LOAD_DLL_FN (library, gcc_jit_struct_as_type);
LOAD_DLL_FN (library, gcc_jit_struct_set_fields);
LOAD_DLL_FN (library, gcc_jit_type_get_const);
LOAD_DLL_FN (library, gcc_jit_type_get_pointer);
LOAD_DLL_FN_OPT (library, gcc_jit_context_add_driver_option);
LOAD_DLL_FN_OPT (library, gcc_jit_global_set_initializer);
LOAD_DLL_FN_OPT (library, gcc_jit_version_major);
LOAD_DLL_FN_OPT (library, gcc_jit_version_minor);
LOAD_DLL_FN_OPT (library, gcc_jit_version_patchlevel);
return true;
}
/* In alphabetical order */
#define gcc_jit_block_add_assignment fn_gcc_jit_block_add_assignment
#define gcc_jit_block_add_comment fn_gcc_jit_block_add_comment
#define gcc_jit_block_add_eval fn_gcc_jit_block_add_eval
#define gcc_jit_block_end_with_conditional fn_gcc_jit_block_end_with_conditional
#define gcc_jit_block_end_with_jump fn_gcc_jit_block_end_with_jump
#define gcc_jit_block_end_with_return fn_gcc_jit_block_end_with_return
#define gcc_jit_block_end_with_void_return fn_gcc_jit_block_end_with_void_return
#define gcc_jit_context_acquire fn_gcc_jit_context_acquire
#define gcc_jit_context_add_driver_option fn_gcc_jit_context_add_driver_option
#define gcc_jit_context_compile_to_file fn_gcc_jit_context_compile_to_file
#define gcc_jit_context_dump_reproducer_to_file fn_gcc_jit_context_dump_reproducer_to_file
#define gcc_jit_context_dump_to_file fn_gcc_jit_context_dump_to_file
#define gcc_jit_context_get_builtin_function fn_gcc_jit_context_get_builtin_function
#define gcc_jit_context_get_first_error fn_gcc_jit_context_get_first_error
#define gcc_jit_context_get_int_type fn_gcc_jit_context_get_int_type
#define gcc_jit_context_get_type fn_gcc_jit_context_get_type
#define gcc_jit_context_new_array_access fn_gcc_jit_context_new_array_access
#define gcc_jit_context_new_array_type fn_gcc_jit_context_new_array_type
#define gcc_jit_context_new_binary_op fn_gcc_jit_context_new_binary_op
#define gcc_jit_context_new_call fn_gcc_jit_context_new_call
#define gcc_jit_context_new_call_through_ptr fn_gcc_jit_context_new_call_through_ptr
#define gcc_jit_context_new_comparison fn_gcc_jit_context_new_comparison
#define gcc_jit_context_new_field fn_gcc_jit_context_new_field
#define gcc_jit_context_new_function fn_gcc_jit_context_new_function
#define gcc_jit_context_new_function_ptr_type fn_gcc_jit_context_new_function_ptr_type
#define gcc_jit_context_new_global fn_gcc_jit_context_new_global
#define gcc_jit_context_new_opaque_struct fn_gcc_jit_context_new_opaque_struct
#define gcc_jit_context_new_param fn_gcc_jit_context_new_param
#define gcc_jit_context_new_rvalue_from_int fn_gcc_jit_context_new_rvalue_from_int
#define gcc_jit_context_new_rvalue_from_long fn_gcc_jit_context_new_rvalue_from_long
#define gcc_jit_context_new_rvalue_from_ptr fn_gcc_jit_context_new_rvalue_from_ptr
#define gcc_jit_context_new_string_literal fn_gcc_jit_context_new_string_literal
#define gcc_jit_context_new_struct_type fn_gcc_jit_context_new_struct_type
#define gcc_jit_context_new_unary_op fn_gcc_jit_context_new_unary_op
#define gcc_jit_context_new_union_type fn_gcc_jit_context_new_union_type
#define gcc_jit_context_release fn_gcc_jit_context_release
#define gcc_jit_context_set_bool_option fn_gcc_jit_context_set_bool_option
#define gcc_jit_context_set_int_option fn_gcc_jit_context_set_int_option
#define gcc_jit_context_set_logfile fn_gcc_jit_context_set_logfile
#define gcc_jit_function_get_param fn_gcc_jit_function_get_param
#define gcc_jit_function_new_block fn_gcc_jit_function_new_block
#define gcc_jit_function_new_local fn_gcc_jit_function_new_local
#define gcc_jit_global_set_initializer fn_gcc_jit_global_set_initializer
#define gcc_jit_lvalue_access_field fn_gcc_jit_lvalue_access_field
#define gcc_jit_lvalue_as_rvalue fn_gcc_jit_lvalue_as_rvalue
#define gcc_jit_lvalue_get_address fn_gcc_jit_lvalue_get_address
#define gcc_jit_param_as_lvalue fn_gcc_jit_param_as_lvalue
#define gcc_jit_param_as_rvalue fn_gcc_jit_param_as_rvalue
#define gcc_jit_rvalue_access_field fn_gcc_jit_rvalue_access_field
#define gcc_jit_rvalue_dereference fn_gcc_jit_rvalue_dereference
#define gcc_jit_rvalue_dereference_field fn_gcc_jit_rvalue_dereference_field
#define gcc_jit_rvalue_get_type fn_gcc_jit_rvalue_get_type
#define gcc_jit_struct_as_type fn_gcc_jit_struct_as_type
#define gcc_jit_struct_set_fields fn_gcc_jit_struct_set_fields
#define gcc_jit_type_get_const fn_gcc_jit_type_get_const
#define gcc_jit_type_get_pointer fn_gcc_jit_type_get_pointer
#define gcc_jit_version_major fn_gcc_jit_version_major
#define gcc_jit_version_minor fn_gcc_jit_version_minor
#define gcc_jit_version_patchlevel fn_gcc_jit_version_patchlevel
#endif
static bool
load_gccjit_if_necessary (bool mandatory)
{
#ifdef WINDOWSNT
static bool tried_to_initialize_once;
static bool gccjit_initialized;
if (!tried_to_initialize_once)
{
tried_to_initialize_once = true;
Lisp_Object status;
gccjit_initialized = init_gccjit_functions ();
status = gccjit_initialized ? Qt : Qnil;
Vlibrary_cache = Fcons (Fcons (Qgccjit, status), Vlibrary_cache);
}
if (mandatory && !gccjit_initialized)
xsignal1 (Qnative_compiler_error, build_string ("libgccjit not found"));
return gccjit_initialized;
#else
return true;
#endif
}
/* Increase this number to force a new Vcomp_abi_hash to be generated. */
#define ABI_VERSION "1"
/* Length of the hashes used for eln file naming. */
#define HASH_LENGTH 8
/* C symbols emitted for the load relocation mechanism. */
#define CURRENT_THREAD_RELOC_SYM "current_thread_reloc"
#define PURE_RELOC_SYM "pure_reloc"
#define DATA_RELOC_SYM "d_reloc"
#define DATA_RELOC_IMPURE_SYM "d_reloc_imp"
#define DATA_RELOC_EPHEMERAL_SYM "d_reloc_eph"
#define FUNC_LINK_TABLE_SYM "freloc_link_table"
#define LINK_TABLE_HASH_SYM "freloc_hash"
#define COMP_UNIT_SYM "comp_unit"
#define TEXT_DATA_RELOC_SYM "text_data_reloc"
#define TEXT_DATA_RELOC_IMPURE_SYM "text_data_reloc_imp"
#define TEXT_DATA_RELOC_EPHEMERAL_SYM "text_data_reloc_eph"
#define TEXT_OPTIM_QLY_SYM "text_optim_qly"
#define TEXT_FDOC_SYM "text_data_fdoc"
#define STR_VALUE(s) #s
#define STR(s) STR_VALUE (s)
#define FIRST(x) \
XCAR(x)
#define SECOND(x) \
XCAR (XCDR (x))
#define THIRD(x) \
XCAR (XCDR (XCDR (x)))
/* Like call1 but stringify and intern. */
#define CALL1I(fun, arg) \
CALLN (Ffuncall, intern_c_string (STR (fun)), arg)
/* Like call2 but stringify and intern. */
#define CALL2I(fun, arg1, arg2) \
CALLN (Ffuncall, intern_c_string (STR (fun)), arg1, arg2)
#define DECL_BLOCK(name, func) \
gcc_jit_block *(name) = \
gcc_jit_function_new_block ((func), STR (name))
#ifndef WINDOWSNT
# ifdef HAVE__SETJMP
# define SETJMP _setjmp
# else
# define SETJMP setjmp
# endif
#else
/* snippet from MINGW-64 setjmp.h */
# define SETJMP _setjmp
#endif
#define SETJMP_NAME SETJMP
/* Max number function importable by native compiled code. */
#define F_RELOC_MAX_SIZE 1500
typedef struct {
void *link_table[F_RELOC_MAX_SIZE];
ptrdiff_t size;
} f_reloc_t;
sigset_t saved_sigset;
static f_reloc_t freloc;
#define NUM_CAST_TYPES 15
enum cast_kind_of_type
{
kind_unsigned,
kind_signed,
kind_pointer
};
typedef struct {
EMACS_INT len;
gcc_jit_rvalue *r_val;
} reloc_array_t;
/* C side of the compiler context. */
typedef struct {
EMACS_INT speed;
EMACS_INT debug;
gcc_jit_context *ctxt;
gcc_jit_type *void_type;
gcc_jit_type *bool_type;
gcc_jit_type *char_type;
gcc_jit_type *int_type;
gcc_jit_type *unsigned_type;
gcc_jit_type *long_type;
gcc_jit_type *unsigned_long_type;
gcc_jit_type *long_long_type;
gcc_jit_type *unsigned_long_long_type;
gcc_jit_type *emacs_int_type;
gcc_jit_type *emacs_uint_type;
gcc_jit_type *void_ptr_type;
gcc_jit_type *char_ptr_type;
gcc_jit_type *ptrdiff_type;
gcc_jit_type *uintptr_type;
gcc_jit_type *size_t_type;
gcc_jit_type *lisp_word_type;
gcc_jit_type *lisp_word_tag_type;
#ifdef LISP_OBJECT_IS_STRUCT
gcc_jit_field *lisp_obj_i;
gcc_jit_struct *lisp_obj_s;
#endif
gcc_jit_type *lisp_obj_type;
gcc_jit_type *lisp_obj_ptr_type;
/* struct Lisp_Cons */
gcc_jit_struct *lisp_cons_s;
gcc_jit_field *lisp_cons_u;
gcc_jit_field *lisp_cons_u_s;
gcc_jit_field *lisp_cons_u_s_car;
gcc_jit_field *lisp_cons_u_s_u;
gcc_jit_field *lisp_cons_u_s_u_cdr;
gcc_jit_type *lisp_cons_type;
gcc_jit_type *lisp_cons_ptr_type;
/* struct jmp_buf. */
gcc_jit_struct *jmp_buf_s;
/* struct handler. */
gcc_jit_struct *handler_s;
gcc_jit_field *handler_jmp_field;
gcc_jit_field *handler_val_field;
gcc_jit_field *handler_next_field;
gcc_jit_type *handler_ptr_type;
gcc_jit_lvalue *loc_handler;
/* struct thread_state. */
gcc_jit_struct *thread_state_s;
gcc_jit_field *m_handlerlist;
gcc_jit_type *thread_state_ptr_type;
gcc_jit_rvalue *current_thread_ref;
/* Other globals. */
gcc_jit_rvalue *pure_ptr;
/* libgccjit has really limited support for casting therefore this union will
be used for the scope. */
gcc_jit_type *cast_union_type;
gcc_jit_function *cast_functions_from_to[NUM_CAST_TYPES][NUM_CAST_TYPES];
/* We add one to make space for the last member which is the "biggest_type"
member. */
gcc_jit_type *cast_types[NUM_CAST_TYPES + 1];
size_t cast_type_sizes[NUM_CAST_TYPES + 1];
enum cast_kind_of_type cast_type_kind[NUM_CAST_TYPES + 1];
const char *cast_type_names[NUM_CAST_TYPES + 1];
gcc_jit_field *cast_union_fields[NUM_CAST_TYPES + 1];
size_t cast_union_field_biggest_type;
gcc_jit_function *func; /* Current function being compiled. */
bool func_has_non_local; /* From comp-func has-non-local slot. */
EMACS_INT func_speed; /* From comp-func speed slot. */
gcc_jit_block *block; /* Current basic block being compiled. */
gcc_jit_lvalue *scratch; /* Used as scratch slot for some code sequence (switch). */
ptrdiff_t frame_size; /* Size of the following array in elements. */
gcc_jit_lvalue **frame; /* Frame slot n -> gcc_jit_lvalue *. */
gcc_jit_rvalue *zero;
gcc_jit_rvalue *one;
gcc_jit_rvalue *inttypebits;
gcc_jit_rvalue *lisp_int0;
gcc_jit_function *pseudovectorp;
gcc_jit_function *bool_to_lisp_obj;
gcc_jit_function *add1;
gcc_jit_function *sub1;
gcc_jit_function *negate;
gcc_jit_function *car;
gcc_jit_function *cdr;
gcc_jit_function *setcar;
gcc_jit_function *setcdr;
gcc_jit_function *check_type;
gcc_jit_function *check_impure;
gcc_jit_function *maybe_gc_or_quit;
Lisp_Object func_blocks_h; /* blk_name -> gcc_block. */
Lisp_Object exported_funcs_h; /* c-func-name -> gcc_jit_function *. */
Lisp_Object imported_funcs_h; /* subr_name -> gcc_jit_field *reloc_field. */
Lisp_Object emitter_dispatcher;
/* Synthesized struct holding data relocs. */
reloc_array_t data_relocs;
/* Same as before but can't go in pure space. */
reloc_array_t data_relocs_impure;
/* Same as before but content does not survive load phase. */
reloc_array_t data_relocs_ephemeral;
/* Global structure holding function relocations. */
gcc_jit_lvalue *func_relocs;
gcc_jit_type *func_relocs_ptr_type;
/* Pointer to this structure local to each function. */
gcc_jit_lvalue *func_relocs_local;
gcc_jit_function *memcpy;
Lisp_Object d_default_idx;
Lisp_Object d_impure_idx;
Lisp_Object d_ephemeral_idx;
} comp_t;
static comp_t comp;
FILE *logfile = NULL;
/* This is used for serialized objects by the reload mechanism. */
typedef struct {
ptrdiff_t len;
char data[];
} static_obj_t;
typedef struct {
reloc_array_t array;
gcc_jit_rvalue *idx;
} imm_reloc_t;
/*
Helper functions called by the run-time.
*/
void helper_unwind_protect (Lisp_Object handler);
Lisp_Object helper_temp_output_buffer_setup (Lisp_Object x);
Lisp_Object helper_unbind_n (Lisp_Object n);
void helper_save_restriction (void);
bool helper_PSEUDOVECTOR_TYPEP_XUNTAG (Lisp_Object a, enum pvec_type code);
void *helper_link_table[] =
{ wrong_type_argument,
helper_PSEUDOVECTOR_TYPEP_XUNTAG,
pure_write_error,
push_handler,
SETJMP_NAME,
record_unwind_protect_excursion,
helper_unbind_n,
helper_save_restriction,
record_unwind_current_buffer,
set_internal,
helper_unwind_protect,
specbind,
maybe_gc,
maybe_quit };
static char * ATTRIBUTE_FORMAT_PRINTF (1, 2)
format_string (const char *format, ...)
{
static char scratch_area[512];
va_list va;
va_start (va, format);
int res = vsnprintf (scratch_area, sizeof (scratch_area), format, va);
if (res >= sizeof (scratch_area))
{
scratch_area[sizeof (scratch_area) - 4] = '.';
scratch_area[sizeof (scratch_area) - 3] = '.';
scratch_area[sizeof (scratch_area) - 2] = '.';
}
va_end (va);
return scratch_area;
}
static Lisp_Object
comp_hash_string (Lisp_Object string)
{
Lisp_Object digest = make_uninit_string (MD5_DIGEST_SIZE * 2);
md5_buffer (SSDATA (string), SCHARS (string), SSDATA (digest));
hexbuf_digest (SSDATA (digest), SDATA (digest), MD5_DIGEST_SIZE);
return Fsubstring (digest, Qnil, make_fixnum (HASH_LENGTH));
}
static Lisp_Object
comp_hash_source_file (Lisp_Object filename)
{
/* Can't use Finsert_file_contents + Fbuffer_hash as this is called
by Fcomp_el_to_eln_filename too early during bootstrap. */
bool is_gz = suffix_p (filename, ".gz");
FILE *f = emacs_fopen (SSDATA (filename), is_gz ? "rb" : "r");
if (!f)
report_file_error ("Opening source file", filename);
Lisp_Object digest = make_uninit_string (MD5_DIGEST_SIZE * 2);
int res = is_gz
? md5_gz_stream (f, SSDATA (digest))
: md5_stream (f, SSDATA (digest));
fclose (f);
if (res)
xsignal2 (Qfile_notify_error, build_string ("hashing failed"), filename);
hexbuf_digest (SSDATA (digest), SSDATA (digest), MD5_DIGEST_SIZE);
return Fsubstring (digest, Qnil, make_fixnum (HASH_LENGTH));
}
/* Produce a key hashing Vcomp_subr_list. */
void
hash_native_abi (void)
{
/* Check runs once. */
eassert (NILP (Vcomp_abi_hash));
Vcomp_abi_hash =
comp_hash_string (
concat3 (build_string (ABI_VERSION),
concat2 (Vemacs_version, Vsystem_configuration),
Fmapconcat (intern_c_string ("subr-name"),
Vcomp_subr_list, build_string (""))));
Vcomp_native_version_dir =
concat3 (Vemacs_version, build_string ("-"), Vcomp_abi_hash);
}
static void
freloc_check_fill (void)
{
if (freloc.size)
return;
eassert (!NILP (Vcomp_subr_list));
if (ARRAYELTS (helper_link_table) > F_RELOC_MAX_SIZE)
goto overflow;
memcpy (freloc.link_table, helper_link_table, sizeof (helper_link_table));
freloc.size = ARRAYELTS (helper_link_table);
Lisp_Object subr_l = Vcomp_subr_list;
FOR_EACH_TAIL (subr_l)
{
if (freloc.size == F_RELOC_MAX_SIZE)
goto overflow;
struct Lisp_Subr *subr = XSUBR (XCAR (subr_l));
freloc.link_table[freloc.size] = subr->function.a0;
freloc.size++;
}
return;
overflow:
fatal ("Overflowing function relocation table, increase F_RELOC_MAX_SIZE");
}
static void
bcall0 (Lisp_Object f)
{
Ffuncall (1, &f);
}
static gcc_jit_block *
retrive_block (Lisp_Object block_name)
{
Lisp_Object value = Fgethash (block_name, comp.func_blocks_h, Qnil);
if (NILP (value))
xsignal2 (Qnative_ice, build_string ("missing basic block"), block_name);
return (gcc_jit_block *) xmint_pointer (value);
}
static void
declare_block (Lisp_Object block_name)
{
char *name_str = SSDATA (SYMBOL_NAME (block_name));
gcc_jit_block *block = gcc_jit_function_new_block (comp.func, name_str);
Lisp_Object value = make_mint_ptr (block);
if (!NILP (Fgethash (block_name, comp.func_blocks_h, Qnil)))
xsignal1 (Qnative_ice, build_string ("double basic block declaration"));
Fputhash (block_name, value, comp.func_blocks_h);
}
static gcc_jit_lvalue *
emit_mvar_lval (Lisp_Object mvar)
{
Lisp_Object mvar_slot = CALL1I (comp-mvar-slot, mvar);
if (EQ (mvar_slot, Qscratch))
{
if (!comp.scratch)
comp.scratch = gcc_jit_function_new_local (comp.func,
NULL,
comp.lisp_obj_type,
"scratch");
return comp.scratch;
}
EMACS_INT slot_n = XFIXNUM (mvar_slot);
eassert (slot_n < comp.frame_size);
return comp.frame[slot_n];
}
static void
register_emitter (Lisp_Object key, void *func)
{
Lisp_Object value = make_mint_ptr (func);
Fputhash (key, value, comp.emitter_dispatcher);
}
static imm_reloc_t
obj_to_reloc (Lisp_Object obj)
{
imm_reloc_t reloc;
Lisp_Object idx;
idx = Fgethash (obj, comp.d_default_idx, Qnil);
if (!NILP (idx)) {
reloc.array = comp.data_relocs;
goto found;
}
idx = Fgethash (obj, comp.d_impure_idx, Qnil);
if (!NILP (idx))
{
reloc.array = comp.data_relocs_impure;
goto found;
}
idx = Fgethash (obj, comp.d_ephemeral_idx, Qnil);
if (!NILP (idx))
{
reloc.array = comp.data_relocs_ephemeral;
goto found;
}
xsignal1 (Qnative_ice,
build_string ("cant't find data in relocation containers"));
assume (false);
found:
eassert (XFIXNUM (idx) < reloc.array.len);
if (!FIXNUMP (idx))
xsignal1 (Qnative_ice,
build_string ("inconsistent data relocation container"));
reloc.idx = gcc_jit_context_new_rvalue_from_int (comp.ctxt,
comp.ptrdiff_type,
XFIXNUM (idx));
return reloc;
}
static void
emit_comment (const char *str)
{
if (comp.debug)
gcc_jit_block_add_comment (comp.block,
NULL,
str);
}
/*
Declare an imported function.
When nargs is MANY (ptrdiff_t nargs, Lisp_Object *args) signature is assumed.
When types is NULL args are assumed to be all Lisp_Objects.
*/
static gcc_jit_field *
declare_imported_func (Lisp_Object subr_sym, gcc_jit_type *ret_type,
int nargs, gcc_jit_type **types)
{
USE_SAFE_ALLOCA;
/* Don't want to declare the same function two times. */
if (!NILP (Fgethash (subr_sym, comp.imported_funcs_h, Qnil)))
xsignal2 (Qnative_ice,
build_string ("unexpected double function declaration"),
subr_sym);
if (nargs == MANY)
{
nargs = 2;
types = SAFE_ALLOCA (nargs * sizeof (* types));
types[0] = comp.ptrdiff_type;
types[1] = comp.lisp_obj_ptr_type;
}
else if (nargs == UNEVALLED)
{
nargs = 1;
types = SAFE_ALLOCA (nargs * sizeof (* types));
types[0] = comp.lisp_obj_type;
}
else if (!types)
{
types = SAFE_ALLOCA (nargs * sizeof (* types));
for (ptrdiff_t i = 0; i < nargs; i++)
types[i] = comp.lisp_obj_type;
}
/* String containing the function ptr name. */
Lisp_Object f_ptr_name =
CALLN (Ffuncall, intern_c_string ("comp-c-func-name"),
subr_sym, make_string ("R", 1));
gcc_jit_type *f_ptr_type =
gcc_jit_type_get_const (
gcc_jit_context_new_function_ptr_type (comp.ctxt,
NULL,
ret_type,
nargs,
types,
0));
gcc_jit_field *field =
gcc_jit_context_new_field (comp.ctxt,
NULL,
f_ptr_type,
SSDATA (f_ptr_name));
Fputhash (subr_sym, make_mint_ptr (field), comp.imported_funcs_h);
SAFE_FREE ();
return field;
}
/* Emit calls fetching from existing declarations. */
static gcc_jit_rvalue *
emit_call (Lisp_Object func, gcc_jit_type *ret_type, ptrdiff_t nargs,
gcc_jit_rvalue **args, bool direct)
{
Lisp_Object gcc_func =
Fgethash (func,
direct ? comp.exported_funcs_h : comp.imported_funcs_h,
Qnil);
if (NILP (gcc_func))
xsignal2 (Qnative_ice,
build_string ("missing function declaration"),
func);
if (direct)
{
emit_comment (format_string ("direct call to: %s",
SSDATA (func)));
return gcc_jit_context_new_call (comp.ctxt,
NULL,
xmint_pointer (gcc_func),
nargs,
args);
}
else
{
/* Inline functions so far don't have a local variable for
function reloc table so we fall back to the global one. Even
if this is not aesthetic calling into C from open-code is
always a fallback and therefore not be performance critical.
To fix this could think do the inline our-self without
relying on GCC. */
gcc_jit_lvalue *f_ptr =
gcc_jit_rvalue_dereference_field (
gcc_jit_lvalue_as_rvalue (comp.func_relocs_local
? comp.func_relocs_local
: comp.func_relocs),
NULL,
(gcc_jit_field *) xmint_pointer (gcc_func));
if (!f_ptr)
xsignal2 (Qnative_ice,
build_string ("missing function relocation"),
func);
emit_comment (format_string ("calling subr: %s",
SSDATA (SYMBOL_NAME (func))));
return gcc_jit_context_new_call_through_ptr (comp.ctxt,
NULL,
gcc_jit_lvalue_as_rvalue (f_ptr),
nargs,
args);
}
}
static gcc_jit_rvalue *
emit_call_ref (Lisp_Object func, ptrdiff_t nargs,
gcc_jit_lvalue *base_arg, bool direct)
{
gcc_jit_rvalue *args[] =
{ gcc_jit_context_new_rvalue_from_int (comp.ctxt,
comp.ptrdiff_type,
nargs),
gcc_jit_lvalue_get_address (base_arg, NULL) };
return emit_call (func, comp.lisp_obj_type, 2, args, direct);
}
/* Close current basic block emitting a conditional. */
static void
emit_cond_jump (gcc_jit_rvalue *test,
gcc_jit_block *then_target, gcc_jit_block *else_target)
{
if (gcc_jit_rvalue_get_type (test) == comp.bool_type)
gcc_jit_block_end_with_conditional (comp.block,
NULL,
test,
then_target,
else_target);
else
/* In case test is not bool we do a logical negation to obtain a bool as
result. */
gcc_jit_block_end_with_conditional (
comp.block,