Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

Similar documents
Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

CSc 453 Interpreters & Interpretation

SOSCON 2016 JerryScript

LECTURE 3. Compiler Phases

Compiler Design (40-414)

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

Dispatch techniques and closure representations

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

JerryScript An ultra-lightweight JavaScript engine for the Internet of Things

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

CS 406/534 Compiler Construction Putting It All Together

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

Principles of Compiler Design

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Playing with bird guts. Jonathan Worthington YAPC::EU::2007

Parsing Scheme (+ (* 2 3) 1) * 1

CS 6353 Compiler Construction Project Assignments

Intermediate Representations

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CPU Structure and Function

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

CSc 520 Principles of Programming Languages

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

CSc 553. Principles of Compilation. 2 : Interpreters. Department of Computer Science University of Arizona

Compiling Techniques

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Lecture08: Scope and Lexical Address

Compiler Code Generation COMP360

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSc 453. Compilers and Systems Software. 18 : Interpreters. Department of Computer Science University of Arizona. Kinds of Interpreters

Short Notes of CS201

Compilers. Compiler Construction Tutorial The Front-end

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS201 - Introduction to Programming Glossary By

Instruction Set Architecture

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

The role of semantic analysis in a compiler

Chapter 2: Instructions How we talk to the computer

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Introduction to Programming Using Java (98-388)

Intermediate Code Generation

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

William Stallings Computer Organization and Architecture. Chapter 11 CPU Structure and Function

Time : 1 Hour Max Marks : 30

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

Alternatives for semantic processing

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

COP4020 Programming Assignment 2 Spring 2011

Syntax-Directed Translation. Lecture 14

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

COP4020 Programming Assignment 2 - Fall 2016

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

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Database Languages and their Compilers

Lecture 4: Instruction Set Design/Pipelining

CS 6353 Compiler Construction Project Assignments

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Run-time Environments

Lecture 4: MIPS Instruction Set

Run-time Environments

Practical Malware Analysis

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

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Programming in Lua The Lua Implementation

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

CJT^jL rafting Cm ompiler

Python Implementation Strategies. Jeremy Hylton Python / Google

COMPILER CONSTRUCTION Seminar 02 TDDB44

UNIT- 5. Chapter 12 Processor Structure and Function

PL Revision overview

A programming language requires two major definitions A simple one pass compiler

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Crafting a Compiler with C (II) Compiler V. S. Interpreter

We can emit stack-machine-style code for expressions via recursion

Abstract Syntax Trees & Top-Down Parsing

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Introduction to Compiler Design

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Instruction Set Principles and Examples. Appendix B

Today's Topics. CISC 458 Winter J.R. Cordy

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Compiler Architecture

Lecture 4: Instruction Set Architecture

CS 415 Midterm Exam Spring SOLUTION

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Inside PHP Tom OSCON th July, 2012

Compilers and Interpreters

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Transcription:

1 Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM Sungkyunkwan University

Contents JerryScript Execution Flow JerryScript Parser Execution Flow Lexing Parsing Compact Bytecode (CBC) JerryScript VM(Virtual Machine) Virtual Machine Execution Flow vm_run(), vm_execute(), vm_loop() 2

JerryScript Parser: Execution Flow <jerry-main/main-unix.c> main() 3 Check arguments... Parse script Run Compiled Code (Virtual Machine) jerry_run() <jerry-core/jerry.c> jerry_parse() <jerry-core/parser/js/js-parser.c> parser_parse_script() parser_parse_source() lexer_next_token() parser_parse_statements() parser_parse_expression() parser_post_processing()

JerryScript Parser 4 Implemented as a recursive descent parser The parser converts the JavaScript source code directly into byte-code. Components Lexer Scanner Parser Statement Parser Expression Parser

JerryScript Parser: Components 5 Lexer Tokenize a source into the meaningful words Scanner Some tokens have multiple meanings dependent to following tokens, and scanner scans following tokens in order to specify current token e.g. for could be for-in statement or general for statement \ could be a start of regular expression or a division operator Predictive Top-down Parser Make a semantic from the statements to the possible terminals Possible terminals: tokens predictable by current token e.g. for of for-in statement is followed by ( in ) statement

Lexing (1/2) parser_parse_source() 6 lexer_next_token() 1. Read a word from current source_p 2. Identify the word as a certain token; identifier, number, string, or operators 3. Fill the token information in context_p (parser_context_t) as an output literal has ecape character ( \ )?

Lexing (2/2): String lexer_next_token() lexer_parse_string() 1. Calculate character size by character encoding type 2. Store current source_p to string_start_p 3. Move source_p to the end of string 4. Fill literal information. 7

