Add scope reordering ("use strict" -> func_decl -> var_decl -> other opcodes); Add --show-opcodes console parameter

This commit is contained in:
Ilmir Usmanov
2014-07-31 19:30:27 +04:00
parent 8c5309d131
commit 2809ffb36d
10 changed files with 602 additions and 22 deletions
+47
View File
@@ -0,0 +1,47 @@
/* 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 COMMON_H
#define COMMON_H
#define OPCODE_SIZE(op) \
sizeof (struct __op_##op) + 1,
static uint8_t opcode_sizes[] = {
OP_LIST (OPCODE_SIZE)
0
};
static bool
opcodes_equal (OPCODE *opcodes1, OPCODE *opcodes2, uint16_t size)
{
uint16_t i;
for (i = 0; i < size; i++)
{
uint8_t opcode_num1 = opcodes1[i].op_idx, opcode_num2 = opcodes2[i].op_idx;
uint8_t j;
if (opcode_num1 != opcode_num2)
return false;
for (j = 1; j < opcode_sizes[opcode_num1]; j++)
if (((uint8_t*)&opcodes1[i])[j] != ((uint8_t*)&opcodes2[i])[j])
return false;
}
return true;
}
#endif // COMMON_H
+94
View File
@@ -0,0 +1,94 @@
/* 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"
#include "interpreter.h"
#include "mem-allocator.h"
#include "opcodes.h"
#include "serializer.h"
#include "optimizer-passes.h"
#include "jerry-libc.h"
#include "deserializer.h"
#include "common.h"
/**
* Unit test's main function.
*/
int
main( int __unused argc,
char __unused **argv)
{
OPCODE test_program[] = {
[0] = getop_assignment( 0, OPCODE_ARG_TYPE_STRING, 1),
[1] = getop_assignment( 1, OPCODE_ARG_TYPE_VARIABLE, 0),
[2] = getop_is_false_jmp (0, 10),
[3] = getop_is_true_jmp (0, 6),
[4] = getop_jmp_up (1),
[5] = getop_jmp_up (4),
[6] = getop_jmp_down (1),
[7] = getop_jmp_down (2),
[8] = getop_jmp_down (2),
[9] = getop_assignment (0, OPCODE_ARG_TYPE_SMALLINT, 253),
[10] = getop_exitval (0)
};
mem_init();
const char *strings[] = { "a",
"b" };
int nums [] = { 2 };
serializer_init (true);
uint16_t offset = serializer_dump_strings (strings, 2);
serializer_dump_nums (nums, 1, offset, 2);
for (int i = 0; i < 11; i++)
serializer_dump_opcode (test_program[i]);
OPCODE * opcodes = (OPCODE *) deserialize_bytecode ();
optimizer_move_opcodes (opcodes + 9, opcodes + 2, 1);
if (!opcodes_equal (opcodes, (OPCODE[]) {
[0] = getop_assignment( 0, OPCODE_ARG_TYPE_STRING, 1),
[1] = getop_assignment( 1, OPCODE_ARG_TYPE_VARIABLE, 0),
[2] = getop_assignment (0, OPCODE_ARG_TYPE_SMALLINT, 253),
[3] = getop_is_false_jmp (0, 10),
[4] = getop_is_true_jmp (0, 6),
[5] = getop_jmp_up (1),
[6] = getop_jmp_up (4),
[7] = getop_jmp_down (1),
[8] = getop_jmp_down (2),
[9] = getop_jmp_down (2),
[10] = getop_exitval (0)
}, 11))
return 1;
optimizer_adjust_jumps (opcodes + 3, opcodes + 10, 1);
if (!opcodes_equal (opcodes, (OPCODE[]) {
[0] = getop_assignment( 0, OPCODE_ARG_TYPE_STRING, 1),
[1] = getop_assignment( 1, OPCODE_ARG_TYPE_VARIABLE, 0),
[2] = getop_assignment (0, OPCODE_ARG_TYPE_SMALLINT, 253),
[3] = getop_is_false_jmp (0, 10),
[4] = getop_is_true_jmp (0, 7),
[5] = getop_jmp_up (1),
[6] = getop_jmp_up (5),
[7] = getop_jmp_down (1),
[8] = getop_jmp_down (2),
[9] = getop_jmp_down (1),
[10] = getop_exitval (0)
}, 11))
return 1;
return 0;
}
+75
View File
@@ -0,0 +1,75 @@
/* 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"
#include "interpreter.h"
#include "mem-allocator.h"
#include "opcodes.h"
#include "serializer.h"
#include "optimizer-passes.h"
#include "jerry-libc.h"
#include "deserializer.h"
#include "common.h"
/**
* Unit test's main function.
*/
int
main( int __unused argc,
char __unused **argv)
{
// Honestly, after RETVAL there must be RET
OPCODE test_program[] = {
[0] = getop_reg_var_decl (5, 5), // tmp6
[1] = getop_assignment (0, OPCODE_ARG_TYPE_STRING, 1), // a = "b"
[2] = getop_var_decl (1), // var b
[3] = getop_func_decl_0 (2), // function c()
[4] = getop_jmp_down (3), // {
[5] = getop_var_decl (1), // var b
[6] = getop_retval (1), // return b; }
[7] = getop_assignment (5, OPCODE_ARG_TYPE_STRING, 3), // "use strict"
[8] = getop_exitval (0)
};
mem_init();
const char *strings[] = { "a", "b", "c", "use strict" };
int nums [] = { 2 };
serializer_init (true);
uint16_t offset = serializer_dump_strings (strings, 4);
serializer_dump_nums (nums, 1, offset, 4);
for (int i = 0; i < 9; i++)
serializer_dump_opcode (test_program[i]);
OPCODE * opcodes = (OPCODE *) deserialize_bytecode ();
optimizer_reorder_scope (1, 8);
if (!opcodes_equal (opcodes, (OPCODE[]) {
[0] = getop_reg_var_decl (5, 5), // tmp6
[1] = getop_assignment (5, OPCODE_ARG_TYPE_STRING, 3), // "use strict"
[2] = getop_func_decl_0 (2), // function c()
[3] = getop_jmp_down (3), // {
[4] = getop_var_decl (1), // var b
[5] = getop_retval (1), // return b; }
[6] = getop_var_decl (1), // var b
[7] = getop_assignment (0, OPCODE_ARG_TYPE_STRING, 1), // a = "b"
[8] = getop_exitval (0)
}, 9))
return 1;
return 0;
}