py: Implement parse bytecode.

This commit is contained in:
Damien George 2015-10-13 16:39:46 +01:00
parent 5bf649f370
commit 61398ab45a
9 changed files with 4889 additions and 14 deletions

3343
py/compile2.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -270,10 +270,12 @@ typedef struct _emit_inline_asm_method_table_t {
void (*start_pass)(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope, mp_obj_t *error_slot);
void (*end_pass)(emit_inline_asm_t *emit, mp_uint_t type_sig);
mp_uint_t (*count_params)(emit_inline_asm_t *emit, mp_uint_t n_params, mp_parse_node_t *pn_params);
void (*end_pass)(emit_inline_asm_t *emit, mp_uint_t type_sig);
mp_uint_t (*count_params)(emit_inline_asm_t *emit, const byte *p, const byte *ptop);
bool (*label)(emit_inline_asm_t *emit, mp_uint_t label_num, qstr label_id);
void (*align)(emit_inline_asm_t *emit, mp_uint_t align);
void (*data)(emit_inline_asm_t *emit, mp_uint_t bytesize, mp_uint_t val);
void (*op)(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, mp_parse_node_t *pn_args);
void (*op)(emit_inline_asm_t *emit, qstr op, mp_uint_t n_args, const byte **pn_args);
} emit_inline_asm_method_table_t;
extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table;

View File

