This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lecture9.tex
120 lines (108 loc) · 5.54 KB
/
lecture9.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
\chapter{Compiler, Assembler, Linker, Loader (CALL)}
\section{Interpretation vs Translation}
We can either \emph{interpret} a high-level language when efficiency is not critical or \emph{translate} to a lower-level language to increase performance.
\begin{itemize}
\item Translated/compiled code is more efficient and helps hide the program source from users.
\item Interpreters have instruction set independence.
\end{itemize}
\section{Compiler}
Compiler converts a high-level language file into an optimized assembly language file.
\emph{Note:} Compilers rely on the architecture of the machine.
\begin{itemize}
\item Input: High-level language code (\texttt{foo.c})
\item Output: Assembly language code (\texttt{foo.a})
\end{itemize}
\subsection{Compiler Steps}
Compilers use FSMs to keep track of the program's logical states and renders the code into an optimized assembly output.
\begin{enumerate}
\item Lexer: Tokenizes inputs
\item Parser: Turns tokens into an abstract syntax tree
\item Semantic Analysis and Optimization: Checks for semantic errors
\item Code Generation: Outputs assembly code
\end{enumerate}
\section{Assembler}
Assembler removes pseudo-instructions, converts what it can to machine language, resolves relative addressing, and creates a checklist for the linker (symbol and relocation tables) to help resolve references later on.
\begin{itemize}
\item Input: Assembly language code (\texttt{foo.a})
\item Output: Object code, i.e. true assembly (\texttt{foo.o})
\end{itemize}
\subsection{Assembler Directives}
Gives directions to assembler but do not produce machine instructions.
\begin{description}
\item[\texttt{.text}:] Subsequent items put in user text segment (machine code)
\item[\texttt{.data}:] Subsequent items put in user data segment (binary rep of data in source file)
\item[\texttt{.global sym}:] Declares \texttt{sym} global and can be referenced from other files
\item[\texttt{.string str}:] Store the string \texttt{str} in memory and null-terminate it
\item[\texttt{.word w1...wn}:] Store the $n$ 32-bit quantities in successive memory words
\end{description}
\subsection{Object Files}
From top to bottom of the object file, the components are:
\begin{enumerate}
\item \textbf{Object File Header:} Holds the size and position of other components in the object file; table of contents.
\item \textbf{Text Segment:} Holds only the machine code.
\item \textbf{Data Segment:} Holds the static information in the original code.
\item \textbf{Relocation Information:} Unresolved references to external libraries or other assembly files.
\item \textbf{Symbol Table:} Keeps a record of all labels defined and used within the \emph{local} assembled file.
\item \textbf{Debugging Information}
\end{enumerate}
\subsection{Producing Machine Language}
Simple cases such as arithmetic have all the information stored within 32-bit instructions. But how are PC-relative branches and jumps handled?
\begin{description}
\item[Forward Reference Problem:] Branch instructions may refer to unseen labels $\implies$ solve using two passes.
\begin{itemize}
\item First pass: remembers positions of labels
\item Second pass: uses label positions to generate code
\end{itemize}
\item[Jumps:] Count number of instructions between target and jump to determine offset (turn PC-relative jumps in the code code into \emph{position-independent code}).
\item[References to Static Data:] Cannot be determined yet, so we create 2 tables:
\begin{enumerate}
\item Symbol Table: items that may be used by other files (exportable)
\begin{itemize}
\item labels: function calling
\item data
\end{itemize}
\item Relocation Table: items whose address this files need, which that linker will replace later
\begin{itemize}
\item external labels jumped to
\item static data
\end{itemize}
\end{enumerate}
\end{description}
\section{Linker}
Linker combines several \texttt{.o} files and resolves absolute addresses.
\begin{itemize}
\item Input: Object code files with information tables (\texttt{foo.o})
\item Output: Executable code with text and data (\texttt{a.out})
\end{itemize}
\subsection{Three Types of Addresses}
\begin{enumerate}
\item \texttt{PC}-relative addressing: NEVER relocate
\begin{itemize}
\item \texttt{beq}, \texttt{bne}, \texttt{jal}
\item \texttt{lla}: \texttt{auipc}, \texttt{addi}
\end{itemize}
\item Absolute/external function addresses: always relocate
\begin{itemize}
\item \texttt{la}: \texttt{auipc}/\texttt{lw} + \texttt{jalr}
\end{itemize}
\item Static data references: always relocate
\begin{itemize}
\item \texttt{lui}/\texttt{addi}
\end{itemize}
\end{enumerate}
\section{Loader}
Loader loads executable into memory and runs program.
\begin{itemize}
\item Input: Executable code with text and data (\texttt{a.out})
\item Output: Program is run
\end{itemize}
\subsection{Loader Steps}
Loader is the OS.
\begin{enumerate}
\item Reads executable file’s header to determine size of text and data segments
\item Creates new address space for program large enough to hold text and data segments, along with a stack segment
\item Copies instructions and data from executable file into the new address space
\item Copies arguments passed to the program onto the stack
\item Initializes machine registers
\item Jumps to start-up routine that copies program’s arguments from stack to registers and sets the PC
\end{enumerate}