Canterbury Christ Church University

Compilers

Course Code: U19952

Course Name: Fundamentals of Computer Systems

Credits: 20

Module Leader: Ali Jaddoa
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Intended Learning Outcomes

  1. Describe the process how high and level programming software translate to the hardware via low level language(s).​

  2. Recall the steps of the compile

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Types of Programming Languages

  • High Level / low level – C#, Java, Python, Ruby, C, C++, assembly ​

  • Declarative / imperative/procedural – SQL, Curl, Prolog​

  • General-purpose/domain specific – HTML, Markdown/up, MATLAB​

  • Object-orientated/concurrent – C#, Java, Python,​

  • Command/complied/script language – batch, bash, Javascript​

  • Answer set - Prolog

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Some Examples of Non-English Programming Languages

Linotte​
It has been a developer for using French keywords, and its “Hello world” program looks like this:​

BonjourLeMonde:​
  début​
    affiche "Bonjour le monde!"​

------------------------

HelloWorld:​
  beginning
    poster "Hello world!

Has a web engine for HTML and PHP and JSP.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

SAKO​
System Automatycznego Kodowania Operacji (Automatic Operation Encoding System) programming language, which uses polish as for its keywords:​

LINIA​
  TEKST:​
  HELLO WORLD​
KONIEC​

---------------------------------

LINE
  TEXT:
  HELLO WORLD
END

Really only used in the late 1950s and early 1960s for the XYZ computers.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Rapira​

Rapira is another awesome example of non-english programming languages. It uses Russian keywords:​

ПРОЦ СТАРТ()​

    ВЫВОД: 'Привет, мир!'​

КОН ПРОЦ​

-------------------

proc start()
     output: 'Hello, world!!!';
end proc
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

EPL

易语言 (Easy Programming Language, as known as EPL):​

公开 类 启动类​
{​
  公开 静态 启动()​
  {​
    控制台.输出("你好,世界!");​
  }​
}
​
-----------------------------------------

public class startup class​
{
  public static start()
  {
    console.output("Hello, World!");
  }
}
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Compiler

A compiler is a program that processes source code written in a programming language.

  • Program Processing: A compiler serves as a crucial tool in handling programs written in various programming languages.

  • Program Generation: It functions as a program generator, capable of producing executable programs in a specified language.

  • Language Translation: The compiler translates programs written in one language into equivalent programs in another language.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

A tool to enable you to program at a higher level , by mapping high level concepts to low level implementation

  • Increased Productivity: Allows for faster and more efficient development by focusing on the logic and design rather than intricate details.

  • Enhanced Readability: Code becomes more readable and understandable, facilitating collaboration and maintenance.

  • Code Portability: Encourages code portability by minimizing dependencies on specific hardware or architecture

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
  • Translates from one language into another

  • Output a low level program which behaves as specified by
    the input, higher level program.

  • Mediate between higher level human concepts, and the
    word by word data manipulation which the machine performs.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Compliler and Interpreter

Compiler? Compiler translates code from a high-level programming language (like Python, JavaScript or Go) into machine code **before the program runs**.
Interpreter? Interpreter translates code written in a high-level programming language into machine code **line-by-line as the code runs**.
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

**Command: gcc -S -O test.c **

(the flag S tells the compiler to produce assembly code, O turns optimisation on).

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Assembly code

Assembly code is a low-level programming language that serves as an interface between high-level programming languages and the computer's hardware.

  • Human-Readable Machine Code: Assembly code is a human-readable representation of machine code, making it more understandable than binary machine code.

  • Close to Hardware: Unlike high-level languages, assembly code provides a direct correspondence to the architecture and operations of the underlying hardware.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
  • Symbolic Representation: Uses mnemonics and symbols to represent machine instructions, making it more comprehensible than raw machine code.
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Example:

  • The .data section declares a null-terminated string "Hello, Assembly!".
  • The .text section contains the program logic.
  • The _start label marks the entry point of the program.
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Compiler Stages

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Lexical Analysis

  • The compiler begins converting the series of characters into tokens​

High Level Code​

int n = 11;​
float q = 1.618;​
if (n < 12)
{​ 
  return q;​
}​
else​
{
  return n;
​}​
Token name​ Example token values​
identifier n, q​
keyword​ int, float, if, else, return, while
separator​ { }, ( ), [ ], ;​
operator​ +,- *, / , = ,< , >, : , ?​
literal​ True, false, 6.02e23, “string”​
comment​ // this is a comment /this is another comment/​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Syntax Analysis

