-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemit-cpp-af-latest.rkt
953 lines (755 loc) · 45.8 KB
/
emit-cpp-af-latest.rkt
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
#lang racket
(require "utils.rkt")
(require "slog-utils.rkt")
; (require print-debug/print-dbg)
(provide emit-cpp)
(define (emit-cpp proc_list filepath slog-flag ast-root desugarred_prog anf_prog alphatized_cps_prog)
; defining cpp header files
; replace the old cpp file, if exists
(append-line filepath "#include<stdio.h>" 'replace)
(append-line filepath "#include<string.h>")
(append-line filepath "#include \"gmp_func.h\"")
(append-line filepath (string-append "#include " "\"../../prelude.hpp\""))
; (append-line filepath "using namespace std;\n" )
(define conflicting_c++_prims '#hash((abs . abs_brouhaha)
(random . random_brouhaha)
(remainder . remainder_brouhaha)
(sqrt . sqrt_brouhaha)
(for . for_brouhaha)
))
(define proc-name-shadowed?
(let loop ([env+ (hash)] [prog+ desugarred_prog])
(match prog+
[`((define (,name . ,_) ,_) ,_ ...)
(loop
(if (hash-has-key? env+ name)
(hash-set env+ name `(shadowed ,@(cdr (hash-ref env+ name))))
(hash-set env+ name `(not-shadowed dummyinfo)))
(cdr prog+))]
[`((define-prim ,f-name ,params ...) ,_ ...)
(loop
(if (hash-has-key? env+ f-name)
(hash-set env+ f-name `(shadowed ,params))
(hash-set env+ f-name `(not-shadowed-dp ,params)))
(cdr prog+))]
[`() env+])))
(define find-global-constants
(let loop ([env+ (hash)] [prog+ anf_prog])
(match prog+
[`((define (,name . ,param) ,body) ,rest ...)
(loop (find-global-constants-helper body env+) rest)]
[`((define-prim ,f-name ,params ...) ,_ ...)
(loop env+ (cdr prog+))]
[`() env+]
)))
; (pretty-print anf_prog)
; (pretty-print find-global-constants)
; (define global-kont-set
; (let loop ([item-set (set)] [prog+ alphatized_cps_prog])
; (match prog+
; [`((define (,ptr ,kont . ,param) ,body) ,rest ...)
; (loop (set-add item-set kont) rest)]
; [`((define (,ptr . ,param) ,body) ,rest ...)
; (loop item-set rest)]
; [`((define-prim ,ptr ,params ...) ,_ ...)
; (loop item-set (cdr prog+))]
; [`() item-set]
; )))
(define global-symbols-set
(let loop ([item-set (set)] [prog+ alphatized_cps_prog])
(match prog+
; [`((define (,ptr ,kont . ,param) ,body) ,rest ...)
; (loop (set-add (set-add item-set ptr) kont) rest)]
[`((define (,ptr . ,param) ,body) ,rest ...)
(loop (set-add item-set ptr) rest)]
[`((define-prim ,ptr ,params ...) ,_ ...)
(loop (set-add item-set ptr) (cdr prog+))]
[`() item-set]
)))
(append-line filepath "\n// declaring global constants at the top")
(hash-map find-global-constants
(lambda (key type)
(cond
[(equal? (car type) 'mpz)
(append-line filepath (format "mpz_t* ~a = (mpz_t *)(GC_MALLOC(sizeof(mpz_t)));" (cadr type)))]
[(equal? (car type) 'mpf)
(append-line filepath (format "mpf_t* ~a = (mpf_t *)(GC_MALLOC(sizeof(mpf_t)));" (cadr type)))]
[(equal? (car type) 'int)
(append-line filepath (format "void* ~a;" (cadr type)))]
[(equal? (car type) 'float)
(append-line filepath (format "void* ~a;" (cadr type)))]
[(or (equal? (car type) 'bool-true) (equal? (car type) 'bool-false))
(append-line filepath (format "void* ~a;" (cadr type)))]
[else (error "Error occured find-global-constants!")]
)
))
(append-line filepath "\n")
(define global-int-vars
(foldl (lambda (idx acc)
; (if (hash-has-key? find-global-constants idx)
; acc
; (hash-set acc idx (gensym 'int)))
(hash-set acc idx (gensym 'int)))
(hash)
(range 1 6)))
(append-line filepath "\n// declaring global integer variable")
(hash-map global-int-vars
(lambda (key val)
; (append-line filepath (format "void* ~a;" val))
(append-line filepath (format "int ~a;" val))
))
; (append-line filepath "\n// declaring global konts at the top")
; (for ([item (set->list global-kont-set)])
; (append-line filepath (format "void *const ~a = nullptr;" item)))
; (append-line filepath "\n")
(append-line filepath "\n// declaring functions at the top")
(define declare-top-level-funcs
(let loop ([env+ (hash)] [prog+ proc_list])
(match prog+
[`((proc (,ptr ,env ,args ...) ,body) ,rest ...)
(define args-str
(foldl (lambda (arg acc) (string-append acc ", void* " (symbol->string arg)))
; (string-append "long numArgs, " (string-append "void* " (symbol->string env)))
(string-append "void* " (symbol->string env))
args))
; (if (string-prefix? (symbol->string ptr) "lam")
; (append-line filepath (format "void ~a_fptr(); // ~a" (get-c-string ptr) ptr))
; (append-line filepath (format "void ~a_fptr(~a); // ~a" (get-c-string ptr) args-str ptr)))
(append-line filepath (format "void ~a_fptr(); // ~a" (get-c-string ptr) ptr))
(when (not (string-prefix? (symbol->string ptr) "lam"))
(define func_name (format "void ~a_spec(~a); // ~a" (get-c-string ptr) args-str ptr))
(append-line filepath func_name))
(if (hash-has-key? conflicting_c++_prims (get-c-string ptr))
(append-line filepath
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(hash-ref conflicting_c++_prims (get-c-string ptr))
(get-c-string ptr)
0))
(append-line filepath
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(get-c-string ptr)
(get-c-string ptr)
0)))
; (+ (length args) 1)) => +1, because we are including the env
(loop (hash-set env+ ptr (+ (length args) 1)) (cdr prog+))
; (if (string-prefix? (symbol->string ptr) "lam")
; (loop (hash-set env+ ptr 0) (cdr prog+))
; (loop (hash-set env+ ptr (+ (length args) 1)) (cdr prog+)))
]
[`((proc (,ptr ,env . ,arg) ,body) ,rest ...)
(define func_name (format "void ~a_fptr(); // ~a" (get-c-string ptr) ptr))
(append-line filepath func_name)
(append-line filepath
(if (hash-has-key? conflicting_c++_prims (get-c-string ptr))
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(hash-ref conflicting_c++_prims (get-c-string ptr))
(get-c-string ptr)
0)
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(get-c-string ptr)
(get-c-string ptr)
0)))
(loop (hash-set env+ ptr 0) (cdr prog+))]
[`((define-prim ,ptr ,params ...) ,_ ...)
(define func_name (format "void ~a_fptr(); // ~a" (get-c-string ptr) ptr))
(append-line filepath func_name)
(append-line filepath
(if (hash-has-key? conflicting_c++_prims (get-c-string ptr))
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(hash-ref conflicting_c++_prims (get-c-string ptr))
(get-c-string ptr)
0)
(format "void* ~a = encode_clo(alloc_clo(~a_fptr, ~a));\n"
(get-c-string ptr)
(get-c-string ptr)
0)))
(loop (hash-set env+ ptr 0) (cdr prog+))]
[`() env+]
)))
; (pretty-print declare-top-level-funcs)
; (pretty-print global-symbols-set)
(append-line filepath "\n")
; (append-line filepath "\n// pre-decoding global symbols")
; (for ([item (set->list global-symbols-set)])
; ;r = reinterpret_cast<void (*)(long, void*, void*, void*, void*)>((decode_clo(q_u45helper))[0]);
; (define arg_size (hash-ref declare-top-level-funcs item))
; (define args-str
; (foldl (lambda (arg acc) (string-append acc ", " arg))
; "long"
; (map (λ (item) "void*") (range 0 arg_size))))
; (append-line filepath
; (if (hash-has-key? conflicting_c++_prims (get-c-string item))
; (format "auto decoded_~a = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);"
; (get-c-string item) (hash-ref conflicting_c++_prims (get-c-string item)))
; (format "auto decoded_~a = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" (get-c-string item) (get-c-string item))))
; ; (if (> arg_size 0)
; ; (append-line filepath
; ; (if (hash-has-key? conflicting_c++_prims (get-c-string item))
; ; (format "auto decoded_~a = reinterpret_cast<void (*)(~a)>((decode_clo(~a))[0]);"
; ; (get-c-string item) (hash-ref conflicting_c++_prims (get-c-string item)))
; ; (format "auto decoded_~a = reinterpret_cast<void (*)(~a)>((decode_clo(~a))[0]);" (get-c-string item) args-str (get-c-string item))))
; ; (append-line filepath
; ; (if (hash-has-key? conflicting_c++_prims (get-c-string item))
; ; (format "auto decoded_~a = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);"
; ; (get-c-string item) (hash-ref conflicting_c++_prims (get-c-string item)))
; ; (format "auto decoded_~a = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" (get-c-string item) (get-c-string item)))))
; )
; (append-line filepath "\n")
(define (convert-proc-body proc_name proc_env proc_arg body)
; (match (p-dbg body)
(match body
[`(let ([,lhs (make-closure ,ptr ,args ...)]) ,letbody)
(define arglength (length args))
; new-closure
(append-line filepath "\n//creating new closure instance")
; (define ptrName (gensym 'ptr))
(define cloName (gensym 'clo))
(append-line filepath (format "void** ~a = alloc_clo(~a_fptr, ~a);" cloName ptr arglength))
(when (> (+ arglength 1) 1)
(append-line filepath "\n//setting env list"))
(for ([i (in-range 1 (+ arglength 1))] [item args])
(if (hash-has-key? conflicting_c++_prims (get-c-string item))
(append-line filepath (format "~a[~a] = ~a;" cloName i (hash-ref conflicting_c++_prims (get-c-string item))))
(append-line filepath (format "~a[~a] = ~a;" cloName i (get-c-string item)))))
(append-line filepath (format "void* ~a = encode_clo(~a);" (get-c-string lhs) cloName))
; (when (> (+ arglength 1) 1) (append-line filepath "\n"))
(append-line filepath "\n")
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(let ([,lhs (prim ,op ,args ...)]) ,letbody)
(define line
(format "void* const ~a = apply_prim_~a_~a(~a);"
(get-c-string lhs)
(get-c-string op)
(length args)
(string-join (map (λ (item) (format "~a" (get-c-string item))) args) ", ")))
(append-line filepath line)
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(let ([,lhs (apply-prim ,op ,arg)]) ,letbody)
(define line
(format "void* ~a = apply_prim_~a(~a);"
(get-c-string lhs)
(get-c-string op)
(get-c-string arg)))
(append-line filepath line)
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(let ([,lhs (env-ref ,env ,idx)]) ,letbody)
; (append-line filepath (format "void* ~a = (decode_clo(~a))[~a];" (get-c-string lhs) env idx))
(append-line filepath (format "void* const ~a = decode_clo_array[~a];" (get-c-string lhs) idx))
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(let ([,lhs ,val]) ,letbody)
; (match (p-dbg val)
(match val
[`(quote ,(? flonum? val))
(match-define `(,type ,varname) (hash-ref find-global-constants val))
(cond
[(equal? type 'float)
(append-line filepath (format "void* const ~a = ~a;" (get-c-string lhs) varname))]
[(equal? type 'mpf)
(append-line filepath (format "void* const ~a = encode_mpf(~a);" (get-c-string lhs) varname))]
[else (error "Error occured in emit-cpp -> proc_body case: (let ([,lhs ,val]) ,letbody)")])
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(quote ,(? exact-integer? val))
(match-define `(,type ,varname) (hash-ref find-global-constants val))
(cond
[(equal? type 'int)
(append-line filepath (format "void* const ~a = ~a;" lhs varname))]
[(equal? type 'mpz)
(append-line filepath (format "void* const ~a = encode_mpz(~a);" (get-c-string lhs) varname))]
[else (error "Error occured in emit-cpp -> proc_body case: (let ([,lhs ,val]) ,letbody)")])
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(quote ,(? boolean? val))
(match-define `(,type ,varname) (hash-ref find-global-constants val))
(cond
[(equal? type 'bool-true)
(append-line filepath (format "void* const ~a = ~a;" (get-c-string lhs) varname))]
[(equal? type 'bool-false)
(append-line filepath (format "void* const ~a = ~a;" (get-c-string lhs) varname))]
[else (error "Error occured in emit-cpp -> proc_body case: (quote ,(? boolean? val)")])
(convert-proc-body proc_name proc_env proc_arg letbody)
]
[`(quote ,(? null? val))
(append-line filepath (format "void* ~a = encode_null();" (get-c-string lhs)))
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(quote ,(? string? val))
(append-line
filepath
(format "void* ~a = encode_str(new(GC) std::string(\"~a\"));" (get-c-string lhs) val))
(convert-proc-body proc_name proc_env proc_arg letbody)]
[`(quote ,(? symbol? val))
(append-line
filepath
(format "void* ~a = encode_str(new(GC) std::string(\"~a\"));" (get-c-string lhs) (symbol->string val)))
(convert-proc-body proc_name proc_env proc_arg letbody)]
[(? symbol?) ; Not sure what this case does or supposed to do - SK
; (pretty-print (~a "cpb: " val))
(append-line filepath
(format "void* ~a = encode_str(new string(\"~a\"));" (get-c-string lhs) val))
(convert-proc-body proc_name proc_env proc_arg letbody)]
[_ (raise (format "Unknown datatype! ~a" val))])]
[`(if ,grd ,texp ,fexp)
(append-line filepath "\n//if-clause")
(define guard (gensym 'if_guard))
; (append-line filepath (format "bool ~a = is_true(~a);" guard grd))
(append-line filepath (format "if(is_true(~a))\n{" grd))
(convert-proc-body proc_name proc_env proc_arg texp)
(append-line filepath "}\nelse\n{")
(convert-proc-body proc_name proc_env proc_arg fexp)
(append-line filepath "}\n")]
[`(clo-apply ,func ,args)
(match-define `(,builtin-func ,res) (is-define-prim ast-root func))
(cond
[(and slog-flag res)
; (displayln (~a "func: " func "builtin-func: " builtin-func " res: " res))
(append-line filepath "\n//clo-apply")
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string builtin-func)))
(append-line filepath (format "arg_buffer[2] = ~a;" (get-c-string args)))
(append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" 2))
(append-line filepath (format "~a_fptr();" (get-c-string builtin-func)))
; (append-line filepath "return nullptr;")
]
[else
(append-line filepath "\n//clo-apply")
(match-define `(,is_define_prim ,is_callable ,arg_count)
(callable-define-prim? proc-name-shadowed? func 0))
; (set! is_define_prim #f)
; (set! is_callable #f)
; (set! arg_count 0)
; (displayln func)
; (displayln is_define_prim)
; (displayln is_callable)
; (displayln arg_count)
; (displayln "---------")
(cond
; one of the define-prim so call that without unpacking closure
[is_define_prim
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string func)))
(append-line filepath (format "arg_buffer[2] = ~a;" (get-c-string args)))
; (append-line filepath (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2)))
(append-line filepath (format "numArgs = ~a;" (hash-ref global-int-vars 2)))
(append-line filepath (format "~a_fptr();" (get-c-string func)))
]
[else
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string func)))
(append-line filepath (format "arg_buffer[2] = ~a;" (get-c-string args)))
; (append-line filepath (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2)))
(append-line filepath (format "numArgs = ~a;" (hash-ref global-int-vars 2)))
; (append-line filepath
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);"
; (get-c-string func)))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath (format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" (get-c-string func)))
])]
)]
[`(clo-app ,func ,args ...)
; builtin-func will hold, either the called builtin define-prim
; or the aliased builtin define-prim by the "func" at the call-site
(match-define `(,builtin-func ,res1) (if slog-flag
(is-define-prim ast-root func)
`(,func #f)))
(match-define `(,temp-func ,res2) (if slog-flag
(check-define-prim-arg-count ast-root builtin-func (- (length args) 1))
`(,func #f)))
(cond
; builtin define-prim with a specific argument count
; builtin define-prim with a specific argument count
[(and slog-flag res1 res2)
(append-line filepath "\n//clo-app")
(define args-str
(foldl (lambda (arg acc) (string-append acc ", " (symbol->string arg)))
(symbol->string (cadr args))
(cddr args)))
(append-line filepath (format "arg_buffer[2] = apply_prim_~a_~a(~a);" (get-c-string builtin-func) (- (length args) 1) args-str))
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string (car args))))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants 2) (format "numArgs = ~a;" (cadr (hash-ref find-global-constants 2)))]
; [(hash-has-key? global-int-vars 2) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2))]
[(hash-has-key? global-int-vars 2) (format "numArgs = ~a;" (hash-ref global-int-vars 2))]
[else (format "numArgs = 2;")]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (2);")]
)
)
; (append-line
; filepath
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);"
; (get-c-string (car args))))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath (format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" (get-c-string (car args))))
]
; not specific argument count, but still one of the builtin so calling that directly
; instead of unpacking the closure
[(and slog-flag res1)
(append-line filepath "\n//clo-app")
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string builtin-func)))
(for ([i (in-range 1 (+ (length args) 1))] [item args])
(append-line filepath (format "arg_buffer[~a] = ~a;" (+ i 1) (get-c-string item))))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants (+ (length args) 1))
; (format "numArgs = ~a;" (cadr (hash-ref find-global-constants (+ (length args) 1))))]
; [(hash-has-key? global-int-vars (+ (length args) 1)) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
[(hash-has-key? global-int-vars (+ (length args) 1)) (format "numArgs = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (~a);" (+ (length args) 1))]
[else (format "numArgs = ~a;" (+ (length args) 1))]
)
)
(if (hash-has-key? conflicting_c++_prims (get-c-string func))
(append-line filepath (format "~a_fptr();" (hash-ref conflicting_c++_prims (get-c-string builtin-func))))
(append-line filepath (format "~a_fptr();" (get-c-string builtin-func)))
)
]
[else
(match-define `(,is_define_prim ,is_callable ,arg_count)
(callable-define-prim? proc-name-shadowed? func (- (length args) 1)))
; (set! is_define_prim #f)
; (set! is_callable #f)
; (set! arg_count 0)
; (displayln func)
; (displayln (- (length args) 1))
; (displayln is_define_prim)
; (displayln is_callable)
; (displayln arg_count)
; (displayln "---------")
; (lambda (arg acc)
; (string-append
; (if (hash-has-key? arg_hash acc)
; (hash-ref arg_hash acc)
; acc)
; ", "
; (if (hash-has-key? arg_hash arg)
; (hash-ref arg_hash (symbol->string arg))
; (symbol->string arg))))
(define args-str
(foldl (lambda (arg acc)
(string-append
(if (hash-has-key? arg_hash acc)
(hash-ref arg_hash acc)
acc)
", "
(if (hash-has-key? arg_hash arg)
(hash-ref arg_hash (symbol->string arg))
(symbol->string arg))))
; (symbol->string (cadr args))
(if (hash-has-key? arg_hash (cadr args))
(hash-ref arg_hash (cadr args))
(symbol->string (cadr args)))
(cddr args)))
(cond
[(and is_define_prim is_callable)
(append-line filepath "\n//clo-app")
(define args-str
(foldl (lambda (arg acc) (string-append acc ", " (symbol->string arg)))
(symbol->string (cadr args))
(cddr args)))
(append-line filepath (format "arg_buffer[2] = apply_prim_~a_~a(~a);" (get-c-string func) arg_count args-str))
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string (car args))))
; (append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" 2))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants 2) (format "numArgs = ~a;" (cadr (hash-ref find-global-constants 2)))]
; [(hash-has-key? global-int-vars 2) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2))]
[(hash-has-key? global-int-vars 2) (format "numArgs = ~a;" (hash-ref global-int-vars 2))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (2);")]
[else (format "numArgs = 2;")]
))
(if (set-member? global-symbols-set (car args))
(begin
(append-line filepath (format "~a_fptr();" (get-c-string (car args)))))
(begin
; (append-line
; filepath
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);"
; (get-c-string (car args))))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath (format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" (get-c-string (car args))))
))
]
[is_define_prim
(append-line filepath "\n//clo-app")
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string func)))
(for ([i (in-range 1 (+ (length args) 1))] [item args])
(append-line filepath (format "arg_buffer[~a] = ~a;" (+ i 1) (get-c-string item))))
; (append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" (+ (length args) 1)))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants (+ (length args) 1))
; (format "numArgs = ~a;" (cadr (hash-ref find-global-constants (+ (length args) 1))))]
; [(hash-has-key? global-int-vars (+ (length args) 1)) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
[(hash-has-key? global-int-vars (+ (length args) 1)) (format "numArgs = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (~a);" (+ (length args) 1))]
[else (format "numArgs = ~a;" (+ (length args) 1))]
))
(append-line filepath (format "~a_fptr();" (get-c-string func)))]
[else
(append-line filepath "\n//clo-app")
(if (hash-has-key? declare-top-level-funcs func)
(if (= (hash-ref declare-top-level-funcs func) 0)
(begin
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string func)))
(for ([i (in-range 1 (+ (length args) 1))] [item args])
(append-line filepath (format "arg_buffer[~a] = ~a;" (+ i 1) (get-c-string item))))
; (append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" (+ (length args) 1)))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants (+ (length args) 1))
; (format "numArgs = ~a;" (cadr (hash-ref find-global-constants (+ (length args) 1))))]
; [(hash-has-key? global-int-vars (+ (length args) 1)) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
[(hash-has-key? global-int-vars (+ (length args) 1)) (format "numArgs = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (~a);" (+ (length args) 1))]
[else (format "numArgs = ~a;" (+ (length args) 1))]
))
(append-line filepath (format "~a_fptr();" (get-c-string func))) )
(append-line filepath
(format "~a_spec(~a);" (get-c-string func)
(foldl (lambda (arg acc) (string-append acc ", " (symbol->string arg)))
; (string-append (number->string (+ (length args) 1)) ", " (symbol->string (get-c-string func)) ", " (symbol->string (car args)))
(string-append (symbol->string (get-c-string func)) ", " (symbol->string (car args)))
(cdr args)))))
(begin
(append-line filepath (format "arg_buffer[1] = ~a;" (get-c-string func)))
(for ([i (in-range 1 (+ (length args) 1))] [item args])
(append-line filepath (format "arg_buffer[~a] = ~a;" (+ i 1) (get-c-string item))))
; (append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" (+ (length args) 1)))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants (+ (length args) 1))
; (format "numArgs = ~a;" (cadr (hash-ref find-global-constants (+ (length args) 1))))]
; [(hash-has-key? global-int-vars (+ (length args) 1)) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
[(hash-has-key? global-int-vars (+ (length args) 1)) (format "numArgs = ~a;" (hash-ref global-int-vars (+ (length args) 1)))]
[else (format "numArgs = ~a;" (+ (length args) 1))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*> (~a);" (+ (length args) 1))]
))
; (append-line
; filepath
; (if (hash-has-key? conflicting_c++_prims (get-c-string func))
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" (hash-ref conflicting_c++_prims (get-c-string func)))
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" (get-c-string func))
; ))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath
(if (hash-has-key? conflicting_c++_prims (get-c-string func))
(format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" (hash-ref conflicting_c++_prims (get-c-string func)))
(format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" (get-c-string func))))
))
])
])]))
(define (convert-procs proc)
; (pretty-print proc)
(match proc
[`(proc (,ptr ,env ,args ...) ,body)
(append-line filepath (format "inline void ~a_fptr() // ~a -> generic version ~a" (get-c-string ptr) ptr "\n{"))
; uncomment these two lines for debugging!
; (append-line filepath (format "std::cout<<\"In ~a_fptr: generic version\"<<std::endl;" (get-c-string ptr)))
; (append-line filepath (format "print_arg_buffer();\n"))
; (append-line filepath "call_counter++;")
; (append-line filepath "//reading number of args")
; (append-line filepath (format "int numArgs = reinterpret_cast<int>(arg_buffer[0]);"))
; (append-line filepath (format "numArgs = reinterpret_cast<long>(arg_buffer[0]);"))
(when (not (string-prefix? (symbol->string env) "_"))
(append-line filepath "//reading env")
(append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env))))
; (append-line filepath "//reading env")
; (append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env)))
(append-line filepath "//reading env and args")
(for ([i (in-range 2 (+ (length args) 2))] [item args])
(append-line filepath (format "void* const ~a = arg_buffer[~a];" (get-c-string item) i)))
(append-line filepath "//decoding closure array")
(append-line filepath (format "void** decode_clo_array = nullptr;"))
(when (string-prefix? (symbol->string env) "e")
(append-line filepath (format "decode_clo_array = decode_clo(~a);" (get-c-string env))))
(convert-proc-body (get-c-string ptr) (get-c-string env) args body)
(append-line filepath "}\n")
(when (not (string-prefix? (symbol->string ptr) "lam"))
(define args-str
(foldl (lambda (arg acc) (string-append acc ", void* " (symbol->string arg)))
; (string-append "long numArgs, " (string-append "void* " (symbol->string env)))
(string-append "void* " (symbol->string env))
args))
(append-line filepath (format "inline void ~a_spec(~a) // ~a ~a" (get-c-string ptr) args-str ptr "\n{"))
; uncomment these two lines for debugging!
; (append-line filepath (format "std::cout<<\"In ~a_fptr\"<<std::endl;" (get-c-string ptr)))
; (append-line filepath (format "print_arg_buffer();\n"))
; (append-line filepath "call_counter++;")
(if (hash-has-key? declare-top-level-funcs ptr)
(begin
(when (= (hash-ref declare-top-level-funcs ptr) 0)
; (append-line filepath "//reading number of args")
; (append-line filepath (format "int numArgs = reinterpret_cast<int>(arg_buffer[0]);"))
; (append-line filepath (format "numArgs = reinterpret_cast<long>(arg_buffer[0]);"))
(append-line filepath "//reading env")
(append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env)))
(append-line filepath "//reading env and args")
(for ([i (in-range 2 (+ (length args) 2))] [item args])
(append-line filepath (format "void* const ~a = arg_buffer[~a];" (get-c-string item) i))))
)
(begin
; (append-line filepath "//reading number of args")
; (append-line filepath (format "int numArgs = reinterpret_cast<int>(arg_buffer[0]);"))
; (append-line filepath (format "numArgs = reinterpret_cast<long>(arg_buffer[0]);"))
(append-line filepath "//reading env")
(append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env)))
(append-line filepath "//reading env and args")
(for ([i (in-range 2 (+ (length args) 2))] [item args])
(append-line filepath (format "void* const ~a = arg_buffer[~a];" (get-c-string item) i)))
)
)
(append-line filepath "//decoding closure array")
(append-line filepath (format "void** decode_clo_array = nullptr;"))
(when (string-prefix? (symbol->string env) "e")
(append-line filepath (format "decode_clo_array = decode_clo(~a);" (get-c-string env))))
(convert-proc-body (get-c-string ptr) (get-c-string env) args body)
(append-line filepath "}\n"))
]
[`(proc (,ptr ,env . ,arg) ,body)
(define func_name (format "void ~a_fptr() // ~a ~a" (get-c-string ptr) ptr "\n{"))
; start of function definitions
(append-line filepath func_name)
; uncomment these two lines for debugging!
; (append-line filepath (format "std::cout<<\"In ~a_fptr\"<<std::endl;" (get-c-string ptr)))
; (append-line filepath (format "print_arg_buffer();\n"))
; (append-line filepath "call_counter++;")
; (append-line filepath "//reading number of args")
; (append-line filepath (format "int numArgs = reinterpret_cast<int>(arg_buffer[0]);"))
; (append-line filepath (format "numArgs = reinterpret_cast<long>(arg_buffer[0]);"))
(append-line filepath "//reading env")
(append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env)))
(append-line filepath "//decoding closure array")
(append-line filepath (format "void** decode_clo_array = nullptr;"))
(when (string-prefix? (symbol->string env) "e")
(append-line filepath (format "decode_clo_array = decode_clo(~a);" (get-c-string env))))
(append-line filepath (format "void* ~a = nullptr;" arg))
(append-line filepath (format "if(is_cons(arg_buffer[2]))\n{"))
(append-line filepath "//(apply e0 e0) case")
(append-line filepath (format "~a = arg_buffer[2];" arg))
(append-line filepath "}\nelse\n{")
(append-line filepath "//building cons cell")
(append-line filepath (format "~a = encode_null();" arg))
(append-line filepath (format "for(int i = numArgs; i >= 2; i--)\n{"))
(append-line filepath (format "~a = prim_cons(arg_buffer[i], ~a);" arg arg))
(append-line filepath (format "\n}\n"))
(append-line filepath "}\n")
(convert-proc-body (get-c-string ptr) (get-c-string env) arg body)
(append-line filepath "}\n")
]
[`(define-prim ,ptr ,args ...)
(define k (gensym 'kont))
(define x (gensym 'x))
(define env (gensym '_env))
(define arg 'lst)
(define newarg (gensym arg))
; (define make-generic-apply-prim-body
; `(let ([,k (prim car ,arg)])
; (let ([,newarg (prim cdr ,arg)])
; (let ((,x (apply-prim ,ptr ,newarg)))
; (clo-app ,k ,x)))))
(define func_name (format "inline void ~a_fptr() // ~a ~a" (get-c-string ptr) ptr "\n{"))
; start of function definitions
(append-line filepath func_name)
; uncomment these two lines for debugging!
; (append-line filepath (format "std::cout<<\"In ~a_fptr\"<<std::endl;" (get-c-string ptr)))
; (append-line filepath (format "print_arg_buffer();\n"))
; (append-line filepath "call_counter++;")
; (append-line filepath "//reading number of args")
; (append-line filepath "// This is the second type of the functions")
; (append-line filepath (format "int numArgs = reinterpret_cast<int>(arg_buffer[0]);"))
; (append-line filepath (format "numArgs = reinterpret_cast<long>(arg_buffer[0]);"))
(append-line filepath "//reading env")
(append-line filepath (format "void* const ~a = arg_buffer[1];" (get-c-string env)))
(append-line filepath "//decoding closure array")
(append-line filepath (format "void** decode_clo_array = nullptr;"))
(append-line filepath (format "if(is_cons(arg_buffer[2]))\n{"))
(append-line filepath "//(apply e0 e0) case")
(append-line filepath (format "void* const ~a = arg_buffer[2];" arg))
(append-line filepath (format "void* const ~a = prim_car(lst);" k))
(append-line filepath (format "void* const ~a = prim_cdr(lst);" newarg))
(append-line filepath (format "void* const ~a = apply_prim_~a(~a);" x (get-c-string ptr) newarg))
(append-line filepath (format "arg_buffer[1] = ~a;" k))
(append-line filepath (format "arg_buffer[2] = ~a;" x))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants 2) (format "arg_buffer[0] = ~a;" (cadr (hash-ref find-global-constants 2)))]
; [(hash-has-key? global-int-vars 2) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2))]
[(hash-has-key? global-int-vars 2) (format "numArgs = ~a;" (hash-ref global-int-vars 2))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*>(2);")]
[else (format "numArgs = 2;")]
))
; (append-line
; filepath
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" k))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath (format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" k))
(append-line filepath "}\nelse\n{")
(append-line filepath (format "void* const ~a = arg_buffer[2];" k))
(append-line filepath (format "void* const ~a = apply_prim_~a(arg_buffer);" x (get-c-string ptr)))
(append-line filepath (format "arg_buffer[1] = ~a;" k))
(append-line filepath (format "arg_buffer[2] = ~a;" x))
; (append-line filepath (format "arg_buffer[0] = reinterpret_cast<void*>(~a);" 2))
(append-line filepath
(cond
; [(hash-has-key? find-global-constants 2) (format "arg_buffer[0] = ~a;" (cadr (hash-ref find-global-constants 2)))]
; [(hash-has-key? global-int-vars 2) (format "arg_buffer[0] = ~a;" (hash-ref global-int-vars 2))]
[(hash-has-key? global-int-vars 2) (format "numArgs = ~a;" (hash-ref global-int-vars 2))]
; [else (format "arg_buffer[0] = reinterpret_cast<void*>(2);")]
[else (format "numArgs = 2;")]
))
; (append-line
; filepath
; (format "auto function_ptr = reinterpret_cast<void (*)()>((decode_clo(~a))[0]);" k))
; (append-line filepath "\n// calling next procedure using a function pointer")
; (append-line filepath "function_ptr();")
(append-line filepath (format "reinterpret_cast<void (*)()>((decode_clo(~a))[0])();" k))
(append-line filepath "}\n")
(append-line filepath "}\n")
]
) ;end of match
;end of function definitions.
)
(map convert-procs proc_list)
; writing the main function.
(append-line filepath "int main(int argc, char **argv)\n{")
(append-line filepath
"mp_set_memory_functions(&allocate_function,
&reallocate_function,
&deallocate_function);")
(append-line filepath "\n// initializing global constants in the main")
(hash-map find-global-constants
(lambda (key type)
(cond
[(equal? (car type) 'mpz)
(append-line filepath (format "mpz_init_set_str(*~a, \"~a\", 10);" (cadr type) key))]
[(equal? (car type) 'mpf)
(append-line filepath (format "mpf_init_set_str(*~a, \"~a\", 10);" (cadr type) key))]
[(equal? (car type) 'int)
(append-line filepath (format "~a = reinterpret_cast<void *>(encode_int(~a));" (cadr type) key))]
[(equal? (car type) 'float)
(append-line filepath (format "~a = reinterpret_cast<void *>(encode_float(~a));" (cadr type) key))]
[(equal? (car type) 'bool-true)
(append-line filepath (format "~a = encode_bool(true);" (cadr type)))]
[(equal? (car type) 'bool-false)
(append-line filepath (format "~a = encode_bool(false);" (cadr type)))]
[else (error "Error occured could not find value in -> find-global-constants!")]
)
))
(append-line filepath "\n")
(append-line filepath "\n// initializing global integer vars")
(hash-map global-int-vars
(lambda (key val)
; (append-line filepath (format "~a = reinterpret_cast<void*>(encode_int(~a));" val key))
; (append-line filepath (format "~a = reinterpret_cast<void *>(~a);" val key))
(append-line filepath (format "~a = ~a;" val key))
))
(append-line filepath "\n//making a call to the brouhaha main function to kick off our C++ emission.")
; (append-line filepath "call_counter++;")
(append-line filepath "void *fhalt_clo = encode_clo(alloc_clo(fhalt,0));")
; (append-line filepath "arg_buffer[0] = 0;")
(append-line filepath "arg_buffer[2] = fhalt_clo;")
(append-line filepath "\n// calling next procedure using a function pointer")
(append-line filepath "brouhaha_main_spec(nullptr, fhalt_clo);")
(append-line filepath "}\n")
;end of main function.
'cpp-emission-done!)