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

Owner
Batuhan Taskaya
Python & F/OSS
Batuhan Taskaya
a simple proof system I made to learn math without any mistakes

math_up a simple proof system I made to learn math without any mistakes 0. Short Introduction test yourself, enjoy your math! math_up is an NBG-based,

양현우 5 Jun 04, 2021
Calibre Libgen Non-fiction / Sci-tech store plugin

CalibreLibgenSci A Libgen Non-Fiction/Sci-tech store plugin for Calibre Installation Download the latest zip file release from here Open Calibre Navig

IDDQD 9 Dec 27, 2022
Eros is an expiremental programming language built using simple Python code.

Eros is an expiremental programming language built using simple Python code. Featuring an easy syntax and unique features like type slicing, the language remains an expirement that grows in down time

zxro 2 Nov 21, 2021
Python with braces. Because Python is awesome, but whitespace is awful.

Bython Python with braces. Because Python is awesome, but whitespace is awful. Bython is a Python preprosessor which translates curly brackets into in

1 Nov 04, 2021
Easily Generate Revolut Business Cards

RevBusinessCardGen Easily Generate Revolut Business Cards Prerequisites Before you begin, ensure you have met the following requirements: You have ins

Younes™ 35 Dec 14, 2022
Supercharge your NFTs with new behaviours and superpowers!

WrapX Supercharge your NFTs with new behaviours and superpowers! WrapX is a collection of Wrappers (currently one - WrapXSet) to decorate your NTFs ad

Emiliano Bonassi 9 Jun 13, 2022
Static bytecode simulator

SEA Static bytecode simulator for creating dependency/dependant based experimental bytecode format for CPython. Example a = random() if a = 5.0:

Batuhan Taskaya 23 Jun 10, 2022
The blancmange curve can be visually built up out of triangle wave functions if the infinite sum is approximated by finite sums of the first few terms.

Blancmange-curve The blancmange curve can be visually built up out of triangle wave functions if the infinite sum is approximated by finite sums of th

Shankar Mahadevan L 1 Nov 30, 2021
A carrot-based color palette you didn't know you needed.

A package to produce a carrot-inspired color palette for python/matplotlib. Install: pip install carrotColors Update: pip install --upgrade carrotColo

10 Sep 28, 2021
With the initiation of the COVID vaccination drive across India for all individuals above the age of 18, I wrote a python script which alerts the user regarding open slots in the vicinity!

cowin_notifier With the initiation of the COVID vaccination drive across India for all individuals above the age of 18, I wrote a python script which

13 Aug 01, 2021
An Advanced Wordlist Library Written In Python For Acm114

RBAPG -RBAPG is the abbreviation of "Rule Based Attack Password Generator". -This module is a wordlist generator module. -You can generate randomly

Aziz Kaplan 11 Aug 28, 2022
mypy plugin for PynamoDB

pynamodb-mypy A plugin for mypy which gives it deeper understanding of PynamoDB (beyond what's possible through type stubs). Usage Add it to the plugi

1 Oct 21, 2022
Feapder的管道扩展

FEAPDER 管道扩展 简介 此模块为feapder的pipelines扩展,感谢广大开发者对feapder的贡献 随着feapder支持的pipelines越来越多,为减少feapder的体积,特将pipelines提出,使用者可按需安装 管道 PostgreSQL 贡献者:沈瑞祥 联系方式:r

boris 9 Dec 07, 2022
This is the improvised version of Dobot Magician which can be implemented for Dobot M1

pydobotM1 This is the edited driver for Dobot M1 version of the original pydobot library intended for use with the Dobot Magician. Here's what you nee

Shaik Abdullah 2 Jul 11, 2022
A web app that is written entirely in Python

University Project About I made this web app to finish a project assigned by my teacher. It is written entirely in Python, thanks to streamlit to make

15 Nov 27, 2022
Cirq is a Python library for writing, manipulating, and optimizing quantum circuits and running them against quantum computers and simulators

Cirq is a Python library for writing, manipulating, and optimizing quantum circuits and running them against quantum computers and simulators. Install

quantumlib 3.6k Jan 07, 2023
A code to clean and extract a bib file based on keywords.

These are two scripts I use to generate clean bib files. clean_bibfile.py: Removes superfluous fields (which are not included in fields_to_keep.json)

Antoine Allard 4 May 16, 2022
A Python program for calculating the 95%CI for GNSS-derived site velocities

GNSS_Vel_95%CI A Python program for calculating the 95%CI for GNSS-derived site velocities Function_GNSS_95CI.py is a Python function for calculating

<a href=[email protected]"> 4 Dec 16, 2022
CarolinaCon CTF Online

CarolinaCon Online CTF CTF challenges from CarolinaCon Online April 23 through April 25, 2021. All challenges from the CTF will eventually be here. Co

49th Security Division 6 May 04, 2022
Tc-python - A Python script to receive message from a twitch chat

Twitch-Chat 📜 I did a script in Python to receive messages from a twitch chat.

miyucode 2 May 31, 2022