Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
282914b7
Kaydet (Commit)
282914b7
authored
Nis 04, 1991
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added error checking for numeric constants; added local/global variable
optimization.
üst
572fd57b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
4 deletions
+112
-4
compile.c
Python/compile.c
+112
-4
No files found.
Python/compile.c
Dosyayı görüntüle @
282914b7
...
...
@@ -41,6 +41,8 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <ctype.h>
extern
int
errno
;
#define OFF(x) offsetof(codeobject, x)
static
struct
memberlist
code_memberlist
[]
=
{
...
...
@@ -349,14 +351,28 @@ parsenumber(s)
char
*
s
;
{
extern
long
strtol
();
extern
double
atof
();
extern
double
strtod
();
char
*
end
=
s
;
long
x
;
double
xx
;
errno
=
0
;
x
=
strtol
(
s
,
&
end
,
0
);
if
(
*
end
==
'\0'
)
if
(
*
end
==
'\0'
)
{
if
(
errno
!=
0
)
{
err_setstr
(
RuntimeError
,
"integer constant too large"
);
return
NULL
;
}
return
newintobject
(
x
);
if
(
*
end
==
'.'
||
*
end
==
'e'
||
*
end
==
'E'
)
return
newfloatobject
(
atof
(
s
));
}
errno
=
0
;
xx
=
strtod
(
s
,
&
end
);
if
(
*
end
==
'\0'
)
{
if
(
errno
!=
0
)
{
err_setstr
(
RuntimeError
,
"float constant too large"
);
return
NULL
;
}
return
newfloatobject
(
xx
);
}
err_setstr
(
RuntimeError
,
"bad number syntax"
);
return
NULL
;
}
...
...
@@ -1752,6 +1768,96 @@ compile_node(c, n)
}
}
/* Optimization for local and global variables.
Attempt to replace all LOAD_NAME instructions that refer to a local
variable with LOAD_LOCAL instructions, and all that refer to a global
variable with LOAD_GLOBAL instructions.
To find all local variables, we check all STORE_NAME and IMPORT_FROM
instructions. This yields all local variables, including arguments,
function definitions, class definitions and import statements.
There is one leak: 'from foo import *' introduces local variables
that we can't know while compiling. If this is the case, LOAD_GLOBAL
instructions are not generated -- LOAD_NAME is left in place for
globals, since it first checks for globals (LOAD_LOCAL is still used
for recognized locals, since it doesn't hurt).
This optimization means that using the same name as a global and
as a local variable within the same scope is now illegal, which
is a change to the language! Also using eval() to introduce new
local variables won't work. But both were bad practice at best.
The optimization doesn't save much: basically, it saves one
unsuccessful dictionary lookup per global (or built-in) variable
reference. On the (slow!) Mac Plus, with 4 local variables,
this saving was measured to be about 0.18 ms. We might save more
by using a different data structure to hold local variables, like
an array indexed by variable number.
NB: this modifies the string object co->co_code!
*/
static
void
optimizer
(
co
)
codeobject
*
co
;
{
char
*
next_instr
,
*
cur_instr
;
object
*
locals
;
int
opcode
;
int
oparg
;
object
*
name
;
int
star_used
;
#define NEXTOP() (*next_instr++)
#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
#define GETITEM(v, i) (getlistitem((v), (i)))
#define GETNAMEOBJ(i) (GETITEM(co->co_names, (i)))
locals
=
newdictobject
();
if
(
locals
==
NULL
)
{
err_clear
();
return
;
/* For now, this is OK */
}
next_instr
=
GETSTRINGVALUE
(
co
->
co_code
);
for
(;;)
{
opcode
=
NEXTOP
();
if
(
opcode
==
STOP_CODE
)
break
;
if
(
HAS_ARG
(
opcode
))
oparg
=
NEXTARG
();
if
(
opcode
==
STORE_NAME
||
opcode
==
IMPORT_FROM
)
{
name
=
GETNAMEOBJ
(
oparg
);
if
(
dict2insert
(
locals
,
name
,
None
)
!=
0
)
{
DECREF
(
locals
);
return
;
/* Sorry */
}
}
}
star_used
=
(
dictlookup
(
locals
,
"*"
)
!=
NULL
);
next_instr
=
GETSTRINGVALUE
(
co
->
co_code
);
for
(;;)
{
cur_instr
=
next_instr
;
opcode
=
NEXTOP
();
if
(
opcode
==
STOP_CODE
)
break
;
if
(
HAS_ARG
(
opcode
))
oparg
=
NEXTARG
();
if
(
opcode
==
LOAD_NAME
)
{
name
=
GETNAMEOBJ
(
oparg
);
if
(
dictlookup
(
locals
,
getstringvalue
(
name
))
!=
NULL
)
*
cur_instr
=
LOAD_LOCAL
;
else
if
(
!
star_used
)
*
cur_instr
=
LOAD_GLOBAL
;
}
}
DECREF
(
locals
);
}
codeobject
*
compile
(
n
,
filename
)
node
*
n
;
...
...
@@ -1768,5 +1874,7 @@ compile(n, filename)
else
co
=
NULL
;
com_free
(
&
sc
);
if
(
co
!=
NULL
)
optimizer
(
co
);
return
co
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment