summaryrefslogtreecommitdiff
path: root/Homework/cs5300/homework-one/compilers-one.org
blob: a723cd8db40061a8ab3a760811b6c29062895375 (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
#+TITLE: Homework - Chapter One
#+AUTHOR: Logan Hunt
#+OPTIONS: toc:nil
#+STARTUP: entitiespretty fold inlineimages
#+LATEX_HEADER: \noindent \notag \usepackage{ dsfont }

* Question One
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.

* Question Two
** a
Compiled machine code is typically much faster than interpreted instructions.
** b
An interpreter can give better error diagnostics.

* Question Three
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.

* Question Four
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.

* Question Five
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.

* Question Six
\begin{verbatim}
R1 = 20
R2 = 12
\end{verbatim}

* Question Seven
id2 is not a register

* Question Eight
\begin{verbatim}
1. R1 = 9
2. R2 = 2
3. R1 = 11
4. R1 = 6.0
5. id3 = R1 = 6.0
\end{verbatim}

* Question Nine
** JavaScript
+ imperative
+ declarative
+ third-generation
+ object-oriented (through prototypes)
+ functional
+ scripting
** Python
+ imperative
+ declarative
+ third-generation
+ object-oriented
+ functional
+ scripting

* Question Ten
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.

* Question Eleven
1. Self-Hosted compilers, themselves. Compiling a compiler with an optimization bug in the hosted compiler would probably
   be a nightmare to fix.
2. Anything in the Linux kernel that runs in user space.
3. ~malloc~

* Question Twelve
This contrived program:
#+BEGIN_SRC C
  int main() {
    int i;
    for (i = 0; i < 10000000; i++);
    return 0;
  }
#+END_SRC

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

* Question Thirteen
| Declaration | Scope        |
| ~int b = 1~ | B_1 - B_2      |
| ~int a = 2~ | B_2 - B_4      |
| ~int b = 2~ | B_2 - B_3 - B_4 |
| ~int b = 3~ | B_3           |
| ~int a = 4~ | B_4 - B_5      |
| ~int b = 4~ | B_4           |
| ~int a = 5~ | B_5           |

* Question Fourteen
\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, ~w = 5, x = 4, y = 5, z = 6~.

* Question Fifteen
\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, ~w = 5, x = 7, y = 16, z = 11~

* Question Sixteen
b : x = 4 \Rightarrow x = (x+3)-1 \Rightarrow x = 6

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

"6,4"