Static bytecode simulator

Overview

SEA

Static bytecode simulator for creating dependency/dependant based experimental bytecode format for CPython.

Example

a = random()

if a >= 5.0:
    print('a is too big')
    a = 5.0
elif a <= 2.0:
    print('a is too small')
    a *= 2

for counter in range(int(a)):
    if counter > 4:
        print('up to 4')
        break
    
    print(counter)

print('done')

Would normally compile to this

Original dis output (click to expand)
  1           0 LOAD_NAME                0 (random)
              2 CALL_FUNCTION            0
              4 STORE_NAME               1 (a)

  3           6 LOAD_NAME                1 (a)
              8 LOAD_CONST               0 (5.0)
             10 COMPARE_OP               5 (>=)
             12 POP_JUMP_IF_FALSE       28

  4          14 LOAD_NAME                2 (print)
             16 LOAD_CONST               1 ('a is too big')
             18 CALL_FUNCTION            1
             20 POP_TOP

  5          22 LOAD_CONST               0 (5.0)
             24 STORE_NAME               1 (a)
             26 JUMP_FORWARD            24 (to 52)

  6     >>   28 LOAD_NAME                1 (a)
             30 LOAD_CONST               2 (2.0)
             32 COMPARE_OP               1 (<=)
             34 POP_JUMP_IF_FALSE       52

  7          36 LOAD_NAME                2 (print)
             38 LOAD_CONST               3 ('a is too small')
             40 CALL_FUNCTION            1
             42 POP_TOP

  8          44 LOAD_NAME                1 (a)
             46 LOAD_CONST               4 (2)
             48 INPLACE_MULTIPLY
             50 STORE_NAME               1 (a)

 10     >>   52 LOAD_NAME                3 (range)
             54 LOAD_NAME                4 (int)
             56 LOAD_NAME                1 (a)
             58 CALL_FUNCTION            1
             60 CALL_FUNCTION            1
             62 GET_ITER
        >>   64 FOR_ITER                32 (to 98)
             66 STORE_NAME               5 (counter)

 11          68 LOAD_NAME                5 (counter)
             70 LOAD_CONST               5 (4)
             72 COMPARE_OP               4 (>)
             74 POP_JUMP_IF_FALSE       88

 12          76 LOAD_NAME                2 (print)
             78 LOAD_CONST               6 ('up to 4')
             80 CALL_FUNCTION            1
             82 POP_TOP

 13          84 POP_TOP
             86 JUMP_ABSOLUTE           98

 15     >>   88 LOAD_NAME                2 (print)
             90 LOAD_NAME                5 (counter)
             92 CALL_FUNCTION            1
             94 POP_TOP
             96 JUMP_ABSOLUTE           64

 17     >>   98 LOAD_NAME                2 (print)
            100 LOAD_CONST               7 ('done')
            102 CALL_FUNCTION            1
            104 POP_TOP
            106 LOAD_CONST               8 (None)
            108 RETURN_VALUE

However with basic SEA, it is getting compiled to this

python -m sea (click to expand)
$0 = LOAD_NAME('random')
$1 = CALL_FUNCTION(0, $0)
$2 = STORE_NAME('a', $1)
$3 = LOAD_NAME('a')
$4 = LOAD_CONST(5.0)
$5 = COMPARE_OP('>=', $3, $4)
$6 = POP_JUMP_IF_FALSE(28, $5)
$7 = LOAD_NAME('print')
$8 = LOAD_CONST('a is too big')
$9 = CALL_FUNCTION(1, $7, $8)
$10 = POP_TOP($9)
$11 = LOAD_CONST(5.0)
$12 = STORE_NAME('a', $11)
$13 = JUMP_FORWARD(52)
$14 = LOAD_NAME('a')
$15 = LOAD_CONST(2.0)
$16 = COMPARE_OP('<=', $14, $15)
$17 = POP_JUMP_IF_FALSE(52, $16)
$18 = LOAD_NAME('print')
$19 = LOAD_CONST('a is too small')
$20 = CALL_FUNCTION(1, $18, $19)
$21 = POP_TOP($20)
$22 = LOAD_NAME('a')
$23 = LOAD_CONST(2)
$24 = INPLACE_MULTIPLY($22, $23)
$25 = STORE_NAME('a', $24)
$26 = LOAD_NAME('range')
$27 = LOAD_NAME('int')
$28 = LOAD_NAME('a')
$29 = CALL_FUNCTION(1, $27, $28)
$30 = CALL_FUNCTION(1, $26, $29)
$31 = GET_ITER($30)
$32 = FOR_ITER(98)
$33 = STORE_NAME('counter', $32)
$34 = LOAD_NAME('counter')
$35 = LOAD_CONST(4)
$36 = COMPARE_OP('>', $34, $35)
$37 = POP_JUMP_IF_FALSE(88, $36)
$38 = LOAD_NAME('print')
$39 = LOAD_CONST('up to 4')
$40 = CALL_FUNCTION(1, $38, $39)
$41 = POP_TOP($40)
$42 = POP_TOP($31)
$43 = JUMP_ABSOLUTE(98)
$44 = LOAD_NAME('print')
$45 = LOAD_NAME('counter')
$46 = CALL_FUNCTION(1, $44, $45)
$47 = POP_TOP($46)
$48 = JUMP_ABSOLUTE(64)
$49 = LOAD_NAME('print')
$50 = LOAD_CONST('done')
$51 = CALL_FUNCTION(1, $49, $50)
$52 = POP_TOP($51)
$53 = LOAD_CONST(None)
$54 = RETURN_VALUE($53)