Parsing (1/2) parser_parse_source() lexer_next_token() 8 parser_parse_statements() parser_parse_statements() 1. Parsing the tokens in a statement with a parser stack 2. Compile the statement to an opcode 3. Emit the opcodes except for last opcode Last opcode remain for morphing based on next token e.g. this.a =... 2 morphings happen this à CBC_PUSH_THIS this.a à CBC_PUSH_PROP_THIS_LITERAL this.a = à CBC_ASSIGN_PROP_THIS_LITERAL

Parsing (2/2) Parsing process Stack depth OPCODE Arguments Example Code 9

Literal Object in Parsing 10 When literal object is constructed during parsing, Parser puts the literal object into literal storage if not exist

Compact Bytecode (CBC) 11 CBC is a unique variable length byte code with lightweight data compression Currently 306 opcodes are defined Majority of the opcodes are variants of the same operation e.g. this.name is a frequent expression in JavaScript, so an opcode is defined to resolve this expression CBC_ASSIGN_PROP_THIS_LITERAL CBC_EXT_FOR_IN_GET_NEXT CBC_PUSH_NUMBER_0 CBC_PUSH_TRUE CBC_PUSH_FALSE

Virtual Machine Virtual machine executes an instruction stream in software Stack-based VMs Use a stack to store intermediate results, variables, etc. Operands are located on stack No need to specify location of operands No need to load operand locations Register-based VMs Use a limited set of registers for that purpose, lik e a real CPU Fewer VM instructions needed Each VM instruction is more expensive 12

Virtual Machine Input: a sequence of instructions Each instruction is identified by its opcode Dispatch Fetch opcode & jump to implementation Most expensive part of execution Fetch Operands Perform the Operation Often cheapest part of executions Input: Bytecode Dispatch Loop Fetch Opcode case: add. case: sub.. 13 Perform the Operation If (Type == string) Result = concat(l, R) Else if (Type == int) Result = L + R....

JerryScript VM 14 The interpreter is a combination of register and stack machines Stack is used for computing temporary values Registers are used for storing local variables The main loop is non-recursive to reduce stack usage Byte-code decompression Byte codes are decoded into a maximum of three atomic opcodes and these opcodes are executed

JerryScript VM: Execution Flow <jerry-main/main-unix.c> main() Check arguments... Parser 15 Parse script jerry_parse() <jerry-core/jerry.c> Virtual Machine Run Compiled Code (Virtual Machine) jerry_run() vm_run_global() <jerry-core/jerry.c> <jerry-core/vm/vm.c> Define Scope vm_run() Setting Frame vm_run_with_inline_stack() vm_execute() vm_run_with_alloca() Dispatch & Perform Operation vm_loop()

Define Scope 16 vm_run_global(): Global scope vm_run_eval() Direct call = Scope of Top Context (Inheritance) Indirect call: Global Scope ecma_op_function_call() Function Scope (Local Scope) Scope - ThisBinding Value - Lexical environment jerry_run() Opcode == Call vm_run_global() vm_run_eval () ecma_op_function_call() Bytecode, thisbinding value, lexical environment, argument (only function call) vm_run() <jerry-core/vm/vm.c>

Setting Frame 17 Setting Frame Frame: execution context of interpreter Bytecode pointer / register start pointer / stack top pointer / current lexical environment / this binding / literal list start pointer Call Stack Size Frame Operand Stack Register Frame Data Call stack size <= 16 vm_run() Call stack size > 16 vm_run_with_inline_stack() vm_run_with_alloc() 4*16 byte: Static Allocation vm_execute() 4*size byte: Dynamic Allocation

VM Execute Function 18 1. Set stack top, registers in frame 2. while(true) 1. Call vm_loop(): Dispatch Loop 2. Check return value 1. No more execute operation è Break 2. Invoke a function è opfunc_call() à à vm_run() 3. Constructor method è opfunc_construct() 3. Free registers vm_execute() vm_loop() : Dispatch Loop

VM Loop: Dispatch Loop 19 Outer loop for opcode s result handling Internal loop for byte code execution Fetch operands Dispatch Fetch opcode & jump to implementation Most expensive part of execution Perform the operation Often cheapest part of executions Dispatch Loop Fetch Opcode case: add. case: sub.. Perform the Operation If (Type == string) Result = concat(l, R) Else if (Type == int) Result = L + R....

Internal Loop in vm_loop() 20 Internal loop for byte code execution Get operand Setting Left value & right value Branch & Literal: bytecode Stack: Top stack of frame Literal Value of Register Or String Dispatch: Fetch opcode & jump to implementation

Example: Perform the Operation Push Push operand value to stack Continue to next bytecode (continue;) Assign Assigning result value Result handling (break) Add Type Checking & Add <int, int>, <int, float>, <float, int>, <float, float>, <string, string> Result handling (break) 21

Outer Loop in vm_loop() Outer loop for exception handling Flags for result handling IDENT Flag: Assign result to register or string STACK Flag By bytecodes» Copy result to stack Reference Flag Popping property and object from stack Setting Result (object.property = result) STACK Flag For branch