-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathcompile.c
3529 lines (3307 loc) · 74.9 KB
/
compile.c
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 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Copyright (c) 2000, BeOpen.com.
Copyright (c) 1995-2000, Corporation for National Research Initiatives.
Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
All rights reserved.
See the file "Misc/COPYRIGHT" for information on usage and
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
******************************************************************/
/* Compile an expression node to intermediate code */
/* XXX TO DO:
XXX add __doc__ attribute == co_doc to code object attributes?
XXX (it's currently the first item of the co_const tuple)
XXX Generate simple jump for break/return outside 'try...finally'
XXX Allow 'continue' inside try-finally
XXX New opcode for loading the initial index for a for loop
XXX other JAR tricks?
*/
#ifndef NO_PRIVATE_NAME_MANGLING
#define PRIVATE_NAME_MANGLING
#endif
#include "Python.h"
#include "node.h"
#include "token.h"
#include "graminit.h"
#include "compile.h"
#include "opcode.h"
#include "structmember.h"
#include <ctype.h>
/* Three symbols from graminit.h are also defined in Python.h, with
Py_ prefixes to their names. Python.h can't include graminit.h
(which defines too many confusing symbols), but we can check here
that they haven't changed (which is very unlikely, but possible). */
#if Py_single_input != single_input
#error "single_input has changed -- update Py_single_input in Python.h"
#endif
#if Py_file_input != file_input
#error "file_input has changed -- update Py_file_input in Python.h"
#endif
#if Py_eval_input != eval_input
#error "eval_input has changed -- update Py_eval_input in Python.h"
#endif
int Py_OptimizeFlag = 0;
#define OP_DELETE 0
#define OP_ASSIGN 1
#define OP_APPLY 2
#define OFF(x) offsetof(PyCodeObject, x)
static struct memberlist code_memberlist[] = {
{"co_argcount", T_INT, OFF(co_argcount), READONLY},
{"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
{"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
{"co_flags", T_INT, OFF(co_flags), READONLY},
{"co_code", T_OBJECT, OFF(co_code), READONLY},
{"co_consts", T_OBJECT, OFF(co_consts), READONLY},
{"co_names", T_OBJECT, OFF(co_names), READONLY},
{"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
{"co_filename", T_OBJECT, OFF(co_filename), READONLY},
{"co_name", T_OBJECT, OFF(co_name), READONLY},
{"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
{"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
{NULL} /* Sentinel */
};
static PyObject *
code_getattr(co, name)
PyCodeObject *co;
char *name;
{
return PyMember_Get((char *)co, code_memberlist, name);
}
static void
code_dealloc(co)
PyCodeObject *co;
{
Py_XDECREF(co->co_code);
Py_XDECREF(co->co_consts);
Py_XDECREF(co->co_names);
Py_XDECREF(co->co_varnames);
Py_XDECREF(co->co_filename);
Py_XDECREF(co->co_name);
Py_XDECREF(co->co_lnotab);
PyObject_DEL(co);
}
static PyObject *
code_repr(co)
PyCodeObject *co;
{
char buf[500];
int lineno = -1;
char *filename = "???";
char *name = "???";
if (co->co_firstlineno != 0)
lineno = co->co_firstlineno;
if (co->co_filename && PyString_Check(co->co_filename))
filename = PyString_AsString(co->co_filename);
if (co->co_name && PyString_Check(co->co_name))
name = PyString_AsString(co->co_name);
sprintf(buf, "<code object %.100s at %p, file \"%.300s\", line %d>",
name, co, filename, lineno);
return PyString_FromString(buf);
}
static int
code_compare(co, cp)
PyCodeObject *co, *cp;
{
int cmp;
cmp = PyObject_Compare(co->co_name, cp->co_name);
if (cmp) return cmp;
cmp = co->co_argcount - cp->co_argcount;
if (cmp) return cmp;
cmp = co->co_nlocals - cp->co_nlocals;
if (cmp) return cmp;
cmp = co->co_flags - cp->co_flags;
if (cmp) return cmp;
cmp = PyObject_Compare(co->co_code, cp->co_code);
if (cmp) return cmp;
cmp = PyObject_Compare(co->co_consts, cp->co_consts);
if (cmp) return cmp;
cmp = PyObject_Compare(co->co_names, cp->co_names);
if (cmp) return cmp;
cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
return cmp;
}
static long
code_hash(co)
PyCodeObject *co;
{
long h, h0, h1, h2, h3, h4;
h0 = PyObject_Hash(co->co_name);
if (h0 == -1) return -1;
h1 = PyObject_Hash(co->co_code);
if (h1 == -1) return -1;
h2 = PyObject_Hash(co->co_consts);
if (h2 == -1) return -1;
h3 = PyObject_Hash(co->co_names);
if (h3 == -1) return -1;
h4 = PyObject_Hash(co->co_varnames);
if (h4 == -1) return -1;
h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^
co->co_argcount ^ co->co_nlocals ^ co->co_flags;
if (h == -1) h = -2;
return h;
}
PyTypeObject PyCode_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0,
"code",
sizeof(PyCodeObject),
0,
(destructor)code_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)code_getattr, /*tp_getattr*/
0, /*tp_setattr*/
(cmpfunc)code_compare, /*tp_compare*/
(reprfunc)code_repr, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
(hashfunc)code_hash, /*tp_hash*/
};
#define NAME_CHARS \
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
PyCodeObject *
PyCode_New(argcount, nlocals, stacksize, flags,
code, consts, names, varnames, filename, name,
firstlineno, lnotab)
int argcount;
int nlocals;
int stacksize;
int flags;
PyObject *code;
PyObject *consts;
PyObject *names;
PyObject *varnames;
PyObject *filename;
PyObject *name;
int firstlineno;
PyObject *lnotab;
{
PyCodeObject *co;
int i;
PyBufferProcs *pb;
/* Check argument types */
if (argcount < 0 || nlocals < 0 ||
code == NULL ||
consts == NULL || !PyTuple_Check(consts) ||
names == NULL || !PyTuple_Check(names) ||
varnames == NULL || !PyTuple_Check(varnames) ||
name == NULL || !PyString_Check(name) ||
filename == NULL || !PyString_Check(filename) ||
lnotab == NULL || !PyString_Check(lnotab)) {
PyErr_BadInternalCall();
return NULL;
}
pb = code->ob_type->tp_as_buffer;
if (pb == NULL ||
pb->bf_getreadbuffer == NULL ||
pb->bf_getsegcount == NULL ||
(*pb->bf_getsegcount)(code, NULL) != 1)
{
PyErr_BadInternalCall();
return NULL;
}
/* Make sure names and varnames are all strings, & intern them */
for (i = PyTuple_Size(names); --i >= 0; ) {
PyObject *v = PyTuple_GetItem(names, i);
if (v == NULL || !PyString_Check(v)) {
PyErr_BadInternalCall();
return NULL;
}
PyString_InternInPlace(&PyTuple_GET_ITEM(names, i));
}
for (i = PyTuple_Size(varnames); --i >= 0; ) {
PyObject *v = PyTuple_GetItem(varnames, i);
if (v == NULL || !PyString_Check(v)) {
PyErr_BadInternalCall();
return NULL;
}
PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i));
}
/* Intern selected string constants */
for (i = PyTuple_Size(consts); --i >= 0; ) {
PyObject *v = PyTuple_GetItem(consts, i);
char *p;
if (!PyString_Check(v))
continue;
p = PyString_AsString(v);
if (strspn(p, NAME_CHARS)
!= (size_t)PyString_Size(v))
continue;
PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
}
co = PyObject_NEW(PyCodeObject, &PyCode_Type);
if (co != NULL) {
co->co_argcount = argcount;
co->co_nlocals = nlocals;
co->co_stacksize = stacksize;
co->co_flags = flags;
Py_INCREF(code);
co->co_code = code;
Py_INCREF(consts);
co->co_consts = consts;
Py_INCREF(names);
co->co_names = names;
Py_INCREF(varnames);
co->co_varnames = varnames;
Py_INCREF(filename);
co->co_filename = filename;
Py_INCREF(name);
co->co_name = name;
co->co_firstlineno = firstlineno;
Py_INCREF(lnotab);
co->co_lnotab = lnotab;
}
return co;
}
/* Data structure used internally */
struct compiling {
PyObject *c_code; /* string */
PyObject *c_consts; /* list of objects */
PyObject *c_const_dict; /* inverse of c_consts */
PyObject *c_names; /* list of strings (names) */
PyObject *c_name_dict; /* inverse of c_names */
PyObject *c_globals; /* dictionary (value=None) */
PyObject *c_locals; /* dictionary (value=localID) */
PyObject *c_varnames; /* list (inverse of c_locals) */
int c_nlocals; /* index of next local */
int c_argcount; /* number of top-level arguments */
int c_flags; /* same as co_flags */
int c_nexti; /* index into c_code */
int c_errors; /* counts errors occurred */
int c_infunction; /* set when compiling a function */
int c_interactive; /* generating code for interactive command */
int c_loops; /* counts nested loops */
int c_begin; /* begin of current loop, for 'continue' */
int c_block[CO_MAXBLOCKS]; /* stack of block types */
int c_nblocks; /* current block stack level */
char *c_filename; /* filename of current node */
char *c_name; /* name of object (e.g. function) */
int c_lineno; /* Current line number */
int c_stacklevel; /* Current stack level */
int c_maxstacklevel; /* Maximum stack level */
int c_firstlineno;
PyObject *c_lnotab; /* Table mapping address to line number */
int c_last_addr, c_last_line, c_lnotab_next;
#ifdef PRIVATE_NAME_MANGLING
char *c_private; /* for private name mangling */
#endif
};
/* Error message including line number */
static void
com_error(c, exc, msg)
struct compiling *c;
PyObject *exc;
char *msg;
{
size_t n = strlen(msg);
PyObject *v;
char buffer[30];
char *s;
c->c_errors++;
if (c->c_lineno <= 1) {
/* Unknown line number or single interactive command */
PyErr_SetString(exc, msg);
return;
}
sprintf(buffer, " (line %d)", c->c_lineno);
v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer));
if (v == NULL)
return; /* MemoryError, too bad */
s = PyString_AS_STRING((PyStringObject *)v);
strcpy(s, msg);
strcat(s, buffer);
PyErr_SetObject(exc, v);
Py_DECREF(v);
}
/* Interface to the block stack */
static void
block_push(c, type)
struct compiling *c;
int type;
{
if (c->c_nblocks >= CO_MAXBLOCKS) {
com_error(c, PyExc_SystemError,
"too many statically nested blocks");
}
else {
c->c_block[c->c_nblocks++] = type;
}
}
static void
block_pop(c, type)
struct compiling *c;
int type;
{
if (c->c_nblocks > 0)
c->c_nblocks--;
if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
com_error(c, PyExc_SystemError, "bad block pop");
}
}
/* Prototype forward declarations */
static int com_init Py_PROTO((struct compiling *, char *));
static void com_free Py_PROTO((struct compiling *));
static void com_push Py_PROTO((struct compiling *, int));
static void com_pop Py_PROTO((struct compiling *, int));
static void com_done Py_PROTO((struct compiling *));
static void com_node Py_PROTO((struct compiling *, struct _node *));
static void com_factor Py_PROTO((struct compiling *, struct _node *));
static void com_addbyte Py_PROTO((struct compiling *, int));
static void com_addint Py_PROTO((struct compiling *, int));
static void com_addoparg Py_PROTO((struct compiling *, int, int));
static void com_addfwref Py_PROTO((struct compiling *, int, int *));
static void com_backpatch Py_PROTO((struct compiling *, int));
static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *, PyObject *));
static int com_addconst Py_PROTO((struct compiling *, PyObject *));
static int com_addname Py_PROTO((struct compiling *, PyObject *));
static void com_addopname Py_PROTO((struct compiling *, int, node *));
static void com_list Py_PROTO((struct compiling *, node *, int));
static int com_argdefs Py_PROTO((struct compiling *, node *));
static int com_newlocal Py_PROTO((struct compiling *, char *));
static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *));
static PyCodeObject *jcompile Py_PROTO((struct _node *, char *,
struct compiling *));
static PyObject *parsestrplus Py_PROTO((node *));
static PyObject *parsestr Py_PROTO((char *));
static int
com_init(c, filename)
struct compiling *c;
char *filename;
{
memset((void *)c, '\0', sizeof(struct compiling));
if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
1000)) == NULL)
goto fail;
if ((c->c_consts = PyList_New(0)) == NULL)
goto fail;
if ((c->c_const_dict = PyDict_New()) == NULL)
goto fail;
if ((c->c_names = PyList_New(0)) == NULL)
goto fail;
if ((c->c_name_dict = PyDict_New()) == NULL)
goto fail;
if ((c->c_globals = PyDict_New()) == NULL)
goto fail;
if ((c->c_locals = PyDict_New()) == NULL)
goto fail;
if ((c->c_varnames = PyList_New(0)) == NULL)
goto fail;
if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
1000)) == NULL)
goto fail;
c->c_nlocals = 0;
c->c_argcount = 0;
c->c_flags = 0;
c->c_nexti = 0;
c->c_errors = 0;
c->c_infunction = 0;
c->c_interactive = 0;
c->c_loops = 0;
c->c_begin = 0;
c->c_nblocks = 0;
c->c_filename = filename;
c->c_name = "?";
c->c_lineno = 0;
c->c_stacklevel = 0;
c->c_maxstacklevel = 0;
c->c_firstlineno = 0;
c->c_last_addr = 0;
c->c_last_line = 0;
c-> c_lnotab_next = 0;
return 1;
fail:
com_free(c);
return 0;
}
static void
com_free(c)
struct compiling *c;
{
Py_XDECREF(c->c_code);
Py_XDECREF(c->c_consts);
Py_XDECREF(c->c_const_dict);
Py_XDECREF(c->c_names);
Py_XDECREF(c->c_name_dict);
Py_XDECREF(c->c_globals);
Py_XDECREF(c->c_locals);
Py_XDECREF(c->c_varnames);
Py_XDECREF(c->c_lnotab);
}
static void
com_push(c, n)
struct compiling *c;
int n;
{
c->c_stacklevel += n;
if (c->c_stacklevel > c->c_maxstacklevel)
c->c_maxstacklevel = c->c_stacklevel;
}
static void
com_pop(c, n)
struct compiling *c;
int n;
{
if (c->c_stacklevel < n) {
/* fprintf(stderr,
"%s:%d: underflow! nexti=%d, level=%d, n=%d\n",
c->c_filename, c->c_lineno,
c->c_nexti, c->c_stacklevel, n); */
c->c_stacklevel = 0;
}
else
c->c_stacklevel -= n;
}
static void
com_done(c)
struct compiling *c;
{
if (c->c_code != NULL)
_PyString_Resize(&c->c_code, c->c_nexti);
if (c->c_lnotab != NULL)
_PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
}
static void
com_addbyte(c, byte)
struct compiling *c;
int byte;
{
int len;
/*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
if (byte < 0 || byte > 255) {
/*
fprintf(stderr, "XXX compiling bad byte: %d\n", byte);
fatal("com_addbyte: byte out of range");
*/
com_error(c, PyExc_SystemError,
"com_addbyte: byte out of range");
}
if (c->c_code == NULL)
return;
len = PyString_Size(c->c_code);
if (c->c_nexti >= len) {
if (_PyString_Resize(&c->c_code, len+1000) != 0) {
c->c_errors++;
return;
}
}
PyString_AsString(c->c_code)[c->c_nexti++] = byte;
}
static void
com_addint(c, x)
struct compiling *c;
int x;
{
com_addbyte(c, x & 0xff);
com_addbyte(c, x >> 8); /* XXX x should be positive */
}
static void
com_add_lnotab(c, addr, line)
struct compiling *c;
int addr;
int line;
{
int size;
char *p;
if (c->c_lnotab == NULL)
return;
size = PyString_Size(c->c_lnotab);
if (c->c_lnotab_next+2 > size) {
if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) {
c->c_errors++;
return;
}
}
p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next;
*p++ = addr;
*p++ = line;
c->c_lnotab_next += 2;
}
static void
com_set_lineno(c, lineno)
struct compiling *c;
int lineno;
{
c->c_lineno = lineno;
if (c->c_firstlineno == 0) {
c->c_firstlineno = c->c_last_line = lineno;
}
else {
int incr_addr = c->c_nexti - c->c_last_addr;
int incr_line = lineno - c->c_last_line;
while (incr_addr > 0 || incr_line > 0) {
int trunc_addr = incr_addr;
int trunc_line = incr_line;
if (trunc_addr > 255)
trunc_addr = 255;
if (trunc_line > 255)
trunc_line = 255;
com_add_lnotab(c, trunc_addr, trunc_line);
incr_addr -= trunc_addr;
incr_line -= trunc_line;
}
c->c_last_addr = c->c_nexti;
c->c_last_line = lineno;
}
}
static void
com_addoparg(c, op, arg)
struct compiling *c;
int op;
int arg;
{
if (op == SET_LINENO) {
com_set_lineno(c, arg);
if (Py_OptimizeFlag)
return;
}
com_addbyte(c, op);
com_addint(c, arg);
}
static void
com_addfwref(c, op, p_anchor)
struct compiling *c;
int op;
int *p_anchor;
{
/* Compile a forward reference for backpatching */
int here;
int anchor;
com_addbyte(c, op);
here = c->c_nexti;
anchor = *p_anchor;
*p_anchor = here;
com_addint(c, anchor == 0 ? 0 : here - anchor);
}
static void
com_backpatch(c, anchor)
struct compiling *c;
int anchor; /* Must be nonzero */
{
unsigned char *code = (unsigned char *) PyString_AsString(c->c_code);
int target = c->c_nexti;
int dist;
int prev;
for (;;) {
/* Make the JUMP instruction at anchor point to target */
prev = code[anchor] + (code[anchor+1] << 8);
dist = target - (anchor+2);
code[anchor] = dist & 0xff;
code[anchor+1] = dist >> 8;
if (!prev)
break;
anchor -= prev;
}
}
/* Handle literals and names uniformly */
static int
com_add(c, list, dict, v)
struct compiling *c;
PyObject *list;
PyObject *dict;
PyObject *v;
{
PyObject *w, *t, *np=NULL;
long n;
t = Py_BuildValue("(OO)", v, v->ob_type);
if (t == NULL)
goto fail;
w = PyDict_GetItem(dict, t);
if (w != NULL) {
n = PyInt_AsLong(w);
} else {
n = PyList_Size(list);
np = PyInt_FromLong(n);
if (np == NULL)
goto fail;
if (PyList_Append(list, v) != 0)
goto fail;
if (PyDict_SetItem(dict, t, np) != 0)
goto fail;
Py_DECREF(np);
}
Py_DECREF(t);
return n;
fail:
Py_XDECREF(np);
Py_XDECREF(t);
c->c_errors++;
return 0;
}
static int
com_addconst(c, v)
struct compiling *c;
PyObject *v;
{
return com_add(c, c->c_consts, c->c_const_dict, v);
}
static int
com_addname(c, v)
struct compiling *c;
PyObject *v;
{
return com_add(c, c->c_names, c->c_name_dict, v);
}
#ifdef PRIVATE_NAME_MANGLING
static int
com_mangle(c, name, buffer, maxlen)
struct compiling *c;
char *name;
char *buffer;
size_t maxlen;
{
/* Name mangling: __private becomes _classname__private.
This is independent from how the name is used. */
char *p;
size_t nlen, plen;
nlen = strlen(name);
if (nlen+2 >= maxlen)
return 0; /* Don't mangle __extremely_long_names */
if (name[nlen-1] == '_' && name[nlen-2] == '_')
return 0; /* Don't mangle __whatever__ */
p = c->c_private;
/* Strip leading underscores from class name */
while (*p == '_')
p++;
if (*p == '\0')
return 0; /* Don't mangle if class is just underscores */
plen = strlen(p);
if (plen + nlen >= maxlen)
plen = maxlen-nlen-2; /* Truncate class name if too long */
/* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
buffer[0] = '_';
strncpy(buffer+1, p, plen);
strcpy(buffer+1+plen, name);
/* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
return 1;
}
#endif
static void
com_addopnamestr(c, op, name)
struct compiling *c;
int op;
char *name;
{
PyObject *v;
int i;
#ifdef PRIVATE_NAME_MANGLING
char buffer[256];
if (name != NULL && name[0] == '_' && name[1] == '_' &&
c->c_private != NULL &&
com_mangle(c, name, buffer, sizeof(buffer)))
name = buffer;
#endif
if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
c->c_errors++;
i = 255;
}
else {
i = com_addname(c, v);
Py_DECREF(v);
}
/* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */
switch (op) {
case LOAD_NAME:
case STORE_NAME:
case DELETE_NAME:
if (PyDict_GetItemString(c->c_globals, name) != NULL) {
switch (op) {
case LOAD_NAME: op = LOAD_GLOBAL; break;
case STORE_NAME: op = STORE_GLOBAL; break;
case DELETE_NAME: op = DELETE_GLOBAL; break;
}
}
}
com_addoparg(c, op, i);
}
static void
com_addopname(c, op, n)
struct compiling *c;
int op;
node *n;
{
char *name;
char buffer[1000];
/* XXX it is possible to write this code without the 1000
chars on the total length of dotted names, I just can't be
bothered right now */
if (TYPE(n) == STAR)
name = "*";
else if (TYPE(n) == dotted_name) {
char *p = buffer;
int i;
name = buffer;
for (i = 0; i < NCH(n); i += 2) {
char *s = STR(CHILD(n, i));
if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
com_error(c, PyExc_MemoryError,
"dotted_name too long");
name = NULL;
break;
}
if (p != buffer)
*p++ = '.';
strcpy(p, s);
p = strchr(p, '\0');
}
}
else {
REQ(n, NAME);
name = STR(n);
}
com_addopnamestr(c, op, name);
}
static PyObject *
parsenumber(co, s)
struct compiling *co;
char *s;
{
extern double atof Py_PROTO((const char *));
char *end;
long x;
double dx;
#ifndef WITHOUT_COMPLEX
Py_complex c;
int imflag;
#endif
errno = 0;
end = s + strlen(s) - 1;
#ifndef WITHOUT_COMPLEX
imflag = *end == 'j' || *end == 'J';
#endif
if (*end == 'l' || *end == 'L')
return PyLong_FromString(s, (char **)0, 0);
if (s[0] == '0')
x = (long) PyOS_strtoul(s, &end, 0);
else
x = PyOS_strtol(s, &end, 0);
if (*end == '\0') {
if (errno != 0) {
com_error(co, PyExc_OverflowError,
"integer literal too large");
return NULL;
}
return PyInt_FromLong(x);
}
/* XXX Huge floats may silently fail */
#ifndef WITHOUT_COMPLEX
if (imflag) {
c.real = 0.;
PyFPE_START_PROTECT("atof", return 0)
c.imag = atof(s);
PyFPE_END_PROTECT(c)
return PyComplex_FromCComplex(c);
}
else
#endif
{
PyFPE_START_PROTECT("atof", return 0)
dx = atof(s);
PyFPE_END_PROTECT(dx)
return PyFloat_FromDouble(dx);
}
}
static PyObject *
parsestr(s)
char *s;
{
PyObject *v;
size_t len;
char *buf;
char *p;
char *end;
int c;
int first = *s;
int quote = first;
int rawmode = 0;
int unicode = 0;
if (isalpha(quote) || quote == '_') {
if (quote == 'u' || quote == 'U') {
quote = *++s;
unicode = 1;
}
if (quote == 'r' || quote == 'R') {
quote = *++s;
rawmode = 1;
}
}
if (quote != '\'' && quote != '\"') {
PyErr_BadInternalCall();
return NULL;
}
s++;
len = strlen(s);
if (len > INT_MAX) {
PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
return NULL;
}
if (s[--len] != quote) {
PyErr_BadInternalCall();
return NULL;
}
if (len >= 4 && s[0] == quote && s[1] == quote) {
s += 2;
len -= 2;
if (s[--len] != quote || s[--len] != quote) {
PyErr_BadInternalCall();
return NULL;
}
}
if (unicode || Py_UnicodeFlag) {
if (rawmode)
return PyUnicode_DecodeRawUnicodeEscape(
s, len, NULL);
else
return PyUnicode_DecodeUnicodeEscape(
s, len, NULL);
}
else if (rawmode || strchr(s, '\\') == NULL) {
return PyString_FromStringAndSize(s, len);
}
v = PyString_FromStringAndSize((char *)NULL, len);
p = buf = PyString_AsString(v);
end = s + len;
while (s < end) {
if (*s != '\\') {
*p++ = *s++;
continue;
}
s++;
switch (*s++) {
/* XXX This assumes ASCII! */
case '\n': break;
case '\\': *p++ = '\\'; break;
case '\'': *p++ = '\''; break;
case '\"': *p++ = '\"'; break;
case 'b': *p++ = '\b'; break;
case 'f': *p++ = '\014'; break; /* FF */
case 't': *p++ = '\t'; break;
case 'n': *p++ = '\n'; break;
case 'r': *p++ = '\r'; break;
case 'v': *p++ = '\013'; break; /* VT */
case 'a': *p++ = '\007'; break; /* BEL, not classic C */
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
c = s[-1] - '0';
if ('0' <= *s && *s <= '7') {
c = (c<<3) + *s++ - '0';
if ('0' <= *s && *s <= '7')
c = (c<<3) + *s++ - '0';
}
*p++ = c;
break;
case 'x':
if (isxdigit(Py_CHARMASK(*s))) {
unsigned int x = 0;
do {
c = Py_CHARMASK(*s);
s++;
x = (x<<4) & ~0xF;
if (isdigit(c))
x += c - '0';
else if (islower(c))
x += 10 + c - 'a';
else
x += 10 + c - 'A';
} while (isxdigit(Py_CHARMASK(*s)));
*p++ = x;
break;
}
/* FALLTHROUGH */
default: *p++ = '\\'; *p++ = s[-1]; break;
}
}
_PyString_Resize(&v, (int)(p - buf));
return v;
}
static PyObject *
parsestrplus(n)
node *n;
{
PyObject *v;
int i;
REQ(CHILD(n, 0), STRING);
if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) {
/* String literal concatenation */
for (i = 1; i < NCH(n); i++) {
PyObject *s;
s = parsestr(STR(CHILD(n, i)));
if (s == NULL)
goto onError;
if (PyString_Check(v) && PyString_Check(s)) {
PyString_ConcatAndDel(&v, s);
if (v == NULL)
goto onError;
}
else {
PyObject *temp;