-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
181 lines (172 loc) · 4.96 KB
/
codegen.c
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
#include "codegen.h"
#include <stdlib.h>
#include <stdint.h>
#include "differentiator.h"
static void generate_code_ft_rec(syntax_tree_node *tree,
FILE *out,
int stackDepth, int esp);
static void generate_code_ft_rec(syntax_tree_node *tree,
FILE *out,
int stackDepth, int esp) {
// supposed that when you call this functions there are at least
// 2 free slots in stack
if (stackDepth < 2) {
fprintf(stderr, "Code generation error! Stack is empty!\n");
abort();
return;
}
switch (tree->op) {
// zero-operand operations
case TOKEN_X:
fprintf(out, " fld qword[ebp+8]\n");
return;
case TOKEN_NUMBER:
{
union {
double d;
uint64_t u64;
} value;
value.d = tree->num;
// fprintf(out, " push qword 0x%llx\n", value.u64); // only in 64-bit mode
fprintf(out, " push dword 0x%x\n", (uint32_t)(value.u64 >> 32));
fprintf(out, " push dword 0x%x\n", (uint32_t)value.u64);
fprintf(out, " fld qword[ebp+%u]\n", esp-8);
fprintf(out, " add esp, 8\n");
return;
}
case TOKEN_E:
fprintf(out, " fldl2e\n");
fprintf(out, " f2xm1\n");
fprintf(out, " fld1\n");
fprintf(out, " faddp\n");
return;
case TOKEN_PI:
fprintf(out, " fldpi\n");
return;
case TOKEN_PLUS:
case TOKEN_MINUS:
case TOKEN_SIN:
case TOKEN_COS:
case TOKEN_TAN:
case TOKEN_CTG:
case TOKEN_DIV:
case TOKEN_MUL:
case TOKEN_EOF:
break;
}
// one-operand operations
generate_code_ft_rec(tree->first, out, stackDepth, esp);
switch (tree->op) {
case TOKEN_SIN:
fprintf(out, " fsin\n");
return;
case TOKEN_COS:
fprintf(out, " fcos\n");
return;
case TOKEN_TAN:
fprintf(out, " fptan\n");
fprintf(out, " fdivp\n");
return;
case TOKEN_CTG:
fprintf(out, " fptan\n");
fprintf(out, " fdivrp\n");
return;
case TOKEN_PLUS:
case TOKEN_MINUS:
case TOKEN_X:
case TOKEN_NUMBER:
case TOKEN_E:
case TOKEN_PI:
case TOKEN_DIV:
case TOKEN_MUL:
case TOKEN_EOF:
break;
}
// two-operand operations
esp -= 8;
fprintf(out, " sub esp, 8\n");
fprintf(out, " fstp qword[ebp+%u]\n", esp);
generate_code_ft_rec(tree->second, out, stackDepth, esp);
switch (tree->op) {
case TOKEN_PLUS:
fprintf(out, " fadd qword[ebp+%u]\n", esp);
break;
case TOKEN_MINUS:
fprintf(out, " fsubr qword[ebp+%u]\n", esp);
break;
case TOKEN_MUL:
fprintf(out, " fmul qword[ebp+%u]\n", esp);
break;
case TOKEN_DIV:
fprintf(out, " fdivr qword[ebp+%u]\n", esp);
break;
case TOKEN_X:
case TOKEN_NUMBER:
case TOKEN_E:
case TOKEN_PI:
case TOKEN_SIN:
case TOKEN_COS:
case TOKEN_TAN:
case TOKEN_CTG:
case TOKEN_EOF:
break;
}
fprintf(out, " add esp, 8\n");
esp += 8;
}
void generate_code_from_tree(syntax_tree_node *tree,
FILE *assembly_output) {
generate_code_ft_rec(tree, assembly_output, 8, 0);
}
static void generate_code_func_prologue(FILE *assembly_output) {
fprintf(assembly_output, " push ebp\n");
fprintf(assembly_output, " mov ebp, esp\n");
}
static void generate_code_func_epilogue(FILE *assembly_output) {
fprintf(assembly_output, " leave\n");
fprintf(assembly_output, " ret\n");
}
void generate_code(syntax_tree_node *tree1,
syntax_tree_node *tree2,
syntax_tree_node *tree3,
double left_border,
double right_border,
FILE *assembly_output) {
fprintf(assembly_output, "section .data\n");
fprintf(assembly_output, "global left_border, right_border\n");
fprintf(assembly_output, " left_border dq %f\n", left_border);
fprintf(assembly_output, " right_border dq %f\n", right_border);
fprintf(assembly_output, "section .text\n");
fprintf(assembly_output, "global f1, f2, f3, g1, g2, g3, _f1, _f2, _f3, "
"_g1, _g2, _g3\n");
fprintf(assembly_output, "f1:\n");
fprintf(assembly_output, "_f1:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(tree1, assembly_output);
generate_code_func_epilogue(assembly_output);
fprintf(assembly_output, "f2:\n");
fprintf(assembly_output, "_f2:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(tree2, assembly_output);
generate_code_func_epilogue(assembly_output);
fprintf(assembly_output, "f3:\n");
fprintf(assembly_output, "_f3:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(tree3, assembly_output);
generate_code_func_epilogue(assembly_output);
fprintf(assembly_output, "g1:\n");
fprintf(assembly_output, "_g1:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(differentiate(tree1), assembly_output);
generate_code_func_epilogue(assembly_output);
fprintf(assembly_output, "g2:\n");
fprintf(assembly_output, "_g2:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(differentiate(tree2), assembly_output);
generate_code_func_epilogue(assembly_output);
fprintf(assembly_output, "g3:\n");
fprintf(assembly_output, "_g3:\n");
generate_code_func_prologue(assembly_output);
generate_code_from_tree(differentiate(tree3), assembly_output);
generate_code_func_epilogue(assembly_output);
}