And with the IR mode, it even supports basic control flow operations accross blocks;

python -m sea --enable-ir (click to expand)
Block $B0: 
    $0 = LOAD_NAME('random')
    $1 = CALL_FUNCTION(0, $0)
    $2 = STORE_NAME('a', $1)
    $3 = LOAD_NAME('a')
    $4 = LOAD_CONST(5.0)
    $5 = COMPARE_OP('>=', $3, $4)
    $6 = POP_JUMP_IF_FALSE(28, $5)
    may jump to: $B1 (on false), $B2 (on true)
Block $B1: 
    $7 = LOAD_NAME('a')
    $8 = LOAD_CONST(2.0)
    $9 = COMPARE_OP('<=', $7, $8)
    $10 = POP_JUMP_IF_FALSE(52, $9)
    may jump to: $B3 (on false), $B4 (on true)
Block $B2: 
    $11 = LOAD_NAME('print')
    $12 = LOAD_CONST('a is too big')
    $13 = CALL_FUNCTION(1, $11, $12)
    $14 = POP_TOP($13)
    $15 = LOAD_CONST(5.0)
    $16 = STORE_NAME('a', $15)
    $17 = JUMP_FORWARD(52)
    may jump to: $B3
Block $B3: 
    $18 = LOAD_NAME('range')
    $19 = LOAD_NAME('int')
    $20 = LOAD_NAME('a')
    $21 = CALL_FUNCTION(1, $19, $20)
    $22 = CALL_FUNCTION(1, $18, $21)
    $23 = GET_ITER($22)
    $24 = FOR_ITER(98)
    may jump to: $B5 (on exhaust), $B6 (on loop)
Block $B4: 
    $25 = LOAD_NAME('print')
    $26 = LOAD_CONST('a is too small')
    $27 = CALL_FUNCTION(1, $25, $26)
    $28 = POP_TOP($27)
    $29 = LOAD_NAME('a')
    $30 = LOAD_CONST(2)
    $31 = INPLACE_MULTIPLY($29, $30)
    $32 = STORE_NAME('a', $31)
    may jump to: $B3
Block $B5: 
    $33 = LOAD_NAME('print')
    $34 = LOAD_CONST('done')
    $35 = CALL_FUNCTION(1, $33, $34)
    $36 = POP_TOP($35)
    $37 = LOAD_CONST(None)
    $38 = RETURN_VALUE($37)
Block $B6: 
    $39 = STORE_NAME('counter', $24)
    $40 = LOAD_NAME('counter')
    $41 = LOAD_CONST(4)
    $42 = COMPARE_OP('>', $40, $41)
    $43 = POP_JUMP_IF_FALSE(88, $42)
    may jump to: $B7 (on false), $B8 (on true)
Block $B7: 
    $44 = LOAD_NAME('print')
    $45 = LOAD_NAME('counter')
    $46 = CALL_FUNCTION(1, $44, $45)
    $47 = POP_TOP($46)
    $48 = JUMP_ABSOLUTE(64)
    may jump to: $B3
Block $B8: 
    $49 = LOAD_NAME('print')
    $50 = LOAD_CONST('up to 4')
    $51 = CALL_FUNCTION(1, $49, $50)
    $52 = POP_TOP($51)
    $53 = POP_TOP($23)
    $54 = JUMP_ABSOLUTE(98)
    may jump to: $B5

For visualizing this output, you can simply pass --show-graph option; graph

You might also like...
Cross-platform MachO/ObjC Static binary analysis tool & library. class-dump + otool + lipo + more

ktool Static Mach-O binary metadata analysis tool / information dumper pip3 install k2l Development is currently taking place on the @python3.10 branc

This Python library searches through a static directory and appends artist, title, track number, album title, duration, and genre to a .json object

This Python library searches through a static directory (needs to match your environment) and appends artist, title, track number, album title, duration, and genre to a .json object. This .json object is then used to post data to a specified table in a local MySQL database, credentials of which the user must set.

Bootstraparse is a personal project started with a specific goal in mind: creating static html pages for direct display from a markdown-like file

Bootstraparse is a personal project started with a specific goal in mind: creating static html pages for direct display from a markdown-like file

LeetComp - Background tasks powering the static content at LeetComp

