Giter VIP home page Giter VIP logo

minic-hosting's Introduction

What is it?

  1. A simple stack-based virtual machine that runs C in the browser.

  2. An interactive tutorial that covers C, how the VM works, and how the language is compiled, everything from the ground up.

Why?

This project demystifies compilers without getting lost in unnecessary details. MiniC visually explores how a compiler can break down the C language into simple instructions and how those are executed by a virtual CPU.

alt text

Can I see it?

  1. Sandbox

  2. Tutorial (for people with 0 programming experience or willing to learn C) :

    • Part 1 - Introduction
    • Part 2 - Expressions (part 1)
    • Part 3 - Expressions (part 2)
    • Part 4 - Variables and program structure

Subscribe

Get notified when new tutorials are released.

Feedback

Join the discussion on our subreddit.

Support

Consider supporting the project.

Documentation

Virtual Instruction Set

Note: BEFORE each instrunction, IP increases by 1

PUSH

  • increases SP by 1 and sets the value at SP to the argument ("Pushes the argument onto the stack")

LEA:

  • similar to PUSH
  • instead of pushing just the argument, it pushes BP + argument ("Loads the Effective Address")
  • used to push the address of a local variable on the stack ("LEA 2" is used to push the address of the second local variable on the stack)

DRF:

  • replaces the address on the top of the stack with the value at that address ("Dereferences the address")
  • e.g. to push the value of a local variable, we use LEA follwed by DRF

POP

  • decreases SP by 1 ("pops a value off the stacK")
  • used if the value on the top of the stack is not needed, to prevent the stack from growing indefinitely
  • e.g. after calling a function without indending to use its returned value

JMP

  • sets IP to the argument ("jump to the address gives as argument")
  • used to compile control structures such as "if" and "while"

JZ:

  • pops a value off the stack, and if that value is 0, it jumps to the argument ("jump if zero")
  • used to compile constrol structures such as "if" and "while"

FSTART:

  • a sequence of simpler instructions performed before each function ("Function START");
    • pushes the value of BP effectively saving it before setting BP
    • sets BP to SP
    • increase SP by the value of the argument, effectively making room for local variables
  • effectively sets up the stack so that BP so that BP points to the old BP, BP + 1 to the first local variable, BP + 2 to the second, etc.
  • before the function returns, BP will be restored to its old value

RET:

  • a sequence of simpler instructions performed before each function RETurns
    • remembers the value on the top of the stack. this is the function return value.
    • sets SP to BP, effectively popping all the local variables and the returned value
    • sets BP to the value on the top of the stack, effectively restoring BP to its value before FSTART
    • performs a POP
    • sets IP to the value on the top of the stack, effectively setting IP to point to the next instrunction after the CALL that created this function
    • performs a POP
    • decreases SP by the argument, effectively popping all the arguments pushed before the function was called
    • replaces the value on the top of the stack (which is the address of the current function) with the function return value remembered earlier all the previous POPs ensure that SP is now set to its original value before calling the function + 1. The top of the stack is the returned value of the function

CALL: a sequence of simpler instructions performed to CALL a function. it assumes that the address of the function followed by all the arguments have already been pushed

  • Pushes IP (which was already increased by 1), effectively pushing the address of the next instruction. This value will be used by the RET instrunction to set IP to the address of next instrunction after CALL
  • sets ip to SP - 1 - argument, with is the address of the FSTART instrunction of the function being called

ALLOC:

  • generated by the new operator
  • a sequence of simpler instructions performed to ALLOCate memory dynammically
    • remembers the value on the top of the stack, which is the requsted amount of memory
    • saves the value of HP
    • increases HP by the requsted amount of memory
    • pushes the previously saved value of HP onto the stack

ASSIGN:

  • used to chnage the value at some address to the value on the top of the stack (ASSiGNment), e.g. "a = 2;"
  • remembers VAL, the value on the top of the stack
  • sets the value at address SP - 1 to VAL
  • performs a POP
  • sets the value on the top of the stack to VAL. This leaves the door open to multiple variable assignment (such as "a = b = 3;")

PLUSEQ:

  • just like ASSIGN, but performs addition instead of replacing the value (e.g. a += 2;)

MINUSEQ:

  • just like ASSIGN, but performs subtraction instead of replacing the value (e.g. a -= 2;)

TIMESEQ:

  • just like ASSIGN, but performs multiplication instead of replacing the value (e.g. a *= 2;)

DIVEQ:

  • just like ASSIGN, but performs division instead of replacing the value (e.g. a /= 2;)

MODEQ:

  • just like ASSIGN, but performs the moduls operation instead of replacing the value (e.g. a %= 2;)

TIMES:

  • POPs the two values on the top of the stack, multiplies them, and pushes the result (e.g. 6 * 3)

DIV:

  • POPs the two values on the top of the stack, divides them, and pushes the result (e.g. 6 / 3)

MOD:

  • POPs the two values on the top of the stack, performs the modulus operation on them, and pushes the result (e.g. 6 % 3)

PLUS:

  • POPs the two values on the top of the stack, adds them, and pushes the result (e.g. 6 + 3)

MINUS:

  • POPs the two values on the top of the stack, substracts them, and pushes the result (e.g. 6 - 3)

NOT:

  • ...
  • is used to compile "!expression"

OPP:

  • ...
  • is used to compile "-" in "-expression" ("OPPosite")

EQ, NE, LT, GT, LTE, GTE, AND, OR:

  • ...
  • are used to compile ==, !=, <, >, <=, >=, &&, || (e.g. "a < 3 || b == 5")

Missing language features

  • only bool, int, char and pointers as data types and they all have the same size in memory

  • no malloc, but operator new is working (like in C++),

  • no static arrays and structs (dynamic arrays and pointer to structs work fine).

  • no arrays of structs (arrays of pointers to structs works fine).

  • no for and switch statements

  • no preprocessor directives

  • no bitwise operators

  • no ++, --, ternary operators

  • no union and no enum

  • no global variables

  • no function pointers

  • no free / delete operator

  • no function overloading

minic-hosting's People

Contributors

0xflotus avatar timdpr avatar vasyop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

minic-hosting's Issues

Feature Request: Option to disable typewriter display of tutorial text.

Awesome project!

Would you consider an option to disable the typewriter-style display of the tutorial text? It might just be me, but I find it really distracting.

Unrelated: The compile button obscures the code. I can open a new issue for it if you'd like. A simple fix would be to move the button to the top of the code column as if it were a header.
screen shot 2019-02-09 at 1 34 33 pm

Only blank page on Safari

When opening the Demo, I see only a blank page when using Safari.

Safari Version 12.0 (14606.1.36.1.3)
macOS Mojave 10.14

The Javascript console shows no errors, only one line
WASM initialized

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.