Creating a Language Using Only Assembly Language

73
Creating a language using only assembly language. Kernel/VM Tanken-tai #11 Koichi Nakamura

description

Creating a language using only assembly language

Transcript of Creating a Language Using Only Assembly Language

Page 1: Creating a Language Using Only Assembly Language

Creating a languageusing only

assembly language.Kernel/VM Tanken-tai #11

Koichi Nakamura

Page 2: Creating a Language Using Only Assembly Language

Codes

•https://github.com/nineties/amber

Page 3: Creating a Language Using Only Assembly Language

Profile

• Koichi Nakamura• twitter: @9_ties

• developing an IoT device• http://idein.jp

Page 4: Creating a Language Using Only Assembly Language

I was a compiler writer

• wrote compilers at student experiment• minCaml compiler by O’Caml

• minCaml compiler by Haskellhttps://github.com/nineties/Choco

• studied optimizing compilers at graduate school• wrote compilers for special purpose CPUs

Page 5: Creating a Language Using Only Assembly Language

Wanted to create my own language

• name: “Amber”• It was ‘“rowl” at first.

• I wanted to enjoy the creation process itself.

• How could I?

Page 6: Creating a Language Using Only Assembly Language

Let’s play with limitations

1. Use assembly language only.

2. No libraries.

3. No code generators.

libc etc.

High-level langs. like C

flex/bison etc.

Page 7: Creating a Language Using Only Assembly Language

Strategy:Bootstrapping

Write language 1 by assembly language

Write a little bit high-level language 2 by language 1

Write Amber by language k

Write Amber by Amber here now

Page 8: Creating a Language Using Only Assembly Language

What’s the point?

• For fun.

• To cultivate knowledge, techniques, know-hows of compiler-writing.• But it’s not cost-effective study method...

• To feel a sense of gratitude and respect for predecessors.

Page 9: Creating a Language Using Only Assembly Language

I’ll show the outline of my development process.

Page 10: Creating a Language Using Only Assembly Language

1. Created “rowl0” by assembly language

Page 11: Creating a Language Using Only Assembly Language

Made a little bit high-level lang. more than asm.

• language name: rowl0

• compiler name: rlc

Page 12: Creating a Language Using Only Assembly Language

From regular expressions of tokens

Page 13: Creating a Language Using Only Assembly Language

Wrote a state transition diagram

Page 14: Creating a Language Using Only Assembly Language

Converted to jump table

Page 15: Creating a Language Using Only Assembly Language

And wrote the lexer

Page 16: Creating a Language Using Only Assembly Language

Wrote rowl0’s syntax by BNF

Page 17: Creating a Language Using Only Assembly Language

Then wrote the parser

• recursive descent method

Page 18: Creating a Language Using Only Assembly Language

Generates codes together with parsing

• writing memory management is difficult here.

• generates codes without building syntax trees.

code generation

parsing

Page 19: Creating a Language Using Only Assembly Language

Completed the first language “rowl0”!

• no symbol tables.• function params must be

p0,p1,p2,...

• to use local variables, allocate stack mems by “allocate(n)” then usex0,x1,x2,...

Page 20: Creating a Language Using Only Assembly Language

2.Created a LISP “rowl-core” by “rowl0”

Page 21: Creating a Language Using Only Assembly Language

Made a LISP temporarily

• language name: rowl-core

• interpreter name: rlci

• easy to implement

• productivity improvement

Page 22: Creating a Language Using Only Assembly Language

Wrote lexer and parser

Page 23: Creating a Language Using Only Assembly Language

Writing became more comfortable

Page 24: Creating a Language Using Only Assembly Language

Wrote eval

Page 25: Creating a Language Using Only Assembly Language

No memory management

• mmap and munmap is the only function

1. Does not recovery garbage memories

2. Allocates fresh memories for new objects

3. So, it will die eventually

• When it can compile the next generation compilers, it’s no problem.

malloc, free

Page 26: Creating a Language Using Only Assembly Language

Completed a LISP “rowl-core”!

• rich functions• lambda, map etc.

• macros

Page 27: Creating a Language Using Only Assembly Language

3.Created a language to write “VM” by “rowl-core”

Page 28: Creating a Language Using Only Assembly Language

Decided to create a VM for the next generation

• Created a language just for writing the virtual machine.

• Defined it as a DSL in the LISP “rowl-core”• No need of writing lexer and parser!

Page 29: Creating a Language Using Only Assembly Language

Wrote the compiler like this

Page 30: Creating a Language Using Only Assembly Language

Now I could use higher-order functions

• productivity was improved a lot

Page 31: Creating a Language Using Only Assembly Language

4.Created a virtual machine “rlvm” by the DSL

Page 32: Creating a Language Using Only Assembly Language

Wrote codes of VM with the DSL like this

Page 33: Creating a Language Using Only Assembly Language

Wrote a garbage collector

• Copying GC

• Cheney’s algorithm

Page 34: Creating a Language Using Only Assembly Language

Wrote primitive functions

Page 35: Creating a Language Using Only Assembly Language

An application of meta-programming

• The table of instructions of the VM

Page 36: Creating a Language Using Only Assembly Language

Generates various codes from the table

• reflects changes of instructions automatically

• It is very easy to make this kind of mechanism with LISP

vm_instructions

eval loop of the VM

LinkerDisassemblerAssembler

Assembler used internally in Amber

Page 37: Creating a Language Using Only Assembly Language

