April 20, 2006

初探 B# embedded virtual machine

之前的 blog [B#:作為嵌入式系統的輕量級程式語言] 提到的 B# Programming Language,其設計者 DeepObjectKnowledge 公司在 Embedded.com 刊載的文章 [B# - A programming language for small footprint embedded systems applications: Part 2],將重點置於 B# embedded virtual machine (B#EVM)。簡單來說,B#EVM 的系統架構如下圖:

這篇文章提到了 Memory Manager、Stack Machine,以及 Multi-Thread Kernel。

之前的 blog 提到:「B# 並非 C# 的簡化,其語言規範還考慮到絕大多數 ISO/IEC standard 的硬體架構的應用,提供了 Device Registers 與 Interrupt Handlers 等語法上的支援,這些如果要在 C 語言層面實現的話,就得像 Linux kernel 一般依賴 GNU Extensions,並佐以一系列的 macro 與 programming manner。所以,B# 的提出就是希望一勞永逸地 (至少有限度的) 解決這種問題,設計 Device driver 不免會有 HAL (Hardware Abstract Layer),無論哪個作業系統,基本上都會採用 Object-based 的思維 。」B#EVM 基於這個思維方式,展現以下的 memory model:

這裡節錄幾個設計的重點:
  • T o efficiently allocate this space and to diminish fragmentation, the memory manager of the B#EVM uses a hybrid of static and dynamic allocations.
  • The code memory space of the B#EVM consists of those subsystems that actually execute the application. These systems provide the B# language with the low-level support to access device registers, create threads, and handle interrupts.
  • To help satisfy the bounded time constraints of embedded systems, a byte map is also used to manage the allocation, deallocation and search for free blocks. The main advantage of this approach is its relative simplicity and efficiency in finding the first free block of N consecutive free blocks.
  • Each partition has a byte map and each byte map is implemented as a linked list. Because the linked list has a maximum fixed length, the search for available memory is also deterministic.
  • B#EVM is a stack-machine interpreter that executes virtual code generated by the B# assembler. Values are pushed onto the stack, operations take values from the stack, and the results are pushed back where they are available for future calculations.
  • Although a single-thread application may contain several namespaces and classes, only one class contains the main entry method and implicitly becomes the main thread. In a multi-thread application, each object (instance of a class) with its own block of code may potentially become a thread (i.e. an active object) and therefore, requires a stack for saving its own context. Any object that does not become a thread shares the stack of the thread to which it belongs.
  • Other virtual machines, such as the Sun JVM and Microsoft IL, have polluted their instruction sets with instructions that embed their information type.
  • The operand stack of B#EVM represents all values as 32-bit elements, regardless of their type. Therefore, to convert from one type to another is quite simply a question of “tagging” the stack element with a particular type.No descriptors are required since the type is set when the value or the reference is allocated on the stack. To store this information, an additional stack within the B#EVM maintains the type information of the corresponding elements in the operand (runtime) stack. In this way, we eliminate the cost of wrappers (in terms of space, access, and allocation time) and avoid the definition of an hybrid type system that requires a clear distinction between value types (bool, char, int, uint, and float) and reference types (ioregs, arrays, strings, and delegates). Another important reason for maintaining type information in this way is to reduce the size of the instruction set.
  • Seven instructions are required where only one instruction is required in B# since arithmetic operations are all promoted to 32 bits. Also, the type conversion instructions that are required before doing any arithmetic calculations are reduced since the add instruction in B#EVM is done within the instruction while checking and promoting (if necessary) the operand types. Although there is additional overhead, the instruction set and the generated code size are reduced.
B# Programming Language toolset 已經提供下載。
由 jserv 發表於 April 20, 2006 05:07 PM
迴響