OpenSmalltalk VM (Cog VM)

repository·Cog·Indexed 20 days ago

https://github.com/opensmalltalk/opensmalltalk-vm

The Cog VM is the virtual machine underlying the Cuis and Squeak Smalltalk dialects. It supports multiple execution models, including Stack VMs (pure interpreters), Cog VMs (JIT-enabled), and experimental Sista VMs (adaptive optimization). It features different memory management representations such as v3 and Spur, and supports both single-threaded and experimental multi-threaded configurations. The core VM is developed in Smalltalk using the VM Simulator and translated to C via the Slang component.

Tokens
30.6K
Snippets
94
Records
151
Agent score
67%

What's inside opensmalltalk-vm

  1. Overview of the fdlibm fork

    Cog

    This repository contains a fork of fdlibm (from www.netlib.org/fdlibm/) specifically modified to allow compilation on modern C/C++ compilers.

    Legacy fdlibm relies on pointer aliasing to access the bits of floating-point representations, which is considered undefined behavior in modern C compilers. This fork addresses that issue by removing pointer aliasing. Additionally, it fixes header issues where Intel 64-bit architectures were not correctly recognized as little-endian.

  2. Understand the build directory hierarchy

    Cog

    The build directory tree is organized by distinct OS/processor combinations. Each combination represents a range of operating system versions and processor architectures for which a specific build provides a functional VM.

    When adding new builds for different OS/processor combinations, do not reuse or 'piggyback' on existing directories intended for different combinations. Instead, create a new, meaningfully distinct directory.

    Examples of distinct combinations:

    • macos64ARMv8: For all currently supported macOS versions on Apple Silicon machines.
    • macos64x64: For all currently supported macOS versions on 64-bit Intel Macs.
  3. Understand Cog VM variants

    Cog

    The Cog VM is available in several configurations based on execution models and memory management:

    Execution Models

    • Stack VMs: Pure interpreters that optimize message sending by keeping method activations on a stack rather than in contexts. They are faster than standard context-based interpreters.
    • Cog VMs: Add a Just-In-Time (JIT) compiler that compiles frequently used methods into machine code.
    • Sista VMs: (In development) Add adaptive optimization with speculative inlining at the bytecode-to-bytecode level.

    Memory Management & Object Representation

    • v3: The original Squeak object representation.
    • Spur: A faster representation using generation scavenging, lazy forwarding, and a segmented heap that can release memory to the host OS. Used by Squeak 5.0+, Cuis 5, and Pharo 5+.

    Threading

    • Single-threaded: Schedules "green" Smalltalk processes above a single-threaded VM.
    • Multi-threaded (Experimental): Allows multiple native threads to share the VM, with switching occurring on FFI calls, callbacks, or Smalltalk process switches. This provides non-blocking FFI calls but does not provide true concurrency.
  4. How multitouch and absolute-positioned input devices are handled

    Cog

    The VM handles input from /dev/input/event* by distinguishing between three types of devices to provide mouse emulation and gesture recognition (like tap-to-click and drag lock). The VM uses libevdev to process raw events and categorizes devices based on how they report multitouch data:

    1. Simple absolute-positioned devices: These do not emit SYN_MT_REPORT or ABS_MT_SLOT events. The VM simply tracks ABS_X/ABS_Y coordinates and uses BTN_TOUCH for tap detection.
    2. Type A multitouch devices: These emit SYN_MT_REPORT. The VM tracks the ABS_MT_POSITION_X/Y of the first finger in each report and resets the finger count to zero upon each SYN_REPORT.
    3. Type B multitouch devices: These emit ABS_MT_SLOT. When a new touch is detected while zero touches are in progress, the VM marks that slot as the active one. It will not attend to any other touches until all active touches have ended.

    Common Behaviors:

    • Mouse Emulation: BTN_TOUCH is used to update the simulated mouse cursor position.
    • Tap-to-Click: The VM performs tap-to-click detection using the BTN_TOUCH signal and the moved flag (implemented in sqUnixEvdevKeyMouse.c).
    • Button Substitution: Multi-finger taps use BTN_TOOL_DOUBLETAP and similar tokens to substitute for middle and right mouse button clicks.
  5. How to develop the core Cog VM

    Cog

    The core VM (execution engine and garbage collector) is developed in Smalltalk using the VM Simulator, not by editing the generated C code in the src directory. The C code in this repository is the output of the Slang component, which translates the Smalltalk framework into C.

    To develop the core VM:

    1. Use the Smalltalk source repository: http://source.squeak.org/VMMaker.html.
    2. Use the scripts in the image/ directory to build a Smalltalk image suitable for VM development and source generation.
    3. Use the processor simulators in the processors/ directory (Bochs for x86, gdb for ARM) to develop the JIT.
  6. Initialize the Cog VM source tree

    Cog

    The Cog VM uses source file substitutions for versioning (replacing $Rev$, $Date$, and $URL$ in sq*SCCSVersion.h files). When cloning the repository for the first time, you must run the update script to install git hooks that manage these stamps during commits and merges.

    Note: If you use git-reset or git-checkout, you should manually run this script again to ensure version stamps remain correct.

    ./scripts/updateSCCSVersions
  7. Overview of the IJG JPEG software

    Cog

    The Independent JPEG Group (IJG) provides C software for JPEG image compression and decompression. It implements JPEG baseline, extended-sequential, and progressive compression processes.

    Key features include:

    • A library of routines for reading and writing JPEG files, intended for reuse in other applications.
    • cjpeg: A sample application for converting images to JPEG.
    • djpeg: A sample application for decompressing JPEG images.
    • jpegtran: A utility for lossless transcoding between different JPEG processes.
    • rdjpgcom and wrjpgcom: Utilities for inserting and extracting textual comments in JFIF files.
    • Color quantization modules (can be compiled out if not required).

    Note: This software is lossy and is intended for 'real-world' scenes (photographs) rather than line drawings or cartoons. It does not support arithmetic-coding variants due to patent restrictions, nor does it support hierarchical or lossless processes.

  8. Overview of the ARMulator simulator in the ARM processor directory

    Cog
    This directory contains the standard release of the ARMulator simulator from Advanced RISC Machines. The simulator is designed to work with GDB. While the original version uses TCP/IP for communication between the simulator and the host, this repository includes a modified version that can be built without TCP/IP support using a custom Makefile.in and a wrapper.c that links directly into GDB and the run command. This modification is intended to simplify usage on non-Unix platforms and within specific simulator rigs.
  9. Overview of Bochs IA-32 (x86) Emulator

    Cog

    Bochs is a highly portable, open-source IA-32 (x86) PC emulator written in C++. It provides complete emulation of the Intel x86 CPU, common I/O devices, and a custom BIOS.

    Key Capabilities:

    • CPU Emulation: Supports 386, 486, Pentium, Pentium II, Pentium III, Pentium 4, or x86-64 CPUs. It includes optional support for MMX, SSEx, and 3DNow! instructions.
    • Operating System Support: Capable of running most major operating systems, including Linux, DOS, and various Windows versions (95, 98, NT, 2000, XP, Vista).
    • Use Case: It allows for complete x86 PC emulation (processor, hardware, and memory), enabling you to run software or entire operating systems within an emulated environment on your host workstation.
  10. Access Bochs documentation guides

    Cog

    The Bochs project documentation is organized into several specialized guides located within the docbook directory structure:

    • Bochs User Guide (user/): Instructions on how to set up and use Bochs.
    • Bochs Development Guide (development/): Information for developers and guidelines on how to contribute to the Bochs project.
    • Bochs Documentation Guide (documentation/): Information on how to contribute to the project's documentation.

    Common assets like images are stored in the images/ directory and can be referenced using the relative path ../images/FILENAME.

  11. Use the CameraPlugin for multi-camera support and frame skipping

    Cog

    The CameraPlugin provides a general-purpose interface for accessing webcams on Unix-like platforms. It supports multiple simultaneously open cameras and attempts to use libv4l2 if available, falling back to internal pixel format conversions if it is not.

    To handle hardware buffering (where the stream might lag behind real-time), the plugin supports frame skipping. This is particularly useful for 'snapshot' use-cases like time-lapse photography or taking precise photos, ensuring you capture the most recent frame rather than a buffered, stale one.