Wrote instruction sets

Page 38: Creating a Language Using Only Assembly Language

Floating point arithmetics

Page 39: Creating a Language Using Only Assembly Language

Multi-precision integer arithmetics

Page 40: Creating a Language Using Only Assembly Language

Exception handling

Page 41: Creating a Language Using Only Assembly Language

Delimited Continuation

Page 42: Creating a Language Using Only Assembly Language

Completed the virtual machine “rlvm”!

• 186 instructions

• stack machine

• copying GC

• exception handling

• shift/reset delimited continuation

• floating-point arithmetics, multi-precision arithmetics

Page 43: Creating a Language Using Only Assembly Language

5.Created a tool chain for “rlvm”

Page 44: Creating a Language Using Only Assembly Language

There was no programming tools for “rlvm”

• Created a tool chain for the VM• a programming language “rowl1”

• its compiler

• assembler

• disassembler

• linker

Page 45: Creating a Language Using Only Assembly Language

Wrote “rowl1”, assembler and compiler

• Defined as a DSL of “rowl-core”

Page 46: Creating a Language Using Only Assembly Language

Wrote linker and disassembler

• Wrote these tools by “rowl1”, so they run on “rlvm”

• The linker requires GC since it uses a lot of memory

Page 47: Creating a Language Using Only Assembly Language

Example outputs of the disassembler

Page 48: Creating a Language Using Only Assembly Language

Ready to program on “rlvm”!

•writing programs for rlvm

• disassembling of byte-codes

• supports separate compilation

• Reached the starting line

Page 49: Creating a Language Using Only Assembly Language

6.Wrote “Amber” by “rowl1”

Page 50: Creating a Language Using Only Assembly Language

Started developing “Amber”

• dynamic scripting language

• instance-based object-oriented system

• run on rlvm

Page 51: Creating a Language Using Only Assembly Language

Wrote an assembler

• The former assembler assembles codes ahead of time and run on rlci

• This assembler assembles codes just in time and run on rlvm• fills addresses by

backpatching

Page 52: Creating a Language Using Only Assembly Language

Wrote the object system

• slots, messages and parent delegation

Page 53: Creating a Language Using Only Assembly Language

Wrote Amber’s core feature on the system

• dynamic pattern-matching engine

• mechanism of partial function fusion

Page 54: Creating a Language Using Only Assembly Language

Wrote the compiler

• Made Amber compiler as one of Amber objects

VM

object system

pattern-matching engine

compiler

Amber’s core system

matching of syntax tree

resource management

Page 55: Creating a Language Using Only Assembly Language

Wrote closure-conversion

Page 56: Creating a Language Using Only Assembly Language

Wrote parsers

• compiles parsers at run-time

• each parser is a usual Amber object (closure)

VM

object system

pattern-matching engine

compiler

Amber core system

compile

parsers

Page 57: Creating a Language Using Only Assembly Language

very simple syntax

1. literals are expressions

2. for a symbol h and expressions e1,..,en (n>=0),h{e1, ..., en} is an expression

3.no other form of Amber’s expression

Page 58: Creating a Language Using Only Assembly Language

Used Packrat parsing method

• scanner less

Page 59: Creating a Language Using Only Assembly Language

Encoding/decoding floating-point literal was difficult

• wrote them by my self because of “no libc” limitation

• require multi-precision integer arithmetic which I wrote before

“3.14” 0x40091eb851eb851f

strtod, sprintf

Page 60: Creating a Language Using Only Assembly Language

Amber interpreter is completed!

• dynamic scripting language

• run on rlvm

• instance-based object oriented system

• dynamic pattern-matching engine

• partial function fusion

• lexical closure

• I got modern programming language!

Page 61: Creating a Language Using Only Assembly Language

7. Created Amber’s standard library

Page 62: Creating a Language Using Only Assembly Language

Amber has strong self extensibility

• Amber’s simple syntax is extended in a standard library• amber/lib/syntax/parse.ab

• Builds its syntax during boot sequence

Page 63: Creating a Language Using Only Assembly Language

Only has very simple syntax at first

used string literal for commentsbecause there is no syntax for comments

Page 64: Creating a Language Using Only Assembly Language

Defines a syntax for defining syntaxes

Page 65: Creating a Language Using Only Assembly Language

Defines Amber’s syntax with the syntax

Page 66: Creating a Language Using Only Assembly Language

Builds macro system

Page 67: Creating a Language Using Only Assembly Language

Gives meanings to syntaxes by macros

Page 68: Creating a Language Using Only Assembly Language

Now Amber got rich syntax

Page 69: Creating a Language Using Only Assembly Language

Extends object system

Page 70: Creating a Language Using Only Assembly Language

Now Amber got rich object system

• Inheritence, mix-in etc.

Page 71: Creating a Language Using Only Assembly Language

Now the development is under suspension

• No plans of further updates

• Try following command to invoke Amber shell

• See the outputs of the make command

% git clone https://github.com/nineties/amber.git% cd amber% make; sudo make install% amber

Page 72: Creating a Language Using Only Assembly Language

Summary

rowl0

rlc rlci

rowl-coreas

lang. for writing VM

rlvm

rowl1

linker

disassembler

compiler

compiler

Amber

interpreter

impl. impl.

run

self-extension

language

tool

• I could reach relatively high-level language. Feel satisfied.

Page 73: Creating a Language Using Only Assembly Language