2010-03-22 07:49:57 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2010-03-22 07:49:57 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-03-22 07:49:57 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2010-03-22 Bernard first version
|
|
|
|
*/
|
2009-07-03 06:48:23 +08:00
|
|
|
#include <finsh.h>
|
|
|
|
|
|
|
|
#include "finsh_vm.h"
|
|
|
|
#include "finsh_ops.h"
|
|
|
|
#include "finsh_var.h"
|
|
|
|
|
|
|
|
/* stack */
|
2018-03-01 21:01:20 +08:00
|
|
|
union finsh_value finsh_vm_stack[FINSH_STACK_MAX];
|
2009-07-03 06:48:23 +08:00
|
|
|
/* text segment */
|
2018-03-01 21:01:20 +08:00
|
|
|
uint8_t text_segment[FINSH_TEXT_MAX];
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
union finsh_value* finsh_sp; /* stack pointer */
|
|
|
|
uint8_t* finsh_pc; /* PC */
|
2009-07-03 06:48:23 +08:00
|
|
|
|
|
|
|
/* syscall list, for dynamic system call register */
|
|
|
|
struct finsh_syscall_item* global_syscall_list = NULL;
|
|
|
|
|
2013-10-09 17:06:40 +08:00
|
|
|
// #define FINSH_VM_DISASSEMBLE
|
2009-07-03 06:48:23 +08:00
|
|
|
void finsh_vm_run()
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
uint8_t op;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* if you want to disassemble the byte code, please define FINSH_VM_DISASSEMBLE */
|
2013-10-09 17:06:40 +08:00
|
|
|
#ifdef FINSH_VM_DISASSEMBLE
|
2018-03-01 21:01:20 +08:00
|
|
|
void finsh_disassemble();
|
|
|
|
finsh_disassemble();
|
2009-07-03 06:48:23 +08:00
|
|
|
#endif
|
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* set sp(stack pointer) to the beginning of stack */
|
|
|
|
finsh_sp = &finsh_vm_stack[0];
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* set pc to the beginning of text segment */
|
|
|
|
finsh_pc = &text_segment[0];
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
while ((finsh_pc - &text_segment[0] >= 0) &&
|
|
|
|
(finsh_pc - &text_segment[0] < FINSH_TEXT_MAX))
|
|
|
|
{
|
|
|
|
/* get op */
|
|
|
|
op = *finsh_pc++;
|
2009-07-03 06:48:23 +08:00
|
|
|
|
2018-03-01 21:01:20 +08:00
|
|
|
/* call op function */
|
|
|
|
op_table[op]();
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_HEAP
|
|
|
|
void finsh_syscall_append(const char* name, syscall_func func)
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
/* create the syscall */
|
|
|
|
struct finsh_syscall_item* item;
|
|
|
|
|
|
|
|
item = (struct finsh_syscall_item*)rt_malloc(sizeof(struct finsh_syscall_item));
|
|
|
|
if (item != RT_NULL)
|
|
|
|
{
|
|
|
|
item->next = NULL;
|
|
|
|
item->syscall.name = rt_strdup(name);
|
|
|
|
item->syscall.func = func;
|
|
|
|
|
|
|
|
if (global_syscall_list == NULL)
|
|
|
|
{
|
|
|
|
global_syscall_list = item;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item->next = global_syscall_list;
|
|
|
|
global_syscall_list = item;
|
|
|
|
}
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct finsh_syscall* finsh_syscall_lookup(const char* name)
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
struct finsh_syscall* index;
|
|
|
|
struct finsh_syscall_item* item;
|
|
|
|
|
|
|
|
for (index = _syscall_table_begin; index < _syscall_table_end; FINSH_NEXT_SYSCALL(index))
|
|
|
|
{
|
|
|
|
if (strcmp(index->name, name) == 0)
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find on syscall list */
|
|
|
|
item = global_syscall_list;
|
|
|
|
while (item != NULL)
|
|
|
|
{
|
|
|
|
if (strncmp(item->syscall.name, name, strlen(name)) == 0)
|
|
|
|
{
|
|
|
|
return &(item->syscall);
|
|
|
|
}
|
|
|
|
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:06:40 +08:00
|
|
|
#ifdef FINSH_VM_DISASSEMBLE
|
2009-07-03 06:48:23 +08:00
|
|
|
void finsh_disassemble()
|
|
|
|
{
|
2018-03-01 21:01:20 +08:00
|
|
|
uint8_t *pc, op;
|
|
|
|
|
|
|
|
pc = &text_segment[0];
|
|
|
|
while (*pc != 0)
|
|
|
|
{
|
|
|
|
op = *pc;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case FINSH_OP_ADD_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("addb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SUB_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("subb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_DIV_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("divb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MOD_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("modb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MUL_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("mulb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_AND_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("andb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_OR_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("orb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_XOR_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("xorb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_BITWISE_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("bwb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHL_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shlb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHR_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shrb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldb %d\n", *pc++);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldb [0x%x]\n", FINSH_GET32(pc));
|
|
|
|
pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_ST_BYTE:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("stb\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_ADD_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("addw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SUB_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("subw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_DIV_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("divw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MOD_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("modw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MUL_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("mulw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_AND_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("andw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_OR_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("orw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_XOR_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("xorw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_BITWISE_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("bww\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHL_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shlw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHR_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shrw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldw %d\n", FINSH_GET16(pc));
|
|
|
|
pc += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldw [0x%x]\n", FINSH_GET32(pc));
|
|
|
|
pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_ST_WORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("stw\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_ADD_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("addd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SUB_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("subd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_DIV_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("divd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MOD_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("modd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_MUL_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("muld\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_AND_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("andd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_OR_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ord\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_XOR_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("xord\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_BITWISE_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("bwd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHL_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shld\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SHR_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("shrd\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldd 0x%x\n", FINSH_GET32(pc));
|
|
|
|
pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldd [0x%x]\n", FINSH_GET32(pc));
|
|
|
|
pc += 4;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_ST_DWORD:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("std\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_POP:
|
|
|
|
rt_kprintf("pop\n");
|
|
|
|
pc ++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_SYSCALL:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("syscall %d\n", *pc++);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_BYTE_STACK:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldb [sp]\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_WORD_STACK:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldw [sp]\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FINSH_OP_LD_VALUE_DWORD_STACK:
|
|
|
|
pc ++;
|
|
|
|
rt_kprintf("ldd [sp]\n");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2009-07-03 06:48:23 +08:00
|
|
|
}
|
|
|
|
#endif
|