This commit is contained in:
e.gavrin 2014-07-04 23:51:42 +04:00
parent aace544c55
commit 2091bfb9e4
6 changed files with 49 additions and 80 deletions

View File

@ -18,54 +18,31 @@
void
gen_bytecode ()
{
#ifdef __HOST
FILE *file = fopen (FILE_NAME, "w+b");
#endif
__int_data.pos = 0;
//TODO REMOVE
{
save_op_data (file, get_op_loop_inf (1));
save_op_data (file, get_op_call_1 (0, 12));
save_op_data (file, get_op_call_1 (0, 13));
save_op_data (file, get_op_call_1 (0, 14));
save_op_data (file, get_op_call_1 (0, 15));
save_op_data (file, get_op_jmp (0)); // mandatory!
}
#ifdef __HOST
fclose (file);
save_op_data (getop_loop_inf (1));
save_op_data (getop_call_1 (0, 12));
save_op_data (getop_call_1 (0, 13));
save_op_data (getop_call_1 (0, 14));
save_op_data (getop_call_1 (0, 15));
//save_op_data (getop_jmp (0));
#ifdef __MCU
// It's mandatory to restart app!
save_op_data (getop_jmp (0));
#endif
}
void
run_int ()
{
OPCODE op_curr;
__int_data.pos = 0;
printf("size%d", sizeof(OPCODE));
#ifdef __HOST
FILE *file = fopen (FILE_NAME, "rb");
if (file == NULL)
while (true)
{
fputs ("File error", stderr);
exit (1);
OPCODE *curr = &__program[__int_data.pos];
curr->opfunc_ptr (*curr);
}
while (!feof (file))
{
if (!fread (&op_curr, sizeof (OPCODE), 1, file))
{
break;
}
__int_data.pos++;
op_curr.opfunc_ptr (op_curr);
fseek (file, __int_data.pos * sizeof (OPCODE), SEEK_SET);
}
fclose (file);
#endif
}

View File

@ -20,12 +20,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME "application.bin"
#endif
#include "opcodes.h"
OPCODE __program[20];
struct
{
int pos;

View File

@ -16,6 +16,11 @@
#ifndef OPCODE_STRUCTURES_H
#define OPCODE_STRUCTURES_H
// Jerry bytecode ver:07/04/2014
//
#define OP_TYPE_IDX uint8_t
OP_DEF (nop) { };
OP_DEF (jmp)
@ -27,8 +32,7 @@ OP_DEF (decl) { };
OP_DEF (decl_func_named)
{
OP_TYPE_IDX name_literal_idx;
OP_TYPE_IDX name_literal_idx;
};
OP_DEF (decl_func_anon) { };

View File

@ -17,30 +17,17 @@
#include "interpreter.h"
void
#ifdef __HOST
save_op_data (FILE *file, OPCODE opdata)
#elif __MCU
save_op_data (OPCODE opdata)
#endif
{
#ifdef __HOST
if (file == NULL)
{
return;
}
fwrite (&opdata, sizeof (OPCODE), 1, file);
#elif __MCU
JERRY_STATIC_ASSERT (false);
#endif
__program[__int_data.pos++] = opdata;
}
void
opfunc_loop_inf (OPCODE opdata)
{
#ifdef __HOST
printf ("loop_inf:idx:%d\n",
printf ("%d::loop_inf:idx:%d\n",
__int_data.pos,
opdata.data.loop_inf.opcode_idx);
#endif
@ -48,20 +35,24 @@ opfunc_loop_inf (OPCODE opdata)
}
void
opfunc_op_call_1 (OPCODE opdata)
opfunc_call_1 (OPCODE opdata)
{
#ifdef __HOST
printf ("op_call_1:idx:%d:%d\n",
printf ("%d::op_call_1:idx:%d:%d\n",
__int_data.pos,
opdata.data.call_1.name_literal_idx,
opdata.data.call_1.arg1_literal_idx);
#endif
__int_data.pos++;
}
void
opfunc_op_jmp (OPCODE opdata)
opfunc_jmp (OPCODE opdata)
{
#ifdef __HOST
printf ("op_jmp:idx:%d\n",
printf ("%d::op_jmp:idx:%d\n",
__int_data.pos,
opdata.data.jmp.opcode_idx);
#endif
@ -69,22 +60,22 @@ opfunc_op_jmp (OPCODE opdata)
}
OPCODE
get_op_jmp (int arg1)
getop_jmp (int arg1)
{
OPCODE opdata;
opdata.opfunc_ptr = opfunc_op_jmp;
opdata.opfunc_ptr = opfunc_jmp;
opdata.data.jmp.opcode_idx = arg1;
return opdata;
}
OPCODE
get_op_call_1 (int arg1, int arg2)
getop_call_1 (int arg1, int arg2)
{
OPCODE opdata;
opdata.opfunc_ptr = opfunc_op_call_1;
opdata.opfunc_ptr = opfunc_call_1;
opdata.data.call_1.name_literal_idx = arg1;
opdata.data.call_1.arg1_literal_idx = arg2;
@ -92,11 +83,11 @@ get_op_call_1 (int arg1, int arg2)
}
OPCODE
get_op_loop_inf (int arg1)
getop_loop_inf (int arg1)
{
OPCODE opdata;
opdata.opfunc_ptr = opfunc_op_call_1;
opdata.opfunc_ptr = opfunc_loop_inf;
opdata.data.loop_inf.opcode_idx = arg1;
return opdata;

View File

@ -26,8 +26,6 @@
#define OP_DEF(name) struct __op_##name
#define OP(name) struct __op_##name name
#define OP_TYPE_IDX uint8_t
OPCODE;
typedef void (*opfunc)(OPCODE);
@ -51,16 +49,15 @@ OPCODE{
}
__packed;
#ifdef __HOST
void save_op_data (FILE*, OPCODE);
#elif __MCU
void save_op_data (OPCODE);
#endif
void opfunc_loop_inf (OPCODE);
void opfunc_call_1 (OPCODE);
void opfunc_jmp (OPCODE);
OPCODE get_op_loop_inf (int);
OPCODE get_op_call_1 (int, int);
OPCODE get_op_jmp (int arg1);
OPCODE getop_loop_inf (int);
OPCODE getop_call_1 (int, int);
OPCODE getop_jmp (int arg1);
#endif /* OPCODES_H */

View File

@ -72,8 +72,8 @@ main (int argc, char **argv)
}
//gen_bytecode (generated_source);
gen_bytecode (file);
// run_int ();
gen_bytecode ();
run_int ();
#ifdef __TARGET_MCU
fake_exit ();