03_Is Javascript is compiled or interpreted ?

Assembly Code: Programs that are coded in an assembly language such as Intel’s x86 assembly language with two types of syntaxes. 

ByteCodePrograms that are coded in some language that is mostly understood by some software. For example, Java emits ByteCode which is interpreted by Java Virtual Machine (JVM). 

Native Code or Machine Code: Programs that are coded in some language that is understood by some CPU or other hardware. Typically an assembly code is compiled into a machine code. This machine code can also be called native code or executable binary. CPU understands only the machine code and nothing else. Therefore, every other higher level language or code has to be compiled to machine code to be executed. 
          
         
                           COMPILED OR INTERPRETED?? 
We can conclude saying that Javascript is an interpreted language. But Javascript is already advancing very fast and some of the modern browsers actually compile Javascript for better performance. V8 engine is one of that. The V8 Engine which is built by Google is open source and written in C++. This engine is used inside Google Chrome. Unlike the rest of the engines, however, V8 is also used for the popular Node.js runtime. 
V8 was first designed to increase the performance of JavaScript execution inside web browsers. In order to obtain speed, V8 translates JavaScript code into more efficient machine code instead of using an interpreter. It compiles JavaScript code into machine code at execution by implementing a JIT (Just-In-Time) compiler like a lot of modern JavaScript engines do such as SpiderMonkey or Rhino (Mozilla). The main difference here is that V8 doesn’t produce byte code or any intermediate code.



  Compiler : 
A compiler is a program that reads a program written in the high-level language and converts it into the machine or low-level language and reports the errors present in the program. It converts the entire source code in one go or could take multiple passes to do so, but at last, the user gets the compiled code which is ready to execute. 
 
Compiler operates on phases; various stages can be grouped into two parts that are: 
  • Analysis Phase of the compiler is also referred to as the front end in which program is divided into fundamental constituent parts and checks grammar, semantic and syntax of the code after which intermediate code is generated. Analysis phase includes lexical analyzer, semantic analyzer and syntax analyzer. 
  • Synthesis phase of the compiler is also known as the back end in which intermediate code is optimized, and the target code is generated. Synthesis phase includes code optimizer and code generator. 

                         Phases of Compiler 

  1. Lexical AnalyzerLexical Analysis is the first phase when compiler scans the source code. This process can be left to right, character by character, and group these characters into tokens.
  2. Here, the character stream from the source program is grouped in meaningful sequences by identifying the tokens. It makes the entry of the corresponding tickets into the symbol table and passes that token to next phase.

  1. Syntax AnalyzerHere, is a list of tasks performed in this phase:
    • Obtain tokens from the lexical analyzer
    • Checks if the expression is syntactically correct or not
    • Report all syntax errors
    • Construct a hierarchical structure which is known as a parse tree.

    Any identifier/number is an expression

    If x is an identifier and y+10 is an expression, then x= y+10 is a statement.

    Consider parse tree for the following example
    (a+b)*c
    
  1. Semantic AnalyzerSemantic analysis checks the semantic consistency of the code. It uses the syntax tree of the previous phase along with the symbol table to verify that the given source code is semantically consistent. It also checks whether the code is conveying an appropriate meaning.
    Semantic Analyzer will check for Type mismatches, incompatible operands, a function called with improper arguments, an undeclared variable, etc
  1. Intermediate code generatorOnce the semantic analysis phase is over the compiler, generates intermediate code for the target machine. It represents a program for some abstract machine.
    Intermediate code is between the high-level and machine level language. This intermediate code needs to be generated in such a manner that makes it easy to translate it into the target machine code.
  1. Code OptimizerThe next phase of is code optimization or Intermediate code. This phase removes unnecessary code line and arranges the sequence of statements to speed up the execution of the program without wasting resources. The main goal of this phase is to improve on the intermediate code to generate a code that runs faster and occupies less space.
  1. Code generator: This is the final phase of the compiler in which target code for a particular machine is generated. It performs operations like memory management, Register assignment, and machine-specific optimization. 
phases of compiler 
The symbol table is somewhat a data structure which manages the identifiers along with the relevant type of data it is storing. Error Handler detect, report, correct the errors encountering in between the different phases of a compiler. 
Definition of Interpreter 
The interpreter is an alternative for implementing a programming language and does the same work as a compiler. Interpreter performs lexingparsing and type checking similar to a compiler. But interpreter processes syntax tree directly to access expressions and execute statement rather than generating code from the syntax tree. 