@ -180,7 +180,7 @@ DEF_RULE(try_stmt_except_and_more, nc, and(3), rule(try_stmt_except_list), opt_r
DEF_RULE(try_stmt_except, nc, and(4), tok(KW_EXCEPT), opt_rule(try_stmt_as_name), tok(DEL_COLON), rule(suite))
DEF_RULE(try_stmt_as_name, nc, and(2), rule(test), opt_rule(as_name))
DEF_RULE(try_stmt_except_list, nc, one_or_more, rule(try_stmt_except))
DEF_RULE(try_stmt_finally, nc, and(3), tok(KW_FINALLY), tok(DEL_COLON), rule(suite))
DEF_RULE(try_stmt_finally, nc, ident | and(3), tok(KW_FINALLY), tok(DEL_COLON), rule(suite))
DEF_RULE(else_stmt, nc, ident | and(3), tok(KW_ELSE), tok(DEL_COLON), rule(suite))
DEF_RULE(with_stmt, c(with_stmt), and(4), tok(KW_WITH), rule(with_stmt_list), tok(DEL_COLON), rule(suite))
DEF_RULE(with_stmt_list, nc, list, rule(with_item), tok(DEL_COMMA))
@ -247,8 +247,7 @@ DEF_RULE(power_dbl_star, nc, ident | and(2), tok(OP_DBL_STAR), rule(factor))
// testlist_comp: (test|star_expr) ( comp_for | (',' (test|star_expr))* [','] )
// trailer: '(' [arglist] ')' | '[' subscriptlist ']' | '.' NAME
DEF_RULE(atom, nc, or(11), tok(NAME), tok(INTEGER), tok(FLOAT_OR_IMAG), rule(atom_string), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
DEF_RULE(atom_string, c(atom_string), one_or_more, rule(string_or_bytes))
DEF_RULE(atom, nc, or(12), tok(NAME), tok(INTEGER), tok(FLOAT_OR_IMAG), tok(STRING), tok(BYTES), tok(ELLIPSIS), tok(KW_NONE), tok(KW_TRUE), tok(KW_FALSE), rule(atom_paren), rule(atom_bracket), rule(atom_brace))
DEF_RULE(string_or_bytes, nc, or(2), tok(STRING), tok(BYTES))
DEF_RULE(atom_paren, c(atom_paren), and(3), tok(DEL_PAREN_OPEN), opt_rule(atom_2b), tok(DEL_PAREN_CLOSE))
DEF_RULE(atom_2b, nc, or(2), rule(yield_expr), rule(testlist_comp))
@ -276,7 +275,7 @@ DEF_RULE(subscript_3, c(subscript_3), and(2), tok(DEL_COLON), opt_rule(subscript
DEF_RULE(subscript_3b, nc, or(2), rule(subscript_3c), rule(subscript_3d))
DEF_RULE(subscript_3c, nc, and(2), tok(DEL_COLON), opt_rule(test))
DEF_RULE(subscript_3d, nc, and(2), rule(test), opt_rule(sliceop))
DEF_RULE(sliceop, nc, and(2), tok(DEL_COLON), opt_rule(test))
DEF_RULE(sliceop, nc, ident | and(2), tok(DEL_COLON), opt_rule(test))
#else
DEF_RULE(subscriptlist, c(generic_tuple), list_with_end, rule(test), tok(DEL_COMMA))
#endif

View File

@ -23,6 +23,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/parse2.h"
#ifndef __MICROPY_INCLUDED_PY_PARSE_H__
#define __MICROPY_INCLUDED_PY_PARSE_H__

1400
py/parse2.c Normal file

File diff suppressed because it is too large Load Diff

133
py/parse2.h Normal file
View File

@ -0,0 +1,133 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013, 2014 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __MICROPY_INCLUDED_PY_PARSE_H__
#define __MICROPY_INCLUDED_PY_PARSE_H__
#include <stdint.h>
#include "py/mpconfig.h"
#include "py/misc.h"
#include "py/qstr.h"
struct _mp_lexer_t;
#define MP_PT_NULL (0)
#define MP_PT_TOKEN (1)
#define MP_PT_SMALL_INT (3)
#define MP_PT_STRING (4)
#define MP_PT_BYTES (5)
#define MP_PT_CONST_OBJECT (8)
#define MP_PT_ID_BASE (10) // +16
#define MP_PT_RULE_BASE (26) // +173-ish
extern const byte pt_const_int0[];
static inline const byte *pt_tok_extract(const byte *p, byte *tok) {
//assert(*p == MP_PT_TOKEN);
p += 1;
*tok = *p++;
return p;
}
static inline bool pt_is_null(const byte *p) {
return *p == MP_PT_NULL;
}
static inline bool pt_is_null_with_top(const byte *p, const byte *ptop) {
return p == ptop || *p == MP_PT_NULL;
}
static inline bool pt_is_small_int(const byte *p) {
return *p == MP_PT_SMALL_INT;
}
static inline bool pt_is_any_rule(const byte *p) {
return *p >= MP_PT_RULE_BASE;
}
static inline mp_uint_t pt_rule_extract_rule_id(const byte *p) {
return *p - MP_PT_RULE_BASE;
}
static inline bool pt_is_any_id(const byte *p) {
return *p >= MP_PT_ID_BASE && *p < MP_PT_RULE_BASE;
}
static inline bool pt_is_id(const byte *p, qstr qst) {
//assert(*p == MP_PT_ID_BASE);
return qst == ((mp_uint_t)p[1] | (((mp_uint_t)p[0] - MP_PT_ID_BASE) << 8));
}
static inline bool pt_is_any_tok(const byte *p) {
return p[0] == MP_PT_TOKEN;
}
static inline bool pt_is_tok(const byte *p, int tok) {
return p[0] == MP_PT_TOKEN && p[1] == tok;
}
static inline bool pt_is_rule(const byte *p, int rule) {
return *p == MP_PT_RULE_BASE + rule;
}
int pt_num_nodes(const byte *p, const byte *ptop);
const byte *pt_next(const byte *p);
//const byte *pt_extract_id(const byte *p, qstr *qst);
static inline const byte *pt_extract_id(const byte *p, qstr *qst) {
//assert(*p == MP_PT_ID_BASE);
*qst = p[1] | ((p[0] - MP_PT_ID_BASE) << 8);
return p + 2;
}
const byte *pt_extract_const_obj(const byte *p, mp_uint_t *idx);
mp_int_t pt_small_int_value(const byte *p);
const byte *pt_get_small_int(const byte *p, mp_int_t *val);
const byte *pt_rule_first(const byte *p);
const byte *pt_rule_extract_top(const byte *p, const byte **ptop);
const byte *pt_rule_extract(const byte *p, mp_uint_t *rule_id, mp_uint_t *src_line, const byte **ptop);
bool pt_is_rule_empty(const byte *p);
const byte *mp_parse_node_extract_list(const byte **p, mp_uint_t pn_kind);
typedef enum {
MP_PARSE_SINGLE_INPUT,
MP_PARSE_FILE_INPUT,
MP_PARSE_EVAL_INPUT,
} mp_parse_input_kind_t;
typedef struct _mp_parse_t {
const byte *root;
mp_uint_t *co_data;
struct _mp_parse_chunk_t *chunk;
} mp_parse_tree_t;
// the parser will raise an exception if an error occurred
// the parser will free the lexer before it returns
mp_parse_tree_t mp_parse(struct _mp_lexer_t *lex, mp_parse_input_kind_t input_kind);
void mp_parse_tree_clear(mp_parse_tree_t *tree);
#endif // __MICROPY_INCLUDED_PY_PARSE_H__

View File

@ -75,9 +75,9 @@ PY_O_BASENAME = \
lexer.o \
lexerstr.o \
lexerunix.o \
parse.o \
parse2.o \
scope.o \
compile.o \
compile2.o \
emitcommon.o \
emitbc.o \
asmx64.o \

View File

@ -30,7 +30,7 @@
#if MICROPY_ENABLE_COMPILER
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options) {
scope_t *scope_new(scope_kind_t kind, const byte *pn, qstr source_file, mp_uint_t emit_options) {
scope_t *scope = m_new0(scope_t, 1);
scope->kind = kind;
scope->pn = pn;
@ -41,8 +41,7 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_u
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]);
pt_extract_id(pn, &scope->simple_name); // function name
break;
case SCOPE_LAMBDA:
scope->simple_name = MP_QSTR__lt_lambda_gt_;
@ -62,7 +61,6 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_u
default:
assert(0);
}
scope->raw_code = mp_emit_glue_new_raw_code();
scope->emit_options = emit_options;
scope->id_info_alloc = MICROPY_ALLOC_SCOPE_ID_INIT;
scope->id_info = m_new(id_info_t, scope->id_info_alloc);

View File

@ -57,8 +57,7 @@ typedef enum { SCOPE_MODULE, SCOPE_FUNCTION, SCOPE_LAMBDA, SCOPE_LIST_COMP, SCOP
typedef struct _scope_t {
scope_kind_t kind;
struct _scope_t *parent;
struct _scope_t *next;
mp_parse_node_t pn;
const byte *pn; // points to the node after the scope index node
qstr source_file;
qstr simple_name;
mp_raw_code_t *raw_code;
@ -75,7 +74,7 @@ typedef struct _scope_t {
id_info_t *id_info;
} scope_t;
scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, mp_uint_t emit_options);
scope_t *scope_new(scope_kind_t kind, const byte *pn, qstr source_file, mp_uint_t emit_options);
void scope_free(scope_t *scope);
id_info_t *scope_find_or_add_id(scope_t *scope, qstr qstr, bool *added);
id_info_t *scope_find(scope_t *scope, qstr qstr);