Scala : an LLVM-targeted Scala compiler

Similar documents
Pace University. Fundamental Concepts of CS121 1

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Notes from a Short Introductory Lecture on Scala (Based on Programming in Scala, 2nd Ed.)

Ruby: Introduction, Basics

Scala, Your Next Programming Language

CS 11 Haskell track: lecture 1

Ruby: Introduction, Basics

G Programming Languages - Fall 2012

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Introduction to Programming Using Java (98-388)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Swift. Introducing swift. Thomas Woodfin

The role of semantic analysis in a compiler

Ruby: Introduction, Basics

Scala. Fernando Medeiros Tomás Paim

Language Proposal: tail

First Programming Language in CS Education The Arguments for Scala

Overloading, Type Classes, and Algebraic Datatypes

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Semantic Analysis. Lecture 9. February 7, 2018

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Classical Themes of Computer Science

CS205: Scalable Software Systems

Chapter 1. Fundamentals of Higher Order Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Index COPYRIGHTED MATERIAL

Functional programming

JavaScript: Sort of a Big Deal,

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

5/23/2015. Core Java Syllabus. VikRam ShaRma

CS 314 Principles of Programming Languages

The Warhol Language Reference Manual

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

CMSC 330: Organization of Programming Languages

G Programming Languages - Fall 2012

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Robot Programming with Lisp

learning objectives learn about variables, their types and their values learn about different number representations

SWIFT! init(title: String) { self.title = title } // required initializer w/ named parameter

GOLD Language Reference Manual

Introduction to OCaml

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Functional Programming. Pure Functional Programming

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Overview. Elements of Programming Languages. Objects. Self-Reference

Concepts of Programming Languages

CS 139 Practice Midterm Questions #2

The Typed Racket Guide

Functions, Closures and Control Abstraction

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

Programming in Scala Second Edition

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

CMSC 330: Organization of Programming Languages. Functional Programming with Lists

Functional Programming and Haskell

CS558 Programming Languages

CS 360 Programming Languages Interpreters

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Typescript on LLVM Language Reference Manual

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Language Reference Manual simplicity

FRAC: Language Reference Manual

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Object Model Comparisons

CMSC 330: Organization of Programming Languages. Rust Basics

Control Structures. Christopher Simpkins CS 3693, Fall Chris Simpkins (Georgia Tech) CS 3693 Scala / 1

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

Introduction to Functional Programming

Axiomatic Rules. Lecture 18: Axiomatic Semantics & Type Safety. Correctness using Axioms & Rules. Axiomatic Rules. Steps in Proof

The Lorax Programming Language

! Broaden your language horizons! Different programming languages! Different language features and tradeoffs. ! Study how languages are implemented

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

Java+- Language Reference Manual

What s different about Factor?

F# for Industrial Applications Worth a Try?

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

CSCI-GA Scripting Languages

Functional programming in LISP

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CHAD Language Reference Manual

...something useful to do with the JVM.

SOAPScript For squeaky-clean code, use SOAPScript Language Summary Bob Nisco Version 0.5 β

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

n n Official Scala website n Scala API n

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

YOLOP Language Reference Manual

DaMPL. Language Reference Manual. Henrique Grando

Review for Test 1 (Chapter 1-5)

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Transcription:

Scala : an LLVM-targeted Scala compiler Da Liu, UNI: dl2997 Contents 1 Background 1 2 Introduction 1 3 Project Design 1 4 Language Prototype Features 2 4.1 Language Features........................................ 2 4.2 Code Example.......................................... 3 4.2.1 General Demonstration with Pattern Matching.................... 3 4.2.2 Conditionals....................................... 3 4.2.3 Looping.......................................... 3 4.2.4 Input and Output.................................... 4 4.2.5 Strings........................................... 4 4.2.6 Functions......................................... 4 4.2.7 Tuples........................................... 5 4.2.8 Higher Order Functions................................. 5 4.3 Language Reserved Words.................................... 6 5 Reference 7 5.1 Scala programming language.................................. 7 5.2 Scala programming language development........................... 7 5.3 Compile Scala to LLVM..................................... 7 5.4 Benchmarking........................................... 8 1 Background Scala is heavily used in daily production among various industries. Being as a general purpose programming language, it is influenced by many ancestors including, Erlang, Haskell, Java, Lisp, OCaml, Scheme, and Smalltalk. Scala has many attractive features, such as cross-platform taking advantage of JVM; as well as with higher level abstraction agnostic to the developer providing immutability with persistent data structures, pattern matching, type inference, higher order functions, lazy evaluation and many other functional programming features.. Scala is truly good at breaking down complex large scale projects into manageable small solutions that work together in a functional fashion. LLVM as a very powerful compiler infrastructure providing many advantages in compiler optimization and interfacing with many languages to ease the compiler backend writing magnificently. 2 Introduction Scala is a prototype towards to be a full-fledged production-ready functional programming language with full support of current version of Scala. It will have fast startup time and potentially be able to leverage LLVM optimization/analyse. The prototype compiler will translate source code with a subset of Scala syntax to LLVM IR, The intermediate representation from frontend of the compiler will be implemented in OCaml, machine code from LLVM eventually running on all LLVM-supported architectures. The frontend means the beginning part of the compiler, which consists of lexical analyzer, tokenizer, abstract syntax tree generator, semantic checker, the end product (intermediate representation) of this compiler frontend will be the input of the subsequent backend compiler, LLVM language binding will be used to create this part. So taking advantage of LLVM, the final output from this compiler will be low level machine code-generated executables; i.e., writing clean and concise, easy to maintain Scala code with functional programming features, while achieving performance approaching to assembly languages. 1 of 8

3 Project Design Compiler LLVM-IR Support x86 and ARM Support a subset of Scala features 1 Benchmarking Testing code Basic algorithms written in Scala, or the targeting Scala-like language Algorithm testing code counterpart in OCaml and/or other programming languages Testing compiler For comparison purposes, to see the differences between the project and existing compilers and interpreters target compiler (LLVM) gcc/g++ Sun Java Development Kit (JDK) Perl Python Benchmark utilities 4 Language Prototype Features The LLVM-Scala Prototype will be a succinct, statically typed, functional programming, JVM-free language with relatively sophisticated type system. The Prototype will share the same file suffix.scala as the Scala programming language. Only a small subset of Scala features will be supported in this Prototype, which were documented in this Proposal; all others that were not mentioned in this Proposal will not be supported or be supported in a very limited way. From the language processing mode point of view, only compilation will be supported in the Prototype; there will be no support for REPL or scripting. Object-oriented feature will be omitted. For instance, there will be no trait, or class. Another example was supporting import in a limited way, in that only supported library or Prototype-compatible code would be allowed to imported into a Prototype code and there is no definition of package. Further more, there will be no support for named arguments, variable number of arguments, companion objects, object comparison, nested functions, nested classes, Actors, file I/O, or exception handling, The undecidable supporting features will be documented in future report when necessary, for instance, the range difference between the Prototype and Scala for Int values. 4.1 Language Features Typically, the supported features include the following: Comment: //, and /* */ Semicolon import Literals List Boolean: true, and false Int Character String 1 Details mentioned in the section of Language Prototype Features 2 of 8

