Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014.

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
Introduction To Compilers And Phase 1 Inside a compiler. Inside a C-- compiler. The compilation process. Example C-- code. Extended Backus-Naur.
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
WPSM Programming Language A simple language that transform simple data structure into complex xML format Wai Y. Wong Peter Chen Seema Gupta Miqdad Mohammed.
Chapter3: Language Translation issues
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
COP4020 Programming Languages
Chapter 1. Introduction.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CSC 338: Compiler design and implementation
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Joe Hummel, the compiler is at your service Chicago Code Camp 2014.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 1, 08/28/03 Prof. Roy Levow.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CSC 8505 Compiler Construction
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Barbara Doyle Jacksonville University What’s New with Visual Studio and C#?
C# 5.0 Alex Davies 22 nd December What we will cover C# 5.0,.NET 4.5, Visual Studio 11 Caller Info Attributes Upgrade from synchronous to asynchronous.
Joe Hummel, the compiler is at your service Chicago Coder Conference, June 2016.
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Open Source Compiler Construction (for the JVM)
Lecture 9 Symbol Table and Attributed Grammars
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
A Simple Syntax-Directed Translator
Introduction to Parsing (adapted from CS 164 at Berkeley)
-by Nisarg Vasavada (Compiled*)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Basic Program Analysis: AST
C# Today and Tomorrow Mads Torgersen,
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
System Programming and administration
Compiler design.
Microsoft Ignite NZ October 2016 SKYCITY, Auckland.
Presentation transcript:

Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014

 Joe Hummel, PhD  Professor:U. of Illinois, Chicago  Consultant:Joe Hummel, Inc.  Trainer:Pluralsight  Microsoft MVP C++  Chicago-based, one daughter adopted from China (now 12!)  Avid sailor SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn

 What is Project Roslyn?  The ".NET Compiler Platform"  Replacement of existing.NET compilers with new ones  csc for C#  vbc for VB.NET SDC Meetup Sept Project Roslyn csc

 Why is this a big deal?  Risky  if they get this wrong, folks can't build their apps  if they get this wrong, MSFT can't build their apps SDC Meetup Sept Project Roslyn

 What's the benefit?  Faster turnaround on new features  inside and outside MSFT  Grow the Visual Studio ecosystem  MUCH easier to build new tools  MUCH easier to extend Visual Studio, C# and VB  MUCH easier to try out new ideas SDC Meetup Sept Project Roslyn

 Status  preview release  open source!  SDC Meetup Sept Project Roslyn

 Open source?  Yes, open source!  Apache license 2.0  You are free to GIT, fork, modify, rebuild, deploy  Anders did this on Build 2014 SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn

 C# and VB compilers were black boxes  predefined switches only way to interact… SDC Meetup Sept Project Roslyn csc > csc.exe main.cs /o /warn:4

SDC Meetup Sept Project Roslyn

 The compilers are now white boxes  You can:  obtain information about a program  modify a program syntactically / semantically  impact the compilation process  change the compiler itself! 12 SDC Meetup Sept 2014 Project Roslyn csc Roslyn

13 SDC Meetup Sept 2014 Project Roslyn csc "Call me every time you see an identifier…" (because I'm renaming all global variables) "Emit this code instead…" (I'm targeting specific HW) // translate resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); // translate resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); Roslyn

 What can we do with this capability?  Infinite possibilities:  better tools — refactoring, analysis, …  better enforcement of coding standards  add scripting support to your app  target new platforms  language research — DSLs, …  compiler research …… SDC Meetup Sept Project Roslyn ? ?

SDC Meetup Sept Project Roslyn

 Front-end vs. Back-end  Front-end deals with syntax ― "grammar"  Back-end deals with semantics ― "meaning" SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn Source language Parsing Assembly language Lexical Analysis Compiler Semantic Analysis High-level Optimizer Code Gen Low-level Optimizer tokens IRIR'IR'' IR''' // comment if (x>100) x = 100; // comment if (x>100) x = 100; if, (, x, >, 100, ), x, =, … syntax errors semantic errors

 Roslyn Intermediate Representation (IR)  Abstract Syntax Tree (AST)  Symbol Table SDC Meetup Sept Project Roslyn + GCD program 0"int", type, … 1"void", type, … 2… 3"getint", funct, type: 0, … 4"putint", funct, type: 1, … 5"i", var, type: 0, … 6"j", var, type: 0, … ……

 How to learn Roslyn AST?  Use the Roslyn Syntax Visualizer!  Open a project  Open a source file  View menu… >> Other Windows >> Roslyn Syntax Visualizer SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn

 Roslyn is BIG  There are many APIs…  There is the source code itself… SDC Meetup Sept Project Roslyn +

 Start small  Let’s create a simple diagnostic that warns about empty catch blocks… SDC Meetup Sept Project Roslyn

 Step 1:  Create new project… >> Roslyn >> Diagnostic with Code Fix  Name >> EmptyCatchDiagnostic SDC Meetup Sept Project Roslyn

 Step 2:  Create Syntax Node Analyzer to detect empty catches SDC Meetup Sept Project Roslyn public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer {. public ImmutableArray SyntaxKindsOfInterest { get { return ImmutableArray.Create( SyntaxKind.CatchClause ); } } // only called for things of interest: public void AnalyzeNode(SyntaxNode node,...) { var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: addDiagnostic(diagnostic); // display: } public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer {. public ImmutableArray SyntaxKindsOfInterest { get { return ImmutableArray.Create( SyntaxKind.CatchClause ); } } // only called for things of interest: public void AnalyzeNode(SyntaxNode node,...) { var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: addDiagnostic(diagnostic); // display: }

 Step 3:  Create Code Fix Provider to optionally correct problem… SDC Meetup Sept Project Roslyn internal class CodeFixProvider : ICodeFixProvider {. // only called for things of interest: public async Task GetFixesAsync(Document document,...) { var root = await document.GetSyntaxRootAsync(cancellationToken); var token = root.FindToken(span.Start); // catch keyword: if (!token.IsKind(SyntaxKind.CatchKeyword)) // sanity check: return null; var catchBlock = (CatchClauseSyntax)token.Parent; var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); return new[] { CodeAction.Create("throw", document.WithSyntaxRoot(newRoot)) }; } internal class CodeFixProvider : ICodeFixProvider {. // only called for things of interest: public async Task GetFixesAsync(Document document,...) { var root = await document.GetSyntaxRootAsync(cancellationToken); var token = root.FindToken(span.Start); // catch keyword: if (!token.IsKind(SyntaxKind.CatchKeyword)) // sanity check: return null; var catchBlock = (CatchClauseSyntax)token.Parent; var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); return new[] { CodeAction.Create("throw", document.WithSyntaxRoot(newRoot)) }; }

 Step 4:  Run!  A.vsix installer is built  A new instance of VS is started  The.vsix is installed  Open a project and test… SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn

 Install Roslyn preview on top of VS 2013…  That supports seems to be gone  Roslyn preview ships as part of VS “14” CTP 3  So just install VS 14, or  Run pre-built VS 14 VM available in Azure VM gallery  See for more detailshttp://roslyn.codeplex.com/ SDC Meetup Sept Project Roslyn

SDC Meetup Sept Project Roslyn

 Thank you for attending!  Joe Hummel, PhD   Materials:  For more information on Roslyn:  Docs / FAQ:   Build 2014 on Channel 9  The Future of C#  SDC Meetup Sept Project Roslyn