..

Introduction

[!question] Differentiate Compiler and Interpreter

Compiler Interpreter
Compiler is a program that translates program written in one programming language into another programming language Interpreter is a program that read a program and directly executes it without requiring the program to be compiled to machine code first
It doesn’t actually execute the program It executes the program to produce output
Example: C, C++ Example: Lisp, Python

[!question]- Comment on the nature of Java in terms of compiling and interpreting

  • Java is an hybrid language
  • Java is first compiled into JVM Byte Code
  • Then the JVM interprets the byte code

[!question]- What are the two objectives of Compiler Design

  • Should not change the meaning of the input program
  • Improve the program in some discernible way in terms of performance

[!question]- What are the types of output generated by a compiler?

  • Other high level language
  • Pure Machine Code
  • Augmented Machine Code
  • Virtual Machine Code

[!question]- What is Pure Machine code? Pure machine code is machine code that doesn’t assume the presence of any OS or runtime library It consists of only the instructions supported by the ISA

[!question]- What is Augmented Machine Code? Code with OS routine and runtime support routines

[!question]- What are the types of Machine Language that is generated by a compiler

  • Assembly
  • Relocatable Binary
  • Absolute Binary

[!question] Differentiate Relocatable binary and Absolute Binary

Relocatable Binary Absolute Binary
Uses relative addressing Absolute addresses
Needs linking Can be executed as it is

[!question]- Why is CD so challenging?

  • Abstraction
  • Multiple solutions
  • Can be formulated as a Discrete Optimization Problem, hence NP hard

Assignment

src

#include <limits.h>
#include <stdio.h>
#define POINT_ADD(p, q, r)                                                     \
  {                                                                            \
    r.x = p.x + q.x;                                                           \
    r.y = p.y + r.y;                                                           \
  }

struct point {
  int x;
  int y;
};

void point_add(struct point p, struct point q, struct point *r) {
  r->x = p.x + q.x;
  r->y = p.y + q.y;
}

static void s_point_add(struct point p, struct point q, struct point *r) {
  r->x = p.x + q.x;
  r->y = p.y + q.y;
}

int main(int argc, char *argv[]) {
  struct point p1, p2, p3;
  p1.x = 1;
  p1.y = 1;
  p2.x = 2;
  p2.y = 2;
  for (int i = 0; i < INT_MAX; i++) {
    point_add(p1, p2, &p3);
    // s_point_add(p1, p2, &p3);
    // POINT_ADD(p1, p2, p3);
  }
  printf("Result is <%d, %d>\n", p3.x, p3.y);
}

Compiling

[deebakkarthi@macbook]semester_7/19CSE401/playground % gcc-13 -o o0.out gcc_opt_test.c
ld: warning: ignoring duplicate libraries: '-lgcc'
[deebakkarthi@macbook]semester_7/19CSE401/playground % gcc-13 -O1 -o o1.out gcc_opt_test.c
ld: warning: ignoring duplicate libraries: '-lgcc'
[deebakkarthi@macbook]semester_7/19CSE401/playground % gcc-13 -O2 -o o2.out gcc_opt_test.c
ld: warning: ignoring duplicate libraries: '-lgcc'
[deebakkarthi@macbook]semester_7/19CSE401/playground % gcc-13 -O3 -o o3.out gcc_opt_test.c
ld: warning: ignoring duplicate libraries: '-lgcc'

Results

[deebakkarthi@macbook]semester_7/19CSE401/playground % time ./o0.out
Result is <3, 3>
./o0.out  4.53s user 0.01s system 99% cpu 4.543 total
[deebakkarthi@macbook]semester_7/19CSE401/playground % time ./o1.out
Result is <3, 3>
./o1.out  0.71s user 0.00s system 99% cpu 0.717 total
[deebakkarthi@macbook]semester_7/19CSE401/playground % time ./o2.out
Result is <3, 3>
./o2.out  0.00s user 0.00s system 60% cpu 0.007 total
[deebakkarthi@macbook]semester_7/19CSE401/playground % time ./o3.out
Result is <3, 3>
./o3.out  0.00s user 0.00s system 66% cpu 0.006 total
[deebakkarthi@macbook]semester_7/19CSE401/playground %

Replacing int with short

time ./o0.out
Result is <3, 3>
        4.56 real         4.55 user         0.00 sys
time ./o1.out
Result is <3, 3>
        0.67 real         0.67 user         0.00 sys
time ./o2.out
Result is <3, 3>
        0.00 real         0.00 user         0.00 sys
time ./o3.out
Result is <3, 3>
        0.00 real         0.00 user         0.00 sys

Making point_add() a macro

time ./o0.out
Result is <3, 1227>
        6.50 real         6.48 user         0.01 sys
time ./o1.out
Result is <3, -1>
        0.69 real         0.69 user         0.00 sys
time ./o2.out
Result is <3, -1>
        0.00 real         0.00 user         0.00 sys
time ./o3.out
Result is <3, -1>
        0.00 real         0.00 user         0.00 sys

Making point_add() static

time ./o0.out
Result is <3, 3>
        4.51 real         4.50 user         0.00 sys
time ./o1.out
Result is <3, 3>
        0.69 real         0.69 user         0.00 sys
time ./o2.out
Result is <3, 3>
        0.00 real         0.00 user         0.00 sys
time ./o3.out
Result is <3, 3>
        0.00 real         0.00 user         0.00 sys