6 |
7 |
8 | {{page.title}}
9 | {{ content }}
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/docs/_layouts/subchapter.liquid:
--------------------------------------------------------------------------------
1 | ---
2 | layout: content
3 | ---
4 | {%- include nav_helpers.liquid -%}
5 |
6 |
7 |
8 | {{ curr_chapter_num }}.{{ curr_subchapter_num }}–{{ page.title }}
9 |
10 |
11 |
12 |
13 |
14 |
15 | {{ content }}
16 |
17 |
18 |
19 | {% include subchapter_list.liquid %}
20 |
--------------------------------------------------------------------------------
/docs/_sass/minima.scss:
--------------------------------------------------------------------------------
1 | @charset "utf-8";
2 |
3 | // Define defaults for each variable.
4 |
5 | $base-font-family: "Helvetica Neue", Helvetica, Arial, sans-serif !default;
6 | $base-font-size: 16px !default;
7 | $base-font-weight: 400 !default;
8 | $small-font-size: $base-font-size * 0.875 !default;
9 | $base-line-height: 1.5 !default;
10 |
11 | $spacing-unit: 30px !default;
12 |
13 | $text-color: #111 !default;
14 | $background-color: #fdfdfd !default;
15 | $brand-color: #2a7ae2 !default;
16 |
17 | $grey-color: #828282 !default;
18 | $grey-color-light: lighten($grey-color, 40%) !default;
19 | $grey-color-dark: darken($grey-color, 25%) !default;
20 |
21 | // Width of the content area
22 | $content-width: 800px !default;
23 |
24 | $on-palm: 600px !default;
25 | $on-laptop: 800px !default;
26 |
27 | // Use media queries like this:
28 | // @include media-query($on-palm) {
29 | // .wrapper {
30 | // padding-right: $spacing-unit / 2;
31 | // padding-left: $spacing-unit / 2;
32 | // }
33 | // }
34 | @mixin media-query($device) {
35 | @media screen and (max-width: $device) {
36 | @content;
37 | }
38 | }
39 |
40 | @mixin relative-font-size($ratio) {
41 | font-size: $base-font-size * $ratio;
42 | }
43 |
44 | // Import partials.
45 | @import
46 | "minima/base",
47 | "minima/layout",
48 | "minima/syntax-highlighting"
49 | ;
50 |
--------------------------------------------------------------------------------
/docs/_sass/minima/_base.scss:
--------------------------------------------------------------------------------
1 | /* Website Colours */
2 | $main: #000000;
3 | $omr-red: #af000c;
4 | $b9-yellow: #feda05;
5 | $side-nav: #e0ebeb;
6 | $top-nav: #b3cccc;
7 | $caption: #666666;
8 | $link-colour: $main;
9 | $link-hover: $main;
10 | $link-clicked: #8c8c8c;
11 | $light: #e7edf4;
12 | $header: #ffffff;
13 |
14 |
15 | %vertical-rhythm {
16 | margin-bottom: $spacing-unit / 2;
17 | }
18 |
--------------------------------------------------------------------------------
/docs/_sass/minima/_layout.scss:
--------------------------------------------------------------------------------
1 |
2 | /* Defaults */
3 |
--------------------------------------------------------------------------------
/docs/_sass/syntax-highlight.scss:
--------------------------------------------------------------------------------
1 |
2 | .highlight div {
3 | padding: 20pt;
4 | }
5 |
6 | .highlight pre {
7 | border-radius: 10px;
8 | padding: 10pt;
9 | }
10 |
11 | .highlight pre { color: #f8f8f2; background-color: #272822; }
12 | .highlight .hll { background-color: #272822; }
13 | .highlight .c { color: #75715e } /* Comment */
14 | .highlight .err { color: #960050; background-color: #1e0010 } /* Error */
15 | .highlight .k { color: #66d9ef } /* Keyword */
16 | .highlight .l { color: #ae81ff } /* Literal */
17 | .highlight .n { color: #f8f8f2 } /* Name */
18 | .highlight .o { color: #f92672 } /* Operator */
19 | .highlight .p { color: #f8f8f2 } /* Punctuation */
20 | .highlight .cm { color: #75715e } /* Comment.Multiline */
21 | .highlight .cp { color: #75715e } /* Comment.Preproc */
22 | .highlight .c1 { color: #75715e } /* Comment.Single */
23 | .highlight .cs { color: #75715e } /* Comment.Special */
24 | .highlight .ge { font-style: italic } /* Generic.Emph */
25 | .highlight .gs { font-weight: bold } /* Generic.Strong */
26 | .highlight .kc { color: #66d9ef } /* Keyword.Constant */
27 | .highlight .kd { color: #66d9ef } /* Keyword.Declaration */
28 | .highlight .kn { color: #f92672 } /* Keyword.Namespace */
29 | .highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
30 | .highlight .kr { color: #66d9ef } /* Keyword.Reserved */
31 | .highlight .kt { color: #66d9ef } /* Keyword.Type */
32 | .highlight .ld { color: #e6db74 } /* Literal.Date */
33 | .highlight .m { color: #ae81ff } /* Literal.Number */
34 | .highlight .s { color: #e6db74 } /* Literal.String */
35 | .highlight .na { color: #a6e22e } /* Name.Attribute */
36 | .highlight .nb { color: #f8f8f2 } /* Name.Builtin */
37 | .highlight .nc { color: #a6e22e } /* Name.Class */
38 | .highlight .no { color: #66d9ef } /* Name.Constant */
39 | .highlight .nd { color: #a6e22e } /* Name.Decorator */
40 | .highlight .ni { color: #f8f8f2 } /* Name.Entity */
41 | .highlight .ne { color: #a6e22e } /* Name.Exception */
42 | .highlight .nf { color: #a6e22e } /* Name.Function */
43 | .highlight .nl { color: #f8f8f2 } /* Name.Label */
44 | .highlight .nn { color: #f8f8f2 } /* Name.Namespace */
45 | .highlight .nx { color: #a6e22e } /* Name.Other */
46 | .highlight .py { color: #f8f8f2 } /* Name.Property */
47 | .highlight .nt { color: #f92672 } /* Name.Tag */
48 | .highlight .nv { color: #f8f8f2 } /* Name.Variable */
49 | .highlight .ow { color: #f92672 } /* Operator.Word */
50 | .highlight .w { color: #f8f8f2 } /* Text.Whitespace */
51 | .highlight .mf { color: #ae81ff } /* Literal.Number.Float */
52 | .highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
53 | .highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
54 | .highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
55 | .highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
56 | .highlight .sc { color: #e6db74 } /* Literal.String.Char */
57 | .highlight .sd { color: #e6db74 } /* Literal.String.Doc */
58 | .highlight .s2 { color: #e6db74 } /* Literal.String.Double */
59 | .highlight .se { color: #ae81ff } /* Literal.String.Escape */
60 | .highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
61 | .highlight .si { color: #e6db74 } /* Literal.String.Interpol */
62 | .highlight .sx { color: #e6db74 } /* Literal.String.Other */
63 | .highlight .sr { color: #e6db74 } /* Literal.String.Regex */
64 | .highlight .s1 { color: #e6db74 } /* Literal.String.Single */
65 | .highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
66 | .highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
67 | .highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
68 | .highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
69 | .highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
70 | .highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
71 |
72 | .highlight .gh { } /* Generic Heading & Diff Header */
73 | .highlight .gu { color: #75715e; } /* Generic.Subheading & Diff Unified/Comment? */
74 | .highlight .gd { color: #f92672; } /* Generic.Deleted & Diff Deleted */
75 | .highlight .gi { color: #a6e22e; } /* Generic.Inserted & Diff Inserted */
76 |
--------------------------------------------------------------------------------
/docs/about.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: About Base9
3 | layout: page
4 | section: about
5 | ---
6 |
7 | Base9 is an educational [language runtime] and JavaScript [interpreter]. It's front-end language is a subset of JavaScript with limited functionality. It compiles into a simple set of [bytecodes] which run on a primitive interpreter. We've also plugged in [OMR], a language-agnostic runtime toolkit, which provides our runtime with a [JIT compiler]! Plugging in OMR is made easy with the use of [JitBuilder], a easy-to-use tool that allows you to programmatically describe the semantics of your language's bytecodes.
8 |
9 | [language runtime]: ./docs/Dictionary.md#language-runtime
10 | [bytecodes]: ./docs/Dictionary.md#bytecode
11 | [interpreter]: ./docs/Dictionary.md#interpreter
12 | [OMR]: https://www.eclipse.org/omr/
13 | [JIT compiler]: ./docs/Dictionary.md#jit-compiler
14 | [JitBuilder]: https://developer.ibm.com/open/2016/07/19/jitbuilder-library-and-eclipse-omr-just-in-time-compilers-made-easy/
15 |
16 | Interested in building language runtimes? Check out our [Build your own Runtime] tutorial to learn how! Or if you want to learn more about base9, visit the [base9 overview section] of the tutorial. And be sure to visit our [documentation page] for a complete listing of our docs and tutorials.
17 |
18 | [Build your own Runtime]: ./build-a-runtime/index.md
19 | [base9 overview section]: ./build-a-runtime/index.md#base9-overview
20 | [documentation page]: ./docs/index.md
21 |
--------------------------------------------------------------------------------
/docs/assets/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/favicon.ico
--------------------------------------------------------------------------------
/docs/assets/images/b9backend.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/b9backend.png
--------------------------------------------------------------------------------
/docs/assets/images/b9frontend.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/b9frontend.png
--------------------------------------------------------------------------------
/docs/assets/images/b9overview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/b9overview.png
--------------------------------------------------------------------------------
/docs/assets/images/bcStack1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/bcStack1.png
--------------------------------------------------------------------------------
/docs/assets/images/bcStack2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/bcStack2.png
--------------------------------------------------------------------------------
/docs/assets/images/bcStack3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/bcStack3.png
--------------------------------------------------------------------------------
/docs/assets/images/bcStack4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/bcStack4.png
--------------------------------------------------------------------------------
/docs/assets/images/binModFunctions.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/binModFunctions.png
--------------------------------------------------------------------------------
/docs/assets/images/binModSections.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/binModSections.png
--------------------------------------------------------------------------------
/docs/assets/images/binModStrings.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/binModStrings.png
--------------------------------------------------------------------------------
/docs/assets/images/downArrow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/downArrow.png
--------------------------------------------------------------------------------
/docs/assets/images/github_logo.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
60 |
--------------------------------------------------------------------------------
/docs/assets/images/jitOverview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/jitOverview.png
--------------------------------------------------------------------------------
/docs/assets/images/legend.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/legend.png
--------------------------------------------------------------------------------
/docs/assets/images/logoBase9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/logoBase9.png
--------------------------------------------------------------------------------
/docs/assets/images/logoIcon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/logoIcon.png
--------------------------------------------------------------------------------
/docs/assets/images/logoREADME.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/logoREADME.png
--------------------------------------------------------------------------------
/docs/assets/images/omrOverview.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/omrOverview.png
--------------------------------------------------------------------------------
/docs/assets/images/perfConsole.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/perfConsole.png
--------------------------------------------------------------------------------
/docs/assets/images/vmDesign.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/images/vmDesign.png
--------------------------------------------------------------------------------
/docs/assets/main.scss:
--------------------------------------------------------------------------------
1 | ---
2 | # Front matter comment to ensure Jekyll properly reads file.
3 | ---
4 |
5 | .h1 {
6 | margin-top: 10px;
7 | }
8 |
9 | .h2 {
10 | margin-top: 40px;
11 | }
12 |
13 | .h3 {
14 | margin-top: 100px;
15 | }
16 |
17 | @import
18 | "syntax-highlight"
19 |
--------------------------------------------------------------------------------
/docs/assets/presentations/CASCON2017_SuperchargeALanguageRuntime.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/presentations/CASCON2017_SuperchargeALanguageRuntime.pdf
--------------------------------------------------------------------------------
/docs/assets/presentations/CUSEC2017_BuilderingAJIT.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/presentations/CUSEC2017_BuilderingAJIT.pdf
--------------------------------------------------------------------------------
/docs/assets/presentations/EclipseOMRandJitBuilder-CUSEC.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/presentations/EclipseOMRandJitBuilder-CUSEC.pdf
--------------------------------------------------------------------------------
/docs/assets/presentations/index.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/b9org/b9/da514541c3fd41fb63d2f798b4a27de394a2dd50/docs/assets/presentations/index.md
--------------------------------------------------------------------------------
/docs/b9.org:
--------------------------------------------------------------------------------
1 | #+TITLE: Road Map
2 |
3 | * OMR Cleanup
4 |
5 | * Minor Tasks
6 | ** TODO Force set the glue component names
7 | ** TODO Port visitor scheme to OMR Marking
8 | ** TODO Remove OMR's object iterator code
9 | ** TODO Single transitionSet opimization
10 | - More than just an opti
11 | - Delay allocating the transition table potentially means less rooting during object map allocation
12 |
13 | * Version 1: Basic object features
14 | ** DONE Transition Tables
15 | ** TODO Fixed / Dynamic Slots
16 | ** TODO Split high-level public api from low-level
17 | ** TODO Write barriers
18 | ** TODO Finalization
19 | - List of objects and finalizers
20 | - Finalization calls are dispatched to threads
21 |
22 | *** TODO Expose OMR thread dispatch library to language
23 | - Possibly expose OMR work-sharing API so languages can work out their own multithreadedness in Finalization
24 | - Reuse the GC's work dispatcher for finalization
25 |
26 | * Version 2: Java-style classes
27 | ** TODO Typed, variable width slots
28 | ** TODO Fat Object Maps
29 | - Merge SlotMap, ObjectMap, EmptyObjectMap to one type.
30 |
31 | * Version 3: Jit
32 | ** TODO Jit Integration
33 |
34 | * Version 4: Arrays
35 | ** TODO Array Objects
36 |
37 | * Version 5: Threading and atomics
38 | ** Atomic sets/gets
39 | ** Object Locks
40 | ** Shared heap lock
41 |
42 | * Version 6
43 | ** TODO Constructors and construction sites
44 | *** TODO Slack Tracking
45 | *** TODO Root Constructors
46 |
47 | * Version 7: Dictionary Objects
48 | ** TODO Dictionary Objects
49 |
50 | * Problems
51 | ** Rooting
52 | *** Rooting through Value-slots
53 | *** Rooting a prexisting pointer sloti
54 |
55 | ```
56 |
57 | for (const Map& map : object->mapHierachy()) {
58 | for (const SlotLookup& slot : map->slotRange()) {
59 | switch(slot.desc.type().coreType()) {
60 | case CoreType::REF:
61 | edge.mark()
62 | case CoreType::VALUE:
63 | return handleValue();
64 | }
65 | object->access(slot);
66 |
67 | }
68 | }
--------------------------------------------------------------------------------
/docs/build-a-runtime/1-0-getting-started.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: chapter
3 | title: Base9
4 | subchapters:
5 | - 1-1-the-frontend-language.md
6 | - 1-2-bytecodes.md
7 | - 1-3-modules.md
8 | - 1-4-implementing-the-interpreter.md
9 | - 1-5-compiling-functions-with-jitbuilder.md
10 | ---
11 |
12 | Before getting started, you should [get yourself set up]. We're going
13 | to use it throughout the tutorial to explore and learn about language runtime
14 | concepts. Don't forget to read [about base9] before getting started.
15 |
16 | [get yourself setup with base9]: ../setup/index.md
17 | [about base9]: ../about.md
18 |
19 | ## Base 9
20 |
21 | Base9 has several major components that we'll discuss throughout the course of this tutorial. We'll provide insight about design decisions and implementation. Many of the design and implementation decisions were made based on the specific needs of the project. You may wish to deviate from these decisions along the way in order to best suit your own project.
22 |
23 | {%include fig_img src="b9overview.png" %}
24 |
25 | The above diagram details the two high-level components of the base9 architecture: the [ahead-of-time compilation] (AOT) and the [virtual machine] (VM) units. The AOT unit runs the frontend language through the frontend compiler (the base9 frontend language is a primitive subset of JavaScript). The frontend compiler outputs the [bytecodes] to be consumed by the VM. The VM (or runtime unit) will either execute tbe bytecodes one by one using the [interpreter], or it can employ the [JIT compiler] to produce optimized native machine code.
26 |
27 | [ahead-of-time compilation]: ../docs/Dictionary.md#ahead-of-time-compilation
28 | [virtual machine]: ../docs/Dictionary.md#virtual-machine
29 | [bytecodes]: ../docs/Dictionary.md#bytecode
30 | [interpreter]: ../docs/Dictionary.md#interpreter
31 | [JIT compiler]: ../docs/Dictionary.md#jit-compiler
32 |
33 | ## OMR and JitBuilder
34 |
35 | [OMR] is an open source and reusable C++ library for building language runtimes. It is designed to run on many different hardware and operating system platforms. It contains a JIT compiler, garbage collection, a platform porting library, a thread library, diagnostic services, and monitoring services. It's language agnostic functionality allows developers to easily bootstrap their own language runtimes. It is most notably employed in [OpenJ9], IBM's open sourced Java Virtual Machine, but has also been integrated with Ruby, CSOM, Lua, Swift, and Rosie Pattern Language.
36 |
37 | [OMR]: https://www.eclipse.org/omr/
38 | [OpenJ9]: https://www.eclipse.org/openj9/
39 |
40 | {% include fig_img src="omrOverview.png" cap="OMR Overview" %}
41 |
42 | The above diagram depicts the base9 components in yellow. These are the components that a developer must implement independantly. The red areas are the components belonging to OMR.
43 |
44 | ## JitBuilder
45 |
46 | [JitBuilder] is an interface to the JIT compiler technology in OMR. It's designed to bootstrap a native-code JIT compiler for interpreted methods, and it allows the user to programatically describe the [intermediate language] (IL) that implements the semantics of the bytecodes. Using JitBuilder to employ the OMR JIT is not strictly necessary, but without it, one would require a deep understanding of JIT Compilation. JitBuilder makes it possible for someone without a background in compilers to easily plug-in and use the JIT compiler for their runtime.
47 |
48 | [JitBuilder]: https://developer.ibm.com/open/2016/07/19/jitbuilder-library-and-eclipse-omr-just-in-time-compilers-made-easy/
49 | [intermediate language]: ../docs/Dictionary.md#intermediate-language
50 |
51 |
--------------------------------------------------------------------------------
/docs/build-a-runtime/1-1-the-frontend-language.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: subchapter
3 | title: The Frontend Language
4 | ---
5 |
6 | ## Frontend
7 |
8 | {% include fig_img src="b9frontend.png" %}
9 |
10 | ### Frontend Language
11 |
12 | Our frontend language is a primitive subset of JavaScript. Let's have a look at some code:
13 |
14 | ```js
15 | b9PrintString("Hello World!");
16 | ```
17 |
18 | `b9PrintString` is a method from a tiny library of functions that will be compiled with every program, called `b9stdlib`. This library acts as a kind of prelude . The library is called b9stdlib and can be found in [b9/js_compiler/b9stdlib.js]. The functions in b9stdlib all call base9 [primitive functions]. In the Hello, World! program, Our script uses `b9PrintString` from b9stdlib to write text to console.
19 |
20 | [test/hello.js]: https://github.com/b9org/b9/blob/master/test/hello.js
21 | [primitive functions]: ../docs/Dictionary.md#primitive-function
22 | [b9/js_compiler/b9stdlib.js]: https://github.com/b9org/b9/blob/master/js_compiler/b9stdlib.js
23 |
24 | ### Frontend Compiler
25 |
26 | The frontend compiler is in [js_compiler/compiler.js]. It takes the source code and uses [Esprima] to convert the program into an [abstract syntax tree] (or AST). The frontend compiler walks the AST and converts it into a portable binary format. This portable binary format is represented as a [binary module].
27 |
28 | [js_compiler/compiler.js]: https://github.com/b9org/b9/blob/master/js_compiler/compile.js
29 | [frontend compiler]: https://github.com/b9org/b9/blob/master/js_compiler/compile.js
30 | [Esprima]: http://esprima.org
31 | [abstract syntax tree]: https://en.wikipedia.org/wiki/Abstract_syntax_tree
32 | [binary module]: ../docs/Dictionary.md#binary-module
33 |
34 | For a brief overview of the frontend compiler, as well as a more in depth look at the binary module, visit the link below:
35 |
36 | [Frontend Compiler and Binary Format](/docs/FrontendAndBinaryMod.md)
37 |
38 | Let’s convert the Hello, World! program to its binary format by running it through the frontend compiler. Hello, World! is in [b9/test/hello.js].
39 |
40 | [b9/test/hello.js]: https://github.com/b9org/b9/blob/master/test/hello.js
41 |
42 | From the root directory, run:
43 |
44 | `node js_compiler/compile.js test/hello.js hello.b9mod`
45 |
46 | The above command will run the frontend compiler on `test/hello.js` and output a binary module with the name `hello.b9mod`. If you run the above command, you'll see `hello.b9mod` in the base9 root directory. The `.b9mod` files are in raw hexadecimal format, and are legible using a hex editor like `dhex`.
47 |
--------------------------------------------------------------------------------
/docs/build-a-runtime/1-2-bytecodes.md:
--------------------------------------------------------------------------------
1 | ---
2 | layout: subchapter
3 | title: Bytecodes
4 | ---
5 |
6 | ## Backend
7 |
8 | {%include fig_img src="b9backend.png" %}
9 |
10 | Let's explore the VM design in greater detail.
11 |
12 | {%include fig_img src="vmDesign.png" %}
13 |
14 | The above diagram shows the components of the VM in detail. The VM takes the binary module and uses the deserializer to convert it into an in-memory Module containing the bytecodes. After the conversion, the VM will employ either the interpreter or the JIT to run the program. The interpreter processes the bytecodes directly and one at a time. The JIT converts the bytecodes to native machine code and returns a function pointer. Once a program is JIT compiled, the bytecodes are no longer interpreted. Instead, the JIT compiled version is always executed. Currently, when we run the JIT, we employ user flags to tell the VM to JIT compile an entire program and to interpret nothing.
15 |
16 |
17 | ### Bytecodes
18 |
19 | The base9 instruction set is stack oriented, which allows for straight-forward compilation and simple VM implementation. All instructions operate on the operand stack, which can be thought of as the VM's memory. One advantage of a stack-based instruction set over a register-based model is that stack-based instructions are smaller, with no need for a register immediate. One disadvantage is that the total number of instructions is larger. For more information on the difference between stack and register based virtual machines, you can [read this article on the internet].
20 |
21 | [read this article on the internet]: https://markfaction.wordpress.com/2012/07/15/stack-based-vs-register-based-virtual-machine-architecture-and-the-dalvik-vm/
22 |
23 | All of the base9 bytecodes are fixed-width. This puts constraints on what we can encode in the instructions, but it simplifies instruction decoding and jumps.
24 |
25 | Finally, the base9 instruction set is untyped. This means that the bytecodes can operate on values of varying types. The way this works is by popping the operands off the operand stack, checking their types, and doing the correct operation once the type is known.
26 |
27 | All base9 bytecodes are defined in [b9/include/b9/instructions.hpp].
28 |
29 | [b9/include/b9/instructions.hpp]: https://github.com/b9org/b9/blob/master/b9/include/b9/instructions.hpp
30 |
31 | ### The Operand Stack
32 |
33 | As mentioned, the base9 bytecodes are stack oriented. Let's take a look at what happens with the operand stack during a simple addition function:
34 |
35 | ```js
36 | function simple_add() {
37 | return 5 + 6;
38 | }
39 | ```
40 |
41 | The following diagrams display the bytecodes generated from the "simple_add()" function.
42 |
43 | **IP** = Instruction Pointer
44 |
45 | **SP** = Stack Pointer
46 |
47 | Push 5 onto the operand stack:
48 |
49 | {%include fig_img src="bcStack1.png" %}
50 |
51 | Push 6 onto the operand stack:
52 |
53 | {%include fig_img src="bcStack2.png" %}
54 |
55 | Pop 5 and 6 off that stack, add them, push the result to the operand stack:
56 |
57 | {%include fig_img src="bcStack3.png" %}
58 |
59 | Pop and return the result from the operand stack:
60 |
61 | {%include fig_img src="bcStack4.png" %}
62 |
63 | ### The Deserializer
64 |
65 | The base9 [deserializer] at [b9/src/deserialize.cpp] is responsible for taking the binary module and converting it to the in-memory Module. The deserializer is used in base9 in two different ways. Firstly, it's used by the VM to convert a binary module to an in-memory Module. Secondly, it is used by the disassembler at [b9/b9disasm/b9disasm.cpp]. The disassembler employs the deserializer to convert a binary module into an assembly-like interpretation, which we're calling [base9 assembly]. It's primarily used for debugging. Click the link below to learn more:
66 |
67 | [deserializer]: ../docs/Dictionary.md#deserializer
68 | [b9/src/deserialize.cpp]: https://github.com/b9org/b9/blob/master/b9/src/deserialize.cpp
69 | [b9/b9disasm/b9disasm.cpp]: https://github.com/b9org/b9/blob/master/b9disasm/b9disasm.cpp
70 | [base9 assembly]: ../docs/B9Assembly.md
71 |
72 | [Base9 Disassembler](../docs/Disassembler.md)
73 |
74 | Let's run the disassembler using the binary module we generated in the [Frontend Compiler section]! From the `build/` directory, run the following command:
75 |
76 | [Frontend Compiler section]: #frontend-compiler
77 |
78 | `b9disasm/b9disasm ../hello.b9mod`
79 |
80 | You should now be looking at a human readable version of the Hello, World! program as represented by [base9 assembly]. You'll notice that the first three functions (`b9PrintString`, `b9PrintNumber`, and `b9PrintStack`) are the b9stdlib functions that are included in each compiled program. They can be ignored. The important part is the `