-
Notifications
You must be signed in to change notification settings - Fork 18
/
special-operators-cl.lisp
1423 lines (1388 loc) · 54.7 KB
/
special-operators-cl.lisp
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
;;;;------------------------------------------------------------------
;;;;
;;;; Copyright (C) 2000-2005,
;;;; Department of Computer Science, University of Tromso, Norway
;;;;
;;;; Filename: special-operators-cl.lisp
;;;; Description: Special operators in the COMMON-LISP package.
;;;; Author: Frode Vatvedt Fjeld <frodef@acm.org>
;;;; Created at: Fri Nov 24 16:31:11 2000
;;;; Distribution: See the accompanying file COPYING.
;;;;
;;;; $Id: special-operators-cl.lisp,v 1.55 2008-07-09 19:57:02 ffjeld Exp $
;;;;
;;;;------------------------------------------------------------------
(in-package movitz)
(define-special-operator progn (&all all &form form &top-level-p top-level-p &result-mode result-mode)
(compiler-call #'compile-implicit-progn
:form (cdr form)
:forward all))
(defun expand-to-operator (operator form env)
"Attempt to compiler-macroexpand FORM to having operator OPERATOR."
(if (and (listp form) (eq operator (first form)))
form
(multiple-value-bind (expansion expanded-p)
(like-compile-macroexpand-form form env)
(if expanded-p
(expand-to-operator operator expansion env)
nil))))
(defun parse-let-var-specs (let-var-specs)
"Given a list of LET variable specifiers (i.e. either VAR or (VAR INIT-FORM)),~
return a list on the canonical form (VAR INIT-FORM)."
(loop for var-spec in let-var-specs
if (symbolp var-spec)
collect `(,var-spec nil)
else if (and (listp var-spec)
(= 1 (length var-spec)))
collect `(,(first var-spec) nil)
else do (assert (and (listp var-spec)
(= 2 (length var-spec))))
and collect var-spec))
(define-special-operator let (&all all &form form &funobj funobj &env env &result-mode result-mode)
"An extent-nested let is this: (let ((foo ..) (bar (.. (let ((zot ..)) ..)))))
where zot is not in foo's scope, but _is_ in foo's extent."
(destructuring-bind (operator let-var-specs &body forms)
form
(declare (ignore operator))
(multiple-value-bind (body declarations)
(parse-declarations-and-body forms)
(if (and (null let-var-specs)
(null declarations))
(compiler-call #'compile-implicit-progn
:forward all
:form body)
(let* ((let-modifies nil)
(let-vars (parse-let-var-specs let-var-specs))
(local-env (make-local-movitz-environment env funobj
:type 'let-env
:declarations declarations))
(init-env #+ignore env
(make-instance 'movitz-environment
:uplink env
:funobj funobj
:extent-uplink local-env))
(stack-used 0)
(binding-var-codes
(loop for (var init-form) in let-vars
if (movitz-env-get var 'special nil local-env)
;; special... see losp/cl/run-time.lisp
collect
(list var
init-form
(append (if (= 0 (num-specials local-env)) ; first special? .. binding tail
`((:locally (:pushl (:edi (:edi-offset dynamic-env)))))
`((:pushl :esp)))
(compiler-call #'compile-form ; binding value
:with-stack-used (incf stack-used)
:env init-env
:defaults all
:form init-form
:modify-accumulate let-modifies
:result-mode :push)
`((:pushl :edi)) ; scratch
(compiler-call #'compile-form ; binding name
:with-stack-used (incf stack-used 2)
:env init-env
:defaults all
:form `(muerte.cl:quote ,var)
:result-mode :push)
(prog1 nil (incf stack-used)))
nil t)
and do (movitz-env-add-binding local-env (make-instance 'dynamic-binding
:name var))
and do (incf (num-specials local-env))
;; lexical...
else collect
(let ((binding (make-instance 'located-binding :name var)))
(movitz-env-add-binding local-env binding)
(compiler-values-bind (&code init-code &functional-p functional-p
&type type &returns init-register
&final-form final-form)
(compiler-call #'compile-form-unprotected
:result-mode binding
:env init-env
:extent local-env
:defaults all
:form init-form)
#+ignore
(compiler-call #'compile-form-to-register
:env init-env
:extent local-env
:defaults all
:form init-form
:modify-accumulate let-modifies)
(when (eq binding init-register)
(setf init-register nil))
;;; (warn "var ~S, type: ~S" var type)
;;; (warn "var ~S init: ~S.." var init-form)
;;; (warn "bind: ~S reg: ~S" binding init-register)
;;; (print-code 'init init-code)
(list var
init-form
init-code
functional-p
(let ((init-type (type-specifier-primary type)))
(assert init-type ()
"The init-form ~S yielded the empty primary type!" type)
init-type)
(case init-register
(:non-local-exit :edi)
(:multiple-values :eax)
(t init-register))
final-form))))))
(setf (stack-used local-env) stack-used)
(flet ((compile-body ()
(if (= 0 (num-specials local-env))
(compiler-call #'compile-implicit-progn
:defaults all
:form body
:env local-env)
(compiler-call #'compile-form
:result-mode (case result-mode
(:push :eax)
(:function :multiple-values)
(t result-mode))
:defaults all
:form `(muerte.cl:progn ,@body)
:modify-accumulate let-modifies
:env local-env))))
(compiler-values-bind (&all body-values &code body-code &returns body-returns)
(compile-body)
;; (print-code 'body body)
;; (print-code 'body-code body-code)
(let ((first-binding (movitz-binding (caar binding-var-codes) local-env nil)))
(cond
;; Is this (let ((#:foo <form>)) (setq bar #:foo)) ?
;; If so, make it into (setq bar <form>)
((and (= 1 (length binding-var-codes))
(typep first-binding 'lexical-binding)
(instruction-is (first body-code) :load-lexical)
(instruction-is (second body-code) :store-lexical)
(null (cddr body-code))
(eq first-binding ; same binding?
(second (first body-code)))
(eq (third (first body-code)) ; same register?
(third (second body-code))))
(let ((dest-binding (second (second body-code))))
(check-type dest-binding lexical-binding)
(compiler-call #'compile-form
:forward all
:extent local-env
:result-mode dest-binding
:form (second (first binding-var-codes)))))
#+ignore
((and (= 1 (length binding-var-codes))
(typep (movitz-binding (caar binding-var-codes) local-env nil)
'lexical-binding)
(member (movitz-binding (caar binding-var-codes) local-env nil)
(find-read-bindings (first body-code)))
(not (code-uses-binding-p (rest body-code) (second (first body-code))
:load t :store nil)))
(let ((tmp-binding (second (first body-code))))
(print-code 'body body-code)
(break "Yuhu: tmp ~S" tmp-binding)
))
(t (let ((code
(append
(loop
for ((var init-form init-code functional-p type init-register
final-form)
. rest-codes)
on binding-var-codes
as binding = (movitz-binding var local-env nil)
;; for bb in binding-var-codes
;; do (warn "bind: ~S" bb)
do (assert type)
(assert (not (binding-lended-p binding)))
appending
(cond
((and (typep binding 'located-binding)
(not (binding-lended-p binding))
(typep final-form 'lexical-binding)
(let ((target-binding final-form))
(and (typep target-binding 'lexical-binding)
(eq (binding-funobj binding)
(binding-funobj target-binding))
#+ignore
(sub-env-p (binding-env binding)
(binding-env target-binding))
(or (and (not (code-uses-binding-p body-code
binding
:load nil
:store t))
(not (code-uses-binding-p body-code
target-binding
:load nil
:store t)))
(and (= 1 (length body-code))
(eq :add (caar body-code)))
(and (>= 1 (length body-code))
(warn "short let body: ~S" body-code))
;; This is the best we can do now to determine
;; if target-binding is ever used again.
(and (eq result-mode :function)
(not (and (bindingp body-returns)
(binding-eql target-binding
body-returns)))
(not (code-uses-binding-p body-code
target-binding
:load t
:store t))
(notany (lambda (code)
(code-uses-binding-p (third code)
target-binding
:load t
:store t))
rest-codes))))))
;; replace read-only binding with the outer binding
(compiler-values-bind (&code new-init-code &final-form target
&type type)
(compiler-call #'compile-form-unprotected
:extent local-env
:form init-form
:result-mode :ignore
:env init-env
:defaults all)
(check-type target lexical-binding)
(change-class binding 'forwarding-binding
:target-binding target)
(let ((btype (if (multiple-value-call #'encoded-allp
(type-specifier-encode
(type-specifier-primary type)))
target
(type-specifier-primary type))))
#+ignore (warn "forwarding ~S -[~S]> ~S"
binding btype target)
(append new-init-code
`((:init-lexvar
,binding
:init-with-register ,target
:init-with-type ,btype))))))
((and (typep binding 'located-binding)
(type-specifier-singleton type)
(not (code-uses-binding-p body-code binding
:load nil :store t)))
;; replace read-only lexical binding with
;; side-effect-free form
#+ignore
(warn "Constant binding: ~S => ~S => ~S"
(binding-name binding)
init-form
(car (type-specifier-singleton type)))
(change-class binding 'constant-object-binding
:object (car (type-specifier-singleton type)))
(if functional-p
nil ; only inject code if it's got side-effects.
(compiler-call #'compile-form-unprotected
:extent local-env
:env init-env
:defaults all
:form init-form
:result-mode :ignore
:modify-accumulate let-modifies)))
((typep binding 'lexical-binding)
(let ((init (type-specifier-singleton
(type-specifier-primary type))))
(cond
((and init (eq *movitz-nil* (car init)))
(append (if functional-p
nil
(compiler-call #'compile-form-unprotected
:extent local-env
:env init-env
:defaults all
:form init-form
:result-mode :ignore
:modify-accumulate let-modifies))
`((:init-lexvar ,binding
:init-with-register :edi
:init-with-type null))))
((and (typep final-form 'lexical-binding)
(eq (binding-funobj final-form)
funobj))
(compiler-values-bind (&code new-init-code
&type new-type
&final-form new-binding)
(compiler-call #'compile-form-unprotected
:extent local-env
:env init-env
:defaults all
:form init-form
:result-mode :ignore
:modify-accumulate let-modifies)
(append (if functional-p
nil
new-init-code)
(let ((ptype (type-specifier-primary new-type)))
`((:init-lexvar ,binding
:init-with-register ,new-binding
:init-with-type ,ptype
))))))
((typep final-form 'constant-object-binding)
#+ignore
(warn "type: ~S or ~S" final-form
(type-specifier-primary type))
(append (if functional-p
nil
(compiler-call #'compile-form-unprotected
:extent local-env
:env init-env
:defaults all
:form init-form
:result-mode :ignore
:modify-accumulate let-modifies))
`((:init-lexvar
,binding
:init-with-register ,final-form
:init-with-type ,(type-specifier-primary type)
))))
(t ;; (warn "for ~S ~S ~S" binding init-register final-form)
(append init-code
`((:init-lexvar
,binding
:init-with-register ,init-register
:init-with-type ,(type-specifier-primary type))))))))
(t init-code)))
(when (plusp (num-specials local-env))
`((:locally (:call (:edi ,(bt:slot-offset 'movitz-run-time-context
'dynamic-variable-install))))
(:locally (:movl :esp (:edi (:edi-offset dynamic-env))))))
body-code
(when (and (plusp (num-specials local-env))
(not (eq :non-local-exit body-returns)))
#+ignore
(warn "let spec ret: ~S, want: ~S ~S"
body-returns result-mode let-var-specs)
`((:movl (:esp ,(+ -4 (* 16 (num-specials local-env)))) :edx)
(:locally (:call (:edi ,(bt:slot-offset 'movitz-run-time-context
'dynamic-variable-uninstall))))
(:locally (:movl :edx (:edi (:edi-offset dynamic-env))))
(:leal (:esp ,(* 16 (num-specials local-env))) :esp))))))
(compiler-values (body-values)
:returns body-returns
:producer (default-compiler-values-producer)
:functional-p (and (body-values :functional-p)
(every #'fourth binding-var-codes))
:modifies let-modifies
:code code))))))))))))
(define-special-operator symbol-macrolet (&all forward &form form &env env &funobj funobj)
(destructuring-bind (symbol-expansions &body declarations-and-body)
(cdr form)
(multiple-value-bind (body declarations)
(parse-declarations-and-body declarations-and-body)
(let ((local-env (make-local-movitz-environment
env funobj
:type 'operator-env
:declarations declarations)))
(loop for symbol-expansion in symbol-expansions
do (destructuring-bind (symbol expansion)
symbol-expansion
(movitz-env-add-binding local-env (make-instance 'symbol-macro-binding
:name symbol
:expander #'(lambda (form env)
(declare (ignore form env))
expansion)))))
(compiler-values-bind (&all body-values &code body-code)
(compiler-call #'compile-implicit-progn
:defaults forward
:form body
:env local-env
:top-level-p (forward :top-level-p))
(compiler-values (body-values)
:code body-code))))))
(define-special-operator macrolet (&all forward &form form &funobj funobj &env env)
(destructuring-bind (macrolet-specs &body declarations-and-body)
(cdr form)
(multiple-value-bind (body declarations)
(parse-declarations-and-body declarations-and-body)
(let ((local-env (make-local-movitz-environment env funobj
:type 'operator-env
:declarations declarations)))
(loop for (name local-lambda-list . local-body-decl-doc) in macrolet-specs
as cl-local-lambda-list = (translate-program local-lambda-list :muerte.cl :cl)
as (local-body local-declarations) =
(multiple-value-list (parse-docstring-declarations-and-body local-body-decl-doc))
as cl-local-body = (translate-program local-body :muerte.cl :cl)
as cl-local-declarations = (translate-program local-declarations :muerte.cl :cl)
as expander = `(lambda (form env)
(declare (ignorable env))
(destructuring-bind ,cl-local-lambda-list
(translate-program (rest form) :muerte.cl :cl)
(declare ,@cl-local-declarations)
(translate-program (block ,name (let () ,@cl-local-body))
:cl :muerte.cl)))
do (movitz-env-add-binding
local-env
(make-instance 'macro-binding
:name name
:expander (movitz-macro-expander-make-function expander
:name name
:type :macrolet))))
(compiler-values-bind (&all body-values &code body-code)
(compiler-call #'compile-implicit-progn
:defaults forward
:form body
:env local-env
:top-level-p (forward :top-level-p))
(compiler-values (body-values)
:code body-code))))))
(define-special-operator multiple-value-prog1 (&all all &form form &result-mode result-mode &env env)
(destructuring-bind (first-form &rest rest-forms)
(cdr form)
(compiler-values-bind (&code form1-code &returns form1-returns &type type)
(compiler-call #'compile-form-unprotected
:defaults all
:result-mode (case (result-mode-type result-mode)
((:boolean-branch-on-true :boolean-branch-on-false)
:eax)
(t result-mode))
:form first-form)
(compiler-call #'special-operator-with-cloak
;; :with-stack-used t
:forward all
:form `(muerte::with-cloak (,form1-returns ,form1-code t ,type)
,@rest-forms)))))
(define-special-operator multiple-value-call (&all all &form form &funobj funobj)
(destructuring-bind (function-form &rest subforms)
(cdr form)
(let* ((local-env (make-instance 'let-env
:uplink (all :env)
:funobj funobj
:stack-used t))
(numargs-binding (movitz-env-add-binding local-env
(make-instance 'located-binding
:name (gensym "m-v-numargs-"))))
(arg-code (loop for subform in subforms
collecting
(compiler-values-bind (&code subform-code &returns subform-returns)
(compiler-call #'compile-form-unprotected
:defaults all
:env local-env
:form subform
:result-mode :multiple-values)
(case subform-returns
(:multiple-values
`(:multiple
,@subform-code
,@(make-compiled-push-current-values)
(:load-lexical ,numargs-binding :eax)
(:addl :ecx :eax)
(:store-lexical ,numargs-binding :eax :type fixnum)))
(t (list :single ; marker, used below
subform))))))
(number-of-multiple-value-subforms (count :multiple arg-code :key #'car))
(number-of-single-value-subforms (count :single arg-code :key #'car)))
(cond
((= 0 number-of-multiple-value-subforms)
(compiler-call #'compile-form
:forward all
:form `(muerte.cl::funcall ,function-form ,@subforms)))
(t (compiler-values ()
:returns :multiple-values
:code (append `((:load-constant ,(1+ number-of-single-value-subforms) :eax)
(:store-lexical ,numargs-binding :eax :type fixnum))
(compiler-call #'compile-form
:defaults all
:env local-env
:form function-form
:result-mode :push)
(loop for ac in arg-code
append (ecase (car ac)
(:single
(compiler-call #'compile-form
:defaults all
:env local-env
:form (second ac)
:result-mode :push))
(:multiple
(cdr ac))))
`((:load-lexical ,numargs-binding :ecx)
;; (:store-lexical ,numargs-binding :ecx :type t)
(:movl (:esp (:ecx 1) -4) :eax)
(:movl (:esp (:ecx 1) -8) :ebx)
(:shrl ,+movitz-fixnum-shift+ :ecx)
(:load-constant muerte.cl::funcall :edx)
(:movl (:edx ,(slot-offset 'movitz-symbol 'function-value))
:esi) ; load new funobj from symbol into ESI
(:call (:esi ,(slot-offset 'movitz-funobj 'code-vector)))
(:load-lexical ,numargs-binding :edx)
;; Use LEA so as not to modify CF
(:leal (:esp :edx) :esp)))))))))
(define-special-operator multiple-value-bind (&all forward &form form &env env &funobj funobj
&result-mode result-mode)
(destructuring-bind (variables values-form &body body-and-declarations)
(cdr form)
(multiple-value-bind (body declarations)
(parse-declarations-and-body body-and-declarations)
(compiler-values-bind (&code values-code &returns values-returns &type values-type)
(compiler-call #'compile-form
:defaults forward
:form values-form
:result-mode :multiple-values #+ignore (list :values (length variables)))
;;; (warn "mv-code: ~W ~W => ~W ~W" values-form (list :values (length variables)) values-returns (last values-code 10))
(let* ((local-env (make-local-movitz-environment
env funobj
:type 'let-env
:declarations declarations))
(lexical-bindings
(loop for variable in variables
as new-binding = (make-instance 'located-binding
:name variable)
do (check-type variable symbol)
collect new-binding
do (cond
((movitz-env-get variable 'special nil env)
(let* ((shadowed-variable (gensym (format nil "m-v-bind-shadowed-~A"
variable))))
(movitz-env-add-binding local-env new-binding shadowed-variable)
(push (list variable shadowed-variable)
(special-variable-shadows local-env))))
(t (movitz-env-add-binding local-env new-binding)))))
(init-var-code
(case (first (operands values-returns))
(t (append
(make-result-and-returns-glue :multiple-values values-returns)
(case (length lexical-bindings)
(0 nil)
(1 `((:init-lexvar ,(first lexical-bindings)
:protect-carry nil
:protect-registers '(:eax))
(:store-lexical ,(first lexical-bindings) :eax
:type ,(type-specifier-primary values-type))))
(2 (let ((done-label (gensym "m-v-bind-done-")))
`((:init-lexvar ,(first lexical-bindings)
:protect-carry t
:protect-registers (:eax :ebx))
(:store-lexical ,(first lexical-bindings) :eax
:type ,(type-specifier-primary values-type)
:protect-registers (:ebx))
(:init-lexvar ,(second lexical-bindings)
:protect-carry t
:protect-registers (:ebx))
(:store-lexical ,(second lexical-bindings) :edi
:type null)
(:jnc ',done-label)
(:cmpl 1 :ecx)
(:jbe ',done-label)
(:store-lexical ,(second lexical-bindings) :ebx
:type ,(type-specifier-nth-value 1 values-type))
,done-label)))
(t (with-labels (m-v-bind (ecx-ok-label))
`((:jc ',ecx-ok-label)
,@(make-immediate-move 1 :ecx) ; CF=0 means arg-count=1
,ecx-ok-label
,@(loop for binding in lexical-bindings as pos upfrom 0
as skip-label = (gensym "m-v-bind-skip-")
as type = (type-specifier-nth-value pos values-type)
append
(case pos
(0 `((:init-lexvar ,binding
:protect-registers (:eax :ebx :ecx))
(:store-lexical ,binding :eax :type ,type
:protect-registers (:eax :ebx :ecx))))
(1 `((:init-lexvar ,binding
:protect-registers (:ebx :ecx))
(:store-lexical ,binding :edi :type null
:protect-registers (:ecx))
(:cmpl 1 :ecx)
(:jbe ',skip-label)
(:store-lexical ,binding :ebx :type ,type
:protect-registers (:ecx))
,skip-label))
(t (if *compiler-use-cmov-p*
`((:init-lexvar ,binding :protect-registers '(:ecx))
(:movl :edi :eax)
(:cmpl ,pos :ecx)
(:locally (:cmova (:edi (:edi-offset values
,(* 4 (- pos 2))))
:eax))
(:store-lexical ,binding :eax :type ,type
:protect-registers (:eax)))
`((:init-lexvar ,binding :protect-registers '(:ecx))
(:movl :edi :eax)
(:cmpl ,pos :ecx)
(:jbe ',skip-label)
(:locally (:movl (:edi (:edi-offset values
,(* 4 (- pos 2))))
:eax))
,skip-label
(:store-lexical ,binding :eax :type ,type
:protect-registers (:ecx))))))))))))))))
(compiler-values-bind (&code body-code &returns body-returns-mode)
(compiler-call #'compile-form-unprotected
:defaults forward
:form `(muerte.cl:let ,(special-variable-shadows local-env) ,@body)
:env local-env)
(compiler-values ()
:returns body-returns-mode
:code (append values-code
init-var-code
body-code))))))))
(define-special-operator setq (&all forward &form form &env env &funobj funobj &result-mode result-mode)
(let ((pairs (cdr form)))
(unless (evenp (length pairs))
(error "Odd list of SETQ pairs: ~S" pairs))
(let* ((last-returns :nothing)
(bindings ())
(code (loop
for (var value-form) on pairs by #'cddr
as binding = (movitz-binding var env)
as pos downfrom (- (length pairs) 2) by 2
as sub-result-mode = (if (zerop pos) result-mode :ignore)
do (pushnew binding bindings)
append
(typecase binding
(symbol-macro-binding
(compiler-values-bind (&code code &returns returns)
(compiler-call #'compile-form-unprotected
:defaults forward
:result-mode sub-result-mode
:form `(muerte.cl:setf ,var ,value-form))
(setf last-returns returns)
code))
(lexical-binding
(case (operator sub-result-mode)
(t ;; :ignore
;; (setf last-returns :nothing)
(compiler-values-bind (&code sub-code &returns sub-returns)
(compiler-call #'compile-form
:defaults forward
:form value-form
:result-mode binding)
(setf last-returns sub-returns)
;; (warn "sub-returns: ~S" sub-returns)
sub-code))
#+ignore
(t (let ((register (accept-register-mode sub-result-mode)))
(compiler-values-bind (&code code &type type)
(compiler-call #'compile-form
:defaults forward
:form value-form
:result-mode register)
(setf last-returns register)
(append code
`((:store-lexical ,binding ,register
:type ,(type-specifier-primary type)))))))))
(t (unless (movitz-env-get var 'special nil env)
(warn "Assuming destination variable ~S with binding ~S is special."
var binding))
(setf last-returns :ebx)
(append (compiler-call #'compile-form
:defaults forward
:form value-form
:result-mode :ebx)
`((:load-constant ,var :eax)
(:locally (:call (:edi (:edi-offset dynamic-variable-store)))))))))))
(compiler-values ()
:code code
:returns last-returns
:functional-p nil))))
(define-special-operator tagbody (&all forward &funobj funobj &form form &env env)
(let* ((save-esp-variable (gensym "tagbody-save-esp"))
(lexical-catch-tag-variable (gensym "tagbody-lexical-catch-tag-"))
(label-set-name (gensym "label-set-"))
(tagbody-env (make-instance 'tagbody-env
:uplink env
:funobj funobj
:save-esp-variable save-esp-variable
:lexical-catch-tag-variable lexical-catch-tag-variable
:exit-result-mode :ignore))
(save-esp-binding (make-instance 'located-binding
:name save-esp-variable))
(lexical-catch-tag-binding (make-instance 'located-binding
:name lexical-catch-tag-variable)))
(movitz-env-add-binding tagbody-env save-esp-binding)
(movitz-env-add-binding tagbody-env lexical-catch-tag-binding)
(movitz-env-load-declarations `((muerte.cl::ignorable ,save-esp-variable ,lexical-catch-tag-variable))
tagbody-env nil)
;; First generate an assembly-level label for each tag.
(let* ((label-set (loop with label-id = 0
for tag-or-statement in (cdr form)
as label = (when (or (symbolp tag-or-statement)
(integerp tag-or-statement))
(gensym (format nil "go-tag-~A-" tag-or-statement)))
when label
do (setf (movitz-env-get tag-or-statement 'go-tag nil tagbody-env)
label)
(setf (movitz-env-get tag-or-statement 'go-tag-label-id nil tagbody-env)
(post-incf label-id))
and collect label))
(tagbody-codes
(loop for tag-or-statement in (cdr form)
if (or (symbolp tag-or-statement) ; Tagbody tags are "compiled" into..
(integerp tag-or-statement)) ; ..their assembly-level labels.
collect (movitz-env-get tag-or-statement 'go-tag nil tagbody-env)
else collect
(compiler-call #'compile-form
:defaults forward
:form tag-or-statement
:env tagbody-env
:result-mode :ignore))))
(let* ((unlexical-target-p (some (lambda (code)
(when (listp code)
(code-uses-binding-p code save-esp-binding)))
tagbody-codes))
(maybe-store-esp-code
(when (or unlexical-target-p
(some (lambda (code)
(when (listp code)
(operators-present-in-code-p code '(:lexical-control-transfer) nil
:test (lambda (x)
(eq tagbody-env (fifth x))))))
tagbody-codes))
`((:init-lexvar ,save-esp-binding
:init-with-register :esp
:init-with-type t)))))
(if (not unlexical-target-p)
(compiler-values ()
:code (append maybe-store-esp-code
(loop for code in tagbody-codes
if (listp code)
append code
else append (list code)))
:returns :nothing)
(let ((code (append `((:declare-label-set ,label-set-name ,label-set)
;; catcher
(:locally (:pushl (:edi (:edi-offset dynamic-env))))
(:pushl ',label-set-name)
(:locally (:pushl (:edi (:edi-offset unbound-function))))
(:pushl :ebp)
(:locally (:movl :esp (:edi (:edi-offset dynamic-env)))))
maybe-store-esp-code
(loop for code in tagbody-codes
if (listp code)
append code
else append (list code '(:movl (:esp) :ebp)))
`((:movl (:esp 12) :edx)
(:locally (:movl :edx (:edi (:edi-offset dynamic-env))))
(:leal (:esp 16) :esp))
)))
(setf (num-specials tagbody-env) 1
(stack-used tagbody-env) 4)
(compiler-values ()
:code code
:returns :nothing)))))))
(define-special-operator go (&all all &form form &env env &funobj funobj)
(destructuring-bind (operator tag)
form
(declare (ignore operator))
(multiple-value-bind (label tagbody-env)
(movitz-env-get tag 'go-tag nil env)
(assert (and label tagbody-env) ()
"Go-tag ~W is not visible." tag)
(multiple-value-bind (stack-distance num-dynamic-slots unwind-protects)
(stack-delta env tagbody-env)
(declare (ignore stack-distance))
(if (and (eq funobj (movitz-environment-funobj tagbody-env))
;; A well-known number of dynamic-slots?
(not (eq t num-dynamic-slots))
;; any unwind-protects between here and there?
(null unwind-protects))
(compiler-values ()
:returns :non-local-exit
:code `((:lexical-control-transfer nil :nothing ,env ,tagbody-env ,label)))
;; Perform a lexical "throw" to the tag. Just like a regular (dynamic) throw.
(let ((save-esp-binding (movitz-binding (save-esp-variable tagbody-env) env))
(label-id (movitz-env-get tag 'go-tag-label-id nil tagbody-env nil)))
(assert label-id)
(compiler-values ()
:returns :non-local-exit
:code `((:load-lexical ,save-esp-binding :edx)
(:movl :edx :eax)
,@(when (plusp label-id)
;; The target jumper points to the tagbody's label-set.
;; Now, install correct jumper within tagbody as target.
`((:addl ,(* 4 label-id) (:edx 8))))
(:locally (:call (:edi (:edi-offset dynamic-unwind-next))))
;; have next-continuation in EAX, final-continuation in EDX
(:locally (:movl :edx (:edi (:edi-offset raw-scratch0)))) ; final continuation
(:locally (:movl :eax (:edi (:edi-offset dynamic-env)))) ; new dynamic-env
(:movl :eax :edx)
(:clc)
(:locally (:call (:edi (:edi-offset dynamic-jump-next))))))))))))
;;; (:locally (:movl :edx (:edi (:edi-offset dynamic-env)))) ; exit to next-env
;;; (:movl :edx :esp) ; enter non-local jump stack mode.
;;; (:movl (:esp) :edx) ; target stack-frame EBP
;;; (:movl (:edx -4) :esi) ; get target funobj into ESI
;;; (:movl (:esp 8) :edx) ; target jumper number
;;; (:jmp (:esi :edx ,(slot-offset 'movitz-funobj 'constant0)))))))))))
(define-special-operator block (&all forward &funobj funobj &form form &env env
&result-mode result-mode)
(destructuring-bind (block-name &body body)
(cdr form)
(let* ((exit-block-label (gensym (format nil "exit-block-~A-" block-name)))
(save-esp-variable (gensym (format nil "block-~A-save-esp-" block-name)))
(lexical-catch-tag-variable (gensym (format nil "block-~A-lexical-catch-tag-" block-name)))
(block-result-mode (case result-mode
((:eax :eax :multiple-values :function :ebx :ecx :ignore)
result-mode)
(t :eax)))
(block-returns-mode (case (result-mode-type block-result-mode)
(:function :multiple-values)
(:ignore :nothing)
((:boolean-branch-on-true :boolean-branch-on-false) :eax)
(t block-result-mode)))
(block-env (make-instance 'lexical-exit-point-env
:uplink env
:funobj funobj
:save-esp-variable save-esp-variable
:lexical-catch-tag-variable lexical-catch-tag-variable
:exit-label exit-block-label
:exit-result-mode block-result-mode))
(save-esp-binding (make-instance 'located-binding
:name save-esp-variable)))
(movitz-env-add-binding block-env save-esp-binding)
(movitz-env-load-declarations `((muerte.cl::ignorable ,save-esp-variable))
block-env nil)
(setf (movitz-env-get block-name :block-name nil block-env)
block-env)
(compiler-values-bind (&code block-code &functional-p block-no-side-effects-p)
(compiler-call #'compile-form
:defaults forward
:result-mode (case block-result-mode
(:function :multiple-values) ; must restore stack
(t block-result-mode))
:form `(muerte.cl:progn ,@body)
:env block-env)
(let ((label-set-name (gensym "block-label-set-"))
(maybe-store-esp-code
(when (and #+ignore (not (eq block-result-mode :function))
(operators-present-in-code-p block-code '(:lexical-control-transfer) nil
:test (lambda (x) (eq block-env (fifth x)))))
`((:init-lexvar ,save-esp-binding
:init-with-register :esp
:init-with-type t)))))
(if (not (code-uses-binding-p block-code save-esp-binding))
(compiler-values ()
:code (append maybe-store-esp-code
block-code
(list exit-block-label))
:returns block-returns-mode
:functional-p block-no-side-effects-p)
(multiple-value-bind (new-code new-returns)
(make-result-and-returns-glue :multiple-values block-returns-mode block-code)
(assert (eq :multiple-values new-returns))
(incf (stack-used block-env) 4)
(setf (num-specials block-env) 1) ; block-env now has one dynamic slot
(compiler-values ()
:code (append `((:declare-label-set ,label-set-name (,exit-block-label))
;; catcher
(:locally (:pushl (:edi (:edi-offset dynamic-env))))
(:pushl ',label-set-name)
(:locally (:pushl (:edi (:edi-offset unbound-function))))
(:pushl :ebp)
(:locally (:movl :esp (:edi (:edi-offset dynamic-env)))))
`((:init-lexvar ,save-esp-binding
:init-with-register :esp
:init-with-type t))
new-code
;; wrapped-code
`(,exit-block-label
(:movl (:esp 0) :ebp)
(:movl (:esp 12) :edx)
(:locally (:movl :edx (:edi (:edi-offset dynamic-env))))
(:leal (:esp 16) :esp)))
:returns :multiple-values
:functional-p block-no-side-effects-p))))))))
(define-special-operator return-from (&all all &form form &env env &funobj funobj)
(destructuring-bind (block-name &optional result-form)
(cdr form)
(let ((block-env (movitz-env-get block-name :block-name nil env)))
(assert block-env (block-name)
"Block-name not found for return-from: ~S." block-name)
(multiple-value-bind (stack-distance num-dynamic-slots unwind-protects)
(stack-delta env block-env)
(declare (ignore stack-distance))
(cond
((and (eq funobj (movitz-environment-funobj block-env))
(not (eq t num-dynamic-slots))
(null unwind-protects))
(compiler-values-bind (&code return-code &returns return-mode)
(compiler-call #'compile-form
:forward all
:form result-form
:result-mode (exit-result-mode block-env))
(compiler-values ()
:returns :non-local-exit
:code (append return-code
`((:lexical-control-transfer nil ,return-mode ,env ,block-env))))))
((not (and (eq funobj (movitz-environment-funobj block-env))
(not (eq t num-dynamic-slots))
(null unwind-protects)))
(compiler-call #'compile-form-unprotected
:forward all
:form `(muerte::exact-throw ,(save-esp-variable block-env)
,result-form))))))))
(define-special-operator require (&form form)
(let ((*require-dependency-chain*
(and (boundp '*require-dependency-chain*)
(symbol-value '*require-dependency-chain*))))
(declare (special *require-dependency-chain*))
(destructuring-bind (module-name &optional path-spec)
(cdr form)
(declare (ignore path-spec))
(push module-name *require-dependency-chain*)
(unless (member module-name (image-movitz-modules *image*))
(when (member module-name (cdr *require-dependency-chain*))
(error "Circular Movitz module dependency chain: ~S"
(reverse (subseq *require-dependency-chain* 0
(1+ (position module-name *require-dependency-chain* :start 1))))))
(let* ((require-path (movitz-module-path form)))
(movitz-compile-file-internal require-path)
(unless (member module-name (image-movitz-modules *image*))
(error "Compiling file ~S didn't provide module ~S."
require-path module-name))))))
(compiler-values ()))
(define-special-operator provide (&form form &funobj funobj &top-level-p top-level-p)
(unless top-level-p
(warn "Provide form not at top-level."))
(destructuring-bind (module-name &key load-priority)
(cdr form)
(declare (special *default-load-priority*))
(pushnew module-name (image-movitz-modules *image*))
(when load-priority
(setf *default-load-priority* load-priority))
(let ((new-priority *default-load-priority*))
(let ((old-tf (member module-name (image-load-time-funobjs *image*) :key #'second)))
(cond
((and new-priority old-tf)
(setf (car old-tf) (list funobj module-name new-priority)))
((and new-priority (not old-tf))
(push (list funobj module-name new-priority)
(image-load-time-funobjs *image*)))
(old-tf
(setf (car old-tf) (list funobj module-name (third (car old-tf)))))
(t (warn "No existing or provided load-time priority for module ~S, will not be loaded!"
module-name))))))
(compiler-values ()))
(define-special-operator eval-when (&all forward &form form &top-level-p top-level-p)
(destructuring-bind (situations &body body)
(cdr form)
(multiple-value-prog1
(if (or (member :execute situations)
(and (member :load-toplevel situations)
top-level-p))
(compiler-call #'compile-implicit-progn
:defaults forward
:top-level-p top-level-p
:form body)
(compiler-values ()))
(when (and (member :compile-toplevel situations)
top-level-p)
(with-compilation-unit ()
(dolist (toplevel-form (translate-program body :muerte.cl :cl
:when :eval
:remove-double-quotes-p t))