### RPython Translator: Running a Demo Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Command to run a demo of the RPython translator and annotator using the 'bpnn.py' example. This command can display call graphs, class hierarchies, and flow graphs. ```bash python2 bin/rpython --view --annotate translator/goal/bpnn.py ``` -------------------------------- ### Starting the RPython Translator Shell Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Command to launch the interactive RPython translator shell. This tool is used to experiment with translating RPython programs into low-level code. ```bash cd rpython python2 bin/translatorshell.py ``` -------------------------------- ### RPython Metaprogramming Example Source: https://rpython.readthedocs.io/en/latest/getting-started Demonstrates metaprogramming in RPython using a generator function. It highlights which parts of the code are considered RPython and which are not, illustrating the concept of RPython as a subset of Python 2 that can be statically compiled. ```python def generator(operation): if operation == 'add': def f(a, b): return a + b else: def f(a, b): return a - b return f add = generator('add') sub = generator('sub') def entry_point(argv): print add(sub(int(argv[1]), 3) 4) return 0 ``` -------------------------------- ### Setup Python Environment and Install Requirements (Shell) Source: https://rpython.readthedocs.io/en/latest/riscv Configures the PATH environment variable to include Python 2.7 binaries and installs Python packages specified in 'requirements.txt' using pip. This is a prerequisite for PyPy development. ```shell schroot -c rv64_ubuntu_24_04 export PATH=/opt/python2/bin:$PATH cd /path/to/pypy/source/tree python2.7 -mpip install -r requirements.txt ``` -------------------------------- ### Install Build Dependencies for RPython Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Installs essential build tools and libraries required for compiling RPython and PyPy. This command uses apt-get within a schroot environment to ensure a consistent build setup. ```bash schroot -c rv64_ubuntu_24_04 -u root -- apt-get install \ build-essential pkg-config libbz2-dev libexpat1-dev libffi-dev \ libgc-dev libgdbm-dev liblzma-dev libncurses5-dev libncursesw5-dev \ libsqlite3-dev libssl-dev tk-dev zlib1g-dev ``` -------------------------------- ### RPython Translator Shell Commands Source: https://rpython.readthedocs.io/en/latest/getting-started Shows commands for interacting with the RPython translator shell. This includes starting the shell, loading RPython code snippets, viewing the translation graph, and performing type annotation. ```shell cd rpython python2 bin/translatorshell.py ``` ```python >>> t = Translation(snippet.is_perfect_number, [int]) >>> t.view() ``` ```python >>> t.annotate() >>> t.view() ``` -------------------------------- ### RPython Translator: Translating Full Programs Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Command to translate a full RPython program, using a modified Pystone example as a demonstration. This utilizes the 'rpython/bin/rpython' script. ```bash python2 bin/rpython translator/goal/targetrpystonedalone ``` -------------------------------- ### RPython Metaprogramming Example Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Demonstrates RPython's metaprogramming capabilities using a Python generator function. Note that closures are not considered RPython. This example shows how RPython code can call functions, and those followed calls must also be RPython. ```python def generator(operation): if operation == 'add': def f(a, b): return a + b else: def f(a, b): return a - b return f add = generator('add') sub = generator('sub') def entry_point(argv): print add(sub(int(argv[1]), 3) 4) return 0 ``` -------------------------------- ### Install Python Packages for PyPy Development Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Sets up the Python environment for PyPy development by installing necessary packages from a requirements file. It involves setting the PATH and navigating to the PyPy source directory. ```bash schroot -c rv64_ubuntu_24_04 export PATH=/opt/python2/bin:$PATH cd /path/to/pypy/source/tree python2.7 -mpip install -r requirements.txt ``` -------------------------------- ### RPython Translator: Compiling Demo to C Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Command to compile the 'bpnn.py' RPython demo to an executable C program named 'bpnn-c'. ```bash python2 bin/rpython translator/goal/bpnn.py ``` -------------------------------- ### Install Git for PyPy Build Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Installs the Git version control system, which is necessary for building PyPy, likely for fetching source code or managing revisions. ```bash schroot -c rv64_ubuntu_24_04 -u root -- apt-get install git ``` -------------------------------- ### Install RISC-V Chroot Dependencies (Ubuntu) Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Installs essential packages for creating a RISC-V 64-bit Ubuntu chroot on an x86 host. Dependencies include debootstrap, schroot, qemu-user-static, binfmt-support, and ubuntu-keyring. ```bash sudo apt-get install debootstrap qemu-user-static binfmt-support schroot # For non-Ubuntu host: sudo apt-get install ubuntu-keyring ``` -------------------------------- ### Install RISC-V Chroot Dependencies (Bash) Source: https://rpython.readthedocs.io/en/latest/riscv Installs essential packages like debootstrap, qemu-user-static, binfmt-support, and schroot required for creating and managing a RISC-V 64-bit Ubuntu chroot environment on a Debian/Ubuntu host. ```bash sudo apt-get install debootstrap qemu-user-static binfmt-support schroot # For non-Ubuntu host: sudo apt-get install ubuntu-keyring ``` -------------------------------- ### Translating RPython to C Code Source: https://rpython.readthedocs.io/en/latest/getting-started Demonstrates the process of translating an RPython flow graph into C code and compiling it into a shared object library. It also shows how to load and use the compiled C function using ctypes. ```python >>> t.rtype() >>> lib = t.compile_c() ``` ```python >>> f = get_c_function(lib, snippet.is_perfect_number) >>> f(5) 0 >>> f(6) 1 ``` -------------------------------- ### Setup CPython 2.7 Pip and Wheel (Chroot) Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Configures the PATH environment variable to include the newly installed CPython 2.7 and then uses ensurepip and pip to install and upgrade pip and wheel packages within the chroot. ```bash $ export PATH=/opt/python2/bin:$PATH $ python2.7 -mensurepip $ python2.7 -mpip install -U pip wheel ``` -------------------------------- ### Install Git for PyPy Testing (Shell) Source: https://rpython.readthedocs.io/en/latest/riscv Installs the Git version control system, which is required to pass all test suites for PyPy development. This command is executed within a schroot environment. ```shell schroot -c rv64_ubuntu_24_04 -u root -- apt-get install git ``` -------------------------------- ### Install PyPy Development Dependencies (Shell) Source: https://rpython.readthedocs.io/en/latest/riscv Installs essential build tools and libraries required for PyPy development using apt-get within a schroot environment. This ensures all necessary components are available for compiling PyPy. ```shell schroot -c rv64_ubuntu_24_04 -u root -- apt-get install \ build-essential pkg-config libbz2-dev libexpat1-dev libffi-dev \ libgc-dev libgdbm-dev liblzma-dev libncurses5-dev libncursesw5-dev \ libsqlite3-dev libssl-dev tk-dev zlib1g-dev ``` -------------------------------- ### RPython Translator Command-Line Execution Source: https://rpython.readthedocs.io/en/latest/getting-started Illustrates how to use the RPython translator from the command line to analyze and compile RPython programs. This includes options for viewing the call graph and annotating types, as well as compiling directly to an executable. ```shell python2 bin/rpython --view --annotate translator/goal/bpnn.py ``` ```shell python2 bin/rpython translator/goal/bpnn.py ``` -------------------------------- ### Example AbstractStrategy with Custom Parameter Source: https://rpython.readthedocs.io/en/latest/_sources/rstrategies.rst This Python example shows how to define a custom `AbstractStrategy` class that includes an additional 'space' parameter. It demonstrates setting attributes, immutability, metaclass, mixins, and the `strategy_factory` method. ```python class AbstractStrategy(AbstractStrategy): _attrs_ = ['space'] _immutable_fields_ = ['space'] __metaclass__ = rstrat.StrategyMetaclass import_from_mixin(rstrat.AbstractStrategy) import_from_mixin(rstrategies.SafeIndexingMixin) def __init__(self, space): self.space = space def strategy_factory(self): return self.space.strategy_factory ``` -------------------------------- ### Rewrite Optimization Examples (RPython) Source: https://rpython.readthedocs.io/en/latest/jit/optimizer Illustrates the rewrite optimization, also known as strength reduction, where operations are simplified to more efficient equivalents. Examples include replacing multiplication by 2 with a left bit shift and simplifying boolean/arithmetic operations with constants. ```python x * 2 == x << 1 x & 0 == 0 x - 0 == x ``` -------------------------------- ### Install ARM Chroot Dependencies with Apt Source: https://rpython.readthedocs.io/en/latest/_sources/arm.rst These commands are executed within the ARM chroot environment to configure apt sources and install necessary development packages. This includes libraries like libffi-dev, libgc-dev, python-dev, build-essential, and ncurses. These are required for building and translating RPython programs for ARM. ```bash schroot -c precise_arm -u root echo "deb http://ports.ubuntu.com/ubuntu-ports/ precise main universe restricted" > /etc/apt/sources.list apt-get update apt-get install libffi-dev libgc-dev python-dev build-essential libncurses5-dev libbz2-dev ``` -------------------------------- ### Create ARM chroot with QEMU Source: https://rpython.readthedocs.io/en/latest/arm This snippet demonstrates how to create a root filesystem for an ARM chroot environment using qemu-debootstrap. It specifies the architecture and distribution, and then copies the qemu-arm-static binary into the chroot for ARM binary execution. ```bash mkdir -p /srv/chroot/precise_arm qemu-debootstrap --variant=buildd --arch=armel precise /srv/chroot/precise_arm/ http://ports.ubuntu.com/ubuntu-ports/ cp /usr/bin/qemu-arm-static /srv/chroot/precise_arm/usr/bin/qemu-arm-static ``` -------------------------------- ### Initialize Scratchbox2 for ARM Cross-Compilation Source: https://rpython.readthedocs.io/en/latest/_sources/arm.rst This command initializes a Scratchbox2 environment for ARM cross-compilation. It uses 'sb2-init' to create a new environment named 'ARM', specifying the Qemu ARM binary and the ARM cross-compiler. This sets up Scratchbox2 to use the provided toolchain for compiling ARM binaries. ```bash cd /srv/chroot/precise_arm sb2-init -c `which qemu-arm` ARM `which arm-linux-gnueabi-gcc` ``` -------------------------------- ### Create ARM Chroot with Qemu-debootstrap Source: https://rpython.readthedocs.io/en/latest/_sources/arm.rst This command creates a Debian-based root filesystem for an ARM architecture using Qemu for bootstrapping. It requires the 'qemu-debootstrap' tool and specifies the architecture ('armel') and Ubuntu release ('precise'). The output is placed in the specified directory. ```bash mkdir -p /srv/chroot/precise_arm qemu-debootstrap --variant=buildd --arch=armel precise /srv/chroot/precise_arm/ http://ports.ubuntu.com/ubuntu-ports/ ``` -------------------------------- ### RPython Translator: Translation and Viewing Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Example of using the RPython translator to create a translation object for a given function and then viewing the resulting graph. This requires the 'snippet' module and the Translation class. ```python t = Translation(snippet.is_perfect_number, [int]) t.view() ``` -------------------------------- ### Example Trace Syntax Source: https://rpython.readthedocs.io/en/latest/jit/optimizer Illustrates the syntax of a trace as processed by the RPython JIT optimizer. This format is used internally and in test suites, showing input variables, labels, operations, guards, and jumps. ```text [p0,i0,i1] label(p0, i0, i1) i2 = getarrayitem_raw(p0, i0, descr=) i3 = int_add(i1,i2) i4 = int_add(i0,1) i5 = int_le(i4, 100) # lower-or-equal guard_true(i5) jump(p0, i4, i3) ``` -------------------------------- ### Example Trace Structure Source: https://rpython.readthedocs.io/en/latest/_sources/jit/optimizer.rst Illustrates the structure of a trace, including input parameters, label, operations, and jump instructions. This format is used in the test suite and is similar to runtime logs. ```text [p0,i0,i1] label(p0, i0, i1) i2 = getarrayitem_raw(p0, i0, descr=) i3 = int_add(i1,i2) i4 = int_add(i0,1) i5 = int_le(i4, 100) # lower-or-equal guard_true(i5) jump(p0, i4, i3) ``` -------------------------------- ### Create RISC-V Ubuntu Chroot Filesystem Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Creates the root filesystem for the RISC-V 64-bit Ubuntu chroot using debootstrap. It specifies the architecture, keyring, and Ubuntu release, then prepares the resolv.conf for schroot. ```bash sudo mkdir -p /srv/chroot sudo debootstrap --arch=riscv64 \ --keyring /usr/share/keyrings/ubuntu-archive-keyring.gpg \ --include=ubuntu-keyring \ noble \ /srv/chroot/rv64_ubuntu_24_04 \ http://ports.ubuntu.com/ubuntu-ports # Rename /etc/resolv.conf so that schroot can copy the host resolv.conf # into chroot. sudo mv /srv/chroot/rv64_ubuntu_24_04/etc/resolv.conf{,.bak} ``` -------------------------------- ### RPython Translator: Type Annotation Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Demonstrates the type annotator in the RPython translator, which infers types for functions. After annotation, the graph can be viewed again to see inferred types. ```python t.annotate() t.view() ``` -------------------------------- ### RPython Translator: Translating to C Code Source: https://rpython.readthedocs.io/en/latest/_sources/getting-started.rst Shows how to translate the RPython flow graph into C code and compile it into a shared library. The compiled function can then be executed using ctypes. ```python t.rtype() lib = t.compile_c() f = get_c_function(lib, snippet.is_perfect_number) f(5) f(6) ``` -------------------------------- ### Prepare CPython 2.7 Installation Directory (Chroot) Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Creates and sets ownership for the CPython 2.7 installation directory within the RISC-V chroot. This ensures the compiled Python can be installed correctly. ```bash schroot -c rv64_ubuntu_24_04 -u root -- mkdir /opt/python2 schroot -c rv64_ubuntu_24_04 -u root -- \ chown $(whoami):$(whoami) /opt/python2 ``` -------------------------------- ### Build and Install CPython 2.7 (Chroot) Source: https://rpython.readthedocs.io/en/latest/_sources/riscv.rst Compiles and installs CPython 2.7 from a patched repository within the RISC-V chroot. It configures the build with specific options and uses make for compilation and installation. ```bash git clone https://github.com/loganchien/cpython27-deprecated -b release_27 cd cpython27-deprecated schroot -c rv64_ubuntu_24_04 $ ./configure --prefix=/opt/python2 \ --enable-shared \ --enable-optimizations \ --with-system-ffi LDFLAGS="-Wl,-rpath,/opt/python2/lib" $ make -j8 $ make install -j8 ``` -------------------------------- ### Python Function Definition Example Source: https://rpython.readthedocs.io/en/latest/translation A simple Python function definition used as an example for the flow graph builder. This function takes an integer 'n' and returns 3*n + 2. ```python def f(n): return 3*n+2 ``` -------------------------------- ### Translate 'Hello World' Example (Python) Source: https://rpython.readthedocs.io/en/latest/riscv Defines a simple Python function 'main' that prints 'Hello World' and a 'target' function to be used by the RPython translator. This snippet demonstrates basic RPython translation. ```python def main(args): print "Hello World" return 0 def target(*args): return main, None ```