annotate lwcc/cc-gencode.c @ 503:17fa03d69df3

Unbork "make install" The merge of the lwcc stuff borked the Makefile some. This should unbork it. At least it should install the binaries in .../bin again.
author William Astle <lost@l-w.ca>
date Thu, 26 Sep 2019 21:37:49 -0600
parents 14a40f8bb4eb
children 59b8c8b15bd4
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
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
30 char *generate_nextlabel(void)
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
31 {
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
32 static int labelnum = 0;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
33 char buf[16];
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
34
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
35 sprintf(buf, "L%d", labelnum++);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
36 return lw_strdup(buf);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
37 }
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
38
499
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
39 void generate_code(node_t *n, FILE *output)
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
40 {
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
41 node_t *nn;
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
42 char *label1, *label2;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
43
499
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
44 switch (n -> type)
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 // function definition - output prologue, then statements, then epilogue
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
47 case NODE_FUNDEF:
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
48 fprintf(output, "_%s\n", n->children->next_child->strval);
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
49 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
50 fprintf(output, "\trts\n");
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
51 break;
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
52
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
53 case NODE_CONST_INT:
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
54 fprintf(output, "\tldd #%s\n", n->strval);
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
55 break;
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
56
501
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
57 case NODE_OPER_PLUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
58 generate_code(n->children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
59 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
60 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
61 fprintf(output, "\taddd ,s++\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
62 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
63
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
64 case NODE_OPER_MINUS:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
65 generate_code(n->children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
66 fprintf(output, "\tpshs d,x\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
67 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
68 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
69 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
70
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
71 case NODE_OPER_TIMES:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
72 generate_code(n -> children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
73 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
74 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
75 fprintf(output, "\tjsr ___mul16i\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
76 break;
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
77
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
78 case NODE_OPER_DIVIDE:
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
79 generate_code(n -> children, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
80 fprintf(output, "\tpshs d\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
81 generate_code(n->children->next_child, output);
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
82 fprintf(output, "\tjsr ___div16i\n");
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
83 break;
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
84
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
85 case NODE_OPER_MOD:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
86 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
87 fprintf(output, "\tpshs d\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
88 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
89 fprintf(output, "\tjsr ___mod16i\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
90 break;
501
f3e9732973f1 Add basic integer operations to lwcc
William Astle <lost@l-w.ca>
parents: 499
diff changeset
91
502
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
92 case NODE_OPER_COND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
93 label1 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
94 label2 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
95 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
96 fprintf(output, "\tsubd #0\n\tbeq %s\n", label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
97 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
98 fprintf(output, "\tbra %s\n%s\n", label2, label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
99 generate_code(n -> children -> next_child -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
100 fprintf(output, "%s\n", label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
101 lw_free(label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
102 lw_free(label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
103 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
104
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
105 case NODE_OPER_COMMA:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
106 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
107 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
108 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
109
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
110 case NODE_OPER_BWAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
111 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
112 fprintf(output, "\tpshs d\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
113 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
114 fprintf(output, "\tandb 1,s\n\tanda ,s++\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
115 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
116
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
117 case NODE_OPER_BWOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
118 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
119 fprintf(output, "\tpshs d\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
120 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
121 fprintf(output, "\torb 1,s\n\tora ,s++\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
122 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
123
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
124 case NODE_OPER_BWXOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
125 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
126 fprintf(output, "\tpshs d\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
127 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
128 fprintf(output, "\teorb 1,s\n\teora ,s++\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
129 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
130
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
131 case NODE_OPER_BAND:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
132 label1 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
133 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
134 fprintf(output, "\tsubd #0\n\tbeq %s\n", label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
135 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
136 fprintf(output, "\tsubd #0\n\tbeq %s\n\tldd #1\n%s\n", label1, label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
137 lw_free(label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
138 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
139
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
140 case NODE_OPER_BOR:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
141 label1 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
142 label2 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
143 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
144 fprintf(output, "\tsubd #0\n\tbne %s\n", label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
145 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
146 fprintf(output, "\tsubd #0\n\tbeq %s\n%s\tldd #1\n%s\n", label2, label1, label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
147 lw_free(label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
148 lw_free(label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
149 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
150
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
151 case NODE_OPER_NE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
152 case NODE_OPER_EQ:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
153 case NODE_OPER_LT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
154 case NODE_OPER_GT:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
155 case NODE_OPER_LE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
156 case NODE_OPER_GE:
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
157 generate_code(n -> children, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
158 fprintf(output, "\tpshs d\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
159 generate_code(n -> children -> next_child, output);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
160 fprintf(output, "\tsubd ,s++\n");
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
161 label1 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
162 label2 = generate_nextlabel();
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
163 fprintf(output, "\t%s %s\n", (
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
164 (n -> type == NODE_OPER_NE ? "bne" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
165 (n -> type == NODE_OPER_EQ ? "beq" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
166 (n -> type == NODE_OPER_LT ? "bge" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
167 (n -> type == NODE_OPER_GT ? "ble" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
168 (n -> type == NODE_OPER_LE ? "bgt" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
169 (n -> type == NODE_OPER_GE ? "blt" :
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
170 "foobar"))))))
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
171 ), label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
172 fprintf(output, "\tldd #0\n\tbra %s\n%s\tldd #1\n%s\n", label2, label1, label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
173 lw_free(label1);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
174 lw_free(label2);
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
175 break;
14a40f8bb4eb Add various operators to lwcc
William Astle <lost@l-w.ca>
parents: 501
diff changeset
176
499
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
177 default:
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
178 for (nn = n -> children; nn; nn = nn -> next_child)
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
179 generate_code(nn, output);
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
180 break;
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
181 }
c3099c5d9d3e Add very simple code generator
William Astle <lost@l-w.ca>
parents:
diff changeset
182 }