Add bytecode generator

This commit is contained in:
Ilmir Usmanov
2014-07-15 19:32:26 +04:00
parent 25ad63c9be
commit 0647cdd4f1
15 changed files with 467 additions and 151 deletions
+1 -1
View File
@@ -194,7 +194,7 @@ __strtof (const char *nptr, char **endptr)
(void) endptr;
JERRY_UNIMPLEMENTED ();
}
} /* __strtof */
/** Calculate the length of a string. */
size_t
+6 -5
View File
@@ -44,11 +44,12 @@ extern int __isalpha (int);
extern int __isdigit (int);
extern int __isxdigit (int);
extern FILE* __fopen(const char *path, const char *mode);
extern int __fclose(FILE *fp);
extern size_t __fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
extern size_t __fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
extern int __fprintf(FILE *stream, const char *format, ...);
extern FILE* __fopen(const char *, const char *);
extern int __fclose(FILE *);
extern void __rewind(FILE *);
extern size_t __fread(void *, size_t, size_t, FILE *);
extern size_t __fwrite(const void *, size_t, size_t, FILE *);
extern int __fprintf(FILE *, const char *, ...);
#define DBL_MANT_DIG ( 52)
#define DBL_DIG ( 10)
+61 -75
View File
@@ -17,15 +17,12 @@
#include "pretty-printer.h"
static int intendation;
static bool was_function_expression;
static bool was_subexpression;
static statement_type prev_stmt;
#define MAX_NAMES 100
void
pp_reset (void)
{
prev_stmt = STMT_EOF;
intendation = 0;
}
@@ -39,7 +36,7 @@ pp_token (token tok)
break;
case TOK_STRING:
__printf ("STRING (%s)\n", tok.data.str);
__printf ("STRING (n%d)\n", tok.data.str);
break;
case TOK_KEYWORD:
@@ -417,11 +414,11 @@ pp_formal_parameter_list (formal_parameter_list param_list)
for (i = 0; i < MAX_PARAMS; i++)
{
if (param_list.names[i] == NULL)
if (param_list.names[i] == null_string)
break;
if (i != 0)
__printf (", ");
__printf ("%s", param_list.names[i]);
__printf ("%s", lexer_get_string_by_id (param_list.names[i]));
}
}
@@ -430,11 +427,10 @@ pp_function_declaration (function_declaration func_decl)
{
__printf ("function ");
if (func_decl.name)
__printf ("%s ", func_decl.name);
__printf ("%s ", lexer_get_string_by_id (func_decl.name));
__putchar ('(');
pp_formal_parameter_list (func_decl.params);
__printf (") ");
was_function_expression = true;
}
static void
@@ -470,7 +466,7 @@ pp_operand (operand op)
if (op.is_literal)
pp_literal (op.data.lit);
else
__printf ("%s", op.data.name);
__printf ("%s", lexer_get_string_by_id (op.data.name));
}
static void
@@ -515,8 +511,8 @@ pp_property_list (property_list prop_list)
static void
pp_call_expression (call_expression expr)
{
JERRY_ASSERT (expr.name);
__printf ("%s (", expr.name);
JERRY_ASSERT (expr.name < MAX_NAMES);
__printf ("%s (", lexer_get_string_by_id (expr.name));
pp_operand_list (expr.args);
__printf (")\n");
}
@@ -832,18 +828,12 @@ pp_expression (expression_list expr_list)
__printf (", ");
pp_assignment_expression (expr_list.exprs[i]);
}
if (was_subexpression && !was_function_expression)
{
__putchar (')');
was_subexpression = false;
}
}
static void
pp_variable_declaration (variable_declaration var_decl)
{
__printf ("%s", var_decl.name);
__printf ("%s", lexer_get_string_by_id (var_decl.name));
if (!is_expression_empty (var_decl.assign_expr))
{
__printf (" = ");
@@ -925,32 +915,9 @@ pp_for_or_for_in_statement (for_or_for_in_statement for_or_for_in_stmt)
void
pp_statement (statement stmt)
{
was_function_expression = false;
was_subexpression = false;
if (prev_stmt == STMT_BLOCK_END)
{
if (stmt.type == STMT_EMPTY)
{
__printf (";\n");
prev_stmt = stmt.type;
return;
}
else
__putchar ('\n');
}
switch (stmt.type)
{
case STMT_BLOCK_START:
__printf ("{\n");
intendation += 2;
break;
case STMT_BLOCK_END:
intendation -= 2;
intend ();
__printf ("}");
case STMT_EMPTY:
break;
case STMT_VARIABLE:
@@ -958,44 +925,67 @@ pp_statement (statement stmt)
pp_variable_declaration_list (stmt.data.var_stmt);
break;
case STMT_EMPTY:
__printf (";\n");
break;
case STMT_IF:
intend ();
__printf ("if (");
pp_expression (stmt.data.expr);
__printf (") ");
pp_assignment_expression (stmt.data.expr);
__printf (") {\n");
intendation += 2;
break;
case STMT_ELSE:
intendation -= 2;
intend ();
__printf ("else ");
__printf ("} else {\n");
intendation += 2;
break;
case STMT_ELSE_IF:
intendation -= 2;
intend ();
__printf ("else if(");
pp_expression (stmt.data.expr);
__printf (") ");
__printf ("} else if(");
pp_assignment_expression (stmt.data.expr);
__printf (") {\n");
intendation += 2;
break;
case STMT_DO:
case STMT_END_IF:
case STMT_END_WHILE:
case STMT_END_WITH:
case STMT_END_SWITCH:
case STMT_END_CATCH:
case STMT_END_FINALLY:
case STMT_END_FUNCTION:
intendation -= 2;
intend ();
__printf ("do ");
__printf ("}\n");
break;
case STMT_DO_WHILE:
intend ();
__printf ("do {");
intendation += 2;
break;
case STMT_END_DO_WHILE:
intendation -= 2;
intend ();
__printf ("} while (");
pp_assignment_expression (stmt.data.expr);
__printf (");\n");
case STMT_WHILE:
intend ();
__printf ("while (");
pp_expression (stmt.data.expr);
__printf (") ");
pp_assignment_expression (stmt.data.expr);
__printf (") {\n");
intendation += 2;
break;
case STMT_FOR_OR_FOR_IN:
intend ();
pp_for_or_for_in_statement (stmt.data.for_stmt);
intendation += 2;
break;
case STMT_CONTINUE:
@@ -1011,16 +1001,15 @@ pp_statement (statement stmt)
case STMT_RETURN:
intend ();
__printf ("return ");
pp_expression (stmt.data.expr);
if (!was_function_expression)
__printf (";\n");
pp_assignment_expression (stmt.data.expr);
break;
case STMT_WITH:
intend ();
__printf ("with (");
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
__printf (") ");
intendation += 2;
break;
case STMT_LABELLED:
@@ -1031,21 +1020,22 @@ pp_statement (statement stmt)
case STMT_SWITCH:
intend ();
__printf ("switch (");
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
__printf (") ");
intendation += 2;
break;
case STMT_CASE:
intend ();
__printf ("case ");
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
__printf (":\n");
break;
case STMT_THROW:
intend ();
__printf ("throw ");
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
__printf (";\n");
break;
@@ -1057,38 +1047,34 @@ pp_statement (statement stmt)
case STMT_CATCH:
intend ();
__printf ("catch (");
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
__printf (") ");
intendation += 2;
break;
case STMT_FINALLY:
intend ();
__printf ("finally ");
intendation += 2;
break;
case STMT_EXPRESSION:
intend ();
pp_expression (stmt.data.expr);
pp_assignment_expression (stmt.data.expr);
break;
case STMT_SUBEXPRESSION_END:
case STMT_END_SUBEXPRESSION:
__putchar (')');
break;
case STMT_FUNCTION:
intend ();
pp_function_declaration (stmt.data.fun_decl);
__printf ("{\n");
intendation += 2;
break;
default:
JERRY_UNREACHABLE ();
}
prev_stmt = stmt.type;
}
void pp_finish (void)
{
if (prev_stmt == STMT_BLOCK_END)
__putchar ('\n');
}
+25
View File
@@ -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 SERIALIZER_H
#define SERIALIZER_H
#include "globals.h"
void serializer_init (void);
void serializer_dump_data (const void *, size_t);
#endif // SERIALIZER_H
+9
View File
@@ -26,6 +26,7 @@
extern void __noreturn exit(int status);
extern FILE* fopen(const char *path, const char *mode);
extern int fclose(FILE *fp);
extern int rewind (FILE *);
extern size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
extern size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
extern int vprintf (__const char *__restrict __format, __builtin_va_list __arg);
@@ -78,6 +79,14 @@ __fopen(const char *path, /**< file path */
return fopen( path, mode);
} /* __fopen */
/** The rewind() function sets the file position
indicator for the stream pointed to by STREAM to the beginning of the file. */
void
__rewind (FILE *stream)
{
rewind (stream);
}
/**
* fclose
*
+31
View File
@@ -0,0 +1,31 @@
/* 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 "serializer.h"
#include "jerry-libc.h"
void
serializer_init (void)
{
}
void
serializer_dump_data (const void *data, size_t size)
{
size_t i;
for (i = 0; i < size; i++)
__putchar (((char *) data)[i]);
}
@@ -0,0 +1,18 @@
/* 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 "globals.h"
TODO (Dump memory)