Skip to content

Commit

Permalink
re-org bh_definition.c && introduce wamr fast interpreter (bytecodeal…
Browse files Browse the repository at this point in the history
…liance#189)

Co-authored-by: Xu Jun
  • Loading branch information
xujuntwt95329 authored Mar 7, 2020
1 parent cfcaca3 commit 057c849
Show file tree
Hide file tree
Showing 44 changed files with 4,103 additions and 1,051 deletions.
2 changes: 2 additions & 0 deletions ATTRIBUTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ WAMR project reused some components from other open source project:
- **freebsd libm**: used in core/shared/platform/alios/bh_math.c
- **littlevgl**: for the gui samples and wrapped the wasm graphic layer.

The WAMR fast interpreter is a clean room development. We would acknowledge the inspirations by [WASM3](https://github.com/wasm3/wasm3) open source project for the approach of pre-calculated oprand stack location.

## Licenses

### wasmtime
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ iwasm VM core

### key features

- [Embeddable with the supporting C API's](./doc/embed_wamr.md)
- 100% compliant to the W3C WASM MVP
- Small runtime binary size (85K for interpreter and 50K for AoT) and low memory usage
- Near to native speed by AoT
- Unique AoT support for embedded systems which have no system loaders
- Near to native speed by AoT
- Self-implemented module loader enables AoT working cross Linux, SGX and MCU systems
- Choices of WASM application libc support: the built-in libc subset for the embedded environment or [WASI](https://github.com/WebAssembly/WASI) for standard libc
- [The mechanism for exporting native API's to WASM applications](./doc/export_native_api.md)
- [Embeddable with the supporting C API's](./doc/embed_wamr.md)
- [The mechanism for exporting native API's to WASM applications](./doc/export_native_api.md)

### Supported architectures and platforms

Expand All @@ -36,11 +37,11 @@ The iwasm supports the following architectures:
- MIPS
- XTENSA

Following platforms are supported:
Following platforms are supported. Refer to [WAMR porting guide](./doc/port_wamr.md) for how to port WAMR to a new platform.

- [Linux](./doc/build_wamr.md#linux), [Zephyr](./doc/build_wamr.md#zephyr), [MacOS](./doc/build_wamr.md#macos), [VxWorks](./doc/build_wamr.md#vxworks), [AliOS-Things](./doc/build_wamr.md#alios-things), [Intel Software Guard Extention (Linux)](./doc/build_wamr.md#linux-sgx-intel-software-guard-extention), [Android](./doc/build_wamr.md#android)

Refer to [WAMR porting guide](./doc/port_wamr.md) for how to port WAMR to a new platform.


### Build wamrc AoT compiler

Expand Down Expand Up @@ -76,27 +77,26 @@ Browse the folder [core/app-framework](./core/app-framework) for how to extend

# Remote application management

The WAMR application manager supports remote application management from the host environment or the cloud through any physical communications such as TCP, UPD, UART, BLE, etc. Its modular design makes it able to support application management for different managed runtimes.
The WAMR application manager supports [remote application management](./core/app-mgr) from the host environment or the cloud through any physical communications such as TCP, UPD, UART, BLE, etc. Its modular design makes it able to support application management for different managed runtimes.

The tool [host_tool](./test-tools/host-tool) communicates to the WAMR app manager for installing/uninstalling the WASM applications on companion chip from the host system. And the [IoT App Store Demo](./test-tools/IoT-APP-Store-Demo/) shows the conception of remotely managing the device applications from the cloud.

Browse the folder [core/app-mgr](./core/app-mgr) for the details.

WAMR SDK
==========

Usually there are two tasks for integrating the WAMR into a particular project:

- Select what WAMR components (vmcore, libc, app-mgr, app-framework components) to be integrated, and get the associated source files added into the project building configuration
- Select what WAMR components (vmcore, libc, app-mgr, app-framework components) to be integrated, and get the associated source files added into the project building configuration
- Generate the APP SDK for developing the WASM apps on the selected libc and framework components

The **[WAMR SDK](./wamr-sdk)** tools is helpful to finish the two tasks quickly. It supports menu configuration for selecting WAMR components and builds the WAMR to a SDK package that includes **runtime SDK** and **APP SDK**. The runtime SDK is used for building the native application and the APP SDK should be shipped to WASM application developers.
The **[WAMR SDK](./wamr-sdk)** tools is helpful to finish the two tasks quickly. It supports menu configuration for selecting WAMR components and builds the WAMR to a SDK package that includes **runtime SDK** and **APP SDK**. The runtime SDK is used for building the native application and the APP SDK should be shipped to WASM application developers.


Samples
=================

The WAMR samples integrate the iwasm VM core, application manager and selected application framework components. The samples are located in folder [samples](./samples):
The WAMR [samples](./samples) integrate the iwasm VM core, application manager and selected application framework components.
- **[Simple](./samples/simple/README.md)**: The runtime is integrated with most of the WAMR APP libraries, and a few WASM applications are provided for testing the WAMR APP API set. It uses **built-in libc** and executes apps in **interpreter** mode by default.
- **[littlevgl](./samples/littlevgl/README.md)**: Demonstrating the graphic user interface application usage on WAMR. The whole [LittlevGL](https://github.com/littlevgl/) 2D user graphic library and the UI application is built into WASM application. It uses **WASI libc** and executes apps in **AoT mode** by default.
- **[gui](./samples/gui/README.md)**: Moved the [LittlevGL](https://github.com/littlevgl/) library into the runtime and defined a WASM application interface by wrapping the littlevgl API. It uses **WASI libc** and executes apps in **interpreter** mode by default.
Expand Down
7 changes: 7 additions & 0 deletions build-scripts/config_common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,11 @@ if (WAMR_BUILD_LIBC_WASI EQUAL 1)
else ()
message (" Libc WASI disbled")
endif ()
if (WAMR_BUILD_FAST_INTERP EQUAL 1)
add_definitions (-DWASM_ENABLE_FAST_INTERP=1)
message (" Fast interpreter enabled")
else ()
add_definitions (-DWASM_ENABLE_FAST_INTERP=0)
message (" Fast interpreter disbled")
endif ()

14 changes: 7 additions & 7 deletions core/app-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Application framework

This folder "app-native-shared" is for the source files shared by both WASM APP and native runtime

- The c files in this directory are compiled into both the WASM APP and runtime.
- The c files in this directory are compiled into both the WASM APP and runtime.
- The header files for distributing to SDK are placed in the "bi-inc" folder.


Expand All @@ -16,7 +16,7 @@ This folder "template" contains a pre-defined directory structure for a framewor



Every other subfolder is framework component. Each component contains two library parts: **app and native**.
Every other subfolder is framework component. Each component contains two library parts: **app and native**.

- The "base" component provide timer API and inter-app communication support. It must be enabled if other components are selected.
- Under the "app" folder of a component, the subfolder "wa_inc" holds all header files that should be included by the WASM applications
Expand Down Expand Up @@ -85,13 +85,13 @@ Generally you should follow following steps to create a new component:

```c
//use right signature for your functions
EXPORT_WASM_API_WITH_SIG(wasm_my_component_api_1, "(i*~)i"),
EXPORT_WASM_API_WITH_SIG(wasm_my_component_api_1, "(i*~)i"),
EXPORT_WASM_API_WITH_SIG(wasm_my_component_api_2, "(i)i"),
```

- Ensure "wasm_lib.cmake" is provided as it will be included by the WAMR SDK building script

- Add a definition in "wasm_lib.cmake" for your component, e.g.
- Add a definition in "wasm_lib.cmake" for your component, e.g.

```cmake
add_definitions (-DAPP_FRAMEWORK_MY_COMPONENT)
Expand All @@ -101,12 +101,12 @@ Generally you should follow following steps to create a new component:
```
#include "lib_export.h"

...
#ifdef APP_FRAMEWORK_MY_COMPONENT // this definition is created in wasm_lib.cmake
#include "my_component_native_api.h"
#endif

static NativeSymbol extended_native_symbol_defs[] = {
...
#ifdef APP_FRAMEWORK_MY_COMPONENT
Expand All @@ -115,7 +115,7 @@ Generally you should follow following steps to create a new component:
};
```
## Sensor component working flow
Expand Down
2 changes: 1 addition & 1 deletion core/app-framework/base/native/base_lib_export.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static NativeSymbol extended_native_symbol_defs[] = {
#include "base_lib.inl"
};

int get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
uint32 get_base_lib_export_apis(NativeSymbol **p_base_lib_apis)
{
*p_base_lib_apis = extended_native_symbol_defs;
return sizeof(extended_native_symbol_defs) / sizeof(NativeSymbol);
Expand Down
2 changes: 1 addition & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,7 @@ create_sections(const uint8 *buf, uint32 size,

memset(section, 0, sizeof(AOTSection));
section->section_type = (int32)section_type;
section->section_body = p;
section->section_body = (uint8*)p;
section->section_body_size = section_size;

if (section_type == AOT_SECTION_TYPE_TEXT) {
Expand Down
7 changes: 1 addition & 6 deletions core/iwasm/common/wasm_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
#define _WASM_NATIVE_H

#include "bh_common.h"
#include "../include/wasm_export.h"
#include "../interpreter/wasm.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct NativeSymbol {
const char *symbol;
void *func_ptr;
const char *signature;
} NativeSymbol;

typedef struct NativeSymbolsNode {
struct NativeSymbolsNode *next;
const char *module_name;
Expand Down
51 changes: 47 additions & 4 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,49 @@ wasm_runtime_destroy()
vm_thread_sys_destroy();
}

bool
wasm_runtime_full_init(RuntimeInitArgs *init_args)
{
if (init_args->mem_alloc_type == Alloc_With_Pool) {
void *heap_buf = init_args->mem_alloc.pool.heap_buf;
uint32 heap_size = init_args->mem_alloc.pool.heap_size;
if (bh_memory_init_with_pool(heap_buf, heap_size) != 0)
return false;
}
else if (init_args->mem_alloc_type == Alloc_With_Allocator) {
void *malloc_func = init_args->mem_alloc.allocator.malloc_func;
void *free_func = init_args->mem_alloc.allocator.free_func;
if (bh_memory_init_with_allocator(malloc_func, free_func) != 0)
return false;
}
else
return false;

if (!wasm_runtime_init())
goto fail1;

if (init_args->n_native_symbols > 0
&& !wasm_runtime_register_natives(init_args->native_module_name,
init_args->native_symbols,
init_args->n_native_symbols))
goto fail2;

return true;

fail2:
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return false;
}

void
wasm_runtime_full_destroy()
{
wasm_runtime_destroy();
bh_memory_destroy();
}

PackageType
get_package_type(const uint8 *buf, uint32 size)
{
Expand Down Expand Up @@ -202,7 +245,7 @@ wasm_runtime_get_module_inst(WASMExecEnv *exec_env)
}

WASMFunctionInstanceCommon *
wasm_runtime_lookup_function(const WASMModuleInstanceCommon *module_inst,
wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
const char *name,
const char *signature)
{
Expand Down Expand Up @@ -339,7 +382,7 @@ wasm_runtime_get_custom_data(WASMModuleInstanceCommon *module_inst)
#if WASM_ENABLE_INTERP != 0
if (module_inst->module_type == Wasm_Module_Bytecode)
return ((WASMModuleInstance*)module_inst)->custom_data;
#endif
#endif
#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT)
return ((AOTModuleInstance*)module_inst)->custom_data.ptr;
Expand Down Expand Up @@ -619,7 +662,7 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env_list[], uint32 env_count,
const char *argv[], uint32 argc)
char *argv[], int argc)
{
WASIArguments *wasi_args = NULL;

Expand Down Expand Up @@ -649,7 +692,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env[], uint32 env_count,
const char *argv[], uint32 argc,
char *argv[], uint32 argc,
char *error_buf, uint32 error_buf_size)
{
WASIContext *wasi_ctx;
Expand Down
38 changes: 15 additions & 23 deletions core/iwasm/common/wasm_runtime_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "bh_thread.h"
#include "wasm_exec_env.h"
#include "wasm_native.h"
#include "../include/wasm_export.h"
#include "../interpreter/wasm.h"
#if WASM_ENABLE_LIBC_WASI != 0
#include "wasmtime_ssp.h"
Expand All @@ -24,13 +25,6 @@ extern "C" {
#define wasm_malloc bh_malloc
#define wasm_free bh_free

/* Package Type */
typedef enum {
Wasm_Module_Bytecode = 0,
Wasm_Module_AoT,
Package_Type_Unknown = 0xFFFF
} PackageType;

typedef struct WASMModuleCommon {
/* Module type, for module loaded from WASM bytecode binary,
this field is Wasm_Module_Bytecode, and this structure should
Expand All @@ -53,19 +47,6 @@ typedef struct WASMModuleInstanceCommon {
uint8 module_inst_data[1];
} WASMModuleInstanceCommon;

typedef void WASMFunctionInstanceCommon;

/* WASM section */
typedef struct WASMSection {
struct WASMSection *next;
/* section type */
int section_type;
/* section body, not include type and size */
const uint8 *section_body;
/* section body size */
uint32 section_body_size;
} WASMSection, AOTSection;

#if WASM_ENABLE_LIBC_WASI != 0
typedef struct WASIContext {
struct fd_table *curfds;
Expand All @@ -74,6 +55,9 @@ typedef struct WASIContext {
} WASIContext;
#endif

typedef package_type_t PackageType;
typedef wasm_section_t WASMSection, AOTSection;

/* See wasm_export.h for description */
bool
wasm_runtime_init();
Expand All @@ -82,6 +66,14 @@ wasm_runtime_init();
void
wasm_runtime_destroy();

/* See wasm_export.h for description */
bool
wasm_runtime_full_init(RuntimeInitArgs *init_args);

/* See wasm_export.h for description */
void
wasm_runtime_full_destroy();

/* See wasm_export.h for description */
PackageType
get_package_type(const uint8 *buf, uint32 size);
Expand Down Expand Up @@ -112,7 +104,7 @@ wasm_runtime_deinstantiate(WASMModuleInstanceCommon *module_inst);

/* See wasm_export.h for description */
WASMFunctionInstanceCommon *
wasm_runtime_lookup_function(const WASMModuleInstanceCommon *module_inst,
wasm_runtime_lookup_function(WASMModuleInstanceCommon * const module_inst,
const char *name, const char *signature);

/* See wasm_export.h for description */
Expand Down Expand Up @@ -245,7 +237,7 @@ wasm_runtime_set_wasi_args(WASMModuleCommon *module,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env_list[], uint32 env_count,
const char *argv[], uint32 argc);
char *argv[], int argc);

/* See wasm_export.h for description */
bool
Expand All @@ -260,7 +252,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
const char *dir_list[], uint32 dir_count,
const char *map_dir_list[], uint32 map_dir_count,
const char *env[], uint32 env_count,
const char *argv[], uint32 argc,
char *argv[], uint32 argc,
char *error_buf, uint32 error_buf_size);

void
Expand Down
20 changes: 3 additions & 17 deletions core/iwasm/include/lib_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef _LIB_EXPORT_H_
#define _LIB_EXPORT_H_

#include <inttypes.h>

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -31,25 +33,9 @@ typedef struct NativeSymbol {
*
* @return the number of the exported API
*/
int
uint32_t
get_base_lib_export_apis(NativeSymbol **p_base_lib_apis);

/**
* Get the exported APIs of extended lib, this API isn't provided by WASM VM,
* it must be provided by developer to register the extended native APIs,
* for example, developer can register his native APIs to extended_native_symbol_defs,
* array, and include file ext_lib_export.h which implements this API.
* And if developer hasn't any native API to register, he can define an empty
* extended_native_symbol_defs array, and then include file ext_lib_export.h to
* implements this API.
*
* @param p_base_lib_apis return the exported API array of extend lib
*
* @return the number of the exported API
*/
int
get_extend_lib_export_apis(NativeSymbol **p_base_lib_apis);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 057c849

Please sign in to comment.