Mercurial > hg > index.cgi
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 |
rev | line source |
---|---|
499 | 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 | |
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 | 74 default: |
75 for (nn = n -> children; nn; nn = nn -> next_child) | |
76 generate_code(nn, output); | |
77 break; | |
78 } | |
79 } |