Add bytecode generator
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "bytecode-generator.h"
|
||||
#include "globals.h"
|
||||
#include "libcoreint/opcodes.h"
|
||||
#include "libruntime/serializer.h"
|
||||
#include "libruntime/jerry-libc.h"
|
||||
|
||||
static uint8_t opcode_index;
|
||||
|
||||
#define MAX_STACK_SIZE 10
|
||||
|
||||
static uint8_t opcode_stack[10];
|
||||
static uint8_t stack_head;
|
||||
|
||||
static void
|
||||
push_opcode (uint8_t opcode)
|
||||
{
|
||||
JERRY_ASSERT (stack_head < MAX_STACK_SIZE);
|
||||
opcode_stack[stack_head++] = opcode;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
pop_opcode (void)
|
||||
{
|
||||
return opcode_stack[--stack_head];
|
||||
}
|
||||
|
||||
void
|
||||
generator_init (void)
|
||||
{
|
||||
opcode_index = 0;
|
||||
stack_head = 0;
|
||||
}
|
||||
|
||||
void
|
||||
generator_dump_strings (const char **strings, uint8_t num)
|
||||
{
|
||||
uint8_t len = num, i;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
serializer_dump_data (&len, 1);
|
||||
len = (uint8_t) (len + __strlen (strings[i]));
|
||||
}
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
serializer_dump_data (strings[i], __strlen (strings[i]) + 1);
|
||||
}
|
||||
|
||||
void
|
||||
generator_dump_statement (statement stmt)
|
||||
{
|
||||
OPCODE opcode;
|
||||
JERRY_STATIC_ASSERT (sizeof (OPCODE) <= sizeof (uint32_t));
|
||||
|
||||
switch (stmt.type)
|
||||
{
|
||||
case STMT_EMPTY:
|
||||
break;
|
||||
|
||||
case STMT_WHILE:
|
||||
TODO (Supports only infinite loops);
|
||||
if (stmt.data.expr.oper == AO_NONE && stmt.data.expr.type == ET_NONE)
|
||||
{
|
||||
operand op = stmt.data.expr.data.ops.op1;
|
||||
if (op.is_literal && op.data.lit.type == LIT_BOOL && op.data.lit.data.is_true)
|
||||
{
|
||||
opcode = getop_loop_inf ((uint8_t) (opcode_index + 1));
|
||||
push_opcode ((uint8_t) (opcode_index + 1));
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case STMT_EXPRESSION:
|
||||
TODO (Supports only calls);
|
||||
if (stmt.data.expr.oper == AO_NONE)
|
||||
{
|
||||
call_expression expr = stmt.data.expr.data.call_expr;
|
||||
JERRY_ASSERT (!is_operand_list_empty (expr.args));
|
||||
if (!is_operand_empty (expr.args.ops[1]))
|
||||
JERRY_UNREACHABLE ();
|
||||
if (expr.args.ops[0].is_literal)
|
||||
JERRY_UNREACHABLE ();
|
||||
opcode = getop_call_1 (expr.name, expr.args.ops[0].data.name);
|
||||
}
|
||||
break;
|
||||
|
||||
case STMT_END_WHILE:
|
||||
opcode = getop_jmp (pop_opcode ());
|
||||
break;
|
||||
|
||||
default:
|
||||
__printf (" generator_dump_statement: %d ", stmt.type);
|
||||
JERRY_UNREACHABLE ();
|
||||
}
|
||||
|
||||
serializer_dump_data (&opcode, sizeof (OPCODE));
|
||||
opcode_index++;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/* Copyright 2014 Samsung Electronics Co., Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef BYTECODE_GENERATOR_H
|
||||
#define BYTECODE_GENERATOR_H
|
||||
|
||||
#include "parser.h"
|
||||
|
||||
void generator_init (void);
|
||||
void generator_dump_strings (const char **, uint8_t);
|
||||
void generator_dump_statement (statement);
|
||||
|
||||
#endif // BYTECODE_GENERATOR_H
|
||||
+24
-3
@@ -81,7 +81,7 @@ static string_and_token keyword_tokens[] =
|
||||
#define MAX_NAMES 100
|
||||
|
||||
static string_and_token seen_names[MAX_NAMES];
|
||||
static size_t seen_names_num;
|
||||
static uint8_t seen_names_num;
|
||||
|
||||
static inline bool
|
||||
is_empty (token tok)
|
||||
@@ -210,9 +210,29 @@ add_to_seen_tokens (string_and_token snt)
|
||||
{
|
||||
JERRY_ASSERT (seen_names_num < MAX_NAMES);
|
||||
|
||||
snt.tok.data.name = (string_id) seen_names_num;
|
||||
seen_names[seen_names_num++] = snt;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
lexer_get_strings (const char **strings)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < seen_names_num; i++)
|
||||
strings[i] = seen_names[i].str;
|
||||
|
||||
return seen_names_num;
|
||||
}
|
||||
|
||||
const char *
|
||||
lexer_get_string_by_id (string_id id)
|
||||
{
|
||||
JERRY_ASSERT (id < seen_names_num);
|
||||
|
||||
return seen_names[id].str;
|
||||
}
|
||||
|
||||
static inline void
|
||||
new_token (void)
|
||||
{
|
||||
@@ -318,7 +338,7 @@ parse_name (void)
|
||||
}
|
||||
|
||||
string = current_token ();
|
||||
known_token = (token) { .type = TOK_NAME, .data.name = string };
|
||||
known_token = (token) { .type = TOK_NAME, .data.name = seen_names_num };
|
||||
|
||||
add_to_seen_tokens ((string_and_token) { .str = string, .tok = known_token });
|
||||
|
||||
@@ -561,7 +581,7 @@ parse_string (void)
|
||||
// Eat up '"'
|
||||
consume_char ();
|
||||
|
||||
res = (token) { .type = TOK_STRING, .data.str = tok };
|
||||
res = (token) { .type = TOK_STRING, .data.str = seen_names_num };
|
||||
|
||||
add_to_seen_tokens ((string_and_token) { .str = tok, .tok = res });
|
||||
|
||||
@@ -588,6 +608,7 @@ lexer_set_file (FILE *ex_file)
|
||||
file = ex_file;
|
||||
lexer_debug_log = __fopen ("lexer.log", "w");
|
||||
saved_token = empty_token;
|
||||
buffer = buffer_start = token_start = NULL;
|
||||
}
|
||||
#else
|
||||
void
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
typedef uint8_t string_id;
|
||||
|
||||
/* Keywords. */
|
||||
typedef enum
|
||||
{
|
||||
@@ -141,11 +143,11 @@ typedef struct
|
||||
{
|
||||
void *none;
|
||||
keyword kw;
|
||||
const char *name;
|
||||
string_id name;
|
||||
bool is_true;
|
||||
int num;
|
||||
float fp_num;
|
||||
const char *str;
|
||||
string_id str;
|
||||
}
|
||||
data;
|
||||
}
|
||||
@@ -161,4 +163,8 @@ void lexer_save_token (token);
|
||||
|
||||
void lexer_dump_buffer_state (void);
|
||||
|
||||
uint8_t lexer_get_strings (const char **);
|
||||
|
||||
const char *lexer_get_string_by_id (string_id id);
|
||||
|
||||
#endif
|
||||
|
||||
+50
-27
@@ -20,7 +20,7 @@
|
||||
bool
|
||||
is_formal_parameter_list_empty (formal_parameter_list list)
|
||||
{
|
||||
return list.names[0] == NULL;
|
||||
return list.names[0] == null_string;
|
||||
}
|
||||
|
||||
bool
|
||||
@@ -56,7 +56,7 @@ is_expression_empty (assignment_expression expr)
|
||||
bool
|
||||
is_variable_declaration_empty (variable_declaration var_decl)
|
||||
{
|
||||
return var_decl.name == NULL && is_expression_empty (var_decl.assign_expr);
|
||||
return var_decl.name == null_string && is_expression_empty (var_decl.assign_expr);
|
||||
}
|
||||
|
||||
bool
|
||||
@@ -65,7 +65,6 @@ is_statement_null (statement stmt)
|
||||
return stmt.type == STMT_NULL && stmt.data.none == NULL;
|
||||
}
|
||||
|
||||
|
||||
static token tok;
|
||||
|
||||
#ifdef __HOST
|
||||
@@ -251,7 +250,7 @@ parse_formal_parameter_list (void)
|
||||
lexer_save_token (tok);
|
||||
|
||||
if (i != MAX_PARAMS - 1)
|
||||
res.names[i + 1] = NULL;
|
||||
res.names[i + 1] = null_string;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -306,7 +305,7 @@ parse_function_expression (void)
|
||||
skip_newlines ();
|
||||
}
|
||||
else
|
||||
res.name = NULL;
|
||||
res.name = null_string;
|
||||
|
||||
current_token_must_be (TOK_OPEN_PAREN);
|
||||
|
||||
@@ -557,7 +556,7 @@ parse_assigment_expression (void)
|
||||
{
|
||||
.oper = AO_NONE,
|
||||
.type = ET_CALL,
|
||||
.var = NULL,
|
||||
.var = null_string,
|
||||
.data.call_expr = parse_call_expression ()
|
||||
};
|
||||
}
|
||||
@@ -608,7 +607,7 @@ parse_assigment_expression (void)
|
||||
default:
|
||||
res.oper = AO_NONE;
|
||||
res.data.ops.op1 = (operand) { .is_literal = false, .data.name = res.var };
|
||||
res.var = NULL;
|
||||
res.var = null_string;
|
||||
goto parse_operator;
|
||||
}
|
||||
|
||||
@@ -1039,7 +1038,7 @@ parse_expression_inside_parens (statement *res)
|
||||
{
|
||||
token_after_newlines_must_be (TOK_OPEN_PAREN);
|
||||
skip_newlines ();
|
||||
res->data.expr = parse_expression ();
|
||||
res->data.expr = parse_assigment_expression ();
|
||||
token_after_newlines_must_be (TOK_CLOSE_PAREN);
|
||||
}
|
||||
|
||||
@@ -1116,6 +1115,37 @@ parser_parse_statement (void)
|
||||
|
||||
skip_newlines ();
|
||||
|
||||
if (current_scopes[scope_index - 1].was_stmt
|
||||
&& (current_scopes[scope_index - 1].type
|
||||
& (SCOPE_IF | SCOPE_WITH | SCOPE_SWITCH | SCOPE_ELSE | SCOPE_CATCH
|
||||
| SCOPE_FUNCTION | SCOPE_WHILE | SCOPE_FOR)))
|
||||
{
|
||||
uint32_t type = current_scopes[scope_index - 1].type;
|
||||
pop_scope ();
|
||||
|
||||
lexer_save_token (tok);
|
||||
|
||||
if (type & SCOPE_IF && !is_keyword (KW_ELSE))
|
||||
return (statement) { .type = STMT_END_IF, .data.none = NULL };
|
||||
if (type & SCOPE_WITH)
|
||||
return (statement) { .type = STMT_END_WITH, .data.none = NULL };
|
||||
if (type & SCOPE_SWITCH)
|
||||
return (statement) { .type = STMT_END_SWITCH, .data.none = NULL };
|
||||
if (type & SCOPE_ELSE)
|
||||
return (statement) { .type = STMT_END_IF, .data.none = NULL };
|
||||
if (type & SCOPE_CATCH && !is_keyword (KW_FINALLY))
|
||||
return (statement) { .type = STMT_END_CATCH, .data.none = NULL };
|
||||
if (type & SCOPE_CATCH)
|
||||
return (statement) { .type = STMT_END_FINALLY, .data.none = NULL };
|
||||
if (type & SCOPE_FUNCTION)
|
||||
return (statement) { .type = STMT_END_FUNCTION, .data.none = NULL };
|
||||
if (type & SCOPE_WHILE)
|
||||
return (statement) { .type = STMT_END_WHILE, .data.none = NULL };
|
||||
if (type & SCOPE_FOR)
|
||||
return (statement) { .type = STMT_END_FOR_OR_FOR_IN, .data.none = NULL };
|
||||
|
||||
}
|
||||
|
||||
if (is_keyword (KW_FINALLY))
|
||||
{
|
||||
res.type = STMT_FINALLY;
|
||||
@@ -1125,12 +1155,6 @@ parser_parse_statement (void)
|
||||
return res;
|
||||
}
|
||||
|
||||
if (current_scopes[scope_index - 1].was_stmt
|
||||
&& (current_scopes[scope_index - 1].type & (SCOPE_IF | SCOPE_DO | SCOPE_WITH | SCOPE_SWITCH | SCOPE_ELSE
|
||||
| SCOPE_CATCH | SCOPE_FINALLY | SCOPE_FUNCTION | SCOPE_WHILE
|
||||
| SCOPE_FOR)))
|
||||
pop_scope ();
|
||||
|
||||
current_scopes[scope_index - 1].was_stmt = true;
|
||||
|
||||
if (tok.type == TOK_EOF)
|
||||
@@ -1144,27 +1168,25 @@ parser_parse_statement (void)
|
||||
{
|
||||
if (tok.type == TOK_CLOSE_PAREN)
|
||||
{
|
||||
res.type = STMT_SUBEXPRESSION_END;
|
||||
res.type = STMT_END_SUBEXPRESSION;
|
||||
pop_scope ();
|
||||
return res;
|
||||
}
|
||||
res.type = STMT_EXPRESSION;
|
||||
res.data.expr = parse_expression ();
|
||||
res.data.expr = parse_assigment_expression ();
|
||||
return res;
|
||||
}
|
||||
if (tok.type == TOK_OPEN_BRACE)
|
||||
{
|
||||
res.type = STMT_BLOCK_START;
|
||||
push_scope (SCOPE_BLOCK);
|
||||
return res;
|
||||
return parser_parse_statement ();
|
||||
}
|
||||
if (tok.type == TOK_CLOSE_BRACE)
|
||||
{
|
||||
current_scope_must_be (SCOPE_BLOCK);
|
||||
res.type = STMT_BLOCK_END;
|
||||
pop_scope ();
|
||||
current_scopes[scope_index - 1].was_stmt = true;
|
||||
return res;
|
||||
return parser_parse_statement ();
|
||||
}
|
||||
if (is_keyword (KW_ELSE))
|
||||
{
|
||||
@@ -1193,6 +1215,7 @@ parser_parse_statement (void)
|
||||
{
|
||||
insert_semicolon ();
|
||||
pop_scope ();
|
||||
res.type = STMT_END_DO_WHILE;
|
||||
}
|
||||
else
|
||||
push_scope (SCOPE_WHILE);
|
||||
@@ -1236,7 +1259,7 @@ parser_parse_statement (void)
|
||||
}
|
||||
if (is_keyword (KW_DO))
|
||||
{
|
||||
res.type = STMT_DO;
|
||||
res.type = STMT_DO_WHILE;
|
||||
push_scope (SCOPE_DO);
|
||||
return res;
|
||||
}
|
||||
@@ -1283,7 +1306,7 @@ parser_parse_statement (void)
|
||||
if (tok.type != TOK_SEMICOLON && tok.type != TOK_NEWLINE)
|
||||
{
|
||||
unsigned int current_scope_index = scope_index;
|
||||
res.data.expr = parse_expression ();
|
||||
res.data.expr = parse_assigment_expression ();
|
||||
if (current_scope_index == scope_index)
|
||||
insert_semicolon ();
|
||||
}
|
||||
@@ -1308,7 +1331,7 @@ parser_parse_statement (void)
|
||||
{
|
||||
res.type = STMT_THROW;
|
||||
tok = lexer_next_token ();
|
||||
res.data.expr = parse_expression ();
|
||||
res.data.expr = parse_assigment_expression ();
|
||||
insert_semicolon ();
|
||||
return res;
|
||||
}
|
||||
@@ -1324,7 +1347,7 @@ parser_parse_statement (void)
|
||||
pop_scope ();
|
||||
current_scope_must_be (SCOPE_SWITCH);
|
||||
skip_newlines ();
|
||||
res.data.expr = parse_expression ();
|
||||
res.data.expr = parse_assigment_expression ();
|
||||
token_after_newlines_must_be (TOK_SEMICOLON);
|
||||
push_scope (SCOPE_CASE);
|
||||
return res;
|
||||
@@ -1352,15 +1375,15 @@ parser_parse_statement (void)
|
||||
{
|
||||
lexer_save_token (tok);
|
||||
tok = saved;
|
||||
expression expr = parse_expression ();
|
||||
assignment_expression expr = parse_assigment_expression ();
|
||||
res.type = STMT_EXPRESSION;
|
||||
res.data.expr = expr;
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
expression expr = parse_expression ();
|
||||
if (!is_expression_empty (expr.exprs[0]))
|
||||
assignment_expression expr = parse_assigment_expression ();
|
||||
if (!is_expression_empty (expr))
|
||||
{
|
||||
res.type = STMT_EXPRESSION;
|
||||
res.data.expr = expr;
|
||||
|
||||
+26
-24
@@ -18,12 +18,7 @@
|
||||
|
||||
#include "globals.h"
|
||||
|
||||
struct source_element_list;
|
||||
struct statement_list;
|
||||
struct statement;
|
||||
struct assignment_expression;
|
||||
struct member_expression;
|
||||
|
||||
#define null_string 255
|
||||
#define MAX_PARAMS 5
|
||||
#define MAX_EXPRS 2
|
||||
#define MAX_PROPERTIES 5
|
||||
@@ -34,14 +29,14 @@ struct member_expression;
|
||||
typedef struct formal_parameter_list
|
||||
{
|
||||
/** Identifiers of a parameter. Next after last parameter is NULL. */
|
||||
const char *names[MAX_PARAMS];
|
||||
uint8_t names[MAX_PARAMS];
|
||||
}
|
||||
formal_parameter_list;
|
||||
|
||||
static const formal_parameter_list
|
||||
empty_formal_parameter_list =
|
||||
{
|
||||
.names = { [0] = NULL }
|
||||
.names = { [0] = null_string }
|
||||
};
|
||||
|
||||
bool is_formal_parameter_list_empty (formal_parameter_list);
|
||||
@@ -51,7 +46,7 @@ bool is_formal_parameter_list_empty (formal_parameter_list);
|
||||
typedef struct
|
||||
{
|
||||
/** Identifier: name of a function. Can be NULL for anonimous functions. */
|
||||
const char *name;
|
||||
uint8_t name;
|
||||
/** List of parameter of a function. Can be NULL. */
|
||||
formal_parameter_list params;
|
||||
}
|
||||
@@ -82,7 +77,7 @@ typedef struct
|
||||
/** Used by null literal, always NULL. */
|
||||
void *none;
|
||||
/** String literal value. */
|
||||
const char *str;
|
||||
uint8_t str;
|
||||
/** Number value. */
|
||||
int num;
|
||||
/** Boolean value. */
|
||||
@@ -100,7 +95,7 @@ typedef struct
|
||||
{
|
||||
void *none;
|
||||
literal lit;
|
||||
const char *name;
|
||||
uint8_t name;
|
||||
}
|
||||
data;
|
||||
}
|
||||
@@ -142,7 +137,7 @@ typedef operand_list argument_list;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
uint8_t name;
|
||||
argument_list args;
|
||||
}
|
||||
call_expression;
|
||||
@@ -262,7 +257,7 @@ typedef struct
|
||||
expression_type type;
|
||||
|
||||
/** NUllable. */
|
||||
const char *var;
|
||||
uint8_t var;
|
||||
|
||||
union
|
||||
{
|
||||
@@ -302,7 +297,7 @@ typedef expression_list expression;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char *name;
|
||||
uint8_t name;
|
||||
assignment_expression assign_expr;
|
||||
}
|
||||
variable_declaration;
|
||||
@@ -310,7 +305,7 @@ variable_declaration;
|
||||
static const variable_declaration
|
||||
empty_variable_declaration =
|
||||
{
|
||||
.name = NULL,
|
||||
.name = null_string,
|
||||
.assign_expr = { .oper = AO_NONE, .type = ET_NONE, .data.none = NULL }
|
||||
};
|
||||
|
||||
@@ -378,33 +373,40 @@ for_or_for_in_statement;
|
||||
typedef enum
|
||||
{
|
||||
STMT_NULL,
|
||||
STMT_BLOCK_START,
|
||||
STMT_BLOCK_END,
|
||||
STMT_VARIABLE,
|
||||
STMT_EMPTY,
|
||||
STMT_IF,
|
||||
STMT_ELSE,
|
||||
STMT_ELSE_IF,
|
||||
STMT_DO,
|
||||
|
||||
STMT_END_IF,
|
||||
STMT_DO_WHILE,
|
||||
STMT_END_DO_WHILE,
|
||||
STMT_WHILE,
|
||||
|
||||
STMT_END_WHILE,
|
||||
STMT_FOR_OR_FOR_IN,
|
||||
STMT_END_FOR_OR_FOR_IN,
|
||||
STMT_CONTINUE,
|
||||
STMT_BREAK,
|
||||
|
||||
STMT_RETURN,
|
||||
STMT_WITH,
|
||||
STMT_END_WITH,
|
||||
STMT_LABELLED,
|
||||
STMT_SWITCH,
|
||||
|
||||
STMT_END_SWITCH,
|
||||
STMT_CASE,
|
||||
STMT_THROW,
|
||||
|
||||
STMT_TRY,
|
||||
STMT_CATCH,
|
||||
STMT_END_CATCH,
|
||||
STMT_FINALLY,
|
||||
STMT_END_FINALLY,
|
||||
STMT_EXPRESSION,
|
||||
STMT_SUBEXPRESSION_END,
|
||||
STMT_END_SUBEXPRESSION,
|
||||
|
||||
STMT_FUNCTION,
|
||||
STMT_END_FUNCTION,
|
||||
STMT_EOF
|
||||
}
|
||||
statement_type;
|
||||
@@ -417,9 +419,9 @@ typedef struct statement
|
||||
{
|
||||
void *none;
|
||||
variable_declaration_list var_stmt;
|
||||
expression expr;
|
||||
assignment_expression expr;
|
||||
for_or_for_in_statement for_stmt;
|
||||
const char *name;
|
||||
uint8_t name;
|
||||
function_declaration fun_decl;
|
||||
}
|
||||
data;
|
||||
|
||||
Reference in New Issue
Block a user