annotate lwasm/struct.c @ 254:c7a41b4c89b3 2.x

Added struct support to LWASM
author lost
date Sat, 19 Dec 2009 06:38:43 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
254
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
1 /*
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
2 struct.c
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
3 Copyright © 2009 William Astle
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
4
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
5 This file is part of LWASM.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
6
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
10 version.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
11
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
15 more details.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
16
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
19
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
20 Contains stuff associated with structure definition.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
21
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
22 Structures have their own name space. Macros will shadow structures so it's
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
23 a good idea to make sure no structures are name the same as macros.
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
24 */
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
25
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
26 #include <config.h>
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
27
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
28 #include <ctype.h>
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
29 #include <stdlib.h>
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
30 #include <string.h>
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
31 #include "lwasm.h"
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
32 #include "instab.h"
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
33 #include "util.h"
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
34
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
35 OPFUNC(pseudo_struct)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
36 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
37 structtab_t *m;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
38
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
39 // actually define a structure
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
40 if (as -> instruct)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
41 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
42 register_error(as, l, 1, "Attempt to define a structure inside a structure");
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
43 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
44 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
45
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
46 as -> instruct = 1;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
47
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
48 // don't actually do anything if not pass 1
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
49 if (as -> passnum != 1)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
50 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
51
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
52
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
53 if (!l -> sym)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
54 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
55 register_error(as, l, 1, "Structure definition with no effect - no symbol");
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
56 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
57 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
58
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
59 // search for macro by same name...
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
60 for (m = as -> structs; m; m = m -> next)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
61 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
62 if (!strcmp(m -> name, l -> sym))
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
63 break;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
64 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
65 if (m)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
66 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
67 register_error(as, l, 1, "Duplicate structure definition");
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
68 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
69 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
70
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
71 m = lwasm_alloc(sizeof(structtab_t));
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
72 m -> name = lwasm_strdup(l -> sym);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
73 m -> next = as -> structs;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
74 m -> fields = NULL;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
75 m -> size = 0;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
76 as -> structs = m;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
77 as -> cstruct = m;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
78
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
79 while (**p && !isspace(**p))
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
80 (*p)++;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
81 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
82
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
83
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
84
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
85 pseudo_endstruct_aux(asmstate_t *as, lwasm_line_t *l, struct struct_sym_e *e, const char *prefix, int *coff)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
86 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
87 char *symname = NULL;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
88
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
89 while (e)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
90 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
91 if (e -> name)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
92 0 == asprintf(&symname, "%s.%s", prefix, e -> name);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
93 else
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
94 0 == asprintf(&symname, "%s.___%d", prefix, *coff);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
95
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
96 // register symbol "symname" as global at coff
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
97 lwasm_register_symbol(as, l, symname, *coff, SYMBOL_NOCHECK);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
98
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
99 if (e -> substruct)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
100 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
101 char * t;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
102 0 == asprintf(&t, "sizeof{%s}", symname);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
103 lwasm_register_symbol(as, l, t, e -> substruct -> size, SYMBOL_NOCHECK);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
104 lwasm_free(t);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
105 pseudo_endstruct_aux(as, l, e -> substruct -> fields, symname, coff);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
106 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
107 else
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
108 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
109 *coff += e -> size;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
110 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
111
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
112 lwasm_free(symname);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
113 e = e -> next;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
114 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
115 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
116
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
117 OPFUNC(pseudo_endstruct)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
118 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
119 int coff = 0;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
120 char *t;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
121
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
122 as -> instruct = 0;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
123
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
124 if (as -> passnum != 1)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
125 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
126
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
127 // now register the global symbols for the structure
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
128 // this is a recursive process
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
129 0 == asprintf(&t, "sizeof{%s}", as -> cstruct -> name);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
130 lwasm_register_symbol(as, l, t, as -> cstruct -> size, SYMBOL_NOCHECK);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
131 lwasm_free(t);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
132
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
133 pseudo_endstruct_aux(as, l, as -> cstruct -> fields, as -> cstruct -> name, &coff);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
134 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
135
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
136
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
137 int expand_struct(asmstate_t *as, lwasm_line_t *l, char **p, char *opc)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
138 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
139 structtab_t *s;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
140 int addr;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
141 char *t;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
142
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
143 if (as -> passnum != 1)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
144 return 0;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
145
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
146 for (s = as -> structs; s; s = s -> next)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
147 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
148 if (!strcmp(opc, s -> name))
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
149 break;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
150 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
151 if (!s)
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
152 return -1;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
153
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
154 if (!(l -> sym))
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
155 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
156 register_error(as, l, 1, "Cannot declare a structure without a symbol name.");
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
157 return;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
158 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
159
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
160 l -> nocodelen = s -> size;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
161
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
162 addr = as -> addr;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
163 as -> addr += s -> size;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
164
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
165 // now register the global symbols for the structure
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
166 // this is a recursive process
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
167 0 == asprintf(&t, "sizeof{%s}", l -> sym);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
168 lwasm_register_symbol(as, l, t, s -> size, SYMBOL_NOCHECK);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
169 lwasm_free(t);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
170
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
171 pseudo_endstruct_aux(as, l, s -> fields, l -> sym, &addr);
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
172
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
173 return 0;
c7a41b4c89b3 Added struct support to LWASM
lost
parents:
diff changeset
174 }