summaryrefslogtreecommitdiff
path: root/Homework/cs5300/homework-one/compilers-one.tex
blob: 223758a05ef8cbf53a793d7400d418697886cbdf (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
% Created 2023-01-16 Mon 13:28
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\noindent \notag \usepackage{ dsfont }
\author{Logan Hunt}
\date{\today}
\title{Homework - Chapter One}
\hypersetup{
 pdfauthor={Logan Hunt},
 pdftitle={Homework - Chapter One},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 28.2 (Org mode 9.5.5)}, 
 pdflang={English}}
\begin{document}

\maketitle

\section{Question One}
\label{sec:orge85a534}
A compiler is a program that reads source in one language, and translates it to an equivalent program to another language,
which can immediately be run in that language. An interpreter, on the other hand, directly executes the source as the
program continues.

\section{Question Two}
\label{sec:orgb550d55}
\subsection{a}
\label{sec:org1f4ed7b}
Compiled machine code is typically much faster than interpreted instructions.
\subsection{b}
\label{sec:orga3b0952}
An interpreter can give better error diagnostics.

\section{Question Three}
\label{sec:orgdddbe9a}
In terms of portability, compiled programs are less so than interpreters. Compiled programs (to machine code) target
a single architecture, and thus require seperate compilation, or cross-compilation over all target architectures.
Interpreted programs on the other hand will run anywhere, as long as there is an interpreter implementation for the
architecture.

\section{Question Four}
\label{sec:org348f3da}
Java is a shady devil that likes to play both sides. Java source is compiled to intermediate Java bytecodes, which
are then interpreted by the Java Virtual Machine. The compilation aspect of Java here, is in the translation to JVM
bytecode instructions.

\section{Question Five}
\label{sec:org2f098fc}
One might want to view generated assembly code to debug an issue in their code and step through the compiler's
output, understand any optimizations the compiler may make, or to just explore its output.

\section{Question Six}
\label{sec:org50a4a97}
\begin{verbatim}
R1 = 20
R2 = 12
\end{verbatim}

\section{Question Seven}
\label{sec:org199da87}
id2 is not a register

\section{Question Eight}
\label{sec:org3216fe2}
\begin{verbatim}
1. R1 = 9
2. R2 = 2
3. R1 = 11
4. R1 = 6.0
5. id3 = R1 = 6.0
\end{verbatim}

\section{Question Nine}
\label{sec:org2cf7709}
\subsection{JavaScript}
\label{sec:org769857f}
\begin{itemize}
\item imperative
\item declarative
\item third-generation
\item object-oriented (through prototypes)
\item functional
\item scripting
\end{itemize}
\subsection{Python}
\label{sec:orgb53d3f0}
\begin{itemize}
\item imperative
\item declarative
\item third-generation
\item object-oriented
\item functional
\item scripting
\end{itemize}

\section{Question Ten}
\label{sec:org6722ba8}
According to the book, "A distinguishing feature of object-oriented programming is the ability of each object to invoke the appropriate
method in response to a message." In C, this is not possible as structs do not have support
for methods.

\section{Question Eleven}
\label{sec:orgc3a76ec}
\begin{enumerate}
\item Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably
be a nightmare to fix.
\item Anything in the Linux kernel that runs in user space.
\item \texttt{malloc}
\end{enumerate}

\section{Question Twelve}
\label{sec:org5f61a58}
This contrived program:
\begin{verbatim}
int main() {
  int i;
  for (i = 0; i < 10000000; i++);
  return 0;
}
\end{verbatim}

It wouldn't be so bad for the compiler to just set i = 10000000.

\section{Question Thirteen}
\label{sec:orgebfa92b}
\begin{center}
\begin{tabular}{ll}
Declaration & Scope\\[0pt]
\texttt{int b = 1} & B\textsubscript{1} - B\textsubscript{2}\\[0pt]
\texttt{int a = 2} & B\textsubscript{2} - B\textsubscript{4}\\[0pt]
\texttt{int b = 2} & B\textsubscript{2} - B\textsubscript{3} - B\textsubscript{4}\\[0pt]
\texttt{int b = 3} & B\textsubscript{3}\\[0pt]
\texttt{int a = 4} & B\textsubscript{4} - B\textsubscript{5}\\[0pt]
\texttt{int b = 4} & B\textsubscript{4}\\[0pt]
\texttt{int a = 5} & B\textsubscript{5}\\[0pt]
\end{tabular}
\end{center}

\section{Question Fourteen}
\label{sec:org5304edd}
\begin{verbatim}
i = 5
j = 8
  i = 4[
j = 9
w = j - i = 9 - 4 = 5
x = j - i = 9 - 5 = 4
  j = 10
y = j - i = 10 - 5 = 5
i = 3
z = j - i = 9 - 3 = 6
\end{verbatim}

Thus, \texttt{w = 5, x = 4, y = 5, z = 6}.

\section{Question Fifteen}
\label{sec:org9f74779}
\begin{verbatim}
i = 2
j = 5
  i = 3
w = i + j = 3 + 2 = 5
x = i + j = 2 + 5 = 7
  j = 9
i = 7
y = i + j = 7 + 9 = 16
i = 6
z = i + j = 6 + 5 = 11
\end{verbatim}

Thus, \texttt{w = 5, x = 7, y = 16, z = 11}

\section{Question Sixteen}
\label{sec:org31a90e4}
b : x = 4 \(\Rightarrow\) x = (x+3)-1 \(\Rightarrow\) x = 6

c : x = 1 \(\Rightarrow\) (x + 3) \(\Rightarrow\) 4

"6,4"
\end{document}