-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemit.h
189 lines (164 loc) · 5.78 KB
/
emit.h
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
/**
* Header file for cminus assembly emitter.
*
* Writes NASM code to an output file given a reference
* to the abstract syntax tree and symbol table.
*
* See emit.c for implementations.
*
* Cameron Tauxe
*
* 16 April, 2018
* - Initial version
* 17 April, 2018
* - Most functions renamed for brevity
* - Added functions to enable emitting functions
**/
#ifndef EMIT
#define EMIT
#include <stdio.h>
#include "common.h"
//Debug flag. If true, all output will also be written to stderr
//(along with some additional information)
int emit_debug;
//Pointer to output FILE
static FILE *emit_file;
//whether or not the file has been set yet
static int emit_file_set = 0;
//keep track of labels and strings created so far
static int emit_labels = 0;
static int emit_strings = 0;
/**
* Emit code for an entire program.
* filename is the name of the file to write to
* prog_root is root AST_Node representing the program
**/
void emit_all(char *filename, AST_Node *prog_root);
/**
* Write the "header" of the NASM code.
* Writes the "%include 'io64.inc'",
* the .data section, with all global varaibles and strings
* and the beginning of the .text section with "global main"
* if a main function is defined
* first_decl is either a VAR_DECL or FUN_DECL type node
* representing the first declaration of the program
**/
void emit_head(AST_Node *first_decl);
/**
* Search the entire tree (starting from the given root)
* for WRITE_STR_STMTs and add their strings to the .data section
* of the NASM code.
**/
void emit_search_strings(AST_Node *prog_root);
/**
* Write a function to the output file.
* This includes the label declaring the function,
* and all the necessary stack frame management.
* fun_decl is a FUN_DECL node representing the function
**/
void emit_function(AST_Node *fun_decl);
/**
* Write code to exectute a statement to the output file.
* stmt is a node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing a statement.
**/
void emit_statement(AST_Node *stmt);
/**
* Write code to execute a BLOCK statement to the output file.
* stmt is a node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing a statement.
**/
void emit_block_stmt(AST_Node *stmt);
/**
* Write code to execute an IF/ELSE statement to the output file.
* stmt is a node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing a statement.
**/
void emit_if_stmt(AST_Node *stmt);
/**
* Write code to execute a LOOP (while) statement to the output file.
* stmt is a node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing a statement.
**/
void emit_loop_stmt(AST_Node *stmt);
/**
* Write code to execute a RETURN statement to the output file.
* stmt is a node representing the statement.
* After returning, the return value will be in register RSI
**/
void emit_ret_stmt(AST_Node *stmt);
/**
* Write code to execute a read statement to the output file.
* stmt is a node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing a statement.
**/
void emit_read_stmt(AST_Node *stmt);
/**
* Write code to exectute a write statement to the output file.
* stmt is a WRITE_STMT type node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing the statement.
**/
void emit_write_stmt(AST_Node *stmt);
/**
* Write code to exectute a write string statement to the output file.
* stmt is a WRITE_STR_STMT type node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing the statement.
**/
void emit_write_str_stmt(AST_Node * stmt);
/**
* Write code to execute an expression statement to the output file.
* stmt is a EXPR_STMT type node representing the statement.
* No assumptions should be made about the value of any registers
* before or after executing the statement.
**/
void emit_expr_stmt(AST_Node *stmt);
/**
* Write code to evaluate an expression to the output file.
* expr is a node representing the expression.
* After evaluating, the expression's value will be in register RSI
**/
void emit_handle_expr(AST_Node *expr);
/**
* Write code to peform an operation to the output file.
* op is the operation to perform.
* Assumes the operation's LHS is in RAX
* and that the RHS is in RBX
* After performing the operation, the result will be in RAX
* RBX may be changed as well.
**/
void emit_handle_operator(OPERATOR op);
/**
* Write code to handle a variable reference to the output file.
* var is a VAR_EXPR node representing the variable reference
* After evaluating, the address of the variable will be in register RAX
* (this will handle array access, so the value of RAX will be with
* the offset already applied)
**/
void emit_handle_var(AST_Node *var);
/**
* Write a line of SASM code to the console.
* Writes the line begining with the given label and ending with the given comment.
* (given label has to include the ':')
* (given comment has to include the ';')
* Written line is formatted like so:
* [label]\t[line]\t\t[comment]\n
**/
void emit_write_line(char *label, char *line, char *comment);
/*
* Write a string to the output file.
* Unlike emit_write_line no special formatting is performed
* (except for a newline at the end)
*/
void emit_write_plain_line(char *text);
/**
* Write a newline to the output file
*/
void emit_newline();
#endif