Code in C Journey Part 2

Code in C Journey Part 2

In this article we will continue talking about how software code journey is ?

Hardware vendor is responsible for the Drivers.
Drivers is the layer between the physical hardware and the Operating system.

Bare-metal is the application running directly on HW (without OS).

Compiler vs Interpreter

Each of which is responsible for translating source code in ML but in a different way.

Compiler (faster)

  • Compiler Approach:

    1. Assume there's a foreign person in some organization talking in a language not so many people can understand so a we need a translator.
    2. This translator will translate every thing from that person and writes it in a paper then if anyone needs the translation he will give him the paper.
  • Is responsible for translating source code in ML.

  • Is responsible for checking every syntax in the code as one piece & no fault-tolerence (if there were tiny mistakes ===> compile error).
  • once the code is converted to machine language it's very fast to run and no need to compile again unless some changes happened.

Interpreter (slower)

  • Interpreter Approach
    Assume another translator in stadium office translating a foreign expert's talk/opinion live

  • Is responsible for translating source code in ML.

  • Is responsible for checking every syntax in the code line by line.
  • will need to start converting every time running the code even no changes happened.

JVM

In java environment it's quite bit different to run some sort of code

  1. Java Code is ==> compiler ==>
    Byte Code (un runnable encrypted ML (Machine Code) )
    ==> JVM (Java Virtual Machine) on any OS to run any java program
    contains JIT (Just-in-Time) if repeated code is found it'll translate
    it to ML & use it whenever needed
    ==> interpreter (dynamic linking) links with library in run time

“JIT” is a technique that will compile parts of the code at runtime so that the compiled version can be used instead. Think of it like a “cached version” of the interpreted code, generated at runtime. For CPU-intensive tasks, having a JIT compiler in PHP boasts significant performance gains.

Static Linking vs Dynamic Linking

Take a look at (references): baeldung.com/cs/static-dynamic-linking-diff..

Example for library linking: Missing DLL library happens in run time

There are 3 apps (about some modules) downloaded on PC & need same libraries in run time by using dynamic linking - better way than - every app contains all libraries in one file code size very big.

Interpreter bad behavior: was repeating converting the same files from source code to machine language ML as long as needed but this was slowing system/app performance solution was in JVM in java virtual machine (JIT -compiler)

byte code is java source code after compilation (bu javac) (but can't be run as if ML) hashed ML. JVM is an interpreter to interpret byte code (enbles me to use dunamic linking) in run time. dyanmic linking:==> JVM contains linker calls library in run time (if not found crash) so java apps = small modules and all needed shared libraries outside called in run time JVM in java virtual machine (JIT -compiler) enhances performance of JVM as follows JIT converts repeated code (from byte code to ML) fixed to be fast in retrieving in the future and JIT compiler executes this ML just like compiler not interpreter.

Java did all this to run on java-based apps any OS just by using JVM

Java: JVM calls library in run time (if not found crash) C : compines library files with source code into one executable file.

clarification about JDK vs JRE JDK: compiler(called as javac) + debugger + JRE (JVM + Other libraries) (if you are java developer) JRE: JVM + Other libraries (if you are a java-based app user)

Java is dynamic linking (reduced size + fast | risk of crashing because of losing libraries). C is static linking (big size + slow | No risk of crashing because of losing libraries).

python is dynamic language : only pros = fast development (quickly)


C compiler journey

Build Process of C code: { C source code -> preprocessor -> compiler -> linker (with libraries) ->finally .exe executable file }

we need 2 things to run any library (code + import name (header of library) something like include)

preprocessor defines headers of libraries needed to prevent compiler from error.

compiler = assembler + compiler compiler code -> aseemply->object (originally 2 steps) now compiler direct from code -> object code (ML)

preprocessor can see file by file but, linker can see all files at the same time.

.c code file - >preprocessor -> .i file (contains headers about libraries needed) -> compiler -> .obj (ML but needs libraries) -> linker (linking library (.a file ML ready to link and run) + code we wrote (.obj) = .exe)

Toolchain i.e. (gcc compiler) is = preprocessor + compiler + linker gcc compiler is mainly called compiler as it's the main component but contains others.


Java error because of missing library = run time error C error because of missing library = (development stage) compile time error (in the build process) exactly in the linker.

python have syntax but implementation is left for open source community (Cpython compiler- PyPy - jython compiler) cpython is the default which has interpreter and a weak compiler (tokenize|tokens checks for small stuff like tabs and spaces) but any syntax error is identified by interpreter not compiler most of the time but sometimes compile error may happen.


LLVM compiler infrastructure to convert any prog language to ML search for it . . .


Compiler Design what does compiler consist of? code -> frontend + intermediate representation + backend -> ML see references

siddharthsingh89.github.io/compiler-design