Syntax analysis is based on the rules based on the specific programming language by constructing the parse tree with the help of tokens. ​

- Interior node: record with an operator field and two fields for children​
  • Leaf: records with 2/more fields; one for token and other information about the token​

  • Ensure that the components of the program fit together meaningfully​

  • Gathers type information and checks for type compatibility​

  • Checks operands are permitted by the source language​

center

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Semantic Analyser

Semantic Analyser will check for Type mismatches, incompatible operands, a function called with improper arguments, an undeclared variable, etc.​

int n = 11;​
float q = 1.618*n;​

In the above code, the semantic analyser will typecast the int n 11 to float 11.0 before multiplication​.

Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Intermediate Code Generation

Removes unnecessary code lines​.

Arranges the sequence of statements to speed up the execution of the program without wasting resources. ​

Consider the following code, how can we remove unnecessary code? ​

a = int_to_float(10)​
b = c * a​
d = e + b​
f = d​
Can become
b = c * 10.0​
f = e+b​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Code Generation

The objective of this phase is to allocate memory locations, storage and generate relocatable machine code or machine instructions.​​

The code generated by this phase is executed to take inputs and generate expected outputs, therefore, checks for unreachable statements.​
Consider the following code, what error would be generated at this stage?​

while (p == 10)​
{ ​
  break;​
  int q = (0.5*8)*p;​
}​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Code Generation

Now we are going to see how we go from C to Assembly to machine code…​

int square(int num) {​
    return num * num;​
}
square:​
  pushq  %rbp​
  movq   %rsp, %rbp​
  movl   %edi, -4(%rbp)​
  movl   -4(%rbp), %eax​
  imull  %eax, %eax​
  popq   %rbp​
  ret​
Memory Addresses
rbp[3]​ 0x0007556ff0e0
rbp[2]​ 0x0007556ff0df​
rbp[1]​ 0x0007556ff0de​
rbp[0]​ 0x0007556ff0dd​
0x0007556ff0dc
0x0007556ff0db​
0x0007556ff0da​
num 0x0007556ff0d9​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University
int square(int num) {​
    return num * num;​
}
square:​
  pushq  %rbp​
  movq   %rsp, %rbp​
  movl   %edi, -4(%rbp)​
  movl   -4(%rbp), %eax​
  imull  %eax, %eax​
  popq   %rbp​
  ret​
HEX
55           0101010148 89 e5​     01001000 10001001 1110010189 7d fc     10001001 01111101 111111000f af c0​     00001111 10011111 1100000054           01010100 ​
C3           11000011
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Error Handling Routine

During compilation process error(s) may occur in all the below-given phases:​

  • Lexical analyser: Wrongly spelled tokens​
  • Syntax analyser: Missing parenthesis​
  • Semantic analyser: Mismatched data types, missing arguments​
  • Intermediate code generator: Mismatched operands for an operator​
  • Code Optimizer: When the statement is not reachable​
  • Code Generator: Unreachable statements​
  • Symbol tables: Error of multiple declared identifiers​
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Menti (What have you leant today?)

  • Scan this
  • Or go to Menti.com and using this code (1613 4215)
Fundamental of Computer Systems: U19952-2023-2024
Canterbury Christ Church University

Labs

  • Start your lab from here where you are going experience compilation of languages C and Python [Optional], and see how the code compiles and the subsequent outputs!​

  • We will be using Godbolt/Compliler Explorer Simulator

Fundamental of Computer Systems: U19952-2023-2024

## Menti - Go to Menti.com and use this code **67 22 75 35**; OR - Scan this ![bg right 90%](../../figures/Week_3_Qr.png) ---

--- ## Symbol Management Table A symbol table contains a record for each identifier with fields for the attributes of the identifier. <p align=center> |Operation|Function| |---|---| |allocate| to allocate a new empty symbol table| |free| to remove all entries and free storage of symbol table| |lookup| to search for a name and return a pointer to its entry| |insert| to insert a name in a symbol table and return a pointer to its entry| |set_attribute| to associate an atrribute to a given entry| |get_attribute| to get an attribute associated with a given entry| </p>