manualy generated blinky opcodes

This commit is contained in:
e.gavrin
2014-07-03 16:23:25 +04:00
parent 1c4873f4b6
commit 3fde3400f4
33 changed files with 186 additions and 102 deletions
+18 -26
View File
@@ -18,21 +18,9 @@
#include "parser.h"
#include "pretty-printer.h"
#include "interpreter.h"
#include "opcode.h"
#include "opcodes.h"
void
safe_opcode (FILE *file, opcode_ptr ptr, int arg1, int arg2)
{
curr_opcode.func = ptr;
curr_opcode.arg1 = arg1;
curr_opcode.arg2 = arg2;
if (file != NULL)
{
fwrite (&curr_opcode, sizeof (struct opcode_packed), 1, file);
}
}
#include "actuators.h"
void
gen_bytecode (FILE *src_file)
@@ -51,14 +39,15 @@ gen_bytecode (FILE *src_file)
pp_finish ();
FILE *file = fopen (FILE_NAME, "w+b");
int i;
for (i = 0; i <= 10; i++)
{
safe_opcode (file, control_op, i, i);
safe_opcode (file, decl_op, i, i);
safe_opcode (file, call_op, i, i);
}
union __opcodes op0;
save_op_loop_inf (file, op0, 1);
save_op_call_1 (file, op0, 0, LED_GREEN);
save_op_call_1 (file, op0, 0, LED_BLUE);
save_op_call_1 (file, op0, 0, LED_ORANGE);
save_op_call_1 (file, op0, 0, LED_RED);
save_op_jmp (file, op0, 0);
fclose (file);
}
@@ -67,6 +56,7 @@ void
run_int ()
{
FILE *file = fopen (FILE_NAME, "rb");
union __opcodes op_curr;
if (file == NULL)
{
@@ -76,10 +66,12 @@ run_int ()
while (!feof (file))
{
fread (&curr_opcode, sizeof (struct opcode_packed), 1, file);
//printf ("read %d, %d, %p\n", curr_opcode.arg1, curr_opcode.arg2, curr_opcode.func);
curr_opcode.func (curr_opcode.arg1, curr_opcode.arg2);
if (!fread (&op_curr, sizeof (union __opcodes), 1, file))
{
break;
}
op_curr.opfunc_ptr (op_curr);
}
fclose (file);
+1 -9
View File
@@ -13,13 +13,6 @@
* limitations under the License.
*/
/*
* File: interpreter.h
* Author: egavrin
*
* Created on July 2, 2014, 3:10 PM
*/
#ifndef INTERPRETER_H
#define INTERPRETER_H
@@ -27,11 +20,10 @@
#include <stdlib.h>
#include <string.h>
#include "opcode.h"
#include "opcodes.h"
#define FILE_NAME "application.bin"
void safe_opcode(FILE *, opcode_ptr, int, int);
void gen_bytecode(FILE*);
void run_int();
+84 -7
View File
@@ -13,21 +13,98 @@
* limitations under the License.
*/
#include "opcode.h"
#include "opcodes.h"
#include <stdio.h>
#include <stdlib.h>
OP_DEFINITION (control_op)
void
opfunc_loop_inf (union __opcodes opdata)
{
printf ("control_op %d, %d\n", arg1, arg2);
printf ("loop_inf:idx:%d\n",
opdata.op_loop_inf.opcode_idx);
}
OP_DEFINITION (decl_op)
void
opfunc_op_call_1 (union __opcodes opdata)
{
printf ("decl_op %d, %d\n", arg1, arg2);
printf ("op_call_1:idx:%d:%d\n",
opdata.op_call_1.name_literal_idx,
opdata.op_call_1.arg_literal_idx);
}
OP_DEFINITION (call_op)
void
opfunc_op_jmp (union __opcodes opdata)
{
printf ("call_op %d, %d\n", arg1, arg2);
printf ("op_jmp:idx:%d\n",
opdata.op_jmp.opcode_idx);
}
void proxy_loop_inf (union __opcodes opdata)
{
opdata.op_loop_inf.opfunc_ptr (opdata);
}
void proxy_op_call_1 (union __opcodes opdata)
{
opdata.op_call_1.opfunc_ptr (opdata);
}
void proxy_op_jmp (union __opcodes opdata)
{
opdata.op_jmp.opfunc_ptr (opdata);
}
void
save_op_jmp (FILE *file, union __opcodes opdata, int arg1)
{
if (file == NULL)
{
return;
}
opdata.opfunc_ptr = proxy_op_jmp;
opdata.op_jmp.opfunc_ptr = opfunc_op_jmp;
opdata.op_jmp.opcode_idx = arg1;
fwrite (&opdata, sizeof (union __opcodes), 1, file);
}
void
save_op_call_1 (FILE *file, union __opcodes opdata, int arg1, int arg2)
{
if (file == NULL)
{
return;
}
opdata.opfunc_ptr = proxy_op_call_1;
opdata.op_call_1.opfunc_ptr = opfunc_op_call_1;
opdata.op_call_1.name_literal_idx = arg1;
opdata.op_call_1.arg_literal_idx = arg2;
fwrite (&opdata, sizeof (union __opcodes), 1, file);
}
void
save_op_loop_inf (FILE *file, union __opcodes opdata, int arg1)
{
if (file == NULL)
{
return;
}
opdata.opfunc_ptr = proxy_loop_inf;
opdata.op_loop_inf.opfunc_ptr = opfunc_loop_inf;
opdata.op_loop_inf.opcode_idx = arg1;
fwrite (&opdata, sizeof (union __opcodes), 1, file);
}
+34 -28
View File
@@ -13,43 +13,49 @@
* limitations under the License.
*/
/*
* File: opcode.h
* Author: egavrin
*
* Created on July 2, 2014, 3:12 PM
*/
#ifndef OPCODES_H
#define OPCODES_H
#ifndef OPCODE_H
#define OPCODE_H
#include "stdio.h"
#define OP_RET_TYPE void
#define OP_ARG_TYPE int
#define OP_INT_TYPE int
#define OP_ATTR_DECL OP_ARG_TYPE, OP_ARG_TYPE
#define OP_ATTR_DEF OP_ARG_TYPE arg1, OP_ARG_TYPE arg2
union __opcodes;
#define OP_DECLARATION(opname) OP_RET_TYPE opname (OP_ATTR_DECL)
#define OP_DEFINITION(opname) OP_RET_TYPE opname (OP_ATTR_DEF)
typedef OP_RET_TYPE (*op_proxy_ptr)(union __opcodes);
// opcode ptr
typedef OP_RET_TYPE (*opcode_ptr)(OP_ATTR_DECL);
typedef OP_RET_TYPE (*opfunc_int_ptr)(union __opcodes);
typedef OP_RET_TYPE (*opfunc_int_int_ptr)(union __opcodes);
struct
__attribute__ ((__packed__))
opcode_packed
union __opcodes
{
opcode_ptr func;
OP_ARG_TYPE arg1;
OP_ARG_TYPE arg2;
}
curr_opcode;
op_proxy_ptr opfunc_ptr;
struct __op_loop_inf
{
opfunc_int_ptr opfunc_ptr;
int opcode_idx;
} op_loop_inf;
#define INIT_OPCODE_PTR(func) opcode_ptr func_ptr = ptr;
/** Call with 1 argument */
struct __op_call_1
{
opfunc_int_int_ptr opfunc_ptr;
int name_literal_idx;
int arg_literal_idx;
} op_call_1;
OP_DECLARATION (decl_op);
OP_DECLARATION (call_op);
OP_DECLARATION (control_op);
struct __op_jmp
{
opfunc_int_ptr opfunc_ptr;
int opcode_idx;
} op_jmp;
} __packed;
#endif /* OPCODE_H */
void save_op_jmp(FILE *, union __opcodes, int);
void save_op_call_1(FILE *, union __opcodes, int, int);
void save_op_loop_inf(FILE *, union __opcodes, int);
#endif /* OPCODES_H */
+17
View File
@@ -13,4 +13,21 @@
* limitations under the License.
*/
#include <stdio.h>
#include "actuators.h"
void led_toggle(int led_id)
{
printf("led_toogle: %d", led_id);
}
void led_on(int led_id)
{
printf("led_on: %d", led_id);
}
void led_off(int led_id)
{
printf("led_off: %d", led_id);
}
+9 -7
View File
@@ -13,16 +13,18 @@
* limitations under the License.
*/
/*
* File: actuators.h
* Author: egavrin
*
* Created on July 2, 2014, 2:06 PM
*/
#ifndef ACTUATORS_H
#define ACTUATORS_H
// STM32 F4
#define LED_GREEN 12
#define LED_ORANGE 13
#define LED_RED 14
#define LED_BLUE 15
void led_toggle(int);
void led_on(int);
void led_off(int);
#endif /* ACTUATORS_H */
-7
View File
@@ -13,13 +13,6 @@
* limitations under the License.
*/
/*
* File: common-io.h
* Author: egavrin
*
* Created on July 2, 2014, 2:14 PM
*/
#ifndef COMMON_IO_H
#define COMMON_IO_H
-7
View File
@@ -13,13 +13,6 @@
* limitations under the License.
*/
/*
* File: sensors.h
* Author: egavrin
*
* Created on July 2, 2014, 2:05 PM
*/
#ifndef SENSORS_H
#define SENSORS_H