-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcodegen.html
142 lines (136 loc) · 9.12 KB
/
codegen.html
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
<!Doctype html>
<html lang="en">
<head>
<title>Code Generation</title>
<meta charset="UTF-8">
<!--<link rel="stylesheet" href="css/bootstrap.min.css">-->
<link rel="stylesheet" href="css/style_new.css">
<script src="js/jquery-1.12.1.min.js" charset="utf-8"></script>
<link rel="stylesheet" href="js/embed-2cd369fa1c0830bd3aa06c21d4f14a13e060d2d31bbaae740f4af4.css"><div id="gist28627206" class="gist">
<link rel="stylesheet" href="js/embed-cbe5b40fa72b0964f90d4919c2da8f8f94d7c9f6c2aa49c07f6fa3.css"><div id="gist28627206" class="gist">
<script src="js/bootstrap.min.js" charset="utf-8"></script>
</head>
<div class="container">
<header id="navtop">
<a href="index.html" class="logo fleft"><img src="img/logo.png" alt=""></a>
<nav class="fright">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<!-- <li><a href="help.html">Help</a></li> -->
<li><a href="roadmap.html">Roadmap</a></li>
<li><a href="documentation.html" class="navactive">Documentation</a></li>
</ul>
</nav>
</header>
<div class="Services-page main grid-wrap">
<header class="grid col-full">
<hr/>
<p class="fleft">Code Generation</p>
<br>
<br>
</header>
<aside class="grid col-one-quarter mq2-col-full sticky_sidebar" style="position: static;">
<menu>
<ul>
<li><a class="sec" href="#nav-introduction">Introduction</a></li>
<li><a class="sec" href="#nav-implementation">Implementation</a></li>
<li><a class="sec" href="#nav-illustration">Illustration</a></li>
</ul>
</menu>
<!-- <a class="button" href="">Download as PDF</a> -->
</aside>
<section class="grid col-three-quarters mq2-col-full">
<div class="grid-wrap">
<article class="grid col-full" id="nav-introduction">
<h2>Introduction</h2>
<p>
In Code Generation step, the ExpL compiler converts the intermediate representation (the abstract syntax tree) of the ExpL program to a machine code that can be readily executed by the XSM machine.
</p>
<div class="syntax">
int <b>codeGen</b>(struct ASTNode *t, file *targetfile)
</div>
<br>
<p>
The codeGen() function takes as input a pointer to the root of an abstract syntax tree and a pointer to a file to which the target code has to be written. The codeGen() function generates assembly code corresponding to the program represented by the AST. The codeGen() function essentially invokes itself <b>recursively</b> to generate assembly code for the subtrees. The result of evaluation is a value which is stored in a register. The codeGen() function returns the register number that would contain the evaluated value when the assembly code is executed. (when no value results in evaluating a tree node, there is no register allocated to store the value and -1 is returned by codeGen() indicating this fact).
</p>
</article>
<article class="grid col-full" id="nav-implementation">
<h2>Implementation</h2>
<p>
codeGen() function recursively generates code for each nodetype. Firstly, the code is generated for the first subtree, 'ptr1' (if not NULL) and it's value is stored in say register R<sub>i</sub> by calling the codeGen() function with pointer to AST 'ptr1'. Similarly, code is generated for second and third subtrees 'ptr2' and 'ptr3' and results are in registers say R<sub>j</sub> and R<sub>k</sub>. Finally, using the registers R<sub>i</sub>, R<sub>j</sub> and R<sub>k</sub>, code is generated for the current node.
</p>
<p>
Lets consider the nodetype PLUS. PLUS has two operands which are represented by subtrees 'ptr1' and 'ptr2'. Code for 'ptr1' and 'ptr2' is generated by the following instructions.
</p>
<pre>
i = codeGen(t->ptr1);
j = codeGen(t->ptr2);
</pre>
<p>
Finally, the value to the current node is evaluated and saved to register R<sub>i</sub> and R<sub>j</sub> is freed.
</p>
<pre>
fprintf() //Add this during implementation
free_register(j);
</pre>
<p>
Following is how code is generated for the nodetype 'while'. Note that, the labels generated here are psuedo addresses. We will deal about replacing the labels with actual address in the <a href="label-translation.html">label translation</a> documentation.
</p>
<p>
We have two subtrees for 'while' nodetype. 'ptr1' representing the conditional expression in while statement and 'ptr2' representing the body of while statement.
<ol>
<li>Get two labels using get_label() function and write down the first label, say 'LL1' to the intermediate code, so that we can identify where exactly the code for while starts.</li>
<li>Then, the code for conditional expresssion represented by 'ptr1' is generated and value stored into a register, say R<sub>i</sub>.</li>
<li>Next, we would check the value in R<sub>i</sub>, if its zero, i.e, conditional expression has evaluated to false, we would jump to the end of the while code. So, the next instruction is to check if R<sub>i</sub> is zero and jump on to second label, say LL2 where the code to the body of while statement ends.</li>
<li>If the value in register R<sub>i</sub> is not zero, i.e, the conditional expression evaluted to true, the we would execute the body of while. Therefore, after the above steps code for 'ptr2' is generated and at the end of it, the jump statement to the first label, LL1 is given, so that, the conditional expression can be evalauted again and the decision whether to execute the body of while is made.</li>
<li>Finally the second label is given,so that we can mark the end of while statement.</li>
</ol>
</p>
<p>
For the code generation for functions, the activities are given <a href="run_data_structures/run-time-stack.html">here.</a>
</p>
<p>
For making library calls, follow the steps given in the <a href="abi.html#nav-eXpOS-system-library-interface">invoking a library module</a> section in <a href="abi.html">Application Binary Interface</a> documentation.
</p>
</article>
<article class="grid col-full" id="nav-illustration">
<h2>Illustration</h2>
<img src="img/tree3.png">
<img src="img/codegen.png">
<p>
Consider the ExpL program given below.
</p>
<script src="js/6cde145e400e15cfda5f1a65366a264f.js"></script>
<p>
The XSM instructions for the above while code (lines 11-12) will be as follows:
</p>
<script src="js/e56548d34b57daf99a4b772685057233.js"></script>
</article>
</section>
</div>
</div>
<script src="js/sticky_sidebar.js" charset="utf-8"></script>
<footer class="center part clearfix">
<ul class="grid col-one-third social">
<li><a href="http://github.com/silcnitc">Github</a></li>
<li> <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">
<img alt="Creative Commons License" style="border-width:0" src="img/creativecommons.png" /></a></li>
</ul>
<div class="grid col-one-third" style="color:black;">
<p style="font-weight: bold;">Contributed By : <a href="https://www.linkedin.com/in/dattathallam">Thallam Sai Sree Datta</a>, <a href="https://www.linkedin.com/in/n-ruthviik-0a0539100">N Ruthvik</a>
</p>
</div>
<nav class="grid col-one-third ">
<ul >
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<!-- <li><a href="uc.html">Contact</a></li> -->
</ul>
</nav>
<br>
</footer>
<script>window.jQuery || document.write('<script src="js/jquery-1.7.2.min.js"><\/script>')</script>
<script src="js/scripts.js"></script>
<script src="js/inject.js"></script>
</html>