An interpreter may require processing the same syntax tree more than once that is the reason why interpretation is comparatively slower than executing the compiled program. 
Compilation and interpretation probably combined to implement a programming language. In which a compiler generates intermediate-level code then the code is interpreted rather than compiled to machine code. 
Employing an interpreter is advantageous during program development, where the most important part is to be able to test a program modification rapidly rather than run the program efficiently. 
      
              Compiler vs Interpreter 

  •  The compiler takes a program as a whole and translates it, but interpreter translates a program statement by statement. 
  • Intermediate code or target code is generated in case of a compiler. As against interpreter doesn’t create intermediate code. 
  • A compiler is comparatively faster than Interpreter as the compiler take the whole program at one go whereas interpreters compile each line of code after the other. 
  • The compiler requires more memory than interpreter because of the generation of object code. 
  • Compiler presents all errors concurrently, and it’s difficult to detect the errors in contrast interpreter display errors of each statement one by one, and it’s easier to detect errors. 
  • In compiler when an error occurs in the program, it stops its translation and after removing error whole program is translated again. On the contrary, when an error takes place in the interpreter, it prevents its translation and after removing the error, translation resumes. 
  • In a compiler, the process requires two steps in which firstly source code is translated to target program then executed. While in Interpreter It’s a one-step process in which Source code is compiled and executed at the same time. 
  • The compiler is used in programming languages like C, C++, C#, Scala, etc. On the other Interpreter is employed in languages like PHP, Ruby, Python, etc. 
 lets discover something you should know as developer.

Dynamic memory allocation

Is memory allocated at runtime using calloc()malloc() and friends. It is sometimes also referred to as ‘heap’ memory, although it has nothing to do with the heap data-structure ref.
int * a = malloc(sizeof(int));
Heap memory is persistent until free() is called. In other words, you control the lifetime of the variable.

Automatic memory allocation:


"A stack is a special area of computer's memory which stores temporary variables created by a function. In stack, variables are declared, stored and initialized during runtime.
It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. The stack section mostly contains methods, local variable, and reference variables."

"occurs for (non-static) variables defined inside functions, and is usually stored on the stack (though the C standard doesn't mandate that a stack is used)".
"Static variables are shared among all instances of a class. Non static variables are specific to that instance of a class. Static variable is like a global variable and is available to all methods. Non static variable is like a local variable and they can be accessed through only instance of a class."
This is what is commonly known as ‘stack’ memory, and is allocated when you enter a new scope (usually when a new function is pushed on the call stack). Once you move out of the scope, the values of automatic memory addresses are undefined, and it is an error to access them.
int a = 43;
Note that scope does not necessarily mean function. Scopes can nest within a function, and the variable will be in-scope only within the block in which it was declared. Note also that where this memory is allocated is not specified. (On a sane system it will be on the stack, or registers for optimisation)

Static memory allocation

Is allocated at compile time, and the lifetime of a variable in static memory is the lifetime of the program.
In C, static memory can be allocated using the static keyword. The scope is the compilation unit only.
Things get more interesting when the extern keyword is considered. When an extern variable is defined the compiler allocates memory for it. When an extern variable is declared, the compiler requires that the variable be defined elsewhere. Failure to declare/define extern variables will cause linking problems, while failure to declare/define static variables will cause compilation problems.
in file scope, the static keyword is optional (outside of a function):
int a = 32;
But not in function scope (inside of a function):
static int a = 32;
Technically, extern and static are two separate classes of variables in C.
extern int a; /* Declaration */
int a; /* Definition */

"the stack and the heap are two memory locations. Value Types (primitive data like bool or integers) are stored directly in the stack. On the other hand Reference Types (objects) only have their memory address stored in the stack, their actual location is in the heap. "

When to use the Heap or stack?

  • You should use heap when you require to allocate a large block of memory. For example, you want to create a large size array or big structure to keep that variable around a long time then you should allocate it on the heap.
  • However, If you are working with relatively small variables that are only required until the function using them is alive. Then you need to use the stack, which is faster and easier.



Comments

Popular posts from this blog

JavaScript — Double Equals vs. Triple Equals - weird things on JavaScript

06_Understanding Execution Context and Execution Stack in Javascript

Creating and inserting element- part-2