Giter Club home page Giter Club logo

tsvm's Introduction

TSVM
====

This is a simple virtual machine for the intermediate code of TSLANG
(The Simple Language), written for an undergraduate compiler course.
Students are required to write a compiler for TSLANG to generate
TSLANG IR.  As optional projects, students may optimise the size of
the generated code, write an optimization pass, or generate machine
code from TSLANG IR.

TSLANG IR
=========

Each file consists of one or more procedures; tsvm starts the
execution from a procedure named main.  Each invocation of a procedure
has a private set of registers.  The name of a register is the letter
r followed by its identifier; for instance, r11 denotes the 11th
register.  A procedure can be defined with the proc keyword as
follows:

  proc main
    mov  r1, 2
    mul  r2, r1, r0
    mov  r0, 0
    ret

A procedure may take a number of arguments; when it is called, the
i-th argument is copied into the i-th register (r0 is the first
argument).  The return value is read from r0 when the function returns
and, if specified, is written to the first operand of the call
instruction (the same register that holds the first argument).  In the
following program, sum3 returns the sum of its three argument.

  proc sum3
    add r0, r0, r1
    add r0, r0, r2
    ret
  proc main
    call iget, r3
    call iget, r1
    call iget, r2
    call sum3, r3, r1, r2
    call iput, r3
    mov  r0, 0
    ret

TSLANG IR instructions include mov, add, sub, mul, div, mod, cmp<,
cmp>, cmp==, cmp<=, cmp>=, ld, st, ret, call, jmp, jz, jnz.  See the
files in the test/ directory for more examples.

BUILTIN PROCEDURES
==================

The iget builtin procedure reads a word from the standard input and
iput writes the a word to the standard output.  The following program
reads a word and after multiplying it by 2, prints it to the standard
output.

  proc main
    call iget, r0
    mov  r1, 2
    mul  r2, r1, r0
    call iput, r2
    mov  r0, 0
    ret

The mem builtin procedure allocates a memory region with the size
specified as its first argument and returns its address.  The rel
instruction frees the given memory region.  The following program
allocates 32 bytes of memory and releases it.  It also demonstrates
ld and st instructions for accessing the memory region.

  proc main
    mov  r1, 32
    call mem, r1
    mov  r2, 5
    st   r2, r1		# write r2 to *r1
    ld   r3, r1		# read r3 from *r1
    call rel, r1
    mov  r0, 0
    ret

tsvm's People

Contributors

aligrudi avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

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.