Tuple Mathematical operation +,,, /, % Shorthand notations: + =, +, =, / =, % = Basic functions: min, max, abs, ceil, floor, round, pow, log, log10, sqrt Conditionals: Conditional operators: ==,! =, <, <=, >, >= Logical operators: &&,,! if, else Loop: for-loop, to, until, while-loop, do-while-loop Pattern matching Input/output: string interpolation, readline, print, println, printf Variable definition and type inference Method declaration and definition Higher order functions class, extends, override 4.2 Code Example 4.2.1 General Demonstration with Pattern Matching The Prototype language will be a subset of the native Scala, hence the compatible code is essentially Scala with confined features. o b j e c t HelloWorld { def main ( args : Array [ S t r i n g ] ) { p r i n t l n ( Hello, world! ) ; HelloWorld. main ( args ) Listing 1: Hello World! Listing 2: Functional programming paradigm // Pattern matching, anonymous f u n c t i o n s, p a r t i t i o n, t u p l e decomposition def quicksort ( a : L i s t [ Double ] ) : L i s t [ Double ] = a match { case Nil => Nil case x : : xs => val ( l t, gt ) = xs. p a r t i t i o n ( < x ) quicksort ( l t ) ++ L i s t ( x ) ++ quicksort ( gt ) p r i n t l n ( quicksort ( L i s t ( 2, 3, 1, 3. 1, 3, 3. 1 4 1 5 9 2 6, 1 4, 3. 1 4 1 5 ) ) ) 4.2.2 Conditionals Listing 3: If-Else statement i f ( ( age >= 5) && ( age <= 6 ) ) { p r i n t l n ( Go to Kindergarten ) e l s e i f ( ( age > 6) && ( age <= 7 ) ) { p r i n t l n ( Go to Grade 1 ) e l s e { p r i n t l n ( Go to Grade + ( age 5 ) ) 3 of 8

4.2.3 Looping The following looping example shows the basic supported feature in the Prototype language. var i = 0 ; while ( i <= 5) { p r i n t l n ( i ) i += 1 Listing 4: Looping do { p r i n t l n ( i ) i += 1 while ( i <= 9) f o r ( i < 1 to 10) { p r i n t l n ( i ) However, the for-looping will not support the following semicolon-separated conditional: f o r ( i < 1 to 5 ; j < 6 to 10) { p r i n t l n ( i : + i ) p r i n t l n ( j : + j ) Listing 5: NO support Listing 6: until keywords in looping val r a n d L e t t e r s = ABCDEFGHIJKLMNOPQRSTUVWXYZ f o r ( i < 0 u n t i l r a n d L e t t e r s. length ) { p r i n t l n ( r a n d L e t t e r s ( i ) ) val a L i s t = L i s t ( 1, 2, 3, 4, 5 ) f o r ( i < a L i s t ) { p r i n t l n ( L i s t i i t e m + i ) Listing 7: Iterate through a List 4.2.4 Input and Output Listing 8: STDOUT with string interpolation val name = Derek val age = 39 val weight = 175.5 p r i n t l n ( s H e l l o $name ) p r i n t l n ( f I am ${ age + 1 and weight $weight %.2 f ) The following printf example showed limited support of native Scala: Listing 9: STDOUT with string interpolation p r i n t f ( %d %s %f %c, 1, s t r i n g, 3. 1, c ) // Supported p r i n t f ( %5d \ n,5) // Right j u s t i f y, NOT supported p r i n t f ( % 5dHi \ n,5) // L e f t j u s t i f y, NOT supported p r i n t f ( %05d\n, 5) // Zero f i l l, NOT supported p r i n t f ( %.5 f \ n, 3. 1 4 ) // Five decimal minimum & maximum, NOT supported p r i n t f ( % 5s \ n, Hi ) // L e f t j u s t i f y String, NOT supported 4 of 8

4.2.5 Strings Listing 10: Available methods in String object var randstring = I saw a dragon f l y by p r i n t l n ( S t r i n g : + randstring ) p r i n t l n ( 3 rd index value : + randstring ( 3 ) ) p r i n t l n ( S t r i n g l e n g t h : + randstring. l ength ( ) ) p r i n t l n ( Concatenate : + randstring. concat ( and explode ) ) p r i n t l n ( Compare s t r i n g s f o r e q u a l i t y ; are s t r i n g s equal + I saw a dragon. e q u a l s ( ran p r i n t l n ( Get index o f a match ; dragon s t a r t s at index :, randstring. indexof ( dragon ) ) p r i n t l n ( Dragon at : + twodragonstring. indexof ( dragon ) ) // Convert a s t r i n g i n t o an array val randarray = randstring. toarray f o r ( v < randarray ) p r i n t l n ( v ) 4.2.6 Functions The function definition takes the following format: Listing 11: Function definition d e f funcname ( param1 : datatype, param2 : datatype ) : returntype = { f u n c t i o n body return valuetoreturn // Give parameters d e f a u l t v a l u e s def getsum (num1 : Int = 1, num2 : Int = 2) : Int = { return num1 + num2 p r i n t l n ( Default sum : + getsum ( ) ) p r i n t l n ( 5 + 4 = + getsum ( 5, 4 ) ) // Recursion f u n c t i o n def f a c t o r i a l (num : BigInt ) : BigInt = { i f (num == 1) 1 e l s e num f a c t o r i a l (num 1) p r i n t l n ( F a c t o r i a l 2 0 : + f a c t o r i a l ( 2 0 ) ) Named arguments and variable number of arguments are not supported. Listing 12: NO support p r i n t l n ( 5 + 4 = + getsum (num2 = 5, num1 = 3 ) ) def getsum2 ( args : Int ) : Int = { var sum : Int = 0 f o r (num < args ) { sum += num sum p r i n t l n ( getsum2 : + getsum2 ( 1, 2, 3, 4 ) ) 4.2.7 Tuples 5 of 8

Listing 13: Supported Tuple operations // Tuples can hold v a l u e s o f many types, but they are immutable var tuplemarge = (103, Marge Simpson, 1 0. 2 5 ) p r i n t f ( %s owes us $%.2 f \n, tuplemarge. 2, tuplemarge. 3 ) // I t e r a t e through a t u p l e tuplemarge. p r o d u c t I t e r a t o r. f o r e a c h { i => p r i n t l n ( i ) // Convert Tuple to S t r i n g p r i n t l n ( tuplemarge. t o S t r i n g ( ) ) 4.2.8 Higher Order Functions Listing 14: Example of supported higher order functions // Functions can be passed l i k e any other v a r i a b l e // i s r e q u i r e d a f t e r the f u n c t i o n to s t a t e the meant f u n c t i o n val log10func = log10 p r i n t l n ( Log10 i s : + log10func ( 1 0 0 0 ) ) // Apply a f u n c t i o n to a l l items o f a l i s t with map L i s t ( 1 0 0 0. 0, 1 0 0 0 0. 0 ).map( log10func ). f o r e a c h ( p r i n t l n ) // Use an anonymous f u n c t i o n with map ; r e c e i v e s an Int x and m u l t i p l i e s everyone by 50 L i s t ( 1, 2, 3 ). map( ( x : Int ) => x 5 0 ). f o r e a c h ( p r i n t l n ) // i. e., L i s t ( 1, 2, 3 ). map( 5 0 ). f o r e a c h ( p r i n t l n ) // F i l t e r p a s s e s only those v a l u e s that meet a c o n d i t i o n L i s t ( 1, 2, 3, 4 ). f i l t e r ( % 2 == 0 ). f o r e a c h ( p r i n t l n ) // Pass d i f f e r e n t f u n c t i o n s to a f u n c t i o n def times3 (num : Int ) = num 3 def times4 (num : Int ) = num 4 // Define the f u n c t i o n parameter type and return type def multit ( func : ( Int ) => Double, num : Int ) = { func (num) p r i n t f ( 3 100 = %.1 f \n, multit ( times3, 100)) p r i n t f ( 4 100 = %.1 f \n, multit ( times4, 100)) Closure will not be supported. For instance, the following code will not work in the Prototype: Listing 15: NO support for closure val d i v i s o r V a l = 5 val d i v i s o r 5 = (num : Double ) => num / d i v i s o r V a l p r i n t l n ( 5 / 5 = + d i v i s o r 5 ( 5. 0 ) ) 4.3 Language Reserved Words case starts for matched expression class starts a class declaration def starts a method declaration do starts a do-while loop else starts else clause for an if clause in case the evaluation of if expression is false extends indicates inheritance via parent class extension to derive child class(es) false boolean value for starts for loop if starts if expression import import members into current scope match starts pattern matching expression 6 of 8

new creates a new instance of a class null represents a null value object instantiated class object override indicates an update for a definition of original member of class return indicates termination from a function call and pass the value on to call stack, if there is any super refers an object s parent this represents object itself to used in loop comprehensions true boolean value type starts a type declaration val represents read-only value variable var represents modifiable variable until used in loop comprehensions while starts a while loop or while block of a do-while loop yield generate results from a loop represents function literal : separator between identifier and type annotation = assignment <- generator expression in comprehensions => separator between argument list and function body in function literals ; optional separator between expression and statement, separator between expression or literals! logical negation & logical AND logical OR && conditional AND conditional OR ==, <, <=, >, >=,!= comparison (,),{, scope confinement symbols for expressions, statements, function blocks; or position access for List, Array etc //, /*, */ comments 5 Reference 5.1 Scala programming language 1. Martin Odersky, The Scala Language Specification, Programming Methods Laboratory, 2014 2. http://www.scala-lang.org/files/archive/spec/2.11/ 3. http://docs.scala-lang.org/cheatsheets/ 7 of 8

5.2 Scala programming language development 1. https://wiki.scala-lang.org/display/siw/compiler+walk-through 2. http://www.scala-lang.org/old/node/215.html 3. http://www.scala-lang.org/contribute/hacker-guide.html 4. https://github.com/lampepfl/dotty.git 5.3 Compile Scala to LLVM 1. http://vmkit.llvm.org/ 2. https://github.com/scala-native/scala-native 3. https://github.com/greedy/scala 4. https://code.google.com/archive/p/slem/ 5.4 Benchmarking 1. http://benchmarksgame.alioth.debian.org/ 8 of 8