Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
2b5141a7
Commit
2b5141a7
authored
Dec 09, 2016
by
Boxiang Sun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[WIP]custom builtin support
parent
d915f9e4
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
142 additions
and
38 deletions
+142
-38
src/codegen/irgen/hooks.cpp
src/codegen/irgen/hooks.cpp
+23
-17
src/core/types.h
src/core/types.h
+2
-0
src/runtime/builtin_modules/sys.cpp
src/runtime/builtin_modules/sys.cpp
+2
-1
src/runtime/frame.cpp
src/runtime/frame.cpp
+46
-1
src/runtime/objmodel.cpp
src/runtime/objmodel.cpp
+68
-19
src/runtime/types.cpp
src/runtime/types.cpp
+1
-0
No files found.
src/codegen/irgen/hooks.cpp
View file @
2b5141a7
...
@@ -384,23 +384,29 @@ static void pickGlobalsAndLocals(Box*& globals, Box*& locals) {
...
@@ -384,23 +384,29 @@ static void pickGlobalsAndLocals(Box*& globals, Box*& locals) {
RELEASE_ASSERT
(
globals
&&
(
globals
->
cls
==
module_cls
||
globals
->
cls
==
dict_cls
),
"Unspported globals type: %s"
,
RELEASE_ASSERT
(
globals
&&
(
globals
->
cls
==
module_cls
||
globals
->
cls
==
dict_cls
),
"Unspported globals type: %s"
,
globals
?
globals
->
cls
->
tp_name
:
"NULL"
);
globals
?
globals
->
cls
->
tp_name
:
"NULL"
);
//
// if (globals) {
if
(
globals
)
{
// // From CPython (they set it to be f->f_builtins):
// From CPython (they set it to be f->f_builtins):
// Box* globals_dict;
Box
*
globals_dict
;
// if (globals->cls == module_cls)
if
(
globals
->
cls
==
module_cls
)
// globals_dict = globals->getAttrWrapper();
globals_dict
=
globals
->
getAttrWrapper
();
// else
else
// globals_dict = globals;
globals_dict
=
globals
;
//
// auto requested_builtins = PyDict_GetItemString(globals_dict, "__builtins__");
auto
requested_builtins
=
PyDict_GetItemString
(
globals_dict
,
"__builtins__"
);
// if (requested_builtins == NULL)
if
(
requested_builtins
==
NULL
)
// PyDict_SetItemString(globals_dict, "__builtins__", PyEval_GetBuiltins());
PyDict_SetItemString
(
globals_dict
,
"__builtins__"
,
PyEval_GetBuiltins
());
// else
else
// RELEASE_ASSERT(requested_builtins == builtins_module
// ((FrameInfo*)cur_thread_state.frame_info)->builtins = requested_builtins;
// || requested_builtins == builtins_module->getAttrWrapper(),
PyDict_SetItemString
(
globals_dict
,
"__builtins__"
,
requested_builtins
);
// "we don't support overriding __builtins__");
// ((FrameInfo*)cur_thread_state.frame_info)->builtins = globals_dict;
// }
// if (requested_builtins != builtins_module
// && requested_builtins != builtins_module->getAttrWrapper()) {
// PyDict_SetItemString(globals_dict, "__builtins__", requested_builtins);
// builtins_module->clearAttrsForDealloc();
// builtins_module = (BoxedModule*)requested_builtins;
// }
}
}
}
extern
"C"
PyObject
*
PyEval_EvalCode
(
PyCodeObject
*
co
,
PyObject
*
globals
,
PyObject
*
locals
)
noexcept
{
extern
"C"
PyObject
*
PyEval_EvalCode
(
PyCodeObject
*
co
,
PyObject
*
globals
,
PyObject
*
locals
)
noexcept
{
...
...
src/core/types.h
View file @
2b5141a7
...
@@ -1053,6 +1053,8 @@ struct FrameInfo {
...
@@ -1053,6 +1053,8 @@ struct FrameInfo {
FrameInfo
*
back
;
FrameInfo
*
back
;
// TODO does this need to be owned? how does cpython do it?
// TODO does this need to be owned? how does cpython do it?
BORROWED
(
BoxedCode
*
)
code
;
BORROWED
(
BoxedCode
*
)
code
;
// What type of this field should be?
BORROWED
(
Box
*
)
builtins
;
BORROWED
(
Box
*
)
updateBoxedLocals
();
BORROWED
(
Box
*
)
updateBoxedLocals
();
...
...
src/runtime/builtin_modules/sys.cpp
View file @
2b5141a7
...
@@ -490,7 +490,8 @@ static PyObject* sys_displayhook(PyObject* self, PyObject* o) noexcept {
...
@@ -490,7 +490,8 @@ static PyObject* sys_displayhook(PyObject* self, PyObject* o) noexcept {
return NULL;
return NULL;
}
}
*/
*/
PyObject
*
builtins
=
builtins_module
;
// PyObject* builtins = builtins_module;
PyObject
*
builtins
=
((
FrameInfo
*
)
cur_thread_state
.
frame_info
)
->
builtins
;
/* Print value except if None */
/* Print value except if None */
/* After printing, also assign to '_' */
/* After printing, also assign to '_' */
...
...
src/runtime/frame.cpp
View file @
2b5141a7
...
@@ -235,9 +235,53 @@ void frameInvalidateBack(BoxedFrame* frame) {
...
@@ -235,9 +235,53 @@ void frameInvalidateBack(BoxedFrame* frame) {
}
}
extern
"C"
void
initFrame
(
FrameInfo
*
frame_info
)
{
extern
"C"
void
initFrame
(
FrameInfo
*
frame_info
)
{
frame_info
->
back
=
(
FrameInfo
*
)(
cur_thread_state
.
frame_info
);
FrameInfo
*
back
=
(
FrameInfo
*
)(
cur_thread_state
.
frame_info
);
frame_info
->
back
=
back
;
cur_thread_state
.
frame_info
=
frame_info
;
cur_thread_state
.
frame_info
=
frame_info
;
Box
*
builtins
;
static
BoxedString
*
builtins_str
=
getStaticString
(
"__builtins__"
);
if
(
back
==
NULL
||
back
->
globals
!=
frame_info
->
globals
)
{
if
(
PyModule_Check
(
frame_info
->
globals
))
builtins
=
frame_info
->
globals
->
getattr
(
builtins_str
);
else
builtins
=
PyDict_GetItem
(
frame_info
->
globals
,
builtins_str
);
if
(
builtins
&&
builtins
->
cls
==
attrwrapper_cls
)
builtins
=
unwrapAttrWrapper
(
builtins
);
if
(
builtins
==
NULL
)
{
builtins
=
PyDict_New
();
if
(
builtins
==
NULL
||
PyDict_SetItemString
(
builtins
,
"None"
,
Py_None
)
<
0
)
RELEASE_ASSERT
(
0
,
"error"
);
}
else
Py_INCREF
(
builtins
);
}
else
{
builtins
=
incref
(
back
->
builtins
);
}
frame_info
->
builtins
=
builtins
;
}
}
// Box* global_dict = PyModule_GetDict(frame_info->globals);
// builtins = PyDict_GetItem(global_dict, builtins_str);
// if (builtins) {
// if (PyModule_Check(builtins)) {
// builtins = PyModule_GetDict(builtins);
// assert(!builtins || PyDict_Check(builtins) || builtins->cls != attrwrapper_cls);
// } else if (!PyDict_Check(builtins) && builtins->cls != attrwrapper_cls)
// builtins = NULL;
// }
// if (builtins == NULL) {
// builtins = PyDict_New();
// if (builtins == NULL || PyDict_SetItemString(builtins, "None", Py_None) < 0)
// return;
// } else
// Py_INCREF(builtins);
// } else {
// builtins = builtins_module;
// // builtins = back->builtins;
// // assert(builtins != NULL && PyDict_Check
// }
// frame_info->builtins = builtins;
// }
FrameInfo
*
const
FrameInfo
::
NO_DEINIT
=
(
FrameInfo
*
)
-
2
;
// not -1 to not match memset(-1)
FrameInfo
*
const
FrameInfo
::
NO_DEINIT
=
(
FrameInfo
*
)
-
2
;
// not -1 to not match memset(-1)
...
@@ -303,6 +347,7 @@ extern "C" void deinitFrame(FrameInfo* frame_info) noexcept {
...
@@ -303,6 +347,7 @@ extern "C" void deinitFrame(FrameInfo* frame_info) noexcept {
Py_CLEAR
(
frame_info
->
boxedLocals
);
Py_CLEAR
(
frame_info
->
boxedLocals
);
Py_CLEAR
(
frame_info
->
globals
);
Py_CLEAR
(
frame_info
->
globals
);
Py_CLEAR
(
frame_info
->
builtins
);
assert
(
!
PyErr_Occurred
());
assert
(
!
PyErr_Occurred
());
if
(
restore_exc
)
if
(
restore_exc
)
...
...
src/runtime/objmodel.cpp
View file @
2b5141a7
...
@@ -7569,24 +7569,28 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) {
...
@@ -7569,24 +7569,28 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) {
static
StatCounter
stat_builtins
(
"getglobal_builtins"
);
static
StatCounter
stat_builtins
(
"getglobal_builtins"
);
stat_builtins
.
log
();
stat_builtins
.
log
();
Box
*
builtins
=
((
FrameInfo
*
)
cur_thread_state
.
frame_info
)
->
builtins
;
if
(
builtins
->
cls
==
module_cls
)
{
Box
*
rtn
;
Box
*
rtn
;
if
(
rewriter
.
get
())
{
RewriterVar
*
builtins
=
rewriter
->
loadConst
((
intptr_t
)
builtins_module
,
Location
::
any
());
if
(
rewriter
.
get
()
&&
builtins
==
builtins_module
)
{
GetattrRewriteArgs
rewrite_args
(
rewriter
.
get
(),
builtins
,
rewriter
->
getReturnDestination
());
RewriterVar
*
r_builtins
=
rewriter
->
loadConst
((
intptr_t
)
builtins
,
Location
::
any
());
GetattrRewriteArgs
rewrite_args
(
rewriter
.
get
(),
r_builtins
,
rewriter
->
getReturnDestination
());
rewrite_args
.
obj_shape_guarded
=
true
;
// always builtin module
rewrite_args
.
obj_shape_guarded
=
true
;
// always builtin module
rtn
=
builtins_module
->
getattr
(
name
,
&
rewrite_args
);
rtn
=
builtins
->
getattr
(
name
,
&
rewrite_args
);
if
(
!
rewrite_args
.
isSuccessful
())
if
(
!
rewrite_args
.
isSuccessful
())
rewriter
.
reset
(
NULL
);
rewriter
.
reset
(
NULL
);
else
if
(
rtn
)
{
else
if
(
rtn
)
{
auto
r_rtn
=
rewrite_args
.
getReturn
(
ReturnConvention
::
HAS_RETURN
);
auto
r_rtn
=
rewrite_args
.
getReturn
(
ReturnConvention
::
HAS_RETURN
);
rewriter
->
commitReturning
(
r_rtn
);
rewriter
->
commitReturning
(
r_rtn
);
}
else
{
}
else
{
rewrite_args
.
getReturn
();
// just to
make the asserts happy
rewrite_args
.
getReturn
();
// just
make the asserts happy
rewriter
.
reset
(
NULL
);
rewriter
.
reset
(
NULL
);
}
}
}
else
{
}
else
{
rtn
=
builtins_module
->
getattr
(
name
);
rtn
=
builtins
->
getattr
(
name
);
}
}
if
(
rtn
)
{
if
(
rtn
)
{
...
@@ -7594,6 +7598,51 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) {
...
@@ -7594,6 +7598,51 @@ extern "C" Box* getGlobal(Box* globals, BoxedString* name) {
Py_INCREF
(
rtn
);
Py_INCREF
(
rtn
);
return
rtn
;
return
rtn
;
}
}
}
else
{
ASSERT
(
builtins
->
cls
==
dict_cls
||
builtins
->
cls
==
attrwrapper_cls
,
"%s"
,
builtins
->
cls
->
tp_name
);
BoxedDict
*
d
=
static_cast
<
BoxedDict
*>
(
builtins
);
rewriter
.
reset
(
NULL
);
REWRITE_ABORTED
(
"Rewriting not implemented for getGlobals with a dict builtins yet"
);
auto
it
=
d
->
d
.
find
(
name
);
if
(
it
!=
d
->
d
.
end
())
{
assert
(
it
->
second
->
ob_refcnt
>
0
);
Py_INCREF
(
it
->
second
);
return
it
->
second
;
}
}
// Box* rtn;
// if (rewriter.get()) {
// RewriterVar* builtins = rewriter->loadConst((intptr_t)((FrameInfo*)cur_thread_state.frame_info)->builtins, Location::any());
// // RewriterVar* builtins = rewriter->loadConst((intptr_t)builtins_module, Location::any());
// GetattrRewriteArgs rewrite_args(rewriter.get(), builtins, rewriter->getReturnDestination());
// rewrite_args.obj_shape_guarded = true; // always builtin module
// rtn = ((FrameInfo*)cur_thread_state.frame_info)->builtins->getattr(name, &rewrite_args);
// // rtn = builtins_module->getattr(name, &rewrite_args);
//
// if (!rewrite_args.isSuccessful())
// rewriter.reset(NULL);
// else if (rtn) {
// auto r_rtn = rewrite_args.getReturn(ReturnConvention::HAS_RETURN);
// rewriter->commitReturning(r_rtn);
// } else {
// rewrite_args.getReturn(); // just to make the asserts happy
// rewriter.reset(NULL);
// }
// } else {
// rtn = ((FrameInfo*)cur_thread_state.frame_info)->builtins->getattr(name);
// // rtn = builtins_module->getattr(name);
// }
//
// if (rtn) {
// assert(rtn->ob_refcnt > 0);
// Py_INCREF(rtn);
// return rtn;
// } else {
// raiseExcHelper(NameError, "name '%s' is not defined", name->data());
// }
}
}
assert
(
name
->
data
()[
name
->
size
()]
==
'\0'
);
assert
(
name
->
data
()[
name
->
size
()]
==
'\0'
);
...
...
src/runtime/types.cpp
View file @
2b5141a7
...
@@ -4814,6 +4814,7 @@ BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn, const cha
...
@@ -4814,6 +4814,7 @@ BORROWED(BoxedModule*) createModule(BoxedString* name, const char* fn, const cha
if
(
name
->
s
()
==
"__main__"
)
if
(
name
->
s
()
==
"__main__"
)
module
->
setattr
(
autoDecref
(
internStringMortal
(
"__builtins__"
)),
builtins_module
,
NULL
);
module
->
setattr
(
autoDecref
(
internStringMortal
(
"__builtins__"
)),
builtins_module
,
NULL
);
// module->setattr(autoDecref(internStringMortal("__builtins__")), builtins_module, NULL);
return
module
;
return
module
;
}
}
...
...
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