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