annotate lwcc/cc-gencode.c @ 501:f3e9732973f1

Add basic integer operations to lwcc Add +, -, *, and / to lwcc parser and code generator. Multiplication and division require helper functions in a yet to be created support library. These operations are integer only for the moment.
author William Astle <lost@l-w.ca>
date Tue, 24 Sep 2019 22:07:56 -0600
parents c3099c5d9d3e
children 14a40f8bb4eb
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
501
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
46 case NODE_OPER_PLUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
47 generate_code(n->children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
48 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
49 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
50 fprintf(output, "\taddd ,s++\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
51 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
52
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
53 case NODE_OPER_MINUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
54 generate_code(n->children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
55 fprintf(output, "\tpshs d,x\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
56 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
57 fprintf(output, "\tstd 2,s\n\tpuls d\n\tsubd ,s++\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
58 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
59
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
60 case NODE_OPER_TIMES:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
61 generate_code(n -> children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
62 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
63 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
64 fprintf(output, "\tjsr ___mul16i\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
65 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
66
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
67 case NODE_OPER_DIVIDE:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
68 generate_code(n -> children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
69 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
70 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
71 fprintf(output, "\tjsr ___div16i\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
72 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
73
499
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
74 default:
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
75 for (nn = n -> children; nn; nn = nn -> next_child)
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
76 generate_code(nn, output);
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
77 break;
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
78 }
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
79 }