-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathbytecode_compiler.cc
2851 lines (2673 loc) · 112 KB
/
bytecode_compiler.cc
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
#include <clasp/core/bytecode_compiler.h>
#include <clasp/core/evaluator.h> // extract_decl...
#include <clasp/core/sysprop.h> // core__get_sysprop
#include <clasp/core/lambdaListHandler.h> // lambda list parsing
#include <clasp/core/designators.h> // calledFunctionDesignator
#include <clasp/core/primitives.h> // gensym, function_block_name
#include <clasp/core/sourceFileInfo.h> // source info stuff
#include <clasp/llvmo/llvmoPackage.h>
#include <clasp/core/bytecode.h>
#include <algorithm> // max
#define VM_CODES
#include <virtualMachine.h>
#undef VM_CODES
namespace comp {
using namespace core;
T_sp Lexenv_O::variableInfo(T_sp varname) {
T_sp vars = this->vars();
if (vars.nilp())
return vars;
else {
T_sp pair = core__alist_assoc_eq(gc::As_assert<Cons_sp>(vars), varname);
if (pair.nilp())
return pair;
else
return oCdr(pair);
}
}
T_sp Lexenv_O::lookupSymbolMacro(T_sp sname) {
T_sp info = this->variableInfo(sname);
if (gc::IsA<SymbolMacroVarInfo_sp>(info))
return gc::As_unsafe<SymbolMacroVarInfo_sp>(info)->expander();
else
return nil<T_O>();
}
T_sp Lexenv_O::functionInfo(T_sp funname) {
T_sp funs = this->funs();
if (funs.nilp())
return funs;
else {
T_sp pair = core__alist_assoc_equal(gc::As_assert<Cons_sp>(funs), funname);
if (pair.nilp())
return pair;
else
return oCdr(pair);
}
}
T_sp Lexenv_O::lookupMacro(T_sp macroname) {
T_sp info = this->functionInfo(macroname);
if (gc::IsA<GlobalMacroInfo_sp>(info))
return gc::As_unsafe<GlobalMacroInfo_sp>(info)->expander();
else if (gc::IsA<LocalMacroInfo_sp>(info))
return gc::As_unsafe<LocalMacroInfo_sp>(info)->expander();
else if (gc::IsA<LocalFunInfo_sp>(info) || gc::IsA<GlobalFunInfo_sp>(info))
return nil<T_O>(); // not a macro, e.g. shadowed
// no info
else
return info;
}
T_sp Lexenv_O::blockInfo(T_sp blockname) {
T_sp blocks = this->blocks();
if (blocks.nilp())
return blocks;
else {
T_sp pair = core__alist_assoc_equal(gc::As_assert<Cons_sp>(blocks), blockname);
if (pair.nilp())
return pair;
else
return oCdr(pair);
}
}
T_sp Lexenv_O::tagInfo(T_sp tagname) {
T_sp tags = this->tags();
if (tags.nilp())
return tags;
else {
T_sp pair = core__alist_assoc_equal(gc::As_assert<Cons_sp>(tags), tagname);
if (pair.nilp())
return pair;
else
return oCdr(pair);
}
}
Lexenv_sp Lexenv_O::bind_vars(List_sp vars, const Context ctxt) {
if (vars.nilp())
return this->asSmartPtr();
size_t frame_start = this->frameEnd();
size_t frame_end = frame_start + vars.unsafe_cons()->length();
Cfunction_sp cf = ctxt.cfunction();
cf->setNlocals(std::max(frame_end, cf->nlocals()));
size_t idx = frame_start;
List_sp new_vars = this->vars();
for (auto cur : vars) {
Symbol_sp var = gc::As<Symbol_sp>(oCar(cur));
if (var->getReadOnly())
SIMPLE_ERROR("Cannot bind constant value {}!", _rep_(var));
auto info = LexicalVarInfo_O::make(idx++, cf);
Cons_sp pair = Cons_O::create(var, info);
new_vars = Cons_O::create(pair, new_vars);
}
return this->sub_vars(new_vars, frame_end);
}
// Save a cons and append call in a common case.
Lexenv_sp Lexenv_O::bind1var(Symbol_sp var, const Context ctxt) {
size_t frame_start = this->frameEnd();
size_t frame_end = frame_start + 1;
Cfunction_sp cf = ctxt.cfunction();
cf->setNlocals(std::max(frame_end, cf->nlocals()));
auto info = LexicalVarInfo_O::make(frame_start, cf);
Cons_sp pair = Cons_O::create(var, info);
Cons_sp new_vars = Cons_O::create(pair, this->vars());
return this->sub_vars(new_vars, frame_end);
}
Lexenv_sp Lexenv_O::bind_funs(List_sp funs, const Context ctxt) {
if (funs.nilp())
return this->asSmartPtr();
size_t frame_start = this->frameEnd();
size_t frame_end = frame_start + funs.unsafe_cons()->length();
Cfunction_sp cf = ctxt.cfunction();
cf->setNlocals(std::max(frame_end, cf->nlocals()));
size_t idx = frame_start;
List_sp new_funs = this->funs();
for (auto cur : funs) {
T_sp name = oCar(cur);
auto info = LocalFunInfo_O::make(idx++, cf);
Cons_sp pair = Cons_O::create(name, info);
new_funs = Cons_O::create(pair, new_funs);
}
return this->sub_funs(new_funs, frame_end);
}
Lexenv_sp Lexenv_O::bind_block(T_sp name, Label_sp exit, const Context ctxt) {
size_t frame_start = this->frameEnd();
Cfunction_sp cf = ctxt.cfunction();
cf->setNlocals(std::max(frame_start + 1, cf->nlocals()));
BlockInfo_sp binfo = BlockInfo_O::make(frame_start, ctxt.cfunction(), exit, ctxt.receiving());
return this->sub_block(Cons_O::create(Cons_O::create(name, binfo), this->blocks()));
}
Lexenv_sp Lexenv_O::bind_tags(List_sp tags, LexicalInfo_sp dynenv, const Context ctxt) {
Cfunction_sp cf = ctxt.cfunction();
cf->setNlocals(std::max(this->frameEnd() + 1, cf->nlocals()));
if (tags.nilp())
return this->asSmartPtr();
List_sp new_tags = this->tags();
for (auto cur : tags) {
T_sp tag = oCar(cur);
TagInfo_sp info = TagInfo_O::make(dynenv, Label_O::make());
Cons_sp pair = Cons_O::create(tag, info);
new_tags = Cons_O::create(pair, new_tags);
}
return this->sub_tags(new_tags);
}
Lexenv_sp Lexenv_O::add_specials(List_sp vars) {
if (vars.nilp())
return this->asSmartPtr();
List_sp new_vars = this->vars();
for (auto cur : vars) {
Symbol_sp var = gc::As<Symbol_sp>(oCar(cur));
if (this->lookupSymbolMacro(var).notnilp())
SIMPLE_PROGRAM_ERROR("A symbol macro was declared SPECIAL:~%~s", var);
auto info = SpecialVarInfo_O::make(var->specialP());
Cons_sp pair = Cons_O::create(var, info);
new_vars = Cons_O::create(pair, new_vars);
}
return this->sub_vars(new_vars, this->frameEnd());
}
static List_sp scrub_decls(List_sp decls) {
// Remove SPECIAL declarations since those are already handled.
ql::list r;
for (auto cur : decls) {
T_sp decl = oCar(cur);
if (!gc::IsA<Cons_sp>(decl) || oCar(decl) != cl::_sym_special)
r << decl;
}
return r.cons();
}
Lexenv_sp Lexenv_O::add_decls(List_sp decls) {
decls = scrub_decls(decls);
if (decls.nilp())
return this->asSmartPtr();
else
return this->sub_decls(Cons_O::append(decls, this->decls()));
}
Lexenv_sp Lexenv_O::macroexpansion_environment() {
ql::list new_vars;
for (auto cur : (List_sp)(this->vars())) {
T_sp pair = oCar(cur);
T_sp info = oCdr(pair);
if (gc::IsA<ConstantVarInfo_sp>(info) || gc::IsA<SymbolMacroVarInfo_sp>(info))
new_vars << pair;
}
ql::list new_funs;
for (auto cur : (List_sp)(this->funs())) {
T_sp pair = oCar(cur);
T_sp info = oCdr(pair);
if (gc::IsA<GlobalMacroInfo_sp>(info) || gc::IsA<LocalMacroInfo_sp>(info))
new_funs << pair;
}
return Lexenv_O::make(new_vars.cons(), nil<T_O>(), nil<T_O>(), new_funs.cons(), this->decls(), 0);
}
CL_DEFUN Lexenv_sp make_null_lexical_environment() {
return Lexenv_O::make(nil<T_O>(), nil<T_O>(), nil<T_O>(), nil<T_O>(), nil<T_O>(), 0);
}
void assemble(const Context context, uint8_t opcode, List_sp operands) {
Cfunction_sp func = context.cfunction();
ComplexVector_byte8_t_sp bytecode = func->bytecode();
bytecode->vectorPushExtend(opcode);
for (auto cur : operands) {
bytecode->vectorPushExtend(clasp_to_integral<uint8_t>(oCar(cur)));
}
}
CL_LAMBDA(code position &rest values);
CL_DEFUN void assemble_into(SimpleVector_byte8_t_sp code, size_t position, List_sp values) {
for (auto cur : values)
(*code)[position++] = clasp_to_integral<uint8_t>(oCar(cur));
}
void assemble_maybe_long(const Context context, uint8_t opcode, List_sp operands) {
Cfunction_sp func = context.cfunction();
ComplexVector_byte8_t_sp bytecode = func->bytecode();
// Check for long operands. Also signal an error if something''s over 16 bits.
bool longp = false;
for (auto cur : operands) {
if (clasp_to_integral<uint16_t>(oCar(cur)) > 255) {
longp = true;
break;
}
}
if (longp) {
bytecode->vectorPushExtend(vm_long);
bytecode->vectorPushExtend(opcode);
for (auto cur : operands) {
uint16_t operand = clasp_to_integral<uint16_t>(oCar(cur));
bytecode->vectorPushExtend(operand & 0xff); // low
bytecode->vectorPushExtend(operand >> 8); // high
}
} else { // normal/short
bytecode->vectorPushExtend(opcode);
for (auto cur : operands)
bytecode->vectorPushExtend(clasp_to_integral<uint8_t>(oCar(cur)));
}
}
CL_DEFUN T_sp var_info(Symbol_sp sym, Lexenv_sp env) {
// Local?
T_sp info = env->variableInfo(sym);
if (info.notnilp())
return info;
// Constant?
// (Constants are also specialP, so we have to check constancy first.)
if (cl__keywordp(sym) || sym->getReadOnly())
return ConstantVarInfo_O::make(sym->symbolValue());
// Globally special?
if (sym->specialP())
return SpecialVarInfo_O::make(true);
// Global symbol macro?
T_mv symmac = core__get_sysprop(sym, ext::_sym_symbolMacro);
MultipleValues& mvn = core::lisp_multipleValues();
if (gc::As_unsafe<T_sp>(mvn.valueGet(1, symmac.number_of_values())).notnilp()) {
T_sp symmac0 = symmac;
Function_sp fsymmac = gc::As_assert<Function_sp>(symmac0);
return SymbolMacroVarInfo_O::make(fsymmac);
}
// Unknown.
return nil<T_O>();
}
// Like the above, but returns a std::variant. Good when you don't need
// to cons up info objects.
VarInfoV var_info_v(Symbol_sp sym, Lexenv_sp env) {
T_sp info = env->variableInfo(sym);
if (gc::IsA<LexicalVarInfo_sp>(info)) // in_place_type_t?
return VarInfoV(LexicalVarInfoV(gc::As_unsafe<LexicalVarInfo_sp>(info)));
else if (gc::IsA<SpecialVarInfo_sp>(info))
return VarInfoV(SpecialVarInfoV(gc::As_unsafe<SpecialVarInfo_sp>(info)));
else if (gc::IsA<SymbolMacroVarInfo_sp>(info))
return VarInfoV(SymbolMacroVarInfoV(gc::As_unsafe<SymbolMacroVarInfo_sp>(info)));
ASSERT(info.nilp());
// Constant?
if (cl__keywordp(sym) || sym->getReadOnly())
return VarInfoV(ConstantVarInfoV(sym->symbolValue()));
// Globally special?
if (sym->specialP())
return VarInfoV(SpecialVarInfoV(true));
// Global symbol macro?
T_mv symmac = core__get_sysprop(sym, ext::_sym_symbolMacro);
MultipleValues& mvn = core::lisp_multipleValues();
if (gc::As_unsafe<T_sp>(mvn.valueGet(1, symmac.number_of_values())).notnilp()) {
T_sp symmac0 = symmac;
Function_sp fsymmac = gc::As_assert<Function_sp>(symmac0);
return VarInfoV(SymbolMacroVarInfoV(fsymmac));
}
// Unknown.
return VarInfoV(NoVarInfoV());
}
CL_DEFUN T_sp fun_info(T_sp name, Lexenv_sp env) {
// Local?
T_sp info = env->functionInfo(name);
if (info.notnilp())
return info;
// Split into setf and not versions.
if (name.consp()) {
List_sp cname = name;
if (oCar(cname) == cl::_sym_setf) {
// take care of (setf . bar) or (setf bar foo) or (setf bar .foo)
// so don't go directly for the cadr
T_sp dname = oCdr(cname);
if (dname.consp()) {
T_sp sss = CONS_CAR(dname);
Symbol_sp fname = gc::As<Symbol_sp>(sss);
if (fname.notnilp() && oCdr(dname).nilp()) {
if (!fname->fboundp_setf())
return nil<T_O>();
if (fname->macroP())
return GlobalMacroInfo_O::make(fname->getSetfFdefinition());
else {
if (cl::_sym_compiler_macro_function->fboundp()) {
T_sp cmexpander = eval::funcall(cl::_sym_compiler_macro_function, name);
return GlobalFunInfo_O::make(cmexpander);
} else
return GlobalFunInfo_O::make(nil<T_O>());
}
}
}
}
// Bad function name.
return nil<T_O>();
} else {
Symbol_sp fname = gc::As<Symbol_sp>(name);
if (!fname->fboundp())
return nil<T_O>();
else if (fname->macroP())
return GlobalMacroInfo_O::make(fname->symbolFunction());
else {
// Look for a compiler macro expander.
if (cl::_sym_compiler_macro_function->fboundp()) {
T_sp cmexpander = eval::funcall(cl::_sym_compiler_macro_function, fname);
return GlobalFunInfo_O::make(cmexpander);
} else
return GlobalFunInfo_O::make(nil<T_O>());
}
}
}
FunInfoV fun_info_v(T_sp name, Lexenv_sp env) {
// Local?
T_sp info = env->functionInfo(name);
if (gc::IsA<LocalFunInfo_sp>(info))
return FunInfoV(LocalFunInfoV(gc::As_unsafe<LocalFunInfo_sp>(info)));
else if (gc::IsA<LocalMacroInfo_sp>(info))
return FunInfoV(LocalMacroInfoV(gc::As_unsafe<LocalMacroInfo_sp>(info)));
ASSERT(info.nilp());
// Split into setf and not versions.
if (name.consp()) {
List_sp cname = name;
T_sp dname = oCdr(cname);
if (oCar(cname) != cl::_sym_setf || !dname.consp() || oCdr(dname).notnilp())
return FunInfoV(NoFunInfoV()); // TODO: error?
T_sp sss = CONS_CAR(dname);
Symbol_sp fname = gc::As<Symbol_sp>(sss);
if (!fname->fboundp_setf())
return FunInfoV(NoFunInfoV());
if (fname->macroP())
return FunInfoV(GlobalMacroInfoV(fname->getSetfFdefinition()));
else if (cl::_sym_compiler_macro_function->fboundp()) {
T_sp cmexpander = eval::funcall(cl::_sym_compiler_macro_function, name);
return FunInfoV(GlobalFunInfoV(cmexpander));
} else
return FunInfoV(GlobalFunInfoV(nil<T_O>()));
} else {
Symbol_sp fname = gc::As<Symbol_sp>(name);
if (!fname->fboundp())
return FunInfoV(NoFunInfoV());
else if (fname->macroP())
return FunInfoV(GlobalMacroInfoV(fname->symbolFunction()));
else if (cl::_sym_compiler_macro_function->fboundp()) {
T_sp cmexpander = eval::funcall(cl::_sym_compiler_macro_function, fname);
return FunInfoV(GlobalFunInfoV(cmexpander));
} else
return FunInfoV(GlobalFunInfoV(nil<T_O>()));
}
}
bool Lexenv_O::notinlinep(T_sp fname) {
for (auto cur : this->decls()) {
T_sp decl = oCar(cur);
if (gc::IsA<Cons_sp>(decl) && oCar(decl) == cl::_sym_notinline && gc::IsA<Cons_sp>(oCdr(decl)) &&
gc::As_unsafe<Cons_sp>(oCdr(decl))->memberEq(fname).notnilp())
return true;
}
return false;
}
// defined out of line for circularity reasons
Module_sp Context::module() const { return this->cfunction()->module(); }
size_t Context::literal_index(T_sp literal) const {
ComplexVector_T_sp literals = this->cfunction()->module()->literals();
// FIXME: Smarter POSITION
for (size_t i = 0; i < literals->length(); ++i) {
T_sp slit = (*literals)[i];
if (gc::IsA<ConstantInfo_sp>(slit) && gc::As_unsafe<ConstantInfo_sp>(slit)->value() == literal)
return i;
}
Fixnum_sp nind = literals->vectorPushExtend(ConstantInfo_O::make(literal));
return nind.unsafe_fixnum();
}
// Like literal-index, but forces insertion. This is used when generating
// a keyword argument parser, since the keywords must be sequential even if
// they've previously appeared in the literals vector.
// This is also used by LTV processing to put in a placeholder.
size_t Context::new_literal_index(T_sp literal) const {
Fixnum_sp nind = this->cfunction()->module()->literals()->vectorPushExtend(ConstantInfo_O::make(literal));
return nind.unsafe_fixnum();
}
// We never coalesce LTVs at the moment. Hypothetically we could, but
// it seems like a pretty marginal thing.
size_t Context::ltv_index(T_sp form, bool read_only_p) const {
LoadTimeValueInfo_sp ltvi = LoadTimeValueInfo_O::make(form, read_only_p);
Fixnum_sp nind = this->cfunction()->module()->literals()->vectorPushExtend(ltvi);
return nind.unsafe_fixnum();
}
size_t Context::cfunction_index(Cfunction_sp fun) const {
ComplexVector_T_sp literals = this->cfunction()->module()->literals();
// FIXME: Smarter POSITION
for (size_t i = 0; i < literals->length(); ++i) {
T_sp slit = (*literals)[i];
if (gc::IsA<Cfunction_sp>(slit) && slit == fun)
return i;
}
Fixnum_sp nind = literals->vectorPushExtend(fun);
return nind.unsafe_fixnum();
}
size_t Context::fcell_index(T_sp name) const {
ComplexVector_T_sp literals = this->cfunction()->module()->literals();
// FIXME: Smarter POSITION
for (size_t i = 0; i < literals->length(); ++i) {
T_sp slit = (*literals)[i];
if (gc::IsA<FunctionCellInfo_sp>(slit) && gc::As_unsafe<FunctionCellInfo_sp>(slit)->fname() == name)
return i;
}
Fixnum_sp nind = literals->vectorPushExtend(FunctionCellInfo_O::make(name));
return nind.unsafe_fixnum();
}
size_t Context::vcell_index(Symbol_sp name) const {
ComplexVector_T_sp literals = this->cfunction()->module()->literals();
// FIXME: Smarter POSITION
for (size_t i = 0; i < literals->length(); ++i) {
T_sp slit = (*literals)[i];
if (gc::IsA<VariableCellInfo_sp>(slit) && gc::As_unsafe<VariableCellInfo_sp>(slit)->vname() == name)
return i;
}
Fixnum_sp nind = literals->vectorPushExtend(VariableCellInfo_O::make(name));
return nind.unsafe_fixnum();
}
size_t Context::closure_index(T_sp info) const {
ComplexVector_T_sp closed = this->cfunction()->closed();
for (size_t i = 0; i < closed->length(); ++i)
if ((*closed)[i] == info)
return i;
Fixnum_sp nind = closed->vectorPushExtend(info);
return nind.unsafe_fixnum();
}
void Context::push_debug_info(T_sp info) const { this->cfunction()->debug_info()->vectorPushExtend(info); }
void Context::emit_jump(Label_sp label) const {
ControlLabelFixup_O::make(label, vm_jump_8, vm_jump_16, vm_jump_24)->contextualize(*this);
}
void Context::emit_jump_if(Label_sp label) const {
ControlLabelFixup_O::make(label, vm_jump_if_8, vm_jump_if_16, vm_jump_if_24)->contextualize(*this);
}
void Context::emit_entry_or_save_sp(LexicalInfo_sp dynenv) const { EntryFixup_O::make(dynenv)->contextualize(*this); }
void Context::emit_ref_or_restore_sp(LexicalInfo_sp dynenv) const { RestoreSPFixup_O::make(dynenv)->contextualize(*this); }
void Context::emit_exit(Label_sp label) const {
ControlLabelFixup_O::make(label, vm_exit_8, vm_exit_16, vm_exit_24)->contextualize(*this);
}
void Context::emit_exit_or_jump(LexicalInfo_sp dynenv, Label_sp label) const {
ExitFixup_O::make(dynenv, label)->contextualize(*this);
}
void Context::maybe_emit_entry_close(LexicalInfo_sp dynenv) const { EntryCloseFixup_O::make(dynenv)->contextualize(*this); }
void Context::emit_catch(Label_sp label) const {
ControlLabelFixup_O::make(label, vm_catch_8, vm_catch_16, 0)->contextualize(*this);
}
void Context::emit_jump_if_supplied(Label_sp label, size_t ind) const {
JumpIfSuppliedFixup_O::make(label, ind)->contextualize(*this);
}
// Push the immutable value or cell of lexical in CONTEXT.
void Context::reference_lexical_info(LexicalInfo_sp info) const {
if (info->cfunction() == this->cfunction())
this->assemble1(vm_ref, info->frameIndex());
else
this->assemble1(vm_closure, this->closure_index(info));
}
void Context::maybe_emit_make_cell(LexicalVarInfo_sp info) const {
LexRefFixup_O::make(info->lex(), vm_make_cell)->contextualize(*this);
}
void Context::maybe_emit_cell_ref(LexicalVarInfo_sp info) const {
LexRefFixup_O::make(info->lex(), vm_cell_ref)->contextualize(*this);
}
// FIXME: This is probably a good candidate for a specialized
// instruction.
void Context::maybe_emit_encage(LexicalVarInfo_sp info) const { EncageFixup_O::make(info->lex())->contextualize(*this); }
void Context::emit_lexical_set(LexicalVarInfo_sp info) const { LexSetFixup_O::make(info->lex())->contextualize(*this); }
void Context::emit_parse_key_args(size_t max_count, size_t key_count, size_t keystart, size_t indx, bool aokp) const {
ComplexVector_byte8_t_sp bytecode = this->cfunction()->bytecode();
if ((max_count < (1 << 8)) && (key_count < (1 << 8)) && (keystart < (1 << 8)) && (indx < (1 << 8))) {
bytecode->vectorPushExtend(vm_parse_key_args);
bytecode->vectorPushExtend(max_count);
bytecode->vectorPushExtend(key_count | (aokp ? 0x80 : 0));
bytecode->vectorPushExtend(keystart);
bytecode->vectorPushExtend(indx);
} else if ((max_count < (1 << 16)) && (key_count < (1 << 16)) && (keystart < (1 << 16)) && (indx < (1 << 16))) {
bytecode->vectorPushExtend(clasp_make_fixnum(vm_long));
bytecode->vectorPushExtend(clasp_make_fixnum(vm_parse_key_args));
bytecode->vectorPushExtend(max_count & 0xff);
bytecode->vectorPushExtend(max_count >> 8);
bytecode->vectorPushExtend(key_count & 0xff);
bytecode->vectorPushExtend((key_count >> 8) | (aokp ? 0x80 : 0));
bytecode->vectorPushExtend(keystart & 0xff);
bytecode->vectorPushExtend(keystart >> 8);
bytecode->vectorPushExtend(indx & 0xff);
bytecode->vectorPushExtend(indx >> 8);
} else
SIMPLE_ERROR("Bytecode compiler limit reached: keyarg indices too large: %zu %zu %zu %zu", max_count, key_count, keystart,
indx);
}
void Context::assemble0(uint8_t opcode) const { this->cfunction()->bytecode()->vectorPushExtend(opcode); }
void Context::assemble1(uint8_t opcode, size_t operand) const {
ComplexVector_byte8_t_sp bytecode = this->cfunction()->bytecode();
if (operand < (1 << 8)) {
bytecode->vectorPushExtend(opcode);
bytecode->vectorPushExtend(operand);
} else if (operand < (1 << 16)) {
bytecode->vectorPushExtend(vm_long);
bytecode->vectorPushExtend(opcode);
bytecode->vectorPushExtend(operand & 0xff);
bytecode->vectorPushExtend(operand >> 8);
} else
SIMPLE_ERROR("Bytecode compiler limit reached: operand %zu too large", operand);
}
void Context::assemble2(uint8_t opcode, size_t operand1, size_t operand2) const {
ComplexVector_byte8_t_sp bytecode = this->cfunction()->bytecode();
if ((operand1 < (1 << 8)) && (operand2 < (1 << 8))) {
bytecode->vectorPushExtend(opcode);
bytecode->vectorPushExtend(operand1);
bytecode->vectorPushExtend(operand2);
} else if ((operand1 < (1 << 16)) && (operand2 < (1 << 16))) {
bytecode->vectorPushExtend(vm_long);
bytecode->vectorPushExtend(opcode);
bytecode->vectorPushExtend(operand1 & 0xff);
bytecode->vectorPushExtend(operand1 >> 8);
bytecode->vectorPushExtend(operand2 & 0xff);
bytecode->vectorPushExtend(operand2 >> 8);
} else
SIMPLE_ERROR("Bytecode compiler limit reached: operands %zu %zu too large", operand1, operand2);
}
void Context::emit_bind(size_t count, size_t offset) const {
switch (count) {
case 1:
this->assemble1(vm_set, offset);
break;
case 0:
break;
default:
this->assemble2(vm_bind, count, offset);
break;
}
}
void Context::emit_call(size_t argcount) const {
switch (this->receiving()) {
case 1:
this->assemble1(vm_call_receive_one, argcount);
break;
case -1:
this->assemble1(vm_call, argcount);
break;
default:
this->assemble2(vm_call_receive_fixed, argcount, this->receiving());
break;
}
}
void Context::emit_mv_call() const {
switch (this->receiving()) {
case 1:
this->assemble0(vm_mv_call_receive_one);
break;
case -1:
this->assemble0(vm_mv_call);
break;
default:
this->assemble1(vm_mv_call_receive_fixed, this->receiving());
break;
}
}
void Context::emit_special_bind(Symbol_sp sym) const { this->assemble1(vm_special_bind, this->vcell_index(sym)); }
void Context::emit_unbind(size_t count) const {
for (size_t i = 0; i < count; ++i)
this->assemble0(vm_unbind);
}
size_t Annotation_O::module_position() { return this->pposition() + gc::As_assert<Cfunction_sp>(this->cfunction())->pposition(); }
void Label_O::contextualize(const Context ctxt) {
Cfunction_sp cfunction = ctxt.cfunction();
this->setPosition(cfunction->bytecode()->length());
this->setCfunction(cfunction);
Fixnum_sp nind = cfunction->annotations()->vectorPushExtend(this->asSmartPtr());
this->setIndex(nind.unsafe_fixnum());
}
void Fixup_O::contextualize(const Context ctxt) {
Cfunction_sp cfunction = ctxt.cfunction();
ComplexVector_byte8_t_sp assembly = cfunction->bytecode();
size_t position = assembly->length();
this->setCfunction(cfunction);
this->setInitialPosition(position);
this->setPosition(position);
Fixnum_sp nind = cfunction->annotations()->vectorPushExtend(this->asSmartPtr());
this->setIndex(nind.unsafe_fixnum());
for (size_t i = 0; i < this->initial_size(); ++i)
assembly->vectorPushExtend(0);
}
ptrdiff_t LabelFixup_O::delta() { return this->label()->module_position() - this->module_position(); }
static void emit_control_label_fixup(size_t size, size_t offset, size_t position, SimpleVector_byte8_t_sp code, uint8_t opcode8,
uint8_t opcode16, uint8_t opcode24) {
// Offset is a size_t so it's a positive integer i.e. dumpable.
switch (size) {
case 2:
(*code)[position] = opcode8;
break;
case 3:
(*code)[position] = opcode16;
break;
case 4:
(*code)[position] = opcode24;
break;
default:
SIMPLE_ERROR("Assembler bug: Impossible size %zu", size);
}
for (size_t i = 0; i < size - 1; ++i) {
// Write the offset one byte at a time, starting with the LSB.
(*code)[position + i + 1] = offset & 0xff;
offset >>= 8;
}
}
void ControlLabelFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
emit_control_label_fixup(this->size(), this->delta(), position, code, this->_opcode8, this->_opcode16, this->_opcode24);
}
size_t resize_control_label_fixup(ptrdiff_t delta) {
if ((-(1 << 7) <= delta) && (delta <= (1 << 7) - 1))
return 2;
if ((-(1 << 15) <= delta) && (delta <= (1 << 15) - 1))
return 3;
if ((-(1 << 23) <= delta) && (delta <= (1 << 23) - 1))
return 4;
else
SIMPLE_ERROR("Bytecode compiler limit reached: Fixup delta too large");
}
size_t ControlLabelFixup_O::resize() { return resize_control_label_fixup(this->delta()); }
void JumpIfSuppliedFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t size = this->size();
switch (size) {
case 3:
(*code)[position] = vm_jump_if_supplied_8;
break;
case 4:
(*code)[position] = vm_jump_if_supplied_16;
break;
default:
SIMPLE_ERROR("Assembler bug: Impossible size %zu", size);
}
(*code)[position + 1] = this->iindex();
size_t offset = this->delta();
for (size_t i = 0; i < size - 2; ++i) {
(*code)[position + i + 2] = offset & 0xff;
offset >>= 8;
}
}
size_t JumpIfSuppliedFixup_O::resize() {
ptrdiff_t delta = this->delta();
if ((-(1 << 7) <= delta) && (delta <= (1 << 7) - 1))
return 3;
if ((-(1 << 15) <= delta) && (delta <= (1 << 15) - 1))
return 4;
else
SIMPLE_ERROR("Bytecode compiler limit reached: Fixup delta too large");
}
void LexRefFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t size = this->size();
switch (size) {
case 1:
(*code)[position] = this->opcode();
break;
default:
UNREACHABLE();
}
}
size_t LexRefFixup_O::resize() { return this->lex()->indirectLexicalP() ? 1 : 0; }
void EncageFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t size = this->size();
size_t index = this->lex()->frameIndex();
switch (size) {
case 5: // FIXME: Use assemble_into?
(*code)[position] = vm_ref;
(*code)[position + 1] = index;
(*code)[position + 2] = vm_make_cell;
(*code)[position + 3] = vm_set;
(*code)[position + 4] = index;
break;
case 9:
(*code)[position] = vm_long;
(*code)[position + 1] = vm_ref;
(*code)[position + 2] = index & 0xff;
(*code)[position + 3] = index >> 8;
(*code)[position + 4] = vm_make_cell;
(*code)[position + 5] = vm_long;
(*code)[position + 6] = vm_set;
(*code)[position + 7] = index & 0xff;
(*code)[position + 8] = index >> 8;
break;
default:
UNREACHABLE();
}
}
size_t EncageFixup_O::resize() {
size_t index = this->lex()->frameIndex();
if (!this->lex()->indirectLexicalP())
return 0;
else if (index < 1 << 8)
return 5;
else if (index < 1 << 16)
return 9;
else
SIMPLE_ERROR("Bytecode compiler limit reached: Fixup delta too large");
}
void LexSetFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t size = this->size();
size_t index = this->lex()->frameIndex();
switch (size) {
case 2:
(*code)[position] = vm_set;
(*code)[position + 1] = index;
break;
case 3:
(*code)[position] = vm_ref;
(*code)[position + 1] = index;
(*code)[position + 2] = vm_cell_set;
break;
case 4:
(*code)[position] = vm_long;
(*code)[position + 1] = vm_set;
(*code)[position + 2] = index & 0xff;
(*code)[position + 3] = index >> 8;
break;
case 5:
(*code)[position] = vm_long;
(*code)[position + 1] = vm_ref;
(*code)[position + 2] = index & 0xff;
(*code)[position + 3] = index >> 8;
(*code)[position + 4] = vm_cell_set;
break;
default:
UNREACHABLE();
}
}
size_t LexSetFixup_O::resize() {
bool indirectp = this->lex()->indirectLexicalP();
size_t index = this->lex()->frameIndex();
if (index < 1 << 8)
if (indirectp)
return 3;
else
return 2;
else if (index < 1 << 16)
if (indirectp)
return 5;
else
return 4;
else
SIMPLE_ERROR("Bytecode compiler limit reached: Fixup delta too large");
}
void EntryFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t index = this->lex()->frameIndex();
if (index >= 1 << 8)
(*code)[position++] = vm_long;
if (this->lex()->closedOverP())
(*code)[position] = vm_entry;
else
(*code)[position] = vm_save_sp;
if (index < 1 << 8)
(*code)[position + 1] = index;
else {
(*code)[position + 1] = index & 0xff;
(*code)[position + 2] = index >> 8;
}
}
size_t EntryFixup_O::resize() { return (this->lex()->frameIndex() < 1 << 8) ? 2 : 4; }
void RestoreSPFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
size_t index = this->lex()->frameIndex();
if (index >= 1 << 8)
(*code)[position++] = vm_long;
if (this->lex()->closedOverP())
(*code)[position] = vm_ref;
else
(*code)[position] = vm_restore_sp;
if (index < 1 << 8)
(*code)[position + 1] = index;
else {
(*code)[position + 1] = index & 0xff;
(*code)[position + 2] = index >> 8;
}
}
size_t RestoreSPFixup_O::resize() { return (this->lex()->frameIndex() < 1 << 8) ? 2 : 4; }
void ExitFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
if (this->lex()->closedOverP())
emit_control_label_fixup(this->size(), this->delta(), position, code, vm_exit_8, vm_exit_16, vm_exit_24);
else
emit_control_label_fixup(this->size(), this->delta(), position, code, vm_jump_8, vm_jump_16, vm_jump_24);
}
size_t ExitFixup_O::resize() { return resize_control_label_fixup(this->delta()); }
void EntryCloseFixup_O::emit(size_t position, SimpleVector_byte8_t_sp code) {
switch (this->size()) {
case 1:
(*code)[position] = vm_entry_close;
break;
default:
UNREACHABLE();
}
}
size_t EntryCloseFixup_O::resize() { return (this->lex()->closedOverP()) ? 1 : 0; }
void Module_O::initialize_cfunction_positions() {
size_t position = 0;
ComplexVector_T_sp cfunctions = this->cfunctions();
for (T_sp tfunction : *cfunctions) {
Cfunction_sp cfunction = gc::As_assert<Cfunction_sp>(tfunction);
cfunction->setPosition(position);
position += cfunction->bytecode()->length();
}
}
void Fixup_O::update_positions(size_t increase) {
Cfunction_sp funct = gc::As_assert<Cfunction_sp>(this->cfunction());
ComplexVector_T_sp annotations = funct->annotations();
size_t nannot = annotations->length();
for (size_t idx = this->iindex() + 1; idx < nannot; ++idx) {
gc::As_assert<Annotation_sp>((*annotations)[idx])->_position += increase;
}
funct->_extra += increase;
ComplexVector_T_sp functions = funct->module()->cfunctions();
size_t nfuns = functions->length();
for (size_t idx = funct->iindex() + 1; idx < nfuns; ++idx) {
gc::As_assert<Cfunction_sp>((*functions)[idx])->_position += increase;
}
}
void Module_O::resolve_fixup_sizes() {
bool changedp;
ComplexVector_T_sp cfunctions = this->cfunctions();
do {
changedp = false;
for (T_sp tfunction : *cfunctions) {
ComplexVector_T_sp annotations = gc::As_assert<Cfunction_sp>(tfunction)->annotations();
for (T_sp tannot : *annotations) {
if (gc::IsA<Fixup_sp>(tannot)) {
Fixup_sp fixup = gc::As_unsafe<Fixup_sp>(tannot);
size_t old_size = fixup->size();
size_t new_size = fixup->resize();
if (old_size != new_size) {
ASSERT(new_size >= old_size);
fixup->setSize(new_size);
fixup->update_positions(new_size - old_size);
changedp = true;
}
}
}
}
} while (changedp);
}
size_t Module_O::bytecode_size() {
ComplexVector_T_sp cfunctions = this->cfunctions();
T_sp tlast_cfunction = (*cfunctions)[cfunctions->length() - 1];
Cfunction_sp last_cfunction = gc::As_assert<Cfunction_sp>(tlast_cfunction);
return last_cfunction->pposition() + last_cfunction->final_size();
}
// Resolve start and end but leave the rest alone.
static void resolve_debug_info(BytecodeDebugInfo_sp info) {
T_sp open_label = info->start();
if (gc::IsA<Label_sp>(open_label))
info->setStart(clasp_make_fixnum(gc::As_unsafe<Label_sp>(open_label)->module_position()));
else // compiler screwed up, but this is just debug info, don't raise stink
info->setStart(clasp_make_fixnum(0));
T_sp close_label = info->end();
if (gc::IsA<Label_sp>(close_label))
info->setEnd(clasp_make_fixnum(gc::As_unsafe<Label_sp>(close_label)->module_position()));
else
info->setEnd(clasp_make_fixnum(0));
}
// Resolve the labels to fixnums, and LVInfos to frame locations.
// If a variable is stored in a cell, we indicate this by wrapping its
// frame location in a cons.
static void resolve_debug_vars(BytecodeDebugVars_sp info) {
resolve_debug_info(info);
for (Cons_sp cur : info->bindings()) {
T_sp tentry = cur->car();
if (gc::IsA<Cons_sp>(tentry)) {
Cons_sp entry = gc::As_unsafe<Cons_sp>(tentry);
T_sp tlvinfo = entry->cdr();
if (gc::IsA<LexicalInfo_sp>(tlvinfo)) {
T_sp name = entry->car();
LexicalInfo_sp lvinfo = gc::As_unsafe<LexicalInfo_sp>(tlvinfo);
auto bdv = BytecodeDebugVar_O::make(name, lvinfo->frameIndex(),
lvinfo->indirectLexicalP(),
lvinfo->decls());
cur->setCar(bdv);
}
}
}
}
static void resolve_ast_tagbody(BytecodeAstTagbody_sp info) {
resolve_debug_info(info);
for (Cons_sp cur : info->tags()) {
T_sp tentry = cur->car();