Files
2025-02-Compiler/notes/1.md

95 lines
1.8 KiB
Markdown

# ISA & Compiler Basics
## Basic Computer
To get a task done by a general-purpose computer, we need:
* Program: A sequence of instructions
* Instruction set: A set of possible instructions
### Von Neuman Architecture
Both Instructions and data are stored in the memory.
Instructions dictate:
1. which and how data are manipulated
2. which instruction should be next
The memory is independent of the CPU.
### How to load a program
```
*.c -(compiler)-> *.s: Assembly program
*.s -(assembler)-> *.o: Object file
*.o -(linker)-[with library(*.o)]-> *.exe: Executable
*.exe -(loader)-> to in memory
```
### Program
Computer is essentially a complex state machine.
**programmer visible state**:
* Memory
* Registers
* Program Counter
Instructions(Program) specify how to transform the values of programmer visible state.
# Compiler
* Front-End
보이는 부분(HW에 신경을 안써도 됨)
* Back-End
HW에 밀접한 최적화를 해줌
## General Structure of a Modern Compiler
* Front-End
* Lexial Analysis
* Syntax Analysis
* Semantic Analysis
* Code Generation - 1
* Back-End
* Control/DataFlow Analysis
* Optimization
* Code Generation - 2
이런 모듈식 구조는 다양한 언어와 하드웨어에 쉽게 적용할 수 있도록 도운다.
### Lexical Analysis (Scanner)
프로그램을 `token`의 의미 단위로 나눔.
그리고 의미가 없는 단위를 지움.
보통 FSA로 구현함.
### Syntax Analysis (Parser)
미리 있는 Grammar를 바탕으로 Syntax Correctness를 진행함.
### Semantic Analysis
* identifier 정의 등
* 타입 체크
* 스코프 체크
* 오버로딩 모호화 해소
* IR로 변환
### Optimization
최적화함
* 상수 최적화
* 안쓰는 변수
* loop에서 안바뀌는 변수
* 다시 똑같이 계산하는 변수 제거
* ...
### Code Generation