Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
micropython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
card10
micropython
Commits
3dea8c9e
Commit
3dea8c9e
authored
8 years ago
by
Damien George
Browse files
Options
Downloads
Patches
Plain Diff
py/scope: Use lookup-table to determine a scope's simple name.
Generates slightly smaller and more efficient code.
parent
6ab2c5e6
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
py/compile.c
+1
-1
1 addition, 1 deletion
py/compile.c
py/emitcommon.c
+4
-4
4 additions, 4 deletions
py/emitcommon.c
py/scope.c
+15
-26
15 additions, 26 deletions
py/scope.c
py/scope.h
+13
-1
13 additions, 1 deletion
py/scope.h
with
33 additions
and
32 deletions
py/compile.c
+
1
−
1
View file @
3dea8c9e
...
...
@@ -3288,7 +3288,7 @@ STATIC void scope_compute_things(scope_t *scope) {
// __class__ is not counted as a local; if it's used then it becomes a ID_INFO_KIND_CELL
continue
;
}
if
(
scope
->
kind
>=
SCOPE_FUNC
TION
&&
scope
->
kind
<=
SCOPE_GEN_EXPR
&&
id
->
kind
==
ID_INFO_KIND_GLOBAL_IMPLICIT
)
{
if
(
SCOPE_
IS_
FUNC
_LIKE
(
scope
->
kind
)
&&
id
->
kind
==
ID_INFO_KIND_GLOBAL_IMPLICIT
)
{
id
->
kind
=
ID_INFO_KIND_GLOBAL_EXPLICIT
;
}
// params always count for 1 local, even if they are a cell
...
...
This diff is collapsed.
Click to expand it.
py/emitcommon.c
+
4
−
4
View file @
3dea8c9e
...
...
@@ -50,12 +50,12 @@ void mp_emit_common_get_id_for_modification(scope_t *scope, qstr qst) {
bool
added
;
id_info_t
*
id
=
scope_find_or_add_id
(
scope
,
qst
,
&
added
);
if
(
added
)
{
if
(
scope
->
kind
==
SCOPE_MODULE
||
scope
->
kind
==
SCOPE_CLASS
)
{
id
->
kind
=
ID_INFO_KIND_GLOBAL_IMPLICIT
;
}
else
{
if
(
SCOPE_IS_FUNC_LIKE
(
scope
->
kind
))
{
id
->
kind
=
ID_INFO_KIND_LOCAL
;
}
else
{
id
->
kind
=
ID_INFO_KIND_GLOBAL_IMPLICIT
;
}
}
else
if
(
scope
->
kind
>=
SCOPE_FUNC
TION
&&
scope
->
kind
<=
SCOPE_GEN_EXPR
&&
id
->
kind
==
ID_INFO_KIND_GLOBAL_IMPLICIT
)
{
}
else
if
(
SCOPE_
IS_
FUNC
_LIKE
(
scope
->
kind
)
&&
id
->
kind
==
ID_INFO_KIND_GLOBAL_IMPLICIT
)
{
// rebind as a local variable
id
->
kind
=
ID_INFO_KIND_LOCAL
;
}
...
...
This diff is collapsed.
Click to expand it.
py/scope.c
+
15
−
26
View file @
3dea8c9e
...
...
@@ -30,37 +30,26 @@
#if MICROPY_ENABLE_COMPILER
// these low numbered qstrs should fit in 8 bits
STATIC
const
uint8_t
scope_simple_name_table
[]
=
{
[
SCOPE_MODULE
]
=
MP_QSTR__lt_module_gt_
,
[
SCOPE_LAMBDA
]
=
MP_QSTR__lt_lambda_gt_
,
[
SCOPE_LIST_COMP
]
=
MP_QSTR__lt_listcomp_gt_
,
[
SCOPE_DICT_COMP
]
=
MP_QSTR__lt_dictcomp_gt_
,
[
SCOPE_SET_COMP
]
=
MP_QSTR__lt_setcomp_gt_
,
[
SCOPE_GEN_EXPR
]
=
MP_QSTR__lt_genexpr_gt_
,
};
scope_t
*
scope_new
(
scope_kind_t
kind
,
mp_parse_node_t
pn
,
qstr
source_file
,
mp_uint_t
emit_options
)
{
scope_t
*
scope
=
m_new0
(
scope_t
,
1
);
scope
->
kind
=
kind
;
scope
->
pn
=
pn
;
scope
->
source_file
=
source_file
;
switch
(
kind
)
{
case
SCOPE_MODULE
:
scope
->
simple_name
=
MP_QSTR__lt_module_gt_
;
break
;
case
SCOPE_FUNCTION
:
case
SCOPE_CLASS
:
assert
(
MP_PARSE_NODE_IS_STRUCT
(
pn
));
scope
->
simple_name
=
MP_PARSE_NODE_LEAF_ARG
(((
mp_parse_node_struct_t
*
)
pn
)
->
nodes
[
0
]);
break
;
case
SCOPE_LAMBDA
:
scope
->
simple_name
=
MP_QSTR__lt_lambda_gt_
;
break
;
case
SCOPE_LIST_COMP
:
scope
->
simple_name
=
MP_QSTR__lt_listcomp_gt_
;
break
;
case
SCOPE_DICT_COMP
:
scope
->
simple_name
=
MP_QSTR__lt_dictcomp_gt_
;
break
;
case
SCOPE_SET_COMP
:
scope
->
simple_name
=
MP_QSTR__lt_setcomp_gt_
;
break
;
case
SCOPE_GEN_EXPR
:
scope
->
simple_name
=
MP_QSTR__lt_genexpr_gt_
;
break
;
default:
assert
(
0
);
if
(
kind
==
SCOPE_FUNCTION
||
kind
==
SCOPE_CLASS
)
{
assert
(
MP_PARSE_NODE_IS_STRUCT
(
pn
));
scope
->
simple_name
=
MP_PARSE_NODE_LEAF_ARG
(((
mp_parse_node_struct_t
*
)
pn
)
->
nodes
[
0
]);
}
else
{
scope
->
simple_name
=
scope_simple_name_table
[
kind
];
}
scope
->
raw_code
=
mp_emit_glue_new_raw_code
();
scope
->
emit_options
=
emit_options
;
...
...
This diff is collapsed.
Click to expand it.
py/scope.h
+
13
−
1
View file @
3dea8c9e
...
...
@@ -52,8 +52,20 @@ typedef struct _id_info_t {
qstr
qst
;
}
id_info_t
;
#define SCOPE_IS_FUNC_LIKE(s) ((s) >= SCOPE_LAMBDA)
// scope is a "block" in Python parlance
typedef
enum
{
SCOPE_MODULE
,
SCOPE_FUNCTION
,
SCOPE_LAMBDA
,
SCOPE_LIST_COMP
,
SCOPE_DICT_COMP
,
SCOPE_SET_COMP
,
SCOPE_GEN_EXPR
,
SCOPE_CLASS
}
scope_kind_t
;
typedef
enum
{
SCOPE_MODULE
,
SCOPE_CLASS
,
SCOPE_LAMBDA
,
SCOPE_LIST_COMP
,
SCOPE_DICT_COMP
,
SCOPE_SET_COMP
,
SCOPE_GEN_EXPR
,
SCOPE_FUNCTION
,
}
scope_kind_t
;
typedef
struct
_scope_t
{
scope_kind_t
kind
;
struct
_scope_t
*
parent
;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment