annotate lwasm/parse.c @ 262:6d09310438a4 2.x

Fixed problem with local symbol context handling in the face of conditional assembly
author lost
date Thu, 07 Jan 2010 03:57:58 +0000
parents 6363b9ebf825
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
1 /*
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
2 parse.c
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
3 Copyright © 2008 William Astle
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
4
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
5 This file is part of LWASM.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
6
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
7 LWASM is free software: you can redistribute it and/or modify it under the
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
10 version.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
11
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
15 more details.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
16
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
19 */
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
20
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
21 /*
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
22 Contains the general parser
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
23 */
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
24
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
25 #define __parse_c_seen__
212
bae1e3ecdce1 More preparation for gnulib integration
lost
parents: 190
diff changeset
26 #include <config.h>
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
27
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
28 #include <ctype.h>
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
29 #include <string.h>
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
30
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
31 #include "lwasm.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
32 #include "instab.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
33 #include "util.h"
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
34
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
35 // parse a line and despatch to the appropriate handlers for opcodes
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
36 int lwasm_parse_line(asmstate_t *as, lwasm_line_t *l)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
37 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
38 char *p, *p2;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
39 char *opc;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
40 int opnum;
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
41 char *sym = NULL;
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
42
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
43 // if this was a bad op first pass (or otherwise a no-process line)
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
44 // ignore it
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
45 if (l -> badop)
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
46 return;
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
47
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
48 p = l -> text;
101
f59c0916753d Fixed relative branches and PCR addressing to handle constant intra-section references properly
lost
parents: 99
diff changeset
49 l -> sect = as -> csect;
236
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 230
diff changeset
50 l -> inmod = as -> inmod;
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
51
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
52 // blank lines are a no brainer
262
6d09310438a4 Fixed problem with local symbol context handling in the face of conditional assembly
lost
parents: 255
diff changeset
53 if (as -> passnum == 2)
6d09310438a4 Fixed problem with local symbol context handling in the face of conditional assembly
lost
parents: 255
diff changeset
54 as -> context = l -> context;
6d09310438a4 Fixed problem with local symbol context handling in the face of conditional assembly
lost
parents: 255
diff changeset
55 else if (!*p)
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
56 {
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
57 as -> context = lwasm_next_context(as);
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
58 return 0;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
59 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
60
52
b9856da2674a Added file inclusion
lost
parents: 44
diff changeset
61 // for output generation later but only on pass 1
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
62 // also used by some pseudo ops on pass 2
52
b9856da2674a Added file inclusion
lost
parents: 44
diff changeset
63 if (as -> passnum == 1)
b9856da2674a Added file inclusion
lost
parents: 44
diff changeset
64 l -> codeaddr = as -> addr;
255
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
65
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
66 // check if there is a line number and ignore it if there is
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
67 if (isdigit(*p))
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
68 {
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
69 for (p2 = p; *p2 && isdigit(*p2); p2++)
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
70 /* do nothing */ ;
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
71
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
72 // we have a line number, skip it and the *first*
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
73 // whitespace character
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
74 if (!*p2 || isspace(*p2))
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
75 p = p2 + 1;
6363b9ebf825 Support files with line numbers in lwasm
lost
parents: 254
diff changeset
76 }
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
77
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
78 // if it's a comment, return (this doesn't cause a context change)
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
79 if (*p == '*' || *p == ';')
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
80 return;
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
81
169
99300be2d3bd Allow lines starting with # to be ignored
lost
parents: 157
diff changeset
82 // if we have C pre-processor directives/output, ignore it
99300be2d3bd Allow lines starting with # to be ignored
lost
parents: 157
diff changeset
83 if (*p == '#')
99300be2d3bd Allow lines starting with # to be ignored
lost
parents: 157
diff changeset
84 return;
99300be2d3bd Allow lines starting with # to be ignored
lost
parents: 157
diff changeset
85
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
86 // if we start with a non-space character, it's a symbol
244
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
87 symbolagain:
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
88 if (!isspace(*p))
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
89 {
253
c537a3a723fc Fixed obscure comment detection bug
lost
parents: 250
diff changeset
90 // if it starts with a comment character, it's not a symbol
c537a3a723fc Fixed obscure comment detection bug
lost
parents: 250
diff changeset
91 if (*p == ';' || *p == '*')
c537a3a723fc Fixed obscure comment detection bug
lost
parents: 250
diff changeset
92 return;
c537a3a723fc Fixed obscure comment detection bug
lost
parents: 250
diff changeset
93
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
94 // we have a symbol specified here
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
95 // parse it out and record it for later use
157
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
96 for (p2 = p; *p2 && !isspace(*p2) && *p2 != ':'; p2++)
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
97 /* do nothing */ ;
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
98
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
99 sym = lwasm_alloc((p2 - p) + 1);
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
100 sym[p2 - p] = '\0';
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
101 memcpy(sym, p, p2 - p);
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
102
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
103 p = p2;
157
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
104 if (!*sym)
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
105 {
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
106 register_error(as, l, 1, "Invalid symbol");
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
107 lwasm_free(sym);
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
108 sym = NULL;
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
109 }
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
110 if (*p == ':')
745721e13970 allow : after symbol at line start
lost
parents: 151
diff changeset
111 p++;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
112 }
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
113 l -> sym = sym;
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
114
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
115 // now skip any whitespace to find the opcode
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
116 while (*p && isspace(*p))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
117 p++;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
118
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
119 // is the line blank?
262
6d09310438a4 Fixed problem with local symbol context handling in the face of conditional assembly
lost
parents: 255
diff changeset
120 if (!*p && !sym && as -> passnum == 1)
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
121 {
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
122 // nothing but white space *is* a context break
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
123 as -> context = lwasm_next_context(as);
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
124 return;
37
538e15927776 Added symbol handling to expression subsystem; adpated instruction handlers to the new scheme; misc fixes
lost
parents: 36
diff changeset
125 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
126
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
127 // parse the opcode
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
128 for (p2 = p; *p2 && !isspace(*p2); p2++)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
129 /* do nothing */ ;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
130
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
131 opc = lwasm_alloc((p2 - p) + 1);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
132 memcpy(opc, p, p2 - p);
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
133 opc[p2 - p] = '\0';
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
134
244
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
135 if (strchr(opc, ':'))
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
136 {
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
137 lwasm_free(opc);
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
138 goto symbolagain;
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
139 }
c8bcc396ec59 Allow symbols to appear after whitespace when terminated by :
lost
parents: 236
diff changeset
140
190
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
141 l -> forceglobal = 0;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
142 // if the opcode contains an =, treat it as "symbol = expr"
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
143 if (!sym && strchr(opc, '='))
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
144 {
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
145 for (p2 = opc; *p2 && *p2 != '='; p2++)
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
146 /* do nothing */ ;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
147 sym = lwasm_alloc((p2 - opc) + 1);
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
148 memcpy(sym, opc, p2 - opc);
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
149 sym[p2 - opc] = '\0';
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
150 l -> sym = sym;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
151
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
152 p2 = p + (p2 - opc) + 1;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
153 // p2++;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
154 opc[0] = '=';
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
155 opc[1] = '\0';
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
156 debug_message(2, "Found opcode = with symbol %s and operand %s", sym, p2);
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
157 l -> forceglobal = 1;
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
158 }
563adfccb645 Added 'sym=expr' opcode handling
lost
parents: 169
diff changeset
159
38
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
160 debug_message(2, "Found operation code: '%s'", opc);
9bd584bb6296 Added debugging message infrastructure
lost
parents: 37
diff changeset
161
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
162 // skip intervening whitespace if present
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
163 while (*p2 && isspace(*p2))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
164 p2++;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
165
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
166 // look up instruction in insn table
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
167 for (opnum = 0; instab[opnum].opcode; opnum++)
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
168 {
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
169 if (!strcasecmp(instab[opnum].opcode, opc))
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
170 break;
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
171 }
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
172
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
173 // if we found no operation, check if we had a comment
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
174 // the reason this check is here is to allow for "private"
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
175 // operation codes like "*pragma" which will be ignored by
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
176 // other assemblers
68
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
177 // also skip empty ops
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
178 if (!(instab[opnum].opcode))
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
179 {
68
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
180 if (*opc == '*' || *opc == ';' || !*opc)
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
181 goto done_line;
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
182 }
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
183
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
184 // now we have the opcode and the symbol, we can decide if we're
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
185 // actually going to do anything with this line
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
186
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
187 // we will NOT call the function if any of the following are true:
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
188
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
189 // - we are skipping a condition and the operation code is not a conditional
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
190 // - we are defining a macro and the operation code is not ENDM
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
191
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
192 // we will call the function in any other circumstance
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
193
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
194 // first condition above
57
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
195 if (as -> inmacro && instab[opnum].endm == 0)
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
196 {
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
197 add_macro_line(as, l -> text);
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
198 goto done_line;
57
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
199 }
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
200
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
201 // second condition above
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
202 if (as -> skipcond && instab[opnum].iscond == 0)
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
203 goto done_line;
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
204
254
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
205 if (as -> instruct && as -> passnum == 1)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
206 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
207 structtab_t *s = NULL;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
208 struct struct_sym_e *e, *e2;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
209
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
210 // look for a structure if not an acceptable opcode
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
211 if (!instab[opnum].opcode)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
212 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
213 for (s = as -> structs; s; s = s -> next)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
214 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
215 if (s == as -> cstruct)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
216 continue;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
217 if (!strcmp(opc, s -> name))
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
218 goto isok;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
219 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
220 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
221 // check for a "reservation opcode"
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
222 if (instab[opnum].opcode && instab[opnum].isreserve)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
223 {
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
224 (instab[opnum].fn)(as, l, &p2, opnum);
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
225
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
226 // if we didn't end on a "space" character or EOL, throw error
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
227 if (p2 && *p2 && !isspace(*p2) && !(l -> err) && as -> passnum == 1)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
228 register_error(as, l, 1, "Bad operand: %s (%d)", p2, as -> passnum);
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
229
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
230 if (as -> instruct == 0)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
231 goto done_line;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
232
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
233 goto isok;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
234 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
235 // carp if not valid
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
236 register_error(as, l, 1, "Only other structs and rmb allowed in struct");
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
237 l -> badop = 1;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
238 goto done_line;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
239
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
240 isok:
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
241 for (e2 = as -> cstruct -> fields; e2 && e2 -> next; e2 = e2 -> next)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
242 /* do nothing */ ;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
243 e = lwasm_alloc(sizeof(struct struct_sym_e));
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
244 e -> next = NULL;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
245 e -> substruct = s;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
246 if (sym)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
247 e -> name = lwasm_strdup(sym);
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
248 else
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
249 e -> name = NULL;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
250 if (s)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
251 e -> size = s -> size;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
252 else
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
253 e -> size = l -> nocodelen;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
254 if (e2)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
255 e2 -> next = e;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
256 else
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
257 as -> cstruct -> fields = e;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
258 as -> cstruct -> size += e -> size;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
259 }
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
260
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
261 // we've registered the symbol as needed
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
262 // now we need to check for a macro call IFF we don't collide with
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
263 // an operation code; otherwise, call the operation function
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
264 if (instab[opnum].opcode)
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
265 {
230
0df2a39a268c Added --6809/--6309 switches and some cleanups in error reporting
lost
parents: 223
diff changeset
266 if (instab[opnum].fn && !(as -> no6309 && instab[opnum].is6309))
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
267 {
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
268 (instab[opnum].fn)(as, l, &p2, opnum);
223
436b36214e35 Fixed lack of error when there are extraneous characters at the end of the operand.
lost
parents: 212
diff changeset
269
436b36214e35 Fixed lack of error when there are extraneous characters at the end of the operand.
lost
parents: 212
diff changeset
270 // if we didn't end on a "space" character or EOL, throw error
250
0986834ec7d3 Added no-op .bank pseudo-op to support compiling gcc6809
lost
parents: 244
diff changeset
271 if (p2 && *p2 && !isspace(*p2) && !(l -> err) && as -> passnum == 1)
236
a58f49a77441 Added os9 target, pragma to control whether $ localizes a symbol, and fixed some condition nesting bugs
lost
parents: 230
diff changeset
272 register_error(as, l, 1, "Bad operand: %s (%d)", p2, as -> passnum);
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
273 }
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
274 else
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
275 {
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
276 // carp about unimplemented operation
230
0df2a39a268c Added --6809/--6309 switches and some cleanups in error reporting
lost
parents: 223
diff changeset
277 if (instab[opnum].is6309)
0df2a39a268c Added --6809/--6309 switches and some cleanups in error reporting
lost
parents: 223
diff changeset
278 register_error(as, l, 1, "Use of 6309 operation code: %s", opc);
0df2a39a268c Added --6809/--6309 switches and some cleanups in error reporting
lost
parents: 223
diff changeset
279 else
0df2a39a268c Added --6809/--6309 switches and some cleanups in error reporting
lost
parents: 223
diff changeset
280 register_error(as, l, 1, "Unimplemented operation code: %s", opc);
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
281 }
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
282 }
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
283 else
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
284 {
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
285 if (expand_macro(as, l, &p2, opc) == 0)
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
286 goto done_line;
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
287
254
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
288 if (expand_struct(as, l, &p2, opc) == 0)
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
289 goto done_line;
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
290
66
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
291 // carp about an unknown operation code and note that fact for
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
292 // pass 2 in case a macro appears later with the same name!
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
293 register_error(as, l, 1, "Uknown operation code: %s", opc);
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
294 l -> badop = 1;
aa9d9fedfdf4 Redid lwasm_parse_line() to correct overly complex logic flaws
lost
parents: 64
diff changeset
295 }
57
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
296
035b95a3690f Added conditional assembly and macros
lost
parents: 52
diff changeset
297 done_line:
68
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
298 if (!(as -> skipcond || as -> inmacro))
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
299 {
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
300 // register symbol if the operation didn't
254
c7a41b4c89b3 Added struct support to LWASM
lost
parents: 253
diff changeset
301 if (sym && instab[opnum].setsym == 0 && as -> instruct == 0)
68
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
302 {
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
303 if (as -> passnum == 1)
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
304 {
71
90a5657d5408 Fixed problem with symbols being registered with the address of the NEXT instruction
lost
parents: 68
diff changeset
305 debug_message(1, "Registering symbol '%s' at %04X", sym, l -> codeaddr);
90a5657d5408 Fixed problem with symbols being registered with the address of the NEXT instruction
lost
parents: 68
diff changeset
306 if (lwasm_register_symbol(as, l, sym, l -> codeaddr, SYMBOL_NORM) < 0)
68
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
307 l -> sym = NULL;
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
308 else
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
309 l -> addrset = 1;
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
310 }
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
311 }
cef25b0088e6 Fixed some problems detected by valgrind and testing
lost
parents: 66
diff changeset
312 }
99
3dcb12a6f4ff Fixed problem handling sections with options on pass 2
lost
parents: 91
diff changeset
313
85
918be0c02239 Started adding object target output
lost
parents: 71
diff changeset
314 l -> sect = as -> csect;
91
718998b673ee Added incomplete references to object output and added support for section base terms in expression handler
lost
parents: 85
diff changeset
315 l -> context = as -> context;
85
918be0c02239 Started adding object target output
lost
parents: 71
diff changeset
316
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
317 lwasm_free(opc);
44
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
318 if (sym)
2330b88f9600 Added simple output listing
lost
parents: 38
diff changeset
319 lwasm_free(sym);
36
99e3b3310bac Added missing parse.c file to repository
lost
parents:
diff changeset
320 }