-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtl-c.scm
1963 lines (1727 loc) · 59.8 KB
/
rtl-c.scm
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
; RTL->C translation support.
; Copyright (C) 2000, 2005, 2009, 2010, 2014 Red Hat, Inc.
; This file is part of CGEN.
; See file COPYING.CGEN for details.
; Generating C from RTL
; ---------------------
; The main way to generate C code from an RTL expression is:
;
; (rtl-c-parsed mode isa-name-list nil '(func mode ...))
;
; E.g.
; (rtl-c-parsed SI (all) nil '(add () SI (const () SI 1) (const () SI 2)))
; -->
; "ADDSI (1, 2)"
;
; The expression is in source form and must be already canonicalized (with
; rtx-canonicalize). There is also rtl-c for the occasions where the rtl
; isn't already canonicalized.
;
; The `set' rtx needs to be handled a little carefully.
; Both the dest and src are processed first, and then code to perform the
; assignment is computed. However, the dest may require more than a simple
; C assignment. Therefore set dests are converted to the specified object
; (e.g. a hardware operand) and then a message is sent to this object to
; perform the actual code generation.
;
; All interesting operands (e.g. regs, mem) are `operand' objects.
; The following messages must be supported by operand objects.
; - get-mode - return mode of operand
; - cxmake-get - return <c-expr> object containing operand's value
; - gen-set-quiet - return string of C code to set operand's value (no tracing)
; - gen-set-trace - return string of C code to set operand's value
;
; Instruction fields are refered to by name.
; Instruction ifields must have these methods:
; - get-mode
; - cxmake-get
;
; Conventions used in this file:
; - see rtl.scm
; The <c-expr> object.
; This is a fully translated expression (i.e. C code).
(define <c-expr>
(class-make '<c-expr> nil
'(
; The mode of C-CODE.
mode
; The translated C code.
c-code
; The source expression, for debugging.
expr
; Attributes of the expression.
atlist
; List of temporaries required to compute the expression.
; ??? wip. These would be combined as the expression is
; built up. Then in sets and other statements, the temporaries
; would be declared.
;(tmps . nil)
)
nil)
)
(method-make!
<c-expr> 'make!
(lambda (self mode c-code atlist)
; FIXME: Extend COS to allow specifying member predicates.
(assert (mode? mode))
(assert (string? c-code))
;(assert (atlist? atlist)) ; FIXME: What should this be?
(elm-set! self 'mode mode)
(elm-set! self 'c-code c-code)
(elm-set! self 'atlist atlist)
self)
)
; Accessor fns
(define cx:mode (elm-make-getter <c-expr> 'mode))
(define cx:c-code (elm-make-getter <c-expr> 'c-code))
(define cx:expr (elm-make-getter <c-expr> 'expr))
(define cx:atlist (elm-make-getter <c-expr> 'atlist))
;(define cx:tmps (elm-make-getter <c-expr> 'tmps))
; Any object with attributes requires the get-atlist method.
(method-make! <c-expr> 'get-atlist (lambda (self) (elm-get self 'atlist)))
; Respond to 'get-mode messages.
(method-make! <c-expr> 'get-mode (lambda (self) (elm-get self 'mode)))
; Respond to 'get-name messages for rtx-dump.
(method-make!
<c-expr> 'get-name
(lambda (self)
(string-append "(" (obj:str-name (elm-get self 'mode)) ") "
(cx:c self)))
)
; Return C code to perform an assignment.
; NEWVAL is a <c-expr> object of the value to be assigned to SELF.
(method-make! <c-expr> 'gen-set-quiet
(lambda (self estate mode indx selector newval)
(string-append " " (cx:c self) " = " (cx:c newval) ";\n"))
)
(method-make! <c-expr> 'gen-set-trace
(lambda (self estate mode indx selector newval)
(string-append " " (cx:c self) " = " (cx:c newval) ";\n"))
)
; Return the C code of CX.
; ??? This used to handle lazy evaluation of the expression.
; Maybe it will again, so it's left in, as a cover fn to cx:c-code.
(define (cx:c cx)
(cx:c-code cx)
)
; Main routine to create a <c-expr> node object.
; MODE is either the mode's symbol (e.g. 'QI) or a <mode> object.
; CODE is a string of C code.
(define (cx:make mode code)
(make <c-expr> (mode-maybe-lookup mode) code nil)
)
; Make copy of CX in new mode MODE.
; MODE must be a <mode> object.
(define (cx-new-mode mode cx)
(make <c-expr> mode (cx:c cx) (cx:atlist cx))
)
; Same as cx:make except with attributes.
(define (cx:make-with-atlist mode code atlist)
(make <c-expr> (mode-maybe-lookup mode) code atlist)
)
; Return a boolean indicated if X is a <c-expr> object.
(define (c-expr? x) (class-instance? <c-expr> x))
; RTX environment support.
(method-make!
<rtx-temp> 'cxmake-get
(lambda (self estate mode indx selector)
(cx:make mode (rtx-temp-value self)))
)
(method-make!
<rtx-temp> 'gen-set-quiet
(lambda (self estate mode indx selector src)
(string-append " " (rtx-temp-value self) " = " (cx:c src) ";\n"))
)
(method-make!
<rtx-temp> 'gen-set-trace
(lambda (self estate mode indx selector src)
(string-append " " (rtx-temp-value self) " = " (cx:c src) ";\n"))
)
(define (gen-temp-defs estate env)
(string-map (lambda (temp)
(let ((temp-obj (cdr temp)))
(string-append " " (mode:c-type (rtx-temp-mode temp-obj))
" " (rtx-temp-value temp-obj) ";\n")))
env)
)
; Top level routines to handle rtl->c translation.
; rtl->c configuration parameters
; #t -> emit calls to rtl cover fns, otherwise emit plain C where possible.
(define /rtl-c-rtl-cover-fns? #f)
; Called before emitting code to configure the generator.
; ??? I think this can go away now (since cover-fn specification is also
; done at each call to rtl-c).
(define (rtl-c-config! . args)
(set! /rtl-c-rtl-cover-fns? #f)
(let loop ((args args))
(if (null? args)
#f ; done
(begin
(case (car args)
((#:rtl-cover-fns?)
(set! /rtl-c-rtl-cover-fns? (cadr args)))
(else (error "rtl-c-config: unknown option:" (car args))))
(loop (cddr args)))))
*UNSPECIFIED*
)
; Subclass of <eval-state> to record additional things needed for rtl->c.
(define <rtl-c-eval-state>
(class-make '<rtl-c-eval-state> '(<eval-state>)
'(
; #t -> emit calls to rtl cover fns.
(rtl-cover-fns? . #f)
; name of output language, "c" or "c++"
(output-language . "c")
; #t if generating code for a macro.
; Each newline is then preceeded with '\\'.
(macro? . #f)
; Boolean indicating if evaluation is for an instruction.
; It's not always possible to look at OWNER, e.g. when we're
; processing semantic fragments.
(for-insn? . #f)
; #f -> reference ifield values using FLD macro.
; #t -> use C variables.
; ??? This is only needed to get correct ifield references
; in opcodes, decoder, and semantics. Maybe a better way to
; go would be to specify the caller's name so there'd be just
; one of these, rather than an increasing number. However,
; for now either way is the same.
; An alternative is to specify a callback to try first.
(ifield-var? . #f)
)
nil)
)
; FIXME: involves upcasting.
(define-getters <rtl-c-eval-state> estate
(rtl-cover-fns? output-language macro? for-insn? ifield-var?)
)
; Return booleans indicating if output language is C/C++.
(define (estate-output-language-c? estate)
(string=? (estate-output-language estate) "c")
)
(define (estate-output-language-c++? estate)
(string=? (estate-output-language estate) "c++")
)
(method-make!
<rtl-c-eval-state> 'vmake!
(lambda (self args)
; Initialize parent class first.
(let loop ((args (send-next self '<rtl-c-eval-state> 'vmake! args))
(unrecognized nil))
(if (null? args)
(reverse! unrecognized) ; ??? Could invoke method to initialize here.
(begin
(case (car args)
((#:rtl-cover-fns?)
(elm-set! self 'rtl-cover-fns? (cadr args)))
((#:output-language)
(elm-set! self 'output-language (cadr args)))
((#:macro?)
(elm-set! self 'macro? (cadr args)))
((#:for-insn?)
(elm-set! self 'for-insn? (cadr args)))
((#:ifield-var?)
(elm-set! self 'ifield-var? (cadr args)))
(else
; Build in reverse order, as we reverse it back when we're done.
(set! unrecognized
(cons (cadr args) (cons (car args) unrecognized)))))
(loop (cddr args) unrecognized)))))
)
;; Build an estate for use in generating C.
;; OVERRIDES is a #:keyword/value list of parameters to apply last.
(define (estate-make-for-rtl-c overrides)
(apply vmake
(append!
(list
<rtl-c-eval-state>
#:expr-fn (lambda (rtx-obj expr mode estate)
(rtl-c-generator rtx-obj))
#:rtl-cover-fns? /rtl-c-rtl-cover-fns?)
overrides))
)
; Translate RTL expression EXPR to C.
; ESTATE is the current rtx evaluation state.
; MODE is a <mode> object.
(define (rtl-c-with-estate estate mode expr)
(cx:c (rtl-c-get estate mode (rtx-eval-with-estate expr mode estate)))
)
; Translate parsed RTL expression X to a string of C code.
; EXPR must have already been fed through rtx-canonicalize.
; MODE is the desired mode of the value or DFLT for "natural mode".
; MODE is a <mode> object.
; OVERRIDES is a #:keyword/value list of arguments to build the eval state
; with.
(define (rtl-c-parsed mode expr . overrides)
;; ??? If we're passed insn-compiled-semantics the output of xops is
;; confusing. Fix by subclassing <operand> -> <xoperand>, and
;; have <xoperand> provide original source expr.
(let ((estate (estate-make-for-rtl-c (cons #:outer-expr
(cons expr overrides)))))
(rtl-c-with-estate estate mode expr))
)
; Same as rtl-c-parsed but EXPR is unparsed.
; ISA-NAME-LIST is the list of ISA(s) in which to evaluate EXPR.
; EXTRA-VARS-ALIST is an association list of extra (symbol <mode> value)
; elements to be used during value lookup.
; MODE is a <mode> object.
(define (rtl-c mode isa-name-list extra-vars-alist expr . overrides)
(let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
isa-name-list extra-vars-alist expr))
(estate (estate-make-for-rtl-c (cons #:outer-expr
(cons canonical-rtl overrides)))))
(rtl-c-with-estate estate mode canonical-rtl))
)
; Same as rtl-c-with-estate except return a <c-expr> object.
; MODE is a <mode> object.
(define (rtl-c-expr-with-estate estate mode expr)
(rtl-c-get estate mode (rtx-eval-with-estate expr mode estate))
)
; Same as rtl-c-parsed except return a <c-expr> object.
; MODE is a <mode> object.
(define (rtl-c-expr-parsed mode expr . overrides)
;; ??? If we're passed insn-compiled-semantics the output of xops is
;; confusing. Fix by subclassing <operand> -> <xoperand>, and
;; have <xoperand> provide original source expr.
(let ((estate (estate-make-for-rtl-c (cons #:outer-expr
(cons expr overrides)))))
(rtl-c-expr-with-estate estate mode expr))
)
; Same as rtl-c-expr-parsed but EXPR is unparsed.
; MODE is a <mode> object.
(define (rtl-c-expr mode isa-name-list extra-vars-alist expr . overrides)
(let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
isa-name-list extra-vars-alist expr))
(estate (estate-make-for-rtl-c (cons #:outer-expr
(cons canonical-rtl overrides)))))
(rtl-c-expr-with-estate estate mode canonical-rtl))
)
; C++ versions of rtl-c routines.
; Build an estate for use in generating C++.
; OVERRIDES is a #:keyword/value list of parameters to apply last.
(define (estate-make-for-rtl-c++ overrides)
(estate-make-for-rtl-c (cons #:output-language (cons "c++" overrides)))
)
; Translate parsed RTL expression X to a string of C++ code.
; EXPR must have already been fed through rtx-canonicalize.
; MODE is the desired mode of the value or DFLT for "natural mode".
; MODE is a <mode> object.
; OVERRIDES is a #:keyword/value list of arguments to build the eval state
; with.
(define (rtl-c++-parsed mode expr . overrides)
;; ??? If we're passed insn-compiled-semantics the output of xops is
;; confusing. Fix by subclassing <operand> -> <xoperand>, and
;; have <xoperand> provide original source expr.
(let ((estate (estate-make-for-rtl-c++ (cons #:outer-expr
(cons expr overrides)))))
(rtl-c-with-estate estate mode expr))
)
; Same as rtl-c++-parsed but EXPR is unparsed.
; MODE is a <mode> object.
(define (rtl-c++ mode isa-name-list extra-vars-alist expr . overrides)
(let* ((canonical-rtl (rtx-canonicalize #f (obj:name mode)
isa-name-list extra-vars-alist expr))
(estate (estate-make-for-rtl-c++ (cons #:outer-expr
(cons canonical-rtl overrides)))))
(rtl-c-with-estate estate mode canonical-rtl))
)
; Top level routines for getting/setting values.
; Return a <c-expr> node to get the value of SRC in mode MODE.
; ESTATE is the current rtl evaluation state.
; MODE is a <mode> object.
; SRC is one of:
; - <c-expr> node
; - rtl expression (e.g. '(add WI dr sr))
; - sequence's local variable name
; - sequence's local variable object
; - operand name
; - operand object
; - an integer
; - a string of C code
; FIXME: Reduce acceptable values of SRC.
; The result has mode MODE, unless MODE is the "default mode indicator"
; (DFLT) in which case the mode of the result is derived from SRC.
;
; ??? mode compatibility checks are wip
(define (/rtl-c-get estate mode src)
(let ((mode mode)) ;;(mode:lookup mode)))
(cond ((c-expr? src)
(cond ((or (mode:eq? 'VOID mode)
(mode:eq? 'DFLT mode)
(mode:eq? (cx:mode src) mode))
src)
((rtx-mode-compatible? mode (cx:mode src))
(cx-new-mode mode src))
(else
(estate-error
estate
(string-append "incompatible mode: "
"(" (obj:str-name (cx:mode src)) " vs "
(obj:str-name mode) ") in "
"\"" (cx:c src) "\"")
(obj:name mode)))))
; The recursive call to /rtl-c-get is in case the result of rtx-eval
; is a hardware object, rtx-func object, or another rtl expression.
; FIXME: simplify
((rtx? src)
(let ((evald-src (rtx-eval-with-estate src mode estate)))
; There must have been some change, otherwise we'll loop forever.
(assert (not (eq? src evald-src)))
(/rtl-c-get estate mode evald-src)))
;; FIXME: Can we ever get a symbol here?
((or (and (symbol? src) (current-op-lookup src))
(operand? src))
(begin
(if (symbol? src)
(set! src (current-op-lookup src)))
(cond ((mode:eq? 'DFLT mode)
; FIXME: Can we get called with 'DFLT anymore?
; FIXME: If we fetch the mode here, operands can assume
; they never get called with "default mode".
(send src 'cxmake-get estate mode #f #f))
((rtx-mode-compatible? mode (op:mode src))
(let ((mode (op:mode src))) ;; FIXME: (rtx-sem-mode mode)))
(send src 'cxmake-get estate mode #f #f)))
(else
;; FIXME: canonicalization should have already caught this
(estate-error
estate
(string-append "operand " (obj:str-name src)
" referenced in incompatible mode")
(obj:name mode))))))
;; FIXME: Can we ever get a symbol here?
((or (and (symbol? src) (rtx-temp-lookup (estate-env-stack estate) src))
(rtx-temp? src))
(begin
(if (symbol? src)
(set! src (rtx-temp-lookup (estate-env-stack estate) src)))
(cond ((mode:eq? 'DFLT mode)
(send src 'cxmake-get estate (rtx-temp-mode src) #f #f))
((rtx-mode-compatible? mode (rtx-temp-mode src))
(let ((mode (rtx-temp-mode src))) ;; FIXME: (rtx-sem-mode mode)))
(send src 'cxmake-get estate mode #f #f)))
(else
;; FIXME: canonicalization should have already caught this
(estate-error
estate
(string-append "sequence temp " (rtx-temp-name src)
" referenced in incompatible mode")
(obj:name mode))))))
((integer? src)
; Default mode of integer argument is INT.
(if (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))
(cx:make INT (number->string src))
(cx:make mode (number->string src))))
((string? src)
; Default mode of string argument is INT.
(if (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))
(cx:make INT src)
(cx:make mode src)))
(else (estate-error estate "/rtl-c-get: invalid argument" src))))
)
;; MODE is either a <mode> object or the mode name.
(define (rtl-c-get estate mode src)
(let ((mode (mode-maybe-lookup mode)))
(logit 4 (spaces (estate-depth estate))
"(rtl-c-get " (mode-real-name mode) " " (rtx-strdump src) ")\n")
(let ((result (/rtl-c-get estate mode src)))
(logit 4 (spaces (estate-depth estate))
"(rtl-c-get " (mode-real-name mode) " " (rtx-strdump src) ") => "
(cx:c result) "\n")
result))
)
; Return a <c-expr> object to set the value of DEST to SRC.
; ESTATE is the current rtl evaluation state.
; MODE is the mode of DEST or DFLT which means fetch the real mode from DEST.
; MODE is either a <mode> object or the mode name.
; DEST is one of:
; - <c-expr> node
; - rtl expression (e.g. '(mem QI dr))
; SRC is an RTX expression. It is important that we evaluate it, instead of
; our caller, because only we know the mode of DEST (which we need to pass
; when evaluating SRC if MODE is DFLT). ??? Can no longer get DFLT, but
; it feels right to continue to evaluate SRC here.
; The mode of the result is always VOID (void).
;
; ??? One possible optimization is to pass the address of the result
; to the computation of SRC. Seems dodgey though.
(define (rtl-c-set-quiet estate mode dest src)
;(display (list 'rtl-c-set-quiet mode dest src)) (newline)
(let* ((mode (mode-maybe-lookup mode))
(xdest (cond ((c-expr? dest)
dest)
((rtx? dest)
(rtx-eval-with-estate dest mode estate))
(else
(estate-error estate
"rtl-c-set-quiet: invalid dest"
dest)))))
(assert (mode? mode))
(if (not (object? xdest))
(estate-error estate "rtl-c-set-quiet: invalid dest" dest))
(cx:make VOID (send xdest 'gen-set-quiet
estate mode #f #f
(rtl-c-get estate mode src))))
)
; Same as rtl-c-set-quiet except also print TRACE_RESULT message.
; MODE is either a <mode> object or the mode name.
; ??? One possible change is to defer the (rtl-c-get src) call to dest's
; set handler. Such sources would be marked accordingly and rtl-c-get
; would recognize them. This would allow, for example, passing the address
; of the result to the computation.
(define (rtl-c-set-trace estate mode dest src)
;(display (list 'rtl-c-set-trace mode dest src)) (newline)
(let* ((mode (mode-maybe-lookup mode))
(xdest (cond ((c-expr? dest)
dest)
((rtx? dest)
(rtx-eval-with-estate dest mode estate))
(else
(estate-error estate
"rtl-c-set-trace: invalid dest"
dest)))))
(assert (mode? mode))
(if (not (object? xdest))
(estate-error estate "rtl-c-set-trace: invalid dest" dest))
(cx:make VOID (send xdest 'gen-set-trace
estate mode #f #f
(rtl-c-get estate mode src))))
)
; Emit C code for each rtx function.
; Table mapping rtx function to C generator.
(define /rtl-c-gen-table #f)
; Return the C generator for <rtx-func> F.
(define (rtl-c-generator f)
(vector-ref /rtl-c-gen-table (rtx-num f))
)
; Support for explicit C/C++ code.
; MODE is the mode name.
; ??? Actually, "support for explicit foreign language code".
; s-c-call needs a better name but "unspec" seems like obfuscation.
; ??? Need to distinguish owner of call (cpu, ???).
(define (s-c-call estate mode name . args)
(cx:make mode
(string-append
(if (estate-output-language-c++? estate)
(string-append "current_cpu->" name " (")
; FIXME: Prepend @cpu@_ to name here, and delete @cpu@_ from
; description file.
(string-append name " (current_cpu"))
(let ((c-args
(string-map (lambda (arg)
(string-append
", "
(cx:c (rtl-c-get estate DFLT arg))))
args)))
(if (estate-output-language-c++? estate)
(string-drop 2 c-args)
c-args))
; If the mode is VOID, this is a statement.
; Otherwise it's an expression.
; ??? Bad assumption! VOID expressions may be used
; within sequences without local vars, which are translated
; to comma-expressions.
(if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
(mode:eq? 'VOID mode))
");\n"
")")
))
)
; Same as c-call except there is no particular owner of the call.
; In general this means making a call to a non-member function,
; whereas c-call makes calls to member functions (in C++ parlance).
; MODE is the mode name.
(define (s-c-raw-call estate mode name . args)
(cx:make mode
(string-append
name " ("
(string-drop 2
(string-map (lambda (elm)
(string-append
", " (cx:c (rtl-c-get estate DFLT elm))))
args))
; If the mode is VOID, this is a statement.
; Otherwise it's an expression.
; ??? Bad assumption! VOID expressions may be used
; within sequences without local vars, which are translated
; to comma-expressions.
(if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
(mode:eq? 'VOID mode))
");\n"
")")
))
)
; Standard arithmetic operations.
; Return a boolean indicating if a cover function/macro should be emitted
; to perform an operation.
; C-OP is a string containing the C operation or #f if there is none.
; MODE is the mode of the operation.
(define (/rtx-use-sem-fn? estate c-op mode)
; If no C operation has been provided, use a macro, or
; if this is the simulator and MODE is not a host mode, use a macro.
; (or (not c-op)
; (and (estate-rtl-cover-fns? estate)
; (not (mode:host? mode))))
; FIXME: The current definition is a temporary hack while host/target-ness
; of INT/UINT is unresolved.
(and (not (obj-has-attr? mode 'FORCE-C))
(or (not c-op)
(and (estate-rtl-cover-fns? estate)
;; NOTE: We can't check (insn? (estate-owner estate)) here.
;; It's not necessarily present for semantic fragments.
(or (estate-for-insn? estate)
(not (mode:host? mode))))))
)
; One operand referenced, result is in same mode.
; MODE is the mode name.
(define (s-unop estate name c-op mode src)
(let* ((val (rtl-c-get estate mode src))
; Refetch mode in case it was DFLT and ensure unsigned->signed.
(mode (mode:lookup mode)) ;;(cx:mode val)) ;; FIXME: can't get DFLT anymore
(sem-mode (rtx-sem-mode mode)))
; FIXME: Argument checking.
(if (/rtx-use-sem-fn? estate c-op mode)
(if (mode-float? mode)
(cx:make sem-mode
(string-append "CGEN_CPU_FPU (current_cpu)->ops->"
(string-downcase name)
(string-downcase (obj:str-name sem-mode))
" (CGEN_CPU_FPU (current_cpu), "
(cx:c val) ")"))
(cx:make sem-mode
(string-append name (obj:str-name sem-mode)
" (" (cx:c val) ")")))
(cx:make mode ; not sem-mode on purpose
(string-append "(" c-op " ("
(cx:c val) "))"))))
)
; Two operands referenced in the same mode producing a result in the same mode.
; MODE is the mode name.
;
; ??? Will eventually want to handle floating point modes specially. Since
; bigger modes may get clumsily passed (there is no pass by reference in C) and
; since we want to eventually handle lazy transformation, FP values could be
; passed by reference. This is easy in C++. C requires more work and is
; defered until it's warranted.
; Implementing this should probably be via a new cxmake-get-ref method,
; rather then complicating cxmake-get. Ditto for rtl-c-get-ref/rtl-c-get.
(define (s-binop estate name c-op mode src1 src2)
;(display (list "binop " name ", mode " mode)) (newline)
(let* ((val1 (rtl-c-get estate mode src1))
; Refetch mode in case it was DFLT and ensure unsigned->signed.
(mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
(sem-mode (rtx-sem-mode mode))
(val2 (rtl-c-get estate mode src2)))
; FIXME: Argument checking.
(if (/rtx-use-sem-fn? estate c-op mode)
(if (mode-float? mode)
(cx:make sem-mode
(string-append "CGEN_CPU_FPU (current_cpu)->ops->"
(string-downcase name)
(string-downcase (obj:str-name sem-mode))
" (CGEN_CPU_FPU (current_cpu), "
(cx:c val1) ", "
(cx:c val2) ")"))
(cx:make sem-mode
(string-append name (obj:str-name sem-mode)
" (" (cx:c val1) ", "
(cx:c val2) ")")))
(cx:make mode ; not sem-mode on purpose
(string-append "(("
(cx:c val1)
") " c-op " ("
(cx:c val2)
"))"))))
)
; Same as s-binop except there's a third argument which is always one bit.
; MODE is the mode name.
(define (s-binop-with-bit estate name mode src1 src2 src3)
(let* ((val1 (rtl-c-get estate mode src1))
; Refetch mode in case it was DFLT and ensure unsigned->signed.
(mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
(sem-mode (rtx-sem-mode mode))
(val2 (rtl-c-get estate mode src2))
(val3 (rtl-c-get estate 'BI src3)))
; FIXME: Argument checking.
(cx:make mode
(string-append name (obj:str-name sem-mode)
" ("
(cx:c val1) ", "
(cx:c val2) ", "
(cx:c val3)
")")))
)
; Shift operations are slightly different than binary operations:
; the mode of src2 is any integral mode.
; MODE is the mode name.
; ??? Note that some cpus have a signed shift left that is semantically
; different from a logical one. May need to create `sla' some day. Later.
(define (s-shop estate name c-op mode src1 src2)
;(display (list "shop " name ", mode " mode)) (newline)
(let* ((val1 (rtl-c-get estate mode src1))
; Refetch mode in case it was DFLT and ensure unsigned->signed
; [sign of operation is determined from operation name, not mode].
(mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
(sem-mode (rtx-sem-mode mode))
(val2 (rtl-c-get estate mode src2)))
; FIXME: Argument checking.
(if (/rtx-use-sem-fn? estate c-op mode)
(cx:make sem-mode
(string-append name (obj:str-name sem-mode)
" (" (cx:c val1) ", "
(cx:c val2) ")"))
(cx:make mode ; not sem-mode on purpose
(string-append "("
;; Ensure correct sign of shift.
(cond ((equal? name "SRL")
(string-append
"("
(cond ((mode-unsigned? mode) (mode:c-type mode))
((mode:eq? mode 'INT) (mode:c-type UINT))
(else (mode:c-type (mode-find (mode:bits mode) 'UINT))))
") "))
((equal? name "SRA")
(string-append
"("
(cond ((mode-signed? mode) (mode:c-type mode))
((mode:eq? mode 'UINT) (mode:c-type INT))
(else (mode:c-type (mode-find (mode:bits mode) 'INT))))
") "))
;; May wish to make this unsigned if not
;; already. Later.
(else ""))
"(" (cx:c val1) ") "
c-op
" (" (cx:c val2) "))"))))
)
; Process andif, orif.
; SRC1 and SRC2 have any arithmetic mode.
; MODE is the mode name.
; The result has mode BI.
; ??? May want to use INT as BI may introduce some slowness
; in the generated code.
(define (s-boolifop estate name c-op src1 src2)
(let* ((val1 (rtl-c-get estate DFLT src1))
(val2 (rtl-c-get estate DFLT src2)))
; FIXME: Argument checking.
; If this is the simulator and MODE is not a host mode, use a macro.
; ??? MODE here being the mode of SRC1. Maybe later.
(if (estate-rtl-cover-fns? estate)
(cx:make (mode:lookup 'BI)
(string-append name ; "BI", leave off mode, no need for it
" (" (cx:c val1) ", "
(cx:c val2) ")"))
(cx:make (mode:lookup 'BI)
(string-append "(("
(cx:c val1)
") " c-op " ("
(cx:c val2)
"))"))))
)
;; Process fp predicates, e.g. nan, qnan, snan.
;; SRC-MODE is the mode name of SRC.
;; The result has mode BI.
(define (s-float-predop estate name src-mode src)
(let* ((val (rtl-c-get estate src-mode src))
(mode (cx:mode val))
(sem-mode (rtx-sem-mode mode)))
;; FIXME: Argument checking.
(if (not (mode-float? mode))
(estate-error estate "non floating-point mode" src-mode))
(cx:make (mode:lookup 'BI)
(string-append "CGEN_CPU_FPU (current_cpu)->ops->"
(string-downcase name)
(string-downcase (obj:str-name sem-mode))
" (CGEN_CPU_FPU (current_cpu), "
(cx:c val) ")")))
)
;; Integer mode conversions.
;; MODE is the mode name.
(define (s-int-convop estate name mode s1)
;; Get S1 in its normal mode, then convert.
(let ((s (rtl-c-get estate DFLT s1))
(mode (mode:lookup mode)))
(if (and (not (estate-rtl-cover-fns? estate))
(mode:host? (cx:mode s)))
(cx:make mode
(string-append "((" (obj:str-name mode) ")"
" (" (obj:str-name (cx:mode s)) ")"
" (" (cx:c s) "))"))
(cx:make mode
(string-append name
(obj:str-name (rtx-sem-mode (cx:mode s)))
(obj:str-name (rtx-sem-mode mode))
" (" (cx:c s) ")"))))
)
;; Floating point mode conversions.
;; MODE is the mode name.
(define (s-float-convop estate name mode how1 s1)
;; Get S1 in its normal mode, then convert.
(let ((s (rtl-c-get estate DFLT s1))
(mode (mode:lookup mode))
(how (rtl-c-get estate DFLT how1)))
(if (and (not (estate-rtl-cover-fns? estate))
(mode:host? (cx:mode s)))
(cx:make mode
(string-append "((" (obj:str-name mode) ")"
" (" (obj:str-name (cx:mode s)) ")"
" (" (cx:c s) "))"))
(cx:make mode
(string-append "CGEN_CPU_FPU (current_cpu)->ops->"
(string-downcase name)
(string-downcase (obj:str-name (rtx-sem-mode (cx:mode s))))
(string-downcase (obj:str-name (rtx-sem-mode mode)))
" (CGEN_CPU_FPU (current_cpu), "
(cx:c how) ", "
(cx:c s) ")"))))
)
; Compare SRC1 and SRC2 in mode MODE.
; NAME is one of eq,ne,lt,le,gt,ge,ltu,leu,gtu,geu.
; MODE is the mode name.
; The result has mode BI.
; ??? May want a host int mode result as BI may introduce some slowness
; in the generated code.
(define (s-cmpop estate name c-op mode src1 src2)
(let* ((val1 (rtl-c-get estate mode src1))
; Refetch mode in case it was DFLT.
(mode (mode:lookup mode)) ;;(cx:mode val1)) ;; FIXME: can't get DFLT anymore
(val2 (rtl-c-get estate mode src2)))
; FIXME: Argument checking.
; If no C operation has been provided, use a macro, or
; if this is the simulator and MODE is not a host mode, use a macro.
(if (/rtx-use-sem-fn? estate c-op mode)
(if (mode-float? mode)
(cx:make (mode:lookup 'BI)
(string-append "CGEN_CPU_FPU (current_cpu)->ops->"
(string-downcase (symbol->string name))
(string-downcase (obj:str-name (rtx-sem-mode mode)))
" (CGEN_CPU_FPU (current_cpu), "
(cx:c val1) ", "
(cx:c val2) ")"))
(cx:make (mode:lookup 'BI)
(string-append (string-upcase (symbol->string name))
(if (memq name '(eq ne))
(obj:str-name (rtx-sem-mode mode))
(obj:str-name mode))
" (" (cx:c val1) ", "
(cx:c val2) ")")))
(cx:make (mode:lookup 'BI)
(string-append "(("
(cx:c val1)
") " c-op " ("
(cx:c val2)
"))"))))
)
; Conditional execution.
; `if' in RTL has a result, like ?: in C.
; We support both: one with a result (non VOID mode), and one without (VOID mode).
; The non-VOID case must have an else part.
; MODE is the mode of the result, not the comparison.
; MODE is the mode name.
; The comparison is expected to return a zero/non-zero value.
; ??? Perhaps this should be a syntax-expr. Later.
(define (s-if estate mode cond then . else)
(if (> (length else) 1)
(estate-error estate "if: too many elements in `else' part" else))
(let ()
(if (or (mode:eq? 'DFLT mode) ;; FIXME: can't get DFLT anymore
(mode:eq? 'VOID mode))
(cx:make mode
(string-append "if (" (cx:c (rtl-c-get estate DFLT cond)) ")"
" {\n" (cx:c (rtl-c-get estate mode then)) "}"
(if (not (null? else))
(string-append " else {\n"
(cx:c (rtl-c-get estate mode (car else)))
"}\n")
"\n")
))
(if (= (length else) 1)
(cx:make mode
(string-append "(("
(cx:c (rtl-c-get estate DFLT cond))
") ? ("
(cx:c (rtl-c-get estate mode then))
") : ("
(cx:c (rtl-c-get estate mode (car else)))
"))"))
(estate-error estate "non-void-mode `if' must have `else' part"))))
)
; A multiway `if'.
; MODE is the mode name.
; If MODE is VOID emit a series of if/else's.
; If MODE is not VOID, emit a series of ?:'s.
; COND-CODE-LIST is a list of lists, each sublist is a list of two elements:
; condition, code. The condition part must return a zero/non-zero value, and
; the code part is treated as a `sequence'.
; This defer argument evaluation, the syntax
; ((... condition ...) ... action ...)
; needs special parsing.
; FIXME: Need more error checking of arguments.
(define (s-cond estate mode . cond-code-list)
;; FIXME: can't get DFLT anymore
(let ((vm? (or (mode:eq? 'DFLT mode) (mode:eq? 'VOID mode))))
(if (null? cond-code-list)
(estate-error estate "empty `cond'"))
(let ((if-part (if vm? "if (" "("))
(then-part (if vm? ") " ") ? "))
(elseif-part (if vm? " else if (" " : ("))
(else-part (if vm? " else " " : "))
(fi-part (if vm? "" ")")))
(let loop ((result
(string-append
if-part
(cx:c (rtl-c-get estate DFLT (caar cond-code-list)))
then-part
(cx:c (apply s-sequence