LeetComp Analysing compensations mentioned on the Leetcode forums (https://kuuts

This is a a CSMA/CA simulator written in Python based on simulator of the same type

This is a a CSMA/CA simulator written in Python based on simulator of the same type found the link https://github.com/StevenSLXie/CSMA-Simulator with

Crypto-trading-simulator - Cryptocurrency trading simulator using Python, Streamlit
Crypto-trading-simulator - Cryptocurrency trading simulator using Python, Streamlit

Crypto Trading Simulator Run streamlit run main.py Dependency Python 3 streamli

Static Features Classifier - A static features classifier for Point-Could clusters using an Attention-RNN model

Static Features Classifier This is a static features classifier for Point-Could

A toy compiler that can convert Python scripts to pickle bytecode 🥒

Pickora 🐰 A small compiler that can convert Python scripts to pickle bytecode. Requirements Python 3.8+ No third-party modules are required. Usage us

Python bytecode manipulation and import process customization to do evil stuff with format strings. Nasty!

formathack Python bytecode manipulation and import process customization to do evil stuff with format strings. Nasty! This is an answer to a StackOver

The disassembler parses evm bytecode from the command line or from a file.

EVM Bytecode Disassembler The disassembler parses evm bytecode from the command line or from a file. It does not matter whether the bytecode is prefix

A simple Python bytecode framework in pure Python

A simple Python bytecode framework in pure Python

A Python Bytecode Disassembler helping reverse engineers in dissecting Python binaries
A Python Bytecode Disassembler helping reverse engineers in dissecting Python binaries

A Python Bytecode Disassembler helping reverse engineers in dissecting Python binaries by disassembling and analyzing the compiled python byte-code(.pyc) files across all python versions (including Python 3.10.*)

An interpreter for the X1 bytecode.

X1 Bytecode Interpreter The X1 Bytecode is bytecode designed for simplicity in programming design and compilation. Bytecode Instructions push

A bytecode vm written in python.

CHex A bytecode vm written in python. hex command meaning note: the first two hex values of a CHex program are the magic number 0x01 (offset in memory

An api, written in Python, for Investopedia's paper trading stock simulator.

investopedia-trading-api An API, written in Python, for Investopedia's paper trading stock simulator. Pull requests welcome. This library is now Pytho

TACTO: A Fast, Flexible and Open-source Simulator for High-Resolution Vision-based Tactile Sensors
TACTO: A Fast, Flexible and Open-source Simulator for High-Resolution Vision-based Tactile Sensors

TACTO: A Fast, Flexible and Open-source Simulator for High-Resolution Vision-based Tactile Sensors This package provides a simulator for vision-based

Autonomous Driving project for Euro Truck Simulator 2
Autonomous Driving project for Euro Truck Simulator 2

hope-autonomous-driving Autonomous Driving project for Euro Truck Simulator 2 Video: How is it working ? In this video, the program processes the imag

GNS3 Graphical Network Simulator

GNS3-gui GNS3 GUI repository.

A Python simple Dice Simulator just for fun
A Python simple Dice Simulator just for fun

Dice Simulator 🎲 A Simple Python Dice Simulator 🧩 🎮 💭 Description: That program make your RPG session more easy and simple. Roll the dice never be

Owner
Batuhan Taskaya
Python & F/OSS
Batuhan Taskaya
Python Monopoly Simulator

Monopoly simulator Original creator: Games Computer Play YouTube: https://www.youtube.com/channel/UCTrp88f-QJ1SqKX8o5IDhWQ Config file (optional) conf

Games Computers Play 37 Jan 3, 2023
HiSim - House Infrastructure Simulator

HiSim is a Python package for simulation and analysis of household scenarios using modern components as alternative to fossil fuel based ones.

FZJ-IEK3 17 Dec 17, 2022
A simple PID tuner and simulator.

PIDtuner-V0.1 PlantPy PID tuner version 0.1 Features Supports first order and ramp process models. Supports Proportional action on PV or error or a sp

null 3 Jun 23, 2022
DNA Storage Simulator that analyzes and simulates DNA storage

DNA Storage Simulator This monorepository contains code for a research project by Mayank Keoliya and supervised by Djordje Jevdjic, that analyzes and

Mayank Keoliya 3 Sep 25, 2022
Architecture example simulator

SCADA architecture Example of a SCADA-like console application, used to serve as a minimal example of a standard architecture of an IIoT system. Insta

null 1 Nov 6, 2021
Ice Skating Simulator for Winter and Christmas [yay]

Ice Skating Simulator for Winter and Christmas [yay]

null 1 Aug 21, 2022
Python PID Controller and Process Simulator (FOPDT) with GUI.

PythonPID_Simulator Python PID Controller and Process Simulator (FOPDT) with GUI. Run the File. Then select Model Values and Tune PID.. Hit Refresh to

null 19 Oct 14, 2022
Python / C++ based particle reaction-diffusion simulator

ReaDDy (Reaction Diffusion Dynamics) is an open source particle based reaction-diffusion simulator that can be configured and run via Python. Currentl

ReaDDy 46 Dec 9, 2022
solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

solsim is the Solana complex systems simulator. It simulates behavior of dynamical systems—DeFi protocols, DAO governance, cryptocurrencies, and more—built on the Solana blockchain

William Wolf 12 Jul 13, 2022
Run the Tianxunet software on the Xiaoyao Android simulator

Run the Tianxunet software on the Xiaoyao Android simulator, and automatically fill in the answers of English listening on the premise of having answers

null 1 Feb 13, 2022