comparison lwcc/cc-gencode.c @ 499:c3099c5d9d3e

Add very simple code generator This very simple code generator is definitely not the final form. It is simply a stop-gap for ongoing development.
author William Astle <lost@l-w.ca>
date Thu, 08 Aug 2019 23:48:18 -0600
parents
children f3e9732973f1
comparison
equal deleted inserted replaced
498:1bd2d590d734 499:c3099c5d9d3e
1 /*
2 lwcc/cc-gencode.c
3
4 Copyright © 2019 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24
25 #include <lw_alloc.h>
26 #include <lw_string.h>
27
28 #include "tree.h"
29
30 void generate_code(node_t *n, FILE *output)
31 {
32 node_t *nn;
33 switch (n -> type)
34 {
35 // function definition - output prologue, then statements, then epilogue
36 case NODE_FUNDEF:
37 fprintf(output, "_%s\n", n->children->next_child->strval);
38 generate_code(n->children->next_child->next_child->next_child, output);
39 fprintf(output, "\trts\n");
40 break;
41
42 case NODE_CONST_INT:
43 fprintf(output, "\tldd #%s\n", n->strval);
44 break;
45
46 default:
47 for (nn = n -> children; nn; nn = nn -> next_child)
48 generate_code(nn, output);
49 break;
50 }
51 }