From 6a624356e4800adc4fbfb5afe120ff31ac4c1f9e Mon Sep 17 00:00:00 2001 From: bernard Date: Sat, 4 Jan 2014 06:02:33 +0800 Subject: [PATCH] [Finsh] code cleanup --- components/finsh/finsh.h | 13 +- components/finsh/finsh_compiler.c | 1701 +++++++++++++++-------------- components/finsh/finsh_token.c | 3 +- components/finsh/finsh_var.c | 3 +- components/finsh/finsh_vm.c | 3 +- components/finsh/shell.c | 23 - 6 files changed, 859 insertions(+), 887 deletions(-) diff --git a/components/finsh/finsh.h b/components/finsh/finsh.h index a8fd761f0f..fb06cd5570 100644 --- a/components/finsh/finsh.h +++ b/components/finsh/finsh.h @@ -69,6 +69,8 @@ typedef unsigned long u_long; !defined(__ADSPBLACKFIN__) && \ !defined(_MSC_VER) +/* only for GNU GCC */ + #if !(defined(__GNUC__) && defined(__x86_64__)) typedef unsigned int size_t; #else @@ -79,13 +81,6 @@ typedef unsigned int size_t; #define NULL RT_NULL #endif -#define memset rt_memset -#define strlen rt_strlen -#define strncpy rt_strncpy -#define strncmp rt_strncmp - -int strcmp (const char *s1, const char *s2); -char *strdup(const char *s); #else /* use libc of armcc */ #include @@ -94,8 +89,8 @@ char *strdup(const char *s); #endif #endif -#define FINSH_VERSION_MAJOR 0 -#define FINSH_VERSION_MINOR 5 +#define FINSH_VERSION_MAJOR 1 +#define FINSH_VERSION_MINOR 0 /** * @addtogroup finsh diff --git a/components/finsh/finsh_compiler.c b/components/finsh/finsh_compiler.c index 96ccbd0b2e..55b3bcbde9 100644 --- a/components/finsh/finsh_compiler.c +++ b/components/finsh/finsh_compiler.c @@ -33,901 +33,904 @@ #include "finsh_var.h" #include "finsh_ops.h" -union finsh_value* finsh_compile_sp; /* stack pointer */ -u_char* finsh_compile_pc; /* PC */ +union finsh_value* finsh_compile_sp; /* stack pointer */ +u_char* finsh_compile_pc; /* PC */ -#define finsh_code_byte(x) do { *finsh_compile_pc = (x); finsh_compile_pc ++; } while(0) -#define finsh_code_word(x) do { FINSH_SET16(finsh_compile_pc, x); finsh_compile_pc +=2; } while(0) +#define finsh_code_byte(x) do { *finsh_compile_pc = (x); finsh_compile_pc ++; } while(0) +#define finsh_code_word(x) do { FINSH_SET16(finsh_compile_pc, x); finsh_compile_pc +=2; } while(0) #define finsh_code_dword(x) do { FINSH_SET32(finsh_compile_pc, x); finsh_compile_pc +=4; } while(0) static int finsh_compile(struct finsh_node* node) { - if (node != NULL) - { - /* compile child node */ - if (finsh_node_child(node) != NULL) + if (node != NULL) + { + /* compile child node */ + if (finsh_node_child(node) != NULL) finsh_compile(finsh_node_child(node)); - /* compile current node */ - switch (node->node_type) - { - case FINSH_NODE_ID: - { - /* identifier::syscall */ - if (node->idtype & FINSH_IDTYPE_SYSCALL) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)node->id.syscall->func); - } - /* identifier::sysvar */ - else if (node->idtype & FINSH_IDTYPE_SYSVAR) - { - struct finsh_sysvar* sysvar; - - sysvar = node->id.sysvar; - if (sysvar != NULL) - { - switch (sysvar->type) - { - case finsh_type_char: - case finsh_type_uchar: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - } - - finsh_code_dword((long)(sysvar->var)); - break; - - case finsh_type_short: - case finsh_type_ushort: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - } - - finsh_code_dword((long)(sysvar->var)); - break; - - case finsh_type_int: - case finsh_type_uint: - case finsh_type_long: - case finsh_type_ulong: - case finsh_type_charp: - case finsh_type_shortp: - case finsh_type_intp: - case finsh_type_longp: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - } - - finsh_code_dword((long)(sysvar->var)); - break; - } - } - } - /* identifier::var */ - else - { - struct finsh_var* var; - - var = node->id.var; - if (var != NULL) - { - switch (var->type) - { - case finsh_type_char: - case finsh_type_uchar: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - } - - finsh_code_dword((long)&(var->value.char_value)); - break; - - case finsh_type_short: - case finsh_type_ushort: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - } - - finsh_code_dword((long)&(var->value.short_value)); - break; - - case finsh_type_int: - case finsh_type_uint: - case finsh_type_long: - case finsh_type_ulong: - case finsh_type_charp: - case finsh_type_shortp: - case finsh_type_intp: - case finsh_type_longp: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* load address */ - finsh_code_byte(FINSH_OP_LD_DWORD); - } - else - { - /* load value */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - } - - finsh_code_dword((long)&(var->value.long_value)); - break; - } - } - } - } - break; - - /* load const */ - case FINSH_NODE_VALUE_CHAR: - finsh_code_byte(FINSH_OP_LD_BYTE); - finsh_code_byte(node->value.char_value); - break; - - case FINSH_NODE_VALUE_INT: - case FINSH_NODE_VALUE_LONG: - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword(node->value.long_value); - break; - - case FINSH_NODE_VALUE_NULL: - case FINSH_NODE_VALUE_STRING: - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((u_long)node->value.ptr); - break; - - /* arithmetic operation */ - case FINSH_NODE_SYS_ADD: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_ADD_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_ADD_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_ADD_DWORD); - break; - - case FINSH_NODE_SYS_SUB: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SUB_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SUB_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SUB_DWORD); - break; - - case FINSH_NODE_SYS_MUL: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MUL_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MUL_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MUL_DWORD); - break; - - case FINSH_NODE_SYS_DIV: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_DIV_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_DIV_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_DIV_DWORD); - break; - - case FINSH_NODE_SYS_MOD: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MOD_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MOD_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MOD_DWORD); - break; - - /* bit operation */ - case FINSH_NODE_SYS_AND: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_AND_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_AND_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_AND_DWORD); - break; - - case FINSH_NODE_SYS_OR: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_OR_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_OR_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_OR_DWORD); - break; - - case FINSH_NODE_SYS_XOR: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_XOR_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_XOR_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_XOR_DWORD); - break; - - case FINSH_NODE_SYS_BITWISE: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_BITWISE_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_BITWISE_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_BITWISE_DWORD); - break; - - case FINSH_NODE_SYS_SHL: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHL_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHL_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHL_DWORD); - break; - - case FINSH_NODE_SYS_SHR: - if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHR_BYTE); - else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHR_WORD); - else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHR_DWORD); - break; - - /* syscall */ - case FINSH_NODE_SYS_FUNC: - { - int parameters; - struct finsh_node* sibling; - - parameters = 0; - sibling = finsh_node_sibling(finsh_node_child(node)); - while (sibling != NULL) - { - parameters ++; - sibling = finsh_node_sibling(sibling); - } - - /* load address of function */ - // finsh_code_dword((long)&(node->var->value.ptr)); - - /* syscall parameters */ - finsh_code_byte(FINSH_OP_SYSCALL); - finsh_code_byte(parameters); - } - break; - - /* assign expression */ - case FINSH_NODE_SYS_ASSIGN: - if (finsh_node_child(node)->node_type == FINSH_NODE_ID) - { - switch (finsh_node_child(node)->data_type) - { - case FINSH_DATA_TYPE_BYTE: - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); - break; - - case FINSH_DATA_TYPE_WORD: - finsh_code_byte(FINSH_OP_ST_WORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); - break; - - case FINSH_DATA_TYPE_DWORD: - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - break; - - default: - finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); - } - } - else if (finsh_node_child(node)->node_type == FINSH_NODE_SYS_GETVALUE) - { - switch ((finsh_node_child(node)->data_type) & 0x0F) - { - case FINSH_DATA_TYPE_BYTE: - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); - break; - - case FINSH_DATA_TYPE_WORD: - finsh_code_byte(FINSH_OP_ST_WORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); - break; - - case FINSH_DATA_TYPE_DWORD: - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - break; - - default: - finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); - } - } - break; - - /* pre-increase */ - case FINSH_NODE_SYS_PREINC: - if (finsh_node_child(node)->node_type == FINSH_NODE_ID) - { - struct finsh_var* var; - var = finsh_node_child(node)->id.var; - - /* ld_dword &id */ - // finsh_code_byte(FINSH_OP_LD_DWORD); - - switch (node->data_type) - { - case FINSH_DATA_TYPE_BYTE: - /* address */ - // finsh_code_dword((long)&(var->value.char_value)); - - /* ld_value_byte &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_byte 1 */ - finsh_code_byte(FINSH_OP_LD_BYTE); - finsh_code_byte(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_ADD_BYTE); - /* st_byte */ - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - - case FINSH_DATA_TYPE_WORD: - /* address */ - // finsh_code_dword((long)&(var->value.short_value)); - - /* ld_value_word &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_word 1 */ - finsh_code_byte(FINSH_OP_LD_WORD); - finsh_code_word(1); - - /* add_word */ - finsh_code_byte(FINSH_OP_ADD_WORD); - /* st_word */ - finsh_code_byte(FINSH_OP_ST_WORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - - case FINSH_DATA_TYPE_DWORD: - /* address */ - // finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword 1 */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword(1); - - /* add_dword */ - finsh_code_byte(FINSH_OP_ADD_DWORD); - /* st_dword */ - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - } - } - break; - - /* pre-decrease */ - case FINSH_NODE_SYS_PREDEC: - if (finsh_node_child(node)->node_type == FINSH_NODE_ID) - { - struct finsh_var* var; - var = finsh_node_child(node)->id.var; - - /* ld_dword &id */ - // finsh_code_byte(FINSH_OP_LD_DWORD); - - switch (node->data_type) - { - case FINSH_DATA_TYPE_BYTE: - /* address */ - // finsh_code_dword((long)&(var->value.char_value)); - - /* ld_value_byte &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_byte 1 */ - finsh_code_byte(FINSH_OP_LD_BYTE); - finsh_code_byte(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_SUB_BYTE); - /* st_byte */ - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - - case FINSH_DATA_TYPE_WORD: - /* address */ - // finsh_code_dword((long)&(var->value.short_value)); - - /* ld_value_word &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_word 1 */ - finsh_code_byte(FINSH_OP_LD_WORD); - finsh_code_word(1); - - /* add_word */ - finsh_code_byte(FINSH_OP_SUB_WORD); - /* st_word */ - finsh_code_byte(FINSH_OP_ST_WORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - - case FINSH_DATA_TYPE_DWORD: - /* address */ - // finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword 1 */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword(1); - - /* add_dword */ - finsh_code_byte(FINSH_OP_SUB_DWORD); - /* st_dword */ - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* load value again */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - - break; - } - } - break; - - /* increase */ - case FINSH_NODE_SYS_INC: - if (finsh_node_child(node)->node_type == FINSH_NODE_ID) - { - struct finsh_var* var; - var = finsh_node_child(node)->id.var; - - switch (node->data_type) - { - case FINSH_DATA_TYPE_BYTE: - /* ld_value_byte &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - // finsh_code_dword((long)&(var->value.char_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_value_byte &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_byte 1 */ - finsh_code_byte(FINSH_OP_LD_BYTE); - finsh_code_byte(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_ADD_BYTE); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - - case FINSH_DATA_TYPE_WORD: - /* ld_value_word &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - // finsh_code_dword((long)&(var->value.short_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_value_word &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_word 1 */ - finsh_code_byte(FINSH_OP_LD_WORD); - finsh_code_word(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_ADD_WORD); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_WORD); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - - case FINSH_DATA_TYPE_DWORD: - /* ld_value_dword &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - // finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_value_dword &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword 1 */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_ADD_DWORD); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - } - } - break; - - /* decrease */ - case FINSH_NODE_SYS_DEC: - if (finsh_node_child(node)->node_type == FINSH_NODE_ID) - { - struct finsh_var* var; - var = finsh_node_child(node)->id.var; - - switch (node->data_type) - { - case FINSH_DATA_TYPE_BYTE: - /* ld_value_byte &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - // finsh_code_dword((long)&(var->value.char_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_value_byte &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); - finsh_code_dword((long)&(var->value.char_value)); - - /* ld_byte 1 */ - finsh_code_byte(FINSH_OP_LD_BYTE); - finsh_code_byte(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_SUB_BYTE); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_BYTE); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - - case FINSH_DATA_TYPE_WORD: - /* ld_value_word &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - // finsh_code_dword((long)&(var->value.short_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_value_word &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_WORD); - finsh_code_dword((long)&(var->value.short_value)); - - /* ld_word 1 */ - finsh_code_byte(FINSH_OP_LD_WORD); - finsh_code_word(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_SUB_WORD); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_WORD); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - - case FINSH_DATA_TYPE_DWORD: - /* ld_value_dword &id */ - // finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - // finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword &id */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_value_dword &id */ - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); - finsh_code_dword((long)&(var->value.long_value)); - - /* ld_dword 1 */ - finsh_code_byte(FINSH_OP_LD_DWORD); - finsh_code_dword(1); - - /* add_byte */ - finsh_code_byte(FINSH_OP_SUB_DWORD); - /* get byte */ - finsh_code_byte(FINSH_OP_ST_DWORD); - - /* pop */ - finsh_code_byte(FINSH_OP_POP); - break; - } - } - break; - - case FINSH_NODE_SYS_NULL: - finsh_code_dword(0); - break; - - case FINSH_NODE_SYS_GETVALUE: - if (node->idtype & FINSH_IDTYPE_ADDRESS) - { - /* nothing will be generated */ - } - else - { - switch (node->data_type) - { - case FINSH_DATA_TYPE_BYTE: - finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); - break; - case FINSH_DATA_TYPE_WORD: - finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); - break; - case FINSH_DATA_TYPE_DWORD: - finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); - break; - default: - break; - } - } - break; - - case FINSH_NODE_SYS_GETADDR: - /* nothing will be generated */ - break; - - default: - finsh_error_set(FINSH_ERROR_UNKNOWN_NODE); - break; - } - - /* compile sibling node */ - if (finsh_node_sibling(node) != NULL) + /* compile current node */ + switch (node->node_type) + { + case FINSH_NODE_ID: + { + /* identifier::syscall */ + if (node->idtype & FINSH_IDTYPE_SYSCALL) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)node->id.syscall->func); + } + /* identifier::sysvar */ + else if (node->idtype & FINSH_IDTYPE_SYSVAR) + { + struct finsh_sysvar* sysvar; + + sysvar = node->id.sysvar; + if (sysvar != NULL) + { + switch (sysvar->type) + { + case finsh_type_char: + case finsh_type_uchar: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + } + + finsh_code_dword((long)(sysvar->var)); + break; + + case finsh_type_short: + case finsh_type_ushort: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + } + + finsh_code_dword((long)(sysvar->var)); + break; + + case finsh_type_int: + case finsh_type_uint: + case finsh_type_long: + case finsh_type_ulong: + case finsh_type_charp: + case finsh_type_shortp: + case finsh_type_intp: + case finsh_type_longp: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + } + + finsh_code_dword((long)(sysvar->var)); + break; + } + } + } + /* identifier::var */ + else + { + struct finsh_var* var; + + var = node->id.var; + if (var != NULL) + { + switch (var->type) + { + case finsh_type_char: + case finsh_type_uchar: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + } + + finsh_code_dword((long)&(var->value.char_value)); + break; + + case finsh_type_short: + case finsh_type_ushort: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + } + + finsh_code_dword((long)&(var->value.short_value)); + break; + + case finsh_type_int: + case finsh_type_uint: + case finsh_type_long: + case finsh_type_ulong: + case finsh_type_charp: + case finsh_type_shortp: + case finsh_type_intp: + case finsh_type_longp: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* load address */ + finsh_code_byte(FINSH_OP_LD_DWORD); + } + else + { + /* load value */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + } + + finsh_code_dword((long)&(var->value.long_value)); + break; + } + } + } + } + break; + + /* load const */ + case FINSH_NODE_VALUE_CHAR: + finsh_code_byte(FINSH_OP_LD_BYTE); + finsh_code_byte(node->value.char_value); + break; + + case FINSH_NODE_VALUE_INT: + case FINSH_NODE_VALUE_LONG: + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword(node->value.long_value); + break; + + case FINSH_NODE_VALUE_NULL: + case FINSH_NODE_VALUE_STRING: + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((u_long)node->value.ptr); + break; + + /* arithmetic operation */ + case FINSH_NODE_SYS_ADD: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_ADD_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_ADD_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_ADD_DWORD); + break; + + case FINSH_NODE_SYS_SUB: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SUB_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SUB_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SUB_DWORD); + break; + + case FINSH_NODE_SYS_MUL: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MUL_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MUL_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MUL_DWORD); + break; + + case FINSH_NODE_SYS_DIV: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_DIV_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_DIV_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_DIV_DWORD); + break; + + case FINSH_NODE_SYS_MOD: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_MOD_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_MOD_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_MOD_DWORD); + break; + + /* bit operation */ + case FINSH_NODE_SYS_AND: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_AND_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_AND_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_AND_DWORD); + break; + + case FINSH_NODE_SYS_OR: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_OR_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_OR_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_OR_DWORD); + break; + + case FINSH_NODE_SYS_XOR: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_XOR_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_XOR_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_XOR_DWORD); + break; + + case FINSH_NODE_SYS_BITWISE: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_BITWISE_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_BITWISE_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_BITWISE_DWORD); + break; + + case FINSH_NODE_SYS_SHL: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHL_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHL_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHL_DWORD); + break; + + case FINSH_NODE_SYS_SHR: + if (node->data_type == FINSH_DATA_TYPE_BYTE) finsh_code_byte(FINSH_OP_SHR_BYTE); + else if (node->data_type == FINSH_DATA_TYPE_WORD) finsh_code_byte(FINSH_OP_SHR_WORD); + else if (node->data_type == FINSH_DATA_TYPE_DWORD) finsh_code_byte(FINSH_OP_SHR_DWORD); + break; + + /* syscall */ + case FINSH_NODE_SYS_FUNC: + { + int parameters; + struct finsh_node* sibling; + + parameters = 0; + if (finsh_node_child(node) != NULL) + { + sibling = finsh_node_sibling(finsh_node_child(node)); + while (sibling != NULL) + { + parameters ++; + sibling = finsh_node_sibling(sibling); + } + + /* load address of function */ + // finsh_code_dword((long)&(node->var->value.ptr)); + + /* syscall parameters */ + finsh_code_byte(FINSH_OP_SYSCALL); + finsh_code_byte(parameters); + } + } + break; + + /* assign expression */ + case FINSH_NODE_SYS_ASSIGN: + if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID) + { + switch (finsh_node_child(node)->data_type) + { + case FINSH_DATA_TYPE_BYTE: + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); + break; + + case FINSH_DATA_TYPE_WORD: + finsh_code_byte(FINSH_OP_ST_WORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); + break; + + case FINSH_DATA_TYPE_DWORD: + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + break; + + default: + finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); + } + } + else if (finsh_node_child(node)->node_type == FINSH_NODE_SYS_GETVALUE) + { + switch ((finsh_node_child(node)->data_type) & 0x0F) + { + case FINSH_DATA_TYPE_BYTE: + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); + break; + + case FINSH_DATA_TYPE_WORD: + finsh_code_byte(FINSH_OP_ST_WORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); + break; + + case FINSH_DATA_TYPE_DWORD: + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + break; + + default: + finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); + } + } + break; + + /* pre-increase */ + case FINSH_NODE_SYS_PREINC: + if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID) + { + struct finsh_var* var; + var = finsh_node_child(node)->id.var; + + /* ld_dword &id */ + // finsh_code_byte(FINSH_OP_LD_DWORD); + + switch (node->data_type) + { + case FINSH_DATA_TYPE_BYTE: + /* address */ + // finsh_code_dword((long)&(var->value.char_value)); + + /* ld_value_byte &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_byte 1 */ + finsh_code_byte(FINSH_OP_LD_BYTE); + finsh_code_byte(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_ADD_BYTE); + /* st_byte */ + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + + case FINSH_DATA_TYPE_WORD: + /* address */ + // finsh_code_dword((long)&(var->value.short_value)); + + /* ld_value_word &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_word 1 */ + finsh_code_byte(FINSH_OP_LD_WORD); + finsh_code_word(1); + + /* add_word */ + finsh_code_byte(FINSH_OP_ADD_WORD); + /* st_word */ + finsh_code_byte(FINSH_OP_ST_WORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + + case FINSH_DATA_TYPE_DWORD: + /* address */ + // finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword 1 */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword(1); + + /* add_dword */ + finsh_code_byte(FINSH_OP_ADD_DWORD); + /* st_dword */ + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + } + } + break; + + /* pre-decrease */ + case FINSH_NODE_SYS_PREDEC: + if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID) + { + struct finsh_var* var; + var = finsh_node_child(node)->id.var; + + /* ld_dword &id */ + // finsh_code_byte(FINSH_OP_LD_DWORD); + + switch (node->data_type) + { + case FINSH_DATA_TYPE_BYTE: + /* address */ + // finsh_code_dword((long)&(var->value.char_value)); + + /* ld_value_byte &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_byte 1 */ + finsh_code_byte(FINSH_OP_LD_BYTE); + finsh_code_byte(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_SUB_BYTE); + /* st_byte */ + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + + case FINSH_DATA_TYPE_WORD: + /* address */ + // finsh_code_dword((long)&(var->value.short_value)); + + /* ld_value_word &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_word 1 */ + finsh_code_byte(FINSH_OP_LD_WORD); + finsh_code_word(1); + + /* add_word */ + finsh_code_byte(FINSH_OP_SUB_WORD); + /* st_word */ + finsh_code_byte(FINSH_OP_ST_WORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + + case FINSH_DATA_TYPE_DWORD: + /* address */ + // finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword 1 */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword(1); + + /* add_dword */ + finsh_code_byte(FINSH_OP_SUB_DWORD); + /* st_dword */ + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* load value again */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + + break; + } + } + break; + + /* increase */ + case FINSH_NODE_SYS_INC: + if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID) + { + struct finsh_var* var; + var = finsh_node_child(node)->id.var; + + switch (node->data_type) + { + case FINSH_DATA_TYPE_BYTE: + /* ld_value_byte &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + // finsh_code_dword((long)&(var->value.char_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_value_byte &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_byte 1 */ + finsh_code_byte(FINSH_OP_LD_BYTE); + finsh_code_byte(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_ADD_BYTE); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + + case FINSH_DATA_TYPE_WORD: + /* ld_value_word &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + // finsh_code_dword((long)&(var->value.short_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_value_word &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_word 1 */ + finsh_code_byte(FINSH_OP_LD_WORD); + finsh_code_word(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_ADD_WORD); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_WORD); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + + case FINSH_DATA_TYPE_DWORD: + /* ld_value_dword &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + // finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_value_dword &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword 1 */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_ADD_DWORD); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + } + } + break; + + /* decrease */ + case FINSH_NODE_SYS_DEC: + if (finsh_node_child(node) && finsh_node_child(node)->node_type == FINSH_NODE_ID) + { + struct finsh_var* var; + var = finsh_node_child(node)->id.var; + + switch (node->data_type) + { + case FINSH_DATA_TYPE_BYTE: + /* ld_value_byte &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + // finsh_code_dword((long)&(var->value.char_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_value_byte &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE); + finsh_code_dword((long)&(var->value.char_value)); + + /* ld_byte 1 */ + finsh_code_byte(FINSH_OP_LD_BYTE); + finsh_code_byte(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_SUB_BYTE); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_BYTE); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + + case FINSH_DATA_TYPE_WORD: + /* ld_value_word &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + // finsh_code_dword((long)&(var->value.short_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_value_word &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_WORD); + finsh_code_dword((long)&(var->value.short_value)); + + /* ld_word 1 */ + finsh_code_byte(FINSH_OP_LD_WORD); + finsh_code_word(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_SUB_WORD); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_WORD); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + + case FINSH_DATA_TYPE_DWORD: + /* ld_value_dword &id */ + // finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + // finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword &id */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_value_dword &id */ + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD); + finsh_code_dword((long)&(var->value.long_value)); + + /* ld_dword 1 */ + finsh_code_byte(FINSH_OP_LD_DWORD); + finsh_code_dword(1); + + /* add_byte */ + finsh_code_byte(FINSH_OP_SUB_DWORD); + /* get byte */ + finsh_code_byte(FINSH_OP_ST_DWORD); + + /* pop */ + finsh_code_byte(FINSH_OP_POP); + break; + } + } + break; + + case FINSH_NODE_SYS_NULL: + finsh_code_dword(0); + break; + + case FINSH_NODE_SYS_GETVALUE: + if (node->idtype & FINSH_IDTYPE_ADDRESS) + { + /* nothing will be generated */ + } + else + { + switch (node->data_type) + { + case FINSH_DATA_TYPE_BYTE: + finsh_code_byte(FINSH_OP_LD_VALUE_BYTE_STACK); + break; + case FINSH_DATA_TYPE_WORD: + finsh_code_byte(FINSH_OP_LD_VALUE_WORD_STACK); + break; + case FINSH_DATA_TYPE_DWORD: + finsh_code_byte(FINSH_OP_LD_VALUE_DWORD_STACK); + break; + default: + break; + } + } + break; + + case FINSH_NODE_SYS_GETADDR: + /* nothing will be generated */ + break; + + default: + finsh_error_set(FINSH_ERROR_UNKNOWN_NODE); + break; + } + + /* compile sibling node */ + if (finsh_node_sibling(node) != NULL) finsh_compile(finsh_node_sibling(node)); - } + } - return 0; + return 0; } static int finsh_type_check(struct finsh_node* node, u_char is_addr) { - if (node != NULL) - { - /* address & value */ - if (node->node_type == FINSH_NODE_SYS_ASSIGN || - node->node_type == FINSH_NODE_SYS_PREINC || - node->node_type == FINSH_NODE_SYS_PREDEC || - node->node_type == FINSH_NODE_SYS_GETADDR) - { - /* address */ - finsh_type_check(finsh_node_child(node), FINSH_IDTYPE_ADDRESS); - } - else if (node->node_type == FINSH_NODE_SYS_GETVALUE && is_addr) - { - /* change the attribute of getvalue in left expr */ - finsh_type_check(finsh_node_child(node), 0); - } - else - { - /* transfer 'av' to child node */ - finsh_type_check(finsh_node_child(node), is_addr); - } + if (node != NULL) + { + /* address & value */ + if (node->node_type == FINSH_NODE_SYS_ASSIGN || + node->node_type == FINSH_NODE_SYS_PREINC || + node->node_type == FINSH_NODE_SYS_PREDEC || + node->node_type == FINSH_NODE_SYS_GETADDR) + { + /* address */ + finsh_type_check(finsh_node_child(node), FINSH_IDTYPE_ADDRESS); + } + else if (node->node_type == FINSH_NODE_SYS_GETVALUE && is_addr) + { + /* change the attribute of getvalue in left expr */ + finsh_type_check(finsh_node_child(node), 0); + } + else + { + /* transfer 'av' to child node */ + finsh_type_check(finsh_node_child(node), is_addr); + } - /* always does not load address in sibling */ - finsh_type_check(finsh_node_sibling(node), FINSH_NODE_VALUE); + /* always does not load address in sibling */ + finsh_type_check(finsh_node_sibling(node), FINSH_NODE_VALUE); - /** set attribute of current node */ + /** set attribute of current node */ - /* make sure the current node is address or value */ - if (node->idtype != FINSH_IDTYPE_SYSCALL) node->idtype |= is_addr; + /* make sure the current node is address or value */ + if (node->idtype != FINSH_IDTYPE_SYSCALL) node->idtype |= is_addr; - if (finsh_node_child(node) != NULL) - { - node->data_type = finsh_node_child(node)->data_type; - return 0; - } + if (finsh_node_child(node) != NULL) + { + node->data_type = finsh_node_child(node)->data_type; + return 0; + } - if (node->node_type == FINSH_NODE_ID) - { - if (node->idtype & FINSH_IDTYPE_VAR) - { - struct finsh_var* var; + if (node->node_type == FINSH_NODE_ID) + { + if (node->idtype & FINSH_IDTYPE_VAR) + { + struct finsh_var* var; - var = node->id.var; - if (var != NULL) - { - switch (var->type) - { - case finsh_type_void: - node->data_type = FINSH_DATA_TYPE_VOID; - break; + var = node->id.var; + if (var != NULL) + { + switch (var->type) + { + case finsh_type_void: + node->data_type = FINSH_DATA_TYPE_VOID; + break; - case finsh_type_char: - case finsh_type_uchar: - node->data_type = FINSH_DATA_TYPE_BYTE; - break; + case finsh_type_char: + case finsh_type_uchar: + node->data_type = FINSH_DATA_TYPE_BYTE; + break; - case finsh_type_short: - case finsh_type_ushort: - node->data_type = FINSH_DATA_TYPE_WORD; - break; + case finsh_type_short: + case finsh_type_ushort: + node->data_type = FINSH_DATA_TYPE_WORD; + break; - case finsh_type_int: - case finsh_type_uint: - case finsh_type_long: - case finsh_type_ulong: - node->data_type = FINSH_DATA_TYPE_DWORD; - break; + case finsh_type_int: + case finsh_type_uint: + case finsh_type_long: + case finsh_type_ulong: + node->data_type = FINSH_DATA_TYPE_DWORD; + break; - case finsh_type_charp: - case finsh_type_voidp: - case finsh_type_shortp: - case finsh_type_intp: - case finsh_type_longp: - node->data_type = FINSH_DATA_TYPE_DWORD; - break; + case finsh_type_charp: + case finsh_type_voidp: + case finsh_type_shortp: + case finsh_type_intp: + case finsh_type_longp: + node->data_type = FINSH_DATA_TYPE_DWORD; + break; - default: - finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); - break; - } - } - } - else if (node->idtype & FINSH_IDTYPE_SYSVAR) - { - struct finsh_sysvar *sysvar; + default: + finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); + break; + } + } + } + else if (node->idtype & FINSH_IDTYPE_SYSVAR) + { + struct finsh_sysvar *sysvar; - sysvar = node->id.sysvar; - if (sysvar != NULL) - { - switch (sysvar->type) - { - case finsh_type_void: - node->data_type = FINSH_DATA_TYPE_VOID; - break; + sysvar = node->id.sysvar; + if (sysvar != NULL) + { + switch (sysvar->type) + { + case finsh_type_void: + node->data_type = FINSH_DATA_TYPE_VOID; + break; - case finsh_type_char: - case finsh_type_uchar: - node->data_type = FINSH_DATA_TYPE_BYTE; - break; + case finsh_type_char: + case finsh_type_uchar: + node->data_type = FINSH_DATA_TYPE_BYTE; + break; - case finsh_type_short: - case finsh_type_ushort: - node->data_type = FINSH_DATA_TYPE_WORD; - break; + case finsh_type_short: + case finsh_type_ushort: + node->data_type = FINSH_DATA_TYPE_WORD; + break; - case finsh_type_int: - case finsh_type_uint: - case finsh_type_long: - case finsh_type_ulong: - node->data_type = FINSH_DATA_TYPE_DWORD; - break; + case finsh_type_int: + case finsh_type_uint: + case finsh_type_long: + case finsh_type_ulong: + node->data_type = FINSH_DATA_TYPE_DWORD; + break; - case finsh_type_charp: - case finsh_type_voidp: - case finsh_type_shortp: - case finsh_type_intp: - case finsh_type_longp: - node->data_type = FINSH_DATA_TYPE_DWORD; - break; + case finsh_type_charp: + case finsh_type_voidp: + case finsh_type_shortp: + case finsh_type_intp: + case finsh_type_longp: + node->data_type = FINSH_DATA_TYPE_DWORD; + break; - default: - finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); - break; - } - } - } - } - else if (node->node_type == FINSH_NODE_VALUE_CHAR) - { - node->data_type = FINSH_DATA_TYPE_BYTE; - } - else if (node->node_type == FINSH_NODE_VALUE_INT || - node->node_type == FINSH_NODE_VALUE_LONG || - node->node_type == FINSH_NODE_VALUE_STRING || - node->node_type == FINSH_NODE_VALUE_NULL) - { - node->data_type = FINSH_DATA_TYPE_DWORD; - } - } - return 0; + default: + finsh_error_set(FINSH_ERROR_UNKNOWN_TYPE); + break; + } + } + } + } + else if (node->node_type == FINSH_NODE_VALUE_CHAR) + { + node->data_type = FINSH_DATA_TYPE_BYTE; + } + else if (node->node_type == FINSH_NODE_VALUE_INT || + node->node_type == FINSH_NODE_VALUE_LONG || + node->node_type == FINSH_NODE_VALUE_STRING || + node->node_type == FINSH_NODE_VALUE_NULL) + { + node->data_type = FINSH_DATA_TYPE_DWORD; + } + } + return 0; } int finsh_compiler_run(struct finsh_node* node) { struct finsh_node* sibling; - /* type check */ - finsh_type_check(node, FINSH_NODE_VALUE); + /* type check */ + finsh_type_check(node, FINSH_NODE_VALUE); - /* clean text segment and vm stack */ - memset(&text_segment[0], 0, sizeof(text_segment)); - memset(&finsh_vm_stack[0], 0, sizeof(finsh_vm_stack[0])); + /* clean text segment and vm stack */ + memset(&text_segment[0], 0, sizeof(text_segment)); + memset(&finsh_vm_stack[0], 0, sizeof(finsh_vm_stack[0])); - /* reset compile stack pointer and pc */ - finsh_compile_sp = &finsh_vm_stack[0]; - finsh_compile_pc = &text_segment[0]; + /* reset compile stack pointer and pc */ + finsh_compile_sp = &finsh_vm_stack[0]; + finsh_compile_pc = &text_segment[0]; /* compile node */ sibling = node; while (sibling != NULL) { - struct finsh_node* current_node; - current_node = sibling; + struct finsh_node* current_node; + current_node = sibling; - /* get sibling node */ - sibling = current_node->sibling; + /* get sibling node */ + sibling = current_node->sibling; - /* clean sibling node */ - current_node->sibling = NULL; - finsh_compile(current_node); + /* clean sibling node */ + current_node->sibling = NULL; + finsh_compile(current_node); - /* pop current value */ - if (sibling != NULL) finsh_code_byte(FINSH_OP_POP); + /* pop current value */ + if (sibling != NULL) finsh_code_byte(FINSH_OP_POP); } return 0; diff --git a/components/finsh/finsh_token.c b/components/finsh/finsh_token.c index 895b15d963..5ab29ad2aa 100644 --- a/components/finsh/finsh_token.c +++ b/components/finsh/finsh_token.c @@ -473,17 +473,16 @@ static int token_proc_escape(struct finsh_token* self) static void token_proc_number(struct finsh_token* self) { char ch; - int b; char *p, buf[128]; long value; value = 0; p = buf; - b = 10; ch = token_next_char(self); if ( ch == '0' ) { + int b; ch = token_next_char(self); if ( ch == 'x' || ch == 'X' )/*it's a hex number*/ { diff --git a/components/finsh/finsh_var.c b/components/finsh/finsh_var.c index cd1e089964..6d6b9ec931 100644 --- a/components/finsh/finsh_var.c +++ b/components/finsh/finsh_var.c @@ -104,7 +104,6 @@ struct finsh_var* finsh_var_lookup(const char* name) } #ifdef RT_USING_HEAP -extern char *strdup(const char *s); void finsh_sysvar_append(const char* name, u_char type, void* var_addr) { /* create a sysvar */ @@ -114,7 +113,7 @@ void finsh_sysvar_append(const char* name, u_char type, void* var_addr) if (item != NULL) { item->next = NULL; - item->sysvar.name = strdup(name); + item->sysvar.name = rt_strdup(name); item->sysvar.type = type; item->sysvar.var = var_addr; diff --git a/components/finsh/finsh_vm.c b/components/finsh/finsh_vm.c index b474966e47..8ed6179273 100644 --- a/components/finsh/finsh_vm.c +++ b/components/finsh/finsh_vm.c @@ -72,7 +72,6 @@ void finsh_vm_run() } #ifdef RT_USING_HEAP -extern char *strdup(const char *s); void finsh_syscall_append(const char* name, syscall_func func) { /* create the syscall */ @@ -82,7 +81,7 @@ void finsh_syscall_append(const char* name, syscall_func func) if (item != RT_NULL) { item->next = NULL; - item->syscall.name = strdup(name); + item->syscall.name = rt_strdup(name); item->syscall.func = func; if (global_syscall_list == NULL) diff --git a/components/finsh/shell.c b/components/finsh/shell.c index 751dd0fc3b..74b7ed7d95 100644 --- a/components/finsh/shell.c +++ b/components/finsh/shell.c @@ -54,29 +54,6 @@ ALIGN(RT_ALIGN_SIZE) static char finsh_thread_stack[FINSH_THREAD_STACK_SIZE]; struct finsh_shell* shell; -#if !defined (RT_USING_NEWLIB) && !defined (RT_USING_MINILIBC) -int strcmp (const char *s1, const char *s2) -{ - while (*s1 && *s1 == *s2) s1++, s2++; - - return (*s1 - *s2); -} - -#ifdef RT_USING_HEAP -char *strdup(const char *s) -{ - size_t len = strlen(s) + 1; - char *tmp = (char *)rt_malloc(len); - - if(!tmp) return NULL; - - rt_memcpy(tmp, s, len); - return tmp; -} -#endif - -#endif - #if defined(RT_USING_DFS) && defined(DFS_USING_WORKDIR) #include const char* finsh_get_prompt()