CompilerDev/Bare Bones Compiler: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content added Content deleted
No edit summary
No edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Rating|2}}
{{In Progress}}{{Rating|2}}
[[Compiler]]s are a very important piece of software in any Operating System Developer's toolkit. Without it, your code would only be code. You would have to write all executables by hand, or in assembly, and then assemble it. Compilers make this easy by allowing us to use a higher level language, such as [[C]], [[C++]], or [[D]], in traditionally lower level programming. For example, Operating System development (such as the first builds of unix and apple DOS) used to have to be written in Assembly, which isn't neccesarily very portable, but with the creation of languages such as C, you are able to program in higher level, human-readable code that can be ported to any operating system that has a c compiler for it. Today, we are going to learn how to write a minimalist c compiler in the C programming language.
[[Compiler]]s are a very important piece of software in any Operating System Developer's toolkit. Without it, your code would only be code. You would have to write all executables by hand, or in assembly, and then assemble it. Compilers make this easy by allowing us to use a higher level language, such as [[C]], [[C++]], or [[D]], in traditionally lower level programming. For example, Operating System development (such as the first builds of unix and apple DOS) used to have to be written in Assembly, which isn't neccesarily very portable, but with the creation of languages such as C, you are able to program in higher level, human-readable code that can be ported to any operating system that has a c compiler for it. Today, we are going to learn how to write a dsl in the C programming language.




Line 12: Line 12:
* Knowledge of the Unix Operating System
* Knowledge of the Unix Operating System


== What This Tutorial Covers ==
== What This Tutorial Doesn't Cover ==


[[Category:Compiler Development]]
== Step 1: Writing a preprocessor ==
The C programming language has what are called preprocessor directives, such as #include, #define, and #undef. These are called preprocessor directives because they are literally processed before the actual code is compiled. This is handled by a seperate program that executes all of these directives and dumps the output in one giant file called a [http://en.wikipedia.org/wiki/Translation_unit_%28programming%29 translation unit]. Before we start writing the preprocessor, lets take a look at each directive in detail.
=== Preprocessor Directives Overview ===
==== The include directive ====

<pre>
#include <some_header.h>
#include "some_header_in_a_specific_dir.h"
</pre>

As you can see, there are two forms of the include directive. The first one searches for the header (.h) file found in-between the < and the > in specific system folders. For example, in a typical Linux release, one of the folders it looks in is /usr/include.

Latest revision as of 17:58, 10 July 2023

This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.
Difficulty level

Medium

Compilers are a very important piece of software in any Operating System Developer's toolkit. Without it, your code would only be code. You would have to write all executables by hand, or in assembly, and then assemble it. Compilers make this easy by allowing us to use a higher level language, such as C, C++, or D, in traditionally lower level programming. For example, Operating System development (such as the first builds of unix and apple DOS) used to have to be written in Assembly, which isn't neccesarily very portable, but with the creation of languages such as C, you are able to program in higher level, human-readable code that can be ported to any operating system that has a c compiler for it. Today, we are going to learn how to write a dsl in the C programming language.


Disclaimer: I am no where near an expert on the matter. This is not for people looking for advanced compiler lessons. This is for people who don't know anything about writing a compiler. Also, the code shown here might not necessarily be the best written, but the theories are tight.

Required Knowledge:

  • Knowledge of the C programming language and its standard library
  • Basic Algorithim Design
  • Ability to read a theory and implement it in code (if you have trouble with this, try practicing on a few algorithms of varying difficulty)
  • Knowledge of the ELF file structure
  • Knowledge of the Unix Operating System

What This Tutorial Covers

What This Tutorial Doesn't Cover