[b0ce08]: src / c / interpreter.d Maximize Restore History

Download this file

interpreter.d    1342 lines (1202 with data), 30.3 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
/*
interpreter.c -- Bytecode interpreter.
*/
/*
Copyright (c) 2001, Juan Jose Garcia Ripoll.
ECL is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
See file '../Copyright' for full details.
*/
#include <string.h>
#include "ecl.h"
#include "ecl-inl.h"
#include "bytecodes.h"
#define next_code(v) *(v++)
#undef frs_pop
#define frs_pop() { cl_stack_top = cl_stack + frs_top->frs_sp; frs_top--; }
/* -------------------- INTERPRETER STACK -------------------- */
cl_index cl_stack_size = 0;
cl_object *cl_stack = NULL;
cl_object *cl_stack_top = NULL;
cl_object *cl_stack_limit = NULL;
static void
cl_stack_set_size(cl_index new_size)
{
cl_index top = cl_stack_top - cl_stack;
cl_object *new_stack;
/*printf("*+*+*+\n");*/
if (top > new_size)
FEerror("Internal error: cannot shrink stack that much.",0);
start_critical_section();
new_stack = (cl_object *)cl_alloc(new_size * sizeof(cl_object));
memcpy(new_stack, cl_stack, cl_stack_size * sizeof(cl_object));
GC_free(cl_stack);
cl_stack_size = new_size;
cl_stack = new_stack;
cl_stack_top = cl_stack + top;
cl_stack_limit = cl_stack + (new_size - 2);
end_critical_section();
}
static void
cl_stack_grow(void)
{
cl_stack_set_size(cl_stack_size + LISP_PAGESIZE);
}
void
cl_stack_push(cl_object x) {
if (cl_stack_top >= cl_stack_limit)
cl_stack_grow();
*(cl_stack_top++) = x;
}
cl_object
cl_stack_pop() {
if (cl_stack_top == cl_stack)
FEerror("Internal error: stack underflow.",0);
return *(--cl_stack_top);
}
cl_index
cl_stack_index() {
return cl_stack_top - cl_stack;
}
void
cl_stack_set_index(cl_index index) {
cl_object *new_top = cl_stack + index;
if (new_top > cl_stack_top)
FEerror("Internal error: tried to advance stack.",0);
cl_stack_top = new_top;
}
void
cl_stack_insert(cl_index where, cl_index n) {
if (cl_stack_top + n > cl_stack_limit) {
cl_index delta = (n + (LISP_PAGESIZE-1))/LISP_PAGESIZE;
cl_stack_set_size(cl_stack_size + delta * LISP_PAGESIZE);
}
cl_stack_top += n;
memmove(&cl_stack[where+n], &cl_stack[where],
(cl_stack_top - cl_stack) * sizeof(*cl_stack));
}
void
cl_stack_pop_n(cl_index index) {
cl_object *new_top = cl_stack_top - index;
if (new_top < cl_stack)
FEerror("Internal error: stack underflow.",0);
cl_stack_top = new_top;
}
int
cl_stack_push_values(void) {
int i;
for (i=0; i<NValues; i++)
cl_stack_push(VALUES(i));
return i;
}
void
cl_stack_pop_values(int n) {
NValues = n;
while (n > 0)
VALUES(--n) = cl_stack_pop();
}
cl_index
cl_stack_push_va_list(cl_va_list args) {
cl_index sp;
sp = cl_stack_top - cl_stack;
while (cl_stack_top + args[0].narg > cl_stack_limit)
cl_stack_grow();
while (args[0].narg > 0) {
args[0].narg--;
*(cl_stack_top++) = va_arg(args[0].args,cl_object);
}
return sp;
}
cl_index
cl_stack_push_list(cl_object list)
{
cl_index n;
cl_object fast, slow;
/* INV: A list's length always fits in a fixnum */
fast = slow = list;
for (n = 0; CONSP(fast); n++, fast = CDR(fast)) {
*cl_stack_top = CAR(fast);
if (++cl_stack_top >= cl_stack_limit)
cl_stack_grow();
if (n & 1) {
/* Circular list? */
if (slow == fast) break;
slow = CDR(slow);
}
}
if (fast != Cnil)
FEtype_error_proper_list(list);
return n;
}
/* ------------------------------ LEXICAL ENV. ------------------------------ */
cl_object lex_env;
static void
bind_var(register cl_object var, register cl_object val)
{
lex_env = CONS(var, CONS(val, lex_env));
}
static void
bind_function(cl_object name, cl_object fun)
{
lex_env = CONS(@':function', CONS(CONS(name, fun), lex_env));
}
static void
bind_tagbody(cl_object id)
{
lex_env = CONS(@':tag', CONS(id, lex_env));
}
static void
bind_block(cl_object name, cl_object id)
{
lex_env = CONS(@':block', CONS(CONS(name, id), lex_env));
}
static void
bind_special(register cl_object var, register cl_object val)
{
bds_bind(var, val);
}
static cl_object
search_local(register int s) {
cl_object x;
for (x = lex_env; s-- > 0 && !Null(x); x = CDDR(x));
if (Null(x))
FEerror("Internal error: local not found.", 0);
return CADR(x);
}
static void
setq_local(register int s, register cl_object v) {
cl_object x;
for (x = lex_env; s-- > 0 && !Null(x); x = CDDR(x));
if (Null(x))
FEerror("Internal error: local ~S not found.", 1, s);
CADR(x) = v;
}
static cl_object
search_tag(cl_object name, cl_object type)
{
cl_object x;
for (x = lex_env; CONSP(x); x = CDDR(x))
if (CAR(x) == type) {
cl_object record = CADR(x);
cl_object the_name = CAR(record);
cl_object the_value = CDR(record);
if (name == the_name)
return the_value;
}
return Cnil;
}
/* -------------------- LAMBDA FUNCTIONS -------------------- */
static void
lambda_bind_var(cl_object var, cl_object val, cl_object specials)
{
if (!member_eq(var, specials))
bind_var(var, val);
else
bind_special(var, val);
}
static cl_object *
lambda_bind(int narg, cl_object lambda_list, cl_index sp)
{
cl_object *data = &lambda_list->bytecodes.data[2];
cl_object specials = lambda_list->bytecodes.data[1];
cl_object aux;
int i, n;
bool other_keys = FALSE;
bool check_remaining = TRUE;
bool allow_other_keys_found = FALSE;
/* 1) REQUIRED ARGUMENTS: N var1 ... varN */
n = fix(next_code(data));
if (narg < n)
check_arg_failed(narg, n);
for (; n; n--, narg--)
lambda_bind_var(next_code(data), cl_stack[sp++], specials);
/* 2) OPTIONAL ARGUMENTS: N var1 value1 flag1 ... varN valueN flagN */
for (n = fix(next_code(data)); n; n--, data+=3) {
if (narg) {
lambda_bind_var(data[0], cl_stack[sp], specials);
sp++; narg--;
if (!Null(data[2]))
lambda_bind_var(data[2], Ct, specials);
} else {
cl_object defaults = data[1];
if (FIXNUMP(defaults)) {
interpret(&data[1] + fix(defaults));
defaults = VALUES(0);
}
lambda_bind_var(data[0], defaults, specials);
if (!Null(data[2]))
lambda_bind_var(data[2], Cnil, specials);
}
}
/* 3) REST ARGUMENT: {rest-var | NIL} */
if (!Null(data[0])) {
cl_object rest = Cnil;
check_remaining = FALSE;
for (i=narg; i; )
rest = CONS(cl_stack[sp+(--i)], rest);
lambda_bind_var(data[0], rest, specials);
}
data++;
/* 4) ALLOW-OTHER-KEYS: { T | NIL } */
other_keys = !Null(next_code(data));
/* 5) KEYWORDS: N key1 var1 value1 flag1 ... keyN varN valueN flagN */
n = fix(next_code(data));
if (n != 0 || other_keys) {
cl_object *keys;
cl_object spp[n];
bool other_found = FALSE;
for (i=0; i<n; i++)
spp[i] = OBJNULL;
for (; narg; narg-=2) {
cl_object key = cl_stack[sp++];
cl_object value = cl_stack[sp++];
if (!SYMBOLP(key))
FEprogram_error("LAMBDA: Keyword expected, got ~S.", 1, key);
keys = data;
for (i = 0; i < n; i++, keys += 4) {
if (key == keys[0]) {
if (spp[i] == OBJNULL)
spp[i] = value;
goto FOUND;
}
}
if (key != @':allow-other-keys')
other_found = TRUE;
else if (!allow_other_keys_found) {
allow_other_keys_found = TRUE;
other_keys = !Null(value);
}
FOUND:
(void)0;
}
if (other_found && !other_keys)
FEprogram_error("LAMBDA: Unknown keys found in function ~S.",
1, lambda_list->bytecodes.data[0]);
for (i=0; i<n; i++, data+=4) {
if (spp[i] != OBJNULL)
lambda_bind_var(data[1],spp[i],specials);
else {
cl_object defaults = data[2];
if (FIXNUMP(defaults)) {
interpret(&data[2] + fix(defaults));
defaults = VALUES(0);
}
lambda_bind_var(data[1],defaults,specials);
}
if (!Null(data[3]))
lambda_bind_var(data[3],(spp[i] != OBJNULL)? Ct : Cnil,specials);
}
}
if (narg && !other_keys && check_remaining)
FEprogram_error("LAMBDA: Too many arguments to function ~S.", 1,
lambda_list->bytecodes.data[0]);
return &data[2];
}
cl_object
lambda_apply(int narg, cl_object fun)
{
cl_index args = cl_stack_index() - narg;
cl_object output, name, *body;
bds_ptr old_bds_top;
volatile bool block;
if (type_of(fun) != t_bytecodes)
FEinvalid_function(fun);
/* 1) Save the lexical environment and set up a new one */
ihs_push(fun);
lex_env = fun->bytecodes.lex;
old_bds_top = bds_top;
/* Establish bindings */
body = lambda_bind(narg, fun, args);
/* If it is a named lambda, set a block for RETURN-FROM */
block = FALSE;
name = fun->bytecodes.data[0];
if (Null(fun->bytecodes.data[0]))
block = FALSE;
else {
block = TRUE;
/* Accept (SETF name) */
if (CONSP(name)) name = CADR(name);
fun = new_frame_id();
bind_block(name, fun);
if (frs_push(FRS_CATCH, fun)) {
goto END;
}
}
/* Process statements */
VALUES(0) = Cnil;
NValues = 0;
interpret(body);
END: if (block) frs_pop();
bds_unwind(old_bds_top);
ihs_pop();
returnn(VALUES(0));
}
/* -------------------- AIDS TO THE INTERPRETER -------------------- */
static inline cl_fixnum
get_oparg(cl_object o) {
return GET_OPARG(o);
}
static inline cl_object *
packed_label(cl_object *v) {
return v + GET_OPARG(v[0]);
}
static inline cl_object *
simple_label(cl_object *v) {
return v + fix(v[0]);
}
static cl_object
search_global(register cl_object s) {
cl_object x = SYM_VAL(s);
if (x == OBJNULL)
FEunbound_variable(s);
return x;
}
/* Similar to funcall(), but registers calls in the IHS stack. */
static cl_object
interpret_funcall(int narg, cl_object fun) {
cl_object *args;
cl_object x;
args = cl_stack_top - narg;
AGAIN:
switch (type_of(fun)) {
case t_cfun:
ihs_push(fun->cfun.name);
lex_env = Cnil;
if (fun->cfun.narg >= 0) {
if (narg != fun->cfun.narg)
check_arg_failed(narg, fun->cfun.narg);
x = APPLY_fixed(narg, fun->cfun.entry, args);
} else {
x = APPLY(narg, fun->cfun.entry, args);
}
ihs_pop();
break;
case t_cclosure:
/* FIXME! Shouldn't we register this call somehow? */
x = APPLY_closure(narg, fun->cclosure.entry, fun->cclosure.env, args);
break;
#ifdef CLOS
case t_gfun:
fun = compute_method(narg, fun, args);
goto AGAIN;
#endif
case t_bytecodes:
x = lambda_apply(narg, fun);
break;
case t_symbol: {
cl_object function = SYM_FUN(fun);
if (function == OBJNULL)
FEundefined_function(fun);
fun = function;
goto AGAIN;
}
default:
FEinvalid_function(fun);
}
cl_stack_pop_n(narg);
return x;
}
@(defun apply (fun lastarg &rest args)
int i;
@
narg -= 2;
for (i = 0; narg; i++,narg--) {
cl_stack_push(lastarg);
lastarg = cl_va_arg(args);
}
loop_for_in (lastarg) {
if (i >= CALL_ARGUMENTS_LIMIT) {
cl_stack_pop_n(i);
FEprogram_error("CALL-ARGUMENTS-LIMIT exceeded",0);
}
cl_stack_push(CAR(lastarg));
i++;
} end_loop_for_in;
returnn(interpret_funcall(i, fun));
@)
/* -------------------- THE INTERPRETER -------------------- */
/* OP_BLOCK label{arg}, block-name{symbol}
...
OP_EXIT
label:
Executes the enclosed code in a named block.
LABEL points to the first instruction after OP_EXIT.
*/
static cl_object *
interpret_block(cl_object *vector) {
cl_object * volatile exit, name;
cl_object id = new_frame_id();
/* 1) Save current environment */
cl_stack_push(lex_env);
/* 2) Set up a block with given name */
exit = packed_label(vector - 1);
bind_block(next_code(vector), id);
if (frs_push(FRS_CATCH,id) == 0)
vector = interpret(vector);
frs_pop();
/* 3) Restore environment */
lex_env = cl_stack_pop();
return exit;
}
static cl_object *
interpret_catch(cl_object *vector) {
cl_object * volatile exit;
exit = packed_label(vector - 1);
if (frs_push(FRS_CATCH,VALUES(0)) == 0)
interpret(vector);
frs_pop();
return exit;
}
/* OP_TAGBODY n{arg}
tag1
label1
...
tagn
labeln
label1:
...
labeln:
...
OP_EXIT
High level construct for the TAGBODY form.
*/
static cl_object *
interpret_tagbody(cl_object *vector) {
cl_index i, ntags = get_oparg(vector[-1]);
cl_object id = new_frame_id();
cl_object *aux, *tag_list = vector;
/* 1) Save current environment */
cl_stack_push(lex_env);
/* 2) Bind tags */
bind_tagbody(id);
/* 3) Wait here for gotos. Each goto sets nlj_tag to a integer
which ranges from 0 to ntags-1, depending on the tag. These
numbers are indices into the jump table and are computed
at compile time.
*/
aux = vector + ntags;
if (frs_push(FRS_CATCH, id) != 0)
aux = simple_label(vector + fix(nlj_tag));
vector = interpret(aux);
frs_pop();
/* 4) Restore environment */
lex_env = cl_stack_pop();
VALUES(0) = Cnil;
NValues = 0;
return vector;
}
/* OP_UNWIND label
... ; code to be protected and whose value is output
OP_EXIT
label:
... ; code executed at exit
OP_EXIT
High level construct for UNWIND-PROTECT. The first piece of code
is executed and its output value is saved. Then the second piece
of code is executed and the output values restored. The second
piece of code is always executed, even if a THROW, RETURN or GO
happen within the first piece of code.
*/
static cl_object *
interpret_unwind_protect(cl_object *vector) {
volatile int nr;
cl_object * volatile exit;
bool unwinding;
exit = packed_label(vector-1);
if (frs_push(FRS_PROTECT, Cnil))
unwinding = TRUE;
else {
interpret(vector);
unwinding = FALSE;
}
frs_pop();
nr = cl_stack_push_values();
exit = interpret(exit);
cl_stack_pop_values(nr);
if (unwinding)
unwind(nlj_fr, nlj_tag);
return exit;
}
/* OP_DO label
... ; code executed within a NIL block
OP_EXIT
label:
High level construct for the DO and BLOCK forms.
*/
static cl_object *
interpret_do(cl_object *vector) {
cl_object *volatile exit;
cl_object id = new_frame_id();
/* 1) Save all environment */
bds_ptr old_bds_top = bds_top;
cl_stack_push(lex_env);
/* 2) Set up new block name */
bind_block(Cnil, id);
exit = packed_label(vector-1);
if (frs_push(FRS_CATCH,id) == 0)
interpret(vector);
frs_pop();
/* 3) Restore all environment */
bds_unwind(old_bds_top);
lex_env = cl_stack_pop();
return exit;
}
/* OP_DOLIST label
... ; code to bind the local variable
OP_EXIT
... ; code executed on each iteration
OP_EXIT
... ; code executed at the end
OP_EXIT
label:
High level construct for the DOLIST iterator. The list over which
we iterate is stored in VALUES(0).
*/
static cl_object *
interpret_dolist(cl_object *vector) {
cl_object *output, *volatile exit;
cl_object list, var;
cl_object id = new_frame_id();
/* 1) Save all environment */
bds_ptr old_bds_top = bds_top;
cl_stack_push(lex_env);
/* 2) Set up a nil block */
bind_block(Cnil, id);
if (frs_push(FRS_CATCH,id) == 0) {
list = VALUES(0);
exit = packed_label(vector - 1);
/* 3) Build list & bind variable*/
vector = interpret(vector);
output = packed_label(vector-1);
/* 4) Repeat until list is exahusted */
while (!endp(list)) {
NValues = 1;
VALUES(0) = CAR(list);
interpret(vector);
list = CDR(list);
}
VALUES(0) = Cnil;
NValues = 1;
interpret(output);
}
frs_pop();
/* 5) Restore environment */
lex_env = cl_stack_pop();
bds_unwind(old_bds_top);
return exit;
}
/* OP_TIMES label
... ; code to bind the local variable
OP_EXIT
... ; code executed on each iteration
OP_EXIT
... ; code executed at the end
OP_EXIT
label:
High level construct for the DOTIMES iterator. The number of times
we iterate is stored in VALUES(0).
*/
static cl_object *
interpret_dotimes(cl_object *vector) {
cl_object *output, *volatile exit;
cl_fixnum length, i;
cl_object var;
cl_object id = new_frame_id();
/* 1) Save all environment */
bds_ptr old_bds_top = bds_top;
cl_stack_push(lex_env);
/* 2) Set up a nil block */
bind_block(Cnil, id);
if (frs_push(FRS_CATCH,id) == 0) {
/* 3) Retrieve number and bind variables */
length = fix(VALUES(0));
exit = packed_label(vector - 1);
vector = interpret(vector);
output = packed_label(vector-1);
/* 4) Loop while needed */
for (i = 0; i < length;) {
interpret(vector);
NValues = 1;
VALUES(0) = MAKE_FIXNUM(++i);
}
interpret(output);
}
frs_pop();
/* 5) Restore environment */
lex_env = cl_stack_pop();
bds_unwind(old_bds_top);
return exit;
}
static cl_object
close_around(cl_object fun, cl_object lex) {
cl_object v = cl_alloc_object(t_bytecodes);
v->bytecodes.size = fun->bytecodes.size;
v->bytecodes.data = fun->bytecodes.data;
v->bytecodes.lex = lex;
return v;
}
/* OP_FLET nfun{arg}
fun1{object}
...
funn{object}
...
OP_EXIT
Executes the enclosed code in a lexical enviroment extended with
the functions "fun1" ... "funn".
*/
static cl_object *
interpret_flet(cl_object *vector) {
cl_index nfun = get_oparg(vector[-1]);
/* 1) Copy the environment so that functions get it without references
to themselves. */
cl_object lex = lex_env;
/* 3) Add new closures to environment */
while (nfun--) {
cl_object fun = next_code(vector);
cl_object f = close_around(fun,lex);
bind_function(f->bytecodes.data[0], f);
}
return vector;
}
/* OP_FLET nfun{arg}
fun1{object}
...
funn{object}
...
OP_EXIT
Executes the enclosed code in a lexical enviroment extended with
the functions "fun1" ... "funn".
*/
static cl_object *
interpret_labels(cl_object *vector) {
cl_index i, nfun = get_oparg(vector[-1]);
cl_object l;
/* 1) Build up a new environment with all functions */
for (i=0; i<nfun; i++) {
cl_object f = next_code(vector);
bind_function(f->bytecodes.data[0], f);
}
/* 2) Update the closures so that all functions can call each other */
for (i=0, l=lex_env; i<nfun; i++) {
cl_object record = CADR(l);
CDR(record) = close_around(CDR(record), lex_env);
l = CDDR(l);
}
return vector;
}
/* OP_MCALL
...
OP_EXIT
Saves the stack pointer, executes the enclosed code and
funcalls VALUE(0) using the content of the stack.
*/
static cl_object *
interpret_mcall(cl_object *vector) {
cl_index sp = cl_stack_index();
vector = interpret(vector);
VALUES(0) = interpret_funcall(cl_stack_index()-sp, VALUES(0));
return vector;
}
/* OP_MPROG1
...
OP_EXIT
Save the values in VALUES(..), execute the code enclosed, and
restore the values.
*/
static cl_object *
interpret_mprog1(cl_object *vector) {
cl_index i,n = NValues;
for (i=0; i<n; i++) {
cl_stack_push(VALUES(i));
}
vector = interpret(vector);
for (i=n; i;) {
VALUES(--i) = cl_stack_pop();
}
NValues = n;
return vector;
}
/* OP_MSETQ n{arg}
{fixnumn}|{symboln}
...
{fixnum1}|{symbol1}
Sets N variables to the N values in VALUES(), filling with
NIL when there are values missing. Local variables are denoted
with an integer which points a position in the lexical environment,
while special variables are denoted just with the name.
*/
static cl_object *
interpret_msetq(cl_object *vector)
{
cl_object var, value;
int i = get_oparg(vector[-1]);
while (i--) {
var = next_code(vector);
value = (i < NValues) ? VALUES(i) : Cnil;
if (FIXNUMP(var))
setq_local(fix(var), value);
else {
if (var->symbol.stype == stp_constant)
FEassignment_to_constant(var);
else
SYM_VAL(var) = value;
}
}
if (NValues > 1) NValues = 1;
return vector;
}
/* OP_PROGV bindings{list}
...
OP_EXIT
Execute the code enclosed with the special variables in BINDINGS
set to the values in the list which was passed in VALUES(0).
*/
static cl_object *
interpret_progv(cl_object *vector) {
cl_object values = VALUES(0);
cl_object vars = cl_stack_pop();
/* 1) Save current environment */
bds_ptr old_bds_top = bds_top;
cl_stack_push(lex_env);
/* 2) Add new bindings */
while (!endp(vars)) {
if (values == Cnil)
bds_bind(CAR(vars), OBJNULL);
else {
bds_bind(CAR(vars), cl_car(values));
values = CDR(values);
}
vars = CDR(vars);
}
vector = interpret(vector);
/* 3) Restore environment */
lex_env = cl_stack_pop();
bds_unwind(old_bds_top);
return vector;
}
cl_object *
interpret(cl_object *vector) {
cl_type t;
cl_object s;
cl_fixnum n;
BEGIN:
s = next_code(vector);
t = type_of(s);
if (t != t_fixnum) {
VALUES(0) = s;
NValues = 1;
goto BEGIN;
}
switch (GET_OP(s)) {
/* OP_QUOTE
Sets VALUES(0) to an immediate value.
*/
case OP_QUOTE:
VALUES(0) = next_code(vector);
NValues = 1;
break;
/* OP_VAR n{arg}, var{symbol}
Sets NValues=1 and VALUES(0) to the value of the n-th local.
VAR is the name of the variable for readability purposes.
*/
case OP_VAR: {
int lex_env_index = get_oparg(s);
VALUES(0) = search_local(lex_env_index);
NValues = 1;
break;
}
/* OP_VARS var{symbol}
Sets NValues=1 and VALUES(0) to the value of the symbol VAR.
VAR should be either a special variable or a constant.
*/
case OP_VARS: {
cl_object var_name = next_code(vector);
VALUES(0) = search_global(var_name);
NValues = 1;
break;
}
/* OP_PUSH
Pushes the object in VALUES(0).
*/
case OP_PUSH:
cl_stack_push(VALUES(0));
break;
/* OP_PUSHV n{arg}
Pushes the value of the n-th local onto the stack.
*/
case OP_PUSHV: {
int lex_env_index = get_oparg(s);
cl_stack_push(search_local(lex_env_index));
break;
}
/* OP_PUSHVS var{symbol}
Pushes the value of the symbol VAR onto the stack.
VAR should be either a special variable or a constant.
*/
case OP_PUSHVS: {
cl_object var_name = next_code(vector);
cl_stack_push(search_global(var_name));
break;
}
/* OP_PUSHQ value{object}
Pushes "value" onto the stack.
*/
case OP_PUSHQ:
cl_stack_push(next_code(vector));
break;
/* OP_CALLG n{arg}, function-name{symbol}
Calls the global function with N arguments which have
been deposited in the stack. The output values are
left in VALUES(...)
*/
case OP_CALLG: {
cl_fixnum n = get_oparg(s);
cl_object fun = next_code(vector);
if (fun->symbol.gfdef == OBJNULL)
FEundefined_function(fun);
VALUES(0) = interpret_funcall(n, fun->symbol.gfdef);
break;
}
/* OP_FCALL n{arg}
Calls the function in VALUES(0) with N arguments which
have been deposited in the stack. The output values
are left in VALUES(...)
*/
case OP_FCALL: {
cl_fixnum n = get_oparg(s);
cl_object fun = VALUES(0);
VALUES(0) = interpret_funcall(n, fun);
break;
}
/* OP_PCALLG n{arg}, function-name{symbol}
Calls the global function with N arguments which have
been deposited in the stack. The first output value is
left on the stack.
*/
case OP_PCALLG: {
cl_fixnum n = get_oparg(s);
cl_object fun = next_code(vector);
if (fun->symbol.gfdef == OBJNULL)
FEundefined_function(fun);
VALUES(0) = interpret_funcall(n, fun->symbol.gfdef);
cl_stack_push(VALUES(0));
break;
}
/* OP_PFCALL n{arg}
Calls the function in VALUES(0) with N arguments which
have been deposited in the stack. The first output value
is pushed on the stack.
*/
case OP_PFCALL: {
cl_fixnum n = get_oparg(s);
cl_object fun = VALUES(0);
VALUES(0) = interpret_funcall(n, fun);
cl_stack_push(VALUES(0));
break;
}
/* OP_EXIT
Marks the end of a high level construct (BLOCK, CATCH...)
*/
case OP_EXIT:
return vector;
/* OP_NOP
Sets VALUES(0) = NIL and NValues = 1
*/
case OP_NOP:
VALUES(0) = Cnil;
NValues = 0;
break;
/* OP_HALT
Marks the end of a function.
*/
case OP_HALT:
return vector-1;
case OP_FLET:
vector = interpret_flet(vector);
break;
case OP_LABELS:
vector = interpret_labels(vector);
break;
/* OP_LFUNCTION n{arg}, function-name{symbol}
Calls the local or global function with N arguments
which have been deposited in the stack.
*/
case OP_LFUNCTION: {
int lex_env_index = get_oparg(s);
cl_object fun_record = search_local(lex_env_index);
cl_object fun_object = CDR(fun_record);
VALUES(0) = fun_object;
NValues = 1;
break;
}
/* OP_FUNCTION name{symbol}
Extracts the function associated to a symbol. The function
may be defined in the global environment or in the local
environment. This last value takes precedence.
*/
case OP_FUNCTION: {
cl_object fun_name = next_code(vector);
cl_object fun_object = SYM_FUN(fun_name);
if (fun_object == OBJNULL || fun_name->symbol.mflag)
FEundefined_function(fun_name);
VALUES(0) = fun_object;
NValues = 1;
break;
}
/* OP_CLOSE name{symbol}
Extracts the function associated to a symbol. The function
may be defined in the global environment or in the local
environment. This last value takes precedence.
*/
case OP_CLOSE: {
cl_object function_object = next_code(vector);
VALUES(0) = close_around(function_object, lex_env);
NValues = 1;
break;
}
/* OP_GO n{arg}, tag-name{symbol}
Jumps to the tag which is defined at the n-th position in
the lexical environment. TAG-NAME is kept for debugging
purposes.
*/
case OP_GO: {
cl_object tag_name = next_code(vector);
cl_object id = search_local(get_oparg(s));
VALUES(0) = Cnil;
NValues = 0;
cl_go(id, tag_name);
break;
}
/* OP_RETURN n{arg}
Returns from the block whose record in the lexical environment
occuppies the n-th position.
*/
case OP_RETURN: {
int lex_env_index = get_oparg(s);
cl_object block_record = search_local(lex_env_index);
cl_object block_name = CAR(block_record);
cl_object id = CDR(block_record);
cl_return_from(id, block_name);
break;
}
/* OP_THROW
Jumps to an enclosing CATCH form whose tag matches the one
of the THROW. The tag is taken from the stack, while the
output values are left in VALUES(...).
*/
case OP_THROW: {
cl_object tag_name = cl_stack_pop();
cl_throw(tag_name);
break;
}
/* OP_JMP label{arg}
OP_JNIL label{arg}
OP_JT label{arg}
OP_JEQ label{arg}, value{object}
OP_JNEQ label{arg}, value{object}
Direct or conditional jumps. The conditional jumps are made
comparing with the value of VALUES(0).
*/
case OP_JMP:
vector = vector - 1 + get_oparg(s);
break;
case OP_JNIL:
NValues = 1;
if (Null(VALUES(0))) vector = vector - 1 + get_oparg(s);
break;
case OP_JT:
NValues = 1;
if (!Null(VALUES(0))) vector = vector - 1 + get_oparg(s);
break;
case OP_JEQ:
if (VALUES(0) == next_code(vector))
vector = vector + get_oparg(s) - 2;
break;
case OP_JNEQ:
if (VALUES(0) != next_code(vector))
vector = vector + get_oparg(s) - 2;
break;
/* OP_UNBIND n{arg}
Undo "n" local bindings.
*/
case OP_UNBIND: {
cl_index n = get_oparg(s);
while (n--)
lex_env = CDDR(lex_env);
break;
}
/* OP_UNBINDS n{arg}
Undo "n" bindings of special variables.
*/
case OP_UNBINDS: {
cl_index n = get_oparg(s);
bds_unwind_n(n);
break;
}
/* OP_BIND name{symbol}
OP_PBIND name{symbol}
OP_BINDS name{symbol}
OP_PBINDS name{symbol}
Binds a lexical or special variable to the either the
value of VALUES(0) or the first value of the stack.
*/
case OP_BIND: {
cl_object var_name = next_code(vector);
cl_object value = VALUES(0);
bind_var(var_name, value);
break;
}
case OP_PBIND: {
cl_object var_name = next_code(vector);
cl_object value = cl_stack_pop();
bind_var(var_name, value);
break;
}
case OP_VBIND: {
int n = get_oparg(s);
cl_object var_name = next_code(vector);
cl_object value = (--n < NValues) ? VALUES(n) : Cnil;
bind_var(var_name, value);
break;
}
case OP_BINDS: {
cl_object var_name = next_code(vector);
cl_object value = VALUES(0);
bind_special(var_name, value);
break;
}
case OP_PBINDS: {
cl_object var_name = next_code(vector);
cl_object value = cl_stack_pop();
bind_special(var_name, value);
break;
}
case OP_VBINDS: {
int n = get_oparg(s);
cl_object var_name = next_code(vector);
cl_object value = (--n < NValues) ? VALUES(n) : Cnil;
bind_special(var_name, value);
break;
}
/* OP_SETQ n{arg}
OP_PSETQ n{arg}
OP_SETQS var-name{symbol}
OP_PSETQS var-name{symbol}
Sets either the n-th local or a special variable VAR-NAME,
to either the value in VALUES(0) (OP_SETQ[S]) or to the
first value on the stack (OP_PSETQ[S]).
*/
case OP_SETQ: {
int lex_env_index = get_oparg(s);
setq_local(lex_env_index, VALUES(0));
break;
}
case OP_SETQS: {
cl_object var = next_code(vector);
if (var->symbol.stype == stp_constant)
FEassignment_to_constant(var);
else
SYM_VAL(var) = VALUES(0);
break;
}
case OP_PSETQ: {
int lex_env_index = get_oparg(s);
setq_local(lex_env_index, cl_stack_pop());
Values[0] = Cnil;
NValues = 1;
break;
}
case OP_PSETQS: {
cl_object var = next_code(vector);
if (var->symbol.stype == stp_constant)
FEassignment_to_constant(var);
else
SYM_VAL(var) = cl_stack_pop();
Values[0] = Cnil;
NValues = 1;
break;
}
case OP_BLOCK:
vector = interpret_block(vector);
break;
case OP_DOLIST:
vector = interpret_dolist(vector);
break;
case OP_DOTIMES:
vector = interpret_dotimes(vector);
break;
case OP_DO:
vector = interpret_do(vector);
break;
case OP_TAGBODY:
vector = interpret_tagbody(vector);
break;
case OP_UNWIND:
vector = interpret_unwind_protect(vector);
break;
case OP_MCALL:
vector = interpret_mcall(vector);
break;
case OP_CATCH:
vector = interpret_catch(vector);
break;
case OP_MSETQ:
vector = interpret_msetq(vector);
break;
case OP_MPROG1:
vector = interpret_mprog1(vector);
break;
case OP_PROGV:
vector = interpret_progv(vector);
break;
/* OP_PUSHVALUES
Pushes the values output by the last form.
*/
case OP_PUSHVALUES: {
int i;
for (i=0; i<NValues; i++)
cl_stack_push(VALUES(i));
break;
}
/* OP_VALUES n{arg}
Pop N values from the stack and store them in VALUES(...)
*/
case OP_VALUES: {
cl_fixnum n = get_oparg(s);
NValues = n;
while (n)
VALUES(--n) = cl_stack_pop();
break;
}
/* OP_NTHVAL
Set VALUES(0) to the N-th value of the VALUES(...) list.
The index N-th is extracted from the top of the stack.
*/
case OP_NTHVAL: {
cl_fixnum n = fix(cl_stack_pop());
if (n < 0 || n >= NValues)
VALUES(0) = Cnil;
else
VALUES(0) = VALUES(n);
NValues = 1;
break;
}
default:
FEerror("Internal error: Unknown code ~S",
1, MAKE_FIXNUM(*(vector-1)));
}
goto BEGIN;
}
@(defun si::interpreter_stack ()
@
@(return Cnil)
@)
void
init_interpreter(void)
{
cl_stack = NULL;
cl_stack_size = 0;
cl_stack_set_size(16*LISP_PAGESIZE);
}